I setup a raw Packet socket using the following:
sockFd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL) );
Then I am trying to set the socket option IP_HDRINCL
using:
int one = 1;
if (setsockopt (sockFd, IPPROTO_IP, IP_HDRINCL, &one, sizeof(one)) < 0)
LogPrint(LOG_UNKNOWN,"Warning: Cannot set HDRINCL!\n");
But I am unable to set this option (I get an error with errno
92 and message "Protocol not available". If I change PF_PACKET
to PF_INET
then the options is set but I have to use PF_PACKET
. So is the开发者_StackOverflowre a way to set this option with the socket created above?
Thanks a bunch.
PF_PACKET
sockets don't have any option that I asked in the question to be set. That option is only available on PF_INET
or PF_INET6
sockets with type SOCK_RAW
.
If one wants the IP headers to be added by the kernel, the one must use the PF_INET
socket.
You can do as following instead:
int sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
It implies that a socket is a raw socket with IP_HDRINCL set. If you use getsockopt
to check the value of the socket, you will see IP_HDRINCL is already set.
精彩评论