فهرست منبع

ncd: add strcmp module

ambrop7 15 سال پیش
والد
کامیت
2afa83b08f
7فایلهای تغییر یافته به همراه151 افزوده شده و 24 حذف شده
  1. 1 0
      blog_channels.txt
  2. 4 0
      generated/blog_channel_ncd_strcmp.h
  3. 25 24
      generated/blog_channels_defines.h
  4. 1 0
      generated/blog_channels_list.h
  5. 1 0
      ncd/CMakeLists.txt
  6. 2 0
      ncd/modules/modules.h
  7. 117 0
      ncd/modules/strcmp.c

+ 1 - 0
blog_channels.txt

@@ -8,6 +8,7 @@ ncd_list 4
 ncd_depend 4
 ncd_concat 4
 ncd_if 4
+ncd_strcmp 4
 ncd_net_backend_physical 4
 ncd_net_backend_badvpn 4
 ncd_net_dns 4

+ 4 - 0
generated/blog_channel_ncd_strcmp.h

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

+ 25 - 24
generated/blog_channels_defines.h

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

+ 1 - 0
generated/blog_channels_list.h

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

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

+ 2 - 0
ncd/modules/modules.h

@@ -32,6 +32,7 @@ extern const struct NCDModuleGroup ncdmodule_list;
 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_net_backend_physical;
 extern const struct NCDModuleGroup ncdmodule_net_backend_badvpn;
 extern const struct NCDModuleGroup ncdmodule_net_dns;
@@ -46,6 +47,7 @@ static const struct NCDModuleGroup *ncd_modules[] = {
     &ncdmodule_depend,
     &ncdmodule_concat,
     &ncdmodule_if,
+    &ncdmodule_strcmp,
     &ncdmodule_net_backend_physical,
     &ncdmodule_net_backend_badvpn,
     &ncdmodule_net_dns,

+ 117 - 0
ncd/modules/strcmp.c

@@ -0,0 +1,117 @@
+/**
+ * @file strcmp.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 comparison module.
+ * 
+ * Synopsis: strcmp(string str1, string str2)
+ * Variables:
+ *   string (empty) - "true" if str1 and str2 are equal, "false" if not
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <ncd/NCDModule.h>
+
+#include <generated/blog_channel_ncd_var.h>
+
+#define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
+
+struct instance {
+    NCDModuleInst *i;
+    NCDValue *arg1;
+    NCDValue *arg2;
+};
+
+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
+    if (!NCDValue_ListRead(o->i->args, 2, &o->arg1, &o->arg2)) {
+        ModuleLog(o->i, BLOG_ERROR, "wrong arity");
+        goto fail1;
+    }
+    if (NCDValue_Type(o->arg1) != NCDVALUE_STRING || NCDValue_Type(o->arg2) != NCDVALUE_STRING) {
+        ModuleLog(o->i, BLOG_ERROR, "wrong type");
+        goto fail1;
+    }
+    
+    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 = (!strcmp(NCDValue_StringValue(o->arg1), NCDValue_StringValue(o->arg2)) ? "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 = "strcmp",
+        .func_new = func_new,
+        .func_free = func_free,
+        .func_getvar = func_getvar
+    }, {
+        .type = NULL
+    }
+};
+
+const struct NCDModuleGroup ncdmodule_strcmp = {
+    .modules = modules
+};