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
)
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__
);
125 pstrcpy(port
, sizeof(port
), qemu_opt_get(opts
, "port"));
126 addr
= qemu_opt_get(opts
, "host");
128 to
= qemu_opt_get_number(opts
, "to", 0);
129 if (qemu_opt_get_bool(opts
, "ipv4", 0))
130 ai
.ai_family
= PF_INET
;
131 if (qemu_opt_get_bool(opts
, "ipv6", 0))
132 ai
.ai_family
= PF_INET6
;
136 snprintf(port
, sizeof(port
), "%d", atoi(port
) + port_offset
);
137 rc
= getaddrinfo(strlen(addr
) ? addr
: NULL
, port
, &ai
, &res
);
139 fprintf(stderr
,"getaddrinfo(%s,%s): %s\n", addr
, port
,
144 /* create socket + bind */
145 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
146 getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
147 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
148 NI_NUMERICHOST
| NI_NUMERICSERV
);
149 slisten
= qemu_socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
151 fprintf(stderr
,"%s: socket(%s): %s\n", __FUNCTION__
,
152 inet_strfamily(e
->ai_family
), strerror(errno
));
156 setsockopt(slisten
,SOL_SOCKET
,SO_REUSEADDR
,(void*)&on
,sizeof(on
));
158 if (e
->ai_family
== PF_INET6
) {
159 /* listen on both ipv4 and ipv6 */
160 setsockopt(slisten
,IPPROTO_IPV6
,IPV6_V6ONLY
,(void*)&off
,
165 port_min
= inet_getport(e
);
166 port_max
= to
? to
+ port_offset
: port_min
;
167 for (p
= port_min
; p
<= port_max
; p
++) {
169 if (bind(slisten
, e
->ai_addr
, e
->ai_addrlen
) == 0) {
173 fprintf(stderr
,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__
,
174 inet_strfamily(e
->ai_family
), uaddr
, inet_getport(e
),
178 closesocket(slisten
);
180 fprintf(stderr
, "%s: FAILED\n", __FUNCTION__
);
185 if (listen(slisten
,1) != 0) {
187 closesocket(slisten
);
191 snprintf(uport
, sizeof(uport
), "%d", inet_getport(e
) - port_offset
);
192 qemu_opt_set(opts
, "host", uaddr
);
193 qemu_opt_set(opts
, "port", uport
);
194 qemu_opt_set(opts
, "ipv6", (e
->ai_family
== PF_INET6
) ? "on" : "off");
195 qemu_opt_set(opts
, "ipv4", (e
->ai_family
!= PF_INET6
) ? "on" : "off");
200 int inet_connect_opts(QemuOpts
*opts
, Error
**errp
)
202 struct addrinfo ai
,*res
,*e
;
205 char uaddr
[INET6_ADDRSTRLEN
+1];
210 memset(&ai
,0, sizeof(ai
));
211 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
212 ai
.ai_family
= PF_UNSPEC
;
213 ai
.ai_socktype
= SOCK_STREAM
;
215 addr
= qemu_opt_get(opts
, "host");
216 port
= qemu_opt_get(opts
, "port");
217 block
= qemu_opt_get_bool(opts
, "block", 0);
218 if (addr
== NULL
|| port
== NULL
) {
219 fprintf(stderr
, "inet_connect: host and/or port not specified\n");
220 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
224 if (qemu_opt_get_bool(opts
, "ipv4", 0))
225 ai
.ai_family
= PF_INET
;
226 if (qemu_opt_get_bool(opts
, "ipv6", 0))
227 ai
.ai_family
= PF_INET6
;
230 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &res
))) {
231 fprintf(stderr
,"getaddrinfo(%s,%s): %s\n", addr
, port
,
233 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
237 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
238 if (getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
239 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
240 NI_NUMERICHOST
| NI_NUMERICSERV
) != 0) {
241 fprintf(stderr
,"%s: getnameinfo: oops\n", __FUNCTION__
);
244 sock
= qemu_socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
246 fprintf(stderr
,"%s: socket(%s): %s\n", __FUNCTION__
,
247 inet_strfamily(e
->ai_family
), strerror(errno
));
250 setsockopt(sock
,SOL_SOCKET
,SO_REUSEADDR
,(void*)&on
,sizeof(on
));
252 socket_set_nonblock(sock
);
254 /* connect to peer */
257 if (connect(sock
, e
->ai_addr
, e
->ai_addrlen
) < 0) {
258 rc
= -socket_error();
260 } while (rc
== -EINTR
);
263 if (!block
&& (rc
== -EINPROGRESS
|| rc
== -EWOULDBLOCK
264 || rc
== -WSAEALREADY
)) {
266 if (!block
&& (rc
== -EINPROGRESS
)) {
268 error_set(errp
, QERR_SOCKET_CONNECT_IN_PROGRESS
);
270 if (NULL
== e
->ai_next
)
271 fprintf(stderr
, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__
,
272 inet_strfamily(e
->ai_family
),
273 e
->ai_canonname
, uaddr
, uport
, strerror(errno
));
281 error_set(errp
, QERR_SOCKET_CONNECT_FAILED
);
286 int inet_dgram_opts(QemuOpts
*opts
)
288 struct addrinfo ai
, *peer
= NULL
, *local
= NULL
;
291 char uaddr
[INET6_ADDRSTRLEN
+1];
295 /* lookup peer addr */
296 memset(&ai
,0, sizeof(ai
));
297 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
298 ai
.ai_family
= PF_UNSPEC
;
299 ai
.ai_socktype
= SOCK_DGRAM
;
301 addr
= qemu_opt_get(opts
, "host");
302 port
= qemu_opt_get(opts
, "port");
303 if (addr
== NULL
|| strlen(addr
) == 0) {
306 if (port
== NULL
|| strlen(port
) == 0) {
307 fprintf(stderr
, "inet_dgram: port not specified\n");
311 if (qemu_opt_get_bool(opts
, "ipv4", 0))
312 ai
.ai_family
= PF_INET
;
313 if (qemu_opt_get_bool(opts
, "ipv6", 0))
314 ai
.ai_family
= PF_INET6
;
316 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &peer
))) {
317 fprintf(stderr
,"getaddrinfo(%s,%s): %s\n", addr
, port
,
322 /* lookup local addr */
323 memset(&ai
,0, sizeof(ai
));
324 ai
.ai_flags
= AI_PASSIVE
;
325 ai
.ai_family
= peer
->ai_family
;
326 ai
.ai_socktype
= SOCK_DGRAM
;
328 addr
= qemu_opt_get(opts
, "localaddr");
329 port
= qemu_opt_get(opts
, "localport");
330 if (addr
== NULL
|| strlen(addr
) == 0) {
333 if (!port
|| strlen(port
) == 0)
336 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &local
))) {
337 fprintf(stderr
,"getaddrinfo(%s,%s): %s\n", addr
, port
,
343 sock
= qemu_socket(peer
->ai_family
, peer
->ai_socktype
, peer
->ai_protocol
);
345 fprintf(stderr
,"%s: socket(%s): %s\n", __FUNCTION__
,
346 inet_strfamily(peer
->ai_family
), strerror(errno
));
349 setsockopt(sock
,SOL_SOCKET
,SO_REUSEADDR
,(void*)&on
,sizeof(on
));
352 if (getnameinfo((struct sockaddr
*)local
->ai_addr
,local
->ai_addrlen
,
353 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
354 NI_NUMERICHOST
| NI_NUMERICSERV
) != 0) {
355 fprintf(stderr
, "%s: getnameinfo: oops\n", __FUNCTION__
);
358 if (bind(sock
, local
->ai_addr
, local
->ai_addrlen
) < 0) {
359 fprintf(stderr
,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__
,
360 inet_strfamily(local
->ai_family
), uaddr
, inet_getport(local
));
364 /* connect to peer */
365 if (getnameinfo((struct sockaddr
*)peer
->ai_addr
, peer
->ai_addrlen
,
366 uaddr
, INET6_ADDRSTRLEN
, uport
, 32,
367 NI_NUMERICHOST
| NI_NUMERICSERV
) != 0) {
368 fprintf(stderr
, "%s: getnameinfo: oops\n", __FUNCTION__
);
371 if (connect(sock
,peer
->ai_addr
,peer
->ai_addrlen
) < 0) {
372 fprintf(stderr
, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__
,
373 inet_strfamily(peer
->ai_family
),
374 peer
->ai_canonname
, uaddr
, uport
, strerror(errno
));
392 /* compatibility wrapper */
393 static int inet_parse(QemuOpts
*opts
, const char *str
)
395 const char *optstr
, *h
;
404 if (1 != sscanf(str
,":%32[^,]%n",port
,&pos
)) {
405 fprintf(stderr
, "%s: portonly parse error (%s)\n",
409 } else if (str
[0] == '[') {
411 if (2 != sscanf(str
,"[%64[^]]]:%32[^,]%n",addr
,port
,&pos
)) {
412 fprintf(stderr
, "%s: ipv6 parse error (%s)\n",
416 qemu_opt_set(opts
, "ipv6", "on");
417 } else if (qemu_isdigit(str
[0])) {
419 if (2 != sscanf(str
,"%64[0-9.]:%32[^,]%n",addr
,port
,&pos
)) {
420 fprintf(stderr
, "%s: ipv4 parse error (%s)\n",
424 qemu_opt_set(opts
, "ipv4", "on");
427 if (2 != sscanf(str
,"%64[^:]:%32[^,]%n",addr
,port
,&pos
)) {
428 fprintf(stderr
, "%s: hostname parse error (%s)\n",
433 qemu_opt_set(opts
, "host", addr
);
434 qemu_opt_set(opts
, "port", port
);
438 h
= strstr(optstr
, ",to=");
440 qemu_opt_set(opts
, "to", h
+4);
441 if (strstr(optstr
, ",ipv4"))
442 qemu_opt_set(opts
, "ipv4", "on");
443 if (strstr(optstr
, ",ipv6"))
444 qemu_opt_set(opts
, "ipv6", "on");
448 int inet_listen(const char *str
, char *ostr
, int olen
,
449 int socktype
, int port_offset
)
455 opts
= qemu_opts_create(&dummy_opts
, NULL
, 0);
456 if (inet_parse(opts
, str
) == 0) {
457 sock
= inet_listen_opts(opts
, port_offset
);
458 if (sock
!= -1 && ostr
) {
459 optstr
= strchr(str
, ',');
460 if (qemu_opt_get_bool(opts
, "ipv6", 0)) {
461 snprintf(ostr
, olen
, "[%s]:%s%s",
462 qemu_opt_get(opts
, "host"),
463 qemu_opt_get(opts
, "port"),
464 optstr
? optstr
: "");
466 snprintf(ostr
, olen
, "%s:%s%s",
467 qemu_opt_get(opts
, "host"),
468 qemu_opt_get(opts
, "port"),
469 optstr
? optstr
: "");
477 int inet_connect(const char *str
, bool block
, Error
**errp
)
482 opts
= qemu_opts_create(&dummy_opts
, NULL
, 0);
483 if (inet_parse(opts
, str
) == 0) {
485 qemu_opt_set(opts
, "block", "on");
487 sock
= inet_connect_opts(opts
, errp
);
489 error_set(errp
, QERR_SOCKET_CREATE_FAILED
);
497 int unix_listen_opts(QemuOpts
*opts
)
499 struct sockaddr_un un
;
500 const char *path
= qemu_opt_get(opts
, "path");
503 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
505 perror("socket(unix)");
509 memset(&un
, 0, sizeof(un
));
510 un
.sun_family
= AF_UNIX
;
511 if (path
&& strlen(path
)) {
512 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
514 char *tmpdir
= getenv("TMPDIR");
515 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s/qemu-socket-XXXXXX",
516 tmpdir
? tmpdir
: "/tmp");
518 * This dummy fd usage silences the mktemp() unsecure warning.
519 * Using mkstemp() doesn't make things more secure here
520 * though. bind() complains about existing files, so we have
521 * to unlink first and thus re-open the race window. The
522 * worst case possible is bind() failing, i.e. a DoS attack.
524 fd
= mkstemp(un
.sun_path
); close(fd
);
525 qemu_opt_set(opts
, "path", un
.sun_path
);
529 if (bind(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
530 fprintf(stderr
, "bind(unix:%s): %s\n", un
.sun_path
, strerror(errno
));
533 if (listen(sock
, 1) < 0) {
534 fprintf(stderr
, "listen(unix:%s): %s\n", un
.sun_path
, strerror(errno
));
545 int unix_connect_opts(QemuOpts
*opts
)
547 struct sockaddr_un un
;
548 const char *path
= qemu_opt_get(opts
, "path");
552 fprintf(stderr
, "unix connect: no path specified\n");
556 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
558 perror("socket(unix)");
562 memset(&un
, 0, sizeof(un
));
563 un
.sun_family
= AF_UNIX
;
564 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
565 if (connect(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
566 fprintf(stderr
, "connect(unix:%s): %s\n", path
, strerror(errno
));
574 /* compatibility wrapper */
575 int unix_listen(const char *str
, char *ostr
, int olen
)
581 opts
= qemu_opts_create(&dummy_opts
, NULL
, 0);
583 optstr
= strchr(str
, ',');
587 path
= g_malloc(len
+1);
588 snprintf(path
, len
+1, "%.*s", len
, str
);
589 qemu_opt_set(opts
, "path", path
);
593 qemu_opt_set(opts
, "path", str
);
596 sock
= unix_listen_opts(opts
);
598 if (sock
!= -1 && ostr
)
599 snprintf(ostr
, olen
, "%s%s", qemu_opt_get(opts
, "path"), optstr
? optstr
: "");
604 int unix_connect(const char *path
)
609 opts
= qemu_opts_create(&dummy_opts
, NULL
, 0);
610 qemu_opt_set(opts
, "path", path
);
611 sock
= unix_connect_opts(opts
);
618 int unix_listen_opts(QemuOpts
*opts
)
620 fprintf(stderr
, "unix sockets are not available on windows\n");
625 int unix_connect_opts(QemuOpts
*opts
)
627 fprintf(stderr
, "unix sockets are not available on windows\n");
632 int unix_listen(const char *path
, char *ostr
, int olen
)
634 fprintf(stderr
, "unix sockets are not available on windows\n");
639 int unix_connect(const char *path
)
641 fprintf(stderr
, "unix sockets are not available on windows\n");
649 static void socket_cleanup(void)
655 int socket_init(void)
661 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
663 err
= WSAGetLastError();
664 fprintf(stderr
, "WSAStartup: %d\n", err
);
667 atexit(socket_cleanup
);