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.

159 lines
4.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_STUN_REQUEST_H_
  11. #define P2P_BASE_STUN_REQUEST_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <map>
  15. #include <string>
  16. #include "p2p/base/stun.h"
  17. #include "rtc_base/message_handler.h"
  18. #include "rtc_base/message_queue.h"
  19. #include "rtc_base/third_party/sigslot/sigslot.h"
  20. #include "rtc_base/thread.h"
  21. namespace cricket {
  22. class StunRequest;
  23. const int kAllRequests = 0;
  24. // Total max timeouts: 39.75 seconds
  25. // For years, this was 9.5 seconds, but for networks that experience moments of
  26. // high RTT (such as 40s on 2G networks), this doesn't work well.
  27. const int STUN_TOTAL_TIMEOUT = 39750; // milliseconds
  28. // Manages a set of STUN requests, sending and resending until we receive a
  29. // response or determine that the request has timed out.
  30. class StunRequestManager {
  31. public:
  32. explicit StunRequestManager(rtc::Thread* thread);
  33. ~StunRequestManager();
  34. // Starts sending the given request (perhaps after a delay).
  35. void Send(StunRequest* request);
  36. void SendDelayed(StunRequest* request, int delay);
  37. // If |msg_type| is kAllRequests, sends all pending requests right away.
  38. // Otherwise, sends those that have a matching type right away.
  39. // Only for testing.
  40. void Flush(int msg_type);
  41. // Returns true if at least one request with |msg_type| is scheduled for
  42. // transmission. For testing only.
  43. bool HasRequest(int msg_type);
  44. // Removes a stun request that was added previously. This will happen
  45. // automatically when a request succeeds, fails, or times out.
  46. void Remove(StunRequest* request);
  47. // Removes all stun requests that were added previously.
  48. void Clear();
  49. // Determines whether the given message is a response to one of the
  50. // outstanding requests, and if so, processes it appropriately.
  51. bool CheckResponse(StunMessage* msg);
  52. bool CheckResponse(const char* data, size_t size);
  53. bool empty() { return requests_.empty(); }
  54. // Set the Origin header for outgoing stun messages.
  55. void set_origin(const std::string& origin) { origin_ = origin; }
  56. // Raised when there are bytes to be sent.
  57. sigslot::signal3<const void*, size_t, StunRequest*> SignalSendPacket;
  58. private:
  59. typedef std::map<std::string, StunRequest*> RequestMap;
  60. rtc::Thread* thread_;
  61. RequestMap requests_;
  62. std::string origin_;
  63. friend class StunRequest;
  64. };
  65. // Represents an individual request to be sent. The STUN message can either be
  66. // constructed beforehand or built on demand.
  67. class StunRequest : public rtc::MessageHandler {
  68. public:
  69. StunRequest();
  70. explicit StunRequest(StunMessage* request);
  71. ~StunRequest() override;
  72. // Causes our wrapped StunMessage to be Prepared
  73. void Construct();
  74. // The manager handling this request (if it has been scheduled for sending).
  75. StunRequestManager* manager() { return manager_; }
  76. // Returns the transaction ID of this request.
  77. const std::string& id() { return msg_->transaction_id(); }
  78. // Returns the reduced transaction ID of this request.
  79. uint32_t reduced_transaction_id() const {
  80. return msg_->reduced_transaction_id();
  81. }
  82. // the origin value
  83. const std::string& origin() const { return origin_; }
  84. void set_origin(const std::string& origin) { origin_ = origin; }
  85. // Returns the STUN type of the request message.
  86. int type();
  87. // Returns a const pointer to |msg_|.
  88. const StunMessage* msg() const;
  89. // Returns a mutable pointer to |msg_|.
  90. StunMessage* mutable_msg();
  91. // Time elapsed since last send (in ms)
  92. int Elapsed() const;
  93. protected:
  94. int count_;
  95. bool timeout_;
  96. std::string origin_;
  97. // Fills in a request object to be sent. Note that request's transaction ID
  98. // will already be set and cannot be changed.
  99. virtual void Prepare(StunMessage* request) {}
  100. // Called when the message receives a response or times out.
  101. virtual void OnResponse(StunMessage* response) {}
  102. virtual void OnErrorResponse(StunMessage* response) {}
  103. virtual void OnTimeout() {}
  104. // Called when the message is sent.
  105. virtual void OnSent();
  106. // Returns the next delay for resends.
  107. virtual int resend_delay();
  108. private:
  109. void set_manager(StunRequestManager* manager);
  110. // Handles messages for sending and timeout.
  111. void OnMessage(rtc::Message* pmsg) override;
  112. StunRequestManager* manager_;
  113. StunMessage* msg_;
  114. int64_t tstamp_;
  115. bool in_rfc5389_retransmission_experiment_;
  116. friend class StunRequestManager;
  117. };
  118. } // namespace cricket
  119. #endif // P2P_BASE_STUN_REQUEST_H_