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.
22 #include "qemu_socket.h"
23 #include "qemu-common.h" /* for qemu_isdigit */
26 # define AI_ADDRCONFIG 0
29 static const int on
=1, off
=0;
31 /* used temporarely until all users are converted to QemuOpts */
32 static QemuOptsList dummy_opts
= {
34 .head
= QTAILQ_HEAD_INITIALIZER(dummy_opts
.head
),
38 .type
= QEMU_OPT_STRING
,
41 .type
= QEMU_OPT_STRING
,
44 .type
= QEMU_OPT_STRING
,
47 .type
= QEMU_OPT_NUMBER
,
50 .type
= QEMU_OPT_BOOL
,
53 .type
= QEMU_OPT_BOOL
,
56 .type
= QEMU_OPT_BOOL
,
62 static int inet_getport(struct addrinfo
*e
)
64 struct sockaddr_in
*i4
;
65 struct sockaddr_in6
*i6
;
67 switch (e
->ai_family
) {
69 i6
= (void*)e
->ai_addr
;
70 return ntohs(i6
->sin6_port
);
72 i4
= (void*)e
->ai_addr
;
73 return ntohs(i4
->sin_port
);
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
) {
86 i6
= (void*)e
->ai_addr
;
87 i6
->sin6_port
= htons(port
);
90 i4
= (void*)e
->ai_addr
;
91 i4
->sin_port
= htons(port
);
96 const char *inet_strfamily(int family
)
99 case PF_INET6
: return "ipv6";
100 case PF_INET
: return "ipv4";
101 case PF_UNIX
: return "unix";
106 int inet_listen_opts(QemuOpts
*opts
, int port_offset
, Error
**errp
)
108 struct addrinfo ai
,*res
,*e
;
111 char uaddr
[INET6_ADDRSTRLEN
+1];
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
);
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
;
137 snprintf(port
, sizeof(port
), "%d", atoi(port
) + port_offset
);
138 rc
= getaddrinfo(strlen(addr
) ? addr
: NULL
, port
, &ai
, &res
);
140 fprintf(stderr
,"getaddrinfo(%s,%s): %s\n", addr
, port
,
142 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
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
);
153 fprintf(stderr
,"%s: socket(%s): %s\n", __FUNCTION__
,
154 inet_strfamily(e
->ai_family
), strerror(errno
));
156 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
161 setsockopt(slisten
,SOL_SOCKET
,SO_REUSEADDR
,(void*)&on
,sizeof(on
));
163 if (e
->ai_family
== PF_INET6
) {
164 /* listen on both ipv4 and ipv6 */
165 setsockopt(slisten
,IPPROTO_IPV6
,IPV6_V6ONLY
,(void*)&off
,
170 port_min
= inet_getport(e
);
171 port_max
= to
? to
+ port_offset
: port_min
;
172 for (p
= port_min
; p
<= port_max
; p
++) {
174 if (bind(slisten
, e
->ai_addr
, e
->ai_addrlen
) == 0) {
178 fprintf(stderr
,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__
,
179 inet_strfamily(e
->ai_family
), uaddr
, inet_getport(e
),
182 error_set(errp
, QERR_SOCKET_BIND_FAILED
);
186 closesocket(slisten
);
188 fprintf(stderr
, "%s: FAILED\n", __FUNCTION__
);
193 if (listen(slisten
,1) != 0) {
194 error_set(errp
, QERR_SOCKET_LISTEN_FAILED
);
196 closesocket(slisten
);
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");
209 int inet_connect_opts(QemuOpts
*opts
, Error
**errp
)
211 struct addrinfo ai
,*res
,*e
;
214 char uaddr
[INET6_ADDRSTRLEN
+1];
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
);
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
;
239 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &res
))) {
240 fprintf(stderr
,"getaddrinfo(%s,%s): %s\n", addr
, port
,
242 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
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__
);
253 sock
= qemu_socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
255 fprintf(stderr
,"%s: socket(%s): %s\n", __FUNCTION__
,
256 inet_strfamily(e
->ai_family
), strerror(errno
));
259 setsockopt(sock
,SOL_SOCKET
,SO_REUSEADDR
,(void*)&on
,sizeof(on
));
261 socket_set_nonblock(sock
);
263 /* connect to peer */
266 if (connect(sock
, e
->ai_addr
, e
->ai_addrlen
) < 0) {
267 rc
= -socket_error();
269 } while (rc
== -EINTR
);
272 if (!block
&& (rc
== -EINPROGRESS
|| rc
== -EWOULDBLOCK
273 || rc
== -WSAEALREADY
)) {
275 if (!block
&& (rc
== -EINPROGRESS
)) {
277 error_set(errp
, QERR_SOCKET_CONNECT_IN_PROGRESS
);
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
));
290 error_set(errp
, QERR_SOCKET_CONNECT_FAILED
);
295 int inet_dgram_opts(QemuOpts
*opts
)
297 struct addrinfo ai
, *peer
= NULL
, *local
= NULL
;
300 char uaddr
[INET6_ADDRSTRLEN
+1];
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) {
315 if (port
== NULL
|| strlen(port
) == 0) {
316 fprintf(stderr
, "inet_dgram: port not specified\n");
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
,
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) {
342 if (!port
|| strlen(port
) == 0)
345 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &local
))) {
346 fprintf(stderr
,"getaddrinfo(%s,%s): %s\n", addr
, port
,
352 sock
= qemu_socket(peer
->ai_family
, peer
->ai_socktype
, peer
->ai_protocol
);
354 fprintf(stderr
,"%s: socket(%s): %s\n", __FUNCTION__
,
355 inet_strfamily(peer
->ai_family
), strerror(errno
));
358 setsockopt(sock
,SOL_SOCKET
,SO_REUSEADDR
,(void*)&on
,sizeof(on
));
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__
);
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
));
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__
);
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
));
401 /* compatibility wrapper */
402 static int inet_parse(QemuOpts
*opts
, const char *str
)
404 const char *optstr
, *h
;
413 if (1 != sscanf(str
,":%32[^,]%n",port
,&pos
)) {
414 fprintf(stderr
, "%s: portonly parse error (%s)\n",
418 } else if (str
[0] == '[') {
420 if (2 != sscanf(str
,"[%64[^]]]:%32[^,]%n",addr
,port
,&pos
)) {
421 fprintf(stderr
, "%s: ipv6 parse error (%s)\n",
425 qemu_opt_set(opts
, "ipv6", "on");
426 } else if (qemu_isdigit(str
[0])) {
428 if (2 != sscanf(str
,"%64[0-9.]:%32[^,]%n",addr
,port
,&pos
)) {
429 fprintf(stderr
, "%s: ipv4 parse error (%s)\n",
433 qemu_opt_set(opts
, "ipv4", "on");
436 if (2 != sscanf(str
,"%64[^:]:%32[^,]%n",addr
,port
,&pos
)) {
437 fprintf(stderr
, "%s: hostname parse error (%s)\n",
442 qemu_opt_set(opts
, "host", addr
);
443 qemu_opt_set(opts
, "port", port
);
447 h
= strstr(optstr
, ",to=");
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");
457 int inet_listen(const char *str
, char *ostr
, int olen
,
458 int socktype
, int port_offset
, Error
**errp
)
464 opts
= qemu_opts_create(&dummy_opts
, NULL
, 0);
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
: "");
475 snprintf(ostr
, olen
, "%s:%s%s",
476 qemu_opt_get(opts
, "host"),
477 qemu_opt_get(opts
, "port"),
478 optstr
? optstr
: "");
482 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
488 int inet_connect(const char *str
, bool block
, Error
**errp
)
493 opts
= qemu_opts_create(&dummy_opts
, NULL
, 0);
494 if (inet_parse(opts
, str
) == 0) {
496 qemu_opt_set(opts
, "block", "on");
498 sock
= inet_connect_opts(opts
, errp
);
500 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
508 int unix_listen_opts(QemuOpts
*opts
)
510 struct sockaddr_un un
;
511 const char *path
= qemu_opt_get(opts
, "path");
514 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
516 perror("socket(unix)");
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
);
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
);
540 if (bind(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
541 fprintf(stderr
, "bind(unix:%s): %s\n", un
.sun_path
, strerror(errno
));
544 if (listen(sock
, 1) < 0) {
545 fprintf(stderr
, "listen(unix:%s): %s\n", un
.sun_path
, strerror(errno
));
556 int unix_connect_opts(QemuOpts
*opts
)
558 struct sockaddr_un un
;
559 const char *path
= qemu_opt_get(opts
, "path");
563 fprintf(stderr
, "unix connect: no path specified\n");
567 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
569 perror("socket(unix)");
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
));
585 /* compatibility wrapper */
586 int unix_listen(const char *str
, char *ostr
, int olen
)
592 opts
= qemu_opts_create(&dummy_opts
, NULL
, 0);
594 optstr
= strchr(str
, ',');
598 path
= g_malloc(len
+1);
599 snprintf(path
, len
+1, "%.*s", len
, str
);
600 qemu_opt_set(opts
, "path", path
);
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
: "");
615 int unix_connect(const char *path
)
620 opts
= qemu_opts_create(&dummy_opts
, NULL
, 0);
621 qemu_opt_set(opts
, "path", path
);
622 sock
= unix_connect_opts(opts
);
629 int unix_listen_opts(QemuOpts
*opts
)
631 fprintf(stderr
, "unix sockets are not available on windows\n");
636 int unix_connect_opts(QemuOpts
*opts
)
638 fprintf(stderr
, "unix sockets are not available on windows\n");
643 int unix_listen(const char *path
, char *ostr
, int olen
)
645 fprintf(stderr
, "unix sockets are not available on windows\n");
650 int unix_connect(const char *path
)
652 fprintf(stderr
, "unix sockets are not available on windows\n");
660 static void socket_cleanup(void)
666 int socket_init(void)
672 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
674 err
= WSAGetLastError();
675 fprintf(stderr
, "WSAStartup: %d\n", err
);
678 atexit(socket_cleanup
);