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