net_ipv4_addr.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * @file net_ipv4_addr.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. * IPv4 address module.
  25. *
  26. * Synopsis: net.ipv4.addr(string ifname, string addr, string prefix)
  27. */
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <ncd/NCDModule.h>
  31. #include <ncd/NCDIfConfig.h>
  32. #include <generated/blog_channel_ncd_net_ipv4_addr.h>
  33. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  34. struct instance {
  35. NCDModuleInst *i;
  36. const char *ifname;
  37. struct ipv4_ifaddr ifaddr;
  38. };
  39. static void * func_new (NCDModuleInst *i)
  40. {
  41. // allocate instance
  42. struct instance *o = malloc(sizeof(*o));
  43. if (!o) {
  44. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  45. goto fail0;
  46. }
  47. // init arguments
  48. o->i = i;
  49. // read arguments
  50. NCDValue *ifname_arg;
  51. NCDValue *addr_arg;
  52. NCDValue *prefix_arg;
  53. if (!NCDValue_ListRead(o->i->args, 3, &ifname_arg, &addr_arg, &prefix_arg)) {
  54. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  55. goto fail1;
  56. }
  57. if (NCDValue_Type(ifname_arg) != NCDVALUE_STRING || NCDValue_Type(addr_arg) != NCDVALUE_STRING || NCDValue_Type(prefix_arg) != NCDVALUE_STRING) {
  58. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  59. goto fail1;
  60. }
  61. o->ifname = NCDValue_StringValue(ifname_arg);
  62. if (!ipaddr_parse_ipv4_addr(NCDValue_StringValue(addr_arg), &o->ifaddr.addr)) {
  63. ModuleLog(o->i, BLOG_ERROR, "wrong address");
  64. goto fail1;
  65. }
  66. if (!ipaddr_parse_ipv4_prefix(NCDValue_StringValue(prefix_arg), &o->ifaddr.prefix)) {
  67. ModuleLog(o->i, BLOG_ERROR, "wrong prefix");
  68. goto fail1;
  69. }
  70. // add address
  71. if (!NCDIfConfig_add_ipv4_addr(o->ifname, o->ifaddr)) {
  72. ModuleLog(o->i, BLOG_ERROR, "failed to add IP address");
  73. goto fail1;
  74. }
  75. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  76. return o;
  77. fail1:
  78. free(o);
  79. fail0:
  80. return NULL;
  81. }
  82. static void func_free (void *vo)
  83. {
  84. struct instance *o = vo;
  85. // remove address
  86. if (!NCDIfConfig_remove_ipv4_addr(o->ifname, o->ifaddr)) {
  87. ModuleLog(o->i, BLOG_ERROR, "failed to remove IP address");
  88. }
  89. // free instance
  90. free(o);
  91. }
  92. static const struct NCDModule modules[] = {
  93. {
  94. .type = "net.ipv4.addr",
  95. .func_new = func_new,
  96. .func_free = func_free
  97. }, {
  98. .type = NULL
  99. }
  100. };
  101. const struct NCDModuleGroup ncdmodule_net_ipv4_addr = {
  102. .modules = modules
  103. };