Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public static string GetLocalIpAddress()
        {
            string retVal = string.Empty;

            foreach (NetworkInterface tmpNetworkIfc in NetworkInterface.GetAllNetworkInterfaces())
            {
                if ((tmpNetworkIfc.NetworkInterfaceType != NetworkInterfaceType.Ethernet &&
                     tmpNetworkIfc.NetworkInterfaceType != NetworkInterfaceType.Wireless80211) ||
                    tmpNetworkIfc.OperationalStatus != OperationalStatus.Up)
                {
                    continue;
                }

                IPInterfaceProperties       adapterProperties = tmpNetworkIfc.GetIPProperties();
                GatewayIPAddressInformation gatewayInfo       = adapterProperties.GatewayAddresses.FirstOrDefault();

                if (gatewayInfo == null || gatewayInfo.Address == null)
                {
                    continue;
                }

                foreach (UnicastIPAddressInformation tmpIpAddr in tmpNetworkIfc.GetIPProperties().UnicastAddresses)
                {
                    if (tmpIpAddr.Address.AddressFamily == AddressFamily.InterNetwork)
                    {
                        retVal = tmpIpAddr.Address.ToString();
                        break;
                    }
                }
            }

            return(retVal);
        }
Exemple #2
0
        private IPAddress ToGatewayAddress(GatewayIPAddressInformation info)
        {
            var firstThreeOctets = info?.Address.GetAddressBytes().Take(3);
            var octets           = firstThreeOctets.Append((byte)0).ToArray();

            return(new IPAddress(octets).MapToIPv6());
        }
Exemple #3
0
        public static String getLocalIPAddress()
        {
            String lRetVal = String.Empty;

            foreach (NetworkInterface lItem in NetworkInterface.GetAllNetworkInterfaces())
            {
                if ((lItem.NetworkInterfaceType == NetworkInterfaceType.Ethernet || lItem.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) &&
                    lItem.OperationalStatus == OperationalStatus.Up)
                {
                    IPInterfaceProperties       lAdapterProperties = lItem.GetIPProperties();
                    GatewayIPAddressInformation lGWInfo            = lAdapterProperties.GatewayAddresses.FirstOrDefault();

                    if (lGWInfo != null && lGWInfo.Address != null)
                    {
                        foreach (UnicastIPAddressInformation lTmpIP in lItem.GetIPProperties().UnicastAddresses)
                        {
                            if (lTmpIP.Address.AddressFamily == AddressFamily.InterNetwork)
                            {
                                lRetVal = lTmpIP.Address.ToString();
                                break;
                            } // if (ip.Add...
                        }     // foreach ...
                    }         // if (lGWIn...
                }             // if (item...
            }                 // foreach (Network...

            return(lRetVal);
        }
Exemple #4
0
        private static string GetLanIpAddress()
        {
            // TODO: support multiple network interfaces
            foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
            {
                GatewayIPAddressInformation gatewayAddress = ni.GetIPProperties().GatewayAddresses.FirstOrDefault();
                if (gatewayAddress != null) //exclude virtual physical nic with no default gateway
                {
                    if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 ||
                        ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet &&
                        ni.OperationalStatus == OperationalStatus.Up)
                    {
                        foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
                        {
                            if (ip.Address.AddressFamily != AddressFamily.InterNetwork ||
                                ip.AddressPreferredLifetime == UInt32.MaxValue) // exclude virtual network addresses
                            {
                                continue;
                            }

                            return(ip.Address.ToString());
                        }
                    }
                }
            }

            return("-");
        }
        /// <summary>
        /// Initialies new instance of type <see cref="GatewayIPAddressInformation"/>
        /// </summary>
        /// <param name="info"></param>
        public GatewayIPAddressInformationAdapter(GatewayIPAddressInformation info)
            : base(info)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            _info = info;
        }
        private Profile MatchNetwork(UnicastIPAddressInformation ipAddr, GatewayIPAddressInformation gw, byte[] addrBytes, byte[] maskBytes)
        {
            var     maxConfidence = 0;
            Profile result        = null;

            foreach (var item in _profiles)
            {
                var confidence = 6;

                if (item.Subnet.Value == null)
                {
                    confidence -= 1;
                }
                else if (!item.Subnet.Value.Equals(ipAddr.IPv4Mask))
                {
                    confidence = 0;
                }

                if (item.IPAddress.Value == null)
                {
                    confidence -= 1;
                }
                else
                {
                    var compBytes = item.IPAddress.Value.GetAddressBytes();
                    for (var i = 0; i < compBytes.Length; i++)
                    {
                        if ((addrBytes[i] & maskBytes[i]) !=
                            (compBytes[i] & maskBytes[i]))
                        {
                            confidence = 0;
                            break;
                        }
                    }
                }

                if (item.Gateway.Value == null)
                {
                    confidence -= 1;
                }
                else if (!item.Gateway.Value.Equals(gw.Address))
                {
                    confidence = 0;
                }

                confidence = Math.Max(0, confidence);
                if (confidence > maxConfidence)
                {
                    maxConfidence = confidence;
                    result        = item;
                }
            }

            return(result);
        }
Exemple #7
0
 private void DropIPV6UPNP()
 {
     foreach (string ConnectionString in ConnectionStrings)
     {
         string IPString   = ConnectionString.Substring(0, ConnectionString.LastIndexOf(":"));
         int    portStart  = ConnectionString.LastIndexOf(":") + 1;
         string portString = ConnectionString.Substring(portStart);
         if (portString.Contains("("))
         {
             portString = portString.Substring(0, portString.LastIndexOf(" ("));
         }
         int       IPV6UPNPPort = Convert.ToInt32(portString);
         IPAddress IP           = IPAddress.Parse(IPString);
         if (IP.AddressFamily == AddressFamily.InterNetworkV6)
         {
             if (!ConnectionString.EndsWith("!)"))
             {
                 UPnPNAT upnpnat = new UPnPNAT();
                 IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;
                 if (IPV4ExternalIPAddress != null)
                 {
                     NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces();
                     foreach (NetworkInterface Interface in Interfaces)
                     {
                         if (Interface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 ||
                             Interface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                         {
                             foreach (UnicastIPAddressInformation ip in Interface.GetIPProperties().UnicastAddresses)
                             {
                                 if (ip.Address.ToString() == IP.ToString() &&
                                     Interface.GetIPProperties().GatewayAddresses.Count > 0)
                                 {
                                     GatewayIPAddressInformation Gateway = Interface.GetIPProperties().GatewayAddresses[0];
                                     if (Gateway.Address.AddressFamily == AddressFamily.InterNetworkV6)
                                     {
                                         try
                                         {
                                             mappings.Remove(IPV6UPNPPort, "TCP");
                                         }
                                         catch (System.Runtime.InteropServices.COMException ex)
                                         {
                                             MessageBox.Show(ex.Message);
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Exemple #8
0
        private bool SetupIPV6UPNP(IPAddress LocalIP)
        {
            int     IPV6ExternalPort = Port;
            UPnPNAT upnpnat          = new UPnPNAT();
            IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;

            if (mappings == null)
            {
                return(false);
            }
            NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface Interface in Interfaces)
            {
                if (Interface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 ||
                    Interface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                {
                    foreach (UnicastIPAddressInformation ip in Interface.GetIPProperties().UnicastAddresses)
                    {
                        if (ip.Address.ToString() == LocalIP.ToString() &&
                            Interface.GetIPProperties().GatewayAddresses.Count > 0)
                        {
                            GatewayIPAddressInformation Gateway = Interface.GetIPProperties().GatewayAddresses[0];
                            if (Gateway.Address.AddressFamily == AddressFamily.InterNetworkV6)
                            {
retry:
                                try
                                {
                                    mappings.Add(IPV6ExternalPort, "TCP", Port, LocalIP.ToString(), true, "Cards-IPv6");
                                    ConnectionStrings.Add(LocalIP.ToString() + ":" + IPV6ExternalPort + " (Internet)");
                                    return(true);
                                }
                                catch (System.Runtime.InteropServices.COMException)
                                {
                                    if (IPV6ExternalPort < 60074)
                                    {
                                        IPV6ExternalPort++;
                                        goto retry;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(false);
        }
Exemple #9
0
        public static void LoadNICs(ComboBox box)
        {
            box.Items.Clear();

            /** Scan through each NIC interface **/
            foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
            {
                /** Verify it suport IPv4, is operating, and is not a loopback **/
                if (nic.Supports(NetworkInterfaceComponent.IPv4) && nic.OperationalStatus == OperationalStatus.Up && nic.NetworkInterfaceType != NetworkInterfaceType.Loopback)
                {
                    NIC      newNic = new NIC();
                    StaticIP ip     = new StaticIP();

                    newNic.Name        = nic.Name;
                    newNic.DHCPEnabled = nic.GetIPProperties().GetIPv4Properties().IsDhcpEnabled;

                    /** We assume the last result is the IPv4 address **/
                    UnicastIPAddressInformation uni = nic.GetIPProperties().UnicastAddresses.LastOrDefault();
                    if (uni != null)
                    {
                        ip.Address    = uni.Address.ToString();
                        ip.SubnetMask = uni.IPv4Mask.ToString();
                    }

                    /** We assume the first gateway address is the default one **/
                    GatewayIPAddressInformation gate = nic.GetIPProperties().GatewayAddresses.FirstOrDefault();
                    if (gate != null)
                    {
                        ip.DefaultGateway = gate.Address.ToString();
                    }

                    newNic.IP = ip;
                    box.Items.Add(newNic);
                }
            }

            if (box.Items.Count > 0)
            {
                box.SelectedIndex = 0;
            }
        }
Exemple #10
0
 /// <summary>
 /// Get the computer's local IP
 /// </summary>
 /// <returns>IP string</returns>
 private string GetIP()
 {
     foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
     {
         GatewayIPAddressInformation addr = ni.GetIPProperties().GatewayAddresses.FirstOrDefault();
         if (addr != null && !addr.Address.ToString().Equals("0.0.0.0"))
         {
             if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
             {
                 foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
                 {
                     if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
                     {
                         return(ip.Address.ToString());
                     }
                 }
             }
         }
     }
     return(String.Empty);
 }
Exemple #11
0
 public static string DeleteList()
 {
     //Discarded unreachable code: IL_0002
     //IL_0003: Incompatible stack heights: 0 vs 1
     NetworkInterface[] allNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
     foreach (NetworkInterface networkInterface in allNetworkInterfaces)
     {
         GatewayIPAddressInformation gatewayIPAddressInformation = networkInterface.GetIPProperties().GatewayAddresses.FirstOrDefault();
         if (gatewayIPAddressInformation != null && (networkInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || (networkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet && networkInterface.OperationalStatus == OperationalStatus.Up)))
         {
             foreach (UnicastIPAddressInformation unicastAddress in networkInterface.GetIPProperties().UnicastAddresses)
             {
                 if (unicastAddress.Address.AddressFamily == AddressFamily.InterNetwork && unicastAddress.AddressPreferredLifetime != uint.MaxValue)
                 {
                     return(unicastAddress.Address.ToString());
                 }
             }
         }
     }
     return("-");
 }
Exemple #12
0
        /// <summary>
        /// Get the default gateway for a given IP address
        /// </summary>
        /// <param name="address">The address to return the IP of, must be on local machine</param>
        /// <returns>The default gateway for the IP</returns>
        public static IPAddress GetGateway(IPAddress address)
        {
            // Modified from
            // http://www.java2s.com/Code/CSharp/Network/GetSubnetMask.htm

            // Modified from
            // http://www.java2s.com/Code/CSharp/Network/GetSubnetMask.htm

            // Iterate over all the network interfaces on this computer (Wifi, Ethernet, etc...)
            foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces())
            {
                // Get each unicast address from the adapter
                // See http://www.vsicam.com/_faq/what-is-the-difference-between-unicast-and-multicast-streams/
                foreach (UnicastIPAddressInformation uniIpAddressInfo in adapter.GetIPProperties().UnicastAddresses)
                {
                    // If this is an inter-network IP address
                    // https://msdn.microsoft.com/en-us/library/system.net.sockets.addressfamily%28v=vs.110%29.aspx
                    if (uniIpAddressInfo.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    {
                        // If this is the droid we're looking for, return it's Net Mask
                        if (address.Equals(uniIpAddressInfo.Address))
                        {
                            GatewayIPAddressInformation gateway = adapter.GetIPProperties().GatewayAddresses.FirstOrDefault();

                            if (gateway != null)
                            {
                                return(gateway.Address);
                            }
                        }
                    }
                }
            }

            // We could not find this IP address
            return(null);
        }
Exemple #13
0
        private void DropIPV4UPNP()
        {
            UPnPNAT upnpnat = new UPnPNAT();
            IStaticPortMappingCollection mappings = upnpnat.StaticPortMappingCollection;

            if (IPV4ExternalIPAddress != null)
            {
                NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface Interface in Interfaces)
                {
                    if (Interface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 ||
                        Interface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                    {
                        foreach (UnicastIPAddressInformation ip in Interface.GetIPProperties().UnicastAddresses)
                        {
                            if (ip.Address.AddressFamily == AddressFamily.InterNetwork &&
                                Interface.GetIPProperties().GatewayAddresses.Count > 0)
                            {
                                GatewayIPAddressInformation Gateway = Interface.GetIPProperties().GatewayAddresses[0];
                                if (Gateway.Address.AddressFamily == AddressFamily.InterNetwork)
                                {
                                    try
                                    {
                                        mappings.Remove(IPV4ExternalPort, "TCP");
                                    }
                                    catch (System.Runtime.InteropServices.COMException ex)
                                    {
                                        MessageBox.Show(ex.Message);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Exemple #14
0
 public virtual void Add(GatewayIPAddressInformation address)
 {
 }
Exemple #15
0
        public static string ShowIPAddresses()
        {
            string s = "";
            //bool dhcpEnabled;
            DateTime localDate = DateTime.Now;

            //Task t = Task.Run(() =>
            //{
            //    Task.Delay(5000).Wait();
            //});

            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            //foreach (NetworkInterface adapter in nics)
            //{
            //    s += adapter.Description + "\n";
            //}
            try
            {
                // for all network interfaces
                foreach (NetworkInterface adapter in nics)
                {
                    //s += nics[i].Description + "\n";

                    // get only Ethernet and active adapters (VM included)
                    if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet && adapter.OperationalStatus == OperationalStatus.Up)
                    {
                        //s += "okokokokokokok\n";

                        //get its properties
                        IPInterfaceProperties   adapterProperties = adapter.GetIPProperties();
                        IPv4InterfaceProperties p = adapterProperties.GetIPv4Properties();

                        // Network interface description
                        //s += nic0.Description + "\n";

                        // Check if its DHCP is enabled
                        if (p.IsDhcpEnabled)
                        {
                            //get its properties
                            UnicastIPAddressInformationCollection uniCast   = adapterProperties.UnicastAddresses;
                            GatewayIPAddressInformationCollection addresses = adapterProperties.GatewayAddresses;
                            GatewayIPAddressInformation           address0  = addresses[0];
                            //s += nic0.Description + " DHCP activé pour cette carte " + p.IsDhcpEnabled + "\n";
                            IPAddress ipDestination = new IPAddress(0x0);                                   // route par défaut

                            if (GetGatewayForDestination(ipDestination) == addresses[0].Address.ToString()) //checks if NIC uses default route for Internet
                            {
                                //s += "ipDestination = " + GetGatewayForDestination(ipDestination) + "\n";
                                //s += "passerelle par défaut : " + addresses[0].Address.ToString() + "\n\n";

                                string lifeTimeFormat = "dddd, MMMM dd, yyyy  hh:mm:ss tt";

                                foreach (UnicastIPAddressInformation uni in uniCast)
                                {
                                    DateTime when;

                                    // Format the lifetimes as Sunday, February 16, 2003 11:33:44 PM
                                    // if en-us is the current culture.

                                    // Calculate the date and time at the end of the lifetimes.

                                    //when = DateTime.UtcNow + TimeSpan.FromSeconds(uni.AddressValidLifetime);
                                    //when = when.ToLocalTime();
                                    //s += "     Valid Life Time ...................... : " + when.ToString(lifeTimeFormat, System.Globalization.CultureInfo.CurrentCulture) + "\n";

                                    //when = DateTime.UtcNow + TimeSpan.FromSeconds(uni.AddressPreferredLifetime);
                                    //when = when.ToLocalTime();
                                    //s += "     Preferred life time .................. : " + when.ToString(lifeTimeFormat, System.Globalization.CultureInfo.CurrentCulture) + "\n";

                                    when = DateTime.UtcNow + TimeSpan.FromSeconds(uni.DhcpLeaseLifetime);
                                    //s += "utc now " + when.ToString() + "\n";

                                    when = when.ToLocalTime();
                                    //s += "local time " + when.ToString() + "\n";

                                    //s += "     DHCP Leased Life Time ................ : " + when.ToString(lifeTimeFormat, System.Globalization.CultureInfo.CurrentCulture) + "\n";

                                    if (uni.DhcpLeaseLifetime > 0 && when > localDate)
                                    {
                                        s += "XXXX est compatible\n";
                                        return(s);
                                    }

                                    s += "\n";
                                    //break;  //only 1st adapter (Ethernet) matters, others are virtual machines
                                }
                            }
                            //break; //only 1st adapter (Ethernet) matters, others are virtual machines
                        }
                    }
                }
                s += "Pour accéder aux services Citypassenger, contacter le support technique\n";
                return(s);
            }
            catch (Exception ex)
            {
                erreur = true;
                s      = "Un problème est survenu lors du lancement de l'application";
                return(s);
            }
        }
Exemple #16
0
 static IPAddress ToIPAddress(GatewayIPAddressInformation info) => info.Address;
Exemple #17
0
        int ISocketsDriver.LoadAdapterConfiguration(uint interfaceIndex, ref NetworkAdapterConfiguration config)
        {
            NetworkInterface        networkInterface = NetworkInterface.GetAllNetworkInterfaces()[interfaceIndex];
            IPInterfaceProperties   props            = networkInterface.GetIPProperties();
            IPv4InterfaceProperties ipv4props        = null;

            try
            {
                ipv4props = props.GetIPv4Properties();
            }
            catch (NetworkInformationException)
            {
            }


            if (ipv4props != null)
            {
                if (props.IsDynamicDnsEnabled)
                {
                    config.flags |= NetworkAdapterConfiguration.FLAGS_DYNAMIC_DNS;
                }

                if (ipv4props.IsDhcpEnabled)
                {
                    config.flags |= NetworkAdapterConfiguration.FLAGS_DHCP;
                }

                for (int iAddress = 0; iAddress < props.UnicastAddresses.Count; iAddress++)
                {
                    UnicastIPAddressInformation unicastAddress = props.UnicastAddresses[iAddress];

                    if (IsIPAddressIPv4(unicastAddress.Address))
                    {
                        config.ipaddr     = this.IPAddressToUint(unicastAddress.Address);
                        config.subnetmask = this.IPAddressToUint(unicastAddress.IPv4Mask);

                        break;
                    }
                }

                for (int iAddress = 0; iAddress < props.GatewayAddresses.Count; iAddress++)
                {
                    GatewayIPAddressInformation gatewayAddress = props.GatewayAddresses[iAddress];

                    if (IsIPAddressIPv4(gatewayAddress.Address))
                    {
                        config.gateway = this.IPAddressToUint(gatewayAddress.Address);

                        break;
                    }
                }

                int dnsServerIndex = 0;
                for (int iAddress = 0; iAddress < props.DnsAddresses.Count && dnsServerIndex < 2; iAddress++)
                {
                    IPAddress address = props.DnsAddresses[iAddress];

                    if (IsIPAddressIPv4(address))
                    {
                        switch (dnsServerIndex)
                        {
                        case 0:
                            config.dnsServer1 = this.IPAddressToUint(address);
                            break;

                        case 1:
                            config.dnsServer2 = this.IPAddressToUint(address);
                            break;
                        }

                        dnsServerIndex++;
                    }
                }

                PhysicalAddress macAddress = networkInterface.GetPhysicalAddress();
                byte[]          macBytes   = macAddress.GetAddressBytes();

                config.networkInterfaceType = (uint)networkInterface.NetworkInterfaceType;
                config.networkAddressLen    = (uint)macBytes.Length;

                unsafe
                {
                    fixed(byte *p = config.networkAddressBuf)
                    {
                        Marshal.Copy(macBytes, 0, new IntPtr(p), macBytes.Length);
                    }
                }
            }

            return(0);
        }
Exemple #18
0
        public static bool ShowIPAddressesBool()
        {
            string   s = "";
            bool     dhcpEnabled;
            DateTime localDate = DateTime.Now;

            //s += "Analyse du réseau de ce PC en cours...\n";
            //s += "============================================ \n";
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

            // for all network interfaces
            foreach (NetworkInterface adapter in nics)
            {
                // get only Ethernet and active adapters (VM included)
                if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet && adapter.OperationalStatus == OperationalStatus.Up)
                {
                    //get its properties
                    IPInterfaceProperties   adapterProperties = adapter.GetIPProperties();
                    IPv4InterfaceProperties p = adapterProperties.GetIPv4Properties();

                    // Network interface description
                    //s += nic0.Description + "\n";

                    // Check if its DHCP is enabled
                    if (p.IsDhcpEnabled)
                    {
                        //get its properties
                        UnicastIPAddressInformationCollection uniCast   = adapterProperties.UnicastAddresses;
                        GatewayIPAddressInformationCollection addresses = adapterProperties.GatewayAddresses;
                        GatewayIPAddressInformation           address0  = addresses[0];
                        //s += nic0.Description + " DHCP activé pour cette carte " + p.IsDhcpEnabled + "\n";
                        IPAddress ipDestination = new IPAddress(0x0);                               // route par défaut

                        if (GetGatewayForDestination(ipDestination) == address0.Address.ToString()) //checks if NIC uses default route for Internet
                        {
                            //s += "ipDestination = " + GetGatewayForDestination(ipDestination) + "\n";
                            //s += "passerelle par défaut : " + addresses[0].Address.ToString() + "\n\n";

                            string lifeTimeFormat = "dddd, MMMM dd, yyyy  hh:mm:ss tt";

                            foreach (UnicastIPAddressInformation uni in uniCast)
                            {
                                DateTime when;

                                // Format the lifetimes as Sunday, February 16, 2003 11:33:44 PM
                                // if en-us is the current culture.

                                // Calculate the date and time at the end of the lifetimes.

                                //when = DateTime.UtcNow + TimeSpan.FromSeconds(uni.AddressValidLifetime);
                                //when = when.ToLocalTime();
                                //s += "     Valid Life Time ...................... : " + when.ToString(lifeTimeFormat, System.Globalization.CultureInfo.CurrentCulture) + "\n";

                                //when = DateTime.UtcNow + TimeSpan.FromSeconds(uni.AddressPreferredLifetime);
                                //when = when.ToLocalTime();
                                //s += "     Preferred life time .................. : " + when.ToString(lifeTimeFormat, System.Globalization.CultureInfo.CurrentCulture) + "\n";

                                when = DateTime.UtcNow + TimeSpan.FromSeconds(uni.DhcpLeaseLifetime);
                                //s += "utc now " + when.ToString() + "\n";

                                when = when.ToLocalTime();
                                //s += "local time " + when.ToString() + "\n";

                                //s += "     DHCP Leased Life Time ................ : " + when.ToString(lifeTimeFormat, System.Globalization.CultureInfo.CurrentCulture) + "\n";

                                if (uni.DhcpLeaseLifetime > 0 && when > localDate)
                                {
                                    dhcpEnabled = true;
                                    return(dhcpEnabled);
                                }
                                //return s;
                                s += "\n";
                                //break;  //only 1st adapter (Ethernet) matters, others are virtual machines
                            }
                        }
                        //break; //only 1st adapter (Ethernet) matters, others are virtual machines
                    }
                }
            }
            //s += " Le DHCP n'est pas fonctionnel sur ce PC\n";
            dhcpEnabled = false;
            return(dhcpEnabled);
        }
Exemple #19
0
        /// <summary>
        /// Create Discovery tasks for a specific Network Interface
        /// </summary>
        /// <param name="netInterface"></param>
        /// <returns></returns>
        private static List <Task <List <Device> > > CreateDiscoverTasks(NetworkInterface netInterface)
        {
            List <Device> devices               = new List <Device>();
            object        deviceLock            = new object();
            List <Task <List <Device> > > tasks = new List <Task <List <Device> > >();

            try
            {
                GatewayIPAddressInformation addr = netInterface.GetIPProperties().GatewayAddresses.FirstOrDefault();

                if (addr != null && !addr.Address.ToString().Equals("0.0.0.0"))
                {
                    if (netInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || netInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                    {
                        foreach (UnicastIPAddressInformation ip in netInterface.GetIPProperties().UnicastAddresses)
                        {
                            if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
                            {
                                for (int cpt = 0; cpt < 3; cpt++)
                                {
                                    Task <List <Device> > t = Task.Factory.StartNew <List <Device> >(() =>
                                    {
                                        Socket ssdpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
                                        {
                                            Blocking            = false,
                                            Ttl                 = 1,
                                            UseOnlyOverlappedIO = true,
                                            MulticastLoopback   = false,
                                        };
                                        ssdpSocket.Bind(new IPEndPoint(ip.Address, 0));
                                        ssdpSocket.SetSocketOption(
                                            SocketOptionLevel.IP,
                                            SocketOptionName.AddMembership,
                                            new MulticastOption(_multicastEndPoint.Address));

                                        ssdpSocket.SendTo(_ssdpDiagram, SocketFlags.None, _multicastEndPoint);

                                        DateTime start = DateTime.Now;
                                        while (DateTime.Now - start < TimeSpan.FromSeconds(1))
                                        {
                                            int available = ssdpSocket.Available;

                                            if (available > 0)
                                            {
                                                byte[] buffer = new byte[available];
                                                int i         = ssdpSocket.Receive(buffer, SocketFlags.None);

                                                if (i > 0)
                                                {
                                                    string response = Encoding.UTF8.GetString(buffer.Take(i).ToArray());
                                                    Device device   = GetDeviceInformationsFromSsdpMessage(response);

                                                    //add only if no device already matching
                                                    if (!devices.Any(d => d.Hostname == device.Hostname))
                                                    {
                                                        lock (deviceLock)
                                                        {
                                                            if (!devices.Any(d => d.Hostname == device.Hostname))
                                                            {
                                                                devices.Add(device);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            Thread.Sleep(10);
                                        }

                                        return(devices);
                                    });
                                    tasks.Add(t);
                                }
                            }
                        }
                    }
                }
            }
            catch { }

            return(tasks);
        }
Exemple #20
0
 // Token: 0x060000B5 RID: 181 RVA: 0x000023F2 File Offset: 0x000005F2
 internal IPAddress method_3(GatewayIPAddressInformation gatewayIPAddressInformation_0)
 {
     return((gatewayIPAddressInformation_0 != null) ? gatewayIPAddressInformation_0.Address : null);
 }
 public static IGatewayIPAddressInformation ToInterface([CanBeNull] this GatewayIPAddressInformation info)
 {
     return((info == null) ? null : new GatewayIPAddressInformationAdapter(info));
 }
Exemple #22
0
 public virtual bool Contains(GatewayIPAddressInformation address)
 {
 }
Exemple #23
0
 public virtual bool Remove(GatewayIPAddressInformation address)
 {
 }