blob: a1c69306885fcc7f0affda58adc599218ebb93d3 [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
11#ifndef WEBRTC_P2P_BASE_STUNREQUEST_H_
12#define WEBRTC_P2P_BASE_STUNREQUEST_H_
13
14#include <map>
15#include <string>
16#include "webrtc/p2p/base/stun.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020017#include "webrtc/rtc_base/sigslot.h"
18#include "webrtc/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:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000035 StunRequestManager(rtc::Thread* thread);
36 ~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();
86 StunRequest(StunMessage* request);
87 virtual ~StunRequest();
88
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
108 // Time elapsed since last send (in ms)
honghaiz34b11eb2016-03-16 08:55:44 -0700109 int Elapsed() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000110
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000111 protected:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000112 int count_;
113 bool timeout_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000114 std::string origin_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000115
116 // Fills in a request object to be sent. Note that request's transaction ID
117 // will already be set and cannot be changed.
118 virtual void Prepare(StunMessage* request) {}
119
120 // Called when the message receives a response or times out.
121 virtual void OnResponse(StunMessage* response) {}
122 virtual void OnErrorResponse(StunMessage* response) {}
123 virtual void OnTimeout() {}
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700124 // Called when the message is sent.
125 virtual void OnSent();
126 // Returns the next delay for resends.
127 virtual int resend_delay();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000128
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000129 private:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000130 void set_manager(StunRequestManager* manager);
131
132 // Handles messages for sending and timeout.
133 void OnMessage(rtc::Message* pmsg);
134
135 StunRequestManager* manager_;
136 StunMessage* msg_;
honghaiz34b11eb2016-03-16 08:55:44 -0700137 int64_t tstamp_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000138
139 friend class StunRequestManager;
140};
141
142} // namespace cricket
143
144#endif // WEBRTC_P2P_BASE_STUNREQUEST_H_