DnsService
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
namespace Relativity.Transfer
{
public class DnsService : IDnsService
{
private readonly ConcurrentDictionary<string, List<string>> dnsIpHostEntryLookup = new ConcurrentDictionary<string, List<string>>();
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Suppress DNS lookup related failures.")]
public IList<string> GetAliases(string hostName)
{
if (!string.IsNullOrEmpty(hostName)) {
string key = hostName.ToUpperInvariant();
List<string> value;
try {
if (dnsIpHostEntryLookup.TryGetValue(key, out value))
return value;
IPHostEntry hostByName = Dns.GetHostByName(hostName);
value = ((hostByName.Aliases == null || hostByName.Aliases.Length == 0) ? new List<string>() : new List<string>(hostByName.Aliases));
dnsIpHostEntryLookup.TryAdd(key, value);
return value;
} catch (Exception) {
value = new List<string>();
dnsIpHostEntryLookup.TryAdd(key, value);
return value;
}
}
throw new ArgumentNullException("hostName");
}
public bool IsBaseOf(Uri serverPath, Uri path)
{
List<Uri> list = CreateUriPaths(serverPath);
List<Uri> list2 = CreateUriPaths(path);
foreach (Uri item in list) {
foreach (Uri item2 in list2) {
if (item.Equals((object)item2) || item.IsBaseOf(item2))
return true;
}
}
return false;
}
private List<Uri> CreateUriPaths(Uri path)
{
IList<string> aliases = GetAliases(path.Host);
List<Uri> list = new List<Uri> {
path
};
if (aliases.Count > 0) {
string components = path.GetComponents(UriComponents.Path, UriFormat.Unescaped);
list.AddRange(from alias in aliases
where string.Compare(alias, path.Host, StringComparison.OrdinalIgnoreCase) != 0
select new UriBuilder {
Host = alias,
Path = components,
Scheme = "file"
} into builder
select builder.Uri);
}
return list;
}
}
}