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/57] Tidy address-union handling: move class into explicit
7 This moves the class argument to cache-insert into an argument,
8 rather then overloading a union in the address argument. Note that
9 tha class is NOT stored in the cache other than for DS/DNSKEY entries,
10 so must always be C_IN except for these. The data-extraction code
11 ensures this as it only attempts to cache C_IN class records.
13 Signed-off-by: Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
15 src/cache.c | 57 ++++++++++++++++++++++-----------------------------
17 src/dnssec.c | 13 +++---------
18 src/rfc1035.c | 12 +++++------
19 4 files changed, 34 insertions(+), 50 deletions(-)
23 @@ -26,7 +26,7 @@ static union bigname *big_free = NULL;
24 static int bignames_left, hash_size;
26 static void make_non_terminals(struct crec *source);
27 -static struct crec *really_insert(char *name, struct all_addr *addr,
28 +static struct crec *really_insert(char *name, struct all_addr *addr, unsigned short class,
29 time_t now, unsigned long ttl, unsigned short flags);
31 /* type->string mapping: this is also used by the name-hash function as a mixing table. */
32 @@ -330,8 +330,8 @@ static int is_expired(time_t now, struct
36 -static struct crec *cache_scan_free(char *name, struct all_addr *addr, time_t now, unsigned short flags,
37 - struct crec **target_crec, unsigned int *target_uid)
38 +static struct crec *cache_scan_free(char *name, struct all_addr *addr, unsigned short class, time_t now,
39 + unsigned short flags, struct crec **target_crec, unsigned int *target_uid)
41 /* Scan and remove old entries.
42 If (flags & F_FORWARD) then remove any forward entries for name and any expired
43 @@ -350,6 +350,8 @@ static struct crec *cache_scan_free(char
44 This entry will get re-used with the same name, to preserve CNAMEs. */
46 struct crec *crecp, **up;
50 if (flags & F_FORWARD)
52 @@ -381,7 +383,7 @@ static struct crec *cache_scan_free(char
55 /* Deletion has to be class-sensitive for DS and DNSKEY */
56 - if ((flags & crecp->flags & (F_DNSKEY | F_DS)) && crecp->uid == addr->addr.dnssec.class)
57 + if ((flags & crecp->flags & (F_DNSKEY | F_DS)) && crecp->uid == class)
59 if (crecp->flags & F_CONFIG)
61 @@ -464,7 +466,7 @@ void cache_start_insert(void)
65 -struct crec *cache_insert(char *name, struct all_addr *addr,
66 +struct crec *cache_insert(char *name, struct all_addr *addr, unsigned short class,
67 time_t now, unsigned long ttl, unsigned short flags)
69 /* Don't log DNSSEC records here, done elsewhere */
70 @@ -478,11 +480,11 @@ struct crec *cache_insert(char *name, st
71 ttl = daemon->min_cache_ttl;
74 - return really_insert(name, addr, now, ttl, flags);
75 + return really_insert(name, addr, class, now, ttl, flags);
79 -static struct crec *really_insert(char *name, struct all_addr *addr,
80 +static struct crec *really_insert(char *name, struct all_addr *addr, unsigned short class,
81 time_t now, unsigned long ttl, unsigned short flags)
83 struct crec *new, *target_crec = NULL;
84 @@ -497,7 +499,7 @@ static struct crec *really_insert(char *
86 /* First remove any expired entries and entries for the name/address we
87 are currently inserting. */
88 - if ((new = cache_scan_free(name, addr, now, flags, &target_crec, &target_uid)))
89 + if ((new = cache_scan_free(name, addr, class, now, flags, &target_crec, &target_uid)))
91 /* We're trying to insert a record over one from
92 /etc/hosts or DHCP, or other config. If the
93 @@ -553,21 +555,14 @@ static struct crec *really_insert(char *
97 - struct all_addr free_addr = new->addr.addr;;
100 - /* For DNSSEC records, addr holds class. */
101 - if (new->flags & (F_DS | F_DNSKEY))
102 - free_addr.addr.dnssec.class = new->uid;
105 + /* For DNSSEC records, uid holds class. */
106 free_avail = 1; /* Must be free space now. */
107 - cache_scan_free(cache_get_name(new), &free_addr, now, new->flags, NULL, NULL);
108 + cache_scan_free(cache_get_name(new), &new->addr.addr, new->uid, now, new->flags, NULL, NULL);
109 daemon->metrics[METRIC_DNS_CACHE_LIVE_FREED]++;
113 - cache_scan_free(NULL, NULL, now, 0, NULL, NULL);
114 + cache_scan_free(NULL, NULL, class, now, 0, NULL, NULL);
118 @@ -615,15 +610,13 @@ static struct crec *really_insert(char *
120 *cache_get_name(new) = 0;
125 - if (flags & (F_DS | F_DNSKEY))
126 - new->uid = addr->addr.dnssec.class;
128 + if (flags & (F_DS | F_DNSKEY))
131 - new->addr.addr = *addr;
135 + new->addr.addr = *addr;
137 new->ttd = now + (time_t)ttl;
138 new->next = new_chain;
139 @@ -747,11 +740,11 @@ int cache_recv_insert(time_t now, int fd
141 if (!read_write(fd, (unsigned char *)&addr, sizeof(addr), 1))
143 - crecp = really_insert(daemon->namebuff, &addr, now, ttl, flags);
144 + crecp = really_insert(daemon->namebuff, &addr, C_IN, now, ttl, flags);
146 else if (flags & F_CNAME)
148 - struct crec *newc = really_insert(daemon->namebuff, NULL, now, ttl, flags);
149 + struct crec *newc = really_insert(daemon->namebuff, NULL, C_IN, now, ttl, flags);
150 /* This relies on the fact the the target of a CNAME immediately preceeds
151 it because of the order of extraction in extract_addresses, and
152 the order reversal on the new_chain. */
153 @@ -780,10 +773,8 @@ int cache_recv_insert(time_t now, int fd
155 if (!read_write(fd, (unsigned char *)&class, sizeof(class), 1))
157 - /* Cache needs to known class for DNSSEC stuff */
158 - addr.addr.dnssec.class = class;
160 - crecp = really_insert(daemon->namebuff, &addr, now, ttl, flags);
162 + crecp = really_insert(daemon->namebuff, NULL, class, now, ttl, flags);
164 if (flags & F_DNSKEY)
166 @@ -1463,7 +1454,7 @@ void cache_add_dhcp_entry(char *host_nam
168 else if (!(crec->flags & F_DHCP))
170 - cache_scan_free(host_name, NULL, 0, crec->flags & (flags | F_CNAME | F_FORWARD), NULL, NULL);
171 + cache_scan_free(host_name, NULL, C_IN, 0, crec->flags & (flags | F_CNAME | F_FORWARD), NULL, NULL);
172 /* scan_free deletes all addresses associated with name */
175 @@ -1490,7 +1481,7 @@ void cache_add_dhcp_entry(char *host_nam
176 if (crec->flags & F_NEG)
179 - cache_scan_free(NULL, (struct all_addr *)host_address, 0, flags, NULL, NULL);
180 + cache_scan_free(NULL, (struct all_addr *)host_address, C_IN, 0, flags, NULL, NULL);
186 @@ -1144,7 +1144,7 @@ struct crec *cache_find_by_name(struct c
187 void cache_end_insert(void);
188 void cache_start_insert(void);
189 int cache_recv_insert(time_t now, int fd);
190 -struct crec *cache_insert(char *name, struct all_addr *addr,
191 +struct crec *cache_insert(char *name, struct all_addr *addr, unsigned short class,
192 time_t now, unsigned long ttl, unsigned short flags);
193 void cache_reload(void);
194 void cache_add_dhcp_entry(char *host_name, int prot, struct all_addr *host_address, time_t ttd);
197 @@ -798,12 +798,9 @@ int dnssec_validate_by_ds(time_t now, st
199 keytag = dnskey_keytag(algo, flags, p, rdlen - 4);
201 - /* Cache needs to known class for DNSSEC stuff */
202 - a.addr.dnssec.class = class;
204 if ((key = blockdata_alloc((char*)p, rdlen - 4)))
206 - if (!(recp1 = cache_insert(name, &a, now, ttl, F_FORWARD | F_DNSKEY | F_DNSSECOK)))
207 + if (!(recp1 = cache_insert(name, &a, class, now, ttl, F_FORWARD | F_DNSKEY | F_DNSSECOK)))
211 @@ -927,12 +924,9 @@ int dnssec_validate_ds(time_t now, struc
215 - /* Cache needs to known class for DNSSEC stuff */
216 - a.addr.dnssec.class = class;
218 if ((key = blockdata_alloc((char*)p, rdlen - 4)))
220 - if (!(crecp = cache_insert(name, &a, now, ttl, F_FORWARD | F_DS | F_DNSSECOK)))
221 + if (!(crecp = cache_insert(name, NULL, class, now, ttl, F_FORWARD | F_DS | F_DNSSECOK)))
225 @@ -1021,8 +1015,7 @@ int dnssec_validate_ds(time_t now, struc
227 cache_start_insert();
229 - a.addr.dnssec.class = class;
230 - if (!cache_insert(name, &a, now, ttl, flags))
231 + if (!cache_insert(name, NULL, class, now, ttl, flags))
237 @@ -701,7 +701,7 @@ int extract_addresses(struct dns_header
241 - cache_insert(name, &addr, now, cttl, name_encoding | secflag | F_REVERSE);
242 + cache_insert(name, &addr, C_IN, now, cttl, name_encoding | secflag | F_REVERSE);
246 @@ -719,7 +719,7 @@ int extract_addresses(struct dns_header
247 ttl = find_soa(header, qlen, NULL, doctored);
250 - cache_insert(NULL, &addr, now, ttl, name_encoding | F_REVERSE | F_NEG | flags | (secure ? F_DNSSECOK : 0));
251 + cache_insert(NULL, &addr, C_IN, now, ttl, name_encoding | F_REVERSE | F_NEG | flags | (secure ? F_DNSSECOK : 0));
255 @@ -773,7 +773,7 @@ int extract_addresses(struct dns_header
258 return 0; /* looped CNAMES */
259 - newc = cache_insert(name, NULL, now, attl, F_CNAME | F_FORWARD | secflag);
260 + newc = cache_insert(name, NULL, C_IN, now, attl, F_CNAME | F_FORWARD | secflag);
263 newc->addr.cname.target.cache = NULL;
264 @@ -833,7 +833,7 @@ int extract_addresses(struct dns_header
268 - newc = cache_insert(name, &addr, now, attl, flags | F_FORWARD | secflag);
269 + newc = cache_insert(name, &addr, C_IN, now, attl, flags | F_FORWARD | secflag);
273 @@ -860,7 +860,7 @@ int extract_addresses(struct dns_header
274 pointing at this, inherit its TTL */
277 - newc = cache_insert(name, NULL, now, ttl ? ttl : cttl, F_FORWARD | F_NEG | flags | (secure ? F_DNSSECOK : 0));
278 + newc = cache_insert(name, NULL, C_IN, now, ttl ? ttl : cttl, F_FORWARD | F_NEG | flags | (secure ? F_DNSSECOK : 0));
282 @@ -1054,7 +1054,7 @@ int check_for_bogus_wildcard(struct dns_
283 /* Found a bogus address. Insert that info here, since there no SOA record
284 to get the ttl from in the normal processing */
285 cache_start_insert();
286 - cache_insert(name, NULL, now, ttl, F_IPV4 | F_FORWARD | F_NEG | F_NXDOMAIN);
287 + cache_insert(name, NULL, C_IN, now, ttl, F_IPV4 | F_FORWARD | F_NEG | F_NXDOMAIN);