-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
flb_network.c
2158 lines (1744 loc) · 59.9 KB
/
flb_network.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
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Fluent Bit
* ==========
* Copyright (C) 2015-2022 The Fluent Bit Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#ifdef FLB_SYSTEM_WINDOWS
#define poll WSAPoll
#else
#include <sys/poll.h>
#endif
#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_compat.h>
#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_socket.h>
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/flb_str.h>
#include <fluent-bit/flb_sds.h>
#include <fluent-bit/flb_network.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_macros.h>
#include <fluent-bit/flb_upstream.h>
#include <fluent-bit/flb_scheduler.h>
#include <monkey/mk_core.h>
#include <ares.h>
#ifndef SOL_TCP
#define SOL_TCP IPPROTO_TCP
#endif
static pthread_once_t local_thread_net_dns_ctx_init = PTHREAD_ONCE_INIT;
FLB_TLS_DEFINE(struct flb_net_dns, flb_net_dns_ctx);
/*
* Initialize thread-local-storage, every worker thread has it owns
* dns context with relevant info populated inside the thread.
*/
static void flb_net_dns_ctx_init_private()
{
FLB_TLS_INIT(flb_net_dns_ctx);
}
void flb_net_dns_ctx_init()
{
pthread_once(&local_thread_net_dns_ctx_init, flb_net_dns_ctx_init_private);
}
struct flb_net_dns *flb_net_dns_ctx_get()
{
return FLB_TLS_GET(flb_net_dns_ctx);
}
void flb_net_dns_ctx_set(struct flb_net_dns *dns_ctx)
{
FLB_TLS_SET(flb_net_dns_ctx, dns_ctx);
}
void flb_net_lib_init()
{
int result;
result = ares_library_init_mem(ARES_LIB_INIT_ALL, flb_malloc, flb_free, flb_realloc);
if(0 != result) {
flb_error("[network] c-ares memory settings initialization error : %s",
ares_strerror(result));
}
}
void flb_net_ctx_init(struct flb_net_dns *dns_ctx)
{
mk_list_init(&dns_ctx->lookups);
mk_list_init(&dns_ctx->lookups_drop);
}
void flb_net_setup_init(struct flb_net_setup *net)
{
net->dns_mode = NULL;
net->dns_resolver = NULL;
net->dns_prefer_ipv4 = FLB_FALSE;
net->keepalive = FLB_TRUE;
net->keepalive_idle_timeout = 30;
net->keepalive_max_recycle = 0;
net->accept_timeout = 10;
net->connect_timeout = 10;
net->io_timeout = 0; /* Infinite time */
net->source_address = NULL;
}
int flb_net_host_set(const char *plugin_name, struct flb_net_host *host, const char *address)
{
int len;
int olen;
const char *s, *e, *u;
memset(host, '\0', sizeof(struct flb_net_host));
olen = strlen(address);
if (olen == strlen(plugin_name)) {
return 0;
}
len = strlen(plugin_name) + 3;
if (olen < len) {
return -1;
}
s = address + len;
if (*s == '[') {
/* IPv6 address (RFC 3986) */
e = strchr(++s, ']');
if (!e) {
return -1;
}
host->name = flb_sds_create_len(s, e - s);
host->ipv6 = FLB_TRUE;
s = e + 1;
}
else {
e = s;
while (!(*e == '\0' || *e == ':' || *e == '/')) {
++e;
}
if (e == s) {
return -1;
}
host->name = flb_sds_create_len(s, e - s);
s = e;
}
if (*s == ':') {
host->port = atoi(++s);
}
u = strchr(s, '/');
if (u) {
host->uri = flb_uri_create(u);
}
host->address = flb_sds_create(address);
if (host->name) {
host->listen = flb_sds_create(host->name);
}
return 0;
}
int flb_net_socket_reset(flb_sockfd_t fd)
{
int status = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &status, sizeof(int)) == -1) {
flb_errno();
return -1;
}
return 0;
}
int flb_net_socket_tcp_nodelay(flb_sockfd_t fd)
{
int on = 1;
int ret;
ret = setsockopt(fd, SOL_TCP, TCP_NODELAY, &on, sizeof(on));
if (ret == -1) {
flb_errno();
return -1;
}
return 0;
}
int flb_net_socket_nonblocking(flb_sockfd_t fd)
{
#ifdef _WIN32
unsigned long on = 1;
if (ioctlsocket(fd, FIONBIO, &on) != 0) {
#else
if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK) == -1) {
#endif
flb_errno();
return -1;
}
return 0;
}
int flb_net_socket_blocking(flb_sockfd_t fd)
{
#ifdef _WIN32
unsigned long off = 0;
if (ioctlsocket(fd, FIONBIO, &off) != 0) {
#else
if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK) == -1) {
#endif
flb_errno();
return -1;
}
return 0;
}
int flb_net_socket_set_rcvtimeout(flb_sockfd_t fd, int timeout_in_seconds)
{
#ifdef FLB_SYSTEM_WINDOWS
/* WINDOWS */
DWORD timeout = timeout_in_seconds * 1000;
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof timeout)
== -1) {
#else
/* LINUX and MAC OS X */
struct timeval tv;
tv.tv_sec = timeout_in_seconds;
tv.tv_usec = 0;
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv) == -1) {
#endif
flb_errno();
return -1;
}
return 0;
}
/*
* Enable the TCP_FASTOPEN feature for server side implemented in Linux Kernel >= 3.7,
* for more details read here:
*
* TCP Fast Open: expediting web services: http://lwn.net/Articles/508865/
*/
int flb_net_socket_tcp_fastopen(flb_sockfd_t fd)
{
int qlen = 5;
return setsockopt(fd, SOL_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen));
}
flb_sockfd_t flb_net_socket_create(int family, int nonblock)
{
flb_sockfd_t fd;
/* create the socket and set the nonblocking flag status */
fd = socket(family, SOCK_STREAM, 0);
if (fd == -1) {
flb_errno();
return -1;
}
if (nonblock) {
flb_net_socket_nonblocking(fd);
}
return fd;
}
flb_sockfd_t flb_net_socket_create_udp(int family, int nonblock)
{
flb_sockfd_t fd;
/* create the socket and set the nonblocking flag status */
fd = socket(family, SOCK_DGRAM, 0);
if (fd == -1) {
flb_errno();
return -1;
}
if (nonblock) {
flb_net_socket_nonblocking(fd);
}
return fd;
}
/*
* Perform TCP connection for a blocking socket. This interface set's the socket
* to non-blocking mode temporary in order to add a timeout to the connection,
* the blocking mode is restored at the end.
*/
static int net_connect_sync(int fd, const struct sockaddr *addr, socklen_t addrlen,
char *host, int port, int connect_timeout)
{
int ret;
int err;
int socket_errno;
struct pollfd pfd_read;
/* Set socket to non-blocking mode */
flb_net_socket_nonblocking(fd);
/* connect(2) */
ret = connect(fd, addr, addrlen);
if (ret == -1) {
/*
* An asynchronous connect can return -1, but what is important is the
* socket status, getting a EINPROGRESS is expected, but any other case
* means a failure.
*/
#ifdef FLB_SYSTEM_WINDOWS
socket_errno = flb_socket_error(fd);
err = 0;
#else
socket_errno = errno;
err = flb_socket_error(fd);
#endif
if (!FLB_EINPROGRESS(socket_errno) || err != 0) {
goto exit_error;
}
/* The connection is still in progress, implement a socket timeout */
flb_trace("[net] connection #%i in process to %s:%i",
fd, host, port);
/*
* Prepare a timeout using poll(2): we could use our own
* event loop mechanism for this, but it will require an
* extra file descriptor, the poll(2) call is straightforward
* for this use case.
*/
pfd_read.fd = fd;
pfd_read.events = POLLOUT;
ret = poll(&pfd_read, 1, connect_timeout * 1000);
if (ret == 0) {
/* Timeout */
flb_error("[net] connection #%i timeout after %i seconds to: "
"%s:%i",
fd, connect_timeout, host, port);
goto exit_error;
}
else if (ret < 0) {
/* Generic error */
flb_errno();
flb_error("[net] connection #%i failed to: %s:%i",
fd, host, port);
goto exit_error;
}
}
/*
* No exception, the connection succeeded, return the normal
* non-blocking mode to the socket.
*/
flb_net_socket_blocking(fd);
return 0;
exit_error:
flb_net_socket_blocking(fd);
return -1;
}
/*
* Asynchronous socket connection: this interface might be called from a co-routine,
* so in order to perform a real async connection and get notified back, it needs
* access to the event loop context and the connection context 'upstream connection.
*/
static int net_connect_async(int fd,
const struct sockaddr *addr, socklen_t addrlen,
char *host, int port, int connect_timeout,
void *async_ctx, struct flb_connection *u_conn)
{
int ret;
int err;
int error = 0;
int socket_errno;
uint32_t mask;
char so_error_buf[256];
char *str;
struct flb_upstream *u;
u = u_conn->upstream;
/* connect(2) */
ret = connect(fd, addr, addrlen);
if (ret == 0) {
return 0;
}
/*
* An asynchronous connect can return -1, but what is important is the
* socket status, getting a EINPROGRESS is expected, but any other case
* means a failure.
*/
#ifdef FLB_SYSTEM_WINDOWS
socket_errno = flb_socket_error(fd);
err = 0;
#else
socket_errno = errno;
err = flb_socket_error(fd);
#endif
/* The logic behind this check is that when establishing a connection
* errno should be EINPROGRESS with no additional information in order
* for it to be a healthy attempt. However, when errno is EINPROGRESS
* and an error occurs it could be saved in the so_error socket field
* which has to be accessed through getsockopt(... SO_ERROR ...) so
* in order to preserve that behavior while also properly detecting
* other errno values as error conditions the comparison was changed.
*
* Windows note : flb_socket_error returns either the value returned
* by WSAGetLastError or the value returned by getsockopt(... SO_ERROR ...)
* if WSAGetLastError returns WSAEWOULDBLOCK as per libevents code.
*
* General note : according to the connect syscall man page (not libc)
* there could be a timing issue with checking SO_ERROR here because
* the suggested use involves checking it after a select or poll call
* returns the socket as writable which is not the case here.
*/
if (!FLB_EINPROGRESS(socket_errno) || err != 0) {
return -1;
}
/* The connection is still in progress, implement a socket timeout */
flb_trace("[net] connection #%i in process to %s:%i",
fd, host, port);
/* Register the connection socket into the main event loop */
MK_EVENT_ZERO(&u_conn->event);
ret = mk_event_add(u_conn->evl,
fd,
FLB_ENGINE_EV_THREAD,
MK_EVENT_WRITE,
&u_conn->event);
u_conn->event.priority = FLB_ENGINE_PRIORITY_CONNECT;
if (ret == -1) {
/*
* If we failed here there no much that we can do, just
* let the caller know that we failed.
*/
return -1;
}
u_conn->coroutine = async_ctx;
/*
* Return the control to the parent caller, we need to wait for
* the event loop to get back to us.
*/
flb_coro_yield(async_ctx, FLB_FALSE);
/* We want this field to hold NULL at all times unless we are explicitly
* waiting to be resumed.
*/
u_conn->coroutine = NULL;
/* Save the mask before the event handler do a reset */
mask = u_conn->event.mask;
/*
* If the socket has been invalidated (e.g: timeout or shutdown), just
* print a debug message and return.
*/
if (u_conn->fd == -1) {
flb_debug("[net] TCP connection not longer available: %s:%i",
u->tcp_host, u->tcp_port);
return -1;
}
/* We got a notification, remove the event registered */
ret = mk_event_del(u_conn->evl, &u_conn->event);
if (ret == -1) {
flb_error("[io] connect event handler error");
return -1;
}
if (u_conn->net_error == ETIMEDOUT) {
flb_debug("[net] TCP connection timed out: %s:%i",
u->tcp_host, u->tcp_port);
return -1;
}
/* Check the connection status */
if (mask & MK_EVENT_WRITE) {
error = flb_socket_error(u_conn->fd);
/* Check the exception */
if (error != 0) {
/*
* The upstream connection might want to override the
* exception (mostly used for local timeouts: ETIMEDOUT.
*/
if (u_conn->net_error > 0) {
error = u_conn->net_error;
}
/* Connection is broken, not much to do here */
str = strerror_r(error, so_error_buf, sizeof(so_error_buf));
flb_error("[net] TCP connection failed: %s:%i (%s)",
u->tcp_host, u->tcp_port, str);
return -1;
}
}
else {
flb_error("[net] TCP connection, unexpected error: %s:%i",
u->tcp_host, u->tcp_port);
return -1;
}
return 0;
}
static void flb_net_dns_lookup_context_destroy(struct flb_dns_lookup_context *lookup_context)
{
mk_list_del(&lookup_context->_head);
ares_destroy(lookup_context->ares_channel);
flb_free(lookup_context);
}
static void flb_net_dns_lookup_context_drop(struct flb_dns_lookup_context *lookup_context)
{
if (!lookup_context->dropped) {
lookup_context->dropped = FLB_TRUE;
mk_list_del(&lookup_context->_head);
mk_list_add(&lookup_context->_head, &lookup_context->dns_ctx->lookups_drop);
if (lookup_context->udp_timer != NULL &&
lookup_context->udp_timer->active) {
flb_sched_timer_invalidate(lookup_context->udp_timer);
lookup_context->udp_timer = NULL;
}
}
}
void flb_net_dns_lookup_context_cleanup(struct flb_net_dns *dns_ctx)
{
struct flb_dns_lookup_context *lookup_context;
struct flb_coro *coroutine;
struct mk_list *head;
struct mk_list *tmp;
mk_list_foreach_safe(head, tmp, &dns_ctx->lookups_drop) {
lookup_context = mk_list_entry(head, struct flb_dns_lookup_context, _head);
coroutine = lookup_context->coroutine;
flb_net_dns_lookup_context_destroy(lookup_context);
if (coroutine != NULL) {
flb_coro_resume(coroutine);
}
}
}
static void flb_net_free_translated_addrinfo(struct addrinfo *input)
{
struct addrinfo *current_record;
struct addrinfo *next_record;
if (input != NULL) {
next_record = NULL;
for (current_record = input ;
current_record != NULL ;
current_record = next_record) {
if (current_record->ai_addr != NULL) {
flb_free(current_record->ai_addr);
}
next_record = current_record->ai_next;
flb_free(current_record);
}
}
}
static void flb_net_append_addrinfo_entry(struct addrinfo **head,
struct addrinfo **tail,
struct addrinfo *entry)
{
if (*head == NULL) {
*head = entry;
}
else {
(*tail)->ai_next = entry;
}
*tail = entry;
}
static struct addrinfo *flb_net_sort_addrinfo_list(struct addrinfo *input,
int preferred_family)
{
struct addrinfo *preferred_results_head;
struct addrinfo *remainder_results_head;
struct addrinfo *preferred_results_tail;
struct addrinfo *remainder_results_tail;
struct addrinfo *current_record;
struct addrinfo *next_record;
remainder_results_head = NULL;
preferred_results_head = NULL;
remainder_results_tail = NULL;
preferred_results_tail = NULL;
current_record = NULL;
next_record = NULL;
for (current_record = input ;
current_record != NULL ;
current_record = next_record) {
next_record = current_record->ai_next;
current_record->ai_next = NULL;
if (preferred_family == current_record->ai_family) {
flb_net_append_addrinfo_entry(&preferred_results_head,
&preferred_results_tail,
current_record);
}
else
{
flb_net_append_addrinfo_entry(&remainder_results_head,
&remainder_results_tail,
current_record);
}
}
if (preferred_results_tail != NULL) {
preferred_results_tail->ai_next = remainder_results_head;
}
if (preferred_results_head == NULL) {
return remainder_results_head;
}
return preferred_results_head;
}
static struct addrinfo *flb_net_translate_ares_addrinfo(struct ares_addrinfo *input)
{
struct addrinfo *previous_output_record;
struct addrinfo *current_output_record;
struct ares_addrinfo_node *current_ares_record;
int failure_detected;
struct addrinfo *output;
output = NULL;
failure_detected = 0;
current_output_record = NULL;
previous_output_record = NULL;
if (input != NULL) {
for (current_ares_record = input->nodes ;
current_ares_record != NULL ;
current_ares_record = current_ares_record->ai_next) {
current_output_record = flb_calloc(1, sizeof(struct addrinfo));
if (current_output_record == NULL) {
flb_errno();
failure_detected = 1;
break;
}
if (output == NULL) {
output = current_output_record;
}
current_output_record->ai_flags = current_ares_record->ai_flags;
current_output_record->ai_family = current_ares_record->ai_family;
current_output_record->ai_socktype = current_ares_record->ai_socktype;
current_output_record->ai_protocol = current_ares_record->ai_protocol;
current_output_record->ai_addrlen = current_ares_record->ai_addrlen;
current_output_record->ai_addr = flb_malloc(current_output_record->ai_addrlen);
if (current_output_record->ai_addr == NULL) {
flb_errno();
failure_detected = 1;
break;
}
memcpy(current_output_record->ai_addr,
current_ares_record->ai_addr,
current_output_record->ai_addrlen);
if (previous_output_record != NULL) {
previous_output_record->ai_next = current_output_record;
}
previous_output_record = current_output_record;
}
}
if (failure_detected) {
if (output != NULL) {
flb_net_free_translated_addrinfo(output);
output = NULL;
}
}
return output;
}
static void flb_net_getaddrinfo_callback(void *arg, int status, int timeouts,
struct ares_addrinfo *res)
{
struct flb_dns_lookup_context *lookup_context;
lookup_context = (struct flb_dns_lookup_context *) arg;
if (lookup_context->finished ||
lookup_context->dropped) {
return;
}
if (ARES_SUCCESS == status) {
*(lookup_context->result) = flb_net_translate_ares_addrinfo(res);
if (*(lookup_context->result) == NULL) {
/* Translation fails only when calloc fails. */
*(lookup_context->result_code) = ARES_ENOMEM;
}
else {
*(lookup_context->result_code) = ARES_SUCCESS;
}
ares_freeaddrinfo(res);
}
else {
*(lookup_context->result_code) = status;
}
lookup_context->finished = 1;
}
static int flb_net_getaddrinfo_event_handler(void *arg)
{
struct flb_dns_lookup_context *lookup_context;
lookup_context = FLB_DNS_LOOKUP_CONTEXT_FOR_EVENT(arg);
if (lookup_context->finished ||
lookup_context->dropped) {
return 0;
}
ares_process_fd(lookup_context->ares_channel,
lookup_context->response_event.fd,
lookup_context->response_event.fd);
if (lookup_context->finished) {
flb_net_dns_lookup_context_drop(lookup_context);
}
return 0;
}
static void flb_net_getaddrinfo_timeout_handler(struct flb_config *config, void *data)
{
struct flb_dns_lookup_context *lookup_context;
(void) config;
lookup_context = (struct flb_dns_lookup_context *) data;
if (lookup_context->finished ||
lookup_context->dropped) {
return;
}
*(lookup_context->udp_timeout_detected) = FLB_TRUE;
lookup_context->finished = FLB_TRUE;
lookup_context->udp_timer = NULL;
/* We deliverately set udp_timer because we don't want flb_net_dns_lookup_context_drop
* to call flb_sched_timer_invalidate on the timer which was already disabled and
* is about to be destroyed after this this callback returns.
*/
ares_cancel(lookup_context->ares_channel);
*(lookup_context->result_code) = ARES_ETIMEOUT;
flb_net_dns_lookup_context_drop(lookup_context);
}
static ares_socket_t flb_dns_ares_socket(int af, int type, int protocol, void *userdata)
{
struct flb_dns_lookup_context *lookup_context;
int event_mask;
ares_socket_t sockfd;
int result;
lookup_context = (struct flb_dns_lookup_context *) userdata;
if (lookup_context->ares_socket_created) {
/* This context already had a connection established and the code is not ready
* to handle multiple connections so we abort the process.
*/
errno = EACCES;
return -1;
}
sockfd = socket(af, type, protocol);
if (sockfd == -1) {
return -1;
}
/* According to configure_socket in ares_process.c:970 if we provide our own socket
* functions we need to set the socket up ourselves but the only specific thing we
* need is for the socket to be set to non blocking mode so that's all we do here.
*/
result = flb_net_socket_nonblocking(sockfd);
if (result) {
flb_socket_close(sockfd);
return -1;
}
lookup_context->ares_socket_type = type;
lookup_context->ares_socket_created = FLB_TRUE;
lookup_context->response_event.mask = MK_EVENT_EMPTY;
lookup_context->response_event.status = MK_EVENT_NONE;
lookup_context->response_event.data = &lookup_context->response_event;
lookup_context->response_event.handler = flb_net_getaddrinfo_event_handler;
lookup_context->response_event.fd = sockfd;
event_mask = MK_EVENT_READ;
if (SOCK_STREAM == type) {
event_mask |= MK_EVENT_WRITE;
}
result = mk_event_add(lookup_context->event_loop, sockfd, FLB_ENGINE_EV_CUSTOM,
event_mask, &lookup_context->response_event);
lookup_context->response_event.priority = FLB_ENGINE_PRIORITY_DNS;
if (result) {
flb_socket_close(sockfd);
return -1;
}
lookup_context->response_event.type = FLB_ENGINE_EV_CUSTOM;
lookup_context->ares_socket_registered = FLB_TRUE;
return sockfd;
}
static int flb_dns_ares_close(ares_socket_t sockfd, void *userdata)
{
struct flb_dns_lookup_context *lookup_context;
int result;
lookup_context = (struct flb_dns_lookup_context *) userdata;
if (lookup_context->ares_socket_registered) {
lookup_context->ares_socket_registered = FLB_FALSE;
mk_event_del(lookup_context->event_loop, &lookup_context->response_event);
}
result = flb_socket_close(sockfd);
return result;
}
static int flb_dns_ares_connect(ares_socket_t sockfd, const struct sockaddr *addr,
ares_socklen_t addrlen, void *userdata)
{
return connect(sockfd, addr, addrlen);
}
static ares_ssize_t flb_dns_ares_recvfrom(ares_socket_t sockfd, void *data,
size_t data_len, int flags,
struct sockaddr *from, ares_socklen_t *from_len,
void *userdata)
{
return recvfrom(sockfd, data, data_len, flags, from, from_len);
}
static ares_ssize_t flb_dns_ares_send(ares_socket_t sockfd, const struct iovec *vec,
int len, void *userdata)
{
return writev(sockfd, vec, len);
}
static struct flb_dns_lookup_context *flb_net_dns_lookup_context_create(
struct flb_net_dns *dns_ctx,
struct mk_event_loop *evl,
struct flb_coro *coroutine,
char dns_mode,
int *result)
{
struct flb_dns_lookup_context *lookup_context;
int local_result;
int optmask;
struct ares_options opts = {0};
local_result = 0;
optmask = 0;
if (result == NULL) {
result = &local_result;
}
/* The initialization order here is important since it makes it easier to handle
* failures
*/
lookup_context = flb_calloc(1, sizeof(struct flb_dns_lookup_context));
if (!lookup_context) {
flb_errno();
*result = ARES_ENOMEM;
return NULL;
}
/* c-ares options: Set the transport layer to the desired protocol and
* the number of retries to 2
*/
optmask = ARES_OPT_FLAGS;
opts.tries = 2;
if (dns_mode == FLB_DNS_USE_TCP) {
opts.flags = ARES_FLAG_USEVC;
}
*result = ares_init_options((ares_channel *) &lookup_context->ares_channel,
&opts, optmask);
if (*result != ARES_SUCCESS) {
flb_free(lookup_context);
return NULL;
}
lookup_context->ares_socket_functions.asocket = flb_dns_ares_socket;
lookup_context->ares_socket_functions.aclose = flb_dns_ares_close;
lookup_context->ares_socket_functions.aconnect = flb_dns_ares_connect;
lookup_context->ares_socket_functions.arecvfrom = flb_dns_ares_recvfrom;
lookup_context->ares_socket_functions.asendv = flb_dns_ares_send;
lookup_context->ares_socket_created = 0;
lookup_context->event_loop = evl;
lookup_context->udp_timer = NULL;
lookup_context->coroutine = coroutine;
lookup_context->finished = 0;
lookup_context->dropped = 0;
lookup_context->dns_ctx = dns_ctx;
ares_set_socket_functions(lookup_context->ares_channel,
&lookup_context->ares_socket_functions,
lookup_context);
*result = ARES_SUCCESS;
mk_list_add(&lookup_context->_head, &dns_ctx->lookups);
return lookup_context;
}
int flb_net_getaddrinfo(const char *node, const char *service, struct addrinfo *hints,
struct addrinfo **res, char *dns_mode_textual, int timeout)
{
int udp_timeout_detected;
struct flb_dns_lookup_context *lookup_context;
int errno_backup;
int result_code;
struct addrinfo *result_data;
struct ares_addrinfo_hints ares_hints;