blob: d45376ea5508d3b8884128ebf725962472902e86 [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:111/*
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 17:11:0011#ifndef P2P_BASE_STUN_REQUEST_H_
12#define P2P_BASE_STUN_REQUEST_H_
henrike@webrtc.org269fb4b2014-10-28 22:20:1113
Yves Gerey3e707812018-11-28 15:47:4914#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 17:08:3316
henrike@webrtc.org269fb4b2014-10-28 22:20:1117#include <map>
18#include <string>
Yves Gerey3e707812018-11-28 15:47:4919
Patrik Höglund56d94522019-11-18 14:53:3220#include "api/transport/stun.h"
Steve Anton10542f22019-01-11 17:11:0021#include "rtc_base/message_handler.h"
Artem Titove41c4332018-07-25 13:04:2822#include "rtc_base/third_party/sigslot/sigslot.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3123#include "rtc_base/thread.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:1124
25namespace cricket {
26
27class StunRequest;
28
honghaiz6b9ab922016-01-05 17:06:1229const int kAllRequests = 0;
30
pthatcher94a2f212017-02-08 22:42:2231// 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.
34const int STUN_TOTAL_TIMEOUT = 39750; // milliseconds
35
henrike@webrtc.org269fb4b2014-10-28 22:20:1136// Manages a set of STUN requests, sending and resending until we receive a
37// response or determine that the request has timed out.
38class StunRequestManager {
pthatcher@webrtc.org0ba15332015-01-10 00:47:0239 public:
Steve Anton6c38cc72017-11-29 18:25:5840 explicit StunRequestManager(rtc::Thread* thread);
henrike@webrtc.org269fb4b2014-10-28 22:20:1141 ~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
honghaiz6b9ab922016-01-05 17:06:1247 // 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 Zhang85975432015-11-12 19:07:1251
honghaize2af9ef2016-03-03 16:27:4752 // 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.org269fb4b2014-10-28 22:20:1156 // 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.org0ba15332015-01-10 00:47:0270 // Set the Origin header for outgoing stun messages.
71 void set_origin(const std::string& origin) { origin_ = origin; }
72
henrike@webrtc.org269fb4b2014-10-28 22:20:1173 // Raised when there are bytes to be sent.
74 sigslot::signal3<const void*, size_t, StunRequest*> SignalSendPacket;
75
pthatcher@webrtc.org0ba15332015-01-10 00:47:0276 private:
henrike@webrtc.org269fb4b2014-10-28 22:20:1177 typedef std::map<std::string, StunRequest*> RequestMap;
78
79 rtc::Thread* thread_;
80 RequestMap requests_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:0281 std::string origin_;
henrike@webrtc.org269fb4b2014-10-28 22:20:1182
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.
88class StunRequest : public rtc::MessageHandler {
pthatcher@webrtc.org0ba15332015-01-10 00:47:0289 public:
henrike@webrtc.org269fb4b2014-10-28 22:20:1190 StunRequest();
Steve Anton6c38cc72017-11-29 18:25:5891 explicit StunRequest(StunMessage* request);
Steve Antonf2737d22017-10-31 23:27:3492 ~StunRequest() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:1193
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 Stein92c42892018-11-28 19:38:52103 // 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.org0ba15332015-01-10 00:47:02108 // the origin value
109 const std::string& origin() const { return origin_; }
110 void set_origin(const std::string& origin) { origin_ = origin; }
111
henrike@webrtc.org269fb4b2014-10-28 22:20:11112 // 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 Orelandbdcee282017-10-10 12:01:40118 // Returns a mutable pointer to |msg_|.
119 StunMessage* mutable_msg();
120
henrike@webrtc.org269fb4b2014-10-28 22:20:11121 // Time elapsed since last send (in ms)
honghaiz34b11eb2016-03-16 15:55:44122 int Elapsed() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11123
pthatcher@webrtc.org0ba15332015-01-10 00:47:02124 protected:
henrike@webrtc.org269fb4b2014-10-28 22:20:11125 int count_;
126 bool timeout_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02127 std::string origin_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11128
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 Thatcher1cf6f812015-05-15 17:40:45137 // Called when the message is sent.
138 virtual void OnSent();
139 // Returns the next delay for resends.
140 virtual int resend_delay();
henrike@webrtc.org269fb4b2014-10-28 22:20:11141
pthatcher@webrtc.org0ba15332015-01-10 00:47:02142 private:
henrike@webrtc.org269fb4b2014-10-28 22:20:11143 void set_manager(StunRequestManager* manager);
144
145 // Handles messages for sending and timeout.
Steve Antonf2737d22017-10-31 23:27:34146 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11147
148 StunRequestManager* manager_;
149 StunMessage* msg_;
honghaiz34b11eb2016-03-16 15:55:44150 int64_t tstamp_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11151
152 friend class StunRequestManager;
153};
154
155} // namespace cricket
156
Steve Anton10542f22019-01-11 17:11:00157#endif // P2P_BASE_STUN_REQUEST_H_