]>
Commit | Line | Data |
---|---|---|
9b589ffb DB |
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 | ||
21 | #include "qemu/osdep.h" | |
22 | #include "qemu-common.h" | |
23 | #include "qemu/sockets.h" | |
24 | #include "socket-helpers.h" | |
25 | ||
26 | #ifndef AI_ADDRCONFIG | |
27 | # define AI_ADDRCONFIG 0 | |
28 | #endif | |
29 | #ifndef EAI_ADDRFAMILY | |
30 | # define EAI_ADDRFAMILY 0 | |
31 | #endif | |
32 | ||
71714178 MAL |
33 | /* |
34 | * @hostname: a DNS name or numeric IP address | |
35 | * | |
36 | * Check whether it is possible to bind & connect to ports | |
37 | * on the DNS name or IP address @hostname. If an IP address | |
38 | * is used, it must not be a wildcard address. | |
39 | * | |
40 | * Returns 0 on success, -1 on error with errno set | |
41 | */ | |
42 | static int socket_can_bind_connect(const char *hostname, int family) | |
9b589ffb | 43 | { |
abd983c0 | 44 | int lfd = -1, cfd = -1, afd = -1; |
9b589ffb | 45 | struct addrinfo ai, *res = NULL; |
abd983c0 DB |
46 | struct sockaddr_storage ss; |
47 | socklen_t sslen = sizeof(ss); | |
48 | int soerr; | |
49 | socklen_t soerrlen = sizeof(soerr); | |
50 | bool check_soerr = false; | |
9b589ffb DB |
51 | int rc; |
52 | int ret = -1; | |
53 | ||
54 | memset(&ai, 0, sizeof(ai)); | |
55 | ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG; | |
71714178 | 56 | ai.ai_family = family; |
9b589ffb DB |
57 | ai.ai_socktype = SOCK_STREAM; |
58 | ||
59 | /* lookup */ | |
60 | rc = getaddrinfo(hostname, NULL, &ai, &res); | |
61 | if (rc != 0) { | |
62 | if (rc == EAI_ADDRFAMILY || | |
63 | rc == EAI_FAMILY) { | |
64 | errno = EADDRNOTAVAIL; | |
65 | } else { | |
66 | errno = EINVAL; | |
67 | } | |
68 | goto cleanup; | |
69 | } | |
70 | ||
abd983c0 DB |
71 | lfd = qemu_socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
72 | if (lfd < 0) { | |
9b589ffb DB |
73 | goto cleanup; |
74 | } | |
75 | ||
abd983c0 DB |
76 | cfd = qemu_socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
77 | if (cfd < 0) { | |
9b589ffb DB |
78 | goto cleanup; |
79 | } | |
80 | ||
abd983c0 DB |
81 | if (bind(lfd, res->ai_addr, res->ai_addrlen) < 0) { |
82 | goto cleanup; | |
83 | } | |
84 | ||
85 | if (listen(lfd, 1) < 0) { | |
86 | goto cleanup; | |
87 | } | |
88 | ||
89 | if (getsockname(lfd, (struct sockaddr *)&ss, &sslen) < 0) { | |
90 | goto cleanup; | |
91 | } | |
92 | ||
93 | qemu_set_nonblock(cfd); | |
94 | if (connect(cfd, (struct sockaddr *)&ss, sslen) < 0) { | |
95 | if (errno == EINPROGRESS) { | |
96 | check_soerr = true; | |
97 | } else { | |
98 | goto cleanup; | |
99 | } | |
100 | } | |
101 | ||
102 | sslen = sizeof(ss); | |
103 | afd = accept(lfd, (struct sockaddr *)&ss, &sslen); | |
104 | if (afd < 0) { | |
105 | goto cleanup; | |
106 | } | |
107 | ||
108 | if (check_soerr) { | |
109 | if (qemu_getsockopt(cfd, SOL_SOCKET, SO_ERROR, &soerr, &soerrlen) < 0) { | |
110 | goto cleanup; | |
111 | } | |
112 | if (soerr) { | |
113 | errno = soerr; | |
114 | goto cleanup; | |
115 | } | |
116 | } | |
117 | ||
9b589ffb DB |
118 | ret = 0; |
119 | ||
120 | cleanup: | |
abd983c0 DB |
121 | if (afd != -1) { |
122 | close(afd); | |
123 | } | |
124 | if (cfd != -1) { | |
125 | close(cfd); | |
126 | } | |
127 | if (lfd != -1) { | |
128 | close(lfd); | |
9b589ffb DB |
129 | } |
130 | if (res) { | |
131 | freeaddrinfo(res); | |
132 | } | |
133 | return ret; | |
134 | } | |
135 | ||
136 | ||
137 | int socket_check_protocol_support(bool *has_ipv4, bool *has_ipv6) | |
138 | { | |
139 | *has_ipv4 = *has_ipv6 = false; | |
140 | ||
71714178 | 141 | if (socket_can_bind_connect("127.0.0.1", PF_INET) < 0) { |
9b589ffb DB |
142 | if (errno != EADDRNOTAVAIL) { |
143 | return -1; | |
144 | } | |
145 | } else { | |
146 | *has_ipv4 = true; | |
147 | } | |
148 | ||
71714178 | 149 | if (socket_can_bind_connect("::1", PF_INET6) < 0) { |
9b589ffb DB |
150 | if (errno != EADDRNOTAVAIL) { |
151 | return -1; | |
152 | } | |
153 | } else { | |
154 | *has_ipv6 = true; | |
155 | } | |
156 | ||
157 | return 0; | |
158 | } |