blob: 9cb47d4232759940c78cabb629e7edcb339d7fc4 [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.
Paolo Bonzinie6d91ab2012-07-09 11:08:39 +020014 *
15 * Contributions after 2012-01-13 are licensed under the terms of the
16 * GNU GPL, version 2 or (at your option) any later version.
aliguori305b0eb2008-11-13 16:19:54 +000017 */
aliguorid247d252008-11-11 20:46:40 +000018#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <ctype.h>
22#include <errno.h>
23#include <unistd.h>
24
25#include "qemu_socket.h"
blueswir147398b92008-11-22 20:04:24 +000026#include "qemu-common.h" /* for qemu_isdigit */
aliguorid247d252008-11-11 20:46:40 +000027
28#ifndef AI_ADDRCONFIG
29# define AI_ADDRCONFIG 0
30#endif
31
aliguorid247d252008-11-11 20:46:40 +000032static const int on=1, off=0;
33
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +020034/* used temporarely until all users are converted to QemuOpts */
Blue Swirlc1390902009-09-12 09:58:51 +000035static QemuOptsList dummy_opts = {
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +020036 .name = "dummy",
Blue Swirl72cf2d42009-09-12 07:36:22 +000037 .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head),
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +020038 .desc = {
39 {
40 .name = "path",
41 .type = QEMU_OPT_STRING,
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +020042 },{
43 .name = "host",
44 .type = QEMU_OPT_STRING,
45 },{
46 .name = "port",
47 .type = QEMU_OPT_STRING,
48 },{
49 .name = "to",
50 .type = QEMU_OPT_NUMBER,
51 },{
52 .name = "ipv4",
53 .type = QEMU_OPT_BOOL,
54 },{
55 .name = "ipv6",
56 .type = QEMU_OPT_BOOL,
Amos Konga6ba35b2012-05-11 00:28:16 +080057 },{
58 .name = "block",
59 .type = QEMU_OPT_BOOL,
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +020060 },
61 { /* end if list */ }
62 },
63};
64
aliguorid247d252008-11-11 20:46:40 +000065static int inet_getport(struct addrinfo *e)
66{
67 struct sockaddr_in *i4;
68 struct sockaddr_in6 *i6;
69
70 switch (e->ai_family) {
71 case PF_INET6:
72 i6 = (void*)e->ai_addr;
73 return ntohs(i6->sin6_port);
74 case PF_INET:
75 i4 = (void*)e->ai_addr;
76 return ntohs(i4->sin_port);
77 default:
78 return 0;
79 }
80}
81
82static void inet_setport(struct addrinfo *e, int port)
83{
84 struct sockaddr_in *i4;
85 struct sockaddr_in6 *i6;
86
87 switch (e->ai_family) {
88 case PF_INET6:
89 i6 = (void*)e->ai_addr;
90 i6->sin6_port = htons(port);
91 break;
92 case PF_INET:
93 i4 = (void*)e->ai_addr;
94 i4->sin_port = htons(port);
95 break;
96 }
97}
98
Luiz Capitulinoc9c4b342010-01-20 11:42:38 -020099const char *inet_strfamily(int family)
aliguorid247d252008-11-11 20:46:40 +0000100{
101 switch (family) {
102 case PF_INET6: return "ipv6";
103 case PF_INET: return "ipv4";
104 case PF_UNIX: return "unix";
105 }
Luiz Capitulinoc4453212010-01-20 11:42:39 -0200106 return "unknown";
aliguorid247d252008-11-11 20:46:40 +0000107}
108
Amos Kong029409e2012-05-11 00:28:26 +0800109int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)
aliguorid247d252008-11-11 20:46:40 +0000110{
111 struct addrinfo ai,*res,*e;
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200112 const char *addr;
aliguorid247d252008-11-11 20:46:40 +0000113 char port[33];
114 char uaddr[INET6_ADDRSTRLEN+1];
115 char uport[33];
Markus Armbruster877691f2012-02-07 15:09:15 +0100116 int slisten, rc, to, port_min, port_max, p;
aliguorid247d252008-11-11 20:46:40 +0000117
118 memset(&ai,0, sizeof(ai));
119 ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
120 ai.ai_family = PF_UNSPEC;
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200121 ai.ai_socktype = SOCK_STREAM;
aliguorid247d252008-11-11 20:46:40 +0000122
Jens Osterkampe23a22e2010-04-12 10:51:01 +0200123 if ((qemu_opt_get(opts, "host") == NULL) ||
124 (qemu_opt_get(opts, "port") == NULL)) {
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200125 fprintf(stderr, "%s: host and/or port not specified\n", __FUNCTION__);
Amos Kong029409e2012-05-11 00:28:26 +0800126 error_set(errp, QERR_SOCKET_CREATE_FAILED);
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200127 return -1;
aliguorid247d252008-11-11 20:46:40 +0000128 }
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200129 pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port"));
130 addr = qemu_opt_get(opts, "host");
aliguorid247d252008-11-11 20:46:40 +0000131
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200132 to = qemu_opt_get_number(opts, "to", 0);
133 if (qemu_opt_get_bool(opts, "ipv4", 0))
aliguorid247d252008-11-11 20:46:40 +0000134 ai.ai_family = PF_INET;
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200135 if (qemu_opt_get_bool(opts, "ipv6", 0))
aliguorid247d252008-11-11 20:46:40 +0000136 ai.ai_family = PF_INET6;
137
138 /* lookup */
139 if (port_offset)
140 snprintf(port, sizeof(port), "%d", atoi(port) + port_offset);
141 rc = getaddrinfo(strlen(addr) ? addr : NULL, port, &ai, &res);
142 if (rc != 0) {
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200143 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
144 gai_strerror(rc));
Amos Kong029409e2012-05-11 00:28:26 +0800145 error_set(errp, QERR_SOCKET_CREATE_FAILED);
aliguorid247d252008-11-11 20:46:40 +0000146 return -1;
147 }
aliguorid247d252008-11-11 20:46:40 +0000148
149 /* create socket + bind */
150 for (e = res; e != NULL; e = e->ai_next) {
vibi39b6efc2009-05-06 15:27:03 +0530151 getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
152 uaddr,INET6_ADDRSTRLEN,uport,32,
153 NI_NUMERICHOST | NI_NUMERICSERV);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100154 slisten = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol);
vibi39b6efc2009-05-06 15:27:03 +0530155 if (slisten < 0) {
aliguorid247d252008-11-11 20:46:40 +0000156 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
157 inet_strfamily(e->ai_family), strerror(errno));
Amos Kong029409e2012-05-11 00:28:26 +0800158 if (!e->ai_next) {
159 error_set(errp, QERR_SOCKET_CREATE_FAILED);
160 }
vibi39b6efc2009-05-06 15:27:03 +0530161 continue;
162 }
aliguorid247d252008-11-11 20:46:40 +0000163
164 setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
165#ifdef IPV6_V6ONLY
166 if (e->ai_family == PF_INET6) {
167 /* listen on both ipv4 and ipv6 */
vibi39b6efc2009-05-06 15:27:03 +0530168 setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,
169 sizeof(off));
aliguorid247d252008-11-11 20:46:40 +0000170 }
171#endif
172
Markus Armbruster877691f2012-02-07 15:09:15 +0100173 port_min = inet_getport(e);
174 port_max = to ? to + port_offset : port_min;
175 for (p = port_min; p <= port_max; p++) {
176 inet_setport(e, p);
aliguorid247d252008-11-11 20:46:40 +0000177 if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {
aliguorid247d252008-11-11 20:46:40 +0000178 goto listen;
179 }
Markus Armbruster877691f2012-02-07 15:09:15 +0100180 if (p == port_max) {
aliguorid247d252008-11-11 20:46:40 +0000181 fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__,
182 inet_strfamily(e->ai_family), uaddr, inet_getport(e),
183 strerror(errno));
Amos Kong029409e2012-05-11 00:28:26 +0800184 if (!e->ai_next) {
185 error_set(errp, QERR_SOCKET_BIND_FAILED);
186 }
aliguorid247d252008-11-11 20:46:40 +0000187 }
aliguorid247d252008-11-11 20:46:40 +0000188 }
189 closesocket(slisten);
190 }
191 fprintf(stderr, "%s: FAILED\n", __FUNCTION__);
192 freeaddrinfo(res);
193 return -1;
194
195listen:
196 if (listen(slisten,1) != 0) {
Amos Kong029409e2012-05-11 00:28:26 +0800197 error_set(errp, QERR_SOCKET_LISTEN_FAILED);
aliguorid247d252008-11-11 20:46:40 +0000198 perror("listen");
199 closesocket(slisten);
vibi39b6efc2009-05-06 15:27:03 +0530200 freeaddrinfo(res);
aliguorid247d252008-11-11 20:46:40 +0000201 return -1;
202 }
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200203 snprintf(uport, sizeof(uport), "%d", inet_getport(e) - port_offset);
204 qemu_opt_set(opts, "host", uaddr);
205 qemu_opt_set(opts, "port", uport);
206 qemu_opt_set(opts, "ipv6", (e->ai_family == PF_INET6) ? "on" : "off");
207 qemu_opt_set(opts, "ipv4", (e->ai_family != PF_INET6) ? "on" : "off");
aliguorid247d252008-11-11 20:46:40 +0000208 freeaddrinfo(res);
209 return slisten;
210}
211
Luiz Capitulino02a08fe2012-08-01 13:42:47 -0300212int inet_connect_opts(QemuOpts *opts, bool *in_progress, Error **errp)
aliguorid247d252008-11-11 20:46:40 +0000213{
214 struct addrinfo ai,*res,*e;
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200215 const char *addr;
216 const char *port;
aliguorid247d252008-11-11 20:46:40 +0000217 char uaddr[INET6_ADDRSTRLEN+1];
218 char uport[33];
219 int sock,rc;
Amos Konga6ba35b2012-05-11 00:28:16 +0800220 bool block;
aliguorid247d252008-11-11 20:46:40 +0000221
222 memset(&ai,0, sizeof(ai));
223 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
224 ai.ai_family = PF_UNSPEC;
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200225 ai.ai_socktype = SOCK_STREAM;
aliguorid247d252008-11-11 20:46:40 +0000226
Luiz Capitulino02a08fe2012-08-01 13:42:47 -0300227 if (in_progress) {
228 *in_progress = false;
229 }
230
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200231 addr = qemu_opt_get(opts, "host");
232 port = qemu_opt_get(opts, "port");
Amos Konga6ba35b2012-05-11 00:28:16 +0800233 block = qemu_opt_get_bool(opts, "block", 0);
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200234 if (addr == NULL || port == NULL) {
235 fprintf(stderr, "inet_connect: host and/or port not specified\n");
Amos Konga6ba35b2012-05-11 00:28:16 +0800236 error_set(errp, QERR_SOCKET_CREATE_FAILED);
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200237 return -1;
aliguorid247d252008-11-11 20:46:40 +0000238 }
239
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200240 if (qemu_opt_get_bool(opts, "ipv4", 0))
aliguorid247d252008-11-11 20:46:40 +0000241 ai.ai_family = PF_INET;
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200242 if (qemu_opt_get_bool(opts, "ipv6", 0))
aliguorid247d252008-11-11 20:46:40 +0000243 ai.ai_family = PF_INET6;
244
245 /* lookup */
246 if (0 != (rc = getaddrinfo(addr, port, &ai, &res))) {
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200247 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
248 gai_strerror(rc));
Amos Konga6ba35b2012-05-11 00:28:16 +0800249 error_set(errp, QERR_SOCKET_CREATE_FAILED);
aliguorid247d252008-11-11 20:46:40 +0000250 return -1;
251 }
aliguorid247d252008-11-11 20:46:40 +0000252
253 for (e = res; e != NULL; e = e->ai_next) {
vibi39b6efc2009-05-06 15:27:03 +0530254 if (getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
255 uaddr,INET6_ADDRSTRLEN,uport,32,
256 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
aliguorid247d252008-11-11 20:46:40 +0000257 fprintf(stderr,"%s: getnameinfo: oops\n", __FUNCTION__);
vibi39b6efc2009-05-06 15:27:03 +0530258 continue;
259 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100260 sock = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol);
vibi39b6efc2009-05-06 15:27:03 +0530261 if (sock < 0) {
aliguorid247d252008-11-11 20:46:40 +0000262 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
vibi39b6efc2009-05-06 15:27:03 +0530263 inet_strfamily(e->ai_family), strerror(errno));
264 continue;
265 }
aliguorid247d252008-11-11 20:46:40 +0000266 setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
Amos Konga6ba35b2012-05-11 00:28:16 +0800267 if (!block) {
268 socket_set_nonblock(sock);
269 }
vibi39b6efc2009-05-06 15:27:03 +0530270 /* connect to peer */
Amos Konga6ba35b2012-05-11 00:28:16 +0800271 do {
272 rc = 0;
273 if (connect(sock, e->ai_addr, e->ai_addrlen) < 0) {
274 rc = -socket_error();
275 }
276 } while (rc == -EINTR);
277
278 #ifdef _WIN32
279 if (!block && (rc == -EINPROGRESS || rc == -EWOULDBLOCK
280 || rc == -WSAEALREADY)) {
281 #else
282 if (!block && (rc == -EINPROGRESS)) {
283 #endif
Luiz Capitulino02a08fe2012-08-01 13:42:47 -0300284 if (in_progress) {
285 *in_progress = true;
286 }
287
Amos Konga6ba35b2012-05-11 00:28:16 +0800288 error_set(errp, QERR_SOCKET_CONNECT_IN_PROGRESS);
289 } else if (rc < 0) {
Markus Armbruster136faa32012-02-07 15:09:14 +0100290 if (NULL == e->ai_next)
aliguorid247d252008-11-11 20:46:40 +0000291 fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
292 inet_strfamily(e->ai_family),
293 e->ai_canonname, uaddr, uport, strerror(errno));
294 closesocket(sock);
vibi39b6efc2009-05-06 15:27:03 +0530295 continue;
296 }
aliguorid247d252008-11-11 20:46:40 +0000297 freeaddrinfo(res);
vibi39b6efc2009-05-06 15:27:03 +0530298 return sock;
aliguorid247d252008-11-11 20:46:40 +0000299 }
Amos Konga6ba35b2012-05-11 00:28:16 +0800300 error_set(errp, QERR_SOCKET_CONNECT_FAILED);
aliguorid247d252008-11-11 20:46:40 +0000301 freeaddrinfo(res);
302 return -1;
303}
304
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200305int inet_dgram_opts(QemuOpts *opts)
306{
307 struct addrinfo ai, *peer = NULL, *local = NULL;
308 const char *addr;
309 const char *port;
310 char uaddr[INET6_ADDRSTRLEN+1];
311 char uport[33];
312 int sock = -1, rc;
313
314 /* lookup peer addr */
315 memset(&ai,0, sizeof(ai));
316 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
317 ai.ai_family = PF_UNSPEC;
318 ai.ai_socktype = SOCK_DGRAM;
319
320 addr = qemu_opt_get(opts, "host");
321 port = qemu_opt_get(opts, "port");
322 if (addr == NULL || strlen(addr) == 0) {
323 addr = "localhost";
324 }
325 if (port == NULL || strlen(port) == 0) {
326 fprintf(stderr, "inet_dgram: port not specified\n");
327 return -1;
328 }
329
330 if (qemu_opt_get_bool(opts, "ipv4", 0))
331 ai.ai_family = PF_INET;
332 if (qemu_opt_get_bool(opts, "ipv6", 0))
333 ai.ai_family = PF_INET6;
334
335 if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) {
336 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
337 gai_strerror(rc));
338 return -1;
339 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200340
341 /* lookup local addr */
342 memset(&ai,0, sizeof(ai));
343 ai.ai_flags = AI_PASSIVE;
344 ai.ai_family = peer->ai_family;
345 ai.ai_socktype = SOCK_DGRAM;
346
347 addr = qemu_opt_get(opts, "localaddr");
348 port = qemu_opt_get(opts, "localport");
349 if (addr == NULL || strlen(addr) == 0) {
350 addr = NULL;
351 }
352 if (!port || strlen(port) == 0)
353 port = "0";
354
355 if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) {
356 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
357 gai_strerror(rc));
358 return -1;
359 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200360
361 /* create socket */
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100362 sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200363 if (sock < 0) {
364 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
365 inet_strfamily(peer->ai_family), strerror(errno));
366 goto err;
367 }
368 setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
369
370 /* bind socket */
371 if (getnameinfo((struct sockaddr*)local->ai_addr,local->ai_addrlen,
372 uaddr,INET6_ADDRSTRLEN,uport,32,
373 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
374 fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
375 goto err;
376 }
377 if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) {
378 fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,
379 inet_strfamily(local->ai_family), uaddr, inet_getport(local));
380 goto err;
381 }
382
383 /* connect to peer */
384 if (getnameinfo((struct sockaddr*)peer->ai_addr, peer->ai_addrlen,
385 uaddr, INET6_ADDRSTRLEN, uport, 32,
386 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
387 fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
388 goto err;
389 }
390 if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) {
391 fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
392 inet_strfamily(peer->ai_family),
393 peer->ai_canonname, uaddr, uport, strerror(errno));
394 goto err;
395 }
396
397 freeaddrinfo(local);
398 freeaddrinfo(peer);
399 return sock;
400
401err:
402 if (-1 != sock)
403 closesocket(sock);
404 if (local)
405 freeaddrinfo(local);
406 if (peer)
407 freeaddrinfo(peer);
408 return -1;
409}
410
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200411/* compatibility wrapper */
412static int inet_parse(QemuOpts *opts, const char *str)
413{
414 const char *optstr, *h;
415 char addr[64];
416 char port[33];
417 int pos;
418
419 /* parse address */
420 if (str[0] == ':') {
421 /* no host given */
422 addr[0] = '\0';
423 if (1 != sscanf(str,":%32[^,]%n",port,&pos)) {
424 fprintf(stderr, "%s: portonly parse error (%s)\n",
425 __FUNCTION__, str);
426 return -1;
427 }
428 } else if (str[0] == '[') {
429 /* IPv6 addr */
430 if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) {
431 fprintf(stderr, "%s: ipv6 parse error (%s)\n",
432 __FUNCTION__, str);
433 return -1;
434 }
Marcelo Tosatti2198a622010-02-09 15:31:46 -0200435 qemu_opt_set(opts, "ipv6", "on");
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200436 } else if (qemu_isdigit(str[0])) {
437 /* IPv4 addr */
438 if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) {
439 fprintf(stderr, "%s: ipv4 parse error (%s)\n",
440 __FUNCTION__, str);
441 return -1;
442 }
Marcelo Tosatti2198a622010-02-09 15:31:46 -0200443 qemu_opt_set(opts, "ipv4", "on");
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200444 } else {
445 /* hostname */
446 if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) {
447 fprintf(stderr, "%s: hostname parse error (%s)\n",
448 __FUNCTION__, str);
449 return -1;
450 }
451 }
452 qemu_opt_set(opts, "host", addr);
453 qemu_opt_set(opts, "port", port);
454
455 /* parse options */
456 optstr = str + pos;
457 h = strstr(optstr, ",to=");
458 if (h)
459 qemu_opt_set(opts, "to", h+4);
460 if (strstr(optstr, ",ipv4"))
Marcelo Tosatti2198a622010-02-09 15:31:46 -0200461 qemu_opt_set(opts, "ipv4", "on");
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200462 if (strstr(optstr, ",ipv6"))
Marcelo Tosatti2198a622010-02-09 15:31:46 -0200463 qemu_opt_set(opts, "ipv6", "on");
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200464 return 0;
465}
466
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200467int inet_listen(const char *str, char *ostr, int olen,
Amos Kong029409e2012-05-11 00:28:26 +0800468 int socktype, int port_offset, Error **errp)
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200469{
470 QemuOpts *opts;
471 char *optstr;
472 int sock = -1;
473
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300474 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200475 if (inet_parse(opts, str) == 0) {
Amos Kong029409e2012-05-11 00:28:26 +0800476 sock = inet_listen_opts(opts, port_offset, errp);
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200477 if (sock != -1 && ostr) {
478 optstr = strchr(str, ',');
479 if (qemu_opt_get_bool(opts, "ipv6", 0)) {
480 snprintf(ostr, olen, "[%s]:%s%s",
481 qemu_opt_get(opts, "host"),
482 qemu_opt_get(opts, "port"),
483 optstr ? optstr : "");
484 } else {
485 snprintf(ostr, olen, "%s:%s%s",
486 qemu_opt_get(opts, "host"),
487 qemu_opt_get(opts, "port"),
488 optstr ? optstr : "");
489 }
490 }
Amos Kong029409e2012-05-11 00:28:26 +0800491 } else {
492 error_set(errp, QERR_SOCKET_CREATE_FAILED);
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200493 }
494 qemu_opts_del(opts);
495 return sock;
496}
497
Luiz Capitulino02a08fe2012-08-01 13:42:47 -0300498int inet_connect(const char *str, bool block, bool *in_progress, Error **errp)
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200499{
500 QemuOpts *opts;
501 int sock = -1;
502
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300503 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
Amos Konga6ba35b2012-05-11 00:28:16 +0800504 if (inet_parse(opts, str) == 0) {
505 if (block) {
506 qemu_opt_set(opts, "block", "on");
507 }
Luiz Capitulino02a08fe2012-08-01 13:42:47 -0300508 sock = inet_connect_opts(opts, in_progress, errp);
Amos Konga6ba35b2012-05-11 00:28:16 +0800509 } else {
510 error_set(errp, QERR_SOCKET_CREATE_FAILED);
511 }
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200512 qemu_opts_del(opts);
513 return sock;
514}
515
aliguorid247d252008-11-11 20:46:40 +0000516#ifndef _WIN32
517
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200518int unix_listen_opts(QemuOpts *opts)
aliguorid247d252008-11-11 20:46:40 +0000519{
520 struct sockaddr_un un;
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200521 const char *path = qemu_opt_get(opts, "path");
522 int sock, fd;
aliguorid247d252008-11-11 20:46:40 +0000523
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100524 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
aliguorid247d252008-11-11 20:46:40 +0000525 if (sock < 0) {
vibi39b6efc2009-05-06 15:27:03 +0530526 perror("socket(unix)");
527 return -1;
aliguorid247d252008-11-11 20:46:40 +0000528 }
529
aliguorid247d252008-11-11 20:46:40 +0000530 memset(&un, 0, sizeof(un));
531 un.sun_family = AF_UNIX;
532 if (path && strlen(path)) {
533 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
534 } else {
535 char *tmpdir = getenv("TMPDIR");
536 snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX",
537 tmpdir ? tmpdir : "/tmp");
538 /*
539 * This dummy fd usage silences the mktemp() unsecure warning.
540 * Using mkstemp() doesn't make things more secure here
541 * though. bind() complains about existing files, so we have
542 * to unlink first and thus re-open the race window. The
543 * worst case possible is bind() failing, i.e. a DoS attack.
544 */
545 fd = mkstemp(un.sun_path); close(fd);
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200546 qemu_opt_set(opts, "path", un.sun_path);
aliguorid247d252008-11-11 20:46:40 +0000547 }
aliguorid247d252008-11-11 20:46:40 +0000548
549 unlink(un.sun_path);
550 if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
551 fprintf(stderr, "bind(unix:%s): %s\n", un.sun_path, strerror(errno));
552 goto err;
553 }
554 if (listen(sock, 1) < 0) {
555 fprintf(stderr, "listen(unix:%s): %s\n", un.sun_path, strerror(errno));
556 goto err;
557 }
558
aliguorid247d252008-11-11 20:46:40 +0000559 return sock;
560
561err:
aliguorid247d252008-11-11 20:46:40 +0000562 closesocket(sock);
563 return -1;
564}
565
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200566int unix_connect_opts(QemuOpts *opts)
aliguorid247d252008-11-11 20:46:40 +0000567{
568 struct sockaddr_un un;
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200569 const char *path = qemu_opt_get(opts, "path");
aliguorid247d252008-11-11 20:46:40 +0000570 int sock;
571
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200572 if (NULL == path) {
573 fprintf(stderr, "unix connect: no path specified\n");
574 return -1;
575 }
576
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100577 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
aliguorid247d252008-11-11 20:46:40 +0000578 if (sock < 0) {
vibi39b6efc2009-05-06 15:27:03 +0530579 perror("socket(unix)");
580 return -1;
aliguorid247d252008-11-11 20:46:40 +0000581 }
582
583 memset(&un, 0, sizeof(un));
584 un.sun_family = AF_UNIX;
585 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
586 if (connect(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
587 fprintf(stderr, "connect(unix:%s): %s\n", path, strerror(errno));
Markus Armbruster9d947472011-11-11 10:40:07 +0100588 close(sock);
aliguorid247d252008-11-11 20:46:40 +0000589 return -1;
590 }
591
aliguorid247d252008-11-11 20:46:40 +0000592 return sock;
593}
594
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200595/* compatibility wrapper */
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200596int unix_listen(const char *str, char *ostr, int olen)
597{
598 QemuOpts *opts;
599 char *path, *optstr;
600 int sock, len;
601
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300602 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200603
604 optstr = strchr(str, ',');
605 if (optstr) {
606 len = optstr - str;
607 if (len) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500608 path = g_malloc(len+1);
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200609 snprintf(path, len+1, "%.*s", len, str);
610 qemu_opt_set(opts, "path", path);
Anthony Liguori7267c092011-08-20 22:09:37 -0500611 g_free(path);
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200612 }
613 } else {
614 qemu_opt_set(opts, "path", str);
615 }
616
617 sock = unix_listen_opts(opts);
618
619 if (sock != -1 && ostr)
620 snprintf(ostr, olen, "%s%s", qemu_opt_get(opts, "path"), optstr ? optstr : "");
621 qemu_opts_del(opts);
622 return sock;
623}
624
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200625int unix_connect(const char *path)
626{
627 QemuOpts *opts;
628 int sock;
629
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300630 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200631 qemu_opt_set(opts, "path", path);
632 sock = unix_connect_opts(opts);
633 qemu_opts_del(opts);
634 return sock;
635}
636
aliguorid247d252008-11-11 20:46:40 +0000637#else
638
Gerd Hoffmann108af7b2009-09-10 10:58:39 +0200639int unix_listen_opts(QemuOpts *opts)
640{
641 fprintf(stderr, "unix sockets are not available on windows\n");
Nick Thomasb82eac92011-02-22 15:44:52 +0000642 errno = ENOTSUP;
Gerd Hoffmann108af7b2009-09-10 10:58:39 +0200643 return -1;
644}
645
646int unix_connect_opts(QemuOpts *opts)
647{
648 fprintf(stderr, "unix sockets are not available on windows\n");
Nick Thomasb82eac92011-02-22 15:44:52 +0000649 errno = ENOTSUP;
Gerd Hoffmann108af7b2009-09-10 10:58:39 +0200650 return -1;
651}
652
aliguorid247d252008-11-11 20:46:40 +0000653int unix_listen(const char *path, char *ostr, int olen)
654{
655 fprintf(stderr, "unix sockets are not available on windows\n");
Nick Thomasb82eac92011-02-22 15:44:52 +0000656 errno = ENOTSUP;
aliguorid247d252008-11-11 20:46:40 +0000657 return -1;
658}
659
660int unix_connect(const char *path)
661{
662 fprintf(stderr, "unix sockets are not available on windows\n");
Nick Thomasb82eac92011-02-22 15:44:52 +0000663 errno = ENOTSUP;
aliguorid247d252008-11-11 20:46:40 +0000664 return -1;
665}
666
667#endif
Paolo Bonzini0706a4d2010-04-01 19:57:08 +0200668
669#ifdef _WIN32
670static void socket_cleanup(void)
671{
672 WSACleanup();
673}
674#endif
675
676int socket_init(void)
677{
678#ifdef _WIN32
679 WSADATA Data;
680 int ret, err;
681
682 ret = WSAStartup(MAKEWORD(2,2), &Data);
683 if (ret != 0) {
684 err = WSAGetLastError();
685 fprintf(stderr, "WSAStartup: %d\n", err);
686 return -1;
687 }
688 atexit(socket_cleanup);
689#endif
690 return 0;
691}