Repository : ssh://git@open-mesh.org/alfred
On branch : master
commit 785bc8b81cf5104b5179a990b288c15560081d3e Author: Sven Eckelmann sven@open-mesh.com Date: Tue Oct 1 14:02:09 2013 +0200
alfred: Close netsock when initialization failed
A failed initialization of the netsock is currently not cleaning up the socket. This is not problematic at the moment because a failed initialization also closes the server.
This can be a problem later when the netsock is reopened with a modified configuration and a failed re-initialization is still accepted.
Signed-off-by: Sven Eckelmann sven@open-mesh.com Signed-off-by: Simon Wunderlich simon@open-mesh.com
785bc8b81cf5104b5179a990b288c15560081d3e netsock.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/netsock.c b/netsock.c index 39281af..bed62e9 100644 --- a/netsock.c +++ b/netsock.c @@ -49,6 +49,8 @@ int netsock_open(struct globals *globals) struct sockaddr_in6 sin6; struct ifreq ifr;
+ globals->netsock = -1; + sock = socket(PF_INET6, SOCK_DGRAM, 0); if (sock < 0) { fprintf(stderr, "can't open socket: %s\n", strerror(errno)); @@ -59,7 +61,7 @@ int netsock_open(struct globals *globals) strncpy(ifr.ifr_name, globals->interface, IFNAMSIZ); if (ioctl(sock, SIOCGIFINDEX, &ifr) == -1) { fprintf(stderr, "can't get interface: %s\n", strerror(errno)); - return -1; + goto err; }
globals->scope_id = ifr.ifr_ifindex; @@ -72,7 +74,7 @@ int netsock_open(struct globals *globals)
if (ioctl(sock, SIOCGIFHWADDR, &ifr) == -1) { fprintf(stderr, "can't get MAC address: %s\n", strerror(errno)); - return -1; + goto err; }
memcpy(&globals->hwaddr, &ifr.ifr_hwaddr.sa_data, 6); @@ -80,16 +82,19 @@ int netsock_open(struct globals *globals)
if (ioctl(sock, SIOCGIFMTU, &ifr) == -1) { fprintf(stderr, "can't get MTU: %s\n", strerror(errno)); - return -1; + goto err; }
if (bind(sock, (struct sockaddr *)&sin6, sizeof(sin6)) < 0) { fprintf(stderr, "can't bind\n"); - return -1; + goto err; }
fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK); globals->netsock = sock;
return 0; +err: + close(sock); + return -1; }