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 | |
honghaiz | 6b9ab92 | 2016-01-05 09:06:12 -0800 | [diff] [blame] | 24 | const int kAllRequests = 0; |
| 25 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 26 | // Manages a set of STUN requests, sending and resending until we receive a |
| 27 | // response or determine that the request has timed out. |
| 28 | class StunRequestManager { |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 29 | public: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 30 | StunRequestManager(rtc::Thread* thread); |
| 31 | ~StunRequestManager(); |
| 32 | |
| 33 | // Starts sending the given request (perhaps after a delay). |
| 34 | void Send(StunRequest* request); |
| 35 | void SendDelayed(StunRequest* request, int delay); |
| 36 | |
honghaiz | 6b9ab92 | 2016-01-05 09:06:12 -0800 | [diff] [blame] | 37 | // If |msg_type| is kAllRequests, sends all pending requests right away. |
| 38 | // Otherwise, sends those that have a matching type right away. |
| 39 | // Only for testing. |
| 40 | void Flush(int msg_type); |
Honghai Zhang | 8597543 | 2015-11-12 11:07:12 -0800 | [diff] [blame] | 41 | |
honghaiz | e2af9ef | 2016-03-03 08:27:47 -0800 | [diff] [blame] | 42 | // Returns true if at least one request with |msg_type| is scheduled for |
| 43 | // transmission. For testing only. |
| 44 | bool HasRequest(int msg_type); |
| 45 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 46 | // Removes a stun request that was added previously. This will happen |
| 47 | // automatically when a request succeeds, fails, or times out. |
| 48 | void Remove(StunRequest* request); |
| 49 | |
| 50 | // Removes all stun requests that were added previously. |
| 51 | void Clear(); |
| 52 | |
| 53 | // Determines whether the given message is a response to one of the |
| 54 | // outstanding requests, and if so, processes it appropriately. |
| 55 | bool CheckResponse(StunMessage* msg); |
| 56 | bool CheckResponse(const char* data, size_t size); |
| 57 | |
| 58 | bool empty() { return requests_.empty(); } |
| 59 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 60 | // Set the Origin header for outgoing stun messages. |
| 61 | void set_origin(const std::string& origin) { origin_ = origin; } |
| 62 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 63 | // Raised when there are bytes to be sent. |
| 64 | sigslot::signal3<const void*, size_t, StunRequest*> SignalSendPacket; |
| 65 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 66 | private: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 67 | typedef std::map<std::string, StunRequest*> RequestMap; |
| 68 | |
| 69 | rtc::Thread* thread_; |
| 70 | RequestMap requests_; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 71 | std::string origin_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 72 | |
| 73 | friend class StunRequest; |
| 74 | }; |
| 75 | |
| 76 | // Represents an individual request to be sent. The STUN message can either be |
| 77 | // constructed beforehand or built on demand. |
| 78 | class StunRequest : public rtc::MessageHandler { |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 79 | public: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 80 | StunRequest(); |
| 81 | StunRequest(StunMessage* request); |
| 82 | virtual ~StunRequest(); |
| 83 | |
| 84 | // Causes our wrapped StunMessage to be Prepared |
| 85 | void Construct(); |
| 86 | |
| 87 | // The manager handling this request (if it has been scheduled for sending). |
| 88 | StunRequestManager* manager() { return manager_; } |
| 89 | |
| 90 | // Returns the transaction ID of this request. |
| 91 | const std::string& id() { return msg_->transaction_id(); } |
| 92 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 93 | // the origin value |
| 94 | const std::string& origin() const { return origin_; } |
| 95 | void set_origin(const std::string& origin) { origin_ = origin; } |
| 96 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 97 | // Returns the STUN type of the request message. |
| 98 | int type(); |
| 99 | |
| 100 | // Returns a const pointer to |msg_|. |
| 101 | const StunMessage* msg() const; |
| 102 | |
| 103 | // Time elapsed since last send (in ms) |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 104 | int Elapsed() const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 105 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 106 | protected: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 107 | int count_; |
| 108 | bool timeout_; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 109 | std::string origin_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 110 | |
| 111 | // Fills in a request object to be sent. Note that request's transaction ID |
| 112 | // will already be set and cannot be changed. |
| 113 | virtual void Prepare(StunMessage* request) {} |
| 114 | |
| 115 | // Called when the message receives a response or times out. |
| 116 | virtual void OnResponse(StunMessage* response) {} |
| 117 | virtual void OnErrorResponse(StunMessage* response) {} |
| 118 | virtual void OnTimeout() {} |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 119 | // Called when the message is sent. |
| 120 | virtual void OnSent(); |
| 121 | // Returns the next delay for resends. |
| 122 | virtual int resend_delay(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 123 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 124 | private: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 125 | void set_manager(StunRequestManager* manager); |
| 126 | |
| 127 | // Handles messages for sending and timeout. |
| 128 | void OnMessage(rtc::Message* pmsg); |
| 129 | |
| 130 | StunRequestManager* manager_; |
| 131 | StunMessage* msg_; |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 132 | int64_t tstamp_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 133 | |
| 134 | friend class StunRequestManager; |
| 135 | }; |
| 136 | |
| 137 | } // namespace cricket |
| 138 | |
| 139 | #endif // WEBRTC_P2P_BASE_STUNREQUEST_H_ |