BDHCPClient.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /**
  2. * @file BDHCPClient.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. #include <stdio.h>
  23. #include <unistd.h>
  24. #include <sys/socket.h>
  25. #include <net/if.h>
  26. #include <net/if_arp.h>
  27. #include <sys/ioctl.h>
  28. #include <misc/debug.h>
  29. #include <misc/byteorder.h>
  30. #include <misc/ethernet_proto.h>
  31. #include <misc/ipv4_proto.h>
  32. #include <misc/udp_proto.h>
  33. #include <system/BLog.h>
  34. #include <dhcpclient/BDHCPClient.h>
  35. #include <generated/blog_channel_BDHCPClient.h>
  36. #define COMPONENT_SOURCE 1
  37. #define COMPONENT_SINK 2
  38. #define DHCP_SERVER_PORT 67
  39. #define DHCP_CLIENT_PORT 68
  40. #define IPUDP_OVERHEAD (sizeof(struct ipv4_header) + sizeof(struct udp_header))
  41. static void error_handler (BDHCPClient *o, int component, const void *data)
  42. {
  43. DebugObject_Access(&o->d_obj);
  44. switch (component) {
  45. case COMPONENT_SOURCE: {
  46. BLog(BLOG_ERROR, "source error");
  47. } break;
  48. case COMPONENT_SINK: {
  49. BLog(BLOG_ERROR, "sink error");
  50. } break;
  51. default:
  52. ASSERT(0);
  53. }
  54. }
  55. static void dhcp_handler (BDHCPClient *o, int event)
  56. {
  57. DebugObject_Access(&o->d_obj);
  58. switch (event) {
  59. case BDHCPCLIENTCORE_EVENT_UP:
  60. ASSERT(!o->up)
  61. o->up = 1;
  62. o->handler(o->user, BDHCPCLIENT_EVENT_UP);
  63. return;
  64. case BDHCPCLIENTCORE_EVENT_DOWN:
  65. ASSERT(o->up)
  66. o->up = 0;
  67. o->handler(o->user, BDHCPCLIENT_EVENT_DOWN);
  68. return;
  69. default:
  70. ASSERT(0);
  71. }
  72. }
  73. static int get_iface_info (const char *ifname, uint8_t *out_mac, int *out_mtu, int *out_ifindex)
  74. {
  75. struct ifreq ifr;
  76. int s = socket(AF_INET, SOCK_DGRAM, 0);
  77. if (!s) {
  78. BLog(BLOG_ERROR, "socket failed");
  79. goto fail0;
  80. }
  81. // get MAC
  82. memset(&ifr, 0, sizeof(ifr));
  83. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname);
  84. if (ioctl(s, SIOCGIFHWADDR, &ifr)) {
  85. BLog(BLOG_ERROR, "ioctl(SIOCGIFHWADDR) failed");
  86. goto fail1;
  87. }
  88. if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
  89. BLog(BLOG_ERROR, "hardware address not ethernet");
  90. goto fail1;
  91. }
  92. memcpy(out_mac, ifr.ifr_hwaddr.sa_data, 6);
  93. // get MTU
  94. memset(&ifr, 0, sizeof(ifr));
  95. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname);
  96. if (ioctl(s, SIOCGIFMTU, &ifr)) {
  97. BLog(BLOG_ERROR, "ioctl(SIOCGIFMTU) failed");
  98. goto fail1;
  99. }
  100. *out_mtu = ifr.ifr_mtu;
  101. // get interface index
  102. memset(&ifr, 0, sizeof(ifr));
  103. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname);
  104. if (ioctl(s, SIOCGIFINDEX, &ifr)) {
  105. BLog(BLOG_ERROR, "ioctl(SIOCGIFINDEX) failed");
  106. goto fail1;
  107. }
  108. *out_ifindex = ifr.ifr_ifindex;
  109. close(s);
  110. return 1;
  111. fail1:
  112. close(s);
  113. fail0:
  114. return 0;
  115. }
  116. int BDHCPClient_Init (BDHCPClient *o, const char *ifname, BReactor *reactor, BDHCPClient_handler handler, void *user)
  117. {
  118. // init arguments
  119. o->reactor = reactor;
  120. o->handler = handler;
  121. o->user = user;
  122. // get interface information
  123. uint8_t if_mac[6];
  124. int if_mtu;
  125. int if_index;
  126. if (!get_iface_info(ifname, if_mac, &if_mtu, &if_index)) {
  127. BLog(BLOG_ERROR, "failed to get interface information");
  128. goto fail0;
  129. }
  130. BLog(BLOG_INFO, "if_mac=%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8" if_mtu=%d if_index=%d",
  131. if_mac[0], if_mac[1], if_mac[2], if_mac[3], if_mac[4], if_mac[5], if_mtu, if_index);
  132. if (if_mtu < IPUDP_OVERHEAD) {
  133. BLog(BLOG_ERROR, "MTU is too small for UDP/IP !?!");
  134. goto fail0;
  135. }
  136. int dhcp_mtu = if_mtu - IPUDP_OVERHEAD;
  137. // init socket
  138. if (BSocket_Init(&o->sock, o->reactor, BADDR_TYPE_PACKET, BSOCKET_TYPE_DGRAM) < 0) {
  139. BLog(BLOG_ERROR, "BSocket_Init failed");
  140. goto fail0;
  141. }
  142. // bind socket
  143. BAddr bind_addr;
  144. BAddr_InitPacket(&bind_addr, hton16(ETHERTYPE_IPV4), if_index, BADDR_PACKET_HEADER_TYPE_ETHERNET, BADDR_PACKET_PACKET_TYPE_HOST, if_mac);
  145. if (BSocket_Bind(&o->sock, &bind_addr) < 0) {
  146. BLog(BLOG_ERROR, "BSocket_Bind failed");
  147. goto fail1;
  148. }
  149. // init error handler
  150. FlowErrorDomain_Init(&o->domain, (FlowErrorDomain_handler)error_handler, o);
  151. // init sending
  152. // init sink
  153. BAddr dest_addr;
  154. uint8_t broadcast_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  155. BAddr_InitPacket(&dest_addr, hton16(ETHERTYPE_IPV4), if_index, BADDR_PACKET_HEADER_TYPE_ETHERNET, BADDR_PACKET_PACKET_TYPE_BROADCAST, broadcast_mac);
  156. BIPAddr local_addr;
  157. BIPAddr_InitInvalid(&local_addr);
  158. DatagramSocketSink_Init(&o->send_sink, FlowErrorReporter_Create(&o->domain, COMPONENT_SINK), &o->sock, if_mtu, dest_addr, local_addr, BReactor_PendingGroup(o->reactor));
  159. // init copier
  160. PacketCopier_Init(&o->send_copier, dhcp_mtu, BReactor_PendingGroup(o->reactor));
  161. // init encoder
  162. DHCPIpUdpEncoder_Init(&o->send_encoder, PacketCopier_GetOutput(&o->send_copier), BReactor_PendingGroup(o->reactor));
  163. // init buffer
  164. if (!SinglePacketBuffer_Init(&o->send_buffer, DHCPIpUdpEncoder_GetOutput(&o->send_encoder), DatagramSocketSink_GetInput(&o->send_sink), BReactor_PendingGroup(o->reactor))) {
  165. BLog(BLOG_ERROR, "SinglePacketBuffer_Init failed");
  166. goto fail2;
  167. }
  168. // init receiving
  169. // init source
  170. DatagramSocketSource_Init(&o->recv_source, FlowErrorReporter_Create(&o->domain, COMPONENT_SOURCE), &o->sock, if_mtu, BReactor_PendingGroup(o->reactor));
  171. // init copier
  172. PacketCopier_Init(&o->recv_copier, dhcp_mtu, BReactor_PendingGroup(o->reactor));
  173. // init decoder
  174. DHCPIpUdpDecoder_Init(&o->recv_decoder, PacketCopier_GetInput(&o->recv_copier), BReactor_PendingGroup(o->reactor));
  175. // init buffer
  176. if (!SinglePacketBuffer_Init(&o->recv_buffer, DatagramSocketSource_GetOutput(&o->recv_source), DHCPIpUdpDecoder_GetInput(&o->recv_decoder), BReactor_PendingGroup(o->reactor))) {
  177. BLog(BLOG_ERROR, "SinglePacketBuffer_Init failed");
  178. goto fail3;
  179. }
  180. // init dhcp
  181. if (!BDHCPClientCore_Init(&o->dhcp, PacketCopier_GetInput(&o->send_copier), PacketCopier_GetOutput(&o->recv_copier), if_mac, o->reactor, (BDHCPClientCore_handler)dhcp_handler, o)) {
  182. BLog(BLOG_ERROR, "BDHCPClientCore_Init failed");
  183. goto fail4;
  184. }
  185. // set not up
  186. o->up = 0;
  187. DebugObject_Init(&o->d_obj);
  188. return 1;
  189. fail4:
  190. SinglePacketBuffer_Free(&o->recv_buffer);
  191. fail3:
  192. DHCPIpUdpDecoder_Free(&o->recv_decoder);
  193. PacketCopier_Free(&o->recv_copier);
  194. DatagramSocketSource_Free(&o->recv_source);
  195. SinglePacketBuffer_Free(&o->send_buffer);
  196. fail2:
  197. DHCPIpUdpEncoder_Free(&o->send_encoder);
  198. PacketCopier_Free(&o->send_copier);
  199. DatagramSocketSink_Free(&o->send_sink);
  200. fail1:
  201. BSocket_Free(&o->sock);
  202. fail0:
  203. return 0;
  204. }
  205. void BDHCPClient_Free (BDHCPClient *o)
  206. {
  207. DebugObject_Free(&o->d_obj);
  208. // free dhcp
  209. BDHCPClientCore_Free(&o->dhcp);
  210. // free receiving
  211. SinglePacketBuffer_Free(&o->recv_buffer);
  212. DHCPIpUdpDecoder_Free(&o->recv_decoder);
  213. PacketCopier_Free(&o->recv_copier);
  214. DatagramSocketSource_Free(&o->recv_source);
  215. // free sending
  216. SinglePacketBuffer_Free(&o->send_buffer);
  217. DHCPIpUdpEncoder_Free(&o->send_encoder);
  218. PacketCopier_Free(&o->send_copier);
  219. DatagramSocketSink_Free(&o->send_sink);
  220. // free socket
  221. BSocket_Free(&o->sock);
  222. }
  223. int BDHCPClient_IsUp (BDHCPClient *o)
  224. {
  225. DebugObject_Access(&o->d_obj);
  226. return o->up;
  227. }
  228. void BDHCPClient_GetClientIP (BDHCPClient *o, uint32_t *out_ip)
  229. {
  230. DebugObject_Access(&o->d_obj);
  231. ASSERT(o->up)
  232. BDHCPClientCore_GetClientIP(&o->dhcp, out_ip);
  233. }
  234. void BDHCPClient_GetClientMask (BDHCPClient *o, uint32_t *out_mask)
  235. {
  236. DebugObject_Access(&o->d_obj);
  237. ASSERT(o->up)
  238. BDHCPClientCore_GetClientMask(&o->dhcp, out_mask);
  239. }
  240. int BDHCPClient_GetRouter (BDHCPClient *o, uint32_t *out_router)
  241. {
  242. DebugObject_Access(&o->d_obj);
  243. ASSERT(o->up)
  244. return BDHCPClientCore_GetRouter(&o->dhcp, out_router);
  245. }
  246. int BDHCPClient_GetDNS (BDHCPClient *o, uint32_t *out_dns_servers, size_t max_dns_servers)
  247. {
  248. DebugObject_Access(&o->d_obj);
  249. ASSERT(o->up)
  250. return BDHCPClientCore_GetDNS(&o->dhcp, out_dns_servers, max_dns_servers);
  251. }