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§
- Dlpi
Handle - A DLPI handle wrapper that implements
Send
andSync
. - Dlpi
Recv - A receiver object returned from
recv_async
wrapped in a future. Callingawait
on this object yields the same result asrecv
. - Drop
Handle - A wrapper for DlpiHandle that closes the DLPI instance when dropped.
Enums§
- Result
Code - 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 ofrecv
. Callingawait
on result yields same result asrecv
. - send
- Send a message over a DLPI link.