fCraft.IPAddressUtil.NetMask C# (CSharp) Method

NetMask() public static method

Creates an IPv4 mask for the given CIDR range.
range is over 32
public static NetMask ( byte range ) : uint
range byte
return uint
        public static uint NetMask( byte range ) {
            if( range > 32 ) throw new ArgumentOutOfRangeException( "range" );
            if( range == 0 ) {
                return 0;
            } else {
                return 0xffffffff << ( 32 - range );
            }
        }
    }

Usage Example

Exemplo n.º 1
0
        public static PlayerInfo[] FindPlayersCidr([NotNull] IPAddress address, byte range, int limit)
        {
            if (address == null)
            {
                throw new ArgumentNullException("address");
            }
            if (range > 32)
            {
                throw new ArgumentOutOfRangeException("range");
            }
            if (limit < 0)
            {
                throw new ArgumentOutOfRangeException("limit");
            }
            CheckIfLoaded();
            List <PlayerInfo> result = new List <PlayerInfo>();
            int  count      = 0;
            uint addressInt = address.AsUInt();
            uint netMask    = IPAddressUtil.NetMask(range);

            PlayerInfo[] cache = PlayerInfoList;
            for (int i = 0; i < cache.Length; i++)
            {
                if (cache[i].LastIP.Match(addressInt, netMask))
                {
                    result.Add(cache[i]);
                    count++;
                    if (count >= limit)
                    {
                        return(result.ToArray());
                    }
                }
            }
            return(result.ToArray());
        }