Simon Kelley | 5954608 | 2012-01-06 20:02:04 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2012 Simon Kelley |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License as published by |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 5 | the Free Software Foundation; version 2 dated June, 1991, or |
| 6 | (at your option) version 3 dated 29 June, 2007. |
| 7 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 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. |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 12 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include "dnsmasq.h" |
| 18 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 19 | static struct crec *cache_head = NULL, *cache_tail = NULL, **hash_table = NULL; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 20 | #ifdef HAVE_DHCP |
| 21 | static struct crec *dhcp_spare = NULL; |
| 22 | #endif |
| 23 | static struct crec *new_chain = NULL; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 24 | static int cache_inserted = 0, cache_live_freed = 0, insert_error; |
| 25 | static union bigname *big_free = NULL; |
| 26 | static int bignames_left, hash_size; |
| 27 | static int uid = 0; |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 28 | #ifdef HAVE_DNSSEC |
| 29 | static struct keydata *keyblock_free = NULL; |
| 30 | #endif |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 31 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 32 | /* type->string mapping: this is also used by the name-hash function as a mixing table. */ |
| 33 | static const struct { |
| 34 | unsigned int type; |
| 35 | const char * const name; |
| 36 | } typestr[] = { |
| 37 | { 1, "A" }, |
| 38 | { 2, "NS" }, |
| 39 | { 5, "CNAME" }, |
| 40 | { 6, "SOA" }, |
| 41 | { 10, "NULL" }, |
| 42 | { 11, "WKS" }, |
| 43 | { 12, "PTR" }, |
| 44 | { 13, "HINFO" }, |
| 45 | { 15, "MX" }, |
| 46 | { 16, "TXT" }, |
| 47 | { 22, "NSAP" }, |
| 48 | { 23, "NSAP_PTR" }, |
| 49 | { 24, "SIG" }, |
| 50 | { 25, "KEY" }, |
| 51 | { 28, "AAAA" }, |
| 52 | { 33, "SRV" }, |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 53 | { 35, "NAPTR" }, |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 54 | { 36, "KX" }, |
| 55 | { 37, "CERT" }, |
| 56 | { 38, "A6" }, |
| 57 | { 39, "DNAME" }, |
| 58 | { 41, "OPT" }, |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 59 | { 48, "DNSKEY" }, |
| 60 | { 249, "TKEY" }, |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 61 | { 250, "TSIG" }, |
| 62 | { 251, "IXFR" }, |
| 63 | { 252, "AXFR" }, |
| 64 | { 253, "MAILB" }, |
| 65 | { 254, "MAILA" }, |
| 66 | { 255, "ANY" } |
| 67 | }; |
| 68 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 69 | static void cache_free(struct crec *crecp); |
| 70 | static void cache_unlink(struct crec *crecp); |
| 71 | static void cache_link(struct crec *crecp); |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 72 | static void rehash(int size); |
| 73 | static void cache_hash(struct crec *crecp); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 74 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 75 | void cache_init(void) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 76 | { |
| 77 | struct crec *crecp; |
| 78 | int i; |
| 79 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 80 | bignames_left = daemon->cachesize/10; |
| 81 | |
| 82 | if (daemon->cachesize > 0) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 83 | { |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 84 | crecp = safe_malloc(daemon->cachesize*sizeof(struct crec)); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 85 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 86 | for (i=0; i < daemon->cachesize; i++, crecp++) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 87 | { |
| 88 | cache_link(crecp); |
| 89 | crecp->flags = 0; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 90 | crecp->uid = uid++; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 94 | /* create initial hash table*/ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 95 | rehash(daemon->cachesize); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 98 | /* In most cases, we create the hash table once here by calling this with (hash_table == NULL) |
| 99 | but if the hosts file(s) are big (some people have 50000 ad-block entries), the table |
| 100 | will be much too small, so the hosts reading code calls rehash every 1000 addresses, to |
| 101 | expand the table. */ |
| 102 | static void rehash(int size) |
| 103 | { |
| 104 | struct crec **new, **old, *p, *tmp; |
| 105 | int i, new_size, old_size; |
| 106 | |
| 107 | /* hash_size is a power of two. */ |
| 108 | for (new_size = 64; new_size < size/10; new_size = new_size << 1); |
| 109 | |
| 110 | /* must succeed in getting first instance, failure later is non-fatal */ |
| 111 | if (!hash_table) |
| 112 | new = safe_malloc(new_size * sizeof(struct crec *)); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 113 | else if (new_size <= hash_size || !(new = whine_malloc(new_size * sizeof(struct crec *)))) |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 114 | return; |
| 115 | |
| 116 | for(i = 0; i < new_size; i++) |
| 117 | new[i] = NULL; |
| 118 | |
| 119 | old = hash_table; |
| 120 | old_size = hash_size; |
| 121 | hash_table = new; |
| 122 | hash_size = new_size; |
| 123 | |
| 124 | if (old) |
| 125 | { |
| 126 | for (i = 0; i < old_size; i++) |
| 127 | for (p = old[i]; p ; p = tmp) |
| 128 | { |
| 129 | tmp = p->hash_next; |
| 130 | cache_hash(p); |
| 131 | } |
| 132 | free(old); |
| 133 | } |
| 134 | } |
| 135 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 136 | static struct crec **hash_bucket(char *name) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 137 | { |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 138 | unsigned int c, val = 017465; /* Barker code - minimum self-correlation in cyclic shift */ |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 139 | const unsigned char *mix_tab = (const unsigned char*)typestr; |
| 140 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 141 | while((c = (unsigned char) *name++)) |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 142 | { |
| 143 | /* don't use tolower and friends here - they may be messed up by LOCALE */ |
| 144 | if (c >= 'A' && c <= 'Z') |
| 145 | c += 'a' - 'A'; |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 146 | val = ((val << 7) | (val >> (32 - 7))) + (mix_tab[(val + c) & 0x3F] ^ c); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 147 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 148 | |
| 149 | /* hash_size is a power of two */ |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 150 | return hash_table + ((val ^ (val >> 16)) & (hash_size - 1)); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static void cache_hash(struct crec *crecp) |
| 154 | { |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 155 | /* maintain an invariant that all entries with F_REVERSE set |
| 156 | are at the start of the hash-chain and all non-reverse |
| 157 | immortal entries are at the end of the hash-chain. |
| 158 | This allows reverse searches and garbage collection to be optimised */ |
| 159 | |
| 160 | struct crec **up = hash_bucket(cache_get_name(crecp)); |
| 161 | |
| 162 | if (!(crecp->flags & F_REVERSE)) |
| 163 | { |
| 164 | while (*up && ((*up)->flags & F_REVERSE)) |
| 165 | up = &((*up)->hash_next); |
| 166 | |
| 167 | if (crecp->flags & F_IMMORTAL) |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 168 | while (*up && !((*up)->flags & F_IMMORTAL)) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 169 | up = &((*up)->hash_next); |
| 170 | } |
| 171 | crecp->hash_next = *up; |
| 172 | *up = crecp; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static void cache_free(struct crec *crecp) |
| 176 | { |
| 177 | crecp->flags &= ~F_FORWARD; |
| 178 | crecp->flags &= ~F_REVERSE; |
Simon Kelley | 26128d2 | 2004-11-14 16:43:54 +0000 | [diff] [blame] | 179 | crecp->uid = uid++; /* invalidate CNAMES pointing to this. */ |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 180 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 181 | if (cache_tail) |
| 182 | cache_tail->next = crecp; |
| 183 | else |
| 184 | cache_head = crecp; |
| 185 | crecp->prev = cache_tail; |
| 186 | crecp->next = NULL; |
| 187 | cache_tail = crecp; |
| 188 | |
| 189 | /* retrieve big name for further use. */ |
| 190 | if (crecp->flags & F_BIGNAME) |
| 191 | { |
| 192 | crecp->name.bname->next = big_free; |
| 193 | big_free = crecp->name.bname; |
| 194 | crecp->flags &= ~F_BIGNAME; |
| 195 | } |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 196 | #ifdef HAVE_DNSSEC |
| 197 | else if (crecp->flags & (F_DNSKEY | F_DS)) |
| 198 | keydata_free(crecp->addr.key.keydata); |
| 199 | #endif |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /* insert a new cache entry at the head of the list (youngest entry) */ |
| 203 | static void cache_link(struct crec *crecp) |
| 204 | { |
| 205 | if (cache_head) /* check needed for init code */ |
| 206 | cache_head->prev = crecp; |
| 207 | crecp->next = cache_head; |
| 208 | crecp->prev = NULL; |
| 209 | cache_head = crecp; |
| 210 | if (!cache_tail) |
| 211 | cache_tail = crecp; |
| 212 | } |
| 213 | |
| 214 | /* remove an arbitrary cache entry for promotion */ |
| 215 | static void cache_unlink (struct crec *crecp) |
| 216 | { |
| 217 | if (crecp->prev) |
| 218 | crecp->prev->next = crecp->next; |
| 219 | else |
| 220 | cache_head = crecp->next; |
| 221 | |
| 222 | if (crecp->next) |
| 223 | crecp->next->prev = crecp->prev; |
| 224 | else |
| 225 | cache_tail = crecp->prev; |
| 226 | } |
| 227 | |
| 228 | char *cache_get_name(struct crec *crecp) |
| 229 | { |
| 230 | if (crecp->flags & F_BIGNAME) |
| 231 | return crecp->name.bname->name; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 232 | else if (crecp->flags & F_NAMEP) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 233 | return crecp->name.namep; |
| 234 | |
| 235 | return crecp->name.sname; |
| 236 | } |
| 237 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 238 | static int is_outdated_cname_pointer(struct crec *crecp) |
| 239 | { |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 240 | if (!(crecp->flags & F_CNAME)) |
| 241 | return 0; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 242 | |
Simon Kelley | cdbee9a | 2012-04-04 21:55:59 +0100 | [diff] [blame] | 243 | /* NB. record may be reused as DS or DNSKEY, where uid is |
| 244 | overloaded for something completely different */ |
| 245 | if (crecp->addr.cname.cache && |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 246 | (crecp->addr.cname.cache->flags & (F_IPV4 | F_IPV6 | F_CNAME)) && |
Simon Kelley | cdbee9a | 2012-04-04 21:55:59 +0100 | [diff] [blame] | 247 | crecp->addr.cname.uid == crecp->addr.cname.cache->uid) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 248 | return 0; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 249 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 250 | return 1; |
| 251 | } |
| 252 | |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 253 | static int is_expired(time_t now, struct crec *crecp) |
| 254 | { |
| 255 | if (crecp->flags & F_IMMORTAL) |
| 256 | return 0; |
| 257 | |
| 258 | if (difftime(now, crecp->ttd) < 0) |
| 259 | return 0; |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 260 | |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 261 | return 1; |
| 262 | } |
| 263 | |
| 264 | static int cache_scan_free(char *name, struct all_addr *addr, time_t now, unsigned short flags) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 265 | { |
| 266 | /* Scan and remove old entries. |
| 267 | If (flags & F_FORWARD) then remove any forward entries for name and any expired |
| 268 | entries but only in the same hash bucket as name. |
| 269 | If (flags & F_REVERSE) then remove any reverse entries for addr and any expired |
| 270 | entries in the whole cache. |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 271 | If (flags == 0) remove any expired entries in the whole cache. |
| 272 | |
| 273 | In the flags & F_FORWARD case, the return code is valid, and returns zero if the |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 274 | name exists in the cache as a HOSTS or DHCP entry (these are never deleted) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 275 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 276 | We take advantage of the fact that hash chains have stuff in the order <reverse>,<other>,<immortal> |
| 277 | so that when we hit an entry which isn't reverse and is immortal, we're done. */ |
| 278 | |
| 279 | struct crec *crecp, **up; |
| 280 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 281 | if (flags & F_FORWARD) |
| 282 | { |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 283 | for (up = hash_bucket(name), crecp = *up; crecp; crecp = crecp->hash_next) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 284 | if (is_expired(now, crecp) || is_outdated_cname_pointer(crecp)) |
| 285 | { |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 286 | *up = crecp->hash_next; |
| 287 | if (!(crecp->flags & (F_HOSTS | F_DHCP))) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 288 | { |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 289 | cache_unlink(crecp); |
| 290 | cache_free(crecp); |
| 291 | } |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 292 | } |
| 293 | else if ((crecp->flags & F_FORWARD) && |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 294 | ((flags & crecp->flags & F_TYPE) || ((crecp->flags | flags) & F_CNAME)) && |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 295 | hostname_isequal(cache_get_name(crecp), name)) |
| 296 | { |
| 297 | if (crecp->flags & (F_HOSTS | F_DHCP)) |
| 298 | return 0; |
| 299 | *up = crecp->hash_next; |
| 300 | cache_unlink(crecp); |
| 301 | cache_free(crecp); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 302 | } |
| 303 | else |
| 304 | up = &crecp->hash_next; |
| 305 | } |
| 306 | else |
| 307 | { |
| 308 | int i; |
| 309 | #ifdef HAVE_IPV6 |
| 310 | int addrlen = (flags & F_IPV6) ? IN6ADDRSZ : INADDRSZ; |
| 311 | #else |
| 312 | int addrlen = INADDRSZ; |
| 313 | #endif |
| 314 | for (i = 0; i < hash_size; i++) |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 315 | for (crecp = hash_table[i], up = &hash_table[i]; |
| 316 | crecp && ((crecp->flags & F_REVERSE) || !(crecp->flags & F_IMMORTAL)); |
| 317 | crecp = crecp->hash_next) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 318 | if (is_expired(now, crecp)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 319 | { |
| 320 | *up = crecp->hash_next; |
| 321 | if (!(crecp->flags & (F_HOSTS | F_DHCP))) |
| 322 | { |
| 323 | cache_unlink(crecp); |
| 324 | cache_free(crecp); |
| 325 | } |
| 326 | } |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 327 | else if (!(crecp->flags & (F_HOSTS | F_DHCP)) && |
| 328 | (flags & crecp->flags & F_REVERSE) && |
| 329 | (flags & crecp->flags & (F_IPV4 | F_IPV6)) && |
| 330 | memcmp(&crecp->addr.addr, addr, addrlen) == 0) |
| 331 | { |
| 332 | *up = crecp->hash_next; |
| 333 | cache_unlink(crecp); |
| 334 | cache_free(crecp); |
| 335 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 336 | else |
| 337 | up = &crecp->hash_next; |
| 338 | } |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 339 | |
| 340 | return 1; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /* Note: The normal calling sequence is |
| 344 | cache_start_insert |
| 345 | cache_insert * n |
| 346 | cache_end_insert |
| 347 | |
| 348 | but an abort can cause the cache_end_insert to be missed |
| 349 | in which can the next cache_start_insert cleans things up. */ |
| 350 | |
| 351 | void cache_start_insert(void) |
| 352 | { |
| 353 | /* Free any entries which didn't get committed during the last |
| 354 | insert due to error. |
| 355 | */ |
| 356 | while (new_chain) |
| 357 | { |
| 358 | struct crec *tmp = new_chain->next; |
| 359 | cache_free(new_chain); |
| 360 | new_chain = tmp; |
| 361 | } |
| 362 | new_chain = NULL; |
| 363 | insert_error = 0; |
| 364 | } |
| 365 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 366 | struct crec *cache_insert(char *name, struct all_addr *addr, |
| 367 | time_t now, unsigned long ttl, unsigned short flags) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 368 | { |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 369 | struct crec *new; |
| 370 | union bigname *big_name = NULL; |
| 371 | int freed_all = flags & F_REVERSE; |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 372 | int free_avail = 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 373 | |
Simon Kelley | 1d86041 | 2012-09-20 20:48:04 +0100 | [diff] [blame^] | 374 | if(daemon->max_cache_ttl < ttl) |
| 375 | ttl = daemon->max_cache_ttl; |
| 376 | |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 377 | /* Don't log keys */ |
| 378 | if (flags & (F_IPV4 | F_IPV6)) |
| 379 | log_query(flags | F_UPSTREAM, name, addr, NULL); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 380 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 381 | /* if previous insertion failed give up now. */ |
| 382 | if (insert_error) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 383 | return NULL; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 384 | |
| 385 | /* First remove any expired entries and entries for the name/address we |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 386 | are currently inserting. Fail is we attempt to delete a name from |
| 387 | /etc/hosts or DHCP. */ |
| 388 | if (!cache_scan_free(name, addr, now, flags)) |
| 389 | { |
| 390 | insert_error = 1; |
| 391 | return NULL; |
| 392 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 393 | |
| 394 | /* Now get a cache entry from the end of the LRU list */ |
| 395 | while (1) { |
| 396 | if (!(new = cache_tail)) /* no entries left - cache is too small, bail */ |
| 397 | { |
| 398 | insert_error = 1; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 399 | return NULL; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /* End of LRU list is still in use: if we didn't scan all the hash |
| 403 | chains for expired entries do that now. If we already tried that |
| 404 | then it's time to start spilling things. */ |
| 405 | |
| 406 | if (new->flags & (F_FORWARD | F_REVERSE)) |
| 407 | { |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 408 | /* If free_avail set, we believe that an entry has been freed. |
| 409 | Bugs have been known to make this not true, resulting in |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 410 | a tight loop here. If that happens, abandon the |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 411 | insert. Once in this state, all inserts will probably fail. */ |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 412 | if (free_avail) |
| 413 | { |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 414 | insert_error = 1; |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 415 | return NULL; |
| 416 | } |
| 417 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 418 | if (freed_all) |
| 419 | { |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 420 | free_avail = 1; /* Must be free space now. */ |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 421 | cache_scan_free(cache_get_name(new), &new->addr.addr, now, new->flags); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 422 | cache_live_freed++; |
| 423 | } |
| 424 | else |
| 425 | { |
| 426 | cache_scan_free(NULL, NULL, now, 0); |
| 427 | freed_all = 1; |
| 428 | } |
| 429 | continue; |
| 430 | } |
| 431 | |
| 432 | /* Check if we need to and can allocate extra memory for a long name. |
| 433 | If that fails, give up now. */ |
| 434 | if (name && (strlen(name) > SMALLDNAME-1)) |
| 435 | { |
| 436 | if (big_free) |
| 437 | { |
| 438 | big_name = big_free; |
| 439 | big_free = big_free->next; |
| 440 | } |
| 441 | else if (!bignames_left || |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 442 | !(big_name = (union bigname *)whine_malloc(sizeof(union bigname)))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 443 | { |
| 444 | insert_error = 1; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 445 | return NULL; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 446 | } |
| 447 | else |
| 448 | bignames_left--; |
| 449 | |
| 450 | } |
| 451 | |
| 452 | /* Got the rest: finally grab entry. */ |
| 453 | cache_unlink(new); |
| 454 | break; |
| 455 | } |
| 456 | |
| 457 | new->flags = flags; |
| 458 | if (big_name) |
| 459 | { |
| 460 | new->name.bname = big_name; |
| 461 | new->flags |= F_BIGNAME; |
| 462 | } |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 463 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 464 | if (name) |
| 465 | strcpy(cache_get_name(new), name); |
| 466 | else |
| 467 | *cache_get_name(new) = 0; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 468 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 469 | if (addr) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 470 | new->addr.addr = *addr; |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 471 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 472 | new->ttd = now + (time_t)ttl; |
| 473 | new->next = new_chain; |
| 474 | new_chain = new; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 475 | |
| 476 | return new; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /* after end of insertion, commit the new entries */ |
| 480 | void cache_end_insert(void) |
| 481 | { |
| 482 | if (insert_error) |
| 483 | return; |
| 484 | |
| 485 | while (new_chain) |
| 486 | { |
| 487 | struct crec *tmp = new_chain->next; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 488 | /* drop CNAMEs which didn't find a target. */ |
| 489 | if (is_outdated_cname_pointer(new_chain)) |
| 490 | cache_free(new_chain); |
| 491 | else |
| 492 | { |
| 493 | cache_hash(new_chain); |
| 494 | cache_link(new_chain); |
| 495 | cache_inserted++; |
| 496 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 497 | new_chain = tmp; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 498 | } |
| 499 | new_chain = NULL; |
| 500 | } |
| 501 | |
| 502 | struct crec *cache_find_by_name(struct crec *crecp, char *name, time_t now, unsigned short prot) |
| 503 | { |
| 504 | struct crec *ans; |
| 505 | |
| 506 | if (crecp) /* iterating */ |
| 507 | ans = crecp->next; |
| 508 | else |
| 509 | { |
| 510 | /* first search, look for relevant entries and push to top of list |
| 511 | also free anything which has expired */ |
| 512 | struct crec *next, **up, **insert = NULL, **chainp = &ans; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 513 | unsigned short ins_flags = 0; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 514 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 515 | for (up = hash_bucket(name), crecp = *up; crecp; crecp = next) |
| 516 | { |
| 517 | next = crecp->hash_next; |
| 518 | |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 519 | if (!is_expired(now, crecp) && !is_outdated_cname_pointer(crecp)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 520 | { |
| 521 | if ((crecp->flags & F_FORWARD) && |
| 522 | (crecp->flags & prot) && |
| 523 | hostname_isequal(cache_get_name(crecp), name)) |
| 524 | { |
| 525 | if (crecp->flags & (F_HOSTS | F_DHCP)) |
| 526 | { |
| 527 | *chainp = crecp; |
| 528 | chainp = &crecp->next; |
| 529 | } |
| 530 | else |
| 531 | { |
| 532 | cache_unlink(crecp); |
| 533 | cache_link(crecp); |
| 534 | } |
| 535 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 536 | /* Move all but the first entry up the hash chain |
| 537 | this implements round-robin. |
| 538 | Make sure that re-ordering doesn't break the hash-chain |
| 539 | order invariants. |
| 540 | */ |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 541 | if (insert && (crecp->flags & (F_REVERSE | F_IMMORTAL)) == ins_flags) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 542 | { |
| 543 | *up = crecp->hash_next; |
| 544 | crecp->hash_next = *insert; |
| 545 | *insert = crecp; |
| 546 | insert = &crecp->hash_next; |
| 547 | } |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 548 | else |
| 549 | { |
| 550 | if (!insert) |
| 551 | { |
| 552 | insert = up; |
| 553 | ins_flags = crecp->flags & (F_REVERSE | F_IMMORTAL); |
| 554 | } |
| 555 | up = &crecp->hash_next; |
| 556 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 557 | } |
| 558 | else |
| 559 | /* case : not expired, incorrect entry. */ |
| 560 | up = &crecp->hash_next; |
| 561 | } |
| 562 | else |
| 563 | { |
| 564 | /* expired entry, free it */ |
| 565 | *up = crecp->hash_next; |
| 566 | if (!(crecp->flags & (F_HOSTS | F_DHCP))) |
| 567 | { |
| 568 | cache_unlink(crecp); |
| 569 | cache_free(crecp); |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | *chainp = cache_head; |
| 575 | } |
| 576 | |
| 577 | if (ans && |
| 578 | (ans->flags & F_FORWARD) && |
| 579 | (ans->flags & prot) && |
| 580 | hostname_isequal(cache_get_name(ans), name)) |
| 581 | return ans; |
| 582 | |
| 583 | return NULL; |
| 584 | } |
| 585 | |
| 586 | struct crec *cache_find_by_addr(struct crec *crecp, struct all_addr *addr, |
| 587 | time_t now, unsigned short prot) |
| 588 | { |
| 589 | struct crec *ans; |
| 590 | #ifdef HAVE_IPV6 |
| 591 | int addrlen = (prot == F_IPV6) ? IN6ADDRSZ : INADDRSZ; |
| 592 | #else |
| 593 | int addrlen = INADDRSZ; |
| 594 | #endif |
| 595 | |
| 596 | if (crecp) /* iterating */ |
| 597 | ans = crecp->next; |
| 598 | else |
| 599 | { |
| 600 | /* first search, look for relevant entries and push to top of list |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 601 | also free anything which has expired. All the reverse entries are at the |
| 602 | start of the hash chain, so we can give up when we find the first |
| 603 | non-REVERSE one. */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 604 | int i; |
| 605 | struct crec **up, **chainp = &ans; |
| 606 | |
Simon Kelley | 1b7ecd1 | 2007-02-05 14:57:57 +0000 | [diff] [blame] | 607 | for (i=0; i<hash_size; i++) |
| 608 | for (crecp = hash_table[i], up = &hash_table[i]; |
| 609 | crecp && (crecp->flags & F_REVERSE); |
| 610 | crecp = crecp->hash_next) |
Simon Kelley | f6b7dc4 | 2005-01-23 12:06:08 +0000 | [diff] [blame] | 611 | if (!is_expired(now, crecp)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 612 | { |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 613 | if ((crecp->flags & prot) && |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 614 | memcmp(&crecp->addr.addr, addr, addrlen) == 0) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 615 | { |
| 616 | if (crecp->flags & (F_HOSTS | F_DHCP)) |
| 617 | { |
| 618 | *chainp = crecp; |
| 619 | chainp = &crecp->next; |
| 620 | } |
| 621 | else |
| 622 | { |
| 623 | cache_unlink(crecp); |
| 624 | cache_link(crecp); |
| 625 | } |
| 626 | } |
| 627 | up = &crecp->hash_next; |
| 628 | } |
| 629 | else |
| 630 | { |
| 631 | *up = crecp->hash_next; |
| 632 | if (!(crecp->flags & (F_HOSTS | F_DHCP))) |
| 633 | { |
| 634 | cache_unlink(crecp); |
| 635 | cache_free(crecp); |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | *chainp = cache_head; |
| 640 | } |
| 641 | |
| 642 | if (ans && |
| 643 | (ans->flags & F_REVERSE) && |
| 644 | (ans->flags & prot) && |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 645 | memcmp(&ans->addr.addr, addr, addrlen) == 0) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 646 | return ans; |
| 647 | |
| 648 | return NULL; |
| 649 | } |
| 650 | |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 651 | static void add_hosts_cname(struct crec *target) |
| 652 | { |
| 653 | struct crec *crec; |
| 654 | struct cname *a; |
| 655 | |
| 656 | for (a = daemon->cnames; a; a = a->next) |
| 657 | if (hostname_isequal(cache_get_name(target), a->target) && |
| 658 | (crec = whine_malloc(sizeof(struct crec)))) |
| 659 | { |
| 660 | crec->flags = F_FORWARD | F_IMMORTAL | F_NAMEP | F_HOSTS | F_CNAME; |
| 661 | crec->name.namep = a->alias; |
| 662 | crec->addr.cname.cache = target; |
| 663 | crec->addr.cname.uid = target->uid; |
| 664 | cache_hash(crec); |
| 665 | add_hosts_cname(crec); /* handle chains */ |
| 666 | } |
| 667 | } |
| 668 | |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 669 | static void add_hosts_entry(struct crec *cache, struct all_addr *addr, int addrlen, |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 670 | int index, struct crec **rhash, int hashsz) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 671 | { |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 672 | struct crec *lookup = cache_find_by_name(NULL, cache_get_name(cache), 0, cache->flags & (F_IPV4 | F_IPV6)); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 673 | int i, nameexists = 0; |
Simon Kelley | 205fafa | 2012-01-11 21:31:51 +0000 | [diff] [blame] | 674 | unsigned int j; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 675 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 676 | /* Remove duplicates in hosts files. */ |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 677 | if (lookup && (lookup->flags & F_HOSTS)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 678 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 679 | nameexists = 1; |
| 680 | if (memcmp(&lookup->addr.addr, addr, addrlen) == 0) |
| 681 | { |
| 682 | free(cache); |
| 683 | return; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | /* Ensure there is only one address -> name mapping (first one trumps) |
Simon Kelley | 205fafa | 2012-01-11 21:31:51 +0000 | [diff] [blame] | 688 | We do this by steam here, The entries are kept in hash chains, linked |
| 689 | by ->next (which is unused at this point) held in hash buckets in |
Simon Kelley | 1ab62ae | 2012-01-12 11:33:16 +0000 | [diff] [blame] | 690 | the array rhash, hashed on address. Note that rhash and the values |
| 691 | in ->next are only valid whilst reading hosts files: the buckets are |
| 692 | then freed, and the ->next pointer used for other things. |
Simon Kelley | 205fafa | 2012-01-11 21:31:51 +0000 | [diff] [blame] | 693 | |
Simon Kelley | 1ab62ae | 2012-01-12 11:33:16 +0000 | [diff] [blame] | 694 | Only insert each unique address once into this hashing structure. |
Simon Kelley | 205fafa | 2012-01-11 21:31:51 +0000 | [diff] [blame] | 695 | |
| 696 | This complexity avoids O(n^2) divergent CPU use whilst reading |
| 697 | large (10000 entry) hosts files. */ |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 698 | |
Simon Kelley | 205fafa | 2012-01-11 21:31:51 +0000 | [diff] [blame] | 699 | /* hash address */ |
| 700 | for (j = 0, i = 0; i < addrlen; i++) |
Simon Kelley | 1ab62ae | 2012-01-12 11:33:16 +0000 | [diff] [blame] | 701 | j = (j*2 +((unsigned char *)addr)[i]) % hashsz; |
Simon Kelley | 915363f | 2012-01-11 22:00:48 +0000 | [diff] [blame] | 702 | |
| 703 | for (lookup = rhash[j]; lookup; lookup = lookup->next) |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 704 | if ((lookup->flags & cache->flags & (F_IPV4 | F_IPV6)) && |
Simon Kelley | 205fafa | 2012-01-11 21:31:51 +0000 | [diff] [blame] | 705 | memcmp(&lookup->addr.addr, addr, addrlen) == 0) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 706 | { |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 707 | cache->flags &= ~F_REVERSE; |
Simon Kelley | 205fafa | 2012-01-11 21:31:51 +0000 | [diff] [blame] | 708 | break; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Simon Kelley | 915363f | 2012-01-11 22:00:48 +0000 | [diff] [blame] | 711 | /* maintain address hash chain, insert new unique address */ |
| 712 | if (!lookup) |
| 713 | { |
| 714 | cache->next = rhash[j]; |
| 715 | rhash[j] = cache; |
| 716 | } |
| 717 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 718 | cache->uid = index; |
Simon Kelley | 915363f | 2012-01-11 22:00:48 +0000 | [diff] [blame] | 719 | memcpy(&cache->addr.addr, addr, addrlen); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 720 | cache_hash(cache); |
| 721 | |
| 722 | /* don't need to do alias stuff for second and subsequent addresses. */ |
| 723 | if (!nameexists) |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 724 | add_hosts_cname(cache); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | static int eatspace(FILE *f) |
| 728 | { |
| 729 | int c, nl = 0; |
| 730 | |
| 731 | while (1) |
| 732 | { |
| 733 | if ((c = getc(f)) == '#') |
| 734 | while (c != '\n' && c != EOF) |
| 735 | c = getc(f); |
Simon Kelley | 832af0b | 2007-01-21 20:01:28 +0000 | [diff] [blame] | 736 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 737 | if (c == EOF) |
| 738 | return 1; |
| 739 | |
| 740 | if (!isspace(c)) |
| 741 | { |
| 742 | ungetc(c, f); |
| 743 | return nl; |
| 744 | } |
| 745 | |
| 746 | if (c == '\n') |
| 747 | nl = 1; |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | static int gettok(FILE *f, char *token) |
| 752 | { |
| 753 | int c, count = 0; |
| 754 | |
| 755 | while (1) |
| 756 | { |
| 757 | if ((c = getc(f)) == EOF) |
| 758 | return (count == 0) ? EOF : 1; |
| 759 | |
| 760 | if (isspace(c) || c == '#') |
| 761 | { |
| 762 | ungetc(c, f); |
| 763 | return eatspace(f); |
| 764 | } |
| 765 | |
| 766 | if (count < (MAXDNAME - 1)) |
| 767 | { |
| 768 | token[count++] = c; |
| 769 | token[count] = 0; |
| 770 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 771 | } |
| 772 | } |
| 773 | |
Simon Kelley | 1ab62ae | 2012-01-12 11:33:16 +0000 | [diff] [blame] | 774 | static int read_hostsfile(char *filename, int index, int cache_size, struct crec **rhash, int hashsz) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 775 | { |
| 776 | FILE *f = fopen(filename, "r"); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 777 | char *token = daemon->namebuff, *domain_suffix = NULL; |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 778 | int addr_count = 0, name_count = cache_size, lineno = 0; |
Simon Kelley | 205fafa | 2012-01-11 21:31:51 +0000 | [diff] [blame] | 779 | unsigned short flags = 0; |
| 780 | struct all_addr addr; |
| 781 | int atnl, addrlen = 0; |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 782 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 783 | if (!f) |
| 784 | { |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 785 | my_syslog(LOG_ERR, _("failed to load names from %s: %s"), filename, strerror(errno)); |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 786 | return 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 787 | } |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 788 | |
| 789 | eatspace(f); |
| 790 | |
| 791 | while ((atnl = gettok(f, token)) != EOF) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 792 | { |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 793 | lineno++; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 794 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 795 | if (inet_pton(AF_INET, token, &addr) > 0) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 796 | { |
| 797 | flags = F_HOSTS | F_IMMORTAL | F_FORWARD | F_REVERSE | F_IPV4; |
| 798 | addrlen = INADDRSZ; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 799 | domain_suffix = get_domain(addr.addr.addr4); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 800 | } |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 801 | #ifdef HAVE_IPV6 |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 802 | else if (inet_pton(AF_INET6, token, &addr) > 0) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 803 | { |
| 804 | flags = F_HOSTS | F_IMMORTAL | F_FORWARD | F_REVERSE | F_IPV6; |
| 805 | addrlen = IN6ADDRSZ; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 806 | domain_suffix = get_domain6(&addr.addr.addr6); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 807 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 808 | #endif |
| 809 | else |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 810 | { |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 811 | my_syslog(LOG_ERR, _("bad address at %s line %d"), filename, lineno); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 812 | while (atnl == 0) |
| 813 | atnl = gettok(f, token); |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 814 | continue; |
| 815 | } |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 816 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 817 | addr_count++; |
| 818 | |
| 819 | /* rehash every 1000 names. */ |
| 820 | if ((name_count - cache_size) > 1000) |
| 821 | { |
| 822 | rehash(name_count); |
| 823 | cache_size = name_count; |
| 824 | } |
| 825 | |
| 826 | while (atnl == 0) |
| 827 | { |
| 828 | struct crec *cache; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 829 | int fqdn, nomem; |
| 830 | char *canon; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 831 | |
| 832 | if ((atnl = gettok(f, token)) == EOF) |
| 833 | break; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 834 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 835 | fqdn = !!strchr(token, '.'); |
| 836 | |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 837 | if ((canon = canonicalise(token, &nomem))) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 838 | { |
| 839 | /* If set, add a version of the name with a default domain appended */ |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 840 | if (option_bool(OPT_EXPAND) && domain_suffix && !fqdn && |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 841 | (cache = whine_malloc(sizeof(struct crec) + |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 842 | strlen(canon)+2+strlen(domain_suffix)-SMALLDNAME))) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 843 | { |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 844 | strcpy(cache->name.sname, canon); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 845 | strcat(cache->name.sname, "."); |
| 846 | strcat(cache->name.sname, domain_suffix); |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 847 | cache->flags = flags; |
| 848 | add_hosts_entry(cache, &addr, addrlen, index, rhash, hashsz); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 849 | name_count++; |
| 850 | } |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 851 | if ((cache = whine_malloc(sizeof(struct crec) + strlen(canon)+1-SMALLDNAME))) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 852 | { |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 853 | strcpy(cache->name.sname, canon); |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 854 | cache->flags = flags; |
| 855 | add_hosts_entry(cache, &addr, addrlen, index, rhash, hashsz); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 856 | name_count++; |
| 857 | } |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 858 | free(canon); |
| 859 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 860 | } |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 861 | else if (!nomem) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 862 | my_syslog(LOG_ERR, _("bad name at %s line %d"), filename, lineno); |
| 863 | } |
| 864 | } |
| 865 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 866 | fclose(f); |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 867 | rehash(name_count); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 868 | |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 869 | my_syslog(LOG_INFO, _("read %s - %d addresses"), filename, addr_count); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 870 | |
Simon Kelley | 4011c4e | 2006-10-28 16:26:19 +0100 | [diff] [blame] | 871 | return name_count; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 874 | void cache_reload(void) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 875 | { |
| 876 | struct crec *cache, **up, *tmp; |
Simon Kelley | 1ab62ae | 2012-01-12 11:33:16 +0000 | [diff] [blame] | 877 | int revhashsz, i, total_size = daemon->cachesize; |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 878 | struct hostsfile *ah; |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 879 | struct host_record *hr; |
| 880 | struct name_list *nl; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 881 | |
Simon Kelley | 59353a6 | 2004-11-21 19:34:28 +0000 | [diff] [blame] | 882 | cache_inserted = cache_live_freed = 0; |
| 883 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 884 | for (i=0; i<hash_size; i++) |
| 885 | for (cache = hash_table[i], up = &hash_table[i]; cache; cache = tmp) |
| 886 | { |
| 887 | tmp = cache->hash_next; |
| 888 | if (cache->flags & F_HOSTS) |
| 889 | { |
| 890 | *up = cache->hash_next; |
| 891 | free(cache); |
| 892 | } |
| 893 | else if (!(cache->flags & F_DHCP)) |
| 894 | { |
| 895 | *up = cache->hash_next; |
| 896 | if (cache->flags & F_BIGNAME) |
| 897 | { |
| 898 | cache->name.bname->next = big_free; |
| 899 | big_free = cache->name.bname; |
| 900 | } |
| 901 | cache->flags = 0; |
| 902 | } |
| 903 | else |
| 904 | up = &cache->hash_next; |
| 905 | } |
| 906 | |
Simon Kelley | e759d42 | 2012-03-16 13:18:57 +0000 | [diff] [blame] | 907 | /* borrow the packet buffer for a temporary by-address hash */ |
| 908 | memset(daemon->packet, 0, daemon->packet_buff_sz); |
| 909 | revhashsz = daemon->packet_buff_sz / sizeof(struct crec *); |
| 910 | /* we overwrote the buffer... */ |
| 911 | daemon->srv_save = NULL; |
| 912 | |
| 913 | /* Do host_records in config. */ |
| 914 | for (hr = daemon->host_records; hr; hr = hr->next) |
| 915 | for (nl = hr->names; nl; nl = nl->next) |
| 916 | { |
| 917 | if (hr->addr.s_addr != 0 && |
| 918 | (cache = whine_malloc(sizeof(struct crec)))) |
| 919 | { |
| 920 | cache->name.namep = nl->name; |
| 921 | cache->flags = F_HOSTS | F_IMMORTAL | F_FORWARD | F_REVERSE | F_IPV4 | F_NAMEP | F_CONFIG; |
| 922 | add_hosts_entry(cache, (struct all_addr *)&hr->addr, INADDRSZ, 0, (struct crec **)daemon->packet, revhashsz); |
| 923 | } |
| 924 | #ifdef HAVE_IPV6 |
| 925 | if (!IN6_IS_ADDR_UNSPECIFIED(&hr->addr6) && |
| 926 | (cache = whine_malloc(sizeof(struct crec)))) |
| 927 | { |
| 928 | cache->name.namep = nl->name; |
| 929 | cache->flags = F_HOSTS | F_IMMORTAL | F_FORWARD | F_REVERSE | F_IPV6 | F_NAMEP | F_CONFIG; |
| 930 | add_hosts_entry(cache, (struct all_addr *)&hr->addr6, IN6ADDRSZ, 0, (struct crec **)daemon->packet, revhashsz); |
| 931 | } |
| 932 | #endif |
| 933 | } |
| 934 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 935 | if (option_bool(OPT_NO_HOSTS) && !daemon->addn_hosts) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 936 | { |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 937 | if (daemon->cachesize > 0) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 938 | my_syslog(LOG_INFO, _("cleared cache")); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 939 | return; |
| 940 | } |
Simon Kelley | 1ab62ae | 2012-01-12 11:33:16 +0000 | [diff] [blame] | 941 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 942 | if (!option_bool(OPT_NO_HOSTS)) |
Simon Kelley | 1ab62ae | 2012-01-12 11:33:16 +0000 | [diff] [blame] | 943 | total_size = read_hostsfile(HOSTSFILE, 0, total_size, (struct crec **)daemon->packet, revhashsz); |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 944 | |
| 945 | daemon->addn_hosts = expand_filelist(daemon->addn_hosts); |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 946 | for (ah = daemon->addn_hosts; ah; ah = ah->next) |
| 947 | if (!(ah->flags & AH_INACTIVE)) |
Simon Kelley | 1ab62ae | 2012-01-12 11:33:16 +0000 | [diff] [blame] | 948 | total_size = read_hostsfile(ah->fname, ah->index, total_size, (struct crec **)daemon->packet, revhashsz); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 951 | char *get_domain(struct in_addr addr) |
| 952 | { |
| 953 | struct cond_domain *c; |
| 954 | |
| 955 | for (c = daemon->cond_domain; c; c = c->next) |
Simon Kelley | d74942a | 2012-02-07 20:51:56 +0000 | [diff] [blame] | 956 | if (!c->is6 && |
| 957 | ntohl(addr.s_addr) >= ntohl(c->start.s_addr) && |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 958 | ntohl(addr.s_addr) <= ntohl(c->end.s_addr)) |
| 959 | return c->domain; |
| 960 | |
| 961 | return daemon->domain_suffix; |
| 962 | } |
| 963 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 964 | |
| 965 | #ifdef HAVE_IPV6 |
| 966 | char *get_domain6(struct in6_addr *addr) |
| 967 | { |
Simon Kelley | d74942a | 2012-02-07 20:51:56 +0000 | [diff] [blame] | 968 | struct cond_domain *c; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 969 | |
| 970 | u64 addrpart = addr6part(addr); |
| 971 | |
Simon Kelley | d74942a | 2012-02-07 20:51:56 +0000 | [diff] [blame] | 972 | for (c = daemon->cond_domain; c; c = c->next) |
| 973 | if (c->is6 && |
| 974 | is_same_net6(addr, &c->start6, 64) && |
| 975 | addrpart >= addr6part(&c->start6) && |
| 976 | addrpart <= addr6part(&c->end6)) |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 977 | return c->domain; |
| 978 | |
| 979 | return daemon->domain_suffix; |
| 980 | } |
| 981 | #endif |
| 982 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 983 | #ifdef HAVE_DHCP |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 984 | struct in_addr a_record_from_hosts(char *name, time_t now) |
| 985 | { |
| 986 | struct crec *crecp = NULL; |
| 987 | struct in_addr ret; |
| 988 | |
| 989 | while ((crecp = cache_find_by_name(crecp, name, now, F_IPV4))) |
| 990 | if (crecp->flags & F_HOSTS) |
| 991 | return *(struct in_addr *)&crecp->addr; |
| 992 | |
| 993 | my_syslog(MS_DHCP | LOG_WARNING, _("No IPv4 address found for %s"), name); |
| 994 | |
| 995 | ret.s_addr = 0; |
| 996 | return ret; |
| 997 | } |
| 998 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 999 | void cache_unhash_dhcp(void) |
| 1000 | { |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1001 | struct crec *cache, **up; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1002 | int i; |
| 1003 | |
| 1004 | for (i=0; i<hash_size; i++) |
| 1005 | for (cache = hash_table[i], up = &hash_table[i]; cache; cache = cache->hash_next) |
| 1006 | if (cache->flags & F_DHCP) |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1007 | { |
| 1008 | *up = cache->hash_next; |
| 1009 | cache->next = dhcp_spare; |
| 1010 | dhcp_spare = cache; |
| 1011 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1012 | else |
| 1013 | up = &cache->hash_next; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 1016 | static void add_dhcp_cname(struct crec *target, time_t ttd) |
| 1017 | { |
| 1018 | struct crec *aliasc; |
| 1019 | struct cname *a; |
| 1020 | |
| 1021 | for (a = daemon->cnames; a; a = a->next) |
| 1022 | if (hostname_isequal(cache_get_name(target), a->target)) |
| 1023 | { |
| 1024 | if ((aliasc = dhcp_spare)) |
| 1025 | dhcp_spare = dhcp_spare->next; |
| 1026 | else /* need new one */ |
| 1027 | aliasc = whine_malloc(sizeof(struct crec)); |
| 1028 | |
| 1029 | if (aliasc) |
| 1030 | { |
| 1031 | aliasc->flags = F_FORWARD | F_NAMEP | F_DHCP | F_CNAME; |
| 1032 | if (ttd == 0) |
| 1033 | aliasc->flags |= F_IMMORTAL; |
| 1034 | else |
| 1035 | aliasc->ttd = ttd; |
| 1036 | aliasc->name.namep = a->alias; |
| 1037 | aliasc->addr.cname.cache = target; |
| 1038 | aliasc->addr.cname.uid = target->uid; |
| 1039 | cache_hash(aliasc); |
| 1040 | add_dhcp_cname(aliasc, ttd); |
| 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1045 | void cache_add_dhcp_entry(char *host_name, int prot, |
| 1046 | struct all_addr *host_address, time_t ttd) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1047 | { |
Simon Kelley | 12d71ed | 2012-08-30 15:16:41 +0100 | [diff] [blame] | 1048 | struct crec *crec = NULL, *fail_crec = NULL; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1049 | unsigned short flags = F_IPV4; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1050 | int in_hosts = 0; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1051 | size_t addrlen = sizeof(struct in_addr); |
| 1052 | |
| 1053 | #ifdef HAVE_IPV6 |
| 1054 | if (prot == AF_INET6) |
| 1055 | { |
| 1056 | flags = F_IPV6; |
| 1057 | addrlen = sizeof(struct in6_addr); |
| 1058 | } |
| 1059 | #endif |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1060 | |
Simon Kelley | 12d71ed | 2012-08-30 15:16:41 +0100 | [diff] [blame] | 1061 | inet_ntop(prot, host_address, daemon->addrbuff, ADDRSTRLEN); |
| 1062 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1063 | while ((crec = cache_find_by_name(crec, host_name, 0, flags | F_CNAME))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1064 | { |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1065 | /* check all addresses associated with name */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1066 | if (crec->flags & F_HOSTS) |
Simon Kelley | 1ab84e2 | 2004-01-29 16:48:35 +0000 | [diff] [blame] | 1067 | { |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1068 | if (crec->flags & F_CNAME) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1069 | my_syslog(MS_DHCP | LOG_WARNING, |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 1070 | _("%s is a CNAME, not giving it to the DHCP lease of %s"), |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1071 | host_name, daemon->addrbuff); |
Simon Kelley | 12d71ed | 2012-08-30 15:16:41 +0100 | [diff] [blame] | 1072 | else if (memcmp(&crec->addr.addr, host_address, addrlen) == 0) |
| 1073 | in_hosts = 1; |
| 1074 | else |
| 1075 | fail_crec = crec; |
Simon Kelley | 1ab84e2 | 2004-01-29 16:48:35 +0000 | [diff] [blame] | 1076 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1077 | else if (!(crec->flags & F_DHCP)) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1078 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1079 | cache_scan_free(host_name, NULL, 0, crec->flags & (flags | F_CNAME | F_FORWARD)); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1080 | /* scan_free deletes all addresses associated with name */ |
| 1081 | break; |
| 1082 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1083 | } |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1084 | |
Simon Kelley | 12d71ed | 2012-08-30 15:16:41 +0100 | [diff] [blame] | 1085 | /* if in hosts, don't need DHCP record */ |
| 1086 | if (in_hosts) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1087 | return; |
Simon Kelley | 12d71ed | 2012-08-30 15:16:41 +0100 | [diff] [blame] | 1088 | |
| 1089 | /* Name in hosts, address doesn't match */ |
| 1090 | if (fail_crec) |
| 1091 | { |
| 1092 | inet_ntop(prot, &fail_crec->addr.addr, daemon->namebuff, MAXDNAME); |
| 1093 | my_syslog(MS_DHCP | LOG_WARNING, |
| 1094 | _("not giving name %s to the DHCP lease of %s because " |
| 1095 | "the name exists in %s with address %s"), |
| 1096 | host_name, daemon->addrbuff, |
| 1097 | record_source(fail_crec->uid), daemon->namebuff); |
| 1098 | return; |
| 1099 | } |
| 1100 | |
| 1101 | if ((crec = cache_find_by_addr(NULL, (struct all_addr *)host_address, 0, flags))) |
| 1102 | { |
| 1103 | if (crec->flags & F_NEG) |
| 1104 | { |
| 1105 | flags |= F_REVERSE; |
| 1106 | cache_scan_free(NULL, (struct all_addr *)host_address, 0, flags); |
| 1107 | } |
| 1108 | } |
| 1109 | else |
| 1110 | flags |= F_REVERSE; |
| 1111 | |
| 1112 | if ((crec = dhcp_spare)) |
Simon Kelley | 6b01084 | 2007-02-12 20:32:07 +0000 | [diff] [blame] | 1113 | dhcp_spare = dhcp_spare->next; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1114 | else /* need new one */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1115 | crec = whine_malloc(sizeof(struct crec)); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1116 | |
| 1117 | if (crec) /* malloc may fail */ |
| 1118 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1119 | crec->flags = flags | F_NAMEP | F_DHCP | F_FORWARD; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1120 | if (ttd == 0) |
| 1121 | crec->flags |= F_IMMORTAL; |
| 1122 | else |
| 1123 | crec->ttd = ttd; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1124 | crec->addr.addr = *host_address; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1125 | crec->name.namep = host_name; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1126 | crec->uid = uid++; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1127 | cache_hash(crec); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1128 | |
Simon Kelley | 611ebc5 | 2012-07-16 16:23:46 +0100 | [diff] [blame] | 1129 | add_dhcp_cname(crec, ttd); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1130 | } |
| 1131 | } |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1132 | #endif |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1133 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1134 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1135 | void dump_cache(time_t now) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1136 | { |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1137 | struct server *serv, *serv1; |
| 1138 | |
| 1139 | my_syslog(LOG_INFO, _("time %lu"), (unsigned long)now); |
| 1140 | my_syslog(LOG_INFO, _("cache size %d, %d/%d cache insertions re-used unexpired cache entries."), |
| 1141 | daemon->cachesize, cache_live_freed, cache_inserted); |
| 1142 | my_syslog(LOG_INFO, _("queries forwarded %u, queries answered locally %u"), |
| 1143 | daemon->queries_forwarded, daemon->local_answer); |
| 1144 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1145 | /* sum counts from different records for same server */ |
| 1146 | for (serv = daemon->servers; serv; serv = serv->next) |
| 1147 | serv->flags &= ~SERV_COUNTED; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1148 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1149 | for (serv = daemon->servers; serv; serv = serv->next) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1150 | if (!(serv->flags & |
| 1151 | (SERV_NO_ADDR | SERV_LITERAL_ADDRESS | SERV_COUNTED | SERV_USE_RESOLV | SERV_NO_REBIND))) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1152 | { |
| 1153 | int port; |
| 1154 | unsigned int queries = 0, failed_queries = 0; |
| 1155 | for (serv1 = serv; serv1; serv1 = serv1->next) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1156 | if (!(serv1->flags & |
| 1157 | (SERV_NO_ADDR | SERV_LITERAL_ADDRESS | SERV_COUNTED | SERV_USE_RESOLV | SERV_NO_REBIND)) && |
| 1158 | sockaddr_isequal(&serv->addr, &serv1->addr)) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1159 | { |
| 1160 | serv1->flags |= SERV_COUNTED; |
| 1161 | queries += serv1->queries; |
| 1162 | failed_queries += serv1->failed_queries; |
| 1163 | } |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 1164 | port = prettyprint_addr(&serv->addr, daemon->addrbuff); |
| 1165 | my_syslog(LOG_INFO, _("server %s#%d: queries sent %u, retried or failed %u"), daemon->addrbuff, port, queries, failed_queries); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1168 | if (option_bool(OPT_DEBUG) || option_bool(OPT_LOG)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1169 | { |
| 1170 | struct crec *cache ; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1171 | int i; |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1172 | my_syslog(LOG_INFO, "Host Address Flags Expires"); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1173 | |
| 1174 | for (i=0; i<hash_size; i++) |
| 1175 | for (cache = hash_table[i]; cache; cache = cache->hash_next) |
| 1176 | { |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1177 | char *a, *p = daemon->namebuff; |
| 1178 | p += sprintf(p, "%-40.40s ", cache_get_name(cache)); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1179 | if ((cache->flags & F_NEG) && (cache->flags & F_FORWARD)) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1180 | a = ""; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1181 | else if (cache->flags & F_CNAME) |
| 1182 | { |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1183 | a = ""; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1184 | if (!is_outdated_cname_pointer(cache)) |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1185 | a = cache_get_name(cache->addr.cname.cache); |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1186 | } |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 1187 | #ifdef HAVE_DNSSEC |
Simon Kelley | cdbee9a | 2012-04-04 21:55:59 +0100 | [diff] [blame] | 1188 | else if (cache->flags & F_DNSKEY) |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 1189 | { |
| 1190 | a = daemon->addrbuff; |
Simon Kelley | cdbee9a | 2012-04-04 21:55:59 +0100 | [diff] [blame] | 1191 | sprintf(a, "%3u %u", cache->addr.key.algo, cache->uid); |
| 1192 | } |
| 1193 | else if (cache->flags & F_DS) |
| 1194 | { |
| 1195 | a = daemon->addrbuff; |
| 1196 | sprintf(a, "%5u %3u %3u %u", cache->addr.key.flags_or_keyid, |
| 1197 | cache->addr.key.algo, cache->addr.key.digest, cache->uid); |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 1198 | } |
| 1199 | #endif |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1200 | else |
| 1201 | { |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 1202 | a = daemon->addrbuff; |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1203 | if (cache->flags & F_IPV4) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 1204 | inet_ntop(AF_INET, &cache->addr.addr, a, ADDRSTRLEN); |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 1205 | #ifdef HAVE_IPV6 |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1206 | else if (cache->flags & F_IPV6) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 1207 | inet_ntop(AF_INET6, &cache->addr.addr, a, ADDRSTRLEN); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1208 | #endif |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | p += sprintf(p, "%-30.30s %s%s%s%s%s%s%s%s%s%s%s%s%s ", a, |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1212 | cache->flags & F_IPV4 ? "4" : "", |
| 1213 | cache->flags & F_IPV6 ? "6" : "", |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 1214 | cache->flags & F_DNSKEY ? "K" : "", |
| 1215 | cache->flags & F_DS ? "S" : "", |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1216 | cache->flags & F_CNAME ? "C" : "", |
| 1217 | cache->flags & F_FORWARD ? "F" : " ", |
| 1218 | cache->flags & F_REVERSE ? "R" : " ", |
| 1219 | cache->flags & F_IMMORTAL ? "I" : " ", |
| 1220 | cache->flags & F_DHCP ? "D" : " ", |
| 1221 | cache->flags & F_NEG ? "N" : " ", |
| 1222 | cache->flags & F_NXDOMAIN ? "X" : " ", |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 1223 | cache->flags & F_HOSTS ? "H" : " ", |
| 1224 | cache->flags & F_DNSSECOK ? "V" : " "); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1225 | #ifdef HAVE_BROKEN_RTC |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1226 | p += sprintf(p, "%lu", cache->flags & F_IMMORTAL ? 0: (unsigned long)(cache->ttd - now)); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1227 | #else |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1228 | p += sprintf(p, "%s", cache->flags & F_IMMORTAL ? "\n" : ctime(&(cache->ttd))); |
| 1229 | /* ctime includes trailing \n - eat it */ |
| 1230 | *(p-1) = 0; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 1231 | #endif |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1232 | my_syslog(LOG_INFO, daemon->namebuff); |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 1233 | } |
| 1234 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1237 | char *record_source(int index) |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1238 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1239 | struct hostsfile *ah; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1240 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1241 | if (index == 0) |
| 1242 | return HOSTSFILE; |
| 1243 | |
| 1244 | for (ah = daemon->addn_hosts; ah; ah = ah->next) |
| 1245 | if (ah->index == index) |
| 1246 | return ah->fname; |
| 1247 | |
| 1248 | return "<unknown>"; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1249 | } |
Simon Kelley | c1bb850 | 2004-08-11 18:40:17 +0100 | [diff] [blame] | 1250 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1251 | void querystr(char *str, unsigned short type) |
| 1252 | { |
| 1253 | unsigned int i; |
| 1254 | |
| 1255 | sprintf(str, "query[type=%d]", type); |
| 1256 | for (i = 0; i < (sizeof(typestr)/sizeof(typestr[0])); i++) |
| 1257 | if (typestr[i].type == type) |
| 1258 | sprintf(str,"query[%s]", typestr[i].name); |
| 1259 | } |
| 1260 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1261 | void log_query(unsigned int flags, char *name, struct all_addr *addr, char *arg) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1262 | { |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 1263 | char *source, *dest = daemon->addrbuff; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1264 | char *verb = "is"; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1265 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1266 | if (!option_bool(OPT_LOG)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1267 | return; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1268 | |
| 1269 | if (addr) |
| 1270 | { |
| 1271 | #ifdef HAVE_IPV6 |
| 1272 | inet_ntop(flags & F_IPV4 ? AF_INET : AF_INET6, |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 1273 | addr, daemon->addrbuff, ADDRSTRLEN); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1274 | #else |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 1275 | strncpy(daemon->addrbuff, inet_ntoa(addr->addr.addr4), ADDRSTRLEN); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1276 | #endif |
| 1277 | } |
| 1278 | |
| 1279 | if (flags & F_REVERSE) |
| 1280 | { |
| 1281 | dest = name; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 1282 | name = daemon->addrbuff; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1283 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1284 | |
| 1285 | if (flags & F_NEG) |
| 1286 | { |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1287 | if (flags & F_NXDOMAIN) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1288 | { |
| 1289 | if (flags & F_IPV4) |
| 1290 | dest = "NXDOMAIN-IPv4"; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1291 | else if (flags & F_IPV6) |
| 1292 | dest = "NXDOMAIN-IPv6"; |
| 1293 | else |
| 1294 | dest = "NXDOMAIN"; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1295 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1296 | else |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1297 | { |
| 1298 | if (flags & F_IPV4) |
| 1299 | dest = "NODATA-IPv4"; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1300 | else if (flags & F_IPV6) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1301 | dest = "NODATA-IPv6"; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1302 | else |
| 1303 | dest = "NODATA"; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1304 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1305 | } |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1306 | else if (flags & F_CNAME) |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1307 | dest = "<CNAME>"; |
| 1308 | else if (flags & F_RRNAME) |
| 1309 | dest = arg; |
Simon Kelley | fd9fa48 | 2004-10-21 20:24:00 +0100 | [diff] [blame] | 1310 | |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 1311 | if (flags & F_CONFIG) |
| 1312 | source = "config"; |
| 1313 | else if (flags & F_DHCP) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1314 | source = "DHCP"; |
| 1315 | else if (flags & F_HOSTS) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1316 | source = arg; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1317 | else if (flags & F_UPSTREAM) |
| 1318 | source = "reply"; |
| 1319 | else if (flags & F_SERVER) |
| 1320 | { |
| 1321 | source = "forwarded"; |
| 1322 | verb = "to"; |
| 1323 | } |
| 1324 | else if (flags & F_QUERY) |
| 1325 | { |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 1326 | source = arg; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1327 | verb = "from"; |
| 1328 | } |
| 1329 | else |
| 1330 | source = "cached"; |
| 1331 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 1332 | if (strlen(name) == 0) |
| 1333 | name = "."; |
| 1334 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1335 | my_syslog(LOG_INFO, "%s %s %s %s", source, name, verb, dest); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
Simon Kelley | 7b4ad2e | 2012-04-04 14:05:35 +0100 | [diff] [blame] | 1338 | #ifdef HAVE_DNSSEC |
| 1339 | struct keydata *keydata_alloc(char *data, size_t len) |
| 1340 | { |
| 1341 | struct keydata *block, *ret = NULL; |
| 1342 | struct keydata **prev = &ret; |
| 1343 | while (len > 0) |
| 1344 | { |
| 1345 | if (keyblock_free) |
| 1346 | { |
| 1347 | block = keyblock_free; |
| 1348 | keyblock_free = block->next; |
| 1349 | } |
| 1350 | else |
| 1351 | block = whine_malloc(sizeof(struct keydata)); |
| 1352 | |
| 1353 | if (!block) |
| 1354 | { |
| 1355 | /* failed to alloc, free partial chain */ |
| 1356 | keydata_free(ret); |
| 1357 | return NULL; |
| 1358 | } |
| 1359 | |
| 1360 | memcpy(block->key, data, len > KEYBLOCK_LEN ? KEYBLOCK_LEN : len); |
| 1361 | data += KEYBLOCK_LEN; |
| 1362 | len -= KEYBLOCK_LEN; |
| 1363 | *prev = block; |
| 1364 | prev = &block->next; |
| 1365 | block->next = NULL; |
| 1366 | } |
| 1367 | |
| 1368 | return ret; |
| 1369 | } |
| 1370 | |
| 1371 | void keydata_free(struct keydata *blocks) |
| 1372 | { |
| 1373 | struct keydata *tmp; |
| 1374 | |
| 1375 | if (blocks) |
| 1376 | { |
| 1377 | for (tmp = blocks; tmp->next; tmp = tmp->next); |
| 1378 | tmp->next = keyblock_free; |
| 1379 | keyblock_free = blocks; |
| 1380 | } |
| 1381 | } |
| 1382 | #endif |
| 1383 | |
| 1384 | |