netmask.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 (NCDModuleInst *i, uint32_t addr)
  69. {
  70. // allocate structure
  71. struct addr_instance *o = malloc(sizeof(*o));
  72. if (!o) {
  73. ModuleLog(i, BLOG_ERROR, "malloc failed");
  74. goto fail0;
  75. }
  76. o->i = i;
  77. NCDModuleInst_Backend_SetUser(i, o);
  78. // remember address
  79. o->addr = addr;
  80. // signal up
  81. NCDModuleInst_Backend_Up(i);
  82. return;
  83. fail0:
  84. NCDModuleInst_Backend_SetError(i);
  85. NCDModuleInst_Backend_Dead(i);
  86. }
  87. static void prefix_to_mask_func_init (NCDModuleInst *i)
  88. {
  89. // read arguments
  90. NCDValRef prefix_arg;
  91. if (!NCDVal_ListRead(i->args, 1, &prefix_arg)) {
  92. ModuleLog(i, BLOG_ERROR, "wrong arity");
  93. goto fail0;
  94. }
  95. if (!NCDVal_IsStringNoNulls(prefix_arg)) {
  96. ModuleLog(i, BLOG_ERROR, "wrong type");
  97. goto fail0;
  98. }
  99. // parse prefix
  100. int prefix;
  101. if (!ipaddr_parse_ipv4_prefix((char *)NCDVal_StringValue(prefix_arg), &prefix)) {
  102. ModuleLog(i, BLOG_ERROR, "bad prefix");
  103. goto fail0;
  104. }
  105. // make mask
  106. uint32_t mask = ipaddr_ipv4_mask_from_prefix(prefix);
  107. addr_func_init_templ(i, mask);
  108. return;
  109. fail0:
  110. NCDModuleInst_Backend_SetError(i);
  111. NCDModuleInst_Backend_Dead(i);
  112. }
  113. static void ipv4_net_from_addr_and_prefix_func_init (NCDModuleInst *i)
  114. {
  115. // read arguments
  116. NCDValRef addr_arg;
  117. NCDValRef prefix_arg;
  118. if (!NCDVal_ListRead(i->args, 2, &addr_arg, &prefix_arg)) {
  119. ModuleLog(i, BLOG_ERROR, "wrong arity");
  120. goto fail0;
  121. }
  122. if (!NCDVal_IsStringNoNulls(addr_arg) || !NCDVal_IsStringNoNulls(prefix_arg)) {
  123. ModuleLog(i, BLOG_ERROR, "wrong type");
  124. goto fail0;
  125. }
  126. // parse addr
  127. uint32_t addr;
  128. if (!ipaddr_parse_ipv4_addr((char *)NCDVal_StringValue(addr_arg), &addr)) {
  129. ModuleLog(i, BLOG_ERROR, "bad addr");
  130. goto fail0;
  131. }
  132. // parse prefix
  133. int prefix;
  134. if (!ipaddr_parse_ipv4_prefix((char *)NCDVal_StringValue(prefix_arg), &prefix)) {
  135. ModuleLog(i, BLOG_ERROR, "bad prefix");
  136. goto fail0;
  137. }
  138. // make network
  139. uint32_t network = (addr & ipaddr_ipv4_mask_from_prefix(prefix));
  140. addr_func_init_templ(i, network);
  141. return;
  142. fail0:
  143. NCDModuleInst_Backend_SetError(i);
  144. NCDModuleInst_Backend_Dead(i);
  145. }
  146. static void addr_func_die (void *vo)
  147. {
  148. struct addr_instance *o = vo;
  149. NCDModuleInst *i = o->i;
  150. // free structure
  151. free(o);
  152. NCDModuleInst_Backend_Dead(i);
  153. }
  154. static int addr_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  155. {
  156. struct addr_instance *o = vo;
  157. if (!strcmp(name, "")) {
  158. uint8_t *x = (void *)&o->addr;
  159. char buf[20];
  160. sprintf(buf, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, x[0], x[1], x[2], x[3]);
  161. *out = NCDVal_NewString(mem, buf);
  162. if (NCDVal_IsInvalid(*out)) {
  163. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  164. }
  165. return 1;
  166. }
  167. return 0;
  168. }
  169. static void mask_to_prefix_func_init (NCDModuleInst *i)
  170. {
  171. // allocate structure
  172. struct prefix_instance *o = malloc(sizeof(*o));
  173. if (!o) {
  174. ModuleLog(i, BLOG_ERROR, "malloc failed");
  175. goto fail0;
  176. }
  177. o->i = i;
  178. NCDModuleInst_Backend_SetUser(i, o);
  179. // read arguments
  180. NCDValRef mask_arg;
  181. if (!NCDVal_ListRead(i->args, 1, &mask_arg)) {
  182. ModuleLog(i, BLOG_ERROR, "wrong arity");
  183. goto fail1;
  184. }
  185. if (!NCDVal_IsStringNoNulls(mask_arg)) {
  186. ModuleLog(i, BLOG_ERROR, "wrong type");
  187. goto fail1;
  188. }
  189. // parse mask
  190. uint32_t mask;
  191. if (!ipaddr_parse_ipv4_addr((char *)NCDVal_StringValue(mask_arg), &mask)) {
  192. ModuleLog(i, BLOG_ERROR, "bad mask");
  193. goto fail1;
  194. }
  195. // build prefix
  196. if (!ipaddr_ipv4_prefix_from_mask(mask, &o->prefix)) {
  197. ModuleLog(i, BLOG_ERROR, "bad mask");
  198. goto fail1;
  199. }
  200. // signal up
  201. NCDModuleInst_Backend_Up(i);
  202. return;
  203. fail1:
  204. free(o);
  205. fail0:
  206. NCDModuleInst_Backend_SetError(i);
  207. NCDModuleInst_Backend_Dead(i);
  208. }
  209. static void prefix_func_die (void *vo)
  210. {
  211. struct prefix_instance *o = vo;
  212. NCDModuleInst *i = o->i;
  213. // free structure
  214. free(o);
  215. NCDModuleInst_Backend_Dead(i);
  216. }
  217. static int prefix_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  218. {
  219. struct prefix_instance *o = vo;
  220. if (!strcmp(name, "")) {
  221. char buf[6];
  222. sprintf(buf, "%d", o->prefix);
  223. *out = NCDVal_NewString(mem, buf);
  224. if (NCDVal_IsInvalid(*out)) {
  225. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  226. }
  227. return 1;
  228. }
  229. return 0;
  230. }
  231. static const struct NCDModule modules[] = {
  232. {
  233. .type = "ipv4_prefix_to_mask",
  234. .func_new = prefix_to_mask_func_init,
  235. .func_die = addr_func_die,
  236. .func_getvar = addr_func_getvar
  237. }, {
  238. .type = "ipv4_mask_to_prefix",
  239. .func_new = mask_to_prefix_func_init,
  240. .func_die = prefix_func_die,
  241. .func_getvar = prefix_func_getvar
  242. }, {
  243. .type = "ipv4_net_from_addr_and_prefix",
  244. .func_new = ipv4_net_from_addr_and_prefix_func_init,
  245. .func_die = addr_func_die,
  246. .func_getvar = addr_func_getvar
  247. }, {
  248. .type = NULL
  249. }
  250. };
  251. const struct NCDModuleGroup ncdmodule_netmask = {
  252. .modules = modules
  253. };