changeset 4248:2844dbb214b5 draft

options: allow static routes to be configured on the command line
author Roy Marples <roy@marples.name>
date Wed, 28 Mar 2018 11:00:18 +0100
parents 22a5ac71f5c8
children c30233f8cca3
files src/if-options.c src/ipv4.c src/route.c src/route.h
diffstat 4 files changed, 29 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/if-options.c	Wed Mar 28 00:22:01 2018 +0100
+++ b/src/if-options.c	Wed Mar 28 11:00:18 2018 +0100
@@ -1086,14 +1086,8 @@
 		    strncmp(arg, "ms_classless_static_routes=",
 		        strlen("ms_classless_static_routes=")) == 0)
 		{
-			struct interface *ifp;
 			struct in_addr addr3;
 
-			ifp = if_find(ctx->ifaces, ifname);
-			if (ifp == NULL) {
-				logerrx("static routes require an interface");
-				return -1;
-			}
 			fp = np = strwhite(p);
 			if (np == NULL) {
 				logerrx("all routes need a gateway");
@@ -1107,7 +1101,7 @@
 				*fp = ' ';
 				return -1;
 			}
-			if ((rt = rt_new(ifp)) == NULL) {
+			if ((rt = rt_new0(ctx)) == NULL) {
 				*fp = ' ';
 				return -1;
 			}
@@ -1117,16 +1111,9 @@
 			TAILQ_INSERT_TAIL(&ifo->routes, rt, rt_next);
 			*fp = ' ';
 		} else if (strncmp(arg, "routers=", strlen("routers=")) == 0) {
-			struct interface *ifp;
-
-			ifp = if_find(ctx->ifaces, ifname);
-			if (ifp == NULL) {
-				logerrx("static routes require an interface");
-				return -1;
-			}
 			if (parse_addr(&addr, NULL, p) == -1)
 				return -1;
-			if ((rt = rt_new(ifp)) == NULL)
+			if ((rt = rt_new0(ctx)) == NULL)
 				return -1;
 			addr2.s_addr = INADDR_ANY;
 			sa_in_init(&rt->rt_dest, &addr2);
--- a/src/ipv4.c	Wed Mar 28 00:22:01 2018 +0100
+++ b/src/ipv4.c	Wed Mar 28 11:00:18 2018 +0100
@@ -289,10 +289,11 @@
 		TAILQ_FOREACH(r, &ifp->options->routes, rt_next) {
 			if (sa_is_unspecified(&r->rt_gateway))
 				break;
-			if ((rt = rt_new(ifp)) == NULL)
+			if ((rt = rt_new0(ifp->ctx)) == NULL)
 				return -1;
+			memcpy(rt, r, sizeof(*rt));
+			rt_setif(rt, ifp);
 			rt->rt_dflags = RTDF_STATIC;
-			memcpy(rt, r, sizeof(*rt));
 			TAILQ_INSERT_TAIL(&nroutes, rt, rt_next);
 		}
 	} else {
--- a/src/route.c	Wed Mar 28 00:22:01 2018 +0100
+++ b/src/route.c	Wed Mar 28 11:00:18 2018 +0100
@@ -146,13 +146,11 @@
 }
 
 struct rt *
-rt_new(struct interface *ifp)
+rt_new0(struct dhcpcd_ctx *ctx)
 {
 	struct rt *rt;
-	struct dhcpcd_ctx *ctx;
 
-	assert(ifp != NULL);
-	ctx = ifp->ctx;
+	assert(ctx != NULL);
 	if ((rt = TAILQ_FIRST(&ctx->froutes)) != NULL)
 		TAILQ_REMOVE(&ctx->froutes, rt, rt_next);
 	else if ((rt = malloc(sizeof(*rt))) == NULL) {
@@ -160,10 +158,30 @@
 		return NULL;
 	}
 	memset(rt, 0, sizeof(*rt));
+	return rt;
+}
+
+void
+rt_setif(struct rt *rt, struct interface *ifp)
+{
+
+	assert(rt != NULL);
+	assert(ifp != NULL);
 	rt->rt_ifp = ifp;
 #ifdef HAVE_ROUTE_METRIC
 	rt->rt_metric = ifp->metric;
 #endif
+}
+
+struct rt *
+rt_new(struct interface *ifp)
+{
+	struct rt *rt;
+
+	assert(ifp != NULL);
+	if ((rt = rt_new0(ifp->ctx)) == NULL)
+		return NULL;
+	rt_setif(rt, ifp);
 	return rt;
 }
 
--- a/src/route.h	Wed Mar 28 00:22:01 2018 +0100
+++ b/src/route.h	Wed Mar 28 11:00:18 2018 +0100
@@ -88,6 +88,8 @@
 void rt_freeif(struct interface *);
 void rt_headclear(struct rt_head *, int);
 void rt_headfreeif(struct rt_head *);
+struct rt * rt_new0(struct dhcpcd_ctx *);
+void rt_setif(struct rt *, struct interface *);
 struct rt * rt_new(struct interface *);
 void rt_recvrt(int, const struct rt *);
 void rt_build(struct dhcpcd_ctx *, int);