]> git.proxmox.com Git - qemu.git/blame - qemu-sockets.c
sockets: add inet_connect_opts
[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
119int inet_listen(const char *str, char *ostr, int olen,
120 int socktype, int port_offset)
121{
122 struct addrinfo ai,*res,*e;
123 char addr[64];
124 char port[33];
125 char uaddr[INET6_ADDRSTRLEN+1];
126 char uport[33];
127 const char *opts, *h;
128 int slisten,rc,pos,to,try_next;
129
130 memset(&ai,0, sizeof(ai));
131 ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
132 ai.ai_family = PF_UNSPEC;
133 ai.ai_socktype = socktype;
134
135 /* parse address */
136 if (str[0] == ':') {
137 /* no host given */
bc575e95 138 addr[0] = '\0';
d247d25f
AL
139 if (1 != sscanf(str,":%32[^,]%n",port,&pos)) {
140 fprintf(stderr, "%s: portonly parse error (%s)\n",
141 __FUNCTION__, str);
142 return -1;
143 }
144 } else if (str[0] == '[') {
145 /* IPv6 addr */
146 if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) {
147 fprintf(stderr, "%s: ipv6 parse error (%s)\n",
148 __FUNCTION__, str);
149 return -1;
150 }
151 ai.ai_family = PF_INET6;
47398b9c 152 } else if (qemu_isdigit(str[0])) {
d247d25f
AL
153 /* IPv4 addr */
154 if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) {
155 fprintf(stderr, "%s: ipv4 parse error (%s)\n",
156 __FUNCTION__, str);
157 return -1;
158 }
159 ai.ai_family = PF_INET;
160 } else {
161 /* hostname */
162 if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) {
163 fprintf(stderr, "%s: hostname parse error (%s)\n",
164 __FUNCTION__, str);
165 return -1;
166 }
167 }
168
169 /* parse options */
170 opts = str + pos;
171 h = strstr(opts, ",to=");
172 to = h ? atoi(h+4) : 0;
173 if (strstr(opts, ",ipv4"))
174 ai.ai_family = PF_INET;
175 if (strstr(opts, ",ipv6"))
176 ai.ai_family = PF_INET6;
177
178 /* lookup */
179 if (port_offset)
180 snprintf(port, sizeof(port), "%d", atoi(port) + port_offset);
181 rc = getaddrinfo(strlen(addr) ? addr : NULL, port, &ai, &res);
182 if (rc != 0) {
183 fprintf(stderr,"%s: getaddrinfo(%s,%s): %s\n", __FUNCTION__,
184 addr, port, gai_strerror(rc));
185 return -1;
186 }
187 if (sockets_debug)
188 inet_print_addrinfo(__FUNCTION__, res);
189
190 /* create socket + bind */
191 for (e = res; e != NULL; e = e->ai_next) {
39b6efc8 192 getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
193 uaddr,INET6_ADDRSTRLEN,uport,32,
194 NI_NUMERICHOST | NI_NUMERICSERV);
d247d25f 195 slisten = socket(e->ai_family, e->ai_socktype, e->ai_protocol);
39b6efc8 196 if (slisten < 0) {
d247d25f
AL
197 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
198 inet_strfamily(e->ai_family), strerror(errno));
39b6efc8 199 continue;
200 }
d247d25f
AL
201
202 setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
203#ifdef IPV6_V6ONLY
204 if (e->ai_family == PF_INET6) {
205 /* listen on both ipv4 and ipv6 */
39b6efc8 206 setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,
207 sizeof(off));
d247d25f
AL
208 }
209#endif
210
211 for (;;) {
212 if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {
213 if (sockets_debug)
214 fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,
39b6efc8 215 inet_strfamily(e->ai_family), uaddr, inet_getport(e));
d247d25f
AL
216 goto listen;
217 }
218 try_next = to && (inet_getport(e) <= to + port_offset);
219 if (!try_next || sockets_debug)
220 fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__,
221 inet_strfamily(e->ai_family), uaddr, inet_getport(e),
222 strerror(errno));
223 if (try_next) {
224 inet_setport(e, inet_getport(e) + 1);
225 continue;
226 }
227 break;
228 }
229 closesocket(slisten);
230 }
231 fprintf(stderr, "%s: FAILED\n", __FUNCTION__);
232 freeaddrinfo(res);
233 return -1;
234
235listen:
236 if (listen(slisten,1) != 0) {
237 perror("listen");
238 closesocket(slisten);
39b6efc8 239 freeaddrinfo(res);
d247d25f
AL
240 return -1;
241 }
242 if (ostr) {
243 if (e->ai_family == PF_INET6) {
244 snprintf(ostr, olen, "[%s]:%d%s", uaddr,
245 inet_getport(e) - port_offset, opts);
246 } else {
247 snprintf(ostr, olen, "%s:%d%s", uaddr,
248 inet_getport(e) - port_offset, opts);
249 }
250 }
251 freeaddrinfo(res);
252 return slisten;
253}
254
f4c94c7c 255int inet_connect_opts(QemuOpts *opts)
d247d25f
AL
256{
257 struct addrinfo ai,*res,*e;
f4c94c7c
GH
258 const char *addr;
259 const char *port;
d247d25f
AL
260 char uaddr[INET6_ADDRSTRLEN+1];
261 char uport[33];
262 int sock,rc;
263
264 memset(&ai,0, sizeof(ai));
265 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
266 ai.ai_family = PF_UNSPEC;
f4c94c7c 267 ai.ai_socktype = SOCK_STREAM;
d247d25f 268
f4c94c7c
GH
269 addr = qemu_opt_get(opts, "host");
270 port = qemu_opt_get(opts, "port");
271 if (addr == NULL || port == NULL) {
272 fprintf(stderr, "inet_connect: host and/or port not specified\n");
273 return -1;
d247d25f
AL
274 }
275
f4c94c7c 276 if (qemu_opt_get_bool(opts, "ipv4", 0))
d247d25f 277 ai.ai_family = PF_INET;
f4c94c7c 278 if (qemu_opt_get_bool(opts, "ipv6", 0))
d247d25f
AL
279 ai.ai_family = PF_INET6;
280
281 /* lookup */
282 if (0 != (rc = getaddrinfo(addr, port, &ai, &res))) {
f4c94c7c
GH
283 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
284 gai_strerror(rc));
d247d25f
AL
285 return -1;
286 }
287 if (sockets_debug)
288 inet_print_addrinfo(__FUNCTION__, res);
289
290 for (e = res; e != NULL; e = e->ai_next) {
39b6efc8 291 if (getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
292 uaddr,INET6_ADDRSTRLEN,uport,32,
293 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
d247d25f 294 fprintf(stderr,"%s: getnameinfo: oops\n", __FUNCTION__);
39b6efc8 295 continue;
296 }
d247d25f 297 sock = socket(e->ai_family, e->ai_socktype, e->ai_protocol);
39b6efc8 298 if (sock < 0) {
d247d25f 299 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
39b6efc8 300 inet_strfamily(e->ai_family), strerror(errno));
301 continue;
302 }
d247d25f
AL
303 setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
304
39b6efc8 305 /* connect to peer */
306 if (connect(sock,e->ai_addr,e->ai_addrlen) < 0) {
d247d25f
AL
307 if (sockets_debug || NULL == e->ai_next)
308 fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
309 inet_strfamily(e->ai_family),
310 e->ai_canonname, uaddr, uport, strerror(errno));
311 closesocket(sock);
39b6efc8 312 continue;
313 }
d247d25f
AL
314 if (sockets_debug)
315 fprintf(stderr, "%s: connect(%s,%s,%s,%s): OK\n", __FUNCTION__,
316 inet_strfamily(e->ai_family),
317 e->ai_canonname, uaddr, uport);
318 freeaddrinfo(res);
39b6efc8 319 return sock;
d247d25f
AL
320 }
321 freeaddrinfo(res);
322 return -1;
323}
324
f4c94c7c
GH
325/* compatibility wrapper */
326static int inet_parse(QemuOpts *opts, const char *str)
327{
328 const char *optstr, *h;
329 char addr[64];
330 char port[33];
331 int pos;
332
333 /* parse address */
334 if (str[0] == ':') {
335 /* no host given */
336 addr[0] = '\0';
337 if (1 != sscanf(str,":%32[^,]%n",port,&pos)) {
338 fprintf(stderr, "%s: portonly parse error (%s)\n",
339 __FUNCTION__, str);
340 return -1;
341 }
342 } else if (str[0] == '[') {
343 /* IPv6 addr */
344 if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) {
345 fprintf(stderr, "%s: ipv6 parse error (%s)\n",
346 __FUNCTION__, str);
347 return -1;
348 }
349 qemu_opt_set(opts, "ipv6", "yes");
350 } else if (qemu_isdigit(str[0])) {
351 /* IPv4 addr */
352 if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) {
353 fprintf(stderr, "%s: ipv4 parse error (%s)\n",
354 __FUNCTION__, str);
355 return -1;
356 }
357 qemu_opt_set(opts, "ipv4", "yes");
358 } else {
359 /* hostname */
360 if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) {
361 fprintf(stderr, "%s: hostname parse error (%s)\n",
362 __FUNCTION__, str);
363 return -1;
364 }
365 }
366 qemu_opt_set(opts, "host", addr);
367 qemu_opt_set(opts, "port", port);
368
369 /* parse options */
370 optstr = str + pos;
371 h = strstr(optstr, ",to=");
372 if (h)
373 qemu_opt_set(opts, "to", h+4);
374 if (strstr(optstr, ",ipv4"))
375 qemu_opt_set(opts, "ipv4", "yes");
376 if (strstr(optstr, ",ipv6"))
377 qemu_opt_set(opts, "ipv6", "yes");
378 return 0;
379}
380
381int inet_connect(const char *str, int socktype)
382{
383 QemuOpts *opts;
384 int sock = -1;
385
386 opts = qemu_opts_create(&dummy_opts, NULL, 0);
387 if (inet_parse(opts, str) == 0)
388 sock = inet_connect_opts(opts);
389 qemu_opts_del(opts);
390 return sock;
391}
392
d247d25f
AL
393#ifndef _WIN32
394
62b6adfb 395int unix_listen_opts(QemuOpts *opts)
d247d25f
AL
396{
397 struct sockaddr_un un;
62b6adfb
GH
398 const char *path = qemu_opt_get(opts, "path");
399 int sock, fd;
d247d25f
AL
400
401 sock = socket(PF_UNIX, SOCK_STREAM, 0);
402 if (sock < 0) {
39b6efc8 403 perror("socket(unix)");
404 return -1;
d247d25f
AL
405 }
406
d247d25f
AL
407 memset(&un, 0, sizeof(un));
408 un.sun_family = AF_UNIX;
409 if (path && strlen(path)) {
410 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
411 } else {
412 char *tmpdir = getenv("TMPDIR");
413 snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX",
414 tmpdir ? tmpdir : "/tmp");
415 /*
416 * This dummy fd usage silences the mktemp() unsecure warning.
417 * Using mkstemp() doesn't make things more secure here
418 * though. bind() complains about existing files, so we have
419 * to unlink first and thus re-open the race window. The
420 * worst case possible is bind() failing, i.e. a DoS attack.
421 */
422 fd = mkstemp(un.sun_path); close(fd);
62b6adfb 423 qemu_opt_set(opts, "path", un.sun_path);
d247d25f 424 }
d247d25f
AL
425
426 unlink(un.sun_path);
427 if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
428 fprintf(stderr, "bind(unix:%s): %s\n", un.sun_path, strerror(errno));
429 goto err;
430 }
431 if (listen(sock, 1) < 0) {
432 fprintf(stderr, "listen(unix:%s): %s\n", un.sun_path, strerror(errno));
433 goto err;
434 }
435
436 if (sockets_debug)
437 fprintf(stderr, "bind(unix:%s): OK\n", un.sun_path);
d247d25f
AL
438 return sock;
439
440err:
d247d25f
AL
441 closesocket(sock);
442 return -1;
443}
444
2af2bf67 445int unix_connect_opts(QemuOpts *opts)
d247d25f
AL
446{
447 struct sockaddr_un un;
2af2bf67 448 const char *path = qemu_opt_get(opts, "path");
d247d25f
AL
449 int sock;
450
2af2bf67
GH
451 if (NULL == path) {
452 fprintf(stderr, "unix connect: no path specified\n");
453 return -1;
454 }
455
d247d25f
AL
456 sock = socket(PF_UNIX, SOCK_STREAM, 0);
457 if (sock < 0) {
39b6efc8 458 perror("socket(unix)");
459 return -1;
d247d25f
AL
460 }
461
462 memset(&un, 0, sizeof(un));
463 un.sun_family = AF_UNIX;
464 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
465 if (connect(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
466 fprintf(stderr, "connect(unix:%s): %s\n", path, strerror(errno));
467 return -1;
468 }
469
470 if (sockets_debug)
471 fprintf(stderr, "connect(unix:%s): OK\n", path);
472 return sock;
473}
474
2af2bf67 475/* compatibility wrapper */
62b6adfb
GH
476int unix_listen(const char *str, char *ostr, int olen)
477{
478 QemuOpts *opts;
479 char *path, *optstr;
480 int sock, len;
481
482 opts = qemu_opts_create(&dummy_opts, NULL, 0);
483
484 optstr = strchr(str, ',');
485 if (optstr) {
486 len = optstr - str;
487 if (len) {
488 path = qemu_malloc(len+1);
489 snprintf(path, len+1, "%.*s", len, str);
490 qemu_opt_set(opts, "path", path);
491 qemu_free(path);
492 }
493 } else {
494 qemu_opt_set(opts, "path", str);
495 }
496
497 sock = unix_listen_opts(opts);
498
499 if (sock != -1 && ostr)
500 snprintf(ostr, olen, "%s%s", qemu_opt_get(opts, "path"), optstr ? optstr : "");
501 qemu_opts_del(opts);
502 return sock;
503}
504
2af2bf67
GH
505int unix_connect(const char *path)
506{
507 QemuOpts *opts;
508 int sock;
509
510 opts = qemu_opts_create(&dummy_opts, NULL, 0);
511 qemu_opt_set(opts, "path", path);
512 sock = unix_connect_opts(opts);
513 qemu_opts_del(opts);
514 return sock;
515}
516
d247d25f
AL
517#else
518
108af7b9
GH
519int unix_listen_opts(QemuOpts *opts)
520{
521 fprintf(stderr, "unix sockets are not available on windows\n");
522 return -1;
523}
524
525int unix_connect_opts(QemuOpts *opts)
526{
527 fprintf(stderr, "unix sockets are not available on windows\n");
528 return -1;
529}
530
d247d25f
AL
531int unix_listen(const char *path, char *ostr, int olen)
532{
533 fprintf(stderr, "unix sockets are not available on windows\n");
534 return -1;
535}
536
537int unix_connect(const char *path)
538{
539 fprintf(stderr, "unix sockets are not available on windows\n");
540 return -1;
541}
542
543#endif