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.
25 #include "qemu_socket.h"
26 #include "qemu-common.h" /* for qemu_isdigit */
27 #include "main-loop.h"
30 # define AI_ADDRCONFIG 0
33 static const int on
=1, off
=0;
35 /* used temporarely until all users are converted to QemuOpts */
36 static QemuOptsList dummy_opts
= {
38 .head
= QTAILQ_HEAD_INITIALIZER(dummy_opts
.head
),
42 .type
= QEMU_OPT_STRING
,
45 .type
= QEMU_OPT_STRING
,
48 .type
= QEMU_OPT_STRING
,
51 .type
= QEMU_OPT_NUMBER
,
54 .type
= QEMU_OPT_BOOL
,
57 .type
= QEMU_OPT_BOOL
,
63 static int inet_getport(struct addrinfo
*e
)
65 struct sockaddr_in
*i4
;
66 struct sockaddr_in6
*i6
;
68 switch (e
->ai_family
) {
70 i6
= (void*)e
->ai_addr
;
71 return ntohs(i6
->sin6_port
);
73 i4
= (void*)e
->ai_addr
;
74 return ntohs(i4
->sin_port
);
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
) {
87 i6
= (void*)e
->ai_addr
;
88 i6
->sin6_port
= htons(port
);
91 i4
= (void*)e
->ai_addr
;
92 i4
->sin_port
= htons(port
);
97 const char *inet_strfamily(int family
)
100 case PF_INET6
: return "ipv6";
101 case PF_INET
: return "ipv4";
102 case PF_UNIX
: return "unix";
107 int inet_listen_opts(QemuOpts
*opts
, int port_offset
, Error
**errp
)
109 struct addrinfo ai
,*res
,*e
;
112 char uaddr
[INET6_ADDRSTRLEN
+1];
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
);
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
;
138 snprintf(port
, sizeof(port
), "%d", atoi(port
) + port_offset
);
139 rc
= getaddrinfo(strlen(addr
) ? addr
: NULL
, port
, &ai
, &res
);
141 fprintf(stderr
,"getaddrinfo(%s,%s): %s\n", addr
, port
,
143 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
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
);
154 fprintf(stderr
,"%s: socket(%s): %s\n", __FUNCTION__
,
155 inet_strfamily(e
->ai_family
), strerror(errno
));
157 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
162 setsockopt(slisten
,SOL_SOCKET
,SO_REUSEADDR
,(void*)&on
,sizeof(on
));
164 if (e
->ai_family
== PF_INET6
) {
165 /* listen on both ipv4 and ipv6 */
166 setsockopt(slisten
,IPPROTO_IPV6
,IPV6_V6ONLY
,(void*)&off
,
171 port_min
= inet_getport(e
);
172 port_max
= to
? to
+ port_offset
: port_min
;
173 for (p
= port_min
; p
<= port_max
; p
++) {
175 if (bind(slisten
, e
->ai_addr
, e
->ai_addrlen
) == 0) {
179 fprintf(stderr
,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__
,
180 inet_strfamily(e
->ai_family
), uaddr
, inet_getport(e
),
183 error_set(errp
, QERR_SOCKET_BIND_FAILED
);
187 closesocket(slisten
);
189 fprintf(stderr
, "%s: FAILED\n", __FUNCTION__
);
194 if (listen(slisten
,1) != 0) {
195 error_set(errp
, QERR_SOCKET_LISTEN_FAILED
);
197 closesocket(slisten
);
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");
211 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
212 ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY)
214 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
215 ((rc) == -EINPROGRESS)
218 /* Struct to store connect state for non blocking connect */
219 typedef struct ConnectState
{
221 struct addrinfo
*addr_list
;
222 struct addrinfo
*current_addr
;
223 NonBlockingConnectHandler
*callback
;
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
;
234 socklen_t valsize
= sizeof(val
);
237 qemu_set_fd_handler2(s
->fd
, NULL
, NULL
, NULL
, NULL
);
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 */
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 */
264 freeaddrinfo(s
->addr_list
);
266 s
->callback(s
->fd
, s
->opaque
);
272 static int inet_connect_addr(struct addrinfo
*addr
, bool *in_progress
,
273 ConnectState
*connect_state
)
277 *in_progress
= false;
279 sock
= qemu_socket(addr
->ai_family
, addr
->ai_socktype
, addr
->ai_protocol
);
281 fprintf(stderr
, "%s: socket(%s): %s\n", __func__
,
282 inet_strfamily(addr
->ai_family
), strerror(errno
));
285 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
286 if (connect_state
!= NULL
) {
287 socket_set_nonblock(sock
);
289 /* connect to peer */
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
,
309 static struct addrinfo
*inet_parse_connect_opts(QemuOpts
*opts
, Error
**errp
)
311 struct addrinfo ai
, *res
;
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
) {
326 "inet_parse_connect_opts: host and/or port not specified\n");
327 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
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
;
339 rc
= getaddrinfo(addr
, port
, &ai
, &res
);
341 fprintf(stderr
, "getaddrinfo(%s,%s): %s\n", addr
, port
,
343 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
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
;
370 ConnectState
*connect_state
= NULL
;
372 res
= inet_parse_connect_opts(opts
, errp
);
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
);
391 } else if (sock
>= 0) {
392 /* non blocking socket immediate success, call callback */
393 if (callback
!= NULL
) {
394 callback(sock
, opaque
);
400 error_set(errp
, QERR_SOCKET_CONNECT_FAILED
);
402 g_free(connect_state
);
407 int inet_dgram_opts(QemuOpts
*opts
)
409 struct addrinfo ai
, *peer
= NULL
, *local
= NULL
;
412 char uaddr
[INET6_ADDRSTRLEN
+1];
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) {
427 if (port
== NULL
|| strlen(port
) == 0) {
428 fprintf(stderr
, "inet_dgram: port not specified\n");
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
,
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) {
454 if (!port
|| strlen(port
) == 0)
457 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &local
))) {
458 fprintf(stderr
,"getaddrinfo(%s,%s): %s\n", addr
, port
,
464 sock
= qemu_socket(peer
->ai_family
, peer
->ai_socktype
, peer
->ai_protocol
);
466 fprintf(stderr
,"%s: socket(%s): %s\n", __FUNCTION__
,
467 inet_strfamily(peer
->ai_family
), strerror(errno
));
470 setsockopt(sock
,SOL_SOCKET
,SO_REUSEADDR
,(void*)&on
,sizeof(on
));
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__
);
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
));
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__
);
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
));
513 /* compatibility wrapper */
514 static int inet_parse(QemuOpts
*opts
, const char *str
)
516 const char *optstr
, *h
;
525 if (1 != sscanf(str
,":%32[^,]%n",port
,&pos
)) {
526 fprintf(stderr
, "%s: portonly parse error (%s)\n",
530 } else if (str
[0] == '[') {
532 if (2 != sscanf(str
,"[%64[^]]]:%32[^,]%n",addr
,port
,&pos
)) {
533 fprintf(stderr
, "%s: ipv6 parse error (%s)\n",
537 qemu_opt_set(opts
, "ipv6", "on");
538 } else if (qemu_isdigit(str
[0])) {
540 if (2 != sscanf(str
,"%64[0-9.]:%32[^,]%n",addr
,port
,&pos
)) {
541 fprintf(stderr
, "%s: ipv4 parse error (%s)\n",
545 qemu_opt_set(opts
, "ipv4", "on");
548 if (2 != sscanf(str
,"%64[^:]:%32[^,]%n",addr
,port
,&pos
)) {
549 fprintf(stderr
, "%s: hostname parse error (%s)\n",
554 qemu_opt_set(opts
, "host", addr
);
555 qemu_opt_set(opts
, "port", port
);
559 h
= strstr(optstr
, ",to=");
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");
569 int inet_listen(const char *str
, char *ostr
, int olen
,
570 int socktype
, int port_offset
, Error
**errp
)
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
: "");
587 snprintf(ostr
, olen
, "%s:%s%s",
588 qemu_opt_get(opts
, "host"),
589 qemu_opt_get(opts
, "port"),
590 optstr
? optstr
: "");
594 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
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
)
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
);
617 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
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
628 * @str: address string
629 * @callback: callback function that is called when connect completes,
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
)
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
);
649 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
657 int unix_listen_opts(QemuOpts
*opts
)
659 struct sockaddr_un un
;
660 const char *path
= qemu_opt_get(opts
, "path");
663 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
665 perror("socket(unix)");
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
);
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
);
689 if (bind(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
690 fprintf(stderr
, "bind(unix:%s): %s\n", un
.sun_path
, strerror(errno
));
693 if (listen(sock
, 1) < 0) {
694 fprintf(stderr
, "listen(unix:%s): %s\n", un
.sun_path
, strerror(errno
));
705 int unix_connect_opts(QemuOpts
*opts
)
707 struct sockaddr_un un
;
708 const char *path
= qemu_opt_get(opts
, "path");
712 fprintf(stderr
, "unix connect: no path specified\n");
716 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
718 perror("socket(unix)");
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
));
734 /* compatibility wrapper */
735 int unix_listen(const char *str
, char *ostr
, int olen
)
741 opts
= qemu_opts_create(&dummy_opts
, NULL
, 0, NULL
);
743 optstr
= strchr(str
, ',');
747 path
= g_malloc(len
+1);
748 snprintf(path
, len
+1, "%.*s", len
, str
);
749 qemu_opt_set(opts
, "path", path
);
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
: "");
764 int unix_connect(const char *path
)
769 opts
= qemu_opts_create(&dummy_opts
, NULL
, 0, NULL
);
770 qemu_opt_set(opts
, "path", path
);
771 sock
= unix_connect_opts(opts
);
778 int unix_listen_opts(QemuOpts
*opts
)
780 fprintf(stderr
, "unix sockets are not available on windows\n");
785 int unix_connect_opts(QemuOpts
*opts
)
787 fprintf(stderr
, "unix sockets are not available on windows\n");
792 int unix_listen(const char *path
, char *ostr
, int olen
)
794 fprintf(stderr
, "unix sockets are not available on windows\n");
799 int unix_connect(const char *path
)
801 fprintf(stderr
, "unix sockets are not available on windows\n");
809 static void socket_cleanup(void)
815 int socket_init(void)
821 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
823 err
= WSAGetLastError();
824 fprintf(stderr
, "WSAStartup: %d\n", err
);
827 atexit(socket_cleanup
);