root / qemu-sockets.c @ 3bc2f570
History | View | Annotate | Download (26 kB)
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.
|
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.
|
17 |
*/
|
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 |
|
25 |
#include "monitor.h" |
26 |
#include "qemu_socket.h" |
27 |
#include "qemu-common.h" /* for qemu_isdigit */ |
28 |
#include "main-loop.h" |
29 |
|
30 |
#ifndef AI_ADDRCONFIG
|
31 |
# define AI_ADDRCONFIG 0 |
32 |
#endif
|
33 |
|
34 |
static const int on=1, off=0; |
35 |
|
36 |
/* used temporarely until all users are converted to QemuOpts */
|
37 |
static QemuOptsList dummy_opts = {
|
38 |
.name = "dummy",
|
39 |
.head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head), |
40 |
.desc = { |
41 |
{ |
42 |
.name = "path",
|
43 |
.type = QEMU_OPT_STRING, |
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, |
59 |
}, |
60 |
{ /* end if list */ }
|
61 |
}, |
62 |
}; |
63 |
|
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 |
|
98 |
const char *inet_strfamily(int family) |
99 |
{ |
100 |
switch (family) {
|
101 |
case PF_INET6: return "ipv6"; |
102 |
case PF_INET: return "ipv4"; |
103 |
case PF_UNIX: return "unix"; |
104 |
} |
105 |
return "unknown"; |
106 |
} |
107 |
|
108 |
int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp) |
109 |
{ |
110 |
struct addrinfo ai,*res,*e;
|
111 |
const char *addr; |
112 |
char port[33]; |
113 |
char uaddr[INET6_ADDRSTRLEN+1]; |
114 |
char uport[33]; |
115 |
int slisten, rc, to, port_min, port_max, p;
|
116 |
|
117 |
memset(&ai,0, sizeof(ai)); |
118 |
ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; |
119 |
ai.ai_family = PF_UNSPEC; |
120 |
ai.ai_socktype = SOCK_STREAM; |
121 |
|
122 |
if ((qemu_opt_get(opts, "host") == NULL) || |
123 |
(qemu_opt_get(opts, "port") == NULL)) { |
124 |
error_setg(errp, "host and/or port not specified");
|
125 |
return -1; |
126 |
} |
127 |
pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port")); |
128 |
addr = qemu_opt_get(opts, "host");
|
129 |
|
130 |
to = qemu_opt_get_number(opts, "to", 0); |
131 |
if (qemu_opt_get_bool(opts, "ipv4", 0)) |
132 |
ai.ai_family = PF_INET; |
133 |
if (qemu_opt_get_bool(opts, "ipv6", 0)) |
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) { |
141 |
error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
|
142 |
gai_strerror(rc)); |
143 |
return -1; |
144 |
} |
145 |
|
146 |
/* create socket + bind */
|
147 |
for (e = res; e != NULL; e = e->ai_next) { |
148 |
getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
|
149 |
uaddr,INET6_ADDRSTRLEN,uport,32,
|
150 |
NI_NUMERICHOST | NI_NUMERICSERV); |
151 |
slisten = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol); |
152 |
if (slisten < 0) { |
153 |
if (!e->ai_next) {
|
154 |
error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED); |
155 |
} |
156 |
continue;
|
157 |
} |
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 */
|
163 |
setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,
|
164 |
sizeof(off));
|
165 |
} |
166 |
#endif
|
167 |
|
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); |
172 |
if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) { |
173 |
goto listen;
|
174 |
} |
175 |
if (p == port_max) {
|
176 |
if (!e->ai_next) {
|
177 |
error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED); |
178 |
} |
179 |
} |
180 |
} |
181 |
closesocket(slisten); |
182 |
} |
183 |
freeaddrinfo(res); |
184 |
return -1; |
185 |
|
186 |
listen:
|
187 |
if (listen(slisten,1) != 0) { |
188 |
error_set_errno(errp, errno, QERR_SOCKET_LISTEN_FAILED); |
189 |
closesocket(slisten); |
190 |
freeaddrinfo(res); |
191 |
return -1; |
192 |
} |
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"); |
198 |
freeaddrinfo(res); |
199 |
return slisten;
|
200 |
} |
201 |
|
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 |
|
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, |
220 |
ConnectState *connect_state, Error **errp); |
221 |
|
222 |
static void wait_for_connect(void *opaque) |
223 |
{ |
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 */
|
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; |
250 |
s->fd = inet_connect_addr(s->current_addr, &in_progress, s, NULL);
|
251 |
/* connect in progress */
|
252 |
if (in_progress) {
|
253 |
return;
|
254 |
} |
255 |
} |
256 |
|
257 |
freeaddrinfo(s->addr_list); |
258 |
} |
259 |
|
260 |
if (s->callback) {
|
261 |
s->callback(s->fd, s->opaque); |
262 |
} |
263 |
g_free(s); |
264 |
} |
265 |
|
266 |
static int inet_connect_addr(struct addrinfo *addr, bool *in_progress, |
267 |
ConnectState *connect_state, Error **errp) |
268 |
{ |
269 |
int sock, rc;
|
270 |
|
271 |
*in_progress = false;
|
272 |
|
273 |
sock = qemu_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); |
274 |
if (sock < 0) { |
275 |
error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED); |
276 |
return -1; |
277 |
} |
278 |
qemu_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
|
279 |
if (connect_state != NULL) { |
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 |
|
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;
|
295 |
} else if (rc < 0) { |
296 |
error_set_errno(errp, errno, QERR_SOCKET_CONNECT_FAILED); |
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;
|
307 |
const char *addr; |
308 |
const char *port; |
309 |
|
310 |
memset(&ai, 0, sizeof(ai)); |
311 |
|
312 |
ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG; |
313 |
ai.ai_family = PF_UNSPEC; |
314 |
ai.ai_socktype = SOCK_STREAM; |
315 |
|
316 |
addr = qemu_opt_get(opts, "host");
|
317 |
port = qemu_opt_get(opts, "port");
|
318 |
if (addr == NULL || port == NULL) { |
319 |
error_setg(errp, "host and/or port not specified");
|
320 |
return NULL; |
321 |
} |
322 |
|
323 |
if (qemu_opt_get_bool(opts, "ipv4", 0)) { |
324 |
ai.ai_family = PF_INET; |
325 |
} |
326 |
if (qemu_opt_get_bool(opts, "ipv6", 0)) { |
327 |
ai.ai_family = PF_INET6; |
328 |
} |
329 |
|
330 |
/* lookup */
|
331 |
rc = getaddrinfo(addr, port, &ai, &res); |
332 |
if (rc != 0) { |
333 |
error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
|
334 |
gai_strerror(rc)); |
335 |
return NULL; |
336 |
} |
337 |
return res;
|
338 |
} |
339 |
|
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".
|
345 |
* @errp: set on error
|
346 |
* @callback: callback function for non-blocking connect
|
347 |
* @opaque: opaque for callback function
|
348 |
*
|
349 |
* Returns: -1 on error, file descriptor on success.
|
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.
|
354 |
*/
|
355 |
int inet_connect_opts(QemuOpts *opts, Error **errp,
|
356 |
NonBlockingConnectHandler *callback, void *opaque)
|
357 |
{ |
358 |
struct addrinfo *res, *e;
|
359 |
int sock = -1; |
360 |
bool in_progress;
|
361 |
ConnectState *connect_state = NULL;
|
362 |
|
363 |
res = inet_parse_connect_opts(opts, errp); |
364 |
if (!res) {
|
365 |
return -1; |
366 |
} |
367 |
|
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; |
373 |
} |
374 |
|
375 |
for (e = res; e != NULL; e = e->ai_next) { |
376 |
if (connect_state != NULL) { |
377 |
connect_state->current_addr = e; |
378 |
} |
379 |
sock = inet_connect_addr(e, &in_progress, connect_state, errp); |
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 |
} |
387 |
break;
|
388 |
} |
389 |
} |
390 |
g_free(connect_state); |
391 |
freeaddrinfo(res); |
392 |
return sock;
|
393 |
} |
394 |
|
395 |
int inet_dgram_opts(QemuOpts *opts, Error **errp)
|
396 |
{ |
397 |
struct addrinfo ai, *peer = NULL, *local = NULL; |
398 |
const char *addr; |
399 |
const char *port; |
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) { |
414 |
error_setg(errp, "remote port not specified");
|
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))) { |
424 |
error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
|
425 |
gai_strerror(rc)); |
426 |
return -1; |
427 |
} |
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))) { |
444 |
error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
|
445 |
gai_strerror(rc)); |
446 |
goto err;
|
447 |
} |
448 |
|
449 |
/* create socket */
|
450 |
sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol); |
451 |
if (sock < 0) { |
452 |
error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED); |
453 |
goto err;
|
454 |
} |
455 |
setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); |
456 |
|
457 |
/* bind socket */
|
458 |
if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) { |
459 |
error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED); |
460 |
goto err;
|
461 |
} |
462 |
|
463 |
/* connect to peer */
|
464 |
if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) { |
465 |
error_set_errno(errp, errno, QERR_SOCKET_CONNECT_FAILED); |
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 |
|
483 |
/* compatibility wrapper */
|
484 |
static InetSocketAddress *inet_parse(const char *str, Error **errp) |
485 |
{ |
486 |
InetSocketAddress *addr; |
487 |
const char *optstr, *h; |
488 |
char host[64]; |
489 |
char port[33]; |
490 |
int to;
|
491 |
int pos;
|
492 |
|
493 |
addr = g_new0(InetSocketAddress, 1);
|
494 |
|
495 |
/* parse address */
|
496 |
if (str[0] == ':') { |
497 |
/* no host given */
|
498 |
host[0] = '\0'; |
499 |
if (1 != sscanf(str, ":%32[^,]%n", port, &pos)) { |
500 |
error_setg(errp, "error parsing port in address '%s'", str);
|
501 |
goto fail;
|
502 |
} |
503 |
} else if (str[0] == '[') { |
504 |
/* IPv6 addr */
|
505 |
if (2 != sscanf(str, "[%64[^]]]:%32[^,]%n", host, port, &pos)) { |
506 |
error_setg(errp, "error parsing IPv6 address '%s'", str);
|
507 |
goto fail;
|
508 |
} |
509 |
addr->ipv6 = addr->has_ipv6 = true;
|
510 |
} else if (qemu_isdigit(str[0])) { |
511 |
/* IPv4 addr */
|
512 |
if (2 != sscanf(str, "%64[0-9.]:%32[^,]%n", host, port, &pos)) { |
513 |
error_setg(errp, "error parsing IPv4 address '%s'", str);
|
514 |
goto fail;
|
515 |
} |
516 |
addr->ipv4 = addr->has_ipv4 = true;
|
517 |
} else {
|
518 |
/* hostname */
|
519 |
if (2 != sscanf(str, "%64[^:]:%32[^,]%n", host, port, &pos)) { |
520 |
error_setg(errp, "error parsing address '%s'", str);
|
521 |
goto fail;
|
522 |
} |
523 |
} |
524 |
|
525 |
addr->host = g_strdup(host); |
526 |
addr->port = g_strdup(port); |
527 |
|
528 |
/* parse options */
|
529 |
optstr = str + pos; |
530 |
h = strstr(optstr, ",to=");
|
531 |
if (h) {
|
532 |
if (1 != sscanf(str, "%d%n", &to, &pos) || |
533 |
(str[pos] != '\0' && str[pos] != ',')) { |
534 |
error_setg(errp, "error parsing to= argument");
|
535 |
goto fail;
|
536 |
} |
537 |
addr->has_to = true;
|
538 |
addr->to = to; |
539 |
} |
540 |
if (strstr(optstr, ",ipv4")) { |
541 |
addr->ipv4 = addr->has_ipv4 = true;
|
542 |
} |
543 |
if (strstr(optstr, ",ipv6")) { |
544 |
addr->ipv6 = addr->has_ipv6 = true;
|
545 |
} |
546 |
return addr;
|
547 |
|
548 |
fail:
|
549 |
qapi_free_InetSocketAddress(addr); |
550 |
return NULL; |
551 |
} |
552 |
|
553 |
static void inet_addr_to_opts(QemuOpts *opts, InetSocketAddress *addr) |
554 |
{ |
555 |
bool ipv4 = addr->ipv4 || !addr->has_ipv4;
|
556 |
bool ipv6 = addr->ipv6 || !addr->has_ipv6;
|
557 |
|
558 |
if (!ipv4 || !ipv6) {
|
559 |
qemu_opt_set_bool(opts, "ipv4", ipv4);
|
560 |
qemu_opt_set_bool(opts, "ipv6", ipv6);
|
561 |
} |
562 |
if (addr->has_to) {
|
563 |
char to[20]; |
564 |
snprintf(to, sizeof(to), "%d", addr->to); |
565 |
qemu_opt_set(opts, "to", to);
|
566 |
} |
567 |
qemu_opt_set(opts, "host", addr->host);
|
568 |
qemu_opt_set(opts, "port", addr->port);
|
569 |
} |
570 |
|
571 |
int inet_listen(const char *str, char *ostr, int olen, |
572 |
int socktype, int port_offset, Error **errp) |
573 |
{ |
574 |
QemuOpts *opts; |
575 |
char *optstr;
|
576 |
int sock = -1; |
577 |
InetSocketAddress *addr; |
578 |
|
579 |
addr = inet_parse(str, errp); |
580 |
if (addr != NULL) { |
581 |
opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); |
582 |
inet_addr_to_opts(opts, addr); |
583 |
qapi_free_InetSocketAddress(addr); |
584 |
sock = inet_listen_opts(opts, port_offset, errp); |
585 |
if (sock != -1 && ostr) { |
586 |
optstr = strchr(str, ',');
|
587 |
if (qemu_opt_get_bool(opts, "ipv6", 0)) { |
588 |
snprintf(ostr, olen, "[%s]:%s%s",
|
589 |
qemu_opt_get(opts, "host"),
|
590 |
qemu_opt_get(opts, "port"),
|
591 |
optstr ? optstr : "");
|
592 |
} else {
|
593 |
snprintf(ostr, olen, "%s:%s%s",
|
594 |
qemu_opt_get(opts, "host"),
|
595 |
qemu_opt_get(opts, "port"),
|
596 |
optstr ? optstr : "");
|
597 |
} |
598 |
} |
599 |
qemu_opts_del(opts); |
600 |
} |
601 |
return sock;
|
602 |
} |
603 |
|
604 |
/**
|
605 |
* Create a blocking socket and connect it to an address.
|
606 |
*
|
607 |
* @str: address string
|
608 |
* @errp: set in case of an error
|
609 |
*
|
610 |
* Returns -1 in case of error, file descriptor on success
|
611 |
**/
|
612 |
int inet_connect(const char *str, Error **errp) |
613 |
{ |
614 |
QemuOpts *opts; |
615 |
int sock = -1; |
616 |
InetSocketAddress *addr; |
617 |
|
618 |
addr = inet_parse(str, errp); |
619 |
if (addr != NULL) { |
620 |
opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); |
621 |
inet_addr_to_opts(opts, addr); |
622 |
qapi_free_InetSocketAddress(addr); |
623 |
sock = inet_connect_opts(opts, errp, NULL, NULL); |
624 |
qemu_opts_del(opts); |
625 |
} |
626 |
return sock;
|
627 |
} |
628 |
|
629 |
/**
|
630 |
* Create a non-blocking socket and connect it to an address.
|
631 |
* Calls the callback function with fd in case of success or -1 in case of
|
632 |
* error.
|
633 |
*
|
634 |
* @str: address string
|
635 |
* @callback: callback function that is called when connect completes,
|
636 |
* cannot be NULL.
|
637 |
* @opaque: opaque for callback function
|
638 |
* @errp: set in case of an error
|
639 |
*
|
640 |
* Returns: -1 on immediate error, file descriptor on success.
|
641 |
**/
|
642 |
int inet_nonblocking_connect(const char *str, |
643 |
NonBlockingConnectHandler *callback, |
644 |
void *opaque, Error **errp)
|
645 |
{ |
646 |
QemuOpts *opts; |
647 |
int sock = -1; |
648 |
InetSocketAddress *addr; |
649 |
|
650 |
g_assert(callback != NULL);
|
651 |
|
652 |
addr = inet_parse(str, errp); |
653 |
if (addr != NULL) { |
654 |
opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); |
655 |
inet_addr_to_opts(opts, addr); |
656 |
qapi_free_InetSocketAddress(addr); |
657 |
sock = inet_connect_opts(opts, errp, callback, opaque); |
658 |
qemu_opts_del(opts); |
659 |
} |
660 |
return sock;
|
661 |
} |
662 |
|
663 |
#ifndef _WIN32
|
664 |
|
665 |
int unix_listen_opts(QemuOpts *opts, Error **errp)
|
666 |
{ |
667 |
struct sockaddr_un un;
|
668 |
const char *path = qemu_opt_get(opts, "path"); |
669 |
int sock, fd;
|
670 |
|
671 |
sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
|
672 |
if (sock < 0) { |
673 |
error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED); |
674 |
return -1; |
675 |
} |
676 |
|
677 |
memset(&un, 0, sizeof(un)); |
678 |
un.sun_family = AF_UNIX; |
679 |
if (path && strlen(path)) {
|
680 |
snprintf(un.sun_path, sizeof(un.sun_path), "%s", path); |
681 |
} else {
|
682 |
char *tmpdir = getenv("TMPDIR"); |
683 |
snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX", |
684 |
tmpdir ? tmpdir : "/tmp");
|
685 |
/*
|
686 |
* This dummy fd usage silences the mktemp() unsecure warning.
|
687 |
* Using mkstemp() doesn't make things more secure here
|
688 |
* though. bind() complains about existing files, so we have
|
689 |
* to unlink first and thus re-open the race window. The
|
690 |
* worst case possible is bind() failing, i.e. a DoS attack.
|
691 |
*/
|
692 |
fd = mkstemp(un.sun_path); close(fd); |
693 |
qemu_opt_set(opts, "path", un.sun_path);
|
694 |
} |
695 |
|
696 |
unlink(un.sun_path); |
697 |
if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) { |
698 |
error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED); |
699 |
goto err;
|
700 |
} |
701 |
if (listen(sock, 1) < 0) { |
702 |
error_set_errno(errp, errno, QERR_SOCKET_LISTEN_FAILED); |
703 |
goto err;
|
704 |
} |
705 |
|
706 |
return sock;
|
707 |
|
708 |
err:
|
709 |
closesocket(sock); |
710 |
return -1; |
711 |
} |
712 |
|
713 |
int unix_connect_opts(QemuOpts *opts, Error **errp,
|
714 |
NonBlockingConnectHandler *callback, void *opaque)
|
715 |
{ |
716 |
struct sockaddr_un un;
|
717 |
const char *path = qemu_opt_get(opts, "path"); |
718 |
ConnectState *connect_state = NULL;
|
719 |
int sock, rc;
|
720 |
|
721 |
if (NULL == path) { |
722 |
error_setg(errp, "unix connect: no path specified\n");
|
723 |
return -1; |
724 |
} |
725 |
|
726 |
sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
|
727 |
if (sock < 0) { |
728 |
error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED); |
729 |
return -1; |
730 |
} |
731 |
if (callback != NULL) { |
732 |
connect_state = g_malloc0(sizeof(*connect_state));
|
733 |
connect_state->callback = callback; |
734 |
connect_state->opaque = opaque; |
735 |
socket_set_nonblock(sock); |
736 |
} |
737 |
|
738 |
memset(&un, 0, sizeof(un)); |
739 |
un.sun_family = AF_UNIX; |
740 |
snprintf(un.sun_path, sizeof(un.sun_path), "%s", path); |
741 |
|
742 |
/* connect to peer */
|
743 |
do {
|
744 |
rc = 0;
|
745 |
if (connect(sock, (struct sockaddr *) &un, sizeof(un)) < 0) { |
746 |
rc = -socket_error(); |
747 |
} |
748 |
} while (rc == -EINTR);
|
749 |
|
750 |
if (connect_state != NULL && QEMU_SOCKET_RC_INPROGRESS(rc)) { |
751 |
connect_state->fd = sock; |
752 |
qemu_set_fd_handler2(sock, NULL, NULL, wait_for_connect, |
753 |
connect_state); |
754 |
return sock;
|
755 |
} else if (rc >= 0) { |
756 |
/* non blocking socket immediate success, call callback */
|
757 |
if (callback != NULL) { |
758 |
callback(sock, opaque); |
759 |
} |
760 |
} |
761 |
|
762 |
if (rc < 0) { |
763 |
error_set_errno(errp, -rc, QERR_SOCKET_CONNECT_FAILED); |
764 |
close(sock); |
765 |
sock = -1;
|
766 |
} |
767 |
|
768 |
g_free(connect_state); |
769 |
return sock;
|
770 |
} |
771 |
|
772 |
#else
|
773 |
|
774 |
int unix_listen_opts(QemuOpts *opts, Error **errp)
|
775 |
{ |
776 |
error_setg(errp, "unix sockets are not available on windows");
|
777 |
errno = ENOTSUP; |
778 |
return -1; |
779 |
} |
780 |
|
781 |
int unix_connect_opts(QemuOpts *opts, Error **errp,
|
782 |
NonBlockingConnectHandler *callback, void *opaque)
|
783 |
{ |
784 |
error_setg(errp, "unix sockets are not available on windows");
|
785 |
errno = ENOTSUP; |
786 |
return -1; |
787 |
} |
788 |
#endif
|
789 |
|
790 |
/* compatibility wrapper */
|
791 |
int unix_listen(const char *str, char *ostr, int olen, Error **errp) |
792 |
{ |
793 |
QemuOpts *opts; |
794 |
char *path, *optstr;
|
795 |
int sock, len;
|
796 |
|
797 |
opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); |
798 |
|
799 |
optstr = strchr(str, ',');
|
800 |
if (optstr) {
|
801 |
len = optstr - str; |
802 |
if (len) {
|
803 |
path = g_malloc(len+1);
|
804 |
snprintf(path, len+1, "%.*s", len, str); |
805 |
qemu_opt_set(opts, "path", path);
|
806 |
g_free(path); |
807 |
} |
808 |
} else {
|
809 |
qemu_opt_set(opts, "path", str);
|
810 |
} |
811 |
|
812 |
sock = unix_listen_opts(opts, errp); |
813 |
|
814 |
if (sock != -1 && ostr) |
815 |
snprintf(ostr, olen, "%s%s", qemu_opt_get(opts, "path"), optstr ? optstr : ""); |
816 |
qemu_opts_del(opts); |
817 |
return sock;
|
818 |
} |
819 |
|
820 |
int unix_connect(const char *path, Error **errp) |
821 |
{ |
822 |
QemuOpts *opts; |
823 |
int sock;
|
824 |
|
825 |
opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); |
826 |
qemu_opt_set(opts, "path", path);
|
827 |
sock = unix_connect_opts(opts, errp, NULL, NULL); |
828 |
qemu_opts_del(opts); |
829 |
return sock;
|
830 |
} |
831 |
|
832 |
|
833 |
int unix_nonblocking_connect(const char *path, |
834 |
NonBlockingConnectHandler *callback, |
835 |
void *opaque, Error **errp)
|
836 |
{ |
837 |
QemuOpts *opts; |
838 |
int sock = -1; |
839 |
|
840 |
g_assert(callback != NULL);
|
841 |
|
842 |
opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); |
843 |
qemu_opt_set(opts, "path", path);
|
844 |
sock = unix_connect_opts(opts, errp, callback, opaque); |
845 |
qemu_opts_del(opts); |
846 |
return sock;
|
847 |
} |
848 |
|
849 |
SocketAddress *socket_parse(const char *str, Error **errp) |
850 |
{ |
851 |
SocketAddress *addr = NULL;
|
852 |
|
853 |
addr = g_new(SocketAddress, 1);
|
854 |
if (strstart(str, "unix:", NULL)) { |
855 |
if (str[5] == '\0') { |
856 |
error_setg(errp, "invalid Unix socket address\n");
|
857 |
goto fail;
|
858 |
} else {
|
859 |
addr->kind = SOCKET_ADDRESS_KIND_UNIX; |
860 |
addr->q_unix = g_new(UnixSocketAddress, 1);
|
861 |
addr->q_unix->path = g_strdup(str + 5);
|
862 |
} |
863 |
} else if (strstart(str, "fd:", NULL)) { |
864 |
if (str[3] == '\0') { |
865 |
error_setg(errp, "invalid file descriptor address\n");
|
866 |
goto fail;
|
867 |
} else {
|
868 |
addr->kind = SOCKET_ADDRESS_KIND_FD; |
869 |
addr->fd = g_new(String, 1);
|
870 |
addr->fd->str = g_strdup(str + 3);
|
871 |
} |
872 |
} else {
|
873 |
addr->kind = SOCKET_ADDRESS_KIND_INET; |
874 |
addr->inet = g_new(InetSocketAddress, 1);
|
875 |
addr->inet = inet_parse(str, errp); |
876 |
if (addr->inet == NULL) { |
877 |
goto fail;
|
878 |
} |
879 |
} |
880 |
return addr;
|
881 |
|
882 |
fail:
|
883 |
qapi_free_SocketAddress(addr); |
884 |
return NULL; |
885 |
} |
886 |
|
887 |
int socket_connect(SocketAddress *addr, Error **errp,
|
888 |
NonBlockingConnectHandler *callback, void *opaque)
|
889 |
{ |
890 |
QemuOpts *opts; |
891 |
int fd;
|
892 |
|
893 |
opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); |
894 |
switch (addr->kind) {
|
895 |
case SOCKET_ADDRESS_KIND_INET:
|
896 |
inet_addr_to_opts(opts, addr->inet); |
897 |
fd = inet_connect_opts(opts, errp, callback, opaque); |
898 |
break;
|
899 |
|
900 |
case SOCKET_ADDRESS_KIND_UNIX:
|
901 |
qemu_opt_set(opts, "path", addr->q_unix->path);
|
902 |
fd = unix_connect_opts(opts, errp, callback, opaque); |
903 |
break;
|
904 |
|
905 |
case SOCKET_ADDRESS_KIND_FD:
|
906 |
fd = monitor_get_fd(cur_mon, addr->fd->str, errp); |
907 |
if (callback) {
|
908 |
callback(fd, opaque); |
909 |
} |
910 |
break;
|
911 |
|
912 |
default:
|
913 |
abort(); |
914 |
} |
915 |
qemu_opts_del(opts); |
916 |
return fd;
|
917 |
} |
918 |
|
919 |
int socket_listen(SocketAddress *addr, Error **errp)
|
920 |
{ |
921 |
QemuOpts *opts; |
922 |
int fd;
|
923 |
|
924 |
opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); |
925 |
switch (addr->kind) {
|
926 |
case SOCKET_ADDRESS_KIND_INET:
|
927 |
inet_addr_to_opts(opts, addr->inet); |
928 |
fd = inet_listen_opts(opts, 0, errp);
|
929 |
break;
|
930 |
|
931 |
case SOCKET_ADDRESS_KIND_UNIX:
|
932 |
qemu_opt_set(opts, "path", addr->q_unix->path);
|
933 |
fd = unix_listen_opts(opts, errp); |
934 |
break;
|
935 |
|
936 |
case SOCKET_ADDRESS_KIND_FD:
|
937 |
fd = monitor_get_fd(cur_mon, addr->fd->str, errp); |
938 |
break;
|
939 |
|
940 |
default:
|
941 |
abort(); |
942 |
} |
943 |
qemu_opts_del(opts); |
944 |
return fd;
|
945 |
} |
946 |
|
947 |
#ifdef _WIN32
|
948 |
static void socket_cleanup(void) |
949 |
{ |
950 |
WSACleanup(); |
951 |
} |
952 |
#endif
|
953 |
|
954 |
int socket_init(void) |
955 |
{ |
956 |
#ifdef _WIN32
|
957 |
WSADATA Data; |
958 |
int ret, err;
|
959 |
|
960 |
ret = WSAStartup(MAKEWORD(2,2), &Data); |
961 |
if (ret != 0) { |
962 |
err = WSAGetLastError(); |
963 |
fprintf(stderr, "WSAStartup: %d\n", err);
|
964 |
return -1; |
965 |
} |
966 |
atexit(socket_cleanup); |
967 |
#endif
|
968 |
return 0; |
969 |
} |