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.

262 lines
7.8 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 "xmppsocket.h"
  28. #ifdef HAVE_CONFIG_H
  29. #include <config.h>
  30. #endif
  31. #include <errno.h>
  32. #include "talk/base/basicdefs.h"
  33. #include "talk/base/logging.h"
  34. #include "talk/base/thread.h"
  35. #ifdef FEATURE_ENABLE_SSL
  36. #include "talk/base/ssladapter.h"
  37. #endif
  38. #ifdef USE_SSLSTREAM
  39. #include "talk/base/socketstream.h"
  40. #ifdef FEATURE_ENABLE_SSL
  41. #include "talk/base/sslstreamadapter.h"
  42. #endif // FEATURE_ENABLE_SSL
  43. #endif // USE_SSLSTREAM
  44. namespace buzz {
  45. XmppSocket::XmppSocket(buzz::TlsOptions tls) : cricket_socket_(NULL),
  46. tls_(tls) {
  47. state_ = buzz::AsyncSocket::STATE_CLOSED;
  48. }
  49. void XmppSocket::CreateCricketSocket(int family) {
  50. talk_base::Thread* pth = talk_base::Thread::Current();
  51. if (family == AF_UNSPEC) {
  52. family = AF_INET;
  53. }
  54. talk_base::AsyncSocket* socket =
  55. pth->socketserver()->CreateAsyncSocket(family, SOCK_STREAM);
  56. #ifndef USE_SSLSTREAM
  57. #ifdef FEATURE_ENABLE_SSL
  58. if (tls_ != buzz::TLS_DISABLED) {
  59. socket = talk_base::SSLAdapter::Create(socket);
  60. }
  61. #endif // FEATURE_ENABLE_SSL
  62. cricket_socket_ = socket;
  63. cricket_socket_->SignalReadEvent.connect(this, &XmppSocket::OnReadEvent);
  64. cricket_socket_->SignalWriteEvent.connect(this, &XmppSocket::OnWriteEvent);
  65. cricket_socket_->SignalConnectEvent.connect(this,
  66. &XmppSocket::OnConnectEvent);
  67. cricket_socket_->SignalCloseEvent.connect(this, &XmppSocket::OnCloseEvent);
  68. #else // USE_SSLSTREAM
  69. cricket_socket_ = socket;
  70. stream_ = new talk_base::SocketStream(cricket_socket_);
  71. #ifdef FEATURE_ENABLE_SSL
  72. if (tls_ != buzz::TLS_DISABLED)
  73. stream_ = talk_base::SSLStreamAdapter::Create(stream_);
  74. #endif // FEATURE_ENABLE_SSL
  75. stream_->SignalEvent.connect(this, &XmppSocket::OnEvent);
  76. #endif // USE_SSLSTREAM
  77. }
  78. XmppSocket::~XmppSocket() {
  79. Close();
  80. #ifndef USE_SSLSTREAM
  81. delete cricket_socket_;
  82. #else // USE_SSLSTREAM
  83. delete stream_;
  84. #endif // USE_SSLSTREAM
  85. }
  86. #ifndef USE_SSLSTREAM
  87. void XmppSocket::OnReadEvent(talk_base::AsyncSocket * socket) {
  88. SignalRead();
  89. }
  90. void XmppSocket::OnWriteEvent(talk_base::AsyncSocket * socket) {
  91. // Write bytes if there are any
  92. while (buffer_.Length() != 0) {
  93. int written = cricket_socket_->Send(buffer_.Data(), buffer_.Length());
  94. if (written > 0) {
  95. buffer_.Consume(written);
  96. continue;
  97. }
  98. if (!cricket_socket_->IsBlocking())
  99. LOG(LS_ERROR) << "Send error: " << cricket_socket_->GetError();
  100. return;
  101. }
  102. }
  103. void XmppSocket::OnConnectEvent(talk_base::AsyncSocket * socket) {
  104. #if defined(FEATURE_ENABLE_SSL)
  105. if (state_ == buzz::AsyncSocket::STATE_TLS_CONNECTING) {
  106. state_ = buzz::AsyncSocket::STATE_TLS_OPEN;
  107. SignalSSLConnected();
  108. OnWriteEvent(cricket_socket_);
  109. return;
  110. }
  111. #endif // !defined(FEATURE_ENABLE_SSL)
  112. state_ = buzz::AsyncSocket::STATE_OPEN;
  113. SignalConnected();
  114. }
  115. void XmppSocket::OnCloseEvent(talk_base::AsyncSocket * socket, int error) {
  116. SignalCloseEvent(error);
  117. }
  118. #else // USE_SSLSTREAM
  119. void XmppSocket::OnEvent(talk_base::StreamInterface* stream,
  120. int events, int err) {
  121. if ((events & talk_base::SE_OPEN)) {
  122. #if defined(FEATURE_ENABLE_SSL)
  123. if (state_ == buzz::AsyncSocket::STATE_TLS_CONNECTING) {
  124. state_ = buzz::AsyncSocket::STATE_TLS_OPEN;
  125. SignalSSLConnected();
  126. events |= talk_base::SE_WRITE;
  127. } else
  128. #endif
  129. {
  130. state_ = buzz::AsyncSocket::STATE_OPEN;
  131. SignalConnected();
  132. }
  133. }
  134. if ((events & talk_base::SE_READ))
  135. SignalRead();
  136. if ((events & talk_base::SE_WRITE)) {
  137. // Write bytes if there are any
  138. while (buffer_.Length() != 0) {
  139. talk_base::StreamResult result;
  140. size_t written;
  141. int error;
  142. result = stream_->Write(buffer_.Data(), buffer_.Length(),
  143. &written, &error);
  144. if (result == talk_base::SR_ERROR) {
  145. LOG(LS_ERROR) << "Send error: " << error;
  146. return;
  147. }
  148. if (result == talk_base::SR_BLOCK)
  149. return;
  150. ASSERT(result == talk_base::SR_SUCCESS);
  151. ASSERT(written > 0);
  152. buffer_.Shift(written);
  153. }
  154. }
  155. if ((events & talk_base::SE_CLOSE))
  156. SignalCloseEvent(err);
  157. }
  158. #endif // USE_SSLSTREAM
  159. buzz::AsyncSocket::State XmppSocket::state() {
  160. return state_;
  161. }
  162. buzz::AsyncSocket::Error XmppSocket::error() {
  163. return buzz::AsyncSocket::ERROR_NONE;
  164. }
  165. int XmppSocket::GetError() {
  166. return 0;
  167. }
  168. bool XmppSocket::Connect(const talk_base::SocketAddress& addr) {
  169. if (cricket_socket_ == NULL) {
  170. CreateCricketSocket(addr.family());
  171. }
  172. if (cricket_socket_->Connect(addr) < 0) {
  173. return cricket_socket_->IsBlocking();
  174. }
  175. return true;
  176. }
  177. bool XmppSocket::Read(char * data, size_t len, size_t* len_read) {
  178. #ifndef USE_SSLSTREAM
  179. int read = cricket_socket_->Recv(data, len);
  180. if (read > 0) {
  181. *len_read = (size_t)read;
  182. return true;
  183. }
  184. #else // USE_SSLSTREAM
  185. talk_base::StreamResult result = stream_->Read(data, len, len_read, NULL);
  186. if (result == talk_base::SR_SUCCESS)
  187. return true;
  188. #endif // USE_SSLSTREAM
  189. return false;
  190. }
  191. bool XmppSocket::Write(const char * data, size_t len) {
  192. buffer_.WriteBytes(data, len);
  193. #ifndef USE_SSLSTREAM
  194. OnWriteEvent(cricket_socket_);
  195. #else // USE_SSLSTREAM
  196. OnEvent(stream_, talk_base::SE_WRITE, 0);
  197. #endif // USE_SSLSTREAM
  198. return true;
  199. }
  200. bool XmppSocket::Close() {
  201. if (state_ != buzz::AsyncSocket::STATE_OPEN)
  202. return false;
  203. #ifndef USE_SSLSTREAM
  204. if (cricket_socket_->Close() == 0) {
  205. state_ = buzz::AsyncSocket::STATE_CLOSED;
  206. SignalClosed();
  207. return true;
  208. }
  209. return false;
  210. #else // USE_SSLSTREAM
  211. state_ = buzz::AsyncSocket::STATE_CLOSED;
  212. stream_->Close();
  213. SignalClosed();
  214. return true;
  215. #endif // USE_SSLSTREAM
  216. }
  217. bool XmppSocket::StartTls(const std::string & domainname) {
  218. #if defined(FEATURE_ENABLE_SSL)
  219. if (tls_ == buzz::TLS_DISABLED)
  220. return false;
  221. #ifndef USE_SSLSTREAM
  222. talk_base::SSLAdapter* ssl_adapter =
  223. static_cast<talk_base::SSLAdapter *>(cricket_socket_);
  224. if (ssl_adapter->StartSSL(domainname.c_str(), false) != 0)
  225. return false;
  226. #else // USE_SSLSTREAM
  227. talk_base::SSLStreamAdapter* ssl_stream =
  228. static_cast<talk_base::SSLStreamAdapter *>(stream_);
  229. if (ssl_stream->StartSSLWithServer(domainname.c_str()) != 0)
  230. return false;
  231. #endif // USE_SSLSTREAM
  232. state_ = buzz::AsyncSocket::STATE_TLS_CONNECTING;
  233. return true;
  234. #else // !defined(FEATURE_ENABLE_SSL)
  235. return false;
  236. #endif // !defined(FEATURE_ENABLE_SSL)
  237. }
  238. } // namespace buzz