X-Git-Url: http://git.maemo.org/git/?a=blobdiff_plain;f=qemu-sockets.c;h=bd49d29a48530f2a6a6bd74b6157974028eb34b0;hb=93102fd6010c68320bc9a008c8cf70cb4a36d4b9;hp=77d9921a794ad4bc43e2c97785754bdc6036e84f;hpb=d247d25f18764402899b37c381bb696a79000b4e;p=qemu diff --git a/qemu-sockets.c b/qemu-sockets.c index 77d9921..bd49d29 100644 --- a/qemu-sockets.c +++ b/qemu-sockets.c @@ -1,3 +1,17 @@ +/* + * inet and unix socket functions for qemu + * + * (c) 2008 Gerd Hoffmann + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; under version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ #include #include #include @@ -6,6 +20,7 @@ #include #include "qemu_socket.h" +#include "qemu-common.h" /* for qemu_isdigit */ #ifndef AI_ADDRCONFIG # define AI_ADDRCONFIG 0 @@ -92,7 +107,7 @@ int inet_listen(const char *str, char *ostr, int olen, /* parse address */ if (str[0] == ':') { /* no host given */ - strcpy(addr,""); + addr[0] = '\0'; if (1 != sscanf(str,":%32[^,]%n",port,&pos)) { fprintf(stderr, "%s: portonly parse error (%s)\n", __FUNCTION__, str); @@ -106,7 +121,7 @@ int inet_listen(const char *str, char *ostr, int olen, return -1; } ai.ai_family = PF_INET6; - } else if (isdigit(str[0])) { + } else if (qemu_isdigit(str[0])) { /* IPv4 addr */ if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) { fprintf(stderr, "%s: ipv4 parse error (%s)\n", @@ -146,21 +161,22 @@ int inet_listen(const char *str, char *ostr, int olen, /* create socket + bind */ for (e = res; e != NULL; e = e->ai_next) { - getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen, - uaddr,INET6_ADDRSTRLEN,uport,32, - NI_NUMERICHOST | NI_NUMERICSERV); + getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen, + uaddr,INET6_ADDRSTRLEN,uport,32, + NI_NUMERICHOST | NI_NUMERICSERV); slisten = socket(e->ai_family, e->ai_socktype, e->ai_protocol); - if (slisten < 0) { + if (slisten < 0) { fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__, inet_strfamily(e->ai_family), strerror(errno)); - continue; - } + continue; + } setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); #ifdef IPV6_V6ONLY if (e->ai_family == PF_INET6) { /* listen on both ipv4 and ipv6 */ - setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,sizeof(off)); + setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off, + sizeof(off)); } #endif @@ -168,7 +184,7 @@ int inet_listen(const char *str, char *ostr, int olen, if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) { if (sockets_debug) fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__, - inet_strfamily(e->ai_family), uaddr, inet_getport(e)); + inet_strfamily(e->ai_family), uaddr, inet_getport(e)); goto listen; } try_next = to && (inet_getport(e) <= to + port_offset); @@ -192,6 +208,7 @@ listen: if (listen(slisten,1) != 0) { perror("listen"); closesocket(slisten); + freeaddrinfo(res); return -1; } if (ostr) { @@ -230,7 +247,7 @@ int inet_connect(const char *str, int socktype) return -1; } ai.ai_family = PF_INET6; - } else if (isdigit(str[0])) { + } else if (qemu_isdigit(str[0])) { /* IPv4 addr */ if (2 != sscanf(str,"%64[0-9.]:%32[^,]",addr,port)) { fprintf(stderr, "%s: ipv4 parse error (%s)\n", @@ -263,35 +280,35 @@ int inet_connect(const char *str, int socktype) inet_print_addrinfo(__FUNCTION__, res); for (e = res; e != NULL; e = e->ai_next) { - if (getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen, - uaddr,INET6_ADDRSTRLEN,uport,32, - NI_NUMERICHOST | NI_NUMERICSERV) != 0) { + if (getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen, + uaddr,INET6_ADDRSTRLEN,uport,32, + NI_NUMERICHOST | NI_NUMERICSERV) != 0) { fprintf(stderr,"%s: getnameinfo: oops\n", __FUNCTION__); - continue; - } + continue; + } sock = socket(e->ai_family, e->ai_socktype, e->ai_protocol); - if (sock < 0) { + if (sock < 0) { fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__, - inet_strfamily(e->ai_family), strerror(errno)); - continue; - } + inet_strfamily(e->ai_family), strerror(errno)); + continue; + } setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); - /* connect to peer */ - if (connect(sock,e->ai_addr,e->ai_addrlen) < 0) { + /* connect to peer */ + if (connect(sock,e->ai_addr,e->ai_addrlen) < 0) { if (sockets_debug || NULL == e->ai_next) fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__, inet_strfamily(e->ai_family), e->ai_canonname, uaddr, uport, strerror(errno)); closesocket(sock); - continue; - } + continue; + } if (sockets_debug) fprintf(stderr, "%s: connect(%s,%s,%s,%s): OK\n", __FUNCTION__, inet_strfamily(e->ai_family), e->ai_canonname, uaddr, uport); freeaddrinfo(res); - return sock; + return sock; } freeaddrinfo(res); return -1; @@ -307,17 +324,17 @@ int unix_listen(const char *str, char *ostr, int olen) sock = socket(PF_UNIX, SOCK_STREAM, 0); if (sock < 0) { - perror("socket(unix)"); - return -1; + perror("socket(unix)"); + return -1; } opts = strchr(str, ','); if (opts) { len = opts - str; - path = malloc(len+1); + path = qemu_malloc(len+1); snprintf(path, len+1, "%.*s", len, str); } else - path = strdup(str); + path = qemu_strdup(str); memset(&un, 0, sizeof(un)); un.sun_family = AF_UNIX; @@ -350,11 +367,11 @@ int unix_listen(const char *str, char *ostr, int olen) if (sockets_debug) fprintf(stderr, "bind(unix:%s): OK\n", un.sun_path); - free(path); + qemu_free(path); return sock; err: - free(path); + qemu_free(path); closesocket(sock); return -1; } @@ -366,8 +383,8 @@ int unix_connect(const char *path) sock = socket(PF_UNIX, SOCK_STREAM, 0); if (sock < 0) { - perror("socket(unix)"); - return -1; + perror("socket(unix)"); + return -1; } memset(&un, 0, sizeof(un));