hw/block-common: Factor out fall back to legacy -drive serial=...
[qemu/agraf.git] / qemu-sockets.c
blob2ae715db769be449f8ee9cb0ed4924a1fb7369ae
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 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <ctype.h>
19 #include <errno.h>
20 #include <unistd.h>
22 #include "qemu_socket.h"
23 #include "qemu-common.h" /* for qemu_isdigit */
25 #ifndef AI_ADDRCONFIG
26 # define AI_ADDRCONFIG 0
27 #endif
29 static const int on=1, off=0;
31 /* used temporarely until all users are converted to QemuOpts */
32 static QemuOptsList dummy_opts = {
33 .name = "dummy",
34 .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head),
35 .desc = {
37 .name = "path",
38 .type = QEMU_OPT_STRING,
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,
54 },{
55 .name = "block",
56 .type = QEMU_OPT_BOOL,
58 { /* end if list */ }
62 static int inet_getport(struct addrinfo *e)
64 struct sockaddr_in *i4;
65 struct sockaddr_in6 *i6;
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;
79 static void inet_setport(struct addrinfo *e, int port)
81 struct sockaddr_in *i4;
82 struct sockaddr_in6 *i6;
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;
96 const char *inet_strfamily(int family)
98 switch (family) {
99 case PF_INET6: return "ipv6";
100 case PF_INET: return "ipv4";
101 case PF_UNIX: return "unix";
103 return "unknown";
106 int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)
108 struct addrinfo ai,*res,*e;
109 const char *addr;
110 char port[33];
111 char uaddr[INET6_ADDRSTRLEN+1];
112 char uport[33];
113 int slisten, rc, to, port_min, port_max, p;
115 memset(&ai,0, sizeof(ai));
116 ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
117 ai.ai_family = PF_UNSPEC;
118 ai.ai_socktype = SOCK_STREAM;
120 if ((qemu_opt_get(opts, "host") == NULL) ||
121 (qemu_opt_get(opts, "port") == NULL)) {
122 fprintf(stderr, "%s: host and/or port not specified\n", __FUNCTION__);
123 error_set(errp, QERR_SOCKET_CREATE_FAILED);
124 return -1;
126 pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port"));
127 addr = qemu_opt_get(opts, "host");
129 to = qemu_opt_get_number(opts, "to", 0);
130 if (qemu_opt_get_bool(opts, "ipv4", 0))
131 ai.ai_family = PF_INET;
132 if (qemu_opt_get_bool(opts, "ipv6", 0))
133 ai.ai_family = PF_INET6;
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) {
140 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
141 gai_strerror(rc));
142 error_set(errp, QERR_SOCKET_CREATE_FAILED);
143 return -1;
146 /* create socket + bind */
147 for (e = res; e != NULL; e = e->ai_next) {
148 getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
149 uaddr,INET6_ADDRSTRLEN,uport,32,
150 NI_NUMERICHOST | NI_NUMERICSERV);
151 slisten = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol);
152 if (slisten < 0) {
153 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
154 inet_strfamily(e->ai_family), strerror(errno));
155 if (!e->ai_next) {
156 error_set(errp, QERR_SOCKET_CREATE_FAILED);
158 continue;
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 */
165 setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,
166 sizeof(off));
168 #endif
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);
174 if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {
175 goto listen;
177 if (p == port_max) {
178 fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__,
179 inet_strfamily(e->ai_family), uaddr, inet_getport(e),
180 strerror(errno));
181 if (!e->ai_next) {
182 error_set(errp, QERR_SOCKET_BIND_FAILED);
186 closesocket(slisten);
188 fprintf(stderr, "%s: FAILED\n", __FUNCTION__);
189 freeaddrinfo(res);
190 return -1;
192 listen:
193 if (listen(slisten,1) != 0) {
194 error_set(errp, QERR_SOCKET_LISTEN_FAILED);
195 perror("listen");
196 closesocket(slisten);
197 freeaddrinfo(res);
198 return -1;
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");
205 freeaddrinfo(res);
206 return slisten;
209 int inet_connect_opts(QemuOpts *opts, Error **errp)
211 struct addrinfo ai,*res,*e;
212 const char *addr;
213 const char *port;
214 char uaddr[INET6_ADDRSTRLEN+1];
215 char uport[33];
216 int sock,rc;
217 bool block;
219 memset(&ai,0, sizeof(ai));
220 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
221 ai.ai_family = PF_UNSPEC;
222 ai.ai_socktype = SOCK_STREAM;
224 addr = qemu_opt_get(opts, "host");
225 port = qemu_opt_get(opts, "port");
226 block = qemu_opt_get_bool(opts, "block", 0);
227 if (addr == NULL || port == NULL) {
228 fprintf(stderr, "inet_connect: host and/or port not specified\n");
229 error_set(errp, QERR_SOCKET_CREATE_FAILED);
230 return -1;
233 if (qemu_opt_get_bool(opts, "ipv4", 0))
234 ai.ai_family = PF_INET;
235 if (qemu_opt_get_bool(opts, "ipv6", 0))
236 ai.ai_family = PF_INET6;
238 /* lookup */
239 if (0 != (rc = getaddrinfo(addr, port, &ai, &res))) {
240 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
241 gai_strerror(rc));
242 error_set(errp, QERR_SOCKET_CREATE_FAILED);
243 return -1;
246 for (e = res; e != NULL; e = e->ai_next) {
247 if (getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
248 uaddr,INET6_ADDRSTRLEN,uport,32,
249 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
250 fprintf(stderr,"%s: getnameinfo: oops\n", __FUNCTION__);
251 continue;
253 sock = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol);
254 if (sock < 0) {
255 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
256 inet_strfamily(e->ai_family), strerror(errno));
257 continue;
259 setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
260 if (!block) {
261 socket_set_nonblock(sock);
263 /* connect to peer */
264 do {
265 rc = 0;
266 if (connect(sock, e->ai_addr, e->ai_addrlen) < 0) {
267 rc = -socket_error();
269 } while (rc == -EINTR);
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) {
279 if (NULL == e->ai_next)
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);
284 sock = -1;
285 continue;
287 freeaddrinfo(res);
288 return sock;
290 error_set(errp, QERR_SOCKET_CONNECT_FAILED);
291 freeaddrinfo(res);
292 return -1;
295 int inet_dgram_opts(QemuOpts *opts)
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;
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;
310 addr = qemu_opt_get(opts, "host");
311 port = qemu_opt_get(opts, "port");
312 if (addr == NULL || strlen(addr) == 0) {
313 addr = "localhost";
315 if (port == NULL || strlen(port) == 0) {
316 fprintf(stderr, "inet_dgram: port not specified\n");
317 return -1;
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;
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;
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;
337 addr = qemu_opt_get(opts, "localaddr");
338 port = qemu_opt_get(opts, "localport");
339 if (addr == NULL || strlen(addr) == 0) {
340 addr = NULL;
342 if (!port || strlen(port) == 0)
343 port = "0";
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;
351 /* create socket */
352 sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol);
353 if (sock < 0) {
354 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
355 inet_strfamily(peer->ai_family), strerror(errno));
356 goto err;
358 setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
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;
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;
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;
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;
387 freeaddrinfo(local);
388 freeaddrinfo(peer);
389 return sock;
391 err:
392 if (-1 != sock)
393 closesocket(sock);
394 if (local)
395 freeaddrinfo(local);
396 if (peer)
397 freeaddrinfo(peer);
398 return -1;
401 /* compatibility wrapper */
402 static int inet_parse(QemuOpts *opts, const char *str)
404 const char *optstr, *h;
405 char addr[64];
406 char port[33];
407 int pos;
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;
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;
425 qemu_opt_set(opts, "ipv6", "on");
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;
433 qemu_opt_set(opts, "ipv4", "on");
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;
442 qemu_opt_set(opts, "host", addr);
443 qemu_opt_set(opts, "port", port);
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"))
451 qemu_opt_set(opts, "ipv4", "on");
452 if (strstr(optstr, ",ipv6"))
453 qemu_opt_set(opts, "ipv6", "on");
454 return 0;
457 int inet_listen(const char *str, char *ostr, int olen,
458 int socktype, int port_offset, Error **errp)
460 QemuOpts *opts;
461 char *optstr;
462 int sock = -1;
464 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
465 if (inet_parse(opts, str) == 0) {
466 sock = inet_listen_opts(opts, port_offset, errp);
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 : "");
481 } else {
482 error_set(errp, QERR_SOCKET_CREATE_FAILED);
484 qemu_opts_del(opts);
485 return sock;
488 int inet_connect(const char *str, bool block, Error **errp)
490 QemuOpts *opts;
491 int sock = -1;
493 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
494 if (inet_parse(opts, str) == 0) {
495 if (block) {
496 qemu_opt_set(opts, "block", "on");
498 sock = inet_connect_opts(opts, errp);
499 } else {
500 error_set(errp, QERR_SOCKET_CREATE_FAILED);
502 qemu_opts_del(opts);
503 return sock;
506 #ifndef _WIN32
508 int unix_listen_opts(QemuOpts *opts)
510 struct sockaddr_un un;
511 const char *path = qemu_opt_get(opts, "path");
512 int sock, fd;
514 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
515 if (sock < 0) {
516 perror("socket(unix)");
517 return -1;
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");
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.
535 fd = mkstemp(un.sun_path); close(fd);
536 qemu_opt_set(opts, "path", un.sun_path);
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;
544 if (listen(sock, 1) < 0) {
545 fprintf(stderr, "listen(unix:%s): %s\n", un.sun_path, strerror(errno));
546 goto err;
549 return sock;
551 err:
552 closesocket(sock);
553 return -1;
556 int unix_connect_opts(QemuOpts *opts)
558 struct sockaddr_un un;
559 const char *path = qemu_opt_get(opts, "path");
560 int sock;
562 if (NULL == path) {
563 fprintf(stderr, "unix connect: no path specified\n");
564 return -1;
567 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
568 if (sock < 0) {
569 perror("socket(unix)");
570 return -1;
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));
578 close(sock);
579 return -1;
582 return sock;
585 /* compatibility wrapper */
586 int unix_listen(const char *str, char *ostr, int olen)
588 QemuOpts *opts;
589 char *path, *optstr;
590 int sock, len;
592 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
594 optstr = strchr(str, ',');
595 if (optstr) {
596 len = optstr - str;
597 if (len) {
598 path = g_malloc(len+1);
599 snprintf(path, len+1, "%.*s", len, str);
600 qemu_opt_set(opts, "path", path);
601 g_free(path);
603 } else {
604 qemu_opt_set(opts, "path", str);
607 sock = unix_listen_opts(opts);
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;
615 int unix_connect(const char *path)
617 QemuOpts *opts;
618 int sock;
620 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
621 qemu_opt_set(opts, "path", path);
622 sock = unix_connect_opts(opts);
623 qemu_opts_del(opts);
624 return sock;
627 #else
629 int unix_listen_opts(QemuOpts *opts)
631 fprintf(stderr, "unix sockets are not available on windows\n");
632 errno = ENOTSUP;
633 return -1;
636 int unix_connect_opts(QemuOpts *opts)
638 fprintf(stderr, "unix sockets are not available on windows\n");
639 errno = ENOTSUP;
640 return -1;
643 int unix_listen(const char *path, char *ostr, int olen)
645 fprintf(stderr, "unix sockets are not available on windows\n");
646 errno = ENOTSUP;
647 return -1;
650 int unix_connect(const char *path)
652 fprintf(stderr, "unix sockets are not available on windows\n");
653 errno = ENOTSUP;
654 return -1;
657 #endif
659 #ifdef _WIN32
660 static void socket_cleanup(void)
662 WSACleanup();
664 #endif
666 int socket_init(void)
668 #ifdef _WIN32
669 WSADATA Data;
670 int ret, err;
672 ret = WSAStartup(MAKEWORD(2,2), &Data);
673 if (ret != 0) {
674 err = WSAGetLastError();
675 fprintf(stderr, "WSAStartup: %d\n", err);
676 return -1;
678 atexit(socket_cleanup);
679 #endif
680 return 0;