コード例 #1
0
        /// <summary>
        /// Return an InetAddress for each interface that matches the
        /// given subnet specified using CIDR notation.
        /// </summary>
        /// <param name="subnet">subnet specified using CIDR notation</param>
        /// <param name="returnSubinterfaces">whether to return IPs associated with subinterfaces
        ///     </param>
        /// <exception cref="System.ArgumentException">if subnet is invalid</exception>
        public static IList <IPAddress> GetIPs(string subnet, bool returnSubinterfaces)
        {
            IList <IPAddress> addrs = new AList <IPAddress>();

            SubnetUtils.SubnetInfo         subnetInfo = new SubnetUtils(subnet).GetInfo();
            Enumeration <NetworkInterface> nifs;

            try
            {
                nifs = NetworkInterface.GetNetworkInterfaces();
            }
            catch (SocketException e)
            {
                Log.Error("Unable to get host interfaces", e);
                return(addrs);
            }
            while (nifs.MoveNext())
            {
                NetworkInterface nif = nifs.Current;
                // NB: adding addresses even if the nif is not up
                AddMatchingAddrs(nif, subnetInfo, addrs);
                if (!returnSubinterfaces)
                {
                    continue;
                }
                Enumeration <NetworkInterface> subNifs = nif.GetSubInterfaces();
                while (subNifs.MoveNext())
                {
                    AddMatchingAddrs(subNifs.Current, subnetInfo, addrs);
                }
            }
            return(addrs);
        }
コード例 #2
0
ファイル: MachineList.cs プロジェクト: orf53975/hadoop.net
 /// <summary>Accepts a collection of ip/cidr/host addresses</summary>
 /// <param name="hostEntries"/>
 /// <param name="addressFactory">addressFactory to convert host to InetAddress</param>
 public MachineList(ICollection <string> hostEntries, MachineList.InetAddressFactory
                    addressFactory)
 {
     this.addressFactory = addressFactory;
     if (hostEntries != null)
     {
         if ((hostEntries.Count == 1) && (hostEntries.Contains(WildcardValue)))
         {
             all           = true;
             ipAddresses   = null;
             hostNames     = null;
             cidrAddresses = null;
         }
         else
         {
             all = false;
             ICollection <string>           ips   = new HashSet <string>();
             IList <SubnetUtils.SubnetInfo> cidrs = new List <SubnetUtils.SubnetInfo>();
             ICollection <string>           hosts = new HashSet <string>();
             foreach (string hostEntry in hostEntries)
             {
                 //ip address range
                 if (hostEntry.IndexOf("/") > -1)
                 {
                     try
                     {
                         SubnetUtils subnet = new SubnetUtils(hostEntry);
                         subnet.SetInclusiveHostCount(true);
                         cidrs.AddItem(subnet.GetInfo());
                     }
                     catch (ArgumentException e)
                     {
                         Log.Warn("Invalid CIDR syntax : " + hostEntry);
                         throw;
                     }
                 }
                 else
                 {
                     if (InetAddresses.IsInetAddress(hostEntry))
                     {
                         //ip address
                         ips.AddItem(hostEntry);
                     }
                     else
                     {
                         //hostname
                         hosts.AddItem(hostEntry);
                     }
                 }
             }
             ipAddresses   = (ips.Count > 0) ? ips : null;
             cidrAddresses = (cidrs.Count > 0) ? cidrs : null;
             hostNames     = (hosts.Count > 0) ? hosts : null;
         }
     }
     else
     {
         all           = false;
         ipAddresses   = null;
         hostNames     = null;
         cidrAddresses = null;
     }
 }