From 70f6a2852eaea68bad0d26e5a5c1c7d8df3d8942 Mon Sep 17 00:00:00 2001 From: Adam Frisby Date: Sat, 23 May 2009 07:51:29 +0000 Subject: * Fixes [irritating] edge case in Util.GetLocalHost which could return an IPv6 address if no non-loopback IPv4 address can be found. * Restores internal IPv6 support to NetworkUtil.* * Fixes bad login unit tests. --- .../Communications/Tests/LoginServiceTests.cs | 45 ++++++++++++++++------ OpenSim/Framework/NetworkUtil.cs | 9 +++-- OpenSim/Framework/Util.cs | 8 ++++ 3 files changed, 47 insertions(+), 15 deletions(-) (limited to 'OpenSim') diff --git a/OpenSim/Framework/Communications/Tests/LoginServiceTests.cs b/OpenSim/Framework/Communications/Tests/LoginServiceTests.cs index a2a388e..7fda38c 100644 --- a/OpenSim/Framework/Communications/Tests/LoginServiceTests.cs +++ b/OpenSim/Framework/Communications/Tests/LoginServiceTests.cs @@ -103,7 +103,10 @@ namespace OpenSim.Framework.Communications.Tests XmlRpcRequest request = new XmlRpcRequest("login_to_simulator", sendParams); - XmlRpcResponse response = loginService.XmlRpcLoginMethod(request, new IPEndPoint(Util.GetLocalHost(), 80)); + IPAddress tmpLocal = Util.GetLocalHost(); + IPEndPoint tmpEnd = new IPEndPoint(tmpLocal, 80); + XmlRpcResponse response = m_loginService.XmlRpcLoginMethod(request, tmpEnd); + Hashtable responseData = (Hashtable)response.Value; Assert.That(responseData["first_name"], Is.EqualTo(m_firstName)); @@ -113,7 +116,7 @@ namespace OpenSim.Framework.Communications.Tests Regex capsSeedPattern = new Regex("^http://" - + m_regionExternalName + + NetworkUtil.GetHostFor(tmpLocal, m_regionExternalName) + ":9000/CAPS/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}0000/$"); Assert.That(capsSeedPattern.IsMatch((string)responseData["seed_capability"]), Is.True); @@ -140,7 +143,10 @@ namespace OpenSim.Framework.Communications.Tests XmlRpcRequest request = new XmlRpcRequest("login_to_simulator", sendParams); - XmlRpcResponse response = m_loginService.XmlRpcLoginMethod(request, new IPEndPoint(Util.GetLocalHost(), 80)); + IPAddress tmpLocal = Util.GetLocalHost(); + IPEndPoint tmpEnd = new IPEndPoint(tmpLocal, 80); + XmlRpcResponse response = m_loginService.XmlRpcLoginMethod(request, tmpEnd); + Hashtable responseData = (Hashtable)response.Value; UserAgentData uagent = m_userProfileData.CurrentAgent; @@ -163,7 +169,7 @@ namespace OpenSim.Framework.Communications.Tests Regex capsSeedPattern = new Regex("^http://" - + m_regionExternalName + + NetworkUtil.GetHostFor(tmpLocal, m_regionExternalName) + ":9000/CAPS/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}0000/$"); Assert.That(capsSeedPattern.IsMatch((string)responseData["seed_capability"]), Is.True); @@ -194,14 +200,16 @@ namespace OpenSim.Framework.Communications.Tests XmlRpcRequest request = new XmlRpcRequest("login_to_simulator", sendParams); - XmlRpcResponse response = m_loginService.XmlRpcLoginMethod(request, new IPEndPoint(Util.GetLocalHost(), 80)); + IPAddress tmpLocal = Util.GetLocalHost(); + IPEndPoint tmpEnd = new IPEndPoint(tmpLocal, 80); + XmlRpcResponse response = m_loginService.XmlRpcLoginMethod(request, tmpEnd); + Hashtable responseData = (Hashtable)response.Value; ArrayList friendslist = (ArrayList) responseData["buddy-list"]; Assert.That(friendslist,Is.Not.Null); - Hashtable buddy1 = (Hashtable) friendslist[0]; Hashtable buddy2 = (Hashtable) friendslist[1]; Assert.That(friendslist.Count, Is.EqualTo(2)); @@ -231,7 +239,10 @@ namespace OpenSim.Framework.Communications.Tests XmlRpcRequest request = new XmlRpcRequest("login_to_simulator", sendParams); - XmlRpcResponse response = m_loginService.XmlRpcLoginMethod(request, new IPEndPoint(Util.GetLocalHost(), 80)); + IPAddress tmpLocal = Util.GetLocalHost(); + IPEndPoint tmpEnd = new IPEndPoint(tmpLocal, 80); + XmlRpcResponse response = m_loginService.XmlRpcLoginMethod(request, tmpEnd); + Hashtable responseData = (Hashtable)response.Value; Assert.That(responseData["message"], Is.EqualTo(error_auth_message)); @@ -256,7 +267,10 @@ namespace OpenSim.Framework.Communications.Tests XmlRpcRequest request = new XmlRpcRequest("login_to_simulator", sendParams); - XmlRpcResponse response = m_loginService.XmlRpcLoginMethod(request, new IPEndPoint(Util.GetLocalHost(), 80)); + IPAddress tmpLocal = Util.GetLocalHost(); + IPEndPoint tmpEnd = new IPEndPoint(tmpLocal, 80); + XmlRpcResponse response = m_loginService.XmlRpcLoginMethod(request, tmpEnd); + Hashtable responseData = (Hashtable)response.Value; Assert.That(responseData["message"], Is.EqualTo(error_auth_message)); @@ -281,7 +295,10 @@ namespace OpenSim.Framework.Communications.Tests XmlRpcRequest request = new XmlRpcRequest("login_to_simulator", sendParams); - XmlRpcResponse response = m_loginService.XmlRpcLoginMethod(request, new IPEndPoint(Util.GetLocalHost(), 80)); + IPAddress tmpLocal = Util.GetLocalHost(); + IPEndPoint tmpEnd = new IPEndPoint(tmpLocal, 80); + XmlRpcResponse response = m_loginService.XmlRpcLoginMethod(request, tmpEnd); + Hashtable responseData = (Hashtable)response.Value; Assert.That(responseData["message"], Is.EqualTo(error_xml_message)); @@ -312,20 +329,24 @@ namespace OpenSim.Framework.Communications.Tests // First we log in. XmlRpcRequest request = new XmlRpcRequest("login_to_simulator", sendParams); - XmlRpcResponse response = m_loginService.XmlRpcLoginMethod(request, new IPEndPoint(Util.GetLocalHost(), 80)); + + IPAddress tmpLocal = Util.GetLocalHost(); + IPEndPoint tmpEnd = new IPEndPoint(tmpLocal, 80); + XmlRpcResponse response = m_loginService.XmlRpcLoginMethod(request, tmpEnd); + Hashtable responseData = (Hashtable)response.Value; Assert.That(responseData["message"], Is.EqualTo("Hello folks")); // Then we try again, this time expecting failure. request = new XmlRpcRequest("login_to_simulator", sendParams); - response = m_loginService.XmlRpcLoginMethod(request, new IPEndPoint(Util.GetLocalHost(), 80)); + response = m_loginService.XmlRpcLoginMethod(request, tmpEnd); responseData = (Hashtable)response.Value; Assert.That(responseData["message"], Is.EqualTo(error_already_logged)); // Finally the third time we should be able to get right back in. request = new XmlRpcRequest("login_to_simulator", sendParams); - response = m_loginService.XmlRpcLoginMethod(request, new IPEndPoint(Util.GetLocalHost(), 80)); + response = m_loginService.XmlRpcLoginMethod(request, tmpEnd); responseData = (Hashtable)response.Value; Assert.That(responseData["message"], Is.EqualTo("Hello folks")); diff --git a/OpenSim/Framework/NetworkUtil.cs b/OpenSim/Framework/NetworkUtil.cs index d4fc1e2..328d3bc 100644 --- a/OpenSim/Framework/NetworkUtil.cs +++ b/OpenSim/Framework/NetworkUtil.cs @@ -73,9 +73,8 @@ namespace OpenSim.Framework private static IPAddress GetExternalIPFor(IPAddress destination, string defaultHostname) { - bool ipv6 = false; // Adds IPv6 Support (Not that any of the major protocols supports it...) - if (ipv6 && destination.AddressFamily == AddressFamily.InterNetworkV6) + if (destination.AddressFamily == AddressFamily.InterNetworkV6) { foreach (IPAddress host in Dns.GetHostAddresses(defaultHostname)) { @@ -91,10 +90,14 @@ namespace OpenSim.Framework return null; // Check if we're accessing localhost. - foreach (IPAddress host in Dns.GetHostAddresses(Dns.GetHostName())) + foreach (KeyValuePair pair in m_subnets) { + IPAddress host = pair.Value; if (host.Equals(destination) && host.AddressFamily == AddressFamily.InterNetwork) + { + m_log.Info("[NATROUTING] Localhost user detected, sending them '" + host + "' instead of '" + defaultHostname + "'"); return destination; + } } // Check for same LAN segment diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index 0128735..ee47e0f 100644 --- a/OpenSim/Framework/Util.cs +++ b/OpenSim/Framework/Util.cs @@ -546,7 +546,15 @@ namespace OpenSim.Framework } if (hosts.Length > 0) + { + foreach (IPAddress host in hosts) + { + if(host.AddressFamily == AddressFamily.InterNetwork) + return host; + } + // Well all else failed... return hosts[0]; + } return null; } -- cgit v1.1