henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #if defined(_MSC_VER) && _MSC_VER < 1300 |
| 12 | #pragma warning(disable:4786) |
| 13 | #endif |
| 14 | |
| 15 | #include <time.h> |
| 16 | #include <errno.h> |
| 17 | |
| 18 | #if defined(WEBRTC_WIN) |
| 19 | #define WIN32_LEAN_AND_MEAN |
| 20 | #include <windows.h> |
| 21 | #include <winsock2.h> |
| 22 | #include <ws2tcpip.h> |
| 23 | #define SECURITY_WIN32 |
| 24 | #include <security.h> |
| 25 | #endif |
| 26 | |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 27 | #include <algorithm> |
| 28 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 29 | #include "webrtc/base/bytebuffer.h" |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame^] | 30 | #include "webrtc/base/checks.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 31 | #include "webrtc/base/common.h" |
| 32 | #include "webrtc/base/httpcommon.h" |
| 33 | #include "webrtc/base/logging.h" |
| 34 | #include "webrtc/base/socketadapters.h" |
| 35 | #include "webrtc/base/stringencode.h" |
| 36 | #include "webrtc/base/stringutils.h" |
| 37 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 38 | namespace rtc { |
| 39 | |
| 40 | BufferedReadAdapter::BufferedReadAdapter(AsyncSocket* socket, size_t size) |
| 41 | : AsyncSocketAdapter(socket), buffer_size_(size), |
| 42 | data_len_(0), buffering_(false) { |
| 43 | buffer_ = new char[buffer_size_]; |
| 44 | } |
| 45 | |
| 46 | BufferedReadAdapter::~BufferedReadAdapter() { |
| 47 | delete [] buffer_; |
| 48 | } |
| 49 | |
| 50 | int BufferedReadAdapter::Send(const void *pv, size_t cb) { |
| 51 | if (buffering_) { |
| 52 | // TODO: Spoof error better; Signal Writeable |
| 53 | socket_->SetError(EWOULDBLOCK); |
| 54 | return -1; |
| 55 | } |
| 56 | return AsyncSocketAdapter::Send(pv, cb); |
| 57 | } |
| 58 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 59 | int BufferedReadAdapter::Recv(void* pv, size_t cb, int64_t* timestamp) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 60 | if (buffering_) { |
| 61 | socket_->SetError(EWOULDBLOCK); |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | size_t read = 0; |
| 66 | |
| 67 | if (data_len_) { |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 68 | read = std::min(cb, data_len_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 69 | memcpy(pv, buffer_, read); |
| 70 | data_len_ -= read; |
| 71 | if (data_len_ > 0) { |
| 72 | memmove(buffer_, buffer_ + read, data_len_); |
| 73 | } |
| 74 | pv = static_cast<char *>(pv) + read; |
| 75 | cb -= read; |
| 76 | } |
| 77 | |
| 78 | // FIX: If cb == 0, we won't generate another read event |
| 79 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 80 | int res = AsyncSocketAdapter::Recv(pv, cb, timestamp); |
deadbeef | c5d0d95 | 2015-07-16 10:22:21 -0700 | [diff] [blame] | 81 | if (res >= 0) { |
| 82 | // Read from socket and possibly buffer; return combined length |
| 83 | return res + static_cast<int>(read); |
| 84 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 85 | |
deadbeef | c5d0d95 | 2015-07-16 10:22:21 -0700 | [diff] [blame] | 86 | if (read > 0) { |
| 87 | // Failed to read from socket, but still read something from buffer |
| 88 | return static_cast<int>(read); |
| 89 | } |
| 90 | |
| 91 | // Didn't read anything; return error from socket |
| 92 | return res; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void BufferedReadAdapter::BufferInput(bool on) { |
| 96 | buffering_ = on; |
| 97 | } |
| 98 | |
| 99 | void BufferedReadAdapter::OnReadEvent(AsyncSocket * socket) { |
| 100 | ASSERT(socket == socket_); |
| 101 | |
| 102 | if (!buffering_) { |
| 103 | AsyncSocketAdapter::OnReadEvent(socket); |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | if (data_len_ >= buffer_size_) { |
| 108 | LOG(INFO) << "Input buffer overflow"; |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame^] | 109 | RTC_NOTREACHED(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 110 | data_len_ = 0; |
| 111 | } |
| 112 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 113 | int len = |
| 114 | socket_->Recv(buffer_ + data_len_, buffer_size_ - data_len_, nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 115 | if (len < 0) { |
| 116 | // TODO: Do something better like forwarding the error to the user. |
| 117 | LOG_ERR(INFO) << "Recv"; |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | data_len_ += len; |
| 122 | |
| 123 | ProcessInput(buffer_, &data_len_); |
| 124 | } |
| 125 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 126 | AsyncProxyServerSocket::AsyncProxyServerSocket(AsyncSocket* socket, |
| 127 | size_t buffer_size) |
| 128 | : BufferedReadAdapter(socket, buffer_size) { |
| 129 | } |
| 130 | |
| 131 | AsyncProxyServerSocket::~AsyncProxyServerSocket() = default; |
| 132 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 133 | /////////////////////////////////////////////////////////////////////////////// |
| 134 | |
| 135 | // This is a SSL v2 CLIENT_HELLO message. |
| 136 | // TODO: Should this have a session id? The response doesn't have a |
| 137 | // certificate, so the hello should have a session id. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 138 | static const uint8_t kSslClientHello[] = { |
| 139 | 0x80, 0x46, // msg len |
| 140 | 0x01, // CLIENT_HELLO |
| 141 | 0x03, 0x01, // SSL 3.1 |
| 142 | 0x00, 0x2d, // ciphersuite len |
| 143 | 0x00, 0x00, // session id len |
| 144 | 0x00, 0x10, // challenge len |
| 145 | 0x01, 0x00, 0x80, 0x03, 0x00, 0x80, 0x07, 0x00, 0xc0, // ciphersuites |
| 146 | 0x06, 0x00, 0x40, 0x02, 0x00, 0x80, 0x04, 0x00, 0x80, // |
| 147 | 0x00, 0x00, 0x04, 0x00, 0xfe, 0xff, 0x00, 0x00, 0x0a, // |
| 148 | 0x00, 0xfe, 0xfe, 0x00, 0x00, 0x09, 0x00, 0x00, 0x64, // |
| 149 | 0x00, 0x00, 0x62, 0x00, 0x00, 0x03, 0x00, 0x00, 0x06, // |
| 150 | 0x1f, 0x17, 0x0c, 0xa6, 0x2f, 0x00, 0x78, 0xfc, // challenge |
| 151 | 0x46, 0x55, 0x2e, 0xb1, 0x83, 0x39, 0xf1, 0xea // |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | // This is a TLSv1 SERVER_HELLO message. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 155 | static const uint8_t kSslServerHello[] = { |
| 156 | 0x16, // handshake message |
| 157 | 0x03, 0x01, // SSL 3.1 |
| 158 | 0x00, 0x4a, // message len |
| 159 | 0x02, // SERVER_HELLO |
| 160 | 0x00, 0x00, 0x46, // handshake len |
| 161 | 0x03, 0x01, // SSL 3.1 |
| 162 | 0x42, 0x85, 0x45, 0xa7, 0x27, 0xa9, 0x5d, 0xa0, // server random |
| 163 | 0xb3, 0xc5, 0xe7, 0x53, 0xda, 0x48, 0x2b, 0x3f, // |
| 164 | 0xc6, 0x5a, 0xca, 0x89, 0xc1, 0x58, 0x52, 0xa1, // |
| 165 | 0x78, 0x3c, 0x5b, 0x17, 0x46, 0x00, 0x85, 0x3f, // |
| 166 | 0x20, // session id len |
| 167 | 0x0e, 0xd3, 0x06, 0x72, 0x5b, 0x5b, 0x1b, 0x5f, // session id |
| 168 | 0x15, 0xac, 0x13, 0xf9, 0x88, 0x53, 0x9d, 0x9b, // |
| 169 | 0xe8, 0x3d, 0x7b, 0x0c, 0x30, 0x32, 0x6e, 0x38, // |
| 170 | 0x4d, 0xa2, 0x75, 0x57, 0x41, 0x6c, 0x34, 0x5c, // |
| 171 | 0x00, 0x04, // RSA/RC4-128/MD5 |
| 172 | 0x00 // null compression |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | AsyncSSLSocket::AsyncSSLSocket(AsyncSocket* socket) |
| 176 | : BufferedReadAdapter(socket, 1024) { |
| 177 | } |
| 178 | |
| 179 | int AsyncSSLSocket::Connect(const SocketAddress& addr) { |
| 180 | // Begin buffering before we connect, so that there isn't a race condition |
| 181 | // between potential senders and receiving the OnConnectEvent signal |
| 182 | BufferInput(true); |
| 183 | return BufferedReadAdapter::Connect(addr); |
| 184 | } |
| 185 | |
| 186 | void AsyncSSLSocket::OnConnectEvent(AsyncSocket * socket) { |
| 187 | ASSERT(socket == socket_); |
| 188 | // TODO: we could buffer output too... |
| 189 | VERIFY(sizeof(kSslClientHello) == |
| 190 | DirectSend(kSslClientHello, sizeof(kSslClientHello))); |
| 191 | } |
| 192 | |
| 193 | void AsyncSSLSocket::ProcessInput(char* data, size_t* len) { |
| 194 | if (*len < sizeof(kSslServerHello)) |
| 195 | return; |
| 196 | |
| 197 | if (memcmp(kSslServerHello, data, sizeof(kSslServerHello)) != 0) { |
| 198 | Close(); |
| 199 | SignalCloseEvent(this, 0); // TODO: error code? |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | *len -= sizeof(kSslServerHello); |
| 204 | if (*len > 0) { |
| 205 | memmove(data, data + sizeof(kSslServerHello), *len); |
| 206 | } |
| 207 | |
| 208 | bool remainder = (*len > 0); |
| 209 | BufferInput(false); |
| 210 | SignalConnectEvent(this); |
| 211 | |
| 212 | // FIX: if SignalConnect causes the socket to be destroyed, we are in trouble |
| 213 | if (remainder) |
| 214 | SignalReadEvent(this); |
| 215 | } |
| 216 | |
| 217 | AsyncSSLServerSocket::AsyncSSLServerSocket(AsyncSocket* socket) |
| 218 | : BufferedReadAdapter(socket, 1024) { |
| 219 | BufferInput(true); |
| 220 | } |
| 221 | |
| 222 | void AsyncSSLServerSocket::ProcessInput(char* data, size_t* len) { |
| 223 | // We only accept client hello messages. |
| 224 | if (*len < sizeof(kSslClientHello)) { |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | if (memcmp(kSslClientHello, data, sizeof(kSslClientHello)) != 0) { |
| 229 | Close(); |
| 230 | SignalCloseEvent(this, 0); |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | *len -= sizeof(kSslClientHello); |
| 235 | |
| 236 | // Clients should not send more data until the handshake is completed. |
| 237 | ASSERT(*len == 0); |
| 238 | |
| 239 | // Send a server hello back to the client. |
| 240 | DirectSend(kSslServerHello, sizeof(kSslServerHello)); |
| 241 | |
| 242 | // Handshake completed for us, redirect input to our parent. |
| 243 | BufferInput(false); |
| 244 | } |
| 245 | |
| 246 | /////////////////////////////////////////////////////////////////////////////// |
| 247 | |
| 248 | AsyncHttpsProxySocket::AsyncHttpsProxySocket(AsyncSocket* socket, |
| 249 | const std::string& user_agent, |
| 250 | const SocketAddress& proxy, |
| 251 | const std::string& username, |
| 252 | const CryptString& password) |
| 253 | : BufferedReadAdapter(socket, 1024), proxy_(proxy), agent_(user_agent), |
| 254 | user_(username), pass_(password), force_connect_(false), state_(PS_ERROR), |
| 255 | context_(0) { |
| 256 | } |
| 257 | |
| 258 | AsyncHttpsProxySocket::~AsyncHttpsProxySocket() { |
| 259 | delete context_; |
| 260 | } |
| 261 | |
| 262 | int AsyncHttpsProxySocket::Connect(const SocketAddress& addr) { |
| 263 | int ret; |
| 264 | LOG(LS_VERBOSE) << "AsyncHttpsProxySocket::Connect(" |
| 265 | << proxy_.ToSensitiveString() << ")"; |
| 266 | dest_ = addr; |
| 267 | state_ = PS_INIT; |
| 268 | if (ShouldIssueConnect()) { |
| 269 | BufferInput(true); |
| 270 | } |
| 271 | ret = BufferedReadAdapter::Connect(proxy_); |
| 272 | // TODO: Set state_ appropriately if Connect fails. |
| 273 | return ret; |
| 274 | } |
| 275 | |
| 276 | SocketAddress AsyncHttpsProxySocket::GetRemoteAddress() const { |
| 277 | return dest_; |
| 278 | } |
| 279 | |
| 280 | int AsyncHttpsProxySocket::Close() { |
| 281 | headers_.clear(); |
| 282 | state_ = PS_ERROR; |
| 283 | dest_.Clear(); |
| 284 | delete context_; |
| 285 | context_ = NULL; |
| 286 | return BufferedReadAdapter::Close(); |
| 287 | } |
| 288 | |
| 289 | Socket::ConnState AsyncHttpsProxySocket::GetState() const { |
| 290 | if (state_ < PS_TUNNEL) { |
| 291 | return CS_CONNECTING; |
| 292 | } else if (state_ == PS_TUNNEL) { |
| 293 | return CS_CONNECTED; |
| 294 | } else { |
| 295 | return CS_CLOSED; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | void AsyncHttpsProxySocket::OnConnectEvent(AsyncSocket * socket) { |
| 300 | LOG(LS_VERBOSE) << "AsyncHttpsProxySocket::OnConnectEvent"; |
| 301 | if (!ShouldIssueConnect()) { |
| 302 | state_ = PS_TUNNEL; |
| 303 | BufferedReadAdapter::OnConnectEvent(socket); |
| 304 | return; |
| 305 | } |
| 306 | SendRequest(); |
| 307 | } |
| 308 | |
| 309 | void AsyncHttpsProxySocket::OnCloseEvent(AsyncSocket * socket, int err) { |
| 310 | LOG(LS_VERBOSE) << "AsyncHttpsProxySocket::OnCloseEvent(" << err << ")"; |
| 311 | if ((state_ == PS_WAIT_CLOSE) && (err == 0)) { |
| 312 | state_ = PS_ERROR; |
| 313 | Connect(dest_); |
| 314 | } else { |
| 315 | BufferedReadAdapter::OnCloseEvent(socket, err); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | void AsyncHttpsProxySocket::ProcessInput(char* data, size_t* len) { |
| 320 | size_t start = 0; |
| 321 | for (size_t pos = start; state_ < PS_TUNNEL && pos < *len;) { |
| 322 | if (state_ == PS_SKIP_BODY) { |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 323 | size_t consume = std::min(*len - pos, content_length_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 324 | pos += consume; |
| 325 | start = pos; |
| 326 | content_length_ -= consume; |
| 327 | if (content_length_ == 0) { |
| 328 | EndResponse(); |
| 329 | } |
| 330 | continue; |
| 331 | } |
| 332 | |
| 333 | if (data[pos++] != '\n') |
| 334 | continue; |
| 335 | |
| 336 | size_t len = pos - start - 1; |
| 337 | if ((len > 0) && (data[start + len - 1] == '\r')) |
| 338 | --len; |
| 339 | |
| 340 | data[start + len] = 0; |
| 341 | ProcessLine(data + start, len); |
| 342 | start = pos; |
| 343 | } |
| 344 | |
| 345 | *len -= start; |
| 346 | if (*len > 0) { |
| 347 | memmove(data, data + start, *len); |
| 348 | } |
| 349 | |
| 350 | if (state_ != PS_TUNNEL) |
| 351 | return; |
| 352 | |
| 353 | bool remainder = (*len > 0); |
| 354 | BufferInput(false); |
| 355 | SignalConnectEvent(this); |
| 356 | |
| 357 | // FIX: if SignalConnect causes the socket to be destroyed, we are in trouble |
| 358 | if (remainder) |
| 359 | SignalReadEvent(this); // TODO: signal this?? |
| 360 | } |
| 361 | |
| 362 | bool AsyncHttpsProxySocket::ShouldIssueConnect() const { |
| 363 | // TODO: Think about whether a more sophisticated test |
| 364 | // than dest port == 80 is needed. |
| 365 | return force_connect_ || (dest_.port() != 80); |
| 366 | } |
| 367 | |
| 368 | void AsyncHttpsProxySocket::SendRequest() { |
| 369 | std::stringstream ss; |
| 370 | ss << "CONNECT " << dest_.ToString() << " HTTP/1.0\r\n"; |
| 371 | ss << "User-Agent: " << agent_ << "\r\n"; |
| 372 | ss << "Host: " << dest_.HostAsURIString() << "\r\n"; |
| 373 | ss << "Content-Length: 0\r\n"; |
| 374 | ss << "Proxy-Connection: Keep-Alive\r\n"; |
| 375 | ss << headers_; |
| 376 | ss << "\r\n"; |
| 377 | std::string str = ss.str(); |
| 378 | DirectSend(str.c_str(), str.size()); |
| 379 | state_ = PS_LEADER; |
| 380 | expect_close_ = true; |
| 381 | content_length_ = 0; |
| 382 | headers_.clear(); |
| 383 | |
| 384 | LOG(LS_VERBOSE) << "AsyncHttpsProxySocket >> " << str; |
| 385 | } |
| 386 | |
| 387 | void AsyncHttpsProxySocket::ProcessLine(char * data, size_t len) { |
| 388 | LOG(LS_VERBOSE) << "AsyncHttpsProxySocket << " << data; |
| 389 | |
| 390 | if (len == 0) { |
| 391 | if (state_ == PS_TUNNEL_HEADERS) { |
| 392 | state_ = PS_TUNNEL; |
| 393 | } else if (state_ == PS_ERROR_HEADERS) { |
| 394 | Error(defer_error_); |
| 395 | return; |
| 396 | } else if (state_ == PS_SKIP_HEADERS) { |
| 397 | if (content_length_) { |
| 398 | state_ = PS_SKIP_BODY; |
| 399 | } else { |
| 400 | EndResponse(); |
| 401 | return; |
| 402 | } |
| 403 | } else { |
| 404 | static bool report = false; |
| 405 | if (!unknown_mechanisms_.empty() && !report) { |
| 406 | report = true; |
| 407 | std::string msg( |
| 408 | "Unable to connect to the Google Talk service due to an incompatibility " |
| 409 | "with your proxy.\r\nPlease help us resolve this issue by submitting the " |
| 410 | "following information to us using our technical issue submission form " |
| 411 | "at:\r\n\r\n" |
| 412 | "http://www.google.com/support/talk/bin/request.py\r\n\r\n" |
| 413 | "We apologize for the inconvenience.\r\n\r\n" |
| 414 | "Information to submit to Google: " |
| 415 | ); |
| 416 | //std::string msg("Please report the following information to foo@bar.com:\r\nUnknown methods: "); |
| 417 | msg.append(unknown_mechanisms_); |
| 418 | #if defined(WEBRTC_WIN) |
| 419 | MessageBoxA(0, msg.c_str(), "Oops!", MB_OK); |
| 420 | #endif |
| 421 | #if defined(WEBRTC_POSIX) |
| 422 | // TODO: Raise a signal so the UI can be separated. |
| 423 | LOG(LS_ERROR) << "Oops!\n\n" << msg; |
| 424 | #endif |
| 425 | } |
| 426 | // Unexpected end of headers |
| 427 | Error(0); |
| 428 | return; |
| 429 | } |
| 430 | } else if (state_ == PS_LEADER) { |
| 431 | unsigned int code; |
| 432 | if (sscanf(data, "HTTP/%*u.%*u %u", &code) != 1) { |
| 433 | Error(0); |
| 434 | return; |
| 435 | } |
| 436 | switch (code) { |
| 437 | case 200: |
| 438 | // connection good! |
| 439 | state_ = PS_TUNNEL_HEADERS; |
| 440 | return; |
| 441 | #if defined(HTTP_STATUS_PROXY_AUTH_REQ) && (HTTP_STATUS_PROXY_AUTH_REQ != 407) |
| 442 | #error Wrong code for HTTP_STATUS_PROXY_AUTH_REQ |
| 443 | #endif |
| 444 | case 407: // HTTP_STATUS_PROXY_AUTH_REQ |
| 445 | state_ = PS_AUTHENTICATE; |
| 446 | return; |
| 447 | default: |
| 448 | defer_error_ = 0; |
| 449 | state_ = PS_ERROR_HEADERS; |
| 450 | return; |
| 451 | } |
| 452 | } else if ((state_ == PS_AUTHENTICATE) |
| 453 | && (_strnicmp(data, "Proxy-Authenticate:", 19) == 0)) { |
| 454 | std::string response, auth_method; |
| 455 | switch (HttpAuthenticate(data + 19, len - 19, |
| 456 | proxy_, "CONNECT", "/", |
| 457 | user_, pass_, context_, response, auth_method)) { |
| 458 | case HAR_IGNORE: |
| 459 | LOG(LS_VERBOSE) << "Ignoring Proxy-Authenticate: " << auth_method; |
| 460 | if (!unknown_mechanisms_.empty()) |
| 461 | unknown_mechanisms_.append(", "); |
| 462 | unknown_mechanisms_.append(auth_method); |
| 463 | break; |
| 464 | case HAR_RESPONSE: |
| 465 | headers_ = "Proxy-Authorization: "; |
| 466 | headers_.append(response); |
| 467 | headers_.append("\r\n"); |
| 468 | state_ = PS_SKIP_HEADERS; |
| 469 | unknown_mechanisms_.clear(); |
| 470 | break; |
| 471 | case HAR_CREDENTIALS: |
| 472 | defer_error_ = SOCKET_EACCES; |
| 473 | state_ = PS_ERROR_HEADERS; |
| 474 | unknown_mechanisms_.clear(); |
| 475 | break; |
| 476 | case HAR_ERROR: |
| 477 | defer_error_ = 0; |
| 478 | state_ = PS_ERROR_HEADERS; |
| 479 | unknown_mechanisms_.clear(); |
| 480 | break; |
| 481 | } |
| 482 | } else if (_strnicmp(data, "Content-Length:", 15) == 0) { |
| 483 | content_length_ = strtoul(data + 15, 0, 0); |
| 484 | } else if (_strnicmp(data, "Proxy-Connection: Keep-Alive", 28) == 0) { |
| 485 | expect_close_ = false; |
| 486 | /* |
| 487 | } else if (_strnicmp(data, "Connection: close", 17) == 0) { |
| 488 | expect_close_ = true; |
| 489 | */ |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | void AsyncHttpsProxySocket::EndResponse() { |
| 494 | if (!expect_close_) { |
| 495 | SendRequest(); |
| 496 | return; |
| 497 | } |
| 498 | |
| 499 | // No point in waiting for the server to close... let's close now |
| 500 | // TODO: Refactor out PS_WAIT_CLOSE |
| 501 | state_ = PS_WAIT_CLOSE; |
| 502 | BufferedReadAdapter::Close(); |
| 503 | OnCloseEvent(this, 0); |
| 504 | } |
| 505 | |
| 506 | void AsyncHttpsProxySocket::Error(int error) { |
| 507 | BufferInput(false); |
| 508 | Close(); |
| 509 | SetError(error); |
| 510 | SignalCloseEvent(this, error); |
| 511 | } |
| 512 | |
| 513 | /////////////////////////////////////////////////////////////////////////////// |
| 514 | |
| 515 | AsyncSocksProxySocket::AsyncSocksProxySocket(AsyncSocket* socket, |
| 516 | const SocketAddress& proxy, |
| 517 | const std::string& username, |
| 518 | const CryptString& password) |
| 519 | : BufferedReadAdapter(socket, 1024), state_(SS_ERROR), proxy_(proxy), |
| 520 | user_(username), pass_(password) { |
| 521 | } |
| 522 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 523 | AsyncSocksProxySocket::~AsyncSocksProxySocket() = default; |
| 524 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 525 | int AsyncSocksProxySocket::Connect(const SocketAddress& addr) { |
| 526 | int ret; |
| 527 | dest_ = addr; |
| 528 | state_ = SS_INIT; |
| 529 | BufferInput(true); |
| 530 | ret = BufferedReadAdapter::Connect(proxy_); |
| 531 | // TODO: Set state_ appropriately if Connect fails. |
| 532 | return ret; |
| 533 | } |
| 534 | |
| 535 | SocketAddress AsyncSocksProxySocket::GetRemoteAddress() const { |
| 536 | return dest_; |
| 537 | } |
| 538 | |
| 539 | int AsyncSocksProxySocket::Close() { |
| 540 | state_ = SS_ERROR; |
| 541 | dest_.Clear(); |
| 542 | return BufferedReadAdapter::Close(); |
| 543 | } |
| 544 | |
| 545 | Socket::ConnState AsyncSocksProxySocket::GetState() const { |
| 546 | if (state_ < SS_TUNNEL) { |
| 547 | return CS_CONNECTING; |
| 548 | } else if (state_ == SS_TUNNEL) { |
| 549 | return CS_CONNECTED; |
| 550 | } else { |
| 551 | return CS_CLOSED; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | void AsyncSocksProxySocket::OnConnectEvent(AsyncSocket* socket) { |
| 556 | SendHello(); |
| 557 | } |
| 558 | |
| 559 | void AsyncSocksProxySocket::ProcessInput(char* data, size_t* len) { |
| 560 | ASSERT(state_ < SS_TUNNEL); |
| 561 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 562 | ByteBufferReader response(data, *len); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 563 | |
| 564 | if (state_ == SS_HELLO) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 565 | uint8_t ver, method; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 566 | if (!response.ReadUInt8(&ver) || |
| 567 | !response.ReadUInt8(&method)) |
| 568 | return; |
| 569 | |
| 570 | if (ver != 5) { |
| 571 | Error(0); |
| 572 | return; |
| 573 | } |
| 574 | |
| 575 | if (method == 0) { |
| 576 | SendConnect(); |
| 577 | } else if (method == 2) { |
| 578 | SendAuth(); |
| 579 | } else { |
| 580 | Error(0); |
| 581 | return; |
| 582 | } |
| 583 | } else if (state_ == SS_AUTH) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 584 | uint8_t ver, status; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 585 | if (!response.ReadUInt8(&ver) || |
| 586 | !response.ReadUInt8(&status)) |
| 587 | return; |
| 588 | |
| 589 | if ((ver != 1) || (status != 0)) { |
| 590 | Error(SOCKET_EACCES); |
| 591 | return; |
| 592 | } |
| 593 | |
| 594 | SendConnect(); |
| 595 | } else if (state_ == SS_CONNECT) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 596 | uint8_t ver, rep, rsv, atyp; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 597 | if (!response.ReadUInt8(&ver) || |
| 598 | !response.ReadUInt8(&rep) || |
| 599 | !response.ReadUInt8(&rsv) || |
| 600 | !response.ReadUInt8(&atyp)) |
| 601 | return; |
| 602 | |
| 603 | if ((ver != 5) || (rep != 0)) { |
| 604 | Error(0); |
| 605 | return; |
| 606 | } |
| 607 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 608 | uint16_t port; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 609 | if (atyp == 1) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 610 | uint32_t addr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 611 | if (!response.ReadUInt32(&addr) || |
| 612 | !response.ReadUInt16(&port)) |
| 613 | return; |
| 614 | LOG(LS_VERBOSE) << "Bound on " << addr << ":" << port; |
| 615 | } else if (atyp == 3) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 616 | uint8_t len; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 617 | std::string addr; |
| 618 | if (!response.ReadUInt8(&len) || |
| 619 | !response.ReadString(&addr, len) || |
| 620 | !response.ReadUInt16(&port)) |
| 621 | return; |
| 622 | LOG(LS_VERBOSE) << "Bound on " << addr << ":" << port; |
| 623 | } else if (atyp == 4) { |
| 624 | std::string addr; |
| 625 | if (!response.ReadString(&addr, 16) || |
| 626 | !response.ReadUInt16(&port)) |
| 627 | return; |
| 628 | LOG(LS_VERBOSE) << "Bound on <IPV6>:" << port; |
| 629 | } else { |
| 630 | Error(0); |
| 631 | return; |
| 632 | } |
| 633 | |
| 634 | state_ = SS_TUNNEL; |
| 635 | } |
| 636 | |
| 637 | // Consume parsed data |
| 638 | *len = response.Length(); |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 639 | memmove(data, response.Data(), *len); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 640 | |
| 641 | if (state_ != SS_TUNNEL) |
| 642 | return; |
| 643 | |
| 644 | bool remainder = (*len > 0); |
| 645 | BufferInput(false); |
| 646 | SignalConnectEvent(this); |
| 647 | |
| 648 | // FIX: if SignalConnect causes the socket to be destroyed, we are in trouble |
| 649 | if (remainder) |
| 650 | SignalReadEvent(this); // TODO: signal this?? |
| 651 | } |
| 652 | |
| 653 | void AsyncSocksProxySocket::SendHello() { |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 654 | ByteBufferWriter request; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 655 | request.WriteUInt8(5); // Socks Version |
| 656 | if (user_.empty()) { |
| 657 | request.WriteUInt8(1); // Authentication Mechanisms |
| 658 | request.WriteUInt8(0); // No authentication |
| 659 | } else { |
| 660 | request.WriteUInt8(2); // Authentication Mechanisms |
| 661 | request.WriteUInt8(0); // No authentication |
| 662 | request.WriteUInt8(2); // Username/Password |
| 663 | } |
| 664 | DirectSend(request.Data(), request.Length()); |
| 665 | state_ = SS_HELLO; |
| 666 | } |
| 667 | |
| 668 | void AsyncSocksProxySocket::SendAuth() { |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 669 | ByteBufferWriter request; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 670 | request.WriteUInt8(1); // Negotiation Version |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 671 | request.WriteUInt8(static_cast<uint8_t>(user_.size())); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 672 | request.WriteString(user_); // Username |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 673 | request.WriteUInt8(static_cast<uint8_t>(pass_.GetLength())); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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); |
| 679 | delete [] sensitive; |
| 680 | DirectSend(request.Data(), request.Length()); |
| 681 | state_ = SS_AUTH; |
| 682 | } |
| 683 | |
| 684 | void AsyncSocksProxySocket::SendConnect() { |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 685 | ByteBufferWriter request; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 686 | request.WriteUInt8(5); // Socks Version |
| 687 | request.WriteUInt8(1); // CONNECT |
| 688 | request.WriteUInt8(0); // Reserved |
tfarina | 20a3461 | 2015-11-02 16:20:22 -0800 | [diff] [blame] | 689 | if (dest_.IsUnresolvedIP()) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 690 | std::string hostname = dest_.hostname(); |
| 691 | request.WriteUInt8(3); // DOMAINNAME |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 692 | request.WriteUInt8(static_cast<uint8_t>(hostname.size())); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 693 | request.WriteString(hostname); // Destination Hostname |
| 694 | } else { |
| 695 | request.WriteUInt8(1); // IPV4 |
| 696 | request.WriteUInt32(dest_.ip()); // Destination IP |
| 697 | } |
| 698 | request.WriteUInt16(dest_.port()); // Destination Port |
| 699 | DirectSend(request.Data(), request.Length()); |
| 700 | state_ = SS_CONNECT; |
| 701 | } |
| 702 | |
| 703 | void AsyncSocksProxySocket::Error(int error) { |
| 704 | state_ = SS_ERROR; |
| 705 | BufferInput(false); |
| 706 | Close(); |
| 707 | SetError(SOCKET_EACCES); |
| 708 | SignalCloseEvent(this, error); |
| 709 | } |
| 710 | |
| 711 | AsyncSocksProxyServerSocket::AsyncSocksProxyServerSocket(AsyncSocket* socket) |
| 712 | : AsyncProxyServerSocket(socket, kBufferSize), state_(SS_HELLO) { |
| 713 | BufferInput(true); |
| 714 | } |
| 715 | |
| 716 | void AsyncSocksProxyServerSocket::ProcessInput(char* data, size_t* len) { |
| 717 | // TODO: See if the whole message has arrived |
| 718 | ASSERT(state_ < SS_CONNECT_PENDING); |
| 719 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 720 | ByteBufferReader response(data, *len); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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); |
| 727 | } |
| 728 | |
| 729 | // Consume parsed data |
| 730 | *len = response.Length(); |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 731 | memmove(data, response.Data(), *len); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 732 | } |
| 733 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 734 | void AsyncSocksProxyServerSocket::DirectSend(const ByteBufferWriter& buf) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 735 | BufferedReadAdapter::DirectSend(buf.Data(), buf.Length()); |
| 736 | } |
| 737 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 738 | void AsyncSocksProxyServerSocket::HandleHello(ByteBufferReader* request) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 739 | uint8_t ver, num_methods; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 740 | if (!request->ReadUInt8(&ver) || |
| 741 | !request->ReadUInt8(&num_methods)) { |
| 742 | Error(0); |
| 743 | return; |
| 744 | } |
| 745 | |
| 746 | if (ver != 5) { |
| 747 | Error(0); |
| 748 | return; |
| 749 | } |
| 750 | |
| 751 | // Handle either no-auth (0) or user/pass auth (2) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 752 | uint8_t method = 0xFF; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 753 | if (num_methods > 0 && !request->ReadUInt8(&method)) { |
| 754 | Error(0); |
| 755 | return; |
| 756 | } |
| 757 | |
| 758 | // TODO: Ask the server which method to use. |
| 759 | SendHelloReply(method); |
| 760 | if (method == 0) { |
| 761 | state_ = SS_CONNECT; |
| 762 | } else if (method == 2) { |
| 763 | state_ = SS_AUTH; |
| 764 | } else { |
| 765 | state_ = SS_ERROR; |
| 766 | } |
| 767 | } |
| 768 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 769 | void AsyncSocksProxyServerSocket::SendHelloReply(uint8_t method) { |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 770 | ByteBufferWriter response; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 771 | response.WriteUInt8(5); // Socks Version |
| 772 | response.WriteUInt8(method); // Auth method |
| 773 | DirectSend(response); |
| 774 | } |
| 775 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 776 | void AsyncSocksProxyServerSocket::HandleAuth(ByteBufferReader* request) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 777 | uint8_t ver, user_len, pass_len; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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)) { |
| 784 | Error(0); |
| 785 | return; |
| 786 | } |
| 787 | |
| 788 | // TODO: Allow for checking of credentials. |
| 789 | SendAuthReply(0); |
| 790 | state_ = SS_CONNECT; |
| 791 | } |
| 792 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 793 | void AsyncSocksProxyServerSocket::SendAuthReply(uint8_t result) { |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 794 | ByteBufferWriter response; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 795 | response.WriteUInt8(1); // Negotiation Version |
| 796 | response.WriteUInt8(result); |
| 797 | DirectSend(response); |
| 798 | } |
| 799 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 800 | void AsyncSocksProxyServerSocket::HandleConnect(ByteBufferReader* request) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 801 | uint8_t ver, command, reserved, addr_type; |
| 802 | uint32_t ip; |
| 803 | uint16_t port; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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)) { |
| 810 | Error(0); |
| 811 | return; |
| 812 | } |
| 813 | |
| 814 | if (ver != 5 || command != 1 || |
| 815 | reserved != 0 || addr_type != 1) { |
| 816 | Error(0); |
| 817 | return; |
| 818 | } |
| 819 | |
| 820 | SignalConnectRequest(this, SocketAddress(ip, port)); |
| 821 | state_ = SS_CONNECT_PENDING; |
| 822 | } |
| 823 | |
| 824 | void AsyncSocksProxyServerSocket::SendConnectResult(int result, |
| 825 | const SocketAddress& addr) { |
| 826 | if (state_ != SS_CONNECT_PENDING) |
| 827 | return; |
| 828 | |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 829 | ByteBufferWriter response; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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); |
| 837 | BufferInput(false); |
| 838 | state_ = SS_TUNNEL; |
| 839 | } |
| 840 | |
| 841 | void AsyncSocksProxyServerSocket::Error(int error) { |
| 842 | state_ = SS_ERROR; |
| 843 | BufferInput(false); |
| 844 | Close(); |
| 845 | SetError(SOCKET_EACCES); |
| 846 | SignalCloseEvent(this, error); |
| 847 | } |
| 848 | |
| 849 | /////////////////////////////////////////////////////////////////////////////// |
| 850 | |
| 851 | LoggingSocketAdapter::LoggingSocketAdapter(AsyncSocket* socket, |
| 852 | LoggingSeverity level, |
| 853 | const char * label, bool hex_mode) |
| 854 | : AsyncSocketAdapter(socket), level_(level), hex_mode_(hex_mode) { |
| 855 | label_.append("["); |
| 856 | label_.append(label); |
| 857 | label_.append("]"); |
| 858 | } |
| 859 | |
| 860 | int LoggingSocketAdapter::Send(const void *pv, size_t cb) { |
| 861 | int res = AsyncSocketAdapter::Send(pv, cb); |
| 862 | if (res > 0) |
| 863 | LogMultiline(level_, label_.c_str(), false, pv, res, hex_mode_, &lms_); |
| 864 | return res; |
| 865 | } |
| 866 | |
| 867 | int LoggingSocketAdapter::SendTo(const void *pv, size_t cb, |
| 868 | const SocketAddress& addr) { |
| 869 | int res = AsyncSocketAdapter::SendTo(pv, cb, addr); |
| 870 | if (res > 0) |
| 871 | LogMultiline(level_, label_.c_str(), false, pv, res, hex_mode_, &lms_); |
| 872 | return res; |
| 873 | } |
| 874 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 875 | int LoggingSocketAdapter::Recv(void* pv, size_t cb, int64_t* timestamp) { |
| 876 | int res = AsyncSocketAdapter::Recv(pv, cb, timestamp); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 877 | if (res > 0) |
| 878 | LogMultiline(level_, label_.c_str(), true, pv, res, hex_mode_, &lms_); |
| 879 | return res; |
| 880 | } |
| 881 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 882 | int LoggingSocketAdapter::RecvFrom(void* pv, |
| 883 | size_t cb, |
| 884 | SocketAddress* paddr, |
| 885 | int64_t* timestamp) { |
| 886 | int res = AsyncSocketAdapter::RecvFrom(pv, cb, paddr, timestamp); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 887 | if (res > 0) |
| 888 | LogMultiline(level_, label_.c_str(), true, pv, res, hex_mode_, &lms_); |
| 889 | return res; |
| 890 | } |
| 891 | |
| 892 | int LoggingSocketAdapter::Close() { |
| 893 | LogMultiline(level_, label_.c_str(), false, NULL, 0, hex_mode_, &lms_); |
| 894 | LogMultiline(level_, label_.c_str(), true, NULL, 0, hex_mode_, &lms_); |
| 895 | LOG_V(level_) << label_ << " Closed locally"; |
| 896 | return socket_->Close(); |
| 897 | } |
| 898 | |
| 899 | void LoggingSocketAdapter::OnConnectEvent(AsyncSocket * socket) { |
| 900 | LOG_V(level_) << label_ << " Connected"; |
| 901 | AsyncSocketAdapter::OnConnectEvent(socket); |
| 902 | } |
| 903 | |
| 904 | void LoggingSocketAdapter::OnCloseEvent(AsyncSocket * socket, int err) { |
| 905 | LogMultiline(level_, label_.c_str(), false, NULL, 0, hex_mode_, &lms_); |
| 906 | LogMultiline(level_, label_.c_str(), true, NULL, 0, hex_mode_, &lms_); |
| 907 | LOG_V(level_) << label_ << " Closed with error: " << err; |
| 908 | AsyncSocketAdapter::OnCloseEvent(socket, err); |
| 909 | } |
| 910 | |
| 911 | /////////////////////////////////////////////////////////////////////////////// |
| 912 | |
| 913 | } // namespace rtc |