net_ipv4_addr_in_network.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * @file net_ipv4_addr_in_network.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. * Synopsis:
  32. * net.ipv4.addr_in_network(string addr, string net_addr, string net_prefix)
  33. * net.ipv4.addr_in_network(string addr, string cidr_net_addr)
  34. * net.ipv4.ifnot_addr_in_network(string addr, string net_addr, string net_prefix)
  35. * net.ipv4.ifnot_addr_in_network(string addr, string cidr_net_addr)
  36. *
  37. * Description:
  38. * Checks if two IPv4 addresses belong to the same subnet.
  39. * The prefix length is given either in the a separate argument or along with
  40. * the second address in CIDR notation (address/prefix).
  41. * This can be used to check whether an address belongs to a certain
  42. * subnet, hence the name.
  43. *
  44. * Variables:
  45. * (empty) - "true" if addresses belong to the same subnet, "false" if not
  46. */
  47. #include <string.h>
  48. #include <misc/ipaddr.h>
  49. #include <ncd/module_common.h>
  50. #include <generated/blog_channel_ncd_net_ipv4_addr_in_network.h>
  51. struct instance {
  52. NCDModuleInst *i;
  53. int value;
  54. };
  55. static void func_new_common (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, int is_ifnot)
  56. {
  57. struct instance *o = vo;
  58. o->i = i;
  59. // read arguments
  60. NCDValRef arg_addr;
  61. NCDValRef arg_net_addr;
  62. NCDValRef arg_net_prefix = NCDVal_NewInvalid();
  63. if (!NCDVal_ListRead(params->args, 2, &arg_addr, &arg_net_addr) &&
  64. !NCDVal_ListRead(params->args, 3, &arg_addr, &arg_net_addr, &arg_net_prefix)
  65. ) {
  66. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  67. goto fail0;
  68. }
  69. if (!NCDVal_IsString(arg_addr) || !NCDVal_IsString(arg_net_addr) ||
  70. (!NCDVal_IsInvalid(arg_net_prefix) && !NCDVal_IsString(arg_net_prefix))
  71. ) {
  72. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  73. goto fail0;
  74. }
  75. // parse addr
  76. uint32_t addr;
  77. if (!ipaddr_parse_ipv4_addr(NCDVal_StringMemRef(arg_addr), &addr)) {
  78. ModuleLog(o->i, BLOG_ERROR, "bad address");
  79. goto fail0;
  80. }
  81. // parse network
  82. struct ipv4_ifaddr network;
  83. if (NCDVal_IsInvalid(arg_net_prefix)) {
  84. if (!ipaddr_parse_ipv4_ifaddr(NCDVal_StringMemRef(arg_net_addr), &network)) {
  85. ModuleLog(o->i, BLOG_ERROR, "bad network in CIDR notation");
  86. goto fail0;
  87. }
  88. } else {
  89. if (!ipaddr_parse_ipv4_addr(NCDVal_StringMemRef(arg_net_addr), &network.addr)) {
  90. ModuleLog(o->i, BLOG_ERROR, "bad network address");
  91. goto fail0;
  92. }
  93. if (!ipaddr_parse_ipv4_prefix(NCDVal_StringMemRef(arg_net_prefix), &network.prefix)) {
  94. ModuleLog(o->i, BLOG_ERROR, "bad network prefix");
  95. goto fail0;
  96. }
  97. }
  98. // test
  99. o->value = ipaddr_ipv4_addrs_in_network(addr, network.addr, network.prefix);
  100. if (is_ifnot && o->value) {
  101. ModuleLog(o->i, BLOG_ERROR, "addresses belong to same subnet, not proceeding");
  102. }
  103. // signal up
  104. if (!is_ifnot || !o->value) {
  105. NCDModuleInst_Backend_Up(o->i);
  106. }
  107. return;
  108. fail0:
  109. NCDModuleInst_Backend_DeadError(i);
  110. }
  111. static void func_new_normal (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  112. {
  113. func_new_common(vo, i, params, 0);
  114. }
  115. static void func_new_ifnot (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  116. {
  117. func_new_common(vo, i, params, 1);
  118. }
  119. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  120. {
  121. struct instance *o = vo;
  122. if (!strcmp(name, "")) {
  123. *out = ncd_make_boolean(mem, o->value);
  124. return 1;
  125. }
  126. return 0;
  127. }
  128. static struct NCDModule modules[] = {
  129. {
  130. .type = "net.ipv4.addr_in_network",
  131. .func_new2 = func_new_normal,
  132. .func_getvar = func_getvar,
  133. .alloc_size = sizeof(struct instance)
  134. }, {
  135. .type = "ip_in_network", // compatibility name
  136. .func_new2 = func_new_normal,
  137. .func_getvar = func_getvar,
  138. .alloc_size = sizeof(struct instance)
  139. }, {
  140. .type = "net.ipv4.ifnot_addr_in_network",
  141. .func_new2 = func_new_ifnot,
  142. .func_getvar = func_getvar,
  143. .alloc_size = sizeof(struct instance)
  144. }, {
  145. .type = NULL
  146. }
  147. };
  148. const struct NCDModuleGroup ncdmodule_net_ipv4_addr_in_network = {
  149. .modules = modules
  150. };