net_watch_interfaces.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**
  2. * @file net_watch_interfaces.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. * Network interface watcher.
  25. *
  26. * Synopsis: net.watch_interfaces()
  27. * Description: reports network interface events. Transitions up when an event is detected, and
  28. * goes down waiting for the next event when net.watch_interfaces::nextevent() is called.
  29. * On startup, "added" events are reported for existing interfaces.
  30. * Variables:
  31. * string event_type - what happened with the interface: "added" or "removed". This may not be
  32. * consistent across events.
  33. * string devname - interface name
  34. *
  35. * Synopsis: net.watch_interfaces::nextevent()
  36. * Description: makes the watch_interfaces module transition down in order to report the next event.
  37. */
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <stdio.h>
  41. #include <ctype.h>
  42. #include <ncd/NCDInterfaceMonitor.h>
  43. #include <ncd/NCDModule.h>
  44. #include <generated/blog_channel_ncd_net_watch_interfaces.h>
  45. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  46. struct instance {
  47. NCDModuleInst *i;
  48. NCDInterfaceMonitor monitor;
  49. FILE *net_file;
  50. char line_buf[256];
  51. int processing;
  52. const char *processing_type;
  53. char processing_name[256];
  54. };
  55. struct nextevent_instance {
  56. NCDModuleInst *i;
  57. };
  58. static void next_file_event (struct instance *o)
  59. {
  60. ASSERT(!o->processing)
  61. ASSERT(o->net_file)
  62. char *name;
  63. while (1) {
  64. if (!fgets(o->line_buf, sizeof(o->line_buf), o->net_file)) {
  65. // close file
  66. fclose(o->net_file);
  67. // set no net file
  68. o->net_file = NULL;
  69. // start processing monitor events
  70. NCDInterfaceMonitor_Continue(&o->monitor);
  71. return;
  72. }
  73. // parse line to get interface name
  74. char *c = o->line_buf;
  75. while (*c && isspace(*c)) {
  76. c++;
  77. }
  78. name = c;
  79. while (*c && *c != ':') {
  80. c++;
  81. }
  82. if (*c != ':') {
  83. continue;
  84. }
  85. *c = '\0';
  86. break;
  87. }
  88. // set event
  89. o->processing_type = "added";
  90. snprintf(o->processing_name, sizeof(o->processing_name), "%s", name);
  91. o->processing = 1;
  92. // signal up
  93. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  94. }
  95. static void next_event (struct instance *o)
  96. {
  97. ASSERT(o->processing)
  98. // set not processing
  99. o->processing = 0;
  100. // signal down
  101. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_DOWN);
  102. if (o->net_file) {
  103. next_file_event(o);
  104. return;
  105. } else {
  106. // continue processing monitor events
  107. NCDInterfaceMonitor_Continue(&o->monitor);
  108. }
  109. }
  110. static void monitor_handler (struct instance *o, const char *ifname, int if_flags)
  111. {
  112. ASSERT(!o->processing)
  113. ASSERT(!o->net_file)
  114. // pause monitor
  115. NCDInterfaceMonitor_Pause(&o->monitor);
  116. // set event
  117. o->processing_type = ((if_flags & NCDIFCONFIG_FLAG_EXISTS) ? "added" : "removed");
  118. snprintf(o->processing_name, sizeof(o->processing_name), "%s", ifname);
  119. o->processing = 1;
  120. // signal up
  121. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  122. }
  123. static void func_new (NCDModuleInst *i)
  124. {
  125. // allocate instance
  126. struct instance *o = malloc(sizeof(*o));
  127. if (!o) {
  128. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  129. goto fail0;
  130. }
  131. NCDModuleInst_Backend_SetUser(i, o);
  132. // init arguments
  133. o->i = i;
  134. // check arguments
  135. if (!NCDValue_ListRead(o->i->args, 0)) {
  136. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  137. goto fail1;
  138. }
  139. // init monitor
  140. if (!NCDInterfaceMonitor_Init(&o->monitor, o->i->reactor, (NCDInterfaceMonitor_handler)monitor_handler, o)) {
  141. ModuleLog(o->i, BLOG_ERROR, "NCDInterfaceMonitor_Init failed");
  142. goto fail1;
  143. }
  144. NCDInterfaceMonitor_Pause(&o->monitor);
  145. // open /proc/net/dev
  146. if (!(o->net_file = fopen("/proc/net/dev", "r"))) {
  147. ModuleLog(o->i, BLOG_ERROR, "fopen(/proc/net/dev) failed");
  148. goto fail2;
  149. }
  150. // set not processing
  151. o->processing = 0;
  152. next_file_event(o);
  153. return;
  154. fail2:
  155. NCDInterfaceMonitor_Free(&o->monitor);
  156. fail1:
  157. free(o);
  158. fail0:
  159. NCDModuleInst_Backend_SetError(i);
  160. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  161. }
  162. static void func_die (void *vo)
  163. {
  164. struct instance *o = vo;
  165. NCDModuleInst *i = o->i;
  166. // close /proc/net/dev
  167. if (o->net_file) {
  168. fclose(o->net_file);
  169. }
  170. // free monitor
  171. NCDInterfaceMonitor_Free(&o->monitor);
  172. // free instance
  173. free(o);
  174. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  175. }
  176. static int func_getvar (void *vo, const char *name, NCDValue *out)
  177. {
  178. struct instance *o = vo;
  179. ASSERT(o->processing)
  180. if (!strcmp(name, "event_type")) {
  181. if (!NCDValue_InitString(out, o->processing_type)) {
  182. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  183. return 0;
  184. }
  185. return 1;
  186. }
  187. if (!strcmp(name, "devname")) {
  188. if (!NCDValue_InitString(out, o->processing_name)) {
  189. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  190. return 0;
  191. }
  192. return 1;
  193. }
  194. return 0;
  195. }
  196. static void nextevent_func_new (NCDModuleInst *i)
  197. {
  198. // allocate instance
  199. struct nextevent_instance *o = malloc(sizeof(*o));
  200. if (!o) {
  201. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  202. goto fail0;
  203. }
  204. NCDModuleInst_Backend_SetUser(i, o);
  205. // init arguments
  206. o->i = i;
  207. // check arguments
  208. if (!NCDValue_ListRead(o->i->args, 0)) {
  209. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  210. goto fail1;
  211. }
  212. // get method object
  213. struct instance *mo = i->method_object->inst_user;
  214. ASSERT(mo->processing)
  215. // signal up.
  216. // Do it before finishing the event so our process does not advance any further if
  217. // we would be killed the event provider going down.
  218. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  219. // wait for next event
  220. next_event(mo);
  221. return;
  222. fail1:
  223. free(o);
  224. fail0:
  225. NCDModuleInst_Backend_SetError(i);
  226. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  227. }
  228. static void nextevent_func_die (void *vo)
  229. {
  230. struct nextevent_instance *o = vo;
  231. NCDModuleInst *i = o->i;
  232. // free instance
  233. free(o);
  234. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  235. }
  236. static const struct NCDModule modules[] = {
  237. {
  238. .type = "net.watch_interfaces",
  239. .func_new = func_new,
  240. .func_die = func_die,
  241. .func_getvar = func_getvar
  242. }, {
  243. .type = "net.watch_interfaces::nextevent",
  244. .func_new = nextevent_func_new,
  245. .func_die = nextevent_func_die
  246. }, {
  247. .type = NULL
  248. }
  249. };
  250. const struct NCDModuleGroup ncdmodule_net_watch_interfaces = {
  251. .modules = modules
  252. };