lib: allow caller to provide prefix storage in sockunion2hostprefix
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>
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 @@
/* 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 @@
{
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));