diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Framework/Util.cs | 184 |
1 files changed, 109 insertions, 75 deletions
diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index 83d9df1..f52a84c 100644 --- a/OpenSim/Framework/Util.cs +++ b/OpenSim/Framework/Util.cs | |||
@@ -429,64 +429,6 @@ namespace OpenSim.Framework | |||
429 | return regionCoord << 8; | 429 | return regionCoord << 8; |
430 | } | 430 | } |
431 | 431 | ||
432 | public static IPEndPoint getEndPoint(IPAddress ia, int port) | ||
433 | { | ||
434 | if(ia == null) | ||
435 | return null; | ||
436 | |||
437 | IPEndPoint newEP = null; | ||
438 | try | ||
439 | { | ||
440 | newEP = new IPEndPoint(ia, port); | ||
441 | } | ||
442 | catch | ||
443 | { | ||
444 | newEP = null; | ||
445 | } | ||
446 | return newEP; | ||
447 | } | ||
448 | |||
449 | public static IPEndPoint getEndPoint(string hostname, int port) | ||
450 | { | ||
451 | IPAddress ia = null; | ||
452 | // If it is already an IP, don't resolve it - just return directly | ||
453 | // we should not need this | ||
454 | if (IPAddress.TryParse(hostname, out ia)) | ||
455 | { | ||
456 | if (ia.Equals(IPAddress.Any) || ia.Equals(IPAddress.IPv6Any)) | ||
457 | return null; | ||
458 | return getEndPoint(ia, port); | ||
459 | } | ||
460 | |||
461 | // Reset for next check | ||
462 | ia = null; | ||
463 | try | ||
464 | { | ||
465 | foreach (IPAddress Adr in Dns.GetHostAddresses(hostname)) | ||
466 | { | ||
467 | if (ia == null) | ||
468 | ia = Adr; | ||
469 | |||
470 | if (Adr.AddressFamily == AddressFamily.InterNetwork) | ||
471 | { | ||
472 | ia = Adr; | ||
473 | break; | ||
474 | } | ||
475 | } | ||
476 | } | ||
477 | catch // (SocketException e) | ||
478 | { | ||
479 | /*throw new Exception( | ||
480 | "Unable to resolve local hostname " + m_externalHostName + " innerException of type '" + | ||
481 | e + "' attached to this exception", e);*/ | ||
482 | // Don't throw a fatal exception here, instead, return Null and handle it in the caller. | ||
483 | // Reason is, on systems such as OSgrid it has occured that known hostnames stop | ||
484 | // resolving and thus make surrounding regions crash out with this exception. | ||
485 | return null; | ||
486 | } | ||
487 | |||
488 | return getEndPoint(ia,port); | ||
489 | } | ||
490 | 432 | ||
491 | public static bool checkServiceURI(string uristr, out string serviceURI) | 433 | public static bool checkServiceURI(string uristr, out string serviceURI) |
492 | { | 434 | { |
@@ -1049,6 +991,8 @@ namespace OpenSim.Framework | |||
1049 | return output.ToString(); | 991 | return output.ToString(); |
1050 | } | 992 | } |
1051 | 993 | ||
994 | private static ExpiringCache<string,IPAddress> dnscache = new ExpiringCache<string, IPAddress>(); | ||
995 | |||
1052 | /// <summary> | 996 | /// <summary> |
1053 | /// Converts a URL to a IPAddress | 997 | /// Converts a URL to a IPAddress |
1054 | /// </summary> | 998 | /// </summary> |
@@ -1066,38 +1010,128 @@ namespace OpenSim.Framework | |||
1066 | /// <returns>An IP address, or null</returns> | 1010 | /// <returns>An IP address, or null</returns> |
1067 | public static IPAddress GetHostFromDNS(string dnsAddress) | 1011 | public static IPAddress GetHostFromDNS(string dnsAddress) |
1068 | { | 1012 | { |
1069 | // Is it already a valid IP? No need to look it up. | 1013 | if(String.IsNullOrWhiteSpace(dnsAddress)) |
1070 | IPAddress ipa; | 1014 | return null; |
1071 | if (IPAddress.TryParse(dnsAddress, out ipa)) | ||
1072 | return ipa; | ||
1073 | 1015 | ||
1074 | IPAddress[] hosts = null; | 1016 | IPAddress ia = null; |
1017 | if(dnscache.TryGetValue(dnsAddress, out ia) && ia != null) | ||
1018 | { | ||
1019 | dnscache.AddOrUpdate(dnsAddress, ia, 300); | ||
1020 | return ia; | ||
1021 | } | ||
1022 | |||
1023 | ia = null; | ||
1024 | // If it is already an IP, don't let GetHostEntry see it | ||
1025 | if (IPAddress.TryParse(dnsAddress, out ia) && ia != null) | ||
1026 | { | ||
1027 | if (ia.Equals(IPAddress.Any) || ia.Equals(IPAddress.IPv6Any)) | ||
1028 | return null; | ||
1029 | dnscache.AddOrUpdate(dnsAddress, ia, 300); | ||
1030 | return ia; | ||
1031 | } | ||
1075 | 1032 | ||
1076 | // Not an IP, lookup required | 1033 | IPHostEntry IPH; |
1077 | try | 1034 | try |
1078 | { | 1035 | { |
1079 | hosts = Dns.GetHostEntry(dnsAddress).AddressList; | 1036 | IPH = Dns.GetHostEntry(dnsAddress); |
1080 | } | 1037 | } |
1081 | catch (Exception e) | 1038 | catch // (SocketException e) |
1082 | { | 1039 | { |
1083 | m_log.WarnFormat("[UTIL]: An error occurred while resolving host name {0}, {1}", dnsAddress, e); | 1040 | return null; |
1041 | } | ||
1042 | |||
1043 | if(IPH == null || IPH.AddressList.Length == 0) | ||
1044 | return null; | ||
1084 | 1045 | ||
1085 | // Still going to throw the exception on for now, since this was what was happening in the first place | 1046 | ia = null; |
1086 | throw e; | 1047 | foreach (IPAddress Adr in IPH.AddressList) |
1048 | { | ||
1049 | if (ia == null) | ||
1050 | ia = Adr; | ||
1051 | |||
1052 | if (Adr.AddressFamily == AddressFamily.InterNetwork) | ||
1053 | { | ||
1054 | ia = Adr; | ||
1055 | break; | ||
1056 | } | ||
1057 | } | ||
1058 | if(ia != null) | ||
1059 | dnscache.AddOrUpdate(dnsAddress, ia, 300); | ||
1060 | return ia; | ||
1061 | } | ||
1062 | |||
1063 | public static IPEndPoint getEndPoint(IPAddress ia, int port) | ||
1064 | { | ||
1065 | if(ia == null) | ||
1066 | return null; | ||
1067 | |||
1068 | IPEndPoint newEP = null; | ||
1069 | try | ||
1070 | { | ||
1071 | newEP = new IPEndPoint(ia, port); | ||
1072 | } | ||
1073 | catch | ||
1074 | { | ||
1075 | newEP = null; | ||
1076 | } | ||
1077 | return newEP; | ||
1078 | } | ||
1079 | |||
1080 | public static IPEndPoint getEndPoint(string hostname, int port) | ||
1081 | { | ||
1082 | if(String.IsNullOrWhiteSpace(hostname)) | ||
1083 | return null; | ||
1084 | |||
1085 | IPAddress ia = null; | ||
1086 | if(dnscache.TryGetValue(hostname, out ia) && ia != null) | ||
1087 | { | ||
1088 | dnscache.AddOrUpdate(hostname, ia, 300); | ||
1089 | return getEndPoint(ia, port); | ||
1087 | } | 1090 | } |
1088 | 1091 | ||
1089 | foreach (IPAddress host in hosts) | 1092 | ia = null; |
1093 | |||
1094 | // If it is already an IP, don't let GetHostEntry see it | ||
1095 | if (IPAddress.TryParse(hostname, out ia) && ia != null) | ||
1090 | { | 1096 | { |
1091 | if (host.AddressFamily == AddressFamily.InterNetwork) | 1097 | if (ia.Equals(IPAddress.Any) || ia.Equals(IPAddress.IPv6Any)) |
1098 | return null; | ||
1099 | |||
1100 | dnscache.AddOrUpdate(hostname, ia, 300); | ||
1101 | return getEndPoint(ia, port); | ||
1102 | } | ||
1103 | |||
1104 | |||
1105 | IPHostEntry IPH; | ||
1106 | try | ||
1107 | { | ||
1108 | IPH = Dns.GetHostEntry(hostname); | ||
1109 | } | ||
1110 | catch // (SocketException e) | ||
1111 | { | ||
1112 | return null; | ||
1113 | } | ||
1114 | |||
1115 | if(IPH == null || IPH.AddressList.Length == 0) | ||
1116 | return null; | ||
1117 | |||
1118 | ia = null; | ||
1119 | foreach (IPAddress Adr in IPH.AddressList) | ||
1120 | { | ||
1121 | if (ia == null) | ||
1122 | ia = Adr; | ||
1123 | |||
1124 | if (Adr.AddressFamily == AddressFamily.InterNetwork) | ||
1092 | { | 1125 | { |
1093 | return host; | 1126 | ia = Adr; |
1127 | break; | ||
1094 | } | 1128 | } |
1095 | } | 1129 | } |
1096 | 1130 | ||
1097 | if (hosts.Length > 0) | 1131 | if(ia != null) |
1098 | return hosts[0]; | 1132 | dnscache.AddOrUpdate(hostname, ia, 300); |
1099 | 1133 | ||
1100 | return null; | 1134 | return getEndPoint(ia,port); |
1101 | } | 1135 | } |
1102 | 1136 | ||
1103 | public static Uri GetURI(string protocol, string hostname, int port, string path) | 1137 | public static Uri GetURI(string protocol, string hostname, int port, string path) |