Search code examples
socketsnetworkingrustudpdhcp

How do I send a UDP datagram with source address 0.0.0.0 in Rust?


I am writing a DHCP client in Rust.

The client must send a DHCPDISCOVER message when there are no configured interfaces. Tools like dhclient are able to do that by sending the message with source address 0.0.0.0.

Unfortunately, with high level Rust primitives I cannot tweak IP header.

Should I completely move to a low level networking solution (libc, nix) or is there a better approach?

The binding code:

let socket = UdpBuilder::new_v4()?;
let socket = socket.bind(addr)?;
let socket = UdpSocket::from_std(socket, &Handle::default())?;
socket.set_broadcast(true)?;

UdpBuilder is from the net2 crate.

UdpSocket is from the tokio crate.

addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0,0,0,0)), 68)


Solution

  • It is not possible with Rust's high level UDP abstractions.

    Low level packet builders must be used instead. For example, BPF on FreeBSD and Mac OS.