forked from sustrik/libdill
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtcp.c
295 lines (263 loc) · 9.96 KB
/
tcp.c
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
/*
Copyright (c) 2017 Martin Sustrik
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include "libdillimpl.h"
#include "fd.h"
#include "utils.h"
/* Secretly export the symbols. More thinking should be done about how
to do this cleanly without breaking encapsulation. */
DILL_EXPORT extern const void *tcp_type;
DILL_EXPORT extern const void *tcp_listener_type;
DILL_EXPORT int tcp_fd(int s);
static int tcp_makeconn(int fd);
/******************************************************************************/
/* TCP connection socket */
/******************************************************************************/
dill_unique_id(tcp_type);
static void *tcp_hquery(struct hvfs *hvfs, const void *type);
static void tcp_hclose(struct hvfs *hvfs);
static int tcp_hdone(struct hvfs *hvfs, int64_t deadline);
static int tcp_bsendl(struct bsock_vfs *bvfs,
struct iolist *first, struct iolist *last, int64_t deadline);
static ssize_t tcp_brecvl(struct bsock_vfs *bvfs,
struct iolist *first, struct iolist *last, int64_t deadline);
struct tcp_conn {
struct hvfs hvfs;
struct bsock_vfs bvfs;
int fd;
struct fd_rxbuf rxbuf;
unsigned int indone : 1;
unsigned int outdone: 1;
unsigned int inerr : 1;
unsigned int outerr : 1;
};
static void *tcp_hquery(struct hvfs *hvfs, const void *type) {
struct tcp_conn *self = (struct tcp_conn*)hvfs;
if(type == bsock_type) return &self->bvfs;
if(type == tcp_type) return self;
errno = ENOTSUP;
return NULL;
}
int tcp_connect(const struct ipaddr *addr, int64_t deadline) {
int err;
/* Open a socket. */
int s = socket(ipaddr_family(addr), SOCK_STREAM, 0);
if(dill_slow(s < 0)) {err = errno; goto error1;}
/* Set it to non-blocking mode. */
int rc = fd_unblock(s);
if(dill_slow(rc < 0)) {err = errno; goto error2;}
/* Connect to the remote endpoint. */
rc = fd_connect(s, ipaddr_sockaddr(addr), ipaddr_len(addr), deadline);
if(dill_slow(rc < 0)) {err = errno; goto error2;}
/* Create the handle. */
int h = tcp_makeconn(s);
if(dill_slow(h < 0)) {err = errno; goto error2;}
return h;
error2:
fd_close(s);
error1:
errno = err;
return -1;
}
static int tcp_bsendl(struct bsock_vfs *bvfs,
struct iolist *first, struct iolist *last, int64_t deadline) {
struct tcp_conn *self = dill_cont(bvfs, struct tcp_conn, bvfs);
if(dill_slow(self->outdone)) {errno = EPIPE; return -1;}
if(dill_slow(self->outerr)) {errno = ECONNRESET; return -1;}
ssize_t sz = fd_send(self->fd, first, last, deadline);
if(dill_fast(sz >= 0)) return sz;
self->outerr = 1;
return -1;
}
static ssize_t tcp_brecvl(struct bsock_vfs *bvfs,
struct iolist *first, struct iolist *last, int64_t deadline) {
struct tcp_conn *self = dill_cont(bvfs, struct tcp_conn, bvfs);
if(dill_slow(self->indone)) {errno = EPIPE; return -1;}
if(dill_slow(self->inerr)) {errno = ECONNRESET; return -1;}
int sz = fd_recv(self->fd, &self->rxbuf, first, last, deadline);
if(dill_fast(sz > 0)) return sz;
if(errno == EPIPE) self->indone = 1;
else self->inerr = 1;
return -1;
}
static int tcp_hdone(struct hvfs *hvfs, int64_t deadline) {
struct tcp_conn *self = (struct tcp_conn*)hvfs;
if(dill_slow(self->outdone)) {errno = EPIPE; return -1;}
if(dill_slow(self->outerr)) {errno = ECONNRESET; return -1;}
/* Flushing the tx buffer is done asynchronously on kernel level. */
int rc = shutdown(self->fd, SHUT_WR);
if(dill_slow(rc < 0)) {
if(errno == ENOTCONN) {self->outerr = 1; errno = ECONNRESET; return -1;}
if(errno == ENOBUFS) {self->outerr = 1; errno = ENOMEM; return -1;}
dill_assert(rc == 0);
}
self->outdone = 1;
return 0;
}
int tcp_close(int s, int64_t deadline) {
int err;
struct tcp_conn *self = hquery(s, tcp_type);
if(dill_slow(!self)) return -1;
if(dill_slow(self->inerr || self->outerr)) {err = ECONNRESET; goto error;}
/* If not done already, flush the outbound data and start the terminal
handshake. */
if(!self->outdone) {
int rc = tcp_hdone(&self->hvfs, deadline);
if(dill_slow(rc < 0)) {err = errno; goto error;}
}
/* Now we are going to read all the inbound data until we reach end of the
stream. That way we can be sure that the peer either received all our
data or consciously closed the connection without reading all of it. */
int rc = tcp_brecvl(&self->bvfs, NULL, NULL, deadline);
dill_assert(rc < 0);
if(dill_slow(errno != EPIPE)) {err = errno; goto error;}
return 0;
error:
tcp_hclose(&self->hvfs);
errno = err;
return -1;
}
static void tcp_hclose(struct hvfs *hvfs) {
struct tcp_conn *self = (struct tcp_conn*)hvfs;
fd_close(self->fd);
free(self);
}
/******************************************************************************/
/* TCP listener socket */
/******************************************************************************/
dill_unique_id(tcp_listener_type);
static void *tcp_listener_hquery(struct hvfs *hvfs, const void *type);
static void tcp_listener_hclose(struct hvfs *hvfs);
struct tcp_listener {
struct hvfs hvfs;
int fd;
struct ipaddr addr;
};
static void *tcp_listener_hquery(struct hvfs *hvfs, const void *type) {
struct tcp_listener *self = (struct tcp_listener*)hvfs;
if(type == tcp_listener_type) return self;
errno = ENOTSUP;
return NULL;
}
int tcp_listen(struct ipaddr *addr, int backlog) {
int err;
/* Open the listening socket. */
int s = socket(ipaddr_family(addr), SOCK_STREAM, 0);
if(dill_slow(s < 0)) {err = errno; goto error1;}
/* Set it to non-blocking mode. */
int rc = fd_unblock(s);
if(dill_slow(rc < 0)) {err = errno; goto error2;}
/* Start listening for incoming connections. */
rc = bind(s, ipaddr_sockaddr(addr), ipaddr_len(addr));
if(dill_slow(rc < 0)) {err = errno; goto error2;}
rc = listen(s, backlog);
if(dill_slow(rc < 0)) {err = errno; goto error2;}
/* If the user requested an ephemeral port,
retrieve the port number assigned by the OS. */
if(ipaddr_port(addr) == 0) {
struct ipaddr baddr;
socklen_t len = sizeof(struct ipaddr);
rc = getsockname(s, (struct sockaddr*)&baddr, &len);
if(rc < 0) {err = errno; goto error2;}
ipaddr_setport(addr, ipaddr_port(&baddr));
}
/* Create the object. */
struct tcp_listener *self = malloc(sizeof(struct tcp_listener));
if(dill_slow(!self)) {err = ENOMEM; goto error2;}
self->hvfs.query = tcp_listener_hquery;
self->hvfs.close = tcp_listener_hclose;
self->hvfs.done = NULL;
self->fd = s;
/* Create handle. */
int h = hmake(&self->hvfs);
if(dill_slow(h < 0)) {err = errno; goto error3;}
return h;
error3:
free(self);
error2:
close(s);
error1:
errno = err;
return -1;
}
int tcp_accept(int s, struct ipaddr *addr, int64_t deadline) {
int err;
/* Retrieve the listener object. */
struct tcp_listener *lst = hquery(s, tcp_listener_type);
if(dill_slow(!lst)) {err = errno; goto error1;}
/* Try to get new connection in a non-blocking way. */
socklen_t addrlen = sizeof(struct ipaddr);
int as = fd_accept(lst->fd, (struct sockaddr*)addr, &addrlen, deadline);
if(dill_slow(as < 0)) {err = errno; goto error1;}
/* Set it to non-blocking mode. */
int rc = fd_unblock(as);
if(dill_slow(rc < 0)) {err = errno; goto error2;}
/* Create the handle. */
int h = tcp_makeconn(as);
if(dill_slow(h < 0)) {err = errno; goto error2;}
return h;
error2:
fd_close(as);
error1:
errno = err;
return -1;
}
int tcp_fd(int s) {
struct tcp_listener *lst = hquery(s, tcp_listener_type);
if(lst) return lst->fd;
struct tcp_conn *conn = hquery(s, tcp_type);
if(conn) return conn->fd;
return -1;
}
static void tcp_listener_hclose(struct hvfs *hvfs) {
struct tcp_listener *self = (struct tcp_listener*)hvfs;
fd_close(self->fd);
free(self);
}
/******************************************************************************/
/* Helpers */
/******************************************************************************/
static int tcp_makeconn(int fd) {
int err;
/* Create the object. */
struct tcp_conn *self = malloc(sizeof(struct tcp_conn));
if(dill_slow(!self)) {err = ENOMEM; goto error1;}
self->hvfs.query = tcp_hquery;
self->hvfs.close = tcp_hclose;
self->hvfs.done = tcp_hdone;
self->bvfs.bsendl = tcp_bsendl;
self->bvfs.brecvl = tcp_brecvl;
self->fd = fd;
fd_initrxbuf(&self->rxbuf);
self->indone = 0;
self->outdone = 0;
self->inerr = 0;
self->outerr = 0;
/* Create the handle. */
int h = hmake(&self->hvfs);
if(dill_slow(h < 0)) {err = errno; goto error2;}
return h;
error2:
free(self);
error1:
errno = err;
return -1;
}