Pcap Incomplete Type

て烟熏妆下的殇ゞ 提交于 2019-12-12 03:11:48

问题


I am attempting to find the MAC address using pcap for a small project. As of right now the structure I am working with looks like this:

    struct ethernet_header
    {
         u_char dhost[6];
         u_char shost[6];
         u_short type;
    };

The call int the code simply loosk like:

    void get_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
    {
         const struct ethernet_header *ethernet;
         const struct ip_header *ip;
         ethernet = (struct ethernet_header *)(packet);
         ip = (struct ip_header *)(packet + 16);

         printf("Destination MAC: %s\n", ethernet->dhost);
    }

The error I am receiveing is

error: dereferencing pointer to incomplete type

Now as far as I know the packet var is being initalized properly because it is being used in other sections of the code without a problem. In the case of the ip struct, this also works fine with no errors. I know what is being loaded into that particluar address I just can't figure out whats going on. Anyone have any ideas.


回答1:


error: dereferencing pointer to incomplete type

You missed including the header file which defines struct ethernet_header in the c file which has the function void get_packet().

The error is because the compiler cannot see the definition of the structure, most likely you are just forward declaring it. However, Since you dereference the pointer to structure the compiler must know the layout of the structure and hence must see the definition of the structure.

So just include it You need to include the header file which contains the definition of the structure in this particular c file.




回答2:


These 2 lines are vulnerable to this type of error. Compiler is unable to typecast the data in any or both statements. typecast it with correct datatype, it will work.

ethernet = (struct ethernet_header *)(packet);
ip = (struct ip_header *)(packet + 16);


来源:https://stackoverflow.com/questions/9733536/pcap-incomplete-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!