فهرست منبع

ncd: modules: add ref

ambrop7 14 سال پیش
والد
کامیت
d2d68e8e08
7فایلهای تغییر یافته به همراه314 افزوده شده و 64 حذف شده
  1. 1 0
      blog_channels.txt
  2. 4 0
      generated/blog_channel_ncd_ref.h
  3. 65 64
      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. 240 0
      ncd/modules/ref.c

+ 1 - 0
blog_channels.txt

@@ -22,6 +22,7 @@ ncd_run 4
 ncd_runonce 4
 ncd_synchronous_process 4
 ncd_call 4
+ncd_ref 4
 ncd_process_manager 4
 ncd_net_backend_waitdevice 4
 ncd_net_backend_waitlink 4

+ 4 - 0
generated/blog_channel_ncd_ref.h

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

+ 65 - 64
generated/blog_channels_defines.h

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

+ 1 - 0
generated/blog_channels_list.h

@@ -22,6 +22,7 @@
 {.name = "ncd_runonce", .loglevel = 4},
 {.name = "ncd_synchronous_process", .loglevel = 4},
 {.name = "ncd_call", .loglevel = 4},
+{.name = "ncd_ref", .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

@@ -56,6 +56,7 @@ add_executable(badvpn-ncd
     modules/runonce.c
     modules/synchronous_process.c
     modules/call.c
+    modules/ref.c
     modules/process_manager.c
     modules/net_backend_waitdevice.c
     modules/net_backend_waitlink.c

+ 2 - 0
ncd/modules/modules.h

@@ -46,6 +46,7 @@ extern const struct NCDModuleGroup ncdmodule_run;
 extern const struct NCDModuleGroup ncdmodule_runonce;
 extern const struct NCDModuleGroup ncdmodule_synchronous_process;
 extern const struct NCDModuleGroup ncdmodule_call;
+extern const struct NCDModuleGroup ncdmodule_ref;
 extern const struct NCDModuleGroup ncdmodule_process_manager;
 extern const struct NCDModuleGroup ncdmodule_net_backend_waitdevice;
 extern const struct NCDModuleGroup ncdmodule_net_backend_waitlink;
@@ -89,6 +90,7 @@ static const struct NCDModuleGroup *ncd_modules[] = {
     &ncdmodule_runonce,
     &ncdmodule_synchronous_process,
     &ncdmodule_call,
+    &ncdmodule_ref,
     &ncdmodule_process_manager,
     &ncdmodule_net_backend_waitdevice,
     &ncdmodule_net_backend_waitlink,

+ 240 - 0
ncd/modules/ref.c

@@ -0,0 +1,240 @@
+/**
+ * @file ref.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
+ * 
+ * References module.
+ * 
+ * Synopsis:
+ *   refhere()
+ * Variables:
+ *   Exposes variables and objects as seen from this refhere() statement.
+ * 
+ * Synopsis:
+ *   ref refhere::ref()
+ *   ref ref::ref()
+ * Variables:
+ *   Exposes variables and objects as seen from the corresponding refhere()
+ *   statement.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <misc/offset.h>
+#include <structure/LinkedList0.h>
+#include <ncd/NCDModule.h>
+
+#include <generated/blog_channel_ncd_ref.h>
+
+#define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
+
+struct refhere_instance {
+    NCDModuleInst *i;
+    LinkedList0 refs_list;
+};
+
+struct ref_instance {
+    NCDModuleInst *i;
+    struct refhere_instance *rh;
+    LinkedList0Node refs_list_node;
+};
+
+static void ref_instance_free (struct ref_instance *o);
+
+static void refhere_func_new (NCDModuleInst *i)
+{
+    // allocate instance
+    struct refhere_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;
+    
+    // check arguments
+    if (!NCDValue_ListRead(i->args, 0)) {
+        ModuleLog(o->i, BLOG_ERROR, "wrong arity");
+        goto fail1;
+    }
+    
+    // init refs list
+    LinkedList0_Init(&o->refs_list);
+    
+    // signal up
+    NCDModuleInst_Backend_Up(o->i);
+    return;
+    
+fail1:
+    free(o);
+fail0:
+    NCDModuleInst_Backend_SetError(i);
+    NCDModuleInst_Backend_Dead(i);
+}
+
+static void refhere_func_die (void *vo)
+{
+    struct refhere_instance *o = vo;
+    NCDModuleInst *i = o->i;
+    
+    // die refs
+    while (!LinkedList0_IsEmpty(&o->refs_list)) {
+        struct ref_instance *ref = UPPER_OBJECT(LinkedList0_GetFirst(&o->refs_list), struct ref_instance, refs_list_node);
+        ASSERT(ref->rh == o)
+        ref_instance_free(ref);
+    }
+    
+    // free instance
+    free(o);
+    
+    NCDModuleInst_Backend_Dead(i);
+}
+
+static int refhere_func_getvar (void *vo, const char *varname, NCDValue *out)
+{
+    struct refhere_instance *o = vo;
+    
+    return NCDModuleInst_Backend_GetVar(o->i, varname, out);
+}
+
+static NCDModuleInst * refhere_func_getobj (void *vo, const char *objname)
+{
+    struct refhere_instance *o = vo;
+    
+    return NCDModuleInst_Backend_GetObj(o->i, objname);
+}
+
+static void ref_func_new_templ (NCDModuleInst *i, struct refhere_instance *rh)
+{
+    // allocate instance
+    struct ref_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;
+    
+    // check arguments
+    if (!NCDValue_ListRead(i->args, 0)) {
+        ModuleLog(o->i, BLOG_ERROR, "wrong arity");
+        goto fail1;
+    }
+    
+    // set refhere
+    o->rh = rh;
+    
+    // add to refhere's refs list
+    LinkedList0_Prepend(&o->rh->refs_list, &o->refs_list_node);
+    
+    // signal up
+    NCDModuleInst_Backend_Up(o->i);
+    return;
+    
+fail1:
+    free(o);
+fail0:
+    NCDModuleInst_Backend_SetError(i);
+    NCDModuleInst_Backend_Dead(i);
+}
+
+static void ref_func_new_from_refhere (NCDModuleInst *i)
+{
+    struct refhere_instance *rh = i->method_object->inst_user;
+    
+    return ref_func_new_templ(i, rh);
+}
+
+static void ref_func_new_from_ref (NCDModuleInst *i)
+{
+    struct ref_instance *ref = i->method_object->inst_user;
+    
+    return ref_func_new_templ(i, ref->rh);
+}
+
+static void ref_instance_free (struct ref_instance *o)
+{
+    NCDModuleInst *i = o->i;
+    
+    // remove from refhere's reft list
+    LinkedList0_Remove(&o->rh->refs_list, &o->refs_list_node);
+    
+    // free instance
+    free(o);
+    
+    NCDModuleInst_Backend_Dead(i);
+}
+
+static void ref_func_die (void *vo)
+{
+    struct ref_instance *o = vo;
+    
+    ref_instance_free(o);
+}
+
+static int ref_func_getvar (void *vo, const char *varname, NCDValue *out)
+{
+    struct ref_instance *o = vo;
+    
+    return NCDModuleInst_Backend_GetVar(o->rh->i, varname, out);
+}
+
+static NCDModuleInst * ref_func_getobj (void *vo, const char *objname)
+{
+    struct ref_instance *o = vo;
+    
+    return NCDModuleInst_Backend_GetObj(o->rh->i, objname);
+}
+
+static const struct NCDModule modules[] = {
+    {
+        .type = "refhere",
+        .func_new = refhere_func_new,
+        .func_die = refhere_func_die,
+        .func_getvar = refhere_func_getvar,
+        .func_getobj = refhere_func_getobj
+    }, {
+        .type = "refhere::ref",
+        .base_type = "ref",
+        .func_new = ref_func_new_from_refhere,
+        .func_die = ref_func_die,
+        .func_getvar = ref_func_getvar,
+        .func_getobj = ref_func_getobj
+    }, {
+        .type = "ref::ref",
+        .base_type = "ref",
+        .func_new = ref_func_new_from_ref,
+        .func_die = ref_func_die,
+        .func_getvar = ref_func_getvar,
+        .func_getobj = ref_func_getobj
+    }, {
+        .type = NULL
+    }
+};
+
+const struct NCDModuleGroup ncdmodule_ref = {
+    .modules = modules
+};