diff options
-rw-r--r-- | lib/sockunion.c | 7 | ||||
-rw-r--r-- | lib/sockunion.h | 8 |
2 files changed, 14 insertions, 1 deletions
diff --git a/lib/sockunion.c b/lib/sockunion.c index 054b17e6..d1fdb189 100644 --- a/lib/sockunion.c +++ b/lib/sockunion.c @@ -164,9 +164,16 @@ str2sockunion (const char *str, union sockunion *su) return -1; } +/*------------------------------------------------------------------------------ + * Construct string for sockunion IP address. + * + * Requires buffer of at least SU_ADDRSTRLEN characters. + */ const char * sockunion2str (union sockunion *su, char *buf, size_t len) { + assert(len >= SU_ADDRSTRLEN) ; + if (su->sa.sa_family == AF_INET) return inet_ntop (AF_INET, &su->sin.sin_addr, buf, len); #ifdef HAVE_IPV6 diff --git a/lib/sockunion.h b/lib/sockunion.h index 2009da2f..97fd8b72 100644 --- a/lib/sockunion.h +++ b/lib/sockunion.h @@ -24,6 +24,7 @@ #define _ZEBRA_SOCKUNION_H #include "symtab.h" +#include "zassert.h" #if 0 union sockunion { @@ -63,9 +64,14 @@ enum connect_result #define AF_INET_UNION AF_INET #endif -/* Sockunion address string length. Same as INET6_ADDRSTRLEN. */ +/* Sockunion address string length. Accommodate either IPv4 or IPv6. */ #define SU_ADDRSTRLEN 46 +CONFIRM(SU_ADDRSTRLEN >= INET_ADDRSTRLEN) ; +#if HAVE_IPV6 +CONFIRM(SU_ADDRSTRLEN >= INET6_ADDRSTRLEN) ; +#endif + /* Macro to set link local index to the IPv6 address. For KAME IPv6 stack. */ #ifdef KAME |