block: fix documentation of block_job_cancel_sync
[qemu/ar7.git] / qemu-sockets.c
blob1f14e8bc63b72f01e831e73a6a5ce845fbe5e09f
1 /*
2 * inet and unix socket functions for qemu
4 * (c) 2008 Gerd Hoffmann <kraxel@redhat.com>
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.
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.
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.
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <unistd.h>
25 #include "qemu_socket.h"
26 #include "qemu-common.h" /* for qemu_isdigit */
27 #include "main-loop.h"
29 #ifndef AI_ADDRCONFIG
30 # define AI_ADDRCONFIG 0
31 #endif
33 static const int on=1, off=0;
35 /* used temporarely until all users are converted to QemuOpts */
36 static QemuOptsList dummy_opts = {
37 .name = "dummy",
38 .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head),
39 .desc = {
41 .name = "path",
42 .type = QEMU_OPT_STRING,
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,
59 { /* end if list */ }
63 static int inet_getport(struct addrinfo *e)
65 struct sockaddr_in *i4;
66 struct sockaddr_in6 *i6;
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;
80 static void inet_setport(struct addrinfo *e, int port)
82 struct sockaddr_in *i4;
83 struct sockaddr_in6 *i6;
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;
97 const char *inet_strfamily(int family)
99 switch (family) {
100 case PF_INET6: return "ipv6";
101 case PF_INET: return "ipv4";
102 case PF_UNIX: return "unix";
104 return "unknown";
107 int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)
109 struct addrinfo ai,*res,*e;
110 const char *addr;
111 char port[33];
112 char uaddr[INET6_ADDRSTRLEN+1];
113 char uport[33];
114 int slisten, rc, to, port_min, port_max, p;
116 memset(&ai,0, sizeof(ai));
117 ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
118 ai.ai_family = PF_UNSPEC;
119 ai.ai_socktype = SOCK_STREAM;
121 if ((qemu_opt_get(opts, "host") == NULL) ||
122 (qemu_opt_get(opts, "port") == NULL)) {
123 fprintf(stderr, "%s: host and/or port not specified\n", __FUNCTION__);
124 error_set(errp, QERR_SOCKET_CREATE_FAILED);
125 return -1;
127 pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port"));
128 addr = qemu_opt_get(opts, "host");
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;
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 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
142 gai_strerror(rc));
143 error_set(errp, QERR_SOCKET_CREATE_FAILED);
144 return -1;
147 /* create socket + bind */
148 for (e = res; e != NULL; e = e->ai_next) {
149 getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
150 uaddr,INET6_ADDRSTRLEN,uport,32,
151 NI_NUMERICHOST | NI_NUMERICSERV);
152 slisten = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol);
153 if (slisten < 0) {
154 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
155 inet_strfamily(e->ai_family), strerror(errno));
156 if (!e->ai_next) {
157 error_set(errp, QERR_SOCKET_CREATE_FAILED);
159 continue;
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 */
166 setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,
167 sizeof(off));
169 #endif
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);
175 if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {
176 goto listen;
178 if (p == port_max) {
179 fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__,
180 inet_strfamily(e->ai_family), uaddr, inet_getport(e),
181 strerror(errno));
182 if (!e->ai_next) {
183 error_set(errp, QERR_SOCKET_BIND_FAILED);
187 closesocket(slisten);
189 fprintf(stderr, "%s: FAILED\n", __FUNCTION__);
190 freeaddrinfo(res);
191 return -1;
193 listen:
194 if (listen(slisten,1) != 0) {
195 error_set(errp, QERR_SOCKET_LISTEN_FAILED);
196 perror("listen");
197 closesocket(slisten);
198 freeaddrinfo(res);
199 return -1;
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");
206 freeaddrinfo(res);
207 return slisten;
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
218 /* Struct to store connect state for non blocking connect */
219 typedef struct ConnectState {
220 int fd;
221 struct addrinfo *addr_list;
222 struct addrinfo *current_addr;
223 NonBlockingConnectHandler *callback;
224 void *opaque;
225 } ConnectState;
227 static int inet_connect_addr(struct addrinfo *addr, bool *in_progress,
228 ConnectState *connect_state);
230 static void wait_for_connect(void *opaque)
232 ConnectState *s = opaque;
233 int val = 0, rc = 0;
234 socklen_t valsize = sizeof(val);
235 bool in_progress;
237 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
239 do {
240 rc = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize);
241 } while (rc == -1 && socket_error() == EINTR);
243 /* update rc to contain error */
244 if (!rc && val) {
245 rc = -1;
248 /* connect error */
249 if (rc < 0) {
250 closesocket(s->fd);
251 s->fd = rc;
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;
264 freeaddrinfo(s->addr_list);
265 if (s->callback) {
266 s->callback(s->fd, s->opaque);
268 g_free(s);
269 return;
272 static int inet_connect_addr(struct addrinfo *addr, bool *in_progress,
273 ConnectState *connect_state)
275 int sock, rc;
277 *in_progress = false;
279 sock = qemu_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
280 if (sock < 0) {
281 fprintf(stderr, "%s: socket(%s): %s\n", __func__,
282 inet_strfamily(addr->ai_family), strerror(errno));
283 return -1;
285 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
286 if (connect_state != NULL) {
287 socket_set_nonblock(sock);
289 /* connect to peer */
290 do {
291 rc = 0;
292 if (connect(sock, addr->ai_addr, addr->ai_addrlen) < 0) {
293 rc = -socket_error();
295 } while (rc == -EINTR);
297 if (connect_state != NULL && QEMU_SOCKET_RC_INPROGRESS(rc)) {
298 connect_state->fd = sock;
299 qemu_set_fd_handler2(sock, NULL, NULL, wait_for_connect,
300 connect_state);
301 *in_progress = true;
302 } else if (rc < 0) {
303 closesocket(sock);
304 return -1;
306 return sock;
309 static struct addrinfo *inet_parse_connect_opts(QemuOpts *opts, Error **errp)
311 struct addrinfo ai, *res;
312 int rc;
313 const char *addr;
314 const char *port;
316 memset(&ai, 0, sizeof(ai));
318 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
319 ai.ai_family = PF_UNSPEC;
320 ai.ai_socktype = SOCK_STREAM;
322 addr = qemu_opt_get(opts, "host");
323 port = qemu_opt_get(opts, "port");
324 if (addr == NULL || port == NULL) {
325 fprintf(stderr,
326 "inet_parse_connect_opts: host and/or port not specified\n");
327 error_set(errp, QERR_SOCKET_CREATE_FAILED);
328 return NULL;
331 if (qemu_opt_get_bool(opts, "ipv4", 0)) {
332 ai.ai_family = PF_INET;
334 if (qemu_opt_get_bool(opts, "ipv6", 0)) {
335 ai.ai_family = PF_INET6;
338 /* lookup */
339 rc = getaddrinfo(addr, port, &ai, &res);
340 if (rc != 0) {
341 fprintf(stderr, "getaddrinfo(%s,%s): %s\n", addr, port,
342 gai_strerror(rc));
343 error_set(errp, QERR_SOCKET_CREATE_FAILED);
344 return NULL;
346 return res;
350 * Create a socket and connect it to an address.
352 * @opts: QEMU options, recognized parameters strings "host" and "port",
353 * bools "ipv4" and "ipv6".
354 * @errp: set on error
355 * @callback: callback function for non-blocking connect
356 * @opaque: opaque for callback function
358 * Returns: -1 on error, file descriptor on success.
360 * If @callback is non-null, the connect is non-blocking. If this
361 * function succeeds, callback will be called when the connection
362 * completes, with the file descriptor on success, or -1 on error.
364 int inet_connect_opts(QemuOpts *opts, Error **errp,
365 NonBlockingConnectHandler *callback, void *opaque)
367 struct addrinfo *res, *e;
368 int sock = -1;
369 bool in_progress;
370 ConnectState *connect_state = NULL;
372 res = inet_parse_connect_opts(opts, errp);
373 if (!res) {
374 return -1;
377 if (callback != NULL) {
378 connect_state = g_malloc0(sizeof(*connect_state));
379 connect_state->addr_list = res;
380 connect_state->callback = callback;
381 connect_state->opaque = opaque;
384 for (e = res; e != NULL; e = e->ai_next) {
385 if (connect_state != NULL) {
386 connect_state->current_addr = e;
388 sock = inet_connect_addr(e, &in_progress, connect_state);
389 if (in_progress) {
390 return sock;
391 } else if (sock >= 0) {
392 /* non blocking socket immediate success, call callback */
393 if (callback != NULL) {
394 callback(sock, opaque);
396 break;
399 if (sock < 0) {
400 error_set(errp, QERR_SOCKET_CONNECT_FAILED);
402 g_free(connect_state);
403 freeaddrinfo(res);
404 return sock;
407 int inet_dgram_opts(QemuOpts *opts)
409 struct addrinfo ai, *peer = NULL, *local = NULL;
410 const char *addr;
411 const char *port;
412 char uaddr[INET6_ADDRSTRLEN+1];
413 char uport[33];
414 int sock = -1, rc;
416 /* lookup peer addr */
417 memset(&ai,0, sizeof(ai));
418 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
419 ai.ai_family = PF_UNSPEC;
420 ai.ai_socktype = SOCK_DGRAM;
422 addr = qemu_opt_get(opts, "host");
423 port = qemu_opt_get(opts, "port");
424 if (addr == NULL || strlen(addr) == 0) {
425 addr = "localhost";
427 if (port == NULL || strlen(port) == 0) {
428 fprintf(stderr, "inet_dgram: port not specified\n");
429 return -1;
432 if (qemu_opt_get_bool(opts, "ipv4", 0))
433 ai.ai_family = PF_INET;
434 if (qemu_opt_get_bool(opts, "ipv6", 0))
435 ai.ai_family = PF_INET6;
437 if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) {
438 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
439 gai_strerror(rc));
440 return -1;
443 /* lookup local addr */
444 memset(&ai,0, sizeof(ai));
445 ai.ai_flags = AI_PASSIVE;
446 ai.ai_family = peer->ai_family;
447 ai.ai_socktype = SOCK_DGRAM;
449 addr = qemu_opt_get(opts, "localaddr");
450 port = qemu_opt_get(opts, "localport");
451 if (addr == NULL || strlen(addr) == 0) {
452 addr = NULL;
454 if (!port || strlen(port) == 0)
455 port = "0";
457 if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) {
458 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
459 gai_strerror(rc));
460 goto err;
463 /* create socket */
464 sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol);
465 if (sock < 0) {
466 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
467 inet_strfamily(peer->ai_family), strerror(errno));
468 goto err;
470 setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
472 /* bind socket */
473 if (getnameinfo((struct sockaddr*)local->ai_addr,local->ai_addrlen,
474 uaddr,INET6_ADDRSTRLEN,uport,32,
475 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
476 fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
477 goto err;
479 if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) {
480 fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,
481 inet_strfamily(local->ai_family), uaddr, inet_getport(local));
482 goto err;
485 /* connect to peer */
486 if (getnameinfo((struct sockaddr*)peer->ai_addr, peer->ai_addrlen,
487 uaddr, INET6_ADDRSTRLEN, uport, 32,
488 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
489 fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
490 goto err;
492 if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) {
493 fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
494 inet_strfamily(peer->ai_family),
495 peer->ai_canonname, uaddr, uport, strerror(errno));
496 goto err;
499 freeaddrinfo(local);
500 freeaddrinfo(peer);
501 return sock;
503 err:
504 if (-1 != sock)
505 closesocket(sock);
506 if (local)
507 freeaddrinfo(local);
508 if (peer)
509 freeaddrinfo(peer);
510 return -1;
513 /* compatibility wrapper */
514 static int inet_parse(QemuOpts *opts, const char *str)
516 const char *optstr, *h;
517 char addr[64];
518 char port[33];
519 int pos;
521 /* parse address */
522 if (str[0] == ':') {
523 /* no host given */
524 addr[0] = '\0';
525 if (1 != sscanf(str,":%32[^,]%n",port,&pos)) {
526 fprintf(stderr, "%s: portonly parse error (%s)\n",
527 __FUNCTION__, str);
528 return -1;
530 } else if (str[0] == '[') {
531 /* IPv6 addr */
532 if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) {
533 fprintf(stderr, "%s: ipv6 parse error (%s)\n",
534 __FUNCTION__, str);
535 return -1;
537 qemu_opt_set(opts, "ipv6", "on");
538 } else if (qemu_isdigit(str[0])) {
539 /* IPv4 addr */
540 if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) {
541 fprintf(stderr, "%s: ipv4 parse error (%s)\n",
542 __FUNCTION__, str);
543 return -1;
545 qemu_opt_set(opts, "ipv4", "on");
546 } else {
547 /* hostname */
548 if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) {
549 fprintf(stderr, "%s: hostname parse error (%s)\n",
550 __FUNCTION__, str);
551 return -1;
554 qemu_opt_set(opts, "host", addr);
555 qemu_opt_set(opts, "port", port);
557 /* parse options */
558 optstr = str + pos;
559 h = strstr(optstr, ",to=");
560 if (h)
561 qemu_opt_set(opts, "to", h+4);
562 if (strstr(optstr, ",ipv4"))
563 qemu_opt_set(opts, "ipv4", "on");
564 if (strstr(optstr, ",ipv6"))
565 qemu_opt_set(opts, "ipv6", "on");
566 return 0;
569 int inet_listen(const char *str, char *ostr, int olen,
570 int socktype, int port_offset, Error **errp)
572 QemuOpts *opts;
573 char *optstr;
574 int sock = -1;
576 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
577 if (inet_parse(opts, str) == 0) {
578 sock = inet_listen_opts(opts, port_offset, errp);
579 if (sock != -1 && ostr) {
580 optstr = strchr(str, ',');
581 if (qemu_opt_get_bool(opts, "ipv6", 0)) {
582 snprintf(ostr, olen, "[%s]:%s%s",
583 qemu_opt_get(opts, "host"),
584 qemu_opt_get(opts, "port"),
585 optstr ? optstr : "");
586 } else {
587 snprintf(ostr, olen, "%s:%s%s",
588 qemu_opt_get(opts, "host"),
589 qemu_opt_get(opts, "port"),
590 optstr ? optstr : "");
593 } else {
594 error_set(errp, QERR_SOCKET_CREATE_FAILED);
596 qemu_opts_del(opts);
597 return sock;
601 * Create a blocking socket and connect it to an address.
603 * @str: address string
604 * @errp: set in case of an error
606 * Returns -1 in case of error, file descriptor on success
608 int inet_connect(const char *str, Error **errp)
610 QemuOpts *opts;
611 int sock = -1;
613 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
614 if (inet_parse(opts, str) == 0) {
615 sock = inet_connect_opts(opts, errp, NULL, NULL);
616 } else {
617 error_set(errp, QERR_SOCKET_CREATE_FAILED);
619 qemu_opts_del(opts);
620 return sock;
624 * Create a non-blocking socket and connect it to an address.
625 * Calls the callback function with fd in case of success or -1 in case of
626 * error.
628 * @str: address string
629 * @callback: callback function that is called when connect completes,
630 * cannot be NULL.
631 * @opaque: opaque for callback function
632 * @errp: set in case of an error
634 * Returns: -1 on immediate error, file descriptor on success.
636 int inet_nonblocking_connect(const char *str,
637 NonBlockingConnectHandler *callback,
638 void *opaque, Error **errp)
640 QemuOpts *opts;
641 int sock = -1;
643 g_assert(callback != NULL);
645 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
646 if (inet_parse(opts, str) == 0) {
647 sock = inet_connect_opts(opts, errp, callback, opaque);
648 } else {
649 error_set(errp, QERR_SOCKET_CREATE_FAILED);
651 qemu_opts_del(opts);
652 return sock;
655 #ifndef _WIN32
657 int unix_listen_opts(QemuOpts *opts)
659 struct sockaddr_un un;
660 const char *path = qemu_opt_get(opts, "path");
661 int sock, fd;
663 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
664 if (sock < 0) {
665 perror("socket(unix)");
666 return -1;
669 memset(&un, 0, sizeof(un));
670 un.sun_family = AF_UNIX;
671 if (path && strlen(path)) {
672 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
673 } else {
674 char *tmpdir = getenv("TMPDIR");
675 snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX",
676 tmpdir ? tmpdir : "/tmp");
678 * This dummy fd usage silences the mktemp() unsecure warning.
679 * Using mkstemp() doesn't make things more secure here
680 * though. bind() complains about existing files, so we have
681 * to unlink first and thus re-open the race window. The
682 * worst case possible is bind() failing, i.e. a DoS attack.
684 fd = mkstemp(un.sun_path); close(fd);
685 qemu_opt_set(opts, "path", un.sun_path);
688 unlink(un.sun_path);
689 if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
690 fprintf(stderr, "bind(unix:%s): %s\n", un.sun_path, strerror(errno));
691 goto err;
693 if (listen(sock, 1) < 0) {
694 fprintf(stderr, "listen(unix:%s): %s\n", un.sun_path, strerror(errno));
695 goto err;
698 return sock;
700 err:
701 closesocket(sock);
702 return -1;
705 int unix_connect_opts(QemuOpts *opts)
707 struct sockaddr_un un;
708 const char *path = qemu_opt_get(opts, "path");
709 int sock;
711 if (NULL == path) {
712 fprintf(stderr, "unix connect: no path specified\n");
713 return -1;
716 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
717 if (sock < 0) {
718 perror("socket(unix)");
719 return -1;
722 memset(&un, 0, sizeof(un));
723 un.sun_family = AF_UNIX;
724 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
725 if (connect(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
726 fprintf(stderr, "connect(unix:%s): %s\n", path, strerror(errno));
727 close(sock);
728 return -1;
731 return sock;
734 /* compatibility wrapper */
735 int unix_listen(const char *str, char *ostr, int olen)
737 QemuOpts *opts;
738 char *path, *optstr;
739 int sock, len;
741 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
743 optstr = strchr(str, ',');
744 if (optstr) {
745 len = optstr - str;
746 if (len) {
747 path = g_malloc(len+1);
748 snprintf(path, len+1, "%.*s", len, str);
749 qemu_opt_set(opts, "path", path);
750 g_free(path);
752 } else {
753 qemu_opt_set(opts, "path", str);
756 sock = unix_listen_opts(opts);
758 if (sock != -1 && ostr)
759 snprintf(ostr, olen, "%s%s", qemu_opt_get(opts, "path"), optstr ? optstr : "");
760 qemu_opts_del(opts);
761 return sock;
764 int unix_connect(const char *path)
766 QemuOpts *opts;
767 int sock;
769 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
770 qemu_opt_set(opts, "path", path);
771 sock = unix_connect_opts(opts);
772 qemu_opts_del(opts);
773 return sock;
776 #else
778 int unix_listen_opts(QemuOpts *opts)
780 fprintf(stderr, "unix sockets are not available on windows\n");
781 errno = ENOTSUP;
782 return -1;
785 int unix_connect_opts(QemuOpts *opts)
787 fprintf(stderr, "unix sockets are not available on windows\n");
788 errno = ENOTSUP;
789 return -1;
792 int unix_listen(const char *path, char *ostr, int olen)
794 fprintf(stderr, "unix sockets are not available on windows\n");
795 errno = ENOTSUP;
796 return -1;
799 int unix_connect(const char *path)
801 fprintf(stderr, "unix sockets are not available on windows\n");
802 errno = ENOTSUP;
803 return -1;
806 #endif
808 #ifdef _WIN32
809 static void socket_cleanup(void)
811 WSACleanup();
813 #endif
815 int socket_init(void)
817 #ifdef _WIN32
818 WSADATA Data;
819 int ret, err;
821 ret = WSAStartup(MAKEWORD(2,2), &Data);
822 if (ret != 0) {
823 err = WSAGetLastError();
824 fprintf(stderr, "WSAStartup: %d\n", err);
825 return -1;
827 atexit(socket_cleanup);
828 #endif
829 return 0;