public static void GetAdaptersAddresses(System.Net.Sockets.AddressFamily addressFamily,
            NetDisc.GAA_FLAGS gaaFlags,
            out List<IP_ADAPTER_ADDRESSES> adaptAddrList)
        {
            adaptAddrList = new List<IP_ADAPTER_ADDRESSES>();
            UInt32 size = (UInt32)Marshal.SizeOf(typeof(IP_ADAPTER_ADDRESSES));
            IntPtr pAdaptAddrBuffer = Marshal.AllocHGlobal((Int32)size);

            uint result = GetAdaptersAddresses((UInt32)addressFamily, (UInt32)gaaFlags, (IntPtr)0, pAdaptAddrBuffer, ref size);

            if (result == NetDisc.ERROR_BUFFER_OVERFLOW)
            {
                Marshal.FreeHGlobal(pAdaptAddrBuffer);
                pAdaptAddrBuffer = Marshal.AllocHGlobal((Int32)size);
                result = GetAdaptersAddresses((UInt32)addressFamily, (UInt32)gaaFlags, (IntPtr)0, pAdaptAddrBuffer, ref size);
            }

            if (result != NetDisc.ERROR_SUCCESS)
            {
                throw new Win32Exception((Int32)result, "GetAdaptersAddresses FAILED.");
            }

            if ((result == NetDisc.ERROR_SUCCESS) && (pAdaptAddrBuffer != IntPtr.Zero))
            {
                IntPtr pTemp = pAdaptAddrBuffer;

                do
                {
                    IP_ADAPTER_ADDRESSES aAB = new IP_ADAPTER_ADDRESSES();
                    aAB = (IP_ADAPTER_ADDRESSES)Marshal.PtrToStructure((IntPtr)pTemp, typeof(IP_ADAPTER_ADDRESSES));
                    adaptAddrList.Add(aAB);

                    pTemp = (IntPtr)aAB.Next;
                }
                while (pTemp != IntPtr.Zero);
            }
        }
Exemple #2
0
        public static void GetAdaptersAddresses(System.Net.Sockets.AddressFamily addressFamily,
                                                NetDisc.GAA_FLAGS gaaFlags,
                                                out List <IP_ADAPTER_ADDRESSES> adaptAddrList)
        {
            adaptAddrList = new List <IP_ADAPTER_ADDRESSES>();
            UInt32 size             = (UInt32)Marshal.SizeOf(typeof(IP_ADAPTER_ADDRESSES));
            IntPtr pAdaptAddrBuffer = Marshal.AllocHGlobal((Int32)size);

            uint result = GetAdaptersAddresses((UInt32)addressFamily, (UInt32)gaaFlags, (IntPtr)0, pAdaptAddrBuffer, ref size);

            if (result == NetDisc.ERROR_BUFFER_OVERFLOW)
            {
                Marshal.FreeHGlobal(pAdaptAddrBuffer);
                pAdaptAddrBuffer = Marshal.AllocHGlobal((Int32)size);
                result           = GetAdaptersAddresses((UInt32)addressFamily, (UInt32)gaaFlags, (IntPtr)0, pAdaptAddrBuffer, ref size);
            }

            if (result != NetDisc.ERROR_SUCCESS)
            {
                throw new Win32Exception((Int32)result, "GetAdaptersAddresses FAILED.");
            }

            if ((result == NetDisc.ERROR_SUCCESS) && (pAdaptAddrBuffer != IntPtr.Zero))
            {
                IntPtr pTemp = pAdaptAddrBuffer;

                do
                {
                    IP_ADAPTER_ADDRESSES aAB = new IP_ADAPTER_ADDRESSES();
                    aAB = (IP_ADAPTER_ADDRESSES)Marshal.PtrToStructure((IntPtr)pTemp, typeof(IP_ADAPTER_ADDRESSES));
                    adaptAddrList.Add(aAB);

                    pTemp = (IntPtr)aAB.Next;
                }while (pTemp != IntPtr.Zero);
            }
        }
Exemple #3
0
        public static void GetIPv4gateway(IP_ADAPTER_ADDRESSES A, IPAddress ipaddr, out int bc, out string ba)
        {
            bc = 32;
            ba = "";
            string first_multi = "";

            Trace.WriteLine("start GetIPv4gateway");

            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in adapters)
            {
                // match c# adapter to win32 adapter A passed in
                if (adapter.Name.CompareTo(Marshal.PtrToStringAuto(A.FriendlyName)) != 0)
                {
                    continue;
                }
                // get a c# view of adapter properties
                IPInterfaceProperties adapterProperties = adapter.GetIPProperties();

                // get the first multicast address
                MulticastIPAddressInformationCollection multiCast = adapterProperties.MulticastAddresses;
                if (multiCast != null)
                {
                    foreach (IPAddressInformation multi in multiCast)
                    {
                        if (first_multi.Length == 0)
                        {
                            multi.Address.ToString();
                        }
                        Trace.WriteLine("       multi " + multi.Address.ToString());
                        continue;
                    }
                }

                UnicastIPAddressInformationCollection uniCast = adapterProperties.UnicastAddresses;
                if (uniCast != null)
                {
                    foreach (UnicastIPAddressInformation uni in uniCast)
                    {
                        // chop off any % char and what trails it
                        string s1 = uni.Address.ToString();
                        string s2 = ipaddr.ToString();
                        if (s1.IndexOf((char)'%') != -1)
                        {
                            s1 = s1.Substring(0, s1.IndexOf((char)'%'));
                        }
                        if (s2.IndexOf((char)'%') != -1)
                        {
                            s2 = s2.Substring(0, s2.IndexOf((char)'%'));
                        }
                        if (s1.Equals(s2))
                        {
                            Trace.WriteLine("unicast addr " + s1);
                            if (uni.IPv4Mask != null)
                            {
                                byte[] bcast_add = uni.Address.GetAddressBytes();

                                // check for ipv4
                                if (uni.IPv4Mask.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                                {
                                    ba = uni.IPv4Mask.ToString();

                                    byte[] ipAdressBytes   = uni.Address.GetAddressBytes();
                                    byte[] subnetMaskBytes = uni.IPv4Mask.GetAddressBytes();

                                    if (ipAdressBytes.Length != subnetMaskBytes.Length)
                                    {
                                        if (ipAdressBytes.Length > 4)
                                        {
                                            Trace.WriteLine("    ipAdressBytes.Length = " + ipAdressBytes.Length.ToString());
                                            Trace.WriteLine("    different addr length " + uni.Address.ToString() + " " + uni.IPv4Mask.ToString());
                                            ba = first_multi;
                                            bc = 64;
                                        }
                                        break;
                                    }
                                    bc = 0;
                                    int totbits = subnetMaskBytes.Length * 8;
                                    for (int i = 0; i < subnetMaskBytes.Length; i++)
                                    {
                                        for (int j = 0; j < totbits; j++)
                                        {
                                            byte maskbit = (byte)(1 << j);
                                            if ((maskbit & subnetMaskBytes[i]) != 0)
                                            {
                                                bc++;
                                            }
                                            else
                                            {
                                                bcast_add[i] |= maskbit;
                                            }
                                        }
                                    }

                                    StringBuilder sb = new StringBuilder(bcast_add.Length * 4);
                                    foreach (byte b in bcast_add)
                                    {
                                        sb.AppendFormat("{0}", b);
                                        sb.Append(".");
                                    }
                                    sb.Remove(sb.Length - 1, 1);
                                    ba = sb.ToString();
                                }
                            }
                            else
                            {
                                ba = first_multi;
                                if (A.IfType == 24)
                                {  // loopback
                                    bc = 8;
                                }
                                else
                                {
                                    bc = 32;
                                }
                                break;
                            }
                        }
                    }
                }

                /* the code below doesn't display any gateways on win7, seems like it should
                 * if (System.Environment.OSVersion.Version.Major >= 6)   //vista or later
                 * {
                 *  Trace.WriteLine("Gateways");
                 *  GatewayIPAddressInformationCollection addresses = adapterProperties.GatewayAddresses;
                 *  if (addresses.Count > 0)
                 *  {
                 *      Trace.WriteLine(adapter.Description);
                 *      foreach (GatewayIPAddressInformation address in addresses)
                 *      {
                 *          Trace.WriteLine("  Gateway Address : {0}",
                 *              address.Address.ToString());
                 *      }
                 *  }
                 * }
                 */
                break;
            }
            Trace.WriteLine("end   GetIPv4gateway");
        }
        public static void GetIPv4gateway(IP_ADAPTER_ADDRESSES A, IPAddress ipaddr, out int bc, out string ba)
        {
            bc = 32;
            ba = "";
            string first_multi = "";

            Trace.WriteLine("start GetIPv4gateway");

            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in adapters)
            {
                // match c# adapter to win32 adapter A passed in
                if (adapter.Name.CompareTo(Marshal.PtrToStringAuto(A.FriendlyName)) != 0)
                {
                    continue;
                }
                // get a c# view of adapter properties
                IPInterfaceProperties adapterProperties = adapter.GetIPProperties();

                // get the first multicast address
                MulticastIPAddressInformationCollection multiCast = adapterProperties.MulticastAddresses;
                if (multiCast != null)
                {
                    foreach (IPAddressInformation multi in multiCast)
                    {
                        if (first_multi.Length == 0)
                        {
                            multi.Address.ToString();
                        }
                        Trace.WriteLine("       multi " + multi.Address.ToString());
                        continue;
                    }
                 }

                UnicastIPAddressInformationCollection uniCast = adapterProperties.UnicastAddresses;
                if (uniCast != null)
                {
                    foreach (UnicastIPAddressInformation uni in uniCast)
                    {
                        // chop off any % char and what trails it
                        string s1 = uni.Address.ToString();
                        string s2 = ipaddr.ToString();
                        if (s1.IndexOf((char)'%') != -1)
                        {
                            s1 = s1.Substring(0, s1.IndexOf((char)'%'));
                        }
                        if (s2.IndexOf((char)'%') != -1)
                        {
                            s2 = s2.Substring(0, s2.IndexOf((char)'%'));
                        }
                        if (s1.Equals(s2))
                        {
                            Trace.WriteLine("unicast addr " + s1);
                            if (uni.IPv4Mask != null)
                            {
                                byte[] bcast_add = uni.Address.GetAddressBytes();

                                // check for ipv4
                                if (uni.IPv4Mask.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                                {
                                    ba = uni.IPv4Mask.ToString();

                                    byte[] ipAdressBytes = uni.Address.GetAddressBytes();
                                    byte[] subnetMaskBytes = uni.IPv4Mask.GetAddressBytes();

                                    if (ipAdressBytes.Length != subnetMaskBytes.Length)
                                    {
                                        if (ipAdressBytes.Length > 4)
                                        {
                                            Trace.WriteLine("    ipAdressBytes.Length = " + ipAdressBytes.Length.ToString());
                                            Trace.WriteLine("    different addr length " + uni.Address.ToString() + " " + uni.IPv4Mask.ToString());
                                            ba = first_multi;
                                            bc = 64;
                                        }
                                        break;
                                    }
                                    bc = 0;
                                    int totbits = subnetMaskBytes.Length * 8;
                                    for (int i = 0; i < subnetMaskBytes.Length; i++)
                                    {
                                        for (int j = 0; j < totbits; j++)
                                        {
                                            byte maskbit = (byte)(1 << j);
                                            if ((maskbit & subnetMaskBytes[i]) != 0)
                                            {
                                                bc++;
                                            }
                                            else
                                            {
                                                bcast_add[i] |= maskbit;
                                            }
                                        }
                                    }

                                    StringBuilder sb = new StringBuilder(bcast_add.Length * 4);
                                    foreach (byte b in bcast_add)
                                    {
                                        sb.AppendFormat("{0}", b);
                                        sb.Append(".");
                                    }
                                    sb.Remove(sb.Length - 1, 1);
                                    ba = sb.ToString();
                                }
                            }
                            else
                            {
                                ba = first_multi;
                                if (A.IfType == 24)
                                {  // loopback
                                    bc = 8;
                                }
                                else
                                {
                                    bc = 32;
                                }
                                break;
                            }
                        }
                    }
                }
                /* the code below doesn't display any gateways on win7, seems like it should
                if (System.Environment.OSVersion.Version.Major >= 6)   //vista or later
                {
                    Trace.WriteLine("Gateways");
                    GatewayIPAddressInformationCollection addresses = adapterProperties.GatewayAddresses;
                    if (addresses.Count > 0)
                    {
                        Trace.WriteLine(adapter.Description);
                        foreach (GatewayIPAddressInformation address in addresses)
                        {
                            Trace.WriteLine("  Gateway Address : {0}",
                                address.Address.ToString());
                        }
                    }
                }
                */
                break;
            }
            Trace.WriteLine("end   GetIPv4gateway");
        }