blob: 56d2597057870a66103e4e189e45c23e3e44004e [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
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 Anton10542f22019-01-11 09:11:00 -080011#ifndef P2P_BASE_STUN_REQUEST_H_
12#define P2P_BASE_STUN_REQUEST_H_
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
Tomas Gunnarssonf22dfdd2022-04-13 09:07:30 +000017#include <functional>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000018#include <map>
Tommi86aa03e2022-04-12 09:17:57 +020019#include <memory>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000020#include <string>
Yves Gerey3e707812018-11-28 16:47:49 +010021
Tommi7ef4f512022-06-14 14:51:29 +020022#include "api/task_queue/task_queue_base.h"
Patrik Höglund56d94522019-11-18 15:53:32 +010023#include "api/transport/stun.h"
Tommi7ef4f512022-06-14 14:51:29 +020024#include "api/units/time_delta.h"
25#include "rtc_base/task_utils/pending_task_safety_flag.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000026
27namespace cricket {
28
29class StunRequest;
30
honghaiz6b9ab922016-01-05 09:06:12 -080031const int kAllRequests = 0;
32
pthatcher94a2f212017-02-08 14:42:22 -080033// 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.
36const int STUN_TOTAL_TIMEOUT = 39750; // milliseconds
37
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000038// Manages a set of STUN requests, sending and resending until we receive a
39// response or determine that the request has timed out.
40class StunRequestManager {
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000041 public:
Tomas Gunnarssonf22dfdd2022-04-13 09:07:30 +000042 StunRequestManager(
Tommi7ef4f512022-06-14 14:51:29 +020043 webrtc::TaskQueueBase* thread,
Tomas Gunnarssonf22dfdd2022-04-13 09:07:30 +000044 std::function<void(const void*, size_t, StunRequest*)> send_packet);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000045 ~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
Artem Titov2dbb4c92021-07-26 15:12:41 +020051 // If `msg_type` is kAllRequests, sends all pending requests right away.
honghaiz6b9ab922016-01-05 09:06:12 -080052 // Otherwise, sends those that have a matching type right away.
53 // Only for testing.
Tommi7ef4f512022-06-14 14:51:29 +020054 // TODO(tommi): Remove this method and update tests that use it to simulate
55 // production code.
Tommi86aa03e2022-04-12 09:17:57 +020056 void FlushForTest(int msg_type);
Honghai Zhang85975432015-11-12 11:07:12 -080057
Artem Titov2dbb4c92021-07-26 15:12:41 +020058 // Returns true if at least one request with `msg_type` is scheduled for
honghaize2af9ef2016-03-03 08:27:47 -080059 // transmission. For testing only.
Tommi7ef4f512022-06-14 14:51:29 +020060 // TODO(tommi): Remove this method and update tests that use it to simulate
61 // production code.
Tommi86aa03e2022-04-12 09:17:57 +020062 bool HasRequestForTest(int msg_type);
honghaize2af9ef2016-03-03 08:27:47 -080063
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000064 // 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
Tommi25452572022-04-12 12:51:40 +020072 // Called from a StunRequest when a timeout occurs.
73 void OnRequestTimedOut(StunRequest* request);
74
Tommi86aa03e2022-04-12 09:17:57 +020075 bool empty() const;
76
Tommi7ef4f512022-06-14 14:51:29 +020077 webrtc::TaskQueueBase* network_thread() const { return thread_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000078
Tomas Gunnarssonf22dfdd2022-04-13 09:07:30 +000079 void SendPacket(const void* data, size_t size, StunRequest* request);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000080
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000081 private:
Tommi25452572022-04-12 12:51:40 +020082 typedef std::map<std::string, std::unique_ptr<StunRequest>> RequestMap;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000083
Tommi7ef4f512022-06-14 14:51:29 +020084 webrtc::TaskQueueBase* const thread_;
Tommi86aa03e2022-04-12 09:17:57 +020085 RequestMap requests_ RTC_GUARDED_BY(thread_);
Tomas Gunnarssonf22dfdd2022-04-13 09:07:30 +000086 const std::function<void(const void*, size_t, StunRequest*)> send_packet_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000087};
88
89// Represents an individual request to be sent. The STUN message can either be
90// constructed beforehand or built on demand.
Tommi7ef4f512022-06-14 14:51:29 +020091class StunRequest {
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000092 public:
Tommi86aa03e2022-04-12 09:17:57 +020093 explicit StunRequest(StunRequestManager& manager);
94 StunRequest(StunRequestManager& manager,
Tommi278b19d2022-04-12 14:03:40 +020095 std::unique_ptr<StunMessage> message);
Tommi7ef4f512022-06-14 14:51:29 +020096 virtual ~StunRequest();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000097
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000098 // The manager handling this request (if it has been scheduled for sending).
Tommi86aa03e2022-04-12 09:17:57 +020099 StunRequestManager* manager() { return &manager_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000100
101 // Returns the transaction ID of this request.
Tommi86aa03e2022-04-12 09:17:57 +0200102 const std::string& id() const { return msg_->transaction_id(); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000103
Zach Stein92c42892018-11-28 11:38:52 -0800104 // 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.org269fb4b2014-10-28 22:20:11 +0000109 // Returns the STUN type of the request message.
110 int type();
111
Artem Titov2dbb4c92021-07-26 15:12:41 +0200112 // Returns a const pointer to `msg_`.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000113 const StunMessage* msg() const;
114
115 // Time elapsed since last send (in ms)
honghaiz34b11eb2016-03-16 08:55:44 -0700116 int Elapsed() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000117
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000118 protected:
Tommi86aa03e2022-04-12 09:17:57 +0200119 friend class StunRequestManager;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000120
Tommi7ef4f512022-06-14 14:51:29 +0200121 // Called by StunRequestManager.
122 void Send(webrtc::TimeDelta delay);
123
124 // Called from FlushForTest.
125 // TODO(tommi): Remove when FlushForTest gets removed.
126 void ResetTasksForTest();
127
Tommi159f3132022-06-03 14:37:31 +0200128 StunMessage* mutable_msg() { return msg_.get(); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000129
130 // Called when the message receives a response or times out.
131 virtual void OnResponse(StunMessage* response) {}
132 virtual void OnErrorResponse(StunMessage* response) {}
133 virtual void OnTimeout() {}
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700134 // Called when the message is sent.
135 virtual void OnSent();
Tommi7ef4f512022-06-14 14:51:29 +0200136 // Returns the next delay for resends in milliseconds.
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700137 virtual int resend_delay();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000138
Tommi86aa03e2022-04-12 09:17:57 +0200139 webrtc::TaskQueueBase* network_thread() const {
140 return manager_.network_thread();
141 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000142
Tommi86aa03e2022-04-12 09:17:57 +0200143 void set_timed_out();
144
145 private:
Tommi7ef4f512022-06-14 14:51:29 +0200146 void SendInternal();
147 // Calls `PostDelayedTask` to queue up a call to SendInternal after the
148 // specified timeout.
149 void SendDelayed(webrtc::TimeDelta delay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000150
Tommi86aa03e2022-04-12 09:17:57 +0200151 StunRequestManager& manager_;
152 const std::unique_ptr<StunMessage> msg_;
153 int64_t tstamp_ RTC_GUARDED_BY(network_thread());
154 int count_ RTC_GUARDED_BY(network_thread());
155 bool timeout_ RTC_GUARDED_BY(network_thread());
Tommi7ef4f512022-06-14 14:51:29 +0200156 webrtc::ScopedTaskSafety task_safety_{
157 webrtc::PendingTaskSafetyFlag::CreateDetachedInactive()};
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000158};
159
160} // namespace cricket
161
Steve Anton10542f22019-01-11 09:11:00 -0800162#endif // P2P_BASE_STUN_REQUEST_H_