alias.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * @file alias.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. * Synopsis:
  25. * alias(string target)
  26. *
  27. * Variables and objects:
  28. * - empty name - resolves target
  29. * - nonempty name N - resolves target.N
  30. */
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <misc/concat_strings.h>
  34. #include <ncd/NCDModule.h>
  35. #include <generated/blog_channel_ncd_alias.h>
  36. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  37. struct instance {
  38. NCDModuleInst *i;
  39. char *target;
  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 *target_arg;
  54. if (!NCDValue_ListRead(o->i->args, 1, &target_arg)) {
  55. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  56. goto fail1;
  57. }
  58. if (NCDValue_Type(target_arg) != NCDVALUE_STRING) {
  59. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  60. goto fail1;
  61. }
  62. o->target = NCDValue_StringValue(target_arg);
  63. // signal up
  64. NCDModuleInst_Backend_Up(o->i);
  65. return;
  66. fail1:
  67. free(o);
  68. fail0:
  69. NCDModuleInst_Backend_SetError(i);
  70. NCDModuleInst_Backend_Dead(i);
  71. }
  72. static void func_die (void *vo)
  73. {
  74. struct instance *o = vo;
  75. NCDModuleInst *i = o->i;
  76. // free instance
  77. free(o);
  78. NCDModuleInst_Backend_Dead(i);
  79. }
  80. static int func_getvar (void *vo, const char *name, NCDValue *out)
  81. {
  82. struct instance *o = vo;
  83. if (!strcmp(name, "")) {
  84. return NCDModuleInst_Backend_GetVar(o->i, o->target, out);
  85. }
  86. char *target_name = concat_strings(3, o->target, ".", name);
  87. if (!target_name) {
  88. ModuleLog(o->i, BLOG_ERROR, "concat_strings failed");
  89. return 0;
  90. }
  91. int res = NCDModuleInst_Backend_GetVar(o->i, target_name, out);
  92. free(target_name);
  93. return res;
  94. }
  95. static NCDModuleInst * func_getobj (void *vo, const char *name)
  96. {
  97. struct instance *o = vo;
  98. if (!strcmp(name, "")) {
  99. return NCDModuleInst_Backend_GetObj(o->i, o->target);
  100. }
  101. char *target_name = concat_strings(3, o->target, ".", name);
  102. if (!target_name) {
  103. ModuleLog(o->i, BLOG_ERROR, "concat_strings failed");
  104. return NULL;
  105. }
  106. NCDModuleInst *res = NCDModuleInst_Backend_GetObj(o->i, target_name);
  107. free(target_name);
  108. return res;
  109. }
  110. static const struct NCDModule modules[] = {
  111. {
  112. .type = "alias",
  113. .func_new = func_new,
  114. .func_die = func_die,
  115. .func_getvar = func_getvar,
  116. .func_getobj = func_getobj
  117. }, {
  118. .type = NULL
  119. }
  120. };
  121. const struct NCDModuleGroup ncdmodule_alias = {
  122. .modules = modules
  123. };