netmask.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. * ipv4_mask_to_prefix(string mask)
  34. *
  35. * Variables:
  36. * string (empty) - converted
  37. */
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <stdio.h>
  41. #include <inttypes.h>
  42. #include <misc/ipaddr.h>
  43. #include <misc/parse_number.h>
  44. #include <ncd/NCDModule.h>
  45. #include <generated/blog_channel_ncd_netmask.h>
  46. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  47. struct prefix_to_mask_instance {
  48. NCDModuleInst *i;
  49. uint32_t mask;
  50. };
  51. struct mask_to_prefix_instance {
  52. NCDModuleInst *i;
  53. int prefix;
  54. };
  55. static void prefix_to_mask_func_init (NCDModuleInst *i)
  56. {
  57. // allocate structure
  58. struct prefix_to_mask_instance *o = malloc(sizeof(*o));
  59. if (!o) {
  60. ModuleLog(i, BLOG_ERROR, "malloc failed");
  61. goto fail0;
  62. }
  63. o->i = i;
  64. NCDModuleInst_Backend_SetUser(i, o);
  65. // read arguments
  66. NCDValue *prefix_arg;
  67. if (!NCDValue_ListRead(i->args, 1, &prefix_arg)) {
  68. ModuleLog(i, BLOG_ERROR, "wrong arity");
  69. goto fail1;
  70. }
  71. if (!NCDValue_IsStringNoNulls(prefix_arg)) {
  72. ModuleLog(i, BLOG_ERROR, "wrong type");
  73. goto fail1;
  74. }
  75. // parse prefix
  76. uintmax_t prefix;
  77. if (!parse_unsigned_integer(NCDValue_StringValue(prefix_arg), &prefix) || prefix > 32) {
  78. ModuleLog(i, BLOG_ERROR, "bad prefix");
  79. goto fail1;
  80. }
  81. // build mask
  82. o->mask = ipaddr_ipv4_mask_from_prefix(prefix);
  83. // signal up
  84. NCDModuleInst_Backend_Up(i);
  85. return;
  86. fail1:
  87. free(o);
  88. fail0:
  89. NCDModuleInst_Backend_SetError(i);
  90. NCDModuleInst_Backend_Dead(i);
  91. }
  92. static void prefix_to_mask_func_die (void *vo)
  93. {
  94. struct prefix_to_mask_instance *o = vo;
  95. NCDModuleInst *i = o->i;
  96. // free structure
  97. free(o);
  98. NCDModuleInst_Backend_Dead(i);
  99. }
  100. static int prefix_to_mask_func_getvar (void *vo, const char *name, NCDValue *out_value)
  101. {
  102. struct prefix_to_mask_instance *o = vo;
  103. if (!strcmp(name, "")) {
  104. uint8_t *x = (void *)&o->mask;
  105. char buf[20];
  106. sprintf(buf, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, x[0], x[1], x[2], x[3]);
  107. if (!NCDValue_InitString(out_value, buf)) {
  108. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  109. return 0;
  110. }
  111. return 1;
  112. }
  113. return 0;
  114. }
  115. static void mask_to_prefix_func_init (NCDModuleInst *i)
  116. {
  117. // allocate structure
  118. struct mask_to_prefix_instance *o = malloc(sizeof(*o));
  119. if (!o) {
  120. ModuleLog(i, BLOG_ERROR, "malloc failed");
  121. goto fail0;
  122. }
  123. o->i = i;
  124. NCDModuleInst_Backend_SetUser(i, o);
  125. // read arguments
  126. NCDValue *mask_arg;
  127. if (!NCDValue_ListRead(i->args, 1, &mask_arg)) {
  128. ModuleLog(i, BLOG_ERROR, "wrong arity");
  129. goto fail1;
  130. }
  131. if (!NCDValue_IsStringNoNulls(mask_arg)) {
  132. ModuleLog(i, BLOG_ERROR, "wrong type");
  133. goto fail1;
  134. }
  135. // parse mask
  136. uint32_t mask;
  137. if (!ipaddr_parse_ipv4_addr(NCDValue_StringValue(mask_arg), &mask)) {
  138. ModuleLog(i, BLOG_ERROR, "bad mask");
  139. goto fail1;
  140. }
  141. // build prefix
  142. if (!ipaddr_ipv4_prefix_from_mask(mask, &o->prefix)) {
  143. ModuleLog(i, BLOG_ERROR, "bad mask");
  144. goto fail1;
  145. }
  146. // signal up
  147. NCDModuleInst_Backend_Up(i);
  148. return;
  149. fail1:
  150. free(o);
  151. fail0:
  152. NCDModuleInst_Backend_SetError(i);
  153. NCDModuleInst_Backend_Dead(i);
  154. }
  155. static void mask_to_prefix_func_die (void *vo)
  156. {
  157. struct mask_to_prefix_instance *o = vo;
  158. NCDModuleInst *i = o->i;
  159. // free structure
  160. free(o);
  161. NCDModuleInst_Backend_Dead(i);
  162. }
  163. static int mask_to_prefix_func_getvar (void *vo, const char *name, NCDValue *out_value)
  164. {
  165. struct mask_to_prefix_instance *o = vo;
  166. if (!strcmp(name, "")) {
  167. char buf[6];
  168. sprintf(buf, "%d", o->prefix);
  169. if (!NCDValue_InitString(out_value, buf)) {
  170. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  171. return 0;
  172. }
  173. return 1;
  174. }
  175. return 0;
  176. }
  177. static const struct NCDModule modules[] = {
  178. {
  179. .type = "ipv4_prefix_to_mask",
  180. .func_new = prefix_to_mask_func_init,
  181. .func_die = prefix_to_mask_func_die,
  182. .func_getvar = prefix_to_mask_func_getvar
  183. }, {
  184. .type = "ipv4_mask_to_prefix",
  185. .func_new = mask_to_prefix_func_init,
  186. .func_die = mask_to_prefix_func_die,
  187. .func_getvar = mask_to_prefix_func_getvar
  188. }, {
  189. .type = NULL
  190. }
  191. };
  192. const struct NCDModuleGroup ncdmodule_netmask = {
  193. .modules = modules
  194. };