問題描述
我正在嘗試設(shè)置 DF(不分段標(biāo)志)以使用 UDP 發(fā)送數(shù)據(jù)包.
I am trying to set the DF (don't fragment flag) for sending packets using UDP.
看Richard Steven 的書Volume 1 Unix Network Programming;Sockets Networking API,我找不到如何設(shè)置它.
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.
我懷疑我會用 setsockopt() 來做,但在第 193 頁的表格中找不到它.
I suspect that I would do it with setsockopt() but can't find it in the table on page 193.
請建議如何做到這一點(diǎn).
Please suggest how this is done.
推薦答案
您可以使用 setsockopt()
調(diào)用來實(shí)現(xiàn),使用 IP_DONTFRAG
選項(xiàng):
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));
這是一個(gè)解釋這個(gè)的頁面更詳細(xì).
Here's a page explaining this in further detail.
對于 Linux,您似乎必須使用值為 IP_PMTUDISC_DO
(或 IP_PMTUDISC_DONT
以將其關(guān)閉)的 IP_MTU_DISCOVER
選項(xiàng):
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));
我沒有測試過這個(gè),只是查看了頭文件和一些網(wǎng)絡(luò)搜索,所以你需要測試它.
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.
至于是否有另一種方式可以設(shè)置 DF 標(biāo)志:
As to whether there's another way the DF flag could be set:
我在我的程序中找不到強(qiáng)制 DF 標(biāo)志"的任何地方已設(shè)置,但 tcpdump
表明已設(shè)置.還有其他方法可以設(shè)置嗎?
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?
從這個(gè)優(yōu)秀的頁面這里:
IP_MTU_DISCOVER:
設(shè)置或接收套接字的路徑 MTU 發(fā)現(xiàn)設(shè)置.啟用后,Linux 將在此套接字上執(zhí)行 RFC 1191 中定義的路徑 MTU 發(fā)現(xiàn).在所有傳出數(shù)據(jù)報(bào)上都設(shè)置了不分段標(biāo)志.系統(tǒng)范圍的默認(rèn)值由 ip_no_pmtu_disc
sysctl
控制,用于 SOCK_STREAM
套接字,并在所有其他套接字上禁用.對于非 SOCK_STREAM
套接字,用戶有責(zé)任將數(shù)據(jù)打包成 MTU 大小的塊,并在必要時(shí)進(jìn)行重新傳輸.如果設(shè)置了此標(biāo)志(使用 EMSGSIZE
),內(nèi)核將拒絕大于已知路徑 MTU 的數(shù)據(jù)包.
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 theip_no_pmtu_disc
sysctl
forSOCK_STREAM
sockets, and disabled on all others. For nonSOCK_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 (withEMSGSIZE
).
在我看來,您可以使用 sysctl
設(shè)置系統(tǒng)范圍的默認(rèn)值:
This looks to me like you can set the system-wide default using sysctl
:
sysctl ip_no_pmtu_disc
返回<代碼>錯(cuò)誤:ip_no_pmtu_disc";在我的系統(tǒng)上是一個(gè)未知的密鑰,但它可能被設(shè)置在你的系統(tǒng)上.除此之外,我不知道還有什么會影響設(shè)置(除了前面提到的 setsockopt()
).
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.
這篇關(guān)于如何在套接字上設(shè)置不分段 (DF) 標(biāo)志?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!