]>
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 */ |
d247d25f AL |
27 | |
28 | #ifndef AI_ADDRCONFIG | |
29 | # define AI_ADDRCONFIG 0 | |
30 | #endif | |
31 | ||
d247d25f AL |
32 | static const int on=1, off=0; |
33 | ||
2af2bf67 | 34 | /* used temporarely until all users are converted to QemuOpts */ |
c1390903 | 35 | static QemuOptsList dummy_opts = { |
2af2bf67 | 36 | .name = "dummy", |
72cf2d4f | 37 | .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head), |
2af2bf67 GH |
38 | .desc = { |
39 | { | |
40 | .name = "path", | |
41 | .type = QEMU_OPT_STRING, | |
f4c94c7c GH |
42 | },{ |
43 | .name = "host", | |
44 | .type = QEMU_OPT_STRING, | |
45 | },{ | |
46 | .name = "port", | |
47 | .type = QEMU_OPT_STRING, | |
48 | },{ | |
49 | .name = "to", | |
50 | .type = QEMU_OPT_NUMBER, | |
51 | },{ | |
52 | .name = "ipv4", | |
53 | .type = QEMU_OPT_BOOL, | |
54 | },{ | |
55 | .name = "ipv6", | |
56 | .type = QEMU_OPT_BOOL, | |
a6ba35b3 AK |
57 | },{ |
58 | .name = "block", | |
59 | .type = QEMU_OPT_BOOL, | |
2af2bf67 GH |
60 | }, |
61 | { /* end if list */ } | |
62 | }, | |
63 | }; | |
64 | ||
d247d25f AL |
65 | static int inet_getport(struct addrinfo *e) |
66 | { | |
67 | struct sockaddr_in *i4; | |
68 | struct sockaddr_in6 *i6; | |
69 | ||
70 | switch (e->ai_family) { | |
71 | case PF_INET6: | |
72 | i6 = (void*)e->ai_addr; | |
73 | return ntohs(i6->sin6_port); | |
74 | case PF_INET: | |
75 | i4 = (void*)e->ai_addr; | |
76 | return ntohs(i4->sin_port); | |
77 | default: | |
78 | return 0; | |
79 | } | |
80 | } | |
81 | ||
82 | static void inet_setport(struct addrinfo *e, int port) | |
83 | { | |
84 | struct sockaddr_in *i4; | |
85 | struct sockaddr_in6 *i6; | |
86 | ||
87 | switch (e->ai_family) { | |
88 | case PF_INET6: | |
89 | i6 = (void*)e->ai_addr; | |
90 | i6->sin6_port = htons(port); | |
91 | break; | |
92 | case PF_INET: | |
93 | i4 = (void*)e->ai_addr; | |
94 | i4->sin_port = htons(port); | |
95 | break; | |
96 | } | |
97 | } | |
98 | ||
c9c4b34e | 99 | const char *inet_strfamily(int family) |
d247d25f AL |
100 | { |
101 | switch (family) { | |
102 | case PF_INET6: return "ipv6"; | |
103 | case PF_INET: return "ipv4"; | |
104 | case PF_UNIX: return "unix"; | |
105 | } | |
c445321e | 106 | return "unknown"; |
d247d25f AL |
107 | } |
108 | ||
029409e5 | 109 | int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp) |
d247d25f AL |
110 | { |
111 | struct addrinfo ai,*res,*e; | |
e5bc776f | 112 | const char *addr; |
d247d25f AL |
113 | char port[33]; |
114 | char uaddr[INET6_ADDRSTRLEN+1]; | |
115 | char uport[33]; | |
877691f9 | 116 | int slisten, rc, to, port_min, port_max, p; |
d247d25f AL |
117 | |
118 | memset(&ai,0, sizeof(ai)); | |
119 | ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; | |
120 | ai.ai_family = PF_UNSPEC; | |
e5bc776f | 121 | ai.ai_socktype = SOCK_STREAM; |
d247d25f | 122 | |
e23a22e6 JO |
123 | if ((qemu_opt_get(opts, "host") == NULL) || |
124 | (qemu_opt_get(opts, "port") == NULL)) { | |
e5bc776f | 125 | fprintf(stderr, "%s: host and/or port not specified\n", __FUNCTION__); |
029409e5 | 126 | error_set(errp, QERR_SOCKET_CREATE_FAILED); |
e5bc776f | 127 | return -1; |
d247d25f | 128 | } |
e5bc776f GH |
129 | pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port")); |
130 | addr = qemu_opt_get(opts, "host"); | |
d247d25f | 131 | |
e5bc776f GH |
132 | to = qemu_opt_get_number(opts, "to", 0); |
133 | if (qemu_opt_get_bool(opts, "ipv4", 0)) | |
d247d25f | 134 | ai.ai_family = PF_INET; |
e5bc776f | 135 | if (qemu_opt_get_bool(opts, "ipv6", 0)) |
d247d25f AL |
136 | ai.ai_family = PF_INET6; |
137 | ||
138 | /* lookup */ | |
139 | if (port_offset) | |
140 | snprintf(port, sizeof(port), "%d", atoi(port) + port_offset); | |
141 | rc = getaddrinfo(strlen(addr) ? addr : NULL, port, &ai, &res); | |
142 | if (rc != 0) { | |
e5bc776f GH |
143 | fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port, |
144 | gai_strerror(rc)); | |
029409e5 | 145 | error_set(errp, QERR_SOCKET_CREATE_FAILED); |
d247d25f AL |
146 | return -1; |
147 | } | |
d247d25f AL |
148 | |
149 | /* create socket + bind */ | |
150 | for (e = res; e != NULL; e = e->ai_next) { | |
39b6efc8 | 151 | getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen, |
152 | uaddr,INET6_ADDRSTRLEN,uport,32, | |
153 | NI_NUMERICHOST | NI_NUMERICSERV); | |
40ff6d7e | 154 | slisten = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol); |
39b6efc8 | 155 | if (slisten < 0) { |
d247d25f AL |
156 | fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__, |
157 | inet_strfamily(e->ai_family), strerror(errno)); | |
029409e5 AK |
158 | if (!e->ai_next) { |
159 | error_set(errp, QERR_SOCKET_CREATE_FAILED); | |
160 | } | |
39b6efc8 | 161 | continue; |
162 | } | |
d247d25f AL |
163 | |
164 | setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); | |
165 | #ifdef IPV6_V6ONLY | |
166 | if (e->ai_family == PF_INET6) { | |
167 | /* listen on both ipv4 and ipv6 */ | |
39b6efc8 | 168 | setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off, |
169 | sizeof(off)); | |
d247d25f AL |
170 | } |
171 | #endif | |
172 | ||
877691f9 MA |
173 | port_min = inet_getport(e); |
174 | port_max = to ? to + port_offset : port_min; | |
175 | for (p = port_min; p <= port_max; p++) { | |
176 | inet_setport(e, p); | |
d247d25f | 177 | if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) { |
d247d25f AL |
178 | goto listen; |
179 | } | |
877691f9 | 180 | if (p == port_max) { |
d247d25f AL |
181 | fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__, |
182 | inet_strfamily(e->ai_family), uaddr, inet_getport(e), | |
183 | strerror(errno)); | |
029409e5 AK |
184 | if (!e->ai_next) { |
185 | error_set(errp, QERR_SOCKET_BIND_FAILED); | |
186 | } | |
d247d25f | 187 | } |
d247d25f AL |
188 | } |
189 | closesocket(slisten); | |
190 | } | |
191 | fprintf(stderr, "%s: FAILED\n", __FUNCTION__); | |
192 | freeaddrinfo(res); | |
193 | return -1; | |
194 | ||
195 | listen: | |
196 | if (listen(slisten,1) != 0) { | |
029409e5 | 197 | error_set(errp, QERR_SOCKET_LISTEN_FAILED); |
d247d25f AL |
198 | perror("listen"); |
199 | closesocket(slisten); | |
39b6efc8 | 200 | freeaddrinfo(res); |
d247d25f AL |
201 | return -1; |
202 | } | |
e5bc776f GH |
203 | snprintf(uport, sizeof(uport), "%d", inet_getport(e) - port_offset); |
204 | qemu_opt_set(opts, "host", uaddr); | |
205 | qemu_opt_set(opts, "port", uport); | |
206 | qemu_opt_set(opts, "ipv6", (e->ai_family == PF_INET6) ? "on" : "off"); | |
207 | qemu_opt_set(opts, "ipv4", (e->ai_family != PF_INET6) ? "on" : "off"); | |
d247d25f AL |
208 | freeaddrinfo(res); |
209 | return slisten; | |
210 | } | |
211 | ||
05bc1d8a MT |
212 | #ifdef _WIN32 |
213 | #define QEMU_SOCKET_RC_INPROGRESS(rc) \ | |
214 | ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY) | |
215 | #else | |
216 | #define QEMU_SOCKET_RC_INPROGRESS(rc) \ | |
217 | ((rc) == -EINPROGRESS) | |
218 | #endif | |
219 | ||
220 | static int inet_connect_addr(struct addrinfo *addr, bool block, | |
221 | bool *in_progress) | |
d247d25f | 222 | { |
05bc1d8a MT |
223 | int sock, rc; |
224 | ||
225 | if (in_progress) { | |
226 | *in_progress = false; | |
227 | } | |
228 | ||
229 | sock = qemu_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); | |
230 | if (sock < 0) { | |
231 | fprintf(stderr, "%s: socket(%s): %s\n", __func__, | |
232 | inet_strfamily(addr->ai_family), strerror(errno)); | |
233 | return -1; | |
234 | } | |
235 | setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); | |
236 | if (!block) { | |
237 | socket_set_nonblock(sock); | |
238 | } | |
239 | /* connect to peer */ | |
240 | do { | |
241 | rc = 0; | |
242 | if (connect(sock, addr->ai_addr, addr->ai_addrlen) < 0) { | |
243 | rc = -socket_error(); | |
244 | } | |
245 | } while (rc == -EINTR); | |
246 | ||
247 | if (!block && QEMU_SOCKET_RC_INPROGRESS(rc)) { | |
248 | if (in_progress) { | |
249 | *in_progress = true; | |
250 | } | |
251 | } else if (rc < 0) { | |
252 | closesocket(sock); | |
253 | return -1; | |
254 | } | |
255 | return sock; | |
256 | } | |
257 | ||
258 | static struct addrinfo *inet_parse_connect_opts(QemuOpts *opts, Error **errp) | |
259 | { | |
260 | struct addrinfo ai, *res; | |
261 | int rc; | |
f4c94c7c GH |
262 | const char *addr; |
263 | const char *port; | |
d247d25f | 264 | |
05bc1d8a | 265 | memset(&ai, 0, sizeof(ai)); |
d247d25f AL |
266 | ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG; |
267 | ai.ai_family = PF_UNSPEC; | |
f4c94c7c | 268 | ai.ai_socktype = SOCK_STREAM; |
d247d25f | 269 | |
f4c94c7c GH |
270 | addr = qemu_opt_get(opts, "host"); |
271 | port = qemu_opt_get(opts, "port"); | |
272 | if (addr == NULL || port == NULL) { | |
05bc1d8a MT |
273 | fprintf(stderr, |
274 | "inet_parse_connect_opts: host and/or port not specified\n"); | |
a6ba35b3 | 275 | error_set(errp, QERR_SOCKET_CREATE_FAILED); |
05bc1d8a | 276 | return NULL; |
d247d25f AL |
277 | } |
278 | ||
05bc1d8a | 279 | if (qemu_opt_get_bool(opts, "ipv4", 0)) { |
d247d25f | 280 | ai.ai_family = PF_INET; |
05bc1d8a MT |
281 | } |
282 | if (qemu_opt_get_bool(opts, "ipv6", 0)) { | |
d247d25f | 283 | ai.ai_family = PF_INET6; |
05bc1d8a | 284 | } |
d247d25f AL |
285 | |
286 | /* lookup */ | |
05bc1d8a MT |
287 | rc = getaddrinfo(addr, port, &ai, &res); |
288 | if (rc != 0) { | |
289 | fprintf(stderr, "getaddrinfo(%s,%s): %s\n", addr, port, | |
f4c94c7c | 290 | gai_strerror(rc)); |
a6ba35b3 | 291 | error_set(errp, QERR_SOCKET_CREATE_FAILED); |
05bc1d8a MT |
292 | return NULL; |
293 | } | |
294 | return res; | |
295 | } | |
296 | ||
297 | int inet_connect_opts(QemuOpts *opts, bool *in_progress, Error **errp) | |
298 | { | |
299 | struct addrinfo *res, *e; | |
300 | int sock = -1; | |
301 | bool block = qemu_opt_get_bool(opts, "block", 0); | |
302 | ||
303 | res = inet_parse_connect_opts(opts, errp); | |
304 | if (!res) { | |
305 | return -1; | |
306 | } | |
307 | ||
308 | if (in_progress) { | |
309 | *in_progress = false; | |
d247d25f | 310 | } |
d247d25f AL |
311 | |
312 | for (e = res; e != NULL; e = e->ai_next) { | |
05bc1d8a MT |
313 | sock = inet_connect_addr(e, block, in_progress); |
314 | if (sock >= 0) { | |
315 | break; | |
a6ba35b3 | 316 | } |
d247d25f | 317 | } |
05bc1d8a MT |
318 | if (sock < 0) { |
319 | error_set(errp, QERR_SOCKET_CONNECT_FAILED); | |
320 | } | |
d247d25f | 321 | freeaddrinfo(res); |
05bc1d8a | 322 | return sock; |
d247d25f AL |
323 | } |
324 | ||
7e1b35b4 GH |
325 | int inet_dgram_opts(QemuOpts *opts) |
326 | { | |
327 | struct addrinfo ai, *peer = NULL, *local = NULL; | |
328 | const char *addr; | |
329 | const char *port; | |
330 | char uaddr[INET6_ADDRSTRLEN+1]; | |
331 | char uport[33]; | |
332 | int sock = -1, rc; | |
333 | ||
334 | /* lookup peer addr */ | |
335 | memset(&ai,0, sizeof(ai)); | |
336 | ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG; | |
337 | ai.ai_family = PF_UNSPEC; | |
338 | ai.ai_socktype = SOCK_DGRAM; | |
339 | ||
340 | addr = qemu_opt_get(opts, "host"); | |
341 | port = qemu_opt_get(opts, "port"); | |
342 | if (addr == NULL || strlen(addr) == 0) { | |
343 | addr = "localhost"; | |
344 | } | |
345 | if (port == NULL || strlen(port) == 0) { | |
346 | fprintf(stderr, "inet_dgram: port not specified\n"); | |
347 | return -1; | |
348 | } | |
349 | ||
350 | if (qemu_opt_get_bool(opts, "ipv4", 0)) | |
351 | ai.ai_family = PF_INET; | |
352 | if (qemu_opt_get_bool(opts, "ipv6", 0)) | |
353 | ai.ai_family = PF_INET6; | |
354 | ||
355 | if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) { | |
356 | fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port, | |
357 | gai_strerror(rc)); | |
358 | return -1; | |
359 | } | |
7e1b35b4 GH |
360 | |
361 | /* lookup local addr */ | |
362 | memset(&ai,0, sizeof(ai)); | |
363 | ai.ai_flags = AI_PASSIVE; | |
364 | ai.ai_family = peer->ai_family; | |
365 | ai.ai_socktype = SOCK_DGRAM; | |
366 | ||
367 | addr = qemu_opt_get(opts, "localaddr"); | |
368 | port = qemu_opt_get(opts, "localport"); | |
369 | if (addr == NULL || strlen(addr) == 0) { | |
370 | addr = NULL; | |
371 | } | |
372 | if (!port || strlen(port) == 0) | |
373 | port = "0"; | |
374 | ||
375 | if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) { | |
376 | fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port, | |
377 | gai_strerror(rc)); | |
39b38459 | 378 | goto err; |
7e1b35b4 | 379 | } |
7e1b35b4 GH |
380 | |
381 | /* create socket */ | |
40ff6d7e | 382 | sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol); |
7e1b35b4 GH |
383 | if (sock < 0) { |
384 | fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__, | |
385 | inet_strfamily(peer->ai_family), strerror(errno)); | |
386 | goto err; | |
387 | } | |
388 | setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); | |
389 | ||
390 | /* bind socket */ | |
391 | if (getnameinfo((struct sockaddr*)local->ai_addr,local->ai_addrlen, | |
392 | uaddr,INET6_ADDRSTRLEN,uport,32, | |
393 | NI_NUMERICHOST | NI_NUMERICSERV) != 0) { | |
394 | fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__); | |
395 | goto err; | |
396 | } | |
397 | if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) { | |
398 | fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__, | |
399 | inet_strfamily(local->ai_family), uaddr, inet_getport(local)); | |
400 | goto err; | |
401 | } | |
402 | ||
403 | /* connect to peer */ | |
404 | if (getnameinfo((struct sockaddr*)peer->ai_addr, peer->ai_addrlen, | |
405 | uaddr, INET6_ADDRSTRLEN, uport, 32, | |
406 | NI_NUMERICHOST | NI_NUMERICSERV) != 0) { | |
407 | fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__); | |
408 | goto err; | |
409 | } | |
410 | if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) { | |
411 | fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__, | |
412 | inet_strfamily(peer->ai_family), | |
413 | peer->ai_canonname, uaddr, uport, strerror(errno)); | |
414 | goto err; | |
415 | } | |
416 | ||
417 | freeaddrinfo(local); | |
418 | freeaddrinfo(peer); | |
419 | return sock; | |
420 | ||
421 | err: | |
422 | if (-1 != sock) | |
423 | closesocket(sock); | |
424 | if (local) | |
425 | freeaddrinfo(local); | |
426 | if (peer) | |
427 | freeaddrinfo(peer); | |
428 | return -1; | |
429 | } | |
430 | ||
f4c94c7c GH |
431 | /* compatibility wrapper */ |
432 | static int inet_parse(QemuOpts *opts, const char *str) | |
433 | { | |
434 | const char *optstr, *h; | |
435 | char addr[64]; | |
436 | char port[33]; | |
437 | int pos; | |
438 | ||
439 | /* parse address */ | |
440 | if (str[0] == ':') { | |
441 | /* no host given */ | |
442 | addr[0] = '\0'; | |
443 | if (1 != sscanf(str,":%32[^,]%n",port,&pos)) { | |
444 | fprintf(stderr, "%s: portonly parse error (%s)\n", | |
445 | __FUNCTION__, str); | |
446 | return -1; | |
447 | } | |
448 | } else if (str[0] == '[') { | |
449 | /* IPv6 addr */ | |
450 | if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) { | |
451 | fprintf(stderr, "%s: ipv6 parse error (%s)\n", | |
452 | __FUNCTION__, str); | |
453 | return -1; | |
454 | } | |
2198a62e | 455 | qemu_opt_set(opts, "ipv6", "on"); |
f4c94c7c GH |
456 | } else if (qemu_isdigit(str[0])) { |
457 | /* IPv4 addr */ | |
458 | if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) { | |
459 | fprintf(stderr, "%s: ipv4 parse error (%s)\n", | |
460 | __FUNCTION__, str); | |
461 | return -1; | |
462 | } | |
2198a62e | 463 | qemu_opt_set(opts, "ipv4", "on"); |
f4c94c7c GH |
464 | } else { |
465 | /* hostname */ | |
466 | if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) { | |
467 | fprintf(stderr, "%s: hostname parse error (%s)\n", | |
468 | __FUNCTION__, str); | |
469 | return -1; | |
470 | } | |
471 | } | |
472 | qemu_opt_set(opts, "host", addr); | |
473 | qemu_opt_set(opts, "port", port); | |
474 | ||
475 | /* parse options */ | |
476 | optstr = str + pos; | |
477 | h = strstr(optstr, ",to="); | |
478 | if (h) | |
479 | qemu_opt_set(opts, "to", h+4); | |
480 | if (strstr(optstr, ",ipv4")) | |
2198a62e | 481 | qemu_opt_set(opts, "ipv4", "on"); |
f4c94c7c | 482 | if (strstr(optstr, ",ipv6")) |
2198a62e | 483 | qemu_opt_set(opts, "ipv6", "on"); |
f4c94c7c GH |
484 | return 0; |
485 | } | |
486 | ||
e5bc776f | 487 | int inet_listen(const char *str, char *ostr, int olen, |
029409e5 | 488 | int socktype, int port_offset, Error **errp) |
e5bc776f GH |
489 | { |
490 | QemuOpts *opts; | |
491 | char *optstr; | |
492 | int sock = -1; | |
493 | ||
8be7e7e4 | 494 | opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); |
e5bc776f | 495 | if (inet_parse(opts, str) == 0) { |
029409e5 | 496 | sock = inet_listen_opts(opts, port_offset, errp); |
e5bc776f GH |
497 | if (sock != -1 && ostr) { |
498 | optstr = strchr(str, ','); | |
499 | if (qemu_opt_get_bool(opts, "ipv6", 0)) { | |
500 | snprintf(ostr, olen, "[%s]:%s%s", | |
501 | qemu_opt_get(opts, "host"), | |
502 | qemu_opt_get(opts, "port"), | |
503 | optstr ? optstr : ""); | |
504 | } else { | |
505 | snprintf(ostr, olen, "%s:%s%s", | |
506 | qemu_opt_get(opts, "host"), | |
507 | qemu_opt_get(opts, "port"), | |
508 | optstr ? optstr : ""); | |
509 | } | |
510 | } | |
029409e5 AK |
511 | } else { |
512 | error_set(errp, QERR_SOCKET_CREATE_FAILED); | |
e5bc776f GH |
513 | } |
514 | qemu_opts_del(opts); | |
515 | return sock; | |
516 | } | |
517 | ||
02a08fef | 518 | int inet_connect(const char *str, bool block, bool *in_progress, Error **errp) |
f4c94c7c GH |
519 | { |
520 | QemuOpts *opts; | |
521 | int sock = -1; | |
522 | ||
8be7e7e4 | 523 | opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); |
a6ba35b3 AK |
524 | if (inet_parse(opts, str) == 0) { |
525 | if (block) { | |
526 | qemu_opt_set(opts, "block", "on"); | |
527 | } | |
02a08fef | 528 | sock = inet_connect_opts(opts, in_progress, errp); |
a6ba35b3 AK |
529 | } else { |
530 | error_set(errp, QERR_SOCKET_CREATE_FAILED); | |
531 | } | |
f4c94c7c GH |
532 | qemu_opts_del(opts); |
533 | return sock; | |
534 | } | |
535 | ||
d247d25f AL |
536 | #ifndef _WIN32 |
537 | ||
62b6adfb | 538 | int unix_listen_opts(QemuOpts *opts) |
d247d25f AL |
539 | { |
540 | struct sockaddr_un un; | |
62b6adfb GH |
541 | const char *path = qemu_opt_get(opts, "path"); |
542 | int sock, fd; | |
d247d25f | 543 | |
40ff6d7e | 544 | sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0); |
d247d25f | 545 | if (sock < 0) { |
39b6efc8 | 546 | perror("socket(unix)"); |
547 | return -1; | |
d247d25f AL |
548 | } |
549 | ||
d247d25f AL |
550 | memset(&un, 0, sizeof(un)); |
551 | un.sun_family = AF_UNIX; | |
552 | if (path && strlen(path)) { | |
553 | snprintf(un.sun_path, sizeof(un.sun_path), "%s", path); | |
554 | } else { | |
555 | char *tmpdir = getenv("TMPDIR"); | |
556 | snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX", | |
557 | tmpdir ? tmpdir : "/tmp"); | |
558 | /* | |
559 | * This dummy fd usage silences the mktemp() unsecure warning. | |
560 | * Using mkstemp() doesn't make things more secure here | |
561 | * though. bind() complains about existing files, so we have | |
562 | * to unlink first and thus re-open the race window. The | |
563 | * worst case possible is bind() failing, i.e. a DoS attack. | |
564 | */ | |
565 | fd = mkstemp(un.sun_path); close(fd); | |
62b6adfb | 566 | qemu_opt_set(opts, "path", un.sun_path); |
d247d25f | 567 | } |
d247d25f AL |
568 | |
569 | unlink(un.sun_path); | |
570 | if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) { | |
571 | fprintf(stderr, "bind(unix:%s): %s\n", un.sun_path, strerror(errno)); | |
572 | goto err; | |
573 | } | |
574 | if (listen(sock, 1) < 0) { | |
575 | fprintf(stderr, "listen(unix:%s): %s\n", un.sun_path, strerror(errno)); | |
576 | goto err; | |
577 | } | |
578 | ||
d247d25f AL |
579 | return sock; |
580 | ||
581 | err: | |
d247d25f AL |
582 | closesocket(sock); |
583 | return -1; | |
584 | } | |
585 | ||
2af2bf67 | 586 | int unix_connect_opts(QemuOpts *opts) |
d247d25f AL |
587 | { |
588 | struct sockaddr_un un; | |
2af2bf67 | 589 | const char *path = qemu_opt_get(opts, "path"); |
d247d25f AL |
590 | int sock; |
591 | ||
2af2bf67 GH |
592 | if (NULL == path) { |
593 | fprintf(stderr, "unix connect: no path specified\n"); | |
594 | return -1; | |
595 | } | |
596 | ||
40ff6d7e | 597 | sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0); |
d247d25f | 598 | if (sock < 0) { |
39b6efc8 | 599 | perror("socket(unix)"); |
600 | return -1; | |
d247d25f AL |
601 | } |
602 | ||
603 | memset(&un, 0, sizeof(un)); | |
604 | un.sun_family = AF_UNIX; | |
605 | snprintf(un.sun_path, sizeof(un.sun_path), "%s", path); | |
606 | if (connect(sock, (struct sockaddr*) &un, sizeof(un)) < 0) { | |
607 | fprintf(stderr, "connect(unix:%s): %s\n", path, strerror(errno)); | |
9d947472 | 608 | close(sock); |
d247d25f AL |
609 | return -1; |
610 | } | |
611 | ||
d247d25f AL |
612 | return sock; |
613 | } | |
614 | ||
2af2bf67 | 615 | /* compatibility wrapper */ |
62b6adfb GH |
616 | int unix_listen(const char *str, char *ostr, int olen) |
617 | { | |
618 | QemuOpts *opts; | |
619 | char *path, *optstr; | |
620 | int sock, len; | |
621 | ||
8be7e7e4 | 622 | opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); |
62b6adfb GH |
623 | |
624 | optstr = strchr(str, ','); | |
625 | if (optstr) { | |
626 | len = optstr - str; | |
627 | if (len) { | |
7267c094 | 628 | path = g_malloc(len+1); |
62b6adfb GH |
629 | snprintf(path, len+1, "%.*s", len, str); |
630 | qemu_opt_set(opts, "path", path); | |
7267c094 | 631 | g_free(path); |
62b6adfb GH |
632 | } |
633 | } else { | |
634 | qemu_opt_set(opts, "path", str); | |
635 | } | |
636 | ||
637 | sock = unix_listen_opts(opts); | |
638 | ||
639 | if (sock != -1 && ostr) | |
640 | snprintf(ostr, olen, "%s%s", qemu_opt_get(opts, "path"), optstr ? optstr : ""); | |
641 | qemu_opts_del(opts); | |
642 | return sock; | |
643 | } | |
644 | ||
2af2bf67 GH |
645 | int unix_connect(const char *path) |
646 | { | |
647 | QemuOpts *opts; | |
648 | int sock; | |
649 | ||
8be7e7e4 | 650 | opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); |
2af2bf67 GH |
651 | qemu_opt_set(opts, "path", path); |
652 | sock = unix_connect_opts(opts); | |
653 | qemu_opts_del(opts); | |
654 | return sock; | |
655 | } | |
656 | ||
d247d25f AL |
657 | #else |
658 | ||
108af7b9 GH |
659 | int unix_listen_opts(QemuOpts *opts) |
660 | { | |
661 | fprintf(stderr, "unix sockets are not available on windows\n"); | |
b82eac92 | 662 | errno = ENOTSUP; |
108af7b9 GH |
663 | return -1; |
664 | } | |
665 | ||
666 | int unix_connect_opts(QemuOpts *opts) | |
667 | { | |
668 | fprintf(stderr, "unix sockets are not available on windows\n"); | |
b82eac92 | 669 | errno = ENOTSUP; |
108af7b9 GH |
670 | return -1; |
671 | } | |
672 | ||
d247d25f AL |
673 | int unix_listen(const char *path, char *ostr, int olen) |
674 | { | |
675 | fprintf(stderr, "unix sockets are not available on windows\n"); | |
b82eac92 | 676 | errno = ENOTSUP; |
d247d25f AL |
677 | return -1; |
678 | } | |
679 | ||
680 | int unix_connect(const char *path) | |
681 | { | |
682 | fprintf(stderr, "unix sockets are not available on windows\n"); | |
b82eac92 | 683 | errno = ENOTSUP; |
d247d25f AL |
684 | return -1; |
685 | } | |
686 | ||
687 | #endif | |
0706a4dc PB |
688 | |
689 | #ifdef _WIN32 | |
690 | static void socket_cleanup(void) | |
691 | { | |
692 | WSACleanup(); | |
693 | } | |
694 | #endif | |
695 | ||
696 | int socket_init(void) | |
697 | { | |
698 | #ifdef _WIN32 | |
699 | WSADATA Data; | |
700 | int ret, err; | |
701 | ||
702 | ret = WSAStartup(MAKEWORD(2,2), &Data); | |
703 | if (ret != 0) { | |
704 | err = WSAGetLastError(); | |
705 | fprintf(stderr, "WSAStartup: %d\n", err); | |
706 | return -1; | |
707 | } | |
708 | atexit(socket_cleanup); | |
709 | #endif | |
710 | return 0; | |
711 | } |