Procházet zdrojové kódy

ncd: add net.backend.waitdevice module

ambrop7 před 15 roky
rodič
revize
6297991794

+ 1 - 0
blog_channels.txt

@@ -14,6 +14,7 @@ ncd_logical 4
 ncd_ip_in_network 4
 ncd_run 4
 ncd_net_backend_physical 4
+ncd_net_backend_waitdevice 4
 ncd_net_backend_badvpn 4
 ncd_net_dns 4
 ncd_net_iptables 4

+ 4 - 0
generated/blog_channel_ncd_net_backend_waitdevice.h

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

+ 35 - 34
generated/blog_channels_defines.h

@@ -14,37 +14,38 @@
 #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_CHANNEL_BThreadWork 47
-#define BLOG_CHANNEL_DPReceive 48
-#define BLOG_NUM_CHANNELS 49
+#define BLOG_CHANNEL_ncd_net_backend_waitdevice 16
+#define BLOG_CHANNEL_ncd_net_backend_badvpn 17
+#define BLOG_CHANNEL_ncd_net_dns 18
+#define BLOG_CHANNEL_ncd_net_iptables 19
+#define BLOG_CHANNEL_ncd_net_ipv4_addr 20
+#define BLOG_CHANNEL_ncd_net_ipv4_dhcp 21
+#define BLOG_CHANNEL_ncd_net_ipv4_route 22
+#define BLOG_CHANNEL_StreamPeerIO 23
+#define BLOG_CHANNEL_DatagramPeerIO 24
+#define BLOG_CHANNEL_BReactor 25
+#define BLOG_CHANNEL_BSignal 26
+#define BLOG_CHANNEL_FragmentProtoAssembler 27
+#define BLOG_CHANNEL_BPredicate 28
+#define BLOG_CHANNEL_ServerConnection 29
+#define BLOG_CHANNEL_Listener 30
+#define BLOG_CHANNEL_DataProto 31
+#define BLOG_CHANNEL_FrameDecider 32
+#define BLOG_CHANNEL_BSocksClient 33
+#define BLOG_CHANNEL_BDHCPClientCore 34
+#define BLOG_CHANNEL_BDHCPClient 35
+#define BLOG_CHANNEL_NCDIfConfig 36
+#define BLOG_CHANNEL_BUnixSignal 37
+#define BLOG_CHANNEL_BProcess 38
+#define BLOG_CHANNEL_StreamSocketSink 39
+#define BLOG_CHANNEL_StreamSocketSource 40
+#define BLOG_CHANNEL_DatagramSocketSink 41
+#define BLOG_CHANNEL_DatagramSocketSource 42
+#define BLOG_CHANNEL_PRStreamSink 43
+#define BLOG_CHANNEL_PRStreamSource 44
+#define BLOG_CHANNEL_BSocketPRFileDesc 45
+#define BLOG_CHANNEL_PacketProtoDecoder 46
+#define BLOG_CHANNEL_DPRelay 47
+#define BLOG_CHANNEL_BThreadWork 48
+#define BLOG_CHANNEL_DPReceive 49
+#define BLOG_NUM_CHANNELS 50

+ 1 - 0
generated/blog_channels_list.h

@@ -14,6 +14,7 @@
 {.name = "ncd_ip_in_network", .loglevel = 4},
 {.name = "ncd_run", .loglevel = 4},
 {.name = "ncd_net_backend_physical", .loglevel = 4},
+{.name = "ncd_net_backend_waitdevice", .loglevel = 4},
 {.name = "ncd_net_backend_badvpn", .loglevel = 4},
 {.name = "ncd_net_dns", .loglevel = 4},
 {.name = "ncd_net_iptables", .loglevel = 4},

+ 1 - 0
ncd/CMakeLists.txt

@@ -16,6 +16,7 @@ add_executable(badvpn-ncd
     modules/ip_in_network.c
     modules/run.c
     modules/net_backend_physical.c
+    modules/net_backend_waitdevice.c
     modules/net_backend_badvpn.c
     modules/net_dns.c
     modules/net_iptables.c

+ 2 - 0
ncd/modules/modules.h

@@ -38,6 +38,7 @@ extern const struct NCDModuleGroup ncdmodule_logical;
 extern const struct NCDModuleGroup ncdmodule_ip_in_network;
 extern const struct NCDModuleGroup ncdmodule_run;
 extern const struct NCDModuleGroup ncdmodule_net_backend_physical;
+extern const struct NCDModuleGroup ncdmodule_net_backend_waitdevice;
 extern const struct NCDModuleGroup ncdmodule_net_backend_badvpn;
 extern const struct NCDModuleGroup ncdmodule_net_dns;
 extern const struct NCDModuleGroup ncdmodule_net_iptables;
@@ -57,6 +58,7 @@ static const struct NCDModuleGroup *ncd_modules[] = {
     &ncdmodule_ip_in_network,
     &ncdmodule_run,
     &ncdmodule_net_backend_physical,
+    &ncdmodule_net_backend_waitdevice,
     &ncdmodule_net_backend_badvpn,
     &ncdmodule_net_dns,
     &ncdmodule_net_iptables,

+ 132 - 0
ncd/modules/net_backend_waitdevice.c

@@ -0,0 +1,132 @@
+/**
+ * @file net_backend_waitdevice.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
+ * 
+ * Module which waits for the presence of a network interface.
+ * 
+ * Synopsis: net.backend.waitdevice(string ifname)
+ */
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <ncd/NCDModule.h>
+#include <ncd/NCDIfConfig.h>
+#include <ncd/NCDInterfaceMonitor.h>
+
+#include <generated/blog_channel_ncd_net_backend_waitdevice.h>
+
+#define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
+
+struct instance {
+    NCDModuleInst *i;
+    const char *ifname;
+    NCDInterfaceMonitor monitor;
+    int up;
+};
+
+static void monitor_handler (struct instance *o, const char *ifname, int if_flags)
+{
+    if (strcmp(ifname, o->ifname)) {
+        return;
+    }
+    
+    int was_up = o->up;
+    o->up = !!(if_flags & NCDIFCONFIG_FLAG_EXISTS);
+    
+    if (o->up && !was_up) {
+        NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
+    }
+    else if (!o->up && was_up) {
+        NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_DOWN);
+    }
+}
+
+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;
+    if (!NCDValue_ListRead(i->args, 1, &arg)) {
+        ModuleLog(o->i, BLOG_ERROR, "wrong arity");
+        goto fail1;
+    }
+    if (NCDValue_Type(arg) != NCDVALUE_STRING) {
+        ModuleLog(o->i, BLOG_ERROR, "wrong type");
+        goto fail1;
+    }
+    o->ifname = NCDValue_StringValue(arg);
+    
+    // init monitor
+    if (!NCDInterfaceMonitor_Init(&o->monitor, o->i->reactor, (NCDInterfaceMonitor_handler)monitor_handler, o)) {
+        ModuleLog(o->i, BLOG_ERROR, "NCDInterfaceMonitor_Init failed");
+        goto fail1;
+    }
+    
+    // query initial state
+    o->up = !!(NCDIfConfig_query(o->ifname) & NCDIFCONFIG_FLAG_EXISTS);
+    
+    if (o->up) {
+        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 monitor
+    NCDInterfaceMonitor_Free(&o->monitor);
+    
+    // free instance
+    free(o);
+}
+
+static const struct NCDModule modules[] = {
+    {
+        .type = "net.backend.waitdevice",
+        .func_new = func_new,
+        .func_free = func_free
+    }, {
+        .type = NULL
+    }
+};
+
+const struct NCDModuleGroup ncdmodule_net_backend_waitdevice = {
+    .modules = modules
+};