- 論壇徽章:
- 0
|
以下是代碼部分,執(zhí)行沒問題,就是用tcpdump -i any -nn dst port 6666 命令無法抓取到數(shù)據(jù),請幫忙解決?
int socketfd = socket(AF_INET,SOCK_RAW,IPPROTO_RAW);
bool flag = true;
setsockopt(socketfd,IPPROTO_IP,IP_HDRINCL,(char*)&flag,sizeof(flag));
struct tcp_hdr tcpheader;
struct ip_hdr ipheader;
struct eth_hdr etheader;
//填充IP首部
ipheader.ip_v = 4;
ipheader.ip_hl = (4 << 4 | sizeof(struct ip_hdr)/sizeof(unsigned long));
ipheader.ip_tos = 0;
ipheader.ip_len = htons(sizeof(tcpheader)+sizeof(ipheader)+istrlen );
ipheader.ip_id = 0;
ipheader.ip_off = 0 ;
ipheader.ip_ttl = 128;
ipheader.ip_p = IPPROTO_TCP;
ipheader.ip_sum = 0;
ipheader.ip_src = inet_addr(szSrcIP);
ipheader.ip_dst = inet_addr(szDesIp);
unsigned short IpBuf[128] = {0};
memcpy( IpBuf,&ipheader,sizeof(ipheader) );
ipheader.ip_sum = in_cksum( IpBuf,sizeof(ipheader) );
//填充TCP首部
tcpheader.th_sport = htons(uSrcPort);
tcpheader.th_dport = htons(uDesPort);
tcpheader.th_seq = 0;
tcpheader.th_ack = 0;
tcpheader.th_off = ( sizeof(tcpheader)/sizeof(unsigned long) << 4 | 0 );
tcpheader.th_x2 = 0;
tcpheader.th_flags = 2;//實(shí)現(xiàn)不同的標(biāo)志位探測,2是syn 1是fin 16是ack
tcpheader.th_win = htons((unsigned short)12345);
tcpheader.th_sum = 0;
tcpheader.th_urp = 0;
unsigned short TcpBuf[128] = {0};
memcpy( TcpBuf, &tcpheader, sizeof(tcpheader) );
tcpheader.th_sum = ip_in_cksum(&ipheader,TcpBuf,sizeof(tcpheader)/*+sizeof(szMsg)*/ );
//填充鏈路層包
unsigned char cSrcMac[] = {0x00,0x0C,0x29,0x3C,0X66,0XA1};
unsigned char cDesMac[] = {0X48,0X5b,0X39,0X17,0X3c,0Xac};
memcpy(ðeader.eth_dst,cDesMac,sizeof(cDesMac));
memcpy(ðeader.eth_src,cSrcMac,sizeof(cSrcMac));
etheader.eth_type= htons(0X0800);
char send_buf[128]={0};
memcpy( send_buf,ðeader,sizeof(etheader) );
memcpy( send_buf + sizeof(etheader), &ipheader,sizeof(ipheader) );
memcpy( send_buf + sizeof(etheader) + sizeof(ipheader), &tcpheader, sizeof(tcpheader) );
// memcpy( send_buf + sizeof(etheader) + sizeof(ipheader) + sizeof(tcpheader),szMsg,sizeof(szMsg) );
int datasize = sizeof(etheader) + sizeof(ipheader)+sizeof(tcpheader)/*+sizeof(szMsg)*/;
struct sockaddr_in srAddr = {0};
srAddr.sin_family = AF_INET;
srAddr.sin_addr.s_addr = inet_addr(szDesIp);
srAddr.sin_port = htons(uDesPort);
int rect = sendto(socketfd,send_buf,datasize,0,(struct sockaddr*)&srAddr,sizeof(srAddr)); |
|