### Get Available Interfaces Source: https://docs.rs/netdev/0.39.0/netdev/interface/index_search=std%3A%3Avec Retrieves a list of all available network interfaces on the system. ```APIDOC ## GET /netdev/interfaces ### Description Get a list of available Network Interfaces. ### Method GET ### Endpoint /netdev/interfaces ### Parameters None ### Request Example None ### Response #### Success Response (200) - **interfaces** (array of strings) - A list containing the names of all available network interfaces. #### Response Example { "interfaces": ["eth0", "wlan0", "lo"] } ``` -------------------------------- ### Get IPv4 Network Components (Rust) Source: https://docs.rs/netdev/0.39.0/netdev/prelude/struct.Ipv4Net_search= Provides examples of accessing the core components of an `Ipv4Net` structure: the address, prefix length, maximum prefix length, netmask, hostmask, network address, and broadcast address. These methods are fundamental for network analysis and manipulation. ```rust let net: Ipv4Net = "10.1.0.0/20".parse().unwrap(); assert_eq!(Ok(net.netmask()), "255.255.240.0".parse()); let net: Ipv4Net = "10.1.0.0/20".parse().unwrap(); assert_eq!(Ok(net.hostmask()), "0.0.15.255".parse()); let net: Ipv4Net = "172.16.123.123/16".parse().unwrap(); assert_eq!(Ok(net.network()), "172.16.0.0".parse()); let net: Ipv4Net = "172.16.0.0/22".parse().unwrap(); assert_eq!(Ok(net.broadcast()), "172.16.3.255".parse()); ``` -------------------------------- ### Get IPv6 Network Properties Source: https://docs.rs/netdev/0.39.0/netdev/prelude/struct.Ipv6Net_search=std%3A%3Avec Provides examples for accessing core properties of an `Ipv6Net` instance, including the address, prefix length, maximum prefix length, network mask, host mask, network address, and broadcast address. ```rust let net: Ipv6Net = "fd00::/24".parse().unwrap(); assert_eq!(Ok(net.netmask()), "ffff:ff00::".parse()); let net: Ipv6Net = "fd00::/24".parse().unwrap(); assert_eq!(Ok(net.hostmask()), "::ff:ffff:ffff:ffff:ffff:ffff:ffff".parse()); let net: Ipv6Net = "fd00:1234:5678::/24".parse().unwrap(); assert_eq!(Ok(net.network()), "fd00:1200::".parse()); let net: Ipv6Net = "fd00:1234:5678::/24".parse().unwrap(); assert_eq!(Ok(net.broadcast()), "fd00:12ff:ffff:ffff:ffff:ffff:ffff:ffff".parse()); ``` -------------------------------- ### Create Ipv4Net with IP and Netmask (Rust) Source: https://docs.rs/netdev/0.39.0/netdev/prelude/struct.Ipv4Net_search=std%3A%3Avec Demonstrates creating an `Ipv4Net` from an `Ipv4Addr` and a netmask. It also includes an example of handling an invalid netmask that results in an error. ```Rust use std::net::Ipv4Addr; use ipnet::{Ipv4Net, PrefixLenError}; let net = Ipv4Net::with_netmask(Ipv4Addr::new(10, 1, 1, 0), Ipv4Addr::new(255, 255, 255, 0)); assert!(net.is_ok()); let bad_prefix_len = Ipv4Net::with_netmask(Ipv4Addr::new(10, 1, 1, 0), Ipv4Addr::new(255, 255, 0, 1)); assert_eq!(bad_prefix_len, Err(PrefixLenError)); ``` -------------------------------- ### Get Network Interfaces Source: https://docs.rs/netdev/0.39.0/netdev/interface/fn.get_interfaces_search=u32+-%3E+bool Retrieves a list of all available network interfaces on the system. ```APIDOC ## GET /interfaces ### Description Get a list of available Network Interfaces. ### Method GET ### Endpoint /interfaces ### Parameters #### Query Parameters None ### Request Example None ### Response #### Success Response (200) - **interfaces** (array of object) - A list of network interface objects. - **name** (string) - The name of the network interface. - **mac_address** (string) - The MAC address of the network interface. - **is_up** (boolean) - Indicates if the interface is currently up. #### Response Example ```json { "interfaces": [ { "name": "eth0", "mac_address": "00:1A:2B:3C:4D:5E", "is_up": true }, { "name": "lo", "mac_address": "00:00:00:00:00:00", "is_up": true } ] } ``` ``` -------------------------------- ### Getting Network Address from Ipv6Net Source: https://docs.rs/netdev/0.39.0/netdev/prelude/struct.Ipv6Net_search= Shows how to retrieve the network address component of an `Ipv6Net` using the `network` method. The result is an `Ipv6Addr` representing the start of the network. ```rust let net: Ipv6Net = "fd00:1234:5678::/24".parse().unwrap(); assert_eq!(Ok(net.network()), "fd00:1200::".parse()); ``` -------------------------------- ### Interface Initialization Methods Source: https://docs.rs/netdev/0.39.0/netdev/interface/interface/struct.Interface Methods for creating new `Interface` instances. `default()` attempts to construct an interface based on system defaults, while `dummy()` creates a placeholder interface. ```rust pub fn default() -> Result Construct a new default Interface instance ``` ```rust pub fn dummy() -> Interface Source ``` -------------------------------- ### MacAddr Constructor Methods Source: https://docs.rs/netdev/0.39.0/netdev/net/mac/struct.MacAddr_search= Methods for creating new `MacAddr` instances. ```APIDOC ## Constructor Methods ### `MacAddr::new(a: u8, b: u8, c: u8, d: u8, e: u8, f: u8) -> MacAddr` Constructs a new `MacAddr` from six octets. ### `MacAddr::from_octets(octets: [u8; 6]) -> MacAddr` Constructs a `MacAddr` from a `[u8; 6]` array. ### `MacAddr::from_hex_format(hex_mac_addr: &str) -> MacAddr` Parses a fixed-width hex string (`xx:xx:xx:xx:xx:xx`) into a `MacAddr`. ``` -------------------------------- ### Create Ipv4Net with IP and Prefix Length (Rust) Source: https://docs.rs/netdev/0.39.0/netdev/prelude/struct.Ipv4Net_search=std%3A%3Avec Shows how to create a new `Ipv4Net` instance using an `Ipv4Addr` and a prefix length. It also demonstrates error handling for invalid prefix lengths. ```Rust use std::net::Ipv4Addr; use ipnet::{Ipv4Net, PrefixLenError}; let net = Ipv4Net::new(Ipv4Addr::new(10, 1, 1, 0), 24); assert!(net.is_ok()); let bad_prefix_len = Ipv4Net::new(Ipv4Addr::new(10, 1, 1, 0), 33); assert_eq!(bad_prefix_len, Err(PrefixLenError)); ``` -------------------------------- ### Create a Dummy Network Interface (Rust) Source: https://docs.rs/netdev/0.39.0/src/netdev/interface/interface.rs_search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Constructs and returns a default `Interface` instance with all fields initialized to their default values. This is useful for testing or when a placeholder interface is needed. ```rust pub fn dummy() -> Interface { Interface { index: 0, name: String::new(), friendly_name: None, description: None, if_type: InterfaceType::Unknown, mac_addr: None, ipv4: Vec::new(), ipv6: Vec::new(), ipv6_scope_ids: Vec::new(), flags: 0, oper_state: OperState::Unknown, transmit_speed: None, receive_speed: None, stats: None, #[cfg(feature = "gateway")] gateway: None, #[cfg(feature = "gateway")] dns_servers: Vec::new(), mtu: None, #[cfg(feature = "gateway")] default: false, } } ``` -------------------------------- ### Get Default Local IP Address (Rust) Source: https://docs.rs/netdev/0.39.0/src/netdev/net/ip.rs Retrieves the IP address of the default network interface by first attempting to get the IPv4 address and falling back to IPv6 if the IPv4 attempt fails. Requires the `gateway` feature. ```rust use std::net::IpAddr; #[cfg(feature = "gateway")] pub fn get_local_ipaddr() -> Option { // binding the IPv4 socket can actually succeed but a later step will fail in IPv6-only, // we call `try_ipv6` in any case where `try_ipv4` fails if let Some(addr) = try_ipv4() { Some(addr) } else { try_ipv6() } } ``` -------------------------------- ### Ipv4Net Creation and Parsing Source: https://docs.rs/netdev/0.39.0/netdev/prelude/struct.Ipv4Net_search=std%3A%3Avec Demonstrates how to create and parse Ipv4Net objects. ```APIDOC ## Ipv4Net An IPv4 network address. Provides a `FromStr` implementation for parsing network addresses represented in CIDR notation. ### §Textual representation `Ipv4Net` provides a `FromStr` implementation for parsing network addresses represented in CIDR notation. See IETF RFC 4632 for the CIDR notation. ### §Examples ```rust use ipnet::Ipv4Net; let net: Ipv4Net = "10.1.1.0/24".parse().unwrap(); assert_eq!(Ok(net.network()), "10.1.1.0".parse()); ``` ## Implementations ### impl Ipv4Net #### pub const fn new( ip: Ipv4Addr, prefix_len: u8, ) -> Result Creates a new IPv4 network address from an `Ipv4Addr` and prefix length. ##### §Examples ```rust use std::net::Ipv4Addr; use ipnet::{Ipv4Net, PrefixLenError}; let net = Ipv4Net::new(Ipv4Addr::new(10, 1, 1, 0), 24); assert!(net.is_ok()); let bad_prefix_len = Ipv4Net::new(Ipv4Addr::new(10, 1, 1, 0), 33); assert_eq!(bad_prefix_len, Err(PrefixLenError)); ``` #### pub const fn new_assert(ip: Ipv4Addr, prefix_len: u8) -> Ipv4Net Creates a new IPv4 network address from an `Ipv4Addr` and prefix length. If called from a const context it will verify prefix length at compile time. Otherwise it will panic at runtime if prefix length is not less then or equal to 32. ##### §Examples ```rust use std::net::Ipv4Addr; use ipnet::{Ipv4Net}; // This code is verified at compile time: const NET: Ipv4Net = Ipv4Net::new_assert(Ipv4Addr::new(10, 1, 1, 0), 24); assert_eq!(NET.prefix_len(), 24); // This code is verified at runtime: let net = Ipv4Net::new_assert(Ipv4Addr::new(10, 1, 1, 0), 24); assert_eq!(NET.prefix_len(), 24); // This code does not compile: // const BAD_PREFIX_LEN: Ipv4Net = Ipv4Net::new_assert(Ipv4Addr::new(10, 1, 1, 0), 33); // This code panics at runtime: // let bad_prefix_len = Ipv4Net::new_assert(Ipv4Addr::new(10, 1, 1, 0), 33); ``` #### pub fn with_netmask( ip: Ipv4Addr, netmask: Ipv4Addr, ) -> Result Creates a new IPv4 network address from an `Ipv4Addr` and netmask. ##### §Examples ```rust use std::net::Ipv4Addr; use ipnet::{Ipv4Net, PrefixLenError}; let net = Ipv4Net::with_netmask(Ipv4Addr::new(10, 1, 1, 0), Ipv4Addr::new(255, 255, 255, 0)); assert!(net.is_ok()); let bad_prefix_len = Ipv4Net::with_netmask(Ipv4Addr::new(10, 1, 1, 0), Ipv4Addr::new(255, 255, 0, 1)); assert_eq!(bad_prefix_len, Err(PrefixLenError)); ``` ``` -------------------------------- ### GET /ip/get_local_ipaddr Source: https://docs.rs/netdev/0.39.0/netdev/net/ip/index_search= Retrieves the IP address of the default network interface. ```APIDOC ## GET /ip/get_local_ipaddr ### Description Retrieves the IP address of the default network interface. ### Method GET ### Endpoint /ip/get_local_ipaddr ### Parameters #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) - **ip_address** (string) - The IP address of the default network interface. #### Response Example { "ip_address": "192.168.1.100" } ``` -------------------------------- ### MacAddr Methods Source: https://docs.rs/netdev/0.39.0/netdev/prelude/struct.MacAddr_search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Provides various methods for creating, manipulating, and querying MacAddr instances. ```APIDOC ## MacAddr Methods ### Constructor Methods - **`new(a: u8, b: u8, c: u8, d: u8, e: u8, f: u8) -> MacAddr`** Constructs a new `MacAddr` from six octets. - **`from_octets(octets: [u8; 6]) -> MacAddr`** Constructs from a `[u8; 6]` array. - **`from_hex_format(hex_mac_addr: &str) -> MacAddr`** Parses a fixed-width hex string (`xx:xx:xx:xx:xx:xx`). - **`zero() -> MacAddr`** Returns the all-zeros address. - **`broadcast() -> MacAddr`** Returns the broadcast address (`ff:ff:ff:ff:ff:ff`). ### Utility Methods - **`octets(&self) -> [u8; 6]`** Returns the 6 octets backing this address. - **`address(&self) -> String`** Returns a colon-separated lowercase hex string (`xx:xx:xx:xx:xx:xx`). - **`is_broadcast(&self) -> bool`** Checks if the address is the broadcast address. - **`is_multicast(&self) -> bool`** Returns `true` if the address is multicast. - **`is_unicast(&self) -> bool`** Returns `true` if the address is unicast. - **`is_locally_administered(&self) -> bool`** Returns `true` if the address is locally administered. - **`is_universal(&self) -> bool`** Returns `true` if the address is universally administered. - **`oui(&self) -> [u8; 3]`** Returns the OUI (first 3 octets). ``` -------------------------------- ### Get Default Interface Source: https://docs.rs/netdev/0.39.0/netdev/interface/index_search=std%3A%3Avec Retrieves the default network interface of the system. ```APIDOC ## GET /netdev/default_interface ### Description Get the default network interface. ### Method GET ### Endpoint /netdev/default_interface ### Parameters None ### Request Example None ### Response #### Success Response (200) - **interface_name** (string) - The name of the default network interface. #### Response Example { "interface_name": "eth0" } ``` -------------------------------- ### Ipv6Net - Constructors Source: https://docs.rs/netdev/0.39.0/netdev/prelude/struct.Ipv6Net_search=std%3A%3Avec Provides methods to create new Ipv6Net instances, either from an IP address and prefix length, or from an IP address and netmask. ```APIDOC ## Ipv6Net An IPv6 network address. ### `new(ip: Ipv6Addr, prefix_len: u8) -> Result` Creates a new IPv6 network address from an `Ipv6Addr` and prefix length. **Parameters** * `ip` (Ipv6Addr) - The IPv6 address. * `prefix_len` (u8) - The prefix length. **Returns** * `Result` - A `Result` containing the `Ipv6Net` on success, or a `PrefixLenError` if the prefix length is invalid. ### `new_assert(ip: Ipv6Addr, prefix_len: u8) -> Ipv6Net` Creates a new IPv6 network address from an `Ipv6Addr` and prefix length. This function will panic at runtime if the prefix length is not less than or equal to 128. **Parameters** * `ip` (Ipv6Addr) - The IPv6 address. * `prefix_len` (u8) - The prefix length. **Returns** * `Ipv6Net` - The created `Ipv6Net` instance. ### `with_netmask(ip: Ipv6Addr, netmask: Ipv6Addr) -> Result` Creates a new IPv6 network address from an `Ipv6Addr` and netmask. **Parameters** * `ip` (Ipv6Addr) - The IPv6 address. * `netmask` (Ipv6Addr) - The netmask. **Returns** * `Result` - A `Result` containing the `Ipv6Net` on success, or a `PrefixLenError` if the resulting prefix length is invalid. ``` -------------------------------- ### Get Network Interfaces Source: https://docs.rs/netdev/0.39.0/netdev/interface/fn.get_interfaces_search=std%3A%3Avec Retrieves a list of all available network interfaces on the system. ```APIDOC ## GET /interfaces ### Description Get a list of available Network Interfaces. ### Method GET ### Endpoint /interfaces ### Parameters None ### Request Example None ### Response #### Success Response (200) - **interfaces** (Array[Interface]) - A list of network interface objects. ```json { "interfaces": [ { "name": "eth0", "mac_address": "00:1A:2B:3C:4D:5E", "ip_addresses": ["192.168.1.100/24"] }, { "name": "lo", "mac_address": null, "ip_addresses": ["127.0.0.1/8"] } ] } ``` #### Error Response (500) - **error** (string) - An error message if the interfaces could not be retrieved. ``` -------------------------------- ### Create Ipv4Net with Compile-Time/Runtime Checks (Rust) Source: https://docs.rs/netdev/0.39.0/netdev/prelude/struct.Ipv4Net_search=std%3A%3Avec Illustrates `new_assert` for creating `Ipv4Net`. It supports compile-time verification of prefix length in const contexts and runtime panics for invalid lengths in other contexts. ```Rust use std::net::Ipv4Addr; use ipnet::{Ipv4Net}; // This code is verified at compile time: const NET: Ipv4Net = Ipv4Net::new_assert(Ipv4Addr::new(10, 1, 1, 0), 24); assert_eq!(NET.prefix_len(), 24); // This code is verified at runtime: let net = Ipv4Net::new_assert(Ipv4Addr::new(10, 1, 1, 0), 24); assert_eq!(NET.prefix_len(), 24); ``` -------------------------------- ### Get Network Interfaces Source: https://docs.rs/netdev/0.39.0/netdev/interface/fn.get_interfaces_search= Retrieves a list of all available network interfaces on the system. ```APIDOC ## GET /interfaces ### Description Get a list of available Network Interfaces. ### Method GET ### Endpoint /interfaces ### Parameters #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) - **interfaces** (Vec) - A vector containing Interface objects. #### Response Example ```json [ { "name": "eth0", "mac_address": "00:1A:2B:3C:4D:5E", "ipv4_addresses": ["192.168.1.100/24"], "ipv6_addresses": ["2001:0db8:85a3:0000:0000:8a2e:0370:7334/64"] }, { "name": "lo", "mac_address": "00:00:00:00:00:00", "ipv4_addresses": ["127.0.0.1/8"], "ipv6_addresses": ["::1/128"] } ] ``` ``` -------------------------------- ### Generic Blanket Implementations for MacAddr Source: https://docs.rs/netdev/0.39.0/netdev/net/mac/struct.MacAddr Documentation for blanket implementations that apply to generic types, including MacAddr. ```APIDOC ### impl Any for T #### fn type_id(&self) -> TypeId ### Description Returns the `TypeId` of the given type. ### impl Borrow for T #### fn borrow(&self) -> &T ### Description Immutably borrows from an owned value. ### impl BorrowMut for T #### fn borrow_mut(&mut self) -> &mut T ### Description Mutably borrows from an owned value. ### impl CloneToUninit for T #### unsafe fn clone_to_uninit(&self, dest: *mut u8) ### Description Performs copy-assignment from `self` to `dest`. This is a nightly-only experimental API. ### impl From for T #### fn from(t: T) -> T ### Description Returns the argument unchanged. ### impl Into for T #### fn into(self) -> U ### Description Calls `U::from(self)`. ### impl ToOwned for T #### type Owned = T #### fn to_owned(&self) -> T #### fn clone_into(&self, target: &mut T) ### Description Creates owned data from borrowed data, usually by cloning, or uses borrowed data to replace owned data. ### impl ToString for T #### fn to_string(&self) -> String ### Description Converts the given value to a `String`. ### impl TryFrom for T #### type Error = Infallible #### fn try_from(value: U) -> Result>::Error> ### Description Performs the conversion. ### impl TryInto for T #### type Error = >::Error #### fn try_into(self) -> Result>::Error> ### Description Performs the conversion. ### Method N/A (Generic Blanket Implementations) ### Endpoint N/A ### Parameters None ### Request Example None ### Response None ``` -------------------------------- ### Get Default Interface Source: https://docs.rs/netdev/0.39.0/netdev/interface/fn.get_default_interface_search=u32+-%3E+bool Retrieves the default network interface configured on the system. ```APIDOC ## GET /interface/default ### Description Retrieves the default network interface configured on the system. ### Method GET ### Endpoint /interface/default ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (200) - **interface** (Interface) - An object representing the default network interface. #### Response Example ```json { "interface": { "name": "eth0", "mac_address": "00:1A:2B:3C:4D:5E", "ipv4": [ { "address": "192.168.1.100", "netmask": "255.255.255.0", "broadcast": "192.168.1.255" } ], "ipv6": [ { "address": "::1", "prefix_length": 128 } ] } } ``` ``` -------------------------------- ### MacAddr Hashing and Ordering Implementations - Rust Source: https://docs.rs/netdev/0.39.0/netdev/net/mac/struct.MacAddr_search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Demonstrates the implementations for `Hash` and ordering traits (`Ord`, `PartialOrd`) for MacAddr. These allow MacAddr instances to be used in hash-based collections and compared with each other. ```rust impl Hash for MacAddr impl Ord for MacAddr impl PartialOrd for MacAddr ``` -------------------------------- ### Get Interface Operational State Source: https://docs.rs/netdev/0.39.0/netdev/interface/state/fn.operstate_search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Retrieves the current operational state of a specified network interface. ```APIDOC ## GET /netdev/interface/state ### Description Retrieves the operational state of a network interface identified by its name. ### Method GET ### Endpoint /netdev/interface/state ### Parameters #### Query Parameters - **if_name** (string) - Required - The name of the network interface (e.g., "eth0"). ### Request Example ``` GET /netdev/interface/state?if_name=eth0 ``` ### Response #### Success Response (200) - **OperState** (enum) - The operational state of the interface. Possible values include Up, Down, Unknown, etc. #### Response Example ```json { "state": "Up" } ``` ``` -------------------------------- ### Create Ipv4Net with IP and Prefix Length (Assert) Source: https://docs.rs/netdev/0.39.0/netdev/prelude/struct.Ipv4Net Demonstrates `new_assert` for creating an Ipv4Net, which verifies the prefix length at compile time in const contexts or panics at runtime otherwise. This is useful for compile-time checks or immediate error feedback. ```rust use std::net::Ipv4Addr; use ipnet::{Ipv4Net}; // This code is verified at compile time: const NET: Ipv4Net = Ipv4Net::new_assert(Ipv4Addr::new(10, 1, 1, 0), 24); assert_eq!(NET.prefix_len(), 24); // This code is verified at runtime: let net = Ipv4Net::new_assert(Ipv4Addr::new(10, 1, 1, 0), 24); assert_eq!(NET.prefix_len(), 24); // This code does not compile: // const BAD_PREFIX_LEN: Ipv4Net = Ipv4Net::new_assert(Ipv4Addr::new(10, 1, 1, 0), 33); // This code panics at runtime: // let bad_prefix_len = Ipv4Net::new_assert(Ipv4Addr::new(10, 1, 1, 0), 33); ``` -------------------------------- ### Get Global IPv6 Addresses Source: https://docs.rs/netdev/0.39.0/src/netdev/interface/interface.rs Retrieves a list of all globally routable IPv6 addresses assigned to the network interface. ```APIDOC ## GET /interfaces/{id}/global-ipv6-addresses ### Description Returns a list of globally routable IPv6 addresses assigned to this interface. ### Method GET ### Endpoint `/interfaces/{id}/global-ipv6-addresses` ### Parameters #### Path Parameters - **id** (string) - Required - The unique identifier of the network interface. ### Request Example ```json {} ``` ### Response #### Success Response (200) - **addresses** (array[string]) - A list of globally routable IPv6 address strings. #### Response Example ```json { "addresses": [ "2606:4700:4700::1111", "2001:4860:4860::8888" ] } ``` ``` -------------------------------- ### MacAddr Comparison Source: https://docs.rs/netdev/0.39.0/netdev/net/mac/struct.MacAddr_search= Provides methods for comparing MacAddr instances. ```APIDOC ## GET /macaddr/compare ### Description Compares two MacAddr instances to determine their relationship. ### Method GET ### Endpoint /macaddr/compare ### Parameters #### Query Parameters - **self** (MacAddr) - Required - The first MacAddr instance. - **other** (MacAddr) - Required - The second MacAddr instance. ### Response #### Success Response (200) - **result** (bool) - True if `self` is greater than or equal to `other`, false otherwise. #### Response Example ```json { "result": true } ``` ``` -------------------------------- ### Get Global IPv4 Addresses Source: https://docs.rs/netdev/0.39.0/src/netdev/interface/interface.rs Retrieves a list of all globally routable IPv4 addresses assigned to the network interface. ```APIDOC ## GET /interfaces/{id}/global-ipv4-addresses ### Description Returns a list of globally routable IPv4 addresses assigned to this interface. ### Method GET ### Endpoint `/interfaces/{id}/global-ipv4-addresses` ### Parameters #### Path Parameters - **id** (string) - Required - The unique identifier of the network interface. ### Request Example ```json {} ``` ### Response #### Success Response (200) - **addresses** (array[string]) - A list of globally routable IPv4 address strings. #### Response Example ```json { "addresses": [ "1.1.1.1", "8.8.8.8" ] } ``` ``` -------------------------------- ### Network Interface Management Source: https://docs.rs/netdev/0.39.0/netdev/prelude/index_search=std%3A%3Avec This section provides functions for retrieving information about network interfaces, including getting all interfaces, the default interface, and their operational states. ```APIDOC ## GET /interfaces ### Description Retrieves a list of all network interfaces on the system. ### Method GET ### Endpoint /interfaces ### Parameters None ### Request Example None ### Response #### Success Response (200) - **interfaces** (array) - A list of network interface objects. - **name** (string) - The name of the interface. - **type** (string) - The type of the interface (e.g., Ethernet, Loopback). - **state** (string) - The operational state of the interface (e.g., Up, Down). #### Response Example ```json { "interfaces": [ { "name": "eth0", "type": "Ethernet", "state": "Up" }, { "name": "lo", "type": "Loopback", "state": "Up" } ] } ``` ## GET /interfaces/default ### Description Retrieves the default network interface. ### Method GET ### Endpoint /interfaces/default ### Parameters None ### Request Example None ### Response #### Success Response (200) - **interface** (object) - The default network interface object. - **name** (string) - The name of the interface. - **type** (string) - The type of the interface. - **state** (string) - The operational state of the interface. #### Response Example ```json { "interface": { "name": "eth0", "type": "Ethernet", "state": "Up" } } ``` ``` -------------------------------- ### MacAddr Trait Implementations: Basic Source: https://docs.rs/netdev/0.39.0/netdev/prelude/struct.MacAddr Demonstrates core trait implementations for MacAddr, including Clone for duplication, Debug for printing, Default for a default value, and Display for user-friendly string formatting. ```rust impl Clone for MacAddr ``` ```rust impl Debug for MacAddr ``` ```rust impl Default for MacAddr ``` ```rust impl Display for MacAddr ``` -------------------------------- ### MacAddr Basic Trait Implementations - Rust Source: https://docs.rs/netdev/0.39.0/netdev/net/mac/struct.MacAddr_search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Shows common Rust trait implementations for MacAddr, including `Clone` for creating copies, `Debug` for printing, `Default` for a default value (likely the zero address), and `Display` for formatted output. ```rust impl Clone for MacAddr impl Debug for MacAddr impl Default for MacAddr impl Display for MacAddr ``` -------------------------------- ### Get Global IP Addresses Source: https://docs.rs/netdev/0.39.0/src/netdev/interface/interface.rs Retrieves a list of all globally routable IP addresses (both IPv4 and IPv6) assigned to the network interface. ```APIDOC ## GET /interfaces/{id}/global-ip-addresses ### Description Returns a list of globally routable IP addresses (both IPv4 and IPv6) assigned to this interface. ### Method GET ### Endpoint `/interfaces/{id}/global-ip-addresses` ### Parameters #### Path Parameters - **id** (string) - Required - The unique identifier of the network interface. ### Request Example ```json {} ``` ### Response #### Success Response (200) - **addresses** (array[string]) - A list of globally routable IP address strings (IPv4 and IPv6). #### Response Example ```json { "addresses": [ "1.1.1.1", "2606:4700:4700::1111" ] } ``` ``` -------------------------------- ### Get Default Gateway Source: https://docs.rs/netdev/0.39.0/netdev/route/fn.get_default_gateway_search= Retrieves the default gateway for the network device. This function returns a Result which can be either the NetworkDevice information or an error string. ```APIDOC ## GET /network/gateway ### Description Retrieves the default gateway for the network device. ### Method GET ### Endpoint /network/gateway ### Parameters None ### Request Example None ### Response #### Success Response (200) - **networkDevice** (object) - Information about the default network gateway. - **ip_address** (string) - The IP address of the gateway. - **mac_address** (string) - The MAC address of the gateway. - **interface_name** (string) - The name of the network interface. #### Response Example ```json { "ip_address": "192.168.1.1", "mac_address": "00:1A:2B:3C:4D:5E", "interface_name": "eth0" } ``` #### Error Response (500) - **error** (string) - A message describing the error encountered while retrieving the default gateway. ``` -------------------------------- ### Rust Interface Constructor Methods Source: https://docs.rs/netdev/0.39.0/netdev/interface/interface/struct.Interface_search=std%3A%3Avec Provides methods for constructing `Interface` instances. `default()` attempts to create a default interface instance, returning a `Result` to handle potential errors. `dummy()` creates a placeholder or dummy interface instance, likely for testing or simulation purposes. ```rust pub fn default() -> Result pub fn dummy() -> Interface ``` -------------------------------- ### Create Ipv6Net with Compile-time or Runtime Prefix Check - Rust Source: https://docs.rs/netdev/0.39.0/netdev/prelude/struct.Ipv6Net_search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Demonstrates `new_assert` for creating an `Ipv6Net`, which offers compile-time verification of prefix length in const contexts and panics at runtime for invalid lengths otherwise. ```rust use std::net::Ipv6Addr; use ipnet::{Ipv6Net}; // This code is verified at compile time: const NET: Ipv6Net = Ipv6Net::new_assert(Ipv6Addr::new(0xfd, 0, 0, 0, 0, 0, 0, 0), 24); assert_eq!(NET.prefix_len(), 24); // This code is verified at runtime: let net = Ipv6Net::new_assert(Ipv6Addr::new(0xfd, 0, 0, 0, 0, 0, 0, 0), 24); assert_eq!(net.prefix_len(), 24); // This code does not compile: // const BAD_PREFIX_LEN: Ipv6Net = Ipv6Net::new_assert(Ipv6Addr::new(0xfd, 0, 0, 0, 0, 0, 0, 0), 129); // This code panics at runtime: // let bad_prefix_len = Ipv6Addr::new_assert(Ipv6Addr::new(0xfd, 0, 0, 0, 0, 0, 0, 0), 129); ``` -------------------------------- ### Get InterfaceType Name Source: https://docs.rs/netdev/0.39.0/src/netdev/interface/types.rs_search=std%3A%3Avec Returns the human-readable string name for an InterfaceType variant. This function provides a general mapping for all defined InterfaceType variants. ```rust /// Returns name of InterfaceType pub fn name(&self) -> String { match *self { InterfaceType::Unknown => String::from("Unknown"), InterfaceType::Ethernet => String::from("Ethernet"), InterfaceType::TokenRing => String::from("Token Ring"), InterfaceType::Fddi => String::from("FDDI"), InterfaceType::BasicIsdn => String::from("Basic ISDN"), InterfaceType::PrimaryIsdn => String::from("Primary ISDN"), InterfaceType::Ppp => String::from("PPP"), InterfaceType::Loopback => String::from("Loopback"), InterfaceType::Ethernet3Megabit => String::from("Ethernet 3 megabit"), InterfaceType::Slip => String::from("SLIP"), InterfaceType::Atm => String::from("ATM"), InterfaceType::GenericModem => String::from("Generic Modem"), InterfaceType::ProprietaryVirtual => String::from("Proprietary Virtual/Internal"), InterfaceType::FastEthernetT => String::from("Fast Ethernet T"), InterfaceType::Isdn => String::from("ISDN"), InterfaceType::FastEthernetFx => String::from("Fast Ethernet FX"), InterfaceType::Wireless80211 => String::from("Wireless IEEE 802.11"), InterfaceType::AsymmetricDsl => String::from("Asymmetric DSL"), InterfaceType::RateAdaptDsl => String::from("Rate Adaptive DSL"), InterfaceType::SymmetricDsl => String::from("Symmetric DSL"), InterfaceType::VeryHighSpeedDsl => String::from("Very High Data Rate DSL"), InterfaceType::IPOverAtm => String::from("IP over ATM"), InterfaceType::GigabitEthernet => String::from("Gigabit Ethernet"), InterfaceType::Tunnel => String::from("Tunnel"), InterfaceType::MultiRateSymmetricDsl => String::from("Multi-Rate Symmetric DSL"), InterfaceType::HighPerformanceSerialBus => String::from("High Performance Serial Bus"), InterfaceType::Bridge => String::from("Bridge"), InterfaceType::Wman => String::from("WMAN"), InterfaceType::Wwanpp => String::from("WWANPP"), InterfaceType::Wwanpp2 => String::from("WWANPP2"), InterfaceType::Can => String::from("CAN"), } } ``` -------------------------------- ### Rust: Create MacAddr Instance Source: https://docs.rs/netdev/0.39.0/netdev/net/mac/struct.MacAddr_search= Provides methods for constructing MacAddr instances. This includes creating a new MacAddr from individual octets, an array of six octets, parsing from a hex string format, and obtaining special addresses like zero and broadcast. ```rust // Constructs a new `MacAddr` from six octets. MacAddr::new(0x00, 0x11, 0x22, 0x33, 0x44, 0x55); // Constructs from a `[u8; 6]` array. MacAddr::from_octets([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]); // Parses a fixed-width hex string (`xx:xx:xx:xx:xx:xx`). MacAddr::from_hex_format("00:11:22:33:44:55"); // Returns the all-zeros address. MacAddr::zero(); // Returns the broadcast address (`ff:ff:ff:ff:ff:ff`). MacAddr::broadcast(); ``` -------------------------------- ### Get Operational State of Network Interface Source: https://docs.rs/netdev/0.39.0/src/netdev/interface/interface.rs_search=std%3A%3Avec Retrieves the current operational state of the network interface. The state is stored directly in the `oper_state` field. ```Rust pub fn oper_state(&self) -> OperState { self.oper_state } ``` -------------------------------- ### MacAddr Ord and PartialOrd Implementations Source: https://docs.rs/netdev/0.39.0/netdev/struct.MacAddr Provides implementations for `Ord` and `PartialOrd` traits, enabling comparison of MacAddr instances. This includes methods for comparison, finding minimum/maximum, and clamping values. ```rust // This method returns an `Ordering` between `self` and `other`. fn cmp(&self, other: &MacAddr) -> Ordering; // Compares and returns the maximum of two values. fn max(self, other: Self) -> Self where Self: Sized; // Compares and returns the minimum of two values. fn min(self, other: Self) -> Self where Self: Sized; // Restrict a value to a certain interval. fn clamp(self, min: Self, max: Self) -> Self where Self: Sized; // This method returns an ordering between `self` and `other` values if one exists. fn partial_cmp(&self, other: &MacAddr) -> Option; // Tests less than (for `self` and `other`) and is used by the `<` operator. fn lt(&self, other: &Rhs) -> bool; // Tests less than or equal to (for `self` and `other`) and is used by the `<=` operator. fn le(&self, other: &Rhs) -> bool; ``` -------------------------------- ### Getting Host Mask from Ipv6Net Source: https://docs.rs/netdev/0.39.0/netdev/prelude/struct.Ipv6Net_search= Illustrates how to obtain the host mask for an `Ipv6Net` using the `hostmask` method. The host mask is returned as an `Ipv6Addr`. ```rust let net: Ipv6Net = "fd00::/24".parse().unwrap(); assert_eq!(Ok(net.hostmask()), "::ff:ffff:ffff:ffff:ffff:ffff:ffff".parse()); ``` -------------------------------- ### Get Port from SockaddrRef Source: https://docs.rs/netdev/0.39.0/src/netdev/os/unix/sockaddr.rs_search= Retrieves the port number from a `SockaddrRef`, performing big-endian to host byte order conversion. This is applicable to both IPv4 and IPv6 socket addresses. ```rust #[inline] pub(crate) fn port(&self) -> u16 { match self { SockaddrRef::V4(sin) => u16::from_be((*sin).sin_port), SockaddrRef::V6(sin6) => u16::from_be((*sin6).sin6_port), } } ``` -------------------------------- ### Generate Subnets from IPv4 Network Source: https://docs.rs/netdev/0.39.0/netdev/prelude/struct.Ipv4Net_search=std%3A%3Avec Illustrates how to generate subnets of a given IPv4 network with a specified new prefix length. Includes examples of successful subnetting and error handling for invalid prefix lengths. ```rust let net: Ipv4Net = "10.0.0.0/24".parse().unwrap(); assert_eq!(net.subnets(26).unwrap().collect::>(), vec![ "10.0.0.0/26".parse::().unwrap(), "10.0.0.64/26".parse().unwrap(), "10.0.0.128/26".parse().unwrap(), "10.0.0.192/26".parse().unwrap(), ]); let net: Ipv4Net = "10.0.0.0/30".parse().unwrap(); assert_eq!(net.subnets(32).unwrap().collect::>(), vec![ "10.0.0.0/32".parse::().unwrap(), "10.0.0.1/32".parse().unwrap(), "10.0.0.2/32".parse().unwrap(), "10.0.0.3/32".parse().unwrap(), ]); let net: Ipv4Net = "10.0.0.0/24".parse().unwrap(); assert_eq!(net.subnets(23), Err(PrefixLenError)); let net: Ipv4Net = "10.0.0.0/24".parse().unwrap(); assert_eq!(net.subnets(33), Err(PrefixLenError)); ``` -------------------------------- ### Get List of IPv6 Addresses Source: https://docs.rs/netdev/0.39.0/src/netdev/interface/interface.rs_search=std%3A%3Avec Returns a vector containing all IPv6 addresses assigned to the network interface. It processes the `ipv6` field to extract each address. ```Rust pub fn ipv6_addrs(&self) -> Vec { self.ipv6.iter().map(|net| net.addr()).collect() } ``` -------------------------------- ### Get OUI from MacAddr - Rust Source: https://docs.rs/netdev/0.39.0/netdev/struct.MacAddr_search=std%3A%3Avec Extracts the Organizationally Unique Identifier (OUI) from a MAC address. The OUI consists of the first three octets of the address. ```rust pub fn oui(&self) -> [u8; 3]; ``` -------------------------------- ### MacAddr Construction and Conversion Methods Source: https://docs.rs/netdev/0.39.0/netdev/struct.MacAddr Provides methods for creating MacAddr instances from various inputs, including individual octets, arrays, and hex string formats. It also includes methods to retrieve the octets. ```rust // Constructs a new `MacAddr` from six octets. pub fn new(a: u8, b: u8, c: u8, d: u8, e: u8, f: u8) -> MacAddr; // Constructs from a `[u8; 6]` array. pub fn from_octets(octets: [u8; 6]) -> MacAddr; // Returns the 6 octets backing this address. pub fn octets(&self) -> [u8; 6]; // Parses a fixed-width hex string (`xx:xx:xx:xx:xx:xx`). pub fn from_hex_format(hex_mac_addr: &str) -> MacAddr; // Converts to this type from the input type. fn from(v: [u8; 6]) -> MacAddr; ``` -------------------------------- ### Getting Network Mask from Ipv6Net Source: https://docs.rs/netdev/0.39.0/netdev/prelude/struct.Ipv6Net_search= Demonstrates retrieving the network mask associated with an `Ipv6Net` instance using the `netmask` method. The mask is returned as an `Ipv6Addr`. ```rust let net: Ipv6Net = "fd00::/24".parse().unwrap(); assert_eq!(Ok(net.netmask()), "ffff:ff00::".parse()); ``` -------------------------------- ### Create Ipv6Net from Address and Prefix Length - Rust Source: https://docs.rs/netdev/0.39.0/netdev/prelude/struct.Ipv6Net_search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Shows how to create a new IPv6 network address using an `Ipv6Addr` and a prefix length. It also illustrates error handling for invalid prefix lengths. ```rust use std::net::Ipv6Addr; use ipnet::{Ipv6Net, PrefixLenError}; let net = Ipv6Net::new(Ipv6Addr::new(0xfd, 0, 0, 0, 0, 0, 0, 0), 24); assert!(net.is_ok()); let bad_prefix_len = Ipv6Net::new(Ipv6Addr::new(0xfd, 0, 0, 0, 0, 0, 0, 0), 129); assert_eq!(bad_prefix_len, Err(PrefixLenError)); ``` -------------------------------- ### Get Ipv4Net Network Address (Rust) Source: https://docs.rs/netdev/0.39.0/netdev/prelude/struct.Ipv4Net_search=std%3A%3Avec Extracts the network address from an `Ipv4Net`. This is the first address in the network range, typically used to identify the network itself. ```Rust let net: Ipv4Net = "172.16.123.123/16".parse().unwrap(); assert_eq!(Ok(net.network()), "172.16.0.0".parse()); ```