Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Dns.cs
Go to the documentation of this file.
9
10namespace System.Net;
11
12public static class Dns
13{
15
16 public static string GetHostName()
17 {
18 ValueStopwatch stopwatch = NameResolutionTelemetry.Log.BeforeResolution(string.Empty);
19 string hostName;
20 try
21 {
22 hostName = NameResolutionPal.GetHostName();
23 }
25 {
26 throw;
27 }
28 NameResolutionTelemetry.Log.AfterResolution(stopwatch, successful: true);
29 if (System.Net.NetEventSource.Log.IsEnabled())
30 {
31 System.Net.NetEventSource.Info(null, hostName, "GetHostName");
32 }
33 return hostName;
34 }
35
36 public static IPHostEntry GetHostEntry(IPAddress address)
37 {
38 if (address == null)
39 {
40 throw new ArgumentNullException("address");
41 }
42 if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))
43 {
44 if (System.Net.NetEventSource.Log.IsEnabled())
45 {
46 System.Net.NetEventSource.Error(address, $"Invalid address '{address}'", "GetHostEntry");
47 }
49 }
51 if (System.Net.NetEventSource.Log.IsEnabled())
52 {
53 System.Net.NetEventSource.Info(address, $"{hostEntryCore} with {hostEntryCore.AddressList.Length} entries", "GetHostEntry");
54 }
55 return hostEntryCore;
56 }
57
59 {
60 return GetHostEntry(hostNameOrAddress, AddressFamily.Unspecified);
61 }
62
64 {
65 if (hostNameOrAddress == null)
66 {
67 throw new ArgumentNullException("hostNameOrAddress");
68 }
71 {
72 if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))
73 {
74 if (System.Net.NetEventSource.Log.IsEnabled())
75 {
76 System.Net.NetEventSource.Error(address, $"Invalid address '{address}'", "GetHostEntry");
77 }
78 throw new ArgumentException(System.SR.Format(System.SR.net_invalid_ip_addr, "hostNameOrAddress"));
79 }
81 }
82 else
83 {
85 }
86 if (System.Net.NetEventSource.Log.IsEnabled())
87 {
88 System.Net.NetEventSource.Info(hostNameOrAddress, $"{hostEntryCore} with {hostEntryCore.AddressList.Length} entries", "GetHostEntry");
89 }
90 return hostEntryCore;
91 }
92
97
102
104 {
105 if (System.Net.NetEventSource.Log.IsEnabled())
106 {
108 hostEntryCoreAsync.ContinueWith(delegate(Task<IPHostEntry> t, object s)
109 {
110 string text = (string)s;
111 if (t.Status == TaskStatus.RanToCompletion)
112 {
113 System.Net.NetEventSource.Info(text, $"{t.Result} with {t.Result.AddressList.Length} entries", "GetHostEntryAsync");
114 }
115 Exception ex = t.Exception?.InnerException;
117 {
118 System.Net.NetEventSource.Error(text, $"{text} DNS lookup failed with {((ExternalException)(object)ex2).ErrorCode}", "GetHostEntryAsync");
119 }
121 {
122 System.Net.NetEventSource.Error(text, $"{text} DNS lookup was canceled", "GetHostEntryAsync");
123 }
125 return hostEntryCoreAsync;
126 }
128 }
129
131 {
132 if (address == null)
133 {
134 throw new ArgumentNullException("address");
135 }
136 if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))
137 {
138 if (System.Net.NetEventSource.Log.IsEnabled())
139 {
140 System.Net.NetEventSource.Error(address, $"Invalid address '{address}'", "GetHostEntryAsync");
141 }
142 throw new ArgumentException(System.SR.net_invalid_ip_addr, "address");
143 }
145 {
147 if (System.Net.NetEventSource.Log.IsEnabled())
148 {
149 System.Net.NetEventSource.Info((IPAddress)s, $"{hostEntryCore} with {hostEntryCore.AddressList.Length} entries", "GetHostEntryAsync");
150 }
151 return hostEntryCore;
152 }, address, CancellationToken.None);
153 }
154
155 public static IAsyncResult BeginGetHostEntry(IPAddress address, AsyncCallback? requestCallback, object? stateObject)
156 {
158 }
159
164
169
171 {
173 }
174
176 {
177 if (hostNameOrAddress == null)
178 {
179 throw new ArgumentNullException("hostNameOrAddress");
180 }
183 {
184 if (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any))
185 {
186 if (System.Net.NetEventSource.Log.IsEnabled())
187 {
188 System.Net.NetEventSource.Error(address, $"Invalid address '{address}'", "GetHostAddresses");
189 }
190 throw new ArgumentException(System.SR.Format(System.SR.net_invalid_ip_addr, "hostNameOrAddress"));
191 }
192 array = ((family != 0 && address.AddressFamily != family) ? Array.Empty<IPAddress>() : new IPAddress[1] { address });
193 }
194 else
195 {
197 }
198 if (System.Net.NetEventSource.Log.IsEnabled())
199 {
200 System.Net.NetEventSource.Info(hostNameOrAddress, array, "GetHostAddresses");
201 }
202 return array;
203 }
204
209
214
219
224
226 {
227 return System.Threading.Tasks.TaskToApm.End<IPAddress[]>(asyncResult ?? throw new ArgumentNullException("asyncResult"));
228 }
229
230 [Obsolete("GetHostByName has been deprecated. Use GetHostEntry instead.")]
231 public static IPHostEntry GetHostByName(string hostName)
232 {
233 if (hostName == null)
234 {
235 throw new ArgumentNullException("hostName");
236 }
237 if (IPAddress.TryParse(hostName, out IPAddress address))
238 {
239 return CreateHostEntryForAddress(address);
240 }
241 return GetHostEntryCore(hostName, AddressFamily.Unspecified);
242 }
243
244 [Obsolete("BeginGetHostByName has been deprecated. Use BeginGetHostEntry instead.")]
245 public static IAsyncResult BeginGetHostByName(string hostName, AsyncCallback? requestCallback, object? stateObject)
246 {
248 }
249
250 [Obsolete("EndGetHostByName has been deprecated. Use EndGetHostEntry instead.")]
255
256 [Obsolete("GetHostByAddress has been deprecated. Use GetHostEntry instead.")]
257 public static IPHostEntry GetHostByAddress(string address)
258 {
259 if (address == null)
260 {
261 throw new ArgumentNullException("address");
262 }
264 if (System.Net.NetEventSource.Log.IsEnabled())
265 {
266 System.Net.NetEventSource.Info(address, hostEntryCore, "GetHostByAddress");
267 }
268 return hostEntryCore;
269 }
270
271 [Obsolete("GetHostByAddress has been deprecated. Use GetHostEntry instead.")]
273 {
274 if (address == null)
275 {
276 throw new ArgumentNullException("address");
277 }
279 if (System.Net.NetEventSource.Log.IsEnabled())
280 {
281 System.Net.NetEventSource.Info(address, hostEntryCore, "GetHostByAddress");
282 }
283 return hostEntryCore;
284 }
285
286 [Obsolete("Resolve has been deprecated. Use GetHostEntry instead.")]
287 public static IPHostEntry Resolve(string hostName)
288 {
289 if (hostName == null)
290 {
291 throw new ArgumentNullException("hostName");
292 }
294 if (IPAddress.TryParse(hostName, out IPAddress address) && (address.AddressFamily != AddressFamily.InterNetworkV6 || SocketProtocolSupportPal.OSSupportsIPv6))
295 {
296 try
297 {
298 iPHostEntry = GetHostEntryCore(address, AddressFamily.Unspecified);
299 }
300 catch (SocketException message)
301 {
302 if (System.Net.NetEventSource.Log.IsEnabled())
303 {
304 System.Net.NetEventSource.Error(hostName, message, "Resolve");
305 }
307 }
308 }
309 else
310 {
311 iPHostEntry = GetHostEntryCore(hostName, AddressFamily.Unspecified);
312 }
313 if (System.Net.NetEventSource.Log.IsEnabled())
314 {
315 System.Net.NetEventSource.Info(hostName, iPHostEntry, "Resolve");
316 }
317 return iPHostEntry;
318 }
319
320 [Obsolete("BeginResolve has been deprecated. Use BeginGetHostEntry instead.")]
321 public static IAsyncResult BeginResolve(string hostName, AsyncCallback? requestCallback, object? stateObject)
322 {
324 }
325
326 [Obsolete("EndResolve has been deprecated. Use EndGetHostEntry instead.")]
328 {
330 try
331 {
333 }
334 catch (SocketException message)
335 {
337 object obj2 = obj;
340 if (iPAddress3 == null)
341 {
342 throw;
343 }
344 if (System.Net.NetEventSource.Log.IsEnabled())
345 {
346 System.Net.NetEventSource.Error(null, message, "EndResolve");
347 }
349 }
350 if (System.Net.NetEventSource.Log.IsEnabled())
351 {
352 System.Net.NetEventSource.Info(null, iPHostEntry, "EndResolve");
353 }
354 return iPHostEntry;
355 }
356
361
366
368 {
369 ValidateHostName(hostName);
370 if (!stopwatch.IsActive)
371 {
372 stopwatch = NameResolutionTelemetry.Log.BeforeResolution(hostName);
373 }
374 object result;
375 try
376 {
377 string hostName2;
378 string[] aliases;
380 int nativeErrorCode;
382 if (socketError != 0)
383 {
384 if (System.Net.NetEventSource.Log.IsEnabled())
385 {
386 System.Net.NetEventSource.Error(hostName, $"{hostName} DNS lookup failed with {socketError}", "GetHostEntryOrAddressesCore");
387 }
389 }
390 result = (justAddresses ? ((object)addresses) : ((object)new IPHostEntry
391 {
392 AddressList = addresses,
393 HostName = hostName2,
394 Aliases = aliases
395 }));
396 }
397 catch when (LogFailure(stopwatch))
398 {
399 throw;
400 }
401 NameResolutionTelemetry.Log.AfterResolution(stopwatch, successful: true);
402 return result;
403 }
404
409
414
416 {
417 if (!stopwatch.IsActive)
418 {
419 stopwatch = NameResolutionTelemetry.Log.BeforeResolution(address);
420 }
421 string text;
422 try
423 {
425 if (errorCode != 0)
426 {
427 if (System.Net.NetEventSource.Log.IsEnabled())
428 {
429 System.Net.NetEventSource.Error(address, $"{address} DNS lookup failed with {errorCode}", "GetHostEntryOrAddressesCore");
430 }
432 }
433 }
434 catch when (LogFailure(stopwatch))
435 {
436 throw;
437 }
438 NameResolutionTelemetry.Log.AfterResolution(stopwatch, successful: true);
439 stopwatch = NameResolutionTelemetry.Log.BeforeResolution(text);
440 object result;
441 try
442 {
443 string hostName;
444 string[] aliases;
448 if (errorCode != 0 && System.Net.NetEventSource.Log.IsEnabled())
449 {
450 System.Net.NetEventSource.Error(address, $"forward lookup for '{text}' failed with {errorCode}", "GetHostEntryOrAddressesCore");
451 }
452 result = (justAddresses ? ((object)addresses) : ((object)new IPHostEntry
453 {
454 HostName = hostName,
455 Aliases = aliases,
456 AddressList = addresses
457 }));
458 }
459 catch when (LogFailure(stopwatch))
460 {
461 throw;
462 }
463 NameResolutionTelemetry.Log.AfterResolution(stopwatch, successful: true);
464 return result;
465 }
466
471
473 {
474 if (hostName == null)
475 {
476 throw new ArgumentNullException("hostName");
477 }
478 if (cancellationToken.IsCancellationRequested)
479 {
480 if (!justAddresses)
481 {
483 }
485 }
486 object key;
487 if (IPAddress.TryParse(hostName, out IPAddress address))
488 {
489 if (throwOnIIPAny && (address.Equals(IPAddress.Any) || address.Equals(IPAddress.IPv6Any)))
490 {
491 if (System.Net.NetEventSource.Log.IsEnabled())
492 {
493 System.Net.NetEventSource.Error(hostName, $"Invalid address '{address}'", "GetHostEntryOrAddressesCoreAsync");
494 }
495 throw new ArgumentException(System.SR.net_invalid_ip_addr, "hostName");
496 }
498 {
499 if (!justAddresses)
500 {
501 return Task.FromResult(CreateHostEntryForAddress(address));
502 }
503 return Task.FromResult((family != 0 && address.AddressFamily != family) ? Array.Empty<IPAddress>() : new IPAddress[1] { address });
504 }
505 key = ((family == AddressFamily.Unspecified) ? address : ((object)new KeyValuePair<IPAddress, AddressFamily>(address, family)));
506 }
507 else
508 {
510 {
511 ValidateHostName(hostName);
513 if (task != null)
514 {
515 return task;
516 }
517 }
518 key = ((family == AddressFamily.Unspecified) ? hostName : ((object)new KeyValuePair<string, AddressFamily>(hostName, family)));
519 }
520 if (justAddresses)
521 {
523 {
524 if (s is string hostName3)
525 {
527 }
529 {
531 }
533 {
535 }
538 }
540 {
541 if (s is string hostName2)
542 {
544 }
546 {
548 }
550 {
551 return GetHostEntryCore(address2, AddressFamily.Unspecified, stopwatch);
552 }
555 }
556
558 {
561 if (addrInfoAsync != null)
562 {
563 return CompleteAsync(addrInfoAsync, hostName, stopwatch2);
564 }
565 return null;
567 {
568 NameResolutionTelemetry.Log.BeforeResolution(hostName);
569 T result = null;
570 try
571 {
572 result = await ((Task<T>)task).ConfigureAwait(continueOnCapturedContext: false);
573 return result;
574 }
575 finally
576 {
577 NameResolutionTelemetry.Log.AfterResolution(stopwatch, result != null);
578 }
579 }
580 }
581
583 {
585 iPHostEntry.HostName = address.ToString();
586 iPHostEntry.Aliases = Array.Empty<string>();
587 iPHostEntry.AddressList = new IPAddress[1] { address };
588 return iPHostEntry;
589 }
590
591 private static void ValidateHostName(string hostName)
592 {
593 if (hostName.Length > 255 || (hostName.Length == 255 && hostName[254] != '.'))
594 {
595 throw new ArgumentOutOfRangeException("hostName", System.SR.Format(System.SR.net_toolong, "hostName", 255.ToString(NumberFormatInfo.CurrentInfo)));
596 }
597 }
598
600 {
601 NameResolutionTelemetry.Log.AfterResolution(stopwatch, successful: false);
602 return false;
603 }
604
606 {
608 Task<TResult> task2 = null;
609 lock (s_tasks)
610 {
611 s_tasks.TryGetValue(key, out var value);
612 if (value == null)
613 {
615 }
616 task2 = value.ContinueWith(delegate
617 {
618 try
619 {
620 return func(key, stopwatch);
621 }
622 finally
623 {
624 lock (s_tasks)
625 {
627 }
628 }
630 if (cancellationToken.CanBeCanceled)
631 {
632 task2.ContinueWith(delegate(Task<TResult> task, object key)
633 {
634 lock (s_tasks)
635 {
637 }
638 }, key, CancellationToken.None, TaskContinuationOptions.OnlyOnCanceled | TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
639 }
640 s_tasks[key] = task2;
641 }
642 return task2;
643 }
644}
bool ICollection< KeyValuePair< TKey, TValue > >. Remove(KeyValuePair< TKey, TValue > keyValuePair)
static IAsyncResult BeginGetHostEntry(IPAddress address, AsyncCallback? requestCallback, object? stateObject)
Definition Dns.cs:155
static Task< IPAddress[]> GetHostAddressesAsync(string hostNameOrAddress)
Definition Dns.cs:205
static Task< IPHostEntry > GetHostEntryAsync(string hostNameOrAddress)
Definition Dns.cs:93
static IPHostEntry GetHostByName(string hostName)
Definition Dns.cs:231
static object GetHostEntryOrAddressesCore(string hostName, bool justAddresses, AddressFamily addressFamily, ValueStopwatch stopwatch)
Definition Dns.cs:367
static void ValidateHostName(string hostName)
Definition Dns.cs:591
static IPHostEntry EndResolve(IAsyncResult asyncResult)
Definition Dns.cs:327
static IPHostEntry GetHostEntry(string hostNameOrAddress, AddressFamily family)
Definition Dns.cs:63
static IPHostEntry GetHostEntryCore(IPAddress address, AddressFamily addressFamily, ValueStopwatch stopwatch=default(ValueStopwatch))
Definition Dns.cs:405
static IPHostEntry EndGetHostByName(IAsyncResult asyncResult)
Definition Dns.cs:251
static IPHostEntry GetHostEntry(string hostNameOrAddress)
Definition Dns.cs:58
static Task< IPAddress[]> GetHostAddressesAsync(string hostNameOrAddress, CancellationToken cancellationToken)
Definition Dns.cs:210
static IPAddress[] GetHostAddressesCore(IPAddress address, AddressFamily addressFamily, ValueStopwatch stopwatch)
Definition Dns.cs:410
static Task< TResult > RunAsync< TResult >(Func< object, ValueStopwatch, TResult > func, object key, CancellationToken cancellationToken)
Definition Dns.cs:605
static IPHostEntry CreateHostEntryForAddress(IPAddress address)
Definition Dns.cs:582
static IPHostEntry EndGetHostEntry(IAsyncResult asyncResult)
Definition Dns.cs:165
static Task< IPHostEntry > GetHostEntryAsync(IPAddress address)
Definition Dns.cs:130
static string GetHostName()
Definition Dns.cs:16
static Task< T > GetAddrInfoWithTelemetryAsync< T >(string hostName, bool justAddresses, AddressFamily addressFamily, CancellationToken cancellationToken)
Definition Dns.cs:557
static IAsyncResult BeginResolve(string hostName, AsyncCallback? requestCallback, object? stateObject)
Definition Dns.cs:321
static IPHostEntry GetHostByAddress(IPAddress address)
Definition Dns.cs:272
static Task< IPAddress[]> GetHostAddressesAsync(string hostNameOrAddress, AddressFamily family, CancellationToken cancellationToken=default(CancellationToken))
Definition Dns.cs:215
static IAsyncResult BeginGetHostEntry(string hostNameOrAddress, AsyncCallback? requestCallback, object? stateObject)
Definition Dns.cs:160
static IAsyncResult BeginGetHostAddresses(string hostNameOrAddress, AsyncCallback? requestCallback, object? state)
Definition Dns.cs:220
static object GetHostEntryOrAddressesCore(IPAddress address, bool justAddresses, AddressFamily addressFamily, ValueStopwatch stopwatch)
Definition Dns.cs:415
static Task< IPHostEntry > GetHostEntryAsync(string hostNameOrAddress, AddressFamily family, CancellationToken cancellationToken=default(CancellationToken))
Definition Dns.cs:103
static IPAddress[] GetHostAddresses(string hostNameOrAddress, AddressFamily family)
Definition Dns.cs:175
static IAsyncResult BeginGetHostByName(string hostName, AsyncCallback? requestCallback, object? stateObject)
Definition Dns.cs:245
static IPHostEntry GetHostByAddress(string address)
Definition Dns.cs:257
static bool LogFailure(ValueStopwatch stopwatch)
Definition Dns.cs:599
static Task< IPHostEntry > GetHostEntryCoreAsync(string hostName, bool justReturnParsedIp, bool throwOnIIPAny, AddressFamily family, CancellationToken cancellationToken)
Definition Dns.cs:467
static IPHostEntry Resolve(string hostName)
Definition Dns.cs:287
static Task< IPHostEntry > GetHostEntryAsync(string hostNameOrAddress, CancellationToken cancellationToken)
Definition Dns.cs:98
static IPAddress[] EndGetHostAddresses(IAsyncResult asyncResult)
Definition Dns.cs:225
static IPHostEntry GetHostEntry(IPAddress address)
Definition Dns.cs:36
static IPHostEntry GetHostEntryCore(string hostName, AddressFamily addressFamily, ValueStopwatch stopwatch=default(ValueStopwatch))
Definition Dns.cs:357
static IPAddress[] GetHostAddresses(string hostNameOrAddress)
Definition Dns.cs:170
static IPAddress[] GetHostAddressesCore(string hostName, AddressFamily addressFamily, ValueStopwatch stopwatch=default(ValueStopwatch))
Definition Dns.cs:362
static Task GetHostEntryOrAddressesCoreAsync(string hostName, bool justReturnParsedIp, bool throwOnIIPAny, bool justAddresses, AddressFamily family, CancellationToken cancellationToken)
Definition Dns.cs:472
static readonly Dictionary< object, Task > s_tasks
Definition Dns.cs:14
static IPAddress Parse(string ipString)
Definition IPAddress.cs:320
override string ToString()
Definition IPAddress.cs:390
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
static readonly IPAddress Any
Definition IPAddress.cs:19
static readonly IPAddress IPv6Any
Definition IPAddress.cs:27
static SocketException CreateSocketException(SocketError errorCode, int platformError)
static unsafe string GetHostName()
static unsafe SocketError TryGetAddrInfo(string name, bool justAddresses, AddressFamily addressFamily, out string hostName, out string[] aliases, out IPAddress[] addresses, out int nativeErrorCode)
static unsafe string TryGetNameInfo(IPAddress addr, out SocketError errorCode, out int nativeErrorCode)
static unsafe Task GetAddrInfoAsync(string hostName, bool justAddresses, AddressFamily family, CancellationToken cancellationToken)
static readonly NameResolutionTelemetry Log
static readonly System.Net.NetEventSource Log
static void Info(object thisOrContextObject, FormattableString formattableString=null, [CallerMemberName] string memberName=null)
static void Error(object thisOrContextObject, FormattableString formattableString, [CallerMemberName] string memberName=null)
static string net_invalid_ip_addr
Definition SR.cs:16
static string net_toolong
Definition SR.cs:14
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
Definition SR.cs:7
static IAsyncResult Begin(Task task, AsyncCallback callback, object state)
Definition TaskToApm.cs:43
static void End(IAsyncResult asyncResult)
Definition TaskToApm.cs:48
static Task FromCanceled(CancellationToken cancellationToken)
Definition Task.cs:3363
static Task CompletedTask
Definition Task.cs:1120
static Microsoft.Extensions.Internal.ValueStopwatch StartNew()