From 8ab7d80b093de2e2ed71737e0138b7a7c2c92f99 Mon Sep 17 00:00:00 2001 From: Diva Canto Date: Fri, 30 Jul 2010 14:04:13 -0700 Subject: Changed the way HG client verification is done: now transforming local and LAN client IPs into external IPs. This addresses some issues related to running both the user agents service and the viewer in the same machine/LAN, which then presents a problem when the user agent goes to an external network. --- OpenSim/Framework/NetworkUtil.cs | 72 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) (limited to 'OpenSim/Framework') diff --git a/OpenSim/Framework/NetworkUtil.cs b/OpenSim/Framework/NetworkUtil.cs index 5fe343d..7c30bd3 100644 --- a/OpenSim/Framework/NetworkUtil.cs +++ b/OpenSim/Framework/NetworkUtil.cs @@ -31,6 +31,7 @@ using System.Net.Sockets; using System.Net; using System.Net.NetworkInformation; using System.Reflection; +using System.Text; using log4net; namespace OpenSim.Framework @@ -180,10 +181,14 @@ namespace OpenSim.Framework throw new ArgumentException("[NetworkUtil] Unable to resolve defaultHostname to an IPv4 address for an IPv4 client"); } + static IPAddress externalIPAddress; + static NetworkUtil() { try { + externalIPAddress = GetExternalIP(); + foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces()) { foreach (UnicastIPAddressInformation address in ni.GetIPProperties().UnicastAddresses) @@ -244,5 +249,72 @@ namespace OpenSim.Framework } return defaultHostname; } + + public static IPAddress GetExternalIPOf(IPAddress user) + { + // Check if we're accessing localhost. + foreach (IPAddress host in Dns.GetHostAddresses(Dns.GetHostName())) + { + if (host.Equals(user) && host.AddressFamily == AddressFamily.InterNetwork) + { + m_log.Info("[NetworkUtil] Localhost user detected, sending '" + externalIPAddress + "' instead of '" + user + "'"); + return externalIPAddress; + } + } + + // Check for same LAN segment + foreach (KeyValuePair subnet in m_subnets) + { + byte[] subnetBytes = subnet.Value.GetAddressBytes(); + byte[] localBytes = subnet.Key.GetAddressBytes(); + byte[] destBytes = user.GetAddressBytes(); + + if (subnetBytes.Length != destBytes.Length || subnetBytes.Length != localBytes.Length) + return user; + + bool valid = true; + + for (int i = 0; i < subnetBytes.Length; i++) + { + if ((localBytes[i] & subnetBytes[i]) != (destBytes[i] & subnetBytes[i])) + { + valid = false; + break; + } + } + + if (subnet.Key.AddressFamily != AddressFamily.InterNetwork) + valid = false; + + if (valid) + { + m_log.Info("[NetworkUtil] Local LAN user detected, sending '" + externalIPAddress + "' instead of '" + user + "'"); + return externalIPAddress; + } + } + + // Otherwise, return user address + return user; + } + + private static IPAddress GetExternalIP() + { + string whatIsMyIp = "http://www.whatismyip.com/automation/n09230945.asp"; + WebClient wc = new WebClient(); + UTF8Encoding utf8 = new UTF8Encoding(); + string requestHtml = ""; + try + { + requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp)); + } + catch (WebException we) + { + // do something with exception + m_log.Info("[NetworkUtil]: Exception in GetExternalIP: " + we.ToString()); + } + + IPAddress externalIp = IPAddress.Parse(requestHtml); + return externalIp; + } } } -- cgit v1.1