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