aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/NetworkUtil.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Framework/NetworkUtil.cs82
1 files changed, 0 insertions, 82 deletions
diff --git a/OpenSim/Framework/NetworkUtil.cs b/OpenSim/Framework/NetworkUtil.cs
index 831ff70..2e94b0d 100644
--- a/OpenSim/Framework/NetworkUtil.cs
+++ b/OpenSim/Framework/NetworkUtil.cs
@@ -181,18 +181,10 @@ namespace OpenSim.Framework
181 throw new ArgumentException("[NetworkUtil] Unable to resolve defaultHostname to an IPv4 address for an IPv4 client"); 181 throw new ArgumentException("[NetworkUtil] Unable to resolve defaultHostname to an IPv4 address for an IPv4 client");
182 } 182 }
183 183
184 static IPAddress externalIPAddress;
185
186 static NetworkUtil() 184 static NetworkUtil()
187 { 185 {
188 try 186 try
189 { 187 {
190 externalIPAddress = GetExternalIP();
191 }
192 catch { /* ignore */ }
193
194 try
195 {
196 foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces()) 188 foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
197 { 189 {
198 foreach (UnicastIPAddressInformation address in ni.GetIPProperties().UnicastAddresses) 190 foreach (UnicastIPAddressInformation address in ni.GetIPProperties().UnicastAddresses)
@@ -254,79 +246,5 @@ namespace OpenSim.Framework
254 return defaultHostname; 246 return defaultHostname;
255 } 247 }
256 248
257 public static IPAddress GetExternalIPOf(IPAddress user)
258 {
259 if (externalIPAddress == null)
260 return user;
261
262 if (user.ToString() == "127.0.0.1")
263 {
264 m_log.Info("[NetworkUtil] 127.0.0.1 user detected, sending '" + externalIPAddress + "' instead of '" + user + "'");
265 return externalIPAddress;
266 }
267 // Check if we're accessing localhost.
268 foreach (IPAddress host in Dns.GetHostAddresses(Dns.GetHostName()))
269 {
270 if (host.Equals(user) && host.AddressFamily == AddressFamily.InterNetwork)
271 {
272 m_log.Info("[NetworkUtil] Localhost user detected, sending '" + externalIPAddress + "' instead of '" + user + "'");
273 return externalIPAddress;
274 }
275 }
276
277 // Check for same LAN segment
278 foreach (KeyValuePair<IPAddress, IPAddress> subnet in m_subnets)
279 {
280 byte[] subnetBytes = subnet.Value.GetAddressBytes();
281 byte[] localBytes = subnet.Key.GetAddressBytes();
282 byte[] destBytes = user.GetAddressBytes();
283
284 if (subnetBytes.Length != destBytes.Length || subnetBytes.Length != localBytes.Length)
285 return user;
286
287 bool valid = true;
288
289 for (int i = 0; i < subnetBytes.Length; i++)
290 {
291 if ((localBytes[i] & subnetBytes[i]) != (destBytes[i] & subnetBytes[i]))
292 {
293 valid = false;
294 break;
295 }
296 }
297
298 if (subnet.Key.AddressFamily != AddressFamily.InterNetwork)
299 valid = false;
300
301 if (valid)
302 {
303 m_log.Info("[NetworkUtil] Local LAN user detected, sending '" + externalIPAddress + "' instead of '" + user + "'");
304 return externalIPAddress;
305 }
306 }
307
308 // Otherwise, return user address
309 return user;
310 }
311
312 private static IPAddress GetExternalIP()
313 {
314 string whatIsMyIp = "http://www.whatismyip.com/automation/n09230945.asp";
315 WebClient wc = new WebClient();
316 UTF8Encoding utf8 = new UTF8Encoding();
317 string requestHtml = "";
318 try
319 {
320 requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp));
321 }
322 catch (WebException we)
323 {
324 m_log.Info("[NetworkUtil]: Exception in GetExternalIP: " + we.ToString());
325 return null;
326 }
327
328 IPAddress externalIp = IPAddress.Parse(requestHtml);
329 return externalIp;
330 }
331 } 249 }
332} 250}