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
|
/* MIT License
*
* Copyright (c) Massachusetts Institute of Technology
* Copyright (c) The c-ares project and its contributors
*
* 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"
void ares_conn_sock_state_cb_update(ares_conn_t *conn,
ares_conn_state_flags_t flags)
{
ares_channel_t *channel = conn->server->channel;
if ((conn->state_flags & ARES_CONN_STATE_CBFLAGS) != flags &&
channel->sock_state_cb) {
channel->sock_state_cb(channel->sock_state_cb_data, conn->fd,
flags & ARES_CONN_STATE_READ ? 1 : 0,
flags & ARES_CONN_STATE_WRITE ? 1 : 0);
}
conn->state_flags &= ~((unsigned int)ARES_CONN_STATE_CBFLAGS);
conn->state_flags |= flags;
}
ares_conn_err_t ares_conn_read(ares_conn_t *conn, void *data, size_t len,
size_t *read_bytes)
{
ares_channel_t *channel = conn->server->channel;
ares_conn_err_t err;
if (!(conn->flags & ARES_CONN_FLAG_TCP)) {
struct sockaddr_storage sa_storage;
ares_socklen_t salen = sizeof(sa_storage);
memset(&sa_storage, 0, sizeof(sa_storage));
err =
ares_socket_recvfrom(channel, conn->fd, ARES_FALSE, data, len, 0,
(struct sockaddr *)&sa_storage, &salen, read_bytes);
#ifdef HAVE_RECVFROM
if (err == ARES_CONN_ERR_SUCCESS &&
!ares_sockaddr_addr_eq((struct sockaddr *)&sa_storage,
&conn->server->addr)) {
err = ARES_CONN_ERR_WOULDBLOCK;
}
#endif
} else {
err = ares_socket_recv(channel, conn->fd, ARES_TRUE, data, len, read_bytes);
}
/* Toggle connected state if needed */
if (err == ARES_CONN_ERR_SUCCESS) {
conn->state_flags |= ARES_CONN_STATE_CONNECTED;
}
return err;
}
/* Use like:
* struct sockaddr_storage sa_storage;
* ares_socklen_t salen = sizeof(sa_storage);
* struct sockaddr *sa = (struct sockaddr *)&sa_storage;
* ares_conn_set_sockaddr(conn, sa, &salen);
*/
static ares_status_t ares_conn_set_sockaddr(const ares_conn_t *conn,
struct sockaddr *sa,
ares_socklen_t *salen)
{
const ares_server_t *server = conn->server;
unsigned short port =
conn->flags & ARES_CONN_FLAG_TCP ? server->tcp_port : server->udp_port;
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
switch (server->addr.family) {
case AF_INET:
sin = (struct sockaddr_in *)(void *)sa;
if (*salen < (ares_socklen_t)sizeof(*sin)) {
return ARES_EFORMERR;
}
*salen = sizeof(*sin);
memset(sin, 0, sizeof(*sin));
sin->sin_family = AF_INET;
sin->sin_port = htons(port);
memcpy(&sin->sin_addr, &server->addr.addr.addr4, sizeof(sin->sin_addr));
return ARES_SUCCESS;
case AF_INET6:
sin6 = (struct sockaddr_in6 *)(void *)sa;
if (*salen < (ares_socklen_t)sizeof(*sin6)) {
return ARES_EFORMERR;
}
*salen = sizeof(*sin6);
memset(sin6, 0, sizeof(*sin6));
sin6->sin6_family = AF_INET6;
sin6->sin6_port = htons(port);
memcpy(&sin6->sin6_addr, &server->addr.addr.addr6,
sizeof(sin6->sin6_addr));
#ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
sin6->sin6_scope_id = server->ll_scope;
#endif
return ARES_SUCCESS;
default:
break;
}
return ARES_EBADFAMILY;
}
static ares_status_t ares_conn_set_self_ip(ares_conn_t *conn, ares_bool_t early)
{
ares_channel_t *channel = conn->server->channel;
struct sockaddr_storage sa_storage;
int rv;
ares_socklen_t len = sizeof(sa_storage);
/* We call this twice on TFO, if we already have the IP we can go ahead and
* skip processing */
if (!early && conn->self_ip.family != AF_UNSPEC) {
return ARES_SUCCESS;
}
memset(&sa_storage, 0, sizeof(sa_storage));
if (channel->sock_funcs.agetsockname == NULL) {
/* Not specified, we can still use cookies cooked with an empty self_ip */
memset(&conn->self_ip, 0, sizeof(conn->self_ip));
return ARES_SUCCESS;
}
rv = channel->sock_funcs.agetsockname(conn->fd,
(struct sockaddr *)(void *)&sa_storage,
&len, channel->sock_func_cb_data);
if (rv != 0) {
/* During TCP FastOpen, we can't get the IP this early since connect()
* may not be called. That's ok, we'll try again later */
if (early && conn->flags & ARES_CONN_FLAG_TCP &&
conn->flags & ARES_CONN_FLAG_TFO) {
memset(&conn->self_ip, 0, sizeof(conn->self_ip));
return ARES_SUCCESS;
}
return ARES_ECONNREFUSED;
}
if (!ares_sockaddr_to_ares_addr(&conn->self_ip, NULL,
(struct sockaddr *)(void *)&sa_storage)) {
return ARES_ECONNREFUSED;
}
return ARES_SUCCESS;
}
ares_conn_err_t ares_conn_write(ares_conn_t *conn, const void *data, size_t len,
size_t *written)
{
ares_channel_t *channel = conn->server->channel;
ares_bool_t is_tfo = ARES_FALSE;
ares_conn_err_t err = ARES_CONN_ERR_SUCCESS;
struct sockaddr_storage sa_storage;
ares_socklen_t salen = 0;
struct sockaddr *sa = NULL;
*written = 0;
/* Don't try to write if not doing initial TFO and not connected */
if (conn->flags & ARES_CONN_FLAG_TCP &&
!(conn->state_flags & ARES_CONN_STATE_CONNECTED) &&
!(conn->flags & ARES_CONN_FLAG_TFO_INITIAL)) {
return ARES_CONN_ERR_WOULDBLOCK;
}
/* On initial write during TFO we need to send an address */
if (conn->flags & ARES_CONN_FLAG_TFO_INITIAL) {
salen = sizeof(sa_storage);
sa = (struct sockaddr *)&sa_storage;
conn->flags &= ~((unsigned int)ARES_CONN_FLAG_TFO_INITIAL);
is_tfo = ARES_TRUE;
if (ares_conn_set_sockaddr(conn, sa, &salen) != ARES_SUCCESS) {
return ARES_CONN_ERR_FAILURE;
}
}
err = ares_socket_write(channel, conn->fd, data, len, written, sa, salen);
if (err != ARES_CONN_ERR_SUCCESS) {
goto done;
}
if (is_tfo) {
/* If using TFO, we might not have been able to get an IP earlier, since
* we hadn't informed the OS of the destination. When using sendto()
* now we have so we should be able to fetch it */
ares_conn_set_self_ip(conn, ARES_FALSE);
goto done;
}
done:
if (err == ARES_CONN_ERR_SUCCESS && len == *written) {
/* Wrote all data, make sure we're not listening for write events unless
* using TFO, in which case we'll need a write event to know when
* we're connected. */
ares_conn_sock_state_cb_update(
conn, ARES_CONN_STATE_READ |
(is_tfo ? ARES_CONN_STATE_WRITE : ARES_CONN_STATE_NONE));
} else if (err == ARES_CONN_ERR_WOULDBLOCK) {
/* Need to wait on more buffer space to write */
ares_conn_sock_state_cb_update(conn, ARES_CONN_STATE_READ |
ARES_CONN_STATE_WRITE);
}
return err;
}
ares_status_t ares_conn_flush(ares_conn_t *conn)
{
const unsigned char *data;
size_t data_len;
size_t count;
ares_conn_err_t err;
ares_status_t status;
ares_bool_t tfo = ARES_FALSE;
if (conn == NULL) {
return ARES_EFORMERR;
}
if (conn->flags & ARES_CONN_FLAG_TFO_INITIAL) {
tfo = ARES_TRUE;
}
do {
if (ares_buf_len(conn->out_buf) == 0) {
status = ARES_SUCCESS;
goto done;
}
if (conn->flags & ARES_CONN_FLAG_TCP) {
data = ares_buf_peek(conn->out_buf, &data_len);
} else {
unsigned short msg_len;
/* Read length, then provide buffer without length */
ares_buf_tag(conn->out_buf);
status = ares_buf_fetch_be16(conn->out_buf, &msg_len);
if (status != ARES_SUCCESS) {
return status;
}
ares_buf_tag_rollback(conn->out_buf);
data = ares_buf_peek(conn->out_buf, &data_len);
if (data_len < (size_t)(msg_len + 2)) {
status = ARES_EFORMERR;
goto done;
}
data += 2;
data_len = msg_len;
}
err = ares_conn_write(conn, data, data_len, &count);
if (err != ARES_CONN_ERR_SUCCESS) {
if (err != ARES_CONN_ERR_WOULDBLOCK) {
status = ARES_ECONNREFUSED;
goto done;
}
status = ARES_SUCCESS;
goto done;
}
/* UDP didn't send the length prefix so augment that here */
if (!(conn->flags & ARES_CONN_FLAG_TCP)) {
count += 2;
}
/* Strip data written from the buffer */
ares_buf_consume(conn->out_buf, count);
status = ARES_SUCCESS;
/* Loop only for UDP since we have to send per-packet. We already
* sent everything we could if using tcp */
} while (!(conn->flags & ARES_CONN_FLAG_TCP));
done:
if (status == ARES_SUCCESS) {
ares_conn_state_flags_t flags = ARES_CONN_STATE_READ;
/* When using TFO, the we need to enabling waiting on a write event to
* be notified of when a connection is actually established */
if (tfo) {
flags |= ARES_CONN_STATE_WRITE;
}
/* If using TCP and not all data was written (partial write), that means
* we need to also wait on a write event */
if (conn->flags & ARES_CONN_FLAG_TCP && ares_buf_len(conn->out_buf)) {
flags |= ARES_CONN_STATE_WRITE;
}
ares_conn_sock_state_cb_update(conn, flags);
}
return status;
}
static ares_status_t ares_conn_connect(ares_conn_t *conn,
const struct sockaddr *sa,
ares_socklen_t salen)
{
ares_conn_err_t err;
err = ares_socket_connect(
conn->server->channel, conn->fd,
(conn->flags & ARES_CONN_FLAG_TFO) ? ARES_TRUE : ARES_FALSE, sa, salen);
if (err != ARES_CONN_ERR_WOULDBLOCK && err != ARES_CONN_ERR_SUCCESS) {
return ARES_ECONNREFUSED;
}
return ARES_SUCCESS;
}
ares_status_t ares_open_connection(ares_conn_t **conn_out,
ares_channel_t *channel,
ares_server_t *server, ares_bool_t is_tcp)
{
ares_status_t status;
struct sockaddr_storage sa_storage;
ares_socklen_t salen = sizeof(sa_storage);
struct sockaddr *sa = (struct sockaddr *)&sa_storage;
ares_conn_t *conn;
ares_llist_node_t *node = NULL;
int stype = is_tcp ? SOCK_STREAM : SOCK_DGRAM;
ares_conn_state_flags_t state_flags;
*conn_out = NULL;
conn = ares_malloc(sizeof(*conn));
if (conn == NULL) {
return ARES_ENOMEM; /* LCOV_EXCL_LINE: OutOfMemory */
}
memset(conn, 0, sizeof(*conn));
conn->fd = ARES_SOCKET_BAD;
conn->server = server;
conn->queries_to_conn = ares_llist_create(NULL);
conn->flags = is_tcp ? ARES_CONN_FLAG_TCP : ARES_CONN_FLAG_NONE;
conn->out_buf = ares_buf_create();
conn->in_buf = ares_buf_create();
if (conn->queries_to_conn == NULL || conn->out_buf == NULL ||
conn->in_buf == NULL) {
/* LCOV_EXCL_START: OutOfMemory */
status = ARES_ENOMEM;
goto done;
/* LCOV_EXCL_STOP */
}
/* Try to enable TFO always if using TCP. it will fail later on if its
* really not supported when we try to enable it on the socket. */
if (conn->flags & ARES_CONN_FLAG_TCP) {
conn->flags |= ARES_CONN_FLAG_TFO;
}
/* Convert into the struct sockaddr structure needed by the OS */
status = ares_conn_set_sockaddr(conn, sa, &salen);
if (status != ARES_SUCCESS) {
goto done;
}
/* Acquire a socket. */
if (ares_socket_open(&conn->fd, channel, server->addr.family, stype, 0) !=
ARES_CONN_ERR_SUCCESS) {
status = ARES_ECONNREFUSED;
goto done;
}
/* Configure channel configured options */
status = ares_socket_configure(
channel, server->addr.family,
(conn->flags & ARES_CONN_FLAG_TCP) ? ARES_TRUE : ARES_FALSE, conn->fd);
if (status != ARES_SUCCESS) {
goto done;
}
/* Enable TFO if possible */
if (conn->flags & ARES_CONN_FLAG_TFO &&
ares_socket_enable_tfo(channel, conn->fd) != ARES_CONN_ERR_SUCCESS) {
conn->flags &= ~((unsigned int)ARES_CONN_FLAG_TFO);
}
if (channel->sock_config_cb) {
int err =
channel->sock_config_cb(conn->fd, stype, channel->sock_config_cb_data);
if (err < 0) {
status = ARES_ECONNREFUSED;
goto done;
}
}
/* Connect */
status = ares_conn_connect(conn, sa, salen);
if (status != ARES_SUCCESS) {
goto done;
}
if (channel->sock_create_cb) {
int err =
channel->sock_create_cb(conn->fd, stype, channel->sock_create_cb_data);
if (err < 0) {
status = ARES_ECONNREFUSED;
goto done;
}
}
/* Let the connection know we haven't written our first packet yet for TFO */
if (conn->flags & ARES_CONN_FLAG_TFO) {
conn->flags |= ARES_CONN_FLAG_TFO_INITIAL;
}
/* Need to store our own ip for DNS cookie support */
status = ares_conn_set_self_ip(conn, ARES_TRUE);
if (status != ARES_SUCCESS) {
goto done; /* LCOV_EXCL_LINE: UntestablePath */
}
/* TCP connections are thrown to the end as we don't spawn multiple TCP
* connections. UDP connections are put on front where the newest connection
* can be quickly pulled */
if (is_tcp) {
node = ares_llist_insert_last(server->connections, conn);
} else {
node = ares_llist_insert_first(server->connections, conn);
}
if (node == NULL) {
/* LCOV_EXCL_START: OutOfMemory */
status = ARES_ENOMEM;
goto done;
/* LCOV_EXCL_STOP */
}
/* Register globally to quickly map event on file descriptor to connection
* node object */
if (!ares_htable_asvp_insert(channel->connnode_by_socket, conn->fd, node)) {
/* LCOV_EXCL_START: OutOfMemory */
status = ARES_ENOMEM;
goto done;
/* LCOV_EXCL_STOP */
}
state_flags = ARES_CONN_STATE_READ;
/* Get notified on connect if using TCP */
if (conn->flags & ARES_CONN_FLAG_TCP) {
state_flags |= ARES_CONN_STATE_WRITE;
}
/* Dot no attempt to update sock state callbacks on TFO until *after* the
* initial write is performed. Due to the notification event, its possible
* an erroneous read can come in before the attempt to write the data which
* might be used to set the ip address */
if (!(conn->flags & ARES_CONN_FLAG_TFO_INITIAL)) {
ares_conn_sock_state_cb_update(conn, state_flags);
}
if (is_tcp) {
server->tcp_conn = conn;
}
done:
if (status != ARES_SUCCESS) {
ares_llist_node_claim(node);
ares_llist_destroy(conn->queries_to_conn);
ares_socket_close(channel, conn->fd);
ares_buf_destroy(conn->out_buf);
ares_buf_destroy(conn->in_buf);
ares_free(conn);
} else {
*conn_out = conn;
}
return status;
}
ares_conn_t *ares_conn_from_fd(const ares_channel_t *channel, ares_socket_t fd)
{
ares_llist_node_t *node;
node = ares_htable_asvp_get_direct(channel->connnode_by_socket, fd);
if (node == NULL) {
return NULL;
}
return ares_llist_node_val(node);
}
|