This repository was archived by the owner on Nov 3, 2021. It is now read-only.
forked from aosp-mirror/platform_external_qemu
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathsockets.c
1568 lines (1299 loc) · 35.1 KB
/
sockets.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
/* Copyright (C) 2007-2008 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** 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.
*/
#ifdef __linux__ /* Recent versions of glibc only define EAI_NODATA, which is an
extension to the POSIX standard, if _GNU_SOURCE is defined. */
# define _GNU_SOURCE 1
#endif
#include "sockets.h"
#include <fcntl.h>
#include <stddef.h>
#include "qemu_debug.h"
#include "qemu-char.h"
#include <stdlib.h>
#include <string.h>
#include "android/utils/path.h"
#include "android/utils/debug.h"
#include "android/utils/misc.h"
#include "android/utils/system.h"
#define D(...) VERBOSE_PRINT(socket,__VA_ARGS__)
#ifdef _WIN32
# define xxWIN32_LEAN_AND_MEAN
# include <windows.h>
# include <winsock2.h>
# include <ws2tcpip.h>
#else /* !_WIN32 */
# include <sys/ioctl.h>
# include <sys/socket.h>
# include <netinet/in.h>
# include <netinet/tcp.h>
# include <netdb.h>
# if HAVE_UNIX_SOCKETS
# include <sys/un.h>
# ifndef UNIX_PATH_MAX
# define UNIX_PATH_MAX (sizeof(((struct sockaddr_un*)0)->sun_path)-1)
# endif
# endif
#endif /* !_WIN32 */
/* QSOCKET_CALL is used to deal with the fact that EINTR happens pretty
* easily in QEMU since we use SIGALRM to implement periodic timers
*/
#ifdef _WIN32
# define QSOCKET_CALL(_ret,_cmd) \
do { _ret = (_cmd); } while ( _ret < 0 && WSAGetLastError() == WSAEINTR )
#else
# define QSOCKET_CALL(_ret,_cmd) \
do { \
errno = 0; \
do { _ret = (_cmd); } while ( _ret < 0 && errno == EINTR ); \
} while (0);
#endif
#ifdef _WIN32
#include <errno.h>
static int winsock_error;
#define WINSOCK_ERRORS_LIST \
EE(WSA_INVALID_HANDLE,EINVAL,"invalid handle") \
EE(WSA_NOT_ENOUGH_MEMORY,ENOMEM,"not enough memory") \
EE(WSA_INVALID_PARAMETER,EINVAL,"invalid parameter") \
EE(WSAEINTR,EINTR,"interrupted function call") \
EE(WSAEALREADY,EALREADY,"operation already in progress") \
EE(WSAEBADF,EBADF,"bad file descriptor") \
EE(WSAEACCES,EACCES,"permission denied") \
EE(WSAEFAULT,EFAULT,"bad address") \
EE(WSAEINVAL,EINVAL,"invalid argument") \
EE(WSAEMFILE,EMFILE,"too many opened files") \
EE(WSAEWOULDBLOCK,EWOULDBLOCK,"resource temporarily unavailable") \
EE(WSAEINPROGRESS,EINPROGRESS,"operation now in progress") \
EE(WSAEALREADY,EAGAIN,"operation already in progress") \
EE(WSAENOTSOCK,EBADF,"socket operation not on socket") \
EE(WSAEDESTADDRREQ,EDESTADDRREQ,"destination address required") \
EE(WSAEMSGSIZE,EMSGSIZE,"message too long") \
EE(WSAEPROTOTYPE,EPROTOTYPE,"wrong protocol type for socket") \
EE(WSAENOPROTOOPT,ENOPROTOOPT,"bad protocol option") \
EE(WSAEADDRINUSE,EADDRINUSE,"address already in use") \
EE(WSAEADDRNOTAVAIL,EADDRNOTAVAIL,"cannot assign requested address") \
EE(WSAENETDOWN,ENETDOWN,"network is down") \
EE(WSAENETUNREACH,ENETUNREACH,"network unreachable") \
EE(WSAENETRESET,ENETRESET,"network dropped connection on reset") \
EE(WSAECONNABORTED,ECONNABORTED,"software caused connection abort") \
EE(WSAECONNRESET,ECONNRESET,"connection reset by peer") \
EE(WSAENOBUFS,ENOBUFS,"no buffer space available") \
EE(WSAEISCONN,EISCONN,"socket is already connected") \
EE(WSAENOTCONN,ENOTCONN,"socket is not connected") \
EE(WSAESHUTDOWN,ESHUTDOWN,"cannot send after socket shutdown") \
EE(WSAETOOMANYREFS,ETOOMANYREFS,"too many references") \
EE(WSAETIMEDOUT,ETIMEDOUT,"connection timed out") \
EE(WSAECONNREFUSED,ECONNREFUSED,"connection refused") \
EE(WSAELOOP,ELOOP,"cannot translate name") \
EE(WSAENAMETOOLONG,ENAMETOOLONG,"name too long") \
EE(WSAEHOSTDOWN,EHOSTDOWN,"host is down") \
EE(WSAEHOSTUNREACH,EHOSTUNREACH,"no route to host") \
typedef struct {
int winsock;
int unix;
const char* string;
} WinsockError;
static const WinsockError _winsock_errors[] = {
#define EE(w,u,s) { w, u, s },
WINSOCK_ERRORS_LIST
#undef EE
{ -1, -1, NULL }
};
/* this function reads the latest winsock error code and updates
* errno to a matching value. It also returns the new value of
* errno.
*/
static int
fix_errno( void )
{
const WinsockError* werr = _winsock_errors;
int unix = EINVAL; /* generic error code */
winsock_error = WSAGetLastError();
for ( ; werr->string != NULL; werr++ ) {
if (werr->winsock == winsock_error) {
unix = werr->unix;
break;
}
}
errno = unix;
return -1;
}
static int
set_errno( int code )
{
winsock_error = -1;
errno = code;
return -1;
}
/* this function returns a string describing the latest Winsock error */
const char*
_errno_str(void)
{
const WinsockError* werr = _winsock_errors;
const char* result = NULL;
for ( ; werr->string; werr++ ) {
if (werr->winsock == winsock_error) {
result = werr->string;
break;
}
}
if (result == NULL) {
result = tempstr_format(
"Unkown socket error (Winsock=0x%08x) errno=%d: %s",
winsock_error, errno, strerror(errno));
}
return result;
}
#else
static int
fix_errno( void )
{
return -1;
}
static int
set_errno( int code )
{
errno = code;
return -1;
}
#endif
/* socket types */
static int
socket_family_to_bsd( SocketFamily family )
{
switch (family) {
case SOCKET_INET: return AF_INET;
case SOCKET_IN6: return AF_INET6;
#if HAVE_UNIX_SOCKETS
case SOCKET_UNIX: return AF_LOCAL;
#endif
default: return -1;
}
}
static int
socket_type_to_bsd( SocketType type )
{
switch (type) {
case SOCKET_DGRAM: return SOCK_DGRAM;
case SOCKET_STREAM: return SOCK_STREAM;
default: return 0;
}
}
static SocketType
socket_type_from_bsd( int type )
{
switch (type) {
case SOCK_DGRAM: return SOCKET_DGRAM;
case SOCK_STREAM: return SOCKET_STREAM;
default: return (SocketType) SOCKET_UNSPEC;
}
}
#if 0
static int
socket_type_check( SocketType type )
{
return (type == SOCKET_DGRAM || type == SOCKET_STREAM);
}
#endif
typedef union {
struct sockaddr sa[1];
struct sockaddr_in in[1];
#if HAVE_IN6_SOCKETS
struct sockaddr_in6 in6[1];
#endif
#if HAVE_UNIX_SOCKETS
struct sockaddr_un un[1];
#endif
} sockaddr_storage;
/* socket addresses */
void
sock_address_init_inet( SockAddress* a, uint32_t ip, uint16_t port )
{
a->family = SOCKET_INET;
a->u.inet.port = port;
a->u.inet.address = ip;
}
void
sock_address_init_in6 ( SockAddress* a, const uint8_t* ip6[16], uint16_t port )
{
a->family = SOCKET_IN6;
a->u.in6.port = port;
memcpy( a->u.in6.address, ip6, sizeof(a->u.in6.address) );
}
void
sock_address_init_unix( SockAddress* a, const char* path )
{
a->family = SOCKET_UNIX;
a->u._unix.path = strdup(path ? path : "");
a->u._unix.owner = 1;
}
void sock_address_done( SockAddress* a )
{
if (a->family == SOCKET_UNIX && a->u._unix.owner) {
a->u._unix.owner = 0;
free((char*)a->u._unix.path);
}
}
static char*
format_char( char* buf, char* end, int c )
{
if (buf < end) {
if (buf+1 == end) {
*buf++ = 0;
} else {
*buf++ = (char) c;
*buf = 0;
}
}
return buf;
}
static char*
format_str( char* buf, char* end, const char* str )
{
int len = strlen(str);
int avail = end - buf;
if (len > avail)
len = avail;
memcpy( buf, str, len );
buf += len;
if (buf == end)
buf[-1] = 0;
else
buf[0] = 0;
return buf;
}
static char*
format_unsigned( char* buf, char* end, unsigned val )
{
char temp[16];
int nn;
for ( nn = 0; val != 0; nn++ ) {
int rem = val % 10;
temp[nn] = '0'+rem;
val /= 10;
}
if (nn == 0)
temp[nn++] = '0';
while (nn > 0)
buf = format_char(buf, end, temp[--nn]);
return buf;
}
static char*
format_hex( char* buf, char* end, unsigned val, int ndigits )
{
int shift = 4*ndigits;
static const char hex[16] = "0123456789abcdef";
while (shift >= 0) {
buf = format_char(buf, end, hex[(val >> shift) & 15]);
shift -= 4;
}
return buf;
}
static char*
format_ip4( char* buf, char* end, uint32_t ip )
{
buf = format_unsigned( buf, end, (unsigned)(ip >> 24) );
buf = format_char( buf, end, '.');
buf = format_unsigned( buf, end, (unsigned)((ip >> 16) & 255));
buf = format_char( buf, end, '.');
buf = format_unsigned( buf, end, (unsigned)((ip >> 8) & 255));
buf = format_char( buf, end, '.');
buf = format_unsigned( buf, end, (unsigned)(ip & 255));
return buf;
}
static char*
format_ip6( char* buf, char* end, const uint8_t* ip6 )
{
int nn;
for (nn = 0; nn < 8; nn++) {
int val = (ip6[0] << 16) | ip6[1];
ip6 += 2;
if (nn > 0)
buf = format_char(buf, end, ':');
if (val == 0)
continue;
buf = format_hex(buf, end, val, 4);
}
return buf;
}
const char*
sock_address_to_string( const SockAddress* a )
{
static char buf0[MAX_PATH];
char *buf = buf0, *end = buf + sizeof(buf0);
switch (a->family) {
case SOCKET_INET:
buf = format_ip4( buf, end, a->u.inet.address );
buf = format_char( buf, end, ':' );
buf = format_unsigned( buf, end, (unsigned) a->u.inet.port );
break;
case SOCKET_IN6:
buf = format_ip6( buf, end, a->u.in6.address );
buf = format_char( buf, end, ':' );
buf = format_unsigned( buf, end, (unsigned) a->u.in6.port );
break;
case SOCKET_UNIX:
buf = format_str( buf, end, a->u._unix.path );
break;
default:
return NULL;
}
return buf0;
}
int
sock_address_equal( const SockAddress* a, const SockAddress* b )
{
if (a->family != b->family)
return 0;
switch (a->family) {
case SOCKET_INET:
return (a->u.inet.address == b->u.inet.address &&
a->u.inet.port == b->u.inet.port);
case SOCKET_IN6:
return (!memcmp(a->u.in6.address, b->u.in6.address, 16) &&
a->u.in6.port == b->u.in6.port);
case SOCKET_UNIX:
return (!strcmp(a->u._unix.path, b->u._unix.path));
default:
return 0;
}
}
int
sock_address_get_port( const SockAddress* a )
{
switch (a->family) {
case SOCKET_INET:
return a->u.inet.port;
case SOCKET_IN6:
return a->u.in6.port;
default:
return -1;
}
}
void
sock_address_set_port( SockAddress* a, uint16_t port )
{
switch (a->family) {
case SOCKET_INET:
a->u.inet.port = port;
break;
case SOCKET_IN6:
a->u.in6.port = port;
break;
default:
;
}
}
const char*
sock_address_get_path( const SockAddress* a )
{
if (a->family == SOCKET_UNIX)
return a->u._unix.path;
else
return NULL;
}
int
sock_address_get_ip( const SockAddress* a )
{
if (a->family == SOCKET_INET)
return a->u.inet.address;
return -1;
}
#if 0
char*
bufprint_sock_address( char* p, char* end, const SockAddress* a )
{
switch (a->family) {
case SOCKET_INET:
{
uint32_t ip = a->u.inet.address;
return bufprint( p, end, "%d.%d.%d.%d:%d",
(ip >> 24) & 255, (ip >> 16) & 255,
(ip >> 8) & 255, ip & 255,
a->u.inet.port );
}
case SOCKET_IN6:
{
int nn = 0;
const char* column = "";
const uint8_t* tab = a->u.in6.address;
for (nn = 0; nn < 16; nn += 2) {
p = bufprint(p, end, "%s%04x", column, (tab[n] << 8) | tab[n+1]);
column = ":";
}
return bufprint(p, end, ":%d", a->u.in6.port);
}
case SOCKET_UNIX:
{
return bufprint(p, end, "%s", a->u._unix.path);
}
default:
return p;
}
}
#endif
static int
sock_address_to_bsd( const SockAddress* a, sockaddr_storage* paddress, socklen_t *psize )
{
switch (a->family) {
case SOCKET_INET:
{
struct sockaddr_in* dst = paddress->in;
*psize = sizeof(*dst);
memset( paddress, 0, *psize );
dst->sin_family = AF_INET;
dst->sin_port = htons(a->u.inet.port);
dst->sin_addr.s_addr = htonl(a->u.inet.address);
}
break;
#if HAVE_IN6_SOCKETS
case SOCKET_IN6:
{
struct sockaddr_in6* dst = paddress->in6;
*psize = sizeof(*dst);
memset( paddress, 0, *psize );
dst->sin6_family = AF_INET6;
dst->sin6_port = htons(a->u.in6.port);
memcpy( dst->sin6_addr.s6_addr, a->u.in6.address, 16 );
}
break;
#endif /* HAVE_IN6_SOCKETS */
#if HAVE_UNIX_SOCKETS
case SOCKET_UNIX:
{
int slen = strlen(a->u._unix.path);
struct sockaddr_un* dst = paddress->un;
if (slen >= UNIX_PATH_MAX)
return -1;
memset( dst, 0, sizeof(*dst) );
dst->sun_family = AF_LOCAL;
memcpy( dst->sun_path, a->u._unix.path, slen );
dst->sun_path[slen] = 0;
*psize = (char*)&dst->sun_path[slen+1] - (char*)dst;
}
break;
#endif /* HAVE_UNIX_SOCKETS */
default:
return set_errno(EINVAL);
}
return 0;
}
static int
sock_address_from_bsd( SockAddress* a, const void* from, size_t fromlen )
{
switch (((struct sockaddr *)from)->sa_family) {
case AF_INET:
{
const struct sockaddr_in* src = from;
if (fromlen < sizeof(*src))
return set_errno(EINVAL);
a->family = SOCKET_INET;
a->u.inet.port = ntohs(src->sin_port);
a->u.inet.address = ntohl(src->sin_addr.s_addr);
}
break;
#ifdef HAVE_IN6_SOCKETS
case AF_INET6:
{
const struct sockaddr_in6* src = from;
if (fromlen < sizeof(*src))
return set_errno(EINVAL);
a->family = SOCKET_IN6;
a->u.in6.port = ntohs(src->sin6_port);
memcpy(a->u.in6.address, src->sin6_addr.s6_addr, 16);
}
break;
#endif
#ifdef HAVE_UNIX_SOCKETS
case AF_LOCAL:
{
const struct sockaddr_un* src = from;
char* end;
if (fromlen < sizeof(*src))
return set_errno(EINVAL);
/* check that the path is zero-terminated */
end = memchr(src->sun_path, 0, UNIX_PATH_MAX);
if (end == NULL)
return set_errno(EINVAL);
a->family = SOCKET_UNIX;
a->u._unix.owner = 1;
a->u._unix.path = strdup(src->sun_path);
}
break;
#endif
default:
return set_errno(EINVAL);
}
return 0;
}
int
sock_address_init_resolve( SockAddress* a, const char* hostname, uint16_t port, int preferIn6 )
{
struct addrinfo hints[1];
struct addrinfo* res;
int ret;
memset(hints, 0, sizeof(hints));
hints->ai_family = preferIn6 ? AF_INET6 : AF_UNSPEC;
ret = getaddrinfo(hostname, NULL, hints, &res);
if (ret != 0) {
int err;
switch (ret) {
case EAI_AGAIN: /* server is down */
case EAI_FAIL: /* server is sick */
err = EHOSTDOWN;
break;
/* NOTE that in x86_64-w64-mingw32 both EAI_NODATA and EAI_NONAME are the same */
#if defined(EAI_NODATA) && (EAI_NODATA != EAI_NONAME)
case EAI_NODATA:
#endif
case EAI_NONAME:
err = ENOENT;
break;
case EAI_MEMORY:
err = ENOMEM;
break;
default:
err = EINVAL;
}
return set_errno(err);
}
/* Parse the returned list of addresses. */
{
struct addrinfo* res_ipv4 = NULL;
struct addrinfo* res_ipv6 = NULL;
struct addrinfo* r;
/* If preferIn6 is false, we stop on the first IPv4 address,
* otherwise, we stop on the first IPv6 one
*/
for (r = res; r != NULL; r = r->ai_next) {
if (r->ai_family == AF_INET && res_ipv4 == NULL) {
res_ipv4 = r;
if (!preferIn6)
break;
}
else if (r->ai_family == AF_INET6 && res_ipv6 == NULL) {
res_ipv6 = r;
if (preferIn6)
break;
}
}
/* Select the best address in 'r', which will be NULL
* if there is no corresponding address.
*/
if (preferIn6) {
r = res_ipv6;
if (r == NULL)
r = res_ipv4;
} else {
r = res_ipv4;
if (r == NULL)
r = res_ipv6;
}
if (r == NULL) {
ret = set_errno(ENOENT);
goto Exit;
}
/* Convert to a SockAddress */
ret = sock_address_from_bsd( a, r->ai_addr, r->ai_addrlen );
if (ret < 0)
goto Exit;
}
/* need to set the port */
switch (a->family) {
case SOCKET_INET: a->u.inet.port = port; break;
case SOCKET_IN6: a->u.in6.port = port; break;
default: ;
}
Exit:
freeaddrinfo(res);
return ret;
}
/* The Winsock headers for mingw lack some definitions */
#ifndef AI_ADDRCONFIG
# define AI_ADDRCONFIG 0
#endif
SockAddress**
sock_address_list_create( const char* hostname,
const char* port,
unsigned flags )
{
SockAddress** list = NULL;
SockAddress* addr;
int nn, count, ret;
struct addrinfo ai, *res, *e;
memset(&ai, 0, sizeof(ai));
ai.ai_flags |= AI_ADDRCONFIG;
ai.ai_family = PF_UNSPEC;
if (flags & SOCKET_LIST_FORCE_INET)
ai.ai_family = PF_INET;
else if (flags & SOCKET_LIST_FORCE_IN6)
ai.ai_family = PF_INET6;
if (flags & SOCKET_LIST_PASSIVE)
ai.ai_flags |= AI_PASSIVE;
else
ai.ai_flags |= AI_CANONNAME;
if (flags & SOCKET_LIST_DGRAM)
ai.ai_socktype = SOCK_DGRAM;
while (1) {
struct addrinfo hints = ai;
ret = getaddrinfo(hostname, port, &hints, &res);
if (ret == 0)
break;
switch (ret) {
#ifdef EAI_ADDRFAMILY
case EAI_ADDRFAMILY:
#endif
case EAI_NODATA:
set_errno(ENOENT);
break;
case EAI_FAMILY:
set_errno(EAFNOSUPPORT);
break;
case EAI_AGAIN:
set_errno(EAGAIN);
break;
#ifdef EAI_SYSTEM
case EAI_SYSTEM:
if (errno == EINTR)
continue;
break;
#endif
default:
set_errno(EINVAL);
}
return NULL;
}
/* allocate result list */
for (count = 0, e = res; e != NULL; e = e->ai_next)
count += 1;
AARRAY_NEW(list, count+1);
AARRAY_NEW(addr, count);
for (nn = 0, e = res; e != NULL; e = e->ai_next) {
ret = sock_address_from_bsd(addr, e->ai_addr, e->ai_addrlen);
if (ret < 0)
continue;
list[nn++] = addr++;
}
list[nn] = NULL;
freeaddrinfo(res);
return list;
}
SockAddress**
sock_address_list_create2(const char* host_and_port, unsigned flags )
{
char host_name[512];
const char* actual_host_name = "localhost";
// Parse host and port name.
const char* port_name = strchr(host_and_port, ':');
if (port_name != NULL) {
int to_copy = MIN(sizeof(host_name)-1, port_name - host_and_port);
if (to_copy != 0) {
memcpy(host_name, host_and_port, to_copy);
host_name[to_copy] = '\0';
actual_host_name = host_name;
port_name++;
} else {
return NULL;
}
} else {
port_name = host_and_port;
}
// Make sure that port_name is not empty.
if (port_name[0] == '\0') {
return NULL;
}
return sock_address_list_create(actual_host_name, port_name, flags);
}
void
sock_address_list_free( SockAddress** list )
{
int nn;
SockAddress* addr;
if (list == NULL)
return;
addr = list[0];
for (nn = 0; list[nn] != NULL; nn++) {
sock_address_done(list[nn]);
list[nn] = NULL;
}
AFREE(addr);
AFREE(list);
}
int
sock_address_get_numeric_info( SockAddress* a,
char* host,
size_t hostlen,
char* serv,
size_t servlen )
{
struct sockaddr* saddr;
socklen_t slen;
int ret;
switch (a->family) {
case SOCKET_INET:
saddr = (struct sockaddr*) &a->u.inet.address;
slen = sizeof(a->u.inet.address);
break;
#if HAVE_IN6_SOCKET
case SOCKET_IN6:
saddr = (struct sockaddr*) &a->u.in6.address;
slen = sizeof(a->u.in6.address);
break;
#endif
default:
return set_errno(EINVAL);
}
ret = getnameinfo( saddr, slen, host, hostlen, serv, servlen,
NI_NUMERICHOST | NI_NUMERICSERV );
switch (ret) {
case 0:
break;
case EAI_AGAIN:
ret = EAGAIN;
break;
default:
ret = EINVAL;
}
return ret;
}
int
socket_create( SocketFamily family, SocketType type )
{
int ret;
int sfamily = socket_family_to_bsd(family);
int stype = socket_type_to_bsd(type);
if (sfamily < 0 || stype < 0) {
return set_errno(EINVAL);
}
QSOCKET_CALL(ret, socket(sfamily, stype, 0));
if (ret < 0)
return fix_errno();
return ret;
}
int
socket_create_inet( SocketType type )
{
return socket_create( SOCKET_INET, type );
}
#if HAVE_IN6_SOCKETS
int
socket_create_in6 ( SocketType type )
{
return socket_create( SOCKET_IN6, type );
}
#endif
#if HAVE_UNIX_SOCKETS
int
socket_create_unix( SocketType type )
{
return socket_create( SOCKET_UNIX, type );
}
#endif
int socket_can_read(int fd)
{
#ifdef _WIN32
unsigned long opt;
if (ioctlsocket(fd, FIONREAD, &opt) < 0)
return 0;
return opt;
#else
int opt;
if (ioctl(fd, FIONREAD, &opt) < 0)
return 0;
return opt;
#endif
}
#define SOCKET_CALL(cmd) \
int ret; \
QSOCKET_CALL(ret, (cmd)); \
if (ret < 0) \
return fix_errno(); \
return ret; \
int
socket_send(int fd, const void* buf, int buflen)
{
SOCKET_CALL(send(fd, buf, buflen, 0))
}
int
socket_send_oob( int fd, const void* buf, int buflen )
{
SOCKET_CALL(send(fd, buf, buflen, MSG_OOB));
}
int
socket_sendto(int fd, const void* buf, int buflen, const SockAddress* to)
{
sockaddr_storage sa;
socklen_t salen;
if (sock_address_to_bsd(to, &sa, &salen) < 0)
return -1;
SOCKET_CALL(sendto(fd, buf, buflen, 0, sa.sa, salen));
}
int
socket_recv(int fd, void* buf, int len)
{
SOCKET_CALL(recv(fd, buf, len, 0));
}
int
socket_recvfrom(int fd, void* buf, int len, SockAddress* from)
{
sockaddr_storage sa;
socklen_t salen = sizeof(sa);
int ret;
QSOCKET_CALL(ret,recvfrom(fd,buf,len,0,sa.sa,&salen));