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