]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
sun: Add rbtree support
authorRoy Marples <roy@marples.name>
Tue, 16 Apr 2019 21:41:47 +0000 (21:41 +0000)
committerRoy Marples <roy@marples.name>
Tue, 16 Apr 2019 21:41:47 +0000 (21:41 +0000)
compat/rbtree.h
src/common.h
src/if-sun.c

index b227fd70c15ba14a0ecc04a9f3f772cf7065ad8d..b0944a9817f6b5c9cd415c20bd09ab70f0502016 100644 (file)
@@ -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
index 3833d8ecd7df3e619dc4dc68d5cfe2925e273305..56870fb570ba299fe1ffe893549dff7176f29269 100644 (file)
 #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
 #  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
index a4e1ae80ccb616845edd223f4fbb17b80d2e3318..90d9bb44edc21cae67e6bfde42958a1f9cc6e26a 100644 (file)
@@ -1222,10 +1222,10 @@ if_plumb(int cmd, const struct dhcpcd_ctx *ctx, int af, const char *ifname)
 
 #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 @@ if_walkrt(struct dhcpcd_ctx *ctx, char *data, size_t len)
                }
 
                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_walkrt(struct dhcpcd_ctx *ctx, char *data, size_t len)
                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 @@ if_walkrt(struct dhcpcd_ctx *ctx, char *data, size_t len)
 
 #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 @@ if_walkrt6(struct dhcpcd_ctx *ctx, char *data, size_t len)
                }
 
                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_walkrt6(struct dhcpcd_ctx *ctx, char *data, size_t len)
                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 @@ if_parsert(struct dhcpcd_ctx *ctx, unsigned int level, unsigned int name,
                 * 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 @@ out:
 
 
 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;