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 | |
Tomas Gunnarsson | f22dfdd | 2022-04-13 09:07:30 | [diff] [blame] | 17 | #include <functional> |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 18 | #include <map> |
Tommi | 86aa03e | 2022-04-12 07:17:57 | [diff] [blame] | 19 | #include <memory> |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 20 | #include <string> |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 21 | |
Artem Titov | c374d11 | 2022-06-16 19:27:45 | [diff] [blame] | 22 | #include "api/task_queue/pending_task_safety_flag.h" |
Tommi | 7ef4f51 | 2022-06-14 12:51:29 | [diff] [blame] | 23 | #include "api/task_queue/task_queue_base.h" |
Patrik Höglund | 56d9452 | 2019-11-18 14:53:32 | [diff] [blame] | 24 | #include "api/transport/stun.h" |
Tommi | 7ef4f51 | 2022-06-14 12:51:29 | [diff] [blame] | 25 | #include "api/units/time_delta.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 26 | |
| 27 | namespace cricket { |
| 28 | |
| 29 | class StunRequest; |
| 30 | |
Tommi | f7b30e0 | 2022-07-06 10:26:48 | [diff] [blame] | 31 | const int kAllRequestsForTest = 0; |
honghaiz | 6b9ab92 | 2016-01-05 17:06:12 | [diff] [blame] | 32 | |
pthatcher | 94a2f21 | 2017-02-08 22:42:22 | [diff] [blame] | 33 | // Total max timeouts: 39.75 seconds |
| 34 | // For years, this was 9.5 seconds, but for networks that experience moments of |
| 35 | // high RTT (such as 40s on 2G networks), this doesn't work well. |
| 36 | const int STUN_TOTAL_TIMEOUT = 39750; // milliseconds |
| 37 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 38 | // Manages a set of STUN requests, sending and resending until we receive a |
| 39 | // response or determine that the request has timed out. |
| 40 | class StunRequestManager { |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 | [diff] [blame] | 41 | public: |
Tomas Gunnarsson | f22dfdd | 2022-04-13 09:07:30 | [diff] [blame] | 42 | StunRequestManager( |
Tommi | 7ef4f51 | 2022-06-14 12:51:29 | [diff] [blame] | 43 | webrtc::TaskQueueBase* thread, |
Tomas Gunnarsson | f22dfdd | 2022-04-13 09:07:30 | [diff] [blame] | 44 | std::function<void(const void*, size_t, StunRequest*)> send_packet); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 45 | ~StunRequestManager(); |
| 46 | |
| 47 | // Starts sending the given request (perhaps after a delay). |
| 48 | void Send(StunRequest* request); |
| 49 | void SendDelayed(StunRequest* request, int delay); |
| 50 | |
Tommi | f7b30e0 | 2022-07-06 10:26:48 | [diff] [blame] | 51 | // If `msg_type` is kAllRequestsForTest, sends all pending requests right |
| 52 | // away. Otherwise, sends those that have a matching type right away. Only for |
| 53 | // testing. |
Tommi | 7ef4f51 | 2022-06-14 12:51:29 | [diff] [blame] | 54 | // TODO(tommi): Remove this method and update tests that use it to simulate |
| 55 | // production code. |
Tommi | 86aa03e | 2022-04-12 07:17:57 | [diff] [blame] | 56 | void FlushForTest(int msg_type); |
Honghai Zhang | 8597543 | 2015-11-12 19:07:12 | [diff] [blame] | 57 | |
Artem Titov | 2dbb4c9 | 2021-07-26 13:12:41 | [diff] [blame] | 58 | // Returns true if at least one request with `msg_type` is scheduled for |
honghaiz | e2af9ef | 2016-03-03 16:27:47 | [diff] [blame] | 59 | // transmission. For testing only. |
Tommi | 7ef4f51 | 2022-06-14 12:51:29 | [diff] [blame] | 60 | // TODO(tommi): Remove this method and update tests that use it to simulate |
| 61 | // production code. |
Tommi | 86aa03e | 2022-04-12 07:17:57 | [diff] [blame] | 62 | bool HasRequestForTest(int msg_type); |
honghaiz | e2af9ef | 2016-03-03 16:27:47 | [diff] [blame] | 63 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 64 | // Removes all stun requests that were added previously. |
| 65 | void Clear(); |
| 66 | |
| 67 | // Determines whether the given message is a response to one of the |
| 68 | // outstanding requests, and if so, processes it appropriately. |
| 69 | bool CheckResponse(StunMessage* msg); |
| 70 | bool CheckResponse(const char* data, size_t size); |
| 71 | |
Tommi | 2545257 | 2022-04-12 10:51:40 | [diff] [blame] | 72 | // Called from a StunRequest when a timeout occurs. |
| 73 | void OnRequestTimedOut(StunRequest* request); |
| 74 | |
Tommi | 86aa03e | 2022-04-12 07:17:57 | [diff] [blame] | 75 | bool empty() const; |
| 76 | |
Tommi | 7ef4f51 | 2022-06-14 12:51:29 | [diff] [blame] | 77 | webrtc::TaskQueueBase* network_thread() const { return thread_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 78 | |
Tomas Gunnarsson | f22dfdd | 2022-04-13 09:07:30 | [diff] [blame] | 79 | void SendPacket(const void* data, size_t size, StunRequest* request); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 80 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 | [diff] [blame] | 81 | private: |
Tommi | 2545257 | 2022-04-12 10:51:40 | [diff] [blame] | 82 | typedef std::map<std::string, std::unique_ptr<StunRequest>> RequestMap; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 83 | |
Tommi | 7ef4f51 | 2022-06-14 12:51:29 | [diff] [blame] | 84 | webrtc::TaskQueueBase* const thread_; |
Tommi | 86aa03e | 2022-04-12 07:17:57 | [diff] [blame] | 85 | RequestMap requests_ RTC_GUARDED_BY(thread_); |
Tomas Gunnarsson | f22dfdd | 2022-04-13 09:07:30 | [diff] [blame] | 86 | const std::function<void(const void*, size_t, StunRequest*)> send_packet_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | // Represents an individual request to be sent. The STUN message can either be |
| 90 | // constructed beforehand or built on demand. |
Tommi | 7ef4f51 | 2022-06-14 12:51:29 | [diff] [blame] | 91 | class StunRequest { |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 | [diff] [blame] | 92 | public: |
Tommi | 86aa03e | 2022-04-12 07:17:57 | [diff] [blame] | 93 | explicit StunRequest(StunRequestManager& manager); |
| 94 | StunRequest(StunRequestManager& manager, |
Tommi | 278b19d | 2022-04-12 12:03:40 | [diff] [blame] | 95 | std::unique_ptr<StunMessage> message); |
Tommi | 7ef4f51 | 2022-06-14 12:51:29 | [diff] [blame] | 96 | virtual ~StunRequest(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 97 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 98 | // The manager handling this request (if it has been scheduled for sending). |
Tommi | 86aa03e | 2022-04-12 07:17:57 | [diff] [blame] | 99 | StunRequestManager* manager() { return &manager_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 100 | |
| 101 | // Returns the transaction ID of this request. |
Tommi | 86aa03e | 2022-04-12 07:17:57 | [diff] [blame] | 102 | const std::string& id() const { return msg_->transaction_id(); } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 103 | |
Zach Stein | 92c4289 | 2018-11-28 19:38:52 | [diff] [blame] | 104 | // Returns the reduced transaction ID of this request. |
| 105 | uint32_t reduced_transaction_id() const { |
| 106 | return msg_->reduced_transaction_id(); |
| 107 | } |
| 108 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 109 | // Returns the STUN type of the request message. |
| 110 | int type(); |
| 111 | |
Artem Titov | 2dbb4c9 | 2021-07-26 13:12:41 | [diff] [blame] | 112 | // Returns a const pointer to `msg_`. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 113 | const StunMessage* msg() const; |
| 114 | |
| 115 | // Time elapsed since last send (in ms) |
honghaiz | 34b11eb | 2016-03-16 15:55:44 | [diff] [blame] | 116 | int Elapsed() const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 117 | |
Jonas Oreland | 02ce588 | 2023-11-15 09:53:40 | [diff] [blame] | 118 | // Add method to explitly allow requests w/o password. |
| 119 | // - STUN_BINDINGs from StunPort to a stun server |
| 120 | // - The initial TURN_ALLOCATE_REQUEST |
| 121 | void SetAuthenticationRequired(bool val) { authentication_required_ = val; } |
| 122 | bool AuthenticationRequired() const { return authentication_required_; } |
| 123 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 | [diff] [blame] | 124 | protected: |
Tommi | 86aa03e | 2022-04-12 07:17:57 | [diff] [blame] | 125 | friend class StunRequestManager; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 126 | |
Tommi | 7ef4f51 | 2022-06-14 12:51:29 | [diff] [blame] | 127 | // Called by StunRequestManager. |
| 128 | void Send(webrtc::TimeDelta delay); |
| 129 | |
| 130 | // Called from FlushForTest. |
| 131 | // TODO(tommi): Remove when FlushForTest gets removed. |
| 132 | void ResetTasksForTest(); |
| 133 | |
Tommi | 159f313 | 2022-06-03 12:37:31 | [diff] [blame] | 134 | StunMessage* mutable_msg() { return msg_.get(); } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 135 | |
| 136 | // Called when the message receives a response or times out. |
| 137 | virtual void OnResponse(StunMessage* response) {} |
| 138 | virtual void OnErrorResponse(StunMessage* response) {} |
| 139 | virtual void OnTimeout() {} |
Peter Thatcher | 1cf6f81 | 2015-05-15 17:40:45 | [diff] [blame] | 140 | // Called when the message is sent. |
| 141 | virtual void OnSent(); |
Tommi | 7ef4f51 | 2022-06-14 12:51:29 | [diff] [blame] | 142 | // Returns the next delay for resends in milliseconds. |
Peter Thatcher | 1cf6f81 | 2015-05-15 17:40:45 | [diff] [blame] | 143 | virtual int resend_delay(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 144 | |
Tommi | 86aa03e | 2022-04-12 07:17:57 | [diff] [blame] | 145 | webrtc::TaskQueueBase* network_thread() const { |
| 146 | return manager_.network_thread(); |
| 147 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 148 | |
Tommi | 86aa03e | 2022-04-12 07:17:57 | [diff] [blame] | 149 | void set_timed_out(); |
| 150 | |
| 151 | private: |
Tommi | 7ef4f51 | 2022-06-14 12:51:29 | [diff] [blame] | 152 | void SendInternal(); |
| 153 | // Calls `PostDelayedTask` to queue up a call to SendInternal after the |
| 154 | // specified timeout. |
| 155 | void SendDelayed(webrtc::TimeDelta delay); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 156 | |
Tommi | 86aa03e | 2022-04-12 07:17:57 | [diff] [blame] | 157 | StunRequestManager& manager_; |
| 158 | const std::unique_ptr<StunMessage> msg_; |
| 159 | int64_t tstamp_ RTC_GUARDED_BY(network_thread()); |
| 160 | int count_ RTC_GUARDED_BY(network_thread()); |
| 161 | bool timeout_ RTC_GUARDED_BY(network_thread()); |
Tommi | 7ef4f51 | 2022-06-14 12:51:29 | [diff] [blame] | 162 | webrtc::ScopedTaskSafety task_safety_{ |
| 163 | webrtc::PendingTaskSafetyFlag::CreateDetachedInactive()}; |
Jonas Oreland | 02ce588 | 2023-11-15 09:53:40 | [diff] [blame] | 164 | bool authentication_required_ = true; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | } // namespace cricket |
| 168 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 169 | #endif // P2P_BASE_STUN_REQUEST_H_ |