maximilliano
maximilliano

Reputation: 163

IP Header bitwise definition explanation needed

In the following code:

struct sniff_ip {
    u_char ip_vhl;      /* version << 4 | header length >> 2 */
    u_char ip_tos;      /* type of service */
    u_short ip_len;     /* total length */
    u_short ip_id;      /* identification */
    u_short ip_off;     /* fragment offset field */
#define IP_RF 0x8000        /* reserved fragment flag */
#define IP_DF 0x4000        /* dont fragment flag */
#define IP_MF 0x2000        /* more fragments flag */
#define IP_OFFMASK 0x1fff   /* mask for fragmenting bits */
    u_char ip_ttl;      /* time to live */
    u_char ip_p;        /* protocol */
    u_short ip_sum;     /* checksum */
    struct in_addr ip_src,ip_dst; /* source and dest address */
};

How does C understand that IP_RF, IP_DF, IP_MF, IP_OFFMASK are part of ip_off? And how to access them?

Upvotes: 1

Views: 569

Answers (1)

corn3lius
corn3lius

Reputation: 4985

#defines are a pre-processor command. They are a compile replacement name and are not limited by the scope of that struct.

The author probably put them there to show callers where they should be used.

more reading.

http://en.wikipedia.org/wiki/C_preprocessor - look at macro definition and expansion.

--or--

http://www.cplusplus.com/doc/tutorial/preprocessor/

Upvotes: 1

Related Questions