]>
git.proxmox.com Git - mirror_qemu.git/blob - qemu-sockets.c
2 * inet and unix socket functions for qemu
4 * (c) 2008 Gerd Hoffmann <kraxel@redhat.com>
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.
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.
22 #include "qemu_socket.h"
23 #include "qemu-common.h" /* for qemu_isdigit */
26 # define AI_ADDRCONFIG 0
29 static int sockets_debug
= 0;
30 static const int on
=1, off
=0;
32 /* used temporarely until all users are converted to QemuOpts */
33 QemuOptsList dummy_opts
= {
35 .head
= TAILQ_HEAD_INITIALIZER(dummy_opts
.head
),
39 .type
= QEMU_OPT_STRING
,
45 static int inet_getport(struct addrinfo
*e
)
47 struct sockaddr_in
*i4
;
48 struct sockaddr_in6
*i6
;
50 switch (e
->ai_family
) {
52 i6
= (void*)e
->ai_addr
;
53 return ntohs(i6
->sin6_port
);
55 i4
= (void*)e
->ai_addr
;
56 return ntohs(i4
->sin_port
);
62 static void inet_setport(struct addrinfo
*e
, int port
)
64 struct sockaddr_in
*i4
;
65 struct sockaddr_in6
*i6
;
67 switch (e
->ai_family
) {
69 i6
= (void*)e
->ai_addr
;
70 i6
->sin6_port
= htons(port
);
73 i4
= (void*)e
->ai_addr
;
74 i4
->sin_port
= htons(port
);
79 static const char *inet_strfamily(int family
)
82 case PF_INET6
: return "ipv6";
83 case PF_INET
: return "ipv4";
84 case PF_UNIX
: return "unix";
89 static void inet_print_addrinfo(const char *tag
, struct addrinfo
*res
)
92 char uaddr
[INET6_ADDRSTRLEN
+1];
95 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
96 getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
97 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
98 NI_NUMERICHOST
| NI_NUMERICSERV
);
99 fprintf(stderr
,"%s: getaddrinfo: family %s, host %s, port %s\n",
100 tag
, inet_strfamily(e
->ai_family
), uaddr
, uport
);
104 int inet_listen(const char *str
, char *ostr
, int olen
,
105 int socktype
, int port_offset
)
107 struct addrinfo ai
,*res
,*e
;
110 char uaddr
[INET6_ADDRSTRLEN
+1];
112 const char *opts
, *h
;
113 int slisten
,rc
,pos
,to
,try_next
;
115 memset(&ai
,0, sizeof(ai
));
116 ai
.ai_flags
= AI_PASSIVE
| AI_ADDRCONFIG
;
117 ai
.ai_family
= PF_UNSPEC
;
118 ai
.ai_socktype
= socktype
;
124 if (1 != sscanf(str
,":%32[^,]%n",port
,&pos
)) {
125 fprintf(stderr
, "%s: portonly parse error (%s)\n",
129 } else if (str
[0] == '[') {
131 if (2 != sscanf(str
,"[%64[^]]]:%32[^,]%n",addr
,port
,&pos
)) {
132 fprintf(stderr
, "%s: ipv6 parse error (%s)\n",
136 ai
.ai_family
= PF_INET6
;
137 } else if (qemu_isdigit(str
[0])) {
139 if (2 != sscanf(str
,"%64[0-9.]:%32[^,]%n",addr
,port
,&pos
)) {
140 fprintf(stderr
, "%s: ipv4 parse error (%s)\n",
144 ai
.ai_family
= PF_INET
;
147 if (2 != sscanf(str
,"%64[^:]:%32[^,]%n",addr
,port
,&pos
)) {
148 fprintf(stderr
, "%s: hostname parse error (%s)\n",
156 h
= strstr(opts
, ",to=");
157 to
= h
? atoi(h
+4) : 0;
158 if (strstr(opts
, ",ipv4"))
159 ai
.ai_family
= PF_INET
;
160 if (strstr(opts
, ",ipv6"))
161 ai
.ai_family
= PF_INET6
;
165 snprintf(port
, sizeof(port
), "%d", atoi(port
) + port_offset
);
166 rc
= getaddrinfo(strlen(addr
) ? addr
: NULL
, port
, &ai
, &res
);
168 fprintf(stderr
,"%s: getaddrinfo(%s,%s): %s\n", __FUNCTION__
,
169 addr
, port
, gai_strerror(rc
));
173 inet_print_addrinfo(__FUNCTION__
, res
);
175 /* create socket + bind */
176 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
177 getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
178 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
179 NI_NUMERICHOST
| NI_NUMERICSERV
);
180 slisten
= socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
182 fprintf(stderr
,"%s: socket(%s): %s\n", __FUNCTION__
,
183 inet_strfamily(e
->ai_family
), strerror(errno
));
187 setsockopt(slisten
,SOL_SOCKET
,SO_REUSEADDR
,(void*)&on
,sizeof(on
));
189 if (e
->ai_family
== PF_INET6
) {
190 /* listen on both ipv4 and ipv6 */
191 setsockopt(slisten
,IPPROTO_IPV6
,IPV6_V6ONLY
,(void*)&off
,
197 if (bind(slisten
, e
->ai_addr
, e
->ai_addrlen
) == 0) {
199 fprintf(stderr
,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__
,
200 inet_strfamily(e
->ai_family
), uaddr
, inet_getport(e
));
203 try_next
= to
&& (inet_getport(e
) <= to
+ port_offset
);
204 if (!try_next
|| sockets_debug
)
205 fprintf(stderr
,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__
,
206 inet_strfamily(e
->ai_family
), uaddr
, inet_getport(e
),
209 inet_setport(e
, inet_getport(e
) + 1);
214 closesocket(slisten
);
216 fprintf(stderr
, "%s: FAILED\n", __FUNCTION__
);
221 if (listen(slisten
,1) != 0) {
223 closesocket(slisten
);
228 if (e
->ai_family
== PF_INET6
) {
229 snprintf(ostr
, olen
, "[%s]:%d%s", uaddr
,
230 inet_getport(e
) - port_offset
, opts
);
232 snprintf(ostr
, olen
, "%s:%d%s", uaddr
,
233 inet_getport(e
) - port_offset
, opts
);
240 int inet_connect(const char *str
, int socktype
)
242 struct addrinfo ai
,*res
,*e
;
245 char uaddr
[INET6_ADDRSTRLEN
+1];
249 memset(&ai
,0, sizeof(ai
));
250 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
251 ai
.ai_family
= PF_UNSPEC
;
252 ai
.ai_socktype
= socktype
;
257 if (2 != sscanf(str
,"[%64[^]]]:%32[^,]",addr
,port
)) {
258 fprintf(stderr
, "%s: ipv6 parse error (%s)\n",
262 ai
.ai_family
= PF_INET6
;
263 } else if (qemu_isdigit(str
[0])) {
265 if (2 != sscanf(str
,"%64[0-9.]:%32[^,]",addr
,port
)) {
266 fprintf(stderr
, "%s: ipv4 parse error (%s)\n",
270 ai
.ai_family
= PF_INET
;
273 if (2 != sscanf(str
,"%64[^:]:%32[^,]",addr
,port
)) {
274 fprintf(stderr
, "%s: hostname parse error (%s)\n",
281 if (strstr(str
, ",ipv4"))
282 ai
.ai_family
= PF_INET
;
283 if (strstr(str
, ",ipv6"))
284 ai
.ai_family
= PF_INET6
;
287 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &res
))) {
288 fprintf(stderr
,"getaddrinfo(%s,%s): %s\n", gai_strerror(rc
),
293 inet_print_addrinfo(__FUNCTION__
, res
);
295 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
296 if (getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
297 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
298 NI_NUMERICHOST
| NI_NUMERICSERV
) != 0) {
299 fprintf(stderr
,"%s: getnameinfo: oops\n", __FUNCTION__
);
302 sock
= socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
304 fprintf(stderr
,"%s: socket(%s): %s\n", __FUNCTION__
,
305 inet_strfamily(e
->ai_family
), strerror(errno
));
308 setsockopt(sock
,SOL_SOCKET
,SO_REUSEADDR
,(void*)&on
,sizeof(on
));
310 /* connect to peer */
311 if (connect(sock
,e
->ai_addr
,e
->ai_addrlen
) < 0) {
312 if (sockets_debug
|| NULL
== e
->ai_next
)
313 fprintf(stderr
, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__
,
314 inet_strfamily(e
->ai_family
),
315 e
->ai_canonname
, uaddr
, uport
, strerror(errno
));
320 fprintf(stderr
, "%s: connect(%s,%s,%s,%s): OK\n", __FUNCTION__
,
321 inet_strfamily(e
->ai_family
),
322 e
->ai_canonname
, uaddr
, uport
);
332 int unix_listen_opts(QemuOpts
*opts
)
334 struct sockaddr_un un
;
335 const char *path
= qemu_opt_get(opts
, "path");
338 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
340 perror("socket(unix)");
344 memset(&un
, 0, sizeof(un
));
345 un
.sun_family
= AF_UNIX
;
346 if (path
&& strlen(path
)) {
347 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
349 char *tmpdir
= getenv("TMPDIR");
350 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s/qemu-socket-XXXXXX",
351 tmpdir
? tmpdir
: "/tmp");
353 * This dummy fd usage silences the mktemp() unsecure warning.
354 * Using mkstemp() doesn't make things more secure here
355 * though. bind() complains about existing files, so we have
356 * to unlink first and thus re-open the race window. The
357 * worst case possible is bind() failing, i.e. a DoS attack.
359 fd
= mkstemp(un
.sun_path
); close(fd
);
360 qemu_opt_set(opts
, "path", un
.sun_path
);
364 if (bind(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
365 fprintf(stderr
, "bind(unix:%s): %s\n", un
.sun_path
, strerror(errno
));
368 if (listen(sock
, 1) < 0) {
369 fprintf(stderr
, "listen(unix:%s): %s\n", un
.sun_path
, strerror(errno
));
374 fprintf(stderr
, "bind(unix:%s): OK\n", un
.sun_path
);
382 int unix_connect_opts(QemuOpts
*opts
)
384 struct sockaddr_un un
;
385 const char *path
= qemu_opt_get(opts
, "path");
389 fprintf(stderr
, "unix connect: no path specified\n");
393 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
395 perror("socket(unix)");
399 memset(&un
, 0, sizeof(un
));
400 un
.sun_family
= AF_UNIX
;
401 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
402 if (connect(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
403 fprintf(stderr
, "connect(unix:%s): %s\n", path
, strerror(errno
));
408 fprintf(stderr
, "connect(unix:%s): OK\n", path
);
412 /* compatibility wrapper */
413 int unix_listen(const char *str
, char *ostr
, int olen
)
419 opts
= qemu_opts_create(&dummy_opts
, NULL
, 0);
421 optstr
= strchr(str
, ',');
425 path
= qemu_malloc(len
+1);
426 snprintf(path
, len
+1, "%.*s", len
, str
);
427 qemu_opt_set(opts
, "path", path
);
431 qemu_opt_set(opts
, "path", str
);
434 sock
= unix_listen_opts(opts
);
436 if (sock
!= -1 && ostr
)
437 snprintf(ostr
, olen
, "%s%s", qemu_opt_get(opts
, "path"), optstr
? optstr
: "");
442 int unix_connect(const char *path
)
447 opts
= qemu_opts_create(&dummy_opts
, NULL
, 0);
448 qemu_opt_set(opts
, "path", path
);
449 sock
= unix_connect_opts(opts
);
456 int unix_listen(const char *path
, char *ostr
, int olen
)
458 fprintf(stderr
, "unix sockets are not available on windows\n");
462 int unix_connect(const char *path
)
464 fprintf(stderr
, "unix sockets are not available on windows\n");