UDP校验方式:
- 在对UDP校验的时候需要在UDP报文之间加上伪头部。
- IP校验的时候不需要伪头部
![伪头部中的源ip、目的IP必须和 IP报文中源ip、目的IP 一致。](https://img-blog.csdnimg.cn/5e8e19d683484d5baf6d5618c9f25f0d.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5LuO5q2k5byA5aeL5L2O6LCD6IyD4pyM77iP,size_16,color_FFFFFF,t_70,g_se,x_16#pic_center)
注意:伪头部中的源ip、目的IP必须和 IP报文中源ip、目的IP 一致。
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <cstring>
#include <sys/ioctl.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <netpacket/packet.h>
#include <unistd.h>
#include <pthread.h>
#include <iostream>
using namespace std;
typedef struct{
u_int32_t saddr;
u_int32_t daddr;
u_int8_t flag;
u_int8_t protocol;
u_int16_t len;
}FAKEHDR;
unsigned short checksum(unsigned short *buf, int len)
{
int nword = len/2;
unsigned long sum;
if(len%2 == 1)
nword++;
for(sum = 0; nword > 0; nword--)
{
sum += *buf;
buf++;
}
sum = (sum>>16) + (sum&0xffff);
sum += (sum>>16);
return ~sum;
}
int main(int argc, char *argv[])
{
int sockfd = socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ALL));
if(sockfd < 0)
{
perror("socket error");
return -1;
}
struct ifreq ethreq;
strncpy(ethreq.ifr_ifrn.ifrn_name,"ens33",IFNAMSIZ);
if(ioctl(sockfd,SIOCGIFINDEX,ðreq) == -1)
{
perror("socket error");
close(sockfd);
return -2;
}
cout<< "请输入要发送的内容:" << endl;
char data[128] = "";
fgets(data,sizeof(data),stdin);
data[strlen(data) - 1 ] = 0;
int dataLen = strlen(data);
if(dataLen %2 != 0)
dataLen++;
struct sockaddr_ll sll;
bzero(&sll,sizeof(sll));
sll.sll_ifindex = ethreq.ifr_ifindex;
unsigned char dstMac[6] = {0x00,0xFF,0xB4,0x31,0xD1,0xE2};
unsigned char srcMac[6] = {0x00,0x0c,29,0xee,0x18,0x53};
unsigned char dstIp[4] = {192,168,123,249};
unsigned char srcIp[4] = {192,168,243,128};
unsigned char msg[1024] = "";
struct ether_header *macHeader = (struct ether_header*)msg;
memcpy(macHeader->ether_dhost,dstMac,6);
memcpy(macHeader->ether_shost,srcMac,6);
macHeader->ether_type = htons(0x0800);
struct iphdr *ipHeader = (struct iphdr*)(msg+14);
ipHeader->version = 4;
ipHeader->ihl = 5;
ipHeader->tos = 0;
ipHeader->tot_len = htons(20+8+dataLen);
ipHeader->id = htons(0);
ipHeader->frag_off = htons(0);
ipHeader->ttl = 128;
ipHeader->protocol = 17;
memcpy(&ipHeader->daddr,dstIp,4);
memcpy(&ipHeader->saddr,srcIp,4);
ipHeader->check = checksum((unsigned short *)ipHeader,20);
struct udphdr *udpHeader = (struct udphdr*)(msg+14+20);
udpHeader->source = htons(8000);
udpHeader->dest = htons(9000);
udpHeader->len = htons(8+dataLen);
unsigned char fakeHdr[256] = "";
FAKEHDR* fakeHeader = (FAKEHDR*)fakeHdr;
memcpy(&fakeHeader->saddr,srcIp,4);
memcpy(&fakeHeader->daddr,dstIp,4);
fakeHeader->protocol = 17;
fakeHeader->flag = 0;
fakeHeader->len = htons(8+dataLen);
memcpy(fakeHdr+12,udpHeader,8+dataLen);
udpHeader->check = checksum((unsigned short *)fakeHdr,12+8+dataLen);
memcpy(msg+14+20+8,data,dataLen);
sendto(sockfd,msg,14+20+8+dataLen,0,(struct sockaddr*)&sll,sizeof(sll));
close(sockfd);
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)