net_ipv4_dhcp.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * @file net_ipv4_dhcp.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. * Physical network interface module.
  25. *
  26. * Synopsis: net.ipv4.dhcp(string ifname)
  27. * Variables:
  28. * string addr - assigned IP address ("A.B.C.D")
  29. * string prefix - address prefix length ("N")
  30. * string gateway - router address ("A.B.C.D")
  31. * list(string) dns_servers - DNS server addresses ("A.B.C.D" ...)
  32. */
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <stdio.h>
  36. #include <misc/debug.h>
  37. #include <misc/ipaddr.h>
  38. #include <dhcpclient/BDHCPClient.h>
  39. #include <ncd/NCDModule.h>
  40. #include <generated/blog_channel_ncd_net_ipv4_dhcp.h>
  41. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  42. struct instance {
  43. NCDModuleInst *i;
  44. BDHCPClient dhcp;
  45. };
  46. static void dhcp_handler (struct instance *o, int event)
  47. {
  48. switch (event) {
  49. case BDHCPCLIENT_EVENT_UP:
  50. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  51. break;
  52. case BDHCPCLIENT_EVENT_DOWN:
  53. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_DOWN);
  54. break;
  55. default:
  56. ASSERT(0);
  57. }
  58. }
  59. static void * func_new (NCDModuleInst *i)
  60. {
  61. // allocate instance
  62. struct instance *o = malloc(sizeof(*o));
  63. if (!o) {
  64. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  65. goto fail0;
  66. }
  67. // init arguments
  68. o->i = i;
  69. // check arguments
  70. NCDValue *arg;
  71. if (!NCDValue_ListRead(o->i->args, 1, &arg)) {
  72. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  73. goto fail1;
  74. }
  75. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  76. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  77. goto fail1;
  78. }
  79. char *ifname = NCDValue_StringValue(arg);
  80. // init DHCP
  81. if (!BDHCPClient_Init(&o->dhcp, ifname, o->i->reactor, (BDHCPClient_handler)dhcp_handler, o)) {
  82. ModuleLog(o->i, BLOG_ERROR, "BDHCPClient_Init failed");
  83. goto fail1;
  84. }
  85. return o;
  86. fail1:
  87. free(o);
  88. fail0:
  89. return NULL;
  90. }
  91. static void func_free (void *vo)
  92. {
  93. struct instance *o = vo;
  94. // free DHCP
  95. BDHCPClient_Free(&o->dhcp);
  96. // free instance
  97. free(o);
  98. }
  99. static int func_getvar (void *vo, const char *name, NCDValue *out)
  100. {
  101. struct instance *o = vo;
  102. if (!strcmp(name, "addr")) {
  103. uint32_t addr;
  104. BDHCPClient_GetClientIP(&o->dhcp, &addr);
  105. uint8_t *b = (uint8_t *)&addr;
  106. char str[50];
  107. sprintf(str, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, b[0], b[1], b[2], b[3]);
  108. if (!NCDValue_InitString(out, str)) {
  109. return 0;
  110. }
  111. return 1;
  112. }
  113. if (!strcmp(name, "prefix")) {
  114. uint32_t addr;
  115. BDHCPClient_GetClientIP(&o->dhcp, &addr);
  116. uint32_t mask;
  117. BDHCPClient_GetClientMask(&o->dhcp, &mask);
  118. struct ipv4_ifaddr ifaddr;
  119. if (!ipaddr_ipv4_ifaddr_from_addr_mask(addr, mask, &ifaddr)) {
  120. return 0;
  121. }
  122. char str[10];
  123. sprintf(str, "%d", ifaddr.prefix);
  124. if (!NCDValue_InitString(out, str)) {
  125. return 0;
  126. }
  127. return 1;
  128. }
  129. if (!strcmp(name, "gateway")) {
  130. uint32_t addr;
  131. if (!BDHCPClient_GetRouter(&o->dhcp, &addr)) {
  132. return 0;
  133. }
  134. uint8_t *b = (uint8_t *)&addr;
  135. char str[50];
  136. sprintf(str, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, b[0], b[1], b[2], b[3]);
  137. if (!NCDValue_InitString(out, str)) {
  138. return 0;
  139. }
  140. return 1;
  141. }
  142. if (!strcmp(name, "dns_servers")) {
  143. uint32_t servers[BDHCPCLIENT_MAX_DOMAIN_NAME_SERVERS];
  144. int num_servers = BDHCPClient_GetDNS(&o->dhcp, servers, BDHCPCLIENT_MAX_DOMAIN_NAME_SERVERS);
  145. NCDValue list;
  146. NCDValue_InitList(&list);
  147. for (int i = 0; i < num_servers; i++) {
  148. uint8_t *b = (uint8_t *)&servers[i];
  149. char str[50];
  150. sprintf(str, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, b[0], b[1], b[2], b[3]);
  151. NCDValue server;
  152. if (!NCDValue_InitString(&server, str)) {
  153. goto fail1;
  154. }
  155. if (!NCDValue_ListAppend(&list, server)) {
  156. NCDValue_Free(&server);
  157. goto fail1;
  158. }
  159. }
  160. *out = list;
  161. return 1;
  162. fail1:
  163. NCDValue_Free(&list);
  164. return 0;
  165. }
  166. return 0;
  167. }
  168. static const struct NCDModule modules[] = {
  169. {
  170. .type = "net.ipv4.dhcp",
  171. .func_new = func_new,
  172. .func_free = func_free,
  173. .func_getvar = func_getvar
  174. }, {
  175. .type = NULL
  176. }
  177. };
  178. const struct NCDModuleGroup ncdmodule_net_ipv4_dhcp = {
  179. .modules = modules
  180. };