net_backend_waitlink.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/extra/NCDIfConfig.h>
  39. #include <ncd/extra/NCDInterfaceMonitor.h>
  40. #include <ncd/module_common.h>
  41. #include <generated/blog_channel_ncd_net_backend_waitlink.h>
  42. struct instance {
  43. NCDModuleInst *i;
  44. NCDInterfaceMonitor monitor;
  45. int up;
  46. };
  47. static void instance_free (struct instance *o, int is_error);
  48. static void monitor_handler (struct instance *o, struct NCDInterfaceMonitor_event event)
  49. {
  50. ASSERT(event.event == NCDIFMONITOR_EVENT_LINK_UP || event.event == NCDIFMONITOR_EVENT_LINK_DOWN)
  51. int was_up = o->up;
  52. o->up = (event.event == NCDIFMONITOR_EVENT_LINK_UP);
  53. if (o->up && !was_up) {
  54. NCDModuleInst_Backend_Up(o->i);
  55. }
  56. else if (!o->up && was_up) {
  57. NCDModuleInst_Backend_Down(o->i);
  58. }
  59. }
  60. static void monitor_handler_error (struct instance *o)
  61. {
  62. ModuleLog(o->i, BLOG_ERROR, "monitor error");
  63. instance_free(o, 1);
  64. }
  65. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  66. {
  67. struct instance *o = vo;
  68. o->i = i;
  69. // check arguments
  70. NCDValRef ifname_arg;
  71. if (!NCDVal_ListRead(params->args, 1, &ifname_arg)) {
  72. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  73. goto fail0;
  74. }
  75. if (!NCDVal_IsStringNoNulls(ifname_arg)) {
  76. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  77. goto fail0;
  78. }
  79. // null terminate ifname
  80. NCDValNullTermString ifname_nts;
  81. if (!NCDVal_StringNullTerminate(ifname_arg, &ifname_nts)) {
  82. ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
  83. goto fail0;
  84. }
  85. // get interface index
  86. int ifindex;
  87. int res = badvpn_get_iface_info(ifname_nts.data, NULL, NULL, &ifindex);
  88. NCDValNullTermString_Free(&ifname_nts);
  89. if (!res) {
  90. ModuleLog(o->i, BLOG_ERROR, "failed to get interface index");
  91. goto fail0;
  92. }
  93. // init monitor
  94. if (!NCDInterfaceMonitor_Init(&o->monitor, ifindex, NCDIFMONITOR_WATCH_LINK, i->params->iparams->reactor, o, (NCDInterfaceMonitor_handler)monitor_handler, (NCDInterfaceMonitor_handler_error)monitor_handler_error)) {
  95. ModuleLog(o->i, BLOG_ERROR, "NCDInterfaceMonitor_Init failed");
  96. goto fail0;
  97. }
  98. // set not up
  99. o->up = 0;
  100. return;
  101. fail0:
  102. NCDModuleInst_Backend_DeadError(i);
  103. }
  104. static void instance_free (struct instance *o, int is_error)
  105. {
  106. // free monitor
  107. NCDInterfaceMonitor_Free(&o->monitor);
  108. if (is_error) {
  109. NCDModuleInst_Backend_DeadError(o->i);
  110. } else {
  111. NCDModuleInst_Backend_Dead(o->i);
  112. }
  113. }
  114. static void func_die (void *vo)
  115. {
  116. struct instance *o = vo;
  117. instance_free(o, 0);
  118. }
  119. static struct NCDModule modules[] = {
  120. {
  121. .type = "net.backend.waitlink",
  122. .func_new2 = func_new,
  123. .func_die = func_die,
  124. .alloc_size = sizeof(struct instance)
  125. }, {
  126. .type = NULL
  127. }
  128. };
  129. const struct NCDModuleGroup ncdmodule_net_backend_waitlink = {
  130. .modules = modules
  131. };