forked from marlam/msmtp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.c
1188 lines (1071 loc) · 28.2 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
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
/*
* net.c
*
* This file is part of msmtp, an SMTP client, and of mpop, a POP3 client.
*
* Copyright (C) 2000, 2003, 2004, 2005, 2006, 2007, 2008, 2012, 2014, 2015,
* 2018, 2019, 2020
* Martin Lambers <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef W32_NATIVE
# define WIN32_LEAN_AND_MEAN /* do not include more than necessary */
# define _WIN32_WINNT 0x0601 /* Windows 7 or later */
# include <winsock2.h>
# include <ws2tcpip.h>
#endif
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#ifdef HAVE_SYS_SELECT_H
# include <sys/select.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
# include <sys/un.h>
#endif
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif
#ifdef HAVE_NETDB_H
# include <netdb.h>
#endif
#ifdef HAVE_LIBIDN
# include <idn2.h>
#endif
#ifdef HAVE_LIBRESOLV
# include <arpa/nameser.h>
# include <resolv.h>
#endif
#include "gettext.h"
#define _(string) gettext(string)
#include "xalloc.h"
#include "readbuf.h"
#include "tools.h"
#include "net.h"
/*
* [Windows only] wsa_strerror()
*
* This function translates WSA error codes to strings.
* It should translate all codes that could be caused by the Windows socket
* functions used in this file:
* WSAStartup, getaddrinfo() or gethostbyname(), socket(), connect(),
* recv(), send()
*/
#ifdef W32_NATIVE
const char *wsa_strerror(int error_code)
{
switch (error_code)
{
case WSA_NOT_ENOUGH_MEMORY:
return _("not enough memory");
case WSAEINTR:
return _("operation aborted");
case WSAEINVAL:
return _("invalid argument");
case WSATYPE_NOT_FOUND:
return _("class type not found");
case WSAENETDOWN:
return _("the network subsystem has failed");
case WSAHOST_NOT_FOUND:
return _("host not found (authoritative)");
case WSATRY_AGAIN:
return _("host not found (nonauthoritative) or server failure");
case WSANO_RECOVERY:
return _("nonrecoverable error");
case WSANO_DATA:
return _("valid name, but no data record of requested type");
case WSAEAFNOSUPPORT:
return _("address family not supported");
case WSAEMFILE:
return _("no socket descriptors available");
case WSAENOBUFS:
return _("no buffer space available");
case WSAEPROTONOSUPPORT:
return _("protocol not supported");
case WSAEPROTOTYPE:
return _("wrong protocol type for this socket");
case WSAESOCKTNOSUPPORT:
return _("socket type is not supported in this address family");
case WSAEADDRNOTAVAIL:
return _("remote address is not valid");
case WSAECONNREFUSED:
return _("connection refused");
case WSAENETUNREACH:
return _("network unreachable");
case WSAETIMEDOUT:
return _("timeout");
case WSAENOTCONN:
return _("socket not connected");
case WSAESHUTDOWN:
return _("the socket was shut down");
case WSAEHOSTUNREACH:
return _("host unreachable");
case WSAECONNRESET:
return _("connection reset by peer");
case WSASYSNOTREADY:
return _("the underlying network subsystem is not ready");
case WSAVERNOTSUPPORTED:
return _("the requested version is not available");
case WSAEINPROGRESS:
return _("a blocking operation is in progress");
case WSAEPROCLIM:
return _("limit on the number of tasks has been reached");
case WSAEFAULT:
return _("invalid request");
default:
return _("unknown error");
}
}
#endif /* W32_NATIVE */
/*
* Wrapper function for recv() that sets an error string on failure.
*/
int net_recv(int fd, void *buf, size_t len, char **errstr)
{
int r = recv(fd, buf, len, 0);
if (r < 0)
{
#ifdef W32_NATIVE
int e = WSAGetLastError();
if (e == WSAETIMEDOUT)
{
*errstr = xasprintf(_("network read error: %s"),
_("the operation timed out"));
}
else
{
*errstr = xasprintf(_("network read error: %s"),
wsa_strerror(e));
}
#else /* !W32_NATIVE */
if (errno == EINTR)
{
*errstr = xasprintf(_("operation aborted"));
}
else if (errno == EAGAIN)
{
*errstr = xasprintf(_("network read error: %s"),
_("the operation timed out"));
}
else
{
*errstr = xasprintf(_("network read error: %s"),
strerror(errno));
}
#endif
return -1;
}
return r;
}
/*
* Wrapper function for send() that sets an error string on failure.
*/
int net_send(int fd, const void *buf, size_t len, char **errstr)
{
#ifdef W32_NATIVE
int ret;
#else /* !W32_NATIVE */
ssize_t ret;
#endif
if ((ret = send(fd, buf, len, 0)) < 0)
{
#ifdef W32_NATIVE
int e = WSAGetLastError();
if (e == WSAETIMEDOUT)
{
*errstr = xasprintf(_("network write error: %s"),
_("the operation timed out"));
}
else
{
*errstr = xasprintf(_("network write error: %s"),
wsa_strerror(e));
}
#else /* !W32_NATIVE */
if (errno == EINTR)
{
*errstr = xasprintf(_("operation aborted"));
}
else if (errno == EAGAIN)
{
*errstr = xasprintf(_("network write error: %s"),
_("the operation timed out"));
}
else
{
*errstr = xasprintf(_("network write error: %s"),
strerror(errno));
}
#endif
}
return ret;
}
/*
* net_lib_init()
*
* see net.h
*/
int net_lib_init(char **errstr)
{
#ifdef W32_NATIVE
WORD wVersionRequested;
WSADATA wsaData;
int error_code;
wVersionRequested = MAKEWORD(2, 0);
if ((error_code = WSAStartup(wVersionRequested, &wsaData)) != 0)
{
*errstr = xasprintf("%s", wsa_strerror(error_code));
return NET_ELIBFAILED;
}
else
{
return NET_EOK;
}
#else /* noone else needs this... */
(void)errstr;
return NET_EOK;
#endif
}
/*
* net_close_socket()
*
* This function is needed because Windows cannot just close() a socket.
*
* see net.h
*/
void net_close_socket(int fd)
{
#ifdef W32_NATIVE
(void)closesocket(fd);
#else
(void)close(fd);
#endif
}
/*
* net_bind_source_ip_to_socket()
*
* This function binds a source IP (in string representation, either IPv6 or IPv4)
* to a socket. It behaves like bind() in terms of return value and errno.
*/
int net_bind_source_ip_to_socket(int fd, const char *source_ip)
{
struct sockaddr_in6 sa6;
struct sockaddr_in sa4;
memset(&sa6, 0, sizeof(sa6));
if (inet_pton(AF_INET6, source_ip, &sa6.sin6_addr) != 0)
{
sa6.sin6_family = AF_INET6;
return bind(fd, (struct sockaddr *)&sa6, sizeof(sa6));
}
else
{
memset(&sa4, 0, sizeof(sa4));
if (inet_pton(AF_INET, source_ip, &sa4.sin_addr) != 0)
{
sa4.sin_family = AF_INET;
return bind(fd, (struct sockaddr *)&sa4, sizeof(sa4));
}
else
{
#ifdef W32_NATIVE
WSASetLastError(WSAEINVAL);
#else
errno = EINVAL;
#endif
return -1;
}
}
}
/*
* net_connect()
*
* connect() with timeout.
*
* This function is equivalent to connect(), except that a connection attempt
* times out after 'timeout' seconds instead of the OS dependant default value.
* A 'timeout' <= 0 will be ignored.
*/
int net_connect(int fd, const struct sockaddr *serv_addr, socklen_t addrlen,
int timeout)
{
#ifdef W32_NATIVE
u_long flags;
DWORD err;
int optlen;
fd_set eset;
#else
int flags;
int err;
socklen_t optlen;
#endif
struct timeval tv;
fd_set wset;
if (timeout <= 0)
{
return connect(fd, serv_addr, addrlen);
}
else
{
/* make socket non-blocking */
#ifdef W32_NATIVE
flags = 1;
if (ioctlsocket(fd, FIONBIO, &flags) == SOCKET_ERROR)
#else
flags = fcntl(fd, F_GETFL, 0);
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
#endif
{
return -1;
}
/* start connect */
if (connect(fd, serv_addr, addrlen) < 0)
{
#ifdef W32_NATIVE
if (WSAGetLastError() != WSAEWOULDBLOCK)
#else
if (errno != EINPROGRESS)
#endif
{
return -1;
}
tv.tv_sec = timeout;
tv.tv_usec = 0;
FD_ZERO(&wset);
FD_SET(fd, &wset);
#ifdef W32_NATIVE
FD_ZERO(&eset);
FD_SET(fd, &eset);
#endif
/* wait for connect() to finish */
#ifdef W32_NATIVE
/* In case of an error on connect(), eset will be affected instead
* of wset (on Windows only). */
if ((err = select(fd + 1, NULL, &wset, &eset, &tv)) <= 0)
#else
if ((err = select(fd + 1, NULL, &wset, NULL, &tv)) <= 0)
#endif
{
/* errno is already set if err < 0 */
if (err == 0)
{
#ifdef W32_NATIVE
WSASetLastError(WSAETIMEDOUT);
#else
errno = ETIMEDOUT;
#endif
}
return -1;
}
/* test for success, set errno */
optlen = sizeof(err);
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &optlen) < 0)
{
return -1;
}
if (err != 0)
{
#ifdef W32_NATIVE
WSASetLastError(err);
#else
errno = err;
#endif
return -1;
}
}
/* restore blocking mode */
#ifdef W32_NATIVE
flags = 0;
if (ioctlsocket(fd, FIONBIO, &flags) == SOCKET_ERROR)
#else
if (fcntl(fd, F_SETFL, flags) == -1)
#endif
{
return -1;
}
return 0;
}
}
/*
* net_set_io_timeout()
*
* Sets a timeout for inout/output operations on the given socket.
*/
void net_set_io_timeout(int socket, int seconds)
{
#ifdef W32_NATIVE
DWORD milliseconds;
if (seconds > 0)
{
milliseconds = seconds * 1000;
(void)setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &milliseconds, sizeof(int));
(void)setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, &milliseconds, sizeof(int));
}
#else /* UNIX */
struct timeval tv;
if (seconds > 0)
{
tv.tv_sec = seconds;
tv.tv_usec = 0;
(void)setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
(void)setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
}
#endif
}
/*
* open_socket()
*
* see net.h
*/
int net_socks5_connect(int fd, const char *hostname, int port, char **errstr)
{
/* maximum size of a SOCKS5 message (send or receive) */
unsigned char buffer[1 + 1 + 1 + 1 + 1 + 255 + 2];
size_t hostname_len = strlen(hostname);
uint16_t nport = htons(port);
size_t len;
int ret;
if (hostname_len > 0xff)
{
/* this hostname cannot be sent in a SOCKS5 message */
*errstr = xasprintf(_("proxy failure: %s"), _("host name too long"));
return NET_EPROXY;
}
/* Send greeting */
buffer[0] = 0x05; /* SOCKS5 */
buffer[1] = 0x01; /* one auth method supported: */
buffer[2] = 0x00; /* no authentication */
if ((ret = net_send(fd, buffer, 3, errstr)) < 0)
{
return NET_EIO;
}
else if (ret < 3)
{
*errstr = xasprintf(_("network write error"));
return NET_EIO;
}
/* Receive greeting */
if ((ret = net_recv(fd, buffer, 2, errstr)) < 0)
{
return NET_EIO;
}
else if (ret < 2)
{
*errstr = xasprintf(_("network read error"));
return NET_EIO;
}
if (buffer[0] != 0x05 /* SOCKS5 */
|| buffer[1] != 0x00) /* no authentication */
{
*errstr = xasprintf(_("proxy failure: %s"), _("unexpected reply"));
return NET_EPROXY;
}
/* Send CONNECT request */
buffer[0] = 0x05; /* SOCKS5 */
buffer[1] = 0x01; /* CONNECT */
buffer[2] = 0x00; /* reserved */
buffer[3] = 0x03; /* Domain name follows */
buffer[4] = hostname_len;
memcpy(buffer + 5, hostname, hostname_len);
memcpy(buffer + 5 + hostname_len, &nport, 2);
len = 5 + hostname_len + 2;
if ((ret = net_send(fd, buffer, len, errstr)) < 0)
{
return NET_EIO;
}
else if ((size_t)ret < len)
{
*errstr = xasprintf(_("network write error"));
return NET_EIO;
}
/* Receive answer */
if ((ret = net_recv(fd, buffer, 5, errstr)) < 0)
{
return NET_EIO;
}
else if (ret < 5)
{
*errstr = xasprintf(_("network read error"));
return NET_EIO;
}
if (buffer[0] != 0x05 /* SOCKS5 */
|| buffer[2] != 0x00 /* reserved */
|| (buffer[3] != 0x01 && buffer[3] != 0x03 && buffer[3] != 0x04))
{
*errstr = xasprintf(_("proxy failure: %s"), _("unexpected reply"));
return NET_EPROXY;
}
if (buffer[3] == 0x01)
{
len = 4 - 1; /* IPv4 */
}
else if (buffer[3] == 0x04)
{
len = 16 - 1; /* IPv6 */
}
else /* Domain name */
{
len = buffer[4];
}
len += 2; /* port number */
if ((ret = net_recv(fd, buffer + 5, len, errstr)) < 0)
{
return NET_EIO;
}
else if ((size_t)ret < len)
{
*errstr = xasprintf(_("network read error"));
return NET_EIO;
}
/* Interpret SOCKS5 status */
switch (buffer[1])
{
case 0x00:
return NET_EOK;
case 0x01:
*errstr = xasprintf(_("proxy failure: %s"), _("general server failure"));
return NET_EPROXY;
case 0x02:
*errstr = xasprintf(_("proxy failure: %s"), _("connection not allowed"));
return NET_EPROXY;
case 0x03:
*errstr = xasprintf(_("proxy failure: %s"), _("network unreachable"));
return NET_EPROXY;
case 0x04:
*errstr = xasprintf(_("proxy failure: %s"), _("host unreachable"));
return NET_EPROXY;
case 0x05:
*errstr = xasprintf(_("proxy failure: %s"), _("connection refused"));
return NET_EPROXY;
case 0x06:
*errstr = xasprintf(_("proxy failure: %s"), _("time-to-live expired"));
return NET_EPROXY;
case 0x07:
*errstr = xasprintf(_("proxy failure: %s"), _("command not supported"));
return NET_EPROXY;
case 0x08:
*errstr = xasprintf(_("proxy failure: %s"), _("address type not supported"));
return NET_EPROXY;
default:
*errstr = xasprintf(_("proxy failure: %s"), _("unknown error"));
return NET_EPROXY;
}
}
int net_open_socket(
const char *socketname,
const char *proxy_hostname, int proxy_port,
const char *hostname, int port,
const char *source_ip,
int timeout,
int *ret_fd, char **canonical_name, char **address,
char **errstr)
{
int fd;
char *port_string;
struct addrinfo hints;
struct addrinfo *res0;
struct addrinfo *res;
int error_code;
int failure_errno;
int cause;
char nameinfo_buffer[NI_MAXHOST];
char *idn_hostname = NULL;
if (socketname)
{
#ifdef W32_NATIVE
*errstr = xasprintf(_("cannot connect to %s: %s"), socketname,
wsa_strerror(WSAESOCKTNOSUPPORT));
return NET_ELIBFAILED;
#else
struct sockaddr_un addr;
if (strlen(socketname) + 1 > sizeof(addr.sun_path))
{
*errstr = xasprintf(_("cannot connect to %s: %s"), socketname,
_("invalid argument"));
return NET_EIO;
}
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
{
*errstr = xasprintf(_("cannot create socket: %s"), strerror(errno));
return NET_ESOCKET;
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, socketname);
if (net_connect(fd, (struct sockaddr *)&addr, sizeof(addr), timeout) < 0)
{
net_close_socket(fd);
*errstr = xasprintf(_("cannot connect to %s: %s"), socketname, strerror(errno));
return NET_ECONNECT;
}
*ret_fd = fd;
if (canonical_name)
{
*canonical_name = NULL;
}
if (address)
{
*address = NULL;
}
return NET_EOK;
#endif
}
if (proxy_hostname)
{
error_code = net_open_socket(NULL, NULL, -1, proxy_hostname, proxy_port,
source_ip, timeout, &fd, NULL, NULL, errstr);
if (error_code != NET_EOK)
{
return error_code;
}
error_code = net_socks5_connect(fd, hostname, port, errstr);
if (error_code != NET_EOK)
{
return error_code;
}
*ret_fd = fd;
if (canonical_name)
{
*canonical_name = NULL;
}
if (address)
{
*address = NULL;
}
return NET_EOK;
}
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = 0;
hints.ai_protocol = 0;
hints.ai_addrlen = 0;
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
port_string = xasprintf("%d", port);
#ifdef HAVE_GAI_IDN
# ifdef AI_IDN
hints.ai_flags |= AI_IDN;
# endif
#elif defined(HAVE_LIBIDN)
idn2_to_ascii_lz(hostname, &idn_hostname, IDN2_NFC_INPUT | IDN2_NONTRANSITIONAL);
#endif
error_code = getaddrinfo(idn_hostname ? idn_hostname : hostname,
port_string, &hints, &res0);
free(idn_hostname);
free(port_string);
if (error_code)
{
#ifdef W32_NATIVE
*errstr = xasprintf(_("cannot locate host %s: %s"),
hostname, wsa_strerror(WSAGetLastError()));
#else
if (error_code == EAI_SYSTEM && errno == EINTR)
{
*errstr = xasprintf(_("operation aborted"));
}
else
{
*errstr = xasprintf(_("cannot locate host %s: %s"), hostname,
error_code == EAI_SYSTEM ? strerror(errno)
: gai_strerror(error_code));
}
#endif
return NET_EHOSTNOTFOUND;
}
fd = -1;
cause = 0;
failure_errno = 0;
for (res = res0; res; res = res->ai_next)
{
fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (fd < 0)
{
cause = 1;
#ifdef W32_NATIVE
failure_errno = WSAGetLastError();
#else
failure_errno = errno;
#endif
continue;
}
if (source_ip && net_bind_source_ip_to_socket(fd, source_ip) != 0)
{
cause = 2;
#ifdef W32_NATIVE
failure_errno = WSAGetLastError();
#else
failure_errno = errno;
#endif
net_close_socket(fd);
fd = -1;
continue;
}
if (net_connect(fd, res->ai_addr, res->ai_addrlen, timeout) < 0)
{
cause = 3;
#ifdef W32_NATIVE
if (WSAGetLastError() != WSAENETUNREACH)
{
failure_errno = WSAGetLastError();
}
#else
if (errno != ENETUNREACH)
{
failure_errno = errno;
}
#endif
net_close_socket(fd);
fd = -1;
continue;
}
break;
}
if (fd >= 0)
{
if (canonical_name)
{
if (getnameinfo(res->ai_addr, res->ai_addrlen, nameinfo_buffer,
sizeof(nameinfo_buffer), NULL, 0, NI_NAMEREQD) == 0)
{
*canonical_name = xstrdup(nameinfo_buffer);
}
else
{
*canonical_name = NULL;
}
}
if (address)
{
if (getnameinfo(res->ai_addr, res->ai_addrlen, nameinfo_buffer,
sizeof(nameinfo_buffer), NULL, 0, NI_NUMERICHOST) == 0)
{
*address = xstrdup(nameinfo_buffer);
}
else
{
*address = NULL;
}
}
}
freeaddrinfo(res0);
if (fd < 0)
{
if (cause == 1)
{
*errstr = xasprintf(_("cannot create socket: %s"),
#ifdef W32_NATIVE
wsa_strerror(failure_errno)
#else
strerror(failure_errno)
#endif
);
return NET_ESOCKET;
}
else if (cause == 2)
{
*errstr = xasprintf(_("cannot bind source ip %s: %s"), source_ip,
#ifdef W32_NATIVE
wsa_strerror(failure_errno)
#else
strerror(failure_errno)
#endif
);
return NET_ESOCKET;
}
else /* cause == 3 */
{
#ifdef W32_NATIVE
if (failure_errno == 0)
{
failure_errno = WSAENETUNREACH;
}
*errstr = xasprintf(_("cannot connect to %s, port %d: %s"),
hostname, port, wsa_strerror(failure_errno));
#else
if (failure_errno == EINTR)
{
*errstr = xasprintf(_("operation aborted"));
}
else
{
if (failure_errno == 0)
{
failure_errno = ENETUNREACH;
}
*errstr = xasprintf(_("cannot connect to %s, port %d: %s"),
hostname, port, strerror(failure_errno));
}
#endif
return NET_ECONNECT;
}
}
net_set_io_timeout(fd, timeout);
*ret_fd = fd;
return NET_EOK;
}
/*
* net_readbuf_read()
*
* Wraps read() to provide buffering for net_gets().
*/
int net_readbuf_read(int fd, readbuf_t *readbuf, char *ptr,
char **errstr)
{
if (readbuf->count <= 0)
{
readbuf->count = net_recv(fd, readbuf->buf, sizeof(readbuf->buf), errstr);
if (readbuf->count < 0)
{
return -1;
}
else if (readbuf->count == 0)
{
return 0;
}
readbuf->ptr = readbuf->buf;
}
readbuf->count--;
*ptr = *((readbuf->ptr)++);
return 1;
}
/*
* net_gets()
*
* see net.h
*/
int net_gets(int fd, readbuf_t *readbuf,
char *str, size_t size, size_t *len, char **errstr)
{
char c;
size_t i;
int ret;
i = 0;
while (i + 1 < size)
{
if ((ret = net_readbuf_read(fd, readbuf, &c, errstr)) == 1)
{
str[i++] = c;
if (c == '\n')
{
break;
}
}
else if (ret == 0)
{
break;
}
else
{
return NET_EIO;
}
}
str[i] = '\0';
*len = i;
return NET_EOK;
}
/*
* net_puts()
*
* see net.h
*/
int net_puts(int fd, const char *s, size_t len, char **errstr)
{
int ret;
if (len < 1)
{
return NET_EOK;
}
if ((ret = net_send(fd, s, len, errstr)) < 0)
{
return NET_EIO;
}