netmask.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. * @file netmask.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. * ipv4_prefix_to_mask(string prefix)
  33. *
  34. * Variables:
  35. * string (empty) - prefix, converted to dotted decimal format without leading
  36. * zeros
  37. *
  38. * Synopsis:
  39. * ipv4_mask_to_prefix(string mask)
  40. *
  41. * Variables:
  42. * string (empty) - mask, converted to prefix length
  43. *
  44. * Synopsis:
  45. * ipv4_net_from_addr_and_prefix(string addr, string prefix)
  46. *
  47. * Variables:
  48. * string (empty) - network part of the address, according to given prefix
  49. * length, in dotted decimal format without leading zeros
  50. */
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <stdio.h>
  54. #include <inttypes.h>
  55. #include <misc/ipaddr.h>
  56. #include <misc/parse_number.h>
  57. #include <ncd/NCDModule.h>
  58. #include <ncd/static_strings.h>
  59. #include <generated/blog_channel_ncd_netmask.h>
  60. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  61. struct addr_instance {
  62. NCDModuleInst *i;
  63. uint32_t addr;
  64. };
  65. struct prefix_instance {
  66. NCDModuleInst *i;
  67. int prefix;
  68. };
  69. static void addr_func_init_templ (void *vo, NCDModuleInst *i, uint32_t addr)
  70. {
  71. struct addr_instance *o = vo;
  72. o->i = i;
  73. // remember address
  74. o->addr = addr;
  75. // signal up
  76. NCDModuleInst_Backend_Up(i);
  77. }
  78. static void prefix_to_mask_func_init (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  79. {
  80. // read arguments
  81. NCDValRef prefix_arg;
  82. if (!NCDVal_ListRead(params->args, 1, &prefix_arg)) {
  83. ModuleLog(i, BLOG_ERROR, "wrong arity");
  84. goto fail0;
  85. }
  86. if (!NCDVal_IsString(prefix_arg)) {
  87. ModuleLog(i, BLOG_ERROR, "wrong type");
  88. goto fail0;
  89. }
  90. // parse prefix
  91. int prefix;
  92. if (!ipaddr_parse_ipv4_prefix_bin((char *)NCDVal_StringData(prefix_arg), NCDVal_StringLength(prefix_arg), &prefix)) {
  93. ModuleLog(i, BLOG_ERROR, "bad prefix");
  94. goto fail0;
  95. }
  96. // make mask
  97. uint32_t mask = ipaddr_ipv4_mask_from_prefix(prefix);
  98. addr_func_init_templ(vo, i, mask);
  99. return;
  100. fail0:
  101. NCDModuleInst_Backend_DeadError(i);
  102. }
  103. static void ipv4_net_from_addr_and_prefix_func_init (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  104. {
  105. // read arguments
  106. NCDValRef addr_arg;
  107. NCDValRef prefix_arg;
  108. if (!NCDVal_ListRead(params->args, 2, &addr_arg, &prefix_arg)) {
  109. ModuleLog(i, BLOG_ERROR, "wrong arity");
  110. goto fail0;
  111. }
  112. if (!NCDVal_IsString(addr_arg) || !NCDVal_IsString(prefix_arg)) {
  113. ModuleLog(i, BLOG_ERROR, "wrong type");
  114. goto fail0;
  115. }
  116. // parse addr
  117. uint32_t addr;
  118. if (!ipaddr_parse_ipv4_addr_bin((char *)NCDVal_StringData(addr_arg), NCDVal_StringLength(addr_arg), &addr)) {
  119. ModuleLog(i, BLOG_ERROR, "bad addr");
  120. goto fail0;
  121. }
  122. // parse prefix
  123. int prefix;
  124. if (!ipaddr_parse_ipv4_prefix_bin((char *)NCDVal_StringData(prefix_arg), NCDVal_StringLength(prefix_arg), &prefix)) {
  125. ModuleLog(i, BLOG_ERROR, "bad prefix");
  126. goto fail0;
  127. }
  128. // make network
  129. uint32_t network = (addr & ipaddr_ipv4_mask_from_prefix(prefix));
  130. addr_func_init_templ(vo, i, network);
  131. return;
  132. fail0:
  133. NCDModuleInst_Backend_DeadError(i);
  134. }
  135. static void addr_func_die (void *vo)
  136. {
  137. struct addr_instance *o = vo;
  138. NCDModuleInst_Backend_Dead(o->i);
  139. }
  140. static int addr_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  141. {
  142. struct addr_instance *o = vo;
  143. if (name == NCD_STRING_EMPTY) {
  144. char buf[IPADDR_PRINT_MAX];
  145. ipaddr_print_addr(o->addr, buf);
  146. *out = NCDVal_NewString(mem, buf);
  147. return 1;
  148. }
  149. return 0;
  150. }
  151. static void mask_to_prefix_func_init (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  152. {
  153. struct prefix_instance *o = vo;
  154. o->i = i;
  155. // read arguments
  156. NCDValRef mask_arg;
  157. if (!NCDVal_ListRead(params->args, 1, &mask_arg)) {
  158. ModuleLog(i, BLOG_ERROR, "wrong arity");
  159. goto fail0;
  160. }
  161. if (!NCDVal_IsString(mask_arg)) {
  162. ModuleLog(i, BLOG_ERROR, "wrong type");
  163. goto fail0;
  164. }
  165. // parse mask
  166. uint32_t mask;
  167. if (!ipaddr_parse_ipv4_addr_bin((char *)NCDVal_StringData(mask_arg), NCDVal_StringLength(mask_arg), &mask)) {
  168. ModuleLog(i, BLOG_ERROR, "bad mask");
  169. goto fail0;
  170. }
  171. // build prefix
  172. if (!ipaddr_ipv4_prefix_from_mask(mask, &o->prefix)) {
  173. ModuleLog(i, BLOG_ERROR, "bad mask");
  174. goto fail0;
  175. }
  176. // signal up
  177. NCDModuleInst_Backend_Up(i);
  178. return;
  179. fail0:
  180. NCDModuleInst_Backend_DeadError(i);
  181. }
  182. static void prefix_func_die (void *vo)
  183. {
  184. struct prefix_instance *o = vo;
  185. NCDModuleInst_Backend_Dead(o->i);
  186. }
  187. static int prefix_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  188. {
  189. struct prefix_instance *o = vo;
  190. if (name == NCD_STRING_EMPTY) {
  191. char buf[6];
  192. sprintf(buf, "%d", o->prefix);
  193. *out = NCDVal_NewString(mem, buf);
  194. return 1;
  195. }
  196. return 0;
  197. }
  198. static struct NCDModule modules[] = {
  199. {
  200. .type = "ipv4_prefix_to_mask",
  201. .func_new2 = prefix_to_mask_func_init,
  202. .func_die = addr_func_die,
  203. .func_getvar2 = addr_func_getvar2,
  204. .alloc_size = sizeof(struct addr_instance)
  205. }, {
  206. .type = "ipv4_mask_to_prefix",
  207. .func_new2 = mask_to_prefix_func_init,
  208. .func_die = prefix_func_die,
  209. .func_getvar2 = prefix_func_getvar2,
  210. .alloc_size = sizeof(struct prefix_instance)
  211. }, {
  212. .type = "ipv4_net_from_addr_and_prefix",
  213. .func_new2 = ipv4_net_from_addr_and_prefix_func_init,
  214. .func_die = addr_func_die,
  215. .func_getvar2 = addr_func_getvar2,
  216. .alloc_size = sizeof(struct addr_instance)
  217. }, {
  218. .type = NULL
  219. }
  220. };
  221. const struct NCDModuleGroup ncdmodule_netmask = {
  222. .modules = modules
  223. };