]> git.proxmox.com Git - mirror_frr.git/commitdiff
lib: allow caller to provide prefix storage in sockunion2hostprefix
authorTimo Teräs <timo.teras@iki.fi>
Fri, 22 May 2015 10:40:57 +0000 (13:40 +0300)
committerDonald Sharp <sharpd@cumulusnetworks.com>
Thu, 9 Jun 2016 12:21:40 +0000 (08:21 -0400)
Avoids a dynamic allocation which is usually freed immediate afterwards.

Signed-off-by: Timo Teräs <timo.teras@iki.fi>
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
bgpd/bgp_network.c
bgpd/bgp_vty.c
bgpd/bgpd.c
lib/prefix.c
lib/prefix.h
lib/vty.c

index a2531e21b233e73e5e4dd6e36f952b7a856ad7d2..c797d0d23609935e2f800e3b76f8df9372249cf8 100644 (file)
@@ -502,28 +502,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;
 
index b966b27c2b490dc39158ae487169322e860775b8..259033cb1f5e1d025accd26e196b35ae429efe3d 100644 (file)
@@ -11117,11 +11117,10 @@ bgp_show_peer (struct vty *vty, struct peer *p, u_char use_json, json_object *js
 
           if (dn_flag[0])
             {
-              struct prefix *prefix = NULL, *range = NULL;
+              struct prefix prefix, *range = NULL;
 
-              prefix = sockunion2hostprefix(&(p->su));
-              if (prefix)
-                range = peer_group_lookup_dynamic_neighbor_range (p->group, prefix);
+              sockunion2hostprefix(&(p->su), &prefix);
+             range = peer_group_lookup_dynamic_neighbor_range (p->group, &prefix);
 
               if (range)
                 {
@@ -11137,11 +11136,10 @@ bgp_show_peer (struct vty *vty, struct peer *p, u_char use_json, json_object *js
 
           if (dn_flag[0])
             {
-              struct prefix *prefix = NULL, *range = NULL;
+              struct prefix prefix, *range = NULL;
 
-              prefix = sockunion2hostprefix(&(p->su));
-              if (prefix)
-                range = peer_group_lookup_dynamic_neighbor_range (p->group, prefix);
+              sockunion2hostprefix(&(p->su), &prefix);
+             range = peer_group_lookup_dynamic_neighbor_range (p->group, &prefix);
 
               if (range)
                 {
index c5587c26087bd106ac2fc95b5fff09b4918e08d8..506b692061282212bb9e6d896b4e5cf06ed91930 100644 (file)
@@ -2486,7 +2486,7 @@ peer_group_listen_range_add (struct peer_group *group, struct prefix *range)
 int
 peer_group_listen_range_del (struct peer_group *group, struct prefix *range)
 {
-  struct prefix *prefix, *prefix2;
+  struct prefix *prefix, prefix2;
   struct listnode *node, *nnode;
   struct peer *peer;
   afi_t afi;
@@ -2512,8 +2512,8 @@ peer_group_listen_range_del (struct peer_group *group, struct prefix *range)
       if (!peer_dynamic_neighbor (peer))
         continue;
 
-      prefix2 = sockunion2hostprefix(&peer->su);
-      if (prefix_match(prefix, prefix2))
+      sockunion2hostprefix(&peer->su, &prefix2);
+      if (prefix_match(prefix, &prefix2))
         {
           if (bgp_debug_neighbor_events(peer))
             zlog_debug ("Deleting dynamic neighbor %s group %s upon "
@@ -3368,19 +3368,16 @@ peer_lookup_dynamic_neighbor (struct bgp *bgp, union sockunion *su)
   struct peer_group *group;
   struct bgp *gbgp;
   struct peer *peer;
-  struct prefix *prefix;
+  struct prefix prefix;
   struct prefix *listen_range;
   int dncount;
   char buf[PREFIX2STR_BUFFER];
   char buf1[PREFIX2STR_BUFFER];
 
-  prefix = sockunion2hostprefix(su);
-  if (!prefix) {
-    return NULL;
-  }
+  sockunion2hostprefix(su, &prefix);
 
   /* See if incoming connection matches a configured listen range. */
-  group = peer_group_lookup_dynamic_neighbor (bgp, prefix, &listen_range);
+  group = peer_group_lookup_dynamic_neighbor (bgp, &prefix, &listen_range);
 
   if (! group)
     return NULL;
@@ -3391,7 +3388,7 @@ peer_lookup_dynamic_neighbor (struct bgp *bgp, union sockunion *su)
   if (! gbgp)
     return NULL;
 
-  prefix2str(prefix, buf, sizeof(buf));
+  prefix2str(&prefix, buf, sizeof(buf));
   prefix2str(listen_range, buf1, sizeof(buf1));
 
   if (bgp_debug_neighbor_events(NULL))
index d0cd8946907e3ee4e10205a2a24af734f1b8ca16..256cd3a49a1508b99c23789007b3cb3191b07ce6 100644 (file)
@@ -714,13 +714,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;
@@ -731,7 +731,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));
index ebdd80b8fd3127de16150da1193d833bf5532d01..6be20de6919e2199dc43b87eb7aec382f14149fd 100644 (file)
@@ -199,7 +199,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);
index 504067ee39e6a78075eb9b7c39199f35e36edb09..85fc9bd5cbd179edf083db018b2b38c3b22aa2b7 100644 (file)
--- a/lib/vty.c
+++ b/lib/vty.c
@@ -1770,7 +1770,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];
 
@@ -1790,13 +1790,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));
@@ -1805,18 +1805,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));
@@ -1825,15 +1823,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));