Unix Programming - Re: Parsing Packets

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > June 2004 > Re: Parsing Packets





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Re: Parsing Packets
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
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com