| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/socket/socket_bio_adapter.h" | 5 #include "net/socket/socket_bio_adapter.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/metrics/field_trial.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 15 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
| 16 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 18 #include "net/socket/socket.h" |
| 17 #include "net/socket/stream_socket.h" | 19 #include "net/socket/stream_socket.h" |
| 18 #include "net/ssl/openssl_ssl_util.h" | 20 #include "net/ssl/openssl_ssl_util.h" |
| 19 #include "third_party/boringssl/src/include/openssl/bio.h" | 21 #include "third_party/boringssl/src/include/openssl/bio.h" |
| 20 | 22 |
| 21 namespace net { | 23 namespace net { |
| 22 | 24 |
| 23 SocketBIOAdapter::SocketBIOAdapter(StreamSocket* socket, | 25 SocketBIOAdapter::SocketBIOAdapter(StreamSocket* socket, |
| 24 int read_buffer_capacity, | 26 int read_buffer_capacity, |
| 25 int write_buffer_capacity, | 27 int write_buffer_capacity, |
| 26 Delegate* delegate) | 28 Delegate* delegate) |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 | 82 |
| 81 if (read_result_ == 0) { | 83 if (read_result_ == 0) { |
| 82 // Instantiate the read buffer and read from the socket. Although only |len| | 84 // Instantiate the read buffer and read from the socket. Although only |len| |
| 83 // bytes were requested, intentionally read to the full buffer size. The SSL | 85 // bytes were requested, intentionally read to the full buffer size. The SSL |
| 84 // layer reads the record header and body in separate reads to avoid | 86 // layer reads the record header and body in separate reads to avoid |
| 85 // overreading, but issuing one is more efficient. SSL sockets are not | 87 // overreading, but issuing one is more efficient. SSL sockets are not |
| 86 // reused after shutdown for non-SSL traffic, so overreading is fine. | 88 // reused after shutdown for non-SSL traffic, so overreading is fine. |
| 87 DCHECK(!read_buffer_); | 89 DCHECK(!read_buffer_); |
| 88 DCHECK_EQ(0, read_offset_); | 90 DCHECK_EQ(0, read_offset_); |
| 89 read_buffer_ = new IOBuffer(read_buffer_capacity_); | 91 read_buffer_ = new IOBuffer(read_buffer_capacity_); |
| 90 int result = socket_->Read(read_buffer_.get(), read_buffer_capacity_, | 92 int result = ERR_READ_IF_READY_NOT_IMPLEMENTED; |
| 91 read_callback_); | 93 if (base::FieldTrialList::FindFullName(Socket::kReadIfReadyTrialName) == |
| 94 "enable") { |
| 95 result = socket_->ReadIfReady( |
| 96 read_buffer_.get(), read_buffer_capacity_, |
| 97 base::Bind(&SocketBIOAdapter::OnSocketReadIfReadyComplete, |
| 98 weak_factory_.GetWeakPtr())); |
| 99 if (result == ERR_IO_PENDING) |
| 100 read_buffer_ = nullptr; |
| 101 } |
| 102 if (result == ERR_READ_IF_READY_NOT_IMPLEMENTED) { |
| 103 result = socket_->Read(read_buffer_.get(), read_buffer_capacity_, |
| 104 read_callback_); |
| 105 } |
| 92 if (result == ERR_IO_PENDING) { | 106 if (result == ERR_IO_PENDING) { |
| 93 read_result_ = ERR_IO_PENDING; | 107 read_result_ = ERR_IO_PENDING; |
| 94 } else { | 108 } else { |
| 95 HandleSocketReadResult(result); | 109 HandleSocketReadResult(result); |
| 96 } | 110 } |
| 97 } | 111 } |
| 98 | 112 |
| 99 // There is a pending Read(). Inform the caller to retry when it completes. | 113 // There is a pending Read(). Inform the caller to retry when it completes. |
| 100 if (read_result_ == ERR_IO_PENDING) { | 114 if (read_result_ == ERR_IO_PENDING) { |
| 101 BIO_set_retry_read(bio()); | 115 BIO_set_retry_read(bio()); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 | 150 |
| 137 // The read buffer is no longer needed. | 151 // The read buffer is no longer needed. |
| 138 if (read_result_ <= 0) | 152 if (read_result_ <= 0) |
| 139 read_buffer_ = nullptr; | 153 read_buffer_ = nullptr; |
| 140 } | 154 } |
| 141 | 155 |
| 142 void SocketBIOAdapter::OnSocketReadComplete(int result) { | 156 void SocketBIOAdapter::OnSocketReadComplete(int result) { |
| 143 DCHECK_EQ(ERR_IO_PENDING, read_result_); | 157 DCHECK_EQ(ERR_IO_PENDING, read_result_); |
| 144 | 158 |
| 145 HandleSocketReadResult(result); | 159 HandleSocketReadResult(result); |
| 146 delegate_->OnReadReady(); | 160 delegate_->OnReadReady(read_result_ > 0 ? OK : read_result_); |
| 161 } |
| 162 |
| 163 void SocketBIOAdapter::OnSocketReadIfReadyComplete(int result) { |
| 164 DCHECK_EQ(ERR_IO_PENDING, read_result_); |
| 165 DCHECK_GE(OK, result); |
| 166 |
| 167 // Do not use HandleSocketReadResult() because |result == OK| doesn't mean EOF |
| 168 read_result_ = result; |
| 169 |
| 170 delegate_->OnReadReady(read_result_); |
| 147 } | 171 } |
| 148 | 172 |
| 149 int SocketBIOAdapter::BIOWrite(const char* in, int len) { | 173 int SocketBIOAdapter::BIOWrite(const char* in, int len) { |
| 150 if (len <= 0) | 174 if (len <= 0) |
| 151 return len; | 175 return len; |
| 152 | 176 |
| 153 // If the write buffer is not empty, there must be a pending Write() to flush | 177 // If the write buffer is not empty, there must be a pending Write() to flush |
| 154 // it. | 178 // it. |
| 155 DCHECK(write_buffer_used_ == 0 || write_error_ == ERR_IO_PENDING); | 179 DCHECK(write_buffer_used_ == 0 || write_error_ == ERR_IO_PENDING); |
| 156 | 180 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 // have been empty.) | 231 // have been empty.) |
| 208 SocketWrite(); | 232 SocketWrite(); |
| 209 | 233 |
| 210 // If a read-interrupting write error was synchronously discovered, | 234 // If a read-interrupting write error was synchronously discovered, |
| 211 // asynchronously notify OnReadReady. See https://crbug.com/249848. Avoid | 235 // asynchronously notify OnReadReady. See https://crbug.com/249848. Avoid |
| 212 // reentrancy by deferring it to a later event loop iteration. | 236 // reentrancy by deferring it to a later event loop iteration. |
| 213 if (write_error_ != OK && write_error_ != ERR_IO_PENDING && | 237 if (write_error_ != OK && write_error_ != ERR_IO_PENDING && |
| 214 read_result_ == ERR_IO_PENDING) { | 238 read_result_ == ERR_IO_PENDING) { |
| 215 base::ThreadTaskRunnerHandle::Get()->PostTask( | 239 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 216 FROM_HERE, base::Bind(&SocketBIOAdapter::CallOnReadReady, | 240 FROM_HERE, base::Bind(&SocketBIOAdapter::CallOnReadReady, |
| 217 weak_factory_.GetWeakPtr())); | 241 weak_factory_.GetWeakPtr(), write_error_)); |
| 218 } | 242 } |
| 219 | 243 |
| 220 return bytes_copied; | 244 return bytes_copied; |
| 221 } | 245 } |
| 222 | 246 |
| 223 void SocketBIOAdapter::SocketWrite() { | 247 void SocketBIOAdapter::SocketWrite() { |
| 224 while (write_error_ == OK && write_buffer_used_ > 0) { | 248 while (write_error_ == OK && write_buffer_used_ > 0) { |
| 225 int write_size = | 249 int write_size = |
| 226 std::min(write_buffer_used_, write_buffer_->RemainingCapacity()); | 250 std::min(write_buffer_used_, write_buffer_->RemainingCapacity()); |
| 227 int result = | 251 int result = |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 | 288 |
| 265 bool was_full = write_buffer_used_ == write_buffer_->capacity(); | 289 bool was_full = write_buffer_used_ == write_buffer_->capacity(); |
| 266 | 290 |
| 267 HandleSocketWriteResult(result); | 291 HandleSocketWriteResult(result); |
| 268 SocketWrite(); | 292 SocketWrite(); |
| 269 | 293 |
| 270 // If transitioning from being unable to accept data to being able to, signal | 294 // If transitioning from being unable to accept data to being able to, signal |
| 271 // OnWriteReady. | 295 // OnWriteReady. |
| 272 if (was_full) { | 296 if (was_full) { |
| 273 base::WeakPtr<SocketBIOAdapter> guard(weak_factory_.GetWeakPtr()); | 297 base::WeakPtr<SocketBIOAdapter> guard(weak_factory_.GetWeakPtr()); |
| 274 delegate_->OnWriteReady(); | 298 DCHECK_GE(OK, write_error_); |
| 299 delegate_->OnWriteReady(write_error_); |
| 275 // OnWriteReady may delete the adapter. | 300 // OnWriteReady may delete the adapter. |
| 276 if (!guard) | 301 if (!guard) |
| 277 return; | 302 return; |
| 278 } | 303 } |
| 279 | 304 |
| 280 // Write errors are fed back into BIO_read once the read buffer is empty. If | 305 // Write errors are fed back into BIO_read once the read buffer is empty. If |
| 281 // BIO_read is currently blocked, signal early that a read result is ready. | 306 // BIO_read is currently blocked, signal early that a read result is ready. |
| 282 if (result < 0 && read_result_ == ERR_IO_PENDING) | 307 if (result < 0 && read_result_ == ERR_IO_PENDING) |
| 283 delegate_->OnReadReady(); | 308 delegate_->OnReadReady(result); |
| 284 } | 309 } |
| 285 | 310 |
| 286 void SocketBIOAdapter::CallOnReadReady() { | 311 void SocketBIOAdapter::CallOnReadReady(int rv) { |
| 287 if (read_result_ == ERR_IO_PENDING) | 312 if (read_result_ == ERR_IO_PENDING) |
| 288 delegate_->OnReadReady(); | 313 delegate_->OnReadReady(rv); |
| 289 } | 314 } |
| 290 | 315 |
| 291 SocketBIOAdapter* SocketBIOAdapter::GetAdapter(BIO* bio) { | 316 SocketBIOAdapter* SocketBIOAdapter::GetAdapter(BIO* bio) { |
| 292 DCHECK_EQ(&kBIOMethod, bio->method); | 317 DCHECK_EQ(&kBIOMethod, bio->method); |
| 293 SocketBIOAdapter* adapter = reinterpret_cast<SocketBIOAdapter*>(bio->ptr); | 318 SocketBIOAdapter* adapter = reinterpret_cast<SocketBIOAdapter*>(bio->ptr); |
| 294 if (adapter) | 319 if (adapter) |
| 295 DCHECK_EQ(bio, adapter->bio()); | 320 DCHECK_EQ(bio, adapter->bio()); |
| 296 return adapter; | 321 return adapter; |
| 297 } | 322 } |
| 298 | 323 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 SocketBIOAdapter::BIOReadWrapper, | 366 SocketBIOAdapter::BIOReadWrapper, |
| 342 nullptr, // puts | 367 nullptr, // puts |
| 343 nullptr, // gets | 368 nullptr, // gets |
| 344 SocketBIOAdapter::BIOCtrlWrapper, | 369 SocketBIOAdapter::BIOCtrlWrapper, |
| 345 nullptr, // create | 370 nullptr, // create |
| 346 nullptr, // destroy | 371 nullptr, // destroy |
| 347 nullptr, // callback_ctrl | 372 nullptr, // callback_ctrl |
| 348 }; | 373 }; |
| 349 | 374 |
| 350 } // namespace net | 375 } // namespace net |
| OLD | NEW |