From e810903443a31747b58eb30c60f4466e17b85c0f Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Wed, 22 Oct 2014 11:14:48 +0200 Subject: [PATCH] miniupnpd/upnputils.c: do something in case inet_ntop() fails --- miniupnpd/upnputils.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/miniupnpd/upnputils.c b/miniupnpd/upnputils.c index 4a972d1..33930b3 100644 --- a/miniupnpd/upnputils.c +++ b/miniupnpd/upnputils.c @@ -19,6 +19,7 @@ #ifdef AF_LINK #include #endif +#include #include "upnputils.h" #include "upnpglobalvars.h" @@ -35,17 +36,23 @@ sockaddr_to_string(const struct sockaddr * addr, char * str, size_t size) switch(addr->sa_family) { +#ifdef AF_INET6 case AF_INET6: - inet_ntop(addr->sa_family, - &((struct sockaddr_in6 *)addr)->sin6_addr, - buffer, sizeof(buffer)); + if(inet_ntop(addr->sa_family, + &((struct sockaddr_in6 *)addr)->sin6_addr, + buffer, sizeof(buffer)) == NULL) { + snprintf(buffer, sizeof(buffer), "inet_ntop: %s", strerror(errno)); + } port = ntohs(((struct sockaddr_in6 *)addr)->sin6_port); n = snprintf(str, size, "[%s]:%hu", buffer, port); break; +#endif /* AF_INET6 */ case AF_INET: - inet_ntop(addr->sa_family, - &((struct sockaddr_in *)addr)->sin_addr, - buffer, sizeof(buffer)); + if(inet_ntop(addr->sa_family, + &((struct sockaddr_in *)addr)->sin_addr, + buffer, sizeof(buffer)) == NULL) { + snprintf(buffer, sizeof(buffer), "inet_ntop: %s", strerror(errno)); + } port = ntohs(((struct sockaddr_in *)addr)->sin_port); n = snprintf(str, size, "%s:%hu", buffer, port); break; @@ -63,7 +70,7 @@ sockaddr_to_string(const struct sockaddr * addr, char * str, size_t size) link_ntoa(sdl)); } break; -#endif +#endif /* AF_LINK */ default: n = snprintf(str, size, "unknown address family %d", addr->sa_family); #if 0