Converting char* to struct

Viewed 2644

In the code here, there is a line:

struct iphdr * iph = (struct iphdr *)buffer;

in ProcessPacket function where buffer is of type char*. buffer has been given value by recvfrom in the main function. How is the simple string (buffer) converted to a structure and how is the data safely extracted?

iphdr:

struct iphdr {
    #if defined(__LITTLE_ENDIAN_BITFIELD)
        __u8    ihl:4,
                version:4;
    #elif defined (__BIG_ENDIAN_BITFIELD)
        __u8    version:4,
                ihl:4;
    #else
        #error  "Please fix <asm/byteorder.h>"
    #endif
         __u8   tos;
         __u16  tot_len;
         __u16  id;
         __u16  frag_off;
         __u8   ttl;
         __u8   protocol;
         __u16  check;
         __u32  saddr;
         __u32  daddr;
         /*The options start here. */
};
3 Answers
Related