From 08abfcd923e9f37d1902db26771b1dc6731eb265 Mon Sep 17 00:00:00 2001 From: Jiri Popelka Date: Fri, 27 Sep 2013 18:40:06 +0200 Subject: [PATCH 1/1] lib/inet6.c:INET6_rresolve() - various fixes 1) Fall-back to numeric address if getnameinfo fails. Reverse lookup is not mandatory, therefore its fail is not an error. Just return numeric address in that case. This makes netstat/route show IPv6 address instead of [UNKNOWN] in case of DNS problems. 2) Pass length of 'name' buffer into function. 'name' is a pointer and therefore sizeof(name) returns size of pointer and not size of the buffer. see http://stackoverflow.com/questions/14298710/c-pointers-and-arrays-sizeof-operator The sizeof() usage was added with commit 604785adc, so I checked all the other changes in that commit and they seem to be OK. 3) remove unused 's' variable Upstream-Status: Pending Signed-off-by: Shan Hai Signed-off-by: Jianchuan Wang --- lib/inet6.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/inet6.c b/lib/inet6.c index 9a484a0..2a9c459 100644 --- a/lib/inet6.c +++ b/lib/inet6.c @@ -84,10 +84,9 @@ static int INET6_resolve(char *name, struct sockaddr_in6 *sin6) #endif -static int INET6_rresolve(char *name, struct sockaddr_in6 *sin6, int numeric) +static int INET6_rresolve(char *name, size_t namelen, + struct sockaddr_in6 *sin6, int numeric) { - int s; - /* Grmpf. -FvK */ if (sin6->sin6_family != AF_INET6) { #ifdef DEBUG @@ -98,21 +97,20 @@ static int INET6_rresolve(char *name, struct sockaddr_in6 *sin6, int numeric) return (-1); } if (numeric & 0x7FFF) { - inet_ntop( AF_INET6, &sin6->sin6_addr, name, 80); + inet_ntop( AF_INET6, &sin6->sin6_addr, name, namelen); return (0); } if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { if (numeric & 0x8000) - strcpy(name, "default"); + safe_strncpy(name, "default", namelen); else - strcpy(name, "[::]"); + safe_strncpy(name, "[::]", namelen); return (0); } - if ((s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6), - name, 255 /* !! */ , NULL, 0, 0))) { - fputs("getnameinfo failed\n", stderr); - return -1; + if (getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6), + name, namelen , NULL, 0, 0)) { + inet_ntop( AF_INET6, &sin6->sin6_addr, name, namelen); } return (0); } @@ -143,7 +141,8 @@ static char *INET6_sprint(struct sockaddr *sap, int numeric) if (sap->sa_family == 0xFFFF || sap->sa_family == 0) return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff)); - if (INET6_rresolve(buff, (struct sockaddr_in6 *) sap, numeric) != 0) + if (INET6_rresolve(buff, sizeof(buff), + (struct sockaddr_in6 *) sap, numeric) != 0) return safe_strncpy(buff, _("[UNKNOWN]"), sizeof(buff)); return (fix_v4_address(buff, &((struct sockaddr_in6 *)sap)->sin6_addr)); } -- 1.8.5.2.233.g932f7e4