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 int sockets_debug = 0;
30 static const int on=1, off=0;
32 /* used temporarely until all users are converted to QemuOpts */
33 QemuOptsList dummy_opts = {
35 .head = TAILQ_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 static 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, "port") == NULL) {
134 fprintf(stderr, "%s: host and/or port not specified\n", __FUNCTION__);
137 pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port"));
138 addr = qemu_opt_get(opts, "host");
140 to = qemu_opt_get_number(opts, "to", 0);
141 if (qemu_opt_get_bool(opts, "ipv4", 0))
142 ai.ai_family = PF_INET;
143 if (qemu_opt_get_bool(opts, "ipv6", 0))
144 ai.ai_family = PF_INET6;
148 snprintf(port, sizeof(port), "%d", atoi(port) + port_offset);
149 rc = getaddrinfo(strlen(addr) ? addr : NULL, port, &ai, &res);
151 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
156 inet_print_addrinfo(__FUNCTION__, res);
158 /* create socket + bind */
159 for (e = res; e != NULL; e = e->ai_next) {
160 getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
161 uaddr,INET6_ADDRSTRLEN,uport,32,
162 NI_NUMERICHOST | NI_NUMERICSERV);
163 slisten = socket(e->ai_family, e->ai_socktype, e->ai_protocol);
165 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
166 inet_strfamily(e->ai_family), strerror(errno));
170 setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
172 if (e->ai_family == PF_INET6) {
173 /* listen on both ipv4 and ipv6 */
174 setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,
180 if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {
182 fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,
183 inet_strfamily(e->ai_family), uaddr, inet_getport(e));
186 try_next = to && (inet_getport(e) <= to + port_offset);
187 if (!try_next || sockets_debug)
188 fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__,
189 inet_strfamily(e->ai_family), uaddr, inet_getport(e),
192 inet_setport(e, inet_getport(e) + 1);
197 closesocket(slisten);
199 fprintf(stderr, "%s: FAILED\n", __FUNCTION__);
204 if (listen(slisten,1) != 0) {
206 closesocket(slisten);
210 snprintf(uport, sizeof(uport), "%d", inet_getport(e) - port_offset);
211 qemu_opt_set(opts, "host", uaddr);
212 qemu_opt_set(opts, "port", uport);
213 qemu_opt_set(opts, "ipv6", (e->ai_family == PF_INET6) ? "on" : "off");
214 qemu_opt_set(opts, "ipv4", (e->ai_family != PF_INET6) ? "on" : "off");
219 int inet_connect_opts(QemuOpts *opts)
221 struct addrinfo ai,*res,*e;
224 char uaddr[INET6_ADDRSTRLEN+1];
228 memset(&ai,0, sizeof(ai));
229 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
230 ai.ai_family = PF_UNSPEC;
231 ai.ai_socktype = SOCK_STREAM;
233 addr = qemu_opt_get(opts, "host");
234 port = qemu_opt_get(opts, "port");
235 if (addr == NULL || port == NULL) {
236 fprintf(stderr, "inet_connect: host and/or port not specified\n");
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,
252 inet_print_addrinfo(__FUNCTION__, res);
254 for (e = res; e != NULL; e = e->ai_next) {
255 if (getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
256 uaddr,INET6_ADDRSTRLEN,uport,32,
257 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
258 fprintf(stderr,"%s: getnameinfo: oops\n", __FUNCTION__);
261 sock = socket(e->ai_family, e->ai_socktype, e->ai_protocol);
263 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
264 inet_strfamily(e->ai_family), strerror(errno));
267 setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
269 /* connect to peer */
270 if (connect(sock,e->ai_addr,e->ai_addrlen) < 0) {
271 if (sockets_debug || NULL == e->ai_next)
272 fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
273 inet_strfamily(e->ai_family),
274 e->ai_canonname, uaddr, uport, strerror(errno));
279 fprintf(stderr, "%s: connect(%s,%s,%s,%s): OK\n", __FUNCTION__,
280 inet_strfamily(e->ai_family),
281 e->ai_canonname, uaddr, uport);
289 /* compatibility wrapper */
290 static int inet_parse(QemuOpts *opts, const char *str)
292 const char *optstr, *h;
301 if (1 != sscanf(str,":%32[^,]%n",port,&pos)) {
302 fprintf(stderr, "%s: portonly parse error (%s)\n",
306 } else if (str[0] == '[') {
308 if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) {
309 fprintf(stderr, "%s: ipv6 parse error (%s)\n",
313 qemu_opt_set(opts, "ipv6", "yes");
314 } else if (qemu_isdigit(str[0])) {
316 if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) {
317 fprintf(stderr, "%s: ipv4 parse error (%s)\n",
321 qemu_opt_set(opts, "ipv4", "yes");
324 if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) {
325 fprintf(stderr, "%s: hostname parse error (%s)\n",
330 qemu_opt_set(opts, "host", addr);
331 qemu_opt_set(opts, "port", port);
335 h = strstr(optstr, ",to=");
337 qemu_opt_set(opts, "to", h+4);
338 if (strstr(optstr, ",ipv4"))
339 qemu_opt_set(opts, "ipv4", "yes");
340 if (strstr(optstr, ",ipv6"))
341 qemu_opt_set(opts, "ipv6", "yes");
345 int inet_listen(const char *str, char *ostr, int olen,
346 int socktype, int port_offset)
352 opts = qemu_opts_create(&dummy_opts, NULL, 0);
353 if (inet_parse(opts, str) == 0) {
354 sock = inet_listen_opts(opts, port_offset);
355 if (sock != -1 && ostr) {
356 optstr = strchr(str, ',');
357 if (qemu_opt_get_bool(opts, "ipv6", 0)) {
358 snprintf(ostr, olen, "[%s]:%s%s",
359 qemu_opt_get(opts, "host"),
360 qemu_opt_get(opts, "port"),
361 optstr ? optstr : "");
363 snprintf(ostr, olen, "%s:%s%s",
364 qemu_opt_get(opts, "host"),
365 qemu_opt_get(opts, "port"),
366 optstr ? optstr : "");
374 int inet_connect(const char *str, int socktype)
379 opts = qemu_opts_create(&dummy_opts, NULL, 0);
380 if (inet_parse(opts, str) == 0)
381 sock = inet_connect_opts(opts);
388 int unix_listen_opts(QemuOpts *opts)
390 struct sockaddr_un un;
391 const char *path = qemu_opt_get(opts, "path");
394 sock = socket(PF_UNIX, SOCK_STREAM, 0);
396 perror("socket(unix)");
400 memset(&un, 0, sizeof(un));
401 un.sun_family = AF_UNIX;
402 if (path && strlen(path)) {
403 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
405 char *tmpdir = getenv("TMPDIR");
406 snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX",
407 tmpdir ? tmpdir : "/tmp");
409 * This dummy fd usage silences the mktemp() unsecure warning.
410 * Using mkstemp() doesn't make things more secure here
411 * though. bind() complains about existing files, so we have
412 * to unlink first and thus re-open the race window. The
413 * worst case possible is bind() failing, i.e. a DoS attack.
415 fd = mkstemp(un.sun_path); close(fd);
416 qemu_opt_set(opts, "path", un.sun_path);
420 if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
421 fprintf(stderr, "bind(unix:%s): %s\n", un.sun_path, strerror(errno));
424 if (listen(sock, 1) < 0) {
425 fprintf(stderr, "listen(unix:%s): %s\n", un.sun_path, strerror(errno));
430 fprintf(stderr, "bind(unix:%s): OK\n", un.sun_path);
438 int unix_connect_opts(QemuOpts *opts)
440 struct sockaddr_un un;
441 const char *path = qemu_opt_get(opts, "path");
445 fprintf(stderr, "unix connect: no path specified\n");
449 sock = socket(PF_UNIX, SOCK_STREAM, 0);
451 perror("socket(unix)");
455 memset(&un, 0, sizeof(un));
456 un.sun_family = AF_UNIX;
457 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
458 if (connect(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
459 fprintf(stderr, "connect(unix:%s): %s\n", path, strerror(errno));
464 fprintf(stderr, "connect(unix:%s): OK\n", path);
468 /* compatibility wrapper */
469 int unix_listen(const char *str, char *ostr, int olen)
475 opts = qemu_opts_create(&dummy_opts, NULL, 0);
477 optstr = strchr(str, ',');
481 path = qemu_malloc(len+1);
482 snprintf(path, len+1, "%.*s", len, str);
483 qemu_opt_set(opts, "path", path);
487 qemu_opt_set(opts, "path", str);
490 sock = unix_listen_opts(opts);
492 if (sock != -1 && ostr)
493 snprintf(ostr, olen, "%s%s", qemu_opt_get(opts, "path"), optstr ? optstr : "");
498 int unix_connect(const char *path)
503 opts = qemu_opts_create(&dummy_opts, NULL, 0);
504 qemu_opt_set(opts, "path", path);
505 sock = unix_connect_opts(opts);
512 int unix_listen_opts(QemuOpts *opts)
514 fprintf(stderr, "unix sockets are not available on windows\n");
518 int unix_connect_opts(QemuOpts *opts)
520 fprintf(stderr, "unix sockets are not available on windows\n");
524 int unix_listen(const char *path, char *ostr, int olen)
526 fprintf(stderr, "unix sockets are not available on windows\n");
530 int unix_connect(const char *path)
532 fprintf(stderr, "unix sockets are not available on windows\n");