3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #if defined(_MSC_VER) && _MSC_VER < 1300
29 #pragma warning(disable:4786)
36 #define WIN32_LEAN_AND_MEAN
40 #define SECURITY_WIN32
44 #include "talk/base/bytebuffer.h"
45 #include "talk/base/common.h"
46 #include "talk/base/httpcommon.h"
47 #include "talk/base/logging.h"
48 #include "talk/base/socketadapters.h"
49 #include "talk/base/stringencode.h"
50 #include "talk/base/stringutils.h"
53 #include "talk/base/sec_buffer.h"
58 BufferedReadAdapter::BufferedReadAdapter(AsyncSocket* socket, size_t size)
59 : AsyncSocketAdapter(socket), buffer_size_(size),
60 data_len_(0), buffering_(false) {
61 buffer_ = new char[buffer_size_];
64 BufferedReadAdapter::~BufferedReadAdapter() {
68 int BufferedReadAdapter::Send(const void *pv, size_t cb) {
70 // TODO: Spoof error better; Signal Writeable
71 socket_->SetError(EWOULDBLOCK);
74 return AsyncSocketAdapter::Send(pv, cb);
77 int BufferedReadAdapter::Recv(void *pv, size_t cb) {
79 socket_->SetError(EWOULDBLOCK);
86 read = _min(cb, data_len_);
87 memcpy(pv, buffer_, read);
90 memmove(buffer_, buffer_ + read, data_len_);
92 pv = static_cast<char *>(pv) + read;
96 // FIX: If cb == 0, we won't generate another read event
98 int res = AsyncSocketAdapter::Recv(pv, cb);
102 return res + static_cast<int>(read);
105 void BufferedReadAdapter::BufferInput(bool on) {
109 void BufferedReadAdapter::OnReadEvent(AsyncSocket * socket) {
110 ASSERT(socket == socket_);
113 AsyncSocketAdapter::OnReadEvent(socket);
117 if (data_len_ >= buffer_size_) {
118 LOG(INFO) << "Input buffer overflow";
123 int len = socket_->Recv(buffer_ + data_len_, buffer_size_ - data_len_);
125 // TODO: Do something better like forwarding the error to the user.
126 LOG_ERR(INFO) << "Recv";
132 ProcessInput(buffer_, &data_len_);
135 ///////////////////////////////////////////////////////////////////////////////
137 // This is a SSL v2 CLIENT_HELLO message.
138 // TODO: Should this have a session id? The response doesn't have a
139 // certificate, so the hello should have a session id.
140 static const uint8 kSslClientHello[] = {
141 0x80, 0x46, // msg len
142 0x01, // CLIENT_HELLO
143 0x03, 0x01, // SSL 3.1
144 0x00, 0x2d, // ciphersuite len
145 0x00, 0x00, // session id len
146 0x00, 0x10, // challenge len
147 0x01, 0x00, 0x80, 0x03, 0x00, 0x80, 0x07, 0x00, 0xc0, // ciphersuites
148 0x06, 0x00, 0x40, 0x02, 0x00, 0x80, 0x04, 0x00, 0x80, //
149 0x00, 0x00, 0x04, 0x00, 0xfe, 0xff, 0x00, 0x00, 0x0a, //
150 0x00, 0xfe, 0xfe, 0x00, 0x00, 0x09, 0x00, 0x00, 0x64, //
151 0x00, 0x00, 0x62, 0x00, 0x00, 0x03, 0x00, 0x00, 0x06, //
152 0x1f, 0x17, 0x0c, 0xa6, 0x2f, 0x00, 0x78, 0xfc, // challenge
153 0x46, 0x55, 0x2e, 0xb1, 0x83, 0x39, 0xf1, 0xea //
156 // This is a TLSv1 SERVER_HELLO message.
157 static const uint8 kSslServerHello[] = {
158 0x16, // handshake message
159 0x03, 0x01, // SSL 3.1
160 0x00, 0x4a, // message len
161 0x02, // SERVER_HELLO
162 0x00, 0x00, 0x46, // handshake len
163 0x03, 0x01, // SSL 3.1
164 0x42, 0x85, 0x45, 0xa7, 0x27, 0xa9, 0x5d, 0xa0, // server random
165 0xb3, 0xc5, 0xe7, 0x53, 0xda, 0x48, 0x2b, 0x3f, //
166 0xc6, 0x5a, 0xca, 0x89, 0xc1, 0x58, 0x52, 0xa1, //
167 0x78, 0x3c, 0x5b, 0x17, 0x46, 0x00, 0x85, 0x3f, //
168 0x20, // session id len
169 0x0e, 0xd3, 0x06, 0x72, 0x5b, 0x5b, 0x1b, 0x5f, // session id
170 0x15, 0xac, 0x13, 0xf9, 0x88, 0x53, 0x9d, 0x9b, //
171 0xe8, 0x3d, 0x7b, 0x0c, 0x30, 0x32, 0x6e, 0x38, //
172 0x4d, 0xa2, 0x75, 0x57, 0x41, 0x6c, 0x34, 0x5c, //
173 0x00, 0x04, // RSA/RC4-128/MD5
174 0x00 // null compression
177 AsyncSSLSocket::AsyncSSLSocket(AsyncSocket* socket)
178 : BufferedReadAdapter(socket, 1024) {
181 int AsyncSSLSocket::Connect(const SocketAddress& addr) {
182 // Begin buffering before we connect, so that there isn't a race condition
183 // between potential senders and receiving the OnConnectEvent signal
185 return BufferedReadAdapter::Connect(addr);
188 void AsyncSSLSocket::OnConnectEvent(AsyncSocket * socket) {
189 ASSERT(socket == socket_);
190 // TODO: we could buffer output too...
191 VERIFY(sizeof(kSslClientHello) ==
192 DirectSend(kSslClientHello, sizeof(kSslClientHello)));
195 void AsyncSSLSocket::ProcessInput(char* data, size_t* len) {
196 if (*len < sizeof(kSslServerHello))
199 if (memcmp(kSslServerHello, data, sizeof(kSslServerHello)) != 0) {
201 SignalCloseEvent(this, 0); // TODO: error code?
205 *len -= sizeof(kSslServerHello);
207 memmove(data, data + sizeof(kSslServerHello), *len);
210 bool remainder = (*len > 0);
212 SignalConnectEvent(this);
214 // FIX: if SignalConnect causes the socket to be destroyed, we are in trouble
216 SignalReadEvent(this);
219 AsyncSSLServerSocket::AsyncSSLServerSocket(AsyncSocket* socket)
220 : BufferedReadAdapter(socket, 1024) {
224 void AsyncSSLServerSocket::ProcessInput(char* data, size_t* len) {
225 // We only accept client hello messages.
226 if (*len < sizeof(kSslClientHello)) {
230 if (memcmp(kSslClientHello, data, sizeof(kSslClientHello)) != 0) {
232 SignalCloseEvent(this, 0);
236 *len -= sizeof(kSslClientHello);
238 // Clients should not send more data until the handshake is completed.
241 // Send a server hello back to the client.
242 DirectSend(kSslServerHello, sizeof(kSslServerHello));
244 // Handshake completed for us, redirect input to our parent.
248 ///////////////////////////////////////////////////////////////////////////////
250 AsyncHttpsProxySocket::AsyncHttpsProxySocket(AsyncSocket* socket,
251 const std::string& user_agent,
252 const SocketAddress& proxy,
253 const std::string& username,
254 const CryptString& password)
255 : BufferedReadAdapter(socket, 1024), proxy_(proxy), agent_(user_agent),
256 user_(username), pass_(password), force_connect_(false), state_(PS_ERROR),
260 AsyncHttpsProxySocket::~AsyncHttpsProxySocket() {
264 int AsyncHttpsProxySocket::Connect(const SocketAddress& addr) {
266 LOG(LS_VERBOSE) << "AsyncHttpsProxySocket::Connect("
267 << proxy_.ToSensitiveString() << ")";
270 if (ShouldIssueConnect()) {
273 ret = BufferedReadAdapter::Connect(proxy_);
274 // TODO: Set state_ appropriately if Connect fails.
278 SocketAddress AsyncHttpsProxySocket::GetRemoteAddress() const {
282 int AsyncHttpsProxySocket::Close() {
288 return BufferedReadAdapter::Close();
291 Socket::ConnState AsyncHttpsProxySocket::GetState() const {
292 if (state_ < PS_TUNNEL) {
293 return CS_CONNECTING;
294 } else if (state_ == PS_TUNNEL) {
301 void AsyncHttpsProxySocket::OnConnectEvent(AsyncSocket * socket) {
302 LOG(LS_VERBOSE) << "AsyncHttpsProxySocket::OnConnectEvent";
303 if (!ShouldIssueConnect()) {
305 BufferedReadAdapter::OnConnectEvent(socket);
311 void AsyncHttpsProxySocket::OnCloseEvent(AsyncSocket * socket, int err) {
312 LOG(LS_VERBOSE) << "AsyncHttpsProxySocket::OnCloseEvent(" << err << ")";
313 if ((state_ == PS_WAIT_CLOSE) && (err == 0)) {
317 BufferedReadAdapter::OnCloseEvent(socket, err);
321 void AsyncHttpsProxySocket::ProcessInput(char* data, size_t* len) {
323 for (size_t pos = start; state_ < PS_TUNNEL && pos < *len;) {
324 if (state_ == PS_SKIP_BODY) {
325 size_t consume = _min(*len - pos, content_length_);
328 content_length_ -= consume;
329 if (content_length_ == 0) {
335 if (data[pos++] != '\n')
338 size_t len = pos - start - 1;
339 if ((len > 0) && (data[start + len - 1] == '\r'))
342 data[start + len] = 0;
343 ProcessLine(data + start, len);
349 memmove(data, data + start, *len);
352 if (state_ != PS_TUNNEL)
355 bool remainder = (*len > 0);
357 SignalConnectEvent(this);
359 // FIX: if SignalConnect causes the socket to be destroyed, we are in trouble
361 SignalReadEvent(this); // TODO: signal this??
364 bool AsyncHttpsProxySocket::ShouldIssueConnect() const {
365 // TODO: Think about whether a more sophisticated test
366 // than dest port == 80 is needed.
367 return force_connect_ || (dest_.port() != 80);
370 void AsyncHttpsProxySocket::SendRequest() {
371 std::stringstream ss;
372 ss << "CONNECT " << dest_.ToString() << " HTTP/1.0\r\n";
373 ss << "User-Agent: " << agent_ << "\r\n";
374 ss << "Host: " << dest_.HostAsURIString() << "\r\n";
375 ss << "Content-Length: 0\r\n";
376 ss << "Proxy-Connection: Keep-Alive\r\n";
379 std::string str = ss.str();
380 DirectSend(str.c_str(), str.size());
382 expect_close_ = true;
386 LOG(LS_VERBOSE) << "AsyncHttpsProxySocket >> " << str;
389 void AsyncHttpsProxySocket::ProcessLine(char * data, size_t len) {
390 LOG(LS_VERBOSE) << "AsyncHttpsProxySocket << " << data;
393 if (state_ == PS_TUNNEL_HEADERS) {
395 } else if (state_ == PS_ERROR_HEADERS) {
398 } else if (state_ == PS_SKIP_HEADERS) {
399 if (content_length_) {
400 state_ = PS_SKIP_BODY;
406 static bool report = false;
407 if (!unknown_mechanisms_.empty() && !report) {
410 "Unable to connect to the Google Talk service due to an incompatibility "
411 "with your proxy.\r\nPlease help us resolve this issue by submitting the "
412 "following information to us using our technical issue submission form "
414 "http://www.google.com/support/talk/bin/request.py\r\n\r\n"
415 "We apologize for the inconvenience.\r\n\r\n"
416 "Information to submit to Google: "
418 //std::string msg("Please report the following information to foo@bar.com:\r\nUnknown methods: ");
419 msg.append(unknown_mechanisms_);
421 MessageBoxA(0, msg.c_str(), "Oops!", MB_OK);
424 // TODO: Raise a signal so the UI can be separated.
425 LOG(LS_ERROR) << "Oops!\n\n" << msg;
428 // Unexpected end of headers
432 } else if (state_ == PS_LEADER) {
434 if (sscanf(data, "HTTP/%*u.%*u %u", &code) != 1) {
441 state_ = PS_TUNNEL_HEADERS;
443 #if defined(HTTP_STATUS_PROXY_AUTH_REQ) && (HTTP_STATUS_PROXY_AUTH_REQ != 407)
444 #error Wrong code for HTTP_STATUS_PROXY_AUTH_REQ
446 case 407: // HTTP_STATUS_PROXY_AUTH_REQ
447 state_ = PS_AUTHENTICATE;
451 state_ = PS_ERROR_HEADERS;
454 } else if ((state_ == PS_AUTHENTICATE)
455 && (_strnicmp(data, "Proxy-Authenticate:", 19) == 0)) {
456 std::string response, auth_method;
457 switch (HttpAuthenticate(data + 19, len - 19,
458 proxy_, "CONNECT", "/",
459 user_, pass_, context_, response, auth_method)) {
461 LOG(LS_VERBOSE) << "Ignoring Proxy-Authenticate: " << auth_method;
462 if (!unknown_mechanisms_.empty())
463 unknown_mechanisms_.append(", ");
464 unknown_mechanisms_.append(auth_method);
467 headers_ = "Proxy-Authorization: ";
468 headers_.append(response);
469 headers_.append("\r\n");
470 state_ = PS_SKIP_HEADERS;
471 unknown_mechanisms_.clear();
473 case HAR_CREDENTIALS:
474 defer_error_ = SOCKET_EACCES;
475 state_ = PS_ERROR_HEADERS;
476 unknown_mechanisms_.clear();
480 state_ = PS_ERROR_HEADERS;
481 unknown_mechanisms_.clear();
484 } else if (_strnicmp(data, "Content-Length:", 15) == 0) {
485 content_length_ = strtoul(data + 15, 0, 0);
486 } else if (_strnicmp(data, "Proxy-Connection: Keep-Alive", 28) == 0) {
487 expect_close_ = false;
489 } else if (_strnicmp(data, "Connection: close", 17) == 0) {
490 expect_close_ = true;
495 void AsyncHttpsProxySocket::EndResponse() {
496 if (!expect_close_) {
501 // No point in waiting for the server to close... let's close now
502 // TODO: Refactor out PS_WAIT_CLOSE
503 state_ = PS_WAIT_CLOSE;
504 BufferedReadAdapter::Close();
505 OnCloseEvent(this, 0);
508 void AsyncHttpsProxySocket::Error(int error) {
512 SignalCloseEvent(this, error);
515 ///////////////////////////////////////////////////////////////////////////////
517 AsyncSocksProxySocket::AsyncSocksProxySocket(AsyncSocket* socket,
518 const SocketAddress& proxy,
519 const std::string& username,
520 const CryptString& password)
521 : BufferedReadAdapter(socket, 1024), state_(SS_ERROR), proxy_(proxy),
522 user_(username), pass_(password) {
525 int AsyncSocksProxySocket::Connect(const SocketAddress& addr) {
530 ret = BufferedReadAdapter::Connect(proxy_);
531 // TODO: Set state_ appropriately if Connect fails.
535 SocketAddress AsyncSocksProxySocket::GetRemoteAddress() const {
539 int AsyncSocksProxySocket::Close() {
542 return BufferedReadAdapter::Close();
545 Socket::ConnState AsyncSocksProxySocket::GetState() const {
546 if (state_ < SS_TUNNEL) {
547 return CS_CONNECTING;
548 } else if (state_ == SS_TUNNEL) {
555 void AsyncSocksProxySocket::OnConnectEvent(AsyncSocket* socket) {
559 void AsyncSocksProxySocket::ProcessInput(char* data, size_t* len) {
560 ASSERT(state_ < SS_TUNNEL);
562 ByteBuffer response(data, *len);
564 if (state_ == SS_HELLO) {
566 if (!response.ReadUInt8(&ver) ||
567 !response.ReadUInt8(&method))
577 } else if (method == 2) {
583 } else if (state_ == SS_AUTH) {
585 if (!response.ReadUInt8(&ver) ||
586 !response.ReadUInt8(&status))
589 if ((ver != 1) || (status != 0)) {
590 Error(SOCKET_EACCES);
595 } else if (state_ == SS_CONNECT) {
596 uint8 ver, rep, rsv, atyp;
597 if (!response.ReadUInt8(&ver) ||
598 !response.ReadUInt8(&rep) ||
599 !response.ReadUInt8(&rsv) ||
600 !response.ReadUInt8(&atyp))
603 if ((ver != 5) || (rep != 0)) {
611 if (!response.ReadUInt32(&addr) ||
612 !response.ReadUInt16(&port))
614 LOG(LS_VERBOSE) << "Bound on " << addr << ":" << port;
615 } else if (atyp == 3) {
618 if (!response.ReadUInt8(&len) ||
619 !response.ReadString(&addr, len) ||
620 !response.ReadUInt16(&port))
622 LOG(LS_VERBOSE) << "Bound on " << addr << ":" << port;
623 } else if (atyp == 4) {
625 if (!response.ReadString(&addr, 16) ||
626 !response.ReadUInt16(&port))
628 LOG(LS_VERBOSE) << "Bound on <IPV6>:" << port;
637 // Consume parsed data
638 *len = response.Length();
639 memcpy(data, response.Data(), *len);
641 if (state_ != SS_TUNNEL)
644 bool remainder = (*len > 0);
646 SignalConnectEvent(this);
648 // FIX: if SignalConnect causes the socket to be destroyed, we are in trouble
650 SignalReadEvent(this); // TODO: signal this??
653 void AsyncSocksProxySocket::SendHello() {
655 request.WriteUInt8(5); // Socks Version
657 request.WriteUInt8(1); // Authentication Mechanisms
658 request.WriteUInt8(0); // No authentication
660 request.WriteUInt8(2); // Authentication Mechanisms
661 request.WriteUInt8(0); // No authentication
662 request.WriteUInt8(2); // Username/Password
664 DirectSend(request.Data(), request.Length());
668 void AsyncSocksProxySocket::SendAuth() {
670 request.WriteUInt8(1); // Negotiation Version
671 request.WriteUInt8(static_cast<uint8>(user_.size()));
672 request.WriteString(user_); // Username
673 request.WriteUInt8(static_cast<uint8>(pass_.GetLength()));
674 size_t len = pass_.GetLength() + 1;
675 char * sensitive = new char[len];
676 pass_.CopyTo(sensitive, true);
677 request.WriteString(sensitive); // Password
678 memset(sensitive, 0, len);
680 DirectSend(request.Data(), request.Length());
684 void AsyncSocksProxySocket::SendConnect() {
686 request.WriteUInt8(5); // Socks Version
687 request.WriteUInt8(1); // CONNECT
688 request.WriteUInt8(0); // Reserved
689 if (dest_.IsUnresolved()) {
690 std::string hostname = dest_.hostname();
691 request.WriteUInt8(3); // DOMAINNAME
692 request.WriteUInt8(static_cast<uint8>(hostname.size()));
693 request.WriteString(hostname); // Destination Hostname
695 request.WriteUInt8(1); // IPV4
696 request.WriteUInt32(dest_.ip()); // Destination IP
698 request.WriteUInt16(dest_.port()); // Destination Port
699 DirectSend(request.Data(), request.Length());
703 void AsyncSocksProxySocket::Error(int error) {
707 SetError(SOCKET_EACCES);
708 SignalCloseEvent(this, error);
711 AsyncSocksProxyServerSocket::AsyncSocksProxyServerSocket(AsyncSocket* socket)
712 : AsyncProxyServerSocket(socket, kBufferSize), state_(SS_HELLO) {
716 void AsyncSocksProxyServerSocket::ProcessInput(char* data, size_t* len) {
717 // TODO: See if the whole message has arrived
718 ASSERT(state_ < SS_CONNECT_PENDING);
720 ByteBuffer response(data, *len);
721 if (state_ == SS_HELLO) {
722 HandleHello(&response);
723 } else if (state_ == SS_AUTH) {
724 HandleAuth(&response);
725 } else if (state_ == SS_CONNECT) {
726 HandleConnect(&response);
729 // Consume parsed data
730 *len = response.Length();
731 memcpy(data, response.Data(), *len);
734 void AsyncSocksProxyServerSocket::DirectSend(const ByteBuffer& buf) {
735 BufferedReadAdapter::DirectSend(buf.Data(), buf.Length());
738 void AsyncSocksProxyServerSocket::HandleHello(ByteBuffer* request) {
739 uint8 ver, num_methods;
740 if (!request->ReadUInt8(&ver) ||
741 !request->ReadUInt8(&num_methods)) {
751 // Handle either no-auth (0) or user/pass auth (2)
753 if (num_methods > 0 && !request->ReadUInt8(&method)) {
758 // TODO: Ask the server which method to use.
759 SendHelloReply(method);
762 } else if (method == 2) {
769 void AsyncSocksProxyServerSocket::SendHelloReply(int method) {
771 response.WriteUInt8(5); // Socks Version
772 response.WriteUInt8(method); // Auth method
773 DirectSend(response);
776 void AsyncSocksProxyServerSocket::HandleAuth(ByteBuffer* request) {
777 uint8 ver, user_len, pass_len;
778 std::string user, pass;
779 if (!request->ReadUInt8(&ver) ||
780 !request->ReadUInt8(&user_len) ||
781 !request->ReadString(&user, user_len) ||
782 !request->ReadUInt8(&pass_len) ||
783 !request->ReadString(&pass, pass_len)) {
788 // TODO: Allow for checking of credentials.
793 void AsyncSocksProxyServerSocket::SendAuthReply(int result) {
795 response.WriteUInt8(1); // Negotiation Version
796 response.WriteUInt8(result);
797 DirectSend(response);
800 void AsyncSocksProxyServerSocket::HandleConnect(ByteBuffer* request) {
801 uint8 ver, command, reserved, addr_type;
804 if (!request->ReadUInt8(&ver) ||
805 !request->ReadUInt8(&command) ||
806 !request->ReadUInt8(&reserved) ||
807 !request->ReadUInt8(&addr_type) ||
808 !request->ReadUInt32(&ip) ||
809 !request->ReadUInt16(&port)) {
814 if (ver != 5 || command != 1 ||
815 reserved != 0 || addr_type != 1) {
820 SignalConnectRequest(this, SocketAddress(ip, port));
821 state_ = SS_CONNECT_PENDING;
824 void AsyncSocksProxyServerSocket::SendConnectResult(int result,
825 const SocketAddress& addr) {
826 if (state_ != SS_CONNECT_PENDING)
830 response.WriteUInt8(5); // Socks version
831 response.WriteUInt8((result != 0)); // 0x01 is generic error
832 response.WriteUInt8(0); // reserved
833 response.WriteUInt8(1); // IPv4 address
834 response.WriteUInt32(addr.ip());
835 response.WriteUInt16(addr.port());
836 DirectSend(response);
841 void AsyncSocksProxyServerSocket::Error(int error) {
845 SetError(SOCKET_EACCES);
846 SignalCloseEvent(this, error);
849 ///////////////////////////////////////////////////////////////////////////////
851 LoggingSocketAdapter::LoggingSocketAdapter(AsyncSocket* socket,
852 LoggingSeverity level,
853 const char * label, bool hex_mode)
854 : AsyncSocketAdapter(socket), level_(level), hex_mode_(hex_mode) {
856 label_.append(label);
860 int LoggingSocketAdapter::Send(const void *pv, size_t cb) {
861 int res = AsyncSocketAdapter::Send(pv, cb);
863 LogMultiline(level_, label_.c_str(), false, pv, res, hex_mode_, &lms_);
867 int LoggingSocketAdapter::SendTo(const void *pv, size_t cb,
868 const SocketAddress& addr) {
869 int res = AsyncSocketAdapter::SendTo(pv, cb, addr);
871 LogMultiline(level_, label_.c_str(), false, pv, res, hex_mode_, &lms_);
875 int LoggingSocketAdapter::Recv(void *pv, size_t cb) {
876 int res = AsyncSocketAdapter::Recv(pv, cb);
878 LogMultiline(level_, label_.c_str(), true, pv, res, hex_mode_, &lms_);
882 int LoggingSocketAdapter::RecvFrom(void *pv, size_t cb, SocketAddress *paddr) {
883 int res = AsyncSocketAdapter::RecvFrom(pv, cb, paddr);
885 LogMultiline(level_, label_.c_str(), true, pv, res, hex_mode_, &lms_);
889 int LoggingSocketAdapter::Close() {
890 LogMultiline(level_, label_.c_str(), false, NULL, 0, hex_mode_, &lms_);
891 LogMultiline(level_, label_.c_str(), true, NULL, 0, hex_mode_, &lms_);
892 LOG_V(level_) << label_ << " Closed locally";
893 return socket_->Close();
896 void LoggingSocketAdapter::OnConnectEvent(AsyncSocket * socket) {
897 LOG_V(level_) << label_ << " Connected";
898 AsyncSocketAdapter::OnConnectEvent(socket);
901 void LoggingSocketAdapter::OnCloseEvent(AsyncSocket * socket, int err) {
902 LogMultiline(level_, label_.c_str(), false, NULL, 0, hex_mode_, &lms_);
903 LogMultiline(level_, label_.c_str(), true, NULL, 0, hex_mode_, &lms_);
904 LOG_V(level_) << label_ << " Closed with error: " << err;
905 AsyncSocketAdapter::OnCloseEvent(socket, err);
908 ///////////////////////////////////////////////////////////////////////////////
910 } // namespace talk_base