blob: 7994fb67092a5e329e8d20baa0f41538d805c6e7 [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>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000016#include <map>
17#include <string>
Yves Gerey3e707812018-11-28 16:47:49 +010018
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "p2p/base/stun.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/message_handler.h"
21#include "rtc_base/message_queue.h"
Artem Titove41c4332018-07-25 15:04:28 +020022#include "rtc_base/third_party/sigslot/sigslot.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/thread.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000024
25namespace cricket {
26
27class StunRequest;
28
honghaiz6b9ab922016-01-05 09:06:12 -080029const int kAllRequests = 0;
30
pthatcher94a2f212017-02-08 14:42:22 -080031// 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:11 +000036// 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:02 +000039 public:
Steve Anton6c38cc72017-11-29 10:25:58 -080040 explicit StunRequestManager(rtc::Thread* thread);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000041 ~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 09:06:12 -080047 // 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 11:07:12 -080051
honghaize2af9ef2016-03-03 08:27:47 -080052 // 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:11 +000056 // 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:02 +000070 // 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:11 +000073 // 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:02 +000076 private:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000077 typedef std::map<std::string, StunRequest*> RequestMap;
78
79 rtc::Thread* thread_;
80 RequestMap requests_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000081 std::string origin_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000082
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:02 +000089 public:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000090 StunRequest();
Steve Anton6c38cc72017-11-29 10:25:58 -080091 explicit StunRequest(StunMessage* request);
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).
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 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
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000108 // 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:11 +0000112 // 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 14:01:40 +0200118 // Returns a mutable pointer to |msg_|.
119 StunMessage* mutable_msg();
120
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000121 // Time elapsed since last send (in ms)
honghaiz34b11eb2016-03-16 08:55:44 -0700122 int Elapsed() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000123
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000124 protected:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000125 int count_;
126 bool timeout_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000127 std::string origin_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000128
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 10:40:45 -0700137 // 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:11 +0000141
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000142 private:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000143 void set_manager(StunRequestManager* manager);
144
145 // Handles messages for sending and timeout.
Steve Antonf2737d22017-10-31 16:27:34 -0700146 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000147
148 StunRequestManager* manager_;
149 StunMessage* msg_;
honghaiz34b11eb2016-03-16 08:55:44 -0700150 int64_t tstamp_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000151
152 friend class StunRequestManager;
153};
154
155} // namespace cricket
156
Steve Anton10542f22019-01-11 09:11:00 -0800157#endif // P2P_BASE_STUN_REQUEST_H_