[quagga-dev 12417] [PATCH 03/10] lib: allow caller to provide prefix storage in sockunion2hostprefix

Timo Teräs timo.teras at iki.fi
Fri May 22 11:40:57 BST 2015


Avoids a dynamic allocation which is usually freed immediate afterwards.

Signed-off-by: Timo Teräs <timo.teras at iki.fi>
---
 bgpd/bgp_network.c | 11 +++++------
 lib/prefix.c       |  6 +++---
 lib/prefix.h       |  2 +-
 lib/vty.c          | 18 ++++++------------
 4 files changed, 15 insertions(+), 22 deletions(-)

diff --git a/bgpd/bgp_network.c b/bgpd/bgp_network.c
index cea430c..9765096 100644
--- a/bgpd/bgp_network.c
+++ b/bgpd/bgp_network.c
@@ -304,28 +304,27 @@ static int
 bgp_update_address (struct interface *ifp, const union sockunion *dst,
 		    union sockunion *addr)
 {
-  struct prefix *p, *sel, *d;
+  struct prefix *p, *sel, d;
   struct connected *connected;
   struct listnode *node;
   int common;
 
-  d = sockunion2hostprefix (dst);
+  sockunion2hostprefix (dst, &d);
   sel = NULL;
   common = -1;
 
   for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected))
     {
       p = connected->address;
-      if (p->family != d->family)
+      if (p->family != d.family)
 	continue;
-      if (prefix_common_bits (p, d) > common)
+      if (prefix_common_bits (p, &d) > common)
 	{
 	  sel = p;
-	  common = prefix_common_bits (sel, d);
+	  common = prefix_common_bits (sel, &d);
 	}
     }
 
-  prefix_free (d);
   if (!sel)
     return 1;
 
diff --git a/lib/prefix.c b/lib/prefix.c
index dbfdc83..57cf12a 100644
--- a/lib/prefix.c
+++ b/lib/prefix.c
@@ -681,13 +681,13 @@ sockunion2prefix (const union sockunion *dest,
 
 /* Utility function of convert between struct prefix <=> union sockunion. */
 struct prefix *
-sockunion2hostprefix (const union sockunion *su)
+sockunion2hostprefix (const union sockunion *su, struct prefix *prefix)
 {
   if (su->sa.sa_family == AF_INET)
     {
       struct prefix_ipv4 *p;
 
-      p = prefix_ipv4_new ();
+      p = prefix ? (struct prefix_ipv4 *) prefix : prefix_ipv4_new ();
       p->family = AF_INET;
       p->prefix = su->sin.sin_addr;
       p->prefixlen = IPV4_MAX_BITLEN;
@@ -698,7 +698,7 @@ sockunion2hostprefix (const union sockunion *su)
     {
       struct prefix_ipv6 *p;
 
-      p = prefix_ipv6_new ();
+      p = prefix ? (struct prefix_ipv6 *) prefix : prefix_ipv6_new ();
       p->family = AF_INET6;
       p->prefixlen = IPV6_MAX_BITLEN;
       memcpy (&p->prefix, &su->sin6.sin6_addr, sizeof (struct in6_addr));
diff --git a/lib/prefix.h b/lib/prefix.h
index a1a0679..404a63a 100644
--- a/lib/prefix.h
+++ b/lib/prefix.h
@@ -189,7 +189,7 @@ extern void apply_mask (struct prefix *);
 
 extern struct prefix *sockunion2prefix (const union sockunion *dest,
                                         const union sockunion *mask);
-extern struct prefix *sockunion2hostprefix (const union sockunion *);
+extern struct prefix *sockunion2hostprefix (const union sockunion *, struct prefix *p);
 extern void prefix2sockunion (const struct prefix *, union sockunion *);
 
 extern struct prefix_ipv4 *prefix_ipv4_new (void);
diff --git a/lib/vty.c b/lib/vty.c
index d623b85..6365808 100644
--- a/lib/vty.c
+++ b/lib/vty.c
@@ -1696,7 +1696,7 @@ vty_accept (struct thread *thread)
   int ret;
   unsigned int on;
   int accept_sock;
-  struct prefix *p = NULL;
+  struct prefix p;
   struct access_list *acl = NULL;
   char buf[SU_ADDRSTRLEN];
 
@@ -1716,13 +1716,13 @@ vty_accept (struct thread *thread)
     }
   set_nonblocking(vty_sock);
 
-  p = sockunion2hostprefix (&su);
+  sockunion2hostprefix (&su, &p);
 
   /* VTY's accesslist apply. */
-  if (p->family == AF_INET && vty_accesslist_name)
+  if (p.family == AF_INET && vty_accesslist_name)
     {
       if ((acl = access_list_lookup (AFI_IP, vty_accesslist_name)) &&
-	  (access_list_apply (acl, p) == FILTER_DENY))
+	  (access_list_apply (acl, &p) == FILTER_DENY))
 	{
 	  zlog (NULL, LOG_INFO, "Vty connection refused from %s",
 		sockunion2str (&su, buf, SU_ADDRSTRLEN));
@@ -1731,18 +1731,16 @@ vty_accept (struct thread *thread)
 	  /* continue accepting connections */
 	  vty_event (VTY_SERV, accept_sock, NULL);
 	  
-	  prefix_free (p);
-
 	  return 0;
 	}
     }
 
 #ifdef HAVE_IPV6
   /* VTY's ipv6 accesslist apply. */
-  if (p->family == AF_INET6 && vty_ipv6_accesslist_name)
+  if (p.family == AF_INET6 && vty_ipv6_accesslist_name)
     {
       if ((acl = access_list_lookup (AFI_IP6, vty_ipv6_accesslist_name)) &&
-	  (access_list_apply (acl, p) == FILTER_DENY))
+	  (access_list_apply (acl, &p) == FILTER_DENY))
 	{
 	  zlog (NULL, LOG_INFO, "Vty connection refused from %s",
 		sockunion2str (&su, buf, SU_ADDRSTRLEN));
@@ -1751,15 +1749,11 @@ vty_accept (struct thread *thread)
 	  /* continue accepting connections */
 	  vty_event (VTY_SERV, accept_sock, NULL);
 	  
-	  prefix_free (p);
-
 	  return 0;
 	}
     }
 #endif /* HAVE_IPV6 */
   
-  prefix_free (p);
-
   on = 1;
   ret = setsockopt (vty_sock, IPPROTO_TCP, TCP_NODELAY, 
 		    (char *) &on, sizeof (on));
-- 
2.4.1





More information about the Quagga-dev mailing list