Diff
Not logged in

Differences From Artifact [de0713baf8]:

To Artifact [0caa540bee]:


399
400
401
402
403
404
405
406
407

408
409
410
411
412
413
414
...
416
417
418
419
420
421
422

423
424
425
426
427
428
429
430
...
469
470
471
472
473
474
475

476
477
478
479
480
481
482
483
...
541
542
543
544
545
546
547


548

549
550
551
552
553
554
555
556
557
558
559



560
561
562
563
564
565
566
...
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619



620

621
622
623
624
625
626
627
        }

        for ifaddr in unsafe { CLinkedListConst::from_ptr(ifaddrs, |a| a.next) }.iter() {
            for addr in unsafe {
                        CLinkedListConst::from_ptr(ifaddr.first_unicast_address, |a| a.next)
                    }
                    .iter() {
                let name =
                    unsafe { CStr::from_ptr(ifaddr.adapter_name) }.to_string_lossy().into_owned();


                let addr = match sockaddr_to_ipaddr(addr.address.lp_socket_address) {
                    None => continue,
                    Some(IpAddr::V4(ipv4_addr)) => {
                        let mut item_netmask = Ipv4Addr::new(0, 0, 0, 0);
                        let mut item_broadcast = None;
                        // Search prefixes for a prefix matching addr
................................................................................
                            unsafe { CLinkedListConst::from_ptr(ifaddr.first_prefix, |p| p.next) }
                                .iter() {
                            let ipprefix = sockaddr_to_ipaddr(prefix.address.lp_socket_address);
                            match ipprefix {
                                Some(IpAddr::V4(ref a)) => {
                                    let mut netmask: [u8; 4] = [0; 4];
                                    for (n, netmask_elt) in

                                        netmask.iter_mut()
                                            .enumerate()
                                            .take(((prefix.prefix_length as usize + 7) / 8)) {
                                        let x_byte = ipv4_addr.octets()[n];
                                        let y_byte = a.octets()[n];
                                        for m in 0..8 {
                                            if (n * 8) + m > prefix.prefix_length as usize {
                                                break;
................................................................................
                            let ipprefix = sockaddr_to_ipaddr(prefix.address.lp_socket_address);
                            match ipprefix {
                                Some(IpAddr::V6(ref a)) => {
                                    // 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, netmask_elt) in

                                        netmask.iter_mut()
                                            .enumerate()
                                            .take(((prefix.prefix_length as usize + 15) / 16)) {
                                        let x_word = ipv6_addr.segments()[n];
                                        let y_word = a.segments()[n];
                                        for m in 0..16 {
                                            if (n * 16) + m > prefix.prefix_length as usize {
                                                break;
................................................................................
    use std::thread;
    use std::time::Duration;

    fn list_system_interfaces(cmd: &str, arg: &str) -> String {
        let start_cmd = if arg == "" {
            Command::new(cmd).stdout(Stdio::piped()).spawn()
        } else {


            Command::new(cmd).arg(arg).stdout(Stdio::piped()).spawn()

        };
        let mut process = match start_cmd {
            Err(why) => {
                println!("couldn't start cmd {} : {}", cmd, why.description());
                return "".to_string();
            }
            Ok(process) => process,
        };
        thread::sleep(Duration::from_millis(1000));
        let _ = process.kill();
        let result: Vec<u8> = unwrap!(process.stdout).bytes().map(|x| unwrap!(x)).collect();



        unwrap!(String::from_utf8(result))
    }

    #[cfg(windows)]
    fn list_system_addrs() -> Vec<IpAddr> {
        use std::net::Ipv6Addr;
        list_system_interfaces("ipconfig", "")
................................................................................
    }

    #[cfg(any(target_os = "freebsd", target_os = "macos", target_os = "ios"))]
    fn list_system_addrs() -> Vec<IpAddr> {
        list_system_interfaces("ifconfig", "")
            .lines()
            .filter_map(|line| {
                println!("{}", line);
                if line.contains("inet ") {
                    let addr_s: Vec<&str> = line.split_whitespace().collect();
                    return Some(IpAddr::V4(unwrap!(Ipv4Addr::from_str(addr_s[1]))));
                }
                None
            })
            .collect()
    }

    #[test]
    fn test_get_if_addrs() {
        let ifaces = unwrap!(get_if_addrs());
        println!("Local interfaces:");
        println!("{:#?}", ifaces);
        // at least one loop back address



        assert!(1 <= ifaces.iter().filter(|interface| interface.is_loopback()).count());

        // one address of IpV4(127.0.0.1)
        let is_loopback =
            |interface: &&Interface| interface.addr.ip() == IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
        assert_eq!(1, ifaces.iter().filter(is_loopback).count());

        // each system address shall be listed
        let system_addrs = list_system_addrs();







|
|
>







 







>
|







 







>
|







 







>
>
|
>










|
>
>
>







 







|
|
|
|
|
|
|









>
>
>
|
>







399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
...
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
...
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
...
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
...
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
        }

        for ifaddr in unsafe { CLinkedListConst::from_ptr(ifaddrs, |a| a.next) }.iter() {
            for addr in unsafe {
                        CLinkedListConst::from_ptr(ifaddr.first_unicast_address, |a| a.next)
                    }
                    .iter() {
                let name = unsafe { CStr::from_ptr(ifaddr.adapter_name) }
                    .to_string_lossy()
                    .into_owned();

                let addr = match sockaddr_to_ipaddr(addr.address.lp_socket_address) {
                    None => continue,
                    Some(IpAddr::V4(ipv4_addr)) => {
                        let mut item_netmask = Ipv4Addr::new(0, 0, 0, 0);
                        let mut item_broadcast = None;
                        // Search prefixes for a prefix matching addr
................................................................................
                            unsafe { CLinkedListConst::from_ptr(ifaddr.first_prefix, |p| p.next) }
                                .iter() {
                            let ipprefix = sockaddr_to_ipaddr(prefix.address.lp_socket_address);
                            match ipprefix {
                                Some(IpAddr::V4(ref a)) => {
                                    let mut netmask: [u8; 4] = [0; 4];
                                    for (n, netmask_elt) in
                                        netmask
                                            .iter_mut()
                                            .enumerate()
                                            .take(((prefix.prefix_length as usize + 7) / 8)) {
                                        let x_byte = ipv4_addr.octets()[n];
                                        let y_byte = a.octets()[n];
                                        for m in 0..8 {
                                            if (n * 8) + m > prefix.prefix_length as usize {
                                                break;
................................................................................
                            let ipprefix = sockaddr_to_ipaddr(prefix.address.lp_socket_address);
                            match ipprefix {
                                Some(IpAddr::V6(ref a)) => {
                                    // 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, netmask_elt) in
                                        netmask
                                            .iter_mut()
                                            .enumerate()
                                            .take(((prefix.prefix_length as usize + 15) / 16)) {
                                        let x_word = ipv6_addr.segments()[n];
                                        let y_word = a.segments()[n];
                                        for m in 0..16 {
                                            if (n * 16) + m > prefix.prefix_length as usize {
                                                break;
................................................................................
    use std::thread;
    use std::time::Duration;

    fn list_system_interfaces(cmd: &str, arg: &str) -> String {
        let start_cmd = if arg == "" {
            Command::new(cmd).stdout(Stdio::piped()).spawn()
        } else {
            Command::new(cmd)
                .arg(arg)
                .stdout(Stdio::piped())
                .spawn()
        };
        let mut process = match start_cmd {
            Err(why) => {
                println!("couldn't start cmd {} : {}", cmd, why.description());
                return "".to_string();
            }
            Ok(process) => process,
        };
        thread::sleep(Duration::from_millis(1000));
        let _ = process.kill();
        let result: Vec<u8> = unwrap!(process.stdout)
            .bytes()
            .map(|x| unwrap!(x))
            .collect();
        unwrap!(String::from_utf8(result))
    }

    #[cfg(windows)]
    fn list_system_addrs() -> Vec<IpAddr> {
        use std::net::Ipv6Addr;
        list_system_interfaces("ipconfig", "")
................................................................................
    }

    #[cfg(any(target_os = "freebsd", target_os = "macos", target_os = "ios"))]
    fn list_system_addrs() -> Vec<IpAddr> {
        list_system_interfaces("ifconfig", "")
            .lines()
            .filter_map(|line| {
                            println!("{}", line);
                            if line.contains("inet ") {
                                let addr_s: Vec<&str> = line.split_whitespace().collect();
                                return Some(IpAddr::V4(unwrap!(Ipv4Addr::from_str(addr_s[1]))));
                            }
                            None
                        })
            .collect()
    }

    #[test]
    fn test_get_if_addrs() {
        let ifaces = unwrap!(get_if_addrs());
        println!("Local interfaces:");
        println!("{:#?}", ifaces);
        // at least one loop back address
        assert!(1 <=
                ifaces
                    .iter()
                    .filter(|interface| interface.is_loopback())
                    .count());
        // one address of IpV4(127.0.0.1)
        let is_loopback =
            |interface: &&Interface| interface.addr.ip() == IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
        assert_eq!(1, ifaces.iter().filter(is_loopback).count());

        // each system address shall be listed
        let system_addrs = list_system_addrs();