Crate dlpi

Source
Expand description

This is a crate for interacting with layer-2 network devices through DLPI. For more details on DLPI see man dlpi and man libdlpi.

use std::io::Result;
use std::thread::spawn;

fn main() -> Result<()> {
    // L2 multicast address to send packets to
    let mc = [0xff, 0xff, 0x00, 0x00, 0x00, 0x47];

    // Open up an interface called sim0 and attach to the ethertype 0x4000
    let dh_recv = dlpi::open("sim0", 0).expect("open recv");
    dlpi::bind(dh_recv, 0x4000).expect("bind recv");
    dlpi::enable_multicast(dh_recv, &mc).expect("enable multicast");

    // strt a receiver thread
    let t = spawn(move || {

        // allocated buffers for a senders L2 address and a message
        let mut src = [0u8; dlpi::sys::DLPI_PHYSADDR_MAX];
        let mut msg = [0; 256];

        // wait for a message
        let n = match dlpi::recv(dh_recv, &mut src, &mut msg, -1, None) {
            Ok((_, len)) => len,
            Err(e) => panic!("recv: {}", e),
        };

    });

    // Open up an interface called sim1 and attach to ethertype 0x4000
    let dh_send = dlpi::open("sim1", 0).expect("send");
    dlpi::bind(dh_send, 0x4000).expect("bind");

    // Send a message
    let message = b"do you know the muffin man?";
    dlpi::send(dh_send, &mc, &message[..], None).expect("send");

    // wait on the receiver and then shut down
    t.join().expect("join recv thread");
    dlpi::close(dh_send);
    dlpi::close(dh_recv);

    Ok(())
}

Re-exports§

pub use libdlpi_sys as sys;

Structs§

DlpiHandle
A DLPI handle wrapper that implements Send and Sync.
DlpiRecv
A receiver object returned from recv_async wrapped in a future. Calling await on this object yields the same result as recv.
DropHandle
A wrapper for DlpiHandle that closes the DLPI instance when dropped.

Enums§

ResultCode
Result of a DLPI operation.

Functions§

bind
Bind a DLPI link to a service access point type.
close
Close the provided handle.
disable_multicast
Disable reception of messages destined to the provided layer-2 address.
enable_multicast
Enable reception of messages destined to the provided layer-2 address.
fd
Get a file descriptor associated with the provided handle.
open
Creates a DLPI link instance.
promisc_off
Disable promiscuous mode for the specified handle. See DL_PROMISC_* for levels.
promisc_on
Enable promiscuous mode for the specified handle. See DL_PROMISC_* for levels.
recv
Receive a message from a DLPI link.
recv_async
An async version of recv. Calling await on result yields same result as recv.
send
Send a message over a DLPI link.