mirror of
https://github.com/dndx/phantun.git
synced 2025-09-10 01:07:28 +08:00
Calculate cmsg buffer size statically
Although generic const fn max hasn't been stabilized yet[1], it's possible to write a concrete impl for usize. Follow-up to #178 and #225. [1] https://github.com/rust-lang/rust/issues/92391
This commit is contained in:
parent
9bdfd76819
commit
4a11c6adc7
@ -62,13 +62,11 @@ pub async fn udp_recv_pktinfo(
|
|||||||
buf: &mut [u8],
|
buf: &mut [u8],
|
||||||
) -> std::io::Result<(usize, SocketAddr, IpAddr)> {
|
) -> std::io::Result<(usize, SocketAddr, IpAddr)> {
|
||||||
sock.async_io(Interest::READABLE, || {
|
sock.async_io(Interest::READABLE, || {
|
||||||
// according to documented struct definition in RFC 3542,
|
const CONTROL_MESSAGE_BUFFER_SIZE: usize = max_usize(
|
||||||
// sizeof(in6_pktinfo) should always be larger than sizeof(in_pktinfo),
|
cmsg_space::<nix::libc::in_pktinfo>(),
|
||||||
// this assert just double checks that. The goal is to avoid
|
cmsg_space::<nix::libc::in6_pktinfo>(),
|
||||||
// a heap allocation with Vec at runtime.
|
);
|
||||||
assert!(cmsg_space::<nix::libc::in6_pktinfo>() >= cmsg_space::<nix::libc::in_pktinfo>());
|
let mut control_message_buffer = [0u8; CONTROL_MESSAGE_BUFFER_SIZE];
|
||||||
|
|
||||||
let mut control_message_buffer = [0u8; cmsg_space::<nix::libc::in6_pktinfo>()];
|
|
||||||
let iov = &mut [std::io::IoSliceMut::new(buf)];
|
let iov = &mut [std::io::IoSliceMut::new(buf)];
|
||||||
let res = nix::sys::socket::recvmsg::<SockaddrStorage>(
|
let res = nix::sys::socket::recvmsg::<SockaddrStorage>(
|
||||||
sock.as_raw_fd(),
|
sock.as_raw_fd(),
|
||||||
@ -151,3 +149,7 @@ pub fn assign_ipv6_address(device_name: &str, local: Ipv6Addr, peer: Ipv6Addr) {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
rtnl.send(&nl_header).unwrap();
|
rtnl.send(&nl_header).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const fn max_usize(a: usize, b: usize) -> usize {
|
||||||
|
if a > b { a } else { b }
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user