PageRenderTime 30ms CodeModel.GetById 16ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmessage/llhost.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 178 lines | 121 code | 24 blank | 33 comment | 10 complexity | 51844895b2ecbdb319091896e99121fb MD5 | raw file
  1/** 
  2 * @file llhost.cpp
  3 * @brief Encapsulates an IP address and a port.
  4 *
  5 * $LicenseInfo:firstyear=2000&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#include "linden_common.h"
 28
 29#include "llhost.h"
 30
 31#include "llerror.h"
 32
 33#if LL_WINDOWS
 34	#define WIN32_LEAN_AND_MEAN
 35	#include <winsock2.h>
 36#else
 37	#include <netdb.h>
 38	#include <netinet/in.h>	// ntonl()
 39	#include <sys/types.h>
 40	#include <sys/socket.h>
 41	#include <arpa/inet.h>
 42#endif
 43
 44LLHost LLHost::invalid(INVALID_PORT,INVALID_HOST_IP_ADDRESS);
 45
 46LLHost::LLHost(const std::string& ip_and_port)
 47{
 48	std::string::size_type colon_index = ip_and_port.find(":");
 49	if (colon_index == std::string::npos)
 50	{
 51		mIP = ip_string_to_u32(ip_and_port.c_str());
 52		mPort = 0;
 53	}
 54	else
 55	{
 56		std::string ip_str(ip_and_port, 0, colon_index);
 57		std::string port_str(ip_and_port, colon_index+1);
 58
 59		mIP = ip_string_to_u32(ip_str.c_str());
 60		mPort = atol(port_str.c_str());
 61	}
 62}
 63
 64std::string LLHost::getString() const
 65{
 66	return llformat("%s:%u", u32_to_ip_string(mIP), mPort);
 67}
 68
 69
 70std::string LLHost::getIPandPort() const
 71{
 72	return getString();
 73}
 74
 75
 76std::string LLHost::getIPString() const
 77{
 78	return std::string( u32_to_ip_string( mIP ) );
 79}
 80
 81
 82std::string LLHost::getHostName() const
 83{
 84	hostent* he;
 85	if (INVALID_HOST_IP_ADDRESS == mIP)
 86	{
 87		llwarns << "LLHost::getHostName() : Invalid IP address" << llendl;
 88		return std::string();
 89	}
 90	he = gethostbyaddr((char *)&mIP, sizeof(mIP), AF_INET);
 91	if (!he)
 92	{
 93#if LL_WINDOWS
 94		llwarns << "LLHost::getHostName() : Couldn't find host name for address " << mIP << ", Error: " 
 95			<< WSAGetLastError() << llendl;
 96#else
 97		llwarns << "LLHost::getHostName() : Couldn't find host name for address " << mIP << ", Error: " 
 98			<< h_errno << llendl;
 99#endif
100		return std::string();
101	}
102	else
103	{
104		return ll_safe_string(he->h_name);
105	}
106}
107
108BOOL LLHost::setHostByName(const std::string& hostname)
109{
110	hostent *he;
111	std::string local_name(hostname);
112
113#if LL_WINDOWS
114	// We may need an equivalent for Linux, but not sure - djs
115	LLStringUtil::toUpper(local_name);
116#endif
117
118	he = gethostbyname(local_name.c_str());	
119	if(!he) 
120	{
121		U32 ip_address = ip_string_to_u32(hostname.c_str());
122		he = gethostbyaddr((char *)&ip_address, sizeof(ip_address), AF_INET);
123	}
124
125	if (he)
126	{
127		mIP = *(U32 *)he->h_addr_list[0];
128		return TRUE;
129	}
130	else 
131	{
132		setAddress(local_name);
133
134		// In windows, h_errno is a macro for WSAGetLastError(), so store value here
135		S32 error_number = h_errno;
136		switch(error_number) 
137		{
138			case TRY_AGAIN:	// XXX how to handle this case? 
139				llwarns << "LLHost::setAddress(): try again" << llendl;
140				break;
141			case HOST_NOT_FOUND:
142			case NO_ADDRESS:	// NO_DATA
143				llwarns << "LLHost::setAddress(): host not found" << llendl;
144				break;
145			case NO_RECOVERY:
146				llwarns << "LLHost::setAddress(): unrecoverable error" << llendl;
147				break;
148			default:
149				llwarns << "LLHost::setAddress(): unknown error - " << error_number << llendl;
150				break;
151		}
152		return FALSE;
153	}
154}
155
156LLHost&	LLHost::operator=(const LLHost &rhs)
157{ 
158	if (this != &rhs)
159	{
160		set(rhs.getAddress(), rhs.getPort());
161	}
162	return *this;		
163}
164
165
166std::ostream& operator<< (std::ostream& os, const LLHost &hh)
167{
168	os << u32_to_ip_string(hh.mIP) << ":" << hh.mPort ;
169	return os;
170}
171
172
173//std::istream& operator>> (std::istream& is, LLHost &rh)
174//{
175//	is >> rh.mIP;
176//    is >> rh.mPort;
177//    return is;
178//}