]> git.proxmox.com Git - mirror_qemu.git/blame - qemu-sockets.c
convert mux chardev to QemuOpts.
[mirror_qemu.git] / qemu-sockets.c
CommitLineData
305b0eb2
AL
1/*
2 * inet and unix socket functions for qemu
3 *
4 * (c) 2008 Gerd Hoffmann <kraxel@redhat.com>
5 *
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.
9 *
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.
14 */
d247d25f
AL
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <ctype.h>
19#include <errno.h>
20#include <unistd.h>
21
22#include "qemu_socket.h"
47398b9c 23#include "qemu-common.h" /* for qemu_isdigit */
d247d25f
AL
24
25#ifndef AI_ADDRCONFIG
26# define AI_ADDRCONFIG 0
27#endif
28
29static int sockets_debug = 0;
30static const int on=1, off=0;
31
2af2bf67
GH
32/* used temporarely until all users are converted to QemuOpts */
33QemuOptsList dummy_opts = {
34 .name = "dummy",
35 .head = TAILQ_HEAD_INITIALIZER(dummy_opts.head),
36 .desc = {
37 {
38 .name = "path",
39 .type = QEMU_OPT_STRING,
f4c94c7c
GH
40 },{
41 .name = "host",
42 .type = QEMU_OPT_STRING,
43 },{
44 .name = "port",
45 .type = QEMU_OPT_STRING,
46 },{
47 .name = "to",
48 .type = QEMU_OPT_NUMBER,
49 },{
50 .name = "ipv4",
51 .type = QEMU_OPT_BOOL,
52 },{
53 .name = "ipv6",
54 .type = QEMU_OPT_BOOL,
2af2bf67
GH
55 },
56 { /* end if list */ }
57 },
58};
59
d247d25f
AL
60static int inet_getport(struct addrinfo *e)
61{
62 struct sockaddr_in *i4;
63 struct sockaddr_in6 *i6;
64
65 switch (e->ai_family) {
66 case PF_INET6:
67 i6 = (void*)e->ai_addr;
68 return ntohs(i6->sin6_port);
69 case PF_INET:
70 i4 = (void*)e->ai_addr;
71 return ntohs(i4->sin_port);
72 default:
73 return 0;
74 }
75}
76
77static void inet_setport(struct addrinfo *e, int port)
78{
79 struct sockaddr_in *i4;
80 struct sockaddr_in6 *i6;
81
82 switch (e->ai_family) {
83 case PF_INET6:
84 i6 = (void*)e->ai_addr;
85 i6->sin6_port = htons(port);
86 break;
87 case PF_INET:
88 i4 = (void*)e->ai_addr;
89 i4->sin_port = htons(port);
90 break;
91 }
92}
93
94static const char *inet_strfamily(int family)
95{
96 switch (family) {
97 case PF_INET6: return "ipv6";
98 case PF_INET: return "ipv4";
99 case PF_UNIX: return "unix";
100 }
101 return "????";
102}
103
104static void inet_print_addrinfo(const char *tag, struct addrinfo *res)
105{
106 struct addrinfo *e;
107 char uaddr[INET6_ADDRSTRLEN+1];
108 char uport[33];
109
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);
116 }
117}
118
e5bc776f 119int inet_listen_opts(QemuOpts *opts, int port_offset)
d247d25f
AL
120{
121 struct addrinfo ai,*res,*e;
e5bc776f 122 const char *addr;
d247d25f
AL
123 char port[33];
124 char uaddr[INET6_ADDRSTRLEN+1];
125 char uport[33];
e5bc776f 126 int slisten,rc,to,try_next;
d247d25f
AL
127
128 memset(&ai,0, sizeof(ai));
129 ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
130 ai.ai_family = PF_UNSPEC;
e5bc776f 131 ai.ai_socktype = SOCK_STREAM;
d247d25f 132
e5bc776f
GH
133 if (qemu_opt_get(opts, "port") == NULL) {
134 fprintf(stderr, "%s: host and/or port not specified\n", __FUNCTION__);
135 return -1;
d247d25f 136 }
e5bc776f
GH
137 pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port"));
138 addr = qemu_opt_get(opts, "host");
d247d25f 139
e5bc776f
GH
140 to = qemu_opt_get_number(opts, "to", 0);
141 if (qemu_opt_get_bool(opts, "ipv4", 0))
d247d25f 142 ai.ai_family = PF_INET;
e5bc776f 143 if (qemu_opt_get_bool(opts, "ipv6", 0))
d247d25f
AL
144 ai.ai_family = PF_INET6;
145
146 /* lookup */
147 if (port_offset)
148 snprintf(port, sizeof(port), "%d", atoi(port) + port_offset);
149 rc = getaddrinfo(strlen(addr) ? addr : NULL, port, &ai, &res);
150 if (rc != 0) {
e5bc776f
GH
151 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
152 gai_strerror(rc));
d247d25f
AL
153 return -1;
154 }
155 if (sockets_debug)
156 inet_print_addrinfo(__FUNCTION__, res);
157
158 /* create socket + bind */
159 for (e = res; e != NULL; e = e->ai_next) {
39b6efc8
VS
160 getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
161 uaddr,INET6_ADDRSTRLEN,uport,32,
162 NI_NUMERICHOST | NI_NUMERICSERV);
d247d25f 163 slisten = socket(e->ai_family, e->ai_socktype, e->ai_protocol);
39b6efc8 164 if (slisten < 0) {
d247d25f
AL
165 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
166 inet_strfamily(e->ai_family), strerror(errno));
39b6efc8
VS
167 continue;
168 }
d247d25f
AL
169
170 setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
171#ifdef IPV6_V6ONLY
172 if (e->ai_family == PF_INET6) {
173 /* listen on both ipv4 and ipv6 */
39b6efc8
VS
174 setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,
175 sizeof(off));
d247d25f
AL
176 }
177#endif
178
179 for (;;) {
180 if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {
181 if (sockets_debug)
182 fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,
39b6efc8 183 inet_strfamily(e->ai_family), uaddr, inet_getport(e));
d247d25f
AL
184 goto listen;
185 }
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),
190 strerror(errno));
191 if (try_next) {
192 inet_setport(e, inet_getport(e) + 1);
193 continue;
194 }
195 break;
196 }
197 closesocket(slisten);
198 }
199 fprintf(stderr, "%s: FAILED\n", __FUNCTION__);
200 freeaddrinfo(res);
201 return -1;
202
203listen:
204 if (listen(slisten,1) != 0) {
205 perror("listen");
206 closesocket(slisten);
39b6efc8 207 freeaddrinfo(res);
d247d25f
AL
208 return -1;
209 }
e5bc776f
GH
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");
d247d25f
AL
215 freeaddrinfo(res);
216 return slisten;
217}
218
f4c94c7c 219int inet_connect_opts(QemuOpts *opts)
d247d25f
AL
220{
221 struct addrinfo ai,*res,*e;
f4c94c7c
GH
222 const char *addr;
223 const char *port;
d247d25f
AL
224 char uaddr[INET6_ADDRSTRLEN+1];
225 char uport[33];
226 int sock,rc;
227
228 memset(&ai,0, sizeof(ai));
229 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
230 ai.ai_family = PF_UNSPEC;
f4c94c7c 231 ai.ai_socktype = SOCK_STREAM;
d247d25f 232
f4c94c7c
GH
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");
237 return -1;
d247d25f
AL
238 }
239
f4c94c7c 240 if (qemu_opt_get_bool(opts, "ipv4", 0))
d247d25f 241 ai.ai_family = PF_INET;
f4c94c7c 242 if (qemu_opt_get_bool(opts, "ipv6", 0))
d247d25f
AL
243 ai.ai_family = PF_INET6;
244
245 /* lookup */
246 if (0 != (rc = getaddrinfo(addr, port, &ai, &res))) {
f4c94c7c
GH
247 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
248 gai_strerror(rc));
d247d25f
AL
249 return -1;
250 }
251 if (sockets_debug)
252 inet_print_addrinfo(__FUNCTION__, res);
253
254 for (e = res; e != NULL; e = e->ai_next) {
39b6efc8
VS
255 if (getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
256 uaddr,INET6_ADDRSTRLEN,uport,32,
257 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
d247d25f 258 fprintf(stderr,"%s: getnameinfo: oops\n", __FUNCTION__);
39b6efc8
VS
259 continue;
260 }
d247d25f 261 sock = socket(e->ai_family, e->ai_socktype, e->ai_protocol);
39b6efc8 262 if (sock < 0) {
d247d25f 263 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
39b6efc8
VS
264 inet_strfamily(e->ai_family), strerror(errno));
265 continue;
266 }
d247d25f
AL
267 setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
268
39b6efc8
VS
269 /* connect to peer */
270 if (connect(sock,e->ai_addr,e->ai_addrlen) < 0) {
d247d25f
AL
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));
275 closesocket(sock);
39b6efc8
VS
276 continue;
277 }
d247d25f
AL
278 if (sockets_debug)
279 fprintf(stderr, "%s: connect(%s,%s,%s,%s): OK\n", __FUNCTION__,
280 inet_strfamily(e->ai_family),
281 e->ai_canonname, uaddr, uport);
282 freeaddrinfo(res);
39b6efc8 283 return sock;
d247d25f
AL
284 }
285 freeaddrinfo(res);
286 return -1;
287}
288
f4c94c7c
GH
289/* compatibility wrapper */
290static int inet_parse(QemuOpts *opts, const char *str)
291{
292 const char *optstr, *h;
293 char addr[64];
294 char port[33];
295 int pos;
296
297 /* parse address */
298 if (str[0] == ':') {
299 /* no host given */
300 addr[0] = '\0';
301 if (1 != sscanf(str,":%32[^,]%n",port,&pos)) {
302 fprintf(stderr, "%s: portonly parse error (%s)\n",
303 __FUNCTION__, str);
304 return -1;
305 }
306 } else if (str[0] == '[') {
307 /* IPv6 addr */
308 if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) {
309 fprintf(stderr, "%s: ipv6 parse error (%s)\n",
310 __FUNCTION__, str);
311 return -1;
312 }
313 qemu_opt_set(opts, "ipv6", "yes");
314 } else if (qemu_isdigit(str[0])) {
315 /* IPv4 addr */
316 if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) {
317 fprintf(stderr, "%s: ipv4 parse error (%s)\n",
318 __FUNCTION__, str);
319 return -1;
320 }
321 qemu_opt_set(opts, "ipv4", "yes");
322 } else {
323 /* hostname */
324 if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) {
325 fprintf(stderr, "%s: hostname parse error (%s)\n",
326 __FUNCTION__, str);
327 return -1;
328 }
329 }
330 qemu_opt_set(opts, "host", addr);
331 qemu_opt_set(opts, "port", port);
332
333 /* parse options */
334 optstr = str + pos;
335 h = strstr(optstr, ",to=");
336 if (h)
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");
342 return 0;
343}
344
e5bc776f
GH
345int inet_listen(const char *str, char *ostr, int olen,
346 int socktype, int port_offset)
347{
348 QemuOpts *opts;
349 char *optstr;
350 int sock = -1;
351
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 : "");
362 } else {
363 snprintf(ostr, olen, "%s:%s%s",
364 qemu_opt_get(opts, "host"),
365 qemu_opt_get(opts, "port"),
366 optstr ? optstr : "");
367 }
368 }
369 }
370 qemu_opts_del(opts);
371 return sock;
372}
373
f4c94c7c
GH
374int inet_connect(const char *str, int socktype)
375{
376 QemuOpts *opts;
377 int sock = -1;
378
379 opts = qemu_opts_create(&dummy_opts, NULL, 0);
380 if (inet_parse(opts, str) == 0)
381 sock = inet_connect_opts(opts);
382 qemu_opts_del(opts);
383 return sock;
384}
385
d247d25f
AL
386#ifndef _WIN32
387
62b6adfb 388int unix_listen_opts(QemuOpts *opts)
d247d25f
AL
389{
390 struct sockaddr_un un;
62b6adfb
GH
391 const char *path = qemu_opt_get(opts, "path");
392 int sock, fd;
d247d25f
AL
393
394 sock = socket(PF_UNIX, SOCK_STREAM, 0);
395 if (sock < 0) {
39b6efc8
VS
396 perror("socket(unix)");
397 return -1;
d247d25f
AL
398 }
399
d247d25f
AL
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);
404 } else {
405 char *tmpdir = getenv("TMPDIR");
406 snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX",
407 tmpdir ? tmpdir : "/tmp");
408 /*
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.
414 */
415 fd = mkstemp(un.sun_path); close(fd);
62b6adfb 416 qemu_opt_set(opts, "path", un.sun_path);
d247d25f 417 }
d247d25f
AL
418
419 unlink(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));
422 goto err;
423 }
424 if (listen(sock, 1) < 0) {
425 fprintf(stderr, "listen(unix:%s): %s\n", un.sun_path, strerror(errno));
426 goto err;
427 }
428
429 if (sockets_debug)
430 fprintf(stderr, "bind(unix:%s): OK\n", un.sun_path);
d247d25f
AL
431 return sock;
432
433err:
d247d25f
AL
434 closesocket(sock);
435 return -1;
436}
437
2af2bf67 438int unix_connect_opts(QemuOpts *opts)
d247d25f
AL
439{
440 struct sockaddr_un un;
2af2bf67 441 const char *path = qemu_opt_get(opts, "path");
d247d25f
AL
442 int sock;
443
2af2bf67
GH
444 if (NULL == path) {
445 fprintf(stderr, "unix connect: no path specified\n");
446 return -1;
447 }
448
d247d25f
AL
449 sock = socket(PF_UNIX, SOCK_STREAM, 0);
450 if (sock < 0) {
39b6efc8
VS
451 perror("socket(unix)");
452 return -1;
d247d25f
AL
453 }
454
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));
460 return -1;
461 }
462
463 if (sockets_debug)
464 fprintf(stderr, "connect(unix:%s): OK\n", path);
465 return sock;
466}
467
2af2bf67 468/* compatibility wrapper */
62b6adfb
GH
469int unix_listen(const char *str, char *ostr, int olen)
470{
471 QemuOpts *opts;
472 char *path, *optstr;
473 int sock, len;
474
475 opts = qemu_opts_create(&dummy_opts, NULL, 0);
476
477 optstr = strchr(str, ',');
478 if (optstr) {
479 len = optstr - str;
480 if (len) {
481 path = qemu_malloc(len+1);
482 snprintf(path, len+1, "%.*s", len, str);
483 qemu_opt_set(opts, "path", path);
484 qemu_free(path);
485 }
486 } else {
487 qemu_opt_set(opts, "path", str);
488 }
489
490 sock = unix_listen_opts(opts);
491
492 if (sock != -1 && ostr)
493 snprintf(ostr, olen, "%s%s", qemu_opt_get(opts, "path"), optstr ? optstr : "");
494 qemu_opts_del(opts);
495 return sock;
496}
497
2af2bf67
GH
498int unix_connect(const char *path)
499{
500 QemuOpts *opts;
501 int sock;
502
503 opts = qemu_opts_create(&dummy_opts, NULL, 0);
504 qemu_opt_set(opts, "path", path);
505 sock = unix_connect_opts(opts);
506 qemu_opts_del(opts);
507 return sock;
508}
509
d247d25f
AL
510#else
511
108af7b9
GH
512int unix_listen_opts(QemuOpts *opts)
513{
514 fprintf(stderr, "unix sockets are not available on windows\n");
515 return -1;
516}
517
518int unix_connect_opts(QemuOpts *opts)
519{
520 fprintf(stderr, "unix sockets are not available on windows\n");
521 return -1;
522}
523
d247d25f
AL
524int unix_listen(const char *path, char *ostr, int olen)
525{
526 fprintf(stderr, "unix sockets are not available on windows\n");
527 return -1;
528}
529
530int unix_connect(const char *path)
531{
532 fprintf(stderr, "unix sockets are not available on windows\n");
533 return -1;
534}
535
536#endif