blob: 8818d7c0de2382642762160d4fbcbf679f15e3b2 [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
Paolo Bonzini83c90892012-12-17 18:19:49 +010025#include "monitor/monitor.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010026#include "qemu/sockets.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010027#include "qemu/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
Kevin Wolfe62be882013-03-15 17:14:24 +010035/* used temporarily until all users are converted to QemuOpts */
36QemuOptsList socket_optslist = {
37 .name = "socket",
38 .head = QTAILQ_HEAD_INITIALIZER(socket_optslist.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)) {
Paolo Bonzinia12fb8a2012-10-02 09:18:02 +0200123 error_setg(errp, "host and/or port not specified");
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) {
Paolo Bonzinia12fb8a2012-10-02 09:18:02 +0200140 error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
141 gai_strerror(rc));
aliguorid247d252008-11-11 20:46:40 +0000142 return -1;
143 }
aliguorid247d252008-11-11 20:46:40 +0000144
145 /* create socket + bind */
146 for (e = res; e != NULL; e = e->ai_next) {
vibi39b6efc2009-05-06 15:27:03 +0530147 getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
148 uaddr,INET6_ADDRSTRLEN,uport,32,
149 NI_NUMERICHOST | NI_NUMERICSERV);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100150 slisten = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol);
vibi39b6efc2009-05-06 15:27:03 +0530151 if (slisten < 0) {
Amos Kong029409e2012-05-11 00:28:26 +0800152 if (!e->ai_next) {
Paolo Bonzinia12fb8a2012-10-02 09:18:02 +0200153 error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED);
Amos Kong029409e2012-05-11 00:28:26 +0800154 }
vibi39b6efc2009-05-06 15:27:03 +0530155 continue;
156 }
aliguorid247d252008-11-11 20:46:40 +0000157
Sebastian Ottlik04fd1c72013-10-02 12:23:16 +0200158 socket_set_fast_reuse(slisten);
aliguorid247d252008-11-11 20:46:40 +0000159#ifdef IPV6_V6ONLY
160 if (e->ai_family == PF_INET6) {
161 /* listen on both ipv4 and ipv6 */
Stefan Weil9957fc72013-03-08 19:58:32 +0100162 qemu_setsockopt(slisten, IPPROTO_IPV6, IPV6_V6ONLY, &off,
163 sizeof(off));
aliguorid247d252008-11-11 20:46:40 +0000164 }
165#endif
166
Markus Armbruster877691f2012-02-07 15:09:15 +0100167 port_min = inet_getport(e);
168 port_max = to ? to + port_offset : port_min;
169 for (p = port_min; p <= port_max; p++) {
170 inet_setport(e, p);
aliguorid247d252008-11-11 20:46:40 +0000171 if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {
aliguorid247d252008-11-11 20:46:40 +0000172 goto listen;
173 }
Markus Armbruster877691f2012-02-07 15:09:15 +0100174 if (p == port_max) {
Amos Kong029409e2012-05-11 00:28:26 +0800175 if (!e->ai_next) {
Paolo Bonzinia12fb8a2012-10-02 09:18:02 +0200176 error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED);
Amos Kong029409e2012-05-11 00:28:26 +0800177 }
aliguorid247d252008-11-11 20:46:40 +0000178 }
aliguorid247d252008-11-11 20:46:40 +0000179 }
180 closesocket(slisten);
181 }
aliguorid247d252008-11-11 20:46:40 +0000182 freeaddrinfo(res);
183 return -1;
184
185listen:
186 if (listen(slisten,1) != 0) {
Paolo Bonzinia12fb8a2012-10-02 09:18:02 +0200187 error_set_errno(errp, errno, QERR_SOCKET_LISTEN_FAILED);
aliguorid247d252008-11-11 20:46:40 +0000188 closesocket(slisten);
vibi39b6efc2009-05-06 15:27:03 +0530189 freeaddrinfo(res);
aliguorid247d252008-11-11 20:46:40 +0000190 return -1;
191 }
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200192 snprintf(uport, sizeof(uport), "%d", inet_getport(e) - port_offset);
193 qemu_opt_set(opts, "host", uaddr);
194 qemu_opt_set(opts, "port", uport);
195 qemu_opt_set(opts, "ipv6", (e->ai_family == PF_INET6) ? "on" : "off");
196 qemu_opt_set(opts, "ipv4", (e->ai_family != PF_INET6) ? "on" : "off");
aliguorid247d252008-11-11 20:46:40 +0000197 freeaddrinfo(res);
198 return slisten;
199}
200
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200201#ifdef _WIN32
202#define QEMU_SOCKET_RC_INPROGRESS(rc) \
203 ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY)
204#else
205#define QEMU_SOCKET_RC_INPROGRESS(rc) \
206 ((rc) == -EINPROGRESS)
207#endif
aliguorid247d252008-11-11 20:46:40 +0000208
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200209/* Struct to store connect state for non blocking connect */
210typedef struct ConnectState {
211 int fd;
212 struct addrinfo *addr_list;
213 struct addrinfo *current_addr;
214 NonBlockingConnectHandler *callback;
215 void *opaque;
216} ConnectState;
217
218static int inet_connect_addr(struct addrinfo *addr, bool *in_progress,
Paolo Bonzini11663b52012-10-02 09:19:01 +0200219 ConnectState *connect_state, Error **errp);
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200220
221static void wait_for_connect(void *opaque)
222{
223 ConnectState *s = opaque;
224 int val = 0, rc = 0;
225 socklen_t valsize = sizeof(val);
226 bool in_progress;
227
228 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
229
230 do {
Stefan Weil9957fc72013-03-08 19:58:32 +0100231 rc = qemu_getsockopt(s->fd, SOL_SOCKET, SO_ERROR, &val, &valsize);
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200232 } while (rc == -1 && socket_error() == EINTR);
233
234 /* update rc to contain error */
235 if (!rc && val) {
236 rc = -1;
237 }
238
239 /* connect error */
240 if (rc < 0) {
241 closesocket(s->fd);
242 s->fd = rc;
243 }
244
245 /* try to connect to the next address on the list */
Paolo Bonzini1fc05ad2012-10-03 13:37:46 +0200246 if (s->current_addr) {
247 while (s->current_addr->ai_next != NULL && s->fd < 0) {
248 s->current_addr = s->current_addr->ai_next;
Paolo Bonzini11663b52012-10-02 09:19:01 +0200249 s->fd = inet_connect_addr(s->current_addr, &in_progress, s, NULL);
Paolo Bonzini1fc05ad2012-10-03 13:37:46 +0200250 /* connect in progress */
251 if (in_progress) {
252 return;
253 }
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200254 }
Paolo Bonzini1fc05ad2012-10-03 13:37:46 +0200255
256 freeaddrinfo(s->addr_list);
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200257 }
258
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200259 if (s->callback) {
260 s->callback(s->fd, s->opaque);
261 }
262 g_free(s);
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200263}
264
265static int inet_connect_addr(struct addrinfo *addr, bool *in_progress,
Paolo Bonzini11663b52012-10-02 09:19:01 +0200266 ConnectState *connect_state, Error **errp)
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200267{
268 int sock, rc;
aliguorid247d252008-11-11 20:46:40 +0000269
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200270 *in_progress = false;
Luiz Capitulino02a08fe2012-08-01 13:42:47 -0300271
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200272 sock = qemu_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
273 if (sock < 0) {
Paolo Bonzini11663b52012-10-02 09:19:01 +0200274 error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED);
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200275 return -1;
276 }
Sebastian Ottlik04fd1c72013-10-02 12:23:16 +0200277 socket_set_fast_reuse(sock);
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200278 if (connect_state != NULL) {
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100279 qemu_set_nonblock(sock);
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200280 }
281 /* connect to peer */
282 do {
283 rc = 0;
284 if (connect(sock, addr->ai_addr, addr->ai_addrlen) < 0) {
285 rc = -socket_error();
286 }
287 } while (rc == -EINTR);
288
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200289 if (connect_state != NULL && QEMU_SOCKET_RC_INPROGRESS(rc)) {
290 connect_state->fd = sock;
291 qemu_set_fd_handler2(sock, NULL, NULL, wait_for_connect,
292 connect_state);
293 *in_progress = true;
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200294 } else if (rc < 0) {
Paolo Bonzini11663b52012-10-02 09:19:01 +0200295 error_set_errno(errp, errno, QERR_SOCKET_CONNECT_FAILED);
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200296 closesocket(sock);
297 return -1;
298 }
299 return sock;
300}
301
302static struct addrinfo *inet_parse_connect_opts(QemuOpts *opts, Error **errp)
303{
304 struct addrinfo ai, *res;
305 int rc;
306 const char *addr;
307 const char *port;
308
309 memset(&ai, 0, sizeof(ai));
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200310
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200311 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
312 ai.ai_family = PF_UNSPEC;
313 ai.ai_socktype = SOCK_STREAM;
314
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200315 addr = qemu_opt_get(opts, "host");
316 port = qemu_opt_get(opts, "port");
317 if (addr == NULL || port == NULL) {
Paolo Bonzinia12fb8a2012-10-02 09:18:02 +0200318 error_setg(errp, "host and/or port not specified");
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200319 return NULL;
320 }
321
322 if (qemu_opt_get_bool(opts, "ipv4", 0)) {
323 ai.ai_family = PF_INET;
324 }
325 if (qemu_opt_get_bool(opts, "ipv6", 0)) {
326 ai.ai_family = PF_INET6;
327 }
328
329 /* lookup */
330 rc = getaddrinfo(addr, port, &ai, &res);
331 if (rc != 0) {
Paolo Bonzinia12fb8a2012-10-02 09:18:02 +0200332 error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
333 gai_strerror(rc));
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200334 return NULL;
335 }
336 return res;
337}
338
Orit Wasserman5db5f442012-09-24 13:11:08 +0200339/**
340 * Create a socket and connect it to an address.
341 *
342 * @opts: QEMU options, recognized parameters strings "host" and "port",
343 * bools "ipv4" and "ipv6".
Orit Wasserman5db5f442012-09-24 13:11:08 +0200344 * @errp: set on error
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200345 * @callback: callback function for non-blocking connect
346 * @opaque: opaque for callback function
Orit Wasserman5db5f442012-09-24 13:11:08 +0200347 *
348 * Returns: -1 on error, file descriptor on success.
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200349 *
350 * If @callback is non-null, the connect is non-blocking. If this
351 * function succeeds, callback will be called when the connection
352 * completes, with the file descriptor on success, or -1 on error.
Orit Wasserman5db5f442012-09-24 13:11:08 +0200353 */
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200354int inet_connect_opts(QemuOpts *opts, Error **errp,
355 NonBlockingConnectHandler *callback, void *opaque)
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200356{
357 struct addrinfo *res, *e;
358 int sock = -1;
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200359 bool in_progress;
360 ConnectState *connect_state = NULL;
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200361
362 res = inet_parse_connect_opts(opts, errp);
363 if (!res) {
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200364 return -1;
aliguorid247d252008-11-11 20:46:40 +0000365 }
366
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200367 if (callback != NULL) {
368 connect_state = g_malloc0(sizeof(*connect_state));
369 connect_state->addr_list = res;
370 connect_state->callback = callback;
371 connect_state->opaque = opaque;
aliguorid247d252008-11-11 20:46:40 +0000372 }
aliguorid247d252008-11-11 20:46:40 +0000373
374 for (e = res; e != NULL; e = e->ai_next) {
Anthony Liguoribaca6f12013-03-05 23:21:16 +0530375 if (error_is_set(errp)) {
376 error_free(*errp);
377 *errp = NULL;
378 }
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200379 if (connect_state != NULL) {
380 connect_state->current_addr = e;
381 }
Paolo Bonzini11663b52012-10-02 09:19:01 +0200382 sock = inet_connect_addr(e, &in_progress, connect_state, errp);
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200383 if (in_progress) {
384 return sock;
385 } else if (sock >= 0) {
386 /* non blocking socket immediate success, call callback */
387 if (callback != NULL) {
388 callback(sock, opaque);
389 }
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200390 break;
vibi39b6efc2009-05-06 15:27:03 +0530391 }
aliguorid247d252008-11-11 20:46:40 +0000392 }
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200393 g_free(connect_state);
aliguorid247d252008-11-11 20:46:40 +0000394 freeaddrinfo(res);
Michael S. Tsirkin05bc1d82012-09-24 13:11:07 +0200395 return sock;
aliguorid247d252008-11-11 20:46:40 +0000396}
397
Paolo Bonzini7fc4e632012-10-02 09:35:32 +0200398int inet_dgram_opts(QemuOpts *opts, Error **errp)
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200399{
400 struct addrinfo ai, *peer = NULL, *local = NULL;
401 const char *addr;
402 const char *port;
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200403 int sock = -1, rc;
404
405 /* lookup peer addr */
406 memset(&ai,0, sizeof(ai));
407 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
408 ai.ai_family = PF_UNSPEC;
409 ai.ai_socktype = SOCK_DGRAM;
410
411 addr = qemu_opt_get(opts, "host");
412 port = qemu_opt_get(opts, "port");
413 if (addr == NULL || strlen(addr) == 0) {
414 addr = "localhost";
415 }
416 if (port == NULL || strlen(port) == 0) {
Paolo Bonzini4f085c82012-10-02 09:25:14 +0200417 error_setg(errp, "remote port not specified");
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200418 return -1;
419 }
420
421 if (qemu_opt_get_bool(opts, "ipv4", 0))
422 ai.ai_family = PF_INET;
423 if (qemu_opt_get_bool(opts, "ipv6", 0))
424 ai.ai_family = PF_INET6;
425
426 if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) {
Paolo Bonzini4f085c82012-10-02 09:25:14 +0200427 error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
428 gai_strerror(rc));
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200429 return -1;
430 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200431
432 /* lookup local addr */
433 memset(&ai,0, sizeof(ai));
434 ai.ai_flags = AI_PASSIVE;
435 ai.ai_family = peer->ai_family;
436 ai.ai_socktype = SOCK_DGRAM;
437
438 addr = qemu_opt_get(opts, "localaddr");
439 port = qemu_opt_get(opts, "localport");
440 if (addr == NULL || strlen(addr) == 0) {
441 addr = NULL;
442 }
443 if (!port || strlen(port) == 0)
444 port = "0";
445
446 if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) {
Paolo Bonzini4f085c82012-10-02 09:25:14 +0200447 error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
448 gai_strerror(rc));
Stefan Weil39b38452012-09-01 09:40:26 +0200449 goto err;
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200450 }
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200451
452 /* create socket */
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100453 sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200454 if (sock < 0) {
Paolo Bonzini4f085c82012-10-02 09:25:14 +0200455 error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200456 goto err;
457 }
Sebastian Ottlik04fd1c72013-10-02 12:23:16 +0200458 socket_set_fast_reuse(sock);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200459
460 /* bind socket */
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200461 if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) {
Paolo Bonzini4f085c82012-10-02 09:25:14 +0200462 error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200463 goto err;
464 }
465
466 /* connect to peer */
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200467 if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) {
Paolo Bonzini4f085c82012-10-02 09:25:14 +0200468 error_set_errno(errp, errno, QERR_SOCKET_CONNECT_FAILED);
Gerd Hoffmann7e1b35b2009-09-10 10:58:51 +0200469 goto err;
470 }
471
472 freeaddrinfo(local);
473 freeaddrinfo(peer);
474 return sock;
475
476err:
477 if (-1 != sock)
478 closesocket(sock);
479 if (local)
480 freeaddrinfo(local);
481 if (peer)
482 freeaddrinfo(peer);
483 return -1;
484}
485
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200486/* compatibility wrapper */
Kevin Wolff17c90b2013-03-15 11:55:29 +0100487InetSocketAddress *inet_parse(const char *str, Error **errp)
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200488{
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200489 InetSocketAddress *addr;
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200490 const char *optstr, *h;
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200491 char host[64];
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200492 char port[33];
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200493 int to;
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200494 int pos;
495
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200496 addr = g_new0(InetSocketAddress, 1);
497
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200498 /* parse address */
499 if (str[0] == ':') {
500 /* no host given */
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200501 host[0] = '\0';
502 if (1 != sscanf(str, ":%32[^,]%n", port, &pos)) {
Paolo Bonzini2f002c42012-09-19 13:22:21 +0200503 error_setg(errp, "error parsing port in address '%s'", str);
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200504 goto fail;
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200505 }
506 } else if (str[0] == '[') {
507 /* IPv6 addr */
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200508 if (2 != sscanf(str, "[%64[^]]]:%32[^,]%n", host, port, &pos)) {
Paolo Bonzini2f002c42012-09-19 13:22:21 +0200509 error_setg(errp, "error parsing IPv6 address '%s'", str);
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200510 goto fail;
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200511 }
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200512 addr->ipv6 = addr->has_ipv6 = true;
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200513 } else {
Ján Tomko391b7b92013-06-03 17:54:55 +0200514 /* hostname or IPv4 addr */
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200515 if (2 != sscanf(str, "%64[^:]:%32[^,]%n", host, port, &pos)) {
Paolo Bonzini2f002c42012-09-19 13:22:21 +0200516 error_setg(errp, "error parsing address '%s'", str);
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200517 goto fail;
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200518 }
Ján Tomko391b7b92013-06-03 17:54:55 +0200519 if (host[strspn(host, "0123456789.")] == '\0') {
520 addr->ipv4 = addr->has_ipv4 = true;
521 }
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200522 }
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200523
524 addr->host = g_strdup(host);
525 addr->port = g_strdup(port);
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200526
527 /* parse options */
528 optstr = str + pos;
529 h = strstr(optstr, ",to=");
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200530 if (h) {
Anthony PERARD1ccbc282012-11-16 04:08:14 +0000531 h += 4;
532 if (sscanf(h, "%d%n", &to, &pos) != 1 ||
533 (h[pos] != '\0' && h[pos] != ',')) {
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200534 error_setg(errp, "error parsing to= argument");
535 goto fail;
536 }
537 addr->has_to = true;
538 addr->to = to;
539 }
540 if (strstr(optstr, ",ipv4")) {
541 addr->ipv4 = addr->has_ipv4 = true;
542 }
543 if (strstr(optstr, ",ipv6")) {
544 addr->ipv6 = addr->has_ipv6 = true;
545 }
546 return addr;
547
548fail:
549 qapi_free_InetSocketAddress(addr);
550 return NULL;
551}
552
Kevin Wolff17c90b2013-03-15 11:55:29 +0100553static void inet_addr_to_opts(QemuOpts *opts, const InetSocketAddress *addr)
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200554{
555 bool ipv4 = addr->ipv4 || !addr->has_ipv4;
556 bool ipv6 = addr->ipv6 || !addr->has_ipv6;
557
558 if (!ipv4 || !ipv6) {
559 qemu_opt_set_bool(opts, "ipv4", ipv4);
560 qemu_opt_set_bool(opts, "ipv6", ipv6);
561 }
562 if (addr->has_to) {
563 char to[20];
564 snprintf(to, sizeof(to), "%d", addr->to);
565 qemu_opt_set(opts, "to", to);
566 }
567 qemu_opt_set(opts, "host", addr->host);
568 qemu_opt_set(opts, "port", addr->port);
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200569}
570
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200571int inet_listen(const char *str, char *ostr, int olen,
Amos Kong029409e2012-05-11 00:28:26 +0800572 int socktype, int port_offset, Error **errp)
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200573{
574 QemuOpts *opts;
575 char *optstr;
576 int sock = -1;
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200577 InetSocketAddress *addr;
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200578
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200579 addr = inet_parse(str, errp);
580 if (addr != NULL) {
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800581 opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort);
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200582 inet_addr_to_opts(opts, addr);
583 qapi_free_InetSocketAddress(addr);
Amos Kong029409e2012-05-11 00:28:26 +0800584 sock = inet_listen_opts(opts, port_offset, errp);
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200585 if (sock != -1 && ostr) {
586 optstr = strchr(str, ',');
587 if (qemu_opt_get_bool(opts, "ipv6", 0)) {
588 snprintf(ostr, olen, "[%s]:%s%s",
589 qemu_opt_get(opts, "host"),
590 qemu_opt_get(opts, "port"),
591 optstr ? optstr : "");
592 } else {
593 snprintf(ostr, olen, "%s:%s%s",
594 qemu_opt_get(opts, "host"),
595 qemu_opt_get(opts, "port"),
596 optstr ? optstr : "");
597 }
598 }
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200599 qemu_opts_del(opts);
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200600 }
Gerd Hoffmanne5bc7762009-09-10 10:58:41 +0200601 return sock;
602}
603
Orit Wasserman5db5f442012-09-24 13:11:08 +0200604/**
605 * Create a blocking socket and connect it to an address.
606 *
607 * @str: address string
608 * @errp: set in case of an error
609 *
610 * Returns -1 in case of error, file descriptor on success
611 **/
612int inet_connect(const char *str, Error **errp)
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200613{
614 QemuOpts *opts;
615 int sock = -1;
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200616 InetSocketAddress *addr;
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200617
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200618 addr = inet_parse(str, errp);
619 if (addr != NULL) {
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800620 opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort);
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200621 inet_addr_to_opts(opts, addr);
622 qapi_free_InetSocketAddress(addr);
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200623 sock = inet_connect_opts(opts, errp, NULL, NULL);
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200624 qemu_opts_del(opts);
Orit Wasserman5db5f442012-09-24 13:11:08 +0200625 }
Orit Wasserman5db5f442012-09-24 13:11:08 +0200626 return sock;
627}
628
629/**
630 * Create a non-blocking socket and connect it to an address.
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200631 * Calls the callback function with fd in case of success or -1 in case of
632 * error.
Orit Wasserman5db5f442012-09-24 13:11:08 +0200633 *
634 * @str: address string
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200635 * @callback: callback function that is called when connect completes,
636 * cannot be NULL.
637 * @opaque: opaque for callback function
Orit Wasserman5db5f442012-09-24 13:11:08 +0200638 * @errp: set in case of an error
639 *
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200640 * Returns: -1 on immediate error, file descriptor on success.
Orit Wasserman5db5f442012-09-24 13:11:08 +0200641 **/
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200642int inet_nonblocking_connect(const char *str,
643 NonBlockingConnectHandler *callback,
644 void *opaque, Error **errp)
Orit Wasserman5db5f442012-09-24 13:11:08 +0200645{
646 QemuOpts *opts;
647 int sock = -1;
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200648 InetSocketAddress *addr;
Orit Wasserman5db5f442012-09-24 13:11:08 +0200649
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200650 g_assert(callback != NULL);
651
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200652 addr = inet_parse(str, errp);
653 if (addr != NULL) {
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800654 opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort);
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200655 inet_addr_to_opts(opts, addr);
656 qapi_free_InetSocketAddress(addr);
Orit Wasserman233aa5c2012-09-24 13:11:09 +0200657 sock = inet_connect_opts(opts, errp, callback, opaque);
Paolo Bonzini879e45c2012-09-19 13:51:46 +0200658 qemu_opts_del(opts);
Amos Konga6ba35b2012-05-11 00:28:16 +0800659 }
Gerd Hoffmannf4c94c72009-09-10 10:58:40 +0200660 return sock;
661}
662
aliguorid247d252008-11-11 20:46:40 +0000663#ifndef _WIN32
664
Paolo Bonzini7fc4e632012-10-02 09:35:32 +0200665int unix_listen_opts(QemuOpts *opts, Error **errp)
aliguorid247d252008-11-11 20:46:40 +0000666{
667 struct sockaddr_un un;
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200668 const char *path = qemu_opt_get(opts, "path");
669 int sock, fd;
aliguorid247d252008-11-11 20:46:40 +0000670
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100671 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
aliguorid247d252008-11-11 20:46:40 +0000672 if (sock < 0) {
Paolo Bonzini58899662012-09-19 13:54:39 +0200673 error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED);
vibi39b6efc2009-05-06 15:27:03 +0530674 return -1;
aliguorid247d252008-11-11 20:46:40 +0000675 }
676
aliguorid247d252008-11-11 20:46:40 +0000677 memset(&un, 0, sizeof(un));
678 un.sun_family = AF_UNIX;
679 if (path && strlen(path)) {
680 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
681 } else {
682 char *tmpdir = getenv("TMPDIR");
683 snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX",
684 tmpdir ? tmpdir : "/tmp");
685 /*
686 * This dummy fd usage silences the mktemp() unsecure warning.
687 * Using mkstemp() doesn't make things more secure here
688 * though. bind() complains about existing files, so we have
689 * to unlink first and thus re-open the race window. The
690 * worst case possible is bind() failing, i.e. a DoS attack.
691 */
692 fd = mkstemp(un.sun_path); close(fd);
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200693 qemu_opt_set(opts, "path", un.sun_path);
aliguorid247d252008-11-11 20:46:40 +0000694 }
aliguorid247d252008-11-11 20:46:40 +0000695
696 unlink(un.sun_path);
697 if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
Paolo Bonzini58899662012-09-19 13:54:39 +0200698 error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED);
aliguorid247d252008-11-11 20:46:40 +0000699 goto err;
700 }
701 if (listen(sock, 1) < 0) {
Paolo Bonzini58899662012-09-19 13:54:39 +0200702 error_set_errno(errp, errno, QERR_SOCKET_LISTEN_FAILED);
aliguorid247d252008-11-11 20:46:40 +0000703 goto err;
704 }
705
aliguorid247d252008-11-11 20:46:40 +0000706 return sock;
707
708err:
aliguorid247d252008-11-11 20:46:40 +0000709 closesocket(sock);
710 return -1;
711}
712
Paolo Bonzini1fc05ad2012-10-03 13:37:46 +0200713int unix_connect_opts(QemuOpts *opts, Error **errp,
714 NonBlockingConnectHandler *callback, void *opaque)
aliguorid247d252008-11-11 20:46:40 +0000715{
716 struct sockaddr_un un;
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200717 const char *path = qemu_opt_get(opts, "path");
Paolo Bonzini1fc05ad2012-10-03 13:37:46 +0200718 ConnectState *connect_state = NULL;
719 int sock, rc;
aliguorid247d252008-11-11 20:46:40 +0000720
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200721 if (NULL == path) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100722 error_setg(errp, "unix connect: no path specified");
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200723 return -1;
724 }
725
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100726 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
aliguorid247d252008-11-11 20:46:40 +0000727 if (sock < 0) {
Paolo Bonzini58899662012-09-19 13:54:39 +0200728 error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED);
vibi39b6efc2009-05-06 15:27:03 +0530729 return -1;
aliguorid247d252008-11-11 20:46:40 +0000730 }
Paolo Bonzini1fc05ad2012-10-03 13:37:46 +0200731 if (callback != NULL) {
732 connect_state = g_malloc0(sizeof(*connect_state));
733 connect_state->callback = callback;
734 connect_state->opaque = opaque;
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100735 qemu_set_nonblock(sock);
Paolo Bonzini1fc05ad2012-10-03 13:37:46 +0200736 }
aliguorid247d252008-11-11 20:46:40 +0000737
738 memset(&un, 0, sizeof(un));
739 un.sun_family = AF_UNIX;
740 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
Paolo Bonzini1fc05ad2012-10-03 13:37:46 +0200741
742 /* connect to peer */
743 do {
744 rc = 0;
745 if (connect(sock, (struct sockaddr *) &un, sizeof(un)) < 0) {
746 rc = -socket_error();
747 }
748 } while (rc == -EINTR);
749
750 if (connect_state != NULL && QEMU_SOCKET_RC_INPROGRESS(rc)) {
751 connect_state->fd = sock;
752 qemu_set_fd_handler2(sock, NULL, NULL, wait_for_connect,
753 connect_state);
754 return sock;
755 } else if (rc >= 0) {
756 /* non blocking socket immediate success, call callback */
757 if (callback != NULL) {
758 callback(sock, opaque);
759 }
aliguorid247d252008-11-11 20:46:40 +0000760 }
761
Paolo Bonzini1fc05ad2012-10-03 13:37:46 +0200762 if (rc < 0) {
Paolo Bonzini58899662012-09-19 13:54:39 +0200763 error_set_errno(errp, -rc, QERR_SOCKET_CONNECT_FAILED);
Paolo Bonzini1fc05ad2012-10-03 13:37:46 +0200764 close(sock);
765 sock = -1;
766 }
767
768 g_free(connect_state);
aliguorid247d252008-11-11 20:46:40 +0000769 return sock;
770}
771
Paolo Bonzini0c814702012-10-18 08:44:00 +0200772#else
773
774int unix_listen_opts(QemuOpts *opts, Error **errp)
775{
Paolo Bonzini58899662012-09-19 13:54:39 +0200776 error_setg(errp, "unix sockets are not available on windows");
Paolo Bonzini0c814702012-10-18 08:44:00 +0200777 errno = ENOTSUP;
778 return -1;
779}
780
Paolo Bonzini1fc05ad2012-10-03 13:37:46 +0200781int unix_connect_opts(QemuOpts *opts, Error **errp,
782 NonBlockingConnectHandler *callback, void *opaque)
Paolo Bonzini0c814702012-10-18 08:44:00 +0200783{
Paolo Bonzini58899662012-09-19 13:54:39 +0200784 error_setg(errp, "unix sockets are not available on windows");
Paolo Bonzini0c814702012-10-18 08:44:00 +0200785 errno = ENOTSUP;
786 return -1;
787}
788#endif
789
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200790/* compatibility wrapper */
Paolo Bonzini7fc4e632012-10-02 09:35:32 +0200791int unix_listen(const char *str, char *ostr, int olen, Error **errp)
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200792{
793 QemuOpts *opts;
794 char *path, *optstr;
795 int sock, len;
796
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800797 opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort);
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200798
799 optstr = strchr(str, ',');
800 if (optstr) {
801 len = optstr - str;
802 if (len) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500803 path = g_malloc(len+1);
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200804 snprintf(path, len+1, "%.*s", len, str);
805 qemu_opt_set(opts, "path", path);
Anthony Liguori7267c092011-08-20 22:09:37 -0500806 g_free(path);
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200807 }
808 } else {
809 qemu_opt_set(opts, "path", str);
810 }
811
Paolo Bonzini7fc4e632012-10-02 09:35:32 +0200812 sock = unix_listen_opts(opts, errp);
Gerd Hoffmann62b6adf2009-09-10 10:58:38 +0200813
814 if (sock != -1 && ostr)
815 snprintf(ostr, olen, "%s%s", qemu_opt_get(opts, "path"), optstr ? optstr : "");
816 qemu_opts_del(opts);
817 return sock;
818}
819
Paolo Bonzini7fc4e632012-10-02 09:35:32 +0200820int unix_connect(const char *path, Error **errp)
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200821{
822 QemuOpts *opts;
823 int sock;
824
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800825 opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort);
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200826 qemu_opt_set(opts, "path", path);
Paolo Bonzini1fc05ad2012-10-03 13:37:46 +0200827 sock = unix_connect_opts(opts, errp, NULL, NULL);
828 qemu_opts_del(opts);
829 return sock;
830}
831
832
833int unix_nonblocking_connect(const char *path,
834 NonBlockingConnectHandler *callback,
835 void *opaque, Error **errp)
836{
837 QemuOpts *opts;
838 int sock = -1;
839
840 g_assert(callback != NULL);
841
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800842 opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort);
Paolo Bonzini1fc05ad2012-10-03 13:37:46 +0200843 qemu_opt_set(opts, "path", path);
844 sock = unix_connect_opts(opts, errp, callback, opaque);
Gerd Hoffmann2af2bf62009-09-10 10:58:37 +0200845 qemu_opts_del(opts);
846 return sock;
847}
848
Paolo Bonzini101f9cb2012-10-23 21:31:53 +0200849SocketAddress *socket_parse(const char *str, Error **errp)
850{
Gerd Hoffmannafde3f82013-06-24 08:39:44 +0200851 SocketAddress *addr;
Paolo Bonzini101f9cb2012-10-23 21:31:53 +0200852
Gerd Hoffmannafde3f82013-06-24 08:39:44 +0200853 addr = g_new0(SocketAddress, 1);
Paolo Bonzini101f9cb2012-10-23 21:31:53 +0200854 if (strstart(str, "unix:", NULL)) {
855 if (str[5] == '\0') {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100856 error_setg(errp, "invalid Unix socket address");
Paolo Bonzini101f9cb2012-10-23 21:31:53 +0200857 goto fail;
858 } else {
859 addr->kind = SOCKET_ADDRESS_KIND_UNIX;
860 addr->q_unix = g_new(UnixSocketAddress, 1);
861 addr->q_unix->path = g_strdup(str + 5);
862 }
863 } else if (strstart(str, "fd:", NULL)) {
864 if (str[3] == '\0') {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100865 error_setg(errp, "invalid file descriptor address");
Paolo Bonzini101f9cb2012-10-23 21:31:53 +0200866 goto fail;
867 } else {
868 addr->kind = SOCKET_ADDRESS_KIND_FD;
869 addr->fd = g_new(String, 1);
870 addr->fd->str = g_strdup(str + 3);
871 }
872 } else {
873 addr->kind = SOCKET_ADDRESS_KIND_INET;
Paolo Bonzini101f9cb2012-10-23 21:31:53 +0200874 addr->inet = inet_parse(str, errp);
875 if (addr->inet == NULL) {
876 goto fail;
877 }
878 }
879 return addr;
880
881fail:
882 qapi_free_SocketAddress(addr);
883 return NULL;
884}
885
886int socket_connect(SocketAddress *addr, Error **errp,
887 NonBlockingConnectHandler *callback, void *opaque)
888{
889 QemuOpts *opts;
890 int fd;
891
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800892 opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort);
Paolo Bonzini101f9cb2012-10-23 21:31:53 +0200893 switch (addr->kind) {
894 case SOCKET_ADDRESS_KIND_INET:
895 inet_addr_to_opts(opts, addr->inet);
896 fd = inet_connect_opts(opts, errp, callback, opaque);
897 break;
898
899 case SOCKET_ADDRESS_KIND_UNIX:
900 qemu_opt_set(opts, "path", addr->q_unix->path);
901 fd = unix_connect_opts(opts, errp, callback, opaque);
902 break;
903
904 case SOCKET_ADDRESS_KIND_FD:
905 fd = monitor_get_fd(cur_mon, addr->fd->str, errp);
Gerd Hoffmannd1ec72a2013-06-24 08:39:46 +0200906 if (fd >= 0 && callback) {
Stefan Hajnoczi1a751eb2013-03-27 10:10:45 +0100907 qemu_set_nonblock(fd);
Paolo Bonzini101f9cb2012-10-23 21:31:53 +0200908 callback(fd, opaque);
909 }
910 break;
911
912 default:
913 abort();
914 }
915 qemu_opts_del(opts);
916 return fd;
917}
918
919int socket_listen(SocketAddress *addr, Error **errp)
920{
921 QemuOpts *opts;
922 int fd;
923
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800924 opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort);
Paolo Bonzini101f9cb2012-10-23 21:31:53 +0200925 switch (addr->kind) {
926 case SOCKET_ADDRESS_KIND_INET:
927 inet_addr_to_opts(opts, addr->inet);
928 fd = inet_listen_opts(opts, 0, errp);
929 break;
930
931 case SOCKET_ADDRESS_KIND_UNIX:
932 qemu_opt_set(opts, "path", addr->q_unix->path);
933 fd = unix_listen_opts(opts, errp);
934 break;
935
936 case SOCKET_ADDRESS_KIND_FD:
937 fd = monitor_get_fd(cur_mon, addr->fd->str, errp);
938 break;
939
940 default:
941 abort();
942 }
943 qemu_opts_del(opts);
944 return fd;
945}
946
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +0100947int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp)
948{
949 QemuOpts *opts;
950 int fd;
951
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800952 opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort);
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +0100953 switch (remote->kind) {
954 case SOCKET_ADDRESS_KIND_INET:
955 qemu_opt_set(opts, "host", remote->inet->host);
956 qemu_opt_set(opts, "port", remote->inet->port);
957 if (local) {
958 qemu_opt_set(opts, "localaddr", local->inet->host);
959 qemu_opt_set(opts, "localport", local->inet->port);
960 }
961 fd = inet_dgram_opts(opts, errp);
962 break;
963
964 default:
965 error_setg(errp, "socket type unsupported for datagram");
Gerd Hoffmann7a5b6af2013-06-24 08:39:56 +0200966 fd = -1;
Gerd Hoffmann3ecc0592013-02-27 14:10:47 +0100967 }
968 qemu_opts_del(opts);
969 return fd;
970}