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
,
59 static int inet_getport(struct addrinfo
*e
)
61 struct sockaddr_in
*i4
;
62 struct sockaddr_in6
*i6
;
64 switch (e
->ai_family
) {
66 i6
= (void*)e
->ai_addr
;
67 return ntohs(i6
->sin6_port
);
69 i4
= (void*)e
->ai_addr
;
70 return ntohs(i4
->sin_port
);
76 static void inet_setport(struct addrinfo
*e
, int port
)
78 struct sockaddr_in
*i4
;
79 struct sockaddr_in6
*i6
;
81 switch (e
->ai_family
) {
83 i6
= (void*)e
->ai_addr
;
84 i6
->sin6_port
= htons(port
);
87 i4
= (void*)e
->ai_addr
;
88 i4
->sin_port
= htons(port
);
93 const char *inet_strfamily(int family
)
96 case PF_INET6
: return "ipv6";
97 case PF_INET
: return "ipv4";
98 case PF_UNIX
: return "unix";
103 int inet_listen_opts(QemuOpts
*opts
, int port_offset
)
105 struct addrinfo ai
,*res
,*e
;
108 char uaddr
[INET6_ADDRSTRLEN
+1];
110 int slisten
, rc
, to
, port_min
, port_max
, p
;
112 memset(&ai
,0, sizeof(ai
));
113 ai
.ai_flags
= AI_PASSIVE
| AI_ADDRCONFIG
;
114 ai
.ai_family
= PF_UNSPEC
;
115 ai
.ai_socktype
= SOCK_STREAM
;
117 if ((qemu_opt_get(opts
, "host") == NULL
) ||
118 (qemu_opt_get(opts
, "port") == NULL
)) {
119 fprintf(stderr
, "%s: host and/or port not specified\n", __FUNCTION__
);
122 pstrcpy(port
, sizeof(port
), qemu_opt_get(opts
, "port"));
123 addr
= qemu_opt_get(opts
, "host");
125 to
= qemu_opt_get_number(opts
, "to", 0);
126 if (qemu_opt_get_bool(opts
, "ipv4", 0))
127 ai
.ai_family
= PF_INET
;
128 if (qemu_opt_get_bool(opts
, "ipv6", 0))
129 ai
.ai_family
= PF_INET6
;
133 snprintf(port
, sizeof(port
), "%d", atoi(port
) + port_offset
);
134 rc
= getaddrinfo(strlen(addr
) ? addr
: NULL
, port
, &ai
, &res
);
136 fprintf(stderr
,"getaddrinfo(%s,%s): %s\n", addr
, port
,
141 /* create socket + bind */
142 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
143 getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
144 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
145 NI_NUMERICHOST
| NI_NUMERICSERV
);
146 slisten
= qemu_socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
148 fprintf(stderr
,"%s: socket(%s): %s\n", __FUNCTION__
,
149 inet_strfamily(e
->ai_family
), strerror(errno
));
153 setsockopt(slisten
,SOL_SOCKET
,SO_REUSEADDR
,(void*)&on
,sizeof(on
));
155 if (e
->ai_family
== PF_INET6
) {
156 /* listen on both ipv4 and ipv6 */
157 setsockopt(slisten
,IPPROTO_IPV6
,IPV6_V6ONLY
,(void*)&off
,
162 port_min
= inet_getport(e
);
163 port_max
= to
? to
+ port_offset
: port_min
;
164 for (p
= port_min
; p
<= port_max
; p
++) {
166 if (bind(slisten
, e
->ai_addr
, e
->ai_addrlen
) == 0) {
170 fprintf(stderr
,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__
,
171 inet_strfamily(e
->ai_family
), uaddr
, inet_getport(e
),
175 closesocket(slisten
);
177 fprintf(stderr
, "%s: FAILED\n", __FUNCTION__
);
182 if (listen(slisten
,1) != 0) {
184 closesocket(slisten
);
188 snprintf(uport
, sizeof(uport
), "%d", inet_getport(e
) - port_offset
);
189 qemu_opt_set(opts
, "host", uaddr
);
190 qemu_opt_set(opts
, "port", uport
);
191 qemu_opt_set(opts
, "ipv6", (e
->ai_family
== PF_INET6
) ? "on" : "off");
192 qemu_opt_set(opts
, "ipv4", (e
->ai_family
!= PF_INET6
) ? "on" : "off");
197 int inet_connect_opts(QemuOpts
*opts
)
199 struct addrinfo ai
,*res
,*e
;
202 char uaddr
[INET6_ADDRSTRLEN
+1];
206 memset(&ai
,0, sizeof(ai
));
207 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
208 ai
.ai_family
= PF_UNSPEC
;
209 ai
.ai_socktype
= SOCK_STREAM
;
211 addr
= qemu_opt_get(opts
, "host");
212 port
= qemu_opt_get(opts
, "port");
213 if (addr
== NULL
|| port
== NULL
) {
214 fprintf(stderr
, "inet_connect: host and/or port not specified\n");
218 if (qemu_opt_get_bool(opts
, "ipv4", 0))
219 ai
.ai_family
= PF_INET
;
220 if (qemu_opt_get_bool(opts
, "ipv6", 0))
221 ai
.ai_family
= PF_INET6
;
224 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &res
))) {
225 fprintf(stderr
,"getaddrinfo(%s,%s): %s\n", addr
, port
,
230 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
231 if (getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
232 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
233 NI_NUMERICHOST
| NI_NUMERICSERV
) != 0) {
234 fprintf(stderr
,"%s: getnameinfo: oops\n", __FUNCTION__
);
237 sock
= qemu_socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
239 fprintf(stderr
,"%s: socket(%s): %s\n", __FUNCTION__
,
240 inet_strfamily(e
->ai_family
), strerror(errno
));
243 setsockopt(sock
,SOL_SOCKET
,SO_REUSEADDR
,(void*)&on
,sizeof(on
));
245 /* connect to peer */
246 if (connect(sock
,e
->ai_addr
,e
->ai_addrlen
) < 0) {
247 if (NULL
== e
->ai_next
)
248 fprintf(stderr
, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__
,
249 inet_strfamily(e
->ai_family
),
250 e
->ai_canonname
, uaddr
, uport
, strerror(errno
));
261 int inet_dgram_opts(QemuOpts
*opts
)
263 struct addrinfo ai
, *peer
= NULL
, *local
= NULL
;
266 char uaddr
[INET6_ADDRSTRLEN
+1];
270 /* lookup peer addr */
271 memset(&ai
,0, sizeof(ai
));
272 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
273 ai
.ai_family
= PF_UNSPEC
;
274 ai
.ai_socktype
= SOCK_DGRAM
;
276 addr
= qemu_opt_get(opts
, "host");
277 port
= qemu_opt_get(opts
, "port");
278 if (addr
== NULL
|| strlen(addr
) == 0) {
281 if (port
== NULL
|| strlen(port
) == 0) {
282 fprintf(stderr
, "inet_dgram: port not specified\n");
286 if (qemu_opt_get_bool(opts
, "ipv4", 0))
287 ai
.ai_family
= PF_INET
;
288 if (qemu_opt_get_bool(opts
, "ipv6", 0))
289 ai
.ai_family
= PF_INET6
;
291 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &peer
))) {
292 fprintf(stderr
,"getaddrinfo(%s,%s): %s\n", addr
, port
,
297 /* lookup local addr */
298 memset(&ai
,0, sizeof(ai
));
299 ai
.ai_flags
= AI_PASSIVE
;
300 ai
.ai_family
= peer
->ai_family
;
301 ai
.ai_socktype
= SOCK_DGRAM
;
303 addr
= qemu_opt_get(opts
, "localaddr");
304 port
= qemu_opt_get(opts
, "localport");
305 if (addr
== NULL
|| strlen(addr
) == 0) {
308 if (!port
|| strlen(port
) == 0)
311 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &local
))) {
312 fprintf(stderr
,"getaddrinfo(%s,%s): %s\n", addr
, port
,
318 sock
= qemu_socket(peer
->ai_family
, peer
->ai_socktype
, peer
->ai_protocol
);
320 fprintf(stderr
,"%s: socket(%s): %s\n", __FUNCTION__
,
321 inet_strfamily(peer
->ai_family
), strerror(errno
));
324 setsockopt(sock
,SOL_SOCKET
,SO_REUSEADDR
,(void*)&on
,sizeof(on
));
327 if (getnameinfo((struct sockaddr
*)local
->ai_addr
,local
->ai_addrlen
,
328 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
329 NI_NUMERICHOST
| NI_NUMERICSERV
) != 0) {
330 fprintf(stderr
, "%s: getnameinfo: oops\n", __FUNCTION__
);
333 if (bind(sock
, local
->ai_addr
, local
->ai_addrlen
) < 0) {
334 fprintf(stderr
,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__
,
335 inet_strfamily(local
->ai_family
), uaddr
, inet_getport(local
));
339 /* connect to peer */
340 if (getnameinfo((struct sockaddr
*)peer
->ai_addr
, peer
->ai_addrlen
,
341 uaddr
, INET6_ADDRSTRLEN
, uport
, 32,
342 NI_NUMERICHOST
| NI_NUMERICSERV
) != 0) {
343 fprintf(stderr
, "%s: getnameinfo: oops\n", __FUNCTION__
);
346 if (connect(sock
,peer
->ai_addr
,peer
->ai_addrlen
) < 0) {
347 fprintf(stderr
, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__
,
348 inet_strfamily(peer
->ai_family
),
349 peer
->ai_canonname
, uaddr
, uport
, strerror(errno
));
367 /* compatibility wrapper */
368 static int inet_parse(QemuOpts
*opts
, const char *str
)
370 const char *optstr
, *h
;
379 if (1 != sscanf(str
,":%32[^,]%n",port
,&pos
)) {
380 fprintf(stderr
, "%s: portonly parse error (%s)\n",
384 } else if (str
[0] == '[') {
386 if (2 != sscanf(str
,"[%64[^]]]:%32[^,]%n",addr
,port
,&pos
)) {
387 fprintf(stderr
, "%s: ipv6 parse error (%s)\n",
391 qemu_opt_set(opts
, "ipv6", "on");
392 } else if (qemu_isdigit(str
[0])) {
394 if (2 != sscanf(str
,"%64[0-9.]:%32[^,]%n",addr
,port
,&pos
)) {
395 fprintf(stderr
, "%s: ipv4 parse error (%s)\n",
399 qemu_opt_set(opts
, "ipv4", "on");
402 if (2 != sscanf(str
,"%64[^:]:%32[^,]%n",addr
,port
,&pos
)) {
403 fprintf(stderr
, "%s: hostname parse error (%s)\n",
408 qemu_opt_set(opts
, "host", addr
);
409 qemu_opt_set(opts
, "port", port
);
413 h
= strstr(optstr
, ",to=");
415 qemu_opt_set(opts
, "to", h
+4);
416 if (strstr(optstr
, ",ipv4"))
417 qemu_opt_set(opts
, "ipv4", "on");
418 if (strstr(optstr
, ",ipv6"))
419 qemu_opt_set(opts
, "ipv6", "on");
423 int inet_listen(const char *str
, char *ostr
, int olen
,
424 int socktype
, int port_offset
)
430 opts
= qemu_opts_create(&dummy_opts
, NULL
, 0);
431 if (inet_parse(opts
, str
) == 0) {
432 sock
= inet_listen_opts(opts
, port_offset
);
433 if (sock
!= -1 && ostr
) {
434 optstr
= strchr(str
, ',');
435 if (qemu_opt_get_bool(opts
, "ipv6", 0)) {
436 snprintf(ostr
, olen
, "[%s]:%s%s",
437 qemu_opt_get(opts
, "host"),
438 qemu_opt_get(opts
, "port"),
439 optstr
? optstr
: "");
441 snprintf(ostr
, olen
, "%s:%s%s",
442 qemu_opt_get(opts
, "host"),
443 qemu_opt_get(opts
, "port"),
444 optstr
? optstr
: "");
452 int inet_connect(const char *str
, int socktype
)
457 opts
= qemu_opts_create(&dummy_opts
, NULL
, 0);
458 if (inet_parse(opts
, str
) == 0)
459 sock
= inet_connect_opts(opts
);
466 int unix_listen_opts(QemuOpts
*opts
)
468 struct sockaddr_un un
;
469 const char *path
= qemu_opt_get(opts
, "path");
472 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
474 perror("socket(unix)");
478 memset(&un
, 0, sizeof(un
));
479 un
.sun_family
= AF_UNIX
;
480 if (path
&& strlen(path
)) {
481 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
483 char *tmpdir
= getenv("TMPDIR");
484 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s/qemu-socket-XXXXXX",
485 tmpdir
? tmpdir
: "/tmp");
487 * This dummy fd usage silences the mktemp() unsecure warning.
488 * Using mkstemp() doesn't make things more secure here
489 * though. bind() complains about existing files, so we have
490 * to unlink first and thus re-open the race window. The
491 * worst case possible is bind() failing, i.e. a DoS attack.
493 fd
= mkstemp(un
.sun_path
); close(fd
);
494 qemu_opt_set(opts
, "path", un
.sun_path
);
498 if (bind(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
499 fprintf(stderr
, "bind(unix:%s): %s\n", un
.sun_path
, strerror(errno
));
502 if (listen(sock
, 1) < 0) {
503 fprintf(stderr
, "listen(unix:%s): %s\n", un
.sun_path
, strerror(errno
));
514 int unix_connect_opts(QemuOpts
*opts
)
516 struct sockaddr_un un
;
517 const char *path
= qemu_opt_get(opts
, "path");
521 fprintf(stderr
, "unix connect: no path specified\n");
525 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
527 perror("socket(unix)");
531 memset(&un
, 0, sizeof(un
));
532 un
.sun_family
= AF_UNIX
;
533 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
534 if (connect(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
535 fprintf(stderr
, "connect(unix:%s): %s\n", path
, strerror(errno
));
543 /* compatibility wrapper */
544 int unix_listen(const char *str
, char *ostr
, int olen
)
550 opts
= qemu_opts_create(&dummy_opts
, NULL
, 0);
552 optstr
= strchr(str
, ',');
556 path
= g_malloc(len
+1);
557 snprintf(path
, len
+1, "%.*s", len
, str
);
558 qemu_opt_set(opts
, "path", path
);
562 qemu_opt_set(opts
, "path", str
);
565 sock
= unix_listen_opts(opts
);
567 if (sock
!= -1 && ostr
)
568 snprintf(ostr
, olen
, "%s%s", qemu_opt_get(opts
, "path"), optstr
? optstr
: "");
573 int unix_connect(const char *path
)
578 opts
= qemu_opts_create(&dummy_opts
, NULL
, 0);
579 qemu_opt_set(opts
, "path", path
);
580 sock
= unix_connect_opts(opts
);
587 int unix_listen_opts(QemuOpts
*opts
)
589 fprintf(stderr
, "unix sockets are not available on windows\n");
594 int unix_connect_opts(QemuOpts
*opts
)
596 fprintf(stderr
, "unix sockets are not available on windows\n");
601 int unix_listen(const char *path
, char *ostr
, int olen
)
603 fprintf(stderr
, "unix sockets are not available on windows\n");
608 int unix_connect(const char *path
)
610 fprintf(stderr
, "unix sockets are not available on windows\n");
618 static void socket_cleanup(void)
624 int socket_init(void)
630 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
632 err
= WSAGetLastError();
633 fprintf(stderr
, "WSAStartup: %d\n", err
);
636 atexit(socket_cleanup
);