netmask.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 <generated/blog_channel_ncd_netmask.h>
  59. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  60. struct addr_instance {
  61. NCDModuleInst *i;
  62. uint32_t addr;
  63. };
  64. struct prefix_instance {
  65. NCDModuleInst *i;
  66. int prefix;
  67. };
  68. static void addr_func_init_templ (void *vo, NCDModuleInst *i, uint32_t addr)
  69. {
  70. struct addr_instance *o = vo;
  71. o->i = i;
  72. // remember address
  73. o->addr = addr;
  74. // signal up
  75. NCDModuleInst_Backend_Up(i);
  76. }
  77. static void prefix_to_mask_func_init (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  78. {
  79. // read arguments
  80. NCDValRef prefix_arg;
  81. if (!NCDVal_ListRead(params->args, 1, &prefix_arg)) {
  82. ModuleLog(i, BLOG_ERROR, "wrong arity");
  83. goto fail0;
  84. }
  85. if (!NCDVal_IsStringNoNulls(prefix_arg)) {
  86. ModuleLog(i, BLOG_ERROR, "wrong type");
  87. goto fail0;
  88. }
  89. // parse prefix
  90. int prefix;
  91. if (!ipaddr_parse_ipv4_prefix((char *)NCDVal_StringValue(prefix_arg), &prefix)) {
  92. ModuleLog(i, BLOG_ERROR, "bad prefix");
  93. goto fail0;
  94. }
  95. // make mask
  96. uint32_t mask = ipaddr_ipv4_mask_from_prefix(prefix);
  97. addr_func_init_templ(vo, i, mask);
  98. return;
  99. fail0:
  100. NCDModuleInst_Backend_SetError(i);
  101. NCDModuleInst_Backend_Dead(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_IsStringNoNulls(addr_arg) || !NCDVal_IsStringNoNulls(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((char *)NCDVal_StringValue(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((char *)NCDVal_StringValue(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_SetError(i);
  134. NCDModuleInst_Backend_Dead(i);
  135. }
  136. static void addr_func_die (void *vo)
  137. {
  138. struct addr_instance *o = vo;
  139. NCDModuleInst_Backend_Dead(o->i);
  140. }
  141. static int addr_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  142. {
  143. struct addr_instance *o = vo;
  144. if (!strcmp(name, "")) {
  145. char buf[IPADDR_PRINT_MAX];
  146. ipaddr_print_addr(o->addr, buf);
  147. *out = NCDVal_NewString(mem, buf);
  148. if (NCDVal_IsInvalid(*out)) {
  149. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  150. }
  151. return 1;
  152. }
  153. return 0;
  154. }
  155. static void mask_to_prefix_func_init (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  156. {
  157. struct prefix_instance *o = vo;
  158. o->i = i;
  159. // read arguments
  160. NCDValRef mask_arg;
  161. if (!NCDVal_ListRead(params->args, 1, &mask_arg)) {
  162. ModuleLog(i, BLOG_ERROR, "wrong arity");
  163. goto fail0;
  164. }
  165. if (!NCDVal_IsStringNoNulls(mask_arg)) {
  166. ModuleLog(i, BLOG_ERROR, "wrong type");
  167. goto fail0;
  168. }
  169. // parse mask
  170. uint32_t mask;
  171. if (!ipaddr_parse_ipv4_addr((char *)NCDVal_StringValue(mask_arg), &mask)) {
  172. ModuleLog(i, BLOG_ERROR, "bad mask");
  173. goto fail0;
  174. }
  175. // build prefix
  176. if (!ipaddr_ipv4_prefix_from_mask(mask, &o->prefix)) {
  177. ModuleLog(i, BLOG_ERROR, "bad mask");
  178. goto fail0;
  179. }
  180. // signal up
  181. NCDModuleInst_Backend_Up(i);
  182. return;
  183. fail0:
  184. NCDModuleInst_Backend_SetError(i);
  185. NCDModuleInst_Backend_Dead(i);
  186. }
  187. static void prefix_func_die (void *vo)
  188. {
  189. struct prefix_instance *o = vo;
  190. NCDModuleInst_Backend_Dead(o->i);
  191. }
  192. static int prefix_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  193. {
  194. struct prefix_instance *o = vo;
  195. if (!strcmp(name, "")) {
  196. char buf[6];
  197. sprintf(buf, "%d", o->prefix);
  198. *out = NCDVal_NewString(mem, buf);
  199. if (NCDVal_IsInvalid(*out)) {
  200. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  201. }
  202. return 1;
  203. }
  204. return 0;
  205. }
  206. static const struct NCDModule modules[] = {
  207. {
  208. .type = "ipv4_prefix_to_mask",
  209. .func_new2 = prefix_to_mask_func_init,
  210. .func_die = addr_func_die,
  211. .func_getvar = addr_func_getvar,
  212. .alloc_size = sizeof(struct addr_instance)
  213. }, {
  214. .type = "ipv4_mask_to_prefix",
  215. .func_new2 = mask_to_prefix_func_init,
  216. .func_die = prefix_func_die,
  217. .func_getvar = prefix_func_getvar,
  218. .alloc_size = sizeof(struct prefix_instance)
  219. }, {
  220. .type = "ipv4_net_from_addr_and_prefix",
  221. .func_new2 = ipv4_net_from_addr_and_prefix_func_init,
  222. .func_die = addr_func_die,
  223. .func_getvar = addr_func_getvar,
  224. .alloc_size = sizeof(struct addr_instance)
  225. }, {
  226. .type = NULL
  227. }
  228. };
  229. struct NCDModuleGroup ncdmodule_netmask = {
  230. .modules = modules
  231. };