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