简体   繁体   English

设置套接字选项为什么对 ICMP 请求中的套接字 (IP_HDRINCL) 如此重要?

[英]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我看到了一个ICMP 请求程序,因为他们使用了setsockopt到一个套接字

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?为什么向内核提及这个套接字(包括 IP 结构)如此重要?

The IP_HDRINCL option does the following (from the man page): IP_HDRINCL选项执行以下操作(来自手册页):

The IPv4 layer generates an IP header when sending a packet unless the IP_HDRINCL socket option is enabled on the socket.除非在套接字上启用IP_HDRINCL套接字选项,否则 IPv4 层在发送数据包时会生成 IP 标头。 When it is enabled, the packet must contain an IP header.启用时,数据包必须包含 IP 标头。 For receiving the IP header is always included in the packet.用于接收的 IP 标头始终包含在数据包中。

Presumably your program is constructing an IP header.据推测,您的程序正在构建一个 IP 标头。 If you remove this option, it will use the kernel's IP header.如果删除此选项,它将使用内核的 IP 标头。 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.也许在某些情况下它想要自定义 IP 标头,并且将其删除将不起作用。

If you post the rest of the program or tell us a bit about it, we might be able to help.如果您发布程序的其余部分或告诉我们一些有关它的信息,我们可能会提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM