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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
|
/* MIT License
*
* Copyright (c) 2024 Brad House
*
* 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 (including the next
* paragraph) 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.
*
* SPDX-License-Identifier: MIT
*/
#include "ares_private.h"
#ifdef HAVE_SYS_UIO_H
# include <sys/uio.h>
#endif
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#ifdef HAVE_NETINET_TCP_H
# include <netinet/tcp.h>
#endif
#ifdef HAVE_NETDB_H
# include <netdb.h>
#endif
#ifdef HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
#endif
#ifdef NETWARE
# include <sys/filio.h>
#endif
#include <assert.h>
#include <fcntl.h>
#include <limits.h>
#if defined(__linux__) && defined(TCP_FASTOPEN_CONNECT)
# define TFO_SUPPORTED 1
# define TFO_SKIP_CONNECT 0
# define TFO_USE_SENDTO 0
# define TFO_USE_CONNECTX 0
# define TFO_CLIENT_SOCKOPT TCP_FASTOPEN_CONNECT
#elif defined(__FreeBSD__) && defined(TCP_FASTOPEN)
# define TFO_SUPPORTED 1
# define TFO_SKIP_CONNECT 1
# define TFO_USE_SENDTO 1
# define TFO_USE_CONNECTX 0
# define TFO_CLIENT_SOCKOPT TCP_FASTOPEN
#elif defined(__APPLE__) && defined(HAVE_CONNECTX)
# define TFO_SUPPORTED 1
# define TFO_SKIP_CONNECT 0
# define TFO_USE_SENDTO 0
# define TFO_USE_CONNECTX 1
# undef TFO_CLIENT_SOCKOPT
#else
# define TFO_SUPPORTED 0
#endif
#ifndef HAVE_WRITEV
/* Structure for scatter/gather I/O. */
struct iovec {
void *iov_base; /* Pointer to data. */
size_t iov_len; /* Length of data. */
};
#endif
ares_status_t
ares_set_socket_functions_ex(ares_channel_t *channel,
const struct ares_socket_functions_ex *funcs,
void *user_data)
{
unsigned int known_versions[] = { 1 };
size_t i;
if (channel == NULL || funcs == NULL) {
return ARES_EFORMERR;
}
/* Check to see if we know the version referenced */
for (i = 0; i < sizeof(known_versions) / sizeof(*known_versions); i++) {
if (funcs->version == known_versions[i]) {
break;
}
}
if (i == sizeof(known_versions) / sizeof(*known_versions)) {
return ARES_EFORMERR;
}
memset(&channel->sock_funcs, 0, sizeof(channel->sock_funcs));
/* Copy individually for ABI compliance. memcpy() with a sizeof would do
* invalid reads */
if (funcs->version >= 1) {
if (funcs->asocket == NULL || funcs->aclose == NULL ||
funcs->asetsockopt == NULL || funcs->aconnect == NULL ||
funcs->arecvfrom == NULL || funcs->asendto == NULL) {
return ARES_EFORMERR;
}
channel->sock_funcs.version = funcs->version;
channel->sock_funcs.flags = funcs->flags;
channel->sock_funcs.asocket = funcs->asocket;
channel->sock_funcs.aclose = funcs->aclose;
channel->sock_funcs.asetsockopt = funcs->asetsockopt;
channel->sock_funcs.aconnect = funcs->aconnect;
channel->sock_funcs.arecvfrom = funcs->arecvfrom;
channel->sock_funcs.asendto = funcs->asendto;
channel->sock_funcs.agetsockname = funcs->agetsockname;
channel->sock_funcs.abind = funcs->abind;
}
/* Implement newer versions here ...*/
channel->sock_func_cb_data = user_data;
return ARES_SUCCESS;
}
static int setsocknonblock(ares_socket_t sockfd, /* operate on this */
int nonblock /* TRUE or FALSE */)
{
#if defined(HAVE_FCNTL_O_NONBLOCK)
/* most recent unix versions */
int flags;
flags = fcntl(sockfd, F_GETFL, 0);
if (nonblock) {
return fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
} else {
return fcntl(sockfd, F_SETFL, flags & (~O_NONBLOCK)); /* LCOV_EXCL_LINE */
}
#elif defined(HAVE_IOCTL_FIONBIO)
/* older unix versions */
int flags = nonblock ? 1 : 0;
return ioctl(sockfd, FIONBIO, &flags);
#elif defined(HAVE_IOCTLSOCKET_FIONBIO)
# ifdef WATT32
char flags = nonblock ? 1 : 0;
# else
/* Windows */
unsigned long flags = nonblock ? 1UL : 0UL;
# endif
return ioctlsocket(sockfd, (long)FIONBIO, &flags);
#elif defined(HAVE_IOCTLSOCKET_CAMEL_FIONBIO)
/* Amiga */
long flags = nonblock ? 1L : 0L;
return IoctlSocket(sockfd, FIONBIO, flags);
#elif defined(HAVE_SETSOCKOPT_SO_NONBLOCK)
/* BeOS */
long b = nonblock ? 1L : 0L;
return setsockopt(sockfd, SOL_SOCKET, SO_NONBLOCK, &b, sizeof(b));
#else
# error "no non-blocking method was found/used/set"
#endif
}
static int default_aclose(ares_socket_t sock, void *user_data)
{
(void)user_data;
#if defined(HAVE_CLOSESOCKET)
return closesocket(sock);
#elif defined(HAVE_CLOSESOCKET_CAMEL)
return CloseSocket(sock);
#elif defined(HAVE_CLOSE_S)
return close_s(sock);
#else
return close(sock);
#endif
}
static ares_socket_t default_asocket(int domain, int type, int protocol,
void *user_data)
{
ares_socket_t s;
(void)user_data;
s = socket(domain, type, protocol);
if (s == ARES_SOCKET_BAD) {
return s;
}
if (setsocknonblock(s, 1) != 0) {
goto fail; /* LCOV_EXCL_LINE */
}
#if defined(FD_CLOEXEC) && !defined(MSDOS)
/* Configure the socket fd as close-on-exec. */
if (fcntl(s, F_SETFD, FD_CLOEXEC) != 0) {
goto fail; /* LCOV_EXCL_LINE */
}
#endif
/* No need to emit SIGPIPE on socket errors */
#if defined(SO_NOSIGPIPE)
{
int opt = 1;
(void)setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, (void *)&opt, sizeof(opt));
}
#endif
if (type == SOCK_STREAM) {
int opt = 1;
#ifdef TCP_NODELAY
/*
* Disable the Nagle algorithm (only relevant for TCP sockets, and thus not
* in configure_socket). In general, in DNS lookups we're pretty much
* interested in firing off a single request and then waiting for a reply,
* so batching isn't very interesting.
*/
if (setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (void *)&opt, sizeof(opt)) !=
0) {
goto fail;
}
#endif
}
#if defined(IPV6_V6ONLY) && defined(USE_WINSOCK)
/* Support for IPv4-mapped IPv6 addresses.
* Linux kernel, NetBSD, FreeBSD and Darwin: default is off;
* Windows Vista and later: default is on;
* DragonFly BSD: acts like off, and dummy setting;
* OpenBSD and earlier Windows: unsupported.
* Linux: controlled by /proc/sys/net/ipv6/bindv6only.
*/
if (domain == PF_INET6) {
int on = 0;
(void)setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&on, sizeof(on));
}
#endif
return s;
fail:
default_aclose(s, user_data);
return ARES_SOCKET_BAD;
}
static int default_asetsockopt(ares_socket_t sock, ares_socket_opt_t opt,
const void *val, ares_socklen_t val_size,
void *user_data)
{
switch (opt) {
case ARES_SOCKET_OPT_SENDBUF_SIZE:
if (val_size != sizeof(int)) {
SET_SOCKERRNO(EINVAL);
return -1;
}
return setsockopt(sock, SOL_SOCKET, SO_SNDBUF, val, val_size);
case ARES_SOCKET_OPT_RECVBUF_SIZE:
if (val_size != sizeof(int)) {
SET_SOCKERRNO(EINVAL);
return -1;
}
return setsockopt(sock, SOL_SOCKET, SO_RCVBUF, val, val_size);
case ARES_SOCKET_OPT_BIND_DEVICE:
if (!ares_str_isprint(val, (size_t)val_size)) {
SET_SOCKERRNO(EINVAL);
return -1;
}
#ifdef SO_BINDTODEVICE
return setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, val, val_size);
#else
SET_SOCKERRNO(ENOSYS);
return -1;
#endif
case ARES_SOCKET_OPT_TCP_FASTOPEN:
if (val_size != sizeof(ares_bool_t)) {
SET_SOCKERRNO(EINVAL);
return -1;
}
#if defined(TFO_CLIENT_SOCKOPT)
{
int oval;
const ares_bool_t *pval = val;
oval = (int)*pval;
return setsockopt(sock, IPPROTO_TCP, TFO_CLIENT_SOCKOPT, (void *)&oval,
sizeof(oval));
}
#elif TFO_SUPPORTED
return 0;
#else
SET_SOCKERRNO(ENOSYS);
return -1;
#endif
}
(void)user_data;
SET_SOCKERRNO(ENOSYS);
return -1;
}
static int default_aconnect(ares_socket_t sock, const struct sockaddr *address,
ares_socklen_t address_len, unsigned int flags,
void *user_data)
{
(void)user_data;
#if defined(TFO_SKIP_CONNECT) && TFO_SKIP_CONNECT
if (flags & ARES_SOCKET_CONN_TCP_FASTOPEN) {
return 0;
}
return connect(sock, address, address_len);
#elif defined(TFO_USE_CONNECTX) && TFO_USE_CONNECTX
if (flags & ARES_SOCKET_CONN_TCP_FASTOPEN) {
sa_endpoints_t endpoints;
memset(&endpoints, 0, sizeof(endpoints));
endpoints.sae_dstaddr = address;
endpoints.sae_dstaddrlen = address_len;
return connectx(sock, &endpoints, SAE_ASSOCID_ANY,
CONNECT_DATA_IDEMPOTENT | CONNECT_RESUME_ON_READ_WRITE,
NULL, 0, NULL, NULL);
} else {
return connect(sock, address, address_len);
}
#else
(void)flags;
return connect(sock, address, address_len);
#endif
}
static ares_ssize_t default_arecvfrom(ares_socket_t sock, void *buffer,
size_t length, int flags,
struct sockaddr *address,
ares_socklen_t *address_len,
void *user_data)
{
(void)user_data;
#ifdef HAVE_RECVFROM
return (ares_ssize_t)recvfrom(sock, buffer, (RECVFROM_TYPE_ARG3)length, flags,
address, address_len);
#else
if (address != NULL && address_len != NULL) {
memset(address, 0, (size_t)*address_len);
address->sa_family = AF_UNSPEC;
}
return (ares_ssize_t)recv(sock, buffer, (RECVFROM_TYPE_ARG3)length, flags);
#endif
}
static ares_ssize_t default_asendto(ares_socket_t sock, const void *buffer,
size_t length, int flags,
const struct sockaddr *address,
ares_socklen_t address_len, void *user_data)
{
(void)user_data;
if (address != NULL) {
#ifdef HAVE_SENDTO
return (ares_ssize_t)sendto((SEND_TYPE_ARG1)sock, (SEND_TYPE_ARG2)buffer,
(SEND_TYPE_ARG3)length, (SEND_TYPE_ARG4)flags,
address, address_len);
#else
(void)address_len;
#endif
}
return (ares_ssize_t)send((SEND_TYPE_ARG1)sock, (SEND_TYPE_ARG2)buffer,
(SEND_TYPE_ARG3)length, (SEND_TYPE_ARG4)flags);
}
static int default_agetsockname(ares_socket_t sock, struct sockaddr *address,
ares_socklen_t *address_len, void *user_data)
{
(void)user_data;
return getsockname(sock, address, address_len);
}
static int default_abind(ares_socket_t sock, unsigned int flags,
const struct sockaddr *address, socklen_t address_len,
void *user_data)
{
(void)user_data;
#ifdef IP_BIND_ADDRESS_NO_PORT
if (flags & ARES_SOCKET_BIND_TCP && flags & ARES_SOCKET_BIND_CLIENT) {
int opt = 1;
(void)setsockopt(sock, SOL_IP, IP_BIND_ADDRESS_NO_PORT, &opt, sizeof(opt));
}
#else
(void)flags;
#endif
return bind(sock, address, address_len);
}
static unsigned int default_aif_nametoindex(const char *ifname, void *user_data)
{
(void)user_data;
return ares_os_if_nametoindex(ifname);
}
static const char *default_aif_indextoname(unsigned int ifindex,
char *ifname_buf,
size_t ifname_buf_len,
void *user_data)
{
(void)user_data;
return ares_os_if_indextoname(ifindex, ifname_buf, ifname_buf_len);
}
static const struct ares_socket_functions_ex default_socket_functions = {
1,
ARES_SOCKFUNC_FLAG_NONBLOCKING,
default_asocket,
default_aclose,
default_asetsockopt,
default_aconnect,
default_arecvfrom,
default_asendto,
default_agetsockname,
default_abind,
default_aif_nametoindex,
default_aif_indextoname
};
void ares_set_socket_functions_def(ares_channel_t *channel)
{
ares_set_socket_functions_ex(channel, &default_socket_functions, NULL);
}
static int legacycb_aclose(ares_socket_t sock, void *user_data)
{
ares_channel_t *channel = user_data;
if (channel->legacy_sock_funcs != NULL &&
channel->legacy_sock_funcs->aclose != NULL) {
return channel->legacy_sock_funcs->aclose(
sock, channel->legacy_sock_funcs_cb_data);
}
return default_aclose(sock, NULL);
}
static ares_socket_t legacycb_asocket(int domain, int type, int protocol,
void *user_data)
{
ares_channel_t *channel = user_data;
if (channel->legacy_sock_funcs != NULL &&
channel->legacy_sock_funcs->asocket != NULL) {
return channel->legacy_sock_funcs->asocket(
domain, type, protocol, channel->legacy_sock_funcs_cb_data);
}
return default_asocket(domain, type, protocol, NULL);
}
static int legacycb_asetsockopt(ares_socket_t sock, ares_socket_opt_t opt,
const void *val, ares_socklen_t val_size,
void *user_data)
{
(void)sock;
(void)opt;
(void)val;
(void)val_size;
(void)user_data;
SET_SOCKERRNO(ENOSYS);
return -1;
}
static int legacycb_aconnect(ares_socket_t sock, const struct sockaddr *address,
ares_socklen_t address_len, unsigned int flags,
void *user_data)
{
ares_channel_t *channel = user_data;
if (channel->legacy_sock_funcs != NULL &&
channel->legacy_sock_funcs->aconnect != NULL) {
return channel->legacy_sock_funcs->aconnect(
sock, address, address_len, channel->legacy_sock_funcs_cb_data);
}
return default_aconnect(sock, address, address_len, flags, NULL);
}
static ares_ssize_t legacycb_arecvfrom(ares_socket_t sock, void *buffer,
size_t length, int flags,
struct sockaddr *address,
ares_socklen_t *address_len,
void *user_data)
{
ares_channel_t *channel = user_data;
if (channel->legacy_sock_funcs != NULL &&
channel->legacy_sock_funcs->arecvfrom != NULL) {
if (address != NULL && address_len != NULL) {
memset(address, 0, (size_t)*address_len);
address->sa_family = AF_UNSPEC;
}
return channel->legacy_sock_funcs->arecvfrom(
sock, buffer, length, flags, address, address_len,
channel->legacy_sock_funcs_cb_data);
}
return default_arecvfrom(sock, buffer, length, flags, address, address_len,
NULL);
}
static ares_ssize_t legacycb_asendto(ares_socket_t sock, const void *buffer,
size_t length, int flags,
const struct sockaddr *address,
ares_socklen_t address_len,
void *user_data)
{
ares_channel_t *channel = user_data;
if (channel->legacy_sock_funcs != NULL &&
channel->legacy_sock_funcs->asendv != NULL) {
struct iovec vec;
vec.iov_base = (void *)((size_t)buffer); /* Cast off const */
vec.iov_len = length;
return channel->legacy_sock_funcs->asendv(
sock, &vec, 1, channel->legacy_sock_funcs_cb_data);
}
return default_asendto(sock, buffer, length, flags, address, address_len,
NULL);
}
static const struct ares_socket_functions_ex legacy_socket_functions = {
1,
0,
legacycb_asocket,
legacycb_aclose,
legacycb_asetsockopt,
legacycb_aconnect,
legacycb_arecvfrom,
legacycb_asendto,
NULL, /* agetsockname */
NULL, /* abind */
NULL, /* aif_nametoindex */
NULL /* aif_indextoname */
};
void ares_set_socket_functions(ares_channel_t *channel,
const struct ares_socket_functions *funcs,
void *data)
{
if (channel == NULL || channel->optmask & ARES_OPT_EVENT_THREAD) {
return;
}
channel->legacy_sock_funcs = funcs;
channel->legacy_sock_funcs_cb_data = data;
ares_set_socket_functions_ex(channel, &legacy_socket_functions, channel);
}
|