]>
Commit | Line | Data |
---|---|---|
305b0eb2 AL |
1 | /* |
2 | * inet and unix socket functions for qemu | |
3 | * | |
4 | * (c) 2008 Gerd Hoffmann <[email protected]> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; under version 2 of the License. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
e6d91ab6 PB |
14 | * |
15 | * Contributions after 2012-01-13 are licensed under the terms of the | |
16 | * GNU GPL, version 2 or (at your option) any later version. | |
305b0eb2 | 17 | */ |
d247d25f AL |
18 | #include <stdio.h> |
19 | #include <stdlib.h> | |
20 | #include <string.h> | |
21 | #include <ctype.h> | |
22 | #include <errno.h> | |
23 | #include <unistd.h> | |
24 | ||
101f9cbc | 25 | #include "monitor.h" |
d247d25f | 26 | #include "qemu_socket.h" |
47398b9c | 27 | #include "qemu-common.h" /* for qemu_isdigit */ |
233aa5c2 | 28 | #include "main-loop.h" |
d247d25f AL |
29 | |
30 | #ifndef AI_ADDRCONFIG | |
31 | # define AI_ADDRCONFIG 0 | |
32 | #endif | |
33 | ||
d247d25f AL |
34 | static const int on=1, off=0; |
35 | ||
2af2bf67 | 36 | /* used temporarely until all users are converted to QemuOpts */ |
c1390903 | 37 | static QemuOptsList dummy_opts = { |
2af2bf67 | 38 | .name = "dummy", |
72cf2d4f | 39 | .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head), |
2af2bf67 GH |
40 | .desc = { |
41 | { | |
42 | .name = "path", | |
43 | .type = QEMU_OPT_STRING, | |
f4c94c7c GH |
44 | },{ |
45 | .name = "host", | |
46 | .type = QEMU_OPT_STRING, | |
47 | },{ | |
48 | .name = "port", | |
49 | .type = QEMU_OPT_STRING, | |
50 | },{ | |
51 | .name = "to", | |
52 | .type = QEMU_OPT_NUMBER, | |
53 | },{ | |
54 | .name = "ipv4", | |
55 | .type = QEMU_OPT_BOOL, | |
56 | },{ | |
57 | .name = "ipv6", | |
58 | .type = QEMU_OPT_BOOL, | |
2af2bf67 GH |
59 | }, |
60 | { /* end if list */ } | |
61 | }, | |
62 | }; | |
63 | ||
d247d25f AL |
64 | static int inet_getport(struct addrinfo *e) |
65 | { | |
66 | struct sockaddr_in *i4; | |
67 | struct sockaddr_in6 *i6; | |
68 | ||
69 | switch (e->ai_family) { | |
70 | case PF_INET6: | |
71 | i6 = (void*)e->ai_addr; | |
72 | return ntohs(i6->sin6_port); | |
73 | case PF_INET: | |
74 | i4 = (void*)e->ai_addr; | |
75 | return ntohs(i4->sin_port); | |
76 | default: | |
77 | return 0; | |
78 | } | |
79 | } | |
80 | ||
81 | static void inet_setport(struct addrinfo *e, int port) | |
82 | { | |
83 | struct sockaddr_in *i4; | |
84 | struct sockaddr_in6 *i6; | |
85 | ||
86 | switch (e->ai_family) { | |
87 | case PF_INET6: | |
88 | i6 = (void*)e->ai_addr; | |
89 | i6->sin6_port = htons(port); | |
90 | break; | |
91 | case PF_INET: | |
92 | i4 = (void*)e->ai_addr; | |
93 | i4->sin_port = htons(port); | |
94 | break; | |
95 | } | |
96 | } | |
97 | ||
c9c4b34e | 98 | const char *inet_strfamily(int family) |
d247d25f AL |
99 | { |
100 | switch (family) { | |
101 | case PF_INET6: return "ipv6"; | |
102 | case PF_INET: return "ipv4"; | |
103 | case PF_UNIX: return "unix"; | |
104 | } | |
c445321e | 105 | return "unknown"; |
d247d25f AL |
106 | } |
107 | ||
029409e5 | 108 | int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp) |
d247d25f AL |
109 | { |
110 | struct addrinfo ai,*res,*e; | |
e5bc776f | 111 | const char *addr; |
d247d25f AL |
112 | char port[33]; |
113 | char uaddr[INET6_ADDRSTRLEN+1]; | |
114 | char uport[33]; | |
877691f9 | 115 | int slisten, rc, to, port_min, port_max, p; |
d247d25f AL |
116 | |
117 | memset(&ai,0, sizeof(ai)); | |
118 | ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; | |
119 | ai.ai_family = PF_UNSPEC; | |
e5bc776f | 120 | ai.ai_socktype = SOCK_STREAM; |
d247d25f | 121 | |
e23a22e6 JO |
122 | if ((qemu_opt_get(opts, "host") == NULL) || |
123 | (qemu_opt_get(opts, "port") == NULL)) { | |
a12fb8ad | 124 | error_setg(errp, "host and/or port not specified"); |
e5bc776f | 125 | return -1; |
d247d25f | 126 | } |
e5bc776f GH |
127 | pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port")); |
128 | addr = qemu_opt_get(opts, "host"); | |
d247d25f | 129 | |
e5bc776f GH |
130 | to = qemu_opt_get_number(opts, "to", 0); |
131 | if (qemu_opt_get_bool(opts, "ipv4", 0)) | |
d247d25f | 132 | ai.ai_family = PF_INET; |
e5bc776f | 133 | if (qemu_opt_get_bool(opts, "ipv6", 0)) |
d247d25f AL |
134 | ai.ai_family = PF_INET6; |
135 | ||
136 | /* lookup */ | |
137 | if (port_offset) | |
138 | snprintf(port, sizeof(port), "%d", atoi(port) + port_offset); | |
139 | rc = getaddrinfo(strlen(addr) ? addr : NULL, port, &ai, &res); | |
140 | if (rc != 0) { | |
a12fb8ad PB |
141 | error_setg(errp, "address resolution failed for %s:%s: %s", addr, port, |
142 | gai_strerror(rc)); | |
d247d25f AL |
143 | return -1; |
144 | } | |
d247d25f AL |
145 | |
146 | /* create socket + bind */ | |
147 | for (e = res; e != NULL; e = e->ai_next) { | |
39b6efc8 VS |
148 | getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen, |
149 | uaddr,INET6_ADDRSTRLEN,uport,32, | |
150 | NI_NUMERICHOST | NI_NUMERICSERV); | |
40ff6d7e | 151 | slisten = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol); |
39b6efc8 | 152 | if (slisten < 0) { |
029409e5 | 153 | if (!e->ai_next) { |
a12fb8ad | 154 | error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED); |
029409e5 | 155 | } |
39b6efc8 VS |
156 | continue; |
157 | } | |
d247d25f AL |
158 | |
159 | setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); | |
160 | #ifdef IPV6_V6ONLY | |
161 | if (e->ai_family == PF_INET6) { | |
162 | /* listen on both ipv4 and ipv6 */ | |
39b6efc8 VS |
163 | setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off, |
164 | sizeof(off)); | |
d247d25f AL |
165 | } |
166 | #endif | |
167 | ||
877691f9 MA |
168 | port_min = inet_getport(e); |
169 | port_max = to ? to + port_offset : port_min; | |
170 | for (p = port_min; p <= port_max; p++) { | |
171 | inet_setport(e, p); | |
d247d25f | 172 | if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) { |
d247d25f AL |
173 | goto listen; |
174 | } | |
877691f9 | 175 | if (p == port_max) { |
029409e5 | 176 | if (!e->ai_next) { |
a12fb8ad | 177 | error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED); |
029409e5 | 178 | } |
d247d25f | 179 | } |
d247d25f AL |
180 | } |
181 | closesocket(slisten); | |
182 | } | |
d247d25f AL |
183 | freeaddrinfo(res); |
184 | return -1; | |
185 | ||
186 | listen: | |
187 | if (listen(slisten,1) != 0) { | |
a12fb8ad | 188 | error_set_errno(errp, errno, QERR_SOCKET_LISTEN_FAILED); |
d247d25f | 189 | closesocket(slisten); |
39b6efc8 | 190 | freeaddrinfo(res); |
d247d25f AL |
191 | return -1; |
192 | } | |
e5bc776f GH |
193 | snprintf(uport, sizeof(uport), "%d", inet_getport(e) - port_offset); |
194 | qemu_opt_set(opts, "host", uaddr); | |
195 | qemu_opt_set(opts, "port", uport); | |
196 | qemu_opt_set(opts, "ipv6", (e->ai_family == PF_INET6) ? "on" : "off"); | |
197 | qemu_opt_set(opts, "ipv4", (e->ai_family != PF_INET6) ? "on" : "off"); | |
d247d25f AL |
198 | freeaddrinfo(res); |
199 | return slisten; | |
200 | } | |
201 | ||
05bc1d8a MT |
202 | #ifdef _WIN32 |
203 | #define QEMU_SOCKET_RC_INPROGRESS(rc) \ | |
204 | ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY) | |
205 | #else | |
206 | #define QEMU_SOCKET_RC_INPROGRESS(rc) \ | |
207 | ((rc) == -EINPROGRESS) | |
208 | #endif | |
209 | ||
233aa5c2 OW |
210 | /* Struct to store connect state for non blocking connect */ |
211 | typedef struct ConnectState { | |
212 | int fd; | |
213 | struct addrinfo *addr_list; | |
214 | struct addrinfo *current_addr; | |
215 | NonBlockingConnectHandler *callback; | |
216 | void *opaque; | |
217 | } ConnectState; | |
218 | ||
219 | static int inet_connect_addr(struct addrinfo *addr, bool *in_progress, | |
11663b55 | 220 | ConnectState *connect_state, Error **errp); |
233aa5c2 OW |
221 | |
222 | static void wait_for_connect(void *opaque) | |
d247d25f | 223 | { |
233aa5c2 OW |
224 | ConnectState *s = opaque; |
225 | int val = 0, rc = 0; | |
226 | socklen_t valsize = sizeof(val); | |
227 | bool in_progress; | |
228 | ||
229 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); | |
230 | ||
231 | do { | |
232 | rc = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize); | |
233 | } while (rc == -1 && socket_error() == EINTR); | |
234 | ||
235 | /* update rc to contain error */ | |
236 | if (!rc && val) { | |
237 | rc = -1; | |
238 | } | |
239 | ||
240 | /* connect error */ | |
241 | if (rc < 0) { | |
242 | closesocket(s->fd); | |
243 | s->fd = rc; | |
244 | } | |
245 | ||
246 | /* try to connect to the next address on the list */ | |
1fc05adf PB |
247 | if (s->current_addr) { |
248 | while (s->current_addr->ai_next != NULL && s->fd < 0) { | |
249 | s->current_addr = s->current_addr->ai_next; | |
11663b55 | 250 | s->fd = inet_connect_addr(s->current_addr, &in_progress, s, NULL); |
1fc05adf PB |
251 | /* connect in progress */ |
252 | if (in_progress) { | |
253 | return; | |
254 | } | |
233aa5c2 | 255 | } |
1fc05adf PB |
256 | |
257 | freeaddrinfo(s->addr_list); | |
233aa5c2 | 258 | } |
05bc1d8a | 259 | |
233aa5c2 OW |
260 | if (s->callback) { |
261 | s->callback(s->fd, s->opaque); | |
05bc1d8a | 262 | } |
233aa5c2 | 263 | g_free(s); |
233aa5c2 OW |
264 | } |
265 | ||
266 | static int inet_connect_addr(struct addrinfo *addr, bool *in_progress, | |
11663b55 | 267 | ConnectState *connect_state, Error **errp) |
233aa5c2 OW |
268 | { |
269 | int sock, rc; | |
270 | ||
271 | *in_progress = false; | |
05bc1d8a MT |
272 | |
273 | sock = qemu_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); | |
274 | if (sock < 0) { | |
11663b55 | 275 | error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED); |
05bc1d8a MT |
276 | return -1; |
277 | } | |
58455eb9 | 278 | qemu_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
233aa5c2 | 279 | if (connect_state != NULL) { |
05bc1d8a MT |
280 | socket_set_nonblock(sock); |
281 | } | |
282 | /* connect to peer */ | |
283 | do { | |
284 | rc = 0; | |
285 | if (connect(sock, addr->ai_addr, addr->ai_addrlen) < 0) { | |
286 | rc = -socket_error(); | |
287 | } | |
288 | } while (rc == -EINTR); | |
289 | ||
233aa5c2 OW |
290 | if (connect_state != NULL && QEMU_SOCKET_RC_INPROGRESS(rc)) { |
291 | connect_state->fd = sock; | |
292 | qemu_set_fd_handler2(sock, NULL, NULL, wait_for_connect, | |
293 | connect_state); | |
294 | *in_progress = true; | |
05bc1d8a | 295 | } else if (rc < 0) { |
11663b55 | 296 | error_set_errno(errp, errno, QERR_SOCKET_CONNECT_FAILED); |
05bc1d8a MT |
297 | closesocket(sock); |
298 | return -1; | |
299 | } | |
300 | return sock; | |
301 | } | |
302 | ||
303 | static struct addrinfo *inet_parse_connect_opts(QemuOpts *opts, Error **errp) | |
304 | { | |
305 | struct addrinfo ai, *res; | |
306 | int rc; | |
f4c94c7c GH |
307 | const char *addr; |
308 | const char *port; | |
d247d25f | 309 | |
05bc1d8a | 310 | memset(&ai, 0, sizeof(ai)); |
233aa5c2 | 311 | |
d247d25f AL |
312 | ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG; |
313 | ai.ai_family = PF_UNSPEC; | |
f4c94c7c | 314 | ai.ai_socktype = SOCK_STREAM; |
d247d25f | 315 | |
f4c94c7c GH |
316 | addr = qemu_opt_get(opts, "host"); |
317 | port = qemu_opt_get(opts, "port"); | |
318 | if (addr == NULL || port == NULL) { | |
a12fb8ad | 319 | error_setg(errp, "host and/or port not specified"); |
05bc1d8a | 320 | return NULL; |
d247d25f AL |
321 | } |
322 | ||
05bc1d8a | 323 | if (qemu_opt_get_bool(opts, "ipv4", 0)) { |
d247d25f | 324 | ai.ai_family = PF_INET; |
05bc1d8a MT |
325 | } |
326 | if (qemu_opt_get_bool(opts, "ipv6", 0)) { | |
d247d25f | 327 | ai.ai_family = PF_INET6; |
05bc1d8a | 328 | } |
d247d25f AL |
329 | |
330 | /* lookup */ | |
05bc1d8a MT |
331 | rc = getaddrinfo(addr, port, &ai, &res); |
332 | if (rc != 0) { | |
a12fb8ad PB |
333 | error_setg(errp, "address resolution failed for %s:%s: %s", addr, port, |
334 | gai_strerror(rc)); | |
05bc1d8a MT |
335 | return NULL; |
336 | } | |
337 | return res; | |
338 | } | |
339 | ||
5db5f44c OW |
340 | /** |
341 | * Create a socket and connect it to an address. | |
342 | * | |
343 | * @opts: QEMU options, recognized parameters strings "host" and "port", | |
344 | * bools "ipv4" and "ipv6". | |
5db5f44c | 345 | * @errp: set on error |
233aa5c2 OW |
346 | * @callback: callback function for non-blocking connect |
347 | * @opaque: opaque for callback function | |
5db5f44c OW |
348 | * |
349 | * Returns: -1 on error, file descriptor on success. | |
233aa5c2 OW |
350 | * |
351 | * If @callback is non-null, the connect is non-blocking. If this | |
352 | * function succeeds, callback will be called when the connection | |
353 | * completes, with the file descriptor on success, or -1 on error. | |
5db5f44c | 354 | */ |
233aa5c2 OW |
355 | int inet_connect_opts(QemuOpts *opts, Error **errp, |
356 | NonBlockingConnectHandler *callback, void *opaque) | |
05bc1d8a MT |
357 | { |
358 | struct addrinfo *res, *e; | |
359 | int sock = -1; | |
233aa5c2 OW |
360 | bool in_progress; |
361 | ConnectState *connect_state = NULL; | |
05bc1d8a MT |
362 | |
363 | res = inet_parse_connect_opts(opts, errp); | |
364 | if (!res) { | |
365 | return -1; | |
366 | } | |
367 | ||
233aa5c2 OW |
368 | if (callback != NULL) { |
369 | connect_state = g_malloc0(sizeof(*connect_state)); | |
370 | connect_state->addr_list = res; | |
371 | connect_state->callback = callback; | |
372 | connect_state->opaque = opaque; | |
d247d25f | 373 | } |
d247d25f AL |
374 | |
375 | for (e = res; e != NULL; e = e->ai_next) { | |
233aa5c2 OW |
376 | if (connect_state != NULL) { |
377 | connect_state->current_addr = e; | |
378 | } | |
11663b55 | 379 | sock = inet_connect_addr(e, &in_progress, connect_state, errp); |
233aa5c2 OW |
380 | if (in_progress) { |
381 | return sock; | |
382 | } else if (sock >= 0) { | |
383 | /* non blocking socket immediate success, call callback */ | |
384 | if (callback != NULL) { | |
385 | callback(sock, opaque); | |
386 | } | |
05bc1d8a | 387 | break; |
a6ba35b3 | 388 | } |
d247d25f | 389 | } |
233aa5c2 | 390 | g_free(connect_state); |
d247d25f | 391 | freeaddrinfo(res); |
05bc1d8a | 392 | return sock; |
d247d25f AL |
393 | } |
394 | ||
7fc4e63e | 395 | int inet_dgram_opts(QemuOpts *opts, Error **errp) |
7e1b35b4 GH |
396 | { |
397 | struct addrinfo ai, *peer = NULL, *local = NULL; | |
398 | const char *addr; | |
399 | const char *port; | |
7e1b35b4 GH |
400 | int sock = -1, rc; |
401 | ||
402 | /* lookup peer addr */ | |
403 | memset(&ai,0, sizeof(ai)); | |
404 | ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG; | |
405 | ai.ai_family = PF_UNSPEC; | |
406 | ai.ai_socktype = SOCK_DGRAM; | |
407 | ||
408 | addr = qemu_opt_get(opts, "host"); | |
409 | port = qemu_opt_get(opts, "port"); | |
410 | if (addr == NULL || strlen(addr) == 0) { | |
411 | addr = "localhost"; | |
412 | } | |
413 | if (port == NULL || strlen(port) == 0) { | |
4f085c82 | 414 | error_setg(errp, "remote port not specified"); |
7e1b35b4 GH |
415 | return -1; |
416 | } | |
417 | ||
418 | if (qemu_opt_get_bool(opts, "ipv4", 0)) | |
419 | ai.ai_family = PF_INET; | |
420 | if (qemu_opt_get_bool(opts, "ipv6", 0)) | |
421 | ai.ai_family = PF_INET6; | |
422 | ||
423 | if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) { | |
4f085c82 PB |
424 | error_setg(errp, "address resolution failed for %s:%s: %s", addr, port, |
425 | gai_strerror(rc)); | |
7e1b35b4 GH |
426 | return -1; |
427 | } | |
7e1b35b4 GH |
428 | |
429 | /* lookup local addr */ | |
430 | memset(&ai,0, sizeof(ai)); | |
431 | ai.ai_flags = AI_PASSIVE; | |
432 | ai.ai_family = peer->ai_family; | |
433 | ai.ai_socktype = SOCK_DGRAM; | |
434 | ||
435 | addr = qemu_opt_get(opts, "localaddr"); | |
436 | port = qemu_opt_get(opts, "localport"); | |
437 | if (addr == NULL || strlen(addr) == 0) { | |
438 | addr = NULL; | |
439 | } | |
440 | if (!port || strlen(port) == 0) | |
441 | port = "0"; | |
442 | ||
443 | if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) { | |
4f085c82 PB |
444 | error_setg(errp, "address resolution failed for %s:%s: %s", addr, port, |
445 | gai_strerror(rc)); | |
39b38459 | 446 | goto err; |
7e1b35b4 | 447 | } |
7e1b35b4 GH |
448 | |
449 | /* create socket */ | |
40ff6d7e | 450 | sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol); |
7e1b35b4 | 451 | if (sock < 0) { |
4f085c82 | 452 | error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED); |
7e1b35b4 GH |
453 | goto err; |
454 | } | |
455 | setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); | |
456 | ||
457 | /* bind socket */ | |
7e1b35b4 | 458 | if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) { |
4f085c82 | 459 | error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED); |
7e1b35b4 GH |
460 | goto err; |
461 | } | |
462 | ||
463 | /* connect to peer */ | |
7e1b35b4 | 464 | if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) { |
4f085c82 | 465 | error_set_errno(errp, errno, QERR_SOCKET_CONNECT_FAILED); |
7e1b35b4 GH |
466 | goto err; |
467 | } | |
468 | ||
469 | freeaddrinfo(local); | |
470 | freeaddrinfo(peer); | |
471 | return sock; | |
472 | ||
473 | err: | |
474 | if (-1 != sock) | |
475 | closesocket(sock); | |
476 | if (local) | |
477 | freeaddrinfo(local); | |
478 | if (peer) | |
479 | freeaddrinfo(peer); | |
480 | return -1; | |
481 | } | |
482 | ||
f4c94c7c | 483 | /* compatibility wrapper */ |
879e45c7 | 484 | static InetSocketAddress *inet_parse(const char *str, Error **errp) |
f4c94c7c | 485 | { |
879e45c7 | 486 | InetSocketAddress *addr; |
f4c94c7c | 487 | const char *optstr, *h; |
879e45c7 | 488 | char host[64]; |
f4c94c7c | 489 | char port[33]; |
879e45c7 | 490 | int to; |
f4c94c7c GH |
491 | int pos; |
492 | ||
879e45c7 PB |
493 | addr = g_new0(InetSocketAddress, 1); |
494 | ||
f4c94c7c GH |
495 | /* parse address */ |
496 | if (str[0] == ':') { | |
497 | /* no host given */ | |
879e45c7 PB |
498 | host[0] = '\0'; |
499 | if (1 != sscanf(str, ":%32[^,]%n", port, &pos)) { | |
2f002c43 | 500 | error_setg(errp, "error parsing port in address '%s'", str); |
879e45c7 | 501 | goto fail; |
f4c94c7c GH |
502 | } |
503 | } else if (str[0] == '[') { | |
504 | /* IPv6 addr */ | |
879e45c7 | 505 | if (2 != sscanf(str, "[%64[^]]]:%32[^,]%n", host, port, &pos)) { |
2f002c43 | 506 | error_setg(errp, "error parsing IPv6 address '%s'", str); |
879e45c7 | 507 | goto fail; |
f4c94c7c | 508 | } |
879e45c7 | 509 | addr->ipv6 = addr->has_ipv6 = true; |
f4c94c7c GH |
510 | } else if (qemu_isdigit(str[0])) { |
511 | /* IPv4 addr */ | |
879e45c7 | 512 | if (2 != sscanf(str, "%64[0-9.]:%32[^,]%n", host, port, &pos)) { |
2f002c43 | 513 | error_setg(errp, "error parsing IPv4 address '%s'", str); |
879e45c7 | 514 | goto fail; |
f4c94c7c | 515 | } |
879e45c7 | 516 | addr->ipv4 = addr->has_ipv4 = true; |
f4c94c7c GH |
517 | } else { |
518 | /* hostname */ | |
879e45c7 | 519 | if (2 != sscanf(str, "%64[^:]:%32[^,]%n", host, port, &pos)) { |
2f002c43 | 520 | error_setg(errp, "error parsing address '%s'", str); |
879e45c7 | 521 | goto fail; |
f4c94c7c GH |
522 | } |
523 | } | |
879e45c7 PB |
524 | |
525 | addr->host = g_strdup(host); | |
526 | addr->port = g_strdup(port); | |
f4c94c7c GH |
527 | |
528 | /* parse options */ | |
529 | optstr = str + pos; | |
530 | h = strstr(optstr, ",to="); | |
879e45c7 | 531 | if (h) { |
1ccbc285 AP |
532 | h += 4; |
533 | if (sscanf(h, "%d%n", &to, &pos) != 1 || | |
534 | (h[pos] != '\0' && h[pos] != ',')) { | |
879e45c7 PB |
535 | error_setg(errp, "error parsing to= argument"); |
536 | goto fail; | |
537 | } | |
538 | addr->has_to = true; | |
539 | addr->to = to; | |
540 | } | |
541 | if (strstr(optstr, ",ipv4")) { | |
542 | addr->ipv4 = addr->has_ipv4 = true; | |
543 | } | |
544 | if (strstr(optstr, ",ipv6")) { | |
545 | addr->ipv6 = addr->has_ipv6 = true; | |
546 | } | |
547 | return addr; | |
548 | ||
549 | fail: | |
550 | qapi_free_InetSocketAddress(addr); | |
551 | return NULL; | |
552 | } | |
553 | ||
554 | static void inet_addr_to_opts(QemuOpts *opts, InetSocketAddress *addr) | |
555 | { | |
556 | bool ipv4 = addr->ipv4 || !addr->has_ipv4; | |
557 | bool ipv6 = addr->ipv6 || !addr->has_ipv6; | |
558 | ||
559 | if (!ipv4 || !ipv6) { | |
560 | qemu_opt_set_bool(opts, "ipv4", ipv4); | |
561 | qemu_opt_set_bool(opts, "ipv6", ipv6); | |
562 | } | |
563 | if (addr->has_to) { | |
564 | char to[20]; | |
565 | snprintf(to, sizeof(to), "%d", addr->to); | |
566 | qemu_opt_set(opts, "to", to); | |
567 | } | |
568 | qemu_opt_set(opts, "host", addr->host); | |
569 | qemu_opt_set(opts, "port", addr->port); | |
f4c94c7c GH |
570 | } |
571 | ||
e5bc776f | 572 | int inet_listen(const char *str, char *ostr, int olen, |
029409e5 | 573 | int socktype, int port_offset, Error **errp) |
e5bc776f GH |
574 | { |
575 | QemuOpts *opts; | |
576 | char *optstr; | |
577 | int sock = -1; | |
879e45c7 | 578 | InetSocketAddress *addr; |
e5bc776f | 579 | |
879e45c7 PB |
580 | addr = inet_parse(str, errp); |
581 | if (addr != NULL) { | |
582 | opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); | |
583 | inet_addr_to_opts(opts, addr); | |
584 | qapi_free_InetSocketAddress(addr); | |
029409e5 | 585 | sock = inet_listen_opts(opts, port_offset, errp); |
e5bc776f GH |
586 | if (sock != -1 && ostr) { |
587 | optstr = strchr(str, ','); | |
588 | if (qemu_opt_get_bool(opts, "ipv6", 0)) { | |
589 | snprintf(ostr, olen, "[%s]:%s%s", | |
590 | qemu_opt_get(opts, "host"), | |
591 | qemu_opt_get(opts, "port"), | |
592 | optstr ? optstr : ""); | |
593 | } else { | |
594 | snprintf(ostr, olen, "%s:%s%s", | |
595 | qemu_opt_get(opts, "host"), | |
596 | qemu_opt_get(opts, "port"), | |
597 | optstr ? optstr : ""); | |
598 | } | |
599 | } | |
879e45c7 | 600 | qemu_opts_del(opts); |
e5bc776f | 601 | } |
e5bc776f GH |
602 | return sock; |
603 | } | |
604 | ||
5db5f44c OW |
605 | /** |
606 | * Create a blocking socket and connect it to an address. | |
607 | * | |
608 | * @str: address string | |
609 | * @errp: set in case of an error | |
610 | * | |
611 | * Returns -1 in case of error, file descriptor on success | |
612 | **/ | |
613 | int inet_connect(const char *str, Error **errp) | |
f4c94c7c GH |
614 | { |
615 | QemuOpts *opts; | |
616 | int sock = -1; | |
879e45c7 | 617 | InetSocketAddress *addr; |
f4c94c7c | 618 | |
879e45c7 PB |
619 | addr = inet_parse(str, errp); |
620 | if (addr != NULL) { | |
621 | opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); | |
622 | inet_addr_to_opts(opts, addr); | |
623 | qapi_free_InetSocketAddress(addr); | |
233aa5c2 | 624 | sock = inet_connect_opts(opts, errp, NULL, NULL); |
879e45c7 | 625 | qemu_opts_del(opts); |
5db5f44c | 626 | } |
5db5f44c OW |
627 | return sock; |
628 | } | |
629 | ||
630 | /** | |
631 | * Create a non-blocking socket and connect it to an address. | |
233aa5c2 OW |
632 | * Calls the callback function with fd in case of success or -1 in case of |
633 | * error. | |
5db5f44c OW |
634 | * |
635 | * @str: address string | |
233aa5c2 OW |
636 | * @callback: callback function that is called when connect completes, |
637 | * cannot be NULL. | |
638 | * @opaque: opaque for callback function | |
5db5f44c OW |
639 | * @errp: set in case of an error |
640 | * | |
233aa5c2 | 641 | * Returns: -1 on immediate error, file descriptor on success. |
5db5f44c | 642 | **/ |
233aa5c2 OW |
643 | int inet_nonblocking_connect(const char *str, |
644 | NonBlockingConnectHandler *callback, | |
645 | void *opaque, Error **errp) | |
5db5f44c OW |
646 | { |
647 | QemuOpts *opts; | |
648 | int sock = -1; | |
879e45c7 | 649 | InetSocketAddress *addr; |
5db5f44c | 650 | |
233aa5c2 OW |
651 | g_assert(callback != NULL); |
652 | ||
879e45c7 PB |
653 | addr = inet_parse(str, errp); |
654 | if (addr != NULL) { | |
655 | opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); | |
656 | inet_addr_to_opts(opts, addr); | |
657 | qapi_free_InetSocketAddress(addr); | |
233aa5c2 | 658 | sock = inet_connect_opts(opts, errp, callback, opaque); |
879e45c7 | 659 | qemu_opts_del(opts); |
a6ba35b3 | 660 | } |
f4c94c7c GH |
661 | return sock; |
662 | } | |
663 | ||
d247d25f AL |
664 | #ifndef _WIN32 |
665 | ||
7fc4e63e | 666 | int unix_listen_opts(QemuOpts *opts, Error **errp) |
d247d25f AL |
667 | { |
668 | struct sockaddr_un un; | |
62b6adfb GH |
669 | const char *path = qemu_opt_get(opts, "path"); |
670 | int sock, fd; | |
d247d25f | 671 | |
40ff6d7e | 672 | sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0); |
d247d25f | 673 | if (sock < 0) { |
58899664 | 674 | error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED); |
39b6efc8 | 675 | return -1; |
d247d25f AL |
676 | } |
677 | ||
d247d25f AL |
678 | memset(&un, 0, sizeof(un)); |
679 | un.sun_family = AF_UNIX; | |
680 | if (path && strlen(path)) { | |
681 | snprintf(un.sun_path, sizeof(un.sun_path), "%s", path); | |
682 | } else { | |
683 | char *tmpdir = getenv("TMPDIR"); | |
684 | snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX", | |
685 | tmpdir ? tmpdir : "/tmp"); | |
686 | /* | |
687 | * This dummy fd usage silences the mktemp() unsecure warning. | |
688 | * Using mkstemp() doesn't make things more secure here | |
689 | * though. bind() complains about existing files, so we have | |
690 | * to unlink first and thus re-open the race window. The | |
691 | * worst case possible is bind() failing, i.e. a DoS attack. | |
692 | */ | |
693 | fd = mkstemp(un.sun_path); close(fd); | |
62b6adfb | 694 | qemu_opt_set(opts, "path", un.sun_path); |
d247d25f | 695 | } |
d247d25f AL |
696 | |
697 | unlink(un.sun_path); | |
698 | if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) { | |
58899664 | 699 | error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED); |
d247d25f AL |
700 | goto err; |
701 | } | |
702 | if (listen(sock, 1) < 0) { | |
58899664 | 703 | error_set_errno(errp, errno, QERR_SOCKET_LISTEN_FAILED); |
d247d25f AL |
704 | goto err; |
705 | } | |
706 | ||
d247d25f AL |
707 | return sock; |
708 | ||
709 | err: | |
d247d25f AL |
710 | closesocket(sock); |
711 | return -1; | |
712 | } | |
713 | ||
1fc05adf PB |
714 | int unix_connect_opts(QemuOpts *opts, Error **errp, |
715 | NonBlockingConnectHandler *callback, void *opaque) | |
d247d25f AL |
716 | { |
717 | struct sockaddr_un un; | |
2af2bf67 | 718 | const char *path = qemu_opt_get(opts, "path"); |
1fc05adf PB |
719 | ConnectState *connect_state = NULL; |
720 | int sock, rc; | |
d247d25f | 721 | |
2af2bf67 | 722 | if (NULL == path) { |
58899664 | 723 | error_setg(errp, "unix connect: no path specified\n"); |
2af2bf67 GH |
724 | return -1; |
725 | } | |
726 | ||
40ff6d7e | 727 | sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0); |
d247d25f | 728 | if (sock < 0) { |
58899664 | 729 | error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED); |
39b6efc8 | 730 | return -1; |
d247d25f | 731 | } |
1fc05adf PB |
732 | if (callback != NULL) { |
733 | connect_state = g_malloc0(sizeof(*connect_state)); | |
734 | connect_state->callback = callback; | |
735 | connect_state->opaque = opaque; | |
736 | socket_set_nonblock(sock); | |
737 | } | |
d247d25f AL |
738 | |
739 | memset(&un, 0, sizeof(un)); | |
740 | un.sun_family = AF_UNIX; | |
741 | snprintf(un.sun_path, sizeof(un.sun_path), "%s", path); | |
1fc05adf PB |
742 | |
743 | /* connect to peer */ | |
744 | do { | |
745 | rc = 0; | |
746 | if (connect(sock, (struct sockaddr *) &un, sizeof(un)) < 0) { | |
747 | rc = -socket_error(); | |
748 | } | |
749 | } while (rc == -EINTR); | |
750 | ||
751 | if (connect_state != NULL && QEMU_SOCKET_RC_INPROGRESS(rc)) { | |
752 | connect_state->fd = sock; | |
753 | qemu_set_fd_handler2(sock, NULL, NULL, wait_for_connect, | |
754 | connect_state); | |
755 | return sock; | |
756 | } else if (rc >= 0) { | |
757 | /* non blocking socket immediate success, call callback */ | |
758 | if (callback != NULL) { | |
759 | callback(sock, opaque); | |
760 | } | |
761 | } | |
762 | ||
763 | if (rc < 0) { | |
58899664 | 764 | error_set_errno(errp, -rc, QERR_SOCKET_CONNECT_FAILED); |
9d947472 | 765 | close(sock); |
1fc05adf | 766 | sock = -1; |
d247d25f AL |
767 | } |
768 | ||
1fc05adf | 769 | g_free(connect_state); |
d247d25f AL |
770 | return sock; |
771 | } | |
772 | ||
0c814709 PB |
773 | #else |
774 | ||
775 | int unix_listen_opts(QemuOpts *opts, Error **errp) | |
776 | { | |
58899664 | 777 | error_setg(errp, "unix sockets are not available on windows"); |
0c814709 PB |
778 | errno = ENOTSUP; |
779 | return -1; | |
780 | } | |
781 | ||
1fc05adf PB |
782 | int unix_connect_opts(QemuOpts *opts, Error **errp, |
783 | NonBlockingConnectHandler *callback, void *opaque) | |
0c814709 | 784 | { |
58899664 | 785 | error_setg(errp, "unix sockets are not available on windows"); |
0c814709 PB |
786 | errno = ENOTSUP; |
787 | return -1; | |
788 | } | |
789 | #endif | |
790 | ||
2af2bf67 | 791 | /* compatibility wrapper */ |
7fc4e63e | 792 | int unix_listen(const char *str, char *ostr, int olen, Error **errp) |
62b6adfb GH |
793 | { |
794 | QemuOpts *opts; | |
795 | char *path, *optstr; | |
796 | int sock, len; | |
797 | ||
8be7e7e4 | 798 | opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); |
62b6adfb GH |
799 | |
800 | optstr = strchr(str, ','); | |
801 | if (optstr) { | |
802 | len = optstr - str; | |
803 | if (len) { | |
7267c094 | 804 | path = g_malloc(len+1); |
62b6adfb GH |
805 | snprintf(path, len+1, "%.*s", len, str); |
806 | qemu_opt_set(opts, "path", path); | |
7267c094 | 807 | g_free(path); |
62b6adfb GH |
808 | } |
809 | } else { | |
810 | qemu_opt_set(opts, "path", str); | |
811 | } | |
812 | ||
7fc4e63e | 813 | sock = unix_listen_opts(opts, errp); |
62b6adfb GH |
814 | |
815 | if (sock != -1 && ostr) | |
816 | snprintf(ostr, olen, "%s%s", qemu_opt_get(opts, "path"), optstr ? optstr : ""); | |
817 | qemu_opts_del(opts); | |
818 | return sock; | |
819 | } | |
820 | ||
7fc4e63e | 821 | int unix_connect(const char *path, Error **errp) |
2af2bf67 GH |
822 | { |
823 | QemuOpts *opts; | |
824 | int sock; | |
825 | ||
8be7e7e4 | 826 | opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); |
2af2bf67 | 827 | qemu_opt_set(opts, "path", path); |
1fc05adf PB |
828 | sock = unix_connect_opts(opts, errp, NULL, NULL); |
829 | qemu_opts_del(opts); | |
830 | return sock; | |
831 | } | |
832 | ||
833 | ||
834 | int unix_nonblocking_connect(const char *path, | |
835 | NonBlockingConnectHandler *callback, | |
836 | void *opaque, Error **errp) | |
837 | { | |
838 | QemuOpts *opts; | |
839 | int sock = -1; | |
840 | ||
841 | g_assert(callback != NULL); | |
842 | ||
843 | opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); | |
844 | qemu_opt_set(opts, "path", path); | |
845 | sock = unix_connect_opts(opts, errp, callback, opaque); | |
2af2bf67 GH |
846 | qemu_opts_del(opts); |
847 | return sock; | |
848 | } | |
849 | ||
101f9cbc PB |
850 | SocketAddress *socket_parse(const char *str, Error **errp) |
851 | { | |
852 | SocketAddress *addr = NULL; | |
853 | ||
854 | addr = g_new(SocketAddress, 1); | |
855 | if (strstart(str, "unix:", NULL)) { | |
856 | if (str[5] == '\0') { | |
857 | error_setg(errp, "invalid Unix socket address\n"); | |
858 | goto fail; | |
859 | } else { | |
860 | addr->kind = SOCKET_ADDRESS_KIND_UNIX; | |
861 | addr->q_unix = g_new(UnixSocketAddress, 1); | |
862 | addr->q_unix->path = g_strdup(str + 5); | |
863 | } | |
864 | } else if (strstart(str, "fd:", NULL)) { | |
865 | if (str[3] == '\0') { | |
866 | error_setg(errp, "invalid file descriptor address\n"); | |
867 | goto fail; | |
868 | } else { | |
869 | addr->kind = SOCKET_ADDRESS_KIND_FD; | |
870 | addr->fd = g_new(String, 1); | |
871 | addr->fd->str = g_strdup(str + 3); | |
872 | } | |
873 | } else { | |
874 | addr->kind = SOCKET_ADDRESS_KIND_INET; | |
875 | addr->inet = g_new(InetSocketAddress, 1); | |
876 | addr->inet = inet_parse(str, errp); | |
877 | if (addr->inet == NULL) { | |
878 | goto fail; | |
879 | } | |
880 | } | |
881 | return addr; | |
882 | ||
883 | fail: | |
884 | qapi_free_SocketAddress(addr); | |
885 | return NULL; | |
886 | } | |
887 | ||
888 | int socket_connect(SocketAddress *addr, Error **errp, | |
889 | NonBlockingConnectHandler *callback, void *opaque) | |
890 | { | |
891 | QemuOpts *opts; | |
892 | int fd; | |
893 | ||
894 | opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); | |
895 | switch (addr->kind) { | |
896 | case SOCKET_ADDRESS_KIND_INET: | |
897 | inet_addr_to_opts(opts, addr->inet); | |
898 | fd = inet_connect_opts(opts, errp, callback, opaque); | |
899 | break; | |
900 | ||
901 | case SOCKET_ADDRESS_KIND_UNIX: | |
902 | qemu_opt_set(opts, "path", addr->q_unix->path); | |
903 | fd = unix_connect_opts(opts, errp, callback, opaque); | |
904 | break; | |
905 | ||
906 | case SOCKET_ADDRESS_KIND_FD: | |
907 | fd = monitor_get_fd(cur_mon, addr->fd->str, errp); | |
908 | if (callback) { | |
909 | callback(fd, opaque); | |
910 | } | |
911 | break; | |
912 | ||
913 | default: | |
914 | abort(); | |
915 | } | |
916 | qemu_opts_del(opts); | |
917 | return fd; | |
918 | } | |
919 | ||
920 | int socket_listen(SocketAddress *addr, Error **errp) | |
921 | { | |
922 | QemuOpts *opts; | |
923 | int fd; | |
924 | ||
925 | opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); | |
926 | switch (addr->kind) { | |
927 | case SOCKET_ADDRESS_KIND_INET: | |
928 | inet_addr_to_opts(opts, addr->inet); | |
929 | fd = inet_listen_opts(opts, 0, errp); | |
930 | break; | |
931 | ||
932 | case SOCKET_ADDRESS_KIND_UNIX: | |
933 | qemu_opt_set(opts, "path", addr->q_unix->path); | |
934 | fd = unix_listen_opts(opts, errp); | |
935 | break; | |
936 | ||
937 | case SOCKET_ADDRESS_KIND_FD: | |
938 | fd = monitor_get_fd(cur_mon, addr->fd->str, errp); | |
939 | break; | |
940 | ||
941 | default: | |
942 | abort(); | |
943 | } | |
944 | qemu_opts_del(opts); | |
945 | return fd; | |
946 | } | |
947 | ||
0706a4dc PB |
948 | #ifdef _WIN32 |
949 | static void socket_cleanup(void) | |
950 | { | |
951 | WSACleanup(); | |
952 | } | |
953 | #endif | |
954 | ||
955 | int socket_init(void) | |
956 | { | |
957 | #ifdef _WIN32 | |
958 | WSADATA Data; | |
959 | int ret, err; | |
960 | ||
961 | ret = WSAStartup(MAKEWORD(2,2), &Data); | |
962 | if (ret != 0) { | |
963 | err = WSAGetLastError(); | |
964 | fprintf(stderr, "WSAStartup: %d\n", err); | |
965 | return -1; | |
966 | } | |
967 | atexit(socket_cleanup); | |
968 | #endif | |
969 | return 0; | |
970 | } |