-
Notifications
You must be signed in to change notification settings - Fork 0
/
closed.c
180 lines (159 loc) · 5.57 KB
/
closed.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
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#define FREEADDRINFO(r) do { \
freeaddrinfo(r); \
r = NULL; \
} while(0)
#define LOG_ERRNO(fmt, ...) do { \
fprintf(stderr, fmt ": %s\n", ##__VA_ARGS__, strerror(errno)); \
} while(0)
#define LOG_GAIERR(err, fmt, ...) do { \
fprintf(stderr, fmt ": %s\n", ##__VA_ARGS__, gai_strerror(err)); \
} while(0)
#define XLOG_GAIERR(err, ...) do { \
LOG_GAIERR(err, ##__VA_ARGS__); \
return -err; \
while(0)
#define XLOG_ERRNO(...) do { \
LOG_ERRNO(##__VA_ARGS__); \
return -errno; \
while(0)
static const struct addrinfo HINTS = {
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM,
.ai_flags = AI_PASSIVE | AI_V4MAPPED | AI_ADDRCONFIG,
.ai_protocol = 0,
.ai_addrlen = 0,
.ai_addr = NULL,
.ai_canonname = NULL,
.ai_next = NULL
};
static const char *const DEFAULT_HOST = "::";
static const char *const DEFAULT_PORT = "8009";
static const int BACKLOG = 10;
static const int YES = 1;
int main (const int argc, const char *const argv[]) {
struct addrinfo *res;
struct addrinfo *res_p;
int rv;
int srv_fd;
fd_set srv_fd_set;
char ni_host[NI_MAXHOST];
char ni_serv[NI_MAXSERV];
struct sockaddr_storage remote_addr_storage;
struct sockaddr *const remote_addr = (struct sockaddr *) &remote_addr_storage;
socklen_t remote_addr_len;
const char *const host = argc > 1 ? argv[1] : DEFAULT_HOST;
const char *const port = argc > 2 ? argv[2] : DEFAULT_PORT;
if ((rv = getaddrinfo(host, port, NULL, &res)) != 0) {
LOG_GAIERR(rv, "getaddrinfo() failed");
goto main_err0;
}
/* socket() bind() listen() */
for (res_p = res; res_p != NULL; res_p = res_p->ai_next) {
if ((rv = getnameinfo(res_p->ai_addr, res_p->ai_addrlen, ni_host,
NI_MAXHOST, ni_serv, NI_MAXSERV, NI_NUMERICHOST
| NI_NUMERICSERV)) == 0) {
if (strchr(ni_host, ':') == NULL) {
/* IPv4 address */
fprintf(stderr, "Trying %s:%s\n", ni_host, ni_serv);
} else {
/* IPv6 literal */
fprintf(stderr, "Trying [%s]:%s\n", ni_host, ni_serv);
}
} else {
if (rv == EAI_SYSTEM) {
LOG_ERRNO("getnameinfo() failed");
} else {
LOG_GAIERR(rv, "getnameinfo() failed");
}
goto main_err1;
}
if ((srv_fd = socket(res_p->ai_family, res_p->ai_socktype, res_p->ai_protocol)) < 0) {
LOG_ERRNO("socket() failed");
} else if (setsockopt(srv_fd, SOL_SOCKET, SO_REUSEADDR, &YES, sizeof(YES)) == -1) {
LOG_ERRNO("setsockopt() failed");
if (close(srv_fd) == -1) {
LOG_ERRNO("close() failed");
goto main_err1;
}
} else if (bind(srv_fd, res_p->ai_addr, res_p->ai_addrlen) == -1) {
LOG_ERRNO("bind() failed");
if (close(srv_fd) == -1) {
LOG_ERRNO("close() failed");
goto main_err1;
}
} else if (listen(srv_fd, BACKLOG) == -1) {
LOG_ERRNO("listen() failed");
if (close(srv_fd) == -1) {
LOG_ERRNO("close() failed");
goto main_err1;
}
} else {
/* Everything worked */
break;
}
}
if (srv_fd < 0) { goto main_err1; }
while(1) {
FD_ZERO(&srv_fd_set);
FD_SET(srv_fd, &srv_fd_set);
if ((rv = select(srv_fd+1, &srv_fd_set, NULL, NULL, NULL)) == -1) {
LOG_ERRNO("select() failed");
goto main_err2;
} else if (rv > 0) {
if (FD_ISSET(srv_fd, &srv_fd_set)) {
remote_addr_len = sizeof(remote_addr_storage);
const int fd = accept(srv_fd, remote_addr, &remote_addr_len);
if (fd == -1) {
LOG_ERRNO("accept() failed");
goto main_err2;
}
if ((rv = getnameinfo(remote_addr, remote_addr_len, ni_host,
NI_MAXHOST, ni_serv, NI_MAXSERV, NI_NUMERICHOST
| NI_NUMERICSERV)) == 0) {
if (strchr(ni_host, ':') == NULL) {
/* IPv4 address */
fprintf(stderr, "%s:%s Connected...", ni_host, ni_serv);
} else {
/* IPv6 literal */
fprintf(stderr, "[%s]:%s Connected...", ni_host, ni_serv);
}
} else {
if (rv == EAI_SYSTEM) {
LOG_ERRNO("getnameinfo() failed: system error");
} else {
LOG_GAIERR(rv, "getnameinfo() failed");
}
if (close(fd) == -1) {
LOG_ERRNO("close() failed");
}
goto main_err2;
}
if (close(fd) == -1) {
LOG_ERRNO("close() failed");
goto main_err2;
}
fprintf(stderr, " closed\n");
}
}
}
close(srv_fd);
FREEADDRINFO(res);
return EXIT_SUCCESS;
main_err2:
if (close(srv_fd) == -1) {
LOG_ERRNO("close() failed");
}
main_err1:
FREEADDRINFO(res);
main_err0:
return EXIT_FAILURE;
}
/* vim: set ts=4 sw=4 sts et ai: */