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