hlen and sizeof(struct ip) in ip_output.c
In ip_output there's 2 lines of code that I can't understand:
mhlen = sizeof(struct ip)
and
if (hlen > sizeof(struct ip)) { ...
why do we need to compare hlen 开发者_如何学Pythonwhich is the lengh of the original ip packet's header to the sizeof(struct ip) ?
can you please help?
It looks like maybe this is from BSD's ip_fragment function? one of those variables is mhlen
, and one is hlen
, so they could be different. The IP header is variable length. The struct ip
has the fixed portion. The hlen
variable has the actual length including the variable length options at the end.
IP headers can have additional options (e.g. source route record, for tracerouting). The total size of the IP header is encoded in the hlen
field as a number of 32 bit words, so it will be 5 (for 20 bytes) if there are no IP options, and greater if there are IP options.
精彩评论