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 #include "talk/p2p/client/basicportallocator.h" 35 36 namespace talk_base { 37 class AsyncHttpRequest; 38 class SignalThread; 39 } 40 41 namespace cricket { 42 43 class HttpPortAllocatorBase : public BasicPortAllocator { 44 public: 45 // The number of HTTP requests we should attempt before giving up. 46 static const int kNumRetries; 47 48 // Records the URL that we will GET in order to create a session. 49 static const char kCreateSessionURL[]; 50 51 HttpPortAllocatorBase(talk_base::NetworkManager* network_manager, 52 const std::string& user_agent); 53 HttpPortAllocatorBase(talk_base::NetworkManager* network_manager, 54 talk_base::PacketSocketFactory* socket_factory, 55 const std::string& user_agent); 56 virtual ~HttpPortAllocatorBase(); 57 58 // CreateSession is defined in BasicPortAllocator but is 59 // redefined here as pure virtual. 60 virtual PortAllocatorSession* CreateSession( 61 const std::string& name, 62 const std::string& session_type) = 0; 63 SetStunHosts(const std::vector<talk_base::SocketAddress> & hosts)64 void SetStunHosts(const std::vector<talk_base::SocketAddress>& hosts) { 65 if (!hosts.empty()) { 66 stun_hosts_ = hosts; 67 } 68 } SetRelayHosts(const std::vector<std::string> & hosts)69 void SetRelayHosts(const std::vector<std::string>& hosts) { 70 if (!hosts.empty()) { 71 relay_hosts_ = hosts; 72 } 73 } SetRelayToken(const std::string & relay)74 void SetRelayToken(const std::string& relay) { relay_token_ = relay; } 75 stun_hosts()76 const std::vector<talk_base::SocketAddress>& stun_hosts() const { 77 return stun_hosts_; 78 } 79 relay_hosts()80 const std::vector<std::string>& relay_hosts() const { 81 return relay_hosts_; 82 } 83 relay_token()84 const std::string& relay_token() const { 85 return relay_token_; 86 } 87 user_agent()88 const std::string& user_agent() const { 89 return agent_; 90 } 91 92 private: 93 std::vector<talk_base::SocketAddress> stun_hosts_; 94 std::vector<std::string> relay_hosts_; 95 std::string relay_token_; 96 std::string agent_; 97 }; 98 99 class RequestData; 100 101 class HttpPortAllocatorSessionBase : public BasicPortAllocatorSession { 102 public: 103 HttpPortAllocatorSessionBase( 104 HttpPortAllocatorBase* allocator, 105 const std::string& name, 106 const std::string& session_type, 107 const std::vector<talk_base::SocketAddress>& stun_hosts, 108 const std::vector<std::string>& relay_hosts, 109 const std::string& relay, 110 const std::string& agent); 111 virtual ~HttpPortAllocatorSessionBase(); 112 relay_token()113 const std::string& relay_token() const { 114 return relay_token_; 115 } 116 user_agent()117 const std::string& user_agent() const { 118 return agent_; 119 } 120 121 virtual void SendSessionRequest(const std::string& host, int port) = 0; 122 virtual void ReceiveSessionResponse(const std::string& response); 123 124 protected: 125 virtual void GetPortConfigurations(); 126 void TryCreateRelaySession(); allocator()127 virtual HttpPortAllocatorBase* allocator() { 128 return static_cast<HttpPortAllocatorBase*>( 129 BasicPortAllocatorSession::allocator()); 130 } 131 132 private: 133 std::vector<std::string> relay_hosts_; 134 std::vector<talk_base::SocketAddress> stun_hosts_; 135 std::string relay_token_; 136 std::string agent_; 137 int attempts_; 138 }; 139 140 class HttpPortAllocator : public HttpPortAllocatorBase { 141 public: 142 HttpPortAllocator(talk_base::NetworkManager* network_manager, 143 const std::string& user_agent); 144 HttpPortAllocator(talk_base::NetworkManager* network_manager, 145 talk_base::PacketSocketFactory* socket_factory, 146 const std::string& user_agent); 147 virtual ~HttpPortAllocator(); 148 virtual PortAllocatorSession* CreateSession(const std::string& name, 149 const std::string& session_type); 150 }; 151 152 class HttpPortAllocatorSession : public HttpPortAllocatorSessionBase { 153 public: 154 HttpPortAllocatorSession( 155 HttpPortAllocator* allocator, 156 const std::string& name, 157 const std::string& session_type, 158 const std::vector<talk_base::SocketAddress>& stun_hosts, 159 const std::vector<std::string>& relay_hosts, 160 const std::string& relay, 161 const std::string& agent); 162 virtual ~HttpPortAllocatorSession(); 163 164 virtual void SendSessionRequest(const std::string& host, int port); 165 166 protected: 167 // Protected for diagnostics. 168 virtual void OnRequestDone(talk_base::SignalThread* request); 169 170 private: 171 std::list<talk_base::AsyncHttpRequest*> requests_; 172 }; 173 174 } // namespace cricket 175 176 #endif // TALK_P2P_CLIENT_HTTPPORTALLOCATOR_H_ 177