I need to send the same multicast UDP packet on all available addresses on my host. I need to send to localhost
/127.0.0.1
and 239.255.0.1
. Rust's std
lib has deprecated multicast behavior since version 1.2, and anyway, to get the specificity I need I have to switch to the more complete net2
.
The net2
API has a bunch of UDP extensions which provide the API I'm looking for. I'm specifically interested in
fn join_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> Result<()>
But I can't figure out what to do for multiaddr vs interface. I thought I would just give the multiaddr 127.0.0.1
or 239.255.0.1
but how do I find the valid IPv4's for the interface
parameter? Is there a Rust function, somewhere out there, that can provide a Vec<Ipv4Addr>
? Do I need to parse the output of ifconfig
?
Here is a stand-alone program which sends out my public interface (had to find that 192.168.0.102
address by hand):
extern crate net2;
use std::net::{ UdpSocket, Ipv4Addr };
use net2::UdpSocketExt;
fn main() {
let sock = UdpSocket::bind("0.0.0.0:2345").unwrap();
let local_addr = Ipv4Addr::new(192, 168, 0, 102);
let multicast_addr = Ipv4Addr::new(239, 255, 0, 1);
sock.join_multicast_v4(&multicast_addr, &local_addr).unwrap();
}
I'm compiling on nightlies if that makes any difference.