用winpcap发送syn数据包,对方收不到

zhangqi_gsts 发布于 2014/05/09 23:28
阅读 967
收藏 0

在本机上,用winpcap发送syn数据包,目的机器还是本机,但ip不是127.0.0.1,为什么我自己收不到呢???在两台主机间也收不到,但虚拟机却可以,这是为什么呢???? 

#include <stdlib.h>
#include <stdio.h>
#include <winsock2.h>
#include <pcap.h>
#pragma comment(lib, "wpcap.lib")
#pragma comment(lib, "ws2_32.lib")


typedef struct   _ETHER_HEADER{ 
u_char   ether_dhost[6]; 
u_char   ether_shost[6]; 
u_short   ether_type;  //如果上一层为IP协议。则ether_type的值就是0x0800
} ETHER_HEADER;


//IP报头格式
typedef struct _IP_HEADER
{
unsigned char   h_lenver;
unsigned char   tos;
unsigned short  total_len;
unsigned short  ident;
unsigned short  frag_and_flags;
unsigned char   ttl;
unsigned char   proto;
unsigned short  checksum;
unsigned int    sourceIP;
unsigned int    destIP;
} IP_HEADER;


//TCP报头格式
typedef struct _TCP_HEADER 
{
USHORT th_sport;
USHORT th_dport;
unsigned int th_seq;
unsigned int th_ack;
unsigned char th_lenres;
unsigned char th_flag;
USHORT th_win;
USHORT th_sum;
USHORT th_urp;
} TCP_HEADER;


//发送数据格式
typedef struct _SYN_DATA
{
IP_HEADER iphrd;
TCP_HEADER tcphrd;
}SYN_DATA;


//校验和算法
USHORT checksum(USHORT *buffer, int size)
{
unsigned long cksum = 0;


while (size > 1) 
{
cksum += *buffer++;
size -= sizeof(USHORT);
}


if (size) 
{
cksum += *(UCHAR*)buffer;
}
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >>16);


return (USHORT)(~cksum);
}


//构造IP头
int mkiphrd(IP_HEADER *ip, unsigned long sip, unsigned long dip)
{
ip->h_lenver = (4<<4|5);
ip->tos = 0;
ip->total_len = sizeof(SYN_DATA);
ip->ident = 0;
ip->frag_and_flags = 0;
ip->ttl = 64;
ip->proto = 6;
ip->checksum = 0;
ip->destIP = dip;
ip->sourceIP = sip;
ip->checksum = checksum((USHORT *)&ip, sizeof(IP_HEADER));
return 1;
}


//构造TCP头
int mktcphrd(SYN_DATA *syndata, unsigned long sip, unsigned long dip, int sport, int dport)
{
syndata->tcphrd.th_sport = sport;
syndata->tcphrd.th_dport = dport;
syndata->tcphrd.th_seq = 1001;
syndata->tcphrd.th_ack = 0;
syndata->tcphrd.th_lenres = 5<<4;
syndata->tcphrd.th_flag = 2;
syndata->tcphrd.th_win = htons(16384);
syndata->tcphrd.th_sum = 0;
syndata->tcphrd.th_urp = 0;


//构造伪报头---关键,以此生成TCP校验和
syndata->iphrd.ttl = 0;
syndata->iphrd.proto =6;
syndata->iphrd.sourceIP = sip;
syndata->iphrd.destIP = dip;
syndata->iphrd.checksum = htons(sizeof(TCP_HEADER));


syndata->tcphrd.th_sum = checksum((USHORT *)(&(syndata->iphrd.ttl)), sizeof(TCP_HEADER)+12);
return 1;
}


int main(int argc, char **argv)
{
pcap_t *fp;


char errbuf[1024] = {0};
u_char packet[1024];


memset( &packet, 0, sizeof packet );


SYN_DATA sendbuf;    //将要发送的数据包(TCP\IP头)
ETHER_HEADER ethbuf;


int sendsize=0;
int iSendNum ;
char data[20] = "Just a Test!";


pcap_if_t *alldevs;
pcap_if_t *d;


char *destip = "10.66.116.175";//"60.176.42.98";
int destport = 1989;


char *sourceip = "10.66.116.175";//"60.176.42.98";
int sourceport = 1988;


//构造发送数据
memset((void *)&sendbuf, 0, sizeof sendbuf);
mktcphrd( &sendbuf, inet_addr(sourceip), inet_addr(destip), htons(sourceport), htons(destport));

mkiphrd( &(sendbuf.iphrd), inet_addr(sourceip), inet_addr(destip));


sendbuf.iphrd.checksum = checksum((USHORT *)&sendbuf, sizeof(IP_HEADER)+sizeof(TCP_HEADER));


/* Retrieve the device list */
if(pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
exit(1);
}


/* Scan the list printing every entry */
//每个网卡都发送
for(d=alldevs;d;d=d->next)
{
/* Open the adapter */
if ((fp = pcap_open_live(d->name, // name of the device
65536, // portion of the packet to capture. It doesn't matter in this case 
1, // promiscuous mode (nonzero means promiscuous)
1000, // read timeout
errbuf // error buffer
)) == NULL)
{
fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", sourceip);
break;
}


//目的主机Mac地址,是我本机MAC
ethbuf.ether_dhost[0] = 0x64;
ethbuf.ether_dhost[1] = 0x27;
ethbuf.ether_dhost[2] = 0x37;
ethbuf.ether_dhost[3] = 0x7D;
ethbuf.ether_dhost[4] = 0xF7;
ethbuf.ether_dhost[5] = 0x72;


//源主机Mac,也是我本机mac
ethbuf.ether_shost[0] = 0x64;
ethbuf.ether_shost[1] = 0x27;
ethbuf.ether_shost[2] = 0x37;
ethbuf.ether_shost[3] = 0x7D;
ethbuf.ether_shost[4] = 0xF7;
ethbuf.ether_shost[5] = 0x72;


ethbuf.ether_type = htons(0x0800); 


memcpy(packet, &ethbuf, sizeof(ethbuf)) ; 
sendsize += sizeof(ethbuf) ; 
memcpy(packet+sendsize, &sendbuf, sizeof(sendbuf)) ; 
sendsize += sizeof(sendbuf) ; 
memcpy(packet+sendsize, data, sizeof(data)) ; 


/* Send down the packet */


int nSendByte = sendsize + strlen( data );
printf("%d\n", nSendByte );


for(iSendNum = 0; iSendNum <1; ++iSendNum)
{
if (pcap_sendpacket(fp, // Adapter
packet, // buffer with the packet
100 // size
) != 0)
{


pcap_close(fp);
break;
}
}
printf("send sucess!\n");
pcap_close(fp);
}
system("pause");
return 0;
}


加载中
返回顶部
顶部