| Lev Walkin 2004-06-30, 8:53 pm |
| chris wrote:
>
>
> Problem fixed:
>
> packet += sizeof(struct ether_header);
> iphdr = (struct ip *) packet;
> printf("%02X packet captured; %d bytes\n", iphdr->ip_p, size);
>
> I guess it had something to do with precedence?
>
No, it does have to do with C basic pointer arithmetics.
If you have pointer to a type, adding a size to it WOULD NOT shift
that pointer to this many bytes:
struct ip *packet;
packet += 1;
In fact, the difference between the packet pointer and its previous
value will be the size of an (struct ip).
So, this code is invalid:
=== cut ===
iphdr = (struct ip *) packet + sizeof(struct ether_header);
=== cut ===
And must be rewritten as:
=== cut ===
iphdr = (struct ip *)(packet + sizeof(struct ether_header));
=== cut ===
--
Lev Walkin
vlm@lionet.info
|