public IPv4Network(IPv4 ipNetwork, int maskLength) { if (maskLength < 0 || maskLength > IPv4.BitCount) { throw new ArgumentException(); } this.maskLength = maskLength; this.ipNetwork = ipNetwork & IPv4.NetMask(maskLength); }
private static void TestBits() { IPv4 a = new IPv4(0x7f7f7f7f); IPv4 b = new IPv4(0xc1c1c1c1); if ((uint)(a | b) != ~0U) { throw new TestFailedException("OR"); } if ((uint)(a & b) != 0x41414141) { throw new TestFailedException("AND"); } if ((uint)(a ^ b) != 0xbebebebe) { throw new TestFailedException("XOR"); } if ((uint)(~a) != 0x80808080) { throw new TestFailedException("NOT"); } if ((uint)IPv4.NetMask(0) != 0) { throw new TestFailedException("NetMask(0)"); } if ((uint)IPv4.NetMask(17) != 0xffff8000) { throw new TestFailedException("NetMask(17)"); } if ((uint)IPv4.NetMask(32) != 0xffffffffu) { throw new TestFailedException("NetMask(32)"); } try { IPv4 n = IPv4.NetMask(66); throw new TestFailedException("Bad Netmask +"); } catch (ArgumentException) { } try { IPv4 n = IPv4.NetMask(-1); throw new TestFailedException("Bad Netmask -"); } catch (ArgumentException) { } }
public static void TestCount() { for (int i = IPv4.BitCount; i >= 0; i--) { IPv4 addr = IPv4.NetMask(i); if (IPv4.GetMaskLength(addr) != i) { throw new TestFailedException( String.Format("TestCount {0}", i) ); } IPv4 addrDup = IPv4.Parse(addr.ToString()); if (addrDup != addr) { throw new TestFailedException( String.Format("TestCountDup {0}", i) ); } } }
public IPv4Network(IPv4 networkAddress, IPv4 networkMask) { this.maskLength = IPv4.GetMaskLength(networkMask); this.ipNetwork = networkAddress & IPv4.NetMask(this.maskLength); }