blob: 99173af9d10e64382d16b090ad1a0979a4983423 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef P2P_BASE_STUNREQUEST_H_
12#define P2P_BASE_STUNREQUEST_H_
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000013
14#include <map>
15#include <string>
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "p2p/base/stun.h"
17#include "rtc_base/sigslot.h"
18#include "rtc_base/thread.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000019
20namespace cricket {
21
22class StunRequest;
23
honghaiz6b9ab922016-01-05 09:06:12 -080024const int kAllRequests = 0;
25
pthatcher94a2f212017-02-08 14:42:22 -080026// Total max timeouts: 39.75 seconds
27// For years, this was 9.5 seconds, but for networks that experience moments of
28// high RTT (such as 40s on 2G networks), this doesn't work well.
29const int STUN_TOTAL_TIMEOUT = 39750; // milliseconds
30
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000031// Manages a set of STUN requests, sending and resending until we receive a
32// response or determine that the request has timed out.
33class StunRequestManager {
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000034 public:
Steve Anton6c38cc72017-11-29 10:25:58 -080035 explicit StunRequestManager(rtc::Thread* thread);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000036 ~StunRequestManager();
37
38 // Starts sending the given request (perhaps after a delay).
39 void Send(StunRequest* request);
40 void SendDelayed(StunRequest* request, int delay);
41
honghaiz6b9ab922016-01-05 09:06:12 -080042 // If |msg_type| is kAllRequests, sends all pending requests right away.
43 // Otherwise, sends those that have a matching type right away.
44 // Only for testing.
45 void Flush(int msg_type);
Honghai Zhang85975432015-11-12 11:07:12 -080046
honghaize2af9ef2016-03-03 08:27:47 -080047 // Returns true if at least one request with |msg_type| is scheduled for
48 // transmission. For testing only.
49 bool HasRequest(int msg_type);
50
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000051 // Removes a stun request that was added previously. This will happen
52 // automatically when a request succeeds, fails, or times out.
53 void Remove(StunRequest* request);
54
55 // Removes all stun requests that were added previously.
56 void Clear();
57
58 // Determines whether the given message is a response to one of the
59 // outstanding requests, and if so, processes it appropriately.
60 bool CheckResponse(StunMessage* msg);
61 bool CheckResponse(const char* data, size_t size);
62
63 bool empty() { return requests_.empty(); }
64
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000065 // Set the Origin header for outgoing stun messages.
66 void set_origin(const std::string& origin) { origin_ = origin; }
67
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000068 // Raised when there are bytes to be sent.
69 sigslot::signal3<const void*, size_t, StunRequest*> SignalSendPacket;
70
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000071 private:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000072 typedef std::map<std::string, StunRequest*> RequestMap;
73
74 rtc::Thread* thread_;
75 RequestMap requests_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000076 std::string origin_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000077
78 friend class StunRequest;
79};
80
81// Represents an individual request to be sent. The STUN message can either be
82// constructed beforehand or built on demand.
83class StunRequest : public rtc::MessageHandler {
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000084 public:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000085 StunRequest();
Steve Anton6c38cc72017-11-29 10:25:58 -080086 explicit StunRequest(StunMessage* request);
Steve Antonf2737d22017-10-31 16:27:34 -070087 ~StunRequest() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000088
89 // Causes our wrapped StunMessage to be Prepared
90 void Construct();
91
92 // The manager handling this request (if it has been scheduled for sending).
93 StunRequestManager* manager() { return manager_; }
94
95 // Returns the transaction ID of this request.
96 const std::string& id() { return msg_->transaction_id(); }
97
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000098 // the origin value
99 const std::string& origin() const { return origin_; }
100 void set_origin(const std::string& origin) { origin_ = origin; }
101
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000102 // Returns the STUN type of the request message.
103 int type();
104
105 // Returns a const pointer to |msg_|.
106 const StunMessage* msg() const;
107
Jonas Orelandbdcee282017-10-10 14:01:40 +0200108 // Returns a mutable pointer to |msg_|.
109 StunMessage* mutable_msg();
110
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000111 // Time elapsed since last send (in ms)
honghaiz34b11eb2016-03-16 08:55:44 -0700112 int Elapsed() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000113
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000114 protected:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000115 int count_;
116 bool timeout_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000117 std::string origin_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000118
119 // Fills in a request object to be sent. Note that request's transaction ID
120 // will already be set and cannot be changed.
121 virtual void Prepare(StunMessage* request) {}
122
123 // Called when the message receives a response or times out.
124 virtual void OnResponse(StunMessage* response) {}
125 virtual void OnErrorResponse(StunMessage* response) {}
126 virtual void OnTimeout() {}
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700127 // Called when the message is sent.
128 virtual void OnSent();
129 // Returns the next delay for resends.
130 virtual int resend_delay();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000131
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000132 private:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000133 void set_manager(StunRequestManager* manager);
134
135 // Handles messages for sending and timeout.
Steve Antonf2737d22017-10-31 16:27:34 -0700136 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000137
138 StunRequestManager* manager_;
139 StunMessage* msg_;
honghaiz34b11eb2016-03-16 08:55:44 -0700140 int64_t tstamp_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000141
142 friend class StunRequestManager;
143};
144
145} // namespace cricket
146
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200147#endif // P2P_BASE_STUNREQUEST_H_