Exemple #1
0
        public static Netmask?GetNetmaskFromString(string Netmask)
        {
            Netmask Result = new Netmask();

            var NetmaskSplit = Netmask.Split(new char[] { '/' }, 2);

            if (NetmaskSplit.Length != 2)
            {
                return(null);
            }

            var NetmaskSegments = NetmaskSplit[0].Split(new char[] { '.' }, 4);

            if (NetmaskSegments.Length != 4)
            {
                return(null);
            }

            UInt32 Mask = 0;

            if (!UInt32.TryParse(NetmaskSplit[1], out Mask))
            {
                return(null);
            }

            UInt32 NetmaskValue = 0;
            UInt32 MaskValue    = Mask == 0 ? 0 : (~(((UInt32)1 << (int)(32 - Mask)) - 1));

            byte TempSegment = 0;

            for (int seg = 0; seg < 4; seg++)
            {
                if (!byte.TryParse(NetmaskSegments[seg], out TempSegment))
                {
                    return(null);
                }

                NetmaskValue  = NetmaskValue << 8;
                NetmaskValue |= TempSegment;
            }

            Result.NetmaskString = Netmask;
            Result.MaskBits      = Mask;
            Result.MaskValue     = MaskValue;
            Result.MaskedAddress = NetmaskValue & MaskValue;

            return(Result);
        }
Exemple #2
0
        public static Netmask?GetNetmaskFromIPAndGenmask(string IP, string Genmask)
        {
            Netmask Result = new Netmask();

            var IPSegments = IP.Split(new char[] { '.' }, 4);

            uint Mask = MaskFromGenmask(Genmask);

            UInt32 NetmaskValue = 0;
            UInt32 MaskValue    = Mask == 32 ? (~(UInt32)0) : (~(((UInt32)1 << (int)(32 - Mask)) - 1));

            byte TempSegment = 0;

            for (int seg = 0; seg < 4; seg++)
            {
                if (!byte.TryParse(IPSegments[seg], out TempSegment))
                {
                    return(null);
                }

                NetmaskValue  = NetmaskValue << 8;
                NetmaskValue |= TempSegment;
            }

            Result.MaskedAddress = NetmaskValue & MaskValue;
            Result.NetmaskString = string.Format("{0}.{1}.{2}.{3}/{4}",
                                                 (Result.MaskedAddress >> 24) & 0xFF,
                                                 (Result.MaskedAddress >> 16) & 0xFF,
                                                 (Result.MaskedAddress >> 8) & 0xFF,
                                                 Result.MaskedAddress & 0xFF,
                                                 Mask);
            Result.MaskBits  = Mask;
            Result.MaskValue = MaskValue;

            return(Result);
        }
Exemple #3
0
 public static bool IPMatchesNetmask(UInt32 IPAsInteger, Netmask Netmask)
 {
     return((IPAsInteger & Netmask.MaskValue) == (Netmask.MaskedAddress & Netmask.MaskValue));
 }