Skip to main content

relibc/header/ifaddrs/
mod.rs

1//! `ifaddrs.h` implementation.
2//!
3//! Non-POSIX, see <https://www.man7.org/linux/man-pages/man3/getifaddrs.3.html>.
4
5use crate::{
6    header::{errno, stdlib, sys_socket::sockaddr},
7    platform::{
8        self,
9        types::{c_char, c_int, c_uint, c_void},
10    },
11};
12
13/// Either the broadcast address associated with `ifa_addr` (if applicable
14/// for the address family) or the destination address of the
15/// point-to-point interface.
16#[repr(C)]
17union ifaddrs_ifa_ifu {
18    /// Broadcast address of interface.
19    ifu_broadaddr: *mut sockaddr,
20    /// Point-to-point destination address.
21    ifu_dstaddr: *mut sockaddr,
22}
23
24/// An entry in a linked list describing the network interfaces of the local
25/// system.
26#[repr(C)]
27pub struct ifaddrs {
28    /// Next item in list.
29    ifa_next: *mut ifaddrs,
30    /// Name of interface.
31    ifa_name: *mut c_char,
32    /// Flags from `SIOCGIFFLAGS`.
33    ifa_flags: c_uint,
34    /// Address of interface.
35    ifa_addr: *mut sockaddr,
36    /// Netmask of interface.
37    ifa_netmask: *mut sockaddr,
38    /// Depends on the bit `IFF_BROADCAST` or `IFF_POINTOPOINT` being set in
39    /// `ifa_flags`. The bits are mutually exclusive.
40    ifa_ifu: ifaddrs_ifa_ifu,
41    /// Address-specific data.
42    ifa_data: *mut c_void,
43}
44
45/// Frees the dynamically allocated memory used by `ifa`.
46#[unsafe(no_mangle)]
47pub unsafe extern "C" fn freeifaddrs(mut ifa: *mut ifaddrs) {
48    while !ifa.is_null() {
49        let next = unsafe { (*ifa).ifa_next };
50        unsafe { stdlib::free(ifa.cast()) };
51        ifa = next;
52    }
53}
54
55/// Creates a linked list of structures describing the network interfaces of
56/// the local system, and stores the address of the first item of the list
57/// in `ifap`.
58///
59/// The data returned by `getifaddrs()` is dynamically allocated and should
60/// be freed using `freeifaddrs()` when no longer needed.
61#[unsafe(no_mangle)]
62pub unsafe extern "C" fn getifaddrs(ifap: *mut *mut ifaddrs) -> c_int {
63    //TODO: implement getifaddrs
64    platform::ERRNO.set(errno::ENOSYS);
65    -1
66}