فهرست منبع

ncd: add some convenience functions for interface modules

ambrop7 15 سال پیش
والد
کامیت
d40051f9c1

+ 0 - 1
blog_channels.txt

@@ -19,4 +19,3 @@ BDHCPClient 4
 NCDIfConfig 4
 BUnixSignal 4
 BProcess 4
-ncd_interface_physical 4

+ 0 - 4
generated/blog_channel_ncd_interface_physical.h

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

+ 1 - 2
generated/blog_channels_defines.h

@@ -19,5 +19,4 @@
 #define BLOG_CHANNEL_NCDIfConfig 18
 #define BLOG_CHANNEL_BUnixSignal 19
 #define BLOG_CHANNEL_BProcess 20
-#define BLOG_CHANNEL_ncd_interface_physical 21
-#define BLOG_NUM_CHANNELS 22
+#define BLOG_NUM_CHANNELS 21

+ 0 - 1
generated/blog_channels_list.h

@@ -19,4 +19,3 @@
 {.name = "NCDIfConfig", .loglevel = 4},
 {.name = "BUnixSignal", .loglevel = 4},
 {.name = "BProcess", .loglevel = 4},
-{.name = "ncd_interface_physical", .loglevel = 4},

+ 41 - 2
ncd/NCDInterfaceModule.h

@@ -23,23 +23,31 @@
 #ifndef BADVPN_NCD_NCDINTERFACEMODULE_H
 #define BADVPN_NCD_NCDINTERFACEMODULE_H
 
+#include <stdarg.h>
+
 #include <system/BReactor.h>
+#include <system/BLog.h>
 #include <ncdconfig/NCDConfig.h>
 
+#include <generated/blog_channel_ncd.h>
+
 #define NCDINTERFACEMODULE_EVENT_UP 1
 #define NCDINTERFACEMODULE_EVENT_DOWN 2
 #define NCDINTERFACEMODULE_EVENT_ERROR 3
 
 typedef void (*NCDInterfaceModule_handler_event) (void *user, int event);
 
-struct NCDInterfaceModule_ncd_params {
+struct NCDInterfaceModuleNCD {
     BReactor *reactor;
     struct NCDConfig_interfaces *conf;
     NCDInterfaceModule_handler_event handler_event;
     void *user;
 };
 
-typedef void * (*NCDInterfaceModule_func_new) (struct NCDInterfaceModule_ncd_params params, int *initial_up_state);
+static void NCDInterfaceModuleNCD_Event (const struct NCDInterfaceModuleNCD *n, int event);
+static void NCDInterfaceModuleNCD_Log (const struct NCDInterfaceModuleNCD *n, int level, const char *fmt, ...);
+
+typedef void * (*NCDInterfaceModule_func_new) (struct NCDInterfaceModuleNCD params, int *initial_up_state);
 typedef void (*NCDInterfaceModule_func_free) (void *o);
 
 struct NCDInterfaceModule {
@@ -48,4 +56,35 @@ struct NCDInterfaceModule {
     NCDInterfaceModule_func_free func_free;
 };
 
+static void * NCDInterfaceModule_New (const struct NCDInterfaceModule *m, struct NCDInterfaceModuleNCD params, int *initial_up_state);
+static void NCDInterfaceModule_Free (const struct NCDInterfaceModule *m, void *o);
+
+void NCDInterfaceModuleNCD_Event (const struct NCDInterfaceModuleNCD *n, int event)
+{
+    ASSERT(event == NCDINTERFACEMODULE_EVENT_UP || event == NCDINTERFACEMODULE_EVENT_DOWN ||
+           event == NCDINTERFACEMODULE_EVENT_ERROR)
+    
+    n->handler_event(n->user, event);
+    return;
+}
+
+void NCDInterfaceModuleNCD_Log (const struct NCDInterfaceModuleNCD *n, int level, const char *fmt, ...)
+{
+    va_list vl;
+    va_start(vl, fmt);
+    BLog_Append("interface %s: module: ", n->conf->name);
+    BLog_LogToChannelVarArg(BLOG_CURRENT_CHANNEL, level, fmt, vl);
+    va_end(vl);
+}
+
+void * NCDInterfaceModule_New (const struct NCDInterfaceModule *m, struct NCDInterfaceModuleNCD params, int *initial_up_state)
+{
+    return m->func_new(params, initial_up_state);
+}
+
+void NCDInterfaceModule_Free (const struct NCDInterfaceModule *m, void *o)
+{
+    m->func_free(o);
+}
+
 #endif

+ 16 - 34
ncd/interface_modules/interface_physical.c

@@ -25,19 +25,16 @@
 
 #include <misc/dead.h>
 #include <system/DebugObject.h>
-#include <system/BLog.h>
 #include <ncd/NCDInterfaceModule.h>
 #include <ncd/NCDIfConfig.h>
 #include <ncd/NCDInterfaceMonitor.h>
 
-#include <generated/blog_channel_ncd_interface_physical.h>
-
 #define STATE_WAITDEVICE 1
 #define STATE_WAITLINK 2
 #define STATE_FINISHED 3
 
 struct instance {
-    struct NCDInterfaceModule_ncd_params params;
+    struct NCDInterfaceModuleNCD params;
     NCDInterfaceMonitor monitor;
     int state;
     DebugObject d_obj;
@@ -46,28 +43,13 @@ struct instance {
     #endif
 };
 
-static void instance_log (struct instance *o, int level, const char *fmt, ...)
-{
-    va_list vl;
-    va_start(vl, fmt);
-    BLog_Append("interface %s: ", o->params.conf->name);
-    BLog_LogToChannelVarArg(BLOG_CURRENT_CHANNEL, level, fmt, vl);
-    va_end(vl);
-}
-
-static void report_event (struct instance *o, int event)
-{
-    o->params.handler_event(o->params.user, event);
-    return;
-}
-
 static void report_error (struct instance *o)
 {
     #ifndef NDEBUG
     DEAD_ENTER(o->d_dead)
     #endif
     
-    report_event(o, NCDINTERFACEMODULE_EVENT_ERROR);
+    NCDInterfaceModuleNCD_Event(&o->params, NCDINTERFACEMODULE_EVENT_ERROR);
     
     #ifndef NDEBUG
     ASSERT(DEAD_KILLED)
@@ -81,23 +63,23 @@ static int try_start (struct instance *o)
     int flags = NCDIfConfig_query(o->params.conf->name);
     
     if (!(flags&NCDIFCONFIG_FLAG_EXISTS)) {
-        instance_log(o, BLOG_INFO, "device doesn't exist");
+        NCDInterfaceModuleNCD_Log(&o->params, BLOG_INFO, "device doesn't exist");
         
         // waiting for device
         o->state = STATE_WAITDEVICE;
     } else {
         if ((flags&NCDIFCONFIG_FLAG_UP)) {
-            instance_log(o, BLOG_ERROR, "device already up - NOT configuring");
+            NCDInterfaceModuleNCD_Log(&o->params, BLOG_ERROR, "device already up - NOT configuring");
             return 0;
         }
         
         // set interface up
         if (!NCDIfConfig_set_up(o->params.conf->name)) {
-            instance_log(o, BLOG_ERROR, "failed to set device up");
+            NCDInterfaceModuleNCD_Log(&o->params, BLOG_ERROR, "failed to set device up");
             return 0;
         }
         
-        instance_log(o, BLOG_INFO, "waiting for link");
+        NCDInterfaceModuleNCD_Log(&o->params, BLOG_INFO, "waiting for link");
         
         // waiting for link
         o->state = STATE_WAITLINK;
@@ -118,20 +100,20 @@ static void monitor_handler (struct instance *o, const char *ifname, int if_flag
         if (o->state > STATE_WAITDEVICE) {
             int prev_state = o->state;
             
-            instance_log(o, BLOG_INFO, "device down");
+            NCDInterfaceModuleNCD_Log(&o->params, BLOG_INFO, "device down");
             
             // set state
             o->state = STATE_WAITDEVICE;
             
             // report
             if (prev_state == STATE_FINISHED) {
-                report_event(o, NCDINTERFACEMODULE_EVENT_DOWN);
+                NCDInterfaceModuleNCD_Event(&o->params, NCDINTERFACEMODULE_EVENT_DOWN);
                 return;
             }
         }
     } else {
         if (o->state == STATE_WAITDEVICE) {
-            instance_log(o, BLOG_INFO, "device up");
+            NCDInterfaceModuleNCD_Log(&o->params, BLOG_INFO, "device up");
             
             if (!try_start(o)) {
                 report_error(o);
@@ -143,36 +125,36 @@ static void monitor_handler (struct instance *o, const char *ifname, int if_flag
         
         if ((if_flags&NCDIFCONFIG_FLAG_RUNNING)) {
             if (o->state == STATE_WAITLINK) {
-                instance_log(o, BLOG_INFO, "link up");
+                NCDInterfaceModuleNCD_Log(&o->params, BLOG_INFO, "link up");
                 
                 // set state
                 o->state = STATE_FINISHED;
                 
                 // report
-                report_event(o, NCDINTERFACEMODULE_EVENT_UP);
+                NCDInterfaceModuleNCD_Event(&o->params, NCDINTERFACEMODULE_EVENT_UP);
                 return;
             }
         } else {
             if (o->state == STATE_FINISHED) {
-                instance_log(o, BLOG_INFO, "link down");
+                NCDInterfaceModuleNCD_Log(&o->params, BLOG_INFO, "link down");
                 
                 // set state
                 o->state = STATE_WAITLINK;
                 
                 // report
-                report_event(o, NCDINTERFACEMODULE_EVENT_DOWN);
+                NCDInterfaceModuleNCD_Event(&o->params, NCDINTERFACEMODULE_EVENT_DOWN);
                 return;
             }
         }
     }
 }
 
-static void * func_new (struct NCDInterfaceModule_ncd_params params, int *initial_up_state)
+static void * func_new (struct NCDInterfaceModuleNCD params, int *initial_up_state)
 {
     // allocate instance
     struct instance *o = malloc(sizeof(*o));
     if (!o) {
-        BLog(BLOG_ERROR, "failed to allocate instance");
+        NCDInterfaceModuleNCD_Log(&params, BLOG_ERROR, "failed to allocate instance");
         goto fail0;
     }
     
@@ -181,7 +163,7 @@ static void * func_new (struct NCDInterfaceModule_ncd_params params, int *initia
     
     // init monitor
     if (!NCDInterfaceMonitor_Init(&o->monitor, o->params.reactor, (NCDInterfaceMonitor_handler)monitor_handler, o)) {
-        instance_log(o, BLOG_ERROR, "NCDInterfaceMonitor_Init failed");
+        NCDInterfaceModuleNCD_Log(&o->params, BLOG_ERROR, "NCDInterfaceMonitor_Init failed");
         goto fail1;
     }
     

+ 3 - 5
ncd/ncd.c

@@ -873,15 +873,14 @@ int interface_module_init (struct interface *iface, int *initial_up_state)
 {
     ASSERT(!iface->module_instance)
     
-    struct NCDInterfaceModule_ncd_params params = { 
+    struct NCDInterfaceModuleNCD params = { 
         .reactor = &ss,
         .conf = iface->conf, 
         .handler_event = (NCDInterfaceModule_handler_event)interface_module_handler_event, 
         .user = iface
     };
     
-    // call module function new
-    if (!(iface->module_instance = iface->module->func_new(params, initial_up_state))) {
+    if (!(iface->module_instance = NCDInterfaceModule_New(iface->module, params, initial_up_state))) {
         interface_log(iface, BLOG_ERROR, "failed to inizialice module instance");
         return 0;
     }
@@ -895,8 +894,7 @@ void interface_module_free (struct interface *iface)
 {
     ASSERT(iface->module_instance)
     
-    // call module function free
-    iface->module->func_free(iface->module_instance);
+    NCDInterfaceModule_Free(iface->module, iface->module_instance);
     
     iface->module_instance = NULL;
 }