net_backend_waitdevice.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * @file net_backend_waitdevice.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. * Module which waits for the presence of a network interface.
  25. *
  26. * Synopsis: net.backend.waitdevice(string ifname)
  27. */
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <ncd/NCDModule.h>
  31. #include <ncd/NCDIfConfig.h>
  32. #include <ncd/NCDInterfaceMonitor.h>
  33. #include <generated/blog_channel_ncd_net_backend_waitdevice.h>
  34. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  35. struct instance {
  36. NCDModuleInst *i;
  37. const char *ifname;
  38. NCDInterfaceMonitor monitor;
  39. int up;
  40. };
  41. static void monitor_handler (struct instance *o, const char *ifname, int if_flags)
  42. {
  43. if (strcmp(ifname, o->ifname)) {
  44. return;
  45. }
  46. int was_up = o->up;
  47. o->up = !!(if_flags & NCDIFCONFIG_FLAG_EXISTS);
  48. if (o->up && !was_up) {
  49. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  50. }
  51. else if (!o->up && was_up) {
  52. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_DOWN);
  53. }
  54. }
  55. static void * func_new (NCDModuleInst *i)
  56. {
  57. // allocate instance
  58. struct instance *o = malloc(sizeof(*o));
  59. if (!o) {
  60. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  61. goto fail0;
  62. }
  63. // init arguments
  64. o->i = i;
  65. // check arguments
  66. NCDValue *arg;
  67. if (!NCDValue_ListRead(i->args, 1, &arg)) {
  68. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  69. goto fail1;
  70. }
  71. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  72. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  73. goto fail1;
  74. }
  75. o->ifname = NCDValue_StringValue(arg);
  76. // init monitor
  77. if (!NCDInterfaceMonitor_Init(&o->monitor, o->i->reactor, (NCDInterfaceMonitor_handler)monitor_handler, o)) {
  78. ModuleLog(o->i, BLOG_ERROR, "NCDInterfaceMonitor_Init failed");
  79. goto fail1;
  80. }
  81. // query initial state
  82. o->up = !!(NCDIfConfig_query(o->ifname) & NCDIFCONFIG_FLAG_EXISTS);
  83. if (o->up) {
  84. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  85. }
  86. return o;
  87. fail1:
  88. free(o);
  89. fail0:
  90. return NULL;
  91. }
  92. static void func_free (void *vo)
  93. {
  94. struct instance *o = vo;
  95. // free monitor
  96. NCDInterfaceMonitor_Free(&o->monitor);
  97. // free instance
  98. free(o);
  99. }
  100. static const struct NCDModule modules[] = {
  101. {
  102. .type = "net.backend.waitdevice",
  103. .func_new = func_new,
  104. .func_free = func_free
  105. }, {
  106. .type = NULL
  107. }
  108. };
  109. const struct NCDModuleGroup ncdmodule_net_backend_waitdevice = {
  110. .modules = modules
  111. };