]> git.proxmox.com Git - qemu.git/blame - qemu-sockets.c
kvmvapic: Disable if there is insufficient memory
[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.
14 */
d247d25f
AL
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <ctype.h>
19#include <errno.h>
20#include <unistd.h>
21
22#include "qemu_socket.h"
47398b9c 23#include "qemu-common.h" /* for qemu_isdigit */
d247d25f
AL
24
25#ifndef AI_ADDRCONFIG
26# define AI_ADDRCONFIG 0
27#endif
28
d247d25f
AL
29static const int on=1, off=0;
30
2af2bf67 31/* used temporarely until all users are converted to QemuOpts */
c1390903 32static QemuOptsList dummy_opts = {
2af2bf67 33 .name = "dummy",
72cf2d4f 34 .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head),
2af2bf67
GH
35 .desc = {
36 {
37 .name = "path",
38 .type = QEMU_OPT_STRING,
f4c94c7c
GH
39 },{
40 .name = "host",
41 .type = QEMU_OPT_STRING,
42 },{
43 .name = "port",
44 .type = QEMU_OPT_STRING,
45 },{
46 .name = "to",
47 .type = QEMU_OPT_NUMBER,
48 },{
49 .name = "ipv4",
50 .type = QEMU_OPT_BOOL,
51 },{
52 .name = "ipv6",
53 .type = QEMU_OPT_BOOL,
a6ba35b3
AK
54 },{
55 .name = "block",
56 .type = QEMU_OPT_BOOL,
2af2bf67
GH
57 },
58 { /* end if list */ }
59 },
60};
61
d247d25f
AL
62static int inet_getport(struct addrinfo *e)
63{
64 struct sockaddr_in *i4;
65 struct sockaddr_in6 *i6;
66
67 switch (e->ai_family) {
68 case PF_INET6:
69 i6 = (void*)e->ai_addr;
70 return ntohs(i6->sin6_port);
71 case PF_INET:
72 i4 = (void*)e->ai_addr;
73 return ntohs(i4->sin_port);
74 default:
75 return 0;
76 }
77}
78
79static void inet_setport(struct addrinfo *e, int port)
80{
81 struct sockaddr_in *i4;
82 struct sockaddr_in6 *i6;
83
84 switch (e->ai_family) {
85 case PF_INET6:
86 i6 = (void*)e->ai_addr;
87 i6->sin6_port = htons(port);
88 break;
89 case PF_INET:
90 i4 = (void*)e->ai_addr;
91 i4->sin_port = htons(port);
92 break;
93 }
94}
95
c9c4b34e 96const char *inet_strfamily(int family)
d247d25f
AL
97{
98 switch (family) {
99 case PF_INET6: return "ipv6";
100 case PF_INET: return "ipv4";
101 case PF_UNIX: return "unix";
102 }
c445321e 103 return "unknown";
d247d25f
AL
104}
105
029409e5 106int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)
d247d25f
AL
107{
108 struct addrinfo ai,*res,*e;
e5bc776f 109 const char *addr;
d247d25f
AL
110 char port[33];
111 char uaddr[INET6_ADDRSTRLEN+1];
112 char uport[33];
877691f9 113 int slisten, rc, to, port_min, port_max, p;
d247d25f
AL
114
115 memset(&ai,0, sizeof(ai));
116 ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
117 ai.ai_family = PF_UNSPEC;
e5bc776f 118 ai.ai_socktype = SOCK_STREAM;
d247d25f 119
e23a22e6
JO
120 if ((qemu_opt_get(opts, "host") == NULL) ||
121 (qemu_opt_get(opts, "port") == NULL)) {
e5bc776f 122 fprintf(stderr, "%s: host and/or port not specified\n", __FUNCTION__);
029409e5 123 error_set(errp, QERR_SOCKET_CREATE_FAILED);
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) {
e5bc776f
GH
140 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
141 gai_strerror(rc));
029409e5 142 error_set(errp, QERR_SOCKET_CREATE_FAILED);
d247d25f
AL
143 return -1;
144 }
d247d25f
AL
145
146 /* create socket + bind */
147 for (e = res; e != NULL; e = e->ai_next) {
39b6efc8 148 getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
149 uaddr,INET6_ADDRSTRLEN,uport,32,
150 NI_NUMERICHOST | NI_NUMERICSERV);
40ff6d7e 151 slisten = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol);
39b6efc8 152 if (slisten < 0) {
d247d25f
AL
153 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
154 inet_strfamily(e->ai_family), strerror(errno));
029409e5
AK
155 if (!e->ai_next) {
156 error_set(errp, QERR_SOCKET_CREATE_FAILED);
157 }
39b6efc8 158 continue;
159 }
d247d25f
AL
160
161 setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
162#ifdef IPV6_V6ONLY
163 if (e->ai_family == PF_INET6) {
164 /* listen on both ipv4 and ipv6 */
39b6efc8 165 setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,
166 sizeof(off));
d247d25f
AL
167 }
168#endif
169
877691f9
MA
170 port_min = inet_getport(e);
171 port_max = to ? to + port_offset : port_min;
172 for (p = port_min; p <= port_max; p++) {
173 inet_setport(e, p);
d247d25f 174 if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {
d247d25f
AL
175 goto listen;
176 }
877691f9 177 if (p == port_max) {
d247d25f
AL
178 fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__,
179 inet_strfamily(e->ai_family), uaddr, inet_getport(e),
180 strerror(errno));
029409e5
AK
181 if (!e->ai_next) {
182 error_set(errp, QERR_SOCKET_BIND_FAILED);
183 }
d247d25f 184 }
d247d25f
AL
185 }
186 closesocket(slisten);
187 }
188 fprintf(stderr, "%s: FAILED\n", __FUNCTION__);
189 freeaddrinfo(res);
190 return -1;
191
192listen:
193 if (listen(slisten,1) != 0) {
029409e5 194 error_set(errp, QERR_SOCKET_LISTEN_FAILED);
d247d25f
AL
195 perror("listen");
196 closesocket(slisten);
39b6efc8 197 freeaddrinfo(res);
d247d25f
AL
198 return -1;
199 }
e5bc776f
GH
200 snprintf(uport, sizeof(uport), "%d", inet_getport(e) - port_offset);
201 qemu_opt_set(opts, "host", uaddr);
202 qemu_opt_set(opts, "port", uport);
203 qemu_opt_set(opts, "ipv6", (e->ai_family == PF_INET6) ? "on" : "off");
204 qemu_opt_set(opts, "ipv4", (e->ai_family != PF_INET6) ? "on" : "off");
d247d25f
AL
205 freeaddrinfo(res);
206 return slisten;
207}
208
a6ba35b3 209int inet_connect_opts(QemuOpts *opts, Error **errp)
d247d25f
AL
210{
211 struct addrinfo ai,*res,*e;
f4c94c7c
GH
212 const char *addr;
213 const char *port;
d247d25f
AL
214 char uaddr[INET6_ADDRSTRLEN+1];
215 char uport[33];
216 int sock,rc;
a6ba35b3 217 bool block;
d247d25f
AL
218
219 memset(&ai,0, sizeof(ai));
220 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
221 ai.ai_family = PF_UNSPEC;
f4c94c7c 222 ai.ai_socktype = SOCK_STREAM;
d247d25f 223
f4c94c7c
GH
224 addr = qemu_opt_get(opts, "host");
225 port = qemu_opt_get(opts, "port");
a6ba35b3 226 block = qemu_opt_get_bool(opts, "block", 0);
f4c94c7c
GH
227 if (addr == NULL || port == NULL) {
228 fprintf(stderr, "inet_connect: host and/or port not specified\n");
a6ba35b3 229 error_set(errp, QERR_SOCKET_CREATE_FAILED);
f4c94c7c 230 return -1;
d247d25f
AL
231 }
232
f4c94c7c 233 if (qemu_opt_get_bool(opts, "ipv4", 0))
d247d25f 234 ai.ai_family = PF_INET;
f4c94c7c 235 if (qemu_opt_get_bool(opts, "ipv6", 0))
d247d25f
AL
236 ai.ai_family = PF_INET6;
237
238 /* lookup */
239 if (0 != (rc = getaddrinfo(addr, port, &ai, &res))) {
f4c94c7c
GH
240 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
241 gai_strerror(rc));
a6ba35b3 242 error_set(errp, QERR_SOCKET_CREATE_FAILED);
d247d25f
AL
243 return -1;
244 }
d247d25f
AL
245
246 for (e = res; e != NULL; e = e->ai_next) {
39b6efc8 247 if (getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
248 uaddr,INET6_ADDRSTRLEN,uport,32,
249 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
d247d25f 250 fprintf(stderr,"%s: getnameinfo: oops\n", __FUNCTION__);
39b6efc8 251 continue;
252 }
40ff6d7e 253 sock = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol);
39b6efc8 254 if (sock < 0) {
d247d25f 255 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
39b6efc8 256 inet_strfamily(e->ai_family), strerror(errno));
257 continue;
258 }
d247d25f 259 setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
a6ba35b3
AK
260 if (!block) {
261 socket_set_nonblock(sock);
262 }
39b6efc8 263 /* connect to peer */
a6ba35b3
AK
264 do {
265 rc = 0;
266 if (connect(sock, e->ai_addr, e->ai_addrlen) < 0) {
267 rc = -socket_error();
268 }
269 } while (rc == -EINTR);
270
271 #ifdef _WIN32
272 if (!block && (rc == -EINPROGRESS || rc == -EWOULDBLOCK
273 || rc == -WSAEALREADY)) {
274 #else
275 if (!block && (rc == -EINPROGRESS)) {
276 #endif
277 error_set(errp, QERR_SOCKET_CONNECT_IN_PROGRESS);
278 } else if (rc < 0) {
136faa36 279 if (NULL == e->ai_next)
d247d25f
AL
280 fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
281 inet_strfamily(e->ai_family),
282 e->ai_canonname, uaddr, uport, strerror(errno));
283 closesocket(sock);
a6ba35b3 284 sock = -1;
39b6efc8 285 continue;
286 }
d247d25f 287 freeaddrinfo(res);
39b6efc8 288 return sock;
d247d25f 289 }
a6ba35b3 290 error_set(errp, QERR_SOCKET_CONNECT_FAILED);
d247d25f
AL
291 freeaddrinfo(res);
292 return -1;
293}
294
7e1b35b4
GH
295int inet_dgram_opts(QemuOpts *opts)
296{
297 struct addrinfo ai, *peer = NULL, *local = NULL;
298 const char *addr;
299 const char *port;
300 char uaddr[INET6_ADDRSTRLEN+1];
301 char uport[33];
302 int sock = -1, rc;
303
304 /* lookup peer addr */
305 memset(&ai,0, sizeof(ai));
306 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
307 ai.ai_family = PF_UNSPEC;
308 ai.ai_socktype = SOCK_DGRAM;
309
310 addr = qemu_opt_get(opts, "host");
311 port = qemu_opt_get(opts, "port");
312 if (addr == NULL || strlen(addr) == 0) {
313 addr = "localhost";
314 }
315 if (port == NULL || strlen(port) == 0) {
316 fprintf(stderr, "inet_dgram: port not specified\n");
317 return -1;
318 }
319
320 if (qemu_opt_get_bool(opts, "ipv4", 0))
321 ai.ai_family = PF_INET;
322 if (qemu_opt_get_bool(opts, "ipv6", 0))
323 ai.ai_family = PF_INET6;
324
325 if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) {
326 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
327 gai_strerror(rc));
328 return -1;
329 }
7e1b35b4
GH
330
331 /* lookup local addr */
332 memset(&ai,0, sizeof(ai));
333 ai.ai_flags = AI_PASSIVE;
334 ai.ai_family = peer->ai_family;
335 ai.ai_socktype = SOCK_DGRAM;
336
337 addr = qemu_opt_get(opts, "localaddr");
338 port = qemu_opt_get(opts, "localport");
339 if (addr == NULL || strlen(addr) == 0) {
340 addr = NULL;
341 }
342 if (!port || strlen(port) == 0)
343 port = "0";
344
345 if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) {
346 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
347 gai_strerror(rc));
348 return -1;
349 }
7e1b35b4
GH
350
351 /* create socket */
40ff6d7e 352 sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol);
7e1b35b4
GH
353 if (sock < 0) {
354 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
355 inet_strfamily(peer->ai_family), strerror(errno));
356 goto err;
357 }
358 setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
359
360 /* bind socket */
361 if (getnameinfo((struct sockaddr*)local->ai_addr,local->ai_addrlen,
362 uaddr,INET6_ADDRSTRLEN,uport,32,
363 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
364 fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
365 goto err;
366 }
367 if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) {
368 fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,
369 inet_strfamily(local->ai_family), uaddr, inet_getport(local));
370 goto err;
371 }
372
373 /* connect to peer */
374 if (getnameinfo((struct sockaddr*)peer->ai_addr, peer->ai_addrlen,
375 uaddr, INET6_ADDRSTRLEN, uport, 32,
376 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
377 fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
378 goto err;
379 }
380 if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) {
381 fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
382 inet_strfamily(peer->ai_family),
383 peer->ai_canonname, uaddr, uport, strerror(errno));
384 goto err;
385 }
386
387 freeaddrinfo(local);
388 freeaddrinfo(peer);
389 return sock;
390
391err:
392 if (-1 != sock)
393 closesocket(sock);
394 if (local)
395 freeaddrinfo(local);
396 if (peer)
397 freeaddrinfo(peer);
398 return -1;
399}
400
f4c94c7c
GH
401/* compatibility wrapper */
402static int inet_parse(QemuOpts *opts, const char *str)
403{
404 const char *optstr, *h;
405 char addr[64];
406 char port[33];
407 int pos;
408
409 /* parse address */
410 if (str[0] == ':') {
411 /* no host given */
412 addr[0] = '\0';
413 if (1 != sscanf(str,":%32[^,]%n",port,&pos)) {
414 fprintf(stderr, "%s: portonly parse error (%s)\n",
415 __FUNCTION__, str);
416 return -1;
417 }
418 } else if (str[0] == '[') {
419 /* IPv6 addr */
420 if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) {
421 fprintf(stderr, "%s: ipv6 parse error (%s)\n",
422 __FUNCTION__, str);
423 return -1;
424 }
2198a62e 425 qemu_opt_set(opts, "ipv6", "on");
f4c94c7c
GH
426 } else if (qemu_isdigit(str[0])) {
427 /* IPv4 addr */
428 if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) {
429 fprintf(stderr, "%s: ipv4 parse error (%s)\n",
430 __FUNCTION__, str);
431 return -1;
432 }
2198a62e 433 qemu_opt_set(opts, "ipv4", "on");
f4c94c7c
GH
434 } else {
435 /* hostname */
436 if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) {
437 fprintf(stderr, "%s: hostname parse error (%s)\n",
438 __FUNCTION__, str);
439 return -1;
440 }
441 }
442 qemu_opt_set(opts, "host", addr);
443 qemu_opt_set(opts, "port", port);
444
445 /* parse options */
446 optstr = str + pos;
447 h = strstr(optstr, ",to=");
448 if (h)
449 qemu_opt_set(opts, "to", h+4);
450 if (strstr(optstr, ",ipv4"))
2198a62e 451 qemu_opt_set(opts, "ipv4", "on");
f4c94c7c 452 if (strstr(optstr, ",ipv6"))
2198a62e 453 qemu_opt_set(opts, "ipv6", "on");
f4c94c7c
GH
454 return 0;
455}
456
e5bc776f 457int inet_listen(const char *str, char *ostr, int olen,
029409e5 458 int socktype, int port_offset, Error **errp)
e5bc776f
GH
459{
460 QemuOpts *opts;
461 char *optstr;
462 int sock = -1;
463
464 opts = qemu_opts_create(&dummy_opts, NULL, 0);
465 if (inet_parse(opts, str) == 0) {
029409e5 466 sock = inet_listen_opts(opts, port_offset, errp);
e5bc776f
GH
467 if (sock != -1 && ostr) {
468 optstr = strchr(str, ',');
469 if (qemu_opt_get_bool(opts, "ipv6", 0)) {
470 snprintf(ostr, olen, "[%s]:%s%s",
471 qemu_opt_get(opts, "host"),
472 qemu_opt_get(opts, "port"),
473 optstr ? optstr : "");
474 } else {
475 snprintf(ostr, olen, "%s:%s%s",
476 qemu_opt_get(opts, "host"),
477 qemu_opt_get(opts, "port"),
478 optstr ? optstr : "");
479 }
480 }
029409e5
AK
481 } else {
482 error_set(errp, QERR_SOCKET_CREATE_FAILED);
e5bc776f
GH
483 }
484 qemu_opts_del(opts);
485 return sock;
486}
487
a6ba35b3 488int inet_connect(const char *str, bool block, Error **errp)
f4c94c7c
GH
489{
490 QemuOpts *opts;
491 int sock = -1;
492
493 opts = qemu_opts_create(&dummy_opts, NULL, 0);
a6ba35b3
AK
494 if (inet_parse(opts, str) == 0) {
495 if (block) {
496 qemu_opt_set(opts, "block", "on");
497 }
498 sock = inet_connect_opts(opts, errp);
499 } else {
500 error_set(errp, QERR_SOCKET_CREATE_FAILED);
501 }
f4c94c7c
GH
502 qemu_opts_del(opts);
503 return sock;
504}
505
d247d25f
AL
506#ifndef _WIN32
507
62b6adfb 508int unix_listen_opts(QemuOpts *opts)
d247d25f
AL
509{
510 struct sockaddr_un un;
62b6adfb
GH
511 const char *path = qemu_opt_get(opts, "path");
512 int sock, fd;
d247d25f 513
40ff6d7e 514 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
d247d25f 515 if (sock < 0) {
39b6efc8 516 perror("socket(unix)");
517 return -1;
d247d25f
AL
518 }
519
d247d25f
AL
520 memset(&un, 0, sizeof(un));
521 un.sun_family = AF_UNIX;
522 if (path && strlen(path)) {
523 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
524 } else {
525 char *tmpdir = getenv("TMPDIR");
526 snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX",
527 tmpdir ? tmpdir : "/tmp");
528 /*
529 * This dummy fd usage silences the mktemp() unsecure warning.
530 * Using mkstemp() doesn't make things more secure here
531 * though. bind() complains about existing files, so we have
532 * to unlink first and thus re-open the race window. The
533 * worst case possible is bind() failing, i.e. a DoS attack.
534 */
535 fd = mkstemp(un.sun_path); close(fd);
62b6adfb 536 qemu_opt_set(opts, "path", un.sun_path);
d247d25f 537 }
d247d25f
AL
538
539 unlink(un.sun_path);
540 if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
541 fprintf(stderr, "bind(unix:%s): %s\n", un.sun_path, strerror(errno));
542 goto err;
543 }
544 if (listen(sock, 1) < 0) {
545 fprintf(stderr, "listen(unix:%s): %s\n", un.sun_path, strerror(errno));
546 goto err;
547 }
548
d247d25f
AL
549 return sock;
550
551err:
d247d25f
AL
552 closesocket(sock);
553 return -1;
554}
555
2af2bf67 556int unix_connect_opts(QemuOpts *opts)
d247d25f
AL
557{
558 struct sockaddr_un un;
2af2bf67 559 const char *path = qemu_opt_get(opts, "path");
d247d25f
AL
560 int sock;
561
2af2bf67
GH
562 if (NULL == path) {
563 fprintf(stderr, "unix connect: no path specified\n");
564 return -1;
565 }
566
40ff6d7e 567 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
d247d25f 568 if (sock < 0) {
39b6efc8 569 perror("socket(unix)");
570 return -1;
d247d25f
AL
571 }
572
573 memset(&un, 0, sizeof(un));
574 un.sun_family = AF_UNIX;
575 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
576 if (connect(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
577 fprintf(stderr, "connect(unix:%s): %s\n", path, strerror(errno));
9d947472 578 close(sock);
d247d25f
AL
579 return -1;
580 }
581
d247d25f
AL
582 return sock;
583}
584
2af2bf67 585/* compatibility wrapper */
62b6adfb
GH
586int unix_listen(const char *str, char *ostr, int olen)
587{
588 QemuOpts *opts;
589 char *path, *optstr;
590 int sock, len;
591
592 opts = qemu_opts_create(&dummy_opts, NULL, 0);
593
594 optstr = strchr(str, ',');
595 if (optstr) {
596 len = optstr - str;
597 if (len) {
7267c094 598 path = g_malloc(len+1);
62b6adfb
GH
599 snprintf(path, len+1, "%.*s", len, str);
600 qemu_opt_set(opts, "path", path);
7267c094 601 g_free(path);
62b6adfb
GH
602 }
603 } else {
604 qemu_opt_set(opts, "path", str);
605 }
606
607 sock = unix_listen_opts(opts);
608
609 if (sock != -1 && ostr)
610 snprintf(ostr, olen, "%s%s", qemu_opt_get(opts, "path"), optstr ? optstr : "");
611 qemu_opts_del(opts);
612 return sock;
613}
614
2af2bf67
GH
615int unix_connect(const char *path)
616{
617 QemuOpts *opts;
618 int sock;
619
620 opts = qemu_opts_create(&dummy_opts, NULL, 0);
621 qemu_opt_set(opts, "path", path);
622 sock = unix_connect_opts(opts);
623 qemu_opts_del(opts);
624 return sock;
625}
626
d247d25f
AL
627#else
628
108af7b9
GH
629int unix_listen_opts(QemuOpts *opts)
630{
631 fprintf(stderr, "unix sockets are not available on windows\n");
b82eac92 632 errno = ENOTSUP;
108af7b9
GH
633 return -1;
634}
635
636int unix_connect_opts(QemuOpts *opts)
637{
638 fprintf(stderr, "unix sockets are not available on windows\n");
b82eac92 639 errno = ENOTSUP;
108af7b9
GH
640 return -1;
641}
642
d247d25f
AL
643int unix_listen(const char *path, char *ostr, int olen)
644{
645 fprintf(stderr, "unix sockets are not available on windows\n");
b82eac92 646 errno = ENOTSUP;
d247d25f
AL
647 return -1;
648}
649
650int unix_connect(const char *path)
651{
652 fprintf(stderr, "unix sockets are not available on windows\n");
b82eac92 653 errno = ENOTSUP;
d247d25f
AL
654 return -1;
655}
656
657#endif
0706a4dc
PB
658
659#ifdef _WIN32
660static void socket_cleanup(void)
661{
662 WSACleanup();
663}
664#endif
665
666int socket_init(void)
667{
668#ifdef _WIN32
669 WSADATA Data;
670 int ret, err;
671
672 ret = WSAStartup(MAKEWORD(2,2), &Data);
673 if (ret != 0) {
674 err = WSAGetLastError();
675 fprintf(stderr, "WSAStartup: %d\n", err);
676 return -1;
677 }
678 atexit(socket_cleanup);
679#endif
680 return 0;
681}