5 releases
0.1.4 | Aug 26, 2022 |
---|---|
0.1.3 | Dec 17, 2018 |
0.1.2 | Apr 28, 2018 |
0.1.1 | Apr 23, 2018 |
0.1.0 | Apr 22, 2018 |
#3 in #ip-pktinfo
45 downloads per month
Used in 2 crates
16KB
238 lines
udp_sas
Source address selection for UDP sockets in Rust
This crate provides an extension trait for std::net::UdpSocket
that supports
source address selection for outgoing UDP datagrams. This is useful for
implementing a UDP server that binds multiple network interfaces.
The implementation relies on socket options
IP_PKTINFO
(for IPv4) and
IPV6_RECVPKTINFO
(for IPv6).
lib.rs
:
This crate provides an extension trait for std::net::UdpSocket
that supports source address
selection for outgoing UDP datagrams. This is useful for implementing a UDP server that binds
multiple network interfaces.
The implementation relies on socket options IP_PKTINFO
(for IPv4) and IPV6_RECVPKTINFO
(for IPv6).
use std::net::{UdpSocket,SocketAddr};
use udp_sas::UdpSas;
fn main() {
demo().unwrap();
}
fn demo() -> std::io::Result<()>
{
let mut buf = [0u8;128];
// Create the server socket and bind it to 0.0.0.0:30012
//
// Note: we will use 127.0.0.23 as source/destination address
// for our datagrams (to demonstrate the crate features)
//
let srv = UdpSocket::bind_sas("0.0.0.0:30012".parse::<SocketAddr>().unwrap())?;
let srv_addr = "127.0.0.23:30012".parse().unwrap();
// Create the client socket and bind it to an anonymous port
//
// Note: we will use 127.0.0.45 as source/destination address
// for our datagrams (to demonstrate the crate features)
//
let cli = UdpSocket::bind_sas("0.0.0.0:0".parse::<SocketAddr>().unwrap())?;
let cli_addr = SocketAddr::new(
"127.0.0.45".parse().unwrap(),
cli.local_addr().unwrap().port());
assert_ne!(cli_addr.port(), 0);
// send a request (msg1) from the client to the server
let msg1 = "What do you get if you multiply six by nine?";
let nb = cli.send_sas(msg1.as_bytes(), &srv_addr, &cli_addr.ip())?;
assert_eq!(nb, msg1.as_bytes().len());
// receive the request on the server
let (nb, peer, local) = srv.recv_sas(&mut buf)?;
assert_eq!(peer, cli_addr);
assert_eq!(local, srv_addr.ip());
assert_eq!(nb, msg1.as_bytes().len());
assert_eq!(&buf[0..nb], msg1.as_bytes());
// send a reply (msg2) from the server to the client
let msg2 = "Forty-two";
let nb = srv.send_sas(msg2.as_bytes(), &peer, &local)?;
assert_eq!(nb, msg2.as_bytes().len());
// receive the reply on the client
let (nb, peer, local) = cli.recv_sas(&mut buf)?;
assert_eq!(peer, srv_addr);
assert_eq!(local, cli_addr.ip());
assert_eq!(nb, msg2.as_bytes().len());
assert_eq!(&buf[0..nb], msg2.as_bytes());
Ok(())
}
Dependencies
~33–520KB