forked from sustrik/dsock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ipaddr.c
333 lines (309 loc) · 10.2 KB
/
ipaddr.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
/*
Copyright (c) 2015 Martin Sustrik
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#if defined __linux__
#define _GNU_SOURCE
#include <netdb.h>
#include <sys/eventfd.h>
#endif
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#if !defined __sun
#include <ifaddrs.h>
#endif
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "dns/dns.h"
#include "dsock.h"
#include "utils.h"
/* Make sure that both IPv4 and IPv6 address fits into ipaddr. */
DSOCK_CT_ASSERT(sizeof(ipaddr) >= sizeof(struct sockaddr_in));
DSOCK_CT_ASSERT(sizeof(ipaddr) >= sizeof(struct sockaddr_in6));
static struct dns_resolv_conf *dsock_dns_conf = NULL;
static struct dns_hosts *dsock_dns_hosts = NULL;
static struct dns_hints *dsock_dns_hints = NULL;
static int ipaddr_ipany(ipaddr *addr, int port, int mode)
{
if(dsock_slow(port < 0 || port > 0xffff)) {errno = EINVAL; return -1;}
if (mode == 0 || mode == IPADDR_IPV4 || mode == IPADDR_PREF_IPV4) {
struct sockaddr_in *ipv4 = (struct sockaddr_in*)addr;
ipv4->sin_family = AF_INET;
ipv4->sin_addr.s_addr = htonl(INADDR_ANY);
ipv4->sin_port = htons((uint16_t)port);
return 0;
}
else {
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6*)addr;
ipv6->sin6_family = AF_INET6;
memcpy(&ipv6->sin6_addr, &in6addr_any, sizeof(in6addr_any));
ipv6->sin6_port = htons((uint16_t)port);
return 0;
}
}
/* Convert literal IPv4 address to a binary one. */
static int ipaddr_ipv4_literal(ipaddr *addr, const char *name, int port) {
struct sockaddr_in *ipv4 = (struct sockaddr_in*)addr;
int rc = inet_pton(AF_INET, name, &ipv4->sin_addr);
dsock_assert(rc >= 0);
if(dsock_slow(rc != 1)) {errno = EINVAL; return -1;}
ipv4->sin_family = AF_INET;
ipv4->sin_port = htons((uint16_t)port);
return 0;
}
/* Convert literal IPv6 address to a binary one. */
static int ipaddr_ipv6_literal(ipaddr *addr, const char *name, int port) {
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6*)addr;
int rc = inet_pton(AF_INET6, name, &ipv6->sin6_addr);
dsock_assert(rc >= 0);
if(dsock_slow(rc != 1)) {errno = EINVAL; return -1;}
ipv6->sin6_family = AF_INET6;
ipv6->sin6_port = htons((uint16_t)port);
return 0;
}
/* Convert literal IPv4 or IPv6 address to a binary one. */
static int ipaddr_literal(ipaddr *addr, const char *name, int port, int mode) {
if(dsock_slow(!addr || port < 0 || port > 0xffff)) {
errno = EINVAL;
return -1;
}
int rc;
switch(mode) {
case IPADDR_IPV4:
return ipaddr_ipv4_literal(addr, name, port);
case IPADDR_IPV6:
return ipaddr_ipv6_literal(addr, name, port);
case 0:
case IPADDR_PREF_IPV4:
rc = ipaddr_ipv4_literal(addr, name, port);
if(rc == 0)
return 0;
return ipaddr_ipv6_literal(addr, name, port);
case IPADDR_PREF_IPV6:
rc = ipaddr_ipv6_literal(addr, name, port);
if(rc == 0)
return 0;
return ipaddr_ipv4_literal(addr, name, port);
default:
dsock_assert(0);
}
}
int ipaddr_family(const ipaddr *addr) {
return ((struct sockaddr*)addr)->sa_family;
}
int ipaddr_len(const ipaddr *addr) {
return ipaddr_family(addr) == AF_INET ?
sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
}
const struct sockaddr *ipaddr_sockaddr(const ipaddr *addr) {
return (const struct sockaddr*)addr;
}
int ipaddr_port(const ipaddr *addr) {
return ntohs(ipaddr_family(addr) == AF_INET ?
((struct sockaddr_in*)addr)->sin_port :
((struct sockaddr_in6*)addr)->sin6_port);
}
void ipaddr_setport(ipaddr *addr, int port) {
if(ipaddr_family(addr) == AF_INET)
((struct sockaddr_in*)addr)->sin_port = htons(port);
else
((struct sockaddr_in6*)addr)->sin6_port = htons(port);
}
/* Convert IP address from network format to ASCII dot notation. */
const char *ipaddr_str(const ipaddr *addr, char *ipstr) {
if(ipaddr_family(addr) == AF_INET) {
return inet_ntop(AF_INET, &(((struct sockaddr_in*)addr)->sin_addr),
ipstr, INET_ADDRSTRLEN);
}
else {
return inet_ntop(AF_INET6, &(((struct sockaddr_in6*)addr)->sin6_addr),
ipstr, INET6_ADDRSTRLEN);
}
}
int ipaddr_local(ipaddr *addr, const char *name, int port, int mode) {
if(!name)
return ipaddr_ipany(addr, port, mode);
int rc = ipaddr_literal(addr, name, port, mode);
#if defined __sun
return rc;
#else
if(rc == 0)
return 0;
/* Address is not a literal. It must be an interface name then. */
struct ifaddrs *ifaces = NULL;
rc = getifaddrs (&ifaces);
dsock_assert (rc == 0);
dsock_assert (ifaces);
/* Find first IPv4 and first IPv6 address. */
struct ifaddrs *ipv4 = NULL;
struct ifaddrs *ipv6 = NULL;
struct ifaddrs *it;
for(it = ifaces; it != NULL; it = it->ifa_next) {
if(!it->ifa_addr)
continue;
if(strcmp(it->ifa_name, name) != 0)
continue;
switch(it->ifa_addr->sa_family) {
case AF_INET:
dsock_assert(!ipv4);
ipv4 = it;
break;
case AF_INET6:
dsock_assert(!ipv6);
ipv6 = it;
break;
}
if(ipv4 && ipv6)
break;
}
/* Choose the correct address family based on mode. */
switch(mode) {
case IPADDR_IPV4:
ipv6 = NULL;
break;
case IPADDR_IPV6:
ipv4 = NULL;
break;
case 0:
case IPADDR_PREF_IPV4:
if(ipv4)
ipv6 = NULL;
break;
case IPADDR_PREF_IPV6:
if(ipv6)
ipv4 = NULL;
break;
default:
dsock_assert(0);
}
if(ipv4) {
struct sockaddr_in *inaddr = (struct sockaddr_in*)addr;
memcpy(inaddr, ipv4->ifa_addr, sizeof (struct sockaddr_in));
inaddr->sin_port = htons(port);
freeifaddrs(ifaces);
return 0;
}
if(ipv6) {
struct sockaddr_in6 *inaddr = (struct sockaddr_in6*)addr;
memcpy(inaddr, ipv6->ifa_addr, sizeof (struct sockaddr_in6));
inaddr->sin6_port = htons(port);
freeifaddrs(ifaces);
return 0;
}
freeifaddrs(ifaces);
errno = ENODEV;
return -1;
#endif
}
int ipaddr_remote(ipaddr *addr, const char *name, int port, int mode,
int64_t deadline) {
int rc = ipaddr_literal(addr, name, port, mode);
if(rc == 0)
return 0;
/* Load DNS config files, unless they are already chached. */
if(dsock_slow(!dsock_dns_conf)) {
/* TODO: Maybe re-read the configuration once in a while? */
dsock_dns_conf = dns_resconf_local(&rc);
dsock_assert(dsock_dns_conf);
dsock_dns_hosts = dns_hosts_local(&rc);
dsock_assert(dsock_dns_hosts);
dsock_dns_hints = dns_hints_local(dsock_dns_conf, &rc);
dsock_assert(dsock_dns_hints);
}
/* Let's do asynchronous DNS query here. */
struct dns_resolver *resolver = dns_res_open(dsock_dns_conf,
dsock_dns_hosts, dsock_dns_hints, NULL, dns_opts(), &rc);
dsock_assert(resolver);
dsock_assert(port >= 0 && port <= 0xffff);
char portstr[8];
snprintf(portstr, sizeof(portstr), "%d", port);
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
struct dns_addrinfo *ai = dns_ai_open(name, portstr, DNS_T_A, &hints,
resolver, &rc);
dsock_assert(ai);
dns_res_close(resolver);
struct addrinfo *ipv4 = NULL;
struct addrinfo *ipv6 = NULL;
struct addrinfo *it = NULL;
while(1) {
rc = dns_ai_nextent(&it, ai);
if(rc == EAGAIN) {
int fd = dns_ai_pollfd(ai);
dsock_assert(fd >= 0);
int rc = fdin(fd, deadline);
/* There's no guarantee that the file descriptor will be reused
in next iteration. We have to clean the fdwait cache here
to be on the safe side. */
int err = errno;
fdclean(fd);
errno = err;
if(dsock_slow(rc < 0)) return -1;
continue;
}
if(rc == ENOENT)
break;
if(!ipv4 && it && it->ai_family == AF_INET)
ipv4 = it;
if(!ipv6 && it && it->ai_family == AF_INET6)
ipv6 = it;
if(ipv4 && ipv6)
break;
}
switch(mode) {
case IPADDR_IPV4:
ipv6 = NULL;
break;
case IPADDR_IPV6:
ipv4 = NULL;
break;
case 0:
case IPADDR_PREF_IPV4:
if(ipv4)
ipv6 = NULL;
break;
case IPADDR_PREF_IPV6:
if(ipv6)
ipv4 = NULL;
break;
default:
dsock_assert(0);
}
if(ipv4) {
struct sockaddr_in *inaddr = (struct sockaddr_in*)addr;
memcpy(inaddr, ipv4->ai_addr, sizeof (struct sockaddr_in));
inaddr->sin_port = htons(port);
dns_ai_close(ai);
return 0;
}
if(ipv6) {
struct sockaddr_in6 *inaddr = (struct sockaddr_in6*)addr;
memcpy(inaddr, ipv6->ai_addr, sizeof (struct sockaddr_in6));
inaddr->sin6_port = htons(port);
dns_ai_close(ai);
return 0;
}
dns_ai_close(ai);
errno = EADDRNOTAVAIL;
return -1;
}