forked from BackTrak/MemCacheDManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkHelper.cs
58 lines (49 loc) · 1.57 KB
/
NetworkHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
namespace MemCacheDManager
{
public class NetworkHelper
{
private static List<string> _resolvedRemotMachineNames = new List<string>();
private static List<string> _resolvedLocalMachineNames = new List<string>();
/// <summary>
/// Tests the passed machine name or network address to see if it is the local machine.
/// </summary>
/// <param name="machineName"></param>
/// <returns></returns>
public static bool IsMachineNameLocal(string machineName)
{
machineName = machineName.Trim().ToLower();
bool machineNameIsLocalMachine = false;
if (_resolvedRemotMachineNames.Contains(machineName) == false && _resolvedLocalMachineNames.Contains(machineName) == false)
{
IPHostEntry ipHostEntryMachineName = Dns.GetHostEntry(machineName);
IPHostEntry ipHostEntryLocal = Dns.GetHostEntry("localhost");
foreach (IPAddress hostEntryIP in ipHostEntryMachineName.AddressList)
{
foreach (IPAddress localEntryIP in ipHostEntryLocal.AddressList)
{
if (hostEntryIP.Equals(localEntryIP) == true)
{
_resolvedLocalMachineNames.Add(machineName);
machineNameIsLocalMachine = true;
break;
}
}
if (machineNameIsLocalMachine == true)
break;
}
if (machineNameIsLocalMachine == false)
_resolvedRemotMachineNames.Add(machineName);
}
else
{
if (_resolvedLocalMachineNames.Contains(machineName.ToLower()) == true)
machineNameIsLocalMachine = true;
}
return machineNameIsLocalMachine;
}
}
}