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 */
29 # define AI_ADDRCONFIG 0
32 static const int on
=1, off
=0;
34 /* used temporarely until all users are converted to QemuOpts */
35 static QemuOptsList dummy_opts
= {
37 .head
= QTAILQ_HEAD_INITIALIZER(dummy_opts
.head
),
41 .type
= QEMU_OPT_STRING
,
44 .type
= QEMU_OPT_STRING
,
47 .type
= QEMU_OPT_STRING
,
50 .type
= QEMU_OPT_NUMBER
,
53 .type
= QEMU_OPT_BOOL
,
56 .type
= QEMU_OPT_BOOL
,
59 .type
= QEMU_OPT_BOOL
,
65 static int inet_getport(struct addrinfo
*e
)
67 struct sockaddr_in
*i4
;
68 struct sockaddr_in6
*i6
;
70 switch (e
->ai_family
) {
72 i6
= (void*)e
->ai_addr
;
73 return ntohs(i6
->sin6_port
);
75 i4
= (void*)e
->ai_addr
;
76 return ntohs(i4
->sin_port
);
82 static void inet_setport(struct addrinfo
*e
, int port
)
84 struct sockaddr_in
*i4
;
85 struct sockaddr_in6
*i6
;
87 switch (e
->ai_family
) {
89 i6
= (void*)e
->ai_addr
;
90 i6
->sin6_port
= htons(port
);
93 i4
= (void*)e
->ai_addr
;
94 i4
->sin_port
= htons(port
);
99 const char *inet_strfamily(int family
)
102 case PF_INET6
: return "ipv6";
103 case PF_INET
: return "ipv4";
104 case PF_UNIX
: return "unix";
109 int inet_listen_opts(QemuOpts
*opts
, int port_offset
, Error
**errp
)
111 struct addrinfo ai
,*res
,*e
;
114 char uaddr
[INET6_ADDRSTRLEN
+1];
116 int slisten
, rc
, to
, port_min
, port_max
, p
;
118 memset(&ai
,0, sizeof(ai
));
119 ai
.ai_flags
= AI_PASSIVE
| AI_ADDRCONFIG
;
120 ai
.ai_family
= PF_UNSPEC
;
121 ai
.ai_socktype
= SOCK_STREAM
;
123 if ((qemu_opt_get(opts
, "host") == NULL
) ||
124 (qemu_opt_get(opts
, "port") == NULL
)) {
125 fprintf(stderr
, "%s: host and/or port not specified\n", __FUNCTION__
);
126 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
129 pstrcpy(port
, sizeof(port
), qemu_opt_get(opts
, "port"));
130 addr
= qemu_opt_get(opts
, "host");
132 to
= qemu_opt_get_number(opts
, "to", 0);
133 if (qemu_opt_get_bool(opts
, "ipv4", 0))
134 ai
.ai_family
= PF_INET
;
135 if (qemu_opt_get_bool(opts
, "ipv6", 0))
136 ai
.ai_family
= PF_INET6
;
140 snprintf(port
, sizeof(port
), "%d", atoi(port
) + port_offset
);
141 rc
= getaddrinfo(strlen(addr
) ? addr
: NULL
, port
, &ai
, &res
);
143 fprintf(stderr
,"getaddrinfo(%s,%s): %s\n", addr
, port
,
145 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
149 /* create socket + bind */
150 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
151 getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
152 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
153 NI_NUMERICHOST
| NI_NUMERICSERV
);
154 slisten
= qemu_socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
156 fprintf(stderr
,"%s: socket(%s): %s\n", __FUNCTION__
,
157 inet_strfamily(e
->ai_family
), strerror(errno
));
159 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
164 setsockopt(slisten
,SOL_SOCKET
,SO_REUSEADDR
,(void*)&on
,sizeof(on
));
166 if (e
->ai_family
== PF_INET6
) {
167 /* listen on both ipv4 and ipv6 */
168 setsockopt(slisten
,IPPROTO_IPV6
,IPV6_V6ONLY
,(void*)&off
,
173 port_min
= inet_getport(e
);
174 port_max
= to
? to
+ port_offset
: port_min
;
175 for (p
= port_min
; p
<= port_max
; p
++) {
177 if (bind(slisten
, e
->ai_addr
, e
->ai_addrlen
) == 0) {
181 fprintf(stderr
,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__
,
182 inet_strfamily(e
->ai_family
), uaddr
, inet_getport(e
),
185 error_set(errp
, QERR_SOCKET_BIND_FAILED
);
189 closesocket(slisten
);
191 fprintf(stderr
, "%s: FAILED\n", __FUNCTION__
);
196 if (listen(slisten
,1) != 0) {
197 error_set(errp
, QERR_SOCKET_LISTEN_FAILED
);
199 closesocket(slisten
);
203 snprintf(uport
, sizeof(uport
), "%d", inet_getport(e
) - port_offset
);
204 qemu_opt_set(opts
, "host", uaddr
);
205 qemu_opt_set(opts
, "port", uport
);
206 qemu_opt_set(opts
, "ipv6", (e
->ai_family
== PF_INET6
) ? "on" : "off");
207 qemu_opt_set(opts
, "ipv4", (e
->ai_family
!= PF_INET6
) ? "on" : "off");
213 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
214 ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY)
216 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
217 ((rc) == -EINPROGRESS)
220 static int inet_connect_addr(struct addrinfo
*addr
, bool block
,
226 *in_progress
= false;
229 sock
= qemu_socket(addr
->ai_family
, addr
->ai_socktype
, addr
->ai_protocol
);
231 fprintf(stderr
, "%s: socket(%s): %s\n", __func__
,
232 inet_strfamily(addr
->ai_family
), strerror(errno
));
235 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
237 socket_set_nonblock(sock
);
239 /* connect to peer */
242 if (connect(sock
, addr
->ai_addr
, addr
->ai_addrlen
) < 0) {
243 rc
= -socket_error();
245 } while (rc
== -EINTR
);
247 if (!block
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
258 static struct addrinfo
*inet_parse_connect_opts(QemuOpts
*opts
, Error
**errp
)
260 struct addrinfo ai
, *res
;
265 memset(&ai
, 0, sizeof(ai
));
266 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
267 ai
.ai_family
= PF_UNSPEC
;
268 ai
.ai_socktype
= SOCK_STREAM
;
270 addr
= qemu_opt_get(opts
, "host");
271 port
= qemu_opt_get(opts
, "port");
272 if (addr
== NULL
|| port
== NULL
) {
274 "inet_parse_connect_opts: host and/or port not specified\n");
275 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
279 if (qemu_opt_get_bool(opts
, "ipv4", 0)) {
280 ai
.ai_family
= PF_INET
;
282 if (qemu_opt_get_bool(opts
, "ipv6", 0)) {
283 ai
.ai_family
= PF_INET6
;
287 rc
= getaddrinfo(addr
, port
, &ai
, &res
);
289 fprintf(stderr
, "getaddrinfo(%s,%s): %s\n", addr
, port
,
291 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
297 int inet_connect_opts(QemuOpts
*opts
, bool *in_progress
, Error
**errp
)
299 struct addrinfo
*res
, *e
;
301 bool block
= qemu_opt_get_bool(opts
, "block", 0);
303 res
= inet_parse_connect_opts(opts
, errp
);
309 *in_progress
= false;
312 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
313 sock
= inet_connect_addr(e
, block
, in_progress
);
319 error_set(errp
, QERR_SOCKET_CONNECT_FAILED
);
325 int inet_dgram_opts(QemuOpts
*opts
)
327 struct addrinfo ai
, *peer
= NULL
, *local
= NULL
;
330 char uaddr
[INET6_ADDRSTRLEN
+1];
334 /* lookup peer addr */
335 memset(&ai
,0, sizeof(ai
));
336 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
337 ai
.ai_family
= PF_UNSPEC
;
338 ai
.ai_socktype
= SOCK_DGRAM
;
340 addr
= qemu_opt_get(opts
, "host");
341 port
= qemu_opt_get(opts
, "port");
342 if (addr
== NULL
|| strlen(addr
) == 0) {
345 if (port
== NULL
|| strlen(port
) == 0) {
346 fprintf(stderr
, "inet_dgram: port not specified\n");
350 if (qemu_opt_get_bool(opts
, "ipv4", 0))
351 ai
.ai_family
= PF_INET
;
352 if (qemu_opt_get_bool(opts
, "ipv6", 0))
353 ai
.ai_family
= PF_INET6
;
355 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &peer
))) {
356 fprintf(stderr
,"getaddrinfo(%s,%s): %s\n", addr
, port
,
361 /* lookup local addr */
362 memset(&ai
,0, sizeof(ai
));
363 ai
.ai_flags
= AI_PASSIVE
;
364 ai
.ai_family
= peer
->ai_family
;
365 ai
.ai_socktype
= SOCK_DGRAM
;
367 addr
= qemu_opt_get(opts
, "localaddr");
368 port
= qemu_opt_get(opts
, "localport");
369 if (addr
== NULL
|| strlen(addr
) == 0) {
372 if (!port
|| strlen(port
) == 0)
375 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &local
))) {
376 fprintf(stderr
,"getaddrinfo(%s,%s): %s\n", addr
, port
,
382 sock
= qemu_socket(peer
->ai_family
, peer
->ai_socktype
, peer
->ai_protocol
);
384 fprintf(stderr
,"%s: socket(%s): %s\n", __FUNCTION__
,
385 inet_strfamily(peer
->ai_family
), strerror(errno
));
388 setsockopt(sock
,SOL_SOCKET
,SO_REUSEADDR
,(void*)&on
,sizeof(on
));
391 if (getnameinfo((struct sockaddr
*)local
->ai_addr
,local
->ai_addrlen
,
392 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
393 NI_NUMERICHOST
| NI_NUMERICSERV
) != 0) {
394 fprintf(stderr
, "%s: getnameinfo: oops\n", __FUNCTION__
);
397 if (bind(sock
, local
->ai_addr
, local
->ai_addrlen
) < 0) {
398 fprintf(stderr
,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__
,
399 inet_strfamily(local
->ai_family
), uaddr
, inet_getport(local
));
403 /* connect to peer */
404 if (getnameinfo((struct sockaddr
*)peer
->ai_addr
, peer
->ai_addrlen
,
405 uaddr
, INET6_ADDRSTRLEN
, uport
, 32,
406 NI_NUMERICHOST
| NI_NUMERICSERV
) != 0) {
407 fprintf(stderr
, "%s: getnameinfo: oops\n", __FUNCTION__
);
410 if (connect(sock
,peer
->ai_addr
,peer
->ai_addrlen
) < 0) {
411 fprintf(stderr
, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__
,
412 inet_strfamily(peer
->ai_family
),
413 peer
->ai_canonname
, uaddr
, uport
, strerror(errno
));
431 /* compatibility wrapper */
432 static int inet_parse(QemuOpts
*opts
, const char *str
)
434 const char *optstr
, *h
;
443 if (1 != sscanf(str
,":%32[^,]%n",port
,&pos
)) {
444 fprintf(stderr
, "%s: portonly parse error (%s)\n",
448 } else if (str
[0] == '[') {
450 if (2 != sscanf(str
,"[%64[^]]]:%32[^,]%n",addr
,port
,&pos
)) {
451 fprintf(stderr
, "%s: ipv6 parse error (%s)\n",
455 qemu_opt_set(opts
, "ipv6", "on");
456 } else if (qemu_isdigit(str
[0])) {
458 if (2 != sscanf(str
,"%64[0-9.]:%32[^,]%n",addr
,port
,&pos
)) {
459 fprintf(stderr
, "%s: ipv4 parse error (%s)\n",
463 qemu_opt_set(opts
, "ipv4", "on");
466 if (2 != sscanf(str
,"%64[^:]:%32[^,]%n",addr
,port
,&pos
)) {
467 fprintf(stderr
, "%s: hostname parse error (%s)\n",
472 qemu_opt_set(opts
, "host", addr
);
473 qemu_opt_set(opts
, "port", port
);
477 h
= strstr(optstr
, ",to=");
479 qemu_opt_set(opts
, "to", h
+4);
480 if (strstr(optstr
, ",ipv4"))
481 qemu_opt_set(opts
, "ipv4", "on");
482 if (strstr(optstr
, ",ipv6"))
483 qemu_opt_set(opts
, "ipv6", "on");
487 int inet_listen(const char *str
, char *ostr
, int olen
,
488 int socktype
, int port_offset
, Error
**errp
)
494 opts
= qemu_opts_create(&dummy_opts
, NULL
, 0, NULL
);
495 if (inet_parse(opts
, str
) == 0) {
496 sock
= inet_listen_opts(opts
, port_offset
, errp
);
497 if (sock
!= -1 && ostr
) {
498 optstr
= strchr(str
, ',');
499 if (qemu_opt_get_bool(opts
, "ipv6", 0)) {
500 snprintf(ostr
, olen
, "[%s]:%s%s",
501 qemu_opt_get(opts
, "host"),
502 qemu_opt_get(opts
, "port"),
503 optstr
? optstr
: "");
505 snprintf(ostr
, olen
, "%s:%s%s",
506 qemu_opt_get(opts
, "host"),
507 qemu_opt_get(opts
, "port"),
508 optstr
? optstr
: "");
512 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
518 int inet_connect(const char *str
, bool block
, bool *in_progress
, Error
**errp
)
523 opts
= qemu_opts_create(&dummy_opts
, NULL
, 0, NULL
);
524 if (inet_parse(opts
, str
) == 0) {
526 qemu_opt_set(opts
, "block", "on");
528 sock
= inet_connect_opts(opts
, in_progress
, errp
);
530 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
538 int unix_listen_opts(QemuOpts
*opts
)
540 struct sockaddr_un un
;
541 const char *path
= qemu_opt_get(opts
, "path");
544 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
546 perror("socket(unix)");
550 memset(&un
, 0, sizeof(un
));
551 un
.sun_family
= AF_UNIX
;
552 if (path
&& strlen(path
)) {
553 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
555 char *tmpdir
= getenv("TMPDIR");
556 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s/qemu-socket-XXXXXX",
557 tmpdir
? tmpdir
: "/tmp");
559 * This dummy fd usage silences the mktemp() unsecure warning.
560 * Using mkstemp() doesn't make things more secure here
561 * though. bind() complains about existing files, so we have
562 * to unlink first and thus re-open the race window. The
563 * worst case possible is bind() failing, i.e. a DoS attack.
565 fd
= mkstemp(un
.sun_path
); close(fd
);
566 qemu_opt_set(opts
, "path", un
.sun_path
);
570 if (bind(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
571 fprintf(stderr
, "bind(unix:%s): %s\n", un
.sun_path
, strerror(errno
));
574 if (listen(sock
, 1) < 0) {
575 fprintf(stderr
, "listen(unix:%s): %s\n", un
.sun_path
, strerror(errno
));
586 int unix_connect_opts(QemuOpts
*opts
)
588 struct sockaddr_un un
;
589 const char *path
= qemu_opt_get(opts
, "path");
593 fprintf(stderr
, "unix connect: no path specified\n");
597 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
599 perror("socket(unix)");
603 memset(&un
, 0, sizeof(un
));
604 un
.sun_family
= AF_UNIX
;
605 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
606 if (connect(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
607 fprintf(stderr
, "connect(unix:%s): %s\n", path
, strerror(errno
));
615 /* compatibility wrapper */
616 int unix_listen(const char *str
, char *ostr
, int olen
)
622 opts
= qemu_opts_create(&dummy_opts
, NULL
, 0, NULL
);
624 optstr
= strchr(str
, ',');
628 path
= g_malloc(len
+1);
629 snprintf(path
, len
+1, "%.*s", len
, str
);
630 qemu_opt_set(opts
, "path", path
);
634 qemu_opt_set(opts
, "path", str
);
637 sock
= unix_listen_opts(opts
);
639 if (sock
!= -1 && ostr
)
640 snprintf(ostr
, olen
, "%s%s", qemu_opt_get(opts
, "path"), optstr
? optstr
: "");
645 int unix_connect(const char *path
)
650 opts
= qemu_opts_create(&dummy_opts
, NULL
, 0, NULL
);
651 qemu_opt_set(opts
, "path", path
);
652 sock
= unix_connect_opts(opts
);
659 int unix_listen_opts(QemuOpts
*opts
)
661 fprintf(stderr
, "unix sockets are not available on windows\n");
666 int unix_connect_opts(QemuOpts
*opts
)
668 fprintf(stderr
, "unix sockets are not available on windows\n");
673 int unix_listen(const char *path
, char *ostr
, int olen
)
675 fprintf(stderr
, "unix sockets are not available on windows\n");
680 int unix_connect(const char *path
)
682 fprintf(stderr
, "unix sockets are not available on windows\n");
690 static void socket_cleanup(void)
696 int socket_init(void)
702 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
704 err
= WSAGetLastError();
705 fprintf(stderr
, "WSAStartup: %d\n", err
);
708 atexit(socket_cleanup
);