1 // Copyright 2022 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 QUICHE_QUIC_CORE_IO_SOCKET_H_
6 #define QUICHE_QUIC_CORE_IO_SOCKET_H_
7
8 #include <functional>
9 #include <string>
10
11 #include "absl/status/status.h"
12 #include "absl/status/statusor.h"
13 #include "absl/strings/string_view.h"
14 #include "absl/types/span.h"
15 #include "quiche/quic/core/quic_types.h"
16 #include "quiche/quic/platform/api/quic_ip_address_family.h"
17 #include "quiche/quic/platform/api/quic_socket_address.h"
18 #include "quiche/common/platform/api/quiche_export.h"
19
20 #if defined(_WIN32)
21 #include <winsock2.h>
22 #endif // defined(_WIN32)
23
24 namespace quic {
25
26 #if defined(_WIN32)
27 using SocketFd = SOCKET;
28 inline constexpr SocketFd kInvalidSocketFd = INVALID_SOCKET;
29 #else
30 using SocketFd = int;
31 inline constexpr SocketFd kInvalidSocketFd = -1;
32 #endif
33
34 // Low-level platform-agnostic socket operations. Closely follows the behavior
35 // of basic POSIX socket APIs, diverging mostly only to convert to/from cleaner
36 // and platform-agnostic types.
37 namespace socket_api {
38 enum class SocketProtocol {
39 kUdp,
40 kTcp,
41 };
42
GetProtocolName(SocketProtocol protocol)43 inline absl::string_view GetProtocolName(SocketProtocol protocol) {
44 switch (protocol) {
45 case SocketProtocol::kUdp:
46 return "UDP";
47 case SocketProtocol::kTcp:
48 return "TCP";
49 }
50
51 return "unknown";
52 }
53
54 struct QUICHE_EXPORT AcceptResult {
55 // Socket for interacting with the accepted connection.
56 SocketFd fd;
57
58 // Address of the connected peer.
59 QuicSocketAddress peer_address;
60 };
61
62 // Creates a socket with blocking or non-blocking behavior.
63 absl::StatusOr<SocketFd> CreateSocket(IpAddressFamily address_family,
64 SocketProtocol protocol,
65 bool blocking = false);
66
67 // Sets socket `fd` to blocking (if `blocking` true) or non-blocking (if
68 // `blocking` false). Must be a change from previous state.
69 absl::Status SetSocketBlocking(SocketFd fd, bool blocking);
70
71 // Sets buffer sizes for socket `fd` to `size` bytes.
72 absl::Status SetReceiveBufferSize(SocketFd fd, QuicByteCount size);
73 absl::Status SetSendBufferSize(SocketFd fd, QuicByteCount size);
74
75 // Connects socket `fd` to `peer_address`. Returns a status with
76 // `absl::StatusCode::kUnavailable` iff the socket is non-blocking and the
77 // connection could not be immediately completed. The socket will then complete
78 // connecting asynchronously, and on becoming writable, the result can be
79 // checked using GetSocketError().
80 absl::Status Connect(SocketFd fd, const QuicSocketAddress& peer_address);
81
82 // Gets and clears socket error information for socket `fd`. Note that returned
83 // error could be either the found socket error, or unusually, an error from the
84 // attempt to retrieve error information. Typically used to determine connection
85 // result after asynchronous completion of a Connect() call.
86 absl::Status GetSocketError(SocketFd fd);
87
88 // Assign `address` to socket `fd`.
89 absl::Status Bind(SocketFd fd, const QuicSocketAddress& address);
90
91 // Gets the address assigned to socket `fd`.
92 absl::StatusOr<QuicSocketAddress> GetSocketAddress(SocketFd fd);
93
94 // Marks socket `fd` as a passive socket listening for connection requests.
95 // `backlog` is the maximum number of queued connection requests. Typically
96 // expected to return a status with `absl::StatusCode::InvalidArgumentError`
97 // if `fd` is not a TCP socket.
98 absl::Status Listen(SocketFd fd, int backlog);
99
100 // Accepts an incoming connection to the listening socket `fd`. The returned
101 // connection socket will be set as non-blocking iff `blocking` is false.
102 // Typically expected to return a status with
103 // `absl::StatusCode::InvalidArgumentError` if `fd` is not a TCP socket or not
104 // listening for connections. Returns a status with
105 // `absl::StatusCode::kUnavailable` iff the socket is non-blocking and no
106 // incoming connection could be immediately accepted.
107 absl::StatusOr<AcceptResult> Accept(SocketFd fd, bool blocking = false);
108
109 // Receives data from socket `fd`. Will fill `buffer.data()` with up to
110 // `buffer.size()` bytes. On success, returns a span pointing to the buffer
111 // but resized to the actual number of bytes received. Returns a status with
112 // `absl::StatusCode::kUnavailable` iff the socket is non-blocking and the
113 // receive operation could not be immediately completed. If `peek` is true,
114 // received data is not removed from the underlying socket data queue.
115 absl::StatusOr<absl::Span<char>> Receive(SocketFd fd, absl::Span<char> buffer,
116 bool peek = false);
117
118 // Sends some or all of the data in `buffer` to socket `fd`. On success,
119 // returns a string_view pointing to the unsent remainder of the buffer (or an
120 // empty string_view if all of `buffer` was successfully sent). Returns a status
121 // with `absl::StatusCode::kUnavailable` iff the socket is non-blocking and the
122 // send operation could not be immediately completed.
123 absl::StatusOr<absl::string_view> Send(SocketFd fd, absl::string_view buffer);
124
125 // Closes socket `fd`.
126 absl::Status Close(SocketFd fd);
127 } // namespace socket_api
128
129 } // namespace quic
130
131 #endif // QUICHE_QUIC_CORE_IO_SOCKET_H_
132