blob: 15ea0c73b734adeaa678a763a54982ae23e68f4d [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"
17#include "webrtc/base/sigslot.h"
18#include "webrtc/base/thread.h"
19
20namespace cricket {
21
22class StunRequest;
23
24// Manages a set of STUN requests, sending and resending until we receive a
25// response or determine that the request has timed out.
26class StunRequestManager {
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000027 public:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000028 StunRequestManager(rtc::Thread* thread);
29 ~StunRequestManager();
30
31 // Starts sending the given request (perhaps after a delay).
32 void Send(StunRequest* request);
33 void SendDelayed(StunRequest* request, int delay);
34
Honghai Zhang85975432015-11-12 11:07:12 -080035 // Sends all pending requests right away. Only for testing.
36 void Flush();
37
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000038 // Removes a stun request that was added previously. This will happen
39 // automatically when a request succeeds, fails, or times out.
40 void Remove(StunRequest* request);
41
42 // Removes all stun requests that were added previously.
43 void Clear();
44
45 // Determines whether the given message is a response to one of the
46 // outstanding requests, and if so, processes it appropriately.
47 bool CheckResponse(StunMessage* msg);
48 bool CheckResponse(const char* data, size_t size);
49
50 bool empty() { return requests_.empty(); }
51
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000052 // Set the Origin header for outgoing stun messages.
53 void set_origin(const std::string& origin) { origin_ = origin; }
54
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000055 // Raised when there are bytes to be sent.
56 sigslot::signal3<const void*, size_t, StunRequest*> SignalSendPacket;
57
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000058 private:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000059 typedef std::map<std::string, StunRequest*> RequestMap;
60
61 rtc::Thread* thread_;
62 RequestMap requests_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000063 std::string origin_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000064
65 friend class StunRequest;
66};
67
68// Represents an individual request to be sent. The STUN message can either be
69// constructed beforehand or built on demand.
70class StunRequest : public rtc::MessageHandler {
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000071 public:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000072 StunRequest();
73 StunRequest(StunMessage* request);
74 virtual ~StunRequest();
75
76 // Causes our wrapped StunMessage to be Prepared
77 void Construct();
78
79 // The manager handling this request (if it has been scheduled for sending).
80 StunRequestManager* manager() { return manager_; }
81
82 // Returns the transaction ID of this request.
83 const std::string& id() { return msg_->transaction_id(); }
84
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000085 // the origin value
86 const std::string& origin() const { return origin_; }
87 void set_origin(const std::string& origin) { origin_ = origin; }
88
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000089 // Returns the STUN type of the request message.
90 int type();
91
92 // Returns a const pointer to |msg_|.
93 const StunMessage* msg() const;
94
95 // Time elapsed since last send (in ms)
Peter Boström0c4e06b2015-10-07 12:23:21 +020096 uint32_t Elapsed() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000097
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +000098 protected:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000099 int count_;
100 bool timeout_;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000101 std::string origin_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000102
103 // Fills in a request object to be sent. Note that request's transaction ID
104 // will already be set and cannot be changed.
105 virtual void Prepare(StunMessage* request) {}
106
107 // Called when the message receives a response or times out.
108 virtual void OnResponse(StunMessage* response) {}
109 virtual void OnErrorResponse(StunMessage* response) {}
110 virtual void OnTimeout() {}
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700111 // Called when the message is sent.
112 virtual void OnSent();
113 // Returns the next delay for resends.
114 virtual int resend_delay();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000115
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000116 private:
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000117 void set_manager(StunRequestManager* manager);
118
119 // Handles messages for sending and timeout.
120 void OnMessage(rtc::Message* pmsg);
121
122 StunRequestManager* manager_;
123 StunMessage* msg_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200124 uint32_t tstamp_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000125
126 friend class StunRequestManager;
127};
128
129} // namespace cricket
130
131#endif // WEBRTC_P2P_BASE_STUNREQUEST_H_