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"
25 # define AI_ADDRCONFIG 0
28 static int sockets_debug = 0;
29 static const int on=1, off=0;
31 static int inet_getport(struct addrinfo *e)
33 struct sockaddr_in *i4;
34 struct sockaddr_in6 *i6;
36 switch (e->ai_family) {
38 i6 = (void*)e->ai_addr;
39 return ntohs(i6->sin6_port);
41 i4 = (void*)e->ai_addr;
42 return ntohs(i4->sin_port);
48 static void inet_setport(struct addrinfo *e, int port)
50 struct sockaddr_in *i4;
51 struct sockaddr_in6 *i6;
53 switch (e->ai_family) {
55 i6 = (void*)e->ai_addr;
56 i6->sin6_port = htons(port);
59 i4 = (void*)e->ai_addr;
60 i4->sin_port = htons(port);
65 static const char *inet_strfamily(int family)
68 case PF_INET6: return "ipv6";
69 case PF_INET: return "ipv4";
70 case PF_UNIX: return "unix";
75 static void inet_print_addrinfo(const char *tag, struct addrinfo *res)
78 char uaddr[INET6_ADDRSTRLEN+1];
81 for (e = res; e != NULL; e = e->ai_next) {
82 getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
83 uaddr,INET6_ADDRSTRLEN,uport,32,
84 NI_NUMERICHOST | NI_NUMERICSERV);
85 fprintf(stderr,"%s: getaddrinfo: family %s, host %s, port %s\n",
86 tag, inet_strfamily(e->ai_family), uaddr, uport);
90 int inet_listen(const char *str, char *ostr, int olen,
91 int socktype, int port_offset)
93 struct addrinfo ai,*res,*e;
96 char uaddr[INET6_ADDRSTRLEN+1];
99 int slisten,rc,pos,to,try_next;
101 memset(&ai,0, sizeof(ai));
102 ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
103 ai.ai_family = PF_UNSPEC;
104 ai.ai_socktype = socktype;
110 if (1 != sscanf(str,":%32[^,]%n",port,&pos)) {
111 fprintf(stderr, "%s: portonly parse error (%s)\n",
115 } else if (str[0] == '[') {
117 if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) {
118 fprintf(stderr, "%s: ipv6 parse error (%s)\n",
122 ai.ai_family = PF_INET6;
123 } else if (isdigit(str[0])) {
125 if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) {
126 fprintf(stderr, "%s: ipv4 parse error (%s)\n",
130 ai.ai_family = PF_INET;
133 if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) {
134 fprintf(stderr, "%s: hostname parse error (%s)\n",
142 h = strstr(opts, ",to=");
143 to = h ? atoi(h+4) : 0;
144 if (strstr(opts, ",ipv4"))
145 ai.ai_family = PF_INET;
146 if (strstr(opts, ",ipv6"))
147 ai.ai_family = PF_INET6;
151 snprintf(port, sizeof(port), "%d", atoi(port) + port_offset);
152 rc = getaddrinfo(strlen(addr) ? addr : NULL, port, &ai, &res);
154 fprintf(stderr,"%s: getaddrinfo(%s,%s): %s\n", __FUNCTION__,
155 addr, port, gai_strerror(rc));
159 inet_print_addrinfo(__FUNCTION__, res);
161 /* create socket + bind */
162 for (e = res; e != NULL; e = e->ai_next) {
163 getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
164 uaddr,INET6_ADDRSTRLEN,uport,32,
165 NI_NUMERICHOST | NI_NUMERICSERV);
166 slisten = socket(e->ai_family, e->ai_socktype, e->ai_protocol);
168 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
169 inet_strfamily(e->ai_family), strerror(errno));
173 setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
175 if (e->ai_family == PF_INET6) {
176 /* listen on both ipv4 and ipv6 */
177 setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,sizeof(off));
182 if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {
184 fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,
185 inet_strfamily(e->ai_family), uaddr, inet_getport(e));
188 try_next = to && (inet_getport(e) <= to + port_offset);
189 if (!try_next || sockets_debug)
190 fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__,
191 inet_strfamily(e->ai_family), uaddr, inet_getport(e),
194 inet_setport(e, inet_getport(e) + 1);
199 closesocket(slisten);
201 fprintf(stderr, "%s: FAILED\n", __FUNCTION__);
206 if (listen(slisten,1) != 0) {
208 closesocket(slisten);
212 if (e->ai_family == PF_INET6) {
213 snprintf(ostr, olen, "[%s]:%d%s", uaddr,
214 inet_getport(e) - port_offset, opts);
216 snprintf(ostr, olen, "%s:%d%s", uaddr,
217 inet_getport(e) - port_offset, opts);
224 int inet_connect(const char *str, int socktype)
226 struct addrinfo ai,*res,*e;
229 char uaddr[INET6_ADDRSTRLEN+1];
233 memset(&ai,0, sizeof(ai));
234 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
235 ai.ai_family = PF_UNSPEC;
236 ai.ai_socktype = socktype;
241 if (2 != sscanf(str,"[%64[^]]]:%32[^,]",addr,port)) {
242 fprintf(stderr, "%s: ipv6 parse error (%s)\n",
246 ai.ai_family = PF_INET6;
247 } else if (isdigit(str[0])) {
249 if (2 != sscanf(str,"%64[0-9.]:%32[^,]",addr,port)) {
250 fprintf(stderr, "%s: ipv4 parse error (%s)\n",
254 ai.ai_family = PF_INET;
257 if (2 != sscanf(str,"%64[^:]:%32[^,]",addr,port)) {
258 fprintf(stderr, "%s: hostname parse error (%s)\n",
265 if (strstr(str, ",ipv4"))
266 ai.ai_family = PF_INET;
267 if (strstr(str, ",ipv6"))
268 ai.ai_family = PF_INET6;
271 if (0 != (rc = getaddrinfo(addr, port, &ai, &res))) {
272 fprintf(stderr,"getaddrinfo(%s,%s): %s\n", gai_strerror(rc),
277 inet_print_addrinfo(__FUNCTION__, res);
279 for (e = res; e != NULL; e = e->ai_next) {
280 if (getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
281 uaddr,INET6_ADDRSTRLEN,uport,32,
282 NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
283 fprintf(stderr,"%s: getnameinfo: oops\n", __FUNCTION__);
286 sock = socket(e->ai_family, e->ai_socktype, e->ai_protocol);
288 fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
289 inet_strfamily(e->ai_family), strerror(errno));
292 setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
294 /* connect to peer */
295 if (connect(sock,e->ai_addr,e->ai_addrlen) < 0) {
296 if (sockets_debug || NULL == e->ai_next)
297 fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
298 inet_strfamily(e->ai_family),
299 e->ai_canonname, uaddr, uport, strerror(errno));
304 fprintf(stderr, "%s: connect(%s,%s,%s,%s): OK\n", __FUNCTION__,
305 inet_strfamily(e->ai_family),
306 e->ai_canonname, uaddr, uport);
316 int unix_listen(const char *str, char *ostr, int olen)
318 struct sockaddr_un un;
322 sock = socket(PF_UNIX, SOCK_STREAM, 0);
324 perror("socket(unix)");
328 opts = strchr(str, ',');
331 path = malloc(len+1);
332 snprintf(path, len+1, "%.*s", len, str);
336 memset(&un, 0, sizeof(un));
337 un.sun_family = AF_UNIX;
338 if (path && strlen(path)) {
339 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
341 char *tmpdir = getenv("TMPDIR");
342 snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX",
343 tmpdir ? tmpdir : "/tmp");
345 * This dummy fd usage silences the mktemp() unsecure warning.
346 * Using mkstemp() doesn't make things more secure here
347 * though. bind() complains about existing files, so we have
348 * to unlink first and thus re-open the race window. The
349 * worst case possible is bind() failing, i.e. a DoS attack.
351 fd = mkstemp(un.sun_path); close(fd);
353 snprintf(ostr, olen, "%s%s", un.sun_path, opts ? opts : "");
356 if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
357 fprintf(stderr, "bind(unix:%s): %s\n", un.sun_path, strerror(errno));
360 if (listen(sock, 1) < 0) {
361 fprintf(stderr, "listen(unix:%s): %s\n", un.sun_path, strerror(errno));
366 fprintf(stderr, "bind(unix:%s): OK\n", un.sun_path);
376 int unix_connect(const char *path)
378 struct sockaddr_un un;
381 sock = socket(PF_UNIX, SOCK_STREAM, 0);
383 perror("socket(unix)");
387 memset(&un, 0, sizeof(un));
388 un.sun_family = AF_UNIX;
389 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
390 if (connect(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
391 fprintf(stderr, "connect(unix:%s): %s\n", path, strerror(errno));
396 fprintf(stderr, "connect(unix:%s): OK\n", path);
402 int unix_listen(const char *path, char *ostr, int olen)
404 fprintf(stderr, "unix sockets are not available on windows\n");
408 int unix_connect(const char *path)
410 fprintf(stderr, "unix sockets are not available on windows\n");