if.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. NCDModuleInst_Backend_SetUser(i, o);
  51. // init arguments
  52. o->i = i;
  53. // check arguments
  54. NCDValue *arg;
  55. if (!NCDValue_ListRead(i->args, 1, &arg)) {
  56. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  57. goto fail1;
  58. }
  59. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  60. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  61. goto fail1;
  62. }
  63. // compute logical value of argument
  64. int c = !strcmp(NCDValue_StringValue(arg), "true");
  65. // signal up if needed
  66. if ((not && !c) || (!not && c)) {
  67. NCDModuleInst_Backend_Up(o->i);
  68. }
  69. return;
  70. fail1:
  71. free(o);
  72. fail0:
  73. NCDModuleInst_Backend_SetError(i);
  74. NCDModuleInst_Backend_Dead(i);
  75. }
  76. static void func_new (NCDModuleInst *i)
  77. {
  78. new_templ(i, 0);
  79. }
  80. static void func_new_not (NCDModuleInst *i)
  81. {
  82. new_templ(i, 1);
  83. }
  84. static void func_die (void *vo)
  85. {
  86. struct instance *o = vo;
  87. NCDModuleInst *i = o->i;
  88. // free instance
  89. free(o);
  90. NCDModuleInst_Backend_Dead(i);
  91. }
  92. static const struct NCDModule modules[] = {
  93. {
  94. .type = "if",
  95. .func_new = func_new,
  96. .func_die = func_die
  97. }, {
  98. .type = "ifnot",
  99. .func_new = func_new_not,
  100. .func_die = func_die
  101. }, {
  102. .type = NULL
  103. }
  104. };
  105. const struct NCDModuleGroup ncdmodule_if = {
  106. .modules = modules
  107. };