cache.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License version 2.1
  6. * as published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #define _GNU_SOURCE
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <fcntl.h>
  17. #include <time.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <sys/types.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #include <arpa/inet.h>
  25. #include <asm/byteorder.h>
  26. #include <arpa/nameser.h>
  27. #include <resolv.h>
  28. #include <time.h>
  29. #include <libubox/usock.h>
  30. #include <libubox/uloop.h>
  31. #include <libubox/avl-cmp.h>
  32. #include <libubox/blobmsg_json.h>
  33. #include <libubox/kvlist.h>
  34. #include <libubus.h>
  35. #include "cache.h"
  36. #include "util.h"
  37. #include "dns.h"
  38. #include "interface.h"
  39. static struct uloop_timeout cache_gc;
  40. struct avl_tree entries;
  41. static AVL_TREE(records, avl_strcmp, true, NULL);
  42. static void
  43. cache_record_free(struct cache_record *r)
  44. {
  45. DBG(2, "%s %s\n", dns_type_string(r->type), r->record);
  46. avl_delete(&records, &r->avl);
  47. free(r);
  48. }
  49. static void
  50. cache_entry_free(struct cache_entry *s)
  51. {
  52. DBG(2, "%s\n", s->entry);
  53. avl_delete(&entries, &s->avl);
  54. free(s);
  55. }
  56. static int
  57. cache_is_expired(time_t t, uint32_t ttl)
  58. {
  59. if (time(NULL) - t >= ttl)
  60. return 1;
  61. return 0;
  62. }
  63. static void
  64. cache_gc_timer(struct uloop_timeout *timeout)
  65. {
  66. struct cache_record *r, *p;
  67. struct cache_entry *s, *t;
  68. avl_for_each_element_safe(&records, r, avl, p)
  69. if (cache_is_expired(r->time, r->ttl))
  70. cache_record_free(r);
  71. avl_for_each_element_safe(&entries, s, avl, t) {
  72. if (!s->host)
  73. continue;
  74. if (cache_is_expired(s->time, s->ttl))
  75. cache_entry_free(s);
  76. }
  77. uloop_timeout_set(timeout, 10000);
  78. }
  79. int
  80. cache_init(void)
  81. {
  82. avl_init(&entries, avl_strcmp, true, NULL);
  83. cache_gc.cb = cache_gc_timer;
  84. uloop_timeout_set(&cache_gc, 10000);
  85. return 0;
  86. }
  87. void cache_cleanup(void)
  88. {
  89. struct cache_record *r, *p;
  90. struct cache_entry *s, *t;
  91. avl_for_each_element_safe(&records, r, avl, p)
  92. cache_record_free(r);
  93. avl_for_each_element_safe(&entries, s, avl, t)
  94. cache_entry_free(s);
  95. }
  96. void
  97. cache_scan(void)
  98. {
  99. struct interface *iface;
  100. struct cache_entry *s;
  101. vlist_for_each_element(&interfaces, iface, node)
  102. avl_for_each_element(&entries, s, avl)
  103. dns_send_question(iface, s->entry, TYPE_PTR);
  104. }
  105. static struct cache_entry*
  106. cache_entry(struct interface *iface, char *entry, int hlen, int ttl)
  107. {
  108. struct cache_entry *s;
  109. char *entry_buf;
  110. char *host_buf;
  111. char *type;
  112. s = avl_find_element(&entries, entry, s, avl);
  113. if (s)
  114. return s;
  115. s = calloc_a(sizeof(*s),
  116. &entry_buf, strlen(entry) + 1,
  117. &host_buf, hlen ? hlen + 1 : 0);
  118. s->avl.key = s->entry = strcpy(entry_buf, entry);
  119. s->time = time(NULL);
  120. s->ttl = ttl;
  121. if (hlen)
  122. s->host = strncpy(host_buf, s->entry, hlen);
  123. type = strstr(entry_buf, "._");
  124. if (type)
  125. type++;
  126. if (type)
  127. s->avl.key = type;
  128. avl_insert(&entries, &s->avl);
  129. if (!hlen)
  130. dns_send_question(iface, entry, TYPE_PTR);
  131. return s;
  132. }
  133. static struct cache_record*
  134. cache_record_find(char *record, int type, int port, int rdlength, uint8_t *rdata)
  135. {
  136. struct cache_record *l = avl_find_element(&records, record, l, avl);
  137. if (!l)
  138. return NULL;
  139. while (l && !avl_is_last(&records, &l->avl) && !strcmp(l->record, record)) {
  140. struct cache_record *r = l;
  141. l = avl_next_element(l, avl);
  142. if (r->type != type)
  143. continue;
  144. if (r->type == TYPE_TXT || (r->type == TYPE_SRV))
  145. return r;
  146. if (r->port != port)
  147. continue;
  148. if (r->rdlength != rdlength)
  149. continue;
  150. if (!!r->rdata != !!rdata)
  151. continue;
  152. if (!r->rdata || !rdata || memcmp(r->rdata, rdata, rdlength))
  153. continue;
  154. return r;
  155. }
  156. return NULL;
  157. }
  158. int
  159. cache_host_is_known(char *record)
  160. {
  161. struct cache_record *l = avl_find_element(&records, record, l, avl);
  162. if (!l)
  163. return 0;
  164. while (l && !avl_is_last(&records, &l->avl) && !strcmp(l->record, record)) {
  165. struct cache_record *r = l;
  166. l = avl_next_element(l, avl);
  167. if ((r->type != TYPE_A) && (r->type != TYPE_AAAA))
  168. continue;
  169. return 1;
  170. }
  171. return 0;
  172. }
  173. void
  174. cache_answer(struct interface *iface, uint8_t *base, int blen, char *name, struct dns_answer *a, uint8_t *rdata)
  175. {
  176. struct dns_srv_data *dsd = (struct dns_srv_data *) rdata;
  177. struct cache_record *r;
  178. int port = 0, dlen = 0, tlen = 0, nlen, rdlength;
  179. char *p = NULL;
  180. char *name_buf;
  181. void *rdata_ptr, *txt_ptr;
  182. int host_len = 0;
  183. static char rdata_buffer[MAX_DATA_LEN + 1];
  184. if (!(a->class & CLASS_IN))
  185. return;
  186. nlen = strlen(name);
  187. switch (a->type) {
  188. case TYPE_PTR:
  189. if (a->rdlength < 2)
  190. return;
  191. if (dn_expand(base, base + blen, rdata, rdata_buffer, MAX_DATA_LEN) < 0) {
  192. perror("process_answer/dn_expand");
  193. return;
  194. }
  195. DBG(1, "A -> %s %s %s\n", dns_type_string(a->type), name, rdata_buffer);
  196. rdlength = strlen(rdata_buffer);
  197. if (strcmp(C_DNS_SD, name) != 0 &&
  198. nlen + 1 < rdlength && !strcmp(rdata_buffer + rdlength - nlen, name))
  199. host_len = rdlength - nlen - 1;
  200. cache_entry(iface, rdata_buffer, host_len, a->ttl);
  201. return;
  202. case TYPE_SRV:
  203. if (a->rdlength < 8)
  204. return;
  205. port = be16_to_cpu(dsd->port);
  206. break;
  207. case TYPE_TXT:
  208. rdlength = a->rdlength;
  209. if (rdlength <= 2)
  210. return;
  211. memcpy(rdata_buffer, &rdata[1], rdlength);
  212. rdata_buffer[rdlength] = rdata_buffer[rdlength + 1] = '\0';
  213. tlen = rdlength + 1;
  214. p = &rdata_buffer[*rdata];
  215. do {
  216. uint8_t v = *p;
  217. *p = '\0';
  218. if (v)
  219. p += v + 1;
  220. } while (*p);
  221. break;
  222. case TYPE_A:
  223. cache_entry(iface, name, strlen(name), a->ttl);
  224. if (a->rdlength != 4)
  225. return;
  226. dlen = 4;
  227. break;
  228. case TYPE_AAAA:
  229. cache_entry(iface, name, strlen(name), a->ttl);
  230. if (a->rdlength != 16)
  231. return;
  232. dlen = 16;
  233. break;
  234. default:
  235. return;
  236. }
  237. r = cache_record_find(name, a->type, port, dlen, rdata);
  238. if (r) {
  239. if (!a->ttl) {
  240. cache_record_free(r);
  241. DBG(1, "D -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
  242. } else {
  243. r->ttl = a->ttl;
  244. DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
  245. }
  246. return;
  247. }
  248. if (!a->ttl)
  249. return;
  250. r = calloc_a(sizeof(*r),
  251. &name_buf, strlen(name) + 1,
  252. &txt_ptr, tlen,
  253. &rdata_ptr, dlen);
  254. r->avl.key = r->record = strcpy(name_buf, name);
  255. r->type = a->type;
  256. r->ttl = a->ttl;
  257. r->port = port;
  258. r->rdlength = dlen;
  259. r->time = time(NULL);
  260. if (tlen)
  261. r->txt = memcpy(txt_ptr, rdata_buffer, tlen);
  262. if (dlen)
  263. r->rdata = memcpy(rdata_ptr, rdata, dlen);
  264. if (avl_insert(&records, &r->avl))
  265. free(r);
  266. else
  267. DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
  268. }
  269. void
  270. cache_dump_records(struct blob_buf *buf, const char *name)
  271. {
  272. struct cache_record *r, *q = avl_find_element(&records, name, r, avl);
  273. const char *txt;
  274. char buffer[MAX_NAME_LEN];
  275. if (!q)
  276. return;
  277. do {
  278. r = q;
  279. switch (r->type) {
  280. case TYPE_TXT:
  281. if (r->txt && strlen(r->txt)) {
  282. txt = r->txt;
  283. do {
  284. blobmsg_add_string(buf, "txt", txt);
  285. txt = &txt[strlen(txt) + 1];
  286. } while (*txt);
  287. }
  288. break;
  289. case TYPE_SRV:
  290. if (r->port)
  291. blobmsg_add_u32(buf, "port", r->port);
  292. break;
  293. case TYPE_A:
  294. if ((r->rdlength == 4) && inet_ntop(AF_INET, r->rdata, buffer, INET6_ADDRSTRLEN))
  295. blobmsg_add_string(buf, "ipv4", buffer);
  296. break;
  297. case TYPE_AAAA:
  298. if ((r->rdlength == 16) && inet_ntop(AF_INET6, r->rdata, buffer, INET6_ADDRSTRLEN))
  299. blobmsg_add_string(buf, "ipv6", buffer);
  300. break;
  301. }
  302. q = avl_next_element(r, avl);
  303. } while (q && !strcmp(r->record, q->record));
  304. }