BDHCPClientCore.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * @file BDHCPClientCore.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * DHCP client, excluding system-dependent details.
  32. */
  33. #ifndef BADVPN_DHCPCLIENT_BDHCPCLIENTCORE_H
  34. #define BADVPN_DHCPCLIENT_BDHCPCLIENTCORE_H
  35. #include <stdint.h>
  36. #include <stddef.h>
  37. #include <misc/dhcp_proto.h>
  38. #include <system/BReactor.h>
  39. #include <base/DebugObject.h>
  40. #include <flow/PacketPassInterface.h>
  41. #include <flow/PacketRecvInterface.h>
  42. #define BDHCPCLIENTCORE_EVENT_UP 1
  43. #define BDHCPCLIENTCORE_EVENT_DOWN 2
  44. #define BDHCPCLIENTCORE_MAX_DOMAIN_NAME_SERVERS 16
  45. typedef void (*BDHCPClientCore_func_getsendermac) (void *user, uint8_t *out_mac);
  46. typedef void (*BDHCPClientCore_handler) (void *user, int event);
  47. struct BDHCPClientCore_opts {
  48. const char *hostname;
  49. const char *vendorclassid;
  50. const uint8_t *clientid;
  51. size_t clientid_len;
  52. };
  53. typedef struct {
  54. PacketPassInterface *send_if;
  55. PacketRecvInterface *recv_if;
  56. uint8_t client_mac_addr[6];
  57. BReactor *reactor;
  58. void *user;
  59. BDHCPClientCore_func_getsendermac func_getsendermac;
  60. BDHCPClientCore_handler handler;
  61. char *hostname;
  62. char *vendorclassid;
  63. uint8_t *clientid;
  64. size_t clientid_len;
  65. struct dhcp_header *send_buf;
  66. struct dhcp_header *recv_buf;
  67. int sending;
  68. BTimer reset_timer;
  69. BTimer request_timer;
  70. BTimer renew_timer;
  71. BTimer renew_request_timer;
  72. BTimer lease_timer;
  73. int state;
  74. int request_count;
  75. uint32_t xid;
  76. int xid_reuse_counter;
  77. struct {
  78. uint32_t yiaddr;
  79. uint32_t dhcp_server_identifier;
  80. } offered;
  81. struct {
  82. uint32_t ip_address_lease_time;
  83. uint32_t subnet_mask;
  84. int have_router;
  85. uint32_t router;
  86. int domain_name_servers_count;
  87. uint32_t domain_name_servers[BDHCPCLIENTCORE_MAX_DOMAIN_NAME_SERVERS];
  88. uint8_t server_mac[6];
  89. } acked;
  90. DebugObject d_obj;
  91. } BDHCPClientCore;
  92. int BDHCPClientCore_Init (BDHCPClientCore *o, PacketPassInterface *send_if, PacketRecvInterface *recv_if, uint8_t *client_mac_addr, struct BDHCPClientCore_opts opts, BReactor *reactor, void *user,
  93. BDHCPClientCore_func_getsendermac func_getsendermac,
  94. BDHCPClientCore_handler handler);
  95. void BDHCPClientCore_Free (BDHCPClientCore *o);
  96. void BDHCPClientCore_GetClientIP (BDHCPClientCore *o, uint32_t *out_ip);
  97. void BDHCPClientCore_GetClientMask (BDHCPClientCore *o, uint32_t *out_mask);
  98. int BDHCPClientCore_GetRouter (BDHCPClientCore *o, uint32_t *out_router);
  99. int BDHCPClientCore_GetDNS (BDHCPClientCore *o, uint32_t *out_dns_servers, size_t max_dns_servers);
  100. void BDHCPClientCore_GetServerMAC (BDHCPClientCore *o, uint8_t *out_mac);
  101. #endif