net_backend_waitlink.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * @file net_backend_waitlink.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 link on a network interface.
  25. *
  26. * Synopsis: net.backend.waitlink(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_waitlink.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_RUNNING);
  48. if (o->up && !was_up) {
  49. NCDModuleInst_Backend_Up(o->i);
  50. }
  51. else if (!o->up && was_up) {
  52. NCDModuleInst_Backend_Down(o->i);
  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. NCDModuleInst_Backend_SetUser(i, o);
  64. // init arguments
  65. o->i = i;
  66. // check arguments
  67. NCDValue *arg;
  68. if (!NCDValue_ListRead(i->args, 1, &arg)) {
  69. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  70. goto fail1;
  71. }
  72. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  73. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  74. goto fail1;
  75. }
  76. o->ifname = NCDValue_StringValue(arg);
  77. // init monitor
  78. if (!NCDInterfaceMonitor_Init(&o->monitor, o->i->reactor, (NCDInterfaceMonitor_handler)monitor_handler, o)) {
  79. ModuleLog(o->i, BLOG_ERROR, "NCDInterfaceMonitor_Init failed");
  80. goto fail1;
  81. }
  82. // query initial state
  83. o->up = !!(NCDIfConfig_query(o->ifname) & NCDIFCONFIG_FLAG_RUNNING);
  84. // signal up if needed
  85. if (o->up) {
  86. NCDModuleInst_Backend_Up(o->i);
  87. }
  88. return;
  89. fail1:
  90. free(o);
  91. fail0:
  92. NCDModuleInst_Backend_SetError(i);
  93. NCDModuleInst_Backend_Dead(i);
  94. }
  95. static void func_die (void *vo)
  96. {
  97. struct instance *o = vo;
  98. NCDModuleInst *i = o->i;
  99. // free monitor
  100. NCDInterfaceMonitor_Free(&o->monitor);
  101. // free instance
  102. free(o);
  103. NCDModuleInst_Backend_Dead(i);
  104. }
  105. static const struct NCDModule modules[] = {
  106. {
  107. .type = "net.backend.waitlink",
  108. .func_new = func_new,
  109. .func_die = func_die
  110. }, {
  111. .type = NULL
  112. }
  113. };
  114. const struct NCDModuleGroup ncdmodule_net_backend_waitlink = {
  115. .modules = modules
  116. };