aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llmessage/llhost.cpp
diff options
context:
space:
mode:
authorJacek Antonelli2008-08-15 23:44:46 -0500
committerJacek Antonelli2008-08-15 23:44:46 -0500
commit38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch)
treeadca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/llmessage/llhost.cpp
parentREADME.txt (diff)
downloadmeta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.zip
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.gz
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.bz2
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.xz
Second Life viewer sources 1.13.2.12
Diffstat (limited to '')
-rw-r--r--linden/indra/llmessage/llhost.cpp237
1 files changed, 237 insertions, 0 deletions
diff --git a/linden/indra/llmessage/llhost.cpp b/linden/indra/llmessage/llhost.cpp
new file mode 100644
index 0000000..c071063
--- /dev/null
+++ b/linden/indra/llmessage/llhost.cpp
@@ -0,0 +1,237 @@
1/**
2 * @file llhost.cpp
3 * @brief Encapsulates an IP address and a port.
4 *
5 * Copyright (c) 2000-2007, Linden Research, Inc.
6 *
7 * The source code in this file ("Source Code") is provided by Linden Lab
8 * to you under the terms of the GNU General Public License, version 2.0
9 * ("GPL"), unless you have obtained a separate licensing agreement
10 * ("Other License"), formally executed by you and Linden Lab. Terms of
11 * the GPL can be found in doc/GPL-license.txt in this distribution, or
12 * online at http://secondlife.com/developers/opensource/gplv2
13 *
14 * There are special exceptions to the terms and conditions of the GPL as
15 * it is applied to this Source Code. View the full text of the exception
16 * in the file doc/FLOSS-exception.txt in this software distribution, or
17 * online at http://secondlife.com/developers/opensource/flossexception
18 *
19 * By copying, modifying or distributing this software, you acknowledge
20 * that you have read and understood your obligations described above,
21 * and agree to abide by those obligations.
22 *
23 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
24 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
25 * COMPLETENESS OR PERFORMANCE.
26 */
27
28#include "linden_common.h"
29
30#include "llhost.h"
31
32#include "llerror.h"
33
34#if LL_WINDOWS
35 #define WIN32_LEAN_AND_MEAN
36 #include <winsock2.h>
37#else
38 #include <netdb.h>
39 #include <netinet/in.h> // ntonl()
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <arpa/inet.h>
43#endif
44
45LLHost LLHost::invalid(INVALID_PORT,INVALID_HOST_IP_ADDRESS);
46
47LLHost::LLHost(const std::string& ip_and_port)
48{
49 std::string::size_type colon_index = ip_and_port.find(":");
50 if (colon_index != std::string::npos)
51 {
52 mIP = ip_string_to_u32(ip_and_port.c_str());
53 mPort = 0;
54 }
55 else
56 {
57 std::string ip_str(ip_and_port, 0, colon_index);
58 std::string port_str(ip_and_port, colon_index+1);
59
60 mIP = ip_string_to_u32(ip_str.c_str());
61 mPort = atol(port_str.c_str());
62 }
63}
64
65void LLHost::getString(char* buffer, S32 length) const
66{
67 if (((U32) length) < MAXADDRSTR + 1 + 5)
68 {
69 llerrs << "LLHost::getString - string too short" << llendl;
70 return;
71 }
72
73 snprintf(buffer, length, "%s:%u", u32_to_ip_string(mIP), mPort); /*Flawfinder: ignore*/
74}
75
76void LLHost::getIPString(char* buffer, S32 length) const
77{
78 if ( ((U32) length) < MAXADDRSTR)
79 {
80 llerrs << "LLHost::getIPString - string too short" << llendl;
81 return;
82 }
83
84 snprintf(buffer, length, "%s", u32_to_ip_string(mIP)); /*Flawfinder: ignore*/
85}
86
87
88std::string LLHost::getIPandPort() const
89{
90 char buffer[MAXADDRSTR + 1 + 5];
91 getString(buffer, sizeof(buffer));
92 return buffer;
93}
94
95
96std::string LLHost::getIPString() const
97{
98 return std::string( u32_to_ip_string( mIP ) );
99}
100
101
102void LLHost::getHostName(char *buf, S32 len) const
103{
104 hostent *he;
105
106 if (INVALID_HOST_IP_ADDRESS == mIP)
107 {
108 llwarns << "LLHost::getHostName() : Invalid IP address" << llendl;
109 buf[0] = '\0';
110 return;
111 }
112 he = gethostbyaddr((char *)&mIP, sizeof(mIP), AF_INET);
113 if (!he)
114 {
115#if LL_WINDOWS
116 llwarns << "LLHost::getHostName() : Couldn't find host name for address " << mIP << ", Error: "
117 << WSAGetLastError() << llendl;
118#else
119 llwarns << "LLHost::getHostName() : Couldn't find host name for address " << mIP << ", Error: "
120 << h_errno << llendl;
121#endif
122 buf[0] = '\0';
123 }
124 else
125 {
126 strncpy(buf, he->h_name, len); /*Flawfinder: ignore*/
127 buf[len-1] = '\0';
128 }
129}
130
131LLString LLHost::getHostName() const
132{
133 hostent *he;
134
135 if (INVALID_HOST_IP_ADDRESS == mIP)
136 {
137 llwarns << "LLHost::getHostName() : Invalid IP address" << llendl;
138 return "";
139 }
140 he = gethostbyaddr((char *)&mIP, sizeof(mIP), AF_INET);
141 if (!he)
142 {
143#if LL_WINDOWS
144 llwarns << "LLHost::getHostName() : Couldn't find host name for address " << mIP << ", Error: "
145 << WSAGetLastError() << llendl;
146#else
147 llwarns << "LLHost::getHostName() : Couldn't find host name for address " << mIP << ", Error: "
148 << h_errno << llendl;
149#endif
150 return "";
151 }
152 else
153 {
154 LLString hostname = he->h_name;
155 return hostname;
156 }
157}
158
159BOOL LLHost::setHostByName(const char *string)
160{
161 hostent *he;
162 char local_name[MAX_STRING]; /*Flawfinder: ignore*/
163
164 if (strlen(string)+1 > MAX_STRING) /*Flawfinder: ignore*/
165 {
166 llerrs << "LLHost::setHostByName() : Address string is too long: "
167 << string << llendl;
168 }
169
170 strncpy(local_name, string,MAX_STRING); /*Flawfinder: ignore*/
171 local_name[MAX_STRING-1] = '\0';
172#if LL_WINDOWS
173 // We may need an equivalent for Linux, but not sure - djs
174 _strupr(local_name);
175#endif
176
177 he = gethostbyname(local_name);
178 if(!he)
179 {
180 U32 ip_address = inet_addr(string);
181 he = gethostbyaddr((char *)&ip_address, sizeof(ip_address), AF_INET);
182 }
183
184 if (he)
185 {
186 mIP = *(U32 *)he->h_addr_list[0];
187 return TRUE;
188 }
189 else
190 {
191 setAddress(local_name);
192
193 // In windows, h_errno is a macro for WSAGetLastError(), so store value here
194 S32 error_number = h_errno;
195 switch(error_number)
196 {
197 case TRY_AGAIN: // XXX how to handle this case?
198 llwarns << "LLHost::setAddress(): try again" << llendl;
199 break;
200 case HOST_NOT_FOUND:
201 case NO_ADDRESS: // NO_DATA
202 llwarns << "LLHost::setAddress(): host not found" << llendl;
203 break;
204 case NO_RECOVERY:
205 llwarns << "LLHost::setAddress(): unrecoverable error" << llendl;
206 break;
207 default:
208 llwarns << "LLHost::setAddress(): unknown error - " << error_number << llendl;
209 break;
210 }
211 return FALSE;
212 }
213}
214
215LLHost& LLHost::operator=(const LLHost &rhs)
216{
217 if (this != &rhs)
218 {
219 set(rhs.getAddress(), rhs.getPort());
220 }
221 return *this;
222}
223
224
225std::ostream& operator<< (std::ostream& os, const LLHost &hh)
226{
227 os << u32_to_ip_string(hh.mIP) << ":" << hh.mPort ;
228 return os;
229}
230
231
232std::istream& operator>> (std::istream& is, LLHost &rh)
233{
234 is >> rh.mIP;
235 is >> rh.mPort;
236 return is;
237}