changeset 4443:ec7ded7718ae draft

sun: Add rbtree support
author Roy Marples <roy@marples.name>
date Tue, 16 Apr 2019 21:41:47 +0000
parents 52d9fd948974
children 8a09e7a73a5a
files compat/rbtree.h src/common.h src/if-sun.c
diffstat 3 files changed, 36 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/compat/rbtree.h	Tue Apr 16 20:02:36 2019 +0100
+++ b/compat/rbtree.h	Tue Apr 16 21:41:47 2019 +0000
@@ -32,6 +32,8 @@
 #ifndef _SYS_RBTREE_H_
 #define	_SYS_RBTREE_H_
 
+#include "common.h"
+
 #if defined(_KERNEL) || defined(_STANDALONE)
 #include <sys/types.h>
 #else
--- a/src/common.h	Tue Apr 16 20:02:36 2019 +0100
+++ b/src/common.h	Tue Apr 16 21:41:47 2019 +0000
@@ -32,10 +32,6 @@
 #include <sys/time.h>
 #include <stdio.h>
 
-#include "config.h"
-#include "defs.h"
-#include "dhcpcd.h"
-
 #ifndef HOSTNAME_MAX_LEN
 #define HOSTNAME_MAX_LEN	250	/* 255 - 3 (FQDN) - 2 (DNS enc) */
 #endif
@@ -143,6 +139,15 @@
 #  define	__predict_false(exp)	(exp)
 # endif
 #endif
+#ifndef __BEGIN_DECLS
+# if defined(__cplusplus)
+#  define	__BEGIN_DECLS		extern "C" {
+#  define	__END_DECLS		};
+# else /* __BEGIN_DECLS */
+#  define	__BEGIN_DECLS
+#  define	__END_DECLS
+# endif /* __BEGIN_DECLS */
+#endif /* __BEGIN_DECLS */
 
 #ifndef __fallthrough
 # if __GNUC__ >= 7
--- a/src/if-sun.c	Tue Apr 16 20:02:36 2019 +0100
+++ b/src/if-sun.c	Tue Apr 16 21:41:47 2019 +0000
@@ -1222,10 +1222,10 @@
 
 #ifdef INET
 static int
-if_walkrt(struct dhcpcd_ctx *ctx, char *data, size_t len)
+if_walkrt(struct dhcpcd_ctx *ctx, rb_tree_t *routes, char *data, size_t len)
 {
 	mib2_ipRouteEntry_t *re, *e;
-	struct rt rt;
+	struct rt rt, *rtn;
 	char ifname[IF_NAMESIZE];
 	struct in_addr in;
 
@@ -1250,7 +1250,6 @@
 		}
 
 		memset(&rt, 0, sizeof(rt));
-		rt.rt_dflags |= RTDF_INIT;
 		in.s_addr = re->ipRouteDest;
 		sa_in_init(&rt.rt_dest, &in);
 		in.s_addr = re->ipRouteMask;
@@ -1264,7 +1263,13 @@
 		if_octetstr(ifname, &re->ipRouteIfIndex, sizeof(ifname));
 		rt.rt_ifp = if_find(ctx->ifaces, ifname);
 		if_finishrt(ctx, &rt);
-		rt_recvrt(RTM_ADD, &rt, 0);
+		if ((rtn = rt_new(rt.rt_ifp)) == NULL) {
+			logerr(__func__);
+			break;
+		}
+		memcpy(rtn, &rt, sizeof(*rtn));
+		if (rb_tree_insert_node(routes, rtn) != rtn)
+			rt_free(rtn);
 	} while (++re < e);
 	return 0;
 }
@@ -1272,10 +1277,10 @@
 
 #ifdef INET6
 static int
-if_walkrt6(struct dhcpcd_ctx *ctx, char *data, size_t len)
+if_walkrt6(struct dhcpcd_ctx *ctx, rb_tree_t *routes, char *data, size_t len)
 {
 	mib2_ipv6RouteEntry_t *re, *e;
-	struct rt rt;
+	struct rt rt, *rtn;
 	char ifname[IF_NAMESIZE];
 	struct in6_addr in6;
 
@@ -1301,7 +1306,6 @@
 		}
 
 		memset(&rt, 0, sizeof(rt));
-		rt.rt_dflags |= RTDF_INIT;
 		sa_in6_init(&rt.rt_dest, &re->ipv6RouteDest);
 		ipv6_mask(&in6, re->ipv6RoutePfxLength);
 		sa_in6_init(&rt.rt_netmask, &in6);
@@ -1310,15 +1314,22 @@
 		if_octetstr(ifname, &re->ipv6RouteIfIndex, sizeof(ifname));
 		rt.rt_ifp = if_find(ctx->ifaces, ifname);
 		if_finishrt(ctx, &rt);
-		rt_recvrt(RTM_ADD, &rt, 0);
+		if ((rtn = rt_new(rt.rt_ifp)) == NULL) {
+			logerr(__func__);
+			break;
+		}
+		memcpy(rtn, &rt, sizeof(*rtn));
+		if (rb_tree_insert_node(routes, rtn) != rtn)
+			rt_free(rtn);
 	} while (++re < e);
 	return 0;
 }
 #endif
 
 static int
-if_parsert(struct dhcpcd_ctx *ctx, unsigned int level, unsigned int name,
-    int (*walkrt)(struct dhcpcd_ctx *, char *, size_t))
+if_parsert(struct dhcpcd_ctx *ctx, rb_tree_t *routes,
+    unsigned int level, unsigned int name,
+    int (*walkrt)(struct dhcpcd_ctx *, rb_tree_t *, char *, size_t))
 {
 	int			s, retval, code, flags;
 	uintptr_t		buf[512 / sizeof(uintptr_t)];
@@ -1403,7 +1414,7 @@
 		 * the next item, so don't move this test higher up
 		 * to avoid the buffer allocation and getmsg calls. */
 		if (req->level == level && req->name == name) {
-			if (walkrt(ctx, databuf.buf, req->len) == -1)
+			if (walkrt(ctx, routes, databuf.buf, req->len) == -1)
 				break;
 		}
 	}
@@ -1416,18 +1427,17 @@
 
 
 int
-if_initrt(struct dhcpcd_ctx *ctx, int af)
+if_initrt(struct dhcpcd_ctx *ctx, rb_tree_t *routes, int af)
 {
 
-	rt_headclear(&ctx->kroutes, af);
 #ifdef INET
 	if ((af == AF_UNSPEC || af == AF_INET) &&
-	    if_parsert(ctx, MIB2_IP,MIB2_IP_ROUTE, if_walkrt) == -1)
+	    if_parsert(ctx, routes, MIB2_IP,MIB2_IP_ROUTE, if_walkrt) == -1)
 		return -1;
 #endif
 #ifdef INET6
 	if ((af == AF_UNSPEC || af == AF_INET6) &&
-	    if_parsert(ctx, MIB2_IP6, MIB2_IP6_ROUTE, if_walkrt6) == -1)
+	    if_parsert(ctx, routes, MIB2_IP6, MIB2_IP6_ROUTE, if_walkrt6) == -1)
 		return -1;
 #endif
 	return 0;