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.

163 lines
6.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2012, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_P2P_BASE_TURNSERVER_H_
  28. #define TALK_P2P_BASE_TURNSERVER_H_
  29. #include <list>
  30. #include <map>
  31. #include <set>
  32. #include <string>
  33. #include "talk/base/messagequeue.h"
  34. #include "talk/base/sigslot.h"
  35. #include "talk/base/socketaddress.h"
  36. namespace talk_base {
  37. class AsyncPacketSocket;
  38. class ByteBuffer;
  39. class PacketSocketFactory;
  40. class Thread;
  41. }
  42. namespace cricket {
  43. class StunMessage;
  44. class TurnMessage;
  45. // The default server port for TURN, as specified in RFC5766.
  46. const int TURN_SERVER_PORT = 3478;
  47. // An interface through which the MD5 credential hash can be retrieved.
  48. class TurnAuthInterface {
  49. public:
  50. // Gets HA1 for the specified user and realm.
  51. // HA1 = MD5(A1) = MD5(username:realm:password).
  52. // Return true if the given username and realm are valid, or false if not.
  53. virtual bool GetKey(const std::string& username, const std::string& realm,
  54. std::string* key) = 0;
  55. };
  56. // The core TURN server class. Give it a socket to listen on via
  57. // AddInternalServerSocket, and a factory to create external sockets via
  58. // SetExternalSocketFactory, and it's ready to go.
  59. // Not yet wired up: TCP support.
  60. class TurnServer : public sigslot::has_slots<> {
  61. public:
  62. explicit TurnServer(talk_base::Thread* thread);
  63. ~TurnServer();
  64. // Gets/sets the realm value to use for the server.
  65. const std::string& realm() const { return realm_; }
  66. void set_realm(const std::string& realm) { realm_ = realm; }
  67. // Gets/sets the value for the SOFTWARE attribute for TURN messages.
  68. const std::string& software() const { return software_; }
  69. void set_software(const std::string& software) { software_ = software; }
  70. // Sets the authentication callback; does not take ownership.
  71. void set_auth_hook(TurnAuthInterface* auth_hook) { auth_hook_ = auth_hook; }
  72. // Starts listening for packets from internal clients.
  73. void AddInternalServerSocket(talk_base::AsyncPacketSocket* socket);
  74. // Specifies the factory to use for creating external sockets.
  75. void SetExternalSocketFactory(talk_base::PacketSocketFactory* factory,
  76. const talk_base::SocketAddress& address);
  77. private:
  78. // The protocol used by the client to connect to the server.
  79. enum ProtocolType {
  80. TURNPROTO_UNKNOWN,
  81. TURNPROTO_UDP,
  82. TURNPROTO_TCP,
  83. TURNPROTO_SSLTCP
  84. };
  85. // Encapsulates the client's connection to the server.
  86. class Connection {
  87. public:
  88. Connection() : proto_(TURNPROTO_UNKNOWN) {}
  89. Connection(const talk_base::SocketAddress& src,
  90. const talk_base::SocketAddress& dst, ProtocolType proto);
  91. const talk_base::SocketAddress& src() const { return src_; }
  92. const talk_base::SocketAddress& dst() const { return dst_; }
  93. bool operator==(const Connection& t) const;
  94. bool operator<(const Connection& t) const;
  95. std::string ToString() const;
  96. private:
  97. talk_base::SocketAddress src_;
  98. talk_base::SocketAddress dst_;
  99. ProtocolType proto_;
  100. };
  101. class Allocation;
  102. class Permission;
  103. class Channel;
  104. typedef std::map<Connection, Allocation*> AllocationMap;
  105. void OnInternalPacket(talk_base::AsyncPacketSocket* socket, const char* data,
  106. size_t size, const talk_base::SocketAddress& address);
  107. void HandleStunMessage(const Connection& conn, const char* data, size_t size);
  108. void HandleBindingRequest(const Connection& conn, const StunMessage* msg);
  109. void HandleAllocateRequest(const Connection& conn, const TurnMessage* msg,
  110. const std::string& key);
  111. bool GetKey(const StunMessage* msg, std::string* key);
  112. bool CheckAuthorization(const Connection& conn, const StunMessage* msg,
  113. const char* data, size_t size,
  114. const std::string& key);
  115. std::string GenerateNonce() const;
  116. bool ValidateNonce(const std::string& nonce) const;
  117. Allocation* FindAllocation(const Connection& conn);
  118. Allocation* CreateAllocation(const Connection& conn, int proto,
  119. const std::string& key);
  120. void SendErrorResponse(const Connection& conn, const StunMessage* req,
  121. int code, const std::string& reason);
  122. void SendErrorResponseWithRealmAndNonce(const Connection& conn,
  123. const StunMessage* req, int code,
  124. const std::string& reason);
  125. void SendStun(const Connection& conn, StunMessage* msg);
  126. void Send(const Connection& conn, const talk_base::ByteBuffer& buf);
  127. void OnAllocationDestroyed(Allocation* allocation);
  128. talk_base::Thread* thread_;
  129. std::string nonce_key_;
  130. std::string realm_;
  131. std::string software_;
  132. TurnAuthInterface* auth_hook_;
  133. talk_base::scoped_ptr<talk_base::AsyncPacketSocket> server_socket_;
  134. talk_base::scoped_ptr<talk_base::PacketSocketFactory>
  135. external_socket_factory_;
  136. talk_base::SocketAddress external_addr_;
  137. AllocationMap allocations_;
  138. };
  139. } // namespace cricket
  140. #endif // TALK_P2P_BASE_TURNSERVER_H_