2 * inet and unix socket functions for qemu
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 int sockets_debug = 0;
30 static const int on=1, off=0;
32 /* used temporarely until all users are converted to QemuOpts */
33 static QemuOptsList dummy_opts = {
35 .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head),
39 .type = QEMU_OPT_STRING,
42 .type = QEMU_OPT_STRING,
45 .type = QEMU_OPT_STRING,
48 .type = QEMU_OPT_NUMBER,
51 .type = QEMU_OPT_BOOL,
54 .type = QEMU_OPT_BOOL,
60 static int inet_getport(struct addrinfo *e)
62 struct sockaddr_in *i4;
63 struct sockaddr_in6 *i6;
65 switch (e->ai_family) {
67 i6 = (void*)e->ai_addr;
68 return ntohs(i6->sin6_port);
70 i4 = (void*)e->ai_addr;
71 return ntohs(i4->sin_port);
77 static void inet_setport(struct addrinfo *e, int port)
79 struct sockaddr_in *i4;
80 struct sockaddr_in6 *i6;
82 switch (e->ai_family) {
84 i6 = (void*)e->ai_addr;
85 i6->sin6_port = htons(port);
88 i4 = (void*)e->ai_addr;
89 i4->sin_port = htons(port);
94 const char *inet_strfamily(int family)
97 case PF_INET6: return "ipv6";
98 case PF_INET: return "ipv4";
99 case PF_UNIX: return "unix";
104 static void inet_print_addrinfo(const char *tag, struct addrinfo *res)
107 char uaddr[INET6_ADDRSTRLEN+1];
110 for (e = res; e != NULL; e = e->ai_next) {
111 getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
112 uaddr,INET6_ADDRSTRLEN,uport,32,
113 NI_NUMERICHOST | NI_NUMERICSERV);
114 fprintf(stderr,"%s: getaddrinfo: family %s, host %s, port %s\n",
115 tag, inet_strfamily(e->ai_family), uaddr, uport);
119 int inet_listen_opts(QemuOpts *opts, int port_offset)
121 struct addrinfo ai,*res,*e;
124 char uaddr[INET6_ADDRSTRLEN+1];
126 int slisten,rc,to,try_next;
128 memset(&ai,0, sizeof(ai));
129 ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
130 ai.ai_family = PF_UNSPEC;
131 ai.ai_socktype = SOCK_STREAM;
133 if ((qemu_opt_get(opts, "host") == NULL) ||
134 (qemu_opt_get(opts, "port") == NULL)) {
135 fprintf(stderr, "%s: host and/or port not specified\n", __FUNCTION__);
138 pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port"));
139 addr = qemu_opt_get(opts, "host");
141 to = qemu_opt_get_number(opts, "to", 0);
142 if (qemu_opt_get_bool(opts, "ipv4", 0))
143 ai.ai_family = PF_INET;
144 if (qemu_opt_get_bool(opts, "ipv6", 0))
145 ai.ai_family = PF_INET6;
149 snprintf(port, sizeof(port), "%d", atoi(port) + port_offset);
150 rc = getaddrinfo(strlen(addr) ? addr : NULL, port, &ai, &res);
152 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
157 inet_print_addrinfo(__FUNCTION__, res);
159 /* create socket + bind */
160 for (e = res; e != NULL; e = e->ai_next) {
161 getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
162 uaddr,INET6_ADDRSTRLEN,uport,32,
163 NI_NUMERICHOST | NI_NUMERICSERV);
164 slisten = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol);
166 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
167 inet_strfamily(e->ai_family), strerror(errno));
171 setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
173 if (e->ai_family == PF_INET6) {
174 /* listen on both ipv4 and ipv6 */
175 setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,
181 if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {
183 fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,
184 inet_strfamily(e->ai_family), uaddr, inet_getport(e));
187 try_next = to && (inet_getport(e) <= to + port_offset);
188 if (!try_next || sockets_debug)
189 fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__,
190 inet_strfamily(e->ai_family), uaddr, inet_getport(e),
193 inet_setport(e, inet_getport(e) + 1);
198 closesocket(slisten);
200 fprintf(stderr, "%s: FAILED\n", __FUNCTION__);
205 if (listen(slisten,1) != 0) {
207 closesocket(slisten);
211 snprintf(uport, sizeof(uport), "%d", inet_getport(e) - port_offset);
212 qemu_opt_set(opts, "host", uaddr);
213 qemu_opt_set(opts, "port", uport);
214 qemu_opt_set(opts, "ipv6", (e->ai_family == PF_INET6) ? "on" : "off");
215 qemu_opt_set(opts, "ipv4", (e->ai_family != PF_INET6) ? "on" : "off");
220 int inet_connect_opts(QemuOpts *opts)
222 struct addrinfo ai,*res,*e;
225 char uaddr[INET6_ADDRSTRLEN+1];
229 memset(&ai,0, sizeof(ai));
230 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
231 ai.ai_family = PF_UNSPEC;
232 ai.ai_socktype = SOCK_STREAM;
234 addr = qemu_opt_get(opts, "host");
235 port = qemu_opt_get(opts, "port");
236 if (addr == NULL || port == NULL) {
237 fprintf(stderr, "inet_connect: host and/or port not specified\n");
241 if (qemu_opt_get_bool(opts, "ipv4", 0))
242 ai.ai_family = PF_INET;
243 if (qemu_opt_get_bool(opts, "ipv6", 0))
244 ai.ai_family = PF_INET6;
247 if (0 != (rc = getaddrinfo(addr, port, &ai, &res))) {
248 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
253 inet_print_addrinfo(__FUNCTION__, res);
255 for (e = res; e != NULL; e = e->ai_next) {
256 if (getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
257 uaddr,INET6_ADDRSTRLEN,uport,32,
258 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
259 fprintf(stderr,"%s: getnameinfo: oops\n", __FUNCTION__);
262 sock = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol);
264 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
265 inet_strfamily(e->ai_family), strerror(errno));
268 setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
270 /* connect to peer */
271 if (connect(sock,e->ai_addr,e->ai_addrlen) < 0) {
272 if (sockets_debug || NULL == e->ai_next)
273 fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
274 inet_strfamily(e->ai_family),
275 e->ai_canonname, uaddr, uport, strerror(errno));
280 fprintf(stderr, "%s: connect(%s,%s,%s,%s): OK\n", __FUNCTION__,
281 inet_strfamily(e->ai_family),
282 e->ai_canonname, uaddr, uport);
290 int inet_dgram_opts(QemuOpts *opts)
292 struct addrinfo ai, *peer = NULL, *local = NULL;
295 char uaddr[INET6_ADDRSTRLEN+1];
299 /* lookup peer addr */
300 memset(&ai,0, sizeof(ai));
301 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
302 ai.ai_family = PF_UNSPEC;
303 ai.ai_socktype = SOCK_DGRAM;
305 addr = qemu_opt_get(opts, "host");
306 port = qemu_opt_get(opts, "port");
307 if (addr == NULL || strlen(addr) == 0) {
310 if (port == NULL || strlen(port) == 0) {
311 fprintf(stderr, "inet_dgram: port not specified\n");
315 if (qemu_opt_get_bool(opts, "ipv4", 0))
316 ai.ai_family = PF_INET;
317 if (qemu_opt_get_bool(opts, "ipv6", 0))
318 ai.ai_family = PF_INET6;
320 if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) {
321 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
326 fprintf(stderr, "%s: peer (%s:%s)\n", __FUNCTION__, addr, port);
327 inet_print_addrinfo(__FUNCTION__, peer);
330 /* lookup local addr */
331 memset(&ai,0, sizeof(ai));
332 ai.ai_flags = AI_PASSIVE;
333 ai.ai_family = peer->ai_family;
334 ai.ai_socktype = SOCK_DGRAM;
336 addr = qemu_opt_get(opts, "localaddr");
337 port = qemu_opt_get(opts, "localport");
338 if (addr == NULL || strlen(addr) == 0) {
341 if (!port || strlen(port) == 0)
344 if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) {
345 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
350 fprintf(stderr, "%s: local (%s:%s)\n", __FUNCTION__, addr, port);
351 inet_print_addrinfo(__FUNCTION__, local);
355 sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol);
357 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
358 inet_strfamily(peer->ai_family), strerror(errno));
361 setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
364 if (getnameinfo((struct sockaddr*)local->ai_addr,local->ai_addrlen,
365 uaddr,INET6_ADDRSTRLEN,uport,32,
366 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
367 fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
370 if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) {
371 fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,
372 inet_strfamily(local->ai_family), uaddr, inet_getport(local));
376 /* connect to peer */
377 if (getnameinfo((struct sockaddr*)peer->ai_addr, peer->ai_addrlen,
378 uaddr, INET6_ADDRSTRLEN, uport, 32,
379 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
380 fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
383 if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) {
384 fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
385 inet_strfamily(peer->ai_family),
386 peer->ai_canonname, uaddr, uport, strerror(errno));
404 /* compatibility wrapper */
405 static int inet_parse(QemuOpts *opts, const char *str)
407 const char *optstr, *h;
416 if (1 != sscanf(str,":%32[^,]%n",port,&pos)) {
417 fprintf(stderr, "%s: portonly parse error (%s)\n",
421 } else if (str[0] == '[') {
423 if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) {
424 fprintf(stderr, "%s: ipv6 parse error (%s)\n",
428 qemu_opt_set(opts, "ipv6", "on");
429 } else if (qemu_isdigit(str[0])) {
431 if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) {
432 fprintf(stderr, "%s: ipv4 parse error (%s)\n",
436 qemu_opt_set(opts, "ipv4", "on");
439 if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) {
440 fprintf(stderr, "%s: hostname parse error (%s)\n",
445 qemu_opt_set(opts, "host", addr);
446 qemu_opt_set(opts, "port", port);
450 h = strstr(optstr, ",to=");
452 qemu_opt_set(opts, "to", h+4);
453 if (strstr(optstr, ",ipv4"))
454 qemu_opt_set(opts, "ipv4", "on");
455 if (strstr(optstr, ",ipv6"))
456 qemu_opt_set(opts, "ipv6", "on");
460 int inet_listen(const char *str, char *ostr, int olen,
461 int socktype, int port_offset)
467 opts = qemu_opts_create(&dummy_opts, NULL, 0);
468 if (inet_parse(opts, str) == 0) {
469 sock = inet_listen_opts(opts, port_offset);
470 if (sock != -1 && ostr) {
471 optstr = strchr(str, ',');
472 if (qemu_opt_get_bool(opts, "ipv6", 0)) {
473 snprintf(ostr, olen, "[%s]:%s%s",
474 qemu_opt_get(opts, "host"),
475 qemu_opt_get(opts, "port"),
476 optstr ? optstr : "");
478 snprintf(ostr, olen, "%s:%s%s",
479 qemu_opt_get(opts, "host"),
480 qemu_opt_get(opts, "port"),
481 optstr ? optstr : "");
489 int inet_connect(const char *str, int socktype)
494 opts = qemu_opts_create(&dummy_opts, NULL, 0);
495 if (inet_parse(opts, str) == 0)
496 sock = inet_connect_opts(opts);
503 int unix_listen_opts(QemuOpts *opts)
505 struct sockaddr_un un;
506 const char *path = qemu_opt_get(opts, "path");
509 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
511 perror("socket(unix)");
515 memset(&un, 0, sizeof(un));
516 un.sun_family = AF_UNIX;
517 if (path && strlen(path)) {
518 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
520 char *tmpdir = getenv("TMPDIR");
521 snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX",
522 tmpdir ? tmpdir : "/tmp");
524 * This dummy fd usage silences the mktemp() unsecure warning.
525 * Using mkstemp() doesn't make things more secure here
526 * though. bind() complains about existing files, so we have
527 * to unlink first and thus re-open the race window. The
528 * worst case possible is bind() failing, i.e. a DoS attack.
530 fd = mkstemp(un.sun_path); close(fd);
531 qemu_opt_set(opts, "path", un.sun_path);
535 if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
536 fprintf(stderr, "bind(unix:%s): %s\n", un.sun_path, strerror(errno));
539 if (listen(sock, 1) < 0) {
540 fprintf(stderr, "listen(unix:%s): %s\n", un.sun_path, strerror(errno));
545 fprintf(stderr, "bind(unix:%s): OK\n", un.sun_path);
553 int unix_connect_opts(QemuOpts *opts)
555 struct sockaddr_un un;
556 const char *path = qemu_opt_get(opts, "path");
560 fprintf(stderr, "unix connect: no path specified\n");
564 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
566 perror("socket(unix)");
570 memset(&un, 0, sizeof(un));
571 un.sun_family = AF_UNIX;
572 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
573 if (connect(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
574 fprintf(stderr, "connect(unix:%s): %s\n", path, strerror(errno));
579 fprintf(stderr, "connect(unix:%s): OK\n", path);
583 /* compatibility wrapper */
584 int unix_listen(const char *str, char *ostr, int olen)
590 opts = qemu_opts_create(&dummy_opts, NULL, 0);
592 optstr = strchr(str, ',');
596 path = g_malloc(len+1);
597 snprintf(path, len+1, "%.*s", len, str);
598 qemu_opt_set(opts, "path", path);
602 qemu_opt_set(opts, "path", str);
605 sock = unix_listen_opts(opts);
607 if (sock != -1 && ostr)
608 snprintf(ostr, olen, "%s%s", qemu_opt_get(opts, "path"), optstr ? optstr : "");
613 int unix_connect(const char *path)
618 opts = qemu_opts_create(&dummy_opts, NULL, 0);
619 qemu_opt_set(opts, "path", path);
620 sock = unix_connect_opts(opts);
627 int unix_listen_opts(QemuOpts *opts)
629 fprintf(stderr, "unix sockets are not available on windows\n");
634 int unix_connect_opts(QemuOpts *opts)
636 fprintf(stderr, "unix sockets are not available on windows\n");
641 int unix_listen(const char *path, char *ostr, int olen)
643 fprintf(stderr, "unix sockets are not available on windows\n");
648 int unix_connect(const char *path)
650 fprintf(stderr, "unix sockets are not available on windows\n");
658 static void socket_cleanup(void)
664 int socket_init(void)
670 ret = WSAStartup(MAKEWORD(2,2), &Data);
672 err = WSAGetLastError();
673 fprintf(stderr, "WSAStartup: %d\n", err);
676 atexit(socket_cleanup);