Jelajahi Sumber

ncd: add ip_in_network module

ambrop7 15 tahun lalu
induk
melakukan
27140ea6e7

+ 1 - 0
blog_channels.txt

@@ -9,6 +9,7 @@ ncd_depend 4
 ncd_concat 4
 ncd_if 4
 ncd_strcmp 4
+ncd_ip_in_network 4
 ncd_net_backend_physical 4
 ncd_net_backend_badvpn 4
 ncd_net_dns 4

+ 4 - 0
generated/blog_channel_ncd_ip_in_network.h

@@ -0,0 +1,4 @@
+#ifdef BLOG_CURRENT_CHANNEL
+#undef BLOG_CURRENT_CHANNEL
+#endif
+#define BLOG_CURRENT_CHANNEL BLOG_CHANNEL_ncd_ip_in_network

+ 25 - 24
generated/blog_channels_defines.h

@@ -9,27 +9,28 @@
 #define BLOG_CHANNEL_ncd_concat 8
 #define BLOG_CHANNEL_ncd_if 9
 #define BLOG_CHANNEL_ncd_strcmp 10
-#define BLOG_CHANNEL_ncd_net_backend_physical 11
-#define BLOG_CHANNEL_ncd_net_backend_badvpn 12
-#define BLOG_CHANNEL_ncd_net_dns 13
-#define BLOG_CHANNEL_ncd_net_iptables 14
-#define BLOG_CHANNEL_ncd_net_ipv4_addr 15
-#define BLOG_CHANNEL_ncd_net_ipv4_dhcp 16
-#define BLOG_CHANNEL_ncd_net_ipv4_route 17
-#define BLOG_CHANNEL_StreamPeerIO 18
-#define BLOG_CHANNEL_DatagramPeerIO 19
-#define BLOG_CHANNEL_BReactor 20
-#define BLOG_CHANNEL_BSignal 21
-#define BLOG_CHANNEL_FragmentProtoAssembler 22
-#define BLOG_CHANNEL_BPredicate 23
-#define BLOG_CHANNEL_ServerConnection 24
-#define BLOG_CHANNEL_Listener 25
-#define BLOG_CHANNEL_DataProto 26
-#define BLOG_CHANNEL_FrameDecider 27
-#define BLOG_CHANNEL_BSocksClient 28
-#define BLOG_CHANNEL_BDHCPClientCore 29
-#define BLOG_CHANNEL_BDHCPClient 30
-#define BLOG_CHANNEL_NCDIfConfig 31
-#define BLOG_CHANNEL_BUnixSignal 32
-#define BLOG_CHANNEL_BProcess 33
-#define BLOG_NUM_CHANNELS 34
+#define BLOG_CHANNEL_ncd_ip_in_network 11
+#define BLOG_CHANNEL_ncd_net_backend_physical 12
+#define BLOG_CHANNEL_ncd_net_backend_badvpn 13
+#define BLOG_CHANNEL_ncd_net_dns 14
+#define BLOG_CHANNEL_ncd_net_iptables 15
+#define BLOG_CHANNEL_ncd_net_ipv4_addr 16
+#define BLOG_CHANNEL_ncd_net_ipv4_dhcp 17
+#define BLOG_CHANNEL_ncd_net_ipv4_route 18
+#define BLOG_CHANNEL_StreamPeerIO 19
+#define BLOG_CHANNEL_DatagramPeerIO 20
+#define BLOG_CHANNEL_BReactor 21
+#define BLOG_CHANNEL_BSignal 22
+#define BLOG_CHANNEL_FragmentProtoAssembler 23
+#define BLOG_CHANNEL_BPredicate 24
+#define BLOG_CHANNEL_ServerConnection 25
+#define BLOG_CHANNEL_Listener 26
+#define BLOG_CHANNEL_DataProto 27
+#define BLOG_CHANNEL_FrameDecider 28
+#define BLOG_CHANNEL_BSocksClient 29
+#define BLOG_CHANNEL_BDHCPClientCore 30
+#define BLOG_CHANNEL_BDHCPClient 31
+#define BLOG_CHANNEL_NCDIfConfig 32
+#define BLOG_CHANNEL_BUnixSignal 33
+#define BLOG_CHANNEL_BProcess 34
+#define BLOG_NUM_CHANNELS 35

+ 1 - 0
generated/blog_channels_list.h

@@ -9,6 +9,7 @@
 {.name = "ncd_concat", .loglevel = 4},
 {.name = "ncd_if", .loglevel = 4},
 {.name = "ncd_strcmp", .loglevel = 4},
+{.name = "ncd_ip_in_network", .loglevel = 4},
 {.name = "ncd_net_backend_physical", .loglevel = 4},
 {.name = "ncd_net_backend_badvpn", .loglevel = 4},
 {.name = "ncd_net_dns", .loglevel = 4},

+ 1 - 0
ncd/CMakeLists.txt

@@ -11,6 +11,7 @@ add_executable(badvpn-ncd
     modules/concat.c
     modules/if.c
     modules/strcmp.c
+    modules/ip_in_network.c
     modules/net_backend_physical.c
     modules/net_backend_badvpn.c
     modules/net_dns.c

+ 141 - 0
ncd/modules/ip_in_network.c

@@ -0,0 +1,141 @@
+/**
+ * @file ip_in_network.c
+ * @author Ambroz Bizjak <ambrop7@gmail.com>
+ * 
+ * @section LICENSE
+ * 
+ * This file is part of BadVPN.
+ * 
+ * BadVPN is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ * 
+ * BadVPN is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * @section DESCRIPTION
+ * 
+ * Module for checking whether two IP addresses belong to the same network.
+ * 
+ * Synopsis: ip_in_network(string addr1, string addr2, string netprefix)
+ * Variables:
+ *   string (empty) - "true" if addr1 and addr2 are in the same network, with
+ *     netprefix prefix, "false" if not (IPv4 only).
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <misc/ipaddr.h>
+#include <ncd/NCDModule.h>
+
+#include <generated/blog_channel_ncd_ip_in_network.h>
+
+#define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
+
+struct instance {
+    NCDModuleInst *i;
+    int value;
+};
+
+static void * func_new (NCDModuleInst *i)
+{
+    // allocate instance
+    struct instance *o = malloc(sizeof(*o));
+    if (!o) {
+        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
+        goto fail0;
+    }
+    
+    // init arguments
+    o->i = i;
+    
+    // read arguments
+    NCDValue *arg_addr1;
+    NCDValue *arg_addr2;
+    NCDValue *arg_netprefix;
+    if (!NCDValue_ListRead(o->i->args, 3, &arg_addr1, &arg_addr2, &arg_netprefix)) {
+        ModuleLog(o->i, BLOG_ERROR, "wrong arity");
+        goto fail1;
+    }
+    if (NCDValue_Type(arg_addr1) != NCDVALUE_STRING || NCDValue_Type(arg_addr2) != NCDVALUE_STRING || NCDValue_Type(arg_netprefix) != NCDVALUE_STRING) {
+        ModuleLog(o->i, BLOG_ERROR, "wrong type");
+        goto fail1;
+    }
+    
+    // parse
+    uint32_t addr1;
+    uint32_t addr2;
+    int netprefix;
+    if (!ipaddr_parse_ipv4_addr(NCDValue_StringValue(arg_addr1), &addr1)) {
+        ModuleLog(o->i, BLOG_ERROR, "wrong addr");
+        goto fail1;
+    }
+    if (!ipaddr_parse_ipv4_addr(NCDValue_StringValue(arg_addr2), &addr2)) {
+        ModuleLog(o->i, BLOG_ERROR, "wrong netaddr");
+        goto fail1;
+    }
+    if (!ipaddr_parse_ipv4_prefix(NCDValue_StringValue(arg_netprefix), &netprefix)) {
+        ModuleLog(o->i, BLOG_ERROR, "wrong netprefix");
+        goto fail1;
+    }
+    
+    // test
+    o->value = ipaddr_ipv4_addrs_in_network(addr1, addr2, netprefix);
+    
+    NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
+    
+    return o;
+    
+fail1:
+    free(o);
+fail0:
+    return NULL;
+}
+
+static void func_free (void *vo)
+{
+    struct instance *o = vo;
+    
+    // free instance
+    free(o);
+}
+
+static int func_getvar (void *vo, const char *name, NCDValue *out)
+{
+    struct instance *o = vo;
+    
+    if (!strcmp(name, "")) {
+        const char *v = (o->value ? "true" : "false");
+        
+        if (!NCDValue_InitString(out, v)) {
+            ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
+            return 0;
+        }
+        
+        return 1;
+    }
+    
+    return 0;
+}
+
+static const struct NCDModule modules[] = {
+    {
+        .type = "ip_in_network",
+        .func_new = func_new,
+        .func_free = func_free,
+        .func_getvar = func_getvar
+    }, {
+        .type = NULL
+    }
+};
+
+const struct NCDModuleGroup ncdmodule_ip_in_network = {
+    .modules = modules
+};

+ 2 - 0
ncd/modules/modules.h

@@ -33,6 +33,7 @@ extern const struct NCDModuleGroup ncdmodule_depend;
 extern const struct NCDModuleGroup ncdmodule_concat;
 extern const struct NCDModuleGroup ncdmodule_if;
 extern const struct NCDModuleGroup ncdmodule_strcmp;
+extern const struct NCDModuleGroup ncdmodule_ip_in_network;
 extern const struct NCDModuleGroup ncdmodule_net_backend_physical;
 extern const struct NCDModuleGroup ncdmodule_net_backend_badvpn;
 extern const struct NCDModuleGroup ncdmodule_net_dns;
@@ -48,6 +49,7 @@ static const struct NCDModuleGroup *ncd_modules[] = {
     &ncdmodule_concat,
     &ncdmodule_if,
     &ncdmodule_strcmp,
+    &ncdmodule_ip_in_network,
     &ncdmodule_net_backend_physical,
     &ncdmodule_net_backend_badvpn,
     &ncdmodule_net_dns,