ambrop7 14 лет назад
Родитель
Сommit
87139ff54b

+ 1 - 0
blog_channels.txt

@@ -24,6 +24,7 @@ ncd_spawn 4
 ncd_call 4
 ncd_ref 4
 ncd_index 4
+ncd_alias 4
 ncd_process_manager 4
 ncd_net_backend_waitdevice 4
 ncd_net_backend_waitlink 4

+ 4 - 0
generated/blog_channel_ncd_alias.h

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

+ 65 - 64
generated/blog_channels_defines.h

@@ -24,67 +24,68 @@
 #define BLOG_CHANNEL_ncd_call 23
 #define BLOG_CHANNEL_ncd_ref 24
 #define BLOG_CHANNEL_ncd_index 25
-#define BLOG_CHANNEL_ncd_process_manager 26
-#define BLOG_CHANNEL_ncd_net_backend_waitdevice 27
-#define BLOG_CHANNEL_ncd_net_backend_waitlink 28
-#define BLOG_CHANNEL_ncd_net_backend_badvpn 29
-#define BLOG_CHANNEL_ncd_net_backend_wpa_supplicant 30
-#define BLOG_CHANNEL_ncd_net_backend_rfkill 31
-#define BLOG_CHANNEL_ncd_net_up 32
-#define BLOG_CHANNEL_ncd_net_dns 33
-#define BLOG_CHANNEL_ncd_net_iptables 34
-#define BLOG_CHANNEL_ncd_net_ipv4_addr 35
-#define BLOG_CHANNEL_ncd_net_ipv4_route 36
-#define BLOG_CHANNEL_ncd_net_ipv4_dhcp 37
-#define BLOG_CHANNEL_ncd_net_watch_interfaces 38
-#define BLOG_CHANNEL_ncd_sys_watch_input 39
-#define BLOG_CHANNEL_ncd_sys_evdev 40
-#define BLOG_CHANNEL_ncd_sys_watch_directory 41
-#define BLOG_CHANNEL_StreamPeerIO 42
-#define BLOG_CHANNEL_DatagramPeerIO 43
-#define BLOG_CHANNEL_BReactor 44
-#define BLOG_CHANNEL_BSignal 45
-#define BLOG_CHANNEL_FragmentProtoAssembler 46
-#define BLOG_CHANNEL_BPredicate 47
-#define BLOG_CHANNEL_ServerConnection 48
-#define BLOG_CHANNEL_Listener 49
-#define BLOG_CHANNEL_DataProto 50
-#define BLOG_CHANNEL_FrameDecider 51
-#define BLOG_CHANNEL_BSocksClient 52
-#define BLOG_CHANNEL_BDHCPClientCore 53
-#define BLOG_CHANNEL_BDHCPClient 54
-#define BLOG_CHANNEL_NCDIfConfig 55
-#define BLOG_CHANNEL_BUnixSignal 56
-#define BLOG_CHANNEL_BProcess 57
-#define BLOG_CHANNEL_PRStreamSink 58
-#define BLOG_CHANNEL_PRStreamSource 59
-#define BLOG_CHANNEL_PacketProtoDecoder 60
-#define BLOG_CHANNEL_DPRelay 61
-#define BLOG_CHANNEL_BThreadWork 62
-#define BLOG_CHANNEL_DPReceive 63
-#define BLOG_CHANNEL_BInputProcess 64
-#define BLOG_CHANNEL_NCDUdevMonitorParser 65
-#define BLOG_CHANNEL_NCDUdevMonitor 66
-#define BLOG_CHANNEL_NCDUdevCache 67
-#define BLOG_CHANNEL_NCDUdevManager 68
-#define BLOG_CHANNEL_BTime 69
-#define BLOG_CHANNEL_BEncryption 70
-#define BLOG_CHANNEL_SPProtoDecoder 71
-#define BLOG_CHANNEL_LineBuffer 72
-#define BLOG_CHANNEL_BTap 73
-#define BLOG_CHANNEL_lwip 74
-#define BLOG_CHANNEL_NCDConfigParser 75
-#define BLOG_CHANNEL_nsskey 76
-#define BLOG_CHANNEL_addr 77
-#define BLOG_CHANNEL_PasswordListener 78
-#define BLOG_CHANNEL_NCDInterfaceMonitor 79
-#define BLOG_CHANNEL_NCDRfkillMonitor 80
-#define BLOG_CHANNEL_udpgw 81
-#define BLOG_CHANNEL_UdpGwClient 82
-#define BLOG_CHANNEL_SocksUdpGwClient 83
-#define BLOG_CHANNEL_BNetwork 84
-#define BLOG_CHANNEL_BConnection 85
-#define BLOG_CHANNEL_BSSLConnection 86
-#define BLOG_CHANNEL_BDatagram 87
-#define BLOG_CHANNEL_PeerChat 88
-#define BLOG_NUM_CHANNELS 89
+#define BLOG_CHANNEL_ncd_alias 26
+#define BLOG_CHANNEL_ncd_process_manager 27
+#define BLOG_CHANNEL_ncd_net_backend_waitdevice 28
+#define BLOG_CHANNEL_ncd_net_backend_waitlink 29
+#define BLOG_CHANNEL_ncd_net_backend_badvpn 30
+#define BLOG_CHANNEL_ncd_net_backend_wpa_supplicant 31
+#define BLOG_CHANNEL_ncd_net_backend_rfkill 32
+#define BLOG_CHANNEL_ncd_net_up 33
+#define BLOG_CHANNEL_ncd_net_dns 34
+#define BLOG_CHANNEL_ncd_net_iptables 35
+#define BLOG_CHANNEL_ncd_net_ipv4_addr 36
+#define BLOG_CHANNEL_ncd_net_ipv4_route 37
+#define BLOG_CHANNEL_ncd_net_ipv4_dhcp 38
+#define BLOG_CHANNEL_ncd_net_watch_interfaces 39
+#define BLOG_CHANNEL_ncd_sys_watch_input 40
+#define BLOG_CHANNEL_ncd_sys_evdev 41
+#define BLOG_CHANNEL_ncd_sys_watch_directory 42
+#define BLOG_CHANNEL_StreamPeerIO 43
+#define BLOG_CHANNEL_DatagramPeerIO 44
+#define BLOG_CHANNEL_BReactor 45
+#define BLOG_CHANNEL_BSignal 46
+#define BLOG_CHANNEL_FragmentProtoAssembler 47
+#define BLOG_CHANNEL_BPredicate 48
+#define BLOG_CHANNEL_ServerConnection 49
+#define BLOG_CHANNEL_Listener 50
+#define BLOG_CHANNEL_DataProto 51
+#define BLOG_CHANNEL_FrameDecider 52
+#define BLOG_CHANNEL_BSocksClient 53
+#define BLOG_CHANNEL_BDHCPClientCore 54
+#define BLOG_CHANNEL_BDHCPClient 55
+#define BLOG_CHANNEL_NCDIfConfig 56
+#define BLOG_CHANNEL_BUnixSignal 57
+#define BLOG_CHANNEL_BProcess 58
+#define BLOG_CHANNEL_PRStreamSink 59
+#define BLOG_CHANNEL_PRStreamSource 60
+#define BLOG_CHANNEL_PacketProtoDecoder 61
+#define BLOG_CHANNEL_DPRelay 62
+#define BLOG_CHANNEL_BThreadWork 63
+#define BLOG_CHANNEL_DPReceive 64
+#define BLOG_CHANNEL_BInputProcess 65
+#define BLOG_CHANNEL_NCDUdevMonitorParser 66
+#define BLOG_CHANNEL_NCDUdevMonitor 67
+#define BLOG_CHANNEL_NCDUdevCache 68
+#define BLOG_CHANNEL_NCDUdevManager 69
+#define BLOG_CHANNEL_BTime 70
+#define BLOG_CHANNEL_BEncryption 71
+#define BLOG_CHANNEL_SPProtoDecoder 72
+#define BLOG_CHANNEL_LineBuffer 73
+#define BLOG_CHANNEL_BTap 74
+#define BLOG_CHANNEL_lwip 75
+#define BLOG_CHANNEL_NCDConfigParser 76
+#define BLOG_CHANNEL_nsskey 77
+#define BLOG_CHANNEL_addr 78
+#define BLOG_CHANNEL_PasswordListener 79
+#define BLOG_CHANNEL_NCDInterfaceMonitor 80
+#define BLOG_CHANNEL_NCDRfkillMonitor 81
+#define BLOG_CHANNEL_udpgw 82
+#define BLOG_CHANNEL_UdpGwClient 83
+#define BLOG_CHANNEL_SocksUdpGwClient 84
+#define BLOG_CHANNEL_BNetwork 85
+#define BLOG_CHANNEL_BConnection 86
+#define BLOG_CHANNEL_BSSLConnection 87
+#define BLOG_CHANNEL_BDatagram 88
+#define BLOG_CHANNEL_PeerChat 89
+#define BLOG_NUM_CHANNELS 90

+ 1 - 0
generated/blog_channels_list.h

@@ -24,6 +24,7 @@
 {.name = "ncd_call", .loglevel = 4},
 {.name = "ncd_ref", .loglevel = 4},
 {.name = "ncd_index", .loglevel = 4},
+{.name = "ncd_alias", .loglevel = 4},
 {.name = "ncd_process_manager", .loglevel = 4},
 {.name = "ncd_net_backend_waitdevice", .loglevel = 4},
 {.name = "ncd_net_backend_waitlink", .loglevel = 4},

+ 1 - 0
ncd/CMakeLists.txt

@@ -58,6 +58,7 @@ add_executable(badvpn-ncd
     modules/call.c
     modules/ref.c
     modules/index.c
+    modules/alias.c
     modules/process_manager.c
     modules/net_backend_waitdevice.c
     modules/net_backend_waitlink.c

+ 150 - 0
ncd/modules/alias.c

@@ -0,0 +1,150 @@
+/**
+ * @file alias.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
+ * 
+ * Synopsis:
+ *   alias(string target)
+ * 
+ * Variables and objects:
+ *   - empty name - resolves target
+ *   - nonempty name N - resolves target.N
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <misc/concat_strings.h>
+#include <ncd/NCDModule.h>
+
+#include <generated/blog_channel_ncd_alias.h>
+
+#define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
+
+struct instance {
+    NCDModuleInst *i;
+    char *target;
+};
+
+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;
+    }
+    NCDModuleInst_Backend_SetUser(i, o);
+    
+    // init arguments
+    o->i = i;
+    
+    // read arguments
+    NCDValue *target_arg;
+    if (!NCDValue_ListRead(o->i->args, 1, &target_arg)) {
+        ModuleLog(o->i, BLOG_ERROR, "wrong arity");
+        goto fail1;
+    }
+    if (NCDValue_Type(target_arg) != NCDVALUE_STRING) {
+        ModuleLog(o->i, BLOG_ERROR, "wrong type");
+        goto fail1;
+    }
+    o->target = NCDValue_StringValue(target_arg);
+    
+    // signal up
+    NCDModuleInst_Backend_Up(o->i);
+    return;
+    
+fail1:
+    free(o);
+fail0:
+    NCDModuleInst_Backend_SetError(i);
+    NCDModuleInst_Backend_Dead(i);
+}
+
+static void func_die (void *vo)
+{
+    struct instance *o = vo;
+    NCDModuleInst *i = o->i;
+    
+    // free instance
+    free(o);
+    
+    NCDModuleInst_Backend_Dead(i);
+}
+
+static int func_getvar (void *vo, const char *name, NCDValue *out)
+{
+    struct instance *o = vo;
+    
+    if (!strcmp(name, "")) {
+        return NCDModuleInst_Backend_GetVar(o->i, o->target, out);
+    }
+    
+    char *target_name = concat_strings(3, o->target, ".", name);
+    if (!target_name) {
+        ModuleLog(o->i, BLOG_ERROR, "concat_strings failed");
+        return 0;
+    }
+    
+    int res = NCDModuleInst_Backend_GetVar(o->i, target_name, out);
+    
+    free(target_name);
+    
+    return res;
+}
+
+static NCDModuleInst * func_getobj (void *vo, const char *name)
+{
+    struct instance *o = vo;
+    
+    if (!strcmp(name, "")) {
+        return NCDModuleInst_Backend_GetObj(o->i, o->target);
+    }
+    
+    char *target_name = concat_strings(3, o->target, ".", name);
+    if (!target_name) {
+        ModuleLog(o->i, BLOG_ERROR, "concat_strings failed");
+        return NULL;
+    }
+    
+    NCDModuleInst *res = NCDModuleInst_Backend_GetObj(o->i, target_name);
+    
+    free(target_name);
+    
+    return res;
+}
+
+static const struct NCDModule modules[] = {
+    {
+        .type = "alias",
+        .func_new = func_new,
+        .func_die = func_die,
+        .func_getvar = func_getvar,
+        .func_getobj = func_getobj
+    }, {
+        .type = NULL
+    }
+};
+
+const struct NCDModuleGroup ncdmodule_alias = {
+    .modules = modules
+};

+ 2 - 0
ncd/modules/modules.h

@@ -48,6 +48,7 @@ extern const struct NCDModuleGroup ncdmodule_spawn;
 extern const struct NCDModuleGroup ncdmodule_call;
 extern const struct NCDModuleGroup ncdmodule_ref;
 extern const struct NCDModuleGroup ncdmodule_index;
+extern const struct NCDModuleGroup ncdmodule_alias;
 extern const struct NCDModuleGroup ncdmodule_process_manager;
 extern const struct NCDModuleGroup ncdmodule_net_backend_waitdevice;
 extern const struct NCDModuleGroup ncdmodule_net_backend_waitlink;
@@ -93,6 +94,7 @@ static const struct NCDModuleGroup *ncd_modules[] = {
     &ncdmodule_call,
     &ncdmodule_ref,
     &ncdmodule_index,
+    &ncdmodule_alias,
     &ncdmodule_process_manager,
     &ncdmodule_net_backend_waitdevice,
     &ncdmodule_net_backend_waitlink,