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