Пример #1
0
        public void Connect(int addressIndex, System.Net.NetworkInformation.NetworkInterface networkInterface, int port = 0)
        {
            if (ConnectionSocket == null)
            {
                throw new System.InvalidOperationException("There must be a ConnectionSocket assigned before calling Connect.");
            }

            if (addressIndex < 0)
            {
                throw new System.IndexOutOfRangeException("addressIndex must be > 0 and < HostEntry.AddressList.Length");
            }

            if (networkInterface == null)
            {
                throw new System.ArgumentNullException("networkInterface");
            }

            NetworkInterface = networkInterface;

            RemoteEndPoint = new System.Net.IPEndPoint(RemoteIPHostEntry.AddressList[addressIndex], port);

            Connect();

            LocalEndPoint = ConnectionSocket.LocalEndPoint;

            RemoteAddressInformation = new IPAddressInformation(RemoteIPEndPoint.Address, RemoteAddressInformation.IsDnsEligible, RemoteAddressInformation.IsTransient);
        }
        private static IPAddress GetSubnetMask(IPAddressInformation ip)
        {
            var nativeInterfaceInfo = NetInfo.GetInterfaceInfo();
            var match = nativeInterfaceInfo.FirstOrDefault(ni => ni?.Address != null &&
                                                           ip?.Address != null &&
                                                           Equals(ni.Address, ip.Address));

            return(match?.Netmask);
        }
Пример #3
0
        /// <summary>
        /// Initializes new instance of <see cref="IPAddressInformationAdapter"/>.
        /// </summary>
        /// <param name="info">Information to be used by the adapter.</param>
        public IPAddressInformationAdapter(IPAddressInformation info)
            : base(info)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            _info = info;
        }
        private void DiscoverDevices(IPAddressInformation address)
        {
            Ping pingSender = new Ping();

            string[]      val     = address.Address.ToString().Split('.');
            List <string> devices = new List <string>();

            for (int i = 1; i < 255; i++)
            {
                string currentIpToPing = string.Format("{0}.{1}.{2}.{3}", val[0], val[1], val[2], i);
                if (pingSender.Send(currentIpToPing, 250).Status == IPStatus.Success)
                {
                    devices.Add(currentIpToPing);
                }
            }
            Console.WriteLine("Items: {0}", devices.Count);
        }
Пример #5
0
 void scan()
 {
     foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
     {
         if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
         {
             foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
             {
                 if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                 {
                     ipInfo = ip;
                     break;
                 }
             }
         }
     }
 }
Пример #6
0
            internal NetAddrInfo(IPAddressInformation addr, int ord)
            {
                m_Name      = addr.Address.ToString();
                m_Order     = ord;
                m_Bytes     = addr.Address.GetAddressBytes();
                m_Transient = addr.IsTransient;
                m_Family    = addr.Address.AddressFamily.ToString();
                m_IPv4      = addr.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork;
                m_IPv6      = addr.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6;
                m_Unicast   = addr is UnicastIPAddressInformation;

                if (m_Unicast)
                {
                    var uaddr = (UnicastIPAddressInformation)addr;
                    m_UnicastIPv4Mask             = uaddr.IPv4Mask.GetAddressBytes();
                    m_UnicastPreferredLifetimeSec = uaddr.AddressPreferredLifetime;
                    m_UnicastValidLifetimeSec     = uaddr.AddressValidLifetime;
                    m_UnicastDHCPLeaseLifetimeSec = uaddr.DhcpLeaseLifetime;
                }
            }
Пример #7
0
        public IPAddressInformation GetCurrentIpAddrInfo()
        {
            IPAddressInformation ipInfo = null;

            var obj = networkComboBox.SelectedItem;

            if (obj != null)
            {
                var item = obj as ComboBoxItem;
                if (item != null)
                {
                    var tag = item.Tag;
                    if (tag != null)
                    {
                        ipInfo = tag as IPAddressInformation;
                    }
                }
            }

            return(ipInfo);
        }
Пример #8
0
        /// <summary>
        ///     Gets the local IP address of this computer
        ///     <para>
        ///         It will return <c>localhost</c> if no networks are available, or <c>127.0.0.1</c> if there are multiple
        ///         interfaces, otherwise the actual IP address
        ///     </para>
        /// </summary>
        /// <returns></returns>
        public static string LocalIpAddress()
        {
            //There is no network available, so IDK
            if (!NetworkInterface.GetIsNetworkAvailable())
            {
                return("localhost");
            }

            //Get all the network interfaces
            NetworkInterface[]      networkInterfaces       = NetworkInterface.GetAllNetworkInterfaces();
            List <NetworkInterface> activeNetworkInterfaces = networkInterfaces.AsValueEnumerable().Where(
                networkInterface =>
                networkInterface.OperationalStatus == OperationalStatus.Up && !networkInterface.IsReceiveOnly)
                                                              .ToList();

            //If there is more then one network interface, default to local host
            if (activeNetworkInterfaces.Count is > 1 or < 1)
            {
                return("127.0.0.1");
            }

            //Get the address
            NetworkInterface activeInterface         = activeNetworkInterfaces[0];
            IPAddressInformationCollection addresses = activeInterface.GetIPProperties().AnycastAddresses;

            // ReSharper disable once ForCanBeConvertedToForeach
            for (int i = 0; i < addresses.Count; i++)
            {
                IPAddressInformation information = addresses[i];
                if (information.Address.AddressFamily == AddressFamily.InterNetwork)
                {
                    return(information.Address.ToString());
                }
            }

            //F**k Do I know what to do if we hit here
            return("localhost");
        }
Пример #9
0
 private static IPAddress ToIPAddress(IPAddressInformation info) => info.Address;
 public virtual void Add(IPAddressInformation address)
 {
 }
 public virtual bool Remove(IPAddressInformation address)
 {
 }
 public virtual bool Contains(IPAddressInformation address)
 {
 }
Пример #13
0
 /// <summary>
 /// 检查IP信息是否符合要求。
 /// </summary>
 /// <param name="info">IP信息</param>
 /// <returns>是否符合要求</returns>
 protected abstract bool CheckIPAddressInformation(IPAddressInformation info);
Пример #14
0
 protected override bool CheckIPAddressInformation(IPAddressInformation info)
 {
     return(info.Address.AddressFamily == AddressFamily.InterNetworkV6);
 }
Пример #15
0
 public static IIPAddressInformation ToInterface([CanBeNull] this IPAddressInformation info)
 {
     return((info == null) ? null : new IPAddressInformationAdapter(info));
 }
Пример #16
0
 internal static bool IsDeepEquals(this IPAddressInformation source, IPAddressInformation target)
 {
     return(source.Address.Equals(target.Address) &&
            source.IsDnsEligible == target.IsDnsEligible &&
            source.IsTransient == target.IsTransient);
 }
Пример #17
0
 internal static int GetDeepHashCode(this IPAddressInformation source)
 {
     return(source.Address.GetHashCode() +
            (source.IsDnsEligible ? 1 : -1) +
            (source.IsTransient ? 10 : -10));
 }
Пример #18
0
 /// <summary>
 /// Creates a new NetworkConnection and <see cref="new System.Net.IPHostEntry"/> using the given address.
 /// </summary>
 /// <param name="address">The address</param>
 public IPNetworkConnection(System.Net.IPAddress address) :
     this(CreateIPHostEntry(string.Empty, address))
 {
     RemoteAddressInformation = new IPAddressInformation(System.Net.IPAddress.None, false, false);
 }
Пример #19
0
 /// <summary>
 /// Creates a new NetworkConnection by resolving the given using <see cref="System.Net.Dns.GetHostEntry"/>
 /// </summary>
 /// <param name="hostNameOrAddress">given</param>
 public IPNetworkConnection(string hostNameOrAddress) :
     this(System.Net.Dns.GetHostEntry(hostNameOrAddress))
 {
     RemoteAddressInformation = new IPAddressInformation(System.Net.IPAddress.None, true, false);
 }
Пример #20
0
 internal static string ToDisplayName(this IPAddressInformation ipAddressInfo, NetworkInterface networkInterface)
 {
     return($"{networkInterface.Name} ({ipAddressInfo.Address})");
 }