You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
8.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef P2P_BASE_RELAY_SERVER_H_
  11. #define P2P_BASE_RELAY_SERVER_H_
  12. #include <map>
  13. #include <string>
  14. #include <vector>
  15. #include "p2p/base/port.h"
  16. #include "p2p/base/stun.h"
  17. #include "rtc_base/async_udp_socket.h"
  18. #include "rtc_base/random.h"
  19. #include "rtc_base/socket_address_pair.h"
  20. #include "rtc_base/thread.h"
  21. #include "rtc_base/time_utils.h"
  22. namespace cricket {
  23. class RelayServerBinding;
  24. class RelayServerConnection;
  25. // Relays traffic between connections to the server that are "bound" together.
  26. // All connections created with the same username/password are bound together.
  27. class RelayServer : public rtc::MessageHandler, public sigslot::has_slots<> {
  28. public:
  29. // Creates a server, which will use this thread to post messages to itself.
  30. explicit RelayServer(rtc::Thread* thread);
  31. ~RelayServer() override;
  32. rtc::Thread* thread() { return thread_; }
  33. // Indicates whether we will print updates of the number of bindings.
  34. bool log_bindings() const { return log_bindings_; }
  35. void set_log_bindings(bool log_bindings) { log_bindings_ = log_bindings; }
  36. // Updates the set of sockets that the server uses to talk to "internal"
  37. // clients. These are clients that do the "port allocations".
  38. void AddInternalSocket(rtc::AsyncPacketSocket* socket);
  39. void RemoveInternalSocket(rtc::AsyncPacketSocket* socket);
  40. // Updates the set of sockets that the server uses to talk to "external"
  41. // clients. These are the clients that do not do allocations. They do not
  42. // know that these addresses represent a relay server.
  43. void AddExternalSocket(rtc::AsyncPacketSocket* socket);
  44. void RemoveExternalSocket(rtc::AsyncPacketSocket* socket);
  45. // Starts listening for connections on this sockets. When someone
  46. // tries to connect, the connection will be accepted and a new
  47. // internal socket will be added.
  48. void AddInternalServerSocket(rtc::AsyncSocket* socket,
  49. cricket::ProtocolType proto);
  50. // Removes this server socket from the list.
  51. void RemoveInternalServerSocket(rtc::AsyncSocket* socket);
  52. // Methods for testing and debuging.
  53. int GetConnectionCount() const;
  54. rtc::SocketAddressPair GetConnection(int connection) const;
  55. bool HasConnection(const rtc::SocketAddress& address) const;
  56. private:
  57. rtc::Thread* thread_;
  58. webrtc::Random random_;
  59. bool log_bindings_;
  60. std::vector<rtc::AsyncPacketSocket*> internal_sockets_;
  61. std::vector<rtc::AsyncPacketSocket*> external_sockets_;
  62. std::vector<rtc::AsyncPacketSocket*> removed_sockets_;
  63. std::map<rtc::AsyncSocket*, cricket::ProtocolType> server_sockets_;
  64. std::map<std::string, RelayServerBinding*> bindings_;
  65. std::map<rtc::SocketAddressPair, RelayServerConnection*> connections_;
  66. // Called when a packet is received by the server on one of its sockets.
  67. void OnInternalPacket(rtc::AsyncPacketSocket* socket,
  68. const char* bytes,
  69. size_t size,
  70. const rtc::SocketAddress& remote_addr,
  71. const int64_t& packet_time_us);
  72. void OnExternalPacket(rtc::AsyncPacketSocket* socket,
  73. const char* bytes,
  74. size_t size,
  75. const rtc::SocketAddress& remote_addr,
  76. const int64_t& packet_time_us);
  77. void OnReadEvent(rtc::AsyncSocket* socket);
  78. // Processes the relevant STUN request types from the client.
  79. bool HandleStun(const char* bytes,
  80. size_t size,
  81. const rtc::SocketAddress& remote_addr,
  82. rtc::AsyncPacketSocket* socket,
  83. std::string* username,
  84. StunMessage* msg);
  85. void HandleStunAllocate(const char* bytes,
  86. size_t size,
  87. const rtc::SocketAddressPair& ap,
  88. rtc::AsyncPacketSocket* socket);
  89. void HandleStun(RelayServerConnection* int_conn,
  90. const char* bytes,
  91. size_t size);
  92. void HandleStunAllocate(RelayServerConnection* int_conn,
  93. const StunMessage& msg);
  94. void HandleStunSend(RelayServerConnection* int_conn, const StunMessage& msg);
  95. // Adds/Removes the a connection or binding.
  96. void AddConnection(RelayServerConnection* conn);
  97. void RemoveConnection(RelayServerConnection* conn);
  98. void RemoveBinding(RelayServerBinding* binding);
  99. // Handle messages in our thread.
  100. void OnMessage(rtc::Message* pmsg) override;
  101. // Called when the timer for checking lifetime times out.
  102. void OnTimeout(RelayServerBinding* binding);
  103. // Accept connections on this server socket.
  104. void AcceptConnection(rtc::AsyncSocket* server_socket);
  105. friend class RelayServerConnection;
  106. friend class RelayServerBinding;
  107. };
  108. // Maintains information about a connection to the server. Each connection is
  109. // part of one and only one binding.
  110. class RelayServerConnection {
  111. public:
  112. RelayServerConnection(RelayServerBinding* binding,
  113. const rtc::SocketAddressPair& addrs,
  114. rtc::AsyncPacketSocket* socket);
  115. ~RelayServerConnection();
  116. RelayServerBinding* binding() { return binding_; }
  117. rtc::AsyncPacketSocket* socket() { return socket_; }
  118. // Returns a pair where the source is the remote address and the destination
  119. // is the local address.
  120. const rtc::SocketAddressPair& addr_pair() { return addr_pair_; }
  121. // Sends a packet to the connected client. If an address is provided, then
  122. // we make sure the internal client receives it, wrapping if necessary.
  123. void Send(const char* data, size_t size);
  124. void Send(const char* data, size_t size, const rtc::SocketAddress& ext_addr);
  125. // Sends a STUN message to the connected client with no wrapping.
  126. void SendStun(const StunMessage& msg);
  127. void SendStunError(const StunMessage& request, int code, const char* desc);
  128. // A locked connection is one for which we know the intended destination of
  129. // any raw packet received.
  130. bool locked() const { return locked_; }
  131. void Lock();
  132. void Unlock();
  133. // Records the address that raw packets should be forwarded to (for internal
  134. // packets only; for external, we already know where they go).
  135. const rtc::SocketAddress& default_destination() const {
  136. return default_dest_;
  137. }
  138. void set_default_destination(const rtc::SocketAddress& addr) {
  139. default_dest_ = addr;
  140. }
  141. private:
  142. RelayServerBinding* binding_;
  143. rtc::SocketAddressPair addr_pair_;
  144. rtc::AsyncPacketSocket* socket_;
  145. bool locked_;
  146. rtc::SocketAddress default_dest_;
  147. };
  148. // Records a set of internal and external connections that we relay between,
  149. // or in other words, that are "bound" together.
  150. class RelayServerBinding : public rtc::MessageHandler {
  151. public:
  152. RelayServerBinding(RelayServer* server,
  153. const std::string& username,
  154. const std::string& password,
  155. int lifetime);
  156. ~RelayServerBinding() override;
  157. RelayServer* server() { return server_; }
  158. int lifetime() { return lifetime_; }
  159. const std::string& username() { return username_; }
  160. const std::string& password() { return password_; }
  161. const std::string& magic_cookie() { return magic_cookie_; }
  162. // Adds/Removes a connection into the binding.
  163. void AddInternalConnection(RelayServerConnection* conn);
  164. void AddExternalConnection(RelayServerConnection* conn);
  165. // We keep track of the use of each binding. If we detect that it was not
  166. // used for longer than the lifetime, then we send a signal.
  167. void NoteUsed();
  168. sigslot::signal1<RelayServerBinding*> SignalTimeout;
  169. // Determines whether the given packet has the magic cookie present (in the
  170. // right place).
  171. bool HasMagicCookie(const char* bytes, size_t size) const;
  172. // Determines the connection to use to send packets to or from the given
  173. // external address.
  174. RelayServerConnection* GetInternalConnection(
  175. const rtc::SocketAddress& ext_addr);
  176. RelayServerConnection* GetExternalConnection(
  177. const rtc::SocketAddress& ext_addr);
  178. // MessageHandler:
  179. void OnMessage(rtc::Message* pmsg) override;
  180. private:
  181. RelayServer* server_;
  182. std::string username_;
  183. std::string password_;
  184. std::string magic_cookie_;
  185. std::vector<RelayServerConnection*> internal_connections_;
  186. std::vector<RelayServerConnection*> external_connections_;
  187. int lifetime_;
  188. int64_t last_used_;
  189. // TODO(?): bandwidth
  190. };
  191. } // namespace cricket
  192. #endif // P2P_BASE_RELAY_SERVER_H_