interface_physical.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * @file interface_physical.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. * @section DESCRIPTION
  23. *
  24. * Physical interface backend.
  25. */
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <ncd/NCDInterfaceModule.h>
  29. #include <ncd/NCDIfConfig.h>
  30. #include <ncd/NCDInterfaceMonitor.h>
  31. #define STATE_WAITDEVICE 1
  32. #define STATE_WAITLINK 2
  33. #define STATE_FINISHED 3
  34. struct instance {
  35. NCDInterfaceModuleInst *i;
  36. NCDInterfaceMonitor monitor;
  37. int state;
  38. };
  39. static int try_start (struct instance *o)
  40. {
  41. // query interface state
  42. int flags = NCDIfConfig_query(o->i->conf->name);
  43. if (!(flags&NCDIFCONFIG_FLAG_EXISTS)) {
  44. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_INFO, "device doesn't exist");
  45. // waiting for device
  46. o->state = STATE_WAITDEVICE;
  47. } else {
  48. if ((flags&NCDIFCONFIG_FLAG_UP)) {
  49. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_ERROR, "device already up - NOT configuring");
  50. return 0;
  51. }
  52. // set interface up
  53. if (!NCDIfConfig_set_up(o->i->conf->name)) {
  54. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_ERROR, "failed to set device up");
  55. return 0;
  56. }
  57. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_INFO, "waiting for link");
  58. // waiting for link
  59. o->state = STATE_WAITLINK;
  60. }
  61. return 1;
  62. }
  63. static void monitor_handler (struct instance *o, const char *ifname, int if_flags)
  64. {
  65. if (strcmp(ifname, o->i->conf->name)) {
  66. return;
  67. }
  68. if (!(if_flags&NCDIFCONFIG_FLAG_EXISTS)) {
  69. if (o->state > STATE_WAITDEVICE) {
  70. int prev_state = o->state;
  71. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_INFO, "device down");
  72. // set state
  73. o->state = STATE_WAITDEVICE;
  74. // report
  75. if (prev_state == STATE_FINISHED) {
  76. NCDInterfaceModuleInst_Backend_Event(o->i, NCDINTERFACEMODULE_EVENT_DOWN);
  77. }
  78. }
  79. } else {
  80. if (o->state == STATE_WAITDEVICE) {
  81. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_INFO, "device up");
  82. if (!try_start(o)) {
  83. NCDInterfaceModuleInst_Backend_Error(o->i);
  84. return;
  85. }
  86. return;
  87. }
  88. if ((if_flags&NCDIFCONFIG_FLAG_RUNNING)) {
  89. if (o->state == STATE_WAITLINK) {
  90. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_INFO, "link up");
  91. // set state
  92. o->state = STATE_FINISHED;
  93. // report
  94. NCDInterfaceModuleInst_Backend_Event(o->i, NCDINTERFACEMODULE_EVENT_UP);
  95. }
  96. } else {
  97. if (o->state == STATE_FINISHED) {
  98. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_INFO, "link down");
  99. // set state
  100. o->state = STATE_WAITLINK;
  101. // report
  102. NCDInterfaceModuleInst_Backend_Event(o->i, NCDINTERFACEMODULE_EVENT_DOWN);
  103. }
  104. }
  105. }
  106. }
  107. static void * func_new (NCDInterfaceModuleInst *i)
  108. {
  109. // allocate instance
  110. struct instance *o = malloc(sizeof(*o));
  111. if (!o) {
  112. NCDInterfaceModuleInst_Backend_Log(i, BLOG_ERROR, "failed to allocate instance");
  113. goto fail0;
  114. }
  115. // init arguments
  116. o->i = i;
  117. // init monitor
  118. if (!NCDInterfaceMonitor_Init(&o->monitor, o->i->reactor, (NCDInterfaceMonitor_handler)monitor_handler, o)) {
  119. NCDInterfaceModuleInst_Backend_Log(o->i, BLOG_ERROR, "NCDInterfaceMonitor_Init failed");
  120. goto fail1;
  121. }
  122. if (!try_start(o)) {
  123. goto fail2;
  124. }
  125. return o;
  126. fail2:
  127. NCDInterfaceMonitor_Free(&o->monitor);
  128. fail1:
  129. free(o);
  130. fail0:
  131. return NULL;
  132. }
  133. static void func_free (void *vo)
  134. {
  135. struct instance *o = vo;
  136. // set interface down
  137. if (o->state > STATE_WAITDEVICE) {
  138. NCDIfConfig_set_down(o->i->conf->name);
  139. }
  140. // free monitor
  141. NCDInterfaceMonitor_Free(&o->monitor);
  142. // free instance
  143. free(o);
  144. }
  145. static void func_finish (void *vo)
  146. {
  147. struct instance *o = vo;
  148. NCDInterfaceModuleInst_Backend_Error(o->i);
  149. return;
  150. }
  151. const struct NCDInterfaceModule ncd_interface_physical = {
  152. .type = "physical",
  153. .func_new = func_new,
  154. .func_free = func_free,
  155. .func_finish = func_finish
  156. };