我试图设置使用UDP发送数据包的DF(不分段标志).
看看Richard Steven的书第1卷Unix网络编程;套接字网络API,我无法找到如何设置它.
我怀疑我会用setsockopt()来做,但是在第193页的表格中找不到它.
请建议如何做到这一点.
解决方法:
你可以通过使用IP_DONTFRAG选项调用setsockopt()来实现::
int val = 1;
setsockopt(sd, IPPROTO_IP, IP_DONTFRAG, &val, sizeof(val));
Here’s页面进一步详细解释了这一点.
对于Linux,您似乎必须使用IP_MTU_DISCOVER选项,其值为IP_PMTUDISC_DO(或IP_PMTUDISC_DONT以将其关闭):
int val = IP_PMTUDISC_DO;
setsockopt(sd, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val));
我没有测试过这个,只是查看了头文件和一些网页搜索,所以你需要测试它.
至于是否可以设置DF标志的另一种方式:
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?
从这个优秀的页面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).
这对我来说就像你可以使用sysctl设置系统范围的默认值:
sysctl ip_no_pmtu_disc
在我的系统上返回“错误:”ip_no_pmtu_disc“是一个未知的密钥”,但它可能会在你的系统上设置.除此之外,我不知道可能影响设置的任何其他东西(除了之前提到的setsockopt()).
标签:packet,c,sockets,udp
来源: https://codeday.me/bug/20190925/1815864.html