-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_listen.c
63 lines (50 loc) · 1.01 KB
/
tcp_listen.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
#include <err.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <unistd.h>
#include "cmd.h"
#include "core.h"
#define USAGE "[-p port] [-s]"
static int tcp_listen_main(int argc, char **argv)
{
int opt, port = 4242;
unsigned serve_flags = 0;
struct sockaddr_in addr;
int sd;
while ((opt = getopt(argc, argv, "p:s")) != -1) {
switch (opt) {
case 'p':
port = atoi(optarg);
break;
case 's':
serve_flags |= SERVE_SPLICE;
break;
default:
goto usage;
}
}
if (optind < argc)
goto usage;
addr = (struct sockaddr_in) {
.sin_family = AF_INET,
.sin_port = port,
};
sd = socket(AF_INET, SOCK_STREAM, 0);
if (sd < 0)
err(-1, "socket");
if (bind(sd, (struct sockaddr *)&addr, sizeof(addr)))
err(-1, "bind");
if (listen(sd, SOMAXCONN))
err(-1, "listen");
do_serve(sd, serve_flags);
close(sd);
return 0;
usage:
errx(-1, "Usage: %s " USAGE, argv[0]);
}
struct cmd tcp_listen_cmd = {
.name = "tcp-listen",
.main = tcp_listen_main,
.usage = USAGE,
};