|
| 1 | +// |
| 2 | +// Licensed to Roland Pihlakas under one or more agreements. |
| 3 | +// Roland Pihlakas licenses this file to you under the GNU Lesser General Public License, ver 2.1. |
| 4 | +// See the LICENSE and copyrights.txt files for more information. |
| 5 | +// |
| 6 | + |
| 7 | + |
| 8 | +//------------------------------------------------------------------------------ |
| 9 | +// <copyright file="IPAddress.cs" company="Microsoft"> |
| 10 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 11 | +// </copyright> |
| 12 | +//------------------------------------------------------------------------------ |
| 13 | + |
| 14 | +using System.Net.Sockets; |
| 15 | +using System.Globalization; |
| 16 | +using System.Text; |
| 17 | + |
| 18 | +namespace System.Net |
| 19 | +{ |
| 20 | + /// <devdoc> |
| 21 | + /// <para>Provides an internet protocol (IP) address.</para> |
| 22 | + /// </devdoc> |
| 23 | + [Serializable] |
| 24 | +#pragma warning disable CS0659 //warning CS0659: 'IPAddressEx' overrides Object.Equals(object o) but does not override Object.GetHashCode() |
| 25 | + public class IPAddressEx |
| 26 | + { |
| 27 | + public static readonly IPAddressEx Any = new IPAddressEx(0x0000000000000000); |
| 28 | + |
| 29 | + // |
| 30 | + // IPv6 Changes: make this internal so other NCL classes that understand about |
| 31 | + // IPv4 and IPv4 can still access it rather than the obsolete property. |
| 32 | + // |
| 33 | + internal long m_Address; |
| 34 | + |
| 35 | + public static readonly IPAddressEx IPv6Any = new IPAddressEx(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0); |
| 36 | + |
| 37 | + /// <devdoc> |
| 38 | + /// <para> |
| 39 | + /// Default to IPv4 address |
| 40 | + /// </para> |
| 41 | + /// </devdoc> |
| 42 | + private AddressFamily m_Family = AddressFamily.InterNetwork; |
| 43 | + private ushort[] m_Numbers = new ushort[NumberOfLabels]; |
| 44 | + private long m_ScopeId = 0; // really uint ! |
| 45 | + |
| 46 | + internal const int IPv4AddressBytes = 4; |
| 47 | + internal const int IPv6AddressBytes = 16; |
| 48 | + |
| 49 | + internal const int NumberOfLabels = IPv6AddressBytes / 2; |
| 50 | + |
| 51 | + |
| 52 | + /// <devdoc> |
| 53 | + /// <para> |
| 54 | + /// Initializes a new instance of the <see cref='System.Net.IPAddressEx'/> |
| 55 | + /// class with the specified |
| 56 | + /// address. |
| 57 | + /// </para> |
| 58 | + /// </devdoc> |
| 59 | + public IPAddressEx(long newAddress) |
| 60 | + { |
| 61 | + if (newAddress < 0 || newAddress > 0x00000000FFFFFFFF) |
| 62 | + { |
| 63 | + throw new ArgumentOutOfRangeException("newAddress"); |
| 64 | + } |
| 65 | + m_Address = newAddress; |
| 66 | + } |
| 67 | + |
| 68 | + /// <devdoc> |
| 69 | + /// <para> |
| 70 | + /// Constructor for an IPv6 Address with a specified Scope. |
| 71 | + /// </para> |
| 72 | + /// </devdoc> |
| 73 | + public IPAddressEx(byte[] address, long scopeid) |
| 74 | + { |
| 75 | + |
| 76 | + if (address == null) |
| 77 | + { |
| 78 | + throw new ArgumentNullException("address"); |
| 79 | + } |
| 80 | + |
| 81 | + if (address.Length != IPv6AddressBytes) |
| 82 | + { |
| 83 | + throw new ArgumentException("Bad IP address", "address"); |
| 84 | + } |
| 85 | + |
| 86 | + m_Family = AddressFamily.InterNetworkV6; |
| 87 | + |
| 88 | + for (int i = 0; i < NumberOfLabels; i++) |
| 89 | + { |
| 90 | + m_Numbers[i] = (ushort)(address[i * 2] * 256 + address[i * 2 + 1]); |
| 91 | + } |
| 92 | + |
| 93 | + // |
| 94 | + // Consider: Since scope is only valid for link-local and site-local |
| 95 | + // addresses we could implement some more robust checking here |
| 96 | + // |
| 97 | + if (scopeid < 0 || scopeid > 0x00000000FFFFFFFF) |
| 98 | + { |
| 99 | + throw new ArgumentOutOfRangeException("scopeid"); |
| 100 | + } |
| 101 | + |
| 102 | + m_ScopeId = scopeid; |
| 103 | + } |
| 104 | + |
| 105 | + /// <devdoc> |
| 106 | + /// <para> |
| 107 | + /// Constructor for IPv4 and IPv6 Address. |
| 108 | + /// </para> |
| 109 | + /// </devdoc> |
| 110 | + public IPAddressEx(byte[] address) |
| 111 | + { |
| 112 | + if (address == null) |
| 113 | + { |
| 114 | + throw new ArgumentNullException("address"); |
| 115 | + } |
| 116 | + if (address.Length != IPv4AddressBytes && address.Length != IPv6AddressBytes) |
| 117 | + { |
| 118 | + throw new ArgumentException("Bad IP address", "address"); |
| 119 | + } |
| 120 | + |
| 121 | + if (address.Length == IPv4AddressBytes) |
| 122 | + { |
| 123 | + m_Family = AddressFamily.InterNetwork; |
| 124 | + m_Address = ((address[3] << 24 | address[2] << 16 | address[1] << 8 | address[0]) & 0x0FFFFFFFF); |
| 125 | + } |
| 126 | + else |
| 127 | + { |
| 128 | + m_Family = AddressFamily.InterNetworkV6; |
| 129 | + |
| 130 | + for (int i = 0; i < NumberOfLabels; i++) |
| 131 | + { |
| 132 | + m_Numbers[i] = (ushort)(address[i * 2] * 256 + address[i * 2 + 1]); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + // |
| 138 | + // we need this internally since we need to interface with winsock |
| 139 | + // and winsock only understands Int32 |
| 140 | + // |
| 141 | + internal IPAddressEx(int newAddress) |
| 142 | + { |
| 143 | + m_Address = (long)newAddress & 0x00000000FFFFFFFF; |
| 144 | + } |
| 145 | + |
| 146 | + /// <devdoc> |
| 147 | + /// <para>Converts an IP address string to an <see cref='System.Net.IPAddress'/> |
| 148 | + /// instance.</para> |
| 149 | + /// </devdoc> |
| 150 | + public static bool TryParse(string ipString, out IPAddressEx addressEx) |
| 151 | + { |
| 152 | + IPAddress address = null; |
| 153 | + IPAddress.TryParse(ipString, out address); //roland |
| 154 | + |
| 155 | + if (address != null) |
| 156 | + addressEx = new IPAddressEx(address.GetAddressBytes()); //roland |
| 157 | + else |
| 158 | + addressEx = null; |
| 159 | + |
| 160 | + //address = InternalParse(ipString, true); |
| 161 | + return (address != null); |
| 162 | + } |
| 163 | + |
| 164 | + public static IPAddressEx Parse(string ipString) |
| 165 | + { |
| 166 | + return new IPAddressEx(IPAddress.Parse(ipString).GetAddressBytes()); //roland |
| 167 | + |
| 168 | + //return InternalParse(ipString, false); |
| 169 | + } |
| 170 | + |
| 171 | + /// <devdoc> |
| 172 | + /// <para> |
| 173 | + /// Provides a copy of the IPAddressEx internals as an array of bytes. |
| 174 | + /// </para> |
| 175 | + /// </devdoc> |
| 176 | + public byte[] GetAddressBytes() |
| 177 | + { |
| 178 | + byte[] bytes; |
| 179 | + if (m_Family == AddressFamily.InterNetworkV6) |
| 180 | + { |
| 181 | + bytes = new byte[NumberOfLabels * 2]; |
| 182 | + |
| 183 | + int j = 0; |
| 184 | + for (int i = 0; i < NumberOfLabels; i++) |
| 185 | + { |
| 186 | + bytes[j++] = (byte)((this.m_Numbers[i] >> 8) & 0xFF); |
| 187 | + bytes[j++] = (byte)((this.m_Numbers[i]) & 0xFF); |
| 188 | + } |
| 189 | + } |
| 190 | + else |
| 191 | + { |
| 192 | + bytes = new byte[IPv4AddressBytes]; |
| 193 | + bytes[0] = (byte)(m_Address); |
| 194 | + bytes[1] = (byte)(m_Address >> 8); |
| 195 | + bytes[2] = (byte)(m_Address >> 16); |
| 196 | + bytes[3] = (byte)(m_Address >> 24); |
| 197 | + } |
| 198 | + return bytes; |
| 199 | + } |
| 200 | + |
| 201 | + public AddressFamily AddressFamily |
| 202 | + { |
| 203 | + get |
| 204 | + { |
| 205 | + return m_Family; |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + /// <devdoc> |
| 210 | + /// <para> |
| 211 | + /// IPv6 Scope identifier. This is really a uint32, but that isn't CLS compliant |
| 212 | + /// </para> |
| 213 | + /// </devdoc> |
| 214 | + public long ScopeId |
| 215 | + { |
| 216 | + get |
| 217 | + { |
| 218 | + // |
| 219 | + // Not valid for IPv4 addresses |
| 220 | + // |
| 221 | + if (m_Family == AddressFamily.InterNetwork) |
| 222 | + { |
| 223 | + throw new SocketException((int)SocketError.OperationNotSupported); |
| 224 | + } |
| 225 | + |
| 226 | + return m_ScopeId; |
| 227 | + } |
| 228 | + set |
| 229 | + { |
| 230 | + // |
| 231 | + // Not valid for IPv4 addresses |
| 232 | + // |
| 233 | + if (m_Family == AddressFamily.InterNetwork) |
| 234 | + { |
| 235 | + throw new SocketException((int)SocketError.OperationNotSupported); |
| 236 | + } |
| 237 | + |
| 238 | + // |
| 239 | + // Consider: Since scope is only valid for link-local and site-local |
| 240 | + // addresses we could implement some more robust checking here |
| 241 | + // |
| 242 | + if (value < 0 || value > 0x00000000FFFFFFFF) |
| 243 | + { |
| 244 | + throw new ArgumentOutOfRangeException("value"); |
| 245 | + } |
| 246 | + if (m_ScopeId != value) |
| 247 | + { |
| 248 | + m_Address = value; |
| 249 | + m_ScopeId = value; |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + /// <devdoc> |
| 255 | + /// <para> |
| 256 | + /// Converts the Internet address to either standard dotted quad format |
| 257 | + /// or standard IPv6 representation. |
| 258 | + /// </para> |
| 259 | + /// </devdoc> |
| 260 | + public override string ToString() |
| 261 | + { |
| 262 | + return new IPAddress(GetAddressBytes()).ToString(); |
| 263 | + } |
| 264 | + |
| 265 | + internal bool Equals(object comparandObj, bool compareScopeId) |
| 266 | + { |
| 267 | + IPAddressEx comparand = comparandObj as IPAddressEx; |
| 268 | + |
| 269 | + if (comparand == null) |
| 270 | + { |
| 271 | + return false; |
| 272 | + } |
| 273 | + // |
| 274 | + // Compare families before address representations |
| 275 | + // |
| 276 | + if (m_Family != comparand.m_Family) |
| 277 | + { |
| 278 | + return false; |
| 279 | + } |
| 280 | + if (m_Family == AddressFamily.InterNetworkV6) |
| 281 | + { |
| 282 | + // |
| 283 | + // For IPv6 addresses, we must compare the full 128bit |
| 284 | + // representation. |
| 285 | + // |
| 286 | + for (int i = 0; i < NumberOfLabels; i++) |
| 287 | + { |
| 288 | + if (comparand.m_Numbers[i] != this.m_Numbers[i]) |
| 289 | + return false; |
| 290 | + } |
| 291 | + // |
| 292 | + // In addition, the scope id's must match as well |
| 293 | + // |
| 294 | + if (comparand.m_ScopeId == this.m_ScopeId) |
| 295 | + return true; |
| 296 | + else |
| 297 | + return (compareScopeId ? false : true); |
| 298 | + } |
| 299 | + else |
| 300 | + { |
| 301 | + // |
| 302 | + // For IPv4 addresses, compare the integer representation. |
| 303 | + // |
| 304 | + return comparand.m_Address == this.m_Address; |
| 305 | + } |
| 306 | + } |
| 307 | + |
| 308 | + /// <devdoc> |
| 309 | + /// <para> |
| 310 | + /// Compares two IP addresses. |
| 311 | + /// </para> |
| 312 | + /// </devdoc> |
| 313 | + public override bool Equals(object comparand) |
| 314 | + { |
| 315 | + return Equals(comparand, true); |
| 316 | + } |
| 317 | + } // class IPAddressEx |
| 318 | +#pragma warning restore CS0659 |
| 319 | +} // namespace System.Net |
0 commit comments