netmask.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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_StringValue(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_SetError(i);
  102. NCDModuleInst_Backend_Dead(i);
  103. }
  104. static void ipv4_net_from_addr_and_prefix_func_init (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  105. {
  106. // read arguments
  107. NCDValRef addr_arg;
  108. NCDValRef prefix_arg;
  109. if (!NCDVal_ListRead(params->args, 2, &addr_arg, &prefix_arg)) {
  110. ModuleLog(i, BLOG_ERROR, "wrong arity");
  111. goto fail0;
  112. }
  113. if (!NCDVal_IsString(addr_arg) || !NCDVal_IsString(prefix_arg)) {
  114. ModuleLog(i, BLOG_ERROR, "wrong type");
  115. goto fail0;
  116. }
  117. // parse addr
  118. uint32_t addr;
  119. if (!ipaddr_parse_ipv4_addr_bin((char *)NCDVal_StringValue(addr_arg), NCDVal_StringLength(addr_arg), &addr)) {
  120. ModuleLog(i, BLOG_ERROR, "bad addr");
  121. goto fail0;
  122. }
  123. // parse prefix
  124. int prefix;
  125. if (!ipaddr_parse_ipv4_prefix_bin((char *)NCDVal_StringValue(prefix_arg), NCDVal_StringLength(prefix_arg), &prefix)) {
  126. ModuleLog(i, BLOG_ERROR, "bad prefix");
  127. goto fail0;
  128. }
  129. // make network
  130. uint32_t network = (addr & ipaddr_ipv4_mask_from_prefix(prefix));
  131. addr_func_init_templ(vo, i, network);
  132. return;
  133. fail0:
  134. NCDModuleInst_Backend_SetError(i);
  135. NCDModuleInst_Backend_Dead(i);
  136. }
  137. static void addr_func_die (void *vo)
  138. {
  139. struct addr_instance *o = vo;
  140. NCDModuleInst_Backend_Dead(o->i);
  141. }
  142. static int addr_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  143. {
  144. struct addr_instance *o = vo;
  145. if (name == NCD_STRING_EMPTY) {
  146. char buf[IPADDR_PRINT_MAX];
  147. ipaddr_print_addr(o->addr, buf);
  148. *out = NCDVal_NewString(mem, buf);
  149. if (NCDVal_IsInvalid(*out)) {
  150. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  151. }
  152. return 1;
  153. }
  154. return 0;
  155. }
  156. static void mask_to_prefix_func_init (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  157. {
  158. struct prefix_instance *o = vo;
  159. o->i = i;
  160. // read arguments
  161. NCDValRef mask_arg;
  162. if (!NCDVal_ListRead(params->args, 1, &mask_arg)) {
  163. ModuleLog(i, BLOG_ERROR, "wrong arity");
  164. goto fail0;
  165. }
  166. if (!NCDVal_IsString(mask_arg)) {
  167. ModuleLog(i, BLOG_ERROR, "wrong type");
  168. goto fail0;
  169. }
  170. // parse mask
  171. uint32_t mask;
  172. if (!ipaddr_parse_ipv4_addr_bin((char *)NCDVal_StringValue(mask_arg), NCDVal_StringLength(mask_arg), &mask)) {
  173. ModuleLog(i, BLOG_ERROR, "bad mask");
  174. goto fail0;
  175. }
  176. // build prefix
  177. if (!ipaddr_ipv4_prefix_from_mask(mask, &o->prefix)) {
  178. ModuleLog(i, BLOG_ERROR, "bad mask");
  179. goto fail0;
  180. }
  181. // signal up
  182. NCDModuleInst_Backend_Up(i);
  183. return;
  184. fail0:
  185. NCDModuleInst_Backend_SetError(i);
  186. NCDModuleInst_Backend_Dead(i);
  187. }
  188. static void prefix_func_die (void *vo)
  189. {
  190. struct prefix_instance *o = vo;
  191. NCDModuleInst_Backend_Dead(o->i);
  192. }
  193. static int prefix_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  194. {
  195. struct prefix_instance *o = vo;
  196. if (name == NCD_STRING_EMPTY) {
  197. char buf[6];
  198. sprintf(buf, "%d", o->prefix);
  199. *out = NCDVal_NewString(mem, buf);
  200. if (NCDVal_IsInvalid(*out)) {
  201. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  202. }
  203. return 1;
  204. }
  205. return 0;
  206. }
  207. static struct NCDModule modules[] = {
  208. {
  209. .type = "ipv4_prefix_to_mask",
  210. .func_new2 = prefix_to_mask_func_init,
  211. .func_die = addr_func_die,
  212. .func_getvar2 = addr_func_getvar2,
  213. .alloc_size = sizeof(struct addr_instance)
  214. }, {
  215. .type = "ipv4_mask_to_prefix",
  216. .func_new2 = mask_to_prefix_func_init,
  217. .func_die = prefix_func_die,
  218. .func_getvar2 = prefix_func_getvar2,
  219. .alloc_size = sizeof(struct prefix_instance)
  220. }, {
  221. .type = "ipv4_net_from_addr_and_prefix",
  222. .func_new2 = ipv4_net_from_addr_and_prefix_func_init,
  223. .func_die = addr_func_die,
  224. .func_getvar2 = addr_func_getvar2,
  225. .alloc_size = sizeof(struct addr_instance)
  226. }, {
  227. .type = NULL
  228. }
  229. };
  230. const struct NCDModuleGroup ncdmodule_netmask = {
  231. .modules = modules
  232. };