예제 #1
0
        private static unsafe GatewayIPAddressInformationCollection GetGatewayAddresses(int interfaceIndex)
        {
            HashSet <IPAddress> addressSet = new HashSet <IPAddress>();

            if (Interop.Sys.EnumerateGatewayAddressesForInterface((uint)interfaceIndex,
                                                                  (gatewayAddressInfo) =>
            {
                byte[] ipBytes = new byte[gatewayAddressInfo->NumAddressBytes];
                fixed(byte *ipArrayPtr = ipBytes)
                {
                    Buffer.MemoryCopy(gatewayAddressInfo->AddressBytes, ipArrayPtr, ipBytes.Length, ipBytes.Length);
                }
                IPAddress ipAddress = new IPAddress(ipBytes);
                addressSet.Add(ipAddress);
            }) == -1)
            {
                throw new NetworkInformationException(SR.net_PInvokeError);
            }

            GatewayIPAddressInformationCollection collection = new GatewayIPAddressInformationCollection();

            foreach (IPAddress address in addressSet)
            {
                collection.InternalAdd(new SimpleGatewayIPAddressInformation(address));
            }

            return(collection);
        }
예제 #2
0
        internal SystemIPv4InterfaceProperties(FixedInfo fixedInfo, IpAdapterInfo ipAdapterInfo)
        {
            this.index            = ipAdapterInfo.index;
            this.routingEnabled   = fixedInfo.EnableRouting;
            this.dhcpEnabled      = ipAdapterInfo.dhcpEnabled;
            this.haveWins         = ipAdapterInfo.haveWins;
            this.gatewayAddresses = ipAdapterInfo.gatewayList.ToIPGatewayAddressCollection();
            this.dhcpAddresses    = ipAdapterInfo.dhcpServer.ToIPAddressCollection();
            IPAddressCollection addresss  = ipAdapterInfo.primaryWinsServer.ToIPAddressCollection();
            IPAddressCollection addresss2 = ipAdapterInfo.secondaryWinsServer.ToIPAddressCollection();

            this.winsServerAddresses = new IPAddressCollection();
            foreach (IPAddress address in addresss)
            {
                this.winsServerAddresses.InternalAdd(address);
            }
            foreach (IPAddress address2 in addresss2)
            {
                this.winsServerAddresses.InternalAdd(address2);
            }
            SystemIPv4InterfaceStatistics statistics = new SystemIPv4InterfaceStatistics((long)this.index);

            this.mtu = (uint)statistics.Mtu;
            if (ComNetOS.IsWin2K)
            {
                this.GetPerAdapterInfo(ipAdapterInfo.index);
            }
            else
            {
                this.dnsAddresses = fixedInfo.DnsAddresses;
            }
        }
        internal static GatewayIPAddressInformationCollection ToGatewayIpAddressInformationCollection(IPAddressCollection addresses)
        {
            GatewayIPAddressInformationCollection gatewayList = new GatewayIPAddressInformationCollection();

            foreach (IPAddress address in addresses)
            {
                gatewayList.InternalAdd(new SystemGatewayIPAddressInformation(address));
            }
            return(gatewayList);
        }
 public LinuxIPInterfaceProperties(LinuxNetworkInterface lni)
     : base(lni)
 {
     _linuxNetworkInterface = lni;
     _gatewayAddresses      = GetGatewayAddresses();
     _dhcpServerAddresses   = GetDhcpServerAddresses();
     _winsServerAddresses   = GetWinsServerAddresses();
     _ipv4Properties        = new LinuxIPv4InterfaceProperties(lni);
     _ipv6Properties        = new LinuxIPv6InterfaceProperties(lni);
 }
예제 #5
0
        static void AddSubsequently(IntPtr head, GatewayIPAddressInformationCollection col)
        {
            Win32_IP_ADDR_STRING a;

            for (IntPtr p = head; p != IntPtr.Zero; p = a.Next)
            {
                a = (Win32_IP_ADDR_STRING)Marshal.PtrToStructure(p, typeof(Win32_IP_ADDR_STRING));
                col.InternalAdd(new SystemGatewayIPAddressInformation(IPAddress.Parse(a.IpAddress)));
            }
        }
 public LinuxIPInterfaceProperties(LinuxNetworkInterface lni, LinuxNetworkInterface.LinuxNetworkInterfaceSystemProperties systemProperties)
     : base(lni, globalConfig: true)
 {
     _linuxNetworkInterface = lni;
     _gatewayAddresses      = GetGatewayAddresses(systemProperties);
     _dhcpServerAddresses   = GetDhcpServerAddresses();
     _winsServerAddresses   = GetWinsServerAddresses();
     _dnsSuffix             = systemProperties.DnsSuffix;
     _dnsAddresses          = systemProperties.DnsAddresses;
     _ipv4Properties        = new LinuxIPv4InterfaceProperties(lni);
     _ipv6Properties        = new LinuxIPv6InterfaceProperties(lni);
 }
예제 #7
0
        internal GatewayIPAddressInformationCollection ToIPGatewayAddressCollection()
        {
            IpAddrString str = this;
            GatewayIPAddressInformationCollection informations = new GatewayIPAddressInformationCollection();

            if (str.IpAddress.Length != 0)
            {
                informations.InternalAdd(new SystemGatewayIPAddressInformation(IPAddress.Parse(str.IpAddress)));
            }
            while (str.Next != IntPtr.Zero)
            {
                str = (IpAddrString)Marshal.PtrToStructure(str.Next, typeof(IpAddrString));
                if (str.IpAddress.Length != 0)
                {
                    informations.InternalAdd(new SystemGatewayIPAddressInformation(IPAddress.Parse(str.IpAddress)));
                }
            }
            return(informations);
        }
예제 #8
0
        private static unsafe GatewayIPAddressInformationCollection GetGatewayAddresses(int interfaceIndex)
        {
            Context context;

            context._interfaceIndex = interfaceIndex;
            context._addressSet     = new HashSet <IPAddress>();
            if (Interop.Sys.EnumerateGatewayAddressesForInterface(Unsafe.AsPointer(ref context), (uint)interfaceIndex, &OnGatewayFound) == -1)
            {
                throw new NetworkInformationException(SR.net_PInvokeError);
            }

            GatewayIPAddressInformationCollection collection = new GatewayIPAddressInformationCollection();

            foreach (IPAddress address in context._addressSet)
            {
                collection.InternalAdd(new SimpleGatewayIPAddressInformation(address));
            }

            return(collection);
        }
예제 #9
0
        internal SystemIPInterfaceProperties(Interop.IpHlpApi.FIXED_INFO fixedInfo, Interop.IpHlpApi.IpAdapterAddresses ipAdapterAddresses)
        {
            _adapterFlags      = ipAdapterAddresses.flags;
            _dnsSuffix         = ipAdapterAddresses.dnsSuffix;
            _dnsEnabled        = fixedInfo.enableDns;
            _dynamicDnsEnabled = ((ipAdapterAddresses.flags & Interop.IpHlpApi.AdapterFlags.DnsEnabled) > 0);

            _multicastAddresses = SystemMulticastIPAddressInformation.ToMulticastIpAddressInformationCollection(
                Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressInformationCollection(ipAdapterAddresses.firstMulticastAddress));
            _dnsAddresses     = Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressCollection(ipAdapterAddresses.firstDnsServerAddress);
            _anycastAddresses = Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressInformationCollection(
                ipAdapterAddresses.firstAnycastAddress);
            _unicastAddresses = SystemUnicastIPAddressInformation.MarshalUnicastIpAddressInformationCollection(
                ipAdapterAddresses.firstUnicastAddress);
            _winsServersAddresses = Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressCollection(
                ipAdapterAddresses.firstWinsServerAddress);
            _gatewayAddresses = SystemGatewayIPAddressInformation.ToGatewayIpAddressInformationCollection(
                Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressCollection(ipAdapterAddresses.firstGatewayAddress));

            _dhcpServers = new InternalIPAddressCollection();
            if (ipAdapterAddresses.dhcpv4Server.address != IntPtr.Zero)
            {
                _dhcpServers.InternalAdd(ipAdapterAddresses.dhcpv4Server.MarshalIPAddress());
            }

            if (ipAdapterAddresses.dhcpv6Server.address != IntPtr.Zero)
            {
                _dhcpServers.InternalAdd(ipAdapterAddresses.dhcpv6Server.MarshalIPAddress());
            }

            if ((_adapterFlags & Interop.IpHlpApi.AdapterFlags.IPv4Enabled) != 0)
            {
                _ipv4Properties = new SystemIPv4InterfaceProperties(fixedInfo, ipAdapterAddresses);
            }

            if ((_adapterFlags & Interop.IpHlpApi.AdapterFlags.IPv6Enabled) != 0)
            {
                _ipv6Properties = new SystemIPv6InterfaceProperties(ipAdapterAddresses.ipv6Index,
                                                                    ipAdapterAddresses.mtu, ipAdapterAddresses.zoneIndices);
            }
        }
예제 #10
0
        // 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);
            }
        }
        // /proc/net/route contains some information about gateway addresses,
        // and seperates the information about by each interface.
        public GatewayIPAddressInformationCollection GetGatewayAddresses()
        {
            GatewayIPAddressInformationCollection collection = new GatewayIPAddressInformationCollection();

            // Columns are as follows (first-line header):
            // Iface  Destination  Gateway  Flags  RefCnt  Use  Metric  Mask  MTU  Window  IRTT
            string[] fileLines = File.ReadAllLines(NetworkFiles.Ipv4RouteFile);
            foreach (string line in fileLines)
            {
                if (line.StartsWith(_linuxNetworkInterface.Name))
                {
                    StringParser parser = new StringParser(line, '\t', skipEmpty: true);
                    parser.MoveNext();
                    parser.MoveNextOrFail();
                    string    gatewayIPHex = parser.MoveAndExtractNext();
                    long      addressValue = Convert.ToInt64(gatewayIPHex, 16);
                    IPAddress address      = new IPAddress(addressValue);
                    collection.InternalAdd(new SimpleGatewayIPAddressInformation(address));
                }
            }

            return(collection);
        }
예제 #12
0
 public BsdIpInterfaceProperties(BsdNetworkInterface oni, int mtu) : base(oni)
 {
     _ipv4Properties   = new BsdIPv4InterfaceProperties(oni, mtu);
     _ipv6Properties   = new BsdIPv6InterfaceProperties(oni, mtu);
     _gatewayAddresses = GetGatewayAddresses(oni.Index);
 }