| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef NET_SOCKET_SOCKET_BIO_ADAPTER_H_ |
| 6 #define NET_SOCKET_SOCKET_BIO_ADAPTER_H_ |
| 7 |
| 8 #include <openssl/base.h> |
| 9 |
| 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/weak_ptr.h" |
| 12 #include "net/base/completion_callback.h" |
| 13 #include "net/base/net_export.h" |
| 14 |
| 15 namespace net { |
| 16 |
| 17 class GrowableIOBuffer; |
| 18 class IOBuffer; |
| 19 class StreamSocket; |
| 20 |
| 21 // An adapter to convert between StreamSocket and OpenSSL BIO I/O models. |
| 22 // |
| 23 // BIO exposes a UNIX-like interface where BIO_read and BIO_write may either |
| 24 // succeed synchronously or be retried (with no memory between calls). |
| 25 // StreamSocket exposes an asynchronous interface where an asynchronous |
| 26 // operation continues running and completes with a callback. |
| 27 // |
| 28 // For reading, SocketBIOAdapter maintains a buffer to pass to |
| 29 // StreamSocket::Read. Once that Read completes, BIO_read synchronously drains |
| 30 // the buffer and signals BIO_should_read once empty. |
| 31 // |
| 32 // For writing, SocketBIOAdapter maintains a ring buffer of data to be written |
| 33 // to the StreamSocket. BIO_write synchronously copies data into the buffer or |
| 34 // signals BIO_should_write if the buffer is full. The ring buffer is drained |
| 35 // asynchronously into the socket. Note this means write errors are reported at |
| 36 // a later BIO_write. |
| 37 // |
| 38 // To work around this delay, write errors are also surfaced out of |
| 39 // BIO_read. Otherwise a failure in the final BIO_write of an application may go |
| 40 // unnoticed. If this occurs, OnReadReady will be signaled as if it were a read |
| 41 // error. See https://crbug.com/249848. |
| 42 class NET_EXPORT_PRIVATE SocketBIOAdapter { |
| 43 public: |
| 44 // A delegate interface for when the sockets are ready. BIO assumes external |
| 45 // knowledge of when to retry operations (such as a select() loop for UNIX), |
| 46 // which is signaled out of StreamSocket's callbacks here. |
| 47 // |
| 48 // Callers should implement these methods and, when signaled, retry the |
| 49 // BIO_read or BIO_write. This usually is done by retrying a higher-level |
| 50 // operation, such as SSL_read or SSL_write. |
| 51 // |
| 52 // Callers may assume that OnReadReady and OnWriteReady will only be called |
| 53 // from a PostTask or StreamSocket callback. |
| 54 class Delegate { |
| 55 public: |
| 56 // Called when the BIO is ready to handle BIO_read, after having previously |
| 57 // been blocked. |
| 58 virtual void OnReadReady() = 0; |
| 59 |
| 60 // Called when the BIO is ready to handle BIO_write, after having previously |
| 61 // been blocked. |
| 62 virtual void OnWriteReady() = 0; |
| 63 |
| 64 protected: |
| 65 virtual ~Delegate() {} |
| 66 }; |
| 67 |
| 68 // Creates a new SocketBIOAdapter for the specified socket. |socket| and |
| 69 // |delegate| must remain valid for the lifetime of the SocketBIOAdapter. |
| 70 SocketBIOAdapter(StreamSocket* socket, |
| 71 int read_buffer_capacity, |
| 72 int write_buffer_capacity, |
| 73 Delegate* delegate); |
| 74 ~SocketBIOAdapter(); |
| 75 |
| 76 BIO* bio() { return bio_.get(); } |
| 77 |
| 78 // Returns true if any data has been read from the underlying StreamSocket, |
| 79 // but not yet consumed by the BIO. |
| 80 bool HasPendingReadData(); |
| 81 |
| 82 private: |
| 83 int BIORead(char* out, int len); |
| 84 void HandleSocketReadResult(int result); |
| 85 void OnSocketReadComplete(int result); |
| 86 |
| 87 int BIOWrite(const char* in, int len); |
| 88 void SocketWrite(); |
| 89 void HandleSocketWriteResult(int result); |
| 90 void OnSocketWriteComplete(int result); |
| 91 void CallOnReadReady(); |
| 92 |
| 93 static SocketBIOAdapter* GetAdapter(BIO* bio); |
| 94 static int BIOReadWrapper(BIO* bio, char* out, int len); |
| 95 static int BIOWriteWrapper(BIO* bio, const char* in, int len); |
| 96 static long BIOCtrlWrapper(BIO* bio, int cmd, long larg, void* parg); |
| 97 |
| 98 static const BIO_METHOD kBIOMethod; |
| 99 |
| 100 bssl::UniquePtr<BIO> bio_; |
| 101 |
| 102 // The pointer is non-owning so this class may be used with both |
| 103 // ClientSocketHandles and raw StreamSockets. |
| 104 StreamSocket* socket_; |
| 105 |
| 106 CompletionCallback read_callback_; |
| 107 CompletionCallback write_callback_; |
| 108 |
| 109 // The capacity of the read buffer. |
| 110 int read_buffer_capacity_; |
| 111 // A buffer containing data from the most recent socket Read(). The buffer is |
| 112 // deallocated when unused. |
| 113 scoped_refptr<IOBuffer> read_buffer_; |
| 114 // The number of bytes of read_buffer_ consumed. |
| 115 int read_offset_; |
| 116 // The result of the most recent socket Read(). If ERR_IO_PENDING, there is a |
| 117 // socket Read() in progress. If another error, Read() has failed. Otherwise, |
| 118 // it is the number of bytes in the buffer (zero if empty). |
| 119 int read_result_; |
| 120 |
| 121 // The capacity of the write buffer. |
| 122 int write_buffer_capacity_; |
| 123 // A ring buffer of data to be written to the transport. The offset of the |
| 124 // buffer is the start of the ring buffer and is advanced on successful |
| 125 // Write(). The buffer is deallocated when unused. |
| 126 scoped_refptr<GrowableIOBuffer> write_buffer_; |
| 127 // The number of bytes of data in write_buffer_. |
| 128 int write_buffer_used_; |
| 129 // The most recent socket Write() error. If ERR_IO_PENDING, there is a socket |
| 130 // Write() in progress. If OK, there is no socket Write() in progress and none |
| 131 // have failed. |
| 132 int write_error_; |
| 133 |
| 134 Delegate* delegate_; |
| 135 |
| 136 base::WeakPtrFactory<SocketBIOAdapter> weak_factory_; |
| 137 |
| 138 DISALLOW_COPY_AND_ASSIGN(SocketBIOAdapter); |
| 139 }; |
| 140 |
| 141 } // namespace net |
| 142 |
| 143 #endif // NET_SOCKET_SOCKET_BIO_ADAPTER_H_ |
| OLD | NEW |