Ver código fonte

ncd: add concat module

ambrop7 15 anos atrás
pai
commit
d41d39b0f8

+ 1 - 0
blog_channels.txt

@@ -6,6 +6,7 @@ ncd 4
 ncd_var 4
 ncd_list 4
 ncd_depend 4
+ncd_concat 4
 ncd_net_backend_physical 4
 ncd_net_backend_badvpn 4
 ncd_net_dns 4

+ 4 - 0
generated/blog_channel_ncd_concat.h

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

+ 24 - 23
generated/blog_channels_defines.h

@@ -6,26 +6,27 @@
 #define BLOG_CHANNEL_ncd_var 5
 #define BLOG_CHANNEL_ncd_list 6
 #define BLOG_CHANNEL_ncd_depend 7
-#define BLOG_CHANNEL_ncd_net_backend_physical 8
-#define BLOG_CHANNEL_ncd_net_backend_badvpn 9
-#define BLOG_CHANNEL_ncd_net_dns 10
-#define BLOG_CHANNEL_ncd_net_ipv4_addr 11
-#define BLOG_CHANNEL_ncd_net_ipv4_dhcp 12
-#define BLOG_CHANNEL_ncd_net_ipv4_route 13
-#define BLOG_CHANNEL_StreamPeerIO 14
-#define BLOG_CHANNEL_DatagramPeerIO 15
-#define BLOG_CHANNEL_BReactor 16
-#define BLOG_CHANNEL_BSignal 17
-#define BLOG_CHANNEL_FragmentProtoAssembler 18
-#define BLOG_CHANNEL_BPredicate 19
-#define BLOG_CHANNEL_ServerConnection 20
-#define BLOG_CHANNEL_Listener 21
-#define BLOG_CHANNEL_DataProto 22
-#define BLOG_CHANNEL_FrameDecider 23
-#define BLOG_CHANNEL_BSocksClient 24
-#define BLOG_CHANNEL_BDHCPClientCore 25
-#define BLOG_CHANNEL_BDHCPClient 26
-#define BLOG_CHANNEL_NCDIfConfig 27
-#define BLOG_CHANNEL_BUnixSignal 28
-#define BLOG_CHANNEL_BProcess 29
-#define BLOG_NUM_CHANNELS 30
+#define BLOG_CHANNEL_ncd_concat 8
+#define BLOG_CHANNEL_ncd_net_backend_physical 9
+#define BLOG_CHANNEL_ncd_net_backend_badvpn 10
+#define BLOG_CHANNEL_ncd_net_dns 11
+#define BLOG_CHANNEL_ncd_net_ipv4_addr 12
+#define BLOG_CHANNEL_ncd_net_ipv4_dhcp 13
+#define BLOG_CHANNEL_ncd_net_ipv4_route 14
+#define BLOG_CHANNEL_StreamPeerIO 15
+#define BLOG_CHANNEL_DatagramPeerIO 16
+#define BLOG_CHANNEL_BReactor 17
+#define BLOG_CHANNEL_BSignal 18
+#define BLOG_CHANNEL_FragmentProtoAssembler 19
+#define BLOG_CHANNEL_BPredicate 20
+#define BLOG_CHANNEL_ServerConnection 21
+#define BLOG_CHANNEL_Listener 22
+#define BLOG_CHANNEL_DataProto 23
+#define BLOG_CHANNEL_FrameDecider 24
+#define BLOG_CHANNEL_BSocksClient 25
+#define BLOG_CHANNEL_BDHCPClientCore 26
+#define BLOG_CHANNEL_BDHCPClient 27
+#define BLOG_CHANNEL_NCDIfConfig 28
+#define BLOG_CHANNEL_BUnixSignal 29
+#define BLOG_CHANNEL_BProcess 30
+#define BLOG_NUM_CHANNELS 31

+ 1 - 0
generated/blog_channels_list.h

@@ -6,6 +6,7 @@
 {.name = "ncd_var", .loglevel = 4},
 {.name = "ncd_list", .loglevel = 4},
 {.name = "ncd_depend", .loglevel = 4},
+{.name = "ncd_concat", .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

@@ -7,6 +7,7 @@ add_executable(badvpn-ncd
     modules/var.c
     modules/list.c
     modules/depend.c
+    modules/concat.c
     modules/net_backend_physical.c
     modules/net_backend_badvpn.c
     modules/net_dns.c

+ 2 - 2
ncd/README

@@ -12,7 +12,7 @@
 # reports having transitioned into the UP state, it initializes the next statement in the DOWN state,
 # and so on.
 #
-# However, execution can go in the other direction to. A statement in the UP state can, at any time,
+# However, execution can go in the other direction too. A statement in the UP state can, at any time,
 # report having transitioned into the DOWN state. At this point, any statements after that one will
 # automatically be de-initialized. The de-initiazation is done from the bottom up. First the last
 # initialized statement after the problematic statement is requested to terminate and enters the
@@ -167,7 +167,7 @@ process vpn {
     # The VPN client program needs to know some IP addresses in order to tell other peers where to connect to.
     # Obtain this informations from variables in the "lan" process through the depend() statement. TODO: not implemented yet!
 
-    # Construct the local address (addr + port). TODO: concat not implemented yet!
+    # Construct the local address (addr + port).
     concat(landep.ipaddr, ":", port) local_addr_arg;
 
     # Construct the Internet address (assuming we are behind a NAT).

+ 135 - 0
ncd/modules/concat.c

@@ -0,0 +1,135 @@
+/**
+ * @file concat.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
+ * 
+ * String concatenation module.
+ * 
+ * Synopsis: concat(string elem1, ..., string elemN)
+ * Variables:
+ *   string (empty) - elem1, ..., elemN concatenated
+ */
+
+#include <stdlib.h>
+
+#include <misc/expstring.h>
+#include <ncd/NCDModule.h>
+
+#include <generated/blog_channel_ncd_concat.h>
+
+#define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
+
+struct instance {
+    NCDModuleInst *i;
+    char *string;
+};
+
+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;
+    
+    // init string
+    ExpString s;
+    if (!ExpString_Init(&s)) {
+        ModuleLog(i, BLOG_ERROR, "ExpString_Init failed");
+        goto fail1;
+    }
+    
+    // append arguments
+    NCDValue *arg = NCDValue_ListFirst(o->i->args);
+    while (arg) {
+        if (NCDValue_Type(arg) != NCDVALUE_STRING) {
+            ModuleLog(i, BLOG_ERROR, "wrong type");
+            goto fail2;
+        }
+        
+        if (!ExpString_Append(&s, NCDValue_StringValue(arg))) {
+            ModuleLog(i, BLOG_ERROR, "ExpString_Append failed");
+            goto fail2;
+        }
+        
+        arg = NCDValue_ListNext(o->i->args, arg);
+    }
+    
+    // set string
+    o->string = ExpString_Get(&s);
+    
+    NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
+    
+    return o;
+    
+fail2:
+    ExpString_Free(&s);
+fail1:
+    free(o);
+fail0:
+    return NULL;
+}
+
+static void func_free (void *vo)
+{
+    struct instance *o = vo;
+    
+    // free string
+    free(o->string);
+    
+    // free instance
+    free(o);
+}
+
+static int func_getvar (void *vo, const char *name, NCDValue *out)
+{
+    struct instance *o = vo;
+    
+    if (!strcmp(name, "")) {
+        if (!NCDValue_InitString(out, o->string)) {
+            ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
+            return 0;
+        }
+        
+        return 1;
+    }
+    
+    return 0;
+}
+
+static const struct NCDModule modules[] = {
+    {
+        .type = "concat",
+        .func_new = func_new,
+        .func_free = func_free,
+        .func_getvar = func_getvar
+    }, {
+        .type = NULL
+    }
+};
+
+const struct NCDModuleGroup ncdmodule_concat = {
+    .modules = modules
+};

+ 2 - 0
ncd/modules/modules.h

@@ -30,6 +30,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_net_backend_physical;
 extern const struct NCDModuleGroup ncdmodule_net_backend_badvpn;
 extern const struct NCDModuleGroup ncdmodule_net_dns;
@@ -41,6 +42,7 @@ static const struct NCDModuleGroup *ncd_modules[] = {
     &ncdmodule_var,
     &ncdmodule_list,
     &ncdmodule_depend,
+    &ncdmodule_concat,
     &ncdmodule_net_backend_physical,
     &ncdmodule_net_backend_badvpn,
     &ncdmodule_net_dns,