]> git.ipfire.org Git - thirdparty/squid.git/blob - src/fqdncache.cc
Merged from trunk
[thirdparty/squid.git] / src / fqdncache.cc
1 /*
2 * $Id$
3 *
4 * DEBUG: section 35 FQDN Cache
5 * AUTHOR: Harvest Derived
6 *
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
9 *
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
32 *
33 */
34
35 #include "squid.h"
36 #include "cbdata.h"
37 #include "DnsLookupDetails.h"
38 #include "event.h"
39 #include "Mem.h"
40 #include "mgr/Registration.h"
41 #include "protos.h"
42 #include "SquidDns.h"
43 #include "SquidTime.h"
44 #include "StatCounters.h"
45 #include "Store.h"
46 #include "wordlist.h"
47
48 /**
49 \defgroup FQDNCacheAPI FQDN Cache API
50 \ingroup Components
51 \section Introduction Introduction
52 \par
53 * The FQDN cache is a built-in component of squid providing
54 * Hostname to IP-Number translation functionality and managing
55 * the involved data-structures. Efficiency concerns require
56 * mechanisms that allow non-blocking access to these mappings.
57 * The FQDN cache usually doesn't block on a request except for
58 * special cases where this is desired (see below).
59 *
60 \todo FQDN Cache should have its own API *.h file.
61 */
62
63 /**
64 \defgroup FQDNCacheInternal FQDN Cache Internals
65 \ingroup FQDNCacheAPI
66 \par
67 * Internally, the execution flow is as follows:
68 * On a miss, fqdncache_nbgethostbyaddr() checks whether a request
69 * for this name is already pending, and if positive, it creates a
70 * new entry using fqdncacheAddEntry(). Then it calls
71 * fqdncacheAddPending() to add a request to the queue together
72 * with data and handler. Else, ifqdncache_dnsDispatch() is called
73 * to directly create a DNS query or to fqdncacheEnqueue() if all
74 * no DNS port is free.
75 *
76 \par
77 * fqdncacheCallback() is called regularly to walk down the pending
78 * list and call handlers.
79 *
80 \par
81 * LRU clean-up is performed through fqdncache_purgelru() according
82 * to the fqdncache_high threshold.
83 */
84
85 /// \ingroup FQDNCacheInternal
86 #define FQDN_LOW_WATER 90
87
88 /// \ingroup FQDNCacheInternal
89 #define FQDN_HIGH_WATER 95
90
91 /**
92 \ingroup FQDNCacheAPI
93 * The data structure used for storing name-address mappings
94 * is a small hashtable (static hash_table *fqdn_table),
95 * where structures of type fqdncache_entry whose most
96 * interesting members are:
97 */
98 class fqdncache_entry
99 {
100 public:
101 hash_link hash; /* must be first */
102 time_t lastref;
103 time_t expires;
104 unsigned char name_count;
105 char *names[FQDN_MAX_NAMES + 1];
106 FQDNH *handler;
107 void *handlerData;
108 char *error_message;
109
110 struct timeval request_time;
111 dlink_node lru;
112 unsigned short locks;
113
114 struct {
115 unsigned int negcached:1;
116 unsigned int fromhosts:1;
117 } flags;
118
119 int age() const; ///< time passed since request_time or -1 if unknown
120 };
121
122 /// \ingroup FQDNCacheInternal
123 static struct _fqdn_cache_stats {
124 int requests;
125 int replies;
126 int hits;
127 int misses;
128 int negative_hits;
129 } FqdncacheStats;
130
131 /// \ingroup FQDNCacheInternal
132 static dlink_list lru_list;
133
134 #if USE_DNSHELPER
135 static HLPCB fqdncacheHandleReply;
136 static int fqdncacheParse(fqdncache_entry *, const char *buf);
137 #else
138 static IDNSCB fqdncacheHandleReply;
139 static int fqdncacheParse(fqdncache_entry *, const rfc1035_rr *, int, const char *error_message);
140 #endif
141 static void fqdncacheRelease(fqdncache_entry *);
142 static fqdncache_entry *fqdncacheCreateEntry(const char *name);
143 static void fqdncacheCallback(fqdncache_entry *, int wait);
144 static fqdncache_entry *fqdncache_get(const char *);
145 static int fqdncacheExpiredEntry(const fqdncache_entry *);
146 static void fqdncacheLockEntry(fqdncache_entry * f);
147 static void fqdncacheUnlockEntry(fqdncache_entry * f);
148 static FREE fqdncacheFreeEntry;
149 static void fqdncacheAddEntry(fqdncache_entry * f);
150
151 /// \ingroup FQDNCacheInternal
152 static hash_table *fqdn_table = NULL;
153
154 /// \ingroup FQDNCacheInternal
155 static long fqdncache_low = 180;
156
157 /// \ingroup FQDNCacheInternal
158 static long fqdncache_high = 200;
159
160 /// \ingroup FQDNCacheInternal
161 inline int fqdncacheCount() { return fqdn_table ? fqdn_table->count : 0; }
162
163 int
164 fqdncache_entry::age() const
165 {
166 return request_time.tv_sec ? tvSubMsec(request_time, current_time) : -1;
167 }
168
169
170 /**
171 \ingroup FQDNCacheInternal
172 * Removes the given fqdncache entry
173 */
174 static void
175 fqdncacheRelease(fqdncache_entry * f)
176 {
177 int k;
178 hash_remove_link(fqdn_table, (hash_link *) f);
179
180 for (k = 0; k < (int) f->name_count; ++k)
181 safe_free(f->names[k]);
182
183 debugs(35, 5, "fqdncacheRelease: Released FQDN record for '" << hashKeyStr(&f->hash) << "'.");
184
185 dlinkDelete(&f->lru, &lru_list);
186
187 safe_free(f->hash.key);
188
189 safe_free(f->error_message);
190
191 memFree(f, MEM_FQDNCACHE_ENTRY);
192 }
193
194 /**
195 \ingroup FQDNCacheInternal
196 \param name FQDN hash string.
197 \retval Match for given name
198 */
199 static fqdncache_entry *
200 fqdncache_get(const char *name)
201 {
202 hash_link *e;
203 static fqdncache_entry *f;
204 f = NULL;
205
206 if (fqdn_table) {
207 if ((e = (hash_link *)hash_lookup(fqdn_table, name)) != NULL)
208 f = (fqdncache_entry *) e;
209 }
210
211 return f;
212 }
213
214 /// \ingroup FQDNCacheInternal
215 static int
216 fqdncacheExpiredEntry(const fqdncache_entry * f)
217 {
218 /* all static entries are locked, so this takes care of them too */
219
220 if (f->locks != 0)
221 return 0;
222
223 if (f->expires > squid_curtime)
224 return 0;
225
226 return 1;
227 }
228
229 /// \ingroup FQDNCacheAPI
230 void
231 fqdncache_purgelru(void *notused)
232 {
233 dlink_node *m;
234 dlink_node *prev = NULL;
235 fqdncache_entry *f;
236 int removed = 0;
237 eventAdd("fqdncache_purgelru", fqdncache_purgelru, NULL, 10.0, 1);
238
239 for (m = lru_list.tail; m; m = prev) {
240 if (fqdncacheCount() < fqdncache_low)
241 break;
242
243 prev = m->prev;
244
245 f = (fqdncache_entry *)m->data;
246
247 if (f->locks != 0)
248 continue;
249
250 fqdncacheRelease(f);
251
252 ++removed;
253 }
254
255 debugs(35, 9, "fqdncache_purgelru: removed " << removed << " entries");
256 }
257
258 /// \ingroup FQDNCacheAPI
259 static void
260 purge_entries_fromhosts(void)
261 {
262 dlink_node *m = lru_list.head;
263 fqdncache_entry *i = NULL;
264 fqdncache_entry *t;
265
266 while (m) {
267 if (i != NULL) { /* need to delay deletion */
268 fqdncacheRelease(i); /* we just override locks */
269 i = NULL;
270 }
271
272 t = (fqdncache_entry *)m->data;
273
274 if (t->flags.fromhosts)
275 i = t;
276
277 m = m->next;
278 }
279
280 if (i != NULL)
281 fqdncacheRelease(i);
282 }
283
284 /**
285 \ingroup FQDNCacheInternal
286 *
287 * Create blank fqdncache_entry
288 */
289 static fqdncache_entry *
290 fqdncacheCreateEntry(const char *name)
291 {
292 static fqdncache_entry *f;
293 f = (fqdncache_entry *)memAllocate(MEM_FQDNCACHE_ENTRY);
294 f->hash.key = xstrdup(name);
295 f->expires = squid_curtime + Config.negativeDnsTtl;
296 return f;
297 }
298
299 /// \ingroup FQDNCacheInternal
300 static void
301 fqdncacheAddEntry(fqdncache_entry * f)
302 {
303 hash_link *e = (hash_link *)hash_lookup(fqdn_table, f->hash.key);
304
305 if (NULL != e) {
306 /* avoid colission */
307 fqdncache_entry *q = (fqdncache_entry *) e;
308 fqdncacheRelease(q);
309 }
310
311 hash_join(fqdn_table, &f->hash);
312 dlinkAdd(f, &f->lru, &lru_list);
313 f->lastref = squid_curtime;
314 }
315
316 /**
317 \ingroup FQDNCacheInternal
318 *
319 * Walks down the pending list, calling handlers
320 */
321 static void
322 fqdncacheCallback(fqdncache_entry * f, int wait)
323 {
324 FQDNH *callback;
325 void *cbdata;
326 f->lastref = squid_curtime;
327
328 if (!f->handler)
329 return;
330
331 fqdncacheLockEntry(f);
332
333 callback = f->handler;
334
335 f->handler = NULL;
336
337 if (cbdataReferenceValidDone(f->handlerData, &cbdata)) {
338 const DnsLookupDetails details(f->error_message, wait);
339 callback(f->name_count ? f->names[0] : NULL, details, cbdata);
340 }
341
342 fqdncacheUnlockEntry(f);
343 }
344
345 /// \ingroup FQDNCacheInternal
346 #if USE_DNSHELPER
347 static int
348 fqdncacheParse(fqdncache_entry *f, const char *inbuf)
349 {
350 LOCAL_ARRAY(char, buf, DNS_INBUF_SZ);
351 char *token;
352 int ttl;
353 const char *name = (const char *)f->hash.key;
354 f->expires = squid_curtime + Config.negativeDnsTtl;
355 f->flags.negcached = 1;
356
357 if (inbuf == NULL) {
358 debugs(35, DBG_IMPORTANT, "fqdncacheParse: Got <NULL> reply in response to '" << name << "'");
359 f->error_message = xstrdup("Internal Error");
360 return -1;
361 }
362
363 xstrncpy(buf, inbuf, DNS_INBUF_SZ);
364 debugs(35, 5, "fqdncacheParse: parsing: {" << buf << "}");
365 token = strtok(buf, w_space);
366
367 if (NULL == token) {
368 debugs(35, DBG_IMPORTANT, "fqdncacheParse: Got <NULL>, expecting '$name' in response to '" << name << "'");
369 f->error_message = xstrdup("Internal Error");
370 return -1;
371 }
372
373 if (0 == strcmp(token, "$fail")) {
374 token = strtok(NULL, "\n");
375 assert(NULL != token);
376 f->error_message = xstrdup(token);
377 return 0;
378 }
379
380 if (0 != strcmp(token, "$name")) {
381 debugs(35, DBG_IMPORTANT, "fqdncacheParse: Got '" << inbuf << "', expecting '$name' in response to '" << name << "'");
382 f->error_message = xstrdup("Internal Error");
383 return -1;
384 }
385
386 token = strtok(NULL, w_space);
387
388 if (NULL == token) {
389 debugs(35, DBG_IMPORTANT, "fqdncacheParse: Got '" << inbuf << "', expecting TTL in response to '" << name << "'");
390 f->error_message = xstrdup("Internal Error");
391 return -1;
392 }
393
394 ttl = atoi(token);
395
396 token = strtok(NULL, w_space);
397
398 if (NULL == token) {
399 debugs(35, DBG_IMPORTANT, "fqdncacheParse: Got '" << inbuf << "', expecting hostname in response to '" << name << "'");
400 f->error_message = xstrdup("Internal Error");
401 return -1;
402 }
403
404 f->names[0] = xstrdup(token);
405 f->name_count = 1;
406
407 if (ttl == 0 || ttl > Config.positiveDnsTtl)
408 ttl = Config.positiveDnsTtl;
409
410 if (ttl < Config.negativeDnsTtl)
411 ttl = Config.negativeDnsTtl;
412
413 f->expires = squid_curtime + ttl;
414
415 f->flags.negcached = 0;
416
417 return f->name_count;
418 }
419
420 #else
421 static int
422 fqdncacheParse(fqdncache_entry *f, const rfc1035_rr * answers, int nr, const char *error_message)
423 {
424 int k;
425 int ttl = 0;
426 const char *name = (const char *)f->hash.key;
427 f->expires = squid_curtime + Config.negativeDnsTtl;
428 f->flags.negcached = 1;
429
430 if (nr < 0) {
431 debugs(35, 3, "fqdncacheParse: Lookup of '" << name << "' failed (" << error_message << ")");
432 f->error_message = xstrdup(error_message);
433 return -1;
434 }
435
436 if (nr == 0) {
437 debugs(35, 3, "fqdncacheParse: No DNS records for '" << name << "'");
438 f->error_message = xstrdup("No DNS records");
439 return 0;
440 }
441
442 debugs(35, 3, "fqdncacheParse: " << nr << " answers for '" << name << "'");
443 assert(answers);
444
445 for (k = 0; k < nr; ++k) {
446 if (answers[k]._class != RFC1035_CLASS_IN)
447 continue;
448
449 if (answers[k].type == RFC1035_TYPE_PTR) {
450 if (!answers[k].rdata[0]) {
451 debugs(35, 2, "fqdncacheParse: blank PTR record for '" << name << "'");
452 continue;
453 }
454
455 if (strchr(answers[k].rdata, ' ')) {
456 debugs(35, 2, "fqdncacheParse: invalid PTR record '" << answers[k].rdata << "' for '" << name << "'");
457 continue;
458 }
459
460 f->names[f->name_count] = xstrdup(answers[k].rdata);
461 ++ f->name_count;
462 } else if (answers[k].type != RFC1035_TYPE_CNAME)
463 continue;
464
465 if (ttl == 0 || (int) answers[k].ttl < ttl)
466 ttl = answers[k].ttl;
467
468 if (f->name_count >= FQDN_MAX_NAMES)
469 break;
470 }
471
472 if (f->name_count == 0) {
473 debugs(35, DBG_IMPORTANT, "fqdncacheParse: No PTR record for '" << name << "'");
474 return 0;
475 }
476
477 if (ttl > Config.positiveDnsTtl)
478 ttl = Config.positiveDnsTtl;
479
480 if (ttl < Config.negativeDnsTtl)
481 ttl = Config.negativeDnsTtl;
482
483 f->expires = squid_curtime + ttl;
484
485 f->flags.negcached = 0;
486
487 return f->name_count;
488 }
489
490 #endif
491
492
493 /**
494 \ingroup FQDNCacheAPI
495 *
496 * Callback for handling DNS results.
497 */
498 static void
499 #if USE_DNSHELPER
500 fqdncacheHandleReply(void *data, char *reply)
501 #else
502 fqdncacheHandleReply(void *data, const rfc1035_rr * answers, int na, const char *error_message)
503 #endif
504 {
505 fqdncache_entry *f;
506 static_cast<generic_cbdata *>(data)->unwrap(&f);
507 ++FqdncacheStats.replies;
508 const int age = f->age();
509 statCounter.dns.svcTime.count(age);
510 #if USE_DNSHELPER
511
512 fqdncacheParse(f, reply);
513 #else
514
515 fqdncacheParse(f, answers, na, error_message);
516 #endif
517
518 fqdncacheAddEntry(f);
519
520 fqdncacheCallback(f, age);
521 }
522
523 /**
524 \ingroup FQDNCacheAPI
525 *
526 \param addr IP address of domain to resolve.
527 \param handler A pointer to the function to be called when
528 * the reply from the FQDN cache
529 * (or the DNS if the FQDN cache misses)
530 \param handlerData Information that is passed to the handler
531 * and does not affect the FQDN cache.
532 */
533 void
534 fqdncache_nbgethostbyaddr(const Ip::Address &addr, FQDNH * handler, void *handlerData)
535 {
536 fqdncache_entry *f = NULL;
537 char name[MAX_IPSTRLEN];
538 generic_cbdata *c;
539 addr.NtoA(name,MAX_IPSTRLEN);
540 debugs(35, 4, "fqdncache_nbgethostbyaddr: Name '" << name << "'.");
541 ++FqdncacheStats.requests;
542
543 if (name[0] == '\0') {
544 debugs(35, 4, "fqdncache_nbgethostbyaddr: Invalid name!");
545 const DnsLookupDetails details("Invalid hostname", -1); // error, no lookup
546 if (handler)
547 handler(NULL, details, handlerData);
548 return;
549 }
550
551 f = fqdncache_get(name);
552
553 if (NULL == f) {
554 /* miss */
555 (void) 0;
556 } else if (fqdncacheExpiredEntry(f)) {
557 /* hit, but expired -- bummer */
558 fqdncacheRelease(f);
559 f = NULL;
560 } else {
561 /* hit */
562 debugs(35, 4, "fqdncache_nbgethostbyaddr: HIT for '" << name << "'");
563
564 if (f->flags.negcached)
565 ++ FqdncacheStats.negative_hits;
566 else
567 ++ FqdncacheStats.hits;
568
569 f->handler = handler;
570
571 f->handlerData = cbdataReference(handlerData);
572
573 fqdncacheCallback(f, -1); // no lookup
574
575 return;
576 }
577
578 debugs(35, 5, "fqdncache_nbgethostbyaddr: MISS for '" << name << "'");
579 ++ FqdncacheStats.misses;
580 f = fqdncacheCreateEntry(name);
581 f->handler = handler;
582 f->handlerData = cbdataReference(handlerData);
583 f->request_time = current_time;
584 c = new generic_cbdata(f);
585 #if USE_DNSHELPER
586 dnsSubmit(hashKeyStr(&f->hash), fqdncacheHandleReply, c);
587 #else
588 idnsPTRLookup(addr, fqdncacheHandleReply, c);
589 #endif
590 }
591
592
593 /**
594 \ingroup FQDNCacheAPI
595 *
596 * Is different in that it only checks if an entry exists in
597 * it's data-structures and does not by default contact the
598 * DNS, unless this is requested, by setting the flags
599 * to FQDN_LOOKUP_IF_MISS.
600 *
601 \param addr address of the FQDN being resolved
602 \param flags values are NULL or FQDN_LOOKUP_IF_MISS. default is NULL.
603 *
604 */
605 const char *
606 fqdncache_gethostbyaddr(const Ip::Address &addr, int flags)
607 {
608 char name[MAX_IPSTRLEN];
609 fqdncache_entry *f = NULL;
610
611 if (addr.IsAnyAddr() || addr.IsNoAddr()) {
612 return NULL;
613 }
614
615 addr.NtoA(name,MAX_IPSTRLEN);
616 ++ FqdncacheStats.requests;
617 f = fqdncache_get(name);
618
619 if (NULL == f) {
620 (void) 0;
621 } else if (fqdncacheExpiredEntry(f)) {
622 fqdncacheRelease(f);
623 f = NULL;
624 } else if (f->flags.negcached) {
625 ++ FqdncacheStats.negative_hits;
626 // ignore f->error_message: the caller just checks FQDN cache presence
627 return NULL;
628 } else {
629 ++ FqdncacheStats.hits;
630 f->lastref = squid_curtime;
631 // ignore f->error_message: the caller just checks FQDN cache presence
632 return f->names[0];
633 }
634
635 /* no entry [any more] */
636
637 ++ FqdncacheStats.misses;
638
639 if (flags & FQDN_LOOKUP_IF_MISS) {
640 fqdncache_nbgethostbyaddr(addr, NULL, NULL);
641 }
642
643 return NULL;
644 }
645
646
647 /**
648 \ingroup FQDNCacheInternal
649 *
650 * Process objects list
651 */
652 void
653 fqdnStats(StoreEntry * sentry)
654 {
655 fqdncache_entry *f = NULL;
656 int k;
657 int ttl;
658
659 if (fqdn_table == NULL)
660 return;
661
662 storeAppendPrintf(sentry, "FQDN Cache Statistics:\n");
663
664 storeAppendPrintf(sentry, "FQDNcache Entries In Use: %d\n",
665 memInUse(MEM_FQDNCACHE_ENTRY));
666
667 storeAppendPrintf(sentry, "FQDNcache Entries Cached: %d\n",
668 fqdncacheCount());
669
670 storeAppendPrintf(sentry, "FQDNcache Requests: %d\n",
671 FqdncacheStats.requests);
672
673 storeAppendPrintf(sentry, "FQDNcache Hits: %d\n",
674 FqdncacheStats.hits);
675
676 storeAppendPrintf(sentry, "FQDNcache Negative Hits: %d\n",
677 FqdncacheStats.negative_hits);
678
679 storeAppendPrintf(sentry, "FQDNcache Misses: %d\n",
680 FqdncacheStats.misses);
681
682 storeAppendPrintf(sentry, "FQDN Cache Contents:\n\n");
683
684 storeAppendPrintf(sentry, "%-45.45s %3s %3s %3s %s\n",
685 "Address", "Flg", "TTL", "Cnt", "Hostnames");
686
687 hash_first(fqdn_table);
688
689 while ((f = (fqdncache_entry *) hash_next(fqdn_table))) {
690 ttl = (f->flags.fromhosts ? -1 : (f->expires - squid_curtime));
691 storeAppendPrintf(sentry, "%-45.45s %c%c %3.3d % 3d",
692 hashKeyStr(&f->hash),
693 f->flags.negcached ? 'N' : ' ',
694 f->flags.fromhosts ? 'H' : ' ',
695 ttl,
696 (int) f->name_count);
697
698 for (k = 0; k < (int) f->name_count; ++k)
699 storeAppendPrintf(sentry, " %s", f->names[k]);
700
701 storeAppendPrintf(sentry, "\n");
702 }
703 }
704
705 /// \ingroup FQDNCacheAPI
706 #if 0
707 const char *
708 fqdnFromAddr(const Ip::Address &addr)
709 {
710 const char *n;
711 static char buf[MAX_IPSTRLEN];
712
713 if (Config.onoff.log_fqdn && (n = fqdncache_gethostbyaddr(addr, 0)))
714 return n;
715
716 /// \todo Perhapse this should use toHostname() instead of straight NtoA.
717 /// that would wrap the IPv6 properly when raw.
718 addr.NtoA(buf, MAX_IPSTRLEN);
719
720 return buf;
721 }
722 #endif
723
724 /// \ingroup FQDNCacheInternal
725 static void
726 fqdncacheLockEntry(fqdncache_entry * f)
727 {
728 if (f->locks++ == 0) {
729 dlinkDelete(&f->lru, &lru_list);
730 dlinkAdd(f, &f->lru, &lru_list);
731 }
732 }
733
734 /// \ingroup FQDNCacheInternal
735 static void
736 fqdncacheUnlockEntry(fqdncache_entry * f)
737 {
738 assert(f->locks > 0);
739 -- f->locks;
740
741 if (fqdncacheExpiredEntry(f))
742 fqdncacheRelease(f);
743 }
744
745 /// \ingroup FQDNCacheInternal
746 static void
747 fqdncacheFreeEntry(void *data)
748 {
749 fqdncache_entry *f = (fqdncache_entry *)data;
750 int k;
751
752 for (k = 0; k < (int) f->name_count; ++k)
753 safe_free(f->names[k]);
754
755 safe_free(f->hash.key);
756
757 safe_free(f->error_message);
758
759 memFree(f, MEM_FQDNCACHE_ENTRY);
760 }
761
762 /// \ingroup FQDNCacheAPI
763 void
764 fqdncacheFreeMemory(void)
765 {
766 hashFreeItems(fqdn_table, fqdncacheFreeEntry);
767 hashFreeMemory(fqdn_table);
768 fqdn_table = NULL;
769 }
770
771 /**
772 \ingroup FQDNCacheAPI
773 *
774 * Recalculate FQDN cache size upon reconfigure.
775 * Is called to clear the FQDN cache's data structures,
776 * cancel all pending requests.
777 */
778 void
779 fqdncache_restart(void)
780 {
781 fqdncache_high = (long) (((float) Config.fqdncache.size *
782 (float) FQDN_HIGH_WATER) / (float) 100);
783 fqdncache_low = (long) (((float) Config.fqdncache.size *
784 (float) FQDN_LOW_WATER) / (float) 100);
785 purge_entries_fromhosts();
786 }
787
788 /**
789 \ingroup FQDNCacheAPI
790 *
791 * Adds a "static" entry from /etc/hosts.
792 \par
793 * The worldist is to be managed by the caller,
794 * including pointed-to strings
795 *
796 \param addr FQDN name to be added.
797 \param hostnames ??
798 */
799 void
800 fqdncacheAddEntryFromHosts(char *addr, wordlist * hostnames)
801 {
802 fqdncache_entry *fce;
803 int j = 0;
804
805 if ((fce = fqdncache_get(addr))) {
806 if (1 == fce->flags.fromhosts) {
807 fqdncacheUnlockEntry(fce);
808 } else if (fce->locks > 0) {
809 debugs(35, DBG_IMPORTANT, "fqdncacheAddEntryFromHosts: can't add static entry for locked address '" << addr << "'");
810 return;
811 } else {
812 fqdncacheRelease(fce);
813 }
814 }
815
816 fce = fqdncacheCreateEntry(addr);
817
818 while (hostnames) {
819 fce->names[j] = xstrdup(hostnames->key);
820 Tolower(fce->names[j]);
821 ++j;
822 hostnames = hostnames->next;
823
824 if (j >= FQDN_MAX_NAMES)
825 break;
826 }
827
828 fce->name_count = j;
829 fce->names[j] = NULL; /* it's safe */
830 fce->flags.fromhosts = 1;
831 fqdncacheAddEntry(fce);
832 fqdncacheLockEntry(fce);
833 }
834
835 /// \ingroup FQDNCacheInternal
836 static void
837 fqdncacheRegisterWithCacheManager(void)
838 {
839 Mgr::RegisterAction("fqdncache", "FQDN Cache Stats and Contents",
840 fqdnStats, 0, 1);
841
842 }
843
844 /**
845 \ingroup FQDNCacheAPI
846 *
847 * Initialize the fqdncache.
848 * Called after IP cache initialization.
849 */
850 void
851 fqdncache_init(void)
852 {
853 int n;
854
855 fqdncacheRegisterWithCacheManager();
856
857 if (fqdn_table)
858 return;
859
860 debugs(35, 3, "Initializing FQDN Cache...");
861
862 memset(&FqdncacheStats, '\0', sizeof(FqdncacheStats));
863
864 memset(&lru_list, '\0', sizeof(lru_list));
865
866 fqdncache_high = (long) (((float) Config.fqdncache.size *
867 (float) FQDN_HIGH_WATER) / (float) 100);
868
869 fqdncache_low = (long) (((float) Config.fqdncache.size *
870 (float) FQDN_LOW_WATER) / (float) 100);
871
872 n = hashPrime(fqdncache_high / 4);
873
874 fqdn_table = hash_create((HASHCMP *) strcmp, n, hash4);
875
876 memDataInit(MEM_FQDNCACHE_ENTRY, "fqdncache_entry",
877 sizeof(fqdncache_entry), 0);
878 }
879
880 #if SQUID_SNMP
881 /**
882 * \ingroup FQDNCacheAPI
883 * The function to return the FQDN statistics via SNMP
884 */
885 variable_list *
886 snmp_netFqdnFn(variable_list * Var, snint * ErrP)
887 {
888 variable_list *Answer = NULL;
889 MemBuf tmp;
890 debugs(49, 5, "snmp_netFqdnFn: Processing request:" << snmpDebugOid(Var->name, Var->name_length, tmp));
891 *ErrP = SNMP_ERR_NOERROR;
892
893 switch (Var->name[LEN_SQ_NET + 1]) {
894
895 case FQDN_ENT:
896 Answer = snmp_var_new_integer(Var->name, Var->name_length,
897 fqdncacheCount(),
898 SMI_GAUGE32);
899 break;
900
901 case FQDN_REQ:
902 Answer = snmp_var_new_integer(Var->name, Var->name_length,
903 FqdncacheStats.requests,
904 SMI_COUNTER32);
905 break;
906
907 case FQDN_HITS:
908 Answer = snmp_var_new_integer(Var->name, Var->name_length,
909 FqdncacheStats.hits,
910 SMI_COUNTER32);
911 break;
912
913 case FQDN_PENDHIT:
914 /* this is now worthless */
915 Answer = snmp_var_new_integer(Var->name, Var->name_length,
916 0,
917 SMI_GAUGE32);
918 break;
919
920 case FQDN_NEGHIT:
921 Answer = snmp_var_new_integer(Var->name, Var->name_length,
922 FqdncacheStats.negative_hits,
923 SMI_COUNTER32);
924 break;
925
926 case FQDN_MISS:
927 Answer = snmp_var_new_integer(Var->name, Var->name_length,
928 FqdncacheStats.misses,
929 SMI_COUNTER32);
930 break;
931
932 case FQDN_GHBN:
933 Answer = snmp_var_new_integer(Var->name, Var->name_length,
934 0, /* deprecated */
935 SMI_COUNTER32);
936 break;
937
938 default:
939 *ErrP = SNMP_ERR_NOSUCHNAME;
940 break;
941 }
942
943 return Answer;
944 }
945
946 #endif /*SQUID_SNMP */