1 /* 2 * libjingle 3 * Copyright 2004--2008, Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #ifndef TALK_P2P_CLIENT_HTTPPORTALLOCATOR_H_ 29 #define TALK_P2P_CLIENT_HTTPPORTALLOCATOR_H_ 30 31 #include <list> 32 #include <string> 33 #include <vector> 34 35 #include "talk/p2p/client/basicportallocator.h" 36 37 class HttpPortAllocatorTest_TestSessionRequestUrl_Test; 38 39 namespace rtc { 40 class AsyncHttpRequest; 41 class SignalThread; 42 } 43 44 namespace cricket { 45 46 class HttpPortAllocatorBase : public BasicPortAllocator { 47 public: 48 // The number of HTTP requests we should attempt before giving up. 49 static const int kNumRetries; 50 51 // Records the URL that we will GET in order to create a session. 52 static const char kCreateSessionURL[]; 53 54 HttpPortAllocatorBase(rtc::NetworkManager* network_manager, 55 const std::string& user_agent); 56 HttpPortAllocatorBase(rtc::NetworkManager* network_manager, 57 rtc::PacketSocketFactory* socket_factory, 58 const std::string& user_agent); 59 virtual ~HttpPortAllocatorBase(); 60 61 // CreateSession is defined in BasicPortAllocator but is 62 // redefined here as pure virtual. 63 virtual PortAllocatorSession* CreateSessionInternal( 64 const std::string& content_name, 65 int component, 66 const std::string& ice_ufrag, 67 const std::string& ice_pwd) = 0; 68 SetStunHosts(const std::vector<rtc::SocketAddress> & hosts)69 void SetStunHosts(const std::vector<rtc::SocketAddress>& hosts) { 70 if (!hosts.empty()) { 71 stun_hosts_ = hosts; 72 } 73 } SetRelayHosts(const std::vector<std::string> & hosts)74 void SetRelayHosts(const std::vector<std::string>& hosts) { 75 if (!hosts.empty()) { 76 relay_hosts_ = hosts; 77 } 78 } SetRelayToken(const std::string & relay)79 void SetRelayToken(const std::string& relay) { relay_token_ = relay; } 80 stun_hosts()81 const std::vector<rtc::SocketAddress>& stun_hosts() const { 82 return stun_hosts_; 83 } 84 relay_hosts()85 const std::vector<std::string>& relay_hosts() const { 86 return relay_hosts_; 87 } 88 relay_token()89 const std::string& relay_token() const { 90 return relay_token_; 91 } 92 user_agent()93 const std::string& user_agent() const { 94 return agent_; 95 } 96 97 private: 98 std::vector<rtc::SocketAddress> stun_hosts_; 99 std::vector<std::string> relay_hosts_; 100 std::string relay_token_; 101 std::string agent_; 102 }; 103 104 class RequestData; 105 106 class HttpPortAllocatorSessionBase : public BasicPortAllocatorSession { 107 public: 108 HttpPortAllocatorSessionBase( 109 HttpPortAllocatorBase* allocator, 110 const std::string& content_name, 111 int component, 112 const std::string& ice_ufrag, 113 const std::string& ice_pwd, 114 const std::vector<rtc::SocketAddress>& stun_hosts, 115 const std::vector<std::string>& relay_hosts, 116 const std::string& relay, 117 const std::string& agent); 118 virtual ~HttpPortAllocatorSessionBase(); 119 relay_token()120 const std::string& relay_token() const { 121 return relay_token_; 122 } 123 user_agent()124 const std::string& user_agent() const { 125 return agent_; 126 } 127 128 virtual void SendSessionRequest(const std::string& host, int port) = 0; 129 virtual void ReceiveSessionResponse(const std::string& response); 130 131 // Made public for testing. Should be protected. 132 std::string GetSessionRequestUrl(); 133 134 protected: 135 virtual void GetPortConfigurations(); 136 void TryCreateRelaySession(); allocator()137 virtual HttpPortAllocatorBase* allocator() { 138 return static_cast<HttpPortAllocatorBase*>( 139 BasicPortAllocatorSession::allocator()); 140 } 141 142 private: 143 std::vector<std::string> relay_hosts_; 144 std::vector<rtc::SocketAddress> stun_hosts_; 145 std::string relay_token_; 146 std::string agent_; 147 int attempts_; 148 }; 149 150 class HttpPortAllocator : public HttpPortAllocatorBase { 151 public: 152 HttpPortAllocator(rtc::NetworkManager* network_manager, 153 const std::string& user_agent); 154 HttpPortAllocator(rtc::NetworkManager* network_manager, 155 rtc::PacketSocketFactory* socket_factory, 156 const std::string& user_agent); 157 virtual ~HttpPortAllocator(); 158 virtual PortAllocatorSession* CreateSessionInternal( 159 const std::string& content_name, 160 int component, 161 const std::string& ice_ufrag, const std::string& ice_pwd); 162 }; 163 164 class HttpPortAllocatorSession : public HttpPortAllocatorSessionBase { 165 public: 166 HttpPortAllocatorSession( 167 HttpPortAllocator* allocator, 168 const std::string& content_name, 169 int component, 170 const std::string& ice_ufrag, 171 const std::string& ice_pwd, 172 const std::vector<rtc::SocketAddress>& stun_hosts, 173 const std::vector<std::string>& relay_hosts, 174 const std::string& relay, 175 const std::string& agent); 176 virtual ~HttpPortAllocatorSession(); 177 178 virtual void SendSessionRequest(const std::string& host, int port); 179 180 protected: 181 // Protected for diagnostics. 182 virtual void OnRequestDone(rtc::SignalThread* request); 183 184 private: 185 std::list<rtc::AsyncHttpRequest*> requests_; 186 }; 187 188 } // namespace cricket 189 190 #endif // TALK_P2P_CLIENT_HTTPPORTALLOCATOR_H_ 191