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
|
/* Licensed under BSD-MIT - see LICENSE file for details */
#include <ccan/net/net.h>
#include <ccan/noerr/noerr.h>
#include <poll.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdbool.h>
#include <netinet/in.h>
#include <assert.h>
struct addrinfo *net_client_lookup(const char *hostname,
const char *service,
int family,
int socktype)
{
struct addrinfo hints;
struct addrinfo *res;
memset(&hints, 0, sizeof(hints));
hints.ai_family = family;
hints.ai_socktype = socktype;
hints.ai_flags = 0;
hints.ai_protocol = 0;
if (getaddrinfo(hostname, service, &hints, &res) != 0)
return NULL;
return res;
}
static bool set_nonblock(int fd, bool nonblock)
{
long flags;
flags = fcntl(fd, F_GETFL);
if (flags == -1)
return false;
if (nonblock)
flags |= O_NONBLOCK;
else
flags &= ~(long)O_NONBLOCK;
return (fcntl(fd, F_SETFL, flags) == 0);
}
static int start_connect(const struct addrinfo *addr, bool *immediate)
{
int fd;
*immediate = false;
fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
if (fd == -1)
return fd;
if (!set_nonblock(fd, true))
goto close;
if (connect(fd, addr->ai_addr, addr->ai_addrlen) == 0) {
/* Immediate connect. */
*immediate = true;
return fd;
}
if (errno == EINPROGRESS)
return fd;
close:
close_noerr(fd);
return -1;
}
int net_connect_async(const struct addrinfo *addrinfo, struct pollfd pfds[2])
{
const struct addrinfo *addr[2] = { NULL, NULL };
unsigned int i;
pfds[0].fd = pfds[1].fd = -1;
pfds[0].events = pfds[1].events = POLLOUT;
/* Give IPv6 a slight advantage, by trying it first. */
for (; addrinfo; addrinfo = addrinfo->ai_next) {
switch (addrinfo->ai_family) {
case AF_INET:
addr[1] = addrinfo;
break;
case AF_INET6:
addr[0] = addrinfo;
break;
default:
continue;
}
}
/* In case we found nothing. */
errno = ENOENT;
for (i = 0; i < 2; i++) {
bool immediate;
if (!addr[i])
continue;
pfds[i].fd = start_connect(addr[i], &immediate);
if (immediate) {
if (pfds[!i].fd != -1)
close(pfds[!i].fd);
if (!set_nonblock(pfds[i].fd, false)) {
close_noerr(pfds[i].fd);
return -1;
}
return pfds[i].fd;
}
}
if (pfds[0].fd != -1 || pfds[1].fd != -1)
errno = EINPROGRESS;
return -1;
}
void net_connect_abort(struct pollfd pfds[2])
{
unsigned int i;
for (i = 0; i < 2; i++) {
if (pfds[i].fd != -1)
close_noerr(pfds[i].fd);
pfds[i].fd = -1;
}
}
int net_connect_complete(struct pollfd pfds[2])
{
unsigned int i;
assert(pfds[0].fd != -1 || pfds[1].fd != -1);
for (i = 0; i < 2; i++) {
int err;
socklen_t errlen = sizeof(err);
if (pfds[i].fd == -1)
continue;
if (pfds[i].revents & POLLHUP) {
/* Linux gives this if connecting to local
* non-listening port */
close(pfds[i].fd);
pfds[i].fd = -1;
if (pfds[!i].fd == -1) {
errno = ECONNREFUSED;
return -1;
}
continue;
}
if (!(pfds[i].revents & POLLOUT))
continue;
if (getsockopt(pfds[i].fd, SOL_SOCKET, SO_ERROR, &err,
&errlen) != 0) {
net_connect_abort(pfds);
return -1;
}
if (err == 0) {
/* Don't hand them non-blocking fd! */
if (!set_nonblock(pfds[i].fd, false)) {
net_connect_abort(pfds);
return -1;
}
/* Close other one. */
if (pfds[!i].fd != -1)
close(pfds[!i].fd);
return pfds[i].fd;
}
}
/* Still going... */
errno = EINPROGRESS;
return -1;
}
int net_connect(const struct addrinfo *addrinfo)
{
struct pollfd pfds[2];
int sockfd;
sockfd = net_connect_async(addrinfo, pfds);
/* Immediate connect or error is easy. */
if (sockfd >= 0 || errno != EINPROGRESS)
return sockfd;
while (poll(pfds, 2, -1) != -1) {
sockfd = net_connect_complete(pfds);
if (sockfd >= 0 || errno != EINPROGRESS)
return sockfd;
}
net_connect_abort(pfds);
return -1;
}
struct addrinfo *net_server_lookup(const char *service,
int family,
int socktype)
{
struct addrinfo *res, hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = family;
hints.ai_socktype = socktype;
hints.ai_flags = AI_PASSIVE;
hints.ai_protocol = 0;
if (getaddrinfo(NULL, service, &hints, &res) != 0)
return NULL;
return res;
}
static bool should_listen(const struct addrinfo *addrinfo)
{
#ifdef SOCK_SEQPACKET
if (addrinfo->ai_socktype == SOCK_SEQPACKET)
return true;
#endif
return (addrinfo->ai_socktype == SOCK_STREAM);
}
static int make_listen_fd(const struct addrinfo *addrinfo)
{
int saved_errno, fd, on = 1;
fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
addrinfo->ai_protocol);
if (fd < 0)
return -1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
goto fail;
if (should_listen(addrinfo) && listen(fd, 5) != 0)
goto fail;
return fd;
fail:
saved_errno = errno;
close(fd);
errno = saved_errno;
return -1;
}
int net_bind(const struct addrinfo *addrinfo, int fds[2])
{
const struct addrinfo *ipv6 = NULL;
const struct addrinfo *ipv4 = NULL;
unsigned int num;
if (addrinfo->ai_family == AF_INET)
ipv4 = addrinfo;
else if (addrinfo->ai_family == AF_INET6)
ipv6 = addrinfo;
if (addrinfo->ai_next) {
if (addrinfo->ai_next->ai_family == AF_INET)
ipv4 = addrinfo->ai_next;
else if (addrinfo->ai_next->ai_family == AF_INET6)
ipv6 = addrinfo->ai_next;
}
num = 0;
/* Take IPv6 first, since it might bind to IPv4 port too. */
if (ipv6) {
if ((fds[num] = make_listen_fd(ipv6)) >= 0)
num++;
else
ipv6 = NULL;
}
if (ipv4) {
if ((fds[num] = make_listen_fd(ipv4)) >= 0)
num++;
else
ipv4 = NULL;
}
if (num == 0)
return -1;
return num;
}
|