lib: remove last uses of sockunion_su2str()

Use of this function is prone to memory leaks.

This fixes a memory accounting bug for vty denied connections.

Signed-off-by: Jorge Boncompte [DTI2] <jorge@dti2.net>
Signed-off-by: David Lamparter <equinox@diac24.net>
diff --git a/lib/sockunion.c b/lib/sockunion.c
index 5977052..cfb6f9c 100644
--- a/lib/sockunion.c
+++ b/lib/sockunion.c
@@ -207,25 +207,6 @@
   return NULL;
 }
 
-char *
-sockunion_su2str (union sockunion *su)
-{
-  char str[SU_ADDRSTRLEN];
-
-  switch (su->sa.sa_family)
-    {
-    case AF_INET:
-      inet_ntop (AF_INET, &su->sin.sin_addr, str, sizeof (str));
-      break;
-#ifdef HAVE_IPV6
-    case AF_INET6:
-      inet_ntop (AF_INET6, &su->sin6.sin6_addr, str, sizeof (str));
-      break;
-#endif /* HAVE_IPV6 */
-    }
-  return XSTRDUP (MTYPE_TMP, str);
-}
-
 /* Convert IPv4 compatible IPv6 address to IPv4 address. */
 static void
 sockunion_normalise_mapped (union sockunion *su)
diff --git a/lib/sockunion.h b/lib/sockunion.h
index 15b97fc..8b06058 100644
--- a/lib/sockunion.h
+++ b/lib/sockunion.h
@@ -94,7 +94,6 @@
 extern int sockunion_cmp (union sockunion *, union sockunion *);
 extern int sockunion_same (union sockunion *, union sockunion *);
 
-extern char *sockunion_su2str (union sockunion *su);
 extern union sockunion *sockunion_str2su (const char *str);
 extern struct in_addr sockunion_get_in_addr (union sockunion *su);
 extern int sockunion_accept (int sock, union sockunion *);
diff --git a/lib/vty.c b/lib/vty.c
index 9a4efe6..70bf564 100644
--- a/lib/vty.c
+++ b/lib/vty.c
@@ -1612,13 +1612,16 @@
 static struct vty *
 vty_create (int vty_sock, union sockunion *su)
 {
+  char buf[SU_ADDRSTRLEN];
   struct vty *vty;
 
+  sockunion2str(su, buf, SU_ADDRSTRLEN);
+
   /* Allocate new vty structure and set up default values. */
   vty = vty_new ();
   vty->fd = vty_sock;
   vty->type = VTY_TERM;
-  vty->address = sockunion_su2str (su);
+  strcpy (vty->address, buf);
   if (no_password_check)
     {
       if (restricted_mode)
@@ -1693,7 +1696,7 @@
   int accept_sock;
   struct prefix *p = NULL;
   struct access_list *acl = NULL;
-  char *bufp;
+  char buf[SU_ADDRSTRLEN];
 
   accept_sock = THREAD_FD (thread);
 
@@ -1719,10 +1722,8 @@
       if ((acl = access_list_lookup (AFI_IP, vty_accesslist_name)) &&
 	  (access_list_apply (acl, p) == FILTER_DENY))
 	{
-	  char *buf;
 	  zlog (NULL, LOG_INFO, "Vty connection refused from %s",
-		(buf = sockunion_su2str (&su)));
-	  free (buf);
+		sockunion2str (&su, buf, SU_ADDRSTRLEN));
 	  close (vty_sock);
 	  
 	  /* continue accepting connections */
@@ -1741,10 +1742,8 @@
       if ((acl = access_list_lookup (AFI_IP6, vty_ipv6_accesslist_name)) &&
 	  (access_list_apply (acl, p) == FILTER_DENY))
 	{
-	  char *buf;
 	  zlog (NULL, LOG_INFO, "Vty connection refused from %s",
-		(buf = sockunion_su2str (&su)));
-	  free (buf);
+		sockunion2str (&su, buf, SU_ADDRSTRLEN));
 	  close (vty_sock);
 	  
 	  /* continue accepting connections */
@@ -1767,9 +1766,7 @@
 	  safe_strerror (errno));
 
   zlog (NULL, LOG_INFO, "Vty connection from %s",
-    (bufp = sockunion_su2str (&su)));
-  if (bufp)
-    XFREE (MTYPE_TMP, bufp);
+	sockunion2str (&su, buf, SU_ADDRSTRLEN));
 
   vty_create (vty_sock, &su);
 
@@ -2193,8 +2190,6 @@
   if (vty->fd > 0)
     close (vty->fd);
 
-  if (vty->address)
-    XFREE (MTYPE_TMP, vty->address);
   if (vty->buf)
     XFREE (MTYPE_VTY, vty->buf);
 
diff --git a/lib/vty.h b/lib/vty.h
index 639d741..e515868 100644
--- a/lib/vty.h
+++ b/lib/vty.h
@@ -23,6 +23,7 @@
 
 #include "thread.h"
 #include "log.h"
+#include "sockunion.h"
 
 #define VTY_BUFSIZ 512
 #define VTY_MAXHIST 20
@@ -39,9 +40,6 @@
   /* Node status of this vty */
   int node;
 
-  /* What address is this vty comming from. */
-  char *address;
-
   /* Failure count */
   int fail;
 
@@ -118,6 +116,9 @@
   /* Timeout seconds and thread. */
   unsigned long v_timeout;
   struct thread *t_timeout;
+
+  /* What address is this vty comming from. */
+  char address[SU_ADDRSTRLEN];
 };
 
 /* Integrated configuration file. */