ifcfg 0.1.2

Get network interfaces information
Documentation
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
use std::{
    io::{Result, Error, Write},
    net::{SocketAddr, IpAddr, Ipv4Addr, Ipv6Addr},
    slice::from_raw_parts,
    ffi::CStr,
    ptr,
    mem,
};

use winapi::{
    ctypes::{c_int, wchar_t},
    shared::{
        basetsd::ULONG64,
        minwindef::{ULONG, DWORD},
        winerror::ERROR_SUCCESS,
        ws2def::{AF_INET, AF_INET6, SOCKADDR},
        ws2ipdef::SOCKADDR_IN6_LH,
    },
    um::iptypes::{
        GAA_FLAG_INCLUDE_GATEWAYS
    },
};

use libc::{c_char, c_void, free, malloc, size_t};

const WORKING_BUFFER_SIZEL: size_t = 15000;
const MAX_ADAPTER_ADDRESS_LENGTH: usize = 8;

#[repr(C)]
struct LengthIfIndex {
    length: ULONG,
    ifindex: DWORD,
}

#[repr(C)]
pub struct LengthFlags {
    length: ULONG,
    flags: DWORD,
}

#[repr(C)]
pub struct LengthReserved {
    length: ULONG,
    reserved: DWORD,
}

#[repr(C)]
pub struct SocketAddress {
    lp_sockaddr: *mut SOCKADDR,
    i_sockaddr_length: c_int,
}

#[repr(C)]
pub struct IpAdapterPrefix {
    aol: LengthIfIndex,
    next: *mut IpAdapterPrefix,
    address: SocketAddress,
    prefix_length: ULONG,
}

#[repr(C)]
pub struct IpAdapterUnicastAddress {
    aol: LengthFlags,
    next: *mut IpAdapterUnicastAddress,
    address: SocketAddress,
    prefix_origin: c_int,
    suffix_origin: c_int,
    dad_state: c_int,
    valid_lifetime: ULONG,
    preferred_lifetime: ULONG,
    lease_lifetime: ULONG,
    on_link_prefix_length: u8,
}

#[repr(C)]
pub struct IpAdapterWinsServerAddress {
    aol: LengthReserved,
    next: *mut IpAdapterWinsServerAddress,
    address: SocketAddress,
}

#[repr(C)]
pub struct IpAdapterGatewayAddress {
    aol: LengthReserved,
    next: *mut IpAdapterGatewayAddress,
    address: SocketAddress,
}

#[repr(C)]
pub struct IpAdapterAddresses {
    aol: LengthIfIndex,
    next: *mut IpAdapterAddresses,
    adapter_name: *mut c_char,
    first_unicast_address: *mut IpAdapterUnicastAddress,
    first_anycast_address: *const c_void,
    first_multicast_address: *const c_void,
    first_dns_server_address: *const c_void,
    dns_suffix: *mut wchar_t,
    description: *mut wchar_t,
    friendly_name: *mut wchar_t,
    physical_address: [u8; MAX_ADAPTER_ADDRESS_LENGTH],
    physical_address_length: DWORD,
    flags: DWORD,
    mtu: DWORD,
    if_type: DWORD,
    oper_status: c_int,
    ipv6_if_index: DWORD,
    zone_indices: [DWORD; 16],
    first_prefix: *mut IpAdapterPrefix,
    transmit_link_speed: ULONG64,
    receive_link_speed: ULONG64,
    first_wins_server_address: *mut IpAdapterWinsServerAddress,
    first_gateway_address: *mut IpAdapterGatewayAddress,
}

impl IpAdapterAddresses {
    pub fn name(&self) -> String {
        c_char_array_to_string(self.adapter_name)
    }

    pub fn friendly_name(&self) -> String {
        u16_array_to_string(self.friendly_name)
    }

    pub fn mac_address(&self) -> String {
        physical_address_to_string(
            self.physical_address,
            self.physical_address_length
        )
    }

    pub fn description(&self) -> String {
        u16_array_to_string(self.description)
    }

    pub fn dns_suffix(&self) -> String {
        u16_array_to_string(self.dns_suffix)
    }

    pub fn prefixes(&self) -> PrefixesIterator {
        PrefixesIterator {
            _head: self,
            next: self.first_prefix,
        }
    }

    pub fn unicast_addresses(&self) -> UnicastAddressesIterator {
        UnicastAddressesIterator {
            _head: self,
            next: self.first_unicast_address,
        }
    }
}

#[link(name = "iphlpapi")]
extern "system" {
    fn GetAdaptersAddresses(
        family: ULONG,
        flags: ULONG,
        reserved: *const c_void,
        addresses: *mut IpAdapterAddresses,
        size: *mut ULONG,
    ) -> ULONG;
}

pub struct Adapters {
    inner: *const IpAdapterAddresses,
}

impl Adapters {
    #[allow(unsafe_code)]
    pub fn new() -> Result<Self> {
        let mut buffersize: ULONG = WORKING_BUFFER_SIZEL as ULONG;
        let mut p_adapter: *mut IpAdapterAddresses;
        // let mut buffersize: c_ulong = 15000;
        // let mut ifaddrs: *mut IpAdapterAddresses;

        loop {
            unsafe {
                p_adapter = malloc(buffersize as size_t) as *mut IpAdapterAddresses;
                if p_adapter.is_null() {
                    panic!("Failed to allocate buffer in IfAddrs()");
                }

                let retcode = GetAdaptersAddresses(
                    0,
                    GAA_FLAG_INCLUDE_GATEWAYS as ULONG,
                    ptr::null(),
                    p_adapter,
                    &mut buffersize,
                );

                match retcode {
                    ERROR_SUCCESS => break,
                    111 => {
                        free(p_adapter as *mut c_void);
                        buffersize *= 2;
                        continue;
                    }
                    _ => return Err(Error::last_os_error()),
                }
            }
        }

        Ok(Self { inner: p_adapter })
    }

    pub fn iter(&self) -> IfAddrsIterator {
        IfAddrsIterator {
            _head: self,
            next: self.inner,
        }
    }
}

impl Drop for Adapters {
    #[allow(unsafe_code)]
    fn drop(&mut self) {
        unsafe {
            free(self.inner as *mut c_void);
        }
    }
}

pub struct IfAddrsIterator<'a> {
    _head: &'a Adapters,
    next: *const IpAdapterAddresses,
}

impl<'a> Iterator for IfAddrsIterator<'a> {
    type Item = &'a IpAdapterAddresses;

    #[allow(unsafe_code)]
    fn next(&mut self) -> Option<Self::Item> {
        if self.next.is_null() {
            return None;
        };

        Some(unsafe {
            let result = &*self.next;
            self.next = (*self.next).next;

            result
        })
    }
}

pub struct PrefixesIterator<'a> {
    _head: &'a IpAdapterAddresses,
    next: *const IpAdapterPrefix,
}

impl<'a> Iterator for PrefixesIterator<'a> {
    type Item = &'a IpAdapterPrefix;

    #[allow(unsafe_code)]
    fn next(&mut self) -> Option<Self::Item> {
        if self.next.is_null() {
            return None;
        };

        Some(unsafe {
            let result = &*self.next;
            self.next = (*self.next).next;

            result
        })
    }
}

pub struct UnicastAddressesIterator<'a> {
    _head: &'a IpAdapterAddresses,
    next: *const IpAdapterUnicastAddress,
}

impl<'a> Iterator for UnicastAddressesIterator<'a> {
    type Item = &'a IpAdapterUnicastAddress;

    #[allow(unsafe_code)]
    fn next(&mut self) -> Option<Self::Item> {
        if self.next.is_null() {
            return None;
        };

        Some(unsafe {
            let result = &*self.next;
            self.next = (*self.next).next;

            result
        })
    }
}

fn u16_array_to_string(p: *const u16) -> String {
    use std::char::{decode_utf16, REPLACEMENT_CHARACTER};
    unsafe {
        if p.is_null() {
            return String::new();
        }
        let mut amt = 0usize;
        while !p.add(amt).is_null() && *p.add(amt) != 0u16 {
            amt += 1;
        }
        let u16s = from_raw_parts(p, amt);
        decode_utf16(u16s.iter().cloned())
            .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))
            .collect::<String>()
    }
}

fn c_char_array_to_string(p: *const c_char) -> String {
    unsafe { CStr::from_ptr(p).to_string_lossy().into_owned() }
}

fn physical_address_to_string(array: [u8; 8], length: DWORD) -> String {
    let mut bytes = Vec::with_capacity(length as usize);
    for (idx, b) in array.iter().enumerate().take(length as usize) {
        if idx == 0 {
            write!(&mut bytes, "{:02X}", b).unwrap();
        } else {
            write!(&mut bytes, "-{:02X}", b).unwrap();
        }
    }
    String::from_utf8_lossy(&bytes[..]).into_owned()
}

// This faster than [u8;4], but v6 is slower if use this..
// And the scan() method is slower also.
fn netmask_v4(bits: u8) -> Ipv4Addr {
    let mut i = (0..4).map(|idx| {
        let idx8 = idx << 3;
        match (bits as usize > idx8, bits as usize > idx8 + 8) {
            (true, true) => 255,
            (true, false) => 255u8.wrapping_shl((8 - bits % 8) as u32),
            _ => 0,
        }
    });
    Ipv4Addr::new(
        i.next().unwrap(),
        i.next().unwrap(),
        i.next().unwrap(),
        i.next().unwrap(),
    )
}

fn netmask_v6(bits: u8) -> Ipv6Addr {
    let mut tmp = [0u16; 8];
    (0..8).for_each(|idx| {
        let idx16 = idx << 4;
        match (bits as usize > idx16, bits as usize > idx16 + 16) {
            (true, true) => {
                tmp[idx] = 0xffff;
            }
            (true, false) => {
                tmp[idx] = 0xffffu16.wrapping_shl((16 - bits % 16) as u32);
            }
            _ => {}
        }
    });
    Ipv6Addr::new(
        tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5], tmp[6], tmp[7],
    )
}

fn sockaddr_to_net_socket_addr(sockaddr: *mut SOCKADDR) -> Option<SocketAddr> {
    let s_addr = unsafe { *sockaddr };
    match sockaddr.into() {
        crate::AddressFamily::IPv4 => {
            let s_addr =IpAddr::V4(Ipv4Addr::new(
                s_addr.sa_data[2] as u8,
                s_addr.sa_data[3] as u8,
                s_addr.sa_data[4] as u8,
                s_addr.sa_data[5] as u8,
            ));
            return Some(SocketAddr::new(s_addr, 0))
        },
        crate::AddressFamily::IPv6 => {
            #[allow(clippy::cast_ptr_alignment)]
                let addr6: *const SOCKADDR_IN6_LH = sockaddr as *const SOCKADDR_IN6_LH;
            let mut a: [u8; 16] = unsafe { *std::ptr::read_unaligned(addr6).sin6_addr.u.Byte() };
            a[..].reverse();
            let a: [u16; 8] = unsafe { mem::transmute(a) };
            let addr = IpAddr::V6(Ipv6Addr::new(
                a[7], a[6], a[5], a[4], a[3], a[2], a[1], a[0],
            ));
            Some(SocketAddr::new(addr, 0))
        },
        _ => None,
    }
}

fn get_netmask(bits: u8) -> Option<SocketAddr> {
    let netmask = if bits <=32 {
        Some(IpAddr::V4(netmask_v4(bits)))
    } else if bits <= 128 {
        Some(IpAddr::V6(netmask_v6(bits)))
    } else {
        None
    };
    match netmask {
        Some(n) => Some(SocketAddr::new(n, 0)),
        None => None
    }
}

impl From<*mut SOCKADDR> for crate::AddressFamily {
    fn from(sockaddr: *mut SOCKADDR) -> crate::AddressFamily {
        match unsafe { *sockaddr }.sa_family as i32 {
            AF_INET => crate::AddressFamily::IPv4,
            AF_INET6 => crate::AddressFamily::IPv6,
            value => crate::AddressFamily::Unknown(value),
        }
    }
}

impl From<&IpAdapterUnicastAddress> for crate::InterfaceAddress {
    fn from(address: &IpAdapterUnicastAddress) -> crate::InterfaceAddress {
        crate::InterfaceAddress {
            address_family: address.address.lp_sockaddr.into(),
            address: sockaddr_to_net_socket_addr(address.address.lp_sockaddr),
            mask: get_netmask(address.on_link_prefix_length.clone()),
            hop: None,
        }
    }
}

impl From<&IpAdapterAddresses> for crate::IfCfg {
    fn from(adapter: &IpAdapterAddresses) -> crate::IfCfg {
        crate::IfCfg {
            name: adapter.friendly_name(),
            mac: adapter.mac_address(),
            addresses: adapter.unicast_addresses()
                .map(|addr| addr.into())
                .map(|mut addr: crate::InterfaceAddress| {
                    let first_gw_addr = adapter.first_gateway_address.clone();
                    if !first_gw_addr.is_null() {
                        let gw_sockaddr = unsafe{(*first_gw_addr).address.lp_sockaddr};
                        let gw_addr = sockaddr_to_net_socket_addr(gw_sockaddr).unwrap();
                        match gw_addr.ip() {
                            IpAddr::V4(ipv4) => {
                                if ipv4.is_broadcast() {
                                    addr.hop = Some(crate::Hops::Broadcast(gw_addr));
                                } else {
                                    addr.hop = Some(crate::Hops::Destination(gw_addr));
                                }
                            },
                            _ => {
                                addr.hop = Some(crate::Hops::Destination(gw_addr));
                            }
                        }
                    }
                    addr
                })
                .collect(),
            description: adapter.description().clone()
        }
    }
}