Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 208
Siva Kannan Avatar asked Mar 01 '14 16:03

Siva Kannan


People also ask

How do I use the IP_hdrincl socket?

This socket permits us to communicate using IP datagrams. Next, we enable the IP_HDRINCL option using the setsockopt function. Next, we create our IP header using the previously defined ipHdr_t typedef. We populate the IP header with a set of common values. The source and destination IP addresses are interesting fields to note.

When should IP options be set in sockets?

For sockets that use a connection-oriented transport service, IP options that are set using setsockopt () are only used if they are set prior to a connect () being issued. After the connection is established, any IP options that the user sets are ignored.

What is the difference between SOCK_RAW and IP_hdrincl?

Applies only to SOCK_RAW sockets. The TCP/IP service provider may set the ID field, if the value supplied by the application is zero. The IP_HDRINCL option is applied only to the SOCK_RAW type of protocol. A TCP/IP service provider that supports SOCK_RAW should also support IP_HDRINCL. Gets or sets the IP_IFLIST state of the socket.

How do I set socket options in the IBM i® API?

#define _XOPEN_SOURCE 520 #include <sys/socket.h> int setsockopt (int socket_descriptor , int level , int option_name , const void * option_value , socklen_t option_length ) The setsockopt () function is used to set socket options. There are two versions of the API, as shown above. The base IBM i ® API uses BSD 4.3 structures and syntax.


1 Answers

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.

like image 100
abligh Avatar answered Nov 15 '22 06:11

abligh