root / qemu-sockets.c @ a12fb8ad
History | View | Annotate | Download (23.3 kB)
1 |
/*
|
---|---|
2 |
* inet and unix socket functions for qemu
|
3 |
*
|
4 |
* (c) 2008 Gerd Hoffmann <kraxel@redhat.com>
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or modify
|
7 |
* it under the terms of the GNU General Public License as published by
|
8 |
* the Free Software Foundation; under version 2 of the License.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
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.
|
17 |
*/
|
18 |
#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" |
26 |
#include "qemu-common.h" /* for qemu_isdigit */ |
27 |
#include "main-loop.h" |
28 |
|
29 |
#ifndef AI_ADDRCONFIG
|
30 |
# define AI_ADDRCONFIG 0 |
31 |
#endif
|
32 |
|
33 |
static const int on=1, off=0; |
34 |
|
35 |
/* used temporarely until all users are converted to QemuOpts */
|
36 |
static QemuOptsList dummy_opts = {
|
37 |
.name = "dummy",
|
38 |
.head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head), |
39 |
.desc = { |
40 |
{ |
41 |
.name = "path",
|
42 |
.type = QEMU_OPT_STRING, |
43 |
},{ |
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, |
58 |
}, |
59 |
{ /* end if list */ }
|
60 |
}, |
61 |
}; |
62 |
|
63 |
static 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 |
|
80 |
static 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 |
|
97 |
const char *inet_strfamily(int family) |
98 |
{ |
99 |
switch (family) {
|
100 |
case PF_INET6: return "ipv6"; |
101 |
case PF_INET: return "ipv4"; |
102 |
case PF_UNIX: return "unix"; |
103 |
} |
104 |
return "unknown"; |
105 |
} |
106 |
|
107 |
int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp) |
108 |
{ |
109 |
struct addrinfo ai,*res,*e;
|
110 |
const char *addr; |
111 |
char port[33]; |
112 |
char uaddr[INET6_ADDRSTRLEN+1]; |
113 |
char uport[33]; |
114 |
int slisten, rc, to, port_min, port_max, p;
|
115 |
|
116 |
memset(&ai,0, sizeof(ai)); |
117 |
ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; |
118 |
ai.ai_family = PF_UNSPEC; |
119 |
ai.ai_socktype = SOCK_STREAM; |
120 |
|
121 |
if ((qemu_opt_get(opts, "host") == NULL) || |
122 |
(qemu_opt_get(opts, "port") == NULL)) { |
123 |
error_setg(errp, "host and/or port not specified");
|
124 |
return -1; |
125 |
} |
126 |
pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port")); |
127 |
addr = qemu_opt_get(opts, "host");
|
128 |
|
129 |
to = qemu_opt_get_number(opts, "to", 0); |
130 |
if (qemu_opt_get_bool(opts, "ipv4", 0)) |
131 |
ai.ai_family = PF_INET; |
132 |
if (qemu_opt_get_bool(opts, "ipv6", 0)) |
133 |
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) { |
140 |
error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
|
141 |
gai_strerror(rc)); |
142 |
return -1; |
143 |
} |
144 |
|
145 |
/* create socket + bind */
|
146 |
for (e = res; e != NULL; e = e->ai_next) { |
147 |
getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
|
148 |
uaddr,INET6_ADDRSTRLEN,uport,32,
|
149 |
NI_NUMERICHOST | NI_NUMERICSERV); |
150 |
slisten = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol); |
151 |
if (slisten < 0) { |
152 |
if (!e->ai_next) {
|
153 |
error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED); |
154 |
} |
155 |
continue;
|
156 |
} |
157 |
|
158 |
setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); |
159 |
#ifdef IPV6_V6ONLY
|
160 |
if (e->ai_family == PF_INET6) {
|
161 |
/* listen on both ipv4 and ipv6 */
|
162 |
setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,
|
163 |
sizeof(off));
|
164 |
} |
165 |
#endif
|
166 |
|
167 |
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); |
171 |
if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) { |
172 |
goto listen;
|
173 |
} |
174 |
if (p == port_max) {
|
175 |
if (!e->ai_next) {
|
176 |
error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED); |
177 |
} |
178 |
} |
179 |
} |
180 |
closesocket(slisten); |
181 |
} |
182 |
freeaddrinfo(res); |
183 |
return -1; |
184 |
|
185 |
listen:
|
186 |
if (listen(slisten,1) != 0) { |
187 |
error_set_errno(errp, errno, QERR_SOCKET_LISTEN_FAILED); |
188 |
closesocket(slisten); |
189 |
freeaddrinfo(res); |
190 |
return -1; |
191 |
} |
192 |
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"); |
197 |
freeaddrinfo(res); |
198 |
return slisten;
|
199 |
} |
200 |
|
201 |
#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
|
208 |
|
209 |
/* Struct to store connect state for non blocking connect */
|
210 |
typedef struct ConnectState { |
211 |
int fd;
|
212 |
struct addrinfo *addr_list;
|
213 |
struct addrinfo *current_addr;
|
214 |
NonBlockingConnectHandler *callback; |
215 |
void *opaque;
|
216 |
} ConnectState; |
217 |
|
218 |
static int inet_connect_addr(struct addrinfo *addr, bool *in_progress, |
219 |
ConnectState *connect_state); |
220 |
|
221 |
static 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 {
|
231 |
rc = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, (void *) &val, &valsize);
|
232 |
} 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 */
|
246 |
if (s->current_addr) {
|
247 |
while (s->current_addr->ai_next != NULL && s->fd < 0) { |
248 |
s->current_addr = s->current_addr->ai_next; |
249 |
s->fd = inet_connect_addr(s->current_addr, &in_progress, s); |
250 |
/* connect in progress */
|
251 |
if (in_progress) {
|
252 |
return;
|
253 |
} |
254 |
} |
255 |
|
256 |
freeaddrinfo(s->addr_list); |
257 |
} |
258 |
|
259 |
if (s->callback) {
|
260 |
s->callback(s->fd, s->opaque); |
261 |
} |
262 |
g_free(s); |
263 |
} |
264 |
|
265 |
static int inet_connect_addr(struct addrinfo *addr, bool *in_progress, |
266 |
ConnectState *connect_state) |
267 |
{ |
268 |
int sock, rc;
|
269 |
|
270 |
*in_progress = false;
|
271 |
|
272 |
sock = qemu_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); |
273 |
if (sock < 0) { |
274 |
fprintf(stderr, "%s: socket(%s): %s\n", __func__,
|
275 |
inet_strfamily(addr->ai_family), strerror(errno)); |
276 |
return -1; |
277 |
} |
278 |
qemu_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
|
279 |
if (connect_state != NULL) { |
280 |
socket_set_nonblock(sock); |
281 |
} |
282 |
/* connect to peer */
|
283 |
do {
|
284 |
rc = 0;
|
285 |
if (connect(sock, addr->ai_addr, addr->ai_addrlen) < 0) { |
286 |
rc = -socket_error(); |
287 |
} |
288 |
} while (rc == -EINTR);
|
289 |
|
290 |
if (connect_state != NULL && QEMU_SOCKET_RC_INPROGRESS(rc)) { |
291 |
connect_state->fd = sock; |
292 |
qemu_set_fd_handler2(sock, NULL, NULL, wait_for_connect, |
293 |
connect_state); |
294 |
*in_progress = true;
|
295 |
} else if (rc < 0) { |
296 |
closesocket(sock); |
297 |
return -1; |
298 |
} |
299 |
return sock;
|
300 |
} |
301 |
|
302 |
static 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)); |
310 |
|
311 |
ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG; |
312 |
ai.ai_family = PF_UNSPEC; |
313 |
ai.ai_socktype = SOCK_STREAM; |
314 |
|
315 |
addr = qemu_opt_get(opts, "host");
|
316 |
port = qemu_opt_get(opts, "port");
|
317 |
if (addr == NULL || port == NULL) { |
318 |
error_setg(errp, "host and/or port not specified");
|
319 |
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) { |
332 |
error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
|
333 |
gai_strerror(rc)); |
334 |
return NULL; |
335 |
} |
336 |
return res;
|
337 |
} |
338 |
|
339 |
/**
|
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".
|
344 |
* @errp: set on error
|
345 |
* @callback: callback function for non-blocking connect
|
346 |
* @opaque: opaque for callback function
|
347 |
*
|
348 |
* Returns: -1 on error, file descriptor on success.
|
349 |
*
|
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.
|
353 |
*/
|
354 |
int inet_connect_opts(QemuOpts *opts, Error **errp,
|
355 |
NonBlockingConnectHandler *callback, void *opaque)
|
356 |
{ |
357 |
struct addrinfo *res, *e;
|
358 |
int sock = -1; |
359 |
bool in_progress;
|
360 |
ConnectState *connect_state = NULL;
|
361 |
|
362 |
res = inet_parse_connect_opts(opts, errp); |
363 |
if (!res) {
|
364 |
return -1; |
365 |
} |
366 |
|
367 |
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; |
372 |
} |
373 |
|
374 |
for (e = res; e != NULL; e = e->ai_next) { |
375 |
if (connect_state != NULL) { |
376 |
connect_state->current_addr = e; |
377 |
} |
378 |
sock = inet_connect_addr(e, &in_progress, connect_state); |
379 |
if (in_progress) {
|
380 |
return sock;
|
381 |
} else if (sock >= 0) { |
382 |
/* non blocking socket immediate success, call callback */
|
383 |
if (callback != NULL) { |
384 |
callback(sock, opaque); |
385 |
} |
386 |
break;
|
387 |
} |
388 |
} |
389 |
if (sock < 0) { |
390 |
error_set(errp, QERR_SOCKET_CONNECT_FAILED); |
391 |
} |
392 |
g_free(connect_state); |
393 |
freeaddrinfo(res); |
394 |
return sock;
|
395 |
} |
396 |
|
397 |
int inet_dgram_opts(QemuOpts *opts, Error **errp)
|
398 |
{ |
399 |
struct addrinfo ai, *peer = NULL, *local = NULL; |
400 |
const char *addr; |
401 |
const char *port; |
402 |
char uaddr[INET6_ADDRSTRLEN+1]; |
403 |
char uport[33]; |
404 |
int sock = -1, rc; |
405 |
|
406 |
/* lookup peer addr */
|
407 |
memset(&ai,0, sizeof(ai)); |
408 |
ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG; |
409 |
ai.ai_family = PF_UNSPEC; |
410 |
ai.ai_socktype = SOCK_DGRAM; |
411 |
|
412 |
addr = qemu_opt_get(opts, "host");
|
413 |
port = qemu_opt_get(opts, "port");
|
414 |
if (addr == NULL || strlen(addr) == 0) { |
415 |
addr = "localhost";
|
416 |
} |
417 |
if (port == NULL || strlen(port) == 0) { |
418 |
fprintf(stderr, "inet_dgram: port not specified\n");
|
419 |
return -1; |
420 |
} |
421 |
|
422 |
if (qemu_opt_get_bool(opts, "ipv4", 0)) |
423 |
ai.ai_family = PF_INET; |
424 |
if (qemu_opt_get_bool(opts, "ipv6", 0)) |
425 |
ai.ai_family = PF_INET6; |
426 |
|
427 |
if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) { |
428 |
fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
|
429 |
gai_strerror(rc)); |
430 |
return -1; |
431 |
} |
432 |
|
433 |
/* lookup local addr */
|
434 |
memset(&ai,0, sizeof(ai)); |
435 |
ai.ai_flags = AI_PASSIVE; |
436 |
ai.ai_family = peer->ai_family; |
437 |
ai.ai_socktype = SOCK_DGRAM; |
438 |
|
439 |
addr = qemu_opt_get(opts, "localaddr");
|
440 |
port = qemu_opt_get(opts, "localport");
|
441 |
if (addr == NULL || strlen(addr) == 0) { |
442 |
addr = NULL;
|
443 |
} |
444 |
if (!port || strlen(port) == 0) |
445 |
port = "0";
|
446 |
|
447 |
if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) { |
448 |
fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
|
449 |
gai_strerror(rc)); |
450 |
goto err;
|
451 |
} |
452 |
|
453 |
/* create socket */
|
454 |
sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol); |
455 |
if (sock < 0) { |
456 |
fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
|
457 |
inet_strfamily(peer->ai_family), strerror(errno)); |
458 |
goto err;
|
459 |
} |
460 |
setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); |
461 |
|
462 |
/* bind socket */
|
463 |
if (getnameinfo((struct sockaddr*)local->ai_addr,local->ai_addrlen, |
464 |
uaddr,INET6_ADDRSTRLEN,uport,32,
|
465 |
NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
|
466 |
fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
|
467 |
goto err;
|
468 |
} |
469 |
if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) { |
470 |
fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,
|
471 |
inet_strfamily(local->ai_family), uaddr, inet_getport(local)); |
472 |
goto err;
|
473 |
} |
474 |
|
475 |
/* connect to peer */
|
476 |
if (getnameinfo((struct sockaddr*)peer->ai_addr, peer->ai_addrlen, |
477 |
uaddr, INET6_ADDRSTRLEN, uport, 32,
|
478 |
NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
|
479 |
fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
|
480 |
goto err;
|
481 |
} |
482 |
if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) { |
483 |
fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
|
484 |
inet_strfamily(peer->ai_family), |
485 |
peer->ai_canonname, uaddr, uport, strerror(errno)); |
486 |
goto err;
|
487 |
} |
488 |
|
489 |
freeaddrinfo(local); |
490 |
freeaddrinfo(peer); |
491 |
return sock;
|
492 |
|
493 |
err:
|
494 |
if (-1 != sock) |
495 |
closesocket(sock); |
496 |
if (local)
|
497 |
freeaddrinfo(local); |
498 |
if (peer)
|
499 |
freeaddrinfo(peer); |
500 |
return -1; |
501 |
} |
502 |
|
503 |
/* compatibility wrapper */
|
504 |
static int inet_parse(QemuOpts *opts, const char *str) |
505 |
{ |
506 |
const char *optstr, *h; |
507 |
char addr[64]; |
508 |
char port[33]; |
509 |
int pos;
|
510 |
|
511 |
/* parse address */
|
512 |
if (str[0] == ':') { |
513 |
/* no host given */
|
514 |
addr[0] = '\0'; |
515 |
if (1 != sscanf(str,":%32[^,]%n",port,&pos)) { |
516 |
fprintf(stderr, "%s: portonly parse error (%s)\n",
|
517 |
__FUNCTION__, str); |
518 |
return -1; |
519 |
} |
520 |
} else if (str[0] == '[') { |
521 |
/* IPv6 addr */
|
522 |
if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) { |
523 |
fprintf(stderr, "%s: ipv6 parse error (%s)\n",
|
524 |
__FUNCTION__, str); |
525 |
return -1; |
526 |
} |
527 |
qemu_opt_set(opts, "ipv6", "on"); |
528 |
} else if (qemu_isdigit(str[0])) { |
529 |
/* IPv4 addr */
|
530 |
if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) { |
531 |
fprintf(stderr, "%s: ipv4 parse error (%s)\n",
|
532 |
__FUNCTION__, str); |
533 |
return -1; |
534 |
} |
535 |
qemu_opt_set(opts, "ipv4", "on"); |
536 |
} else {
|
537 |
/* hostname */
|
538 |
if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) { |
539 |
fprintf(stderr, "%s: hostname parse error (%s)\n",
|
540 |
__FUNCTION__, str); |
541 |
return -1; |
542 |
} |
543 |
} |
544 |
qemu_opt_set(opts, "host", addr);
|
545 |
qemu_opt_set(opts, "port", port);
|
546 |
|
547 |
/* parse options */
|
548 |
optstr = str + pos; |
549 |
h = strstr(optstr, ",to=");
|
550 |
if (h)
|
551 |
qemu_opt_set(opts, "to", h+4); |
552 |
if (strstr(optstr, ",ipv4")) |
553 |
qemu_opt_set(opts, "ipv4", "on"); |
554 |
if (strstr(optstr, ",ipv6")) |
555 |
qemu_opt_set(opts, "ipv6", "on"); |
556 |
return 0; |
557 |
} |
558 |
|
559 |
int inet_listen(const char *str, char *ostr, int olen, |
560 |
int socktype, int port_offset, Error **errp) |
561 |
{ |
562 |
QemuOpts *opts; |
563 |
char *optstr;
|
564 |
int sock = -1; |
565 |
|
566 |
opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); |
567 |
if (inet_parse(opts, str) == 0) { |
568 |
sock = inet_listen_opts(opts, port_offset, errp); |
569 |
if (sock != -1 && ostr) { |
570 |
optstr = strchr(str, ',');
|
571 |
if (qemu_opt_get_bool(opts, "ipv6", 0)) { |
572 |
snprintf(ostr, olen, "[%s]:%s%s",
|
573 |
qemu_opt_get(opts, "host"),
|
574 |
qemu_opt_get(opts, "port"),
|
575 |
optstr ? optstr : "");
|
576 |
} else {
|
577 |
snprintf(ostr, olen, "%s:%s%s",
|
578 |
qemu_opt_get(opts, "host"),
|
579 |
qemu_opt_get(opts, "port"),
|
580 |
optstr ? optstr : "");
|
581 |
} |
582 |
} |
583 |
} else {
|
584 |
error_set(errp, QERR_SOCKET_CREATE_FAILED); |
585 |
} |
586 |
qemu_opts_del(opts); |
587 |
return sock;
|
588 |
} |
589 |
|
590 |
/**
|
591 |
* Create a blocking socket and connect it to an address.
|
592 |
*
|
593 |
* @str: address string
|
594 |
* @errp: set in case of an error
|
595 |
*
|
596 |
* Returns -1 in case of error, file descriptor on success
|
597 |
**/
|
598 |
int inet_connect(const char *str, Error **errp) |
599 |
{ |
600 |
QemuOpts *opts; |
601 |
int sock = -1; |
602 |
|
603 |
opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); |
604 |
if (inet_parse(opts, str) == 0) { |
605 |
sock = inet_connect_opts(opts, errp, NULL, NULL); |
606 |
} else {
|
607 |
error_set(errp, QERR_SOCKET_CREATE_FAILED); |
608 |
} |
609 |
qemu_opts_del(opts); |
610 |
return sock;
|
611 |
} |
612 |
|
613 |
/**
|
614 |
* Create a non-blocking socket and connect it to an address.
|
615 |
* Calls the callback function with fd in case of success or -1 in case of
|
616 |
* error.
|
617 |
*
|
618 |
* @str: address string
|
619 |
* @callback: callback function that is called when connect completes,
|
620 |
* cannot be NULL.
|
621 |
* @opaque: opaque for callback function
|
622 |
* @errp: set in case of an error
|
623 |
*
|
624 |
* Returns: -1 on immediate error, file descriptor on success.
|
625 |
**/
|
626 |
int inet_nonblocking_connect(const char *str, |
627 |
NonBlockingConnectHandler *callback, |
628 |
void *opaque, Error **errp)
|
629 |
{ |
630 |
QemuOpts *opts; |
631 |
int sock = -1; |
632 |
|
633 |
g_assert(callback != NULL);
|
634 |
|
635 |
opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); |
636 |
if (inet_parse(opts, str) == 0) { |
637 |
sock = inet_connect_opts(opts, errp, callback, opaque); |
638 |
} else {
|
639 |
error_set(errp, QERR_SOCKET_CREATE_FAILED); |
640 |
} |
641 |
qemu_opts_del(opts); |
642 |
return sock;
|
643 |
} |
644 |
|
645 |
#ifndef _WIN32
|
646 |
|
647 |
int unix_listen_opts(QemuOpts *opts, Error **errp)
|
648 |
{ |
649 |
struct sockaddr_un un;
|
650 |
const char *path = qemu_opt_get(opts, "path"); |
651 |
int sock, fd;
|
652 |
|
653 |
sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
|
654 |
if (sock < 0) { |
655 |
perror("socket(unix)");
|
656 |
return -1; |
657 |
} |
658 |
|
659 |
memset(&un, 0, sizeof(un)); |
660 |
un.sun_family = AF_UNIX; |
661 |
if (path && strlen(path)) {
|
662 |
snprintf(un.sun_path, sizeof(un.sun_path), "%s", path); |
663 |
} else {
|
664 |
char *tmpdir = getenv("TMPDIR"); |
665 |
snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX", |
666 |
tmpdir ? tmpdir : "/tmp");
|
667 |
/*
|
668 |
* This dummy fd usage silences the mktemp() unsecure warning.
|
669 |
* Using mkstemp() doesn't make things more secure here
|
670 |
* though. bind() complains about existing files, so we have
|
671 |
* to unlink first and thus re-open the race window. The
|
672 |
* worst case possible is bind() failing, i.e. a DoS attack.
|
673 |
*/
|
674 |
fd = mkstemp(un.sun_path); close(fd); |
675 |
qemu_opt_set(opts, "path", un.sun_path);
|
676 |
} |
677 |
|
678 |
unlink(un.sun_path); |
679 |
if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) { |
680 |
fprintf(stderr, "bind(unix:%s): %s\n", un.sun_path, strerror(errno));
|
681 |
goto err;
|
682 |
} |
683 |
if (listen(sock, 1) < 0) { |
684 |
fprintf(stderr, "listen(unix:%s): %s\n", un.sun_path, strerror(errno));
|
685 |
goto err;
|
686 |
} |
687 |
|
688 |
return sock;
|
689 |
|
690 |
err:
|
691 |
closesocket(sock); |
692 |
return -1; |
693 |
} |
694 |
|
695 |
int unix_connect_opts(QemuOpts *opts, Error **errp,
|
696 |
NonBlockingConnectHandler *callback, void *opaque)
|
697 |
{ |
698 |
struct sockaddr_un un;
|
699 |
const char *path = qemu_opt_get(opts, "path"); |
700 |
ConnectState *connect_state = NULL;
|
701 |
int sock, rc;
|
702 |
|
703 |
if (NULL == path) { |
704 |
fprintf(stderr, "unix connect: no path specified\n");
|
705 |
return -1; |
706 |
} |
707 |
|
708 |
sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
|
709 |
if (sock < 0) { |
710 |
perror("socket(unix)");
|
711 |
return -1; |
712 |
} |
713 |
if (callback != NULL) { |
714 |
connect_state = g_malloc0(sizeof(*connect_state));
|
715 |
connect_state->callback = callback; |
716 |
connect_state->opaque = opaque; |
717 |
socket_set_nonblock(sock); |
718 |
} |
719 |
|
720 |
memset(&un, 0, sizeof(un)); |
721 |
un.sun_family = AF_UNIX; |
722 |
snprintf(un.sun_path, sizeof(un.sun_path), "%s", path); |
723 |
|
724 |
/* connect to peer */
|
725 |
do {
|
726 |
rc = 0;
|
727 |
if (connect(sock, (struct sockaddr *) &un, sizeof(un)) < 0) { |
728 |
rc = -socket_error(); |
729 |
} |
730 |
} while (rc == -EINTR);
|
731 |
|
732 |
if (connect_state != NULL && QEMU_SOCKET_RC_INPROGRESS(rc)) { |
733 |
connect_state->fd = sock; |
734 |
qemu_set_fd_handler2(sock, NULL, NULL, wait_for_connect, |
735 |
connect_state); |
736 |
return sock;
|
737 |
} else if (rc >= 0) { |
738 |
/* non blocking socket immediate success, call callback */
|
739 |
if (callback != NULL) { |
740 |
callback(sock, opaque); |
741 |
} |
742 |
} |
743 |
|
744 |
if (rc < 0) { |
745 |
fprintf(stderr, "connect(unix:%s): %s\n", path, strerror(errno));
|
746 |
close(sock); |
747 |
sock = -1;
|
748 |
} |
749 |
|
750 |
g_free(connect_state); |
751 |
return sock;
|
752 |
} |
753 |
|
754 |
#else
|
755 |
|
756 |
int unix_listen_opts(QemuOpts *opts, Error **errp)
|
757 |
{ |
758 |
fprintf(stderr, "unix sockets are not available on windows\n");
|
759 |
errno = ENOTSUP; |
760 |
return -1; |
761 |
} |
762 |
|
763 |
int unix_connect_opts(QemuOpts *opts, Error **errp,
|
764 |
NonBlockingConnectHandler *callback, void *opaque)
|
765 |
{ |
766 |
fprintf(stderr, "unix sockets are not available on windows\n");
|
767 |
errno = ENOTSUP; |
768 |
return -1; |
769 |
} |
770 |
#endif
|
771 |
|
772 |
/* compatibility wrapper */
|
773 |
int unix_listen(const char *str, char *ostr, int olen, Error **errp) |
774 |
{ |
775 |
QemuOpts *opts; |
776 |
char *path, *optstr;
|
777 |
int sock, len;
|
778 |
|
779 |
opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); |
780 |
|
781 |
optstr = strchr(str, ',');
|
782 |
if (optstr) {
|
783 |
len = optstr - str; |
784 |
if (len) {
|
785 |
path = g_malloc(len+1);
|
786 |
snprintf(path, len+1, "%.*s", len, str); |
787 |
qemu_opt_set(opts, "path", path);
|
788 |
g_free(path); |
789 |
} |
790 |
} else {
|
791 |
qemu_opt_set(opts, "path", str);
|
792 |
} |
793 |
|
794 |
sock = unix_listen_opts(opts, errp); |
795 |
|
796 |
if (sock != -1 && ostr) |
797 |
snprintf(ostr, olen, "%s%s", qemu_opt_get(opts, "path"), optstr ? optstr : ""); |
798 |
qemu_opts_del(opts); |
799 |
return sock;
|
800 |
} |
801 |
|
802 |
int unix_connect(const char *path, Error **errp) |
803 |
{ |
804 |
QemuOpts *opts; |
805 |
int sock;
|
806 |
|
807 |
opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); |
808 |
qemu_opt_set(opts, "path", path);
|
809 |
sock = unix_connect_opts(opts, errp, NULL, NULL); |
810 |
qemu_opts_del(opts); |
811 |
return sock;
|
812 |
} |
813 |
|
814 |
|
815 |
int unix_nonblocking_connect(const char *path, |
816 |
NonBlockingConnectHandler *callback, |
817 |
void *opaque, Error **errp)
|
818 |
{ |
819 |
QemuOpts *opts; |
820 |
int sock = -1; |
821 |
|
822 |
g_assert(callback != NULL);
|
823 |
|
824 |
opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); |
825 |
qemu_opt_set(opts, "path", path);
|
826 |
sock = unix_connect_opts(opts, errp, callback, opaque); |
827 |
qemu_opts_del(opts); |
828 |
return sock;
|
829 |
} |
830 |
|
831 |
#ifdef _WIN32
|
832 |
static void socket_cleanup(void) |
833 |
{ |
834 |
WSACleanup(); |
835 |
} |
836 |
#endif
|
837 |
|
838 |
int socket_init(void) |
839 |
{ |
840 |
#ifdef _WIN32
|
841 |
WSADATA Data; |
842 |
int ret, err;
|
843 |
|
844 |
ret = WSAStartup(MAKEWORD(2,2), &Data); |
845 |
if (ret != 0) { |
846 |
err = WSAGetLastError(); |
847 |
fprintf(stderr, "WSAStartup: %d\n", err);
|
848 |
return -1; |
849 |
} |
850 |
atexit(socket_cleanup); |
851 |
#endif
|
852 |
return 0; |
853 |
} |