29 using System.Collections.Generic;
30 using System.Net.Sockets;
32 using System.Net.NetworkInformation;
33 using System.Reflection;
37 namespace OpenSim.Framework
47 public static class NetworkUtil
50 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
52 private static bool m_disabled =
true;
54 public static bool Enabled
56 set { m_disabled = value; }
57 get {
return m_disabled; }
61 static readonly Dictionary<IPAddress,IPAddress> m_subnets =
new Dictionary<IPAddress, IPAddress>();
63 public static IPAddress GetIPFor(IPAddress user, IPAddress simulator)
69 foreach (IPAddress host
in Dns.GetHostAddresses(Dns.GetHostName()))
71 if (host.Equals(user) && host.AddressFamily == AddressFamily.InterNetwork)
73 m_log.Info(
"[NetworkUtil] Localhost user detected, sending them '" + host +
"' instead of '" + simulator +
"'");
79 foreach (KeyValuePair<IPAddress, IPAddress> subnet
in m_subnets)
81 byte[] subnetBytes = subnet.Value.GetAddressBytes();
82 byte[] localBytes = subnet.Key.GetAddressBytes();
83 byte[] destBytes = user.GetAddressBytes();
85 if (subnetBytes.Length != destBytes.Length || subnetBytes.Length != localBytes.Length)
90 for (
int i = 0; i < subnetBytes.Length; i++)
92 if ((localBytes[i] & subnetBytes[i]) != (destBytes[i] & subnetBytes[i]))
99 if (subnet.Key.AddressFamily != AddressFamily.InterNetwork)
104 m_log.Info(
"[NetworkUtil] Local LAN user detected, sending them '" + subnet.Key +
"' instead of '" + simulator +
"'");
113 private static IPAddress GetExternalIPFor(IPAddress destination,
string defaultHostname)
116 if (destination.AddressFamily == AddressFamily.InterNetworkV6)
118 foreach (IPAddress host
in Dns.GetHostAddresses(defaultHostname))
120 if (host.AddressFamily == AddressFamily.InterNetworkV6)
122 m_log.Info(
"[NetworkUtil] Localhost user detected, sending them '" + host +
"' instead of '" + defaultHostname +
"'");
128 if (destination.AddressFamily != AddressFamily.InterNetwork)
132 foreach (KeyValuePair<IPAddress, IPAddress> pair
in m_subnets)
134 IPAddress host = pair.Value;
135 if (host.Equals(destination) && host.AddressFamily == AddressFamily.InterNetwork)
137 m_log.Info(
"[NATROUTING] Localhost user detected, sending them '" + host +
"' instead of '" + defaultHostname +
"'");
143 foreach (KeyValuePair<IPAddress, IPAddress> subnet
in m_subnets)
145 byte[] subnetBytes = subnet.Value.GetAddressBytes();
146 byte[] localBytes = subnet.Key.GetAddressBytes();
147 byte[] destBytes = destination.GetAddressBytes();
149 if (subnetBytes.Length != destBytes.Length || subnetBytes.Length != localBytes.Length)
154 for (
int i=0;i<subnetBytes.Length;i++)
156 if ((localBytes[i] & subnetBytes[i]) != (destBytes[i] & subnetBytes[i]))
163 if (subnet.Key.AddressFamily != AddressFamily.InterNetwork)
168 m_log.Info(
"[NetworkUtil] Local LAN user detected, sending them '" + subnet.Key +
"' instead of '" + defaultHostname +
"'");
174 foreach (IPAddress host
in Dns.GetHostAddresses(defaultHostname))
176 if (host.AddressFamily == AddressFamily.InterNetwork)
181 throw new ArgumentException(
"[NetworkUtil] Unable to resolve defaultHostname to an IPv4 address for an IPv4 client");
188 foreach (NetworkInterface ni
in NetworkInterface.GetAllNetworkInterfaces())
190 foreach (UnicastIPAddressInformation address
in ni.GetIPProperties().UnicastAddresses)
192 if (address.Address.AddressFamily == AddressFamily.InterNetwork)
194 if (address.IPv4Mask != null)
196 m_subnets.Add(address.Address, address.IPv4Mask);
202 catch (NotImplementedException)
208 public static IPAddress GetIPFor(IPEndPoint user,
string defaultHostname)
213 IPAddress rtn = GetExternalIPFor(user.Address, defaultHostname);
221 if (IPAddress.TryParse(defaultHostname, out ia))
226 foreach (IPAddress Adr
in Dns.GetHostAddresses(defaultHostname))
228 if (Adr.AddressFamily == AddressFamily.InterNetwork)
238 public static string GetHostFor(IPAddress user,
string defaultHostname)
242 IPAddress rtn = GetExternalIPFor(user, defaultHostname);
244 return rtn.ToString();
246 return defaultHostname;