11using System ;
22using System . Collections . Generic ;
3+ using System . IO ;
34using System . Linq ;
45using System . Net ;
56using System . Net . NetworkInformation ;
@@ -254,7 +255,7 @@ public static IReadOnlyCollection<NameServer> ResolveNameServers(bool skipIPv6Si
254255 }
255256 catch ( Exception ex )
256257 {
257- logger ? . LogInformation ( ex , "Resolving name servers using .NET framework failed." ) ;
258+ logger ? . LogWarning ( ex , "Resolving name servers using .NET framework failed." ) ;
258259 exceptions . Add ( ex ) ;
259260 }
260261
@@ -270,7 +271,7 @@ public static IReadOnlyCollection<NameServer> ResolveNameServers(bool skipIPv6Si
270271 }
271272 catch ( Exception ex )
272273 {
273- logger ? . LogInformation ( ex , "Resolving name servers using native implementation failed." ) ;
274+ logger ? . LogWarning ( ex , "Resolving name servers using native implementation failed." ) ;
274275 exceptions . Add ( ex ) ;
275276 }
276277 }
@@ -305,36 +306,39 @@ public static IReadOnlyCollection<NameServer> ResolveNameServers(bool skipIPv6Si
305306 }
306307
307308#endif
308- if ( ! fallbackToGooglePublicDns && exceptions . Count > 0 )
309- {
310- if ( exceptions . Count > 1 )
311- {
312- throw new AggregateException ( "Error resolving name servers" , exceptions ) ;
313- }
314- else
315- {
316- throw new InvalidOperationException ( "Error resolving name servers" , exceptions . First ( ) ) ;
317- }
318- }
319-
320309 IReadOnlyCollection < NameServer > filtered = nameServers
321310 . Where ( p => ( p . IPEndPoint . Address . AddressFamily == AddressFamily . InterNetwork
322311 || p . IPEndPoint . Address . AddressFamily == AddressFamily . InterNetworkV6 )
323312 && ( ! p . IPEndPoint . Address . IsIPv6SiteLocal || ! skipIPv6SiteLocal ) )
324313 . ToArray ( ) ;
325314
326- filtered = ValidateNameServers ( filtered , logger ) ;
315+ try
316+ {
317+ filtered = ValidateNameServers ( filtered , logger ) ;
318+ }
319+ catch ( Exception ex )
320+ {
321+ logger ? . LogWarning ( ex , "NameServer validation failed." ) ;
322+ exceptions . Add ( ex ) ;
323+ }
327324
328- if ( filtered . Count == 0 && fallbackToGooglePublicDns )
325+ if ( filtered . Count == 0 )
329326 {
330- logger ? . LogWarning ( "Could not resolve any NameServers, falling back to Google public servers." ) ;
331- return new NameServer [ ]
327+ if ( ! fallbackToGooglePublicDns && exceptions . Count > 0 )
332328 {
333- GooglePublicDnsIPv6 ,
334- GooglePublicDns2IPv6 ,
335- GooglePublicDns ,
336- GooglePublicDns2 ,
337- } ;
329+ throw new InvalidOperationException ( "Could not resolve any NameServers." , exceptions . First ( ) ) ;
330+ }
331+ else if ( fallbackToGooglePublicDns )
332+ {
333+ logger ? . LogWarning ( "Could not resolve any NameServers, falling back to Google public servers." ) ;
334+ return new NameServer [ ]
335+ {
336+ GooglePublicDns ,
337+ GooglePublicDns2 ,
338+ GooglePublicDnsIPv6 ,
339+ GooglePublicDns2IPv6
340+ } ;
341+ }
338342 }
339343
340344 logger ? . LogDebug ( "Resolved {0} name servers: [{1}]." , filtered . Count , string . Join ( "," , filtered . AsEnumerable ( ) ) ) ;
@@ -357,18 +361,37 @@ public static IReadOnlyCollection<NameServer> ResolveNameServers(bool skipIPv6Si
357361 public static IReadOnlyCollection < NameServer > ResolveNameServersNative ( )
358362 {
359363 List < NameServer > addresses = new List < NameServer > ( ) ;
364+
365+ #if NET5_0_OR_GREATER
366+ if ( OperatingSystem . IsWindows ( ) )
367+ #else
360368 if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
369+ #endif
361370 {
362- var fixedInfo = Windows . IpHlpApi . FixedNetworkInformation . GetFixedInformation ( ) ;
363-
364- foreach ( var ip in fixedInfo . DnsAddresses )
371+ try
365372 {
366- addresses . Add ( new NameServer ( ip , DefaultPort , fixedInfo . DomainName ) ) ;
373+ var fixedInfo = Windows . IpHlpApi . FixedNetworkInformation . GetFixedInformation ( ) ;
374+
375+ foreach ( var ip in fixedInfo . DnsAddresses )
376+ {
377+ addresses . Add ( new NameServer ( ip , DefaultPort , fixedInfo . DomainName ) ) ;
378+ }
367379 }
380+ catch { }
368381 }
382+ #if NET5_0_OR_GREATER
383+ if ( OperatingSystem . IsLinux ( ) || OperatingSystem . IsMacOS ( ) )
384+ #else
369385 else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) || RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
386+ #endif
370387 {
371- addresses = Linux . StringParsingHelpers . ParseDnsAddressesFromResolvConfFile ( EtcResolvConfFile ) ;
388+ try
389+ {
390+ addresses = Linux . StringParsingHelpers . ParseDnsAddressesFromResolvConfFile ( EtcResolvConfFile ) ;
391+ }
392+ catch ( Exception e ) when ( e is FileNotFoundException || e is UnauthorizedAccessException )
393+ {
394+ }
372395 }
373396
374397 return addresses ;
@@ -380,12 +403,7 @@ public static IReadOnlyCollection<NameServer> ResolveNameServersNative()
380403 /// <returns>Returns a collection of name servers from the policy table</returns>
381404 public static IReadOnlyCollection < NameServer > ResolveNameResolutionPolicyServers ( )
382405 {
383- if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
384- {
385- return NameResolutionPolicy . Resolve ( ) ;
386- }
387-
388- return Array . Empty < NameServer > ( ) ;
406+ return NameResolutionPolicy . Resolve ( ) ;
389407 }
390408
391409#endif
@@ -413,16 +431,20 @@ private static IReadOnlyCollection<NameServer> QueryNetworkInterfaces()
413431 var result = new HashSet < NameServer > ( ) ;
414432
415433 var adapters = NetworkInterface . GetAllNetworkInterfaces ( ) ;
434+ if ( adapters == null )
435+ {
436+ return result . ToArray ( ) ;
437+ }
416438
417439 foreach ( NetworkInterface networkInterface in
418440 adapters
419- . Where ( p => ( p . OperationalStatus == OperationalStatus . Up || p . OperationalStatus == OperationalStatus . Unknown )
441+ . Where ( p => p != null && ( p . OperationalStatus == OperationalStatus . Up || p . OperationalStatus == OperationalStatus . Unknown )
420442 && p . NetworkInterfaceType != NetworkInterfaceType . Loopback ) )
421443 {
422- var properties = networkInterface . GetIPProperties ( ) ;
444+ var properties = networkInterface ? . GetIPProperties ( ) ;
423445
424446 // Can be null under mono for whatever reason...
425- if ( properties == null )
447+ if ( properties ? . DnsAddresses == null )
426448 {
427449 continue ;
428450 }
0 commit comments