-
Notifications
You must be signed in to change notification settings - Fork 0
/
cl.c
198 lines (155 loc) · 4.41 KB
/
cl.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
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <poll.h>
#include <getopt.h>
typedef struct {
int count;
int lfd;
int verbose;
size_t maxfd;
} cl_state_t;
static int cl_listen(cl_state_t *cp, const char *addr, const char *port);
static int cl_accept(cl_state_t *cp);
static const struct option long_options[] = {
{"verbose", no_argument, NULL, 'v'}, {NULL, 0, NULL, 0}};
int main(int argc, char *argv[]) {
cl_state_t *cp;
struct rlimit maxfd;
int ch = 0;
cp = calloc(1, sizeof(cl_state_t));
if (cp == NULL)
err(EXIT_FAILURE, "calloc");
if (getrlimit(RLIMIT_NOFILE, &maxfd) < 0)
return -1;
cp->maxfd = maxfd.rlim_cur;
while ((ch = getopt_long(argc, argv, "v", long_options, NULL)) != -1) {
switch (ch) {
case 'v':
cp->verbose++;
break;
default:
errx(EXIT_FAILURE, "usage: <count> <localaddr> <localport>");
}
}
argc -= optind;
argv += optind;
if (setvbuf(stdout, NULL, _IOLBF, 0) < 0)
err(EXIT_FAILURE, "setvbuf");
if (argc != 3) {
errx(EXIT_FAILURE, "usage: <count> <localaddr> <localport>");
}
cp->count = atoi(argv[0]);
if (cp->count <= 0)
errx(EXIT_FAILURE, "usage: <count> <localaddr> <localport>");
if (cl_listen(cp, argv[1], argv[2]) < 0)
err(112, "listen:%s:%s", argv[1], argv[2]);
if (cl_accept(cp) < 0)
err(113, "accept:%s -> %s:%s", argv[0], argv[1], argv[2]);
exit(0);
}
static int cl_accept(cl_state_t *cp) {
struct pollfd *fds;
fds = calloc(cp->maxfd, sizeof(struct pollfd));
if (fds == NULL)
return -1;
/* listening socket */
fds[cp->lfd].fd = cp->lfd;
fds[cp->lfd].events = POLLIN;
for (;;) {
if (poll(fds, cp->maxfd, -1) < 0) {
switch (errno) {
case EINTR:
continue;
default:
return -1;
}
}
if (fds[cp->lfd].revents & (POLLERR | POLLHUP | POLLNVAL))
return -1;
if (fds[cp->lfd].revents & POLLIN) {
int fd;
struct sockaddr_storage paddr;
socklen_t plen;
char addrstr[INET6_ADDRSTRLEN] = {0};
plen = sizeof(paddr);
fd = accept(cp->lfd, (struct sockaddr *)&paddr, &plen);
if (fd < 0)
return -1;
cp->count--;
if (cp->verbose > 0) {
switch (((struct sockaddr *)&paddr)->sa_family) {
case AF_INET6:
(void)fprintf(
stderr, "accept:%d:%s:%u\n", cp->count,
inet_ntop(AF_INET6,
&(((const struct sockaddr_in6 *)&paddr)->sin6_addr),
addrstr, sizeof(addrstr)),
ntohs(((const struct sockaddr_in6 *)&paddr)->sin6_port));
break;
case AF_INET:
(void)fprintf(
stderr, "accept:%d:%s:%u\n", cp->count,
inet_ntop(AF_INET,
&(((const struct sockaddr_in *)&paddr)->sin_addr),
addrstr, sizeof(addrstr)),
ntohs(((const struct sockaddr_in *)&paddr)->sin_port));
break;
default:
errno = EINVAL;
return -1;
}
}
if (close(fd) < 0)
return -1;
if (cp->count <= 0)
return 0;
}
}
}
static int cl_listen(cl_state_t *cp, const char *addr, const char *port) {
struct addrinfo hints = {0};
struct addrinfo *res;
struct addrinfo *rp;
int s;
int enable = 1;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV | AI_PASSIVE;
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
s = getaddrinfo(addr, port, &hints, &res);
if (s != 0) {
(void)fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
return -1;
}
for (rp = res; rp != NULL; rp = rp->ai_next) {
cp->lfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (setsockopt(cp->lfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) <
0)
return -1;
if (setsockopt(cp->lfd, SOL_SOCKET, SO_REUSEPORT, &enable, sizeof(enable)) <
0)
return -1;
if (bind(cp->lfd, rp->ai_addr, rp->ai_addrlen) < 0)
return -1;
if (listen(cp->lfd, SOMAXCONN) >= 0)
break;
}
freeaddrinfo(res);
if (rp == NULL)
return -1;
return 0;
}