blob: 2b1ed2f0e91b4ac1b3e071b2056a33def79d9f0e [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 */
Orit Wasserman233aa5c2012-09-24 13:11:09 +020027#include "main-loop.h"
aliguorid247d252008-11-11 20:46:40 +000028
29#ifndef AI_ADDRCONFIG
30# define AI_ADDRCONFIG 0
31#endif
32
aliguorid247d252008-11-11 20:46:40 +000033static const int on=1, off=0;
34
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +020035/* used temporarely until all users are converted to QemuOpts */
Blue Swirlc1390902009-09-12 09:58:51 +000036static QemuOptsList dummy_opts = {
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +020037 .name = "dummy",
Blue Swirl72cf2d42009-09-12 07:36:22 +000038 .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head),
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +020039 .desc = {
40 {
41 .name = "path",
42 .type = QEMU_OPT_STRING,
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +020043 },{
44 .name = "host",
45 .type = QEMU_OPT_STRING,
46 },{
47 .name = "port",
48 .type = QEMU_OPT_STRING,
49 },{
50 .name = "to",
51 .type = QEMU_OPT_NUMBER,
52 },{
53 .name = "ipv4",
54 .type = QEMU_OPT_BOOL,
55 },{
56 .name = "ipv6",
57 .type = QEMU_OPT_BOOL,
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +020058 },
59 { /* end if list */ }
60 },
61};
62
aliguorid247d252008-11-11 20:46:40 +000063static int inet_getport(struct addrinfo *e)
64{
65 struct sockaddr_in *i4;
66 struct sockaddr_in6 *i6;
67
68 switch (e->ai_family) {
69 case PF_INET6:
70 i6 = (void*)e->ai_addr;
71 return ntohs(i6->sin6_port);
72 case PF_INET:
73 i4 = (void*)e->ai_addr;
74 return ntohs(i4->sin_port);
75 default:
76 return 0;
77 }
78}
79
80static void inet_setport(struct addrinfo *e, int port)
81{
82 struct sockaddr_in *i4;
83 struct sockaddr_in6 *i6;
84
85 switch (e->ai_family) {
86 case PF_INET6:
87 i6 = (void*)e->ai_addr;
88 i6->sin6_port = htons(port);
89 break;
90 case PF_INET:
91 i4 = (void*)e->ai_addr;
92 i4->sin_port = htons(port);
93 break;
94 }
95}
96
Luiz Capitulinoc9c4b342010-01-20 11:42:38 -020097const char *inet_strfamily(int family)
aliguorid247d252008-11-11 20:46:40 +000098{
99 switch (family) {
100 case PF_INET6: return "ipv6";
101 case PF_INET: return "ipv4";
102 case PF_UNIX: return "unix";
103 }
Luiz Capitulinoc4453212010-01-20 11:42:39 -0200104 return "unknown";
aliguorid247d252008-11-11 20:46:40 +0000105}
106
Amos Kong029409e2012-05-11 00:28:26 +0800107int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)
aliguorid247d252008-11-11 20:46:40 +0000108{
109 struct addrinfo ai,*res,*e;
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200110 const char *addr;
aliguorid247d252008-11-11 20:46:40 +0000111 char port[33];
112 char uaddr[INET6_ADDRSTRLEN+1];
113 char uport[33];
Markus Armbruster877691f2012-02-07 15:09:15 +0100114 int slisten, rc, to, port_min, port_max, p;
aliguorid247d252008-11-11 20:46:40 +0000115
116 memset(&ai,0, sizeof(ai));
117 ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
118 ai.ai_family = PF_UNSPEC;
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200119 ai.ai_socktype = SOCK_STREAM;
aliguorid247d252008-11-11 20:46:40 +0000120
Jens Osterkampe23a22e2010-04-12 10:51:01 +0200121 if ((qemu_opt_get(opts, "host") == NULL) ||
122 (qemu_opt_get(opts, "port") == NULL)) {
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200123 fprintf(stderr, "%s: host and/or port not specified\n", __FUNCTION__);
Amos Kong029409e2012-05-11 00:28:26 +0800124 error_set(errp, QERR_SOCKET_CREATE_FAILED);
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200125 return -1;
aliguorid247d252008-11-11 20:46:40 +0000126 }
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200127 pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port"));
128 addr = qemu_opt_get(opts, "host");
aliguorid247d252008-11-11 20:46:40 +0000129
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200130 to = qemu_opt_get_number(opts, "to", 0);
131 if (qemu_opt_get_bool(opts, "ipv4", 0))
aliguorid247d252008-11-11 20:46:40 +0000132 ai.ai_family = PF_INET;
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200133 if (qemu_opt_get_bool(opts, "ipv6", 0))
aliguorid247d252008-11-11 20:46:40 +0000134 ai.ai_family = PF_INET6;
135
136 /* lookup */
137 if (port_offset)
138 snprintf(port, sizeof(port), "%d", atoi(port) + port_offset);
139 rc = getaddrinfo(strlen(addr) ? addr : NULL, port, &ai, &res);
140 if (rc != 0) {
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200141 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
142 gai_strerror(rc));
Amos Kong029409e2012-05-11 00:28:26 +0800143 error_set(errp, QERR_SOCKET_CREATE_FAILED);
aliguorid247d252008-11-11 20:46:40 +0000144 return -1;
145 }
aliguorid247d252008-11-11 20:46:40 +0000146
147 /* create socket + bind */
148 for (e = res; e != NULL; e = e->ai_next) {
vibi39b6efc2009-05-06 15:27:03 +0530149 getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
150 uaddr,INET6_ADDRSTRLEN,uport,32,
151 NI_NUMERICHOST | NI_NUMERICSERV);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100152 slisten = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol);
vibi39b6efc2009-05-06 15:27:03 +0530153 if (slisten < 0) {
aliguorid247d252008-11-11 20:46:40 +0000154 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
155 inet_strfamily(e->ai_family), strerror(errno));
Amos Kong029409e2012-05-11 00:28:26 +0800156 if (!e->ai_next) {
157 error_set(errp, QERR_SOCKET_CREATE_FAILED);
158 }
vibi39b6efc2009-05-06 15:27:03 +0530159 continue;
160 }
aliguorid247d252008-11-11 20:46:40 +0000161
162 setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
163#ifdef IPV6_V6ONLY
164 if (e->ai_family == PF_INET6) {
165 /* listen on both ipv4 and ipv6 */
vibi39b6efc2009-05-06 15:27:03 +0530166 setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,
167 sizeof(off));
aliguorid247d252008-11-11 20:46:40 +0000168 }
169#endif
170
Markus Armbruster877691f2012-02-07 15:09:15 +0100171 port_min = inet_getport(e);
172 port_max = to ? to + port_offset : port_min;
173 for (p = port_min; p <= port_max; p++) {
174 inet_setport(e, p);
aliguorid247d252008-11-11 20:46:40 +0000175 if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {
aliguorid247d252008-11-11 20:46:40 +0000176 goto listen;
177 }
Markus Armbruster877691f2012-02-07 15:09:15 +0100178 if (p == port_max) {
aliguorid247d252008-11-11 20:46:40 +0000179 fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__,
180 inet_strfamily(e->ai_family), uaddr, inet_getport(e),
181 strerror(errno));
Amos Kong029409e2012-05-11 00:28:26 +0800182 if (!e->ai_next) {
183 error_set(errp, QERR_SOCKET_BIND_FAILED);
184 }
aliguorid247d252008-11-11 20:46:40 +0000185 }
aliguorid247d252008-11-11 20:46:40 +0000186 }
187 closesocket(slisten);
188 }
189 fprintf(stderr, "%s: FAILED\n", __FUNCTION__);
190 freeaddrinfo(res);
191 return -1;
192
193listen:
194 if (listen(slisten,1) != 0) {
Amos Kong029409e2012-05-11 00:28:26 +0800195 error_set(errp, QERR_SOCKET_LISTEN_FAILED);
aliguorid247d252008-11-11 20:46:40 +0000196 perror("listen");
197 closesocket(slisten);
vibi39b6efc2009-05-06 15:27:03 +0530198 freeaddrinfo(res);
aliguorid247d252008-11-11 20:46:40 +0000199 return -1;
200 }
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200201 snprintf(uport, sizeof(uport), "%d", inet_getport(e) - port_offset);
202 qemu_opt_set(opts, "host", uaddr);
203 qemu_opt_set(opts, "port", uport);
204 qemu_opt_set(opts, "ipv6", (e->ai_family == PF_INET6) ? "on" : "off");
205 qemu_opt_set(opts, "ipv4", (e->ai_family != PF_INET6) ? "on" : "off");
aliguorid247d252008-11-11 20:46:40 +0000206 freeaddrinfo(res);
207 return slisten;
208}
209
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200210#ifdef _WIN32
211#define QEMU_SOCKET_RC_INPROGRESS(rc) \
212 ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY)
213#else
214#define QEMU_SOCKET_RC_INPROGRESS(rc) \
215 ((rc) == -EINPROGRESS)
216#endif
aliguorid247d252008-11-11 20:46:40 +0000217
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200218/* Struct to store connect state for non blocking connect */
219typedef struct ConnectState {
220 int fd;
221 struct addrinfo *addr_list;
222 struct addrinfo *current_addr;
223 NonBlockingConnectHandler *callback;
224 void *opaque;
225} ConnectState;
226
227static int inet_connect_addr(struct addrinfo *addr, bool *in_progress,
228 ConnectState *connect_state);
229
230static void wait_for_connect(void *opaque)
231{
232 ConnectState *s = opaque;
233 int val = 0, rc = 0;
234 socklen_t valsize = sizeof(val);
235 bool in_progress;
236
237 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
238
239 do {
240 rc = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize);
241 } while (rc == -1 && socket_error() == EINTR);
242
243 /* update rc to contain error */
244 if (!rc && val) {
245 rc = -1;
246 }
247
248 /* connect error */
249 if (rc < 0) {
250 closesocket(s->fd);
251 s->fd = rc;
252 }
253
254 /* try to connect to the next address on the list */
255 while (s->current_addr->ai_next != NULL && s->fd < 0) {
256 s->current_addr = s->current_addr->ai_next;
257 s->fd = inet_connect_addr(s->current_addr, &in_progress, s);
258 /* connect in progress */
259 if (in_progress) {
260 return;
261 }
262 }
263
264 freeaddrinfo(s->addr_list);
265 if (s->callback) {
266 s->callback(s->fd, s->opaque);
267 }
268 g_free(s);
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200269}
270
271static int inet_connect_addr(struct addrinfo *addr, bool *in_progress,
272 ConnectState *connect_state)
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200273{
274 int sock, rc;
aliguorid247d252008-11-11 20:46:40 +0000275
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200276 *in_progress = false;
Luiz Capitulino02a08fe2012-08-01 13:42:47 -0300277
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200278 sock = qemu_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
279 if (sock < 0) {
280 fprintf(stderr, "%s: socket(%s): %s\n", __func__,
281 inet_strfamily(addr->ai_family), strerror(errno));
282 return -1;
283 }
Stefan Weil58455eb2012-09-28 19:07:39 +0200284 qemu_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200285 if (connect_state != NULL) {
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200286 socket_set_nonblock(sock);
287 }
288 /* connect to peer */
289 do {
290 rc = 0;
291 if (connect(sock, addr->ai_addr, addr->ai_addrlen) < 0) {
292 rc = -socket_error();
293 }
294 } while (rc == -EINTR);
295
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200296 if (connect_state != NULL && QEMU_SOCKET_RC_INPROGRESS(rc)) {
297 connect_state->fd = sock;
298 qemu_set_fd_handler2(sock, NULL, NULL, wait_for_connect,
299 connect_state);
300 *in_progress = true;
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200301 } else if (rc < 0) {
302 closesocket(sock);
303 return -1;
304 }
305 return sock;
306}
307
308static struct addrinfo *inet_parse_connect_opts(QemuOpts *opts, Error **errp)
309{
310 struct addrinfo ai, *res;
311 int rc;
312 const char *addr;
313 const char *port;
314
315 memset(&ai, 0, sizeof(ai));
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200316
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200317 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
318 ai.ai_family = PF_UNSPEC;
319 ai.ai_socktype = SOCK_STREAM;
320
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200321 addr = qemu_opt_get(opts, "host");
322 port = qemu_opt_get(opts, "port");
323 if (addr == NULL || port == NULL) {
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200324 fprintf(stderr,
325 "inet_parse_connect_opts: host and/or port not specified\n");
Amos Konga6ba35b2012-05-11 00:28:16 +0800326 error_set(errp, QERR_SOCKET_CREATE_FAILED);
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200327 return NULL;
328 }
329
330 if (qemu_opt_get_bool(opts, "ipv4", 0)) {
331 ai.ai_family = PF_INET;
332 }
333 if (qemu_opt_get_bool(opts, "ipv6", 0)) {
334 ai.ai_family = PF_INET6;
335 }
336
337 /* lookup */
338 rc = getaddrinfo(addr, port, &ai, &res);
339 if (rc != 0) {
340 fprintf(stderr, "getaddrinfo(%s,%s): %s\n", addr, port,
341 gai_strerror(rc));
342 error_set(errp, QERR_SOCKET_CREATE_FAILED);
343 return NULL;
344 }
345 return res;
346}
347
Orit Wasserman5db5f442012-09-24 13:11:08 +0200348/**
349 * Create a socket and connect it to an address.
350 *
351 * @opts: QEMU options, recognized parameters strings "host" and "port",
352 * bools "ipv4" and "ipv6".
Orit Wasserman5db5f442012-09-24 13:11:08 +0200353 * @errp: set on error
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200354 * @callback: callback function for non-blocking connect
355 * @opaque: opaque for callback function
Orit Wasserman5db5f442012-09-24 13:11:08 +0200356 *
357 * Returns: -1 on error, file descriptor on success.
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200358 *
359 * If @callback is non-null, the connect is non-blocking. If this
360 * function succeeds, callback will be called when the connection
361 * completes, with the file descriptor on success, or -1 on error.
Orit Wasserman5db5f442012-09-24 13:11:08 +0200362 */
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200363int inet_connect_opts(QemuOpts *opts, Error **errp,
364 NonBlockingConnectHandler *callback, void *opaque)
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200365{
366 struct addrinfo *res, *e;
367 int sock = -1;
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200368 bool in_progress;
369 ConnectState *connect_state = NULL;
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200370
371 res = inet_parse_connect_opts(opts, errp);
372 if (!res) {
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200373 return -1;
aliguorid247d252008-11-11 20:46:40 +0000374 }
375
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200376 if (callback != NULL) {
377 connect_state = g_malloc0(sizeof(*connect_state));
378 connect_state->addr_list = res;
379 connect_state->callback = callback;
380 connect_state->opaque = opaque;
aliguorid247d252008-11-11 20:46:40 +0000381 }
aliguorid247d252008-11-11 20:46:40 +0000382
383 for (e = res; e != NULL; e = e->ai_next) {
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200384 if (connect_state != NULL) {
385 connect_state->current_addr = e;
386 }
387 sock = inet_connect_addr(e, &in_progress, connect_state);
388 if (in_progress) {
389 return sock;
390 } else if (sock >= 0) {
391 /* non blocking socket immediate success, call callback */
392 if (callback != NULL) {
393 callback(sock, opaque);
394 }
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200395 break;
vibi39b6efc2009-05-06 15:27:03 +0530396 }
aliguorid247d252008-11-11 20:46:40 +0000397 }
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200398 if (sock < 0) {
399 error_set(errp, QERR_SOCKET_CONNECT_FAILED);
400 }
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200401 g_free(connect_state);
aliguorid247d252008-11-11 20:46:40 +0000402 freeaddrinfo(res);
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200403 return sock;
aliguorid247d252008-11-11 20:46:40 +0000404}
405
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200406int inet_dgram_opts(QemuOpts *opts)
407{
408 struct addrinfo ai, *peer = NULL, *local = NULL;
409 const char *addr;
410 const char *port;
411 char uaddr[INET6_ADDRSTRLEN+1];
412 char uport[33];
413 int sock = -1, rc;
414
415 /* lookup peer addr */
416 memset(&ai,0, sizeof(ai));
417 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
418 ai.ai_family = PF_UNSPEC;
419 ai.ai_socktype = SOCK_DGRAM;
420
421 addr = qemu_opt_get(opts, "host");
422 port = qemu_opt_get(opts, "port");
423 if (addr == NULL || strlen(addr) == 0) {
424 addr = "localhost";
425 }
426 if (port == NULL || strlen(port) == 0) {
427 fprintf(stderr, "inet_dgram: port not specified\n");
428 return -1;
429 }
430
431 if (qemu_opt_get_bool(opts, "ipv4", 0))
432 ai.ai_family = PF_INET;
433 if (qemu_opt_get_bool(opts, "ipv6", 0))
434 ai.ai_family = PF_INET6;
435
436 if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) {
437 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
438 gai_strerror(rc));
439 return -1;
440 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200441
442 /* lookup local addr */
443 memset(&ai,0, sizeof(ai));
444 ai.ai_flags = AI_PASSIVE;
445 ai.ai_family = peer->ai_family;
446 ai.ai_socktype = SOCK_DGRAM;
447
448 addr = qemu_opt_get(opts, "localaddr");
449 port = qemu_opt_get(opts, "localport");
450 if (addr == NULL || strlen(addr) == 0) {
451 addr = NULL;
452 }
453 if (!port || strlen(port) == 0)
454 port = "0";
455
456 if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) {
457 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
458 gai_strerror(rc));
Stefan Weil39b38452012-09-01 09:40:26 +0200459 goto err;
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200460 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200461
462 /* create socket */
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100463 sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200464 if (sock < 0) {
465 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
466 inet_strfamily(peer->ai_family), strerror(errno));
467 goto err;
468 }
469 setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
470
471 /* bind socket */
472 if (getnameinfo((struct sockaddr*)local->ai_addr,local->ai_addrlen,
473 uaddr,INET6_ADDRSTRLEN,uport,32,
474 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
475 fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
476 goto err;
477 }
478 if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) {
479 fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,
480 inet_strfamily(local->ai_family), uaddr, inet_getport(local));
481 goto err;
482 }
483
484 /* connect to peer */
485 if (getnameinfo((struct sockaddr*)peer->ai_addr, peer->ai_addrlen,
486 uaddr, INET6_ADDRSTRLEN, uport, 32,
487 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
488 fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
489 goto err;
490 }
491 if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) {
492 fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
493 inet_strfamily(peer->ai_family),
494 peer->ai_canonname, uaddr, uport, strerror(errno));
495 goto err;
496 }
497
498 freeaddrinfo(local);
499 freeaddrinfo(peer);
500 return sock;
501
502err:
503 if (-1 != sock)
504 closesocket(sock);
505 if (local)
506 freeaddrinfo(local);
507 if (peer)
508 freeaddrinfo(peer);
509 return -1;
510}
511
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200512/* compatibility wrapper */
513static int inet_parse(QemuOpts *opts, const char *str)
514{
515 const char *optstr, *h;
516 char addr[64];
517 char port[33];
518 int pos;
519
520 /* parse address */
521 if (str[0] == ':') {
522 /* no host given */
523 addr[0] = '\0';
524 if (1 != sscanf(str,":%32[^,]%n",port,&pos)) {
525 fprintf(stderr, "%s: portonly parse error (%s)\n",
526 __FUNCTION__, str);
527 return -1;
528 }
529 } else if (str[0] == '[') {
530 /* IPv6 addr */
531 if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) {
532 fprintf(stderr, "%s: ipv6 parse error (%s)\n",
533 __FUNCTION__, str);
534 return -1;
535 }
Marcelo Tosatti2198a622010-02-09 15:31:46 -0200536 qemu_opt_set(opts, "ipv6", "on");
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200537 } else if (qemu_isdigit(str[0])) {
538 /* IPv4 addr */
539 if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) {
540 fprintf(stderr, "%s: ipv4 parse error (%s)\n",
541 __FUNCTION__, str);
542 return -1;
543 }
Marcelo Tosatti2198a622010-02-09 15:31:46 -0200544 qemu_opt_set(opts, "ipv4", "on");
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200545 } else {
546 /* hostname */
547 if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) {
548 fprintf(stderr, "%s: hostname parse error (%s)\n",
549 __FUNCTION__, str);
550 return -1;
551 }
552 }
553 qemu_opt_set(opts, "host", addr);
554 qemu_opt_set(opts, "port", port);
555
556 /* parse options */
557 optstr = str + pos;
558 h = strstr(optstr, ",to=");
559 if (h)
560 qemu_opt_set(opts, "to", h+4);
561 if (strstr(optstr, ",ipv4"))
Marcelo Tosatti2198a622010-02-09 15:31:46 -0200562 qemu_opt_set(opts, "ipv4", "on");
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200563 if (strstr(optstr, ",ipv6"))
Marcelo Tosatti2198a622010-02-09 15:31:46 -0200564 qemu_opt_set(opts, "ipv6", "on");
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200565 return 0;
566}
567
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200568int inet_listen(const char *str, char *ostr, int olen,
Amos Kong029409e2012-05-11 00:28:26 +0800569 int socktype, int port_offset, Error **errp)
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200570{
571 QemuOpts *opts;
572 char *optstr;
573 int sock = -1;
574
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300575 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200576 if (inet_parse(opts, str) == 0) {
Amos Kong029409e2012-05-11 00:28:26 +0800577 sock = inet_listen_opts(opts, port_offset, errp);
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200578 if (sock != -1 && ostr) {
579 optstr = strchr(str, ',');
580 if (qemu_opt_get_bool(opts, "ipv6", 0)) {
581 snprintf(ostr, olen, "[%s]:%s%s",
582 qemu_opt_get(opts, "host"),
583 qemu_opt_get(opts, "port"),
584 optstr ? optstr : "");
585 } else {
586 snprintf(ostr, olen, "%s:%s%s",
587 qemu_opt_get(opts, "host"),
588 qemu_opt_get(opts, "port"),
589 optstr ? optstr : "");
590 }
591 }
Amos Kong029409e2012-05-11 00:28:26 +0800592 } else {
593 error_set(errp, QERR_SOCKET_CREATE_FAILED);
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200594 }
595 qemu_opts_del(opts);
596 return sock;
597}
598
Orit Wasserman5db5f442012-09-24 13:11:08 +0200599/**
600 * Create a blocking socket and connect it to an address.
601 *
602 * @str: address string
603 * @errp: set in case of an error
604 *
605 * Returns -1 in case of error, file descriptor on success
606 **/
607int inet_connect(const char *str, Error **errp)
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200608{
609 QemuOpts *opts;
610 int sock = -1;
611
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300612 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
Amos Konga6ba35b2012-05-11 00:28:16 +0800613 if (inet_parse(opts, str) == 0) {
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200614 sock = inet_connect_opts(opts, errp, NULL, NULL);
Orit Wasserman5db5f442012-09-24 13:11:08 +0200615 } else {
616 error_set(errp, QERR_SOCKET_CREATE_FAILED);
617 }
618 qemu_opts_del(opts);
619 return sock;
620}
621
622/**
623 * Create a non-blocking socket and connect it to an address.
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200624 * Calls the callback function with fd in case of success or -1 in case of
625 * error.
Orit Wasserman5db5f442012-09-24 13:11:08 +0200626 *
627 * @str: address string
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200628 * @callback: callback function that is called when connect completes,
629 * cannot be NULL.
630 * @opaque: opaque for callback function
Orit Wasserman5db5f442012-09-24 13:11:08 +0200631 * @errp: set in case of an error
632 *
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200633 * Returns: -1 on immediate error, file descriptor on success.
Orit Wasserman5db5f442012-09-24 13:11:08 +0200634 **/
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200635int inet_nonblocking_connect(const char *str,
636 NonBlockingConnectHandler *callback,
637 void *opaque, Error **errp)
Orit Wasserman5db5f442012-09-24 13:11:08 +0200638{
639 QemuOpts *opts;
640 int sock = -1;
641
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200642 g_assert(callback != NULL);
643
Orit Wasserman5db5f442012-09-24 13:11:08 +0200644 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
645 if (inet_parse(opts, str) == 0) {
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200646 sock = inet_connect_opts(opts, errp, callback, opaque);
Amos Konga6ba35b2012-05-11 00:28:16 +0800647 } else {
648 error_set(errp, QERR_SOCKET_CREATE_FAILED);
649 }
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200650 qemu_opts_del(opts);
651 return sock;
652}
653
aliguorid247d252008-11-11 20:46:40 +0000654#ifndef _WIN32
655
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200656int unix_listen_opts(QemuOpts *opts)
aliguorid247d252008-11-11 20:46:40 +0000657{
658 struct sockaddr_un un;
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200659 const char *path = qemu_opt_get(opts, "path");
660 int sock, fd;
aliguorid247d252008-11-11 20:46:40 +0000661
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100662 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
aliguorid247d252008-11-11 20:46:40 +0000663 if (sock < 0) {
vibi39b6efc2009-05-06 15:27:03 +0530664 perror("socket(unix)");
665 return -1;
aliguorid247d252008-11-11 20:46:40 +0000666 }
667
aliguorid247d252008-11-11 20:46:40 +0000668 memset(&un, 0, sizeof(un));
669 un.sun_family = AF_UNIX;
670 if (path && strlen(path)) {
671 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
672 } else {
673 char *tmpdir = getenv("TMPDIR");
674 snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX",
675 tmpdir ? tmpdir : "/tmp");
676 /*
677 * This dummy fd usage silences the mktemp() unsecure warning.
678 * Using mkstemp() doesn't make things more secure here
679 * though. bind() complains about existing files, so we have
680 * to unlink first and thus re-open the race window. The
681 * worst case possible is bind() failing, i.e. a DoS attack.
682 */
683 fd = mkstemp(un.sun_path); close(fd);
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200684 qemu_opt_set(opts, "path", un.sun_path);
aliguorid247d252008-11-11 20:46:40 +0000685 }
aliguorid247d252008-11-11 20:46:40 +0000686
687 unlink(un.sun_path);
688 if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
689 fprintf(stderr, "bind(unix:%s): %s\n", un.sun_path, strerror(errno));
690 goto err;
691 }
692 if (listen(sock, 1) < 0) {
693 fprintf(stderr, "listen(unix:%s): %s\n", un.sun_path, strerror(errno));
694 goto err;
695 }
696
aliguorid247d252008-11-11 20:46:40 +0000697 return sock;
698
699err:
aliguorid247d252008-11-11 20:46:40 +0000700 closesocket(sock);
701 return -1;
702}
703
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200704int unix_connect_opts(QemuOpts *opts)
aliguorid247d252008-11-11 20:46:40 +0000705{
706 struct sockaddr_un un;
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200707 const char *path = qemu_opt_get(opts, "path");
aliguorid247d252008-11-11 20:46:40 +0000708 int sock;
709
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200710 if (NULL == path) {
711 fprintf(stderr, "unix connect: no path specified\n");
712 return -1;
713 }
714
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100715 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
aliguorid247d252008-11-11 20:46:40 +0000716 if (sock < 0) {
vibi39b6efc2009-05-06 15:27:03 +0530717 perror("socket(unix)");
718 return -1;
aliguorid247d252008-11-11 20:46:40 +0000719 }
720
721 memset(&un, 0, sizeof(un));
722 un.sun_family = AF_UNIX;
723 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
724 if (connect(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
725 fprintf(stderr, "connect(unix:%s): %s\n", path, strerror(errno));
Markus Armbruster9d947472011-11-11 10:40:07 +0100726 close(sock);
aliguorid247d252008-11-11 20:46:40 +0000727 return -1;
728 }
729
aliguorid247d252008-11-11 20:46:40 +0000730 return sock;
731}
732
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200733/* compatibility wrapper */
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200734int unix_listen(const char *str, char *ostr, int olen)
735{
736 QemuOpts *opts;
737 char *path, *optstr;
738 int sock, len;
739
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300740 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200741
742 optstr = strchr(str, ',');
743 if (optstr) {
744 len = optstr - str;
745 if (len) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500746 path = g_malloc(len+1);
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200747 snprintf(path, len+1, "%.*s", len, str);
748 qemu_opt_set(opts, "path", path);
Anthony Liguori7267c092011-08-20 22:09:37 -0500749 g_free(path);
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200750 }
751 } else {
752 qemu_opt_set(opts, "path", str);
753 }
754
755 sock = unix_listen_opts(opts);
756
757 if (sock != -1 && ostr)
758 snprintf(ostr, olen, "%s%s", qemu_opt_get(opts, "path"), optstr ? optstr : "");
759 qemu_opts_del(opts);
760 return sock;
761}
762
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200763int unix_connect(const char *path)
764{
765 QemuOpts *opts;
766 int sock;
767
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300768 opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200769 qemu_opt_set(opts, "path", path);
770 sock = unix_connect_opts(opts);
771 qemu_opts_del(opts);
772 return sock;
773}
774
aliguorid247d252008-11-11 20:46:40 +0000775#else
776
Gerd Hoffmann108af7b2009-09-10 10:58:39 +0200777int unix_listen_opts(QemuOpts *opts)
778{
779 fprintf(stderr, "unix sockets are not available on windows\n");
Nick Thomasb82eac92011-02-22 15:44:52 +0000780 errno = ENOTSUP;
Gerd Hoffmann108af7b2009-09-10 10:58:39 +0200781 return -1;
782}
783
784int unix_connect_opts(QemuOpts *opts)
785{
786 fprintf(stderr, "unix sockets are not available on windows\n");
Nick Thomasb82eac92011-02-22 15:44:52 +0000787 errno = ENOTSUP;
Gerd Hoffmann108af7b2009-09-10 10:58:39 +0200788 return -1;
789}
790
aliguorid247d252008-11-11 20:46:40 +0000791int unix_listen(const char *path, char *ostr, int olen)
792{
793 fprintf(stderr, "unix sockets are not available on windows\n");
Nick Thomasb82eac92011-02-22 15:44:52 +0000794 errno = ENOTSUP;
aliguorid247d252008-11-11 20:46:40 +0000795 return -1;
796}
797
798int unix_connect(const char *path)
799{
800 fprintf(stderr, "unix sockets are not available on windows\n");
Nick Thomasb82eac92011-02-22 15:44:52 +0000801 errno = ENOTSUP;
aliguorid247d252008-11-11 20:46:40 +0000802 return -1;
803}
804
805#endif
Paolo Bonzini0706a4d2010-04-01 19:57:08 +0200806
807#ifdef _WIN32
808static void socket_cleanup(void)
809{
810 WSACleanup();
811}
812#endif
813
814int socket_init(void)
815{
816#ifdef _WIN32
817 WSADATA Data;
818 int ret, err;
819
820 ret = WSAStartup(MAKEWORD(2,2), &Data);
821 if (ret != 0) {
822 err = WSAGetLastError();
823 fprintf(stderr, "WSAStartup: %d\n", err);
824 return -1;
825 }
826 atexit(socket_cleanup);
827#endif
828 return 0;
829}