Exemplo n.º 1
0
        private async Task <IPAddress> GetAvailableAddress(NetworkConfiguration config, CancellationToken cancellationToken)
        {
            var allAddresses =
                await context.NetworkDevices.Select(nd => nd.ClientAddress).ToListAsync(cancellationToken);

            IPAddressRange range       = new IPAddressRange(config.PoolMin, config.PoolMax);
            var            freeAddress = range.AsEnumerable().FirstOrDefault(address => !allAddresses.Contains(address));

            return(freeAddress ?? throw new ApplicationException("Unable to assign new address"));
        }
Exemplo n.º 2
0
        public static IPAddress[] ScanLocalIps()
        {
            var results = new List <IPAddress>();

            foreach (var ni in NetworkInterface.GetAllNetworkInterfaces())
            {
                var gateways = GetGatewayAddresses();
                foreach (var gw in gateways)
                {
                    var range = new IPAddressRange(gw, 24 /*GetMaskLength(ni.GetIPProperties().UnicastAddresses.First().IPv4Mask)*/);
                    results.AddRange(range.AsEnumerable().Where(_ => Ping(_, 50)));
                }
            }

            return(results.ToArray());
        }
Exemplo n.º 3
0
    public void Test1()
    {
        var minIp = new IPAddress(new byte[]{ 192, 168, 8, 100 });
        var maxIp = new IPAddress(new byte[]{ 192, 168, 8, 200 });

        IPAddressRange range = new IPAddressRange(minIp, maxIp);

        foreach (var ipAddress in range.AsEnumerable())
        {

            Console.WriteLine(ipAddress);
            
        }
        

        Assert.Pass();
    }
Exemplo n.º 4
0
        protected virtual IPAddress AddressAllocation(int id)
        {
            if (0 == id)
            {
                return(null);
            }
            lock (_addressAllocation)
            {
                if (_addressAllocation.TryGetValue(id, out IPAddress address) && address != null)
                {
                    return(address);
                }
                foreach (IPAddress i in _dhcpAddressAllocationRange.AsEnumerable())
                {
                    if (i == null)
                    {
                        continue;
                        fixed(byte *p = i.GetAddressBytes())
                        {
                            byte l = p[3];

                            if (l <= 1 || l >= 255)
                            {
                                continue;
                            }
                        }

                        if (_assignedAddresses.Contains(i))
                        {
                            continue;
                        }
                        _addressAllocation[id] = i;
                        _assignedAddresses.Add(i);
                        return(i);
                }
            }
            return(null);
        }
Exemplo n.º 5
0
 public static IEnumerable <Scan> FromRange(IPAddressRange range, int timeout = 30)
 {
     return(range.AsEnumerable().Select(ip => new Scan(ip.ToString(), timeout)));
 }