henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 20 | namespace cricket { |
| 21 | |
| 22 | class 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. |
| 26 | class StunRequestManager { |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 27 | public: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 28 | 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 Zhang | 8597543 | 2015-11-12 11:07:12 -0800 | [diff] [blame] | 35 | // Sends all pending requests right away. Only for testing. |
| 36 | void Flush(); |
| 37 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 38 | // 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.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 52 | // Set the Origin header for outgoing stun messages. |
| 53 | void set_origin(const std::string& origin) { origin_ = origin; } |
| 54 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 55 | // Raised when there are bytes to be sent. |
| 56 | sigslot::signal3<const void*, size_t, StunRequest*> SignalSendPacket; |
| 57 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 58 | private: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 59 | typedef std::map<std::string, StunRequest*> RequestMap; |
| 60 | |
| 61 | rtc::Thread* thread_; |
| 62 | RequestMap requests_; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 63 | std::string origin_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 64 | |
| 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. |
| 70 | class StunRequest : public rtc::MessageHandler { |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 71 | public: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 72 | 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.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 85 | // the origin value |
| 86 | const std::string& origin() const { return origin_; } |
| 87 | void set_origin(const std::string& origin) { origin_ = origin; } |
| 88 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 89 | // 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öm | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 96 | uint32_t Elapsed() const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 97 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 98 | protected: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 99 | int count_; |
| 100 | bool timeout_; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 101 | std::string origin_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 102 | |
| 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 Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 111 | // Called when the message is sent. |
| 112 | virtual void OnSent(); |
| 113 | // Returns the next delay for resends. |
| 114 | virtual int resend_delay(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 115 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 116 | private: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 117 | 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öm | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 124 | uint32_t tstamp_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 125 | |
| 126 | friend class StunRequestManager; |
| 127 | }; |
| 128 | |
| 129 | } // namespace cricket |
| 130 | |
| 131 | #endif // WEBRTC_P2P_BASE_STUNREQUEST_H_ |