Browse Source

ncd: add list concatenation module

ambrop7 15 years ago
parent
commit
cd61b3ce15

+ 1 - 0
blog_channels.txt

@@ -7,6 +7,7 @@ ncd_var 4
 ncd_list 4
 ncd_depend 4
 ncd_concat 4
+ncd_concatlist 4
 ncd_if 4
 ncd_strcmp 4
 ncd_logical 4

+ 4 - 0
generated/blog_channel_ncd_concatlist.h

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

+ 39 - 38
generated/blog_channels_defines.h

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

+ 1 - 0
generated/blog_channels_list.h

@@ -7,6 +7,7 @@
 {.name = "ncd_list", .loglevel = 4},
 {.name = "ncd_depend", .loglevel = 4},
 {.name = "ncd_concat", .loglevel = 4},
+{.name = "ncd_concatlist", .loglevel = 4},
 {.name = "ncd_if", .loglevel = 4},
 {.name = "ncd_strcmp", .loglevel = 4},
 {.name = "ncd_logical", .loglevel = 4},

+ 1 - 0
ncd/CMakeLists.txt

@@ -9,6 +9,7 @@ add_executable(badvpn-ncd
     modules/list.c
     modules/depend.c
     modules/concat.c
+    modules/concatlist.c
     modules/if.c
     modules/strcmp.c
     modules/logical.c

+ 137 - 0
ncd/modules/concatlist.c

@@ -0,0 +1,137 @@
+/**
+ * @file concatlist.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
+ * 
+ * List concatenation module.
+ * 
+ * Synopsis: concatlist(list elem1, ..., list elemN)
+ * Variables:
+ *   list (empty) - elem1, ..., elemN concatenated
+ */
+
+#include <stdlib.h>
+
+#include <ncd/NCDModule.h>
+
+#include <generated/blog_channel_ncd_concatlist.h>
+
+#define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
+
+struct instance {
+    NCDModuleInst *i;
+};
+
+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;
+    
+    // check arguments
+    NCDValue *arg = NCDValue_ListFirst(o->i->args);
+    while (arg) {
+        if (NCDValue_Type(arg) != NCDVALUE_LIST) {
+            ModuleLog(i, BLOG_ERROR, "wrong type");
+            goto fail1;
+        }
+        
+        arg = NCDValue_ListNext(o->i->args, arg);
+    }
+    
+    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, "")) {
+        NCDValue_InitList(out);
+        
+        NCDValue *arg = NCDValue_ListFirst(o->i->args);
+        while (arg) {
+            NCDValue *val = NCDValue_ListFirst(arg);
+            while (val) {
+                NCDValue copy;
+                if (!NCDValue_InitCopy(&copy, val)) {
+                    ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
+                    goto fail1;
+                }
+                
+                if (!NCDValue_ListAppend(out, copy)) {
+                    ModuleLog(o->i, BLOG_ERROR, "NCDValue_ListAppend failed");
+                    NCDValue_Free(&copy);
+                    goto fail1;
+                }
+                
+                val = NCDValue_ListNext(arg, val);
+            }
+            
+            arg = NCDValue_ListNext(o->i->args, arg);
+        }
+        
+        return 1;
+        
+    fail1:
+        NCDValue_Free(out);
+    fail0:
+        return 0;
+    }
+    
+    return 0;
+}
+
+static const struct NCDModule modules[] = {
+    {
+        .type = "concatlist",
+        .func_new = func_new,
+        .func_free = func_free,
+        .func_getvar = func_getvar
+    }, {
+        .type = NULL
+    }
+};
+
+const struct NCDModuleGroup ncdmodule_concatlist = {
+    .modules = modules
+};

+ 2 - 0
ncd/modules/modules.h

@@ -31,6 +31,7 @@ extern const struct NCDModuleGroup ncdmodule_var;
 extern const struct NCDModuleGroup ncdmodule_list;
 extern const struct NCDModuleGroup ncdmodule_depend;
 extern const struct NCDModuleGroup ncdmodule_concat;
+extern const struct NCDModuleGroup ncdmodule_concatlist;
 extern const struct NCDModuleGroup ncdmodule_if;
 extern const struct NCDModuleGroup ncdmodule_strcmp;
 extern const struct NCDModuleGroup ncdmodule_logical;
@@ -49,6 +50,7 @@ static const struct NCDModuleGroup *ncd_modules[] = {
     &ncdmodule_list,
     &ncdmodule_depend,
     &ncdmodule_concat,
+    &ncdmodule_concatlist,
     &ncdmodule_if,
     &ncdmodule_strcmp,
     &ncdmodule_logical,