NCDInterfaceModule.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * @file NCDInterfaceModule.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #ifndef BADVPN_NCD_NCDINTERFACEMODULE_H
  23. #define BADVPN_NCD_NCDINTERFACEMODULE_H
  24. #include <stdarg.h>
  25. #include <system/BReactor.h>
  26. #include <system/BLog.h>
  27. #include <ncdconfig/NCDConfig.h>
  28. #include <generated/blog_channel_ncd.h>
  29. #define NCDINTERFACEMODULE_EVENT_UP 1
  30. #define NCDINTERFACEMODULE_EVENT_DOWN 2
  31. #define NCDINTERFACEMODULE_EVENT_ERROR 3
  32. typedef void (*NCDInterfaceModule_handler_event) (void *user, int event);
  33. struct NCDInterfaceModuleNCD {
  34. BReactor *reactor;
  35. struct NCDConfig_interfaces *conf;
  36. NCDInterfaceModule_handler_event handler_event;
  37. void *user;
  38. };
  39. static void NCDInterfaceModuleNCD_Event (const struct NCDInterfaceModuleNCD *n, int event);
  40. static void NCDInterfaceModuleNCD_Log (const struct NCDInterfaceModuleNCD *n, int level, const char *fmt, ...);
  41. typedef void * (*NCDInterfaceModule_func_new) (struct NCDInterfaceModuleNCD params, int *initial_up_state);
  42. typedef void (*NCDInterfaceModule_func_free) (void *o);
  43. struct NCDInterfaceModule {
  44. const char *type;
  45. NCDInterfaceModule_func_new func_new;
  46. NCDInterfaceModule_func_free func_free;
  47. };
  48. static void * NCDInterfaceModule_New (const struct NCDInterfaceModule *m, struct NCDInterfaceModuleNCD params, int *initial_up_state);
  49. static void NCDInterfaceModule_Free (const struct NCDInterfaceModule *m, void *o);
  50. void NCDInterfaceModuleNCD_Event (const struct NCDInterfaceModuleNCD *n, int event)
  51. {
  52. ASSERT(event == NCDINTERFACEMODULE_EVENT_UP || event == NCDINTERFACEMODULE_EVENT_DOWN ||
  53. event == NCDINTERFACEMODULE_EVENT_ERROR)
  54. n->handler_event(n->user, event);
  55. return;
  56. }
  57. void NCDInterfaceModuleNCD_Log (const struct NCDInterfaceModuleNCD *n, int level, const char *fmt, ...)
  58. {
  59. va_list vl;
  60. va_start(vl, fmt);
  61. BLog_Append("interface %s: module: ", n->conf->name);
  62. BLog_LogToChannelVarArg(BLOG_CURRENT_CHANNEL, level, fmt, vl);
  63. va_end(vl);
  64. }
  65. void * NCDInterfaceModule_New (const struct NCDInterfaceModule *m, struct NCDInterfaceModuleNCD params, int *initial_up_state)
  66. {
  67. return m->func_new(params, initial_up_state);
  68. }
  69. void NCDInterfaceModule_Free (const struct NCDInterfaceModule *m, void *o)
  70. {
  71. m->func_free(o);
  72. }
  73. #endif