net_backend_waitlink.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * @file net_backend_waitlink.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Module which waits for the link on a network interface.
  32. *
  33. * Synopsis: net.backend.waitlink(string ifname)
  34. */
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <misc/get_iface_info.h>
  38. #include <ncd/NCDModule.h>
  39. #include <ncd/extra/NCDIfConfig.h>
  40. #include <ncd/extra/NCDInterfaceMonitor.h>
  41. #include <generated/blog_channel_ncd_net_backend_waitlink.h>
  42. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  43. struct instance {
  44. NCDModuleInst *i;
  45. NCDInterfaceMonitor monitor;
  46. int up;
  47. };
  48. static void instance_free (struct instance *o, int is_error);
  49. static void monitor_handler (struct instance *o, struct NCDInterfaceMonitor_event event)
  50. {
  51. ASSERT(event.event == NCDIFMONITOR_EVENT_LINK_UP || event.event == NCDIFMONITOR_EVENT_LINK_DOWN)
  52. int was_up = o->up;
  53. o->up = (event.event == NCDIFMONITOR_EVENT_LINK_UP);
  54. if (o->up && !was_up) {
  55. NCDModuleInst_Backend_Up(o->i);
  56. }
  57. else if (!o->up && was_up) {
  58. NCDModuleInst_Backend_Down(o->i);
  59. }
  60. }
  61. static void monitor_handler_error (struct instance *o)
  62. {
  63. ModuleLog(o->i, BLOG_ERROR, "monitor error");
  64. instance_free(o, 1);
  65. }
  66. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  67. {
  68. struct instance *o = vo;
  69. o->i = i;
  70. // check arguments
  71. NCDValRef ifname_arg;
  72. if (!NCDVal_ListRead(params->args, 1, &ifname_arg)) {
  73. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  74. goto fail0;
  75. }
  76. if (!NCDVal_IsStringNoNulls(ifname_arg)) {
  77. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  78. goto fail0;
  79. }
  80. // null terminate ifname
  81. NCDValNullTermString ifname_nts;
  82. if (!NCDVal_StringNullTerminate(ifname_arg, &ifname_nts)) {
  83. ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
  84. goto fail0;
  85. }
  86. // get interface index
  87. int ifindex;
  88. int res = badvpn_get_iface_info(ifname_nts.data, NULL, NULL, &ifindex);
  89. NCDValNullTermString_Free(&ifname_nts);
  90. if (!res) {
  91. ModuleLog(o->i, BLOG_ERROR, "failed to get interface index");
  92. goto fail0;
  93. }
  94. // init monitor
  95. if (!NCDInterfaceMonitor_Init(&o->monitor, ifindex, NCDIFMONITOR_WATCH_LINK, i->params->iparams->reactor, o, (NCDInterfaceMonitor_handler)monitor_handler, (NCDInterfaceMonitor_handler_error)monitor_handler_error)) {
  96. ModuleLog(o->i, BLOG_ERROR, "NCDInterfaceMonitor_Init failed");
  97. goto fail0;
  98. }
  99. // set not up
  100. o->up = 0;
  101. return;
  102. fail0:
  103. NCDModuleInst_Backend_DeadError(i);
  104. }
  105. static void instance_free (struct instance *o, int is_error)
  106. {
  107. // free monitor
  108. NCDInterfaceMonitor_Free(&o->monitor);
  109. if (is_error) {
  110. NCDModuleInst_Backend_DeadError(o->i);
  111. } else {
  112. NCDModuleInst_Backend_Dead(o->i);
  113. }
  114. }
  115. static void func_die (void *vo)
  116. {
  117. struct instance *o = vo;
  118. instance_free(o, 0);
  119. }
  120. static struct NCDModule modules[] = {
  121. {
  122. .type = "net.backend.waitlink",
  123. .func_new2 = func_new,
  124. .func_die = func_die,
  125. .alloc_size = sizeof(struct instance)
  126. }, {
  127. .type = NULL
  128. }
  129. };
  130. const struct NCDModuleGroup ncdmodule_net_backend_waitlink = {
  131. .modules = modules
  132. };