net.inet.udp.maxdgramは、FreeBSDベースのオペレーティングシステムにおいて、UDPパケットの最大受信サイズを制御するためのシステムパラメータです。
このパラメータはUDPパケットの受信バッファサイズを設定するものであり、通常はデフォルトで9216バイトです。これは、UDPパケットが受信バッファを超えるサイズになると、トリム(切り捨て)されることを意味します。
以下の9217にするとMessage too long (os error 40)になります。
let socket = UdpSocket::bind("127.0.0.1:0")?;
loop {
let mut input = String::new();
io::stdin().read_line(&mut input)?;
input = "a".to_string().repeat(9217);
socket.send_to(input.as_bytes(), address)?;
let mut buffer = [0u8; 1024];
socket.recv_from(&mut buffer).expect("failed to receive");
print!(
"{}",
str::from_utf8(&buffer).expect("failed to convert to String")
);
}我が子を調べてみると・・・
dai@Mac-Studio ~ % sysctl -a | grep udp
net.inet.ip.portrange.ipport_allow_udp_port_exhaustion: 0
net.inet.udp.checksum: 1
net.inet.udp.maxdgram: 9216
net.inet.udp.recvspace: 786896
net.inet.udp.log_in_vain: 0
net.inet.udp.blackhole: 0
net.inet.udp.pcbcount: 73
net.inet.udp.randomize_ports: 1確かに9216が最大サイズに設定されてました。
以下のコマンドで設定変更できます。
dai@Mac-Studio ~ % sysctl -w net.inet.udp.maxdgram=65507