dns_cache.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*************************************************************************
  2. *
  3. * Copyright (C) 2018-2023 Ruilin Peng (Nick) <[email protected]>.
  4. *
  5. * smartdns is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * smartdns is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef _SMARTDNS_CACHE_H
  19. #define _SMARTDNS_CACHE_H
  20. #include "atomic.h"
  21. #include "dns.h"
  22. #include "dns_conf.h"
  23. #include "hash.h"
  24. #include "hashtable.h"
  25. #include "list.h"
  26. #include <stdlib.h>
  27. #include <time.h>
  28. #ifdef __cpluscplus
  29. extern "C" {
  30. #endif
  31. #define DNS_CACHE_TTL_MIN 1
  32. #define DNS_CACHE_VERSION_LEN 32
  33. #define DNS_CACHE_GROUP_NAME_LEN 32
  34. #define MAGIC_NUMBER 0x6548634163536e44
  35. #define MAGIC_CACHE_DATA 0x44615461
  36. enum CACHE_TYPE {
  37. CACHE_TYPE_NONE,
  38. CACHE_TYPE_ADDR,
  39. CACHE_TYPE_PACKET,
  40. };
  41. enum CACHE_RECORD_TYPE {
  42. CACHE_RECORD_TYPE_ACTIVE,
  43. CACHE_RECORD_TYPE_INACTIVE,
  44. };
  45. struct dns_cache_data_head {
  46. enum CACHE_TYPE cache_type;
  47. int is_soa;
  48. ssize_t size;
  49. };
  50. struct dns_cache_data {
  51. struct dns_cache_data_head head;
  52. unsigned char data[0];
  53. };
  54. struct dns_cache_addr {
  55. struct dns_cache_data_head head;
  56. struct dns_cache_addr_data {
  57. unsigned int cname_ttl;
  58. char soa;
  59. char cname[DNS_MAX_CNAME_LEN];
  60. union {
  61. unsigned char ipv4_addr[DNS_RR_A_LEN];
  62. unsigned char ipv6_addr[DNS_RR_AAAA_LEN];
  63. unsigned char addr[0];
  64. };
  65. } addr_data;
  66. };
  67. struct dns_cache_packet {
  68. struct dns_cache_data_head head;
  69. unsigned char data[0];
  70. };
  71. struct dns_cache_info {
  72. char domain[DNS_MAX_CNAME_LEN];
  73. dns_type_t qtype;
  74. char dns_group_name[DNS_GROUP_NAME_LEN];
  75. uint32_t query_flag;
  76. int ttl;
  77. int hitnum;
  78. int speed;
  79. int no_inactive;
  80. int hitnum_update_add;
  81. time_t insert_time;
  82. time_t replace_time;
  83. };
  84. struct dns_cache_record {
  85. uint32_t magic;
  86. enum CACHE_RECORD_TYPE type;
  87. struct dns_cache_info info;
  88. };
  89. struct dns_cache {
  90. struct hlist_node node;
  91. struct list_head list;
  92. struct list_head check_list;
  93. atomic_t ref;
  94. int del_pending;
  95. struct dns_cache_info info;
  96. struct dns_cache_data *cache_data;
  97. };
  98. struct dns_cache_file {
  99. uint64_t magic;
  100. char version[DNS_CACHE_VERSION_LEN];
  101. uint32_t cache_number;
  102. };
  103. struct dns_cache_key {
  104. const char *domain;
  105. dns_type_t qtype;
  106. const char *dns_group_name;
  107. uint32_t query_flag;
  108. };
  109. enum CACHE_TYPE dns_cache_data_type(struct dns_cache_data *cache_data);
  110. uint32_t dns_cache_get_query_flag(struct dns_cache *dns_cache);
  111. const char *dns_cache_get_dns_group_name(struct dns_cache *dns_cache);
  112. void dns_cache_data_free(struct dns_cache_data *data);
  113. struct dns_cache_data *dns_cache_new_data_packet(void *packet, size_t packet_len);
  114. int dns_cache_init(int size, int enable_inactive, int inactive_list_expired);
  115. int dns_cache_replace(struct dns_cache_key *key, int ttl, int speed, int no_inactive, struct dns_cache_data *cache_data);
  116. int dns_cache_replace_inactive(struct dns_cache_key *key, int ttl, int speed, int no_inactive, struct dns_cache_data *cache_data);
  117. int dns_cache_insert(struct dns_cache_key *key, int ttl, int speed, int no_inactive, struct dns_cache_data *cache_data);
  118. struct dns_cache *dns_cache_lookup(struct dns_cache_key *key);
  119. void dns_cache_delete(struct dns_cache *dns_cache);
  120. void dns_cache_get(struct dns_cache *dns_cache);
  121. void dns_cache_release(struct dns_cache *dns_cache);
  122. int dns_cache_hitnum_dec_get(struct dns_cache *dns_cache);
  123. void dns_cache_update(struct dns_cache *dns_cache);
  124. typedef void dns_cache_callback(struct dns_cache *dns_cache);
  125. void dns_cache_invalidate(dns_cache_callback precallback, int ttl_pre, unsigned int max_callback_num,
  126. dns_cache_callback inactive_precallback, int ttl_inactive_pre);
  127. int dns_cache_get_ttl(struct dns_cache *dns_cache);
  128. int dns_cache_get_cname_ttl(struct dns_cache *dns_cache);
  129. int dns_cache_is_soa(struct dns_cache *dns_cache);
  130. struct dns_cache_data *dns_cache_new_data(void);
  131. struct dns_cache_data *dns_cache_get_data(struct dns_cache *dns_cache);
  132. void dns_cache_set_data_addr(struct dns_cache_data *dns_cache, char *cname, int cname_ttl, unsigned char *addr,
  133. int addr_len);
  134. void dns_cache_set_data_soa(struct dns_cache_data *dns_cache, char *cname, int cname_ttl);
  135. void dns_cache_destroy(void);
  136. int dns_cache_load(const char *file);
  137. int dns_cache_save(const char *file);
  138. #ifdef __cpluscplus
  139. }
  140. #endif
  141. #endif // !_SMARTDNS_CACHE_H