/// <summary> /// Overrides System.Object.ToString to return /// this object rendered in a quad-dotted notation /// </summary> public override string ToString() { if (m_Family == AddressFamily.InterNetwork) { return(ToString(m_Address)); } else { ushort[] numbers = m_Numbers.Clone() as ushort[]; for (int i = 0; i < numbers.Length; i++) { numbers[i] = (ushort)NetworkToHostOrder((short)numbers[i]); } IPv6Address v6 = new IPv6Address(numbers); v6.ScopeId = ScopeId; return(v6.ToString()); } }
public static bool IsLoopback(IPv6Address addr) { if (addr.address[7] != 1) { return(false); } int num = addr.address[6] >> 8; if (num != 127 && num != 0) { return(false); } for (int i = 0; i < 4; i++) { if (addr.address[i] != 0) { return(false); } } return(addr.address[5] == 0 || addr.address[5] == ushort.MaxValue); }
public override bool Equals(object other) { IPv6Address pv6Address = other as IPv6Address; if (pv6Address != null) { for (int i = 0; i < 8; i++) { if (address[i] != pv6Address.address[i]) { return(false); } } return(true); } IPAddress iPAddress = other as IPAddress; if (iPAddress != null) { for (int j = 0; j < 5; j++) { if (address[j] != 0) { return(false); } } if (address[5] != 0 && address[5] != ushort.MaxValue) { return(false); } long internalIPv4Address = iPAddress.InternalIPv4Address; if (address[6] != (ushort)(((int)(internalIPv4Address & 0xFF) << 8) + (int)((internalIPv4Address >> 8) & 0xFF)) || address[7] != (ushort)(((int)((internalIPv4Address >> 16) & 0xFF) << 8) + (int)((internalIPv4Address >> 24) & 0xFF))) { return(false); } return(true); } return(false); }
public override bool Equals(object other) { IPv6Address pv6Address = other as IPv6Address; if (pv6Address != null) { for (int i = 0; i < 8; i++) { if (this.address[i] != pv6Address.address[i]) { return(false); } } return(true); } IPAddress ipaddress = other as IPAddress; if (ipaddress == null) { return(false); } for (int j = 0; j < 5; j++) { if (this.address[j] != 0) { return(false); } } if (this.address[5] != 0 && this.address[5] != 65535) { return(false); } long internalIPv4Address = ipaddress.InternalIPv4Address; return(this.address[6] == (ushort)(((int)(internalIPv4Address & 255L) << 8) + (int)(internalIPv4Address >> 8 & 255L)) && this.address[7] == (ushort)(((int)(internalIPv4Address >> 16 & 255L) << 8) + (int)(internalIPv4Address >> 24 & 255L))); }
public static bool TryParse(string ipString, out IPv6Address result) { result = null; if (ipString == null) { return(false); } if (ipString.Length > 2 && ipString [0] == '[' && ipString [ipString.Length - 1] == ']') { ipString = ipString.Substring(1, ipString.Length - 2); } if (ipString.Length < 2) { return(false); } int prefixLen = 0; int scopeId = 0; int pos = ipString.LastIndexOf('/'); if (pos != -1) { string prefix = ipString.Substring(pos + 1); if (!TryParse(prefix, out prefixLen)) { prefixLen = -1; } if (prefixLen < 0 || prefixLen > 128) { return(false); } ipString = ipString.Substring(0, pos); } else { pos = ipString.LastIndexOf('%'); if (pos != -1) { string prefix = ipString.Substring(pos + 1); if (!TryParse(prefix, out scopeId)) { scopeId = 0; } ipString = ipString.Substring(0, pos); } } // // At this point the prefix/suffixes have been removed // and we only have to deal with the ipv4 or ipv6 addressed // ushort [] addr = new ushort [8]; // // Is there an ipv4 address at the end? // int pos2 = ipString.LastIndexOf(':'); if (pos2 == -1) { return(false); } int slots = 0; if (pos2 < (ipString.Length - 1)) { string ipv4Str = ipString.Substring(pos2 + 1); if (ipv4Str.IndexOf('.') != -1) { IPAddress ip; if (!IPAddress.TryParse(ipv4Str, out ip)) { return(false); } long a = ip.InternalIPv4Address; addr [6] = (ushort)(((int)(a & 0xff) << 8) + ((int)((a >> 8) & 0xff))); addr [7] = (ushort)(((int)((a >> 16) & 0xff) << 8) + ((int)((a >> 24) & 0xff))); if (pos2 > 0 && ipString [pos2 - 1] == ':') { ipString = ipString.Substring(0, pos2 + 1); } else { ipString = ipString.Substring(0, pos2); } slots = 2; } } // // Only an ipv6 block remains, either: // "hexnumbers::hexnumbers", "hexnumbers::" or "hexnumbers" // int c = ipString.IndexOf("::", StringComparison.Ordinal); if (c != -1) { int right_slots = Fill(addr, ipString.Substring(c + 2)); if (right_slots == -1) { return(false); } if (right_slots + slots > 8) { return(false); } int d = 8 - slots - right_slots; for (int i = right_slots; i > 0; i--) { addr [i + d - 1] = addr [i - 1]; addr [i - 1] = 0; } int left_slots = Fill(addr, ipString.Substring(0, c)); if (left_slots == -1) { return(false); } if (left_slots + right_slots + slots > 7) { return(false); } } else { if (Fill(addr, ipString) != 8 - slots) { return(false); } } result = new IPv6Address(addr, prefixLen, scopeId); return(true); }
public override int GetHashCode() { return(IPv6Address.Hash(((int)this.address[0] << 16) + (int)this.address[1], ((int)this.address[2] << 16) + (int)this.address[3], ((int)this.address[4] << 16) + (int)this.address[5], ((int)this.address[6] << 16) + (int)this.address[7])); }
private int AsIPv4Int() { return(((int)IPv6Address.SwapUShort(this.address[7]) << 16) + (int)IPv6Address.SwapUShort(this.address[6])); }
public static bool TryParse(string ipString, out IPv6Address result) { result = null; if (ipString == null) { return(false); } if (ipString.Length > 2 && ipString[0] == '[' && ipString[ipString.Length - 1] == ']') { ipString = ipString.Substring(1, ipString.Length - 2); } if (ipString.Length < 2) { return(false); } int num = 0; int num2 = 0; int num3 = ipString.LastIndexOf('/'); if (num3 != -1) { string prefix = ipString.Substring(num3 + 1); if (!IPv6Address.TryParse(prefix, out num)) { num = -1; } if (num < 0 || num > 128) { return(false); } ipString = ipString.Substring(0, num3); } else { num3 = ipString.LastIndexOf('%'); if (num3 != -1) { string prefix2 = ipString.Substring(num3 + 1); if (!IPv6Address.TryParse(prefix2, out num2)) { num2 = 0; } ipString = ipString.Substring(0, num3); } } ushort[] array = new ushort[8]; bool flag = false; int num4 = ipString.LastIndexOf(':'); if (num4 == -1) { return(false); } int num5 = 0; if (num4 < ipString.Length - 1) { string text = ipString.Substring(num4 + 1); if (text.IndexOf('.') != -1) { IPAddress ipaddress; if (!IPAddress.TryParse(text, out ipaddress)) { return(false); } long internalIPv4Address = ipaddress.InternalIPv4Address; array[6] = (ushort)(((int)(internalIPv4Address & 255L) << 8) + (int)(internalIPv4Address >> 8 & 255L)); array[7] = (ushort)(((int)(internalIPv4Address >> 16 & 255L) << 8) + (int)(internalIPv4Address >> 24 & 255L)); if (num4 > 0 && ipString[num4 - 1] == ':') { ipString = ipString.Substring(0, num4 + 1); } else { ipString = ipString.Substring(0, num4); } flag = true; num5 = 2; } } int num6 = ipString.IndexOf("::"); if (num6 != -1) { int num7 = IPv6Address.Fill(array, ipString.Substring(num6 + 2)); if (num7 == -1) { return(false); } if (num7 + num5 > 8) { return(false); } int num8 = 8 - num5 - num7; for (int i = num7; i > 0; i--) { array[i + num8 - 1] = array[i - 1]; array[i - 1] = 0; } int num9 = IPv6Address.Fill(array, ipString.Substring(0, num6)); if (num9 == -1) { return(false); } if (num9 + num7 + num5 > 7) { return(false); } } else if (IPv6Address.Fill(array, ipString) != 8 - num5) { return(false); } bool flag2 = false; for (int j = 0; j < num5; j++) { if (array[j] != 0 || (j == 5 && array[j] != 65535)) { flag2 = true; } } if (flag && !flag2) { for (int k = 0; k < 5; k++) { if (array[k] != 0) { return(false); } } if (array[5] != 0 && array[5] != 65535) { return(false); } } result = new IPv6Address(array, num, num2); return(true); }
public static bool TryParse(string ipString, out IPv6Address result) { result = null; if (ipString == null) { return(false); } if (ipString.Length > 2 && ipString[0] == '[' && ipString[ipString.Length - 1] == ']') { ipString = ipString.Substring(1, ipString.Length - 2); } if (ipString.Length < 2) { return(false); } int res = 0; int res2 = 0; int num = ipString.LastIndexOf('/'); if (num != -1) { string prefix = ipString.Substring(num + 1); if (!TryParse(prefix, out res)) { res = -1; } if (res < 0 || res > 128) { return(false); } ipString = ipString.Substring(0, num); } else { num = ipString.LastIndexOf('%'); if (num != -1) { string prefix2 = ipString.Substring(num + 1); if (!TryParse(prefix2, out res2)) { res2 = 0; } ipString = ipString.Substring(0, num); } } ushort[] array = new ushort[8]; bool flag = false; int num2 = ipString.LastIndexOf(':'); if (num2 == -1) { return(false); } int num3 = 0; if (num2 < ipString.Length - 1) { string text = ipString.Substring(num2 + 1); if (text.IndexOf('.') != -1) { if (!IPAddress.TryParse(text, out IPAddress iPAddress)) { return(false); } long internalIPv4Address = iPAddress.InternalIPv4Address; array[6] = (ushort)(((int)(internalIPv4Address & 0xFF) << 8) + (int)((internalIPv4Address >> 8) & 0xFF)); array[7] = (ushort)(((int)((internalIPv4Address >> 16) & 0xFF) << 8) + (int)((internalIPv4Address >> 24) & 0xFF)); ipString = ((num2 <= 0 || ipString[num2 - 1] != ':') ? ipString.Substring(0, num2) : ipString.Substring(0, num2 + 1)); flag = true; num3 = 2; } } int num4 = ipString.IndexOf("::"); if (num4 != -1) { int num5 = Fill(array, ipString.Substring(num4 + 2)); if (num5 == -1) { return(false); } if (num5 + num3 > 8) { return(false); } int num6 = 8 - num3 - num5; for (int num7 = num5; num7 > 0; num7--) { array[num7 + num6 - 1] = array[num7 - 1]; array[num7 - 1] = 0; } int num8 = Fill(array, ipString.Substring(0, num4)); if (num8 == -1) { return(false); } if (num8 + num5 + num3 > 7) { return(false); } } else if (Fill(array, ipString) != 8 - num3) { return(false); } bool flag2 = false; for (int i = 0; i < num3; i++) { if (array[i] != 0 || (i == 5 && array[i] != ushort.MaxValue)) { flag2 = true; } } if (flag && !flag2) { for (int j = 0; j < 5; j++) { if (array[j] != 0) { return(false); } } if (array[5] != 0 && array[5] != ushort.MaxValue) { return(false); } } result = new IPv6Address(array, res, res2); return(true); }