void GetDNSServersFromOS() { IntPtr dsa; int len = _monodroid_get_dns_servers(out dsa); if (len <= 0) { return; } var servers = new IntPtr [len]; Marshal.Copy(dsa, servers, 0, len); dns_servers = new IPAddressCollection(); foreach (IntPtr s in servers) { string server_ip = Marshal.PtrToStringAnsi(s); Marshal.FreeHGlobal(s); IPAddress addr; if (!IPAddress.TryParse(server_ip, out addr)) { continue; } dns_servers.InternalAdd(addr); } Marshal.FreeHGlobal(dsa); }
internal IPAddressCollection ToIPAddressCollection() { IpAddrString str = this; IPAddressCollection addresss = new IPAddressCollection(); if (str.IpAddress.Length != 0) { addresss.InternalAdd(IPAddress.Parse(str.IpAddress)); } while (str.Next != IntPtr.Zero) { str = (IpAddrString)Marshal.PtrToStructure(str.Next, typeof(IpAddrString)); if (str.IpAddress.Length != 0) { addresss.InternalAdd(IPAddress.Parse(str.IpAddress)); } } return(addresss); }
internal static IPAddressCollection ToAddressCollection(IntPtr ptr, IPVersion versionSupported) { IPAddressCollection addresss = new IPAddressCollection(); if (ptr != IntPtr.Zero) { IPEndPoint point; IpAdapterAddress address = (IpAdapterAddress)Marshal.PtrToStructure(ptr, typeof(IpAdapterAddress)); AddressFamily family = (address.address.addressLength > 0x10) ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork; SocketAddress socketAddress = new SocketAddress(family, address.address.addressLength); Marshal.Copy(address.address.address, socketAddress.m_Buffer, 0, address.address.addressLength); if (family == AddressFamily.InterNetwork) { point = (IPEndPoint)IPEndPoint.Any.Create(socketAddress); } else { point = (IPEndPoint)IPEndPoint.IPv6Any.Create(socketAddress); } addresss.InternalAdd(point.Address); while (address.next != IntPtr.Zero) { address = (IpAdapterAddress)Marshal.PtrToStructure(address.next, typeof(IpAdapterAddress)); family = (address.address.addressLength > 0x10) ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork; if (((family == AddressFamily.InterNetwork) && ((versionSupported & IPVersion.IPv4) > IPVersion.None)) || ((family == AddressFamily.InterNetworkV6) && ((versionSupported & IPVersion.IPv6) > IPVersion.None))) { socketAddress = new SocketAddress(family, address.address.addressLength); Marshal.Copy(address.address.address, socketAddress.m_Buffer, 0, address.address.addressLength); if (family == AddressFamily.InterNetwork) { point = (IPEndPoint)IPEndPoint.Any.Create(socketAddress); } else { point = (IPEndPoint)IPEndPoint.IPv6Any.Create(socketAddress); } addresss.InternalAdd(point.Address); } } } return(addresss); }
// This constructor is for Vista and newer internal SystemIPInterfaceProperties(FixedInfo fixedInfo, IpAdapterAddresses ipAdapterAddresses) { adapterFlags = ipAdapterAddresses.flags; dnsSuffix = ipAdapterAddresses.dnsSuffix; dnsEnabled = fixedInfo.EnableDns; dynamicDnsEnabled = ((ipAdapterAddresses.flags & AdapterFlags.DnsEnabled) > 0); multicastAddresses = SystemMulticastIPAddressInformation.ToMulticastIpAddressInformationCollection( IpAdapterAddress.MarshalIpAddressInformationCollection(ipAdapterAddresses.firstMulticastAddress)); dnsAddresses = IpAdapterAddress.MarshalIpAddressCollection(ipAdapterAddresses.firstDnsServerAddress); anycastAddresses = IpAdapterAddress.MarshalIpAddressInformationCollection( ipAdapterAddresses.firstAnycastAddress); unicastAddresses = SystemUnicastIPAddressInformation.MarshalUnicastIpAddressInformationCollection( ipAdapterAddresses.firstUnicastAddress); winsServersAddresses = IpAdapterAddress.MarshalIpAddressCollection( ipAdapterAddresses.firstWinsServerAddress); gatewayAddresses = SystemGatewayIPAddressInformation.ToGatewayIpAddressInformationCollection( IpAdapterAddress.MarshalIpAddressCollection(ipAdapterAddresses.firstGatewayAddress)); dhcpServers = new IPAddressCollection(); if (ipAdapterAddresses.dhcpv4Server.address != IntPtr.Zero) { dhcpServers.InternalAdd(ipAdapterAddresses.dhcpv4Server.MarshalIPAddress()); } if (ipAdapterAddresses.dhcpv6Server.address != IntPtr.Zero) { dhcpServers.InternalAdd(ipAdapterAddresses.dhcpv6Server.MarshalIPAddress()); } if ((adapterFlags & AdapterFlags.IPv4Enabled) != 0) { ipv4Properties = new SystemIPv4InterfaceProperties(fixedInfo, ipAdapterAddresses); } if ((adapterFlags & AdapterFlags.IPv6Enabled) != 0) { ipv6Properties = new SystemIPv6InterfaceProperties(ipAdapterAddresses.ipv6Index, ipAdapterAddresses.mtu, ipAdapterAddresses.zoneIndices); } }
internal static IPAddressCollection MarshalIpAddressCollection(IntPtr ptr) { IPAddressCollection addressList = new IPAddressCollection(); while (ptr != IntPtr.Zero) { IpAdapterAddress addressStructure = (IpAdapterAddress)Marshal.PtrToStructure(ptr, typeof(IpAdapterAddress)); IPAddress address = addressStructure.address.MarshalIPAddress(); addressList.InternalAdd(address); ptr = addressStructure.next; } return(addressList); }
void ParseResolvConf() { try { DateTime wt = File.GetLastWriteTime("/etc/resolv.conf"); if (wt <= last_parse) { return; } last_parse = wt; dns_suffix = ""; dns_servers = new IPAddressCollection(); using (StreamReader reader = new StreamReader("/etc/resolv.conf")) { string str; string line; while ((line = reader.ReadLine()) != null) { line = line.Trim(); if (line.Length == 0 || line [0] == '#') { continue; } Match match = ns.Match(line); if (match.Success) { try { str = match.Groups ["address"].Value; str = str.Trim(); dns_servers.InternalAdd(IPAddress.Parse(str)); } catch { } } else { match = search.Match(line); if (match.Success) { str = match.Groups ["domain"].Value; string [] parts = str.Split(','); dns_suffix = parts [0].Trim(); } } } } } catch { } }
IPAddressCollection ParseRouteInfo(string iface) { var col = new IPAddressCollection(); try { using (StreamReader reader = new StreamReader("/proc/net/route")) { string line; reader.ReadLine(); // Ignore first line while ((line = reader.ReadLine()) != null) { line = line.Trim(); if (line.Length == 0) { continue; } string [] parts = line.Split('\t'); if (parts.Length < 3) { continue; } string gw_address = parts [2].Trim(); byte [] ipbytes = new byte [4]; if (gw_address.Length == 8 && iface.Equals(parts [0], StringComparison.OrdinalIgnoreCase)) { for (int i = 0; i < 4; i++) { if (!Byte.TryParse(gw_address.Substring(i * 2, 2), NumberStyles.HexNumber, null, out ipbytes [3 - i])) { continue; } } IPAddress ip = new IPAddress(ipbytes); if (!ip.Equals(IPAddress.Any) && !col.Contains(ip)) { col.InternalAdd(ip); } } } } } catch { } return(col); }