/// <summary> /// Determines whether two Object instances are equal. /// </summary> /// <param name="o">Object to be compared to.</param> /// <returns>True if <c>o</c> is an IPv6 address and numerically /// the same as instance.</returns> public override bool Equals(object o) { if (o is IPv6) { IPv6 ipo = (IPv6)o; return(this == ipo); } return(false); }
public IPv6Network(IPv6 ipNetwork, int maskLength) { if (maskLength < 0 || maskLength > IPv6.BitCount) { throw new ArgumentException(); } this.maskLength = maskLength; this.ipNetwork = ipNetwork & IPv6.NetMask(maskLength); }
private static void TestRoll() { IPv6 a = new IPv6(0x01234567, 0x89abcdef, 0x02461357, 0x8ace9bdf); if ((a << 4) != new IPv6(0x12345678, 0x9abcdef0, 0x24613578, 0xace9bdf0)) { throw new TestFailedException("a << 4"); } if ((a << 36) != new IPv6(0x9abcdef0, 0x24613578, 0xace9bdf0, 0)) { throw new TestFailedException("a << 36"); } if ((a << 68) != new IPv6(0x24613578, 0xace9bdf0, 0, 0)) { throw new TestFailedException("a << 68"); } if ((a << 100) != new IPv6(0xace9bdf0, 0, 0, 0)) { throw new TestFailedException("a << 100"); } if ((a << 128) != new IPv6(0, 0, 0, 0)) { throw new TestFailedException("a << 128"); } if ((a >> 4) != new IPv6(0x00123456, 0x789abcde, 0xf0246135, 0x78ace9bd)) { throw new TestFailedException("a >> 4"); } if ((a >> 36) != new IPv6(0, 0x00123456, 0x789abcde, 0xf0246135)) { throw new TestFailedException("a >> 36"); } if ((a >> 68) != new IPv6(0, 0, 0x00123456, 0x789abcde)) { throw new TestFailedException("a >> 68"); } if ((a >> 100) != new IPv6(0, 0, 0, 0x00123456)) { throw new TestFailedException("a >> 100"); } if ((a >> 128) != new IPv6(0, 0, 0, 0)) { throw new TestFailedException("a >> 128"); } }
/// <summary> /// Get the mask length from an IPv6 address representing a netmask. /// </summary> /// <param name="netmask"></param> public static int GetMaskLength(IPv6 netmask) { int i = 0; while (netmask.GetBit(i) == true) { i++; } return(i); }
private static void TestCompare() { IPv6 a = new IPv6(0x11111111, 0x22222222, 0x33333333, 0x44444444); IPv6 b = new IPv6(0x11111111, 0x22222222, 0x33333333, 0x44444443); if ((a == a) == false) { throw new TestFailedException("a == a"); } if ((a != b) == false) { throw new TestFailedException("a == b"); } if ((a > b) == false) { throw new TestFailedException("a > b"); } if ((a >= b) == false) { throw new TestFailedException("a >= b"); } if ((a >= a) == false) { throw new TestFailedException("a >= a"); } if ((a > a) == true) { throw new TestFailedException("a > a"); } if ((a < b) == true) { throw new TestFailedException("a < b"); } if ((a <= b) == true) { throw new TestFailedException("a <= b"); } if ((a <= a) != true) { throw new TestFailedException("a <= a"); } if ((a < a) == true) { throw new TestFailedException("a < a"); } }
/// <summary> /// Converts an IP address string into an IPv6 instance. /// </summary> /// <exception cref="ArgumentNullException">Thrown if /// <c>ipString</c> is null.</exception> /// <returns> <c>true</c> on success, <c>false</c> on failure.</returns> public static bool Parse(string ipString, out IPv6 address) { try { address = Parse(ipString); } catch (FormatException) { address = IPv6.Zero; return(false); } return(true); }
private static void TestMath() { IPv6 a = IPv6.Zero; if (--a != IPv6.AllOnes) { throw new TestFailedException("--"); } if (++a != IPv6.Zero) { throw new TestFailedException("++"); } }
public int CompareTo(object other) { if (other == null) { return(1); } if (other is IPv6) { IPv6 value = (IPv6)other; if (this < value) { return(-1); } if (this > value) { return(+1); } return(0); } throw new ArgumentException("Arg_MustBeIPv6"); }
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) ); } } }
/// <summary> /// /// Parse an IPv6 network string representation into an IPv6Network. /// /// <para> The representation should either be <ipAddress/> or /// <ipAddress/>/<masklength/>. /// </para> /// /// <para> Example forms of IPv6 Networks are: 10.0.2.0/24, /// 10.0.0.1. /// </para> /// /// </summary> /// /// <exception cref="ArgumentNullException"></exception> /// /// <exception cref="FormatException"> /// Thrown when IP address component of format is invalid or too many /// slashes appear in string argument, or netmask length is not a valid /// integer. /// </exception> /// /// <exception cref="ArgumentException"> /// Thrown when specified mask length is greater than /// <c>IPv6.BitCount</c> or less than zero. /// </exception> /// /// <exception cref="OverflowException"> /// Netmask length overflows valid values for integers /// </exception> /// public static IPv6Network Parse(string ipNetwork) { if (ipNetwork == null) { throw new ArgumentNullException("ipNetwork"); } string [] pieces = ipNetwork.Split('/'); if (pieces.Length > 2) { throw new FormatException("slash overload"); } int maskLength = IPv6.BitCount; if (pieces.Length == 2) { maskLength = Int32.Parse(pieces[1]); } return(new IPv6Network(IPv6.Parse(pieces[0]), maskLength)); }
private static void TestBasics() { IPv6 address = IPv6.Parse("ab1e:d00d::1057:d05"); IPv6 mask = IPv6.Parse("ffff:fffe::"); int masklen = 31; if (new IPv6Network(address, masklen) != new IPv6Network(address, mask)) { throw new TestFailedException("Constructors"); } if (IPv6Network.Parse("10::/24") != new IPv6Network(new IPv6(0x00100000, 0x0, 0x0, 0x0), 24)) { Console.WriteLine(IPv6Network.Parse("10::/24")); Console.WriteLine(new IPv6Network(new IPv6(0x00100000, 0x0, 0x0, 0x0), 24)); throw new TestFailedException("10::/24"); } if (IPv6Network.Parse("10::/32") != new IPv6Network(new IPv6(0x00100000, 0x0, 0x0, 0x0), 32)) { throw new TestFailedException("10::/32"); } if (IPv6Network.Parse("10::/1") != new IPv6Network(new IPv6(0x00100000, 0x0, 0x0, 0x0), 1)) { throw new TestFailedException("10::/1"); } try { IPv6Network.Parse("10:://12"); throw new TestFailedException("double slash"); } catch (FormatException) { } try { IPv6Network.Parse("10::/129"); throw new TestFailedException("netmask length"); } catch (ArgumentException) { } try { IPv6Network.Parse("10::/xx"); throw new TestFailedException("netmask content"); } catch (FormatException) { } try { IPv6Network.Parse("10::x/10"); throw new TestFailedException("network content"); } catch (FormatException) { } try { IPv6Network.Parse("10::/3333333333333333333333333333"); throw new TestFailedException("netmask overflow"); } catch (OverflowException) { } }
public SrepV6Pair(string s, IPv6 a) { srep = s; v6rep = a; }
private static void TestParse() { // // grumble, grumble, no clean struct array initializer syntax !?! // // Okay, these are pairs of Valid IPv6 string representations // and their manually parsed IPv6 representations. // SrepV6Pair [] pairs = new SrepV6Pair[] { new SrepV6Pair( "FfEe:DdCc:BbAa:9876:5432:1001:2345:6789", new IPv6(0xffeeddcc, 0xbbaa9876, 0x54321001, 0x23456789) ), new SrepV6Pair( "ffe8:c181::192:101", new IPv6(0xffe8c181U, 0x0U, 0x0U, 0x01920101U) ), new SrepV6Pair( "::", IPv6.Zero ), new SrepV6Pair( "0:0:0:0:0:0:0:0", IPv6.Zero ), new SrepV6Pair( "1::", new IPv6(0x00010000U, 0x0U, 0x0U, 0x0U) ), new SrepV6Pair( "12::", new IPv6(0x00120000U, 0x0U, 0x0U, 0x0U) ), new SrepV6Pair( "123::", new IPv6(0x01230000U, 0x0U, 0x0U, 0x0U) ), new SrepV6Pair( "1234::", new IPv6(0x12340000U, 0x0U, 0x0U, 0x0U) ), new SrepV6Pair( "1234:5678:9abc:def0::", new IPv6(0x12345678U, 0x9abcdef0U, 0x0U, 0x0U) ), new SrepV6Pair( "::1", new IPv6(0x0U, 0x0U, 0x0U, 0x1U) ), new SrepV6Pair( "::12", new IPv6(0x0U, 0x0U, 0x0U, 0x12U) ), new SrepV6Pair( "::123", new IPv6(0x0U, 0x0U, 0x0U, 0x123U) ), new SrepV6Pair( "::1234", new IPv6(0x0U, 0x0U, 0x0U, 0x1234U) ), new SrepV6Pair( "::1234:5678:9abc:def0", new IPv6(0x0U, 0x0U, 0x12345678U, 0x9abcdef0U) ), }; for (int i = 0; i < pairs.Length; i++) { IPv6 a = IPv6.Parse(pairs[i].srep); if (a != pairs[i].v6rep) { Console.WriteLine("{0} {1}", a, pairs[i].v6rep); throw new TestFailedException( String.Format("Parse {0}", i) ); } IPv6 dup = IPv6.Parse(a.ToString()); if (dup != a) { throw new TestFailedException( String.Format("ToString {0}", i) ); } } // // Test bogus strings // string [] badSreps = new string [] { "Frank", "00000::", "0:1:2:3:4:5:6:7:8", "0.1.2.3.4::0.1.2.3.4.", "0:1:2:3:4:5:6:7:trailing junk", }; for (int i = 0; i < badSreps.Length; i++) { try { IPv6 bad = IPv6.Parse(badSreps[i]); throw new TestFailedException(badSreps[i]); } catch (FormatException) { } } }
private static void TestBasics() { // Create an IPv6 address from a set of uints IPv6 a = new IPv6(0xf0e1d2c3, 0xb4a59687, 0x78695a4b, 0x3c2d1e0f); // Then check it against equivalent array of Bytes byte[] xBytes = { 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87, 0x78, 0x69, 0x5a, 0x4b, 0x3c, 0x2d, 0x1e, 0x0f }; byte[] aBytes = a.GetAddressBytes(); if (aBytes.Length != 16) { throw new TestFailedException("GetAddressBytes Length"); } for (int i = 0; i < aBytes.Length; i++) { if (aBytes[i] != xBytes[i]) { throw new TestFailedException("GetAddressBytes Values"); } } // Instantiate from array of bytes and then check a = IPv6.ParseBytes(xBytes); aBytes = a.GetAddressBytes(); if (aBytes.Length != 16) { throw new TestFailedException("GetAddressBytes Length"); } for (int i = 0; i < aBytes.Length; i++) { if (aBytes[i] != xBytes[i]) { throw new TestFailedException("GetAddressBytes Values"); } } // Test System.Net.IPAddress Constructor IPAddress ipa = new IPAddress(aBytes); if (new IPv6(ipa) != a) { throw new TestFailedException("IPv6(IPAddress) Constructor"); } // Test basic string parsing a = IPv6.Parse("f0e1:d2c3:b4a5:9687:7869:5a4b:3c2d:1e0f"); aBytes = a.GetAddressBytes(); if (aBytes.Length != 16) { throw new TestFailedException("GetAddressBytes Length"); } for (int i = 0; i < aBytes.Length; i++) { if (aBytes[i] != xBytes[i]) { throw new TestFailedException("GetAddressBytes Values"); } } }
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); }
public static void TestCompare() { IPv6 address = IPv6.Parse("dead:beef:cafe:babe:5ca1:ab1e:b01d:face"); IPv6Network outer = new IPv6Network(address, 124); IPv6Network inner = new IPv6Network(address, 126); if (outer.Contains(outer) == false) { throw new TestFailedException("outer.Contains(outer)"); } if (outer.Contains(inner) == false) { throw new TestFailedException("outer.Contains(inner)"); } if (inner.Contains(outer) == true) { throw new TestFailedException("inner.Contains(outer)"); } if (outer.Contains(address) == false) { throw new TestFailedException("outer.Contains(address)"); } if (inner.Contains(address) == false) { throw new TestFailedException("inner.Contains(address)"); } if (outer.IsMoreSpecificThan(inner) == true) { throw new TestFailedException("outer.IsMoreSpecificThan(inner)"); } if (outer.IsLessSpecificThan(inner) == false) { throw new TestFailedException("outer.IsLessSpecificThan(inner)"); } if ((outer == outer) == false) { throw new TestFailedException("operator=="); } if ((outer != inner) == false) { throw new TestFailedException("operator!="); } if (outer.Contains(outer.UpperBound) == false) { throw new TestFailedException("outer.Contains(outer.UpperBound)"); } if (outer.Contains(outer.LowerBound) == false) { throw new TestFailedException("outer.Contains(outer.LowerBound)"); } }
public bool Contains(IPv6 ipAddress) { return((ipAddress & this.NetMask) == this.ipNetwork); }