]> git.proxmox.com Git - mirror_qemu.git/blob - qemu-sockets.c
Rename target_phys_addr_t to hwaddr
[mirror_qemu.git] / qemu-sockets.c
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 "qemu_socket.h"
26 #include "qemu-common.h" /* for qemu_isdigit */
27 #include "main-loop.h"
28
29 #ifndef AI_ADDRCONFIG
30 # define AI_ADDRCONFIG 0
31 #endif
32
33 static const int on=1, off=0;
34
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 = {
40 {
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,
58 },
59 { /* end if list */ }
60 },
61 };
62
63 static 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
80 static 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
97 const char *inet_strfamily(int family)
98 {
99 switch (family) {
100 case PF_INET6: return "ipv6";
101 case PF_INET: return "ipv4";
102 case PF_UNIX: return "unix";
103 }
104 return "unknown";
105 }
106
107 int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)
108 {
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;
115
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;
120
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;
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 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
142 gai_strerror(rc));
143 error_set(errp, QERR_SOCKET_CREATE_FAILED);
144 return -1;
145 }
146
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);
158 }
159 continue;
160 }
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 */
166 setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,
167 sizeof(off));
168 }
169 #endif
170
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;
177 }
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);
184 }
185 }
186 }
187 closesocket(slisten);
188 }
189 fprintf(stderr, "%s: FAILED\n", __FUNCTION__);
190 freeaddrinfo(res);
191 return -1;
192
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;
200 }
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;
208 }
209
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
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;
226
227 static int inet_connect_addr(struct addrinfo *addr, bool *in_progress,
228 ConnectState *connect_state);
229
230 static void wait_for_connect(void *opaque)
231 {
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 }
263
264 freeaddrinfo(s->addr_list);
265 if (s->callback) {
266 s->callback(s->fd, s->opaque);
267 }
268 g_free(s);
269 }
270
271 static int inet_connect_addr(struct addrinfo *addr, bool *in_progress,
272 ConnectState *connect_state)
273 {
274 int sock, rc;
275
276 *in_progress = false;
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 }
284 qemu_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
285 if (connect_state != NULL) {
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
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;
301 } else if (rc < 0) {
302 closesocket(sock);
303 return -1;
304 }
305 return sock;
306 }
307
308 static struct addrinfo *inet_parse_connect_opts(QemuOpts *opts, Error **errp)
309 {
310 struct addrinfo ai, *res;
311 int rc;
312 const char *addr;
313 const char *port;
314
315 memset(&ai, 0, sizeof(ai));
316
317 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
318 ai.ai_family = PF_UNSPEC;
319 ai.ai_socktype = SOCK_STREAM;
320
321 addr = qemu_opt_get(opts, "host");
322 port = qemu_opt_get(opts, "port");
323 if (addr == NULL || port == NULL) {
324 fprintf(stderr,
325 "inet_parse_connect_opts: host and/or port not specified\n");
326 error_set(errp, QERR_SOCKET_CREATE_FAILED);
327 return NULL;
328 }
329
330 if (qemu_opt_get_bool(opts, "ipv4", 0)) {
331 ai.ai_family = PF_INET;
332 }
333 if (qemu_opt_get_bool(opts, "ipv6", 0)) {
334 ai.ai_family = PF_INET6;
335 }
336
337 /* lookup */
338 rc = getaddrinfo(addr, port, &ai, &res);
339 if (rc != 0) {
340 fprintf(stderr, "getaddrinfo(%s,%s): %s\n", addr, port,
341 gai_strerror(rc));
342 error_set(errp, QERR_SOCKET_CREATE_FAILED);
343 return NULL;
344 }
345 return res;
346 }
347
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".
353 * @errp: set on error
354 * @callback: callback function for non-blocking connect
355 * @opaque: opaque for callback function
356 *
357 * Returns: -1 on error, file descriptor on success.
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.
362 */
363 int inet_connect_opts(QemuOpts *opts, Error **errp,
364 NonBlockingConnectHandler *callback, void *opaque)
365 {
366 struct addrinfo *res, *e;
367 int sock = -1;
368 bool in_progress;
369 ConnectState *connect_state = NULL;
370
371 res = inet_parse_connect_opts(opts, errp);
372 if (!res) {
373 return -1;
374 }
375
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;
381 }
382
383 for (e = res; e != NULL; e = e->ai_next) {
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 }
395 break;
396 }
397 }
398 if (sock < 0) {
399 error_set(errp, QERR_SOCKET_CONNECT_FAILED);
400 }
401 g_free(connect_state);
402 freeaddrinfo(res);
403 return sock;
404 }
405
406 int 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 }
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));
459 goto err;
460 }
461
462 /* create socket */
463 sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol);
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
502 err:
503 if (-1 != sock)
504 closesocket(sock);
505 if (local)
506 freeaddrinfo(local);
507 if (peer)
508 freeaddrinfo(peer);
509 return -1;
510 }
511
512 /* compatibility wrapper */
513 static 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 }
536 qemu_opt_set(opts, "ipv6", "on");
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 }
544 qemu_opt_set(opts, "ipv4", "on");
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"))
562 qemu_opt_set(opts, "ipv4", "on");
563 if (strstr(optstr, ",ipv6"))
564 qemu_opt_set(opts, "ipv6", "on");
565 return 0;
566 }
567
568 int inet_listen(const char *str, char *ostr, int olen,
569 int socktype, int port_offset, Error **errp)
570 {
571 QemuOpts *opts;
572 char *optstr;
573 int sock = -1;
574
575 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
576 if (inet_parse(opts, str) == 0) {
577 sock = inet_listen_opts(opts, port_offset, errp);
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 }
592 } else {
593 error_set(errp, QERR_SOCKET_CREATE_FAILED);
594 }
595 qemu_opts_del(opts);
596 return sock;
597 }
598
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 **/
607 int inet_connect(const char *str, Error **errp)
608 {
609 QemuOpts *opts;
610 int sock = -1;
611
612 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
613 if (inet_parse(opts, str) == 0) {
614 sock = inet_connect_opts(opts, errp, NULL, NULL);
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.
624 * Calls the callback function with fd in case of success or -1 in case of
625 * error.
626 *
627 * @str: address string
628 * @callback: callback function that is called when connect completes,
629 * cannot be NULL.
630 * @opaque: opaque for callback function
631 * @errp: set in case of an error
632 *
633 * Returns: -1 on immediate error, file descriptor on success.
634 **/
635 int inet_nonblocking_connect(const char *str,
636 NonBlockingConnectHandler *callback,
637 void *opaque, Error **errp)
638 {
639 QemuOpts *opts;
640 int sock = -1;
641
642 g_assert(callback != NULL);
643
644 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
645 if (inet_parse(opts, str) == 0) {
646 sock = inet_connect_opts(opts, errp, callback, opaque);
647 } else {
648 error_set(errp, QERR_SOCKET_CREATE_FAILED);
649 }
650 qemu_opts_del(opts);
651 return sock;
652 }
653
654 #ifndef _WIN32
655
656 int unix_listen_opts(QemuOpts *opts)
657 {
658 struct sockaddr_un un;
659 const char *path = qemu_opt_get(opts, "path");
660 int sock, fd;
661
662 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
663 if (sock < 0) {
664 perror("socket(unix)");
665 return -1;
666 }
667
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);
684 qemu_opt_set(opts, "path", un.sun_path);
685 }
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
697 return sock;
698
699 err:
700 closesocket(sock);
701 return -1;
702 }
703
704 int unix_connect_opts(QemuOpts *opts)
705 {
706 struct sockaddr_un un;
707 const char *path = qemu_opt_get(opts, "path");
708 int sock;
709
710 if (NULL == path) {
711 fprintf(stderr, "unix connect: no path specified\n");
712 return -1;
713 }
714
715 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
716 if (sock < 0) {
717 perror("socket(unix)");
718 return -1;
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));
726 close(sock);
727 return -1;
728 }
729
730 return sock;
731 }
732
733 /* compatibility wrapper */
734 int unix_listen(const char *str, char *ostr, int olen)
735 {
736 QemuOpts *opts;
737 char *path, *optstr;
738 int sock, len;
739
740 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
741
742 optstr = strchr(str, ',');
743 if (optstr) {
744 len = optstr - str;
745 if (len) {
746 path = g_malloc(len+1);
747 snprintf(path, len+1, "%.*s", len, str);
748 qemu_opt_set(opts, "path", path);
749 g_free(path);
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
763 int unix_connect(const char *path)
764 {
765 QemuOpts *opts;
766 int sock;
767
768 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
769 qemu_opt_set(opts, "path", path);
770 sock = unix_connect_opts(opts);
771 qemu_opts_del(opts);
772 return sock;
773 }
774
775 #else
776
777 int unix_listen_opts(QemuOpts *opts)
778 {
779 fprintf(stderr, "unix sockets are not available on windows\n");
780 errno = ENOTSUP;
781 return -1;
782 }
783
784 int unix_connect_opts(QemuOpts *opts)
785 {
786 fprintf(stderr, "unix sockets are not available on windows\n");
787 errno = ENOTSUP;
788 return -1;
789 }
790
791 int unix_listen(const char *path, char *ostr, int olen)
792 {
793 fprintf(stderr, "unix sockets are not available on windows\n");
794 errno = ENOTSUP;
795 return -1;
796 }
797
798 int unix_connect(const char *path)
799 {
800 fprintf(stderr, "unix sockets are not available on windows\n");
801 errno = ENOTSUP;
802 return -1;
803 }
804
805 #endif
806
807 #ifdef _WIN32
808 static void socket_cleanup(void)
809 {
810 WSACleanup();
811 }
812 #endif
813
814 int 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 }