1
0

0019-Tidy-address-union-handling-move-class-into-explicit.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. From 65a01b71bb433c9466e4c78a73a8d8ed218ed4e8 Mon Sep 17 00:00:00 2001
  2. From: Simon Kelley <simon@thekelleys.org.uk>
  3. Date: Mon, 31 Dec 2018 23:56:33 +0000
  4. Subject: [PATCH 19/32] Tidy address-union handling: move class into explicit
  5. argument.
  6. This moves the class argument to cache-insert into an argument,
  7. rather then overloading a union in the address argument. Note that
  8. tha class is NOT stored in the cache other than for DS/DNSKEY entries,
  9. so must always be C_IN except for these. The data-extraction code
  10. ensures this as it only attempts to cache C_IN class records.
  11. Signed-off-by: Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
  12. ---
  13. src/cache.c | 57 ++++++++++++++++++++++-----------------------------
  14. src/dnsmasq.h | 2 +-
  15. src/dnssec.c | 13 +++---------
  16. src/rfc1035.c | 12 +++++------
  17. 4 files changed, 34 insertions(+), 50 deletions(-)
  18. --- a/src/cache.c
  19. +++ b/src/cache.c
  20. @@ -26,7 +26,7 @@ static union bigname *big_free = NULL;
  21. static int bignames_left, hash_size;
  22. static void make_non_terminals(struct crec *source);
  23. -static struct crec *really_insert(char *name, struct all_addr *addr,
  24. +static struct crec *really_insert(char *name, struct all_addr *addr, unsigned short class,
  25. time_t now, unsigned long ttl, unsigned short flags);
  26. /* type->string mapping: this is also used by the name-hash function as a mixing table. */
  27. @@ -330,8 +330,8 @@ static int is_expired(time_t now, struct
  28. return 1;
  29. }
  30. -static struct crec *cache_scan_free(char *name, struct all_addr *addr, time_t now, unsigned short flags,
  31. - struct crec **target_crec, unsigned int *target_uid)
  32. +static struct crec *cache_scan_free(char *name, struct all_addr *addr, unsigned short class, time_t now,
  33. + unsigned short flags, struct crec **target_crec, unsigned int *target_uid)
  34. {
  35. /* Scan and remove old entries.
  36. If (flags & F_FORWARD) then remove any forward entries for name and any expired
  37. @@ -350,6 +350,8 @@ static struct crec *cache_scan_free(char
  38. This entry will get re-used with the same name, to preserve CNAMEs. */
  39. struct crec *crecp, **up;
  40. +
  41. + (void)class;
  42. if (flags & F_FORWARD)
  43. {
  44. @@ -381,7 +383,7 @@ static struct crec *cache_scan_free(char
  45. #ifdef HAVE_DNSSEC
  46. /* Deletion has to be class-sensitive for DS and DNSKEY */
  47. - if ((flags & crecp->flags & (F_DNSKEY | F_DS)) && crecp->uid == addr->addr.dnssec.class)
  48. + if ((flags & crecp->flags & (F_DNSKEY | F_DS)) && crecp->uid == class)
  49. {
  50. if (crecp->flags & F_CONFIG)
  51. return crecp;
  52. @@ -464,7 +466,7 @@ void cache_start_insert(void)
  53. insert_error = 0;
  54. }
  55. -struct crec *cache_insert(char *name, struct all_addr *addr,
  56. +struct crec *cache_insert(char *name, struct all_addr *addr, unsigned short class,
  57. time_t now, unsigned long ttl, unsigned short flags)
  58. {
  59. /* Don't log DNSSEC records here, done elsewhere */
  60. @@ -478,11 +480,11 @@ struct crec *cache_insert(char *name, st
  61. ttl = daemon->min_cache_ttl;
  62. }
  63. - return really_insert(name, addr, now, ttl, flags);
  64. + return really_insert(name, addr, class, now, ttl, flags);
  65. }
  66. -static struct crec *really_insert(char *name, struct all_addr *addr,
  67. +static struct crec *really_insert(char *name, struct all_addr *addr, unsigned short class,
  68. time_t now, unsigned long ttl, unsigned short flags)
  69. {
  70. struct crec *new, *target_crec = NULL;
  71. @@ -497,7 +499,7 @@ static struct crec *really_insert(char *
  72. /* First remove any expired entries and entries for the name/address we
  73. are currently inserting. */
  74. - if ((new = cache_scan_free(name, addr, now, flags, &target_crec, &target_uid)))
  75. + if ((new = cache_scan_free(name, addr, class, now, flags, &target_crec, &target_uid)))
  76. {
  77. /* We're trying to insert a record over one from
  78. /etc/hosts or DHCP, or other config. If the
  79. @@ -553,21 +555,14 @@ static struct crec *really_insert(char *
  80. if (freed_all)
  81. {
  82. - struct all_addr free_addr = new->addr.addr;;
  83. -
  84. -#ifdef HAVE_DNSSEC
  85. - /* For DNSSEC records, addr holds class. */
  86. - if (new->flags & (F_DS | F_DNSKEY))
  87. - free_addr.addr.dnssec.class = new->uid;
  88. -#endif
  89. -
  90. + /* For DNSSEC records, uid holds class. */
  91. free_avail = 1; /* Must be free space now. */
  92. - cache_scan_free(cache_get_name(new), &free_addr, now, new->flags, NULL, NULL);
  93. + cache_scan_free(cache_get_name(new), &new->addr.addr, new->uid, now, new->flags, NULL, NULL);
  94. daemon->metrics[METRIC_DNS_CACHE_LIVE_FREED]++;
  95. }
  96. else
  97. {
  98. - cache_scan_free(NULL, NULL, now, 0, NULL, NULL);
  99. + cache_scan_free(NULL, NULL, class, now, 0, NULL, NULL);
  100. freed_all = 1;
  101. }
  102. }
  103. @@ -615,15 +610,13 @@ static struct crec *really_insert(char *
  104. else
  105. *cache_get_name(new) = 0;
  106. - if (addr)
  107. - {
  108. #ifdef HAVE_DNSSEC
  109. - if (flags & (F_DS | F_DNSKEY))
  110. - new->uid = addr->addr.dnssec.class;
  111. - else
  112. + if (flags & (F_DS | F_DNSKEY))
  113. + new->uid = class;
  114. #endif
  115. - new->addr.addr = *addr;
  116. - }
  117. +
  118. + if (addr)
  119. + new->addr.addr = *addr;
  120. new->ttd = now + (time_t)ttl;
  121. new->next = new_chain;
  122. @@ -747,11 +740,11 @@ int cache_recv_insert(time_t now, int fd
  123. {
  124. if (!read_write(fd, (unsigned char *)&addr, sizeof(addr), 1))
  125. return 0;
  126. - crecp = really_insert(daemon->namebuff, &addr, now, ttl, flags);
  127. + crecp = really_insert(daemon->namebuff, &addr, C_IN, now, ttl, flags);
  128. }
  129. else if (flags & F_CNAME)
  130. {
  131. - struct crec *newc = really_insert(daemon->namebuff, NULL, now, ttl, flags);
  132. + struct crec *newc = really_insert(daemon->namebuff, NULL, C_IN, now, ttl, flags);
  133. /* This relies on the fact the the target of a CNAME immediately preceeds
  134. it because of the order of extraction in extract_addresses, and
  135. the order reversal on the new_chain. */
  136. @@ -780,10 +773,8 @@ int cache_recv_insert(time_t now, int fd
  137. if (!read_write(fd, (unsigned char *)&class, sizeof(class), 1))
  138. return 0;
  139. - /* Cache needs to known class for DNSSEC stuff */
  140. - addr.addr.dnssec.class = class;
  141. -
  142. - crecp = really_insert(daemon->namebuff, &addr, now, ttl, flags);
  143. +
  144. + crecp = really_insert(daemon->namebuff, NULL, class, now, ttl, flags);
  145. if (flags & F_DNSKEY)
  146. {
  147. @@ -1463,7 +1454,7 @@ void cache_add_dhcp_entry(char *host_nam
  148. }
  149. else if (!(crec->flags & F_DHCP))
  150. {
  151. - cache_scan_free(host_name, NULL, 0, crec->flags & (flags | F_CNAME | F_FORWARD), NULL, NULL);
  152. + cache_scan_free(host_name, NULL, C_IN, 0, crec->flags & (flags | F_CNAME | F_FORWARD), NULL, NULL);
  153. /* scan_free deletes all addresses associated with name */
  154. break;
  155. }
  156. @@ -1490,7 +1481,7 @@ void cache_add_dhcp_entry(char *host_nam
  157. if (crec->flags & F_NEG)
  158. {
  159. flags |= F_REVERSE;
  160. - cache_scan_free(NULL, (struct all_addr *)host_address, 0, flags, NULL, NULL);
  161. + cache_scan_free(NULL, (struct all_addr *)host_address, C_IN, 0, flags, NULL, NULL);
  162. }
  163. }
  164. else
  165. --- a/src/dnsmasq.h
  166. +++ b/src/dnsmasq.h
  167. @@ -1144,7 +1144,7 @@ struct crec *cache_find_by_name(struct c
  168. void cache_end_insert(void);
  169. void cache_start_insert(void);
  170. int cache_recv_insert(time_t now, int fd);
  171. -struct crec *cache_insert(char *name, struct all_addr *addr,
  172. +struct crec *cache_insert(char *name, struct all_addr *addr, unsigned short class,
  173. time_t now, unsigned long ttl, unsigned short flags);
  174. void cache_reload(void);
  175. void cache_add_dhcp_entry(char *host_name, int prot, struct all_addr *host_address, time_t ttd);
  176. --- a/src/dnssec.c
  177. +++ b/src/dnssec.c
  178. @@ -798,12 +798,9 @@ int dnssec_validate_by_ds(time_t now, st
  179. algo = *p++;
  180. keytag = dnskey_keytag(algo, flags, p, rdlen - 4);
  181. - /* Cache needs to known class for DNSSEC stuff */
  182. - a.addr.dnssec.class = class;
  183. -
  184. if ((key = blockdata_alloc((char*)p, rdlen - 4)))
  185. {
  186. - if (!(recp1 = cache_insert(name, &a, now, ttl, F_FORWARD | F_DNSKEY | F_DNSSECOK)))
  187. + if (!(recp1 = cache_insert(name, &a, class, now, ttl, F_FORWARD | F_DNSKEY | F_DNSSECOK)))
  188. {
  189. blockdata_free(key);
  190. return STAT_BOGUS;
  191. @@ -927,12 +924,9 @@ int dnssec_validate_ds(time_t now, struc
  192. algo = *p++;
  193. digest = *p++;
  194. - /* Cache needs to known class for DNSSEC stuff */
  195. - a.addr.dnssec.class = class;
  196. -
  197. if ((key = blockdata_alloc((char*)p, rdlen - 4)))
  198. {
  199. - if (!(crecp = cache_insert(name, &a, now, ttl, F_FORWARD | F_DS | F_DNSSECOK)))
  200. + if (!(crecp = cache_insert(name, NULL, class, now, ttl, F_FORWARD | F_DS | F_DNSSECOK)))
  201. {
  202. blockdata_free(key);
  203. return STAT_BOGUS;
  204. @@ -1021,8 +1015,7 @@ int dnssec_validate_ds(time_t now, struc
  205. {
  206. cache_start_insert();
  207. - a.addr.dnssec.class = class;
  208. - if (!cache_insert(name, &a, now, ttl, flags))
  209. + if (!cache_insert(name, NULL, class, now, ttl, flags))
  210. return STAT_BOGUS;
  211. cache_end_insert();
  212. --- a/src/rfc1035.c
  213. +++ b/src/rfc1035.c
  214. @@ -701,7 +701,7 @@ int extract_addresses(struct dns_header
  215. goto cname_loop;
  216. }
  217. - cache_insert(name, &addr, now, cttl, name_encoding | secflag | F_REVERSE);
  218. + cache_insert(name, &addr, C_IN, now, cttl, name_encoding | secflag | F_REVERSE);
  219. found = 1;
  220. }
  221. @@ -719,7 +719,7 @@ int extract_addresses(struct dns_header
  222. ttl = find_soa(header, qlen, NULL, doctored);
  223. }
  224. if (ttl)
  225. - cache_insert(NULL, &addr, now, ttl, name_encoding | F_REVERSE | F_NEG | flags | (secure ? F_DNSSECOK : 0));
  226. + cache_insert(NULL, &addr, C_IN, now, ttl, name_encoding | F_REVERSE | F_NEG | flags | (secure ? F_DNSSECOK : 0));
  227. }
  228. }
  229. else
  230. @@ -773,7 +773,7 @@ int extract_addresses(struct dns_header
  231. {
  232. if (!cname_count--)
  233. return 0; /* looped CNAMES */
  234. - newc = cache_insert(name, NULL, now, attl, F_CNAME | F_FORWARD | secflag);
  235. + newc = cache_insert(name, NULL, C_IN, now, attl, F_CNAME | F_FORWARD | secflag);
  236. if (newc)
  237. {
  238. newc->addr.cname.target.cache = NULL;
  239. @@ -833,7 +833,7 @@ int extract_addresses(struct dns_header
  240. }
  241. #endif
  242. - newc = cache_insert(name, &addr, now, attl, flags | F_FORWARD | secflag);
  243. + newc = cache_insert(name, &addr, C_IN, now, attl, flags | F_FORWARD | secflag);
  244. if (newc && cpp)
  245. {
  246. next_uid(newc);
  247. @@ -860,7 +860,7 @@ int extract_addresses(struct dns_header
  248. pointing at this, inherit its TTL */
  249. if (ttl || cpp)
  250. {
  251. - newc = cache_insert(name, NULL, now, ttl ? ttl : cttl, F_FORWARD | F_NEG | flags | (secure ? F_DNSSECOK : 0));
  252. + newc = cache_insert(name, NULL, C_IN, now, ttl ? ttl : cttl, F_FORWARD | F_NEG | flags | (secure ? F_DNSSECOK : 0));
  253. if (newc && cpp)
  254. {
  255. next_uid(newc);
  256. @@ -1054,7 +1054,7 @@ int check_for_bogus_wildcard(struct dns_
  257. /* Found a bogus address. Insert that info here, since there no SOA record
  258. to get the ttl from in the normal processing */
  259. cache_start_insert();
  260. - cache_insert(name, NULL, now, ttl, F_IPV4 | F_FORWARD | F_NEG | F_NXDOMAIN);
  261. + cache_insert(name, NULL, C_IN, now, ttl, F_IPV4 | F_FORWARD | F_NEG | F_NXDOMAIN);
  262. cache_end_insert();
  263. return 1;