4 releases

Uses old Rust 2015

0.1.3 Dec 17, 2018
0.1.2 Apr 28, 2018
0.1.1 Apr 23, 2018
0.1.0 Apr 22, 2018

#41 in #udp

Download history 27/week @ 2021-06-03 13/week @ 2021-06-10 152/week @ 2021-06-17 48/week @ 2021-06-24 77/week @ 2021-07-01 88/week @ 2021-07-08 149/week @ 2021-07-15 52/week @ 2021-07-22 183/week @ 2021-07-29 142/week @ 2021-08-05 112/week @ 2021-08-12 136/week @ 2021-08-19 68/week @ 2021-08-26 63/week @ 2021-09-02 66/week @ 2021-09-09 79/week @ 2021-09-16

155 downloads per month
Used in udp_sas_mio

MIT/Apache

16KB
238 lines

udp_sas

Build Status Crates.io

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

~185KB