ip_in_network.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * @file ip_in_network.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Module for checking whether two IP addresses belong to the same network.
  25. *
  26. * Synopsis: ip_in_network(string addr1, string addr2, string netprefix)
  27. * Variables:
  28. * string (empty) - "true" if addr1 and addr2 are in the same network, with
  29. * netprefix prefix, "false" if not (IPv4 only).
  30. */
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <misc/ipaddr.h>
  34. #include <ncd/NCDModule.h>
  35. #include <generated/blog_channel_ncd_ip_in_network.h>
  36. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  37. struct instance {
  38. NCDModuleInst *i;
  39. int value;
  40. };
  41. static void func_new (NCDModuleInst *i)
  42. {
  43. // allocate instance
  44. struct instance *o = malloc(sizeof(*o));
  45. if (!o) {
  46. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  47. goto fail0;
  48. }
  49. NCDModuleInst_Backend_SetUser(i, o);
  50. // init arguments
  51. o->i = i;
  52. // read arguments
  53. NCDValue *arg_addr1;
  54. NCDValue *arg_addr2;
  55. NCDValue *arg_netprefix;
  56. if (!NCDValue_ListRead(o->i->args, 3, &arg_addr1, &arg_addr2, &arg_netprefix)) {
  57. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  58. goto fail1;
  59. }
  60. if (NCDValue_Type(arg_addr1) != NCDVALUE_STRING || NCDValue_Type(arg_addr2) != NCDVALUE_STRING || NCDValue_Type(arg_netprefix) != NCDVALUE_STRING) {
  61. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  62. goto fail1;
  63. }
  64. // parse
  65. uint32_t addr1;
  66. uint32_t addr2;
  67. int netprefix;
  68. if (!ipaddr_parse_ipv4_addr(NCDValue_StringValue(arg_addr1), &addr1)) {
  69. ModuleLog(o->i, BLOG_ERROR, "wrong addr1");
  70. goto fail1;
  71. }
  72. if (!ipaddr_parse_ipv4_addr(NCDValue_StringValue(arg_addr2), &addr2)) {
  73. ModuleLog(o->i, BLOG_ERROR, "wrong addr2");
  74. goto fail1;
  75. }
  76. if (!ipaddr_parse_ipv4_prefix(NCDValue_StringValue(arg_netprefix), &netprefix)) {
  77. ModuleLog(o->i, BLOG_ERROR, "wrong netprefix");
  78. goto fail1;
  79. }
  80. // test
  81. o->value = ipaddr_ipv4_addrs_in_network(addr1, addr2, netprefix);
  82. // signal up
  83. NCDModuleInst_Backend_Up(o->i);
  84. return;
  85. fail1:
  86. free(o);
  87. fail0:
  88. NCDModuleInst_Backend_SetError(i);
  89. NCDModuleInst_Backend_Dead(i);
  90. }
  91. static void func_die (void *vo)
  92. {
  93. struct instance *o = vo;
  94. NCDModuleInst *i = o->i;
  95. // free instance
  96. free(o);
  97. NCDModuleInst_Backend_Dead(i);
  98. }
  99. static int func_getvar (void *vo, const char *name, NCDValue *out)
  100. {
  101. struct instance *o = vo;
  102. if (!strcmp(name, "")) {
  103. const char *v = (o->value ? "true" : "false");
  104. if (!NCDValue_InitString(out, v)) {
  105. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  106. return 0;
  107. }
  108. return 1;
  109. }
  110. return 0;
  111. }
  112. static const struct NCDModule modules[] = {
  113. {
  114. .type = "ip_in_network",
  115. .func_new = func_new,
  116. .func_die = func_die,
  117. .func_getvar = func_getvar
  118. }, {
  119. .type = NULL
  120. }
  121. };
  122. const struct NCDModuleGroup ncdmodule_ip_in_network = {
  123. .modules = modules
  124. };