Example #1
0
        /// <summary>
        /// Returns a collection containing the subnets of this collection given.
        /// </summary>
        /// <param name="source">The <see cref="Collection{IPObject}"/>.</param>
        /// <returns>Collection{IPObject} object containing the subnets.</returns>
        public static Collection <IPObject> AsNetworks(this Collection <IPObject> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            Collection <IPObject> res = new Collection <IPObject>();

            foreach (IPObject i in source)
            {
                if (i is IPNetAddress nw)
                {
                    // Add the subnet calculated from the interface address/mask.
                    var na = nw.NetworkAddress;
                    na.Tag = i.Tag;
                    res.AddItem(na);
                }
                else if (i is IPHost ipHost)
                {
                    // Flatten out IPHost and add all its ip addresses.
                    foreach (var addr in ipHost.GetAddresses())
                    {
                        IPNetAddress host = new IPNetAddress(addr)
                        {
                            Tag = i.Tag
                        };

                        res.AddItem(host);
                    }
                }
            }

            return(res);
        }
Example #2
0
        /// <summary>
        /// Try to parse the address and subnet strings into an IPNetAddress object.
        /// </summary>
        /// <param name="addr">IP address to parse. Can be CIDR or X.X.X.X notation.</param>
        /// <param name="ip">Resultant object.</param>
        /// <returns>True if the values parsed successfully. False if not, resulting in the IP being null.</returns>
        public static bool TryParse(string addr, out IPNetAddress ip)
        {
            if (!string.IsNullOrEmpty(addr))
            {
                addr = addr.Trim();

                // Try to parse it as is.
                if (IPAddress.TryParse(addr, out IPAddress? res))
                {
                    ip = new IPNetAddress(res);
                    return(true);
                }

                // Is it a network?
                string[] tokens = addr.Split('/');

                if (tokens.Length == 2)
                {
                    tokens[0] = tokens[0].TrimEnd();
                    tokens[1] = tokens[1].TrimStart();

                    if (IPAddress.TryParse(tokens[0], out res))
                    {
                        // Is the subnet part a cidr?
                        if (byte.TryParse(tokens[1], out byte cidr))
                        {
                            ip = new IPNetAddress(res, cidr);
                            return(true);
                        }

                        // Is the subnet in x.y.a.b form?
                        if (IPAddress.TryParse(tokens[1], out IPAddress? mask))
                        {
                            ip = new IPNetAddress(res, MaskToCidr(mask));
                            return(true);
                        }
                    }
                }
            }

            ip = None;
            return(false);
        }
Example #3
0
        public static bool TryParse(string host, out IPHost hostObj)
        {
            if (string.IsNullOrWhiteSpace(host))
            {
                hostObj = IPHost.None;
                return(false);
            }

            // See if it's an IPv6 with port address e.g. [::1] or [::1]:120.
            int i = host.IndexOf(']', StringComparison.Ordinal);

            if (i != -1)
            {
                return(TryParse(host.Remove(i - 1).TrimStart(' ', '['), out hostObj));
            }

            if (IPNetAddress.TryParse(host, out var netAddress))
            {
                // Host name is an ip address, so fake resolve.
                hostObj = new IPHost(host, netAddress.Address);
                return(true);
            }

            // Is it a host, IPv4/6 with/out port?
            string[] hosts = host.Split(':');

            if (hosts.Length <= 2)
            {
                // This is either a hostname: port, or an IP4:port.
                host = hosts[0];

                if (string.Equals("localhost", host, StringComparison.OrdinalIgnoreCase))
                {
                    hostObj = new IPHost(host);
                    return(true);
                }

                if (IPAddress.TryParse(host, out var netIP))
                {
                    // Host name is an ip address, so fake resolve.
                    hostObj = new IPHost(host, netIP);
                    return(true);
                }
            }
            else
            {
                // Invalid host name, as it cannot contain :
                hostObj = new IPHost(string.Empty, IPAddress.None);
                return(false);
            }

            // Use regular expression as CheckHostName isn't RFC5892 compliant.
            // Modified from gSkinner's expression at https://stackoverflow.com/questions/11809631/fully-qualified-domain-name-validation
            string pattern = @"(?im)^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){0,127}(?![0-9]*$)[a-z0-9-]+\.?)$";

            if (Regex.IsMatch(host, pattern))
            {
                hostObj = new IPHost(host);
                return(true);
            }

            hostObj = IPHost.None;
            return(false);
        }
Example #4
0
        public static bool TryParse(string host, out IPHost hostObj)
        {
            if (!string.IsNullOrEmpty(host))
            {
                // See if it's an IPv6 with port address e.g. [::1]:120.
                int i = host.IndexOf("]:", StringComparison.OrdinalIgnoreCase);
                if (i != -1)
                {
                    return(TryParse(host.Remove(i - 1).TrimStart(' ', '['), out hostObj));
                }
                else
                {
                    // See if it's an IPv6 in [] with no port.
                    i = host.IndexOf(']', StringComparison.OrdinalIgnoreCase);
                    if (i != -1)
                    {
                        return(TryParse(host.Remove(i - 1).TrimStart(' ', '['), out hostObj));
                    }

                    // Is it a host or IPv4 with port?
                    string[] hosts = host.Split(':');

                    if (hosts.Length > 2)
                    {
                        hostObj = new IPHost(string.Empty, IPAddress.None);
                        return(false);
                    }

                    // Remove port from IPv4 if it exists.
                    host = hosts[0];

                    if (string.Equals("localhost", host, StringComparison.OrdinalIgnoreCase))
                    {
                        hostObj = new IPHost(host, new IPAddress(Ipv4Loopback));
                        return(true);
                    }

                    if (IPNetAddress.TryParse(host, out IPNetAddress netIP))
                    {
                        // Host name is an ip address, so fake resolve.
                        hostObj = new IPHost(host, netIP.Address);
                        return(true);
                    }
                }

                // Only thing left is to see if it's a host string.
                if (!string.IsNullOrEmpty(host))
                {
                    // Use regular expression as CheckHostName isn't RFC5892 compliant.
                    // Modified from gSkinner's expression at https://stackoverflow.com/questions/11809631/fully-qualified-domain-name-validation
                    Regex re = new Regex(@"^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){0,127}(?![0-9]*$)[a-z0-9-]+\.?)$", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                    if (re.Match(host).Success)
                    {
                        hostObj = new IPHost(host);
                        return(true);
                    }
                }
            }

            hostObj = IPHost.None;
            return(false);
        }