PageRenderTime 189ms CodeModel.GetById 0ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1
  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. #include "linden_common.h"
  27. #include "llhost.h"
  28. #include "llerror.h"
  29. #if LL_WINDOWS
  30. #define WIN32_LEAN_AND_MEAN
  31. #include <winsock2.h>
  32. #else
  33. #include <netdb.h>
  34. #include <netinet/in.h> // ntonl()
  35. #include <sys/types.h>
  36. #include <sys/socket.h>
  37. #include <arpa/inet.h>
  38. #endif
  39. LLHost LLHost::invalid(INVALID_PORT,INVALID_HOST_IP_ADDRESS);
  40. LLHost::LLHost(const std::string& ip_and_port)
  41. {
  42. std::string::size_type colon_index = ip_and_port.find(":");
  43. if (colon_index == std::string::npos)
  44. {
  45. mIP = ip_string_to_u32(ip_and_port.c_str());
  46. mPort = 0;
  47. }
  48. else
  49. {
  50. std::string ip_str(ip_and_port, 0, colon_index);
  51. std::string port_str(ip_and_port, colon_index+1);
  52. mIP = ip_string_to_u32(ip_str.c_str());
  53. mPort = atol(port_str.c_str());
  54. }
  55. }
  56. std::string LLHost::getString() const
  57. {
  58. return llformat("%s:%u", u32_to_ip_string(mIP), mPort);
  59. }
  60. std::string LLHost::getIPandPort() const
  61. {
  62. return getString();
  63. }
  64. std::string LLHost::getIPString() const
  65. {
  66. return std::string( u32_to_ip_string( mIP ) );
  67. }
  68. std::string LLHost::getHostName() const
  69. {
  70. hostent* he;
  71. if (INVALID_HOST_IP_ADDRESS == mIP)
  72. {
  73. llwarns << "LLHost::getHostName() : Invalid IP address" << llendl;
  74. return std::string();
  75. }
  76. he = gethostbyaddr((char *)&mIP, sizeof(mIP), AF_INET);
  77. if (!he)
  78. {
  79. #if LL_WINDOWS
  80. llwarns << "LLHost::getHostName() : Couldn't find host name for address " << mIP << ", Error: "
  81. << WSAGetLastError() << llendl;
  82. #else
  83. llwarns << "LLHost::getHostName() : Couldn't find host name for address " << mIP << ", Error: "
  84. << h_errno << llendl;
  85. #endif
  86. return std::string();
  87. }
  88. else
  89. {
  90. return ll_safe_string(he->h_name);
  91. }
  92. }
  93. BOOL LLHost::setHostByName(const std::string& hostname)
  94. {
  95. hostent *he;
  96. std::string local_name(hostname);
  97. #if LL_WINDOWS
  98. // We may need an equivalent for Linux, but not sure - djs
  99. LLStringUtil::toUpper(local_name);
  100. #endif
  101. he = gethostbyname(local_name.c_str());
  102. if(!he)
  103. {
  104. U32 ip_address = ip_string_to_u32(hostname.c_str());
  105. he = gethostbyaddr((char *)&ip_address, sizeof(ip_address), AF_INET);
  106. }
  107. if (he)
  108. {
  109. mIP = *(U32 *)he->h_addr_list[0];
  110. return TRUE;
  111. }
  112. else
  113. {
  114. setAddress(local_name);
  115. // In windows, h_errno is a macro for WSAGetLastError(), so store value here
  116. S32 error_number = h_errno;
  117. switch(error_number)
  118. {
  119. case TRY_AGAIN: // XXX how to handle this case?
  120. llwarns << "LLHost::setAddress(): try again" << llendl;
  121. break;
  122. case HOST_NOT_FOUND:
  123. case NO_ADDRESS: // NO_DATA
  124. llwarns << "LLHost::setAddress(): host not found" << llendl;
  125. break;
  126. case NO_RECOVERY:
  127. llwarns << "LLHost::setAddress(): unrecoverable error" << llendl;
  128. break;
  129. default:
  130. llwarns << "LLHost::setAddress(): unknown error - " << error_number << llendl;
  131. break;
  132. }
  133. return FALSE;
  134. }
  135. }
  136. LLHost& LLHost::operator=(const LLHost &rhs)
  137. {
  138. if (this != &rhs)
  139. {
  140. set(rhs.getAddress(), rhs.getPort());
  141. }
  142. return *this;
  143. }
  144. std::ostream& operator<< (std::ostream& os, const LLHost &hh)
  145. {
  146. os << u32_to_ip_string(hh.mIP) << ":" << hh.mPort ;
  147. return os;
  148. }
  149. //std::istream& operator>> (std::istream& is, LLHost &rh)
  150. //{
  151. // is >> rh.mIP;
  152. // is >> rh.mPort;
  153. // return is;
  154. //}