Просмотр исходного кода

ncd: modules: net_ipv4_route: allow destination network in CIDR notation

ambrop7 13 лет назад
Родитель
Сommit
e85246753d
1 измененных файлов с 28 добавлено и 14 удалено
  1. 28 14
      ncd/modules/net_ipv4_route.c

+ 28 - 14
ncd/modules/net_ipv4_route.c

@@ -32,9 +32,12 @@
  * 
  * 
  * Synopsis:
  * Synopsis:
  *     net.ipv4.route(string dest, string dest_prefix, string gateway, string metric, string ifname)
  *     net.ipv4.route(string dest, string dest_prefix, string gateway, string metric, string ifname)
+ *     net.ipv4.route(string cidr_dest, string gateway, string metric, string ifname)
+ * 
  * Description:
  * Description:
- *     Adds an IPv4 route to the system's routing table on initiailzation, and removes it on
- *     deinitialization.
+ *     Adds an IPv4 route to the system's routing table on initiailzation, and
+ *     removes it on deinitialization. The second form takes the destination in
+ *     CIDR notation (a.b.c.d/n).
  *     If 'gateway' is "none", the route will only be associated with an interface.
  *     If 'gateway' is "none", the route will only be associated with an interface.
  *     If 'gateway' is "blackhole", the route will be a blackhole route (and 'ifname' is unused).
  *     If 'gateway' is "blackhole", the route will be a blackhole route (and 'ifname' is unused).
  */
  */
@@ -75,28 +78,39 @@ static void func_new (NCDModuleInst *i)
     
     
     // read arguments
     // read arguments
     NCDValRef dest_arg;
     NCDValRef dest_arg;
-    NCDValRef dest_prefix_arg;
+    NCDValRef dest_prefix_arg = NCDVal_NewInvalid();
     NCDValRef gateway_arg;
     NCDValRef gateway_arg;
     NCDValRef metric_arg;
     NCDValRef metric_arg;
     NCDValRef ifname_arg;
     NCDValRef ifname_arg;
-    if (!NCDVal_ListRead(o->i->args, 5, &dest_arg, &dest_prefix_arg, &gateway_arg, &metric_arg, &ifname_arg)) {
+    if (!NCDVal_ListRead(i->args, 4, &dest_arg, &gateway_arg, &metric_arg, &ifname_arg) &&
+        !NCDVal_ListRead(i->args, 5, &dest_arg, &dest_prefix_arg, &gateway_arg, &metric_arg, &ifname_arg)
+    ) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
         goto fail1;
         goto fail1;
     }
     }
-    if (!NCDVal_IsStringNoNulls(dest_arg) || !NCDVal_IsStringNoNulls(dest_prefix_arg) || !NCDVal_IsStringNoNulls(gateway_arg) ||
-        !NCDVal_IsStringNoNulls(metric_arg) || !NCDVal_IsStringNoNulls(ifname_arg)) {
+    if (!NCDVal_IsStringNoNulls(dest_arg) || !NCDVal_IsStringNoNulls(gateway_arg) ||
+        !NCDVal_IsStringNoNulls(metric_arg) || !NCDVal_IsStringNoNulls(ifname_arg) ||
+        (!NCDVal_IsInvalid(dest_prefix_arg) && !NCDVal_IsStringNoNulls(dest_prefix_arg))
+    ) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
         goto fail1;
         goto fail1;
     }
     }
     
     
     // read dest
     // read dest
-    if (!ipaddr_parse_ipv4_addr((char *)NCDVal_StringValue(dest_arg), &o->dest.addr)) {
-        ModuleLog(o->i, BLOG_ERROR, "wrong dest addr");
-        goto fail1;
-    }
-    if (!ipaddr_parse_ipv4_prefix((char *)NCDVal_StringValue(dest_prefix_arg), &o->dest.prefix)) {
-        ModuleLog(o->i, BLOG_ERROR, "wrong dest prefix");
-        goto fail1;
+    if (NCDVal_IsInvalid(dest_prefix_arg)) {
+        if (!ipaddr_parse_ipv4_ifaddr(NCDVal_StringValue(dest_arg), &o->dest)) {
+            ModuleLog(o->i, BLOG_ERROR, "wrong CIDR notation dest");
+            goto fail1;
+        }
+    } else {
+        if (!ipaddr_parse_ipv4_addr(NCDVal_StringValue(dest_arg), &o->dest.addr)) {
+            ModuleLog(o->i, BLOG_ERROR, "wrong dest addr");
+            goto fail1;
+        }
+        if (!ipaddr_parse_ipv4_prefix(NCDVal_StringValue(dest_prefix_arg), &o->dest.prefix)) {
+            ModuleLog(o->i, BLOG_ERROR, "wrong dest prefix");
+            goto fail1;
+        }
     }
     }
     
     
     // read gateway and choose type
     // read gateway and choose type
@@ -107,7 +121,7 @@ static void func_new (NCDModuleInst *i)
     else if (!strcmp(gateway_str, "blackhole")) {
     else if (!strcmp(gateway_str, "blackhole")) {
         o->type = TYPE_BLACKHOLE;
         o->type = TYPE_BLACKHOLE;
     } else {
     } else {
-        if (!ipaddr_parse_ipv4_addr((char *)gateway_str, &o->gateway)) {
+        if (!ipaddr_parse_ipv4_addr(gateway_str, &o->gateway)) {
             ModuleLog(o->i, BLOG_ERROR, "wrong gateway");
             ModuleLog(o->i, BLOG_ERROR, "wrong gateway");
             goto fail1;
             goto fail1;
         }
         }