Doubt in networking headers

Status
Not open for further replies.

navenmou

Full Member level 4
Joined
Sep 25, 2010
Messages
228
Helped
49
Reputation
98
Reaction score
46
Trophy points
1,318
Location
Bangalore, India
Activity points
2,588
Hi all

Please explain below thing

#define IP_HL(ip) (((ip)->ip_vhl) & 0x0f)
 

IP headers have variable length so the macro extracts the length of the IP header which is in the least significant 4 bits of the first byte (the ip_vhl field)

have a look at the header specification
IPv4 - Wikipedia, the free encyclopedia
 
Last edited:

so long as you know the identifiers used in the structure you can write macros

this is an example of the declaration of struct ip {}
Code:
/*
 * Structure of an internet header, naked of options.
 */
struct ip {
#ifdef _IP_VHL
	u_char	ip_vhl;			/* version << 4 | header length >> 2 */
#else
#if BYTE_ORDER == LITTLE_ENDIAN
	u_int	ip_hl:4,		/* header length */
		ip_v:4;			/* version */
#endif
#if BYTE_ORDER == BIG_ENDIAN
	u_int	ip_v:4,			/* version */
		ip_hl:4;		/* header length */
#endif
...
from
**broken link removed**
 

no no i am not asking about ip headers

like ip header how we define macro for tcp header...
 

to access the TCP source port
Code:
// TCP header
struct tcphdr {
	u_short	th_sport;		/* source port */
	u_short	th_dport;		/* destination port */
	// etc etc
} tcp;

// macro to access source port
#define sourcePort tcp.th_sport

int main()
{
    sourcePort=80;
    cout << sourcePort << endl;
}
TCP header is from
**broken link removed**
 

try
Code:
   cout << sizeof(struct tcphdr) << endl;
in the above case would give 4, i.e. two 16 bit integers
 

#define TH_OFF(th) (((th)->th_offx2 & 0xf0) >> 4)

What is this mean??
 

your are accessing the th_offx2 feild
1. the th_offx2 & 0xf0 extracts the field (4 bits in bits 4 to 7) setting all other bits to 0
2. then (th_offx2 & 0xf0) >> 4 shifts it right 4 bits so you have the value in bits 0 to 3
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…