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 static int inet_getport(struct addrinfo
*e
)
34 struct sockaddr_in
*i4
;
35 struct sockaddr_in6
*i6
;
37 switch (e
->ai_family
) {
39 i6
= (void*)e
->ai_addr
;
40 return ntohs(i6
->sin6_port
);
42 i4
= (void*)e
->ai_addr
;
43 return ntohs(i4
->sin_port
);
49 static void inet_setport(struct addrinfo
*e
, int port
)
51 struct sockaddr_in
*i4
;
52 struct sockaddr_in6
*i6
;
54 switch (e
->ai_family
) {
56 i6
= (void*)e
->ai_addr
;
57 i6
->sin6_port
= htons(port
);
60 i4
= (void*)e
->ai_addr
;
61 i4
->sin_port
= htons(port
);
66 static const char *inet_strfamily(int family
)
69 case PF_INET6
: return "ipv6";
70 case PF_INET
: return "ipv4";
71 case PF_UNIX
: return "unix";
76 static void inet_print_addrinfo(const char *tag
, struct addrinfo
*res
)
79 char uaddr
[INET6_ADDRSTRLEN
+1];
82 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
83 getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
84 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
85 NI_NUMERICHOST
| NI_NUMERICSERV
);
86 fprintf(stderr
,"%s: getaddrinfo: family %s, host %s, port %s\n",
87 tag
, inet_strfamily(e
->ai_family
), uaddr
, uport
);
91 int inet_listen(const char *str
, char *ostr
, int olen
,
92 int socktype
, int port_offset
)
94 struct addrinfo ai
,*res
,*e
;
97 char uaddr
[INET6_ADDRSTRLEN
+1];
100 int slisten
,rc
,pos
,to
,try_next
;
102 memset(&ai
,0, sizeof(ai
));
103 ai
.ai_flags
= AI_PASSIVE
| AI_ADDRCONFIG
;
104 ai
.ai_family
= PF_UNSPEC
;
105 ai
.ai_socktype
= socktype
;
111 if (1 != sscanf(str
,":%32[^,]%n",port
,&pos
)) {
112 fprintf(stderr
, "%s: portonly parse error (%s)\n",
116 } else if (str
[0] == '[') {
118 if (2 != sscanf(str
,"[%64[^]]]:%32[^,]%n",addr
,port
,&pos
)) {
119 fprintf(stderr
, "%s: ipv6 parse error (%s)\n",
123 ai
.ai_family
= PF_INET6
;
124 } else if (qemu_isdigit(str
[0])) {
126 if (2 != sscanf(str
,"%64[0-9.]:%32[^,]%n",addr
,port
,&pos
)) {
127 fprintf(stderr
, "%s: ipv4 parse error (%s)\n",
131 ai
.ai_family
= PF_INET
;
134 if (2 != sscanf(str
,"%64[^:]:%32[^,]%n",addr
,port
,&pos
)) {
135 fprintf(stderr
, "%s: hostname parse error (%s)\n",
143 h
= strstr(opts
, ",to=");
144 to
= h
? atoi(h
+4) : 0;
145 if (strstr(opts
, ",ipv4"))
146 ai
.ai_family
= PF_INET
;
147 if (strstr(opts
, ",ipv6"))
148 ai
.ai_family
= PF_INET6
;
152 snprintf(port
, sizeof(port
), "%d", atoi(port
) + port_offset
);
153 rc
= getaddrinfo(strlen(addr
) ? addr
: NULL
, port
, &ai
, &res
);
155 fprintf(stderr
,"%s: getaddrinfo(%s,%s): %s\n", __FUNCTION__
,
156 addr
, port
, gai_strerror(rc
));
160 inet_print_addrinfo(__FUNCTION__
, res
);
162 /* create socket + bind */
163 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
164 getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
165 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
166 NI_NUMERICHOST
| NI_NUMERICSERV
);
167 slisten
= socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
169 fprintf(stderr
,"%s: socket(%s): %s\n", __FUNCTION__
,
170 inet_strfamily(e
->ai_family
), strerror(errno
));
174 setsockopt(slisten
,SOL_SOCKET
,SO_REUSEADDR
,(void*)&on
,sizeof(on
));
176 if (e
->ai_family
== PF_INET6
) {
177 /* listen on both ipv4 and ipv6 */
178 setsockopt(slisten
,IPPROTO_IPV6
,IPV6_V6ONLY
,(void*)&off
,sizeof(off
));
183 if (bind(slisten
, e
->ai_addr
, e
->ai_addrlen
) == 0) {
185 fprintf(stderr
,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__
,
186 inet_strfamily(e
->ai_family
), uaddr
, inet_getport(e
));
189 try_next
= to
&& (inet_getport(e
) <= to
+ port_offset
);
190 if (!try_next
|| sockets_debug
)
191 fprintf(stderr
,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__
,
192 inet_strfamily(e
->ai_family
), uaddr
, inet_getport(e
),
195 inet_setport(e
, inet_getport(e
) + 1);
200 closesocket(slisten
);
202 fprintf(stderr
, "%s: FAILED\n", __FUNCTION__
);
207 if (listen(slisten
,1) != 0) {
209 closesocket(slisten
);
213 if (e
->ai_family
== PF_INET6
) {
214 snprintf(ostr
, olen
, "[%s]:%d%s", uaddr
,
215 inet_getport(e
) - port_offset
, opts
);
217 snprintf(ostr
, olen
, "%s:%d%s", uaddr
,
218 inet_getport(e
) - port_offset
, opts
);
225 int inet_connect(const char *str
, int socktype
)
227 struct addrinfo ai
,*res
,*e
;
230 char uaddr
[INET6_ADDRSTRLEN
+1];
234 memset(&ai
,0, sizeof(ai
));
235 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
236 ai
.ai_family
= PF_UNSPEC
;
237 ai
.ai_socktype
= socktype
;
242 if (2 != sscanf(str
,"[%64[^]]]:%32[^,]",addr
,port
)) {
243 fprintf(stderr
, "%s: ipv6 parse error (%s)\n",
247 ai
.ai_family
= PF_INET6
;
248 } else if (qemu_isdigit(str
[0])) {
250 if (2 != sscanf(str
,"%64[0-9.]:%32[^,]",addr
,port
)) {
251 fprintf(stderr
, "%s: ipv4 parse error (%s)\n",
255 ai
.ai_family
= PF_INET
;
258 if (2 != sscanf(str
,"%64[^:]:%32[^,]",addr
,port
)) {
259 fprintf(stderr
, "%s: hostname parse error (%s)\n",
266 if (strstr(str
, ",ipv4"))
267 ai
.ai_family
= PF_INET
;
268 if (strstr(str
, ",ipv6"))
269 ai
.ai_family
= PF_INET6
;
272 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &res
))) {
273 fprintf(stderr
,"getaddrinfo(%s,%s): %s\n", gai_strerror(rc
),
278 inet_print_addrinfo(__FUNCTION__
, res
);
280 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
281 if (getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
282 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
283 NI_NUMERICHOST
| NI_NUMERICSERV
) != 0) {
284 fprintf(stderr
,"%s: getnameinfo: oops\n", __FUNCTION__
);
287 sock
= socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
289 fprintf(stderr
,"%s: socket(%s): %s\n", __FUNCTION__
,
290 inet_strfamily(e
->ai_family
), strerror(errno
));
293 setsockopt(sock
,SOL_SOCKET
,SO_REUSEADDR
,(void*)&on
,sizeof(on
));
295 /* connect to peer */
296 if (connect(sock
,e
->ai_addr
,e
->ai_addrlen
) < 0) {
297 if (sockets_debug
|| NULL
== e
->ai_next
)
298 fprintf(stderr
, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__
,
299 inet_strfamily(e
->ai_family
),
300 e
->ai_canonname
, uaddr
, uport
, strerror(errno
));
305 fprintf(stderr
, "%s: connect(%s,%s,%s,%s): OK\n", __FUNCTION__
,
306 inet_strfamily(e
->ai_family
),
307 e
->ai_canonname
, uaddr
, uport
);
317 int unix_listen(const char *str
, char *ostr
, int olen
)
319 struct sockaddr_un un
;
323 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
325 perror("socket(unix)");
329 opts
= strchr(str
, ',');
332 path
= malloc(len
+1);
333 snprintf(path
, len
+1, "%.*s", len
, str
);
337 memset(&un
, 0, sizeof(un
));
338 un
.sun_family
= AF_UNIX
;
339 if (path
&& strlen(path
)) {
340 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
342 char *tmpdir
= getenv("TMPDIR");
343 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s/qemu-socket-XXXXXX",
344 tmpdir
? tmpdir
: "/tmp");
346 * This dummy fd usage silences the mktemp() unsecure warning.
347 * Using mkstemp() doesn't make things more secure here
348 * though. bind() complains about existing files, so we have
349 * to unlink first and thus re-open the race window. The
350 * worst case possible is bind() failing, i.e. a DoS attack.
352 fd
= mkstemp(un
.sun_path
); close(fd
);
354 snprintf(ostr
, olen
, "%s%s", un
.sun_path
, opts
? opts
: "");
357 if (bind(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
358 fprintf(stderr
, "bind(unix:%s): %s\n", un
.sun_path
, strerror(errno
));
361 if (listen(sock
, 1) < 0) {
362 fprintf(stderr
, "listen(unix:%s): %s\n", un
.sun_path
, strerror(errno
));
367 fprintf(stderr
, "bind(unix:%s): OK\n", un
.sun_path
);
377 int unix_connect(const char *path
)
379 struct sockaddr_un un
;
382 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
384 perror("socket(unix)");
388 memset(&un
, 0, sizeof(un
));
389 un
.sun_family
= AF_UNIX
;
390 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
391 if (connect(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
392 fprintf(stderr
, "connect(unix:%s): %s\n", path
, strerror(errno
));
397 fprintf(stderr
, "connect(unix:%s): OK\n", path
);
403 int unix_listen(const char *path
, char *ostr
, int olen
)
405 fprintf(stderr
, "unix sockets are not available on windows\n");
409 int unix_connect(const char *path
)
411 fprintf(stderr
, "unix sockets are not available on windows\n");