llhost.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * @file llhost.h
  3. * @brief a LLHost uniquely defines a host (Simulator, Proxy or other)
  4. * across the network
  5. *
  6. * $LicenseInfo:firstyear=2000&license=viewergpl$
  7. *
  8. * Copyright (c) 2000-2009, Linden Research, Inc.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #ifndef LL_LLHOST_H
  34. #define LL_LLHOST_H
  35. #include <iostream>
  36. #include <string>
  37. #include "llerror.h"
  38. ///////////////////////////////////////////////////////////////////////////////
  39. // This used to be in llnet.h header, but it is better kept here, since it is
  40. // OS-independent and used by modules using LLHost and not llnet.cpp cross-
  41. // platform functions which implementation is OS-dependent. HB
  42. // Useful MTU constants
  43. constexpr S32 ETHERNET_MTU_BYTES = 1500;
  44. constexpr S32 MTUBYTES = 1200;
  45. constexpr S32 MTUBITS = MTUBYTES * 8;
  46. constexpr S32 MTUU32S = MTUBITS / 32;
  47. // For automatic port discovery when running multiple viewers on one host
  48. constexpr U32 PORT_DISCOVERY_RANGE_MIN = 13000;
  49. constexpr U32 PORT_DISCOVERY_RANGE_MAX = PORT_DISCOVERY_RANGE_MIN + 50;
  50. #define NET_BUFFER_SIZE (0x2000)
  51. // Request a free local port from the operating system
  52. #define NET_USE_OS_ASSIGNED_PORT 0
  53. // 123.567.901.345 = 15 chars + \0 + 1 for good luck
  54. constexpr U32 MAXADDRSTR = 17;
  55. // Returns pointer to internal string buffer, "(bad IP addr)" on failure,
  56. // cannot nest calls
  57. const char* u32_to_ip_string(U32 ip);
  58. // NULL on failure, ip_string on success, you must allocate at least MAXADDRSTR
  59. // chars
  60. char* u32_to_ip_string(U32 ip, char* ip_string);
  61. // Wrapper for inet_addr()
  62. U32 ip_string_to_u32(const char* ip_string);
  63. extern const char* LOOPBACK_ADDRESS_STRING;
  64. extern const char* BROADCAST_ADDRESS_STRING;
  65. ///////////////////////////////////////////////////////////////////////////////
  66. constexpr U32 INVALID_PORT = 0;
  67. constexpr U32 INVALID_HOST_IP_ADDRESS = 0x0;
  68. class LLHost
  69. {
  70. protected:
  71. LOG_CLASS(LLHost);
  72. public:
  73. // CREATORS
  74. // STL's hash_map expect this T()
  75. LLHost()
  76. : mPort(INVALID_PORT),
  77. mIP(INVALID_HOST_IP_ADDRESS)
  78. {
  79. }
  80. LLHost(U32 ipv4_addr, U32 port)
  81. : mPort(port),
  82. mIP(ipv4_addr)
  83. {
  84. }
  85. LLHost(const std::string& ipv4_addr, U32 port)
  86. : mPort(port)
  87. {
  88. mIP = ip_string_to_u32(ipv4_addr.c_str());
  89. }
  90. explicit LLHost(U64 ip_port)
  91. {
  92. U32 ip = (U32)(ip_port >> 32);
  93. U32 port = (U32)(ip_port & (U64)0xFFFFFFFF);
  94. mIP = ip;
  95. mPort = port;
  96. }
  97. explicit LLHost(const std::string& ip_and_port);
  98. ~LLHost() {}
  99. // MANIPULATORS
  100. void set(U32 ip, U32 port) { mIP = ip; mPort = port; }
  101. void set(const std::string& ipstr, U32 port) { mIP = ip_string_to_u32(ipstr.c_str()); mPort = port; }
  102. void setAddress(const std::string& ipstr) { mIP = ip_string_to_u32(ipstr.c_str()); }
  103. void setAddress(U32 ip) { mIP = ip; }
  104. void setPort(U32 port) { mPort = port; }
  105. bool setHostByName(const std::string& hname);
  106. LLHost& operator=(const LLHost& rhs);
  107. void invalidate() { mIP = INVALID_HOST_IP_ADDRESS; mPort = INVALID_PORT;};
  108. // READERS
  109. LL_INLINE U32 getAddress() const { return mIP; }
  110. LL_INLINE U32 getPort() const { return mPort; }
  111. LL_INLINE bool isOk() const { return mIP != INVALID_HOST_IP_ADDRESS && mPort != INVALID_PORT; }
  112. LL_INLINE bool isInvalid() const { return mIP == INVALID_HOST_IP_ADDRESS || mPort == INVALID_PORT; }
  113. size_t hash() const { return (mIP << 16) | (mPort & 0xffff); }
  114. std::string getIPString() const;
  115. std::string getHostName() const;
  116. std::string getIPandPort() const;
  117. LL_INLINE std::string getUntrustedSimulatorCap() const
  118. {
  119. return mUntrustedSimCap;
  120. }
  121. LL_INLINE void setUntrustedSimulatorCap(const std::string& url)
  122. {
  123. mUntrustedSimCap = url;
  124. }
  125. friend std::ostream& operator<<(std::ostream& os, const LLHost& hh);
  126. // This operator is not well defined. does it expect a
  127. // "192.168.1.1:80" notation or "int int" format? Phoenix 2007-05-18
  128. //friend std::istream& operator>> (std::istream& is, LLHost& hh);
  129. friend LL_INLINE bool operator==(const LLHost& lhs, const LLHost& rhs);
  130. friend LL_INLINE bool operator!=(const LLHost& lhs, const LLHost& rhs);
  131. friend LL_INLINE bool operator<(const LLHost& lhs, const LLHost& rhs);
  132. public:
  133. static const LLHost invalid;
  134. protected:
  135. std::string mUntrustedSimCap;
  136. U32 mPort;
  137. U32 mIP;
  138. };
  139. // std::hash implementation for LLHost
  140. namespace std
  141. {
  142. template<> struct hash<LLHost>
  143. {
  144. LL_INLINE size_t operator()(const LLHost& host) const noexcept
  145. {
  146. return host.hash();
  147. }
  148. };
  149. }
  150. // For use with boost::unordered_map and boost::unordered_set
  151. LL_INLINE size_t hash_value(const LLHost& host) noexcept
  152. {
  153. return host.hash();
  154. }
  155. LL_INLINE bool operator==(const LLHost& lhs, const LLHost& rhs)
  156. {
  157. return lhs.mIP == rhs.mIP && lhs.mPort == rhs.mPort;
  158. }
  159. LL_INLINE bool operator!=(const LLHost& lhs, const LLHost& rhs)
  160. {
  161. return lhs.mIP != rhs.mIP || lhs.mPort != rhs.mPort;
  162. }
  163. LL_INLINE bool operator<(const LLHost& lhs, const LLHost& rhs)
  164. {
  165. if (lhs.mIP < rhs.mIP)
  166. {
  167. return true;
  168. }
  169. if (lhs.mIP > rhs.mIP)
  170. {
  171. return false;
  172. }
  173. return lhs.mPort < rhs.mPort;
  174. }
  175. #endif // LL_LLHOST_H