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
,
184 if (bind(slisten
, e
->ai_addr
, e
->ai_addrlen
) == 0) {
186 fprintf(stderr
,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__
,
187 inet_strfamily(e
->ai_family
), uaddr
, inet_getport(e
));
190 try_next
= to
&& (inet_getport(e
) <= to
+ port_offset
);
191 if (!try_next
|| sockets_debug
)
192 fprintf(stderr
,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__
,
193 inet_strfamily(e
->ai_family
), uaddr
, inet_getport(e
),
196 inet_setport(e
, inet_getport(e
) + 1);
201 closesocket(slisten
);
203 fprintf(stderr
, "%s: FAILED\n", __FUNCTION__
);
208 if (listen(slisten
,1) != 0) {
210 closesocket(slisten
);
215 if (e
->ai_family
== PF_INET6
) {
216 snprintf(ostr
, olen
, "[%s]:%d%s", uaddr
,
217 inet_getport(e
) - port_offset
, opts
);
219 snprintf(ostr
, olen
, "%s:%d%s", uaddr
,
220 inet_getport(e
) - port_offset
, opts
);
227 int inet_connect(const char *str
, int socktype
)
229 struct addrinfo ai
,*res
,*e
;
232 char uaddr
[INET6_ADDRSTRLEN
+1];
236 memset(&ai
,0, sizeof(ai
));
237 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
238 ai
.ai_family
= PF_UNSPEC
;
239 ai
.ai_socktype
= socktype
;
244 if (2 != sscanf(str
,"[%64[^]]]:%32[^,]",addr
,port
)) {
245 fprintf(stderr
, "%s: ipv6 parse error (%s)\n",
249 ai
.ai_family
= PF_INET6
;
250 } else if (qemu_isdigit(str
[0])) {
252 if (2 != sscanf(str
,"%64[0-9.]:%32[^,]",addr
,port
)) {
253 fprintf(stderr
, "%s: ipv4 parse error (%s)\n",
257 ai
.ai_family
= PF_INET
;
260 if (2 != sscanf(str
,"%64[^:]:%32[^,]",addr
,port
)) {
261 fprintf(stderr
, "%s: hostname parse error (%s)\n",
268 if (strstr(str
, ",ipv4"))
269 ai
.ai_family
= PF_INET
;
270 if (strstr(str
, ",ipv6"))
271 ai
.ai_family
= PF_INET6
;
274 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &res
))) {
275 fprintf(stderr
,"getaddrinfo(%s,%s): %s\n", gai_strerror(rc
),
280 inet_print_addrinfo(__FUNCTION__
, res
);
282 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
283 if (getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
284 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
285 NI_NUMERICHOST
| NI_NUMERICSERV
) != 0) {
286 fprintf(stderr
,"%s: getnameinfo: oops\n", __FUNCTION__
);
289 sock
= socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
291 fprintf(stderr
,"%s: socket(%s): %s\n", __FUNCTION__
,
292 inet_strfamily(e
->ai_family
), strerror(errno
));
295 setsockopt(sock
,SOL_SOCKET
,SO_REUSEADDR
,(void*)&on
,sizeof(on
));
297 /* connect to peer */
298 if (connect(sock
,e
->ai_addr
,e
->ai_addrlen
) < 0) {
299 if (sockets_debug
|| NULL
== e
->ai_next
)
300 fprintf(stderr
, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__
,
301 inet_strfamily(e
->ai_family
),
302 e
->ai_canonname
, uaddr
, uport
, strerror(errno
));
307 fprintf(stderr
, "%s: connect(%s,%s,%s,%s): OK\n", __FUNCTION__
,
308 inet_strfamily(e
->ai_family
),
309 e
->ai_canonname
, uaddr
, uport
);
319 int unix_listen(const char *str
, char *ostr
, int olen
)
321 struct sockaddr_un un
;
325 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
327 perror("socket(unix)");
331 opts
= strchr(str
, ',');
334 path
= qemu_malloc(len
+1);
335 snprintf(path
, len
+1, "%.*s", len
, str
);
337 path
= qemu_strdup(str
);
339 memset(&un
, 0, sizeof(un
));
340 un
.sun_family
= AF_UNIX
;
341 if (path
&& strlen(path
)) {
342 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
344 char *tmpdir
= getenv("TMPDIR");
345 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s/qemu-socket-XXXXXX",
346 tmpdir
? tmpdir
: "/tmp");
348 * This dummy fd usage silences the mktemp() unsecure warning.
349 * Using mkstemp() doesn't make things more secure here
350 * though. bind() complains about existing files, so we have
351 * to unlink first and thus re-open the race window. The
352 * worst case possible is bind() failing, i.e. a DoS attack.
354 fd
= mkstemp(un
.sun_path
); close(fd
);
356 snprintf(ostr
, olen
, "%s%s", un
.sun_path
, opts
? opts
: "");
359 if (bind(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
360 fprintf(stderr
, "bind(unix:%s): %s\n", un
.sun_path
, strerror(errno
));
363 if (listen(sock
, 1) < 0) {
364 fprintf(stderr
, "listen(unix:%s): %s\n", un
.sun_path
, strerror(errno
));
369 fprintf(stderr
, "bind(unix:%s): OK\n", un
.sun_path
);
379 int unix_connect(const char *path
)
381 struct sockaddr_un un
;
384 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
386 perror("socket(unix)");
390 memset(&un
, 0, sizeof(un
));
391 un
.sun_family
= AF_UNIX
;
392 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
393 if (connect(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
394 fprintf(stderr
, "connect(unix:%s): %s\n", path
, strerror(errno
));
399 fprintf(stderr
, "connect(unix:%s): OK\n", path
);
405 int unix_listen(const char *path
, char *ostr
, int olen
)
407 fprintf(stderr
, "unix sockets are not available on windows\n");
411 int unix_connect(const char *path
)
413 fprintf(stderr
, "unix sockets are not available on windows\n");