Пример #1
0
        protected unsafe void ProcessIpv6Address(Interop.Sys.IpAddressInfo *addressInfo, uint scopeId)
        {
            IPAddress address = IPAddressUtil.GetIPAddressFromNativeInfo(addressInfo);

            AddAddress(address);
            _ipv6ScopeId = scopeId;
        }
        private static unsafe T TransformNetworkInterfacess <T>(Func <int, IntPtr, int, IntPtr, T> transform)
        {
            int interfaceCount = 0;
            int addressCount   = 0;

            Interop.Sys.NetworkInterfaceInfo *networkInterfaceInfo = null;
            Interop.Sys.IpAddressInfo *       addressInfo          = null;

            if (Interop.Sys.GetNetworkInterfaces(ref interfaceCount, ref networkInterfaceInfo, ref addressCount, ref addressInfo) != 0)
            {
                string message = Interop.Sys.GetLastErrorInfo().GetErrorMessage();
                throw new NetworkInformationException(message);
            }

            // the native implementation of Interop.Sys.GetNetworkInterfaces allocates one block of memory
            // for both networkInterfaceInfo and addressInfo so we only need to call free once pointing at
            // the start of the network interfaces list
            var globalMemory = (IntPtr)networkInterfaceInfo;

            try
            {
                return(transform(interfaceCount, (IntPtr)networkInterfaceInfo, addressCount, (IntPtr)addressInfo));
            }
            finally
            {
                Marshal.FreeHGlobal(globalMemory);
            }
        }
Пример #3
0
        protected unsafe void ProcessIpv4Address(Interop.Sys.IpAddressInfo *addressInfo)
        {
            IPAddress ipAddress = IPAddressUtil.GetIPAddressFromNativeInfo(addressInfo);

            AddAddress(ipAddress, addressInfo->PrefixLength);
            _index = addressInfo->InterfaceIndex;
        }
Пример #4
0
        protected unsafe void ProcessIpv4Address(Interop.Sys.IpAddressInfo *addressInfo, Interop.Sys.IpAddressInfo *netMask)
        {
            IPAddress ipAddress      = IPAddressUtil.GetIPAddressFromNativeInfo(addressInfo);
            IPAddress netMaskAddress = IPAddressUtil.GetIPAddressFromNativeInfo(netMask);

            AddAddress(ipAddress);
            _netMasks[ipAddress] = netMaskAddress;
        }
Пример #5
0
        protected static unsafe void ProcessIpv6Address(UnixNetworkInterface uni,
                                                        Interop.Sys.IpAddressInfo *addressInfo,
                                                        uint scopeId)
        {
            IPAddress address = IPAddressUtil.GetIPAddressFromNativeInfo(addressInfo);

            uni.AddAddress(address);
            uni._ipv6ScopeId = scopeId;
        }
Пример #6
0
        protected unsafe void ProcessIpv6Address(Interop.Sys.IpAddressInfo *addressInfo, uint scopeId)
        {
            IPAddress address = IPAddressUtil.GetIPAddressFromNativeInfo(addressInfo);

            address.ScopeId = scopeId;
            AddAddress(address, addressInfo->PrefixLength);
            _ipv6ScopeId = scopeId;
            _index       = addressInfo->InterfaceIndex;
        }
Пример #7
0
        protected static unsafe void ProcessIpv4Address(UnixNetworkInterface uni,
                                                        Interop.Sys.IpAddressInfo *addressInfo,
                                                        Interop.Sys.IpAddressInfo *netMask)
        {
            IPAddress ipAddress      = IPAddressUtil.GetIPAddressFromNativeInfo(addressInfo);
            IPAddress netMaskAddress = IPAddressUtil.GetIPAddressFromNativeInfo(netMask);

            uni.AddAddress(ipAddress);
            uni._netMasks[ipAddress] = netMaskAddress;
        }
Пример #8
0
        internal unsafe void AddAddress(Interop.Sys.IpAddressInfo *addressInfo)
        {
            var address = new IPAddress(new ReadOnlySpan <byte>(addressInfo->AddressBytes, addressInfo->NumAddressBytes));

            if (address.IsIPv6LinkLocal)
            {
                address.ScopeId = addressInfo->InterfaceIndex;
            }

            AddAddress(address, addressInfo->PrefixLength);
        }
Пример #9
0
        /// <summary>
        /// Copies the address bytes out of the given native info's buffer and constructs a new IPAddress.
        /// </summary>
        /// <param name="addressInfo">A pointer to a native IpAddressInfo structure.</param>
        /// <returns>A new IPAddress created with the information in the native structure.</returns>
        public static unsafe IPAddress GetIPAddressFromNativeInfo(Interop.Sys.IpAddressInfo *addressInfo)
        {
            byte[] ipBytes = new byte[addressInfo->NumAddressBytes];
            fixed(byte *ipArrayPtr = ipBytes)
            {
                Buffer.MemoryCopy(addressInfo->AddressBytes, ipArrayPtr, ipBytes.Length, ipBytes.Length);
            }

            IPAddress ipAddress = new IPAddress(ipBytes);

            return(ipAddress);
        }
Пример #10
0
        protected static unsafe void ProcessIpv6Address(UnixNetworkInterface uni,
                                                        Interop.Sys.IpAddressInfo *addressInfo,
                                                        uint scopeId)
        {
            byte[] ipBytes = new byte[addressInfo->NumAddressBytes];
            fixed(byte *ipArrayPtr = ipBytes)
            {
                Buffer.MemoryCopy(addressInfo->AddressBytes, ipArrayPtr, ipBytes.Length, ipBytes.Length);
            }

            IPAddress address = new IPAddress(ipBytes);

            uni.AddAddress(address);
            uni._ipv6ScopeId = scopeId;
        }
Пример #11
0
        protected static unsafe void ProcessIpv4Address(UnixNetworkInterface uni,
                                                        Interop.Sys.IpAddressInfo *addressInfo,
                                                        Interop.Sys.IpAddressInfo *netMask)
        {
            byte[] ipBytes = new byte[addressInfo->NumAddressBytes];
            fixed(byte *ipArrayPtr = ipBytes)
            {
                Buffer.MemoryCopy(addressInfo->AddressBytes, ipArrayPtr, ipBytes.Length, ipBytes.Length);
            }

            IPAddress ipAddress = new IPAddress(ipBytes);

            byte[] ipBytes2 = new byte[netMask->NumAddressBytes];
            fixed(byte *ipArrayPtr = ipBytes2)
            {
                Buffer.MemoryCopy(netMask->AddressBytes, ipArrayPtr, ipBytes2.Length, ipBytes2.Length);
            }

            IPAddress netMaskAddress = new IPAddress(ipBytes2);

            uni.AddAddress(ipAddress);
            uni._netMasks[ipAddress] = netMaskAddress;
        }
Пример #12
0
        public static unsafe NetworkInterface[] GetLinuxNetworkInterfaces()
        {
            var systemProperties = new LinuxNetworkInterfaceSystemProperties();

            int interfaceCount = 0;
            int addressCount   = 0;

            Interop.Sys.NetworkInterfaceInfo *nii = null;
            Interop.Sys.IpAddressInfo *       ai  = null;
            IntPtr globalMemory = (IntPtr)null;

            if (Interop.Sys.GetNetworkInterfaces(ref interfaceCount, ref nii, ref addressCount, ref ai) != 0)
            {
                string message = Interop.Sys.GetLastErrorInfo().GetErrorMessage();
                throw new NetworkInformationException(message);
            }

            globalMemory = (IntPtr)nii;
            try
            {
                NetworkInterface[] interfaces = new NetworkInterface[interfaceCount];
                Dictionary <int, LinuxNetworkInterface> interfacesByIndex = new Dictionary <int, LinuxNetworkInterface>(interfaceCount);

                for (int i = 0; i < interfaceCount; i++)
                {
                    var lni = new LinuxNetworkInterface(Marshal.PtrToStringAnsi((IntPtr)nii->Name) !, nii->InterfaceIndex, systemProperties);
                    lni._interfaceType     = (NetworkInterfaceType)nii->HardwareType;
                    lni._speed             = nii->Speed;
                    lni._operationalStatus = (OperationalStatus)nii->OperationalState;
                    lni._mtu = nii->Mtu;
                    lni._supportsMulticast = nii->SupportsMulticast != 0;

                    if (nii->NumAddressBytes > 0)
                    {
                        lni._physicalAddress = new PhysicalAddress(new ReadOnlySpan <byte>(nii->AddressBytes, nii->NumAddressBytes).ToArray());
                    }

                    interfaces[i] = lni;
                    interfacesByIndex.Add(nii->InterfaceIndex, lni);
                    nii++;
                }

                while (addressCount != 0)
                {
                    var address = new IPAddress(new ReadOnlySpan <byte>(ai->AddressBytes, ai->NumAddressBytes));
                    if (address.IsIPv6LinkLocal)
                    {
                        address.ScopeId = ai->InterfaceIndex;
                    }

                    if (interfacesByIndex.TryGetValue(ai->InterfaceIndex, out LinuxNetworkInterface? lni))
                    {
                        lni.AddAddress(address, ai->PrefixLength);
                    }

                    ai++;
                    addressCount--;
                }

                return(interfaces);
            }
            finally
            {
                Marshal.FreeHGlobal(globalMemory);
            }
        }
Пример #13
0
 private static unsafe void OnGatewayFound(void *pContext, Interop.Sys.IpAddressInfo *gatewayAddressInfo)
 {
     ref Context context = ref Unsafe.As <byte, Context>(ref *(byte *)pContext);
Пример #14
0
 private static unsafe void ProcessIpv6Address(void *pContext, byte *ifaceName, Interop.Sys.IpAddressInfo *ipAddr, uint *scopeId)
 {
     (*(int *)pContext)++;
 }
Пример #15
0
 private static unsafe void ProcessIpv4Address(void *pContext, byte *ifaceName, Interop.Sys.IpAddressInfo *ipAddr)
 {
     ref Context context = ref Unsafe.As <byte, Context>(ref *(byte *)pContext);
Пример #16
0
        /// <summary>
        /// Copies the address bytes out of the given native info's buffer and constructs a new IPAddress.
        /// </summary>
        /// <param name="addressInfo">A pointer to a native IpAddressInfo structure.</param>
        /// <returns>A new IPAddress created with the information in the native structure.</returns>
        public static unsafe IPAddress GetIPAddressFromNativeInfo(Interop.Sys.IpAddressInfo *addressInfo)
        {
            IPAddress ipAddress = new IPAddress(new ReadOnlySpan <byte>(addressInfo->AddressBytes, addressInfo->NumAddressBytes));

            return(ipAddress);
        }