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