set socket option is why so important for a socket (IP_HDRINCL) In ICMP request?

Siva Kannan picture Siva Kannan · Mar 1, 2014 · Viewed 8k times · Source

I am new to socket programming

I saw a ICMP request program , in that they used setsockopt to a socket

int on = 1;

setsockopt(s, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on))

but even if I do not use this statement, the program runs correctly. Why is it so important to mention to the kernel this socket including the IP structure?

Answer

abligh picture abligh · Mar 1, 2014

The IP_HDRINCL option does the following (from the man page):

The IPv4 layer generates an IP header when sending a packet unless the IP_HDRINCL socket option is enabled on the socket. When it is enabled, the packet must contain an IP header. For receiving the IP header is always included in the packet.

Presumably your program is constructing an IP header. If you remove this option, it will use the kernel's IP header. Whether that 'works' or not depends on what your program does. Perhaps under some circumstances it wants to customise the IP header and with this removed that will not work.

If you post the rest of the program or tell us a bit about it, we might be able to help.