1 /* Copyright (C) 2007-2008 The Android Open Source Project 2 ** 3 ** This software is licensed under the terms of the GNU General Public 4 ** License version 2, as published by the Free Software Foundation, and 5 ** may be copied, distributed, and modified under those terms. 6 ** 7 ** This program is distributed in the hope that it will be useful, 8 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 9 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 ** GNU General Public License for more details. 11 */ 12 #ifdef __linux__ /* Recent versions of glibc only define EAI_NODATA, which is an 13 extension to the POSIX standard, if _GNU_SOURCE is defined. */ 14 # define _GNU_SOURCE 1 15 #endif 16 17 #include "android/sockets.h" 18 #include <fcntl.h> 19 #include <stddef.h> 20 #include "android/qemu-debug.h" 21 #include "sysemu/char.h" 22 #include <stdlib.h> 23 #include <string.h> 24 #include "android/utils/path.h" 25 #include "android/utils/debug.h" 26 #include "android/utils/eintr_wrapper.h" 27 #include "android/utils/misc.h" 28 #include "android/utils/system.h" 29 30 #define D(...) VERBOSE_PRINT(socket,__VA_ARGS__) 31 32 #ifdef _WIN32 33 # define xxWIN32_LEAN_AND_MEAN 34 # include <windows.h> 35 # include <winsock2.h> 36 # include <ws2tcpip.h> 37 #else /* !_WIN32 */ 38 # include <sys/ioctl.h> 39 # include <sys/socket.h> 40 # include <netinet/in.h> 41 # include <netinet/tcp.h> 42 # include <netdb.h> 43 # if HAVE_UNIX_SOCKETS 44 # include <sys/un.h> 45 # ifndef UNIX_PATH_MAX 46 # define UNIX_PATH_MAX (sizeof(((struct sockaddr_un*)0)->sun_path)-1) 47 # endif 48 # endif 49 #endif /* !_WIN32 */ 50 51 52 53 /* QSOCKET_CALL is used to deal with the fact that EINTR happens pretty 54 * easily in QEMU since we use SIGALRM to implement periodic timers 55 */ 56 #ifdef _WIN32 57 # define QSOCKET_CALL(_ret,_cmd) \ 58 do { _ret = (_cmd); } while ( _ret < 0 && WSAGetLastError() == WSAEINTR ) 59 #else 60 # define QSOCKET_CALL(_ret,_cmd) \ 61 do { \ 62 errno = 0; \ 63 _ret = HANDLE_EINTR(_cmd); \ 64 } while (0); 65 #endif 66 67 #ifdef _WIN32 68 69 #include <errno.h> 70 71 static int winsock_error; 72 73 #define WINSOCK_ERRORS_LIST \ 74 EE(WSA_INVALID_HANDLE,EINVAL,"invalid handle") \ 75 EE(WSA_NOT_ENOUGH_MEMORY,ENOMEM,"not enough memory") \ 76 EE(WSA_INVALID_PARAMETER,EINVAL,"invalid parameter") \ 77 EE(WSAEINTR,EINTR,"interrupted function call") \ 78 EE(WSAEALREADY,EALREADY,"operation already in progress") \ 79 EE(WSAEBADF,EBADF,"bad file descriptor") \ 80 EE(WSAEACCES,EACCES,"permission denied") \ 81 EE(WSAEFAULT,EFAULT,"bad address") \ 82 EE(WSAEINVAL,EINVAL,"invalid argument") \ 83 EE(WSAEMFILE,EMFILE,"too many opened files") \ 84 EE(WSAEWOULDBLOCK,EWOULDBLOCK,"resource temporarily unavailable") \ 85 EE(WSAEINPROGRESS,EINPROGRESS,"operation now in progress") \ 86 EE(WSAEALREADY,EAGAIN,"operation already in progress") \ 87 EE(WSAENOTSOCK,EBADF,"socket operation not on socket") \ 88 EE(WSAEDESTADDRREQ,EDESTADDRREQ,"destination address required") \ 89 EE(WSAEMSGSIZE,EMSGSIZE,"message too long") \ 90 EE(WSAEPROTOTYPE,EPROTOTYPE,"wrong protocol type for socket") \ 91 EE(WSAENOPROTOOPT,ENOPROTOOPT,"bad protocol option") \ 92 EE(WSAEADDRINUSE,EADDRINUSE,"address already in use") \ 93 EE(WSAEADDRNOTAVAIL,EADDRNOTAVAIL,"cannot assign requested address") \ 94 EE(WSAENETDOWN,ENETDOWN,"network is down") \ 95 EE(WSAENETUNREACH,ENETUNREACH,"network unreachable") \ 96 EE(WSAENETRESET,ENETRESET,"network dropped connection on reset") \ 97 EE(WSAECONNABORTED,ECONNABORTED,"software caused connection abort") \ 98 EE(WSAECONNRESET,ECONNRESET,"connection reset by peer") \ 99 EE(WSAENOBUFS,ENOBUFS,"no buffer space available") \ 100 EE(WSAEISCONN,EISCONN,"socket is already connected") \ 101 EE(WSAENOTCONN,ENOTCONN,"socket is not connected") \ 102 EE(WSAESHUTDOWN,ESHUTDOWN,"cannot send after socket shutdown") \ 103 EE(WSAETOOMANYREFS,ETOOMANYREFS,"too many references") \ 104 EE(WSAETIMEDOUT,ETIMEDOUT,"connection timed out") \ 105 EE(WSAECONNREFUSED,ECONNREFUSED,"connection refused") \ 106 EE(WSAELOOP,ELOOP,"cannot translate name") \ 107 EE(WSAENAMETOOLONG,ENAMETOOLONG,"name too long") \ 108 EE(WSAEHOSTDOWN,EHOSTDOWN,"host is down") \ 109 EE(WSAEHOSTUNREACH,EHOSTUNREACH,"no route to host") \ 110 111 typedef struct { 112 int winsock; 113 int unix; 114 const char* string; 115 } WinsockError; 116 117 static const WinsockError _winsock_errors[] = { 118 #define EE(w,u,s) { w, u, s }, 119 WINSOCK_ERRORS_LIST 120 #undef EE 121 { -1, -1, NULL } 122 }; 123 124 /* this function reads the latest winsock error code and updates 125 * errno to a matching value. It also returns the new value of 126 * errno. 127 */ 128 static int 129 fix_errno( void ) 130 { 131 const WinsockError* werr = _winsock_errors; 132 int unix = EINVAL; /* generic error code */ 133 134 winsock_error = WSAGetLastError(); 135 136 for ( ; werr->string != NULL; werr++ ) { 137 if (werr->winsock == winsock_error) { 138 unix = werr->unix; 139 break; 140 } 141 } 142 errno = unix; 143 return -1; 144 } 145 146 static int 147 set_errno( int code ) 148 { 149 winsock_error = -1; 150 errno = code; 151 return -1; 152 } 153 154 /* this function returns a string describing the latest Winsock error */ 155 const char* 156 _errno_str(void) 157 { 158 const WinsockError* werr = _winsock_errors; 159 const char* result = NULL; 160 161 for ( ; werr->string; werr++ ) { 162 if (werr->winsock == winsock_error) { 163 result = werr->string; 164 break; 165 } 166 } 167 168 if (result == NULL) { 169 result = tempstr_format( 170 "Unkown socket error (Winsock=0x%08x) errno=%d: %s", 171 winsock_error, errno, strerror(errno)); 172 } 173 return result; 174 } 175 #else 176 static int 177 fix_errno( void ) 178 { 179 return -1; 180 } 181 182 static int 183 set_errno( int code ) 184 { 185 errno = code; 186 return -1; 187 } 188 #endif 189 190 /* socket types */ 191 192 static int 193 socket_family_to_bsd( SocketFamily family ) 194 { 195 switch (family) { 196 case SOCKET_INET: return AF_INET; 197 case SOCKET_IN6: return AF_INET6; 198 #if HAVE_UNIX_SOCKETS 199 case SOCKET_UNIX: return AF_LOCAL; 200 #endif 201 default: return -1; 202 } 203 } 204 205 static int 206 socket_type_to_bsd( SocketType type ) 207 { 208 switch (type) { 209 case SOCKET_DGRAM: return SOCK_DGRAM; 210 case SOCKET_STREAM: return SOCK_STREAM; 211 default: return 0; 212 } 213 } 214 215 static SocketType 216 socket_type_from_bsd( int type ) 217 { 218 switch (type) { 219 case SOCK_DGRAM: return SOCKET_DGRAM; 220 case SOCK_STREAM: return SOCKET_STREAM; 221 default: return (SocketType) SOCKET_UNSPEC; 222 } 223 } 224 225 #if 0 226 static int 227 socket_type_check( SocketType type ) 228 { 229 return (type == SOCKET_DGRAM || type == SOCKET_STREAM); 230 } 231 #endif 232 233 typedef union { 234 struct sockaddr sa[1]; 235 struct sockaddr_in in[1]; 236 #if HAVE_IN6_SOCKETS 237 struct sockaddr_in6 in6[1]; 238 #endif 239 #if HAVE_UNIX_SOCKETS 240 struct sockaddr_un un[1]; 241 #endif 242 } sockaddr_storage; 243 244 /* socket addresses */ 245 246 void 247 sock_address_init_inet( SockAddress* a, uint32_t ip, uint16_t port ) 248 { 249 a->family = SOCKET_INET; 250 a->u.inet.port = port; 251 a->u.inet.address = ip; 252 } 253 254 void 255 sock_address_init_in6 ( SockAddress* a, const uint8_t* ip6[16], uint16_t port ) 256 { 257 a->family = SOCKET_IN6; 258 a->u.in6.port = port; 259 memcpy( a->u.in6.address, ip6, sizeof(a->u.in6.address) ); 260 } 261 262 void 263 sock_address_init_unix( SockAddress* a, const char* path ) 264 { 265 a->family = SOCKET_UNIX; 266 a->u._unix.path = strdup(path ? path : ""); 267 a->u._unix.owner = 1; 268 } 269 270 void sock_address_done( SockAddress* a ) 271 { 272 if (a->family == SOCKET_UNIX && a->u._unix.owner) { 273 a->u._unix.owner = 0; 274 free((char*)a->u._unix.path); 275 } 276 } 277 278 static char* 279 format_char( char* buf, char* end, int c ) 280 { 281 if (buf < end) { 282 if (buf+1 == end) { 283 *buf++ = 0; 284 } else { 285 *buf++ = (char) c; 286 *buf = 0; 287 } 288 } 289 return buf; 290 } 291 292 static char* 293 format_str( char* buf, char* end, const char* str ) 294 { 295 int len = strlen(str); 296 int avail = end - buf; 297 298 if (len > avail) 299 len = avail; 300 301 memcpy( buf, str, len ); 302 buf += len; 303 304 if (buf == end) 305 buf[-1] = 0; 306 else 307 buf[0] = 0; 308 309 return buf; 310 } 311 312 static char* 313 format_unsigned( char* buf, char* end, unsigned val ) 314 { 315 char temp[16]; 316 int nn; 317 318 for ( nn = 0; val != 0; nn++ ) { 319 int rem = val % 10; 320 temp[nn] = '0'+rem; 321 val /= 10; 322 } 323 324 if (nn == 0) 325 temp[nn++] = '0'; 326 327 while (nn > 0) 328 buf = format_char(buf, end, temp[--nn]); 329 330 return buf; 331 } 332 333 static char* 334 format_hex( char* buf, char* end, unsigned val, int ndigits ) 335 { 336 int shift = 4*ndigits; 337 static const char hex[16] = "0123456789abcdef"; 338 339 while (shift >= 0) { 340 buf = format_char(buf, end, hex[(val >> shift) & 15]); 341 shift -= 4; 342 } 343 return buf; 344 } 345 346 static char* 347 format_ip4( char* buf, char* end, uint32_t ip ) 348 { 349 buf = format_unsigned( buf, end, (unsigned)(ip >> 24) ); 350 buf = format_char( buf, end, '.'); 351 buf = format_unsigned( buf, end, (unsigned)((ip >> 16) & 255)); 352 buf = format_char( buf, end, '.'); 353 buf = format_unsigned( buf, end, (unsigned)((ip >> 8) & 255)); 354 buf = format_char( buf, end, '.'); 355 buf = format_unsigned( buf, end, (unsigned)(ip & 255)); 356 return buf; 357 } 358 359 static char* 360 format_ip6( char* buf, char* end, const uint8_t* ip6 ) 361 { 362 int nn; 363 for (nn = 0; nn < 8; nn++) { 364 int val = (ip6[0] << 16) | ip6[1]; 365 ip6 += 2; 366 if (nn > 0) 367 buf = format_char(buf, end, ':'); 368 if (val == 0) 369 continue; 370 buf = format_hex(buf, end, val, 4); 371 } 372 return buf; 373 } 374 375 const char* 376 sock_address_to_string( const SockAddress* a ) 377 { 378 static char buf0[MAX_PATH]; 379 char *buf = buf0, *end = buf + sizeof(buf0); 380 381 switch (a->family) { 382 case SOCKET_INET: 383 buf = format_ip4( buf, end, a->u.inet.address ); 384 buf = format_char( buf, end, ':' ); 385 buf = format_unsigned( buf, end, (unsigned) a->u.inet.port ); 386 break; 387 388 case SOCKET_IN6: 389 buf = format_ip6( buf, end, a->u.in6.address ); 390 buf = format_char( buf, end, ':' ); 391 buf = format_unsigned( buf, end, (unsigned) a->u.in6.port ); 392 break; 393 394 case SOCKET_UNIX: 395 buf = format_str( buf, end, a->u._unix.path ); 396 break; 397 398 default: 399 return NULL; 400 } 401 402 return buf0; 403 } 404 405 int 406 sock_address_equal( const SockAddress* a, const SockAddress* b ) 407 { 408 if (a->family != b->family) 409 return 0; 410 411 switch (a->family) { 412 case SOCKET_INET: 413 return (a->u.inet.address == b->u.inet.address && 414 a->u.inet.port == b->u.inet.port); 415 416 case SOCKET_IN6: 417 return (!memcmp(a->u.in6.address, b->u.in6.address, 16) && 418 a->u.in6.port == b->u.in6.port); 419 420 case SOCKET_UNIX: 421 return (!strcmp(a->u._unix.path, b->u._unix.path)); 422 423 default: 424 return 0; 425 } 426 } 427 428 int 429 sock_address_get_port( const SockAddress* a ) 430 { 431 switch (a->family) { 432 case SOCKET_INET: 433 return a->u.inet.port; 434 case SOCKET_IN6: 435 return a->u.in6.port; 436 default: 437 return -1; 438 } 439 } 440 441 void 442 sock_address_set_port( SockAddress* a, uint16_t port ) 443 { 444 switch (a->family) { 445 case SOCKET_INET: 446 a->u.inet.port = port; 447 break; 448 case SOCKET_IN6: 449 a->u.in6.port = port; 450 break; 451 default: 452 ; 453 } 454 } 455 456 const char* 457 sock_address_get_path( const SockAddress* a ) 458 { 459 if (a->family == SOCKET_UNIX) 460 return a->u._unix.path; 461 else 462 return NULL; 463 } 464 465 int 466 sock_address_get_ip( const SockAddress* a ) 467 { 468 if (a->family == SOCKET_INET) 469 return a->u.inet.address; 470 471 return -1; 472 } 473 474 #if 0 475 char* 476 bufprint_sock_address( char* p, char* end, const SockAddress* a ) 477 { 478 switch (a->family) { 479 case SOCKET_INET: 480 { 481 uint32_t ip = a->u.inet.address; 482 483 return bufprint( p, end, "%d.%d.%d.%d:%d", 484 (ip >> 24) & 255, (ip >> 16) & 255, 485 (ip >> 8) & 255, ip & 255, 486 a->u.inet.port ); 487 } 488 case SOCKET_IN6: 489 { 490 int nn = 0; 491 const char* column = ""; 492 const uint8_t* tab = a->u.in6.address; 493 for (nn = 0; nn < 16; nn += 2) { 494 p = bufprint(p, end, "%s%04x", column, (tab[n] << 8) | tab[n+1]); 495 column = ":"; 496 } 497 return bufprint(p, end, ":%d", a->u.in6.port); 498 } 499 case SOCKET_UNIX: 500 { 501 return bufprint(p, end, "%s", a->u._unix.path); 502 } 503 default: 504 return p; 505 } 506 } 507 #endif 508 509 static int 510 sock_address_to_bsd( const SockAddress* a, sockaddr_storage* paddress, socklen_t *psize ) 511 { 512 switch (a->family) { 513 case SOCKET_INET: 514 { 515 struct sockaddr_in* dst = paddress->in; 516 517 *psize = sizeof(*dst); 518 519 memset( paddress, 0, *psize ); 520 521 dst->sin_family = AF_INET; 522 dst->sin_port = htons(a->u.inet.port); 523 dst->sin_addr.s_addr = htonl(a->u.inet.address); 524 } 525 break; 526 527 #if HAVE_IN6_SOCKETS 528 case SOCKET_IN6: 529 { 530 struct sockaddr_in6* dst = paddress->in6; 531 532 *psize = sizeof(*dst); 533 534 memset( paddress, 0, *psize ); 535 536 dst->sin6_family = AF_INET6; 537 dst->sin6_port = htons(a->u.in6.port); 538 memcpy( dst->sin6_addr.s6_addr, a->u.in6.address, 16 ); 539 } 540 break; 541 #endif /* HAVE_IN6_SOCKETS */ 542 543 #if HAVE_UNIX_SOCKETS 544 case SOCKET_UNIX: 545 { 546 int slen = strlen(a->u._unix.path); 547 struct sockaddr_un* dst = paddress->un; 548 549 if (slen >= UNIX_PATH_MAX) 550 return -1; 551 552 memset( dst, 0, sizeof(*dst) ); 553 554 dst->sun_family = AF_LOCAL; 555 memcpy( dst->sun_path, a->u._unix.path, slen ); 556 dst->sun_path[slen] = 0; 557 558 *psize = (char*)&dst->sun_path[slen+1] - (char*)dst; 559 } 560 break; 561 #endif /* HAVE_UNIX_SOCKETS */ 562 563 default: 564 return set_errno(EINVAL); 565 } 566 567 return 0; 568 } 569 570 static int 571 sock_address_from_bsd( SockAddress* a, const void* from, size_t fromlen ) 572 { 573 switch (((struct sockaddr *)from)->sa_family) { 574 case AF_INET: 575 { 576 const struct sockaddr_in* src = from; 577 578 if (fromlen < sizeof(*src)) 579 return set_errno(EINVAL); 580 581 a->family = SOCKET_INET; 582 a->u.inet.port = ntohs(src->sin_port); 583 a->u.inet.address = ntohl(src->sin_addr.s_addr); 584 } 585 break; 586 587 #ifdef HAVE_IN6_SOCKETS 588 case AF_INET6: 589 { 590 const struct sockaddr_in6* src = from; 591 592 if (fromlen < sizeof(*src)) 593 return set_errno(EINVAL); 594 595 a->family = SOCKET_IN6; 596 a->u.in6.port = ntohs(src->sin6_port); 597 memcpy(a->u.in6.address, src->sin6_addr.s6_addr, 16); 598 } 599 break; 600 #endif 601 602 #ifdef HAVE_UNIX_SOCKETS 603 case AF_LOCAL: 604 { 605 const struct sockaddr_un* src = from; 606 char* end; 607 608 if (fromlen < sizeof(*src)) 609 return set_errno(EINVAL); 610 611 /* check that the path is zero-terminated */ 612 end = memchr(src->sun_path, 0, UNIX_PATH_MAX); 613 if (end == NULL) 614 return set_errno(EINVAL); 615 616 a->family = SOCKET_UNIX; 617 a->u._unix.owner = 1; 618 a->u._unix.path = strdup(src->sun_path); 619 } 620 break; 621 #endif 622 623 default: 624 return set_errno(EINVAL); 625 } 626 return 0; 627 } 628 629 630 int 631 sock_address_init_resolve( SockAddress* a, const char* hostname, uint16_t port, int preferIn6 ) 632 { 633 struct addrinfo hints[1]; 634 struct addrinfo* res; 635 int ret; 636 637 memset(hints, 0, sizeof(hints)); 638 hints->ai_family = preferIn6 ? AF_INET6 : AF_UNSPEC; 639 640 ret = getaddrinfo(hostname, NULL, hints, &res); 641 if (ret != 0) { 642 int err; 643 644 switch (ret) { 645 case EAI_AGAIN: /* server is down */ 646 case EAI_FAIL: /* server is sick */ 647 err = EHOSTDOWN; 648 break; 649 650 /* NOTE that in x86_64-w64-mingw32 both EAI_NODATA and EAI_NONAME are the same */ 651 #if defined(EAI_NODATA) && (EAI_NODATA != EAI_NONAME) 652 case EAI_NODATA: 653 #endif 654 case EAI_NONAME: 655 err = ENOENT; 656 break; 657 658 case EAI_MEMORY: 659 err = ENOMEM; 660 break; 661 662 default: 663 err = EINVAL; 664 } 665 return set_errno(err); 666 } 667 668 /* Parse the returned list of addresses. */ 669 { 670 struct addrinfo* res_ipv4 = NULL; 671 struct addrinfo* res_ipv6 = NULL; 672 struct addrinfo* r; 673 674 /* If preferIn6 is false, we stop on the first IPv4 address, 675 * otherwise, we stop on the first IPv6 one 676 */ 677 for (r = res; r != NULL; r = r->ai_next) { 678 if (r->ai_family == AF_INET && res_ipv4 == NULL) { 679 res_ipv4 = r; 680 if (!preferIn6) 681 break; 682 } 683 else if (r->ai_family == AF_INET6 && res_ipv6 == NULL) { 684 res_ipv6 = r; 685 if (preferIn6) 686 break; 687 } 688 } 689 690 /* Select the best address in 'r', which will be NULL 691 * if there is no corresponding address. 692 */ 693 if (preferIn6) { 694 r = res_ipv6; 695 if (r == NULL) 696 r = res_ipv4; 697 } else { 698 r = res_ipv4; 699 if (r == NULL) 700 r = res_ipv6; 701 } 702 703 if (r == NULL) { 704 ret = set_errno(ENOENT); 705 goto Exit; 706 } 707 708 /* Convert to a SockAddress */ 709 ret = sock_address_from_bsd( a, r->ai_addr, r->ai_addrlen ); 710 if (ret < 0) 711 goto Exit; 712 } 713 714 /* need to set the port */ 715 switch (a->family) { 716 case SOCKET_INET: a->u.inet.port = port; break; 717 case SOCKET_IN6: a->u.in6.port = port; break; 718 default: ; 719 } 720 721 Exit: 722 freeaddrinfo(res); 723 return ret; 724 } 725 726 /* The Winsock headers for mingw lack some definitions */ 727 #ifndef AI_ADDRCONFIG 728 # define AI_ADDRCONFIG 0 729 #endif 730 731 SockAddress** 732 sock_address_list_create( const char* hostname, 733 const char* port, 734 unsigned flags ) 735 { 736 SockAddress** list = NULL; 737 SockAddress* addr; 738 int nn, count, ret; 739 struct addrinfo ai, *res, *e; 740 741 memset(&ai, 0, sizeof(ai)); 742 ai.ai_flags |= AI_ADDRCONFIG; 743 ai.ai_family = PF_UNSPEC; 744 745 if (flags & SOCKET_LIST_FORCE_INET) 746 ai.ai_family = PF_INET; 747 else if (flags & SOCKET_LIST_FORCE_IN6) 748 ai.ai_family = PF_INET6; 749 750 if (flags & SOCKET_LIST_PASSIVE) 751 ai.ai_flags |= AI_PASSIVE; 752 else 753 ai.ai_flags |= AI_CANONNAME; 754 755 if (flags & SOCKET_LIST_DGRAM) 756 ai.ai_socktype = SOCK_DGRAM; 757 758 while (1) { 759 struct addrinfo hints = ai; 760 761 ret = getaddrinfo(hostname, port, &hints, &res); 762 if (ret == 0) 763 break; 764 765 switch (ret) { 766 #ifdef EAI_ADDRFAMILY 767 case EAI_ADDRFAMILY: 768 #endif 769 case EAI_NODATA: 770 set_errno(ENOENT); 771 break; 772 case EAI_FAMILY: 773 set_errno(EAFNOSUPPORT); 774 break; 775 case EAI_AGAIN: 776 set_errno(EAGAIN); 777 break; 778 #ifdef EAI_SYSTEM 779 case EAI_SYSTEM: 780 if (errno == EINTR) 781 continue; 782 break; 783 #endif 784 default: 785 set_errno(EINVAL); 786 } 787 return NULL; 788 } 789 790 /* allocate result list */ 791 for (count = 0, e = res; e != NULL; e = e->ai_next) 792 count += 1; 793 794 AARRAY_NEW(list, count+1); 795 AARRAY_NEW(addr, count); 796 797 for (nn = 0, e = res; e != NULL; e = e->ai_next) { 798 799 ret = sock_address_from_bsd(addr, e->ai_addr, e->ai_addrlen); 800 if (ret < 0) 801 continue; 802 803 list[nn++] = addr++; 804 } 805 list[nn] = NULL; 806 freeaddrinfo(res); 807 return list; 808 } 809 810 SockAddress** 811 sock_address_list_create2(const char* host_and_port, unsigned flags ) 812 { 813 char host_name[512]; 814 const char* actual_host_name = "localhost"; 815 // Parse host and port name. 816 const char* port_name = strchr(host_and_port, ':'); 817 if (port_name != NULL) { 818 int to_copy = MIN(sizeof(host_name)-1, port_name - host_and_port); 819 if (to_copy != 0) { 820 memcpy(host_name, host_and_port, to_copy); 821 host_name[to_copy] = '\0'; 822 actual_host_name = host_name; 823 port_name++; 824 } else { 825 return NULL; 826 } 827 } else { 828 port_name = host_and_port; 829 } 830 // Make sure that port_name is not empty. 831 if (port_name[0] == '\0') { 832 return NULL; 833 } 834 return sock_address_list_create(actual_host_name, port_name, flags); 835 } 836 837 void 838 sock_address_list_free( SockAddress** list ) 839 { 840 int nn; 841 SockAddress* addr; 842 843 if (list == NULL) 844 return; 845 846 addr = list[0]; 847 for (nn = 0; list[nn] != NULL; nn++) { 848 sock_address_done(list[nn]); 849 list[nn] = NULL; 850 } 851 AFREE(addr); 852 AFREE(list); 853 } 854 855 int 856 sock_address_get_numeric_info( SockAddress* a, 857 char* host, 858 size_t hostlen, 859 char* serv, 860 size_t servlen ) 861 { 862 struct sockaddr* saddr; 863 socklen_t slen; 864 int ret; 865 866 switch (a->family) { 867 case SOCKET_INET: 868 saddr = (struct sockaddr*) &a->u.inet.address; 869 slen = sizeof(a->u.inet.address); 870 break; 871 872 #if HAVE_IN6_SOCKET 873 case SOCKET_IN6: 874 saddr = (struct sockaddr*) &a->u.in6.address; 875 slen = sizeof(a->u.in6.address); 876 break; 877 #endif 878 default: 879 return set_errno(EINVAL); 880 } 881 882 ret = getnameinfo( saddr, slen, host, hostlen, serv, servlen, 883 NI_NUMERICHOST | NI_NUMERICSERV ); 884 885 switch (ret) { 886 case 0: 887 break; 888 case EAI_AGAIN: 889 ret = EAGAIN; 890 break; 891 default: 892 ret = EINVAL; 893 } 894 return ret; 895 } 896 897 int 898 socket_create( SocketFamily family, SocketType type ) 899 { 900 int ret; 901 int sfamily = socket_family_to_bsd(family); 902 int stype = socket_type_to_bsd(type); 903 904 if (sfamily < 0 || stype < 0) { 905 return set_errno(EINVAL); 906 } 907 908 QSOCKET_CALL(ret, socket(sfamily, stype, 0)); 909 if (ret < 0) 910 return fix_errno(); 911 912 return ret; 913 } 914 915 916 int 917 socket_create_inet( SocketType type ) 918 { 919 return socket_create( SOCKET_INET, type ); 920 } 921 922 #if HAVE_IN6_SOCKETS 923 int 924 socket_create_in6 ( SocketType type ) 925 { 926 return socket_create( SOCKET_IN6, type ); 927 } 928 #endif 929 930 #if HAVE_UNIX_SOCKETS 931 int 932 socket_create_unix( SocketType type ) 933 { 934 return socket_create( SOCKET_UNIX, type ); 935 } 936 #endif 937 938 int socket_can_read(int fd) 939 { 940 #ifdef _WIN32 941 unsigned long opt; 942 943 if (ioctlsocket(fd, FIONREAD, &opt) < 0) 944 return 0; 945 946 return opt; 947 #else 948 int opt; 949 950 if (ioctl(fd, FIONREAD, &opt) < 0) 951 return 0; 952 953 return opt; 954 #endif 955 } 956 957 #define SOCKET_CALL(cmd) \ 958 int ret; \ 959 QSOCKET_CALL(ret, (cmd)); \ 960 if (ret < 0) \ 961 return fix_errno(); \ 962 return ret; \ 963 964 int 965 socket_send(int fd, const void* buf, int buflen) 966 { 967 SOCKET_CALL(send(fd, buf, buflen, 0)) 968 } 969 970 int 971 socket_send_oob( int fd, const void* buf, int buflen ) 972 { 973 SOCKET_CALL(send(fd, buf, buflen, MSG_OOB)); 974 } 975 976 int 977 socket_sendto(int fd, const void* buf, int buflen, const SockAddress* to) 978 { 979 sockaddr_storage sa; 980 socklen_t salen; 981 982 if (sock_address_to_bsd(to, &sa, &salen) < 0) 983 return -1; 984 985 SOCKET_CALL(sendto(fd, buf, buflen, 0, sa.sa, salen)); 986 } 987 988 int 989 socket_recv(int fd, void* buf, int len) 990 { 991 SOCKET_CALL(recv(fd, buf, len, 0)); 992 } 993 994 int 995 socket_recvfrom(int fd, void* buf, int len, SockAddress* from) 996 { 997 sockaddr_storage sa; 998 socklen_t salen = sizeof(sa); 999 int ret; 1000 1001 QSOCKET_CALL(ret,recvfrom(fd,buf,len,0,sa.sa,&salen)); 1002 if (ret < 0) 1003 return fix_errno(); 1004 1005 if (sock_address_from_bsd(from, &sa, salen) < 0) 1006 return -1; 1007 1008 return ret; 1009 } 1010 1011 int 1012 socket_connect( int fd, const SockAddress* address ) 1013 { 1014 sockaddr_storage addr; 1015 socklen_t addrlen; 1016 1017 if (sock_address_to_bsd(address, &addr, &addrlen) < 0) 1018 return -1; 1019 1020 SOCKET_CALL(connect(fd,addr.sa,addrlen)); 1021 } 1022 1023 int 1024 socket_bind( int fd, const SockAddress* address ) 1025 { 1026 sockaddr_storage addr; 1027 socklen_t addrlen; 1028 1029 if (sock_address_to_bsd(address, &addr, &addrlen) < 0) 1030 return -1; 1031 1032 SOCKET_CALL(bind(fd, addr.sa, addrlen)); 1033 } 1034 1035 int 1036 socket_get_address( int fd, SockAddress* address ) 1037 { 1038 sockaddr_storage addr; 1039 socklen_t addrlen = sizeof(addr); 1040 int ret; 1041 1042 QSOCKET_CALL(ret, getsockname(fd, addr.sa, &addrlen)); 1043 if (ret < 0) 1044 return fix_errno(); 1045 1046 return sock_address_from_bsd(address, &addr, addrlen); 1047 } 1048 1049 int 1050 socket_get_peer_address( int fd, SockAddress* address ) 1051 { 1052 sockaddr_storage addr; 1053 socklen_t addrlen = sizeof(addr); 1054 int ret; 1055 1056 QSOCKET_CALL(ret, getpeername(fd, addr.sa, &addrlen)); 1057 if (ret < 0) 1058 return fix_errno(); 1059 1060 return sock_address_from_bsd(address, &addr, addrlen); 1061 } 1062 1063 int 1064 socket_listen( int fd, int backlog ) 1065 { 1066 SOCKET_CALL(listen(fd, backlog)); 1067 } 1068 1069 int 1070 socket_accept( int fd, SockAddress* address ) 1071 { 1072 sockaddr_storage addr; 1073 socklen_t addrlen = sizeof(addr); 1074 int ret; 1075 1076 QSOCKET_CALL(ret, accept(fd, addr.sa, &addrlen)); 1077 if (ret < 0) 1078 return fix_errno(); 1079 1080 if (address) { 1081 if (sock_address_from_bsd(address, &addr, addrlen) < 0) { 1082 socket_close(ret); 1083 return -1; 1084 } 1085 } 1086 return ret; 1087 } 1088 1089 static int 1090 socket_getoption(int fd, int domain, int option, int defaut) 1091 { 1092 int ret; 1093 while (1) { 1094 #ifdef _WIN32 1095 DWORD opt = (DWORD)-1; 1096 #else 1097 int opt = -1; 1098 #endif 1099 socklen_t optlen = sizeof(opt); 1100 ret = HANDLE_EINTR( 1101 getsockopt(fd, domain, option, (char*)&opt, &optlen)); 1102 if (ret == 0) 1103 return (int)opt; 1104 else 1105 return defaut; 1106 } 1107 #undef OPT_CAST 1108 } 1109 1110 1111 SocketType socket_get_type(int fd) 1112 { 1113 int so_type = socket_getoption(fd, SOL_SOCKET, SO_TYPE, -1); 1114 return socket_type_from_bsd(so_type); 1115 } 1116 1117 int socket_set_nonblock(int fd) 1118 { 1119 #ifdef _WIN32 1120 unsigned long opt = 1; 1121 return ioctlsocket(fd, FIONBIO, &opt); 1122 #else 1123 int flags = fcntl(fd, F_GETFL); 1124 return fcntl(fd, F_SETFL, flags | O_NONBLOCK); 1125 #endif 1126 } 1127 1128 int socket_set_blocking(int fd) 1129 { 1130 #ifdef _WIN32 1131 unsigned long opt = 0; 1132 return ioctlsocket(fd, FIONBIO, &opt); 1133 #else 1134 int flags = fcntl(fd, F_GETFL); 1135 return fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); 1136 #endif 1137 } 1138 1139 static int 1140 socket_setoption(int fd, int domain, int option, int _flag) 1141 { 1142 #ifdef _WIN32 1143 DWORD flag = (DWORD) _flag; 1144 #else 1145 int flag = _flag; 1146 #endif 1147 return setsockopt( fd, domain, option, (const char*)&flag, sizeof(flag) ); 1148 } 1149 1150 int socket_set_xreuseaddr(int fd) 1151 { 1152 #ifdef _WIN32 1153 /* on Windows, SO_REUSEADDR is used to indicate that several programs can 1154 * bind to the same port. this is completely different from the Unix 1155 * semantics. instead of SO_EXCLUSIVEADDR to ensure that explicitely prevent 1156 * this. 1157 */ 1158 return socket_setoption(fd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, 1); 1159 #else 1160 return socket_setoption(fd, SOL_SOCKET, SO_REUSEADDR, 1); 1161 #endif 1162 } 1163 1164 1165 int socket_set_oobinline(int fd) 1166 { 1167 return socket_setoption(fd, SOL_SOCKET, SO_OOBINLINE, 1); 1168 } 1169 1170 int socket_set_cork(int fd, int v) 1171 { 1172 #if defined(SOL_TCP) && defined(TCP_CORK) 1173 return socket_setoption(fd, SOL_TCP, TCP_CORK, v); 1174 #else 1175 return 0; 1176 #endif 1177 } 1178 1179 int socket_set_nodelay(int fd) 1180 { 1181 return socket_setoption(fd, IPPROTO_TCP, TCP_NODELAY, 1); 1182 } 1183 1184 int socket_set_ipv6only(int fd) 1185 { 1186 /* IPV6_ONLY is only supported since Vista on Windows, 1187 * and the Mingw headers lack its definition anyway. 1188 */ 1189 #if defined(_WIN32) && !defined(IPV6_V6ONLY) 1190 return 0; 1191 #else 1192 return socket_setoption(fd, IPPROTO_IPV6, IPV6_V6ONLY, 1); 1193 #endif 1194 } 1195 1196 1197 int socket_get_error(int fd) 1198 { 1199 return socket_getoption(fd, SOL_SOCKET, SO_ERROR, -1); 1200 } 1201 1202 #ifdef _WIN32 1203 #include <stdlib.h> 1204 1205 static void socket_cleanup(void) 1206 { 1207 WSACleanup(); 1208 } 1209 1210 int socket_init(void) 1211 { 1212 WSADATA Data; 1213 int ret; 1214 1215 ret = WSAStartup(MAKEWORD(2,2), &Data); 1216 if (ret != 0) { 1217 (void) WSAGetLastError(); 1218 return -1; 1219 } 1220 atexit(socket_cleanup); 1221 return 0; 1222 } 1223 1224 #else /* !_WIN32 */ 1225 1226 int socket_init(void) 1227 { 1228 return 0; /* nothing to do on Unix */ 1229 } 1230 1231 #endif /* !_WIN32 */ 1232 1233 #ifdef _WIN32 1234 1235 static void 1236 socket_close_handler( void* _fd ) 1237 { 1238 int fd = (int)_fd; 1239 int ret; 1240 char buff[64]; 1241 1242 /* we want to drain the read side of the socket before closing it */ 1243 do { 1244 ret = recv( fd, buff, sizeof(buff), 0 ); 1245 } while (ret < 0 && WSAGetLastError() == WSAEINTR); 1246 1247 if (ret < 0 && WSAGetLastError() == EWOULDBLOCK) 1248 return; 1249 1250 qemu_set_fd_handler( fd, NULL, NULL, NULL ); 1251 closesocket( fd ); 1252 } 1253 1254 void 1255 socket_close( int fd ) 1256 { 1257 int old_errno = errno; 1258 1259 shutdown( fd, SD_BOTH ); 1260 /* we want to drain the socket before closing it */ 1261 qemu_set_fd_handler( fd, socket_close_handler, NULL, (void*)fd ); 1262 1263 errno = old_errno; 1264 } 1265 1266 #else /* !_WIN32 */ 1267 1268 #include <unistd.h> 1269 1270 void 1271 socket_close( int fd ) 1272 { 1273 int old_errno = errno; 1274 1275 shutdown( fd, SHUT_RDWR ); 1276 IGNORE_EINTR(close( fd )); 1277 1278 errno = old_errno; 1279 } 1280 1281 #endif /* !_WIN32 */ 1282 1283 1284 static int 1285 socket_bind_server( int s, const SockAddress* to, SocketType type ) 1286 { 1287 socket_set_xreuseaddr(s); 1288 1289 if (socket_bind(s, to) < 0) { 1290 D("could not bind server socket address %s: %s", 1291 sock_address_to_string(to), errno_str); 1292 goto FAIL; 1293 } 1294 1295 if (type == SOCKET_STREAM) { 1296 if (socket_listen(s, 4) < 0) { 1297 D("could not listen server socket %s: %s", 1298 sock_address_to_string(to), errno_str); 1299 goto FAIL; 1300 } 1301 } 1302 return s; 1303 1304 FAIL: 1305 socket_close(s); 1306 return -1; 1307 } 1308 1309 1310 static int 1311 socket_connect_client( int s, const SockAddress* to ) 1312 { 1313 if (socket_connect(s, to) < 0) { 1314 D( "could not connect client socket to %s: %s\n", 1315 sock_address_to_string(to), errno_str ); 1316 socket_close(s); 1317 return -1; 1318 } 1319 1320 socket_set_nonblock( s ); 1321 return s; 1322 } 1323 1324 1325 static int 1326 socket_in_server( int address, int port, SocketType type ) 1327 { 1328 SockAddress addr; 1329 int s; 1330 1331 sock_address_init_inet( &addr, address, port ); 1332 s = socket_create_inet( type ); 1333 if (s < 0) 1334 return -1; 1335 1336 return socket_bind_server( s, &addr, type ); 1337 } 1338 1339 1340 static int 1341 socket_in_client( SockAddress* to, SocketType type ) 1342 { 1343 int s; 1344 1345 s = socket_create_inet( type ); 1346 if (s < 0) return -1; 1347 1348 return socket_connect_client( s, to ); 1349 } 1350 1351 1352 int 1353 socket_loopback_server( int port, SocketType type ) 1354 { 1355 return socket_in_server( SOCK_ADDRESS_INET_LOOPBACK, port, type ); 1356 } 1357 1358 int 1359 socket_loopback_client( int port, SocketType type ) 1360 { 1361 SockAddress addr; 1362 1363 sock_address_init_inet( &addr, SOCK_ADDRESS_INET_LOOPBACK, port ); 1364 return socket_in_client( &addr, type ); 1365 } 1366 1367 1368 int 1369 socket_network_client( const char* host, int port, SocketType type ) 1370 { 1371 SockAddress addr; 1372 1373 if (sock_address_init_resolve( &addr, host, port, 0) < 0) 1374 return -1; 1375 1376 return socket_in_client( &addr, type ); 1377 } 1378 1379 1380 int 1381 socket_anyaddr_server( int port, SocketType type ) 1382 { 1383 return socket_in_server( SOCK_ADDRESS_INET_ANY, port, type ); 1384 } 1385 1386 int 1387 socket_accept_any( int server_fd ) 1388 { 1389 int fd; 1390 1391 QSOCKET_CALL(fd, accept( server_fd, NULL, 0 )); 1392 if (fd < 0) { 1393 D( "could not accept client connection from fd %d: %s", 1394 server_fd, errno_str ); 1395 return -1; 1396 } 1397 1398 /* set to non-blocking */ 1399 socket_set_nonblock( fd ); 1400 return fd; 1401 } 1402 1403 1404 #if HAVE_UNIX_SOCKETS 1405 1406 int 1407 socket_unix_server( const char* name, SocketType type ) 1408 { 1409 SockAddress addr; 1410 int s, ret; 1411 1412 s = socket_create_unix( type ); 1413 if (s < 0) 1414 return -1; 1415 1416 sock_address_init_unix( &addr, name ); 1417 1418 HANDLE_EINTR(unlink(name)); 1419 1420 ret = socket_bind_server( s, &addr, type ); 1421 1422 sock_address_done( &addr ); 1423 return ret; 1424 } 1425 1426 int 1427 socket_unix_client( const char* name, SocketType type ) 1428 { 1429 SockAddress addr; 1430 int s, ret; 1431 1432 s = socket_create_unix(type); 1433 if (s < 0) 1434 return -1; 1435 1436 sock_address_init_unix( &addr, name ); 1437 1438 ret = socket_connect_client( s, &addr ); 1439 1440 sock_address_done( &addr ); 1441 return ret; 1442 } 1443 1444 #endif /* HAVE_UNIX_SOCKETS */ 1445 1446 1447 1448 int 1449 socket_pair(int *fd1, int *fd2) 1450 { 1451 #ifndef _WIN32 1452 int fds[2]; 1453 int ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds); 1454 1455 if (!ret) { 1456 socket_set_nonblock(fds[0]); 1457 socket_set_nonblock(fds[1]); 1458 *fd1 = fds[0]; 1459 *fd2 = fds[1]; 1460 } 1461 return ret; 1462 #else /* _WIN32 */ 1463 /* on Windows, select() only works with network sockets, which 1464 * means we absolutely cannot use Win32 PIPEs to implement 1465 * socket pairs with the current event loop implementation. 1466 * We're going to do like Cygwin: create a random pair 1467 * of localhost TCP sockets and connect them together 1468 */ 1469 int s0, s1, s2, port; 1470 struct sockaddr_in sockin; 1471 socklen_t len; 1472 1473 /* first, create the 'server' socket. 1474 * a port number of 0 means 'any port between 1024 and 5000. 1475 * see Winsock bind() documentation for details */ 1476 s0 = socket_loopback_server( 0, SOCK_STREAM ); 1477 if (s0 < 0) 1478 return -1; 1479 1480 /* now connect a client socket to it, we first need to 1481 * extract the server socket's port number */ 1482 len = sizeof sockin; 1483 if (getsockname(s0, (struct sockaddr*) &sockin, &len) < 0) { 1484 closesocket (s0); 1485 return -1; 1486 } 1487 1488 port = ntohs(sockin.sin_port); 1489 s2 = socket_loopback_client( port, SOCK_STREAM ); 1490 if (s2 < 0) { 1491 closesocket(s0); 1492 return -1; 1493 } 1494 1495 /* we need to accept the connection on the server socket 1496 * this will create the second socket for the pair 1497 */ 1498 len = sizeof sockin; 1499 s1 = accept(s0, (struct sockaddr*) &sockin, &len); 1500 if (s1 == INVALID_SOCKET) { 1501 closesocket (s0); 1502 closesocket (s2); 1503 return -1; 1504 } 1505 socket_set_nonblock(s1); 1506 1507 /* close server socket */ 1508 closesocket(s0); 1509 *fd1 = s1; 1510 *fd2 = s2; 1511 return 0; 1512 #endif /* _WIN32 */ 1513 } 1514 1515 1516 1517 int 1518 socket_mcast_inet_add_membership( int s, uint32_t ip ) 1519 { 1520 struct ip_mreq imr; 1521 1522 imr.imr_multiaddr.s_addr = htonl(ip); 1523 imr.imr_interface.s_addr = htonl(INADDR_ANY); 1524 1525 if ( setsockopt( s, IPPROTO_IP, IP_ADD_MEMBERSHIP, 1526 (const char *)&imr, 1527 sizeof(struct ip_mreq)) < 0 ) 1528 { 1529 return fix_errno(); 1530 } 1531 return 0; 1532 } 1533 1534 int 1535 socket_mcast_inet_drop_membership( int s, uint32_t ip ) 1536 { 1537 struct ip_mreq imr; 1538 1539 imr.imr_multiaddr.s_addr = htonl(ip); 1540 imr.imr_interface.s_addr = htonl(INADDR_ANY); 1541 1542 if ( setsockopt( s, IPPROTO_IP, IP_DROP_MEMBERSHIP, 1543 (const char *)&imr, 1544 sizeof(struct ip_mreq)) < 0 ) 1545 { 1546 return fix_errno(); 1547 } 1548 return 0; 1549 } 1550 1551 int 1552 socket_mcast_inet_set_loop( int s, int enabled ) 1553 { 1554 return socket_setoption( s, IPPROTO_IP, IP_MULTICAST_LOOP, !!enabled ); 1555 } 1556 1557 int 1558 socket_mcast_inet_set_ttl( int s, int ttl ) 1559 { 1560 return socket_setoption( s, IPPROTO_IP, IP_MULTICAST_TTL, ttl ); 1561 } 1562 1563 1564 char* 1565 host_name( void ) 1566 { 1567 static char buf[256]; /* 255 is the max host name length supported by DNS */ 1568 int ret; 1569 1570 QSOCKET_CALL(ret, gethostname(buf, sizeof(buf))); 1571 1572 if (ret < 0) 1573 return "localhost"; 1574 else 1575 return buf; 1576 } 1577 1578 1579 // Temporary work-arounds until we get rid of this source file. 1580 1581 int qemu_getsockopt(int sock, int level, int optname, void* optval, 1582 size_t* optlen) { 1583 socklen_t len = (socklen_t) *optlen; 1584 int ret = getsockopt(sock, level, optname, (char*)optval, &len); 1585 *optlen = (size_t) len; 1586 return ret; 1587 } 1588 1589 int qemu_setsockopt(int sock, int level, int optname, const void* optval, 1590 size_t optlen) { 1591 return setsockopt(sock, level, optname, (const char*)optval, 1592 (socklen_t)optlen); 1593 } 1594 1595 int qemu_recv(int sock, void* buf, size_t len, int flags) { 1596 return recv(sock, buf, len, flags); 1597 } 1598