public IPv6Network(IPv6 ipNetwork, int maskLength) { if (maskLength < 0 || maskLength > IPv6.BitCount) { throw new ArgumentException(); } this.maskLength = maskLength; this.ipNetwork = ipNetwork & IPv6.NetMask(maskLength); }
public static void TestCount() { for (int i = 0; i < IPv6.BitCount; i++) { IPv6 addr = IPv6.NetMask(i); if (IPv6.GetMaskLength(addr) != i) { throw new TestFailedException( String.Format("TestCount {0}", i) ); } IPv6 addrDup = IPv6.Parse(addr.ToString()); if (addrDup != addr) { throw new TestFailedException( String.Format("TestCountDup {0}", i) ); } } }
private static void TestBits() { IPv6 a = new IPv6(0x7f7f7f7f, 0x7f7f7f7f, 0x7f7f7f7f, 0x7f7f7f7f); IPv6 b = new IPv6(0xc1c1c1c1, 0xc1c1c1c1, 0xc1c1c1c1, 0xc1c1c1c1); IPv6 goal; goal = new IPv6(~0U, ~0U, ~0U, ~0U); if ((a | b) != goal) { throw new TestFailedException("OR"); } goal = new IPv6(0x41414141, 0x41414141, 0x41414141, 0x41414141); if ((a & b) != goal) { throw new TestFailedException("AND"); } goal = new IPv6(0xbebebebe, 0xbebebebe, 0xbebebebe, 0xbebebebe); if ((a ^ b) != goal) { throw new TestFailedException("XOR"); } goal = new IPv6(0x80808080, 0x80808080, 0x80808080, 0x80808080); if (~a != goal) { throw new TestFailedException("NOT"); } // Test netmask for (int i = 0; i < 128; i++) { IPv6 mask = IPv6.NetMask(i); goal = IPv6.AllOnes << (128 - i); if (mask != goal) { Console.WriteLine("{0} {1} {2}", i, mask, goal); throw new TestFailedException("NetMask"); } goal = ~(IPv6.AllOnes >> i); if (mask != goal) { Console.WriteLine("{0} {1} {2}", i, mask, goal); throw new TestFailedException("NetMask2"); } } try { IPv6 n = IPv6.NetMask(129); throw new TestFailedException("Bad Netmask +"); } catch (ArgumentException) { } try { IPv6 n = IPv6.NetMask(-1); throw new TestFailedException("Bad Netmask -"); } catch (ArgumentException) { } }
public IPv6Network(IPv6 networkAddress, IPv6 networkMask) { this.maskLength = IPv6.GetMaskLength(networkMask); this.ipNetwork = networkAddress & IPv6.NetMask(this.maskLength); }