Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
IPAddress.cs
Go to the documentation of this file.
6
7namespace System.Net;
8
9public class IPAddress
10{
11 private sealed class ReadOnlyIPAddress : IPAddress
12 {
14 : base(newAddress)
15 {
16 }
17 }
18
19 public static readonly IPAddress Any = new ReadOnlyIPAddress(new byte[4] { 0, 0, 0, 0 });
20
21 public static readonly IPAddress Loopback = new ReadOnlyIPAddress(new byte[4] { 127, 0, 0, 1 });
22
23 public static readonly IPAddress Broadcast = new ReadOnlyIPAddress(new byte[4] { 255, 255, 255, 255 });
24
25 public static readonly IPAddress None = Broadcast;
26
27 public static readonly IPAddress IPv6Any = new IPAddress((ReadOnlySpan<byte>)new byte[16]
28 {
29 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
30 0, 0, 0, 0, 0, 0
31 }, 0L);
32
33 public static readonly IPAddress IPv6Loopback = new IPAddress((ReadOnlySpan<byte>)new byte[16]
34 {
35 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
36 0, 0, 0, 0, 0, 1
37 }, 0L);
38
39 public static readonly IPAddress IPv6None = IPv6Any;
40
41 private static readonly IPAddress s_loopbackMappedToIPv6 = new IPAddress((ReadOnlySpan<byte>)new byte[16]
42 {
43 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44 255, 255, 127, 0, 0, 1
45 }, 0L);
46
47 private uint _addressOrScopeId;
48
49 private readonly ushort[] _numbers;
50
51 private string _toString;
52
53 private int _hashCode;
54
55 private bool IsIPv4 => _numbers == null;
56
57 private bool IsIPv6 => _numbers != null;
58
59 private uint PrivateAddress
60 {
61 get
62 {
63 return _addressOrScopeId;
64 }
65 set
66 {
67 _toString = null;
68 _hashCode = 0;
70 }
71 }
72
73 private uint PrivateScopeId
74 {
75 get
76 {
77 return _addressOrScopeId;
78 }
79 set
80 {
81 _toString = null;
82 _hashCode = 0;
84 }
85 }
86
88 {
89 get
90 {
91 if (!IsIPv4)
92 {
93 return AddressFamily.InterNetworkV6;
94 }
95 return AddressFamily.InterNetwork;
96 }
97 }
98
99 public long ScopeId
100 {
101 get
102 {
103 if (IsIPv4)
104 {
105 throw new SocketException(SocketError.OperationNotSupported);
106 }
107 return PrivateScopeId;
108 }
109 set
110 {
111 if (IsIPv4)
112 {
113 throw new SocketException(SocketError.OperationNotSupported);
114 }
115 if (value < 0 || value > uint.MaxValue)
116 {
117 throw new ArgumentOutOfRangeException("value");
118 }
119 PrivateScopeId = (uint)value;
120 }
121 }
122
123 public bool IsIPv6Multicast
124 {
125 get
126 {
127 if (IsIPv6)
128 {
129 return (_numbers[0] & 0xFF00) == 65280;
130 }
131 return false;
132 }
133 }
134
135 public bool IsIPv6LinkLocal
136 {
137 get
138 {
139 if (IsIPv6)
140 {
141 return (_numbers[0] & 0xFFC0) == 65152;
142 }
143 return false;
144 }
145 }
146
147 public bool IsIPv6SiteLocal
148 {
149 get
150 {
151 if (IsIPv6)
152 {
153 return (_numbers[0] & 0xFFC0) == 65216;
154 }
155 return false;
156 }
157 }
158
159 public bool IsIPv6Teredo
160 {
161 get
162 {
163 if (IsIPv6 && _numbers[0] == 8193)
164 {
165 return _numbers[1] == 0;
166 }
167 return false;
168 }
169 }
170
172 {
173 get
174 {
175 if (IsIPv6)
176 {
177 return (_numbers[0] & 0xFE00) == 64512;
178 }
179 return false;
180 }
181 }
182
184 {
185 get
186 {
187 if (IsIPv4)
188 {
189 return false;
190 }
191 for (int i = 0; i < 5; i++)
192 {
193 if (_numbers[i] != 0)
194 {
195 return false;
196 }
197 }
198 return _numbers[5] == ushort.MaxValue;
199 }
200 }
201
202 [Obsolete("IPAddress.Address is address family dependent and has been deprecated. Use IPAddress.Equals to perform comparisons instead.")]
203 public long Address
204 {
205 get
206 {
207 if (AddressFamily == AddressFamily.InterNetworkV6)
208 {
209 throw new SocketException(SocketError.OperationNotSupported);
210 }
211 return PrivateAddress;
212 }
213 set
214 {
215 if (AddressFamily == AddressFamily.InterNetworkV6)
216 {
217 throw new SocketException(SocketError.OperationNotSupported);
218 }
219 if (PrivateAddress != value)
220 {
221 if (this is ReadOnlyIPAddress)
222 {
223 throw new SocketException(SocketError.OperationNotSupported);
224 }
225 PrivateAddress = (uint)value;
226 }
227 }
228 }
229
230 public IPAddress(long newAddress)
231 {
232 if (newAddress < 0 || newAddress > uint.MaxValue)
233 {
234 throw new ArgumentOutOfRangeException("newAddress");
235 }
236 PrivateAddress = (uint)newAddress;
237 }
238
239 public IPAddress(byte[] address, long scopeid)
240 : this(new ReadOnlySpan<byte>(address ?? ThrowAddressNullException()), scopeid)
241 {
242 }
243
244 public IPAddress(ReadOnlySpan<byte> address, long scopeid)
245 {
246 if (address.Length != 16)
247 {
248 throw new ArgumentException(System.SR.dns_bad_ip_address, "address");
249 }
250 if (scopeid < 0 || scopeid > uint.MaxValue)
251 {
252 throw new ArgumentOutOfRangeException("scopeid");
253 }
254 _numbers = new ushort[8];
255 for (int i = 0; i < 8; i++)
256 {
257 _numbers[i] = (ushort)(address[i * 2] * 256 + address[i * 2 + 1]);
258 }
259 PrivateScopeId = (uint)scopeid;
260 }
261
262 internal IPAddress(ReadOnlySpan<ushort> numbers, uint scopeid)
263 {
264 ushort[] array = new ushort[8];
265 for (int i = 0; i < array.Length; i++)
266 {
267 array[i] = numbers[i];
268 }
269 _numbers = array;
270 PrivateScopeId = scopeid;
271 }
272
273 private IPAddress(ushort[] numbers, uint scopeid)
274 {
275 _numbers = numbers;
276 PrivateScopeId = scopeid;
277 }
278
279 public IPAddress(byte[] address)
280 : this(new ReadOnlySpan<byte>(address ?? ThrowAddressNullException()))
281 {
282 }
283
285 {
286 if (address.Length == 4)
287 {
288 PrivateAddress = MemoryMarshal.Read<uint>(address);
289 return;
290 }
291 if (address.Length == 16)
292 {
293 _numbers = new ushort[8];
294 for (int i = 0; i < 8; i++)
295 {
296 _numbers[i] = (ushort)(address[i * 2] * 256 + address[i * 2 + 1]);
297 }
298 return;
299 }
300 throw new ArgumentException(System.SR.dns_bad_ip_address, "address");
301 }
302
303 public static bool TryParse([NotNullWhen(true)] string? ipString, [NotNullWhen(true)] out IPAddress? address)
304 {
305 if (ipString == null)
306 {
307 address = null;
308 return false;
309 }
310 address = IPAddressParser.Parse(ipString.AsSpan(), tryParse: true);
311 return address != null;
312 }
313
314 public static bool TryParse(ReadOnlySpan<char> ipSpan, [NotNullWhen(true)] out IPAddress? address)
315 {
316 address = IPAddressParser.Parse(ipSpan, tryParse: true);
317 return address != null;
318 }
319
320 public static IPAddress Parse(string ipString)
321 {
322 if (ipString == null)
323 {
324 throw new ArgumentNullException("ipString");
325 }
326 return IPAddressParser.Parse(ipString.AsSpan(), tryParse: false);
327 }
328
329 public static IPAddress Parse(ReadOnlySpan<char> ipSpan)
330 {
331 return IPAddressParser.Parse(ipSpan, tryParse: false);
332 }
333
334 public bool TryWriteBytes(Span<byte> destination, out int bytesWritten)
335 {
336 if (IsIPv6)
337 {
338 if (destination.Length < 16)
339 {
340 bytesWritten = 0;
341 return false;
342 }
344 bytesWritten = 16;
345 }
346 else
347 {
348 if (destination.Length < 4)
349 {
350 bytesWritten = 0;
351 return false;
352 }
354 bytesWritten = 4;
355 }
356 return true;
357 }
358
359 [MethodImpl(MethodImplOptions.AggressiveInlining)]
361 {
362 int num = 0;
363 for (int i = 0; i < 8; i++)
364 {
365 destination[num++] = (byte)((uint)(_numbers[i] >> 8) & 0xFFu);
366 destination[num++] = (byte)(_numbers[i] & 0xFFu);
367 }
368 }
369
370 [MethodImpl(MethodImplOptions.AggressiveInlining)]
372 {
373 uint value = PrivateAddress;
374 MemoryMarshal.Write(destination, ref value);
375 }
376
377 public byte[] GetAddressBytes()
378 {
379 if (IsIPv6)
380 {
381 byte[] array = new byte[16];
383 return array;
384 }
385 byte[] array2 = new byte[4];
386 WriteIPv4Bytes(array2);
387 return array2;
388 }
389
390 public override string ToString()
391 {
392 if (_toString == null)
393 {
395 }
396 return _toString;
397 }
398
399 public bool TryFormat(Span<char> destination, out int charsWritten)
400 {
401 if (!IsIPv4)
402 {
404 }
406 }
407
408 public static long HostToNetworkOrder(long host)
409 {
411 {
412 return host;
413 }
415 }
416
417 public static int HostToNetworkOrder(int host)
418 {
420 {
421 return host;
422 }
424 }
425
426 public static short HostToNetworkOrder(short host)
427 {
429 {
430 return host;
431 }
433 }
434
435 public static long NetworkToHostOrder(long network)
436 {
437 return HostToNetworkOrder(network);
438 }
439
440 public static int NetworkToHostOrder(int network)
441 {
442 return HostToNetworkOrder(network);
443 }
444
445 public static short NetworkToHostOrder(short network)
446 {
447 return HostToNetworkOrder(network);
448 }
449
450 public static bool IsLoopback(IPAddress address)
451 {
452 if (address == null)
453 {
455 }
456 if (address.IsIPv6)
457 {
458 if (!address.Equals(IPv6Loopback))
459 {
460 return address.Equals(s_loopbackMappedToIPv6);
461 }
462 return true;
463 }
464 long num = (uint)HostToNetworkOrder(-16777216);
465 return (address.PrivateAddress & num) == (Loopback.PrivateAddress & num);
466 }
467
468 public override bool Equals([NotNullWhen(true)] object? comparand)
469 {
470 if (comparand is IPAddress comparand2)
471 {
472 return Equals(comparand2);
473 }
474 return false;
475 }
476
477 internal bool Equals(IPAddress comparand)
478 {
479 if (AddressFamily != comparand.AddressFamily)
480 {
481 return false;
482 }
483 if (IsIPv6)
484 {
486 ReadOnlySpan<byte> source2 = MemoryMarshal.AsBytes<ushort>(comparand._numbers);
487 if (MemoryMarshal.Read<ulong>(source) == MemoryMarshal.Read<ulong>(source2) && MemoryMarshal.Read<ulong>(source.Slice(8)) == MemoryMarshal.Read<ulong>(source2.Slice(8)))
488 {
489 return PrivateScopeId == comparand.PrivateScopeId;
490 }
491 return false;
492 }
493 return comparand.PrivateAddress == PrivateAddress;
494 }
495
496 public override int GetHashCode()
497 {
498 if (_hashCode == 0)
499 {
500 if (IsIPv6)
501 {
503 _hashCode = HashCode.Combine(MemoryMarshal.Read<uint>(source), MemoryMarshal.Read<uint>(source.Slice(4)), MemoryMarshal.Read<uint>(source.Slice(8)), MemoryMarshal.Read<uint>(source.Slice(12)), _addressOrScopeId);
504 }
505 else
506 {
508 }
509 }
510 return _hashCode;
511 }
512
514 {
515 if (IsIPv6)
516 {
517 return this;
518 }
519 uint num = (uint)NetworkToHostOrder((int)PrivateAddress);
520 return new IPAddress(new ushort[8]
521 {
522 0,
523 0,
524 0,
525 0,
526 0,
527 65535,
528 (ushort)(num >> 16),
529 (ushort)num
530 }, 0u);
531 }
532
534 {
535 if (IsIPv4)
536 {
537 return this;
538 }
539 uint host = (uint)((_numbers[6] << 16) | _numbers[7]);
540 return new IPAddress((uint)HostToNetworkOrder((int)host));
541 }
542
543 [DoesNotReturn]
544 private static byte[] ThrowAddressNullException()
545 {
546 throw new ArgumentNullException("address");
547 }
548}
static readonly bool IsLittleEndian
static sbyte ReverseEndianness(sbyte value)
static IPAddress Parse(ReadOnlySpan< char > ipSpan, bool tryParse)
static unsafe string IPv4AddressToString(uint address)
static string IPv6AddressToString(ushort[] address, uint scopeId)
ReadOnlyIPAddress(ReadOnlySpan< byte > newAddress)
Definition IPAddress.cs:13
static int NetworkToHostOrder(int network)
Definition IPAddress.cs:440
static readonly IPAddress Broadcast
Definition IPAddress.cs:23
static IPAddress Parse(ReadOnlySpan< char > ipSpan)
Definition IPAddress.cs:329
static readonly IPAddress IPv6None
Definition IPAddress.cs:39
static readonly IPAddress None
Definition IPAddress.cs:25
IPAddress(ReadOnlySpan< ushort > numbers, uint scopeid)
Definition IPAddress.cs:262
static readonly IPAddress IPv6Loopback
Definition IPAddress.cs:33
static int HostToNetworkOrder(int host)
Definition IPAddress.cs:417
IPAddress MapToIPv6()
Definition IPAddress.cs:513
static IPAddress Parse(string ipString)
Definition IPAddress.cs:320
IPAddress MapToIPv4()
Definition IPAddress.cs:533
static readonly IPAddress Loopback
Definition IPAddress.cs:21
IPAddress(ReadOnlySpan< byte > address)
Definition IPAddress.cs:284
static byte[] ThrowAddressNullException()
Definition IPAddress.cs:544
static bool IsLoopback(IPAddress address)
Definition IPAddress.cs:450
static long HostToNetworkOrder(long host)
Definition IPAddress.cs:408
byte[] GetAddressBytes()
Definition IPAddress.cs:377
static long NetworkToHostOrder(long network)
Definition IPAddress.cs:435
override int GetHashCode()
Definition IPAddress.cs:496
readonly ushort[] _numbers
Definition IPAddress.cs:49
override string ToString()
Definition IPAddress.cs:390
static short NetworkToHostOrder(short network)
Definition IPAddress.cs:445
static short HostToNetworkOrder(short host)
Definition IPAddress.cs:426
bool TryWriteBytes(Span< byte > destination, out int bytesWritten)
Definition IPAddress.cs:334
bool TryFormat(Span< char > destination, out int charsWritten)
Definition IPAddress.cs:399
override bool Equals([NotNullWhen(true)] object? comparand)
Definition IPAddress.cs:468
static bool TryParse([NotNullWhen(true)] string? ipString, [NotNullWhen(true)] out IPAddress? address)
Definition IPAddress.cs:303
IPAddress(long newAddress)
Definition IPAddress.cs:230
static readonly IPAddress Any
Definition IPAddress.cs:19
IPAddress(ReadOnlySpan< byte > address, long scopeid)
Definition IPAddress.cs:244
static bool TryParse(ReadOnlySpan< char > ipSpan, [NotNullWhen(true)] out IPAddress? address)
Definition IPAddress.cs:314
void WriteIPv4Bytes(Span< byte > destination)
Definition IPAddress.cs:371
IPAddress(ushort[] numbers, uint scopeid)
Definition IPAddress.cs:273
static readonly IPAddress IPv6Any
Definition IPAddress.cs:27
static readonly IPAddress s_loopbackMappedToIPv6
Definition IPAddress.cs:41
IPAddress(byte[] address, long scopeid)
Definition IPAddress.cs:239
IPAddress(byte[] address)
Definition IPAddress.cs:279
void WriteIPv6Bytes(Span< byte > destination)
Definition IPAddress.cs:360
bool Equals(IPAddress comparand)
Definition IPAddress.cs:477
AddressFamily AddressFamily
Definition IPAddress.cs:88
static string dns_bad_ip_address
Definition SR.cs:28
Definition SR.cs:7