forked from scylladb/seastar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.hh
344 lines (299 loc) · 10.2 KB
/
api.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* This file is open source software, licensed to you under the terms
* of the Apache License, Version 2.0 (the "License"). See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. You may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright (C) 2014 Cloudius Systems, Ltd.
*/
#ifndef NET_API_HH_
#define NET_API_HH_
#include <memory>
#include <vector>
#include <cstring>
#include "core/future.hh"
#include "net/byteorder.hh"
#include "net/packet.hh"
#include "core/print.hh"
#include "core/temporary_buffer.hh"
#include "core/iostream.hh"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <boost/variant.hpp>
struct ipv4_addr;
class socket_address {
public:
union {
::sockaddr_storage sas;
::sockaddr sa;
::sockaddr_in in;
} u;
socket_address(sockaddr_in sa) {
u.in = sa;
}
socket_address(ipv4_addr);
socket_address() = default;
::sockaddr& as_posix_sockaddr() { return u.sa; }
::sockaddr_in& as_posix_sockaddr_in() { return u.in; }
const ::sockaddr& as_posix_sockaddr() const { return u.sa; }
const ::sockaddr_in& as_posix_sockaddr_in() const { return u.in; }
};
namespace seastar {
enum class transport {
TCP = IPPROTO_TCP,
SCTP = IPPROTO_SCTP
};
}
struct listen_options {
seastar::transport proto = seastar::transport::TCP;
bool reuse_address = false;
listen_options(bool rua = false)
: reuse_address(rua)
{}
};
struct ipv4_addr {
uint32_t ip;
uint16_t port;
ipv4_addr() : ip(0), port(0) {}
ipv4_addr(uint32_t ip, uint16_t port) : ip(ip), port(port) {}
ipv4_addr(uint16_t port) : ip(0), port(port) {}
ipv4_addr(const std::string &addr);
ipv4_addr(const std::string &addr, uint16_t port);
ipv4_addr(const socket_address &sa) {
ip = net::ntoh(sa.u.in.sin_addr.s_addr);
port = net::ntoh(sa.u.in.sin_port);
}
ipv4_addr(socket_address &&sa) : ipv4_addr(sa) {}
};
static inline
bool is_ip_unspecified(ipv4_addr &addr) {
return addr.ip == 0;
}
static inline
bool is_port_unspecified(ipv4_addr &addr) {
return addr.port == 0;
}
static inline
std::ostream& operator<<(std::ostream &os, ipv4_addr addr) {
fprint(os, "%d.%d.%d.%d",
(addr.ip >> 24) & 0xff,
(addr.ip >> 16) & 0xff,
(addr.ip >> 8) & 0xff,
(addr.ip) & 0xff);
return os << ":" << addr.port;
}
static inline
socket_address make_ipv4_address(ipv4_addr addr) {
socket_address sa;
sa.u.in.sin_family = AF_INET;
sa.u.in.sin_port = htons(addr.port);
sa.u.in.sin_addr.s_addr = htonl(addr.ip);
return sa;
}
namespace net {
// see linux tcp(7) for parameter explanation
struct tcp_keepalive_params {
std::chrono::seconds idle; // TCP_KEEPIDLE
std::chrono::seconds interval; // TCP_KEEPINTVL
unsigned count; // TCP_KEEPCNT
};
// see linux sctp(7) for parameter explanation
struct sctp_keepalive_params {
std::chrono::seconds interval; // spp_hbinterval
unsigned count; // spp_pathmaxrt
};
using keepalive_params = boost::variant<tcp_keepalive_params, sctp_keepalive_params>;
/// \cond internal
class connected_socket_impl;
class socket_impl;
class server_socket_impl;
class udp_channel_impl;
class get_impl;
/// \endcond
class udp_datagram_impl {
public:
virtual ~udp_datagram_impl() {};
virtual ipv4_addr get_src() = 0;
virtual ipv4_addr get_dst() = 0;
virtual uint16_t get_dst_port() = 0;
virtual packet& get_data() = 0;
};
class udp_datagram final {
private:
std::unique_ptr<udp_datagram_impl> _impl;
public:
udp_datagram(std::unique_ptr<udp_datagram_impl>&& impl) : _impl(std::move(impl)) {};
ipv4_addr get_src() { return _impl->get_src(); }
ipv4_addr get_dst() { return _impl->get_dst(); }
uint16_t get_dst_port() { return _impl->get_dst_port(); }
packet& get_data() { return _impl->get_data(); }
};
class udp_channel {
private:
std::unique_ptr<udp_channel_impl> _impl;
public:
udp_channel();
udp_channel(std::unique_ptr<udp_channel_impl>);
~udp_channel();
udp_channel(udp_channel&&);
udp_channel& operator=(udp_channel&&);
future<udp_datagram> receive();
future<> send(ipv4_addr dst, const char* msg);
future<> send(ipv4_addr dst, packet p);
bool is_closed() const;
void close();
};
} /* namespace net */
// TODO: remove from global NS
/// \addtogroup networking-module
/// @{
/// A TCP (or other stream-based protocol) connection.
///
/// A \c connected_socket represents a full-duplex stream between
/// two endpoints, a local endpoint and a remote endpoint.
class connected_socket {
friend class net::get_impl;
std::unique_ptr<net::connected_socket_impl> _csi;
public:
/// Constructs a \c connected_socket not corresponding to a connection
connected_socket();
~connected_socket();
/// \cond internal
explicit connected_socket(std::unique_ptr<net::connected_socket_impl> csi);
/// \endcond
/// Moves a \c connected_socket object.
connected_socket(connected_socket&& cs) noexcept;
/// Move-assigns a \c connected_socket object.
connected_socket& operator=(connected_socket&& cs) noexcept;
/// Gets the input stream.
///
/// Gets an object returning data sent from the remote endpoint.
input_stream<char> input();
/// Gets the output stream.
///
/// Gets an object that sends data to the remote endpoint.
/// \param buffer_size how much data to buffer
output_stream<char> output(size_t buffer_size = 8192);
/// Sets the TCP_NODELAY option (disabling Nagle's algorithm)
void set_nodelay(bool nodelay);
/// Gets the TCP_NODELAY option (Nagle's algorithm)
///
/// \return whether the nodelay option is enabled or not
bool get_nodelay() const;
/// Sets SO_KEEPALIVE option (enable keepalive timer on a socket)
void set_keepalive(bool keepalive);
/// Gets O_KEEPALIVE option
/// \return whether the keepalive option is enabled or not
bool get_keepalive() const;
/// Sets TCP keepalive parameters
void set_keepalive_parameters(const net::keepalive_params& p);
/// Get TCP keepalive parameters
net::keepalive_params get_keepalive_parameters() const;
/// Disables output to the socket.
///
/// Current or future writes that have not been successfully flushed
/// will immediately fail with an error. This is useful to abort
/// operations on a socket that is not making progress due to a
/// peer failure.
future<> shutdown_output();
/// Disables input from the socket.
///
/// Current or future reads will immediately fail with an error.
/// This is useful to abort operations on a socket that is not making
/// progress due to a peer failure.
future<> shutdown_input();
/// Disables socket input and output.
///
/// Equivalent to \ref shutdown_input() and \ref shutdown_output().
};
/// @}
/// \addtogroup networking-module
/// @{
namespace seastar {
/// The seastar socket.
///
/// A \c socket that allows a connection to be established between
/// two endpoints.
class socket {
std::unique_ptr<net::socket_impl> _si;
public:
~socket();
/// \cond internal
explicit socket(std::unique_ptr<net::socket_impl> si);
/// \endcond
/// Moves a \c seastar::socket object.
socket(socket&&) noexcept;
/// Move-assigns a \c seastar::socket object.
seastar::socket& operator=(seastar::socket&&) noexcept;
/// Attempts to establish the connection.
///
/// \return a \ref connected_socket representing the connection.
future<connected_socket> connect(socket_address sa, socket_address local = socket_address(::sockaddr_in{AF_INET, INADDR_ANY, {0}}), seastar::transport proto = seastar::transport::TCP);
/// Stops any in-flight connection attempt.
///
/// Cancels the connection attempt if it's still in progress, and
/// terminates the connection if it has already been established.
void shutdown();
};
} /* namespace seastar */
/// @}
/// \addtogroup networking-module
/// @{
/// A listening socket, waiting to accept incoming network connections.
class server_socket {
std::unique_ptr<net::server_socket_impl> _ssi;
public:
/// Constructs a \c server_socket not corresponding to a connection
server_socket();
/// \cond internal
explicit server_socket(std::unique_ptr<net::server_socket_impl> ssi);
/// \endcond
/// Moves a \c server_socket object.
server_socket(server_socket&& ss) noexcept;
~server_socket();
/// Move-assigns a \c server_socket object.
server_socket& operator=(server_socket&& cs) noexcept;
/// Accepts the next connection to successfully connect to this socket.
///
/// \return a \ref connected_socket representing the connection, and
/// a \ref socket_address describing the remote endpoint.
///
/// \see listen(socket_address sa)
/// \see listen(socket_address sa, listen_options opts)
future<connected_socket, socket_address> accept();
/// Stops any \ref accept() in progress.
///
/// Current and future \ref accept() calls will terminate immediately
/// with an error.
void abort_accept();
};
/// @}
class network_stack {
public:
virtual ~network_stack() {}
virtual server_socket listen(socket_address sa, listen_options opts) = 0;
// FIXME: local parameter assumes ipv4 for now, fix when adding other AF
future<connected_socket> connect(socket_address sa, socket_address local = socket_address(::sockaddr_in{AF_INET, INADDR_ANY, {0}}), seastar::transport proto = seastar::transport::TCP) {
return socket().connect(sa, local, proto);
}
virtual seastar::socket socket() = 0;
virtual net::udp_channel make_udp_channel(ipv4_addr addr = {}) = 0;
virtual future<> initialize() {
return make_ready_future();
}
virtual bool has_per_core_namespace() = 0;
};
#endif