net_ipv4_arp_probe.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * @file net_ipv4_arp_probe.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. * ARP probing module.
  25. *
  26. * Synopsis:
  27. * net.ipv4.arp_probe(string ifname, string addr)
  28. *
  29. * Description:
  30. * Monitors local presence of an IPv4 host on a network interface.
  31. * On initialization, may take some time to determine whether
  32. * the host is present or not, then goes to UP state. When it
  33. * determines that presence has changed, toggles itself DOWN then
  34. * UP to expose the new determination.
  35. *
  36. * Variables:
  37. * exists - "true" if the host exists, "false" if not
  38. */
  39. #include <stdlib.h>
  40. #include <misc/ipaddr.h>
  41. #include <arpprobe/BArpProbe.h>
  42. #include <ncd/NCDModule.h>
  43. #include <generated/blog_channel_ncd_net_ipv4_arp_probe.h>
  44. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  45. #define STATE_UNKNOWN 1
  46. #define STATE_EXIST 2
  47. #define STATE_NOEXIST 3
  48. struct instance {
  49. NCDModuleInst *i;
  50. BArpProbe arpprobe;
  51. int state;
  52. };
  53. static void instance_free (struct instance *o);
  54. static void arpprobe_handler (struct instance *o, int event)
  55. {
  56. switch (event) {
  57. case BARPPROBE_EVENT_EXIST: {
  58. ASSERT(o->state == STATE_UNKNOWN || o->state == STATE_NOEXIST)
  59. ModuleLog(o->i, BLOG_INFO, "exist");
  60. if (o->state == STATE_NOEXIST) {
  61. // signal down
  62. NCDModuleInst_Backend_Down(o->i);
  63. }
  64. // signal up
  65. NCDModuleInst_Backend_Up(o->i);
  66. // set state exist
  67. o->state = STATE_EXIST;
  68. } break;
  69. case BARPPROBE_EVENT_NOEXIST: {
  70. ASSERT(o->state == STATE_UNKNOWN || o->state == STATE_EXIST)
  71. ModuleLog(o->i, BLOG_INFO, "noexist");
  72. if (o->state == STATE_EXIST) {
  73. // signal down
  74. NCDModuleInst_Backend_Down(o->i);
  75. }
  76. // signal up
  77. NCDModuleInst_Backend_Up(o->i);
  78. // set state noexist
  79. o->state = STATE_NOEXIST;
  80. } break;
  81. case BARPPROBE_EVENT_ERROR: {
  82. ModuleLog(o->i, BLOG_ERROR, "error");
  83. // set error
  84. NCDModuleInst_Backend_SetError(o->i);
  85. // die
  86. instance_free(o);
  87. return;
  88. } break;
  89. default: ASSERT(0);
  90. }
  91. }
  92. static void func_new (NCDModuleInst *i)
  93. {
  94. // allocate instance
  95. struct instance *o = malloc(sizeof(*o));
  96. if (!o) {
  97. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  98. goto fail0;
  99. }
  100. NCDModuleInst_Backend_SetUser(i, o);
  101. // init arguments
  102. o->i = i;
  103. // read arguments
  104. NCDValue *arg_ifname;
  105. NCDValue *arg_addr;
  106. if (!NCDValue_ListRead(i->args, 2, &arg_ifname, &arg_addr)) {
  107. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  108. goto fail1;
  109. }
  110. if (NCDValue_Type(arg_ifname) != NCDVALUE_STRING || NCDValue_Type(arg_addr) != NCDVALUE_STRING) {
  111. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  112. goto fail1;
  113. }
  114. char *ifname = NCDValue_StringValue(arg_ifname);
  115. char *addr_str = NCDValue_StringValue(arg_addr);
  116. // parse address
  117. uint32_t addr;
  118. if (!ipaddr_parse_ipv4_addr(addr_str, &addr)) {
  119. ModuleLog(o->i, BLOG_ERROR, "wrong address");
  120. goto fail1;
  121. }
  122. // init arpprobe
  123. if (!BArpProbe_Init(&o->arpprobe, ifname, addr, i->reactor, o, (BArpProbe_handler)arpprobe_handler)) {
  124. ModuleLog(o->i, BLOG_ERROR, "BArpProbe_Init failed");
  125. goto fail1;
  126. }
  127. // set state unknown
  128. o->state = STATE_UNKNOWN;
  129. return;
  130. fail1:
  131. free(o);
  132. fail0:
  133. NCDModuleInst_Backend_SetError(i);
  134. NCDModuleInst_Backend_Dead(i);
  135. }
  136. static void instance_free (struct instance *o)
  137. {
  138. NCDModuleInst *i = o->i;
  139. // free arpprobe
  140. BArpProbe_Free(&o->arpprobe);
  141. // free instance
  142. free(o);
  143. NCDModuleInst_Backend_Dead(i);
  144. }
  145. static void func_die (void *vo)
  146. {
  147. struct instance *o = vo;
  148. instance_free(o);
  149. }
  150. static int func_getvar (void *vo, const char *name, NCDValue *out)
  151. {
  152. struct instance *o = vo;
  153. ASSERT(o->state == STATE_EXIST || o->state == STATE_NOEXIST)
  154. if (!strcmp(name, "exists")) {
  155. const char *str = (o->state == STATE_EXIST ? "true" : "false");
  156. if (!NCDValue_InitString(out, str)) {
  157. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  158. return 0;
  159. }
  160. return 1;
  161. }
  162. return 0;
  163. }
  164. static const struct NCDModule modules[] = {
  165. {
  166. .type = "net.ipv4.arp_probe",
  167. .func_new = func_new,
  168. .func_die = func_die,
  169. .func_getvar = func_getvar
  170. }, {
  171. .type = NULL
  172. }
  173. };
  174. const struct NCDModuleGroup ncdmodule_net_ipv4_arp_probe = {
  175. .modules = modules
  176. };