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