xref: /aosp_15_r20/external/cronet/net/socket/stream_socket.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2012 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker #ifndef NET_SOCKET_STREAM_SOCKET_H_
6*6777b538SAndroid Build Coastguard Worker #define NET_SOCKET_STREAM_SOCKET_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #include <stdint.h>
9*6777b538SAndroid Build Coastguard Worker 
10*6777b538SAndroid Build Coastguard Worker #include <optional>
11*6777b538SAndroid Build Coastguard Worker #include <string_view>
12*6777b538SAndroid Build Coastguard Worker 
13*6777b538SAndroid Build Coastguard Worker #include "base/functional/bind.h"
14*6777b538SAndroid Build Coastguard Worker #include "net/base/net_errors.h"
15*6777b538SAndroid Build Coastguard Worker #include "net/base/net_export.h"
16*6777b538SAndroid Build Coastguard Worker #include "net/dns/public/resolve_error_info.h"
17*6777b538SAndroid Build Coastguard Worker #include "net/socket/next_proto.h"
18*6777b538SAndroid Build Coastguard Worker #include "net/socket/socket.h"
19*6777b538SAndroid Build Coastguard Worker 
20*6777b538SAndroid Build Coastguard Worker namespace net {
21*6777b538SAndroid Build Coastguard Worker 
22*6777b538SAndroid Build Coastguard Worker class IPEndPoint;
23*6777b538SAndroid Build Coastguard Worker class NetLogWithSource;
24*6777b538SAndroid Build Coastguard Worker class SSLCertRequestInfo;
25*6777b538SAndroid Build Coastguard Worker class SSLInfo;
26*6777b538SAndroid Build Coastguard Worker class SocketTag;
27*6777b538SAndroid Build Coastguard Worker 
28*6777b538SAndroid Build Coastguard Worker class NET_EXPORT StreamSocket : public Socket {
29*6777b538SAndroid Build Coastguard Worker  public:
30*6777b538SAndroid Build Coastguard Worker   using BeforeConnectCallback = base::RepeatingCallback<int()>;
31*6777b538SAndroid Build Coastguard Worker 
32*6777b538SAndroid Build Coastguard Worker   ~StreamSocket() override = default;
33*6777b538SAndroid Build Coastguard Worker 
34*6777b538SAndroid Build Coastguard Worker   // Sets a callback to be invoked before establishing a connection. This allows
35*6777b538SAndroid Build Coastguard Worker   // setting options, like receive and send buffer size, when they will take
36*6777b538SAndroid Build Coastguard Worker   // effect. The callback should return net::OK on success, and an error on
37*6777b538SAndroid Build Coastguard Worker   // failure. It must not return net::ERR_IO_PENDING.
38*6777b538SAndroid Build Coastguard Worker   //
39*6777b538SAndroid Build Coastguard Worker   // If multiple connection attempts are made, the callback will be invoked for
40*6777b538SAndroid Build Coastguard Worker   // each one.
41*6777b538SAndroid Build Coastguard Worker   virtual void SetBeforeConnectCallback(
42*6777b538SAndroid Build Coastguard Worker       const BeforeConnectCallback& before_connect_callback);
43*6777b538SAndroid Build Coastguard Worker 
44*6777b538SAndroid Build Coastguard Worker   // Called to establish a connection.  Returns OK if the connection could be
45*6777b538SAndroid Build Coastguard Worker   // established synchronously.  Otherwise, ERR_IO_PENDING is returned and the
46*6777b538SAndroid Build Coastguard Worker   // given callback will run asynchronously when the connection is established
47*6777b538SAndroid Build Coastguard Worker   // or when an error occurs.  The result is some other error code if the
48*6777b538SAndroid Build Coastguard Worker   // connection could not be established.
49*6777b538SAndroid Build Coastguard Worker   //
50*6777b538SAndroid Build Coastguard Worker   // The socket's Read and Write methods may not be called until Connect
51*6777b538SAndroid Build Coastguard Worker   // succeeds.
52*6777b538SAndroid Build Coastguard Worker   //
53*6777b538SAndroid Build Coastguard Worker   // It is valid to call Connect on an already connected socket, in which case
54*6777b538SAndroid Build Coastguard Worker   // OK is simply returned.
55*6777b538SAndroid Build Coastguard Worker   //
56*6777b538SAndroid Build Coastguard Worker   // Connect may also be called again after a call to the Disconnect method.
57*6777b538SAndroid Build Coastguard Worker   //
58*6777b538SAndroid Build Coastguard Worker   virtual int Connect(CompletionOnceCallback callback) = 0;
59*6777b538SAndroid Build Coastguard Worker 
60*6777b538SAndroid Build Coastguard Worker   // Called to confirm the TLS handshake, if any, indicating that replay
61*6777b538SAndroid Build Coastguard Worker   // protection is ready. Returns OK if the handshake could complete
62*6777b538SAndroid Build Coastguard Worker   // synchronously or had already been confirmed. Otherwise, ERR_IO_PENDING is
63*6777b538SAndroid Build Coastguard Worker   // returned and the given callback will run asynchronously when the connection
64*6777b538SAndroid Build Coastguard Worker   // is established or when an error occurs.  The result is some other error
65*6777b538SAndroid Build Coastguard Worker   // code if the connection could not be completed.
66*6777b538SAndroid Build Coastguard Worker   //
67*6777b538SAndroid Build Coastguard Worker   // This operation is only needed if TLS early data is enabled, in which case
68*6777b538SAndroid Build Coastguard Worker   // Connect returns early and Write initially sends early data, which does not
69*6777b538SAndroid Build Coastguard Worker   // have TLS's usual security properties. The caller must call this function
70*6777b538SAndroid Build Coastguard Worker   // and wait for handshake confirmation before sending data that is not
71*6777b538SAndroid Build Coastguard Worker   // replay-safe.
72*6777b538SAndroid Build Coastguard Worker   //
73*6777b538SAndroid Build Coastguard Worker   // ConfirmHandshake may run concurrently with Read or Write, but, as with Read
74*6777b538SAndroid Build Coastguard Worker   // and Write, at most one pending ConfirmHandshake operation may be in
75*6777b538SAndroid Build Coastguard Worker   // progress at a time.
76*6777b538SAndroid Build Coastguard Worker   virtual int ConfirmHandshake(CompletionOnceCallback callback);
77*6777b538SAndroid Build Coastguard Worker 
78*6777b538SAndroid Build Coastguard Worker   // Called to disconnect a socket.  Does nothing if the socket is already
79*6777b538SAndroid Build Coastguard Worker   // disconnected.  After calling Disconnect it is possible to call Connect
80*6777b538SAndroid Build Coastguard Worker   // again to establish a new connection.
81*6777b538SAndroid Build Coastguard Worker   //
82*6777b538SAndroid Build Coastguard Worker   // If IO (Connect, Read, or Write) is pending when the socket is
83*6777b538SAndroid Build Coastguard Worker   // disconnected, the pending IO is cancelled, and the completion callback
84*6777b538SAndroid Build Coastguard Worker   // will not be called.
85*6777b538SAndroid Build Coastguard Worker   virtual void Disconnect() = 0;
86*6777b538SAndroid Build Coastguard Worker 
87*6777b538SAndroid Build Coastguard Worker   // Called to test if the connection is still alive.  Returns false if a
88*6777b538SAndroid Build Coastguard Worker   // connection wasn't established or the connection is dead.  True is returned
89*6777b538SAndroid Build Coastguard Worker   // if the connection was terminated, but there is unread data in the incoming
90*6777b538SAndroid Build Coastguard Worker   // buffer.
91*6777b538SAndroid Build Coastguard Worker   virtual bool IsConnected() const = 0;
92*6777b538SAndroid Build Coastguard Worker 
93*6777b538SAndroid Build Coastguard Worker   // Called to test if the connection is still alive and idle.  Returns false
94*6777b538SAndroid Build Coastguard Worker   // if a connection wasn't established, the connection is dead, or there is
95*6777b538SAndroid Build Coastguard Worker   // unread data in the incoming buffer.
96*6777b538SAndroid Build Coastguard Worker   virtual bool IsConnectedAndIdle() const = 0;
97*6777b538SAndroid Build Coastguard Worker 
98*6777b538SAndroid Build Coastguard Worker   // Copies the peer address to |address| and returns a network error code.
99*6777b538SAndroid Build Coastguard Worker   // ERR_SOCKET_NOT_CONNECTED will be returned if the socket is not connected.
100*6777b538SAndroid Build Coastguard Worker   virtual int GetPeerAddress(IPEndPoint* address) const = 0;
101*6777b538SAndroid Build Coastguard Worker 
102*6777b538SAndroid Build Coastguard Worker   // Copies the local address to |address| and returns a network error code.
103*6777b538SAndroid Build Coastguard Worker   // ERR_SOCKET_NOT_CONNECTED will be returned if the socket is not bound.
104*6777b538SAndroid Build Coastguard Worker   virtual int GetLocalAddress(IPEndPoint* address) const = 0;
105*6777b538SAndroid Build Coastguard Worker 
106*6777b538SAndroid Build Coastguard Worker   // Gets the NetLog for this socket.
107*6777b538SAndroid Build Coastguard Worker   virtual const NetLogWithSource& NetLog() const = 0;
108*6777b538SAndroid Build Coastguard Worker 
109*6777b538SAndroid Build Coastguard Worker   // Returns true if the socket ever had any reads or writes.  StreamSockets
110*6777b538SAndroid Build Coastguard Worker   // layered on top of transport sockets should return if their own Read() or
111*6777b538SAndroid Build Coastguard Worker   // Write() methods had been called, not the underlying transport's.
112*6777b538SAndroid Build Coastguard Worker   virtual bool WasEverUsed() const = 0;
113*6777b538SAndroid Build Coastguard Worker 
114*6777b538SAndroid Build Coastguard Worker   // Returns the protocol negotiated via ALPN for this socket, or
115*6777b538SAndroid Build Coastguard Worker   // kProtoUnknown will be returned if ALPN is not applicable.
116*6777b538SAndroid Build Coastguard Worker   virtual NextProto GetNegotiatedProtocol() const = 0;
117*6777b538SAndroid Build Coastguard Worker 
118*6777b538SAndroid Build Coastguard Worker   // Get data received from peer in ALPS TLS extension.
119*6777b538SAndroid Build Coastguard Worker   // Returns a (possibly empty) value if a TLS version supporting ALPS was used
120*6777b538SAndroid Build Coastguard Worker   // and ALPS was negotiated, nullopt otherwise.
121*6777b538SAndroid Build Coastguard Worker   virtual std::optional<std::string_view> GetPeerApplicationSettings() const;
122*6777b538SAndroid Build Coastguard Worker 
123*6777b538SAndroid Build Coastguard Worker   // Gets the SSL connection information of the socket.  Returns false if
124*6777b538SAndroid Build Coastguard Worker   // SSL was not used by this socket.
125*6777b538SAndroid Build Coastguard Worker   virtual bool GetSSLInfo(SSLInfo* ssl_info) = 0;
126*6777b538SAndroid Build Coastguard Worker 
127*6777b538SAndroid Build Coastguard Worker   // Gets the SSL CertificateRequest info of the socket after Connect failed
128*6777b538SAndroid Build Coastguard Worker   // with ERR_SSL_CLIENT_AUTH_CERT_NEEDED.  Must not be called on a socket that
129*6777b538SAndroid Build Coastguard Worker   // does not support SSL.
130*6777b538SAndroid Build Coastguard Worker   virtual void GetSSLCertRequestInfo(
131*6777b538SAndroid Build Coastguard Worker       SSLCertRequestInfo* cert_request_info) const;
132*6777b538SAndroid Build Coastguard Worker 
133*6777b538SAndroid Build Coastguard Worker   // Returns the total number of number bytes read by the socket. This only
134*6777b538SAndroid Build Coastguard Worker   // counts the payload bytes. Transport headers are not counted. Returns
135*6777b538SAndroid Build Coastguard Worker   // 0 if the socket does not implement the function. The count is reset when
136*6777b538SAndroid Build Coastguard Worker   // Disconnect() is called.
137*6777b538SAndroid Build Coastguard Worker   virtual int64_t GetTotalReceivedBytes() const = 0;
138*6777b538SAndroid Build Coastguard Worker 
139*6777b538SAndroid Build Coastguard Worker   // Apply |tag| to this socket. If socket isn't yet connected, tag will be
140*6777b538SAndroid Build Coastguard Worker   // applied when socket is later connected. If Connect() fails or socket
141*6777b538SAndroid Build Coastguard Worker   // is closed, tag is cleared. If this socket is layered upon or wraps an
142*6777b538SAndroid Build Coastguard Worker   // underlying socket, |tag| will be applied to the underlying socket in the
143*6777b538SAndroid Build Coastguard Worker   // same manner as if ApplySocketTag() was called on the underlying socket.
144*6777b538SAndroid Build Coastguard Worker   // The tag can be applied at any time, in other words active sockets can be
145*6777b538SAndroid Build Coastguard Worker   // retagged with a different tag. Sockets wrapping multiplexed sockets
146*6777b538SAndroid Build Coastguard Worker   // (e.g. sockets who proxy through a QUIC or Spdy stream) cannot be tagged as
147*6777b538SAndroid Build Coastguard Worker   // the tag would inadvertently affect other streams; calling ApplySocketTag()
148*6777b538SAndroid Build Coastguard Worker   // in this case will result in CHECK(false).
149*6777b538SAndroid Build Coastguard Worker   virtual void ApplySocketTag(const SocketTag& tag) = 0;
150*6777b538SAndroid Build Coastguard Worker };
151*6777b538SAndroid Build Coastguard Worker 
152*6777b538SAndroid Build Coastguard Worker }  // namespace net
153*6777b538SAndroid Build Coastguard Worker 
154*6777b538SAndroid Build Coastguard Worker #endif  // NET_SOCKET_STREAM_SOCKET_H_
155