socket-helpers.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Helper functions for tests using sockets
  3. *
  4. * Copyright 2015-2018 Red Hat, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 or
  9. * (at your option) version 3 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qemu-common.h"
  22. #include "qemu/sockets.h"
  23. #include "socket-helpers.h"
  24. #ifndef AI_ADDRCONFIG
  25. # define AI_ADDRCONFIG 0
  26. #endif
  27. #ifndef EAI_ADDRFAMILY
  28. # define EAI_ADDRFAMILY 0
  29. #endif
  30. /*
  31. * @hostname: a DNS name or numeric IP address
  32. *
  33. * Check whether it is possible to bind & connect to ports
  34. * on the DNS name or IP address @hostname. If an IP address
  35. * is used, it must not be a wildcard address.
  36. *
  37. * Returns 0 on success, -1 on error with errno set
  38. */
  39. static int socket_can_bind_connect(const char *hostname, int family)
  40. {
  41. int lfd = -1, cfd = -1, afd = -1;
  42. struct addrinfo ai, *res = NULL;
  43. struct sockaddr_storage ss;
  44. socklen_t sslen = sizeof(ss);
  45. int soerr;
  46. socklen_t soerrlen = sizeof(soerr);
  47. bool check_soerr = false;
  48. int rc;
  49. int ret = -1;
  50. memset(&ai, 0, sizeof(ai));
  51. ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
  52. ai.ai_family = family;
  53. ai.ai_socktype = SOCK_STREAM;
  54. /* lookup */
  55. rc = getaddrinfo(hostname, NULL, &ai, &res);
  56. if (rc != 0) {
  57. if (rc == EAI_ADDRFAMILY ||
  58. rc == EAI_FAMILY) {
  59. errno = EADDRNOTAVAIL;
  60. } else {
  61. errno = EINVAL;
  62. }
  63. goto cleanup;
  64. }
  65. lfd = qemu_socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  66. if (lfd < 0) {
  67. goto cleanup;
  68. }
  69. cfd = qemu_socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  70. if (cfd < 0) {
  71. goto cleanup;
  72. }
  73. if (bind(lfd, res->ai_addr, res->ai_addrlen) < 0) {
  74. goto cleanup;
  75. }
  76. if (listen(lfd, 1) < 0) {
  77. goto cleanup;
  78. }
  79. if (getsockname(lfd, (struct sockaddr *)&ss, &sslen) < 0) {
  80. goto cleanup;
  81. }
  82. qemu_set_nonblock(cfd);
  83. if (connect(cfd, (struct sockaddr *)&ss, sslen) < 0) {
  84. if (errno == EINPROGRESS) {
  85. check_soerr = true;
  86. } else {
  87. goto cleanup;
  88. }
  89. }
  90. sslen = sizeof(ss);
  91. afd = accept(lfd, (struct sockaddr *)&ss, &sslen);
  92. if (afd < 0) {
  93. goto cleanup;
  94. }
  95. if (check_soerr) {
  96. if (qemu_getsockopt(cfd, SOL_SOCKET, SO_ERROR, &soerr, &soerrlen) < 0) {
  97. goto cleanup;
  98. }
  99. if (soerr) {
  100. errno = soerr;
  101. goto cleanup;
  102. }
  103. }
  104. ret = 0;
  105. cleanup:
  106. if (afd != -1) {
  107. close(afd);
  108. }
  109. if (cfd != -1) {
  110. close(cfd);
  111. }
  112. if (lfd != -1) {
  113. close(lfd);
  114. }
  115. if (res) {
  116. freeaddrinfo(res);
  117. }
  118. return ret;
  119. }
  120. int socket_check_protocol_support(bool *has_ipv4, bool *has_ipv6)
  121. {
  122. *has_ipv4 = *has_ipv6 = false;
  123. if (socket_can_bind_connect("127.0.0.1", PF_INET) < 0) {
  124. if (errno != EADDRNOTAVAIL) {
  125. return -1;
  126. }
  127. } else {
  128. *has_ipv4 = true;
  129. }
  130. if (socket_can_bind_connect("::1", PF_INET6) < 0) {
  131. if (errno != EADDRNOTAVAIL) {
  132. return -1;
  133. }
  134. } else {
  135. *has_ipv6 = true;
  136. }
  137. return 0;
  138. }