ip_in_network.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. // init arguments
  50. o->i = i;
  51. // read arguments
  52. NCDValue *arg_addr1;
  53. NCDValue *arg_addr2;
  54. NCDValue *arg_netprefix;
  55. if (!NCDValue_ListRead(o->i->args, 3, &arg_addr1, &arg_addr2, &arg_netprefix)) {
  56. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  57. goto fail1;
  58. }
  59. if (NCDValue_Type(arg_addr1) != NCDVALUE_STRING || NCDValue_Type(arg_addr2) != NCDVALUE_STRING || NCDValue_Type(arg_netprefix) != NCDVALUE_STRING) {
  60. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  61. goto fail1;
  62. }
  63. // parse
  64. uint32_t addr1;
  65. uint32_t addr2;
  66. int netprefix;
  67. if (!ipaddr_parse_ipv4_addr(NCDValue_StringValue(arg_addr1), &addr1)) {
  68. ModuleLog(o->i, BLOG_ERROR, "wrong addr");
  69. goto fail1;
  70. }
  71. if (!ipaddr_parse_ipv4_addr(NCDValue_StringValue(arg_addr2), &addr2)) {
  72. ModuleLog(o->i, BLOG_ERROR, "wrong netaddr");
  73. goto fail1;
  74. }
  75. if (!ipaddr_parse_ipv4_prefix(NCDValue_StringValue(arg_netprefix), &netprefix)) {
  76. ModuleLog(o->i, BLOG_ERROR, "wrong netprefix");
  77. goto fail1;
  78. }
  79. // test
  80. o->value = ipaddr_ipv4_addrs_in_network(addr1, addr2, netprefix);
  81. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  82. return o;
  83. fail1:
  84. free(o);
  85. fail0:
  86. return NULL;
  87. }
  88. static void func_free (void *vo)
  89. {
  90. struct instance *o = vo;
  91. // free instance
  92. free(o);
  93. }
  94. static int func_getvar (void *vo, const char *name, NCDValue *out)
  95. {
  96. struct instance *o = vo;
  97. if (!strcmp(name, "")) {
  98. const char *v = (o->value ? "true" : "false");
  99. if (!NCDValue_InitString(out, v)) {
  100. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  101. return 0;
  102. }
  103. return 1;
  104. }
  105. return 0;
  106. }
  107. static const struct NCDModule modules[] = {
  108. {
  109. .type = "ip_in_network",
  110. .func_new = func_new,
  111. .func_free = func_free,
  112. .func_getvar = func_getvar
  113. }, {
  114. .type = NULL
  115. }
  116. };
  117. const struct NCDModuleGroup ncdmodule_ip_in_network = {
  118. .modules = modules
  119. };