-
Notifications
You must be signed in to change notification settings - Fork 71
/
net.c
909 lines (752 loc) 路 21.8 KB
/
net.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
/*
* Copyright(c) 2012 Tim Ruehsen
* Copyright(c) 2015-2016 Free Software Foundation, Inc.
*
* This file is part of libwget.
*
* Libwget is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Libwget is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libwget. If not, see <https://www.gnu.org/licenses/>.
*
*
* network routines
*
* Changelog
* 25.04.2012 Tim Ruehsen created
* 16.11.2012 new functions tcp_set_family() and tcp_set_preferred_family()
*
* RFC 7413: TCP Fast Open
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <stdarg.h>
#include <c-ctype.h>
#include <time.h>
#include <errno.h>
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#elif defined HAVE_WS2TCPIP_H
# include <ws2tcpip.h>
#endif
#include <netdb.h>
#include <netinet/in.h>
#ifdef HAVE_NETINET_TCP_H
# include <netinet/tcp.h>
#endif
#if ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
# include <sys/ioctl.h>
#else
# include <fcntl.h>
#endif
#include <wget.h>
#include "private.h"
#include "net.h"
// resolver / DNS cache entry
struct ADDR_ENTRY {
const char *
host;
const char *
port;
struct addrinfo *
addrinfo;
};
static struct wget_tcp_st _global_tcp = {
.sockfd = -1,
.dns_timeout = -1,
.connect_timeout = -1,
.timeout = -1,
.family = AF_UNSPEC,
.caching = 1,
#if defined(TCP_FASTOPEN) && defined(MSG_FASTOPEN)
.tcp_fastopen = 1,
.first_send = 1
#endif
};
// resolver / DNS cache container
static wget_vector_t
*dns_cache;
static wget_thread_mutex_t
dns_mutex = WGET_THREAD_MUTEX_INITIALIZER;
static struct addrinfo *_wget_dns_cache_get(const char *host, const char *port)
{
if (dns_cache) {
struct ADDR_ENTRY *entryp, entry = { .host = host, .port = port };
int index;
wget_thread_mutex_lock(&dns_mutex);
entryp = wget_vector_get(dns_cache, (index = wget_vector_find(dns_cache, &entry)));
wget_thread_mutex_unlock(&dns_mutex);
if (entryp) {
// DNS cache entry found
debug_printf("Found dns cache entry #%d\n", index);
return entryp->addrinfo;
}
}
return NULL;
}
static int G_GNUC_WGET_PURE _compare_addr(struct ADDR_ENTRY *a1, struct ADDR_ENTRY *a2)
{
int n;
if ((n = wget_strcasecmp(a1->host, a2->host)) == 0)
return wget_strcasecmp_ascii(a1->port, a2->port);
return n;
}
static void _free_dns(struct ADDR_ENTRY *entry)
{
freeaddrinfo(entry->addrinfo);
}
static struct addrinfo * _wget_dns_cache_add(const char *host, const char *port, struct addrinfo *addrinfo)
{
// insert addrinfo into dns cache
size_t hostlen = host ? strlen(host) + 1 : 0;
size_t portlen = port ? strlen(port) + 1 : 0;
struct ADDR_ENTRY *entryp = xmalloc(sizeof(struct ADDR_ENTRY) + hostlen + portlen);
int index;
if (host) {
entryp->host = ((char *)entryp) + sizeof(struct ADDR_ENTRY);
memcpy((char *)entryp->host, host, hostlen); // ugly cast, but semantically ok
} else
entryp->host = NULL;
if (port) {
entryp->port = ((char *)entryp) + sizeof(struct ADDR_ENTRY) + hostlen;
memcpy((char *)entryp->port, port, portlen); // ugly cast, but semantically ok
} else
entryp->port = NULL;
entryp->addrinfo = addrinfo;
wget_thread_mutex_lock(&dns_mutex);
if (!dns_cache) {
dns_cache = wget_vector_create(4, -2, (wget_vector_compare_t)_compare_addr);
wget_vector_set_destructor(dns_cache, (wget_vector_destructor_t)_free_dns);
}
if ((index = wget_vector_find(dns_cache, entryp)) == -1) {
debug_printf("Add dns cache entry %s:%s\n", host, port);
wget_vector_insert_sorted_noalloc(dns_cache, entryp);
} else {
// race condition:
xfree(entryp);
freeaddrinfo(addrinfo);
addrinfo = wget_vector_get(dns_cache, index);
}
wget_thread_mutex_unlock(&dns_mutex);
return addrinfo;
}
void wget_dns_cache_free(void)
{
wget_thread_mutex_lock(&dns_mutex);
wget_vector_free(&dns_cache);
wget_thread_mutex_unlock(&dns_mutex);
}
struct addrinfo *wget_tcp_resolve(wget_tcp_t *tcp, const char *host, const char *port)
{
static wget_thread_mutex_t
mutex = WGET_THREAD_MUTEX_INITIALIZER;
struct addrinfo *addrinfo, hints;
int rc = 0, ai_flags = 0;
if (!tcp)
tcp = &_global_tcp;
// if (!port)
// port = "0";
// get the IP address for the server
for (int tries = 0, max = 3; tries < max; tries++) {
// if port is NULL,
if (tcp->caching) {
if ((addrinfo = _wget_dns_cache_get(host, port)))
return addrinfo;
// prevent multiple address resolutions of the same host/port
wget_thread_mutex_lock(&mutex);
// now try again
if ((addrinfo = _wget_dns_cache_get(host, port))) {
wget_thread_mutex_unlock(&mutex);
return addrinfo;
}
}
addrinfo = NULL;
ai_flags |= (port && c_isdigit(*port) ? AI_NUMERICSERV : 0);
ai_flags |= AI_ADDRCONFIG;
if (tcp->passive) {
ai_flags |= AI_PASSIVE;
}
memset(&hints, 0 ,sizeof(hints));
hints.ai_family = tcp->family;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = ai_flags;
if (port)
debug_printf("resolving %s:%s...\n", host, port);
else
debug_printf("resolving %s...\n", host);
if ((rc = getaddrinfo(host, port, &hints, &addrinfo)) == 0 || rc != EAI_AGAIN)
break;
if (tries < max - 1) {
if (tcp->caching)
wget_thread_mutex_unlock(&mutex);
wget_millisleep(100);
}
}
if (rc) {
error_printf(_("Failed to resolve %s:%s (%s)\n"), host, port, gai_strerror(rc));
if (tcp->caching)
wget_thread_mutex_unlock(&mutex);
return NULL;
}
if (tcp->family == AF_UNSPEC && tcp->preferred_family != AF_UNSPEC) {
struct addrinfo *preferred = NULL, *preferred_tail = NULL;
struct addrinfo *unpreferred = NULL, *unpreferred_tail = NULL;
// split address list into preferred and not preferred, keeping the original order
for (struct addrinfo *ai = addrinfo; ai;) {
if (ai->ai_family == tcp->preferred_family) {
if (preferred_tail)
preferred_tail->ai_next = ai;
else
preferred = ai; // remember the head of the list
preferred_tail = ai;
ai = ai->ai_next;
preferred_tail->ai_next = NULL;
} else {
if (unpreferred_tail)
unpreferred_tail->ai_next = ai;
else
unpreferred = ai; // remember the head of the list
unpreferred_tail = ai;
ai = ai->ai_next;
unpreferred_tail->ai_next = NULL;
}
}
// merge preferred + not preferred
if (preferred) {
preferred_tail->ai_next = unpreferred;
addrinfo = preferred;
} else {
addrinfo = unpreferred;
}
}
if (wget_logger_is_active(wget_get_logger(WGET_LOGGER_DEBUG))) {
for (struct addrinfo *ai = addrinfo; ai; ai = ai->ai_next) {
char adr[NI_MAXHOST], sport[NI_MAXSERV];
if ((rc = getnameinfo(ai->ai_addr, ai->ai_addrlen, adr, sizeof(adr), sport, sizeof(sport), NI_NUMERICHOST | NI_NUMERICSERV)) == 0)
debug_printf("has %s:%s\n", adr, sport);
else
debug_printf("has ???:%s (%s)\n", sport, gai_strerror(rc));
}
}
if (tcp->caching) {
// In case of a race condition the already existing addrinfo is returned.
// The addrinfo argument given to _wget_dns_cache_add() will be freed in this case.
addrinfo = _wget_dns_cache_add(host, port, addrinfo);
wget_thread_mutex_unlock(&mutex);
}
return addrinfo;
}
static int G_GNUC_WGET_CONST _value_to_family(int value)
{
switch (value) {
case WGET_NET_FAMILY_IPV4:
return AF_INET;
case WGET_NET_FAMILY_IPV6:
return AF_INET6;
default:
return AF_UNSPEC;
}
}
static int G_GNUC_WGET_CONST _family_to_value(int family)
{
switch (family) {
case AF_INET:
return WGET_NET_FAMILY_IPV4;
case AF_INET6:
return WGET_NET_FAMILY_IPV6;
default:
return WGET_NET_FAMILY_ANY;
}
}
void wget_tcp_set_tcp_fastopen(wget_tcp_t *tcp, int tcp_fastopen)
{
#if defined(TCP_FASTOPEN) && defined(MSG_FASTOPEN)
(tcp ? tcp : &_global_tcp)->tcp_fastopen = tcp_fastopen;
#endif
}
int wget_tcp_get_tcp_fastopen(wget_tcp_t *tcp)
{
return (tcp ? tcp : &_global_tcp)->tcp_fastopen;
}
void wget_tcp_set_tls_false_start(wget_tcp_t *tcp, int false_start)
{
(tcp ? tcp : &_global_tcp)->tls_false_start = false_start;
}
int wget_tcp_get_tls_false_start(wget_tcp_t *tcp)
{
return (tcp ? tcp : &_global_tcp)->tls_false_start;
}
void wget_tcp_set_dns_caching(wget_tcp_t *tcp, int caching)
{
(tcp ? tcp : &_global_tcp)->caching = caching;
}
int wget_tcp_get_dns_caching(wget_tcp_t *tcp)
{
return (tcp ? tcp : &_global_tcp)->caching;
}
void wget_tcp_set_protocol(wget_tcp_t *tcp, int protocol)
{
(tcp ? tcp : &_global_tcp)->protocol = protocol;
}
int wget_tcp_get_protocol(wget_tcp_t *tcp)
{
return (tcp ? tcp : &_global_tcp)->protocol;
}
void wget_tcp_set_preferred_family(wget_tcp_t *tcp, int family)
{
(tcp ? tcp : &_global_tcp)->preferred_family = _value_to_family(family);
}
int wget_tcp_get_preferred_family(wget_tcp_t *tcp)
{
return _family_to_value((tcp ? tcp : &_global_tcp)->preferred_family);
}
void wget_tcp_set_family(wget_tcp_t *tcp, int family)
{
(tcp ? tcp : &_global_tcp)->family = _value_to_family(family);
}
int wget_tcp_get_family(wget_tcp_t *tcp)
{
return _family_to_value((tcp ? tcp : &_global_tcp)->family);
}
int wget_tcp_get_local_port(wget_tcp_t *tcp)
{
if (tcp) {
struct sockaddr_storage addr_store;
struct sockaddr *addr = (struct sockaddr *)&addr_store;
socklen_t addr_len = sizeof(addr_store);
char s_port[NI_MAXSERV];
// get automatic retrieved port number
if (getsockname(tcp->sockfd, addr, &addr_len)==0) {
if (getnameinfo(addr, addr_len, NULL, 0, s_port, sizeof(s_port), NI_NUMERICSERV)==0)
return atoi(s_port);
}
}
return 0;
}
void wget_tcp_set_dns_timeout(wget_tcp_t *tcp, int timeout)
{
(tcp ? tcp : &_global_tcp)->dns_timeout = timeout;
}
void wget_tcp_set_connect_timeout(wget_tcp_t *tcp, int timeout)
{
(tcp ? tcp : &_global_tcp)->connect_timeout = timeout;
}
void wget_tcp_set_timeout(wget_tcp_t *tcp, int timeout)
{
(tcp ? tcp : &_global_tcp)->timeout = timeout;
}
int wget_tcp_get_timeout(wget_tcp_t *tcp)
{
return (tcp ? tcp : &_global_tcp)->timeout;
}
void wget_tcp_set_bind_address(wget_tcp_t *tcp, const char *bind_address)
{
if (!tcp)
tcp = &_global_tcp;
if (tcp->bind_addrinfo_allocated) {
freeaddrinfo(tcp->bind_addrinfo);
tcp->bind_addrinfo = NULL;
}
if (bind_address) {
char copy[strlen(bind_address) + 1], *s = copy;
const char *host;
memcpy(copy, bind_address, sizeof(copy));
if (*s == '[') {
// IPv6 address within brackets
char *p = strrchr(s, ']');
if (p) {
host = s + 1;
s = p + 1;
} else {
// something is broken
host = s + 1;
while (*s) s++;
}
} else {
host = s;
while (*s && *s != ':')
s++;
}
if (*s == ':') {
*s = 0;
tcp->bind_addrinfo = wget_tcp_resolve(tcp, host, s + 1); // bind to host + specified port
} else {
tcp->bind_addrinfo = wget_tcp_resolve(tcp, host, NULL); // bind to host on any port
}
tcp->bind_addrinfo_allocated = !tcp->caching;
}
}
void wget_tcp_set_ssl(wget_tcp_t *tcp, int ssl)
{
(tcp ? tcp : &_global_tcp)->ssl = ssl;
}
int wget_tcp_get_ssl(wget_tcp_t *tcp)
{
return (tcp ? tcp : &_global_tcp)->ssl;
}
void wget_tcp_set_ssl_hostname(wget_tcp_t *tcp, const char *hostname)
{
if (!tcp)
tcp = &_global_tcp;
xfree(tcp->ssl_hostname);
tcp->ssl_hostname = wget_strdup(hostname);
}
const char *wget_tcp_get_ssl_hostname(wget_tcp_t *tcp)
{
return (tcp ? tcp : &_global_tcp)->ssl_hostname;
}
wget_tcp_t *wget_tcp_init(void)
{
wget_tcp_t *tcp = xmalloc(sizeof(wget_tcp_t));
*tcp = _global_tcp;
tcp->ssl_hostname = wget_strdup(_global_tcp.ssl_hostname);
return tcp;
}
void wget_tcp_deinit(wget_tcp_t **_tcp)
{
wget_tcp_t *tcp;
if (!_tcp) {
xfree(_global_tcp.ssl_hostname);
return;
}
if ((tcp = *_tcp)) {
wget_tcp_close(tcp);
if (tcp->bind_addrinfo_allocated) {
freeaddrinfo(tcp->bind_addrinfo);
tcp->bind_addrinfo = NULL;
}
xfree(tcp->ssl_hostname);
xfree(tcp);
if (_tcp)
*_tcp = NULL;
}
}
static void _set_async(int fd)
{
#if ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
unsigned long blocking = 0;
if (ioctl(fd, FIONBIO, &blocking))
error_printf_exit(_("Failed to set socket to non-blocking\n"));
#else
int flags;
if ((flags = fcntl(fd, F_GETFL)) < 0)
error_printf_exit(_("Failed to get socket flags\n"));
if (fcntl(fd, F_SETFL, flags | O_NDELAY) < 0)
error_printf_exit(_("Failed to set socket to non-blocking\n"));
#endif
}
int wget_tcp_ready_2_transfer(wget_tcp_t *tcp, int flags)
{
return wget_ready_2_transfer(tcp->sockfd, tcp->timeout, flags);
}
int wget_tcp_connect(wget_tcp_t *tcp, const char *host, const char *port)
{
struct addrinfo *ai;
int sockfd = -1, rc, ret = WGET_E_UNKNOWN;
char adr[NI_MAXHOST], s_port[NI_MAXSERV];
int debug = wget_logger_is_active(wget_get_logger(WGET_LOGGER_DEBUG));
if (tcp->addrinfo_allocated)
freeaddrinfo(tcp->addrinfo);
tcp->addrinfo = wget_tcp_resolve(tcp, host, port);
tcp->addrinfo_allocated = !tcp->caching;
for (ai = tcp->addrinfo; ai; ai = ai->ai_next) {
if (debug) {
if ((rc = getnameinfo(ai->ai_addr, ai->ai_addrlen, adr, sizeof(adr), s_port, sizeof(s_port), NI_NUMERICHOST | NI_NUMERICSERV)) == 0)
debug_printf("trying %s:%s...\n", adr, s_port);
else
debug_printf("trying ???:%s (%s)...\n", s_port, gai_strerror(rc));
}
if ((sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) != -1) {
int on = 1;
_set_async(sockfd);
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)) == -1)
error_printf(_("Failed to set socket option REUSEADDR\n"));
on = 1;
if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (void *)&on, sizeof(on)) == -1)
error_printf(_("Failed to set socket option NODELAY\n"));
if (tcp->bind_addrinfo) {
if (debug) {
if ((rc = getnameinfo(tcp->bind_addrinfo->ai_addr, tcp->bind_addrinfo->ai_addrlen, adr, sizeof(adr), s_port, sizeof(s_port), NI_NUMERICHOST | NI_NUMERICSERV)) == 0)
debug_printf("binding to %s:%s...\n", adr, s_port);
else
debug_printf("binding to ???:%s (%s)...\n", s_port, gai_strerror(rc));
}
if (bind(sockfd, tcp->bind_addrinfo->ai_addr, tcp->bind_addrinfo->ai_addrlen) != 0) {
error_printf(_("Failed to bind (%d)\n"), errno);
close(sockfd);
return -1;
}
}
if (tcp->tcp_fastopen) {
rc = 0;
errno = 0;
tcp->connect_addrinfo = ai;
tcp->first_send = 1;
} else {
rc = connect(sockfd, ai->ai_addr, ai->ai_addrlen);
tcp->first_send = 0;
}
if (rc < 0
&& errno != EAGAIN
&& errno != EINPROGRESS
) {
error_printf(_("Failed to connect (%d)\n"), errno);
ret = WGET_E_CONNECT;
close(sockfd);
} else {
tcp->sockfd = sockfd;
if (tcp->ssl) {
// tcp->ssl_session = wget_ssl_open(tcp->sockfd, tcp->ssl_hostname, tcp->connect_timeout);
// if (!tcp->ssl_session) {
if ((ret = wget_ssl_open(tcp))) {
if (ret == WGET_E_CERTIFICATE) {
wget_tcp_close(tcp);
break; /* stop here - the server cert couldn't be validated */
}
// do not free tcp->addrinfo when calling wget_tcp_close()
struct addrinfo *ai_tmp = tcp->addrinfo;
tcp->addrinfo = NULL;
wget_tcp_close(tcp);
tcp->addrinfo = ai_tmp;
continue;
}
}
return WGET_E_SUCCESS;
}
} else
error_printf(_("Failed to create socket (%d)\n"), errno);
}
return ret;
}
int wget_tcp_listen(wget_tcp_t *tcp, const char *host, const char *port, int backlog)
{
struct addrinfo *ai;
int sockfd = -1, rc;
char adr[NI_MAXHOST], s_port[NI_MAXSERV];
int debug = wget_logger_is_active(wget_get_logger(WGET_LOGGER_DEBUG));
if (tcp->bind_addrinfo_allocated)
freeaddrinfo(tcp->bind_addrinfo);
tcp->passive = 1;
tcp->bind_addrinfo = wget_tcp_resolve(tcp, host, port);
tcp->bind_addrinfo_allocated = !tcp->caching;
for (ai = tcp->bind_addrinfo; ai; ai = ai->ai_next) {
if (debug) {
if ((rc = getnameinfo(ai->ai_addr, ai->ai_addrlen, adr, sizeof(adr), s_port, sizeof(s_port), NI_NUMERICHOST | NI_NUMERICSERV)) == 0)
debug_printf("try to listen on %s:%s...\n", adr, s_port);
else
debug_printf("failed to listen on %s:%s (%s)...\n", host, s_port, gai_strerror(rc));
}
if ((sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) != -1) {
int on = 1;
_set_async(sockfd);
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)) == -1)
error_printf(_("Failed to set socket option REUSEADDR\n"));
on = 1;
if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (void *)&on, sizeof(on)) == -1)
error_printf(_("Failed to set socket option NODELAY\n"));
#ifdef TCP_FASTOPEN
if (tcp->tcp_fastopen) {
on = 1;
if (setsockopt(sockfd, IPPROTO_TCP, TCP_FASTOPEN, &on, sizeof(on)) == -1)
error_printf(_("Failed to set socket option FASTOPEN\n"));
tcp->first_send = 0;
}
#endif
if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) != 0) {
error_printf(_("Failed to bind (%d)\n"), errno);
close(sockfd);
return -1;
}
if (listen(sockfd, backlog) == 0) {
tcp->sockfd = sockfd;
if (debug) {
if (!port)
snprintf(s_port, sizeof(s_port), "%d", wget_tcp_get_local_port(tcp));
if ((rc = getnameinfo(ai->ai_addr, ai->ai_addrlen, adr, sizeof(adr), NULL, 0, NI_NUMERICHOST)) == 0)
debug_printf("%ssecure listen on %s:%s...\n", tcp->ssl ? "" : "in", adr, port ? port : s_port);
else
debug_printf("%ssecure listen on %s:%s (%s)...\n", tcp->ssl ? "" : "in", host, port ? port : s_port, gai_strerror(rc));
}
return 0;
} else {
error_printf(_("Failed to listen (%d)\n"), errno);
close(sockfd);
}
} else
error_printf(_("Failed to create socket (%d)\n"), errno);
}
return -1;
}
wget_tcp_t *wget_tcp_accept(wget_tcp_t *parent_tcp)
{
int sockfd;
// 0: no timeout / immediate
// -1: INFINITE timeout
if (parent_tcp->timeout) {
if (wget_ready_2_read(parent_tcp->sockfd, parent_tcp->timeout) <= 0)
return NULL;
}
if ((sockfd = accept(parent_tcp->sockfd, parent_tcp->bind_addrinfo->ai_addr, &parent_tcp->bind_addrinfo->ai_addrlen)) != -1) {
wget_tcp_t *tcp = xmalloc(sizeof(wget_tcp_t));
*tcp = *parent_tcp;
tcp->sockfd = sockfd;
tcp->ssl_hostname = NULL;
tcp->addrinfo = NULL;
tcp->bind_addrinfo = NULL;
if (tcp->ssl) {
if (wget_tcp_tls_start(tcp))
wget_tcp_deinit(&tcp);
}
return tcp;
}
error_printf(_("Failed to accept (%d)\n"), errno);
return NULL;
}
int wget_tcp_tls_start(wget_tcp_t *tcp)
{
if (tcp->passive)
return wget_ssl_server_open(tcp);
else
return wget_ssl_open(tcp);
}
void wget_tcp_tls_stop(wget_tcp_t *tcp)
{
if (tcp->passive)
wget_ssl_server_close(&tcp->ssl_session);
else
wget_ssl_close(&tcp->ssl_session);
}
ssize_t wget_tcp_read(wget_tcp_t *tcp, char *buf, size_t count)
{
ssize_t rc;
if (tcp->ssl_session) {
rc = wget_ssl_read_timeout(tcp->ssl_session, buf, count, tcp->timeout);
} else {
// 0: no timeout / immediate
// -1: INFINITE timeout
if (tcp->timeout) {
if ((rc = wget_ready_2_read(tcp->sockfd, tcp->timeout)) <= 0)
return rc;
}
// rc = read(tcp->sockfd, buf, count);
rc = recvfrom(tcp->sockfd, buf, count, 0, NULL, NULL);
}
if (rc < 0)
error_printf(_("Failed to read %zu bytes (%d)\n"), count, errno);
return rc;
}
ssize_t wget_tcp_write(wget_tcp_t *tcp, const char *buf, size_t count)
{
ssize_t nwritten = 0, n;
int rc;
if (tcp->ssl_session)
return wget_ssl_write_timeout(tcp->ssl_session, buf, count, tcp->timeout);
while (count) {
#ifdef MSG_FASTOPEN
if (tcp->tcp_fastopen && tcp->first_send) {
n = sendto(tcp->sockfd, buf, count, MSG_FASTOPEN,
tcp->connect_addrinfo->ai_addr, tcp->connect_addrinfo->ai_addrlen);
tcp->first_send = 0;
if (n < 0 && errno == EOPNOTSUPP) {
// fallback from fastopen, e.g. when fastopen is disabled in system
tcp->tcp_fastopen = 0;
rc = connect(tcp->sockfd, tcp->connect_addrinfo->ai_addr, tcp->connect_addrinfo->ai_addrlen);
if (rc < 0
&& errno != EAGAIN
&& errno != ENOTCONN
&& errno != EINPROGRESS)
{
error_printf(_("Failed to connect (%d)\n"), errno);
return -1;
}
errno = EAGAIN;
}
} else
#endif
n = send(tcp->sockfd, buf, count, 0);
if (n >= 0) {
if ((size_t)n >= count)
return nwritten + n;
count -= n;
buf += n;
nwritten += n;
} else {
if (errno != EAGAIN
&& errno != ENOTCONN
&& errno != EINPROGRESS
) {
error_printf(_("Failed to write %zu bytes (%d)\n"), count, errno);
return -1;
}
// 0: no timeout / immediate
// -1: INFINITE timeout
if (tcp->timeout) {
if ((rc = wget_ready_2_write(tcp->sockfd, tcp->timeout)) <= 0)
return rc;
}
}
}
return 0;
}
ssize_t wget_tcp_vprintf(wget_tcp_t *tcp, const char *fmt, va_list args)
{
char sbuf[4096];
wget_buffer_t buf;
ssize_t len2;
wget_buffer_init(&buf, sbuf, sizeof(sbuf));
wget_buffer_vprintf(&buf, fmt, args);
len2 = wget_tcp_write(tcp, buf.data, buf.length);
wget_buffer_deinit(&buf);
if (len2 > 0)
debug_write(buf.data, len2);
if (len2 > 0 && (ssize_t) buf.length != len2)
error_printf("tcp_send: internal error: length mismatch %zu != %zd\n", buf.length, len2);
return len2;
}
ssize_t wget_tcp_printf(wget_tcp_t *tcp, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
ssize_t len = wget_tcp_vprintf(tcp, fmt, args);
va_end(args);
return len;
}
void wget_tcp_close(wget_tcp_t *tcp)
{
if (tcp) {
wget_tcp_tls_stop(tcp);
if (tcp->sockfd != -1) {
close(tcp->sockfd);
tcp->sockfd = -1;
}
if (tcp->addrinfo_allocated) {
freeaddrinfo(tcp->addrinfo);
}
tcp->addrinfo = NULL;
}
}
// for Windows compatibility
#include "sockets.h"
int wget_net_init(void)
{
return gl_sockets_startup(SOCKETS_2_2);
}
int wget_net_deinit(void)
{
return gl_sockets_cleanup();
}