blob: af2e14e7977d02c5ae6cfd81f5a3c737ab48fa03 [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
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000017#include <map>
18#include <string>
Yves Gerey3e707812018-11-28 16:47:49 +010019
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "p2p/base/stun.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/message_handler.h"
22#include "rtc_base/message_queue.h"
Artem Titove41c4332018-07-25 15:04:28 +020023#include "rtc_base/third_party/sigslot/sigslot.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:
Steve Anton6c38cc72017-11-29 10:25:58 -080041 explicit StunRequestManager(rtc::Thread* thread);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000042 ~StunRequestManager();
43
44 // Starts sending the given request (perhaps after a delay).
45 void Send(StunRequest* request);
46 void SendDelayed(StunRequest* request, int delay);
47
honghaiz6b9ab922016-01-05 09:06:12 -080048 // If |msg_type| is kAllRequests, sends all pending requests right away.
49 // Otherwise, sends those that have a matching type right away.
50 // Only for testing.
51 void Flush(int msg_type);
Honghai Zhang85975432015-11-12 11:07:12 -080052
honghaize2af9ef2016-03-03 08:27:47 -080053 // Returns true if at least one request with |msg_type| is scheduled for
54 // transmission. For testing only.
55 bool HasRequest(int msg_type);
56
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000057 // Removes a stun request that was added previously. This will happen
58 // automatically when a request succeeds, fails, or times out.
59 void Remove(StunRequest* request);
60
61 // Removes all stun requests that were added previously.
62 void Clear();
63
64 // Determines whether the given message is a response to one of the
65 // outstanding requests, and if so, processes it appropriately.
66 bool CheckResponse(StunMessage* msg);
67 bool CheckResponse(const char* data, size_t size);
68
69 bool empty() { return requests_.empty(); }
70
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000071 // Set the Origin header for outgoing stun messages.
72 void set_origin(const std::string& origin) { origin_ = origin; }
73
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000074 // Raised when there are bytes to be sent.
75 sigslot::signal3<const void*, size_t, StunRequest*> SignalSendPacket;
76
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000077 private:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000078 typedef std::map<std::string, StunRequest*> RequestMap;
79
80 rtc::Thread* thread_;
81 RequestMap requests_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000082 std::string origin_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000083
84 friend class StunRequest;
85};
86
87// Represents an individual request to be sent. The STUN message can either be
88// constructed beforehand or built on demand.
89class StunRequest : public rtc::MessageHandler {
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000090 public:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000091 StunRequest();
Steve Anton6c38cc72017-11-29 10:25:58 -080092 explicit StunRequest(StunMessage* request);
Steve Antonf2737d22017-10-31 16:27:34 -070093 ~StunRequest() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000094
95 // Causes our wrapped StunMessage to be Prepared
96 void Construct();
97
98 // The manager handling this request (if it has been scheduled for sending).
99 StunRequestManager* manager() { return manager_; }
100
101 // Returns the transaction ID of this request.
102 const std::string& id() { return msg_->transaction_id(); }
103
Zach Stein92c42892018-11-28 11:38:52 -0800104 // Returns the reduced transaction ID of this request.
105 uint32_t reduced_transaction_id() const {
106 return msg_->reduced_transaction_id();
107 }
108
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000109 // the origin value
110 const std::string& origin() const { return origin_; }
111 void set_origin(const std::string& origin) { origin_ = origin; }
112
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000113 // Returns the STUN type of the request message.
114 int type();
115
116 // Returns a const pointer to |msg_|.
117 const StunMessage* msg() const;
118
Jonas Orelandbdcee282017-10-10 14:01:40 +0200119 // Returns a mutable pointer to |msg_|.
120 StunMessage* mutable_msg();
121
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000122 // Time elapsed since last send (in ms)
honghaiz34b11eb2016-03-16 08:55:44 -0700123 int Elapsed() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000124
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000125 protected:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000126 int count_;
127 bool timeout_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000128 std::string origin_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000129
130 // Fills in a request object to be sent. Note that request's transaction ID
131 // will already be set and cannot be changed.
132 virtual void Prepare(StunMessage* request) {}
133
134 // Called when the message receives a response or times out.
135 virtual void OnResponse(StunMessage* response) {}
136 virtual void OnErrorResponse(StunMessage* response) {}
137 virtual void OnTimeout() {}
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700138 // Called when the message is sent.
139 virtual void OnSent();
140 // Returns the next delay for resends.
141 virtual int resend_delay();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000142
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000143 private:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000144 void set_manager(StunRequestManager* manager);
145
146 // Handles messages for sending and timeout.
Steve Antonf2737d22017-10-31 16:27:34 -0700147 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000148
149 StunRequestManager* manager_;
150 StunMessage* msg_;
honghaiz34b11eb2016-03-16 08:55:44 -0700151 int64_t tstamp_;
Bjorn Terelius0d289722019-02-01 15:22:20 +0100152 bool in_rfc5389_retransmission_experiment_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000153
154 friend class StunRequestManager;
155};
156
157} // namespace cricket
158
Steve Anton10542f22019-01-11 09:11:00 -0800159#endif // P2P_BASE_STUN_REQUEST_H_