| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.IO; |
| | | 7 | | using System.Net; |
| | | 8 | | using System.Net.Sockets; |
| | | 9 | | using System.Runtime.InteropServices; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.Channels |
| | | 12 | | { |
| | | 13 | | internal static class DnsCache |
| | | 14 | | { |
| | | 15 | | private const string LinuxProcHostnamePath = "/proc/sys/kernel/hostname"; |
| | | 16 | | |
| | | 17 | | // Double-checked locking pattern requires volatile for read/write synchronization |
| | | 18 | | private static volatile string s_machineName; |
| | | 19 | | |
| | 4 | 20 | | private static object ThisLock { get; } = new object(); |
| | | 21 | | |
| | | 22 | | public static string MachineName |
| | | 23 | | { |
| | | 24 | | get |
| | | 25 | | { |
| | 12 | 26 | | if (s_machineName == null) |
| | | 27 | | { |
| | 2 | 28 | | lock (ThisLock) |
| | | 29 | | { |
| | 2 | 30 | | if (s_machineName == null) |
| | | 31 | | { |
| | | 32 | | try |
| | | 33 | | { |
| | 2 | 34 | | s_machineName = Dns.GetHostEntry(string.Empty).HostName; |
| | 2 | 35 | | } |
| | | 36 | | catch (SocketException exception) |
| | | 37 | | { |
| | 0 | 38 | | DiagnosticUtility.TraceHandledException(exception, |
| | 0 | 39 | | TraceEventType.Information); |
| | 0 | 40 | | } |
| | | 41 | | } |
| | | 42 | | |
| | 2 | 43 | | if (s_machineName == null) // prior attempt failed |
| | | 44 | | { |
| | | 45 | | try |
| | | 46 | | { |
| | | 47 | | // This uses a different native API so might work where the previous one didn't. |
| | 0 | 48 | | s_machineName = Dns.GetHostName(); |
| | 0 | 49 | | } |
| | | 50 | | catch (SocketException exception) |
| | | 51 | | { |
| | 0 | 52 | | DiagnosticUtility.TraceHandledException(exception, |
| | 0 | 53 | | TraceEventType.Information); |
| | 0 | 54 | | } |
| | | 55 | | } |
| | | 56 | | |
| | 2 | 57 | | if (s_machineName == null && RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && File.Exists(Li |
| | | 58 | | { |
| | | 59 | | try |
| | | 60 | | { |
| | | 61 | | // Final attempt, try to get the hostname from the proc filesystem if running on Linux |
| | 0 | 62 | | string[] hostnameFile = File.ReadAllLines(LinuxProcHostnamePath); |
| | 0 | 63 | | if (hostnameFile.Length > 0 && !string.IsNullOrEmpty(hostnameFile[0])) |
| | | 64 | | { |
| | 0 | 65 | | s_machineName = hostnameFile[0]; |
| | | 66 | | } |
| | 0 | 67 | | } |
| | | 68 | | catch (Exception exception) // There's no common base exception that File.ReadAllLines can t |
| | | 69 | | { |
| | 0 | 70 | | DiagnosticUtility.TraceHandledException(exception, |
| | 0 | 71 | | TraceEventType.Information); |
| | 0 | 72 | | } |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | // Final fallback if every other mechanism fails |
| | 2 | 76 | | if (s_machineName == null) |
| | | 77 | | { |
| | 0 | 78 | | s_machineName = "localhost"; |
| | | 79 | | } |
| | 2 | 80 | | } |
| | | 81 | | } |
| | | 82 | | |
| | 12 | 83 | | return s_machineName; |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | } |
| | | 87 | | } |