NCDInterfaceModule.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * @file NCDInterfaceModule.c
  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. #include <ncd/NCDInterfaceModule.h>
  23. static void event_job_handler (NCDInterfaceModuleInst *n)
  24. {
  25. DebugObject_Access(&n->d_obj);
  26. n->handler_event(n->user, (n->up ? NCDINTERFACEMODULE_EVENT_UP : NCDINTERFACEMODULE_EVENT_DOWN));
  27. return;
  28. }
  29. static void finish_job_handler (NCDInterfaceModuleInst *n)
  30. {
  31. ASSERT(n->finishing)
  32. DebugObject_Access(&n->d_obj);
  33. n->m->func_finish(n->inst_user);
  34. return;
  35. }
  36. int NCDInterfaceModuleInst_Init (
  37. NCDInterfaceModuleInst *n, const struct NCDInterfaceModule *m, BReactor *reactor, BProcessManager *manager,
  38. struct NCDConfig_interfaces *conf, NCDInterfaceModule_handler_event handler_event,
  39. NCDInterfaceModule_handler_error handler_error,
  40. void *user
  41. )
  42. {
  43. // init arguments
  44. n->m = m;
  45. n->reactor = reactor;
  46. n->manager = manager;
  47. n->conf = conf;
  48. n->handler_event = handler_event;
  49. n->handler_error = handler_error;
  50. n->user = user;
  51. // init event job
  52. BPending_Init(&n->event_job, BReactor_PendingGroup(n->reactor), (BPending_handler)event_job_handler, n);
  53. // init finish job
  54. BPending_Init(&n->finish_job, BReactor_PendingGroup(n->reactor), (BPending_handler)finish_job_handler, n);
  55. // set not up
  56. n->up = 0;
  57. // set not finishing
  58. n->finishing = 0;
  59. // init backend
  60. if (!(n->inst_user = n->m->func_new(n))) {
  61. goto fail1;
  62. }
  63. DebugObject_Init(&n->d_obj);
  64. #ifndef NDEBUG
  65. DEAD_INIT(n->d_dead);
  66. #endif
  67. return 1;
  68. fail1:
  69. BPending_Free(&n->finish_job);
  70. BPending_Free(&n->event_job);
  71. return 0;
  72. }
  73. void NCDInterfaceModuleInst_Free (NCDInterfaceModuleInst *n)
  74. {
  75. DebugObject_Free(&n->d_obj);
  76. #ifndef NDEBUG
  77. DEAD_KILL(n->d_dead);
  78. #endif
  79. // free backend
  80. n->m->func_free(n->inst_user);
  81. // free finish job
  82. BPending_Free(&n->finish_job);
  83. // free event job
  84. BPending_Free(&n->event_job);
  85. }
  86. void NCDInterfaceModuleInst_Finish (NCDInterfaceModuleInst *n)
  87. {
  88. ASSERT(!n->finishing)
  89. DebugObject_Access(&n->d_obj);
  90. // set finishing
  91. n->finishing = 1;
  92. // set job
  93. BPending_Set(&n->finish_job);
  94. }
  95. void NCDInterfaceModuleInst_Backend_Event (NCDInterfaceModuleInst *n, int event)
  96. {
  97. ASSERT(event == NCDINTERFACEMODULE_EVENT_UP || event == NCDINTERFACEMODULE_EVENT_DOWN)
  98. ASSERT((event == NCDINTERFACEMODULE_EVENT_UP) == !n->up)
  99. ASSERT(!BPending_IsSet(&n->event_job))
  100. ASSERT(!n->finishing)
  101. // change up state
  102. n->up = !n->up;
  103. // set job
  104. BPending_Set(&n->event_job);
  105. }
  106. void NCDInterfaceModuleInst_Backend_Error (NCDInterfaceModuleInst *n)
  107. {
  108. #ifndef NDEBUG
  109. DEAD_ENTER(n->d_dead)
  110. #endif
  111. n->handler_error(n->user);
  112. #ifndef NDEBUG
  113. ASSERT(DEAD_KILLED)
  114. DEAD_LEAVE(n->d_dead);
  115. #endif
  116. }
  117. void NCDInterfaceModuleInst_Backend_Log (NCDInterfaceModuleInst *n, int level, const char *fmt, ...)
  118. {
  119. va_list vl;
  120. va_start(vl, fmt);
  121. BLog_Append("interface %s: module: ", n->conf->name);
  122. BLog_LogToChannelVarArg(BLOG_CURRENT_CHANNEL, level, fmt, vl);
  123. va_end(vl);
  124. }