dns_cache.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. /*************************************************************************
  2. *
  3. * Copyright (C) 2018-2020 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. #include "dns_cache.h"
  19. #include "stringutil.h"
  20. #include "tlog.h"
  21. #include <errno.h>
  22. #include <fcntl.h>
  23. #include <pthread.h>
  24. #include <string.h>
  25. #include <sys/types.h>
  26. #define DNS_CACHE_MAX_HITNUM 5000
  27. #define DNS_CACHE_HITNUM_STEP 2
  28. #define DNS_CACHE_HITNUM_STEP_MAX 6
  29. struct dns_cache_head {
  30. DECLARE_HASHTABLE(cache_hash, 10);
  31. struct list_head cache_list;
  32. struct list_head inactive_list;
  33. atomic_t num;
  34. int size;
  35. int enable_inactive;
  36. int inactive_list_expired;
  37. pthread_mutex_t lock;
  38. };
  39. static struct dns_cache_head dns_cache_head;
  40. int dns_cache_init(int size, int enable_inactive, int inactive_list_expired)
  41. {
  42. INIT_LIST_HEAD(&dns_cache_head.cache_list);
  43. INIT_LIST_HEAD(&dns_cache_head.inactive_list);
  44. hash_init(dns_cache_head.cache_hash);
  45. atomic_set(&dns_cache_head.num, 0);
  46. dns_cache_head.size = size;
  47. dns_cache_head.enable_inactive = enable_inactive;
  48. dns_cache_head.inactive_list_expired = inactive_list_expired;
  49. pthread_mutex_init(&dns_cache_head.lock, NULL);
  50. return 0;
  51. }
  52. static __attribute__((unused)) struct dns_cache *_dns_cache_last(void)
  53. {
  54. struct dns_cache *dns_cache = NULL;
  55. dns_cache = list_last_entry(&dns_cache_head.inactive_list, struct dns_cache, list);
  56. if (dns_cache) {
  57. return dns_cache;
  58. }
  59. return list_last_entry(&dns_cache_head.cache_list, struct dns_cache, list);
  60. }
  61. static struct dns_cache *_dns_inactive_cache_first(void)
  62. {
  63. struct dns_cache *dns_cache = NULL;
  64. dns_cache = list_first_entry_or_null(&dns_cache_head.inactive_list, struct dns_cache, list);
  65. if (dns_cache) {
  66. return dns_cache;
  67. }
  68. return list_first_entry_or_null(&dns_cache_head.cache_list, struct dns_cache, list);
  69. }
  70. static void _dns_cache_delete(struct dns_cache *dns_cache)
  71. {
  72. hash_del(&dns_cache->node);
  73. list_del_init(&dns_cache->list);
  74. atomic_dec(&dns_cache_head.num);
  75. dns_cache_data_free(dns_cache->cache_data);
  76. free(dns_cache);
  77. }
  78. void dns_cache_get(struct dns_cache *dns_cache)
  79. {
  80. if (atomic_inc_return(&dns_cache->ref) == 1) {
  81. tlog(TLOG_ERROR, "BUG: dns_cache is invalid.");
  82. return;
  83. }
  84. }
  85. void dns_cache_release(struct dns_cache *dns_cache)
  86. {
  87. if (dns_cache == NULL) {
  88. return;
  89. }
  90. if (!atomic_dec_and_test(&dns_cache->ref)) {
  91. return;
  92. }
  93. _dns_cache_delete(dns_cache);
  94. }
  95. static void _dns_cache_remove(struct dns_cache *dns_cache)
  96. {
  97. hash_del(&dns_cache->node);
  98. list_del_init(&dns_cache->list);
  99. dns_cache_release(dns_cache);
  100. }
  101. static void _dns_cache_move_inactive(struct dns_cache *dns_cache)
  102. {
  103. list_del_init(&dns_cache->list);
  104. list_add_tail(&dns_cache->list, &dns_cache_head.inactive_list);
  105. }
  106. enum CACHE_TYPE dns_cache_data_type(struct dns_cache_data *cache_data)
  107. {
  108. return cache_data->head.cache_type;
  109. }
  110. uint32_t dns_cache_get_cache_flag(struct dns_cache_data *cache_data)
  111. {
  112. return cache_data->head.cache_flag;
  113. }
  114. void dns_cache_data_free(struct dns_cache_data *data)
  115. {
  116. if (data == NULL) {
  117. return;
  118. }
  119. free(data);
  120. }
  121. struct dns_cache_data *dns_cache_new_data(void)
  122. {
  123. struct dns_cache_addr *cache_addr = malloc(sizeof(struct dns_cache_addr));
  124. memset(cache_addr, 0, sizeof(struct dns_cache_addr));
  125. if (cache_addr == NULL) {
  126. return NULL;
  127. }
  128. cache_addr->head.cache_type = CACHE_TYPE_NONE;
  129. cache_addr->head.size = sizeof(struct dns_cache_addr) - sizeof(struct dns_cache_data_head);
  130. return (struct dns_cache_data *)cache_addr;
  131. }
  132. void dns_cache_set_data_soa(struct dns_cache_data *dns_cache, int32_t cache_flag, char *cname, int cname_ttl)
  133. {
  134. if (dns_cache == NULL) {
  135. goto errout;
  136. }
  137. dns_cache->head.is_soa = 1;
  138. if (dns_cache->head.cache_type == CACHE_TYPE_PACKET) {
  139. return;
  140. }
  141. struct dns_cache_addr *cache_addr = (struct dns_cache_addr *)dns_cache;
  142. if (cache_addr == NULL) {
  143. goto errout;
  144. }
  145. memset(cache_addr->addr_data.addr, 0, sizeof(cache_addr->addr_data.addr));
  146. if (cname) {
  147. safe_strncpy(cache_addr->addr_data.cname, cname, DNS_MAX_CNAME_LEN);
  148. cache_addr->addr_data.cname_ttl = cname_ttl;
  149. }
  150. cache_addr->head.cache_flag = cache_flag;
  151. cache_addr->addr_data.soa = 1;
  152. cache_addr->head.cache_type = CACHE_TYPE_ADDR;
  153. cache_addr->head.size = sizeof(struct dns_cache_addr) - sizeof(struct dns_cache_data_head);
  154. errout:
  155. return;
  156. }
  157. void dns_cache_set_data_addr(struct dns_cache_data *dns_cache, uint32_t cache_flag, char *cname, int cname_ttl,
  158. unsigned char *addr, int addr_len)
  159. {
  160. if (dns_cache == NULL) {
  161. goto errout;
  162. }
  163. struct dns_cache_addr *cache_addr = (struct dns_cache_addr *)dns_cache;
  164. if (cache_addr == NULL) {
  165. goto errout;
  166. }
  167. if (addr_len == DNS_RR_A_LEN) {
  168. memcpy(cache_addr->addr_data.addr, addr, DNS_RR_A_LEN);
  169. } else if (addr_len != DNS_RR_AAAA_LEN) {
  170. memcpy(cache_addr->addr_data.addr, addr, DNS_RR_AAAA_LEN);
  171. } else {
  172. goto errout;
  173. }
  174. if (cname) {
  175. safe_strncpy(cache_addr->addr_data.cname, cname, DNS_MAX_CNAME_LEN);
  176. cache_addr->addr_data.cname_ttl = cname_ttl;
  177. }
  178. cache_addr->head.cache_flag = cache_flag;
  179. cache_addr->head.cache_type = CACHE_TYPE_ADDR;
  180. cache_addr->head.size = sizeof(struct dns_cache_addr) - sizeof(struct dns_cache_data_head);
  181. errout:
  182. return;
  183. }
  184. struct dns_cache_data *dns_cache_new_data_packet(uint32_t cache_flag, void *packet, size_t packet_len)
  185. {
  186. struct dns_cache_packet *cache_packet = NULL;
  187. size_t data_size = 0;
  188. if (packet == NULL || packet_len <= 0) {
  189. return NULL;
  190. }
  191. data_size = sizeof(*cache_packet) + packet_len;
  192. cache_packet = malloc(data_size);
  193. if (cache_packet == NULL) {
  194. return NULL;
  195. }
  196. memcpy(cache_packet->data, packet, packet_len);
  197. memset(&cache_packet->head, 0, sizeof(cache_packet->head));
  198. cache_packet->head.cache_flag = cache_flag;
  199. cache_packet->head.cache_type = CACHE_TYPE_PACKET;
  200. cache_packet->head.size = packet_len;
  201. return (struct dns_cache_data *)cache_packet;
  202. }
  203. static int _dns_cache_replace(char *domain, int ttl, dns_type_t qtype, int speed, int inactive,
  204. struct dns_cache_data *cache_data)
  205. {
  206. struct dns_cache *dns_cache = NULL;
  207. struct dns_cache_data *old_cache_data = NULL;
  208. if (dns_cache_head.size <= 0) {
  209. return 0;
  210. }
  211. /* lookup existing cache */
  212. dns_cache = dns_cache_lookup(domain, qtype);
  213. if (dns_cache == NULL) {
  214. return dns_cache_insert(domain, ttl, qtype, speed, cache_data);
  215. }
  216. if (ttl < DNS_CACHE_TTL_MIN) {
  217. ttl = DNS_CACHE_TTL_MIN;
  218. }
  219. /* update cache data */
  220. pthread_mutex_lock(&dns_cache_head.lock);
  221. dns_cache->del_pending = 0;
  222. dns_cache->info.ttl = ttl;
  223. dns_cache->info.qtype = qtype;
  224. dns_cache->info.ttl = ttl;
  225. dns_cache->info.speed = speed;
  226. old_cache_data = dns_cache->cache_data;
  227. dns_cache->cache_data = cache_data;
  228. list_del_init(&dns_cache->list);
  229. if (inactive == 0) {
  230. time(&dns_cache->info.insert_time);
  231. time(&dns_cache->info.replace_time);
  232. list_add_tail(&dns_cache->list, &dns_cache_head.cache_list);
  233. } else {
  234. time(&dns_cache->info.replace_time);
  235. list_add_tail(&dns_cache->list, &dns_cache_head.inactive_list);
  236. }
  237. pthread_mutex_unlock(&dns_cache_head.lock);
  238. dns_cache_data_free(old_cache_data);
  239. dns_cache_release(dns_cache);
  240. return 0;
  241. }
  242. int dns_cache_replace(char *domain, int ttl, dns_type_t qtype, int speed, struct dns_cache_data *cache_data)
  243. {
  244. return _dns_cache_replace(domain, ttl, qtype, speed, 0, cache_data);
  245. }
  246. int dns_cache_replace_inactive(char *domain, int ttl, dns_type_t qtype, int speed, struct dns_cache_data *cache_data)
  247. {
  248. return _dns_cache_replace(domain, ttl, qtype, speed, 1, cache_data);
  249. }
  250. static void _dns_cache_remove_by_domain(const char *domain, dns_type_t qtype)
  251. {
  252. uint32_t key = 0;
  253. struct dns_cache *dns_cache = NULL;
  254. key = hash_string(domain);
  255. key = jhash(&qtype, sizeof(qtype), key);
  256. pthread_mutex_lock(&dns_cache_head.lock);
  257. hash_for_each_possible(dns_cache_head.cache_hash, dns_cache, node, key)
  258. {
  259. if (dns_cache->info.qtype != qtype) {
  260. continue;
  261. }
  262. if (strncmp(domain, dns_cache->info.domain, DNS_MAX_CNAME_LEN) != 0) {
  263. continue;
  264. }
  265. _dns_cache_remove(dns_cache);
  266. break;
  267. }
  268. pthread_mutex_unlock(&dns_cache_head.lock);
  269. }
  270. static int _dns_cache_insert(struct dns_cache_info *info, struct dns_cache_data *cache_data, struct list_head *head)
  271. {
  272. uint32_t key = 0;
  273. struct dns_cache *dns_cache = NULL;
  274. /* if cache already exists, free */
  275. _dns_cache_remove_by_domain(info->domain, info->qtype);
  276. dns_cache = malloc(sizeof(*dns_cache));
  277. if (dns_cache == NULL) {
  278. goto errout;
  279. }
  280. memset(dns_cache, 0, sizeof(*dns_cache));
  281. key = hash_string(info->domain);
  282. key = jhash(&info->qtype, sizeof(info->qtype), key);
  283. atomic_set(&dns_cache->ref, 1);
  284. memcpy(&dns_cache->info, info, sizeof(*info));
  285. dns_cache->del_pending = 0;
  286. dns_cache->cache_data = cache_data;
  287. pthread_mutex_lock(&dns_cache_head.lock);
  288. hash_add(dns_cache_head.cache_hash, &dns_cache->node, key);
  289. list_add_tail(&dns_cache->list, head);
  290. INIT_LIST_HEAD(&dns_cache->check_list);
  291. /* Release extra cache, remove oldest cache record */
  292. if (atomic_inc_return(&dns_cache_head.num) > dns_cache_head.size) {
  293. struct dns_cache *del_cache = NULL;
  294. del_cache = _dns_inactive_cache_first();
  295. if (del_cache) {
  296. _dns_cache_remove(del_cache);
  297. }
  298. }
  299. pthread_mutex_unlock(&dns_cache_head.lock);
  300. return 0;
  301. errout:
  302. if (dns_cache) {
  303. free(dns_cache);
  304. }
  305. return -1;
  306. }
  307. int dns_cache_insert(char *domain, int ttl, dns_type_t qtype, int speed, struct dns_cache_data *cache_data)
  308. {
  309. struct dns_cache_info info;
  310. if (cache_data == NULL || domain == NULL) {
  311. return -1;
  312. }
  313. if (dns_cache_head.size <= 0) {
  314. dns_cache_data_free(cache_data);
  315. return 0;
  316. }
  317. if (ttl < DNS_CACHE_TTL_MIN) {
  318. ttl = DNS_CACHE_TTL_MIN;
  319. }
  320. info.hitnum = 3;
  321. safe_strncpy(info.domain, domain, DNS_MAX_CNAME_LEN);
  322. info.qtype = qtype;
  323. info.ttl = ttl;
  324. info.hitnum_update_add = DNS_CACHE_HITNUM_STEP;
  325. info.speed = speed;
  326. time(&info.insert_time);
  327. time(&info.replace_time);
  328. return _dns_cache_insert(&info, cache_data, &dns_cache_head.cache_list);
  329. }
  330. struct dns_cache *dns_cache_lookup(char *domain, dns_type_t qtype)
  331. {
  332. uint32_t key = 0;
  333. struct dns_cache *dns_cache = NULL;
  334. struct dns_cache *dns_cache_ret = NULL;
  335. time_t now = 0;
  336. if (dns_cache_head.size <= 0) {
  337. return NULL;
  338. }
  339. key = hash_string(domain);
  340. key = jhash(&qtype, sizeof(qtype), key);
  341. time(&now);
  342. /* find cache */
  343. pthread_mutex_lock(&dns_cache_head.lock);
  344. hash_for_each_possible(dns_cache_head.cache_hash, dns_cache, node, key)
  345. {
  346. if (dns_cache->info.qtype != qtype) {
  347. continue;
  348. }
  349. if (strncmp(domain, dns_cache->info.domain, DNS_MAX_CNAME_LEN) != 0) {
  350. continue;
  351. }
  352. dns_cache_ret = dns_cache;
  353. break;
  354. }
  355. if (dns_cache_ret) {
  356. /* Return NULL if the cache times out */
  357. if (dns_cache_head.enable_inactive == 0 && (now - dns_cache_ret->info.insert_time > dns_cache_ret->info.ttl)) {
  358. _dns_cache_remove(dns_cache_ret);
  359. dns_cache_ret = NULL;
  360. } else {
  361. dns_cache_get(dns_cache_ret);
  362. }
  363. }
  364. pthread_mutex_unlock(&dns_cache_head.lock);
  365. return dns_cache_ret;
  366. }
  367. int dns_cache_get_ttl(struct dns_cache *dns_cache)
  368. {
  369. time_t now = 0;
  370. int ttl = 0;
  371. time(&now);
  372. ttl = dns_cache->info.insert_time + dns_cache->info.ttl - now;
  373. if (ttl < 0) {
  374. return 0;
  375. }
  376. return ttl;
  377. }
  378. int dns_cache_get_cname_ttl(struct dns_cache *dns_cache)
  379. {
  380. time_t now = 0;
  381. int ttl = 0;
  382. time(&now);
  383. struct dns_cache_addr *cache_addr = (struct dns_cache_addr *)dns_cache_get_data(dns_cache);
  384. if (cache_addr->head.cache_type != CACHE_TYPE_ADDR) {
  385. return 0;
  386. }
  387. ttl = dns_cache->info.insert_time + cache_addr->addr_data.cname_ttl - now;
  388. if (ttl < 0) {
  389. return 0;
  390. }
  391. int addr_ttl = dns_cache_get_ttl(dns_cache);
  392. if (ttl < addr_ttl && ttl < 0) {
  393. return addr_ttl;
  394. }
  395. if (ttl < 0) {
  396. return 0;
  397. }
  398. return ttl;
  399. }
  400. int dns_cache_is_soa(struct dns_cache *dns_cache)
  401. {
  402. if (dns_cache == NULL) {
  403. return 0;
  404. }
  405. if (dns_cache->cache_data->head.is_soa) {
  406. return 1;
  407. }
  408. return 0;
  409. }
  410. struct dns_cache_data *dns_cache_get_data(struct dns_cache *dns_cache)
  411. {
  412. return dns_cache->cache_data;
  413. }
  414. void dns_cache_delete(struct dns_cache *dns_cache)
  415. {
  416. pthread_mutex_lock(&dns_cache_head.lock);
  417. _dns_cache_remove(dns_cache);
  418. pthread_mutex_unlock(&dns_cache_head.lock);
  419. }
  420. int dns_cache_hitnum_dec_get(struct dns_cache *dns_cache)
  421. {
  422. pthread_mutex_lock(&dns_cache_head.lock);
  423. dns_cache->info.hitnum--;
  424. if (dns_cache->info.hitnum_update_add > DNS_CACHE_HITNUM_STEP) {
  425. dns_cache->info.hitnum_update_add--;
  426. }
  427. pthread_mutex_unlock(&dns_cache_head.lock);
  428. return dns_cache->info.hitnum;
  429. }
  430. void dns_cache_update(struct dns_cache *dns_cache)
  431. {
  432. pthread_mutex_lock(&dns_cache_head.lock);
  433. if (!list_empty(&dns_cache->list)) {
  434. list_del_init(&dns_cache->list);
  435. list_add_tail(&dns_cache->list, &dns_cache_head.cache_list);
  436. dns_cache->info.hitnum += dns_cache->info.hitnum_update_add;
  437. if (dns_cache->info.hitnum > DNS_CACHE_MAX_HITNUM) {
  438. dns_cache->info.hitnum = DNS_CACHE_MAX_HITNUM;
  439. }
  440. if (dns_cache->info.hitnum_update_add < DNS_CACHE_HITNUM_STEP_MAX) {
  441. dns_cache->info.hitnum_update_add++;
  442. }
  443. }
  444. pthread_mutex_unlock(&dns_cache_head.lock);
  445. }
  446. static void _dns_cache_remove_expired_ttl(dns_cache_callback inactive_precallback, int ttl_inactive_pre,
  447. unsigned int max_callback_num, const time_t *now)
  448. {
  449. struct dns_cache *dns_cache = NULL;
  450. struct dns_cache *tmp = NULL;
  451. unsigned int callback_num = 0;
  452. int ttl = 0;
  453. LIST_HEAD(checklist);
  454. pthread_mutex_lock(&dns_cache_head.lock);
  455. list_for_each_entry_safe(dns_cache, tmp, &dns_cache_head.inactive_list, list)
  456. {
  457. ttl = dns_cache->info.insert_time + dns_cache->info.ttl - *now;
  458. if (ttl > 0) {
  459. continue;
  460. }
  461. if (dns_cache_head.inactive_list_expired + ttl < 0) {
  462. _dns_cache_remove(dns_cache);
  463. continue;
  464. }
  465. ttl = *now - dns_cache->info.replace_time;
  466. if (ttl < ttl_inactive_pre || inactive_precallback == NULL) {
  467. continue;
  468. }
  469. if (callback_num >= max_callback_num) {
  470. continue;
  471. }
  472. if (dns_cache->del_pending == 1) {
  473. continue;
  474. }
  475. /* If the TTL time is in the pre-timeout range, call callback function */
  476. dns_cache_get(dns_cache);
  477. list_add_tail(&dns_cache->check_list, &checklist);
  478. dns_cache->del_pending = 1;
  479. callback_num++;
  480. }
  481. pthread_mutex_unlock(&dns_cache_head.lock);
  482. list_for_each_entry_safe(dns_cache, tmp, &checklist, check_list)
  483. {
  484. /* run inactive_precallback */
  485. if (inactive_precallback) {
  486. inactive_precallback(dns_cache);
  487. }
  488. dns_cache_release(dns_cache);
  489. }
  490. }
  491. void dns_cache_invalidate(dns_cache_callback precallback, int ttl_pre, unsigned int max_callback_num,
  492. dns_cache_callback inactive_precallback, int ttl_inactive_pre)
  493. {
  494. struct dns_cache *dns_cache = NULL;
  495. struct dns_cache *tmp = NULL;
  496. time_t now = 0;
  497. int ttl = 0;
  498. LIST_HEAD(checklist);
  499. unsigned int callback_num = 0;
  500. if (max_callback_num <= 0) {
  501. max_callback_num = -1;
  502. }
  503. if (dns_cache_head.size <= 0) {
  504. return;
  505. }
  506. time(&now);
  507. pthread_mutex_lock(&dns_cache_head.lock);
  508. list_for_each_entry_safe(dns_cache, tmp, &dns_cache_head.cache_list, list)
  509. {
  510. ttl = dns_cache->info.insert_time + dns_cache->info.ttl - now;
  511. if (ttl > 0 && ttl < ttl_pre) {
  512. /* If the TTL time is in the pre-timeout range, call callback function */
  513. if (precallback && dns_cache->del_pending == 0 && callback_num < max_callback_num) {
  514. list_add_tail(&dns_cache->check_list, &checklist);
  515. dns_cache_get(dns_cache);
  516. dns_cache->del_pending = 1;
  517. callback_num++;
  518. continue;
  519. }
  520. }
  521. if (ttl < 0) {
  522. if (dns_cache_head.enable_inactive) {
  523. _dns_cache_move_inactive(dns_cache);
  524. } else {
  525. _dns_cache_remove(dns_cache);
  526. }
  527. }
  528. }
  529. pthread_mutex_unlock(&dns_cache_head.lock);
  530. if (dns_cache_head.enable_inactive && dns_cache_head.inactive_list_expired != 0) {
  531. _dns_cache_remove_expired_ttl(inactive_precallback, ttl_inactive_pre, max_callback_num, &now);
  532. }
  533. list_for_each_entry_safe(dns_cache, tmp, &checklist, check_list)
  534. {
  535. /* run callback */
  536. if (precallback) {
  537. precallback(dns_cache);
  538. }
  539. list_del(&dns_cache->check_list);
  540. dns_cache_release(dns_cache);
  541. }
  542. }
  543. static int _dns_cache_read_record(int fd, uint32_t cache_number)
  544. {
  545. unsigned int i = 0;
  546. ssize_t ret = 0;
  547. struct dns_cache_record cache_record;
  548. struct dns_cache_data_head data_head;
  549. struct dns_cache_data *cache_data = NULL;
  550. struct list_head *head = NULL;
  551. for (i = 0; i < cache_number; i++) {
  552. ret = read(fd, &cache_record, sizeof(cache_record));
  553. if (ret != sizeof(cache_record)) {
  554. tlog(TLOG_ERROR, "read cache failed, %s", strerror(errno));
  555. goto errout;
  556. }
  557. if (cache_record.magic != MAGIC_CACHE_DATA) {
  558. tlog(TLOG_ERROR, "magic is invalid.");
  559. goto errout;
  560. }
  561. if (cache_record.type == CACHE_RECORD_TYPE_ACTIVE) {
  562. head = &dns_cache_head.cache_list;
  563. } else {
  564. head = &dns_cache_head.inactive_list;
  565. }
  566. ret = read(fd, &data_head, sizeof(data_head));
  567. if (ret != sizeof(data_head)) {
  568. tlog(TLOG_ERROR, "read data head failed, %s", strerror(errno));
  569. goto errout;
  570. }
  571. if (data_head.size > 1024 * 8) {
  572. tlog(TLOG_ERROR, "data may invalid, skip load cache.");
  573. goto errout;
  574. }
  575. cache_data = malloc(data_head.size + sizeof(data_head));
  576. if (cache_data == NULL) {
  577. tlog(TLOG_ERROR, "malloc cache data failed %s", strerror(errno));
  578. goto errout;
  579. }
  580. memcpy(&cache_data->head, &data_head, sizeof(data_head));
  581. ret = read(fd, cache_data->data, data_head.size);
  582. if (ret != data_head.size) {
  583. tlog(TLOG_ERROR, "read cache data failed, %s", strerror(errno));
  584. goto errout;
  585. }
  586. if (_dns_cache_insert(&cache_record.info, cache_data, head) != 0) {
  587. tlog(TLOG_ERROR, "insert cache data failed.");
  588. cache_data = NULL;
  589. goto errout;
  590. }
  591. cache_data = NULL;
  592. }
  593. return 0;
  594. errout:
  595. if (cache_data) {
  596. free(cache_data);
  597. }
  598. return -1;
  599. }
  600. int dns_cache_load(const char *file)
  601. {
  602. int fd = -1;
  603. ssize_t ret = 0;
  604. off_t filesize = 0;
  605. fd = open(file, O_RDONLY);
  606. if (fd < 0) {
  607. return 0;
  608. }
  609. filesize = lseek(fd, 0, SEEK_END);
  610. lseek(fd, 0, SEEK_SET);
  611. posix_fadvise(fd, 0, filesize, POSIX_FADV_WILLNEED | POSIX_FADV_SEQUENTIAL);
  612. struct dns_cache_file cache_file;
  613. ret = read(fd, &cache_file, sizeof(cache_file));
  614. if (ret != sizeof(cache_file)) {
  615. tlog(TLOG_ERROR, "read cache head failed.");
  616. goto errout;
  617. }
  618. if (cache_file.magic != MAGIC_NUMBER) {
  619. tlog(TLOG_ERROR, "cache file is invalid.");
  620. goto errout;
  621. }
  622. if (strncmp(cache_file.version, __TIMESTAMP__, DNS_CACHE_VERSION_LEN - 1) != 0) {
  623. tlog(TLOG_WARN, "cache version is different, skip load cache.");
  624. goto errout;
  625. }
  626. tlog(TLOG_INFO, "load cache file %s, total %d records", file, cache_file.cache_number);
  627. if (_dns_cache_read_record(fd, cache_file.cache_number) != 0) {
  628. goto errout;
  629. }
  630. close(fd);
  631. return 0;
  632. errout:
  633. if (fd > 0) {
  634. close(fd);
  635. }
  636. return -1;
  637. }
  638. static int _dns_cache_write_record(int fd, uint32_t *cache_number, enum CACHE_RECORD_TYPE type, struct list_head *head)
  639. {
  640. struct dns_cache *dns_cache = NULL;
  641. struct dns_cache *tmp = NULL;
  642. struct dns_cache_record cache_record;
  643. pthread_mutex_lock(&dns_cache_head.lock);
  644. list_for_each_entry_safe_reverse(dns_cache, tmp, head, list)
  645. {
  646. cache_record.magic = MAGIC_CACHE_DATA;
  647. cache_record.type = type;
  648. memcpy(&cache_record.info, &dns_cache->info, sizeof(struct dns_cache_info));
  649. ssize_t ret = write(fd, &cache_record, sizeof(cache_record));
  650. if (ret != sizeof(cache_record)) {
  651. tlog(TLOG_ERROR, "write cache failed, %s", strerror(errno));
  652. goto errout;
  653. }
  654. struct dns_cache_data *cache_data = dns_cache->cache_data;
  655. ret = write(fd, cache_data, sizeof(*cache_data) + cache_data->head.size);
  656. if (ret != (int)sizeof(*cache_data) + cache_data->head.size) {
  657. tlog(TLOG_ERROR, "write cache data failed, %s", strerror(errno));
  658. goto errout;
  659. }
  660. (*cache_number)++;
  661. }
  662. pthread_mutex_unlock(&dns_cache_head.lock);
  663. return 0;
  664. errout:
  665. pthread_mutex_unlock(&dns_cache_head.lock);
  666. return -1;
  667. }
  668. static int _dns_cache_write_records(int fd, uint32_t *cache_number)
  669. {
  670. if (_dns_cache_write_record(fd, cache_number, CACHE_RECORD_TYPE_ACTIVE, &dns_cache_head.cache_list) != 0) {
  671. return -1;
  672. }
  673. if (_dns_cache_write_record(fd, cache_number, CACHE_RECORD_TYPE_INACTIVE, &dns_cache_head.inactive_list) != 0) {
  674. return -1;
  675. }
  676. return 0;
  677. }
  678. int dns_cache_save(const char *file)
  679. {
  680. int fd = -1;
  681. uint32_t cache_number = 0;
  682. tlog(TLOG_DEBUG, "write cache file %s", file);
  683. fd = open(file, O_TRUNC | O_CREAT | O_WRONLY, 0640);
  684. if (fd < 0) {
  685. tlog(TLOG_ERROR, "create file %s failed, %s", file, strerror(errno));
  686. goto errout;
  687. }
  688. struct dns_cache_file cache_file;
  689. memset(&cache_file, 0, sizeof(cache_file));
  690. cache_file.magic = MAGIC_NUMBER;
  691. safe_strncpy(cache_file.version, __TIMESTAMP__, DNS_CACHE_VERSION_LEN);
  692. cache_file.cache_number = 0;
  693. if (lseek(fd, sizeof(cache_file), SEEK_SET) < 0) {
  694. tlog(TLOG_ERROR, "seek file %s failed, %s", file, strerror(errno));
  695. goto errout;
  696. }
  697. if (_dns_cache_write_records(fd, &cache_number) != 0) {
  698. tlog(TLOG_ERROR, "write record to file %s failed.", file);
  699. goto errout;
  700. }
  701. if (lseek(fd, 0, SEEK_SET) < 0) {
  702. tlog(TLOG_ERROR, "seek file %s failed, %s", file, strerror(errno));
  703. goto errout;
  704. }
  705. cache_file.cache_number = cache_number;
  706. if (write(fd, &cache_file, sizeof(cache_file)) != sizeof(cache_file)) {
  707. tlog(TLOG_ERROR, "write file head %s failed, %s, %d", file, strerror(errno), fd);
  708. goto errout;
  709. }
  710. tlog(TLOG_DEBUG, "wrote total %d records.", cache_number);
  711. close(fd);
  712. return 0;
  713. errout:
  714. if (fd > 0) {
  715. close(fd);
  716. }
  717. return -1;
  718. }
  719. void dns_cache_destroy(void)
  720. {
  721. struct dns_cache *dns_cache = NULL;
  722. struct dns_cache *tmp = NULL;
  723. pthread_mutex_lock(&dns_cache_head.lock);
  724. list_for_each_entry_safe(dns_cache, tmp, &dns_cache_head.inactive_list, list)
  725. {
  726. _dns_cache_delete(dns_cache);
  727. }
  728. list_for_each_entry_safe(dns_cache, tmp, &dns_cache_head.cache_list, list)
  729. {
  730. _dns_cache_delete(dns_cache);
  731. }
  732. pthread_mutex_unlock(&dns_cache_head.lock);
  733. pthread_mutex_destroy(&dns_cache_head.lock);
  734. }