net_ipv4_addr_in_network.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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/NCDModule.h>
  50. #include <generated/blog_channel_ncd_net_ipv4_addr_in_network.h>
  51. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  52. struct instance {
  53. NCDModuleInst *i;
  54. int value;
  55. };
  56. static void func_new_common (void *vo, NCDModuleInst *i, int is_ifnot)
  57. {
  58. struct instance *o = vo;
  59. o->i = i;
  60. // read arguments
  61. NCDValRef arg_addr;
  62. NCDValRef arg_net_addr;
  63. NCDValRef arg_net_prefix = NCDVal_NewInvalid();
  64. if (!NCDVal_ListRead(i->args, 2, &arg_addr, &arg_net_addr) &&
  65. !NCDVal_ListRead(i->args, 3, &arg_addr, &arg_net_addr, &arg_net_prefix)
  66. ) {
  67. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  68. goto fail0;
  69. }
  70. if (!NCDVal_IsString(arg_addr) || !NCDVal_IsString(arg_net_addr) ||
  71. (!NCDVal_IsInvalid(arg_net_prefix) && !NCDVal_IsString(arg_net_prefix))
  72. ) {
  73. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  74. goto fail0;
  75. }
  76. // parse addr
  77. uint32_t addr;
  78. if (!ipaddr_parse_ipv4_addr_bin(NCDVal_StringValue(arg_addr), NCDVal_StringLength(arg_addr), &addr)) {
  79. ModuleLog(o->i, BLOG_ERROR, "bad address");
  80. goto fail0;
  81. }
  82. // parse network
  83. struct ipv4_ifaddr network;
  84. if (NCDVal_IsInvalid(arg_net_prefix)) {
  85. if (!ipaddr_parse_ipv4_ifaddr_bin(NCDVal_StringValue(arg_net_addr), NCDVal_StringLength(arg_net_addr), &network)) {
  86. ModuleLog(o->i, BLOG_ERROR, "bad network in CIDR notation");
  87. goto fail0;
  88. }
  89. } else {
  90. if (!ipaddr_parse_ipv4_addr_bin(NCDVal_StringValue(arg_net_addr), NCDVal_StringLength(arg_net_addr), &network.addr)) {
  91. ModuleLog(o->i, BLOG_ERROR, "bad network address");
  92. goto fail0;
  93. }
  94. if (!ipaddr_parse_ipv4_prefix_bin(NCDVal_StringValue(arg_net_prefix), NCDVal_StringLength(arg_net_prefix), &network.prefix)) {
  95. ModuleLog(o->i, BLOG_ERROR, "bad network prefix");
  96. goto fail0;
  97. }
  98. }
  99. // test
  100. o->value = ipaddr_ipv4_addrs_in_network(addr, network.addr, network.prefix);
  101. if (is_ifnot && o->value) {
  102. ModuleLog(o->i, BLOG_ERROR, "addresses belong to same subnet, not proceeding");
  103. }
  104. // signal up
  105. if (!is_ifnot || !o->value) {
  106. NCDModuleInst_Backend_Up(o->i);
  107. }
  108. return;
  109. fail0:
  110. NCDModuleInst_Backend_SetError(i);
  111. NCDModuleInst_Backend_Dead(i);
  112. }
  113. static void func_new_normal (void *vo, NCDModuleInst *i)
  114. {
  115. func_new_common(vo, i, 0);
  116. }
  117. static void func_new_ifnot (void *vo, NCDModuleInst *i)
  118. {
  119. func_new_common(vo, i, 1);
  120. }
  121. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  122. {
  123. struct instance *o = vo;
  124. if (!strcmp(name, "")) {
  125. const char *v = (o->value ? "true" : "false");
  126. *out = NCDVal_NewString(mem, v);
  127. if (NCDVal_IsInvalid(*out)) {
  128. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  129. }
  130. return 1;
  131. }
  132. return 0;
  133. }
  134. static const struct NCDModule modules[] = {
  135. {
  136. .type = "net.ipv4.addr_in_network",
  137. .func_new2 = func_new_normal,
  138. .func_getvar = func_getvar,
  139. .alloc_size = sizeof(struct instance)
  140. }, {
  141. .type = "net.ipv4.ifnot_addr_in_network",
  142. .func_new2 = func_new_ifnot,
  143. .func_getvar = func_getvar,
  144. .alloc_size = sizeof(struct instance)
  145. }, {
  146. .type = NULL
  147. }
  148. };
  149. const struct NCDModuleGroup ncdmodule_net_ipv4_addr_in_network = {
  150. .modules = modules
  151. };