如何在套接字上设置不分片(DF)标志? [英] How to set the don't fragment (DF) flag on a socket?

查看:4220
本文介绍了如何在套接字上设置不分片(DF)标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



查看理查德·史蒂文的第1卷Unix网络编程;第2章:使用UDP发送数据包的DF(不碎片标记) Sockets Networking API,我无法找到如何设置这个。



我怀疑我将使用setsockopt(),但无法在表中找到它。



请建议如何操作。

解决方案

使用 IP_DONTFRAG 选项::

$ b,使用 setsockopt()
$ b

  int val = 1; 
setsockopt(sd,IPPROTO_IP,IP_DONTFRAG,& val,sizeof(val));

这里有一个更详细地解释这一点的页面。



必须使用值 IP_PMTUDISC_DO (或 IP_PMTUDISC_DONT IP_MTU_DISCOVER c>将其关闭):

  int val = IP_PMTUDISC_DO; 
setsockopt(sd,IPPROTO_IP,IP_MTU_DISCOVER,& val,sizeof(val));

我没有测试过,只是看着头文件和一个网页搜索



至于是否有另一种方式可以设置DF标志:


我发现在我的程序中没有设置force DF flag,但 tcpdump 表明它是。有没有其他方法可以设置?


从这个优秀的网页此处


IP_MTU_DISCOVER: 设置或接收套接字的路径MTU发现设置。启用后,Linux将在此套接字上执行RFC 1191中定义的Path MTU Discovery。在所有传出数据报上设置不分片标志。系统范围内的默认值由 ipk_STREAM 控制 ip_no_pmtu_disc sysctl c>套接字,并禁用所有其他。对于非 SOCK_STREAM 套接字,用户有责任将MTU大小的块中的数据打包,并在必要时进行重传。如果设置了此标志(使用 EMSGSIZE ),内核将拒绝大于已知路径MTU的数据包。


在我看来,你可以使用 sysctl

  sysctl ip_no_pmtu_disc 

返回错误:ip_no_pmtu_disc是我的系统上的未知密钥,但它可能会在您的系统上设置。除此之外,我不知道任何其他( setsockopt()如前所述),可以影响设置。


I am trying to set the DF (don't fragment flag) for sending packets using UDP.

Looking at the Richard Steven's book Volume 1 Unix Network Programming; The Sockets Networking API, I am unable to find how to set this.

I suspect that I would do it with setsockopt() but can't find it in the table on page 193.

Please suggest how this is done.

解决方案

You do it with the setsockopt() call, by using the IP_DONTFRAG option::

int val = 1;
setsockopt(sd, IPPROTO_IP, IP_DONTFRAG, &val, sizeof(val));

Here's a page explaining this in further detail.

For Linux, it appears you have to use the IP_MTU_DISCOVER option with the value IP_PMTUDISC_DO (or IP_PMTUDISC_DONT to turn it off):

int val = IP_PMTUDISC_DO;
setsockopt(sd, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val));

I haven't tested this, just looked in the header files and a bit of a web search so you'll need to test it.

As to whether there's another way the DF flag could be set:

I find nowhere in my program where the "force DF flag" is set, yet tcpdump suggests it is. Is there any other way this could get set?

From this excellent page here:

IP_MTU_DISCOVER: Sets or receives the Path MTU Discovery setting for a socket. When enabled, Linux will perform Path MTU Discovery as defined in RFC 1191 on this socket. The don't fragment flag is set on all outgoing datagrams. The system-wide default is controlled by the ip_no_pmtu_disc sysctl for SOCK_STREAM sockets, and disabled on all others. For non SOCK_STREAM sockets it is the user's responsibility to packetize the data in MTU sized chunks and to do the retransmits if necessary. The kernel will reject packets that are bigger than the known path MTU if this flag is set (with EMSGSIZE).

This looks to me like you can set the system-wide default using sysctl:

sysctl ip_no_pmtu_disc

returns "error: "ip_no_pmtu_disc" is an unknown key" on my system but it may be set on yours. Other than that, I'm not aware of anything else (other than setsockopt() as previously mentioned) that can affect the setting.

这篇关于如何在套接字上设置不分片(DF)标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆