0

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.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
Blackninja543
  • 3,639
  • 5
  • 23
  • 32
  • @nos The structure is defined in the same file its being called. same goes for ip_header as well which does work. – Blackninja543 Mar 16 '12 at 07:45
  • @nos Turns out gcc decided not to warn me about an unknown data type during the compile time. The initialization was wrong, thanks for the help. – Blackninja543 Mar 16 '12 at 07:57

2 Answers2

2

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.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Its a quick program so I didn't go about creating header files, this is just a structure defined at the top of main.c. The same goes for IP_header which does work. – Blackninja543 Mar 16 '12 at 07:45
  • @Blackninja543: As I mentioned in the answer the problem is that compiler cannot *see* the definiiton of the structure when you dereference a pointer to it inside the function. Lookout for errors, typos on that basis. – Alok Save Mar 16 '12 at 07:48
  • Well that was stupid....... looks like I miss spelled the initial declaration and the compiler decided not to warn me about an unknown declaration type...... Thx for the help – Blackninja543 Mar 16 '12 at 07:55
0

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);
Rahul Raina
  • 3,322
  • 25
  • 30