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");
212 int inet_connect_opts(QemuOpts
*opts
, bool *in_progress
, Error
**errp
)
214 struct addrinfo ai
,*res
,*e
;
217 char uaddr
[INET6_ADDRSTRLEN
+1];
222 memset(&ai
,0, sizeof(ai
));
223 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
224 ai
.ai_family
= PF_UNSPEC
;
225 ai
.ai_socktype
= SOCK_STREAM
;
228 *in_progress
= false;
231 addr
= qemu_opt_get(opts
, "host");
232 port
= qemu_opt_get(opts
, "port");
233 block
= qemu_opt_get_bool(opts
, "block", 0);
234 if (addr
== NULL
|| port
== NULL
) {
235 fprintf(stderr
, "inet_connect: host and/or port not specified\n");
236 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
240 if (qemu_opt_get_bool(opts
, "ipv4", 0))
241 ai
.ai_family
= PF_INET
;
242 if (qemu_opt_get_bool(opts
, "ipv6", 0))
243 ai
.ai_family
= PF_INET6
;
246 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &res
))) {
247 fprintf(stderr
,"getaddrinfo(%s,%s): %s\n", addr
, port
,
249 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
253 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
254 if (getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
255 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
256 NI_NUMERICHOST
| NI_NUMERICSERV
) != 0) {
257 fprintf(stderr
,"%s: getnameinfo: oops\n", __FUNCTION__
);
260 sock
= qemu_socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
262 fprintf(stderr
,"%s: socket(%s): %s\n", __FUNCTION__
,
263 inet_strfamily(e
->ai_family
), strerror(errno
));
266 setsockopt(sock
,SOL_SOCKET
,SO_REUSEADDR
,(void*)&on
,sizeof(on
));
268 socket_set_nonblock(sock
);
270 /* connect to peer */
273 if (connect(sock
, e
->ai_addr
, e
->ai_addrlen
) < 0) {
274 rc
= -socket_error();
276 } while (rc
== -EINTR
);
279 if (!block
&& (rc
== -EINPROGRESS
|| rc
== -EWOULDBLOCK
280 || rc
== -WSAEALREADY
)) {
282 if (!block
&& (rc
== -EINPROGRESS
)) {
288 if (NULL
== e
->ai_next
)
289 fprintf(stderr
, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__
,
290 inet_strfamily(e
->ai_family
),
291 e
->ai_canonname
, uaddr
, uport
, strerror(errno
));
298 error_set(errp
, QERR_SOCKET_CONNECT_FAILED
);
303 int inet_dgram_opts(QemuOpts
*opts
)
305 struct addrinfo ai
, *peer
= NULL
, *local
= NULL
;
308 char uaddr
[INET6_ADDRSTRLEN
+1];
312 /* lookup peer addr */
313 memset(&ai
,0, sizeof(ai
));
314 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
315 ai
.ai_family
= PF_UNSPEC
;
316 ai
.ai_socktype
= SOCK_DGRAM
;
318 addr
= qemu_opt_get(opts
, "host");
319 port
= qemu_opt_get(opts
, "port");
320 if (addr
== NULL
|| strlen(addr
) == 0) {
323 if (port
== NULL
|| strlen(port
) == 0) {
324 fprintf(stderr
, "inet_dgram: port not specified\n");
328 if (qemu_opt_get_bool(opts
, "ipv4", 0))
329 ai
.ai_family
= PF_INET
;
330 if (qemu_opt_get_bool(opts
, "ipv6", 0))
331 ai
.ai_family
= PF_INET6
;
333 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &peer
))) {
334 fprintf(stderr
,"getaddrinfo(%s,%s): %s\n", addr
, port
,
339 /* lookup local addr */
340 memset(&ai
,0, sizeof(ai
));
341 ai
.ai_flags
= AI_PASSIVE
;
342 ai
.ai_family
= peer
->ai_family
;
343 ai
.ai_socktype
= SOCK_DGRAM
;
345 addr
= qemu_opt_get(opts
, "localaddr");
346 port
= qemu_opt_get(opts
, "localport");
347 if (addr
== NULL
|| strlen(addr
) == 0) {
350 if (!port
|| strlen(port
) == 0)
353 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &local
))) {
354 fprintf(stderr
,"getaddrinfo(%s,%s): %s\n", addr
, port
,
360 sock
= qemu_socket(peer
->ai_family
, peer
->ai_socktype
, peer
->ai_protocol
);
362 fprintf(stderr
,"%s: socket(%s): %s\n", __FUNCTION__
,
363 inet_strfamily(peer
->ai_family
), strerror(errno
));
366 setsockopt(sock
,SOL_SOCKET
,SO_REUSEADDR
,(void*)&on
,sizeof(on
));
369 if (getnameinfo((struct sockaddr
*)local
->ai_addr
,local
->ai_addrlen
,
370 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
371 NI_NUMERICHOST
| NI_NUMERICSERV
) != 0) {
372 fprintf(stderr
, "%s: getnameinfo: oops\n", __FUNCTION__
);
375 if (bind(sock
, local
->ai_addr
, local
->ai_addrlen
) < 0) {
376 fprintf(stderr
,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__
,
377 inet_strfamily(local
->ai_family
), uaddr
, inet_getport(local
));
381 /* connect to peer */
382 if (getnameinfo((struct sockaddr
*)peer
->ai_addr
, peer
->ai_addrlen
,
383 uaddr
, INET6_ADDRSTRLEN
, uport
, 32,
384 NI_NUMERICHOST
| NI_NUMERICSERV
) != 0) {
385 fprintf(stderr
, "%s: getnameinfo: oops\n", __FUNCTION__
);
388 if (connect(sock
,peer
->ai_addr
,peer
->ai_addrlen
) < 0) {
389 fprintf(stderr
, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__
,
390 inet_strfamily(peer
->ai_family
),
391 peer
->ai_canonname
, uaddr
, uport
, strerror(errno
));
409 /* compatibility wrapper */
410 static int inet_parse(QemuOpts
*opts
, const char *str
)
412 const char *optstr
, *h
;
421 if (1 != sscanf(str
,":%32[^,]%n",port
,&pos
)) {
422 fprintf(stderr
, "%s: portonly parse error (%s)\n",
426 } else if (str
[0] == '[') {
428 if (2 != sscanf(str
,"[%64[^]]]:%32[^,]%n",addr
,port
,&pos
)) {
429 fprintf(stderr
, "%s: ipv6 parse error (%s)\n",
433 qemu_opt_set(opts
, "ipv6", "on");
434 } else if (qemu_isdigit(str
[0])) {
436 if (2 != sscanf(str
,"%64[0-9.]:%32[^,]%n",addr
,port
,&pos
)) {
437 fprintf(stderr
, "%s: ipv4 parse error (%s)\n",
441 qemu_opt_set(opts
, "ipv4", "on");
444 if (2 != sscanf(str
,"%64[^:]:%32[^,]%n",addr
,port
,&pos
)) {
445 fprintf(stderr
, "%s: hostname parse error (%s)\n",
450 qemu_opt_set(opts
, "host", addr
);
451 qemu_opt_set(opts
, "port", port
);
455 h
= strstr(optstr
, ",to=");
457 qemu_opt_set(opts
, "to", h
+4);
458 if (strstr(optstr
, ",ipv4"))
459 qemu_opt_set(opts
, "ipv4", "on");
460 if (strstr(optstr
, ",ipv6"))
461 qemu_opt_set(opts
, "ipv6", "on");
465 int inet_listen(const char *str
, char *ostr
, int olen
,
466 int socktype
, int port_offset
, Error
**errp
)
472 opts
= qemu_opts_create(&dummy_opts
, NULL
, 0, NULL
);
473 if (inet_parse(opts
, str
) == 0) {
474 sock
= inet_listen_opts(opts
, port_offset
, errp
);
475 if (sock
!= -1 && ostr
) {
476 optstr
= strchr(str
, ',');
477 if (qemu_opt_get_bool(opts
, "ipv6", 0)) {
478 snprintf(ostr
, olen
, "[%s]:%s%s",
479 qemu_opt_get(opts
, "host"),
480 qemu_opt_get(opts
, "port"),
481 optstr
? optstr
: "");
483 snprintf(ostr
, olen
, "%s:%s%s",
484 qemu_opt_get(opts
, "host"),
485 qemu_opt_get(opts
, "port"),
486 optstr
? optstr
: "");
490 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
496 int inet_connect(const char *str
, bool block
, bool *in_progress
, Error
**errp
)
501 opts
= qemu_opts_create(&dummy_opts
, NULL
, 0, NULL
);
502 if (inet_parse(opts
, str
) == 0) {
504 qemu_opt_set(opts
, "block", "on");
506 sock
= inet_connect_opts(opts
, in_progress
, errp
);
508 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
516 int unix_listen_opts(QemuOpts
*opts
)
518 struct sockaddr_un un
;
519 const char *path
= qemu_opt_get(opts
, "path");
522 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
524 perror("socket(unix)");
528 memset(&un
, 0, sizeof(un
));
529 un
.sun_family
= AF_UNIX
;
530 if (path
&& strlen(path
)) {
531 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
533 char *tmpdir
= getenv("TMPDIR");
534 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s/qemu-socket-XXXXXX",
535 tmpdir
? tmpdir
: "/tmp");
537 * This dummy fd usage silences the mktemp() unsecure warning.
538 * Using mkstemp() doesn't make things more secure here
539 * though. bind() complains about existing files, so we have
540 * to unlink first and thus re-open the race window. The
541 * worst case possible is bind() failing, i.e. a DoS attack.
543 fd
= mkstemp(un
.sun_path
); close(fd
);
544 qemu_opt_set(opts
, "path", un
.sun_path
);
548 if (bind(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
549 fprintf(stderr
, "bind(unix:%s): %s\n", un
.sun_path
, strerror(errno
));
552 if (listen(sock
, 1) < 0) {
553 fprintf(stderr
, "listen(unix:%s): %s\n", un
.sun_path
, strerror(errno
));
564 int unix_connect_opts(QemuOpts
*opts
)
566 struct sockaddr_un un
;
567 const char *path
= qemu_opt_get(opts
, "path");
571 fprintf(stderr
, "unix connect: no path specified\n");
575 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
577 perror("socket(unix)");
581 memset(&un
, 0, sizeof(un
));
582 un
.sun_family
= AF_UNIX
;
583 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
584 if (connect(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
585 fprintf(stderr
, "connect(unix:%s): %s\n", path
, strerror(errno
));
593 /* compatibility wrapper */
594 int unix_listen(const char *str
, char *ostr
, int olen
)
600 opts
= qemu_opts_create(&dummy_opts
, NULL
, 0, NULL
);
602 optstr
= strchr(str
, ',');
606 path
= g_malloc(len
+1);
607 snprintf(path
, len
+1, "%.*s", len
, str
);
608 qemu_opt_set(opts
, "path", path
);
612 qemu_opt_set(opts
, "path", str
);
615 sock
= unix_listen_opts(opts
);
617 if (sock
!= -1 && ostr
)
618 snprintf(ostr
, olen
, "%s%s", qemu_opt_get(opts
, "path"), optstr
? optstr
: "");
623 int unix_connect(const char *path
)
628 opts
= qemu_opts_create(&dummy_opts
, NULL
, 0, NULL
);
629 qemu_opt_set(opts
, "path", path
);
630 sock
= unix_connect_opts(opts
);
637 int unix_listen_opts(QemuOpts
*opts
)
639 fprintf(stderr
, "unix sockets are not available on windows\n");
644 int unix_connect_opts(QemuOpts
*opts
)
646 fprintf(stderr
, "unix sockets are not available on windows\n");
651 int unix_listen(const char *path
, char *ostr
, int olen
)
653 fprintf(stderr
, "unix sockets are not available on windows\n");
658 int unix_connect(const char *path
)
660 fprintf(stderr
, "unix sockets are not available on windows\n");
668 static void socket_cleanup(void)
674 int socket_init(void)
680 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
682 err
= WSAGetLastError();
683 fprintf(stderr
, "WSAStartup: %d\n", err
);
686 atexit(socket_cleanup
);