Quellcode durchsuchen

ncd: add module for logical operators

ambrop7 vor 15 Jahren
Ursprung
Commit
873710cc86

+ 1 - 0
blog_channels.txt

@@ -9,6 +9,7 @@ ncd_depend 4
 ncd_concat 4
 ncd_if 4
 ncd_strcmp 4
+ncd_logical 4
 ncd_ip_in_network 4
 ncd_run 4
 ncd_net_backend_physical 4

+ 4 - 0
generated/blog_channel_ncd_logical.h

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

+ 36 - 35
generated/blog_channels_defines.h

@@ -9,38 +9,39 @@
 #define BLOG_CHANNEL_ncd_concat 8
 #define BLOG_CHANNEL_ncd_if 9
 #define BLOG_CHANNEL_ncd_strcmp 10
-#define BLOG_CHANNEL_ncd_ip_in_network 11
-#define BLOG_CHANNEL_ncd_run 12
-#define BLOG_CHANNEL_ncd_net_backend_physical 13
-#define BLOG_CHANNEL_ncd_net_backend_badvpn 14
-#define BLOG_CHANNEL_ncd_net_dns 15
-#define BLOG_CHANNEL_ncd_net_iptables 16
-#define BLOG_CHANNEL_ncd_net_ipv4_addr 17
-#define BLOG_CHANNEL_ncd_net_ipv4_dhcp 18
-#define BLOG_CHANNEL_ncd_net_ipv4_route 19
-#define BLOG_CHANNEL_StreamPeerIO 20
-#define BLOG_CHANNEL_DatagramPeerIO 21
-#define BLOG_CHANNEL_BReactor 22
-#define BLOG_CHANNEL_BSignal 23
-#define BLOG_CHANNEL_FragmentProtoAssembler 24
-#define BLOG_CHANNEL_BPredicate 25
-#define BLOG_CHANNEL_ServerConnection 26
-#define BLOG_CHANNEL_Listener 27
-#define BLOG_CHANNEL_DataProto 28
-#define BLOG_CHANNEL_FrameDecider 29
-#define BLOG_CHANNEL_BSocksClient 30
-#define BLOG_CHANNEL_BDHCPClientCore 31
-#define BLOG_CHANNEL_BDHCPClient 32
-#define BLOG_CHANNEL_NCDIfConfig 33
-#define BLOG_CHANNEL_BUnixSignal 34
-#define BLOG_CHANNEL_BProcess 35
-#define BLOG_CHANNEL_StreamSocketSink 36
-#define BLOG_CHANNEL_StreamSocketSource 37
-#define BLOG_CHANNEL_DatagramSocketSink 38
-#define BLOG_CHANNEL_DatagramSocketSource 39
-#define BLOG_CHANNEL_PRStreamSink 40
-#define BLOG_CHANNEL_PRStreamSource 41
-#define BLOG_CHANNEL_BSocketPRFileDesc 42
-#define BLOG_CHANNEL_PacketProtoDecoder 43
-#define BLOG_CHANNEL_DPRelay 44
-#define BLOG_NUM_CHANNELS 45
+#define BLOG_CHANNEL_ncd_logical 11
+#define BLOG_CHANNEL_ncd_ip_in_network 12
+#define BLOG_CHANNEL_ncd_run 13
+#define BLOG_CHANNEL_ncd_net_backend_physical 14
+#define BLOG_CHANNEL_ncd_net_backend_badvpn 15
+#define BLOG_CHANNEL_ncd_net_dns 16
+#define BLOG_CHANNEL_ncd_net_iptables 17
+#define BLOG_CHANNEL_ncd_net_ipv4_addr 18
+#define BLOG_CHANNEL_ncd_net_ipv4_dhcp 19
+#define BLOG_CHANNEL_ncd_net_ipv4_route 20
+#define BLOG_CHANNEL_StreamPeerIO 21
+#define BLOG_CHANNEL_DatagramPeerIO 22
+#define BLOG_CHANNEL_BReactor 23
+#define BLOG_CHANNEL_BSignal 24
+#define BLOG_CHANNEL_FragmentProtoAssembler 25
+#define BLOG_CHANNEL_BPredicate 26
+#define BLOG_CHANNEL_ServerConnection 27
+#define BLOG_CHANNEL_Listener 28
+#define BLOG_CHANNEL_DataProto 29
+#define BLOG_CHANNEL_FrameDecider 30
+#define BLOG_CHANNEL_BSocksClient 31
+#define BLOG_CHANNEL_BDHCPClientCore 32
+#define BLOG_CHANNEL_BDHCPClient 33
+#define BLOG_CHANNEL_NCDIfConfig 34
+#define BLOG_CHANNEL_BUnixSignal 35
+#define BLOG_CHANNEL_BProcess 36
+#define BLOG_CHANNEL_StreamSocketSink 37
+#define BLOG_CHANNEL_StreamSocketSource 38
+#define BLOG_CHANNEL_DatagramSocketSink 39
+#define BLOG_CHANNEL_DatagramSocketSource 40
+#define BLOG_CHANNEL_PRStreamSink 41
+#define BLOG_CHANNEL_PRStreamSource 42
+#define BLOG_CHANNEL_BSocketPRFileDesc 43
+#define BLOG_CHANNEL_PacketProtoDecoder 44
+#define BLOG_CHANNEL_DPRelay 45
+#define BLOG_NUM_CHANNELS 46

+ 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_logical", .loglevel = 4},
 {.name = "ncd_ip_in_network", .loglevel = 4},
 {.name = "ncd_run", .loglevel = 4},
 {.name = "ncd_net_backend_physical", .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/logical.c
     modules/ip_in_network.c
     modules/run.c
     modules/net_backend_physical.c

+ 170 - 0
ncd/modules/logical.c

@@ -0,0 +1,170 @@
+/**
+ * @file logical.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 logical operators.
+ * 
+ * Synopsis: not(string val)
+ * Variables:
+ *   string (empty) - "true" if val does not equal "true", "false" otherwise
+ * 
+ * Synopsis: or(string val1, string val2)
+ * Variables:
+ *   string (empty) - "true" if val1 or val2 equal "true", "false" otherwise
+ * 
+ * Synopsis: and(string val1, string val2)
+ * Variables:
+ *   string (empty) - "true" if val1 and val2 equal "true", "false" otherwise
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <ncd/NCDModule.h>
+
+#include <generated/blog_channel_ncd_logical.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, int not, int or)
+{
+    // 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;
+    
+    if (not) {
+        NCDValue *arg;
+        if (!NCDValue_ListRead(o->i->args, 1, &arg)) {
+            ModuleLog(o->i, BLOG_ERROR, "wrong arity");
+            goto fail1;
+        }
+        if (NCDValue_Type(arg) != NCDVALUE_STRING) {
+            ModuleLog(o->i, BLOG_ERROR, "wrong type");
+            goto fail1;
+        }
+        
+        o->value = !!strcmp(NCDValue_StringValue(arg), "true");
+    } else {
+        NCDValue *arg1;
+        NCDValue *arg2;
+        if (!NCDValue_ListRead(o->i->args, 2, &arg1, &arg2)) {
+            ModuleLog(o->i, BLOG_ERROR, "wrong arity");
+            goto fail1;
+        }
+        if (NCDValue_Type(arg1) != NCDVALUE_STRING || NCDValue_Type(arg2) != NCDVALUE_STRING) {
+            ModuleLog(o->i, BLOG_ERROR, "wrong type");
+            goto fail1;
+        }
+        
+        if (or) {
+            o->value = (!strcmp(NCDValue_StringValue(arg1), "true") || !strcmp(NCDValue_StringValue(arg2), "true"));
+        } else {
+            o->value = (!strcmp(NCDValue_StringValue(arg1), "true") && !strcmp(NCDValue_StringValue(arg2), "true"));
+        }
+    }
+    
+    NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
+    
+    return o;
+    
+fail1:
+    free(o);
+fail0:
+    return NULL;
+}
+
+static void * func_new_not (NCDModuleInst *i)
+{
+    return func_new(i, 1, 0);
+}
+
+static void * func_new_or (NCDModuleInst *i)
+{
+    return func_new(i, 0, 1);
+}
+
+static void * func_new_and (NCDModuleInst *i)
+{
+    return func_new(i, 0, 0);
+}
+
+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 = "not",
+        .func_new = func_new_not,
+        .func_free = func_free,
+        .func_getvar = func_getvar
+    }, {
+        .type = "or",
+        .func_new = func_new_or,
+        .func_free = func_free,
+        .func_getvar = func_getvar
+    }, {
+        .type = "and",
+        .func_new = func_new_and,
+        .func_free = func_free,
+        .func_getvar = func_getvar
+    }, {
+        .type = NULL
+    }
+};
+
+const struct NCDModuleGroup ncdmodule_logical = {
+    .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_logical;
 extern const struct NCDModuleGroup ncdmodule_ip_in_network;
 extern const struct NCDModuleGroup ncdmodule_run;
 extern const struct NCDModuleGroup ncdmodule_net_backend_physical;
@@ -50,6 +51,7 @@ static const struct NCDModuleGroup *ncd_modules[] = {
     &ncdmodule_concat,
     &ncdmodule_if,
     &ncdmodule_strcmp,
+    &ncdmodule_logical,
     &ncdmodule_ip_in_network,
     &ncdmodule_run,
     &ncdmodule_net_backend_physical,