wrote:
> Let's say we have a raw byte buffer in memory:
>
> char unsigned data[112];
>
> Within this buffer is data that you got from your network card, an
> ethernet frame to be exact. An ethernet frame is laid out as follows:
>
> First 6 octets: Destination MAC address
> Second 6 octets: Source MAC address
> Next two octets: Protocol
>
> In order to analyse the ethernet frame, I was thinking that maybe we
> could make an exact-size struct as follows:
Why go to all that trouble? One thing which is guaranteed to work, as
long as your layout is correct and chars are indeed 8 bits, is
#define PROTOCOL 12
#if (ENDIAN)
#define RAW_I16(x,y) (((int)x&0xff)<<8 + (y&0xff))
#else
#define RAW_I16(x,y) (((int)y&0xff)<<8 + (x&0xff))
#endif
if (RAW_I16(buffer[PROTOCOL], buffer[PROTOCOL+1]) == 0x0800)
puts("Contains an IP packet.");