if.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @file if.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. * Conditional module.
  25. *
  26. * Synopsis: if(string cond)
  27. * Description: on initialization, transitions to UP state if cond equals "true", else
  28. * remains in the DOWN state indefinitely.
  29. *
  30. * Synopsis: ifnot(string cond)
  31. * Description: on initialization, transitions to UP state if cond does not equal "true", else
  32. * remains in the DOWN state indefinitely.
  33. */
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <ncd/NCDModule.h>
  37. #include <generated/blog_channel_ncd_if.h>
  38. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  39. struct instance {
  40. NCDModuleInst *i;
  41. };
  42. static void * new_templ (NCDModuleInst *i, int not)
  43. {
  44. // allocate instance
  45. struct instance *o = malloc(sizeof(*o));
  46. if (!o) {
  47. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  48. goto fail0;
  49. }
  50. // init arguments
  51. o->i = i;
  52. // check arguments
  53. NCDValue *arg;
  54. if (!NCDValue_ListRead(i->args, 1, &arg)) {
  55. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  56. goto fail1;
  57. }
  58. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  59. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  60. goto fail1;
  61. }
  62. int c = !strcmp(NCDValue_StringValue(arg), "true");
  63. if ((not && !c) || (!not && c)) {
  64. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  65. }
  66. return o;
  67. fail1:
  68. free(o);
  69. fail0:
  70. return NULL;
  71. }
  72. static void * func_new (NCDModuleInst *i)
  73. {
  74. return new_templ(i, 0);
  75. }
  76. static void * func_new_not (NCDModuleInst *i)
  77. {
  78. return new_templ(i, 1);
  79. }
  80. static void func_free (void *vo)
  81. {
  82. struct instance *o = vo;
  83. // free instance
  84. free(o);
  85. }
  86. static const struct NCDModule modules[] = {
  87. {
  88. .type = "if",
  89. .func_new = func_new,
  90. .func_free = func_free
  91. }, {
  92. .type = "ifnot",
  93. .func_new = func_new_not,
  94. .func_free = func_free
  95. }, {
  96. .type = NULL
  97. }
  98. };
  99. const struct NCDModuleGroup ncdmodule_if = {
  100. .modules = modules
  101. };