如果我用以下参数和默认设置打印出在原始套接字上发送的IP报头的报头字段值,我看到协议号设置为'2‘,这属于IGMP的协议号。
sock = socket(PF_INET, SOCK_RAW, AF_INET); 但是,如果我使用TCP报头访问数据包,除了数据偏移量获取了错误的值之外,大多数字段的值都是正确的。此外,我无法在Linux平台(Ubuntu)上使用IGMP报头正确读取报头。
用于读出报头:
printf("\n");
printf("IP Header\n");
printf(" |-IP Version : %d\n",(unsigned int)iph->version);
printf(" |-IP Header Length : %d DWORDS or %d Bytes\n",(unsigned int)iph->ihl, ((unsigned int)(iph->ihl))*4);
printf(" |-Type Of Service : %d\n",(unsigned int)iph->tos);
printf(" |-IP Total Length : %d Bytes(Size of Packet)\n",ntohs(iph->tot_len));
printf(" |-Identification : %d\n",ntohs(iph->id));
printf(" |-TTL : %d\n",(unsigned int)iph->ttl);
printf(" |-Protocol : %d\n",(unsigned int)iph->protocol);
printf(" |-Checksum : %d\n",ntohs(iph->check));
printf(" |-Source IP : %s\n",inet_ntoa(source.sin_addr));
printf(" |-Destination IP : %s\n",inet_ntoa(dest.sin_addr));
// IGMP Print
printf("\n");
printf("IGMP Header\n");
printf(" |-IGMP Type : %d\n",(unsigned int)head->igmp_type);
printf(" |-IGMP Code : %d\n",(unsigned int)head->igmp_code);
printf(" |-IGMP Checksum : %d\n",(unsigned int)head->igmp_cksum);
printf(" |-IGMP Address : %s\n",inet_ntoa(head->igmp_group.sin_addr);这给出了IGMP数据包的错误:
error: dereferencing pointer to incomplete type对于所有4个字段。
如果我把它当作TCP报头,它工作得很好。
我使用原始套接字在守护进程和作为其管理代理的CLI之间进行通信。
发布于 2012-01-20 12:27:58
我认为您严重未能理解原始套接字的用途。它们是一个工具,可以让你手动组装或检查IP数据包的内容,包括IP数据包内的较高级别的报头,告诉它是否是TCP,UDP,ICMP,IGMP等。它们不是直接使用的通信手段,而是一种在较低级别上绕过现有的较高级别通信方法的手段。
对于您的应用程序,您几乎肯定需要TCP或UDP套接字。
https://stackoverflow.com/questions/8936709
复制相似问题