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.

178 lines
6.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2011, 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/base/basicpacketsocketfactory.h"
  28. #include "talk/base/asyncudpsocket.h"
  29. #include "talk/base/asynctcpsocket.h"
  30. #include "talk/base/logging.h"
  31. #include "talk/base/socketadapters.h"
  32. #include "talk/base/thread.h"
  33. namespace talk_base {
  34. BasicPacketSocketFactory::BasicPacketSocketFactory()
  35. : thread_(Thread::Current()),
  36. socket_factory_(NULL) {
  37. }
  38. BasicPacketSocketFactory::BasicPacketSocketFactory(Thread* thread)
  39. : thread_(thread),
  40. socket_factory_(NULL) {
  41. }
  42. BasicPacketSocketFactory::BasicPacketSocketFactory(
  43. SocketFactory* socket_factory)
  44. : thread_(NULL),
  45. socket_factory_(socket_factory) {
  46. }
  47. BasicPacketSocketFactory::~BasicPacketSocketFactory() {
  48. }
  49. AsyncPacketSocket* BasicPacketSocketFactory::CreateUdpSocket(
  50. const SocketAddress& address, int min_port, int max_port) {
  51. // UDP sockets are simple.
  52. talk_base::AsyncSocket* socket =
  53. socket_factory()->CreateAsyncSocket(
  54. address.family(), SOCK_DGRAM);
  55. if (!socket) {
  56. return NULL;
  57. }
  58. if (BindSocket(socket, address, min_port, max_port) < 0) {
  59. LOG(LS_ERROR) << "UDP bind failed with error "
  60. << socket->GetError();
  61. delete socket;
  62. return NULL;
  63. }
  64. return new talk_base::AsyncUDPSocket(socket);
  65. }
  66. AsyncPacketSocket* BasicPacketSocketFactory::CreateServerTcpSocket(
  67. const SocketAddress& local_address, int min_port, int max_port, bool ssl) {
  68. talk_base::AsyncSocket* socket =
  69. socket_factory()->CreateAsyncSocket(local_address.family(),
  70. SOCK_STREAM);
  71. if (!socket) {
  72. return NULL;
  73. }
  74. if (BindSocket(socket, local_address, min_port, max_port) < 0) {
  75. LOG(LS_ERROR) << "TCP bind failed with error "
  76. << socket->GetError();
  77. delete socket;
  78. return NULL;
  79. }
  80. // If using SSLTCP, wrap the TCP socket in a pseudo-SSL socket.
  81. if (ssl) {
  82. socket = new talk_base::AsyncSSLSocket(socket);
  83. }
  84. // Set TCP_NODELAY (via OPT_NODELAY) for improved performance.
  85. // See http://go/gtalktcpnodelayexperiment
  86. socket->SetOption(talk_base::Socket::OPT_NODELAY, 1);
  87. return new talk_base::AsyncTCPSocket(socket, true);
  88. }
  89. AsyncPacketSocket* BasicPacketSocketFactory::CreateClientTcpSocket(
  90. const SocketAddress& local_address, const SocketAddress& remote_address,
  91. const ProxyInfo& proxy_info, const std::string& user_agent, bool ssl) {
  92. talk_base::AsyncSocket* socket =
  93. socket_factory()->CreateAsyncSocket(local_address.family(), SOCK_STREAM);
  94. if (!socket) {
  95. return NULL;
  96. }
  97. if (BindSocket(socket, local_address, 0, 0) < 0) {
  98. LOG(LS_ERROR) << "TCP bind failed with error "
  99. << socket->GetError();
  100. delete socket;
  101. return NULL;
  102. }
  103. // If using a proxy, wrap the socket in a proxy socket.
  104. if (proxy_info.type == talk_base::PROXY_SOCKS5) {
  105. socket = new talk_base::AsyncSocksProxySocket(
  106. socket, proxy_info.address, proxy_info.username, proxy_info.password);
  107. } else if (proxy_info.type == talk_base::PROXY_HTTPS) {
  108. socket = new talk_base::AsyncHttpsProxySocket(
  109. socket, user_agent, proxy_info.address,
  110. proxy_info.username, proxy_info.password);
  111. }
  112. // If using SSLTCP, wrap the TCP socket in a pseudo-SSL socket.
  113. if (ssl) {
  114. socket = new talk_base::AsyncSSLSocket(socket);
  115. }
  116. if (socket->Connect(remote_address) < 0) {
  117. LOG(LS_ERROR) << "TCP connect failed with error "
  118. << socket->GetError();
  119. delete socket;
  120. return NULL;
  121. }
  122. // Finally, wrap that socket in a TCP packet socket.
  123. talk_base::AsyncTCPSocket* tcp_socket =
  124. new talk_base::AsyncTCPSocket(socket, false);
  125. // Set TCP_NODELAY (via OPT_NODELAY) for improved performance.
  126. // See http://go/gtalktcpnodelayexperiment
  127. tcp_socket->SetOption(talk_base::Socket::OPT_NODELAY, 1);
  128. return tcp_socket;
  129. }
  130. int BasicPacketSocketFactory::BindSocket(
  131. AsyncSocket* socket, const SocketAddress& local_address,
  132. int min_port, int max_port) {
  133. int ret = -1;
  134. if (min_port == 0 && max_port == 0) {
  135. // If there's no port range, let the OS pick a port for us.
  136. ret = socket->Bind(local_address);
  137. } else {
  138. // Otherwise, try to find a port in the provided range.
  139. for (int port = min_port; ret < 0 && port <= max_port; ++port) {
  140. ret = socket->Bind(talk_base::SocketAddress(local_address.ipaddr(),
  141. port));
  142. }
  143. }
  144. return ret;
  145. }
  146. SocketFactory* BasicPacketSocketFactory::socket_factory() {
  147. if (thread_) {
  148. ASSERT(thread_ == Thread::Current());
  149. return thread_->socketserver();
  150. } else {
  151. return socket_factory_;
  152. }
  153. }
  154. } // namespace talk_base