blob: e6e6c72dfdd53c52785957da4f63c1b34a3f651b [file] [log] [blame]
aliguori305b0eb2008-11-13 16:19:54 +00001/*
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 */
aliguorid247d252008-11-11 20:46:40 +000015#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"
blueswir147398b92008-11-22 20:04:24 +000023#include "qemu-common.h" /* for qemu_isdigit */
aliguorid247d252008-11-11 20:46:40 +000024
25#ifndef AI_ADDRCONFIG
26# define AI_ADDRCONFIG 0
27#endif
28
aliguorid247d252008-11-11 20:46:40 +000029static const int on=1, off=0;
30
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +020031/* used temporarely until all users are converted to QemuOpts */
Blue Swirlc1390902009-09-12 09:58:51 +000032static QemuOptsList dummy_opts = {
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +020033 .name = "dummy",
Blue Swirl72cf2d42009-09-12 07:36:22 +000034 .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head),
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +020035 .desc = {
36 {
37 .name = "path",
38 .type = QEMU_OPT_STRING,
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +020039 },{
40 .name = "host",
41 .type = QEMU_OPT_STRING,
42 },{
43 .name = "port",
44 .type = QEMU_OPT_STRING,
45 },{
46 .name = "to",
47 .type = QEMU_OPT_NUMBER,
48 },{
49 .name = "ipv4",
50 .type = QEMU_OPT_BOOL,
51 },{
52 .name = "ipv6",
53 .type = QEMU_OPT_BOOL,
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +020054 },
55 { /* end if list */ }
56 },
57};
58
aliguorid247d252008-11-11 20:46:40 +000059static int inet_getport(struct addrinfo *e)
60{
61 struct sockaddr_in *i4;
62 struct sockaddr_in6 *i6;
63
64 switch (e->ai_family) {
65 case PF_INET6:
66 i6 = (void*)e->ai_addr;
67 return ntohs(i6->sin6_port);
68 case PF_INET:
69 i4 = (void*)e->ai_addr;
70 return ntohs(i4->sin_port);
71 default:
72 return 0;
73 }
74}
75
76static void inet_setport(struct addrinfo *e, int port)
77{
78 struct sockaddr_in *i4;
79 struct sockaddr_in6 *i6;
80
81 switch (e->ai_family) {
82 case PF_INET6:
83 i6 = (void*)e->ai_addr;
84 i6->sin6_port = htons(port);
85 break;
86 case PF_INET:
87 i4 = (void*)e->ai_addr;
88 i4->sin_port = htons(port);
89 break;
90 }
91}
92
Luiz Capitulinoc9c4b342010-01-20 11:42:38 -020093const char *inet_strfamily(int family)
aliguorid247d252008-11-11 20:46:40 +000094{
95 switch (family) {
96 case PF_INET6: return "ipv6";
97 case PF_INET: return "ipv4";
98 case PF_UNIX: return "unix";
99 }
Luiz Capitulinoc4453212010-01-20 11:42:39 -0200100 return "unknown";
aliguorid247d252008-11-11 20:46:40 +0000101}
102
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200103int inet_listen_opts(QemuOpts *opts, int port_offset)
aliguorid247d252008-11-11 20:46:40 +0000104{
105 struct addrinfo ai,*res,*e;
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200106 const char *addr;
aliguorid247d252008-11-11 20:46:40 +0000107 char port[33];
108 char uaddr[INET6_ADDRSTRLEN+1];
109 char uport[33];
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200110 int slisten,rc,to,try_next;
aliguorid247d252008-11-11 20:46:40 +0000111
112 memset(&ai,0, sizeof(ai));
113 ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
114 ai.ai_family = PF_UNSPEC;
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200115 ai.ai_socktype = SOCK_STREAM;
aliguorid247d252008-11-11 20:46:40 +0000116
Jens Osterkampe23a22e2010-04-12 10:51:01 +0200117 if ((qemu_opt_get(opts, "host") == NULL) ||
118 (qemu_opt_get(opts, "port") == NULL)) {
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200119 fprintf(stderr, "%s: host and/or port not specified\n", __FUNCTION__);
120 return -1;
aliguorid247d252008-11-11 20:46:40 +0000121 }
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200122 pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port"));
123 addr = qemu_opt_get(opts, "host");
aliguorid247d252008-11-11 20:46:40 +0000124
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200125 to = qemu_opt_get_number(opts, "to", 0);
126 if (qemu_opt_get_bool(opts, "ipv4", 0))
aliguorid247d252008-11-11 20:46:40 +0000127 ai.ai_family = PF_INET;
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200128 if (qemu_opt_get_bool(opts, "ipv6", 0))
aliguorid247d252008-11-11 20:46:40 +0000129 ai.ai_family = PF_INET6;
130
131 /* lookup */
132 if (port_offset)
133 snprintf(port, sizeof(port), "%d", atoi(port) + port_offset);
134 rc = getaddrinfo(strlen(addr) ? addr : NULL, port, &ai, &res);
135 if (rc != 0) {
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200136 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
137 gai_strerror(rc));
aliguorid247d252008-11-11 20:46:40 +0000138 return -1;
139 }
aliguorid247d252008-11-11 20:46:40 +0000140
141 /* create socket + bind */
142 for (e = res; e != NULL; e = e->ai_next) {
vibi39b6efc2009-05-06 15:27:03 +0530143 getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
144 uaddr,INET6_ADDRSTRLEN,uport,32,
145 NI_NUMERICHOST | NI_NUMERICSERV);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100146 slisten = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol);
vibi39b6efc2009-05-06 15:27:03 +0530147 if (slisten < 0) {
aliguorid247d252008-11-11 20:46:40 +0000148 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
149 inet_strfamily(e->ai_family), strerror(errno));
vibi39b6efc2009-05-06 15:27:03 +0530150 continue;
151 }
aliguorid247d252008-11-11 20:46:40 +0000152
153 setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
154#ifdef IPV6_V6ONLY
155 if (e->ai_family == PF_INET6) {
156 /* listen on both ipv4 and ipv6 */
vibi39b6efc2009-05-06 15:27:03 +0530157 setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,
158 sizeof(off));
aliguorid247d252008-11-11 20:46:40 +0000159 }
160#endif
161
162 for (;;) {
163 if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {
aliguorid247d252008-11-11 20:46:40 +0000164 goto listen;
165 }
166 try_next = to && (inet_getport(e) <= to + port_offset);
Markus Armbruster136faa32012-02-07 15:09:14 +0100167 if (!try_next)
aliguorid247d252008-11-11 20:46:40 +0000168 fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__,
169 inet_strfamily(e->ai_family), uaddr, inet_getport(e),
170 strerror(errno));
171 if (try_next) {
172 inet_setport(e, inet_getport(e) + 1);
173 continue;
174 }
175 break;
176 }
177 closesocket(slisten);
178 }
179 fprintf(stderr, "%s: FAILED\n", __FUNCTION__);
180 freeaddrinfo(res);
181 return -1;
182
183listen:
184 if (listen(slisten,1) != 0) {
185 perror("listen");
186 closesocket(slisten);
vibi39b6efc2009-05-06 15:27:03 +0530187 freeaddrinfo(res);
aliguorid247d252008-11-11 20:46:40 +0000188 return -1;
189 }
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200190 snprintf(uport, sizeof(uport), "%d", inet_getport(e) - port_offset);
191 qemu_opt_set(opts, "host", uaddr);
192 qemu_opt_set(opts, "port", uport);
193 qemu_opt_set(opts, "ipv6", (e->ai_family == PF_INET6) ? "on" : "off");
194 qemu_opt_set(opts, "ipv4", (e->ai_family != PF_INET6) ? "on" : "off");
aliguorid247d252008-11-11 20:46:40 +0000195 freeaddrinfo(res);
196 return slisten;
197}
198
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200199int inet_connect_opts(QemuOpts *opts)
aliguorid247d252008-11-11 20:46:40 +0000200{
201 struct addrinfo ai,*res,*e;
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200202 const char *addr;
203 const char *port;
aliguorid247d252008-11-11 20:46:40 +0000204 char uaddr[INET6_ADDRSTRLEN+1];
205 char uport[33];
206 int sock,rc;
207
208 memset(&ai,0, sizeof(ai));
209 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
210 ai.ai_family = PF_UNSPEC;
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200211 ai.ai_socktype = SOCK_STREAM;
aliguorid247d252008-11-11 20:46:40 +0000212
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200213 addr = qemu_opt_get(opts, "host");
214 port = qemu_opt_get(opts, "port");
215 if (addr == NULL || port == NULL) {
216 fprintf(stderr, "inet_connect: host and/or port not specified\n");
217 return -1;
aliguorid247d252008-11-11 20:46:40 +0000218 }
219
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200220 if (qemu_opt_get_bool(opts, "ipv4", 0))
aliguorid247d252008-11-11 20:46:40 +0000221 ai.ai_family = PF_INET;
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200222 if (qemu_opt_get_bool(opts, "ipv6", 0))
aliguorid247d252008-11-11 20:46:40 +0000223 ai.ai_family = PF_INET6;
224
225 /* lookup */
226 if (0 != (rc = getaddrinfo(addr, port, &ai, &res))) {
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200227 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
228 gai_strerror(rc));
aliguorid247d252008-11-11 20:46:40 +0000229 return -1;
230 }
aliguorid247d252008-11-11 20:46:40 +0000231
232 for (e = res; e != NULL; e = e->ai_next) {
vibi39b6efc2009-05-06 15:27:03 +0530233 if (getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
234 uaddr,INET6_ADDRSTRLEN,uport,32,
235 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
aliguorid247d252008-11-11 20:46:40 +0000236 fprintf(stderr,"%s: getnameinfo: oops\n", __FUNCTION__);
vibi39b6efc2009-05-06 15:27:03 +0530237 continue;
238 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100239 sock = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol);
vibi39b6efc2009-05-06 15:27:03 +0530240 if (sock < 0) {
aliguorid247d252008-11-11 20:46:40 +0000241 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
vibi39b6efc2009-05-06 15:27:03 +0530242 inet_strfamily(e->ai_family), strerror(errno));
243 continue;
244 }
aliguorid247d252008-11-11 20:46:40 +0000245 setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
246
vibi39b6efc2009-05-06 15:27:03 +0530247 /* connect to peer */
248 if (connect(sock,e->ai_addr,e->ai_addrlen) < 0) {
Markus Armbruster136faa32012-02-07 15:09:14 +0100249 if (NULL == e->ai_next)
aliguorid247d252008-11-11 20:46:40 +0000250 fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
251 inet_strfamily(e->ai_family),
252 e->ai_canonname, uaddr, uport, strerror(errno));
253 closesocket(sock);
vibi39b6efc2009-05-06 15:27:03 +0530254 continue;
255 }
aliguorid247d252008-11-11 20:46:40 +0000256 freeaddrinfo(res);
vibi39b6efc2009-05-06 15:27:03 +0530257 return sock;
aliguorid247d252008-11-11 20:46:40 +0000258 }
259 freeaddrinfo(res);
260 return -1;
261}
262
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200263int inet_dgram_opts(QemuOpts *opts)
264{
265 struct addrinfo ai, *peer = NULL, *local = NULL;
266 const char *addr;
267 const char *port;
268 char uaddr[INET6_ADDRSTRLEN+1];
269 char uport[33];
270 int sock = -1, rc;
271
272 /* lookup peer addr */
273 memset(&ai,0, sizeof(ai));
274 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
275 ai.ai_family = PF_UNSPEC;
276 ai.ai_socktype = SOCK_DGRAM;
277
278 addr = qemu_opt_get(opts, "host");
279 port = qemu_opt_get(opts, "port");
280 if (addr == NULL || strlen(addr) == 0) {
281 addr = "localhost";
282 }
283 if (port == NULL || strlen(port) == 0) {
284 fprintf(stderr, "inet_dgram: port not specified\n");
285 return -1;
286 }
287
288 if (qemu_opt_get_bool(opts, "ipv4", 0))
289 ai.ai_family = PF_INET;
290 if (qemu_opt_get_bool(opts, "ipv6", 0))
291 ai.ai_family = PF_INET6;
292
293 if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) {
294 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
295 gai_strerror(rc));
296 return -1;
297 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200298
299 /* lookup local addr */
300 memset(&ai,0, sizeof(ai));
301 ai.ai_flags = AI_PASSIVE;
302 ai.ai_family = peer->ai_family;
303 ai.ai_socktype = SOCK_DGRAM;
304
305 addr = qemu_opt_get(opts, "localaddr");
306 port = qemu_opt_get(opts, "localport");
307 if (addr == NULL || strlen(addr) == 0) {
308 addr = NULL;
309 }
310 if (!port || strlen(port) == 0)
311 port = "0";
312
313 if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) {
314 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
315 gai_strerror(rc));
316 return -1;
317 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200318
319 /* create socket */
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100320 sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200321 if (sock < 0) {
322 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
323 inet_strfamily(peer->ai_family), strerror(errno));
324 goto err;
325 }
326 setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
327
328 /* bind socket */
329 if (getnameinfo((struct sockaddr*)local->ai_addr,local->ai_addrlen,
330 uaddr,INET6_ADDRSTRLEN,uport,32,
331 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
332 fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
333 goto err;
334 }
335 if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) {
336 fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,
337 inet_strfamily(local->ai_family), uaddr, inet_getport(local));
338 goto err;
339 }
340
341 /* connect to peer */
342 if (getnameinfo((struct sockaddr*)peer->ai_addr, peer->ai_addrlen,
343 uaddr, INET6_ADDRSTRLEN, uport, 32,
344 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
345 fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
346 goto err;
347 }
348 if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) {
349 fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
350 inet_strfamily(peer->ai_family),
351 peer->ai_canonname, uaddr, uport, strerror(errno));
352 goto err;
353 }
354
355 freeaddrinfo(local);
356 freeaddrinfo(peer);
357 return sock;
358
359err:
360 if (-1 != sock)
361 closesocket(sock);
362 if (local)
363 freeaddrinfo(local);
364 if (peer)
365 freeaddrinfo(peer);
366 return -1;
367}
368
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200369/* compatibility wrapper */
370static int inet_parse(QemuOpts *opts, const char *str)
371{
372 const char *optstr, *h;
373 char addr[64];
374 char port[33];
375 int pos;
376
377 /* parse address */
378 if (str[0] == ':') {
379 /* no host given */
380 addr[0] = '\0';
381 if (1 != sscanf(str,":%32[^,]%n",port,&pos)) {
382 fprintf(stderr, "%s: portonly parse error (%s)\n",
383 __FUNCTION__, str);
384 return -1;
385 }
386 } else if (str[0] == '[') {
387 /* IPv6 addr */
388 if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) {
389 fprintf(stderr, "%s: ipv6 parse error (%s)\n",
390 __FUNCTION__, str);
391 return -1;
392 }
Marcelo Tosatti2198a622010-02-09 15:31:46 -0200393 qemu_opt_set(opts, "ipv6", "on");
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200394 } else if (qemu_isdigit(str[0])) {
395 /* IPv4 addr */
396 if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) {
397 fprintf(stderr, "%s: ipv4 parse error (%s)\n",
398 __FUNCTION__, str);
399 return -1;
400 }
Marcelo Tosatti2198a622010-02-09 15:31:46 -0200401 qemu_opt_set(opts, "ipv4", "on");
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200402 } else {
403 /* hostname */
404 if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) {
405 fprintf(stderr, "%s: hostname parse error (%s)\n",
406 __FUNCTION__, str);
407 return -1;
408 }
409 }
410 qemu_opt_set(opts, "host", addr);
411 qemu_opt_set(opts, "port", port);
412
413 /* parse options */
414 optstr = str + pos;
415 h = strstr(optstr, ",to=");
416 if (h)
417 qemu_opt_set(opts, "to", h+4);
418 if (strstr(optstr, ",ipv4"))
Marcelo Tosatti2198a622010-02-09 15:31:46 -0200419 qemu_opt_set(opts, "ipv4", "on");
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200420 if (strstr(optstr, ",ipv6"))
Marcelo Tosatti2198a622010-02-09 15:31:46 -0200421 qemu_opt_set(opts, "ipv6", "on");
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200422 return 0;
423}
424
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200425int inet_listen(const char *str, char *ostr, int olen,
426 int socktype, int port_offset)
427{
428 QemuOpts *opts;
429 char *optstr;
430 int sock = -1;
431
432 opts = qemu_opts_create(&dummy_opts, NULL, 0);
433 if (inet_parse(opts, str) == 0) {
434 sock = inet_listen_opts(opts, port_offset);
435 if (sock != -1 && ostr) {
436 optstr = strchr(str, ',');
437 if (qemu_opt_get_bool(opts, "ipv6", 0)) {
438 snprintf(ostr, olen, "[%s]:%s%s",
439 qemu_opt_get(opts, "host"),
440 qemu_opt_get(opts, "port"),
441 optstr ? optstr : "");
442 } else {
443 snprintf(ostr, olen, "%s:%s%s",
444 qemu_opt_get(opts, "host"),
445 qemu_opt_get(opts, "port"),
446 optstr ? optstr : "");
447 }
448 }
449 }
450 qemu_opts_del(opts);
451 return sock;
452}
453
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200454int inet_connect(const char *str, int socktype)
455{
456 QemuOpts *opts;
457 int sock = -1;
458
459 opts = qemu_opts_create(&dummy_opts, NULL, 0);
460 if (inet_parse(opts, str) == 0)
461 sock = inet_connect_opts(opts);
462 qemu_opts_del(opts);
463 return sock;
464}
465
aliguorid247d252008-11-11 20:46:40 +0000466#ifndef _WIN32
467
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200468int unix_listen_opts(QemuOpts *opts)
aliguorid247d252008-11-11 20:46:40 +0000469{
470 struct sockaddr_un un;
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200471 const char *path = qemu_opt_get(opts, "path");
472 int sock, fd;
aliguorid247d252008-11-11 20:46:40 +0000473
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100474 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
aliguorid247d252008-11-11 20:46:40 +0000475 if (sock < 0) {
vibi39b6efc2009-05-06 15:27:03 +0530476 perror("socket(unix)");
477 return -1;
aliguorid247d252008-11-11 20:46:40 +0000478 }
479
aliguorid247d252008-11-11 20:46:40 +0000480 memset(&un, 0, sizeof(un));
481 un.sun_family = AF_UNIX;
482 if (path && strlen(path)) {
483 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
484 } else {
485 char *tmpdir = getenv("TMPDIR");
486 snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX",
487 tmpdir ? tmpdir : "/tmp");
488 /*
489 * This dummy fd usage silences the mktemp() unsecure warning.
490 * Using mkstemp() doesn't make things more secure here
491 * though. bind() complains about existing files, so we have
492 * to unlink first and thus re-open the race window. The
493 * worst case possible is bind() failing, i.e. a DoS attack.
494 */
495 fd = mkstemp(un.sun_path); close(fd);
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200496 qemu_opt_set(opts, "path", un.sun_path);
aliguorid247d252008-11-11 20:46:40 +0000497 }
aliguorid247d252008-11-11 20:46:40 +0000498
499 unlink(un.sun_path);
500 if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
501 fprintf(stderr, "bind(unix:%s): %s\n", un.sun_path, strerror(errno));
502 goto err;
503 }
504 if (listen(sock, 1) < 0) {
505 fprintf(stderr, "listen(unix:%s): %s\n", un.sun_path, strerror(errno));
506 goto err;
507 }
508
aliguorid247d252008-11-11 20:46:40 +0000509 return sock;
510
511err:
aliguorid247d252008-11-11 20:46:40 +0000512 closesocket(sock);
513 return -1;
514}
515
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200516int unix_connect_opts(QemuOpts *opts)
aliguorid247d252008-11-11 20:46:40 +0000517{
518 struct sockaddr_un un;
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200519 const char *path = qemu_opt_get(opts, "path");
aliguorid247d252008-11-11 20:46:40 +0000520 int sock;
521
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200522 if (NULL == path) {
523 fprintf(stderr, "unix connect: no path specified\n");
524 return -1;
525 }
526
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100527 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
aliguorid247d252008-11-11 20:46:40 +0000528 if (sock < 0) {
vibi39b6efc2009-05-06 15:27:03 +0530529 perror("socket(unix)");
530 return -1;
aliguorid247d252008-11-11 20:46:40 +0000531 }
532
533 memset(&un, 0, sizeof(un));
534 un.sun_family = AF_UNIX;
535 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
536 if (connect(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
537 fprintf(stderr, "connect(unix:%s): %s\n", path, strerror(errno));
Markus Armbruster9d947472011-11-11 10:40:07 +0100538 close(sock);
aliguorid247d252008-11-11 20:46:40 +0000539 return -1;
540 }
541
aliguorid247d252008-11-11 20:46:40 +0000542 return sock;
543}
544
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200545/* compatibility wrapper */
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200546int unix_listen(const char *str, char *ostr, int olen)
547{
548 QemuOpts *opts;
549 char *path, *optstr;
550 int sock, len;
551
552 opts = qemu_opts_create(&dummy_opts, NULL, 0);
553
554 optstr = strchr(str, ',');
555 if (optstr) {
556 len = optstr - str;
557 if (len) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500558 path = g_malloc(len+1);
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200559 snprintf(path, len+1, "%.*s", len, str);
560 qemu_opt_set(opts, "path", path);
Anthony Liguori7267c092011-08-20 22:09:37 -0500561 g_free(path);
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200562 }
563 } else {
564 qemu_opt_set(opts, "path", str);
565 }
566
567 sock = unix_listen_opts(opts);
568
569 if (sock != -1 && ostr)
570 snprintf(ostr, olen, "%s%s", qemu_opt_get(opts, "path"), optstr ? optstr : "");
571 qemu_opts_del(opts);
572 return sock;
573}
574
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200575int unix_connect(const char *path)
576{
577 QemuOpts *opts;
578 int sock;
579
580 opts = qemu_opts_create(&dummy_opts, NULL, 0);
581 qemu_opt_set(opts, "path", path);
582 sock = unix_connect_opts(opts);
583 qemu_opts_del(opts);
584 return sock;
585}
586
aliguorid247d252008-11-11 20:46:40 +0000587#else
588
Gerd Hoffmann108af7b2009-09-10 10:58:39 +0200589int unix_listen_opts(QemuOpts *opts)
590{
591 fprintf(stderr, "unix sockets are not available on windows\n");
Nick Thomasb82eac92011-02-22 15:44:52 +0000592 errno = ENOTSUP;
Gerd Hoffmann108af7b2009-09-10 10:58:39 +0200593 return -1;
594}
595
596int unix_connect_opts(QemuOpts *opts)
597{
598 fprintf(stderr, "unix sockets are not available on windows\n");
Nick Thomasb82eac92011-02-22 15:44:52 +0000599 errno = ENOTSUP;
Gerd Hoffmann108af7b2009-09-10 10:58:39 +0200600 return -1;
601}
602
aliguorid247d252008-11-11 20:46:40 +0000603int unix_listen(const char *path, char *ostr, int olen)
604{
605 fprintf(stderr, "unix sockets are not available on windows\n");
Nick Thomasb82eac92011-02-22 15:44:52 +0000606 errno = ENOTSUP;
aliguorid247d252008-11-11 20:46:40 +0000607 return -1;
608}
609
610int unix_connect(const char *path)
611{
612 fprintf(stderr, "unix sockets are not available on windows\n");
Nick Thomasb82eac92011-02-22 15:44:52 +0000613 errno = ENOTSUP;
aliguorid247d252008-11-11 20:46:40 +0000614 return -1;
615}
616
617#endif
Paolo Bonzini0706a4d2010-04-01 19:57:08 +0200618
619#ifdef _WIN32
620static void socket_cleanup(void)
621{
622 WSACleanup();
623}
624#endif
625
626int socket_init(void)
627{
628#ifdef _WIN32
629 WSADATA Data;
630 int ret, err;
631
632 ret = WSAStartup(MAKEWORD(2,2), &Data);
633 if (ret != 0) {
634 err = WSAGetLastError();
635 fprintf(stderr, "WSAStartup: %d\n", err);
636 return -1;
637 }
638 atexit(socket_cleanup);
639#endif
640 return 0;
641}