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