blob: 0d00772b159beccd6a4048b99949265341b0760f [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
Patrik Höglund56d94522019-11-18 15:53:32 +010022#include "api/transport/stun.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/message_handler.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/thread.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000025
26namespace cricket {
27
28class StunRequest;
29
honghaiz6b9ab922016-01-05 09:06:12 -080030const int kAllRequests = 0;
31
pthatcher94a2f212017-02-08 14:42:22 -080032// 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.
35const int STUN_TOTAL_TIMEOUT = 39750; // milliseconds
36
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000037// Manages a set of STUN requests, sending and resending until we receive a
38// response or determine that the request has timed out.
39class StunRequestManager {
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000040 public:
Tomas Gunnarssonf22dfdd2022-04-13 09:07:30 +000041 StunRequestManager(
42 rtc::Thread* thread,
43 std::function<void(const void*, size_t, StunRequest*)> send_packet);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000044 ~StunRequestManager();
45
46 // Starts sending the given request (perhaps after a delay).
47 void Send(StunRequest* request);
48 void SendDelayed(StunRequest* request, int delay);
49
Artem Titov2dbb4c92021-07-26 15:12:41 +020050 // If `msg_type` is kAllRequests, sends all pending requests right away.
honghaiz6b9ab922016-01-05 09:06:12 -080051 // Otherwise, sends those that have a matching type right away.
52 // Only for testing.
Tommi86aa03e2022-04-12 09:17:57 +020053 void FlushForTest(int msg_type);
Honghai Zhang85975432015-11-12 11:07:12 -080054
Artem Titov2dbb4c92021-07-26 15:12:41 +020055 // Returns true if at least one request with `msg_type` is scheduled for
honghaize2af9ef2016-03-03 08:27:47 -080056 // transmission. For testing only.
Tommi86aa03e2022-04-12 09:17:57 +020057 bool HasRequestForTest(int msg_type);
honghaize2af9ef2016-03-03 08:27:47 -080058
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000059 // Removes all stun requests that were added previously.
60 void Clear();
61
62 // Determines whether the given message is a response to one of the
63 // outstanding requests, and if so, processes it appropriately.
64 bool CheckResponse(StunMessage* msg);
65 bool CheckResponse(const char* data, size_t size);
66
Tommi25452572022-04-12 12:51:40 +020067 // Called from a StunRequest when a timeout occurs.
68 void OnRequestTimedOut(StunRequest* request);
69
Tommi86aa03e2022-04-12 09:17:57 +020070 bool empty() const;
71
72 // TODO(tommi): Use TaskQueueBase* instead of rtc::Thread.
73 rtc::Thread* network_thread() const { return thread_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000074
Tomas Gunnarssonf22dfdd2022-04-13 09:07:30 +000075 void SendPacket(const void* data, size_t size, StunRequest* request);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000076
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000077 private:
Tommi25452572022-04-12 12:51:40 +020078 typedef std::map<std::string, std::unique_ptr<StunRequest>> RequestMap;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000079
Tomas Gunnarsson4d76a132020-09-12 15:41:52 +020080 rtc::Thread* const thread_;
Tommi86aa03e2022-04-12 09:17:57 +020081 RequestMap requests_ RTC_GUARDED_BY(thread_);
Tomas Gunnarssonf22dfdd2022-04-13 09:07:30 +000082 const std::function<void(const void*, size_t, StunRequest*)> send_packet_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000083};
84
85// Represents an individual request to be sent. The STUN message can either be
86// constructed beforehand or built on demand.
Tomas Gunnarsson4d76a132020-09-12 15:41:52 +020087class StunRequest : public rtc::MessageHandler {
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000088 public:
Tommi86aa03e2022-04-12 09:17:57 +020089 explicit StunRequest(StunRequestManager& manager);
90 StunRequest(StunRequestManager& manager,
Tommi278b19d2022-04-12 14:03:40 +020091 std::unique_ptr<StunMessage> message);
Steve Antonf2737d22017-10-31 16:27:34 -070092 ~StunRequest() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000093
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).
Tommi86aa03e2022-04-12 09:17:57 +020098 StunRequestManager* manager() { return &manager_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000099
100 // Returns the transaction ID of this request.
Tommi86aa03e2022-04-12 09:17:57 +0200101 const std::string& id() const { return msg_->transaction_id(); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000102
Zach Stein92c42892018-11-28 11:38:52 -0800103 // Returns the reduced transaction ID of this request.
104 uint32_t reduced_transaction_id() const {
105 return msg_->reduced_transaction_id();
106 }
107
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000108 // Returns the STUN type of the request message.
109 int type();
110
Artem Titov2dbb4c92021-07-26 15:12:41 +0200111 // Returns a const pointer to `msg_`.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000112 const StunMessage* msg() const;
113
114 // Time elapsed since last send (in ms)
honghaiz34b11eb2016-03-16 08:55:44 -0700115 int Elapsed() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000116
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000117 protected:
Tommi86aa03e2022-04-12 09:17:57 +0200118 friend class StunRequestManager;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000119
120 // Fills in a request object to be sent. Note that request's transaction ID
121 // will already be set and cannot be changed.
Tommi278b19d2022-04-12 14:03:40 +0200122 virtual void Prepare(StunMessage* message) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000123
124 // Called when the message receives a response or times out.
125 virtual void OnResponse(StunMessage* response) {}
126 virtual void OnErrorResponse(StunMessage* response) {}
127 virtual void OnTimeout() {}
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700128 // Called when the message is sent.
129 virtual void OnSent();
130 // Returns the next delay for resends.
131 virtual int resend_delay();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000132
Tommi86aa03e2022-04-12 09:17:57 +0200133 webrtc::TaskQueueBase* network_thread() const {
134 return manager_.network_thread();
135 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000136
Tommi86aa03e2022-04-12 09:17:57 +0200137 void set_timed_out();
138
139 private:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000140 // Handles messages for sending and timeout.
Steve Antonf2737d22017-10-31 16:27:34 -0700141 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000142
Tommi86aa03e2022-04-12 09:17:57 +0200143 StunRequestManager& manager_;
144 const std::unique_ptr<StunMessage> msg_;
145 int64_t tstamp_ RTC_GUARDED_BY(network_thread());
146 int count_ RTC_GUARDED_BY(network_thread());
147 bool timeout_ RTC_GUARDED_BY(network_thread());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000148};
149
150} // namespace cricket
151
Steve Anton10542f22019-01-11 09:11:00 -0800152#endif // P2P_BASE_STUN_REQUEST_H_