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?
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.
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.
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.
#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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With