You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1163 lines
38 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, 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. #include "talk/p2p/client/basicportallocator.h"
  28. #include <string>
  29. #include <vector>
  30. #include "talk/base/basicpacketsocketfactory.h"
  31. #include "talk/base/common.h"
  32. #include "talk/base/helpers.h"
  33. #include "talk/base/host.h"
  34. #include "talk/base/logging.h"
  35. #include "talk/p2p/base/common.h"
  36. #include "talk/p2p/base/port.h"
  37. #include "talk/p2p/base/relayport.h"
  38. #include "talk/p2p/base/stunport.h"
  39. #include "talk/p2p/base/tcpport.h"
  40. #include "talk/p2p/base/turnport.h"
  41. #include "talk/p2p/base/udpport.h"
  42. using talk_base::CreateRandomId;
  43. using talk_base::CreateRandomString;
  44. namespace {
  45. const uint32 MSG_CONFIG_START = 1;
  46. const uint32 MSG_CONFIG_READY = 2;
  47. const uint32 MSG_ALLOCATE = 3;
  48. const uint32 MSG_ALLOCATION_PHASE = 4;
  49. const uint32 MSG_SHAKE = 5;
  50. const uint32 MSG_SEQUENCEOBJECTS_CREATED = 6;
  51. const uint32 MSG_CONFIG_STOP = 7;
  52. const uint32 ALLOCATE_DELAY = 250;
  53. const uint32 ALLOCATION_STEP_DELAY = 1 * 1000;
  54. const int PHASE_UDP = 0;
  55. const int PHASE_RELAY = 1;
  56. const int PHASE_TCP = 2;
  57. const int PHASE_SSLTCP = 3;
  58. const int kNumPhases = 4;
  59. // Both these values are in bytes.
  60. const int kLargeSocketSendBufferSize = 128 * 1024;
  61. const int kNormalSocketSendBufferSize = 64 * 1024;
  62. const char UDP_PROTOCOL_NAME[] = "udp";
  63. // Returns the phase in which a given local candidate (or rather, the port that
  64. // gave rise to that local candidate) would have been created.
  65. int LocalCandidateToPhase(const cricket::Candidate& candidate) {
  66. cricket::ProtocolType proto;
  67. bool result = cricket::StringToProto(candidate.protocol().c_str(), &proto);
  68. if (result) {
  69. if (candidate.type() == cricket::LOCAL_PORT_TYPE) {
  70. switch (proto) {
  71. case cricket::PROTO_UDP: return PHASE_UDP;
  72. case cricket::PROTO_TCP: return PHASE_TCP;
  73. default: ASSERT(false);
  74. }
  75. } else if (candidate.type() == cricket::STUN_PORT_TYPE) {
  76. return PHASE_UDP;
  77. } else if (candidate.type() == cricket::RELAY_PORT_TYPE) {
  78. switch (proto) {
  79. case cricket::PROTO_UDP: return PHASE_RELAY;
  80. case cricket::PROTO_TCP: return PHASE_TCP;
  81. case cricket::PROTO_SSLTCP: return PHASE_SSLTCP;
  82. default: ASSERT(false);
  83. }
  84. } else {
  85. ASSERT(false);
  86. }
  87. } else {
  88. ASSERT(false);
  89. }
  90. return PHASE_UDP; // reached only with assert failure
  91. }
  92. const int SHAKE_MIN_DELAY = 45 * 1000; // 45 seconds
  93. const int SHAKE_MAX_DELAY = 90 * 1000; // 90 seconds
  94. int ShakeDelay() {
  95. int range = SHAKE_MAX_DELAY - SHAKE_MIN_DELAY + 1;
  96. return SHAKE_MIN_DELAY + CreateRandomId() % range;
  97. }
  98. } // namespace
  99. namespace cricket {
  100. const uint32 DISABLE_ALL_PHASES =
  101. PORTALLOCATOR_DISABLE_UDP
  102. | PORTALLOCATOR_DISABLE_TCP
  103. | PORTALLOCATOR_DISABLE_STUN
  104. | PORTALLOCATOR_DISABLE_RELAY;
  105. // Performs the allocation of ports, in a sequenced (timed) manner, for a given
  106. // network and IP address.
  107. class AllocationSequence : public talk_base::MessageHandler,
  108. public sigslot::has_slots<> {
  109. public:
  110. enum State {
  111. kInit, // Initial state.
  112. kRunning, // Started allocating ports.
  113. kStopped, // Stopped from running.
  114. kCompleted, // All ports are allocated.
  115. // kInit --> kRunning --> {kCompleted|kStopped}
  116. };
  117. AllocationSequence(BasicPortAllocatorSession* session,
  118. talk_base::Network* network,
  119. PortConfiguration* config,
  120. uint32 flags);
  121. ~AllocationSequence();
  122. bool Init();
  123. State state() const { return state_; }
  124. // Disables the phases for a new sequence that this one already covers for an
  125. // equivalent network setup.
  126. void DisableEquivalentPhases(talk_base::Network* network,
  127. PortConfiguration* config, uint32* flags);
  128. // Starts and stops the sequence. When started, it will continue allocating
  129. // new ports on its own timed schedule.
  130. void Start();
  131. void Stop();
  132. // MessageHandler
  133. void OnMessage(talk_base::Message* msg);
  134. void EnableProtocol(ProtocolType proto);
  135. bool ProtocolEnabled(ProtocolType proto) const;
  136. // Signal from AllocationSequence, when it's done with allocating ports.
  137. // This signal is useful, when port allocation fails which doesn't result
  138. // in any candidates. Using this signal BasicPortAllocatorSession can send
  139. // its candidate discovery conclusion signal. Without this signal,
  140. // BasicPortAllocatorSession doesn't have any event to trigger signal. This
  141. // can also be achieved by starting timer in BPAS.
  142. sigslot::signal1<AllocationSequence*> SignalPortAllocationComplete;
  143. private:
  144. typedef std::vector<ProtocolType> ProtocolList;
  145. bool IsFlagSet(uint32 flag) {
  146. return ((flags_ & flag) != 0);
  147. }
  148. void CreateUDPPorts();
  149. void CreateTCPPorts();
  150. void CreateStunPorts();
  151. void CreateRelayPorts();
  152. void CreateGturnPort(const RelayServerConfig& config);
  153. void CreateTurnPort(const RelayServerConfig& config);
  154. void OnReadPacket(talk_base::AsyncPacketSocket* socket,
  155. const char* data, size_t size,
  156. const talk_base::SocketAddress& remote_addr);
  157. void OnPortDestroyed(PortInterface* port);
  158. BasicPortAllocatorSession* session_;
  159. talk_base::Network* network_;
  160. talk_base::IPAddress ip_;
  161. PortConfiguration* config_;
  162. State state_;
  163. int step_;
  164. int step_of_phase_[kNumPhases];
  165. uint32 flags_;
  166. ProtocolList protocols_;
  167. talk_base::scoped_ptr<talk_base::AsyncPacketSocket> udp_socket_;
  168. // Keeping a list of all UDP based ports.
  169. std::deque<Port*> ports;
  170. };
  171. // BasicPortAllocator
  172. BasicPortAllocator::BasicPortAllocator(
  173. talk_base::NetworkManager* network_manager,
  174. talk_base::PacketSocketFactory* socket_factory)
  175. : network_manager_(network_manager),
  176. socket_factory_(socket_factory) {
  177. ASSERT(socket_factory_ != NULL);
  178. Construct();
  179. }
  180. BasicPortAllocator::BasicPortAllocator(
  181. talk_base::NetworkManager* network_manager)
  182. : network_manager_(network_manager),
  183. socket_factory_(NULL) {
  184. Construct();
  185. }
  186. BasicPortAllocator::BasicPortAllocator(
  187. talk_base::NetworkManager* network_manager,
  188. talk_base::PacketSocketFactory* socket_factory,
  189. const talk_base::SocketAddress& stun_address)
  190. : network_manager_(network_manager),
  191. socket_factory_(socket_factory),
  192. stun_address_(stun_address) {
  193. ASSERT(socket_factory_ != NULL);
  194. Construct();
  195. }
  196. BasicPortAllocator::BasicPortAllocator(
  197. talk_base::NetworkManager* network_manager,
  198. const talk_base::SocketAddress& stun_address,
  199. const talk_base::SocketAddress& relay_address_udp,
  200. const talk_base::SocketAddress& relay_address_tcp,
  201. const talk_base::SocketAddress& relay_address_ssl)
  202. : network_manager_(network_manager),
  203. socket_factory_(NULL),
  204. stun_address_(stun_address) {
  205. RelayServerConfig config(RELAY_GTURN);
  206. if (!relay_address_udp.IsAny())
  207. config.ports.push_back(ProtocolAddress(relay_address_udp, PROTO_UDP));
  208. if (!relay_address_tcp.IsAny())
  209. config.ports.push_back(ProtocolAddress(relay_address_tcp, PROTO_TCP));
  210. if (!relay_address_ssl.IsAny())
  211. config.ports.push_back(ProtocolAddress(relay_address_ssl, PROTO_SSLTCP));
  212. AddRelay(config);
  213. Construct();
  214. }
  215. void BasicPortAllocator::Construct() {
  216. best_writable_phase_ = -1;
  217. allow_tcp_listen_ = true;
  218. }
  219. BasicPortAllocator::~BasicPortAllocator() {
  220. }
  221. int BasicPortAllocator::best_writable_phase() const {
  222. // If we are configured with an HTTP proxy, the best bet is to use the relay
  223. if ((best_writable_phase_ == -1)
  224. && ((proxy().type == talk_base::PROXY_HTTPS)
  225. || (proxy().type == talk_base::PROXY_UNKNOWN))) {
  226. return PHASE_RELAY;
  227. }
  228. return best_writable_phase_;
  229. }
  230. PortAllocatorSession *BasicPortAllocator::CreateSessionInternal(
  231. const std::string& content_name, int component,
  232. const std::string& ice_ufrag, const std::string& ice_pwd) {
  233. return new BasicPortAllocatorSession(this, content_name, component,
  234. ice_ufrag, ice_pwd);
  235. }
  236. void BasicPortAllocator::AddWritablePhase(int phase) {
  237. if ((best_writable_phase_ == -1) || (phase < best_writable_phase_))
  238. best_writable_phase_ = phase;
  239. }
  240. // BasicPortAllocatorSession
  241. BasicPortAllocatorSession::BasicPortAllocatorSession(
  242. BasicPortAllocator *allocator,
  243. const std::string& content_name,
  244. int component,
  245. const std::string& ice_ufrag,
  246. const std::string& ice_pwd)
  247. : PortAllocatorSession(content_name, component,
  248. ice_ufrag, ice_pwd, allocator->flags()),
  249. allocator_(allocator), network_thread_(NULL),
  250. socket_factory_(allocator->socket_factory()),
  251. configuration_done_(false),
  252. allocation_started_(false),
  253. network_manager_started_(false),
  254. running_(false),
  255. allocation_sequences_created_(false) {
  256. allocator_->network_manager()->SignalNetworksChanged.connect(
  257. this, &BasicPortAllocatorSession::OnNetworksChanged);
  258. allocator_->network_manager()->StartUpdating();
  259. }
  260. BasicPortAllocatorSession::~BasicPortAllocatorSession() {
  261. allocator_->network_manager()->StopUpdating();
  262. if (network_thread_ != NULL)
  263. network_thread_->Clear(this);
  264. std::vector<PortData>::iterator it;
  265. for (it = ports_.begin(); it != ports_.end(); it++)
  266. delete it->port();
  267. for (uint32 i = 0; i < configs_.size(); ++i)
  268. delete configs_[i];
  269. for (uint32 i = 0; i < sequences_.size(); ++i)
  270. delete sequences_[i];
  271. }
  272. void BasicPortAllocatorSession::GetInitialPorts() {
  273. network_thread_ = talk_base::Thread::Current();
  274. if (!socket_factory_) {
  275. owned_socket_factory_.reset(
  276. new talk_base::BasicPacketSocketFactory(network_thread_));
  277. socket_factory_ = owned_socket_factory_.get();
  278. }
  279. network_thread_->Post(this, MSG_CONFIG_START);
  280. if (flags() & PORTALLOCATOR_ENABLE_SHAKER)
  281. network_thread_->PostDelayed(ShakeDelay(), this, MSG_SHAKE);
  282. }
  283. void BasicPortAllocatorSession::StartGetAllPorts() {
  284. ASSERT(talk_base::Thread::Current() == network_thread_);
  285. running_ = true;
  286. if (allocation_started_)
  287. network_thread_->PostDelayed(ALLOCATE_DELAY, this, MSG_ALLOCATE);
  288. for (uint32 i = 0; i < sequences_.size(); ++i)
  289. sequences_[i]->Start();
  290. for (size_t i = 0; i < ports_.size(); ++i)
  291. ports_[i].port()->Start();
  292. }
  293. void BasicPortAllocatorSession::StopGetAllPorts() {
  294. ASSERT(talk_base::Thread::Current() == network_thread_);
  295. running_ = false;
  296. network_thread_->Clear(this, MSG_ALLOCATE);
  297. for (uint32 i = 0; i < sequences_.size(); ++i)
  298. sequences_[i]->Stop();
  299. network_thread_->Post(this, MSG_CONFIG_STOP);
  300. }
  301. void BasicPortAllocatorSession::OnMessage(talk_base::Message *message) {
  302. switch (message->message_id) {
  303. case MSG_CONFIG_START:
  304. ASSERT(talk_base::Thread::Current() == network_thread_);
  305. GetPortConfigurations();
  306. break;
  307. case MSG_CONFIG_READY:
  308. ASSERT(talk_base::Thread::Current() == network_thread_);
  309. OnConfigReady(static_cast<PortConfiguration*>(message->pdata));
  310. break;
  311. case MSG_ALLOCATE:
  312. ASSERT(talk_base::Thread::Current() == network_thread_);
  313. OnAllocate();
  314. break;
  315. case MSG_SHAKE:
  316. ASSERT(talk_base::Thread::Current() == network_thread_);
  317. OnShake();
  318. break;
  319. case MSG_SEQUENCEOBJECTS_CREATED:
  320. ASSERT(talk_base::Thread::Current() == network_thread_);
  321. OnAllocationSequenceObjectsCreated();
  322. break;
  323. case MSG_CONFIG_STOP:
  324. ASSERT(talk_base::Thread::Current() == network_thread_);
  325. OnConfigStop();
  326. break;
  327. default:
  328. ASSERT(false);
  329. }
  330. }
  331. void BasicPortAllocatorSession::GetPortConfigurations() {
  332. PortConfiguration* config = new PortConfiguration(allocator_->stun_address(),
  333. username(),
  334. password());
  335. for (size_t i = 0; i < allocator_->relays().size(); ++i) {
  336. config->AddRelay(allocator_->relays()[i]);
  337. }
  338. ConfigReady(config);
  339. }
  340. void BasicPortAllocatorSession::ConfigReady(PortConfiguration* config) {
  341. network_thread_->Post(this, MSG_CONFIG_READY, config);
  342. }
  343. // Adds a configuration to the list.
  344. void BasicPortAllocatorSession::OnConfigReady(PortConfiguration* config) {
  345. if (config)
  346. configs_.push_back(config);
  347. AllocatePorts();
  348. }
  349. void BasicPortAllocatorSession::OnConfigStop() {
  350. ASSERT(talk_base::Thread::Current() == network_thread_);
  351. // If any of the allocated ports have not completed the candidates allocation,
  352. // mark those as error. Since session doesn't need any new candidates
  353. // at this stage of the allocation, it's safe to discard any new candidates.
  354. bool send_signal = false;
  355. for (std::vector<PortData>::iterator it = ports_.begin();
  356. it != ports_.end(); ++it) {
  357. if (!it->complete()) {
  358. // Updating port state to error, which didn't finish allocating candidates
  359. // yet.
  360. it->set_error();
  361. send_signal = true;
  362. }
  363. }
  364. // Did we stop any running sequences?
  365. for (std::vector<AllocationSequence*>::iterator it = sequences_.begin();
  366. it != sequences_.end() && !send_signal; ++it) {
  367. if ((*it)->state() == AllocationSequence::kStopped) {
  368. send_signal = true;
  369. }
  370. }
  371. // If we stopped anything that was running, send a done signal now.
  372. if (send_signal) {
  373. MaybeSignalCandidatesAllocationDone();
  374. }
  375. }
  376. void BasicPortAllocatorSession::AllocatePorts() {
  377. ASSERT(talk_base::Thread::Current() == network_thread_);
  378. network_thread_->Post(this, MSG_ALLOCATE);
  379. }
  380. void BasicPortAllocatorSession::OnAllocate() {
  381. if (network_manager_started_)
  382. DoAllocate();
  383. allocation_started_ = true;
  384. if (running_)
  385. network_thread_->PostDelayed(ALLOCATE_DELAY, this, MSG_ALLOCATE);
  386. }
  387. // For each network, see if we have a sequence that covers it already. If not,
  388. // create a new sequence to create the appropriate ports.
  389. void BasicPortAllocatorSession::DoAllocate() {
  390. bool done_signal_needed = false;
  391. std::vector<talk_base::Network*> networks;
  392. allocator_->network_manager()->GetNetworks(&networks);
  393. if (networks.empty()) {
  394. LOG(LS_WARNING) << "Machine has no networks; no ports will be allocated";
  395. done_signal_needed = true;
  396. } else {
  397. for (uint32 i = 0; i < networks.size(); ++i) {
  398. PortConfiguration* config = NULL;
  399. if (configs_.size() > 0)
  400. config = configs_.back();
  401. uint32 sequence_flags = flags();
  402. if ((sequence_flags & DISABLE_ALL_PHASES) == DISABLE_ALL_PHASES) {
  403. // If all the ports are disabled we should just fire the allocation
  404. // done event and return.
  405. done_signal_needed = true;
  406. break;
  407. }
  408. // Disables phases that are not specified in this config.
  409. if (!config || config->stun_address.IsNil()) {
  410. // No STUN ports specified in this config.
  411. sequence_flags |= PORTALLOCATOR_DISABLE_STUN;
  412. }
  413. if (!config || config->relays.empty()) {
  414. // No relay ports specified in this config.
  415. sequence_flags |= PORTALLOCATOR_DISABLE_RELAY;
  416. }
  417. if (!(sequence_flags & PORTALLOCATOR_ENABLE_IPV6) &&
  418. networks[i]->ip().family() == AF_INET6) {
  419. // Skip IPv6 networks unless the flag's been set.
  420. continue;
  421. }
  422. // Disable phases that would only create ports equivalent to
  423. // ones that we have already made.
  424. DisableEquivalentPhases(networks[i], config, &sequence_flags);
  425. if ((sequence_flags & DISABLE_ALL_PHASES) == DISABLE_ALL_PHASES) {
  426. // New AllocationSequence would have nothing to do, so don't make it.
  427. continue;
  428. }
  429. AllocationSequence* sequence =
  430. new AllocationSequence(this, networks[i], config, sequence_flags);
  431. if (!sequence->Init()) {
  432. delete sequence;
  433. continue;
  434. }
  435. done_signal_needed = true;
  436. sequence->SignalPortAllocationComplete.connect(
  437. this, &BasicPortAllocatorSession::OnPortAllocationComplete);
  438. if (running_)
  439. sequence->Start();
  440. sequences_.push_back(sequence);
  441. }
  442. }
  443. if (done_signal_needed) {
  444. network_thread_->Post(this, MSG_SEQUENCEOBJECTS_CREATED);
  445. }
  446. }
  447. void BasicPortAllocatorSession::OnNetworksChanged() {
  448. network_manager_started_ = true;
  449. if (allocation_started_)
  450. DoAllocate();
  451. }
  452. void BasicPortAllocatorSession::DisableEquivalentPhases(
  453. talk_base::Network* network, PortConfiguration* config, uint32* flags) {
  454. for (uint32 i = 0; i < sequences_.size() &&
  455. (*flags & DISABLE_ALL_PHASES) != DISABLE_ALL_PHASES; ++i) {
  456. sequences_[i]->DisableEquivalentPhases(network, config, flags);
  457. }
  458. }
  459. void BasicPortAllocatorSession::AddAllocatedPort(Port* port,
  460. AllocationSequence * seq,
  461. bool prepare_address) {
  462. if (!port)
  463. return;
  464. LOG(LS_INFO) << "Adding allocated port for " << content_name();
  465. port->set_content_name(content_name());
  466. port->set_component(component_);
  467. port->set_generation(generation());
  468. if (allocator_->proxy().type != talk_base::PROXY_NONE)
  469. port->set_proxy(allocator_->user_agent(), allocator_->proxy());
  470. port->set_send_retransmit_count_attribute((allocator_->flags() &
  471. PORTALLOCATOR_ENABLE_STUN_RETRANSMIT_ATTRIBUTE) != 0);
  472. if (content_name().compare(CN_VIDEO) == 0 &&
  473. component_ == cricket::ICE_CANDIDATE_COMPONENT_RTP) {
  474. // For video RTP alone, we set send-buffer sizes. This used to be set in the
  475. // engines/channels.
  476. int sendBufSize = (flags() & PORTALLOCATOR_USE_LARGE_SOCKET_SEND_BUFFERS)
  477. ? kLargeSocketSendBufferSize
  478. : kNormalSocketSendBufferSize;
  479. port->SetOption(talk_base::Socket::OPT_SNDBUF, sendBufSize);
  480. }
  481. PortData data(port, seq);
  482. ports_.push_back(data);
  483. port->SignalCandidateReady.connect(
  484. this, &BasicPortAllocatorSession::OnCandidateReady);
  485. port->SignalAddressReady.connect(this,
  486. &BasicPortAllocatorSession::OnPortReady);
  487. port->SignalConnectionCreated.connect(this,
  488. &BasicPortAllocatorSession::OnConnectionCreated);
  489. port->SignalDestroyed.connect(this,
  490. &BasicPortAllocatorSession::OnPortDestroyed);
  491. port->SignalAddressError.connect(
  492. this, &BasicPortAllocatorSession::OnAddressError);
  493. LOG_J(LS_INFO, port) << "Added port to allocator";
  494. if (prepare_address)
  495. port->PrepareAddress();
  496. if (running_)
  497. port->Start();
  498. }
  499. void BasicPortAllocatorSession::OnAllocationSequenceObjectsCreated() {
  500. allocation_sequences_created_ = true;
  501. // Send candidate allocation complete signal if we have no sequences.
  502. MaybeSignalCandidatesAllocationDone();
  503. }
  504. void BasicPortAllocatorSession::OnCandidateReady(
  505. Port* port, const Candidate& c) {
  506. ASSERT(talk_base::Thread::Current() == network_thread_);
  507. PortData* data = FindPort(port);
  508. ASSERT(data != NULL);
  509. // Discarding any candidate signal if port allocation status is
  510. // already in completed state.
  511. if (data->complete())
  512. return;
  513. // Send candidates whose protocol is enabled.
  514. std::vector<Candidate> candidates;
  515. ProtocolType pvalue;
  516. if (StringToProto(c.protocol().c_str(), &pvalue) &&
  517. data->sequence()->ProtocolEnabled(pvalue)) {
  518. candidates.push_back(c);
  519. }
  520. if (!candidates.empty()) {
  521. SignalCandidatesReady(this, candidates);
  522. }
  523. }
  524. void BasicPortAllocatorSession::OnPortReady(Port* port) {
  525. ASSERT(talk_base::Thread::Current() == network_thread_);
  526. PortData* data = FindPort(port);
  527. ASSERT(data != NULL);
  528. // Ignore any late signals.
  529. if (data->complete())
  530. return;
  531. data->set_ready();
  532. SignalPortReady(this, port);
  533. // Send candidate allocation complete signal if this was the last port.
  534. MaybeSignalCandidatesAllocationDone();
  535. }
  536. void BasicPortAllocatorSession::OnAddressError(Port* port) {
  537. ASSERT(talk_base::Thread::Current() == network_thread_);
  538. PortData* data = FindPort(port);
  539. ASSERT(data != NULL);
  540. // We might have already given up on this port and stopped it.
  541. if (data->complete())
  542. return;
  543. // SignalAddressError is currently sent from StunPort. But this signal
  544. // itself is generic.
  545. data->set_error();
  546. // Send candidate allocation complete signal if this was the last port.
  547. MaybeSignalCandidatesAllocationDone();
  548. }
  549. void BasicPortAllocatorSession::OnProtocolEnabled(AllocationSequence* seq,
  550. ProtocolType proto) {
  551. std::vector<Candidate> candidates;
  552. for (std::vector<PortData>::iterator it = ports_.begin();
  553. it != ports_.end(); ++it) {
  554. if (it->sequence() != seq)
  555. continue;
  556. const std::vector<Candidate>& potentials = it->port()->Candidates();
  557. for (size_t i = 0; i < potentials.size(); ++i) {
  558. ProtocolType pvalue;
  559. if (!StringToProto(potentials[i].protocol().c_str(), &pvalue))
  560. continue;
  561. if (pvalue == proto) {
  562. candidates.push_back(potentials[i]);
  563. }
  564. }
  565. }
  566. if (!candidates.empty()) {
  567. SignalCandidatesReady(this, candidates);
  568. }
  569. }
  570. void BasicPortAllocatorSession::OnPortAllocationComplete(
  571. AllocationSequence* seq) {
  572. // Send candidate allocation complete signal if all ports are done.
  573. MaybeSignalCandidatesAllocationDone();
  574. }
  575. void BasicPortAllocatorSession::MaybeSignalCandidatesAllocationDone() {
  576. // Send signal only if all required AllocationSequence objects
  577. // are created.
  578. if (!allocation_sequences_created_)
  579. return;
  580. // Check that all port allocation sequences are complete.
  581. for (std::vector<AllocationSequence*>::iterator it = sequences_.begin();
  582. it != sequences_.end(); ++it) {
  583. if ((*it)->state() == AllocationSequence::kRunning)
  584. return;
  585. }
  586. // If all allocated ports are in ready state, session must have got all
  587. // expected candidates. Session will trigger candidates allocation complete
  588. // signal.
  589. for (std::vector<PortData>::iterator it = ports_.begin();
  590. it != ports_.end(); ++it) {
  591. if (!it->complete())
  592. return;
  593. }
  594. LOG(LS_INFO) << "All candidates gathered for " << content_name_ << ":"
  595. << component_ << ":" << generation();
  596. SignalCandidatesAllocationDone(this);
  597. }
  598. void BasicPortAllocatorSession::OnPortDestroyed(
  599. PortInterface* port) {
  600. ASSERT(talk_base::Thread::Current() == network_thread_);
  601. for (std::vector<PortData>::iterator iter = ports_.begin();
  602. iter != ports_.end(); ++iter) {
  603. if (port == iter->port()) {
  604. ports_.erase(iter);
  605. LOG_J(LS_INFO, port) << "Removed port from allocator ("
  606. << static_cast<int>(ports_.size()) << " remaining)";
  607. return;
  608. }
  609. }
  610. ASSERT(false);
  611. }
  612. void BasicPortAllocatorSession::OnConnectionCreated(Port* port,
  613. Connection* conn) {
  614. conn->SignalStateChange.connect(this,
  615. &BasicPortAllocatorSession::OnConnectionStateChange);
  616. }
  617. void BasicPortAllocatorSession::OnConnectionStateChange(Connection* conn) {
  618. if (conn->write_state() == Connection::STATE_WRITABLE)
  619. allocator_->AddWritablePhase(
  620. LocalCandidateToPhase(conn->local_candidate()));
  621. }
  622. void BasicPortAllocatorSession::OnShake() {
  623. LOG(INFO) << ">>>>> SHAKE <<<<< >>>>> SHAKE <<<<< >>>>> SHAKE <<<<<";
  624. std::vector<Port*> ports;
  625. std::vector<Connection*> connections;
  626. for (size_t i = 0; i < ports_.size(); ++i) {
  627. if (ports_[i].ready())
  628. ports.push_back(ports_[i].port());
  629. }
  630. for (size_t i = 0; i < ports.size(); ++i) {
  631. Port::AddressMap::const_iterator iter;
  632. for (iter = ports[i]->connections().begin();
  633. iter != ports[i]->connections().end();
  634. ++iter) {
  635. connections.push_back(iter->second);
  636. }
  637. }
  638. LOG(INFO) << ">>>>> Destroying " << ports.size() << " ports and "
  639. << connections.size() << " connections";
  640. for (size_t i = 0; i < connections.size(); ++i)
  641. connections[i]->Destroy();
  642. if (running_ || (ports.size() > 0) || (connections.size() > 0))
  643. network_thread_->PostDelayed(ShakeDelay(), this, MSG_SHAKE);
  644. }
  645. BasicPortAllocatorSession::PortData* BasicPortAllocatorSession::FindPort(
  646. Port* port) {
  647. for (std::vector<PortData>::iterator it = ports_.begin();
  648. it != ports_.end(); ++it) {
  649. if (it->port() == port) {
  650. return &*it;
  651. }
  652. }
  653. return NULL;
  654. }
  655. // AllocationSequence
  656. AllocationSequence::AllocationSequence(BasicPortAllocatorSession* session,
  657. talk_base::Network* network,
  658. PortConfiguration* config,
  659. uint32 flags)
  660. : session_(session),
  661. network_(network),
  662. ip_(network->ip()),
  663. config_(config),
  664. state_(kInit),
  665. step_(0),
  666. flags_(flags),
  667. udp_socket_(NULL) {
  668. // All of the phases up until the best-writable phase so far run in step 0.
  669. // The other phases follow sequentially in the steps after that. If there is
  670. // no best-writable so far, then only phase 0 occurs in step 0.
  671. int last_phase_in_step_zero =
  672. talk_base::_max(0, session->allocator()->best_writable_phase());
  673. for (int phase = 0; phase < kNumPhases; ++phase)
  674. step_of_phase_[phase] = talk_base::_max(0, phase - last_phase_in_step_zero);
  675. }
  676. bool AllocationSequence::Init() {
  677. if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET) &&
  678. !IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_UFRAG)) {
  679. LOG(LS_ERROR) << "Shared socket option can't be set without "
  680. << "shared ufrag.";
  681. ASSERT(false);
  682. return false;
  683. }
  684. if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) {
  685. udp_socket_.reset(session_->socket_factory()->CreateUdpSocket(
  686. talk_base::SocketAddress(ip_, 0), session_->allocator()->min_port(),
  687. session_->allocator()->max_port()));
  688. if (udp_socket_) {
  689. udp_socket_->SignalReadPacket.connect(
  690. this, &AllocationSequence::OnReadPacket);
  691. }
  692. // Continuing if |udp_socket_| is NULL, as local TCP and RelayPort using TCP
  693. // are next available options to setup a communication channel.
  694. }
  695. // Immediately perform phase 0.
  696. OnMessage(NULL);
  697. return true;
  698. }
  699. AllocationSequence::~AllocationSequence() {
  700. session_->network_thread()->Clear(this);
  701. }
  702. void AllocationSequence::DisableEquivalentPhases(talk_base::Network* network,
  703. PortConfiguration* config, uint32* flags) {
  704. if (!((network == network_) && (ip_ == network->ip()))) {
  705. // Different network setup; nothing is equivalent.
  706. return;
  707. }
  708. // Else turn off the stuff that we've already got covered.
  709. // Every config implicitly specifies local, so turn that off right away.
  710. *flags |= PORTALLOCATOR_DISABLE_UDP;
  711. *flags |= PORTALLOCATOR_DISABLE_TCP;
  712. if (config_ && config) {
  713. if (config_->stun_address == config->stun_address) {
  714. // Already got this STUN server covered.
  715. *flags |= PORTALLOCATOR_DISABLE_STUN;
  716. }
  717. if (!config_->relays.empty()) {
  718. // Already got relays covered.
  719. // NOTE: This will even skip a _different_ set of relay servers if we
  720. // were to be given one, but that never happens in our codebase. Should
  721. // probably get rid of the list in PortConfiguration and just keep a
  722. // single relay server in each one.
  723. *flags |= PORTALLOCATOR_DISABLE_RELAY;
  724. }
  725. }
  726. }
  727. void AllocationSequence::Start() {
  728. state_ = kRunning;
  729. session_->network_thread()->PostDelayed(ALLOCATION_STEP_DELAY,
  730. this,
  731. MSG_ALLOCATION_PHASE);
  732. }
  733. void AllocationSequence::Stop() {
  734. // If the port is completed, don't set it to stopped.
  735. if (state_ == kRunning) {
  736. state_ = kStopped;
  737. session_->network_thread()->Clear(this, MSG_ALLOCATION_PHASE);
  738. }
  739. }
  740. void AllocationSequence::OnMessage(talk_base::Message* msg) {
  741. ASSERT(talk_base::Thread::Current() == session_->network_thread());
  742. if (msg)
  743. ASSERT(msg->message_id == MSG_ALLOCATION_PHASE);
  744. const char* const PHASE_NAMES[kNumPhases] = {
  745. "Udp", "Relay", "Tcp", "SslTcp"
  746. };
  747. // Perform all of the phases in the current step.
  748. for (int phase = 0; phase < kNumPhases; phase++) {
  749. if (step_of_phase_[phase] != step_)
  750. continue;
  751. LOG_J(LS_INFO, network_) << "Allocation Phase=" << PHASE_NAMES[phase]
  752. << " (Step=" << step_ << ")";
  753. switch (phase) {
  754. case PHASE_UDP:
  755. CreateUDPPorts();
  756. CreateStunPorts();
  757. EnableProtocol(PROTO_UDP);
  758. break;
  759. case PHASE_RELAY:
  760. CreateRelayPorts();
  761. break;
  762. case PHASE_TCP:
  763. CreateTCPPorts();
  764. EnableProtocol(PROTO_TCP);
  765. break;
  766. case PHASE_SSLTCP:
  767. state_ = kCompleted;
  768. EnableProtocol(PROTO_SSLTCP);
  769. break;
  770. default:
  771. ASSERT(false);
  772. }
  773. // If all phases in AllocationSequence are completed, no allocation
  774. // steps needed further. Canceling pending signal.
  775. if (phase == kNumPhases - 1) {
  776. session_->network_thread()->Clear(this, MSG_ALLOCATION_PHASE);
  777. state_ = kCompleted;
  778. SignalPortAllocationComplete(this);
  779. }
  780. }
  781. step_ += 1;
  782. if (state() == kRunning) {
  783. session_->network_thread()->PostDelayed(ALLOCATION_STEP_DELAY,
  784. this,
  785. MSG_ALLOCATION_PHASE);
  786. }
  787. }
  788. void AllocationSequence::EnableProtocol(ProtocolType proto) {
  789. if (!ProtocolEnabled(proto)) {
  790. protocols_.push_back(proto);
  791. session_->OnProtocolEnabled(this, proto);
  792. }
  793. }
  794. bool AllocationSequence::ProtocolEnabled(ProtocolType proto) const {
  795. for (ProtocolList::const_iterator it = protocols_.begin();
  796. it != protocols_.end(); ++it) {
  797. if (*it == proto)
  798. return true;
  799. }
  800. return false;
  801. }
  802. void AllocationSequence::CreateUDPPorts() {
  803. if (IsFlagSet(PORTALLOCATOR_DISABLE_UDP)) {
  804. LOG(LS_VERBOSE) << "AllocationSequence: UDP ports disabled, skipping.";
  805. return;
  806. }
  807. // TODO(mallinath) - Remove UDPPort creating socket after shared socket
  808. // is enabled completely.
  809. UDPPort* port = NULL;
  810. if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET) && udp_socket_) {
  811. port = UDPPort::Create(session_->network_thread(), network_,
  812. udp_socket_.get(),
  813. session_->username(), session_->password());
  814. } else {
  815. port = UDPPort::Create(session_->network_thread(),
  816. session_->socket_factory(),
  817. network_, ip_,
  818. session_->allocator()->min_port(),
  819. session_->allocator()->max_port(),
  820. session_->username(), session_->password());
  821. }
  822. if (port) {
  823. ports.push_back(port);
  824. // If shared socket is enabled, STUN candidate will be allocated by the
  825. // UDPPort.
  826. if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET) &&
  827. !IsFlagSet(PORTALLOCATOR_DISABLE_STUN)) {
  828. ASSERT(config_ && !config_->stun_address.IsNil());
  829. if (!(config_ && !config_->stun_address.IsNil())) {
  830. LOG(LS_WARNING)
  831. << "AllocationSequence: No STUN server configured, skipping.";
  832. return;
  833. }
  834. port->set_server_addr(config_->stun_address);
  835. }
  836. session_->AddAllocatedPort(port, this);
  837. port->SignalDestroyed.connect(this, &AllocationSequence::OnPortDestroyed);
  838. }
  839. }
  840. void AllocationSequence::CreateTCPPorts() {
  841. if (IsFlagSet(PORTALLOCATOR_DISABLE_TCP)) {
  842. LOG(LS_VERBOSE) << "AllocationSequence: TCP ports disabled, skipping.";
  843. return;
  844. }
  845. Port* port = TCPPort::Create(session_->network_thread(),
  846. session_->socket_factory(),
  847. network_, ip_,
  848. session_->allocator()->min_port(),
  849. session_->allocator()->max_port(),
  850. session_->username(), session_->password(),
  851. session_->allocator()->allow_tcp_listen());
  852. if (port) {
  853. session_->AddAllocatedPort(port, this);
  854. // Since TCPPort is not created using shared socket, |port| will not be
  855. // added to the dequeue.
  856. }
  857. }
  858. void AllocationSequence::CreateStunPorts() {
  859. if (IsFlagSet(PORTALLOCATOR_DISABLE_STUN)) {
  860. LOG(LS_VERBOSE) << "AllocationSequence: STUN ports disabled, skipping.";
  861. return;
  862. }
  863. if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) {
  864. LOG(LS_INFO) << "AllocationSequence: "
  865. << "UDPPort will be handling the STUN candidate generation.";
  866. return;
  867. }
  868. // If BasicPortAllocatorSession::OnAllocate left STUN ports enabled then we
  869. // ought to have an address for them here.
  870. ASSERT(config_ && !config_->stun_address.IsNil());
  871. if (!(config_ && !config_->stun_address.IsNil())) {
  872. LOG(LS_WARNING)
  873. << "AllocationSequence: No STUN server configured, skipping.";
  874. return;
  875. }
  876. StunPort* port = StunPort::Create(session_->network_thread(),
  877. session_->socket_factory(),
  878. network_, ip_,
  879. session_->allocator()->min_port(),
  880. session_->allocator()->max_port(),
  881. session_->username(), session_->password(),
  882. config_->stun_address);
  883. if (port) {
  884. session_->AddAllocatedPort(port, this);
  885. // Since StunPort is not created using shared socket, |port| will not be
  886. // added to the dequeue.
  887. }
  888. }
  889. void AllocationSequence::CreateRelayPorts() {
  890. if (IsFlagSet(PORTALLOCATOR_DISABLE_RELAY)) {
  891. LOG(LS_VERBOSE) << "AllocationSequence: Relay ports disabled, skipping.";
  892. return;
  893. }
  894. // If BasicPortAllocatorSession::OnAllocate left relay ports enabled then we
  895. // ought to have a relay list for them here.
  896. ASSERT(config_ && !config_->relays.empty());
  897. if (!(config_ && !config_->relays.empty())) {
  898. LOG(LS_WARNING)
  899. << "AllocationSequence: No relay server configured, skipping.";
  900. return;
  901. }
  902. PortConfiguration::RelayList::const_iterator relay;
  903. for (relay = config_->relays.begin();
  904. relay != config_->relays.end(); ++relay) {
  905. if (relay->type == RELAY_GTURN) {
  906. CreateGturnPort(*relay);
  907. } else if (relay->type == RELAY_TURN) {
  908. CreateTurnPort(*relay);
  909. } else {
  910. ASSERT(false);
  911. }
  912. }
  913. }
  914. void AllocationSequence::CreateGturnPort(const RelayServerConfig& config) {
  915. // TODO(mallinath) - Rename RelayPort to GTurnPort.
  916. RelayPort* port = RelayPort::Create(session_->network_thread(),
  917. session_->socket_factory(),
  918. network_, ip_,
  919. session_->allocator()->min_port(),
  920. session_->allocator()->max_port(),
  921. config_->username, config_->password);
  922. if (port) {
  923. // Since RelayPort is not created using shared socket, |port| will not be
  924. // added to the dequeue.
  925. // Note: We must add the allocated port before we add addresses because
  926. // the latter will create candidates that need name and preference
  927. // settings. However, we also can't prepare the address (normally
  928. // done by AddAllocatedPort) until we have these addresses. So we
  929. // wait to do that until below.
  930. session_->AddAllocatedPort(port, this, false);
  931. // Add the addresses of this protocol.
  932. PortList::const_iterator relay_port;
  933. for (relay_port = config.ports.begin();
  934. relay_port != config.ports.end();
  935. ++relay_port) {
  936. port->AddServerAddress(*relay_port);
  937. port->AddExternalAddress(*relay_port);
  938. }
  939. // Start fetching an address for this port.
  940. port->PrepareAddress();
  941. }
  942. }
  943. void AllocationSequence::CreateTurnPort(const RelayServerConfig& config) {
  944. PortList::const_iterator relay_port;
  945. for (relay_port = config.ports.begin();
  946. relay_port != config.ports.end(); ++relay_port) {
  947. if (relay_port->proto == PROTO_UDP) {
  948. TurnPort* port = TurnPort::Create(session_->network_thread(),
  949. session_->socket_factory(),
  950. network_, ip_,
  951. session_->allocator()->min_port(),
  952. session_->allocator()->max_port(),
  953. session_->username(),
  954. session_->password(),
  955. relay_port->address,
  956. config.credentials);
  957. if (port) {
  958. session_->AddAllocatedPort(port, this);
  959. }
  960. } else {
  961. LOG(LS_WARNING) << ProtoToString(relay_port->proto)
  962. << " server address: " << relay_port->address.ToString()
  963. << " is not currently supported.";
  964. }
  965. }
  966. }
  967. void AllocationSequence::OnReadPacket(
  968. talk_base::AsyncPacketSocket* socket, const char* data, size_t size,
  969. const talk_base::SocketAddress& remote_addr) {
  970. ASSERT(socket == udp_socket_.get());
  971. for (std::deque<Port*>::iterator iter = ports.begin();
  972. iter != ports.end(); ++iter) {
  973. // We have only one port in the queue.
  974. // TODO(mallinath) - Add shared socket support to Relay and Turn ports.
  975. if ((*iter)->HandleIncomingPacket(socket, data, size, remote_addr)) {
  976. break;
  977. }
  978. }
  979. }
  980. void AllocationSequence::OnPortDestroyed(PortInterface* port) {
  981. std::deque<Port*>::iterator iter =
  982. std::find(ports.begin(), ports.end(), port);
  983. ASSERT(iter != ports.end());
  984. ports.erase(iter);
  985. }
  986. // PortConfiguration
  987. PortConfiguration::PortConfiguration(
  988. const talk_base::SocketAddress& stun_address,
  989. const std::string& username,
  990. const std::string& password)
  991. : stun_address(stun_address),
  992. username(username),
  993. password(password) {
  994. }
  995. void PortConfiguration::AddRelay(const RelayServerConfig& config) {
  996. relays.push_back(config);
  997. }
  998. bool PortConfiguration::SupportsProtocol(
  999. const RelayServerConfig& relay, ProtocolType type) {
  1000. PortList::const_iterator relay_port;
  1001. for (relay_port = relay.ports.begin();
  1002. relay_port != relay.ports.end();
  1003. ++relay_port) {
  1004. if (relay_port->proto == type)
  1005. return true;
  1006. }
  1007. return false;
  1008. }
  1009. } // namespace cricket