-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp.c
416 lines (382 loc) · 16.2 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// A simple TCP client and server sporting some async features.
#define _POSIX_C_SOURCE 200809L // 2008.09 needed for SA_RESETHAND and the S_IF* fd types
#include <sys/socket.h> // socket(), struct sockaddr, ...
#include <netinet/in.h> // struct inaddr_in{,6}, ...
#include <arpa/inet.h> // inet_ntop()
#include <netdb.h> // getaddrinfo() and struct addrinfo
#include <sys/un.h> // support unix sockets in sockaddr2str()
#include <sys/select.h> // to avoid blocking on either stdin or the socket when the other has data
#include <fcntl.h> // for making stdin nonblocking
#include <sys/stat.h> // for checking which kind of file stdin is
#include <signal.h> // for restoring stdin on Ctrl-C
#include <errno.h>
#include <string.h> // strlen() and strerror()
#include <stdio.h>
#include <stdlib.h> // exit() and strtoul()
#include <unistd.h> // close() and read()
#include <stdarg.h> // used by checkerr()
#include <stdbool.h>
#define BUFFER_SIZE 4096
#define LISTEN_BACKLOG 0
/* helper functions */
// print error messages and exit if `ret` is negative,
// otherwise pass it through to caller.
int checkerr(int ret, const char *desc, ...) {
if (ret >= 0) {
return ret;
}
int err = errno;
fprintf(stderr, "Failed to ");
va_list args;
va_start(args, desc);
vfprintf(stderr, desc, args);
va_end(args);
fprintf(stderr, ": %s\n", strerror(err));
exit(1);
}
// parse string as unsigned short and detect and handle invalid input
in_port_t parseport(const char *arg) {
char *end;
unsigned long num = strtoul(arg, &end, 10);
if (*arg < '0' || *arg > '9' || *end != '\0' || num > 0xffff) {
fprintf(stderr, "Bad port number %s\n", arg);
exit(1);
}
return (in_port_t)num;
}
// format a socket address, using a global buffer to avoid needing to free()
char* sockaddr2str(const struct sockaddr *sa) {
static char ipstr[100];
if (sa->sa_family == AF_INET) {
struct sockaddr_in *ipv4 = (struct sockaddr_in*)sa;
// let inet_ntop handle endianness of the uint32_t address
inet_ntop(AF_INET, &(ipv4->sin_addr), ipstr, sizeof(ipstr));
snprintf(&ipstr[strlen(ipstr)], sizeof(ipstr)-strlen(ipstr), ":%d", ntohs(ipv4->sin_port));
return ipstr;
} else if (sa->sa_family == AF_INET6) {
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6*)sa;
// special-case IPv4-in-IPv6 addresses ::ffff:AABB:CCDD
const uint8_t v4in6[12] = {0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff};
if (!memcmp(ipv6->sin6_addr.s6_addr, v4in6, sizeof(v4in6))) {
snprintf(ipstr, sizeof(ipstr), "[%d.%d.%d.%d]:%d",
ipv6->sin6_addr.s6_addr[12], ipv6->sin6_addr.s6_addr[13],
ipv6->sin6_addr.s6_addr[14], ipv6->sin6_addr.s6_addr[15],
ntohs(ipv6->sin6_port)
);
} else {
ipstr[0] = '[';
// let inet_ntop handle the smart formatting of IPv6 addresses
inet_ntop(AF_INET6, &(ipv6->sin6_addr), &ipstr[1], sizeof(ipstr)-1);
size_t len = strlen(ipstr);
if (ipv6->sin6_scope_id != 0) {
len += snprintf(&ipstr[len], sizeof(ipstr)-len, "%%%d", ipv6->sin6_scope_id);
}
snprintf(&ipstr[len], sizeof(ipstr)-len, "]:%d", ntohs(ipv6->sin6_port));
}
return ipstr;
} else {
snprintf(ipstr, sizeof(ipstr), "{address of unexpected type %d}", sa->sa_family);
return ipstr;
}
}
// get the local address of the socket and format it using sockaddr2str()
char* local2str(int sock) {
struct sockaddr_storage local;
socklen_t len = sizeof(struct sockaddr_storage);
checkerr(getsockname(sock, (struct sockaddr*)&local, &len),
"get local address of socket %d", sock);
return sockaddr2str((struct sockaddr*)&local);
}
/* simple socket-creation functions avoiding the complexity of getaddrinfo() for common cases */
// create a socket and listen on any interface (both IPv6 and IPv4) at the given port
int listen_any(const char *port) {
struct sockaddr_in6 any;
memset(&any, 0, sizeof(struct sockaddr_in6));
any.sin6_family = AF_INET6;
any.sin6_port = port==NULL ? 0 : htons(parseport(port));
any.sin6_addr = in6addr_any; // not really necessary; it's already zero
int listener = checkerr(socket(any.sin6_family, SOCK_STREAM, 0), "create TCP socket");
// you probably want to set SO_REUSEADDR, see try_bind() below
checkerr(bind(listener, (struct sockaddr*)&any, sizeof(struct sockaddr_in6)),
"bind to %s", sockaddr2str((struct sockaddr*)&any));
// query socket address because it might have changed if port was 0
checkerr(listen(listener, LISTEN_BACKLOG), "listen on %s", local2str(listener));
return listener;
}
// create a socket and connect it to the IPv4 loopback address 127.0.0.1
int connect_to_localhost(const char *port) {
port = port==NULL ? "23" : port; // connect to telnet port if unspecified
struct sockaddr_in localhost;
memset(&localhost, 0, sizeof(struct sockaddr_in));
localhost.sin_family = AF_INET;
localhost.sin_port = htons(parseport(port));
localhost.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
int conn = checkerr(socket(AF_INET, SOCK_STREAM, 0), "create TCP socket");
checkerr(connect(conn, (struct sockaddr*)&localhost, sizeof(struct sockaddr_in)),
"connect to %s", sockaddr2str((struct sockaddr*)&localhost));
return conn;
}
/* robust socket-creation functions wrapping getaddrinfo() */
// resolve an address with getaddrinfo(), calling use() for each option
void resolve(
const char *addr, const char *port,
bool(*use)(const struct addrinfo*, int*), int *sock
) {
struct addrinfo hints;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; // both IPv4 and IPv6
hints.ai_socktype = SOCK_STREAM;
struct addrinfo *ai;
int gai_ret = getaddrinfo(addr, port, &hints, &ai);
// for some reason, getaddrinfo uses a different set of error codes
if (gai_ret != 0) {
fprintf(stderr, "Failed to resolve %s:%s: %s\n", addr, port, gai_strerror(gai_ret));
exit(1);
}
struct addrinfo *root = ai; // need to free later
bool succeeded = false;
while (ai != NULL && !succeeded) {
succeeded = use(ai, sock);
ai = ai->ai_next;
}
freeaddrinfo(root);
if (!succeeded) {
fprintf(stderr, "All options failed for %s:%s\n", addr, port);
exit(1);
}
}
// create a socket and bind it to ai->ai_addr
bool try_bind(const struct addrinfo *ai, int *sock) {
fprintf(stderr, "Trying to bind to %s\n", sockaddr2str(ai->ai_addr));
*sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (*sock == -1) {
fprintf(stderr, "Cannot create socket: %s\n", strerror(errno));
return false;
}
// enable re-binding to the address immediately after another process stopped using it
const int yes = 1;
if (setsockopt(*sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
fprintf(stderr, "Cannot set SO_REUSEADDR, bind()ing anyway\n");
}
if (bind(*sock, ai->ai_addr, ai->ai_addrlen) == -1) {
fprintf(stderr, "Cannot bind to %s: %s\n", sockaddr2str(ai->ai_addr), strerror(errno));
checkerr(close(*sock), "close the failed socket");
*sock = -1;
return false;
}
fprintf(stderr, "Local address: %s\n", local2str(*sock));
return true;
}
// connect a socket to ai->ai_addr, creating a socket if it doesn't exists
bool try_connect(const struct addrinfo *ai, int *sock) {
bool new_socket = *sock == -1;
fprintf(stderr, "Trying to connect to %s\n", sockaddr2str(ai->ai_addr));
if (new_socket) {
*sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (*sock == -1) {
fprintf(stderr, "Cannot create socket: %s\n", strerror(errno));
return false;
}
}
if (connect(*sock, ai->ai_addr, ai->ai_addrlen) == -1) {
fprintf(stderr, "Cannot connect to %s", sockaddr2str(ai->ai_addr));
// split because sockaddr2str() reuses string buffer
fprintf(stderr, " (from %s): %s\n", local2str(*sock), strerror(errno));
if (new_socket) {
checkerr(close(*sock), "close the failed socket");
*sock = -1;
}
return false;
}
return true;
}
// create a socket and listen on the address
int listen_on(const char *addr, const char *port) {
addr = addr==NULL ? "localhost" : addr;
port = port==NULL ? "0" : port;
int listener; // try_bind() always creates a new socket
resolve(addr, port, try_bind, &listener);
checkerr(listen(listener, LISTEN_BACKLOG), "listen on %s", local2str(listener));
return listener;
}
// connect to domain:port after binding to ip:port
int connect_from_to(
const char *from_ip, const char *from_port,
const char *to_domain, const char *to_port
) {
from_port = from_port==NULL ? "0" : from_port;
to_port = to_port==NULL ? "23" : to_port; // connect to telnet port if unspecified
int conn; // try_bind() always creates a new socket
resolve(from_ip, from_port, try_bind, &conn);
resolve(to_domain, to_port, try_connect, &conn);
return conn;
}
// connect to domain:port without binding
int connect_to(const char *domain, const char *port) {
domain = domain==NULL ? "localhost" : domain;
port = port==NULL ? "23" : port; // connect to telnet port if unspecified
int conn = -1; // tells try_connect() to also create a socket
resolve(domain, port, try_connect, &conn);
return conn;
}
/* program modes */
// accept a single incoming connection at a time,
// waiting until the client disconnects before accepting another
void accept_loop(int listener, ssize_t(*perform)(int)) {
struct sockaddr_storage client;
socklen_t addrlen = sizeof(struct sockaddr_storage);
while (true) {
int conn = checkerr(accept(listener, (struct sockaddr*)&client, &addrlen),
"accept connection");
char *client_str = sockaddr2str((struct sockaddr*)&client);
fprintf(stderr, "Accepted connection from %s\n", client_str);
int lastret = perform(conn);
if (lastret == -1 && errno != EPIPE) {
fprintf(stderr, "Error with %s: %s\n", client_str, strerror(errno));
} else {
fprintf(stderr, "Closed by client\n");
}
checkerr(close(conn), "close connection to %s", client_str);
}
}
// call perform() then close() the socket
void client(int conn, ssize_t(*perform)(int)) {
fprintf(stderr, "Connected, from %s\n", local2str(conn));
ssize_t lastret = perform(conn);
if (lastret == -1 && errno != EPIPE) {
fprintf(stderr, "Error: %s\n", strerror(errno));
} else {
fprintf(stderr, "Closed by server\n");
}
checkerr(close(conn), "close connection");
}
// send the received data back to the sender and to stdout
ssize_t echo(int conn) {
char buf[BUFFER_SIZE];
while (true) {
ssize_t received = recv(conn, &buf, sizeof(buf), MSG_NOSIGNAL);
if (received <= 0) {
return received;
}
checkerr(fwrite(&buf, received, 1, stdout), "write to stdout");
ssize_t sent = 0;
while (sent < received) {
ssize_t this_send = send(conn, &buf[sent], received-sent, MSG_NOSIGNAL);
if (this_send <= 0) {
return this_send;
}
sent += this_send;
}
}
}
#define STDIN 0
#define STDOUT 1
// used to restore nonblocking-ness on exit
// is inititalized by make_stdin_nonblocking() if used
int original_stdin_flags;
void restore_stdin_flags() {
fcntl(STDIN, F_SETFL, original_stdin_flags);
// ignore any error; program is terminating already and printf() isn't signal-safe
}
void signal_handler(int signal) {
restore_stdin_flags();
raise(signal); // continue to default handler (this handler was registered as oneshot)
}
// set stdin to nonblocking mode and register functions to restore it at program exit
void make_stdin_nonblocking() {
// set stdin to nonblocking mode unless it's a file
// (because it would then never make progress on Linux)
struct stat stdinfo;
checkerr(fstat(STDIN, &stdinfo), "stat stdin");
if ((stdinfo.st_mode & S_IFMT) != S_IFREG) {
original_stdin_flags = checkerr(fcntl(STDIN, F_GETFL, 0), "get flags for stdin");
checkerr(fcntl(STDIN, F_SETFL, original_stdin_flags | O_NONBLOCK),
"make stdin nonblocking");
// restore blockingness on exit, otherwise `git add -p` and other commands stop working
// afterwards. (The `reset` command doesn't restore this either, but invoking `bash` then
// exiting fixes it).
atexit(restore_stdin_flags);
struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_handler = signal_handler;
act.sa_flags = SA_RESETHAND; // resume with default handler after restoring
sigaction(SIGINT, &act, NULL);
sigaction(SIGTERM, &act, NULL);
}
}
// send stdin to socket and socket to stdout using select() to avoid blocking on either side,
// and disconnect when peer has nothing more to send.
ssize_t interactive_async_read(int conn) {
make_stdin_nonblocking();
char buf[BUFFER_SIZE];
ssize_t received;
// main loop; runs until either stdin reaches EOF or peer disconnects
while (true) {
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(STDIN, &readfds);
FD_SET(conn, &readfds);
fd_set errfds = readfds;
checkerr(select(conn+1, &readfds, NULL, &errfds, NULL), "select()");
while ((received = recv(conn, &buf, sizeof(buf), MSG_NOSIGNAL | MSG_DONTWAIT)) > 0) {
checkerr(fwrite(&buf, received, 1, stdout), "write to stdout");
}
if (received == 0 || (errno != EAGAIN && errno != EWOULDBLOCK)) {
return received;
}
ssize_t to_send;
while ((to_send = read(STDIN, &buf, sizeof(buf))) > 0) {
ssize_t sent = 0;
while (sent < to_send) {
ssize_t this_send = send(conn, &buf[sent], to_send-sent, MSG_NOSIGNAL);
if (this_send <= 0) {
return this_send;
}
sent += this_send;
}
}
if (to_send == 0) {
break;
} else if (errno != EAGAIN && errno != EWOULDBLOCK) {
return to_send;
}
}
// nothing more to send, but wait for response
if (shutdown(conn, SHUT_WR) == -1) {
fprintf(stderr, "Cannot shutdown send side: %s\n", strerror(errno));
// don't exit()
}
while ((received = recv(conn, &buf, sizeof(buf), MSG_NOSIGNAL /*(do wait now)*/)) > 0) {
checkerr(fwrite(&buf, received, 1, stdout), "write to stdout");
}
return received;
}
int main(int argc, char **argv) {
if (argc == 3 && !strcmp(argv[1], "listen")) {
accept_loop(listen_any(argv[2]), interactive_async_read);
} else if (argc == 4 && !strcmp(argv[1], "listen")) {
accept_loop(listen_on(argv[2], argv[3]), interactive_async_read);
} else if (argc == 3 && !strcmp(argv[1], "echo")) {
accept_loop(listen_any(argv[2]), echo);
} else if (argc == 4 && !strcmp(argv[1], "echo")) {
accept_loop(listen_on(argv[2], argv[3]), echo);
} else if (argc == 2) {
client(connect_to_localhost(argv[1]), interactive_async_read);
} else if (argc == 3) {
client(connect_to(argv[1], argv[2]), interactive_async_read);
} else if (argc == 5) {
client(connect_from_to(argv[1], argv[2], argv[3], argv[4]), interactive_async_read);
} else {
fprintf(stderr, "Usage:\n");
fprintf(stderr, "\ttcp [[source_addr source_port] domain] port - select()-based client\n");
fprintf(stderr, "\ttcp listen [addr] port - select()-based server\n");
fprintf(stderr, "\ttcp echo [addr] port - echo server\n");
exit(1);
}
return 0;
}
// Things this program doesn't do (for simplicity), but code that wants to be robust should consider:
// * Retry accept() if it fails with ECONNABORTED, ECONNRESET or ECONNREFUSED.
// * Retry reads, writes, connect(), select() and accept() if they fail with EINTR.
// * Avoid using global variables, or at least make them thread-local.
// * Set CLOEXEC on created sockets. (using SOCK_CLOEXEC and accept4() where available)