root / qemu-sockets.c @ 12381085
History | View | Annotate | Download (18.5 kB)
1 | 305b0eb2 | aliguori | /*
|
---|---|---|---|
2 | 305b0eb2 | aliguori | * inet and unix socket functions for qemu
|
3 | 305b0eb2 | aliguori | *
|
4 | 305b0eb2 | aliguori | * (c) 2008 Gerd Hoffmann <kraxel@redhat.com>
|
5 | 305b0eb2 | aliguori | *
|
6 | 305b0eb2 | aliguori | * This program is free software; you can redistribute it and/or modify
|
7 | 305b0eb2 | aliguori | * it under the terms of the GNU General Public License as published by
|
8 | 305b0eb2 | aliguori | * the Free Software Foundation; under version 2 of the License.
|
9 | 305b0eb2 | aliguori | *
|
10 | 305b0eb2 | aliguori | * This program is distributed in the hope that it will be useful,
|
11 | 305b0eb2 | aliguori | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 | 305b0eb2 | aliguori | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 | 305b0eb2 | aliguori | * GNU General Public License for more details.
|
14 | 305b0eb2 | aliguori | */
|
15 | d247d25f | aliguori | #include <stdio.h> |
16 | d247d25f | aliguori | #include <stdlib.h> |
17 | d247d25f | aliguori | #include <string.h> |
18 | d247d25f | aliguori | #include <ctype.h> |
19 | d247d25f | aliguori | #include <errno.h> |
20 | d247d25f | aliguori | #include <unistd.h> |
21 | d247d25f | aliguori | |
22 | d247d25f | aliguori | #include "qemu_socket.h" |
23 | 47398b9c | blueswir1 | #include "qemu-common.h" /* for qemu_isdigit */ |
24 | d247d25f | aliguori | |
25 | d247d25f | aliguori | #ifndef AI_ADDRCONFIG
|
26 | d247d25f | aliguori | # define AI_ADDRCONFIG 0 |
27 | d247d25f | aliguori | #endif
|
28 | d247d25f | aliguori | |
29 | d247d25f | aliguori | static int sockets_debug = 0; |
30 | d247d25f | aliguori | static const int on=1, off=0; |
31 | d247d25f | aliguori | |
32 | 2af2bf67 | Gerd Hoffmann | /* used temporarely until all users are converted to QemuOpts */
|
33 | c1390903 | Blue Swirl | static QemuOptsList dummy_opts = {
|
34 | 2af2bf67 | Gerd Hoffmann | .name = "dummy",
|
35 | 72cf2d4f | Blue Swirl | .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head), |
36 | 2af2bf67 | Gerd Hoffmann | .desc = { |
37 | 2af2bf67 | Gerd Hoffmann | { |
38 | 2af2bf67 | Gerd Hoffmann | .name = "path",
|
39 | 2af2bf67 | Gerd Hoffmann | .type = QEMU_OPT_STRING, |
40 | f4c94c7c | Gerd Hoffmann | },{ |
41 | f4c94c7c | Gerd Hoffmann | .name = "host",
|
42 | f4c94c7c | Gerd Hoffmann | .type = QEMU_OPT_STRING, |
43 | f4c94c7c | Gerd Hoffmann | },{ |
44 | f4c94c7c | Gerd Hoffmann | .name = "port",
|
45 | f4c94c7c | Gerd Hoffmann | .type = QEMU_OPT_STRING, |
46 | f4c94c7c | Gerd Hoffmann | },{ |
47 | f4c94c7c | Gerd Hoffmann | .name = "to",
|
48 | f4c94c7c | Gerd Hoffmann | .type = QEMU_OPT_NUMBER, |
49 | f4c94c7c | Gerd Hoffmann | },{ |
50 | f4c94c7c | Gerd Hoffmann | .name = "ipv4",
|
51 | f4c94c7c | Gerd Hoffmann | .type = QEMU_OPT_BOOL, |
52 | f4c94c7c | Gerd Hoffmann | },{ |
53 | f4c94c7c | Gerd Hoffmann | .name = "ipv6",
|
54 | f4c94c7c | Gerd Hoffmann | .type = QEMU_OPT_BOOL, |
55 | 2af2bf67 | Gerd Hoffmann | }, |
56 | 2af2bf67 | Gerd Hoffmann | { /* end if list */ }
|
57 | 2af2bf67 | Gerd Hoffmann | }, |
58 | 2af2bf67 | Gerd Hoffmann | }; |
59 | 2af2bf67 | Gerd Hoffmann | |
60 | d247d25f | aliguori | static int inet_getport(struct addrinfo *e) |
61 | d247d25f | aliguori | { |
62 | d247d25f | aliguori | struct sockaddr_in *i4;
|
63 | d247d25f | aliguori | struct sockaddr_in6 *i6;
|
64 | d247d25f | aliguori | |
65 | d247d25f | aliguori | switch (e->ai_family) {
|
66 | d247d25f | aliguori | case PF_INET6:
|
67 | d247d25f | aliguori | i6 = (void*)e->ai_addr;
|
68 | d247d25f | aliguori | return ntohs(i6->sin6_port);
|
69 | d247d25f | aliguori | case PF_INET:
|
70 | d247d25f | aliguori | i4 = (void*)e->ai_addr;
|
71 | d247d25f | aliguori | return ntohs(i4->sin_port);
|
72 | d247d25f | aliguori | default:
|
73 | d247d25f | aliguori | return 0; |
74 | d247d25f | aliguori | } |
75 | d247d25f | aliguori | } |
76 | d247d25f | aliguori | |
77 | d247d25f | aliguori | static void inet_setport(struct addrinfo *e, int port) |
78 | d247d25f | aliguori | { |
79 | d247d25f | aliguori | struct sockaddr_in *i4;
|
80 | d247d25f | aliguori | struct sockaddr_in6 *i6;
|
81 | d247d25f | aliguori | |
82 | d247d25f | aliguori | switch (e->ai_family) {
|
83 | d247d25f | aliguori | case PF_INET6:
|
84 | d247d25f | aliguori | i6 = (void*)e->ai_addr;
|
85 | d247d25f | aliguori | i6->sin6_port = htons(port); |
86 | d247d25f | aliguori | break;
|
87 | d247d25f | aliguori | case PF_INET:
|
88 | d247d25f | aliguori | i4 = (void*)e->ai_addr;
|
89 | d247d25f | aliguori | i4->sin_port = htons(port); |
90 | d247d25f | aliguori | break;
|
91 | d247d25f | aliguori | } |
92 | d247d25f | aliguori | } |
93 | d247d25f | aliguori | |
94 | d247d25f | aliguori | static const char *inet_strfamily(int family) |
95 | d247d25f | aliguori | { |
96 | d247d25f | aliguori | switch (family) {
|
97 | d247d25f | aliguori | case PF_INET6: return "ipv6"; |
98 | d247d25f | aliguori | case PF_INET: return "ipv4"; |
99 | d247d25f | aliguori | case PF_UNIX: return "unix"; |
100 | d247d25f | aliguori | } |
101 | d247d25f | aliguori | return "????"; |
102 | d247d25f | aliguori | } |
103 | d247d25f | aliguori | |
104 | d247d25f | aliguori | static void inet_print_addrinfo(const char *tag, struct addrinfo *res) |
105 | d247d25f | aliguori | { |
106 | d247d25f | aliguori | struct addrinfo *e;
|
107 | d247d25f | aliguori | char uaddr[INET6_ADDRSTRLEN+1]; |
108 | d247d25f | aliguori | char uport[33]; |
109 | d247d25f | aliguori | |
110 | d247d25f | aliguori | for (e = res; e != NULL; e = e->ai_next) { |
111 | d247d25f | aliguori | getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
|
112 | d247d25f | aliguori | uaddr,INET6_ADDRSTRLEN,uport,32,
|
113 | d247d25f | aliguori | NI_NUMERICHOST | NI_NUMERICSERV); |
114 | d247d25f | aliguori | fprintf(stderr,"%s: getaddrinfo: family %s, host %s, port %s\n",
|
115 | d247d25f | aliguori | tag, inet_strfamily(e->ai_family), uaddr, uport); |
116 | d247d25f | aliguori | } |
117 | d247d25f | aliguori | } |
118 | d247d25f | aliguori | |
119 | e5bc776f | Gerd Hoffmann | int inet_listen_opts(QemuOpts *opts, int port_offset) |
120 | d247d25f | aliguori | { |
121 | d247d25f | aliguori | struct addrinfo ai,*res,*e;
|
122 | e5bc776f | Gerd Hoffmann | const char *addr; |
123 | d247d25f | aliguori | char port[33]; |
124 | d247d25f | aliguori | char uaddr[INET6_ADDRSTRLEN+1]; |
125 | d247d25f | aliguori | char uport[33]; |
126 | e5bc776f | Gerd Hoffmann | int slisten,rc,to,try_next;
|
127 | d247d25f | aliguori | |
128 | d247d25f | aliguori | memset(&ai,0, sizeof(ai)); |
129 | d247d25f | aliguori | ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; |
130 | d247d25f | aliguori | ai.ai_family = PF_UNSPEC; |
131 | e5bc776f | Gerd Hoffmann | ai.ai_socktype = SOCK_STREAM; |
132 | d247d25f | aliguori | |
133 | e5bc776f | Gerd Hoffmann | if (qemu_opt_get(opts, "port") == NULL) { |
134 | e5bc776f | Gerd Hoffmann | fprintf(stderr, "%s: host and/or port not specified\n", __FUNCTION__);
|
135 | e5bc776f | Gerd Hoffmann | return -1; |
136 | d247d25f | aliguori | } |
137 | e5bc776f | Gerd Hoffmann | pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port")); |
138 | e5bc776f | Gerd Hoffmann | addr = qemu_opt_get(opts, "host");
|
139 | d247d25f | aliguori | |
140 | e5bc776f | Gerd Hoffmann | to = qemu_opt_get_number(opts, "to", 0); |
141 | e5bc776f | Gerd Hoffmann | if (qemu_opt_get_bool(opts, "ipv4", 0)) |
142 | d247d25f | aliguori | ai.ai_family = PF_INET; |
143 | e5bc776f | Gerd Hoffmann | if (qemu_opt_get_bool(opts, "ipv6", 0)) |
144 | d247d25f | aliguori | ai.ai_family = PF_INET6; |
145 | d247d25f | aliguori | |
146 | d247d25f | aliguori | /* lookup */
|
147 | d247d25f | aliguori | if (port_offset)
|
148 | d247d25f | aliguori | snprintf(port, sizeof(port), "%d", atoi(port) + port_offset); |
149 | d247d25f | aliguori | rc = getaddrinfo(strlen(addr) ? addr : NULL, port, &ai, &res);
|
150 | d247d25f | aliguori | if (rc != 0) { |
151 | e5bc776f | Gerd Hoffmann | fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
|
152 | e5bc776f | Gerd Hoffmann | gai_strerror(rc)); |
153 | d247d25f | aliguori | return -1; |
154 | d247d25f | aliguori | } |
155 | d247d25f | aliguori | if (sockets_debug)
|
156 | d247d25f | aliguori | inet_print_addrinfo(__FUNCTION__, res); |
157 | d247d25f | aliguori | |
158 | d247d25f | aliguori | /* create socket + bind */
|
159 | d247d25f | aliguori | for (e = res; e != NULL; e = e->ai_next) { |
160 | 39b6efc8 | vibi | getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
|
161 | 39b6efc8 | vibi | uaddr,INET6_ADDRSTRLEN,uport,32,
|
162 | 39b6efc8 | vibi | NI_NUMERICHOST | NI_NUMERICSERV); |
163 | 40ff6d7e | Kevin Wolf | slisten = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol); |
164 | 39b6efc8 | vibi | if (slisten < 0) { |
165 | d247d25f | aliguori | fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
|
166 | d247d25f | aliguori | inet_strfamily(e->ai_family), strerror(errno)); |
167 | 39b6efc8 | vibi | continue;
|
168 | 39b6efc8 | vibi | } |
169 | d247d25f | aliguori | |
170 | d247d25f | aliguori | setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); |
171 | d247d25f | aliguori | #ifdef IPV6_V6ONLY
|
172 | d247d25f | aliguori | if (e->ai_family == PF_INET6) {
|
173 | d247d25f | aliguori | /* listen on both ipv4 and ipv6 */
|
174 | 39b6efc8 | vibi | setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,
|
175 | 39b6efc8 | vibi | sizeof(off));
|
176 | d247d25f | aliguori | } |
177 | d247d25f | aliguori | #endif
|
178 | d247d25f | aliguori | |
179 | d247d25f | aliguori | for (;;) {
|
180 | d247d25f | aliguori | if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) { |
181 | d247d25f | aliguori | if (sockets_debug)
|
182 | d247d25f | aliguori | fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,
|
183 | 39b6efc8 | vibi | inet_strfamily(e->ai_family), uaddr, inet_getport(e)); |
184 | d247d25f | aliguori | goto listen;
|
185 | d247d25f | aliguori | } |
186 | d247d25f | aliguori | try_next = to && (inet_getport(e) <= to + port_offset); |
187 | d247d25f | aliguori | if (!try_next || sockets_debug)
|
188 | d247d25f | aliguori | fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__,
|
189 | d247d25f | aliguori | inet_strfamily(e->ai_family), uaddr, inet_getport(e), |
190 | d247d25f | aliguori | strerror(errno)); |
191 | d247d25f | aliguori | if (try_next) {
|
192 | d247d25f | aliguori | inet_setport(e, inet_getport(e) + 1);
|
193 | d247d25f | aliguori | continue;
|
194 | d247d25f | aliguori | } |
195 | d247d25f | aliguori | break;
|
196 | d247d25f | aliguori | } |
197 | d247d25f | aliguori | closesocket(slisten); |
198 | d247d25f | aliguori | } |
199 | d247d25f | aliguori | fprintf(stderr, "%s: FAILED\n", __FUNCTION__);
|
200 | d247d25f | aliguori | freeaddrinfo(res); |
201 | d247d25f | aliguori | return -1; |
202 | d247d25f | aliguori | |
203 | d247d25f | aliguori | listen:
|
204 | d247d25f | aliguori | if (listen(slisten,1) != 0) { |
205 | d247d25f | aliguori | perror("listen");
|
206 | d247d25f | aliguori | closesocket(slisten); |
207 | 39b6efc8 | vibi | freeaddrinfo(res); |
208 | d247d25f | aliguori | return -1; |
209 | d247d25f | aliguori | } |
210 | e5bc776f | Gerd Hoffmann | snprintf(uport, sizeof(uport), "%d", inet_getport(e) - port_offset); |
211 | e5bc776f | Gerd Hoffmann | qemu_opt_set(opts, "host", uaddr);
|
212 | e5bc776f | Gerd Hoffmann | qemu_opt_set(opts, "port", uport);
|
213 | e5bc776f | Gerd Hoffmann | qemu_opt_set(opts, "ipv6", (e->ai_family == PF_INET6) ? "on" : "off"); |
214 | e5bc776f | Gerd Hoffmann | qemu_opt_set(opts, "ipv4", (e->ai_family != PF_INET6) ? "on" : "off"); |
215 | d247d25f | aliguori | freeaddrinfo(res); |
216 | d247d25f | aliguori | return slisten;
|
217 | d247d25f | aliguori | } |
218 | d247d25f | aliguori | |
219 | f4c94c7c | Gerd Hoffmann | int inet_connect_opts(QemuOpts *opts)
|
220 | d247d25f | aliguori | { |
221 | d247d25f | aliguori | struct addrinfo ai,*res,*e;
|
222 | f4c94c7c | Gerd Hoffmann | const char *addr; |
223 | f4c94c7c | Gerd Hoffmann | const char *port; |
224 | d247d25f | aliguori | char uaddr[INET6_ADDRSTRLEN+1]; |
225 | d247d25f | aliguori | char uport[33]; |
226 | d247d25f | aliguori | int sock,rc;
|
227 | d247d25f | aliguori | |
228 | d247d25f | aliguori | memset(&ai,0, sizeof(ai)); |
229 | d247d25f | aliguori | ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG; |
230 | d247d25f | aliguori | ai.ai_family = PF_UNSPEC; |
231 | f4c94c7c | Gerd Hoffmann | ai.ai_socktype = SOCK_STREAM; |
232 | d247d25f | aliguori | |
233 | f4c94c7c | Gerd Hoffmann | addr = qemu_opt_get(opts, "host");
|
234 | f4c94c7c | Gerd Hoffmann | port = qemu_opt_get(opts, "port");
|
235 | f4c94c7c | Gerd Hoffmann | if (addr == NULL || port == NULL) { |
236 | f4c94c7c | Gerd Hoffmann | fprintf(stderr, "inet_connect: host and/or port not specified\n");
|
237 | f4c94c7c | Gerd Hoffmann | return -1; |
238 | d247d25f | aliguori | } |
239 | d247d25f | aliguori | |
240 | f4c94c7c | Gerd Hoffmann | if (qemu_opt_get_bool(opts, "ipv4", 0)) |
241 | d247d25f | aliguori | ai.ai_family = PF_INET; |
242 | f4c94c7c | Gerd Hoffmann | if (qemu_opt_get_bool(opts, "ipv6", 0)) |
243 | d247d25f | aliguori | ai.ai_family = PF_INET6; |
244 | d247d25f | aliguori | |
245 | d247d25f | aliguori | /* lookup */
|
246 | d247d25f | aliguori | if (0 != (rc = getaddrinfo(addr, port, &ai, &res))) { |
247 | f4c94c7c | Gerd Hoffmann | fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
|
248 | f4c94c7c | Gerd Hoffmann | gai_strerror(rc)); |
249 | d247d25f | aliguori | return -1; |
250 | d247d25f | aliguori | } |
251 | d247d25f | aliguori | if (sockets_debug)
|
252 | d247d25f | aliguori | inet_print_addrinfo(__FUNCTION__, res); |
253 | d247d25f | aliguori | |
254 | d247d25f | aliguori | for (e = res; e != NULL; e = e->ai_next) { |
255 | 39b6efc8 | vibi | if (getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen, |
256 | 39b6efc8 | vibi | uaddr,INET6_ADDRSTRLEN,uport,32,
|
257 | 39b6efc8 | vibi | NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
|
258 | d247d25f | aliguori | fprintf(stderr,"%s: getnameinfo: oops\n", __FUNCTION__);
|
259 | 39b6efc8 | vibi | continue;
|
260 | 39b6efc8 | vibi | } |
261 | 40ff6d7e | Kevin Wolf | sock = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol); |
262 | 39b6efc8 | vibi | if (sock < 0) { |
263 | d247d25f | aliguori | fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
|
264 | 39b6efc8 | vibi | inet_strfamily(e->ai_family), strerror(errno)); |
265 | 39b6efc8 | vibi | continue;
|
266 | 39b6efc8 | vibi | } |
267 | d247d25f | aliguori | setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); |
268 | d247d25f | aliguori | |
269 | 39b6efc8 | vibi | /* connect to peer */
|
270 | 39b6efc8 | vibi | if (connect(sock,e->ai_addr,e->ai_addrlen) < 0) { |
271 | d247d25f | aliguori | if (sockets_debug || NULL == e->ai_next) |
272 | d247d25f | aliguori | fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
|
273 | d247d25f | aliguori | inet_strfamily(e->ai_family), |
274 | d247d25f | aliguori | e->ai_canonname, uaddr, uport, strerror(errno)); |
275 | d247d25f | aliguori | closesocket(sock); |
276 | 39b6efc8 | vibi | continue;
|
277 | 39b6efc8 | vibi | } |
278 | d247d25f | aliguori | if (sockets_debug)
|
279 | d247d25f | aliguori | fprintf(stderr, "%s: connect(%s,%s,%s,%s): OK\n", __FUNCTION__,
|
280 | d247d25f | aliguori | inet_strfamily(e->ai_family), |
281 | d247d25f | aliguori | e->ai_canonname, uaddr, uport); |
282 | d247d25f | aliguori | freeaddrinfo(res); |
283 | 39b6efc8 | vibi | return sock;
|
284 | d247d25f | aliguori | } |
285 | d247d25f | aliguori | freeaddrinfo(res); |
286 | d247d25f | aliguori | return -1; |
287 | d247d25f | aliguori | } |
288 | d247d25f | aliguori | |
289 | 7e1b35b4 | Gerd Hoffmann | int inet_dgram_opts(QemuOpts *opts)
|
290 | 7e1b35b4 | Gerd Hoffmann | { |
291 | 7e1b35b4 | Gerd Hoffmann | struct addrinfo ai, *peer = NULL, *local = NULL; |
292 | 7e1b35b4 | Gerd Hoffmann | const char *addr; |
293 | 7e1b35b4 | Gerd Hoffmann | const char *port; |
294 | 7e1b35b4 | Gerd Hoffmann | char uaddr[INET6_ADDRSTRLEN+1]; |
295 | 7e1b35b4 | Gerd Hoffmann | char uport[33]; |
296 | 7e1b35b4 | Gerd Hoffmann | int sock = -1, rc; |
297 | 7e1b35b4 | Gerd Hoffmann | |
298 | 7e1b35b4 | Gerd Hoffmann | /* lookup peer addr */
|
299 | 7e1b35b4 | Gerd Hoffmann | memset(&ai,0, sizeof(ai)); |
300 | 7e1b35b4 | Gerd Hoffmann | ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG; |
301 | 7e1b35b4 | Gerd Hoffmann | ai.ai_family = PF_UNSPEC; |
302 | 7e1b35b4 | Gerd Hoffmann | ai.ai_socktype = SOCK_DGRAM; |
303 | 7e1b35b4 | Gerd Hoffmann | |
304 | 7e1b35b4 | Gerd Hoffmann | addr = qemu_opt_get(opts, "host");
|
305 | 7e1b35b4 | Gerd Hoffmann | port = qemu_opt_get(opts, "port");
|
306 | 7e1b35b4 | Gerd Hoffmann | if (addr == NULL || strlen(addr) == 0) { |
307 | 7e1b35b4 | Gerd Hoffmann | addr = "localhost";
|
308 | 7e1b35b4 | Gerd Hoffmann | } |
309 | 7e1b35b4 | Gerd Hoffmann | if (port == NULL || strlen(port) == 0) { |
310 | 7e1b35b4 | Gerd Hoffmann | fprintf(stderr, "inet_dgram: port not specified\n");
|
311 | 7e1b35b4 | Gerd Hoffmann | return -1; |
312 | 7e1b35b4 | Gerd Hoffmann | } |
313 | 7e1b35b4 | Gerd Hoffmann | |
314 | 7e1b35b4 | Gerd Hoffmann | if (qemu_opt_get_bool(opts, "ipv4", 0)) |
315 | 7e1b35b4 | Gerd Hoffmann | ai.ai_family = PF_INET; |
316 | 7e1b35b4 | Gerd Hoffmann | if (qemu_opt_get_bool(opts, "ipv6", 0)) |
317 | 7e1b35b4 | Gerd Hoffmann | ai.ai_family = PF_INET6; |
318 | 7e1b35b4 | Gerd Hoffmann | |
319 | 7e1b35b4 | Gerd Hoffmann | if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) { |
320 | 7e1b35b4 | Gerd Hoffmann | fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
|
321 | 7e1b35b4 | Gerd Hoffmann | gai_strerror(rc)); |
322 | 7e1b35b4 | Gerd Hoffmann | return -1; |
323 | 7e1b35b4 | Gerd Hoffmann | } |
324 | 7e1b35b4 | Gerd Hoffmann | if (sockets_debug) {
|
325 | 7e1b35b4 | Gerd Hoffmann | fprintf(stderr, "%s: peer (%s:%s)\n", __FUNCTION__, addr, port);
|
326 | 7e1b35b4 | Gerd Hoffmann | inet_print_addrinfo(__FUNCTION__, peer); |
327 | 7e1b35b4 | Gerd Hoffmann | } |
328 | 7e1b35b4 | Gerd Hoffmann | |
329 | 7e1b35b4 | Gerd Hoffmann | /* lookup local addr */
|
330 | 7e1b35b4 | Gerd Hoffmann | memset(&ai,0, sizeof(ai)); |
331 | 7e1b35b4 | Gerd Hoffmann | ai.ai_flags = AI_PASSIVE; |
332 | 7e1b35b4 | Gerd Hoffmann | ai.ai_family = peer->ai_family; |
333 | 7e1b35b4 | Gerd Hoffmann | ai.ai_socktype = SOCK_DGRAM; |
334 | 7e1b35b4 | Gerd Hoffmann | |
335 | 7e1b35b4 | Gerd Hoffmann | addr = qemu_opt_get(opts, "localaddr");
|
336 | 7e1b35b4 | Gerd Hoffmann | port = qemu_opt_get(opts, "localport");
|
337 | 7e1b35b4 | Gerd Hoffmann | if (addr == NULL || strlen(addr) == 0) { |
338 | 7e1b35b4 | Gerd Hoffmann | addr = NULL;
|
339 | 7e1b35b4 | Gerd Hoffmann | } |
340 | 7e1b35b4 | Gerd Hoffmann | if (!port || strlen(port) == 0) |
341 | 7e1b35b4 | Gerd Hoffmann | port = "0";
|
342 | 7e1b35b4 | Gerd Hoffmann | |
343 | 7e1b35b4 | Gerd Hoffmann | if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) { |
344 | 7e1b35b4 | Gerd Hoffmann | fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
|
345 | 7e1b35b4 | Gerd Hoffmann | gai_strerror(rc)); |
346 | 7e1b35b4 | Gerd Hoffmann | return -1; |
347 | 7e1b35b4 | Gerd Hoffmann | } |
348 | 7e1b35b4 | Gerd Hoffmann | if (sockets_debug) {
|
349 | 7e1b35b4 | Gerd Hoffmann | fprintf(stderr, "%s: local (%s:%s)\n", __FUNCTION__, addr, port);
|
350 | 7e1b35b4 | Gerd Hoffmann | inet_print_addrinfo(__FUNCTION__, local); |
351 | 7e1b35b4 | Gerd Hoffmann | } |
352 | 7e1b35b4 | Gerd Hoffmann | |
353 | 7e1b35b4 | Gerd Hoffmann | /* create socket */
|
354 | 40ff6d7e | Kevin Wolf | sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol); |
355 | 7e1b35b4 | Gerd Hoffmann | if (sock < 0) { |
356 | 7e1b35b4 | Gerd Hoffmann | fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
|
357 | 7e1b35b4 | Gerd Hoffmann | inet_strfamily(peer->ai_family), strerror(errno)); |
358 | 7e1b35b4 | Gerd Hoffmann | goto err;
|
359 | 7e1b35b4 | Gerd Hoffmann | } |
360 | 7e1b35b4 | Gerd Hoffmann | setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); |
361 | 7e1b35b4 | Gerd Hoffmann | |
362 | 7e1b35b4 | Gerd Hoffmann | /* bind socket */
|
363 | 7e1b35b4 | Gerd Hoffmann | if (getnameinfo((struct sockaddr*)local->ai_addr,local->ai_addrlen, |
364 | 7e1b35b4 | Gerd Hoffmann | uaddr,INET6_ADDRSTRLEN,uport,32,
|
365 | 7e1b35b4 | Gerd Hoffmann | NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
|
366 | 7e1b35b4 | Gerd Hoffmann | fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
|
367 | 7e1b35b4 | Gerd Hoffmann | goto err;
|
368 | 7e1b35b4 | Gerd Hoffmann | } |
369 | 7e1b35b4 | Gerd Hoffmann | if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) { |
370 | 7e1b35b4 | Gerd Hoffmann | fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,
|
371 | 7e1b35b4 | Gerd Hoffmann | inet_strfamily(local->ai_family), uaddr, inet_getport(local)); |
372 | 7e1b35b4 | Gerd Hoffmann | goto err;
|
373 | 7e1b35b4 | Gerd Hoffmann | } |
374 | 7e1b35b4 | Gerd Hoffmann | |
375 | 7e1b35b4 | Gerd Hoffmann | /* connect to peer */
|
376 | 7e1b35b4 | Gerd Hoffmann | if (getnameinfo((struct sockaddr*)peer->ai_addr, peer->ai_addrlen, |
377 | 7e1b35b4 | Gerd Hoffmann | uaddr, INET6_ADDRSTRLEN, uport, 32,
|
378 | 7e1b35b4 | Gerd Hoffmann | NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
|
379 | 7e1b35b4 | Gerd Hoffmann | fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
|
380 | 7e1b35b4 | Gerd Hoffmann | goto err;
|
381 | 7e1b35b4 | Gerd Hoffmann | } |
382 | 7e1b35b4 | Gerd Hoffmann | if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) { |
383 | 7e1b35b4 | Gerd Hoffmann | fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
|
384 | 7e1b35b4 | Gerd Hoffmann | inet_strfamily(peer->ai_family), |
385 | 7e1b35b4 | Gerd Hoffmann | peer->ai_canonname, uaddr, uport, strerror(errno)); |
386 | 7e1b35b4 | Gerd Hoffmann | goto err;
|
387 | 7e1b35b4 | Gerd Hoffmann | } |
388 | 7e1b35b4 | Gerd Hoffmann | |
389 | 7e1b35b4 | Gerd Hoffmann | freeaddrinfo(local); |
390 | 7e1b35b4 | Gerd Hoffmann | freeaddrinfo(peer); |
391 | 7e1b35b4 | Gerd Hoffmann | return sock;
|
392 | 7e1b35b4 | Gerd Hoffmann | |
393 | 7e1b35b4 | Gerd Hoffmann | err:
|
394 | 7e1b35b4 | Gerd Hoffmann | if (-1 != sock) |
395 | 7e1b35b4 | Gerd Hoffmann | closesocket(sock); |
396 | 7e1b35b4 | Gerd Hoffmann | if (local)
|
397 | 7e1b35b4 | Gerd Hoffmann | freeaddrinfo(local); |
398 | 7e1b35b4 | Gerd Hoffmann | if (peer)
|
399 | 7e1b35b4 | Gerd Hoffmann | freeaddrinfo(peer); |
400 | 7e1b35b4 | Gerd Hoffmann | return -1; |
401 | 7e1b35b4 | Gerd Hoffmann | } |
402 | 7e1b35b4 | Gerd Hoffmann | |
403 | f4c94c7c | Gerd Hoffmann | /* compatibility wrapper */
|
404 | f4c94c7c | Gerd Hoffmann | static int inet_parse(QemuOpts *opts, const char *str) |
405 | f4c94c7c | Gerd Hoffmann | { |
406 | f4c94c7c | Gerd Hoffmann | const char *optstr, *h; |
407 | f4c94c7c | Gerd Hoffmann | char addr[64]; |
408 | f4c94c7c | Gerd Hoffmann | char port[33]; |
409 | f4c94c7c | Gerd Hoffmann | int pos;
|
410 | f4c94c7c | Gerd Hoffmann | |
411 | f4c94c7c | Gerd Hoffmann | /* parse address */
|
412 | f4c94c7c | Gerd Hoffmann | if (str[0] == ':') { |
413 | f4c94c7c | Gerd Hoffmann | /* no host given */
|
414 | f4c94c7c | Gerd Hoffmann | addr[0] = '\0'; |
415 | f4c94c7c | Gerd Hoffmann | if (1 != sscanf(str,":%32[^,]%n",port,&pos)) { |
416 | f4c94c7c | Gerd Hoffmann | fprintf(stderr, "%s: portonly parse error (%s)\n",
|
417 | f4c94c7c | Gerd Hoffmann | __FUNCTION__, str); |
418 | f4c94c7c | Gerd Hoffmann | return -1; |
419 | f4c94c7c | Gerd Hoffmann | } |
420 | f4c94c7c | Gerd Hoffmann | } else if (str[0] == '[') { |
421 | f4c94c7c | Gerd Hoffmann | /* IPv6 addr */
|
422 | f4c94c7c | Gerd Hoffmann | if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) { |
423 | f4c94c7c | Gerd Hoffmann | fprintf(stderr, "%s: ipv6 parse error (%s)\n",
|
424 | f4c94c7c | Gerd Hoffmann | __FUNCTION__, str); |
425 | f4c94c7c | Gerd Hoffmann | return -1; |
426 | f4c94c7c | Gerd Hoffmann | } |
427 | f4c94c7c | Gerd Hoffmann | qemu_opt_set(opts, "ipv6", "yes"); |
428 | f4c94c7c | Gerd Hoffmann | } else if (qemu_isdigit(str[0])) { |
429 | f4c94c7c | Gerd Hoffmann | /* IPv4 addr */
|
430 | f4c94c7c | Gerd Hoffmann | if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) { |
431 | f4c94c7c | Gerd Hoffmann | fprintf(stderr, "%s: ipv4 parse error (%s)\n",
|
432 | f4c94c7c | Gerd Hoffmann | __FUNCTION__, str); |
433 | f4c94c7c | Gerd Hoffmann | return -1; |
434 | f4c94c7c | Gerd Hoffmann | } |
435 | f4c94c7c | Gerd Hoffmann | qemu_opt_set(opts, "ipv4", "yes"); |
436 | f4c94c7c | Gerd Hoffmann | } else {
|
437 | f4c94c7c | Gerd Hoffmann | /* hostname */
|
438 | f4c94c7c | Gerd Hoffmann | if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) { |
439 | f4c94c7c | Gerd Hoffmann | fprintf(stderr, "%s: hostname parse error (%s)\n",
|
440 | f4c94c7c | Gerd Hoffmann | __FUNCTION__, str); |
441 | f4c94c7c | Gerd Hoffmann | return -1; |
442 | f4c94c7c | Gerd Hoffmann | } |
443 | f4c94c7c | Gerd Hoffmann | } |
444 | f4c94c7c | Gerd Hoffmann | qemu_opt_set(opts, "host", addr);
|
445 | f4c94c7c | Gerd Hoffmann | qemu_opt_set(opts, "port", port);
|
446 | f4c94c7c | Gerd Hoffmann | |
447 | f4c94c7c | Gerd Hoffmann | /* parse options */
|
448 | f4c94c7c | Gerd Hoffmann | optstr = str + pos; |
449 | f4c94c7c | Gerd Hoffmann | h = strstr(optstr, ",to=");
|
450 | f4c94c7c | Gerd Hoffmann | if (h)
|
451 | f4c94c7c | Gerd Hoffmann | qemu_opt_set(opts, "to", h+4); |
452 | f4c94c7c | Gerd Hoffmann | if (strstr(optstr, ",ipv4")) |
453 | f4c94c7c | Gerd Hoffmann | qemu_opt_set(opts, "ipv4", "yes"); |
454 | f4c94c7c | Gerd Hoffmann | if (strstr(optstr, ",ipv6")) |
455 | f4c94c7c | Gerd Hoffmann | qemu_opt_set(opts, "ipv6", "yes"); |
456 | f4c94c7c | Gerd Hoffmann | return 0; |
457 | f4c94c7c | Gerd Hoffmann | } |
458 | f4c94c7c | Gerd Hoffmann | |
459 | e5bc776f | Gerd Hoffmann | int inet_listen(const char *str, char *ostr, int olen, |
460 | e5bc776f | Gerd Hoffmann | int socktype, int port_offset) |
461 | e5bc776f | Gerd Hoffmann | { |
462 | e5bc776f | Gerd Hoffmann | QemuOpts *opts; |
463 | e5bc776f | Gerd Hoffmann | char *optstr;
|
464 | e5bc776f | Gerd Hoffmann | int sock = -1; |
465 | e5bc776f | Gerd Hoffmann | |
466 | e5bc776f | Gerd Hoffmann | opts = qemu_opts_create(&dummy_opts, NULL, 0); |
467 | e5bc776f | Gerd Hoffmann | if (inet_parse(opts, str) == 0) { |
468 | e5bc776f | Gerd Hoffmann | sock = inet_listen_opts(opts, port_offset); |
469 | e5bc776f | Gerd Hoffmann | if (sock != -1 && ostr) { |
470 | e5bc776f | Gerd Hoffmann | optstr = strchr(str, ',');
|
471 | e5bc776f | Gerd Hoffmann | if (qemu_opt_get_bool(opts, "ipv6", 0)) { |
472 | e5bc776f | Gerd Hoffmann | snprintf(ostr, olen, "[%s]:%s%s",
|
473 | e5bc776f | Gerd Hoffmann | qemu_opt_get(opts, "host"),
|
474 | e5bc776f | Gerd Hoffmann | qemu_opt_get(opts, "port"),
|
475 | e5bc776f | Gerd Hoffmann | optstr ? optstr : "");
|
476 | e5bc776f | Gerd Hoffmann | } else {
|
477 | e5bc776f | Gerd Hoffmann | snprintf(ostr, olen, "%s:%s%s",
|
478 | e5bc776f | Gerd Hoffmann | qemu_opt_get(opts, "host"),
|
479 | e5bc776f | Gerd Hoffmann | qemu_opt_get(opts, "port"),
|
480 | e5bc776f | Gerd Hoffmann | optstr ? optstr : "");
|
481 | e5bc776f | Gerd Hoffmann | } |
482 | e5bc776f | Gerd Hoffmann | } |
483 | e5bc776f | Gerd Hoffmann | } |
484 | e5bc776f | Gerd Hoffmann | qemu_opts_del(opts); |
485 | e5bc776f | Gerd Hoffmann | return sock;
|
486 | e5bc776f | Gerd Hoffmann | } |
487 | e5bc776f | Gerd Hoffmann | |
488 | f4c94c7c | Gerd Hoffmann | int inet_connect(const char *str, int socktype) |
489 | f4c94c7c | Gerd Hoffmann | { |
490 | f4c94c7c | Gerd Hoffmann | QemuOpts *opts; |
491 | f4c94c7c | Gerd Hoffmann | int sock = -1; |
492 | f4c94c7c | Gerd Hoffmann | |
493 | f4c94c7c | Gerd Hoffmann | opts = qemu_opts_create(&dummy_opts, NULL, 0); |
494 | f4c94c7c | Gerd Hoffmann | if (inet_parse(opts, str) == 0) |
495 | f4c94c7c | Gerd Hoffmann | sock = inet_connect_opts(opts); |
496 | f4c94c7c | Gerd Hoffmann | qemu_opts_del(opts); |
497 | f4c94c7c | Gerd Hoffmann | return sock;
|
498 | f4c94c7c | Gerd Hoffmann | } |
499 | f4c94c7c | Gerd Hoffmann | |
500 | d247d25f | aliguori | #ifndef _WIN32
|
501 | d247d25f | aliguori | |
502 | 62b6adfb | Gerd Hoffmann | int unix_listen_opts(QemuOpts *opts)
|
503 | d247d25f | aliguori | { |
504 | d247d25f | aliguori | struct sockaddr_un un;
|
505 | 62b6adfb | Gerd Hoffmann | const char *path = qemu_opt_get(opts, "path"); |
506 | 62b6adfb | Gerd Hoffmann | int sock, fd;
|
507 | d247d25f | aliguori | |
508 | 40ff6d7e | Kevin Wolf | sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
|
509 | d247d25f | aliguori | if (sock < 0) { |
510 | 39b6efc8 | vibi | perror("socket(unix)");
|
511 | 39b6efc8 | vibi | return -1; |
512 | d247d25f | aliguori | } |
513 | d247d25f | aliguori | |
514 | d247d25f | aliguori | memset(&un, 0, sizeof(un)); |
515 | d247d25f | aliguori | un.sun_family = AF_UNIX; |
516 | d247d25f | aliguori | if (path && strlen(path)) {
|
517 | d247d25f | aliguori | snprintf(un.sun_path, sizeof(un.sun_path), "%s", path); |
518 | d247d25f | aliguori | } else {
|
519 | d247d25f | aliguori | char *tmpdir = getenv("TMPDIR"); |
520 | d247d25f | aliguori | snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX", |
521 | d247d25f | aliguori | tmpdir ? tmpdir : "/tmp");
|
522 | d247d25f | aliguori | /*
|
523 | d247d25f | aliguori | * This dummy fd usage silences the mktemp() unsecure warning.
|
524 | d247d25f | aliguori | * Using mkstemp() doesn't make things more secure here
|
525 | d247d25f | aliguori | * though. bind() complains about existing files, so we have
|
526 | d247d25f | aliguori | * to unlink first and thus re-open the race window. The
|
527 | d247d25f | aliguori | * worst case possible is bind() failing, i.e. a DoS attack.
|
528 | d247d25f | aliguori | */
|
529 | d247d25f | aliguori | fd = mkstemp(un.sun_path); close(fd); |
530 | 62b6adfb | Gerd Hoffmann | qemu_opt_set(opts, "path", un.sun_path);
|
531 | d247d25f | aliguori | } |
532 | d247d25f | aliguori | |
533 | d247d25f | aliguori | unlink(un.sun_path); |
534 | d247d25f | aliguori | if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) { |
535 | d247d25f | aliguori | fprintf(stderr, "bind(unix:%s): %s\n", un.sun_path, strerror(errno));
|
536 | d247d25f | aliguori | goto err;
|
537 | d247d25f | aliguori | } |
538 | d247d25f | aliguori | if (listen(sock, 1) < 0) { |
539 | d247d25f | aliguori | fprintf(stderr, "listen(unix:%s): %s\n", un.sun_path, strerror(errno));
|
540 | d247d25f | aliguori | goto err;
|
541 | d247d25f | aliguori | } |
542 | d247d25f | aliguori | |
543 | d247d25f | aliguori | if (sockets_debug)
|
544 | d247d25f | aliguori | fprintf(stderr, "bind(unix:%s): OK\n", un.sun_path);
|
545 | d247d25f | aliguori | return sock;
|
546 | d247d25f | aliguori | |
547 | d247d25f | aliguori | err:
|
548 | d247d25f | aliguori | closesocket(sock); |
549 | d247d25f | aliguori | return -1; |
550 | d247d25f | aliguori | } |
551 | d247d25f | aliguori | |
552 | 2af2bf67 | Gerd Hoffmann | int unix_connect_opts(QemuOpts *opts)
|
553 | d247d25f | aliguori | { |
554 | d247d25f | aliguori | struct sockaddr_un un;
|
555 | 2af2bf67 | Gerd Hoffmann | const char *path = qemu_opt_get(opts, "path"); |
556 | d247d25f | aliguori | int sock;
|
557 | d247d25f | aliguori | |
558 | 2af2bf67 | Gerd Hoffmann | if (NULL == path) { |
559 | 2af2bf67 | Gerd Hoffmann | fprintf(stderr, "unix connect: no path specified\n");
|
560 | 2af2bf67 | Gerd Hoffmann | return -1; |
561 | 2af2bf67 | Gerd Hoffmann | } |
562 | 2af2bf67 | Gerd Hoffmann | |
563 | 40ff6d7e | Kevin Wolf | sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
|
564 | d247d25f | aliguori | if (sock < 0) { |
565 | 39b6efc8 | vibi | perror("socket(unix)");
|
566 | 39b6efc8 | vibi | return -1; |
567 | d247d25f | aliguori | } |
568 | d247d25f | aliguori | |
569 | d247d25f | aliguori | memset(&un, 0, sizeof(un)); |
570 | d247d25f | aliguori | un.sun_family = AF_UNIX; |
571 | d247d25f | aliguori | snprintf(un.sun_path, sizeof(un.sun_path), "%s", path); |
572 | d247d25f | aliguori | if (connect(sock, (struct sockaddr*) &un, sizeof(un)) < 0) { |
573 | d247d25f | aliguori | fprintf(stderr, "connect(unix:%s): %s\n", path, strerror(errno));
|
574 | d247d25f | aliguori | return -1; |
575 | d247d25f | aliguori | } |
576 | d247d25f | aliguori | |
577 | d247d25f | aliguori | if (sockets_debug)
|
578 | d247d25f | aliguori | fprintf(stderr, "connect(unix:%s): OK\n", path);
|
579 | d247d25f | aliguori | return sock;
|
580 | d247d25f | aliguori | } |
581 | d247d25f | aliguori | |
582 | 2af2bf67 | Gerd Hoffmann | /* compatibility wrapper */
|
583 | 62b6adfb | Gerd Hoffmann | int unix_listen(const char *str, char *ostr, int olen) |
584 | 62b6adfb | Gerd Hoffmann | { |
585 | 62b6adfb | Gerd Hoffmann | QemuOpts *opts; |
586 | 62b6adfb | Gerd Hoffmann | char *path, *optstr;
|
587 | 62b6adfb | Gerd Hoffmann | int sock, len;
|
588 | 62b6adfb | Gerd Hoffmann | |
589 | 62b6adfb | Gerd Hoffmann | opts = qemu_opts_create(&dummy_opts, NULL, 0); |
590 | 62b6adfb | Gerd Hoffmann | |
591 | 62b6adfb | Gerd Hoffmann | optstr = strchr(str, ',');
|
592 | 62b6adfb | Gerd Hoffmann | if (optstr) {
|
593 | 62b6adfb | Gerd Hoffmann | len = optstr - str; |
594 | 62b6adfb | Gerd Hoffmann | if (len) {
|
595 | 62b6adfb | Gerd Hoffmann | path = qemu_malloc(len+1);
|
596 | 62b6adfb | Gerd Hoffmann | snprintf(path, len+1, "%.*s", len, str); |
597 | 62b6adfb | Gerd Hoffmann | qemu_opt_set(opts, "path", path);
|
598 | 62b6adfb | Gerd Hoffmann | qemu_free(path); |
599 | 62b6adfb | Gerd Hoffmann | } |
600 | 62b6adfb | Gerd Hoffmann | } else {
|
601 | 62b6adfb | Gerd Hoffmann | qemu_opt_set(opts, "path", str);
|
602 | 62b6adfb | Gerd Hoffmann | } |
603 | 62b6adfb | Gerd Hoffmann | |
604 | 62b6adfb | Gerd Hoffmann | sock = unix_listen_opts(opts); |
605 | 62b6adfb | Gerd Hoffmann | |
606 | 62b6adfb | Gerd Hoffmann | if (sock != -1 && ostr) |
607 | 62b6adfb | Gerd Hoffmann | snprintf(ostr, olen, "%s%s", qemu_opt_get(opts, "path"), optstr ? optstr : ""); |
608 | 62b6adfb | Gerd Hoffmann | qemu_opts_del(opts); |
609 | 62b6adfb | Gerd Hoffmann | return sock;
|
610 | 62b6adfb | Gerd Hoffmann | } |
611 | 62b6adfb | Gerd Hoffmann | |
612 | 2af2bf67 | Gerd Hoffmann | int unix_connect(const char *path) |
613 | 2af2bf67 | Gerd Hoffmann | { |
614 | 2af2bf67 | Gerd Hoffmann | QemuOpts *opts; |
615 | 2af2bf67 | Gerd Hoffmann | int sock;
|
616 | 2af2bf67 | Gerd Hoffmann | |
617 | 2af2bf67 | Gerd Hoffmann | opts = qemu_opts_create(&dummy_opts, NULL, 0); |
618 | 2af2bf67 | Gerd Hoffmann | qemu_opt_set(opts, "path", path);
|
619 | 2af2bf67 | Gerd Hoffmann | sock = unix_connect_opts(opts); |
620 | 2af2bf67 | Gerd Hoffmann | qemu_opts_del(opts); |
621 | 2af2bf67 | Gerd Hoffmann | return sock;
|
622 | 2af2bf67 | Gerd Hoffmann | } |
623 | 2af2bf67 | Gerd Hoffmann | |
624 | d247d25f | aliguori | #else
|
625 | d247d25f | aliguori | |
626 | 108af7b9 | Gerd Hoffmann | int unix_listen_opts(QemuOpts *opts)
|
627 | 108af7b9 | Gerd Hoffmann | { |
628 | 108af7b9 | Gerd Hoffmann | fprintf(stderr, "unix sockets are not available on windows\n");
|
629 | 108af7b9 | Gerd Hoffmann | return -1; |
630 | 108af7b9 | Gerd Hoffmann | } |
631 | 108af7b9 | Gerd Hoffmann | |
632 | 108af7b9 | Gerd Hoffmann | int unix_connect_opts(QemuOpts *opts)
|
633 | 108af7b9 | Gerd Hoffmann | { |
634 | 108af7b9 | Gerd Hoffmann | fprintf(stderr, "unix sockets are not available on windows\n");
|
635 | 108af7b9 | Gerd Hoffmann | return -1; |
636 | 108af7b9 | Gerd Hoffmann | } |
637 | 108af7b9 | Gerd Hoffmann | |
638 | d247d25f | aliguori | int unix_listen(const char *path, char *ostr, int olen) |
639 | d247d25f | aliguori | { |
640 | d247d25f | aliguori | fprintf(stderr, "unix sockets are not available on windows\n");
|
641 | d247d25f | aliguori | return -1; |
642 | d247d25f | aliguori | } |
643 | d247d25f | aliguori | |
644 | d247d25f | aliguori | int unix_connect(const char *path) |
645 | d247d25f | aliguori | { |
646 | d247d25f | aliguori | fprintf(stderr, "unix sockets are not available on windows\n");
|
647 | d247d25f | aliguori | return -1; |
648 | d247d25f | aliguori | } |
649 | d247d25f | aliguori | |
650 | d247d25f | aliguori | #endif |