BDHCPClientCore.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 <random/BRandom2.h>
  41. #include <flow/PacketPassInterface.h>
  42. #include <flow/PacketRecvInterface.h>
  43. #define BDHCPCLIENTCORE_EVENT_UP 1
  44. #define BDHCPCLIENTCORE_EVENT_DOWN 2
  45. #define BDHCPCLIENTCORE_MAX_DOMAIN_NAME_SERVERS 16
  46. typedef void (*BDHCPClientCore_func_getsendermac) (void *user, uint8_t *out_mac);
  47. typedef void (*BDHCPClientCore_handler) (void *user, int event);
  48. struct BDHCPClientCore_opts {
  49. const char *hostname;
  50. const char *vendorclassid;
  51. const uint8_t *clientid;
  52. size_t clientid_len;
  53. };
  54. typedef struct {
  55. PacketPassInterface *send_if;
  56. PacketRecvInterface *recv_if;
  57. uint8_t client_mac_addr[6];
  58. BReactor *reactor;
  59. BRandom2 *random2;
  60. void *user;
  61. BDHCPClientCore_func_getsendermac func_getsendermac;
  62. BDHCPClientCore_handler handler;
  63. char *hostname;
  64. char *vendorclassid;
  65. uint8_t *clientid;
  66. size_t clientid_len;
  67. struct dhcp_header *send_buf;
  68. struct dhcp_header *recv_buf;
  69. int sending;
  70. BTimer reset_timer;
  71. BTimer request_timer;
  72. BTimer renew_timer;
  73. BTimer renew_request_timer;
  74. BTimer lease_timer;
  75. int state;
  76. int request_count;
  77. uint32_t xid;
  78. int xid_reuse_counter;
  79. struct {
  80. uint32_t yiaddr;
  81. uint32_t dhcp_server_identifier;
  82. } offered;
  83. struct {
  84. uint32_t ip_address_lease_time;
  85. uint32_t subnet_mask;
  86. int have_router;
  87. uint32_t router;
  88. int domain_name_servers_count;
  89. uint32_t domain_name_servers[BDHCPCLIENTCORE_MAX_DOMAIN_NAME_SERVERS];
  90. uint8_t server_mac[6];
  91. } acked;
  92. DebugObject d_obj;
  93. } BDHCPClientCore;
  94. int BDHCPClientCore_Init (BDHCPClientCore *o, PacketPassInterface *send_if, PacketRecvInterface *recv_if,
  95. uint8_t *client_mac_addr, struct BDHCPClientCore_opts opts, BReactor *reactor,
  96. BRandom2 *random2, void *user,
  97. BDHCPClientCore_func_getsendermac func_getsendermac,
  98. BDHCPClientCore_handler handler);
  99. void BDHCPClientCore_Free (BDHCPClientCore *o);
  100. void BDHCPClientCore_GetClientIP (BDHCPClientCore *o, uint32_t *out_ip);
  101. void BDHCPClientCore_GetClientMask (BDHCPClientCore *o, uint32_t *out_mask);
  102. int BDHCPClientCore_GetRouter (BDHCPClientCore *o, uint32_t *out_router);
  103. int BDHCPClientCore_GetDNS (BDHCPClientCore *o, uint32_t *out_dns_servers, size_t max_dns_servers);
  104. void BDHCPClientCore_GetServerMAC (BDHCPClientCore *o, uint8_t *out_mac);
  105. #endif