6 releases (breaking)

0.5.0 Jan 15, 2025
0.4.0 Jan 2, 2025
0.3.0 Sep 10, 2024
0.2.0 Feb 1, 2024
0.0.0 Nov 22, 2023

#194 in Network programming

Download history 23/week @ 2024-10-30 17/week @ 2024-11-06 8/week @ 2024-11-13 13/week @ 2024-11-20 8/week @ 2024-11-27 513/week @ 2024-12-04 424/week @ 2024-12-11 1085/week @ 2024-12-18 119/week @ 2024-12-25 941/week @ 2025-01-01 1331/week @ 2025-01-08 807/week @ 2025-01-15 976/week @ 2025-01-22 977/week @ 2025-01-29 976/week @ 2025-02-05 522/week @ 2025-02-12

3,667 downloads per month
Used in 2 crates

MIT/Apache

93KB
2K SLoC

edge-dhcp

CI crates.io Documentation

Async + no_std + no-alloc implementation of the DHCP protocol.

For other protocols, look at the edge-net aggregator crate documentation.

Examples

DHCP client

//! NOTE: Run this example with `sudo` to be able to bind to the interface, as it uses raw sockets which require root privileges.

use core::net::{Ipv4Addr, SocketAddrV4};

use edge_dhcp::client::Client;
use edge_dhcp::io::{client::Lease, DEFAULT_CLIENT_PORT, DEFAULT_SERVER_PORT};
use edge_nal::{MacAddr, RawBind};
use edge_raw::io::RawSocket2Udp;

use log::info;

fn main() {
    env_logger::init_from_env(
        env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"),
    );

    futures_lite::future::block_on(run(
        2, // The interface index of the interface (e.g. eno0) to use; run `ip addr` to see it
        [0x4c, 0xcc, 0x6a, 0xa2, 0x23, 0xf5], // Your MAC addr here; run `ip addr` to see it
    ))
    .unwrap();
}

async fn run(if_index: u32, if_mac: MacAddr) -> Result<(), anyhow::Error> {
    let mut client = Client::new(rand::thread_rng(), if_mac);

    let stack = edge_nal_std::Interface::new(if_index);
    let mut buf = [0; 1500];

    loop {
        let mut socket: RawSocket2Udp<_> = RawSocket2Udp::new(
            stack.bind().await?,
            Some(SocketAddrV4::new(
                Ipv4Addr::UNSPECIFIED,
                DEFAULT_CLIENT_PORT,
            )),
            Some(SocketAddrV4::new(
                Ipv4Addr::UNSPECIFIED,
                DEFAULT_SERVER_PORT,
            )),
            [255; 6], // Broadcast
        );

        let (mut lease, options) = Lease::new(&mut client, &mut socket, &mut buf).await?;

        info!("Got lease {lease:?} with options {options:?}");

        info!("Entering an endless loop to keep the lease...");

        lease.keep(&mut client, &mut socket, &mut buf).await?;
    }
}

DHCP server

//! NOTE: Run this example with `sudo` to be able to bind to the interface, as it uses raw sockets which require root privileges.

use core::net::{Ipv4Addr, SocketAddrV4};

use edge_dhcp::io::{self, DEFAULT_CLIENT_PORT, DEFAULT_SERVER_PORT};
use edge_dhcp::server::{Server, ServerOptions};
use edge_nal::RawBind;
use edge_raw::io::RawSocket2Udp;

fn main() {
    env_logger::init_from_env(
        env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"),
    );

    futures_lite::future::block_on(run(
        0, // The interface index of the interface (e.g. eno0) to use; run `ip addr` to see it
    ))
    .unwrap();
}

async fn run(if_index: u32) -> Result<(), anyhow::Error> {
    let stack = edge_nal_std::Interface::new(if_index);

    let mut buf = [0; 1500];

    let ip = Ipv4Addr::new(192, 168, 0, 1);

    let mut socket: RawSocket2Udp<_> = RawSocket2Udp::new(
        stack.bind().await?,
        Some(SocketAddrV4::new(
            Ipv4Addr::UNSPECIFIED,
            DEFAULT_SERVER_PORT,
        )),
        Some(SocketAddrV4::new(
            Ipv4Addr::UNSPECIFIED,
            DEFAULT_CLIENT_PORT,
        )),
        [0; 6],
    );

    let mut gw_buf = [Ipv4Addr::UNSPECIFIED];

    io::server::run(
        &mut Server::<_, 64>::new_with_et(ip), // Will give IP addresses in the range 192.168.0.50 - 192.168.0.200, subnet 255.255.255.0
        &ServerOptions::new(ip, Some(&mut gw_buf)),
        &mut socket,
        &mut buf,
    )
    .await?;

    Ok(())
}

Dependencies

~0.7–1.4MB
~30K SLoC

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy