blob: 2ae715db769be449f8ee9cb0ed4924a1fb7369ae [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,
Amos Konga6ba35b2012-05-11 00:28:16 +080054 },{
55 .name = "block",
56 .type = QEMU_OPT_BOOL,
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +020057 },
58 { /* end if list */ }
59 },
60};
61
aliguorid247d252008-11-11 20:46:40 +000062static int inet_getport(struct addrinfo *e)
63{
64 struct sockaddr_in *i4;
65 struct sockaddr_in6 *i6;
66
67 switch (e->ai_family) {
68 case PF_INET6:
69 i6 = (void*)e->ai_addr;
70 return ntohs(i6->sin6_port);
71 case PF_INET:
72 i4 = (void*)e->ai_addr;
73 return ntohs(i4->sin_port);
74 default:
75 return 0;
76 }
77}
78
79static void inet_setport(struct addrinfo *e, int port)
80{
81 struct sockaddr_in *i4;
82 struct sockaddr_in6 *i6;
83
84 switch (e->ai_family) {
85 case PF_INET6:
86 i6 = (void*)e->ai_addr;
87 i6->sin6_port = htons(port);
88 break;
89 case PF_INET:
90 i4 = (void*)e->ai_addr;
91 i4->sin_port = htons(port);
92 break;
93 }
94}
95
Luiz Capitulinoc9c4b342010-01-20 11:42:38 -020096const char *inet_strfamily(int family)
aliguorid247d252008-11-11 20:46:40 +000097{
98 switch (family) {
99 case PF_INET6: return "ipv6";
100 case PF_INET: return "ipv4";
101 case PF_UNIX: return "unix";
102 }
Luiz Capitulinoc4453212010-01-20 11:42:39 -0200103 return "unknown";
aliguorid247d252008-11-11 20:46:40 +0000104}
105
Amos Kong029409e2012-05-11 00:28:26 +0800106int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)
aliguorid247d252008-11-11 20:46:40 +0000107{
108 struct addrinfo ai,*res,*e;
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200109 const char *addr;
aliguorid247d252008-11-11 20:46:40 +0000110 char port[33];
111 char uaddr[INET6_ADDRSTRLEN+1];
112 char uport[33];
Markus Armbruster877691f2012-02-07 15:09:15 +0100113 int slisten, rc, to, port_min, port_max, p;
aliguorid247d252008-11-11 20:46:40 +0000114
115 memset(&ai,0, sizeof(ai));
116 ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
117 ai.ai_family = PF_UNSPEC;
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200118 ai.ai_socktype = SOCK_STREAM;
aliguorid247d252008-11-11 20:46:40 +0000119
Jens Osterkampe23a22e2010-04-12 10:51:01 +0200120 if ((qemu_opt_get(opts, "host") == NULL) ||
121 (qemu_opt_get(opts, "port") == NULL)) {
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200122 fprintf(stderr, "%s: host and/or port not specified\n", __FUNCTION__);
Amos Kong029409e2012-05-11 00:28:26 +0800123 error_set(errp, QERR_SOCKET_CREATE_FAILED);
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200124 return -1;
aliguorid247d252008-11-11 20:46:40 +0000125 }
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200126 pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port"));
127 addr = qemu_opt_get(opts, "host");
aliguorid247d252008-11-11 20:46:40 +0000128
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200129 to = qemu_opt_get_number(opts, "to", 0);
130 if (qemu_opt_get_bool(opts, "ipv4", 0))
aliguorid247d252008-11-11 20:46:40 +0000131 ai.ai_family = PF_INET;
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200132 if (qemu_opt_get_bool(opts, "ipv6", 0))
aliguorid247d252008-11-11 20:46:40 +0000133 ai.ai_family = PF_INET6;
134
135 /* lookup */
136 if (port_offset)
137 snprintf(port, sizeof(port), "%d", atoi(port) + port_offset);
138 rc = getaddrinfo(strlen(addr) ? addr : NULL, port, &ai, &res);
139 if (rc != 0) {
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200140 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
141 gai_strerror(rc));
Amos Kong029409e2012-05-11 00:28:26 +0800142 error_set(errp, QERR_SOCKET_CREATE_FAILED);
aliguorid247d252008-11-11 20:46:40 +0000143 return -1;
144 }
aliguorid247d252008-11-11 20:46:40 +0000145
146 /* create socket + bind */
147 for (e = res; e != NULL; e = e->ai_next) {
vibi39b6efc2009-05-06 15:27:03 +0530148 getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
149 uaddr,INET6_ADDRSTRLEN,uport,32,
150 NI_NUMERICHOST | NI_NUMERICSERV);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100151 slisten = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol);
vibi39b6efc2009-05-06 15:27:03 +0530152 if (slisten < 0) {
aliguorid247d252008-11-11 20:46:40 +0000153 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
154 inet_strfamily(e->ai_family), strerror(errno));
Amos Kong029409e2012-05-11 00:28:26 +0800155 if (!e->ai_next) {
156 error_set(errp, QERR_SOCKET_CREATE_FAILED);
157 }
vibi39b6efc2009-05-06 15:27:03 +0530158 continue;
159 }
aliguorid247d252008-11-11 20:46:40 +0000160
161 setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
162#ifdef IPV6_V6ONLY
163 if (e->ai_family == PF_INET6) {
164 /* listen on both ipv4 and ipv6 */
vibi39b6efc2009-05-06 15:27:03 +0530165 setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,
166 sizeof(off));
aliguorid247d252008-11-11 20:46:40 +0000167 }
168#endif
169
Markus Armbruster877691f2012-02-07 15:09:15 +0100170 port_min = inet_getport(e);
171 port_max = to ? to + port_offset : port_min;
172 for (p = port_min; p <= port_max; p++) {
173 inet_setport(e, p);
aliguorid247d252008-11-11 20:46:40 +0000174 if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {
aliguorid247d252008-11-11 20:46:40 +0000175 goto listen;
176 }
Markus Armbruster877691f2012-02-07 15:09:15 +0100177 if (p == port_max) {
aliguorid247d252008-11-11 20:46:40 +0000178 fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__,
179 inet_strfamily(e->ai_family), uaddr, inet_getport(e),
180 strerror(errno));
Amos Kong029409e2012-05-11 00:28:26 +0800181 if (!e->ai_next) {
182 error_set(errp, QERR_SOCKET_BIND_FAILED);
183 }
aliguorid247d252008-11-11 20:46:40 +0000184 }
aliguorid247d252008-11-11 20:46:40 +0000185 }
186 closesocket(slisten);
187 }
188 fprintf(stderr, "%s: FAILED\n", __FUNCTION__);
189 freeaddrinfo(res);
190 return -1;
191
192listen:
193 if (listen(slisten,1) != 0) {
Amos Kong029409e2012-05-11 00:28:26 +0800194 error_set(errp, QERR_SOCKET_LISTEN_FAILED);
aliguorid247d252008-11-11 20:46:40 +0000195 perror("listen");
196 closesocket(slisten);
vibi39b6efc2009-05-06 15:27:03 +0530197 freeaddrinfo(res);
aliguorid247d252008-11-11 20:46:40 +0000198 return -1;
199 }
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200200 snprintf(uport, sizeof(uport), "%d", inet_getport(e) - port_offset);
201 qemu_opt_set(opts, "host", uaddr);
202 qemu_opt_set(opts, "port", uport);
203 qemu_opt_set(opts, "ipv6", (e->ai_family == PF_INET6) ? "on" : "off");
204 qemu_opt_set(opts, "ipv4", (e->ai_family != PF_INET6) ? "on" : "off");
aliguorid247d252008-11-11 20:46:40 +0000205 freeaddrinfo(res);
206 return slisten;
207}
208
Amos Konga6ba35b2012-05-11 00:28:16 +0800209int inet_connect_opts(QemuOpts *opts, Error **errp)
aliguorid247d252008-11-11 20:46:40 +0000210{
211 struct addrinfo ai,*res,*e;
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200212 const char *addr;
213 const char *port;
aliguorid247d252008-11-11 20:46:40 +0000214 char uaddr[INET6_ADDRSTRLEN+1];
215 char uport[33];
216 int sock,rc;
Amos Konga6ba35b2012-05-11 00:28:16 +0800217 bool block;
aliguorid247d252008-11-11 20:46:40 +0000218
219 memset(&ai,0, sizeof(ai));
220 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
221 ai.ai_family = PF_UNSPEC;
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200222 ai.ai_socktype = SOCK_STREAM;
aliguorid247d252008-11-11 20:46:40 +0000223
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200224 addr = qemu_opt_get(opts, "host");
225 port = qemu_opt_get(opts, "port");
Amos Konga6ba35b2012-05-11 00:28:16 +0800226 block = qemu_opt_get_bool(opts, "block", 0);
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200227 if (addr == NULL || port == NULL) {
228 fprintf(stderr, "inet_connect: host and/or port not specified\n");
Amos Konga6ba35b2012-05-11 00:28:16 +0800229 error_set(errp, QERR_SOCKET_CREATE_FAILED);
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200230 return -1;
aliguorid247d252008-11-11 20:46:40 +0000231 }
232
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200233 if (qemu_opt_get_bool(opts, "ipv4", 0))
aliguorid247d252008-11-11 20:46:40 +0000234 ai.ai_family = PF_INET;
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200235 if (qemu_opt_get_bool(opts, "ipv6", 0))
aliguorid247d252008-11-11 20:46:40 +0000236 ai.ai_family = PF_INET6;
237
238 /* lookup */
239 if (0 != (rc = getaddrinfo(addr, port, &ai, &res))) {
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200240 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
241 gai_strerror(rc));
Amos Konga6ba35b2012-05-11 00:28:16 +0800242 error_set(errp, QERR_SOCKET_CREATE_FAILED);
aliguorid247d252008-11-11 20:46:40 +0000243 return -1;
244 }
aliguorid247d252008-11-11 20:46:40 +0000245
246 for (e = res; e != NULL; e = e->ai_next) {
vibi39b6efc2009-05-06 15:27:03 +0530247 if (getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
248 uaddr,INET6_ADDRSTRLEN,uport,32,
249 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
aliguorid247d252008-11-11 20:46:40 +0000250 fprintf(stderr,"%s: getnameinfo: oops\n", __FUNCTION__);
vibi39b6efc2009-05-06 15:27:03 +0530251 continue;
252 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100253 sock = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol);
vibi39b6efc2009-05-06 15:27:03 +0530254 if (sock < 0) {
aliguorid247d252008-11-11 20:46:40 +0000255 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
vibi39b6efc2009-05-06 15:27:03 +0530256 inet_strfamily(e->ai_family), strerror(errno));
257 continue;
258 }
aliguorid247d252008-11-11 20:46:40 +0000259 setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
Amos Konga6ba35b2012-05-11 00:28:16 +0800260 if (!block) {
261 socket_set_nonblock(sock);
262 }
vibi39b6efc2009-05-06 15:27:03 +0530263 /* connect to peer */
Amos Konga6ba35b2012-05-11 00:28:16 +0800264 do {
265 rc = 0;
266 if (connect(sock, e->ai_addr, e->ai_addrlen) < 0) {
267 rc = -socket_error();
268 }
269 } while (rc == -EINTR);
270
271 #ifdef _WIN32
272 if (!block && (rc == -EINPROGRESS || rc == -EWOULDBLOCK
273 || rc == -WSAEALREADY)) {
274 #else
275 if (!block && (rc == -EINPROGRESS)) {
276 #endif
277 error_set(errp, QERR_SOCKET_CONNECT_IN_PROGRESS);
278 } else if (rc < 0) {
Markus Armbruster136faa32012-02-07 15:09:14 +0100279 if (NULL == e->ai_next)
aliguorid247d252008-11-11 20:46:40 +0000280 fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
281 inet_strfamily(e->ai_family),
282 e->ai_canonname, uaddr, uport, strerror(errno));
283 closesocket(sock);
Amos Konga6ba35b2012-05-11 00:28:16 +0800284 sock = -1;
vibi39b6efc2009-05-06 15:27:03 +0530285 continue;
286 }
aliguorid247d252008-11-11 20:46:40 +0000287 freeaddrinfo(res);
vibi39b6efc2009-05-06 15:27:03 +0530288 return sock;
aliguorid247d252008-11-11 20:46:40 +0000289 }
Amos Konga6ba35b2012-05-11 00:28:16 +0800290 error_set(errp, QERR_SOCKET_CONNECT_FAILED);
aliguorid247d252008-11-11 20:46:40 +0000291 freeaddrinfo(res);
292 return -1;
293}
294
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200295int inet_dgram_opts(QemuOpts *opts)
296{
297 struct addrinfo ai, *peer = NULL, *local = NULL;
298 const char *addr;
299 const char *port;
300 char uaddr[INET6_ADDRSTRLEN+1];
301 char uport[33];
302 int sock = -1, rc;
303
304 /* lookup peer addr */
305 memset(&ai,0, sizeof(ai));
306 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
307 ai.ai_family = PF_UNSPEC;
308 ai.ai_socktype = SOCK_DGRAM;
309
310 addr = qemu_opt_get(opts, "host");
311 port = qemu_opt_get(opts, "port");
312 if (addr == NULL || strlen(addr) == 0) {
313 addr = "localhost";
314 }
315 if (port == NULL || strlen(port) == 0) {
316 fprintf(stderr, "inet_dgram: port not specified\n");
317 return -1;
318 }
319
320 if (qemu_opt_get_bool(opts, "ipv4", 0))
321 ai.ai_family = PF_INET;
322 if (qemu_opt_get_bool(opts, "ipv6", 0))
323 ai.ai_family = PF_INET6;
324
325 if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) {
326 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
327 gai_strerror(rc));
328 return -1;
329 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200330
331 /* lookup local addr */
332 memset(&ai,0, sizeof(ai));
333 ai.ai_flags = AI_PASSIVE;
334 ai.ai_family = peer->ai_family;
335 ai.ai_socktype = SOCK_DGRAM;
336
337 addr = qemu_opt_get(opts, "localaddr");
338 port = qemu_opt_get(opts, "localport");
339 if (addr == NULL || strlen(addr) == 0) {
340 addr = NULL;
341 }
342 if (!port || strlen(port) == 0)
343 port = "0";
344
345 if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) {
346 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
347 gai_strerror(rc));
348 return -1;
349 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200350
351 /* create socket */
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100352 sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200353 if (sock < 0) {
354 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
355 inet_strfamily(peer->ai_family), strerror(errno));
356 goto err;
357 }
358 setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
359
360 /* bind socket */
361 if (getnameinfo((struct sockaddr*)local->ai_addr,local->ai_addrlen,
362 uaddr,INET6_ADDRSTRLEN,uport,32,
363 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
364 fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
365 goto err;
366 }
367 if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) {
368 fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,
369 inet_strfamily(local->ai_family), uaddr, inet_getport(local));
370 goto err;
371 }
372
373 /* connect to peer */
374 if (getnameinfo((struct sockaddr*)peer->ai_addr, peer->ai_addrlen,
375 uaddr, INET6_ADDRSTRLEN, uport, 32,
376 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
377 fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
378 goto err;
379 }
380 if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) {
381 fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
382 inet_strfamily(peer->ai_family),
383 peer->ai_canonname, uaddr, uport, strerror(errno));
384 goto err;
385 }
386
387 freeaddrinfo(local);
388 freeaddrinfo(peer);
389 return sock;
390
391err:
392 if (-1 != sock)
393 closesocket(sock);
394 if (local)
395 freeaddrinfo(local);
396 if (peer)
397 freeaddrinfo(peer);
398 return -1;
399}
400
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200401/* compatibility wrapper */
402static int inet_parse(QemuOpts *opts, const char *str)
403{
404 const char *optstr, *h;
405 char addr[64];
406 char port[33];
407 int pos;
408
409 /* parse address */
410 if (str[0] == ':') {
411 /* no host given */
412 addr[0] = '\0';
413 if (1 != sscanf(str,":%32[^,]%n",port,&pos)) {
414 fprintf(stderr, "%s: portonly parse error (%s)\n",
415 __FUNCTION__, str);
416 return -1;
417 }
418 } else if (str[0] == '[') {
419 /* IPv6 addr */
420 if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) {
421 fprintf(stderr, "%s: ipv6 parse error (%s)\n",
422 __FUNCTION__, str);
423 return -1;
424 }
Marcelo Tosatti2198a622010-02-09 15:31:46 -0200425 qemu_opt_set(opts, "ipv6", "on");
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200426 } else if (qemu_isdigit(str[0])) {
427 /* IPv4 addr */
428 if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) {
429 fprintf(stderr, "%s: ipv4 parse error (%s)\n",
430 __FUNCTION__, str);
431 return -1;
432 }
Marcelo Tosatti2198a622010-02-09 15:31:46 -0200433 qemu_opt_set(opts, "ipv4", "on");
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200434 } else {
435 /* hostname */
436 if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) {
437 fprintf(stderr, "%s: hostname parse error (%s)\n",
438 __FUNCTION__, str);
439 return -1;
440 }
441 }
442 qemu_opt_set(opts, "host", addr);
443 qemu_opt_set(opts, "port", port);
444
445 /* parse options */
446 optstr = str + pos;
447 h = strstr(optstr, ",to=");
448 if (h)
449 qemu_opt_set(opts, "to", h+4);
450 if (strstr(optstr, ",ipv4"))
Marcelo Tosatti2198a622010-02-09 15:31:46 -0200451 qemu_opt_set(opts, "ipv4", "on");
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200452 if (strstr(optstr, ",ipv6"))
Marcelo Tosatti2198a622010-02-09 15:31:46 -0200453 qemu_opt_set(opts, "ipv6", "on");
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200454 return 0;
455}
456
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200457int inet_listen(const char *str, char *ostr, int olen,
Amos Kong029409e2012-05-11 00:28:26 +0800458 int socktype, int port_offset, Error **errp)
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200459{
460 QemuOpts *opts;
461 char *optstr;
462 int sock = -1;
463
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300464 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200465 if (inet_parse(opts, str) == 0) {
Amos Kong029409e2012-05-11 00:28:26 +0800466 sock = inet_listen_opts(opts, port_offset, errp);
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200467 if (sock != -1 && ostr) {
468 optstr = strchr(str, ',');
469 if (qemu_opt_get_bool(opts, "ipv6", 0)) {
470 snprintf(ostr, olen, "[%s]:%s%s",
471 qemu_opt_get(opts, "host"),
472 qemu_opt_get(opts, "port"),
473 optstr ? optstr : "");
474 } else {
475 snprintf(ostr, olen, "%s:%s%s",
476 qemu_opt_get(opts, "host"),
477 qemu_opt_get(opts, "port"),
478 optstr ? optstr : "");
479 }
480 }
Amos Kong029409e2012-05-11 00:28:26 +0800481 } else {
482 error_set(errp, QERR_SOCKET_CREATE_FAILED);
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200483 }
484 qemu_opts_del(opts);
485 return sock;
486}
487
Amos Konga6ba35b2012-05-11 00:28:16 +0800488int inet_connect(const char *str, bool block, Error **errp)
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200489{
490 QemuOpts *opts;
491 int sock = -1;
492
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300493 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
Amos Konga6ba35b2012-05-11 00:28:16 +0800494 if (inet_parse(opts, str) == 0) {
495 if (block) {
496 qemu_opt_set(opts, "block", "on");
497 }
498 sock = inet_connect_opts(opts, errp);
499 } else {
500 error_set(errp, QERR_SOCKET_CREATE_FAILED);
501 }
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200502 qemu_opts_del(opts);
503 return sock;
504}
505
aliguorid247d252008-11-11 20:46:40 +0000506#ifndef _WIN32
507
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200508int unix_listen_opts(QemuOpts *opts)
aliguorid247d252008-11-11 20:46:40 +0000509{
510 struct sockaddr_un un;
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200511 const char *path = qemu_opt_get(opts, "path");
512 int sock, fd;
aliguorid247d252008-11-11 20:46:40 +0000513
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100514 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
aliguorid247d252008-11-11 20:46:40 +0000515 if (sock < 0) {
vibi39b6efc2009-05-06 15:27:03 +0530516 perror("socket(unix)");
517 return -1;
aliguorid247d252008-11-11 20:46:40 +0000518 }
519
aliguorid247d252008-11-11 20:46:40 +0000520 memset(&un, 0, sizeof(un));
521 un.sun_family = AF_UNIX;
522 if (path && strlen(path)) {
523 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
524 } else {
525 char *tmpdir = getenv("TMPDIR");
526 snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX",
527 tmpdir ? tmpdir : "/tmp");
528 /*
529 * This dummy fd usage silences the mktemp() unsecure warning.
530 * Using mkstemp() doesn't make things more secure here
531 * though. bind() complains about existing files, so we have
532 * to unlink first and thus re-open the race window. The
533 * worst case possible is bind() failing, i.e. a DoS attack.
534 */
535 fd = mkstemp(un.sun_path); close(fd);
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200536 qemu_opt_set(opts, "path", un.sun_path);
aliguorid247d252008-11-11 20:46:40 +0000537 }
aliguorid247d252008-11-11 20:46:40 +0000538
539 unlink(un.sun_path);
540 if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
541 fprintf(stderr, "bind(unix:%s): %s\n", un.sun_path, strerror(errno));
542 goto err;
543 }
544 if (listen(sock, 1) < 0) {
545 fprintf(stderr, "listen(unix:%s): %s\n", un.sun_path, strerror(errno));
546 goto err;
547 }
548
aliguorid247d252008-11-11 20:46:40 +0000549 return sock;
550
551err:
aliguorid247d252008-11-11 20:46:40 +0000552 closesocket(sock);
553 return -1;
554}
555
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200556int unix_connect_opts(QemuOpts *opts)
aliguorid247d252008-11-11 20:46:40 +0000557{
558 struct sockaddr_un un;
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200559 const char *path = qemu_opt_get(opts, "path");
aliguorid247d252008-11-11 20:46:40 +0000560 int sock;
561
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200562 if (NULL == path) {
563 fprintf(stderr, "unix connect: no path specified\n");
564 return -1;
565 }
566
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100567 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
aliguorid247d252008-11-11 20:46:40 +0000568 if (sock < 0) {
vibi39b6efc2009-05-06 15:27:03 +0530569 perror("socket(unix)");
570 return -1;
aliguorid247d252008-11-11 20:46:40 +0000571 }
572
573 memset(&un, 0, sizeof(un));
574 un.sun_family = AF_UNIX;
575 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
576 if (connect(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
577 fprintf(stderr, "connect(unix:%s): %s\n", path, strerror(errno));
Markus Armbruster9d947472011-11-11 10:40:07 +0100578 close(sock);
aliguorid247d252008-11-11 20:46:40 +0000579 return -1;
580 }
581
aliguorid247d252008-11-11 20:46:40 +0000582 return sock;
583}
584
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200585/* compatibility wrapper */
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200586int unix_listen(const char *str, char *ostr, int olen)
587{
588 QemuOpts *opts;
589 char *path, *optstr;
590 int sock, len;
591
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300592 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200593
594 optstr = strchr(str, ',');
595 if (optstr) {
596 len = optstr - str;
597 if (len) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500598 path = g_malloc(len+1);
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200599 snprintf(path, len+1, "%.*s", len, str);
600 qemu_opt_set(opts, "path", path);
Anthony Liguori7267c092011-08-20 22:09:37 -0500601 g_free(path);
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200602 }
603 } else {
604 qemu_opt_set(opts, "path", str);
605 }
606
607 sock = unix_listen_opts(opts);
608
609 if (sock != -1 && ostr)
610 snprintf(ostr, olen, "%s%s", qemu_opt_get(opts, "path"), optstr ? optstr : "");
611 qemu_opts_del(opts);
612 return sock;
613}
614
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200615int unix_connect(const char *path)
616{
617 QemuOpts *opts;
618 int sock;
619
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300620 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200621 qemu_opt_set(opts, "path", path);
622 sock = unix_connect_opts(opts);
623 qemu_opts_del(opts);
624 return sock;
625}
626
aliguorid247d252008-11-11 20:46:40 +0000627#else
628
Gerd Hoffmann108af7b2009-09-10 10:58:39 +0200629int unix_listen_opts(QemuOpts *opts)
630{
631 fprintf(stderr, "unix sockets are not available on windows\n");
Nick Thomasb82eac92011-02-22 15:44:52 +0000632 errno = ENOTSUP;
Gerd Hoffmann108af7b2009-09-10 10:58:39 +0200633 return -1;
634}
635
636int unix_connect_opts(QemuOpts *opts)
637{
638 fprintf(stderr, "unix sockets are not available on windows\n");
Nick Thomasb82eac92011-02-22 15:44:52 +0000639 errno = ENOTSUP;
Gerd Hoffmann108af7b2009-09-10 10:58:39 +0200640 return -1;
641}
642
aliguorid247d252008-11-11 20:46:40 +0000643int unix_listen(const char *path, char *ostr, int olen)
644{
645 fprintf(stderr, "unix sockets are not available on windows\n");
Nick Thomasb82eac92011-02-22 15:44:52 +0000646 errno = ENOTSUP;
aliguorid247d252008-11-11 20:46:40 +0000647 return -1;
648}
649
650int unix_connect(const char *path)
651{
652 fprintf(stderr, "unix sockets are not available on windows\n");
Nick Thomasb82eac92011-02-22 15:44:52 +0000653 errno = ENOTSUP;
aliguorid247d252008-11-11 20:46:40 +0000654 return -1;
655}
656
657#endif
Paolo Bonzini0706a4d2010-04-01 19:57:08 +0200658
659#ifdef _WIN32
660static void socket_cleanup(void)
661{
662 WSACleanup();
663}
664#endif
665
666int socket_init(void)
667{
668#ifdef _WIN32
669 WSADATA Data;
670 int ret, err;
671
672 ret = WSAStartup(MAKEWORD(2,2), &Data);
673 if (ret != 0) {
674 err = WSAGetLastError();
675 fprintf(stderr, "WSAStartup: %d\n", err);
676 return -1;
677 }
678 atexit(socket_cleanup);
679#endif
680 return 0;
681}