RustForge

foundry

Foundry OS: the operating system built on the Rustbelt kernel

foundry / rust / net / src / bin / ifconfig.rs
1398 bytes ยท raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//! `ifconfig` (alias `ip`): show the network card and our IP address.
//!
//!     eth0: virtio-net  MAC 52:54:00:12:34:56  IRQ 11
//!           inet 10.0.2.15/24  gateway 10.0.2.2  DNS 10.0.2.3  (DHCP)
//!           RX 12 packets (2 KiB)  TX 9 packets (1 KiB)
#![no_std]
#![no_main]

#[macro_use]
extern crate ulib;
ulib::entry!(main);

use ulib::Ip;

fn main() -> i64 {
    let mut info = ulib::netinfo();
    if info.present == 0 {
        eprintln!("ifconfig: no network card (QEMU needs -device virtio-net-pci)");
        return 1;
    }
    // Right after boot DHCP may still be running: give it up to 3 s.
    for _ in 0..30 {
        if info.configured != 0 {
            break;
        }
        ulib::sleep_ms(100);
        info = ulib::netinfo();
    }
    let m = info.mac;
    println!(
        "eth0: virtio-net  MAC {:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}  IRQ {}",
        m[0], m[1], m[2], m[3], m[4], m[5], info.irq
    );
    if info.configured != 0 {
        println!(
            "      inet {}/{}  gateway {}  DNS {}  (DHCP)",
            Ip(info.address), info.prefix_len, Ip(info.gateway), Ip(info.dns)
        );
    } else {
        println!("      no address yet (waiting for DHCP)");
    }
    println!(
        "      RX {} packets ({} KiB)  TX {} packets ({} KiB)",
        info.rx_packets, info.rx_bytes / 1024, info.tx_packets, info.tx_bytes / 1024
    );
    0
}