henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef P2P_BASE_STUNREQUEST_H_ |
| 12 | #define P2P_BASE_STUNREQUEST_H_ |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 13 | |
| 14 | #include <map> |
| 15 | #include <string> |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "p2p/base/stun.h" |
| 17 | #include "rtc_base/sigslot.h" |
| 18 | #include "rtc_base/thread.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 19 | |
| 20 | namespace cricket { |
| 21 | |
| 22 | class StunRequest; |
| 23 | |
honghaiz | 6b9ab92 | 2016-01-05 09:06:12 -0800 | [diff] [blame] | 24 | const int kAllRequests = 0; |
| 25 | |
pthatcher | 94a2f21 | 2017-02-08 14:42:22 -0800 | [diff] [blame] | 26 | // Total max timeouts: 39.75 seconds |
| 27 | // For years, this was 9.5 seconds, but for networks that experience moments of |
| 28 | // high RTT (such as 40s on 2G networks), this doesn't work well. |
| 29 | const int STUN_TOTAL_TIMEOUT = 39750; // milliseconds |
| 30 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 31 | // Manages a set of STUN requests, sending and resending until we receive a |
| 32 | // response or determine that the request has timed out. |
| 33 | class StunRequestManager { |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 34 | public: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 35 | StunRequestManager(rtc::Thread* thread); |
| 36 | ~StunRequestManager(); |
| 37 | |
| 38 | // Starts sending the given request (perhaps after a delay). |
| 39 | void Send(StunRequest* request); |
| 40 | void SendDelayed(StunRequest* request, int delay); |
| 41 | |
honghaiz | 6b9ab92 | 2016-01-05 09:06:12 -0800 | [diff] [blame] | 42 | // If |msg_type| is kAllRequests, sends all pending requests right away. |
| 43 | // Otherwise, sends those that have a matching type right away. |
| 44 | // Only for testing. |
| 45 | void Flush(int msg_type); |
Honghai Zhang | 8597543 | 2015-11-12 11:07:12 -0800 | [diff] [blame] | 46 | |
honghaiz | e2af9ef | 2016-03-03 08:27:47 -0800 | [diff] [blame] | 47 | // Returns true if at least one request with |msg_type| is scheduled for |
| 48 | // transmission. For testing only. |
| 49 | bool HasRequest(int msg_type); |
| 50 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 51 | // Removes a stun request that was added previously. This will happen |
| 52 | // automatically when a request succeeds, fails, or times out. |
| 53 | void Remove(StunRequest* request); |
| 54 | |
| 55 | // Removes all stun requests that were added previously. |
| 56 | void Clear(); |
| 57 | |
| 58 | // Determines whether the given message is a response to one of the |
| 59 | // outstanding requests, and if so, processes it appropriately. |
| 60 | bool CheckResponse(StunMessage* msg); |
| 61 | bool CheckResponse(const char* data, size_t size); |
| 62 | |
| 63 | bool empty() { return requests_.empty(); } |
| 64 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 65 | // Set the Origin header for outgoing stun messages. |
| 66 | void set_origin(const std::string& origin) { origin_ = origin; } |
| 67 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 68 | // Raised when there are bytes to be sent. |
| 69 | sigslot::signal3<const void*, size_t, StunRequest*> SignalSendPacket; |
| 70 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 71 | private: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 72 | typedef std::map<std::string, StunRequest*> RequestMap; |
| 73 | |
| 74 | rtc::Thread* thread_; |
| 75 | RequestMap requests_; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 76 | std::string origin_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 77 | |
| 78 | friend class StunRequest; |
| 79 | }; |
| 80 | |
| 81 | // Represents an individual request to be sent. The STUN message can either be |
| 82 | // constructed beforehand or built on demand. |
| 83 | class StunRequest : public rtc::MessageHandler { |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 84 | public: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 85 | StunRequest(); |
| 86 | StunRequest(StunMessage* request); |
Steve Anton | f2737d2 | 2017-10-31 16:27:34 -0700 | [diff] [blame^] | 87 | ~StunRequest() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 88 | |
| 89 | // Causes our wrapped StunMessage to be Prepared |
| 90 | void Construct(); |
| 91 | |
| 92 | // The manager handling this request (if it has been scheduled for sending). |
| 93 | StunRequestManager* manager() { return manager_; } |
| 94 | |
| 95 | // Returns the transaction ID of this request. |
| 96 | const std::string& id() { return msg_->transaction_id(); } |
| 97 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 98 | // the origin value |
| 99 | const std::string& origin() const { return origin_; } |
| 100 | void set_origin(const std::string& origin) { origin_ = origin; } |
| 101 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 102 | // Returns the STUN type of the request message. |
| 103 | int type(); |
| 104 | |
| 105 | // Returns a const pointer to |msg_|. |
| 106 | const StunMessage* msg() const; |
| 107 | |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 108 | // Returns a mutable pointer to |msg_|. |
| 109 | StunMessage* mutable_msg(); |
| 110 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 111 | // Time elapsed since last send (in ms) |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 112 | int Elapsed() const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 113 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 114 | protected: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 115 | int count_; |
| 116 | bool timeout_; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 117 | std::string origin_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 118 | |
| 119 | // Fills in a request object to be sent. Note that request's transaction ID |
| 120 | // will already be set and cannot be changed. |
| 121 | virtual void Prepare(StunMessage* request) {} |
| 122 | |
| 123 | // Called when the message receives a response or times out. |
| 124 | virtual void OnResponse(StunMessage* response) {} |
| 125 | virtual void OnErrorResponse(StunMessage* response) {} |
| 126 | virtual void OnTimeout() {} |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 127 | // Called when the message is sent. |
| 128 | virtual void OnSent(); |
| 129 | // Returns the next delay for resends. |
| 130 | virtual int resend_delay(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 131 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 132 | private: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 133 | void set_manager(StunRequestManager* manager); |
| 134 | |
| 135 | // Handles messages for sending and timeout. |
Steve Anton | f2737d2 | 2017-10-31 16:27:34 -0700 | [diff] [blame^] | 136 | void OnMessage(rtc::Message* pmsg) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 137 | |
| 138 | StunRequestManager* manager_; |
| 139 | StunMessage* msg_; |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 140 | int64_t tstamp_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 141 | |
| 142 | friend class StunRequestManager; |
| 143 | }; |
| 144 | |
| 145 | } // namespace cricket |
| 146 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 147 | #endif // P2P_BASE_STUNREQUEST_H_ |