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