Linux setsockopt 的奇怪错误,“协议不可用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10004969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:36:29  来源:igfitidea点击:

Weird error with setsockopt, "Protocol not available"

clinuxsockets

提问by W00t

The applicable code is below, as is the exact error.

适用的代码如下,确切的错误也是如此。

sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
if ( setsockopt( sd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on) ) < 0 )
{
    printf("%s\n", strerror(errno));
}
if ( setsockopt( sd, IPPROTO_IP, IP_DF, &on, sizeof(on) ) < 0 )
{
    printf("%s\n", strerror(errno));
    printf("DF\n");
}

Error:

错误:

root@PC:~# gcc main.c
main.c: In function ‘main':
main.c:71: warning: format not a string literal and no format arguments
root@PC:~# ./a.out localhost
Protocol not available
DF

It's odd that the second setsockopt is erroring, while the first one isn't.

奇怪的是,第二个 setsockopt 出错了,而第一个没有。

采纳答案by Ignacio Vazquez-Abrams

IP_DFis a packet flag, not a socket option. Valid socket options are given in the ip(7)man page.

IP_DF是数据包标志,而不是套接字选项。ip(7)手册页中给出了有效的套接字选项。

回答by sardok

from ip(7) man page;

来自 ip(7) 手册页;

   IP_NODEFRAG (since Linux 2.6.36)
          If enabled (argument is nonzero), the reassembly of outgoing packets is
          disabled in the netfilter layer.  This option is only valid for
          SOCK_RAW sockets.  The argument is an integer.