netmask.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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/module_common.h>
  58. #include <generated/blog_channel_ncd_netmask.h>
  59. struct addr_instance {
  60. NCDModuleInst *i;
  61. uint32_t addr;
  62. };
  63. struct prefix_instance {
  64. NCDModuleInst *i;
  65. int prefix;
  66. };
  67. static void addr_func_init_templ (void *vo, NCDModuleInst *i, uint32_t addr)
  68. {
  69. struct addr_instance *o = vo;
  70. o->i = i;
  71. // remember address
  72. o->addr = addr;
  73. // signal up
  74. NCDModuleInst_Backend_Up(i);
  75. }
  76. static void prefix_to_mask_func_init (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  77. {
  78. // read arguments
  79. NCDValRef prefix_arg;
  80. if (!NCDVal_ListRead(params->args, 1, &prefix_arg)) {
  81. ModuleLog(i, BLOG_ERROR, "wrong arity");
  82. goto fail0;
  83. }
  84. if (!NCDVal_IsString(prefix_arg)) {
  85. ModuleLog(i, BLOG_ERROR, "wrong type");
  86. goto fail0;
  87. }
  88. // parse prefix
  89. int prefix;
  90. if (!ipaddr_parse_ipv4_prefix(NCDVal_StringMemRef(prefix_arg), &prefix)) {
  91. ModuleLog(i, BLOG_ERROR, "bad prefix");
  92. goto fail0;
  93. }
  94. // make mask
  95. uint32_t mask = ipaddr_ipv4_mask_from_prefix(prefix);
  96. addr_func_init_templ(vo, i, mask);
  97. return;
  98. fail0:
  99. NCDModuleInst_Backend_DeadError(i);
  100. }
  101. static void ipv4_net_from_addr_and_prefix_func_init (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  102. {
  103. // read arguments
  104. NCDValRef addr_arg;
  105. NCDValRef prefix_arg;
  106. if (!NCDVal_ListRead(params->args, 2, &addr_arg, &prefix_arg)) {
  107. ModuleLog(i, BLOG_ERROR, "wrong arity");
  108. goto fail0;
  109. }
  110. if (!NCDVal_IsString(addr_arg) || !NCDVal_IsString(prefix_arg)) {
  111. ModuleLog(i, BLOG_ERROR, "wrong type");
  112. goto fail0;
  113. }
  114. // parse addr
  115. uint32_t addr;
  116. if (!ipaddr_parse_ipv4_addr(NCDVal_StringMemRef(addr_arg), &addr)) {
  117. ModuleLog(i, BLOG_ERROR, "bad addr");
  118. goto fail0;
  119. }
  120. // parse prefix
  121. int prefix;
  122. if (!ipaddr_parse_ipv4_prefix(NCDVal_StringMemRef(prefix_arg), &prefix)) {
  123. ModuleLog(i, BLOG_ERROR, "bad prefix");
  124. goto fail0;
  125. }
  126. // make network
  127. uint32_t network = (addr & ipaddr_ipv4_mask_from_prefix(prefix));
  128. addr_func_init_templ(vo, i, network);
  129. return;
  130. fail0:
  131. NCDModuleInst_Backend_DeadError(i);
  132. }
  133. static void addr_func_die (void *vo)
  134. {
  135. struct addr_instance *o = vo;
  136. NCDModuleInst_Backend_Dead(o->i);
  137. }
  138. static int addr_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  139. {
  140. struct addr_instance *o = vo;
  141. if (name == NCD_STRING_EMPTY) {
  142. char buf[IPADDR_PRINT_MAX];
  143. ipaddr_print_addr(o->addr, buf);
  144. *out = NCDVal_NewString(mem, buf);
  145. return 1;
  146. }
  147. return 0;
  148. }
  149. static void mask_to_prefix_func_init (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  150. {
  151. struct prefix_instance *o = vo;
  152. o->i = i;
  153. // read arguments
  154. NCDValRef mask_arg;
  155. if (!NCDVal_ListRead(params->args, 1, &mask_arg)) {
  156. ModuleLog(i, BLOG_ERROR, "wrong arity");
  157. goto fail0;
  158. }
  159. if (!NCDVal_IsString(mask_arg)) {
  160. ModuleLog(i, BLOG_ERROR, "wrong type");
  161. goto fail0;
  162. }
  163. // parse mask
  164. uint32_t mask;
  165. if (!ipaddr_parse_ipv4_addr(NCDVal_StringMemRef(mask_arg), &mask)) {
  166. ModuleLog(i, BLOG_ERROR, "bad mask");
  167. goto fail0;
  168. }
  169. // build prefix
  170. if (!ipaddr_ipv4_prefix_from_mask(mask, &o->prefix)) {
  171. ModuleLog(i, BLOG_ERROR, "bad mask");
  172. goto fail0;
  173. }
  174. // signal up
  175. NCDModuleInst_Backend_Up(i);
  176. return;
  177. fail0:
  178. NCDModuleInst_Backend_DeadError(i);
  179. }
  180. static void prefix_func_die (void *vo)
  181. {
  182. struct prefix_instance *o = vo;
  183. NCDModuleInst_Backend_Dead(o->i);
  184. }
  185. static int prefix_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  186. {
  187. struct prefix_instance *o = vo;
  188. if (name == NCD_STRING_EMPTY) {
  189. char buf[6];
  190. sprintf(buf, "%d", o->prefix);
  191. *out = NCDVal_NewString(mem, buf);
  192. return 1;
  193. }
  194. return 0;
  195. }
  196. static struct NCDModule modules[] = {
  197. {
  198. .type = "ipv4_prefix_to_mask",
  199. .func_new2 = prefix_to_mask_func_init,
  200. .func_die = addr_func_die,
  201. .func_getvar2 = addr_func_getvar2,
  202. .alloc_size = sizeof(struct addr_instance)
  203. }, {
  204. .type = "ipv4_mask_to_prefix",
  205. .func_new2 = mask_to_prefix_func_init,
  206. .func_die = prefix_func_die,
  207. .func_getvar2 = prefix_func_getvar2,
  208. .alloc_size = sizeof(struct prefix_instance)
  209. }, {
  210. .type = "ipv4_net_from_addr_and_prefix",
  211. .func_new2 = ipv4_net_from_addr_and_prefix_func_init,
  212. .func_die = addr_func_die,
  213. .func_getvar2 = addr_func_getvar2,
  214. .alloc_size = sizeof(struct addr_instance)
  215. }, {
  216. .type = NULL
  217. }
  218. };
  219. const struct NCDModuleGroup ncdmodule_netmask = {
  220. .modules = modules
  221. };