使用 struct ip 和 struct iphdr 解引用指向不完整类型的指针
你好,我正在尝试解析一些数据包,当使用 struct ip 时,我收到:“取消引用指向不完整类型的指针”错误消息。 所以我尝试使用 struct iphdr 但仍然有同样的问题 这是我用来解析数据包的函数:
PrintPacketInHex(char *mesg, unsigned char *p, int len){
printf(mesg);
while(len--){
printf("%.2X ",*p);
p++;
}
}
/*parse the sniffed packets*/
ParseEthernetHeader(unsigned char *packet, int len){
struct ethhdr *ethernet_header;
struct ip *ip_header;
//+sizeof(struct iphdr)
if(len> sizeof(struct ethhdr)){
ethernet_header = (struct ethhdr*) packet;
packet = packet +16;
ip_header = (struct ip*) packet;
/* first set of 6 bytes are destination MAC*/
PrintPacketInHex("destination MAC : ", ethernet_header->h_dest,6);
printf("\n");
/*scond set of 6 bytes are source MAC */
PrintPacketInHex("source MAC : ", ethernet_header->h_source,6);
printf("\n");
/*last 2 bytes in ethernet header are the protocol in carries */
PrintPacketInHex("protocol: ", (void*)ðernet_header->h_proto,2);
printf("\n");
/*the next 4 bytes are ip version bytes */
PrintPacketInHex("ip version: ", ip_header->ip_v,4);
printf("\n");
/*the next 4 bytes are internet header length */
PrintPacketInHex("internet header length: ", ip_header->ip_ihl,4);
printf("\n");
/*the next 8 bytes are type of srvice bytes */
PrintPacketInHex("type of service: ", ip_header->ip_tos,8);
printf("\n");
/*the next 8 bytes are total length bytes */
PrintPacketInHex("total length: ", ip_header->ip_len,16);
printf("\n");
/*the next 8 bytes are identification bytes */
PrintPacketInHex("identification: ", ip_header->ip_id,16);
printf("\n");
/*the next 8 bytes are flags bytes
PrintPacketInHex("Flags: ", "flags",3);
printf("\n");*/
/*the next 13 bytes flags and offset bytes */
PrintPacketInHex("offset: ", ip_header->ip_off,16);
printf("\n");
/*the next 8 bytes time to live bytes */
PrintPacketInHex("time to live: ", ip_header->ip_ttl,8);
printf("\n");
/*the next 8 bytes protocol bytes */
PrintPacketInHex("protocol: ", ip_header->ip_p,8);
printf("\n");
/*the next 16 bytes are checksum bytes */
PrintPacketInHex("checksum: ", ip_header->ip_sum,16);
printf("\n");
/*the next 32 bytes source ip adress bytes */
PrintPacketInHex("source ip: ", ip_header->ip_src,32);
printf("\n");
/*the next 32 bytes destination ip adress bytes */
PrintPacketInHex("destination ip: ", ip_header->dip_dst,32);
printf("\n");
}
else printf("packet size too small ! \n");
}
我知道程序中还有其他错误,但想在寻找其他问题之前解决这个问题。 谢谢
hello i'm trying to parse some packets, and when using struct ip i get the: "dereferencing pointer to incomplete type" error message.
So i tryed then with struct iphdr but still have the same problem
Here is the function i'm using for parsing the packet:
PrintPacketInHex(char *mesg, unsigned char *p, int len){
printf(mesg);
while(len--){
printf("%.2X ",*p);
p++;
}
}
/*parse the sniffed packets*/
ParseEthernetHeader(unsigned char *packet, int len){
struct ethhdr *ethernet_header;
struct ip *ip_header;
//+sizeof(struct iphdr)
if(len> sizeof(struct ethhdr)){
ethernet_header = (struct ethhdr*) packet;
packet = packet +16;
ip_header = (struct ip*) packet;
/* first set of 6 bytes are destination MAC*/
PrintPacketInHex("destination MAC : ", ethernet_header->h_dest,6);
printf("\n");
/*scond set of 6 bytes are source MAC */
PrintPacketInHex("source MAC : ", ethernet_header->h_source,6);
printf("\n");
/*last 2 bytes in ethernet header are the protocol in carries */
PrintPacketInHex("protocol: ", (void*)ðernet_header->h_proto,2);
printf("\n");
/*the next 4 bytes are ip version bytes */
PrintPacketInHex("ip version: ", ip_header->ip_v,4);
printf("\n");
/*the next 4 bytes are internet header length */
PrintPacketInHex("internet header length: ", ip_header->ip_ihl,4);
printf("\n");
/*the next 8 bytes are type of srvice bytes */
PrintPacketInHex("type of service: ", ip_header->ip_tos,8);
printf("\n");
/*the next 8 bytes are total length bytes */
PrintPacketInHex("total length: ", ip_header->ip_len,16);
printf("\n");
/*the next 8 bytes are identification bytes */
PrintPacketInHex("identification: ", ip_header->ip_id,16);
printf("\n");
/*the next 8 bytes are flags bytes
PrintPacketInHex("Flags: ", "flags",3);
printf("\n");*/
/*the next 13 bytes flags and offset bytes */
PrintPacketInHex("offset: ", ip_header->ip_off,16);
printf("\n");
/*the next 8 bytes time to live bytes */
PrintPacketInHex("time to live: ", ip_header->ip_ttl,8);
printf("\n");
/*the next 8 bytes protocol bytes */
PrintPacketInHex("protocol: ", ip_header->ip_p,8);
printf("\n");
/*the next 16 bytes are checksum bytes */
PrintPacketInHex("checksum: ", ip_header->ip_sum,16);
printf("\n");
/*the next 32 bytes source ip adress bytes */
PrintPacketInHex("source ip: ", ip_header->ip_src,32);
printf("\n");
/*the next 32 bytes destination ip adress bytes */
PrintPacketInHex("destination ip: ", ip_header->dip_dst,32);
printf("\n");
}
else printf("packet size too small ! \n");
}
i know that there are other errors in the program but want to solve this problem before looking for other problems.
thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的编译单元必须包含 struct ethhdr 和 struct ip 的定义,以便您能够访问它们的成员。
Your compilation unit must include the definitions of
struct ethhdr
andstruct ip
for you to be able to access their members.