]>
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 VS |
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 VS |
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 VS |
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 | ||
a6ba35b3 | 212 | int inet_connect_opts(QemuOpts *opts, Error **errp) |
d247d25f AL |
213 | { |
214 | struct addrinfo ai,*res,*e; | |
f4c94c7c GH |
215 | const char *addr; |
216 | const char *port; | |
d247d25f AL |
217 | char uaddr[INET6_ADDRSTRLEN+1]; |
218 | char uport[33]; | |
219 | int sock,rc; | |
a6ba35b3 | 220 | bool block; |
d247d25f AL |
221 | |
222 | memset(&ai,0, sizeof(ai)); | |
223 | ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG; | |
224 | ai.ai_family = PF_UNSPEC; | |
f4c94c7c | 225 | ai.ai_socktype = SOCK_STREAM; |
d247d25f | 226 | |
f4c94c7c GH |
227 | addr = qemu_opt_get(opts, "host"); |
228 | port = qemu_opt_get(opts, "port"); | |
a6ba35b3 | 229 | block = qemu_opt_get_bool(opts, "block", 0); |
f4c94c7c GH |
230 | if (addr == NULL || port == NULL) { |
231 | fprintf(stderr, "inet_connect: host and/or port not specified\n"); | |
a6ba35b3 | 232 | error_set(errp, QERR_SOCKET_CREATE_FAILED); |
f4c94c7c | 233 | return -1; |
d247d25f AL |
234 | } |
235 | ||
f4c94c7c | 236 | if (qemu_opt_get_bool(opts, "ipv4", 0)) |
d247d25f | 237 | ai.ai_family = PF_INET; |
f4c94c7c | 238 | if (qemu_opt_get_bool(opts, "ipv6", 0)) |
d247d25f AL |
239 | ai.ai_family = PF_INET6; |
240 | ||
241 | /* lookup */ | |
242 | if (0 != (rc = getaddrinfo(addr, port, &ai, &res))) { | |
f4c94c7c GH |
243 | fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port, |
244 | gai_strerror(rc)); | |
a6ba35b3 | 245 | error_set(errp, QERR_SOCKET_CREATE_FAILED); |
d247d25f AL |
246 | return -1; |
247 | } | |
d247d25f AL |
248 | |
249 | for (e = res; e != NULL; e = e->ai_next) { | |
39b6efc8 VS |
250 | if (getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen, |
251 | uaddr,INET6_ADDRSTRLEN,uport,32, | |
252 | NI_NUMERICHOST | NI_NUMERICSERV) != 0) { | |
d247d25f | 253 | fprintf(stderr,"%s: getnameinfo: oops\n", __FUNCTION__); |
39b6efc8 VS |
254 | continue; |
255 | } | |
40ff6d7e | 256 | sock = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol); |
39b6efc8 | 257 | if (sock < 0) { |
d247d25f | 258 | fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__, |
39b6efc8 VS |
259 | inet_strfamily(e->ai_family), strerror(errno)); |
260 | continue; | |
261 | } | |
d247d25f | 262 | setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); |
a6ba35b3 AK |
263 | if (!block) { |
264 | socket_set_nonblock(sock); | |
265 | } | |
39b6efc8 | 266 | /* connect to peer */ |
a6ba35b3 AK |
267 | do { |
268 | rc = 0; | |
269 | if (connect(sock, e->ai_addr, e->ai_addrlen) < 0) { | |
270 | rc = -socket_error(); | |
271 | } | |
272 | } while (rc == -EINTR); | |
273 | ||
274 | #ifdef _WIN32 | |
275 | if (!block && (rc == -EINPROGRESS || rc == -EWOULDBLOCK | |
276 | || rc == -WSAEALREADY)) { | |
277 | #else | |
278 | if (!block && (rc == -EINPROGRESS)) { | |
279 | #endif | |
280 | error_set(errp, QERR_SOCKET_CONNECT_IN_PROGRESS); | |
281 | } else if (rc < 0) { | |
136faa36 | 282 | if (NULL == e->ai_next) |
d247d25f AL |
283 | fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__, |
284 | inet_strfamily(e->ai_family), | |
285 | e->ai_canonname, uaddr, uport, strerror(errno)); | |
286 | closesocket(sock); | |
39b6efc8 VS |
287 | continue; |
288 | } | |
d247d25f | 289 | freeaddrinfo(res); |
39b6efc8 | 290 | return sock; |
d247d25f | 291 | } |
a6ba35b3 | 292 | error_set(errp, QERR_SOCKET_CONNECT_FAILED); |
d247d25f AL |
293 | freeaddrinfo(res); |
294 | return -1; | |
295 | } | |
296 | ||
7e1b35b4 GH |
297 | int inet_dgram_opts(QemuOpts *opts) |
298 | { | |
299 | struct addrinfo ai, *peer = NULL, *local = NULL; | |
300 | const char *addr; | |
301 | const char *port; | |
302 | char uaddr[INET6_ADDRSTRLEN+1]; | |
303 | char uport[33]; | |
304 | int sock = -1, rc; | |
305 | ||
306 | /* lookup peer addr */ | |
307 | memset(&ai,0, sizeof(ai)); | |
308 | ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG; | |
309 | ai.ai_family = PF_UNSPEC; | |
310 | ai.ai_socktype = SOCK_DGRAM; | |
311 | ||
312 | addr = qemu_opt_get(opts, "host"); | |
313 | port = qemu_opt_get(opts, "port"); | |
314 | if (addr == NULL || strlen(addr) == 0) { | |
315 | addr = "localhost"; | |
316 | } | |
317 | if (port == NULL || strlen(port) == 0) { | |
318 | fprintf(stderr, "inet_dgram: port not specified\n"); | |
319 | return -1; | |
320 | } | |
321 | ||
322 | if (qemu_opt_get_bool(opts, "ipv4", 0)) | |
323 | ai.ai_family = PF_INET; | |
324 | if (qemu_opt_get_bool(opts, "ipv6", 0)) | |
325 | ai.ai_family = PF_INET6; | |
326 | ||
327 | if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) { | |
328 | fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port, | |
329 | gai_strerror(rc)); | |
330 | return -1; | |
331 | } | |
7e1b35b4 GH |
332 | |
333 | /* lookup local addr */ | |
334 | memset(&ai,0, sizeof(ai)); | |
335 | ai.ai_flags = AI_PASSIVE; | |
336 | ai.ai_family = peer->ai_family; | |
337 | ai.ai_socktype = SOCK_DGRAM; | |
338 | ||
339 | addr = qemu_opt_get(opts, "localaddr"); | |
340 | port = qemu_opt_get(opts, "localport"); | |
341 | if (addr == NULL || strlen(addr) == 0) { | |
342 | addr = NULL; | |
343 | } | |
344 | if (!port || strlen(port) == 0) | |
345 | port = "0"; | |
346 | ||
347 | if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) { | |
348 | fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port, | |
349 | gai_strerror(rc)); | |
350 | return -1; | |
351 | } | |
7e1b35b4 GH |
352 | |
353 | /* create socket */ | |
40ff6d7e | 354 | sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol); |
7e1b35b4 GH |
355 | if (sock < 0) { |
356 | fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__, | |
357 | inet_strfamily(peer->ai_family), strerror(errno)); | |
358 | goto err; | |
359 | } | |
360 | setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); | |
361 | ||
362 | /* bind socket */ | |
363 | if (getnameinfo((struct sockaddr*)local->ai_addr,local->ai_addrlen, | |
364 | uaddr,INET6_ADDRSTRLEN,uport,32, | |
365 | NI_NUMERICHOST | NI_NUMERICSERV) != 0) { | |
366 | fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__); | |
367 | goto err; | |
368 | } | |
369 | if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) { | |
370 | fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__, | |
371 | inet_strfamily(local->ai_family), uaddr, inet_getport(local)); | |
372 | goto err; | |
373 | } | |
374 | ||
375 | /* connect to peer */ | |
376 | if (getnameinfo((struct sockaddr*)peer->ai_addr, peer->ai_addrlen, | |
377 | uaddr, INET6_ADDRSTRLEN, uport, 32, | |
378 | NI_NUMERICHOST | NI_NUMERICSERV) != 0) { | |
379 | fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__); | |
380 | goto err; | |
381 | } | |
382 | if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) { | |
383 | fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__, | |
384 | inet_strfamily(peer->ai_family), | |
385 | peer->ai_canonname, uaddr, uport, strerror(errno)); | |
386 | goto err; | |
387 | } | |
388 | ||
389 | freeaddrinfo(local); | |
390 | freeaddrinfo(peer); | |
391 | return sock; | |
392 | ||
393 | err: | |
394 | if (-1 != sock) | |
395 | closesocket(sock); | |
396 | if (local) | |
397 | freeaddrinfo(local); | |
398 | if (peer) | |
399 | freeaddrinfo(peer); | |
400 | return -1; | |
401 | } | |
402 | ||
f4c94c7c GH |
403 | /* compatibility wrapper */ |
404 | static int inet_parse(QemuOpts *opts, const char *str) | |
405 | { | |
406 | const char *optstr, *h; | |
407 | char addr[64]; | |
408 | char port[33]; | |
409 | int pos; | |
410 | ||
411 | /* parse address */ | |
412 | if (str[0] == ':') { | |
413 | /* no host given */ | |
414 | addr[0] = '\0'; | |
415 | if (1 != sscanf(str,":%32[^,]%n",port,&pos)) { | |
416 | fprintf(stderr, "%s: portonly parse error (%s)\n", | |
417 | __FUNCTION__, str); | |
418 | return -1; | |
419 | } | |
420 | } else if (str[0] == '[') { | |
421 | /* IPv6 addr */ | |
422 | if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) { | |
423 | fprintf(stderr, "%s: ipv6 parse error (%s)\n", | |
424 | __FUNCTION__, str); | |
425 | return -1; | |
426 | } | |
2198a62e | 427 | qemu_opt_set(opts, "ipv6", "on"); |
f4c94c7c GH |
428 | } else if (qemu_isdigit(str[0])) { |
429 | /* IPv4 addr */ | |
430 | if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) { | |
431 | fprintf(stderr, "%s: ipv4 parse error (%s)\n", | |
432 | __FUNCTION__, str); | |
433 | return -1; | |
434 | } | |
2198a62e | 435 | qemu_opt_set(opts, "ipv4", "on"); |
f4c94c7c GH |
436 | } else { |
437 | /* hostname */ | |
438 | if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) { | |
439 | fprintf(stderr, "%s: hostname parse error (%s)\n", | |
440 | __FUNCTION__, str); | |
441 | return -1; | |
442 | } | |
443 | } | |
444 | qemu_opt_set(opts, "host", addr); | |
445 | qemu_opt_set(opts, "port", port); | |
446 | ||
447 | /* parse options */ | |
448 | optstr = str + pos; | |
449 | h = strstr(optstr, ",to="); | |
450 | if (h) | |
451 | qemu_opt_set(opts, "to", h+4); | |
452 | if (strstr(optstr, ",ipv4")) | |
2198a62e | 453 | qemu_opt_set(opts, "ipv4", "on"); |
f4c94c7c | 454 | if (strstr(optstr, ",ipv6")) |
2198a62e | 455 | qemu_opt_set(opts, "ipv6", "on"); |
f4c94c7c GH |
456 | return 0; |
457 | } | |
458 | ||
e5bc776f | 459 | int inet_listen(const char *str, char *ostr, int olen, |
029409e5 | 460 | int socktype, int port_offset, Error **errp) |
e5bc776f GH |
461 | { |
462 | QemuOpts *opts; | |
463 | char *optstr; | |
464 | int sock = -1; | |
465 | ||
8be7e7e4 | 466 | opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); |
e5bc776f | 467 | if (inet_parse(opts, str) == 0) { |
029409e5 | 468 | sock = inet_listen_opts(opts, port_offset, errp); |
e5bc776f GH |
469 | if (sock != -1 && ostr) { |
470 | optstr = strchr(str, ','); | |
471 | if (qemu_opt_get_bool(opts, "ipv6", 0)) { | |
472 | snprintf(ostr, olen, "[%s]:%s%s", | |
473 | qemu_opt_get(opts, "host"), | |
474 | qemu_opt_get(opts, "port"), | |
475 | optstr ? optstr : ""); | |
476 | } else { | |
477 | snprintf(ostr, olen, "%s:%s%s", | |
478 | qemu_opt_get(opts, "host"), | |
479 | qemu_opt_get(opts, "port"), | |
480 | optstr ? optstr : ""); | |
481 | } | |
482 | } | |
029409e5 AK |
483 | } else { |
484 | error_set(errp, QERR_SOCKET_CREATE_FAILED); | |
e5bc776f GH |
485 | } |
486 | qemu_opts_del(opts); | |
487 | return sock; | |
488 | } | |
489 | ||
a6ba35b3 | 490 | int inet_connect(const char *str, bool block, Error **errp) |
f4c94c7c GH |
491 | { |
492 | QemuOpts *opts; | |
493 | int sock = -1; | |
494 | ||
8be7e7e4 | 495 | opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); |
a6ba35b3 AK |
496 | if (inet_parse(opts, str) == 0) { |
497 | if (block) { | |
498 | qemu_opt_set(opts, "block", "on"); | |
499 | } | |
500 | sock = inet_connect_opts(opts, errp); | |
501 | } else { | |
502 | error_set(errp, QERR_SOCKET_CREATE_FAILED); | |
503 | } | |
f4c94c7c GH |
504 | qemu_opts_del(opts); |
505 | return sock; | |
506 | } | |
507 | ||
d247d25f AL |
508 | #ifndef _WIN32 |
509 | ||
62b6adfb | 510 | int unix_listen_opts(QemuOpts *opts) |
d247d25f AL |
511 | { |
512 | struct sockaddr_un un; | |
62b6adfb GH |
513 | const char *path = qemu_opt_get(opts, "path"); |
514 | int sock, fd; | |
d247d25f | 515 | |
40ff6d7e | 516 | sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0); |
d247d25f | 517 | if (sock < 0) { |
39b6efc8 VS |
518 | perror("socket(unix)"); |
519 | return -1; | |
d247d25f AL |
520 | } |
521 | ||
d247d25f AL |
522 | memset(&un, 0, sizeof(un)); |
523 | un.sun_family = AF_UNIX; | |
524 | if (path && strlen(path)) { | |
525 | snprintf(un.sun_path, sizeof(un.sun_path), "%s", path); | |
526 | } else { | |
527 | char *tmpdir = getenv("TMPDIR"); | |
528 | snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX", | |
529 | tmpdir ? tmpdir : "/tmp"); | |
530 | /* | |
531 | * This dummy fd usage silences the mktemp() unsecure warning. | |
532 | * Using mkstemp() doesn't make things more secure here | |
533 | * though. bind() complains about existing files, so we have | |
534 | * to unlink first and thus re-open the race window. The | |
535 | * worst case possible is bind() failing, i.e. a DoS attack. | |
536 | */ | |
537 | fd = mkstemp(un.sun_path); close(fd); | |
62b6adfb | 538 | qemu_opt_set(opts, "path", un.sun_path); |
d247d25f | 539 | } |
d247d25f AL |
540 | |
541 | unlink(un.sun_path); | |
542 | if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) { | |
543 | fprintf(stderr, "bind(unix:%s): %s\n", un.sun_path, strerror(errno)); | |
544 | goto err; | |
545 | } | |
546 | if (listen(sock, 1) < 0) { | |
547 | fprintf(stderr, "listen(unix:%s): %s\n", un.sun_path, strerror(errno)); | |
548 | goto err; | |
549 | } | |
550 | ||
d247d25f AL |
551 | return sock; |
552 | ||
553 | err: | |
d247d25f AL |
554 | closesocket(sock); |
555 | return -1; | |
556 | } | |
557 | ||
2af2bf67 | 558 | int unix_connect_opts(QemuOpts *opts) |
d247d25f AL |
559 | { |
560 | struct sockaddr_un un; | |
2af2bf67 | 561 | const char *path = qemu_opt_get(opts, "path"); |
d247d25f AL |
562 | int sock; |
563 | ||
2af2bf67 GH |
564 | if (NULL == path) { |
565 | fprintf(stderr, "unix connect: no path specified\n"); | |
566 | return -1; | |
567 | } | |
568 | ||
40ff6d7e | 569 | sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0); |
d247d25f | 570 | if (sock < 0) { |
39b6efc8 VS |
571 | perror("socket(unix)"); |
572 | return -1; | |
d247d25f AL |
573 | } |
574 | ||
575 | memset(&un, 0, sizeof(un)); | |
576 | un.sun_family = AF_UNIX; | |
577 | snprintf(un.sun_path, sizeof(un.sun_path), "%s", path); | |
578 | if (connect(sock, (struct sockaddr*) &un, sizeof(un)) < 0) { | |
579 | fprintf(stderr, "connect(unix:%s): %s\n", path, strerror(errno)); | |
9d947472 | 580 | close(sock); |
d247d25f AL |
581 | return -1; |
582 | } | |
583 | ||
d247d25f AL |
584 | return sock; |
585 | } | |
586 | ||
2af2bf67 | 587 | /* compatibility wrapper */ |
62b6adfb GH |
588 | int unix_listen(const char *str, char *ostr, int olen) |
589 | { | |
590 | QemuOpts *opts; | |
591 | char *path, *optstr; | |
592 | int sock, len; | |
593 | ||
8be7e7e4 | 594 | opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); |
62b6adfb GH |
595 | |
596 | optstr = strchr(str, ','); | |
597 | if (optstr) { | |
598 | len = optstr - str; | |
599 | if (len) { | |
7267c094 | 600 | path = g_malloc(len+1); |
62b6adfb GH |
601 | snprintf(path, len+1, "%.*s", len, str); |
602 | qemu_opt_set(opts, "path", path); | |
7267c094 | 603 | g_free(path); |
62b6adfb GH |
604 | } |
605 | } else { | |
606 | qemu_opt_set(opts, "path", str); | |
607 | } | |
608 | ||
609 | sock = unix_listen_opts(opts); | |
610 | ||
611 | if (sock != -1 && ostr) | |
612 | snprintf(ostr, olen, "%s%s", qemu_opt_get(opts, "path"), optstr ? optstr : ""); | |
613 | qemu_opts_del(opts); | |
614 | return sock; | |
615 | } | |
616 | ||
2af2bf67 GH |
617 | int unix_connect(const char *path) |
618 | { | |
619 | QemuOpts *opts; | |
620 | int sock; | |
621 | ||
8be7e7e4 | 622 | opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL); |
2af2bf67 GH |
623 | qemu_opt_set(opts, "path", path); |
624 | sock = unix_connect_opts(opts); | |
625 | qemu_opts_del(opts); | |
626 | return sock; | |
627 | } | |
628 | ||
d247d25f AL |
629 | #else |
630 | ||
108af7b9 GH |
631 | int unix_listen_opts(QemuOpts *opts) |
632 | { | |
633 | fprintf(stderr, "unix sockets are not available on windows\n"); | |
b82eac92 | 634 | errno = ENOTSUP; |
108af7b9 GH |
635 | return -1; |
636 | } | |
637 | ||
638 | int unix_connect_opts(QemuOpts *opts) | |
639 | { | |
640 | fprintf(stderr, "unix sockets are not available on windows\n"); | |
b82eac92 | 641 | errno = ENOTSUP; |
108af7b9 GH |
642 | return -1; |
643 | } | |
644 | ||
d247d25f AL |
645 | int unix_listen(const char *path, char *ostr, int olen) |
646 | { | |
647 | fprintf(stderr, "unix sockets are not available on windows\n"); | |
b82eac92 | 648 | errno = ENOTSUP; |
d247d25f AL |
649 | return -1; |
650 | } | |
651 | ||
652 | int unix_connect(const char *path) | |
653 | { | |
654 | fprintf(stderr, "unix sockets are not available on windows\n"); | |
b82eac92 | 655 | errno = ENOTSUP; |
d247d25f AL |
656 | return -1; |
657 | } | |
658 | ||
659 | #endif | |
0706a4dc PB |
660 | |
661 | #ifdef _WIN32 | |
662 | static void socket_cleanup(void) | |
663 | { | |
664 | WSACleanup(); | |
665 | } | |
666 | #endif | |
667 | ||
668 | int socket_init(void) | |
669 | { | |
670 | #ifdef _WIN32 | |
671 | WSADATA Data; | |
672 | int ret, err; | |
673 | ||
674 | ret = WSAStartup(MAKEWORD(2,2), &Data); | |
675 | if (ret != 0) { | |
676 | err = WSAGetLastError(); | |
677 | fprintf(stderr, "WSAStartup: %d\n", err); | |
678 | return -1; | |
679 | } | |
680 | atexit(socket_cleanup); | |
681 | #endif | |
682 | return 0; | |
683 | } |