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 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 42 | // Removes a stun request that was added previously. This will happen |
| 43 | // automatically when a request succeeds, fails, or times out. |
| 44 | void Remove(StunRequest* request); |
| 45 | |
| 46 | // Removes all stun requests that were added previously. |
| 47 | void Clear(); |
| 48 | |
| 49 | // Determines whether the given message is a response to one of the |
| 50 | // outstanding requests, and if so, processes it appropriately. |
| 51 | bool CheckResponse(StunMessage* msg); |
| 52 | bool CheckResponse(const char* data, size_t size); |
| 53 | |
| 54 | bool empty() { return requests_.empty(); } |
| 55 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 56 | // Set the Origin header for outgoing stun messages. |
| 57 | void set_origin(const std::string& origin) { origin_ = origin; } |
| 58 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 59 | // Raised when there are bytes to be sent. |
| 60 | sigslot::signal3<const void*, size_t, StunRequest*> SignalSendPacket; |
| 61 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 62 | private: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 63 | typedef std::map<std::string, StunRequest*> RequestMap; |
| 64 | |
| 65 | rtc::Thread* thread_; |
| 66 | RequestMap requests_; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 67 | std::string origin_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 68 | |
| 69 | friend class StunRequest; |
| 70 | }; |
| 71 | |
| 72 | // Represents an individual request to be sent. The STUN message can either be |
| 73 | // constructed beforehand or built on demand. |
| 74 | class StunRequest : public rtc::MessageHandler { |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 75 | public: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 76 | StunRequest(); |
| 77 | StunRequest(StunMessage* request); |
| 78 | virtual ~StunRequest(); |
| 79 | |
| 80 | // Causes our wrapped StunMessage to be Prepared |
| 81 | void Construct(); |
| 82 | |
| 83 | // The manager handling this request (if it has been scheduled for sending). |
| 84 | StunRequestManager* manager() { return manager_; } |
| 85 | |
| 86 | // Returns the transaction ID of this request. |
| 87 | const std::string& id() { return msg_->transaction_id(); } |
| 88 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 89 | // the origin value |
| 90 | const std::string& origin() const { return origin_; } |
| 91 | void set_origin(const std::string& origin) { origin_ = origin; } |
| 92 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 93 | // Returns the STUN type of the request message. |
| 94 | int type(); |
| 95 | |
| 96 | // Returns a const pointer to |msg_|. |
| 97 | const StunMessage* msg() const; |
| 98 | |
| 99 | // Time elapsed since last send (in ms) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 100 | uint32_t Elapsed() const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 101 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 102 | protected: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 103 | int count_; |
| 104 | bool timeout_; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 105 | std::string origin_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 106 | |
| 107 | // Fills in a request object to be sent. Note that request's transaction ID |
| 108 | // will already be set and cannot be changed. |
| 109 | virtual void Prepare(StunMessage* request) {} |
| 110 | |
| 111 | // Called when the message receives a response or times out. |
| 112 | virtual void OnResponse(StunMessage* response) {} |
| 113 | virtual void OnErrorResponse(StunMessage* response) {} |
| 114 | virtual void OnTimeout() {} |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 115 | // Called when the message is sent. |
| 116 | virtual void OnSent(); |
| 117 | // Returns the next delay for resends. |
| 118 | virtual int resend_delay(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 119 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 120 | private: |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 121 | void set_manager(StunRequestManager* manager); |
| 122 | |
| 123 | // Handles messages for sending and timeout. |
| 124 | void OnMessage(rtc::Message* pmsg); |
| 125 | |
| 126 | StunRequestManager* manager_; |
| 127 | StunMessage* msg_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 128 | uint32_t tstamp_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 129 | |
| 130 | friend class StunRequestManager; |
| 131 | }; |
| 132 | |
| 133 | } // namespace cricket |
| 134 | |
| 135 | #endif // WEBRTC_P2P_BASE_STUNREQUEST_H_ |