From: Roy Marples Date: Tue, 16 Apr 2019 21:41:47 +0000 (+0000) Subject: sun: Add rbtree support X-Git-Tag: v8.0.0~52 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=699f61b16a92a3971b65a4fa7f28c9a528a42cb0;p=thirdparty%2Fdhcpcd.git sun: Add rbtree support --- diff --git a/compat/rbtree.h b/compat/rbtree.h index b227fd70..b0944a98 100644 --- a/compat/rbtree.h +++ b/compat/rbtree.h @@ -32,6 +32,8 @@ #ifndef _SYS_RBTREE_H_ #define _SYS_RBTREE_H_ +#include "common.h" + #if defined(_KERNEL) || defined(_STANDALONE) #include #else diff --git a/src/common.h b/src/common.h index 3833d8ec..56870fb5 100644 --- a/src/common.h +++ b/src/common.h @@ -32,10 +32,6 @@ #include #include -#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 diff --git a/src/if-sun.c b/src/if-sun.c index a4e1ae80..90d9bb44 100644 --- a/src/if-sun.c +++ b/src/if-sun.c @@ -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;