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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
// Copyright 2015 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under (1) the MaidSafe.net Commercial License,
// version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
// licence you accepted on initial access to the Software (the "Licences").
//
// By contributing code to the SAFE Network Software, or to this project generally, you agree to be
// bound by the terms of the MaidSafe Contributor Agreement, version 1.0.  This, along with the
// Licenses can be found in the root directory of this project at LICENSE, COPYING and CONTRIBUTOR.
//
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.
//
// Please review the Licences for the specific language governing permissions and limitations
// relating to use of the SAFE Network Software.

//! #get_if_addrs
//! Retrieve interface IP addresses in windows and posix systems

#![doc(html_logo_url =
           "https://raw.githubusercontent.com/maidsafe/QA/master/Images/maidsafe_logo.png",
       html_favicon_url = "http://maidsafe.net/img/favicon.ico",
       html_root_url = "http://maidsafe.github.io/get_if_addrs/")]

// For explanation of lint checks, run `rustc -W help` or see
// https://github.com/maidsafe/QA/blob/master/Documentation/Rust%20Lint%20Checks.md
#![forbid(bad_style, exceeding_bitshifts, mutable_transmutes, no_mangle_const_items,
          unknown_crate_types, warnings)]
#![deny(deprecated, drop_with_repr_extern, improper_ctypes, missing_docs,
        non_shorthand_field_patterns, overflowing_literals, plugin_as_library,
        private_no_mangle_fns, private_no_mangle_statics, stable_features,
        unconditional_recursion, unknown_lints, unsafe_code, unused, unused_allocation,
        unused_attributes, unused_comparisons, unused_features, unused_parens, while_true)]
#![warn(trivial_casts, trivial_numeric_casts, unused_extern_crates, unused_import_braces,
        unused_qualifications, unused_results, variant_size_differences)]
#![allow(box_pointers, fat_ptr_transmutes, missing_copy_implementations,
         missing_debug_implementations)]

extern crate ip;
extern crate libc;

use std::net::Ipv4Addr;
use ip::IpAddr;

/// Details about an interface on this host
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct IfAddr {
    /// The name of the interface
    pub name: String,
    /// The IP address of the interface
    pub addr: IpAddr,
    /// The netmask of the interface
    pub netmask: IpAddr,
    /// How to send a broadcast on the interface
    pub broadcast: IpAddr,
}

impl IfAddr {
    /// Create a new IfAddr
    pub fn new() -> IfAddr {
        IfAddr {
            name: String::new(),
            addr: IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
            netmask: IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
            broadcast: IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
        }
    }
}

#[cfg(not(windows))]
mod getifaddrs_posix {
    use super::IfAddr;
    use std::net::{Ipv4Addr, Ipv6Addr};
    use ip::IpAddr;
    use std::{mem, str};
    use std::ffi::CStr;
    use libc::consts::os::bsd44::{AF_INET, AF_INET6};
    use libc::funcs::bsd43::getifaddrs as posix_getifaddrs;
    use libc::funcs::bsd43::freeifaddrs as posix_freeifaddrs;
    use libc::types::os::common::bsd44::ifaddrs as posix_ifaddrs;
    use libc::types::os::common::bsd44::sockaddr as posix_sockaddr;
    use libc::types::os::common::bsd44::sockaddr_in as posix_sockaddr_in;
    use libc::types::os::common::bsd44::sockaddr_in6 as posix_sockaddr_in6;

    #[allow(unsafe_code)]
    fn sockaddr_to_ipaddr(sockaddr: *const posix_sockaddr) -> Option<IpAddr> {
        if sockaddr.is_null() {
            return None;
        }
        if unsafe { *sockaddr }.sa_family as u32 == AF_INET as u32 {
            let sa = &unsafe { *(sockaddr as *const posix_sockaddr_in) };
            Some(IpAddr::V4(Ipv4Addr::new(((sa.sin_addr.s_addr) & 255) as u8,
                                          ((sa.sin_addr.s_addr >> 8) & 255) as u8,
                                          ((sa.sin_addr.s_addr >> 16) & 255) as u8,
                                          ((sa.sin_addr.s_addr >> 24) & 255) as u8)))
        } else if unsafe { *sockaddr }.sa_family as u32 == AF_INET6 as u32 {
            let sa = &unsafe { *(sockaddr as *const posix_sockaddr_in6) };
            // Ignore all fe80:: addresses as these are link locals
            if sa.sin6_addr.s6_addr[0] == 0x80fe {
                return None;
            }
            Some(IpAddr::V6(Ipv6Addr::new(((sa.sin6_addr.s6_addr[0] & 255) << 8) |
                                          ((sa.sin6_addr.s6_addr[0] >> 8) & 255),
                                          ((sa.sin6_addr.s6_addr[1] & 255) << 8) |
                                          ((sa.sin6_addr.s6_addr[1] >> 8) & 255),
                                          ((sa.sin6_addr.s6_addr[2] & 255) << 8) |
                                          ((sa.sin6_addr.s6_addr[2] >> 8) & 255),
                                          ((sa.sin6_addr.s6_addr[3] & 255) << 8) |
                                          ((sa.sin6_addr.s6_addr[3] >> 8) & 255),
                                          ((sa.sin6_addr.s6_addr[4] & 255) << 8) |
                                          ((sa.sin6_addr.s6_addr[4] >> 8) & 255),
                                          ((sa.sin6_addr.s6_addr[5] & 255) << 8) |
                                          ((sa.sin6_addr.s6_addr[5] >> 8) & 255),
                                          ((sa.sin6_addr.s6_addr[6] & 255) << 8) |
                                          ((sa.sin6_addr.s6_addr[6] >> 8) & 255),
                                          ((sa.sin6_addr.s6_addr[7] & 255) << 8) |
                                          ((sa.sin6_addr.s6_addr[7] >> 8) & 255))))
        } else {
            None
        }
    }

    #[cfg(any(target_os = "linux", target_os = "android", target_os = "nacl"))]
    fn do_broadcast(ifaddr: &posix_ifaddrs) -> IpAddr {
        sockaddr_to_ipaddr(ifaddr.ifa_ifu).unwrap_or(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)))
    }

    #[cfg(any(target_os = "freebsd", target_os = "macos", target_os = "ios"))]
    fn do_broadcast(ifaddr: &posix_ifaddrs) -> IpAddr {
        sockaddr_to_ipaddr(ifaddr.ifa_dstaddr).unwrap_or(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)))
    }

    /// Return a vector of IP details for all the valid interfaces on this host
    #[allow(unsafe_code)]
    pub fn getifaddrs() -> Vec<IfAddr> {
        let mut ret = Vec::<IfAddr>::new();
        let mut ifaddrs: *mut posix_ifaddrs;
        unsafe {
            ifaddrs = mem::uninitialized();
            if -1 == posix_getifaddrs(&mut ifaddrs) {
                panic!("failed to retrieve interface details from getifaddrs()");
            }
        }

        let mut ifaddr = ifaddrs;
        let mut first = true;
        while !ifaddr.is_null() {
            if first {
                first = false;
            } else {
                ifaddr = unsafe { (*ifaddr).ifa_next };
            }
            if ifaddr.is_null() {
                break;
            }
            let ifaddr = &unsafe { *ifaddr };
            // debug!("ifaddr1={}, next={}", ifaddr as u64, ifaddr.ifa_next as u64);
            if ifaddr.ifa_addr.is_null() {
                continue;
            }
            let mut item = IfAddr::new();
            let name = unsafe { CStr::from_ptr(ifaddr.ifa_name) }.to_bytes();
            item.name = item.name + str::from_utf8(name).unwrap();
            match sockaddr_to_ipaddr(ifaddr.ifa_addr) {
                Some(a) => item.addr = a,
                None => continue,
            };
            if let Some(a) = sockaddr_to_ipaddr(ifaddr.ifa_netmask) {
                item.netmask = a
            };
            if (ifaddr.ifa_flags & 2) != 0 {
                item.broadcast = do_broadcast(ifaddr);
            }
            ret.push(item);
        }
        unsafe {
            posix_freeifaddrs(ifaddrs);
        }
        ret
    }
}

/// For non-Windows operating system, use this function to get address
#[cfg(not(windows))]
pub fn getifaddrs() -> Vec<IfAddr> {
    getifaddrs_posix::getifaddrs()
}

#[cfg(windows)]
mod getifaddrs_windows {
    use super::IfAddr;
    use std::net::{Ipv4Addr, Ipv6Addr};
    use ip::IpAddr;
    use std::{str, ptr};
    use std::ffi::CStr;
    use libc::types::common::c95::c_void;
    use libc::types::os::arch::c95::{c_char, c_ulong, size_t, c_int};
    use libc::types::os::arch::extra::*;   // libc source code says this is all the Windows integral types
    use libc::consts::os::extra::*;        // win32 status code, constants etc
    use libc::consts::os::bsd44::*;        // the winsock constants
    use libc::types::os::common::bsd44::*; // the winsock types
    use libc;

    #[repr(C)]
    struct SocketAddress {
        pub lp_socket_address: *const sockaddr,
        pub i_socket_address_length: c_int,
    }
    #[repr(C)]
    struct IpAdapterUnicastAddress {
        pub length: c_ulong,
        pub flags: DWORD,
        pub next: *const IpAdapterUnicastAddress,
        // Loads more follows, but I'm not bothering to map these for now
        pub address: SocketAddress,
    }
    #[repr(C)]
    struct IpAdapterPrefix {
        pub length: c_ulong,
        pub flags: DWORD,
        pub next: *const IpAdapterPrefix,
        pub address: SocketAddress,
        pub prefix_length: c_ulong,
    }
    #[repr(C)]
    struct IpAdapterAddresses {
        pub length: c_ulong,
        pub if_index: DWORD,
        pub next: *const IpAdapterAddresses,
        pub adapter_name: *const c_char,
        pub first_unicast_address: *const IpAdapterUnicastAddress,
        first_anycast_address: *const c_void,
        first_multicast_address: *const c_void,
        first_dns_server_address: *const c_void,
        dns_suffix: *const c_void,
        description: *const c_void,
        friendly_name: *const c_void,
        physical_address: [c_char; 8],
        physical_address_length: DWORD,
        flags: DWORD,
        mtu: DWORD,
        if_type: DWORD,
        oper_status: c_int,
        ipv6_if_index: DWORD,
        zone_indices: [DWORD; 16],
        // Loads more follows, but I'm not bothering to map these for now
        pub first_prefix: *const IpAdapterPrefix,
    }
    #[link(name="Iphlpapi")]
    extern "system" {
        /// get adapter's addresses
        fn GetAdaptersAddresses(family: c_ulong,
                                flags: c_ulong,
                                reserved: *const c_void,
                                addresses: *const IpAdapterAddresses,
                                size: *mut c_ulong)
                                -> c_ulong;
    }

    #[allow(unsafe_code)]
    fn sockaddr_to_ipaddr(sockaddr: *const sockaddr) -> Option<IpAddr> {
        if sockaddr.is_null() {
            return None;
        }
        if unsafe { *sockaddr }.sa_family as u32 == AF_INET as u32 {
            let ref sa = unsafe { *(sockaddr as *const sockaddr_in) };
            // Ignore all 169.254.x.x addresses as these are not active interfaces
            if sa.sin_addr.s_addr & 65535 == 0xfea9 {
                return None;
            }
            Some(IpAddr::V4(Ipv4Addr::new(((sa.sin_addr.s_addr >> 0) & 255) as u8,
                                          ((sa.sin_addr.s_addr >> 8) & 255) as u8,
                                          ((sa.sin_addr.s_addr >> 16) & 255) as u8,
                                          ((sa.sin_addr.s_addr >> 24) & 255) as u8)))
        } else if unsafe { *sockaddr }.sa_family as u32 == AF_INET6 as u32 {
            let ref sa = unsafe { *(sockaddr as *const sockaddr_in6) };
            // Ignore all fe80:: addresses as these are link locals
            if sa.sin6_addr.s6_addr[0] == 0x80fe {
                return None;
            }
            Some(IpAddr::V6(Ipv6Addr::new(((sa.sin6_addr.s6_addr[0] & 255) << 8) |
                                          ((sa.sin6_addr.s6_addr[0] >> 8) & 255),
                                          ((sa.sin6_addr.s6_addr[1] & 255) << 8) |
                                          ((sa.sin6_addr.s6_addr[1] >> 8) & 255),
                                          ((sa.sin6_addr.s6_addr[2] & 255) << 8) |
                                          ((sa.sin6_addr.s6_addr[2] >> 8) & 255),
                                          ((sa.sin6_addr.s6_addr[3] & 255) << 8) |
                                          ((sa.sin6_addr.s6_addr[3] >> 8) & 255),
                                          ((sa.sin6_addr.s6_addr[4] & 255) << 8) |
                                          ((sa.sin6_addr.s6_addr[4] >> 8) & 255),
                                          ((sa.sin6_addr.s6_addr[5] & 255) << 8) |
                                          ((sa.sin6_addr.s6_addr[5] >> 8) & 255),
                                          ((sa.sin6_addr.s6_addr[6] & 255) << 8) |
                                          ((sa.sin6_addr.s6_addr[6] >> 8) & 255),
                                          ((sa.sin6_addr.s6_addr[7] & 255) << 8) |
                                          ((sa.sin6_addr.s6_addr[7] >> 8) & 255))))
        } else {
            None
        }
    }

    // trivial_numeric_casts lint may become allow by default.
    // Refer: https://github.com/rust-lang/rfcs/issues/1020
    /// Return a vector of IP details for all the valid interfaces on this host
    #[allow(unsafe_code, trivial_numeric_casts)]
    pub fn getifaddrs() -> Vec<IfAddr> {
        let mut ret = Vec::<IfAddr>::new();
        let mut ifaddrs: *const IpAdapterAddresses;
        let mut buffersize: c_ulong = 15000;
        loop {
            unsafe {
                ifaddrs = libc::malloc(buffersize as size_t) as *mut IpAdapterAddresses;
                if ifaddrs.is_null() {
                    panic!("Failed to allocate buffer in getifaddrs()");
                }
                let retcode =
                    GetAdaptersAddresses(0,
                                         // GAA_FLAG_SKIP_ANYCAST       |
                                         // GAA_FLAG_SKIP_MULTICAST     |
                                         // GAA_FLAG_SKIP_DNS_SERVER    |
                                         // GAA_FLAG_INCLUDE_PREFIX     |
                                         // GAA_FLAG_SKIP_FRIENDLY_NAME
                                         0x3e,
                                         ptr::null(),
                                         ifaddrs,
                                         &mut buffersize) as c_int;
                match retcode {
                    ERROR_SUCCESS => break,
                    111 => {
                        libc::free(ifaddrs as *mut c_void);
                        buffersize = buffersize * 2;
                        continue;
                    }
                    _ => panic!("GetAdaptersAddresses() failed with error code {}", retcode),
                }
            }
        }

        let mut ifaddr = ifaddrs;
        let mut first = true;
        while !ifaddr.is_null() {
            if first {
                first = false;
            } else {
                ifaddr = unsafe { (*ifaddr).next };
            }
            if ifaddr.is_null() {
                break;
            }
            let ref ifaddr = unsafe { &*ifaddr };
            // debug!("ifaddr1={}, next={}", ifaddr as u64, ifaddr.ifa_next as u64);

            let mut addr = ifaddr.first_unicast_address;
            if addr.is_null() {
                continue;
            }
            let mut firstaddr = true;
            while !addr.is_null() {
                if firstaddr {
                    firstaddr = false;
                } else {
                    addr = unsafe { (*addr).next };
                }
                if addr.is_null() {
                    break;
                }

                let mut item = IfAddr::new();
                let name = unsafe { CStr::from_ptr(ifaddr.adapter_name) }.to_bytes();
                item.name = item.name + str::from_utf8(name).unwrap();

                let ipaddr = sockaddr_to_ipaddr(unsafe { (*addr).address.lp_socket_address });
                if !ipaddr.is_some() {
                    continue;
                }
                item.addr = ipaddr.unwrap();

                // Search prefixes for a prefix matching addr
                let mut prefix = ifaddr.first_prefix;
                if !prefix.is_null() {
                    let mut first_prefix = true;
                    'prefixloop: while !prefix.is_null() {
                        if first_prefix {
                            first_prefix = false;
                        } else {
                            prefix = unsafe { (*prefix).next };
                        }
                        if prefix.is_null() {
                            break;
                        }

                        let ipprefix = sockaddr_to_ipaddr(unsafe {
                            (*prefix).address.lp_socket_address
                        });
                        if !ipprefix.is_some() {
                            continue;
                        }
                        match ipprefix.unwrap() {
                            IpAddr::V4(ref a) => {
                                if let IpAddr::V4(b) = item.addr {
                                    let mut netmask: [u8; 4] = [0; 4];
                                    for n in 0..unsafe { (*prefix).prefix_length as usize + 7 } /
                                                8 {
                                        let x_byte = b.octets()[n];
                                        let y_byte = a.octets()[n];
                                        for m in 0..8 {
                                            if (n * 8) + m >
                                               unsafe { (*prefix).prefix_length as usize } {
                                                break;
                                            }
                                            let bit = 1 << m;
                                            if (x_byte & bit) == (y_byte & bit) {
                                                netmask[n] = netmask[n] | bit;
                                            } else {
                                                continue 'prefixloop;
                                            }
                                        }
                                    }
                                    item.netmask = IpAddr::V4(Ipv4Addr::new(netmask[0],
                                                                            netmask[1],
                                                                            netmask[2],
                                                                            netmask[3]));
                                    let mut broadcast: [u8; 4] = b.octets();
                                    for n in 0..4 {
                                        broadcast[n] = broadcast[n] | !netmask[n];
                                    }
                                    item.broadcast = IpAddr::V4(Ipv4Addr::new(broadcast[0],
                                                                              broadcast[1],
                                                                              broadcast[2],
                                                                              broadcast[3]));
                                    break 'prefixloop;
                                }
                            }
                            IpAddr::V6(ref a) => {
                                if let IpAddr::V6(b) = item.addr {
                                    // Iterate the bits in the prefix, if they all match this prefix
                                    // is the right one, else try the next prefix
                                    let mut netmask: [u16; 8] = [0; 8];
                                    for n in 0..unsafe { (*prefix).prefix_length as usize + 15 } /
                                                16 {
                                        let x_word = b.segments()[n];
                                        let y_word = a.segments()[n];
                                        for m in 0..16 {
                                            if (n * 16) + m >
                                               unsafe { (*prefix).prefix_length as usize } {
                                                break;
                                            }
                                            let bit = 1 << m;
                                            if (x_word & bit) == (y_word & bit) {
                                                netmask[n] = netmask[n] | bit;
                                            } else {
                                                continue 'prefixloop;
                                            }
                                        }
                                    }
                                    item.netmask = IpAddr::V6(Ipv6Addr::new(netmask[0],
                                                                            netmask[1],
                                                                            netmask[2],
                                                                            netmask[3],
                                                                            netmask[4],
                                                                            netmask[5],
                                                                            netmask[6],
                                                                            netmask[7]));
                                    break 'prefixloop;
                                }
                            }
                        };
                    }
                }
                ret.push(item);
            }
        }
        unsafe {
            libc::free(ifaddrs as *mut c_void);
        }
        ret
    }
}
#[cfg(windows)]
/// Get address
pub fn getifaddrs() -> Vec<IfAddr> {
    getifaddrs_windows::getifaddrs()
}

fn is_loopback(ip_addr: &IpAddr) -> bool {
    match *ip_addr {
        IpAddr::V4(a) => a.octets()[0] == 127,
        IpAddr::V6(a) => a.segments() == [0, 0, 0, 0, 0, 0, 0, 1],
    }
}

/// Remove loopback address(s)
pub fn filter_loopback(mut ifaddrs: Vec<IfAddr>) -> Vec<IfAddr> {
    ifaddrs.retain(|x| !is_loopback(&x.addr));
    ifaddrs
}

#[cfg(test)]
mod test {
    use super::{getifaddrs, filter_loopback, is_loopback};

    #[test]
    fn test_filter_loopback() {
        let ifaddrs = filter_loopback(getifaddrs());
        for ifaddr in ifaddrs {
            assert!(!is_loopback(&ifaddr.addr));
        }
    }
}