我在C(Windows 8.1环境)中编写代理。我在Linux中做了同样没有任何麻烦,但在Windows中看起来不一样。 首先,我知道MS Windows发送RAW TCP数据的限制并阅读了这篇文章。我还发现WinPcap是一个潜在的解决方案。不同之处在于我需要添加以太网帧(DstMac,SrcMac和协议类型)并运行以下代码:
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", d->name);
}
然后我创建了一个数据包:
u_char * data;
data = (u_char *)malloc(sizeof(u_char)*(ip_size+14));
memcpy((void*)data, (void*)dmac, 6);
memcpy((void*)(data + 6), (void*)smac, 6);
USHORT TmpType = 8;
memcpy((void*)(data + 12), (void*)&TmpType, 2);
memcpy(data+14, iphdr, ip_size); //iphdr is the full packet -(minus) ethernet header
//ip_size is the sizeof(iphdr)
pcap_sendpacket(fp, data, ip_size+14); //14: size of ethernet header
free(data);
在我的测试中,第一个iphdr是TCP Syn数据包。当我将它发送到网络时,我可以在Wireshark中捕获它。但是,远程服务器不响应此数据包,我得到[TCP Out-of-Ordfer]消息,然后是一些RST数据包,没有任何反应。 我的问题是: 1- winpcap是Windows中唯一可行的解决方案吗? 2-如果不是,您能否提供在C中开发RAW TCP套接字的任何示例/资源? 3-如果是,导致服务器无法响应我手动制作的TCP Syn的原因。
PS:我使用相同的代码在linux中创建TCP SYN,它运行良好。所以,我毫不怀疑tcphdr包含IP头,TCP头和有效负载,在IP和TCP头中都有正确的校验和。