2 * DEBUG: section 14 IP Cache
3 * AUTHOR: Harvest Derived
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
34 #include "CacheManager.h"
37 #include "DnsLookupDetails.h"
39 #include "ip/Address.h"
43 #include "mgr/Registration.h"
45 #include "SquidConfig.h"
47 #include "SquidTime.h"
48 #include "StatCounters.h"
53 #include "snmp_core.h"
57 \defgroup IPCacheAPI IP Cache API
59 \section Introduction Introduction
61 * The IP cache is a built-in component of squid providing
62 * Hostname to IP-Number translation functionality and managing
63 * the involved data-structures. Efficiency concerns require
64 * mechanisms that allow non-blocking access to these mappings.
65 * The IP cache usually doesn't block on a request except for
66 * special cases where this is desired (see below).
68 \todo IP Cache should have its own API *.h header file.
72 \defgroup IPCacheInternal IP Cache Internals
74 \todo when IP cache is provided as a class. These sub-groups will be obsolete
75 * for now they are used to seperate the public and private functions.
76 * with the private ones all being in IPCachInternal and public in IPCacheAPI
78 \section InternalOperation Internal Operation
80 * Internally, the execution flow is as follows: On a miss,
81 * ipcache_getnbhostbyname checks whether a request for
82 * this name is already pending, and if positive, it creates
83 * a new entry using ipcacheAddNew with the IP_PENDING
84 * flag set . Then it calls ipcacheAddPending to add a
85 * request to the queue together with data and handler. Else,
86 * ipcache_dnsDispatch() is called to directly create a
87 * DNS query or to ipcacheEnqueue() if all no DNS port
88 * is free. ipcache_call_pending() is called regularly
89 * to walk down the pending list and call handlers. LRU clean-up
90 * is performed through ipcache_purgelru() according to
91 * the ipcache_high threshold.
97 * The data structure used for storing name-address mappings
98 * is a small hashtable (static hash_table *ip_table),
99 * where structures of type ipcache_entry whose most
100 * interesting members are:
105 hash_link hash
; /* must be first */
113 struct timeval request_time
;
115 unsigned short locks
;
121 int age() const; ///< time passed since request_time or -1 if unknown
124 /// \ingroup IPCacheInternal
125 static struct _ipcache_stats
{
139 /// \ingroup IPCacheInternal
140 static dlink_list lru_list
;
143 static void stat_ipcache_get(StoreEntry
*);
145 static FREE ipcacheFreeEntry
;
146 static IDNSCB ipcacheHandleReply
;
147 static int ipcacheExpiredEntry(ipcache_entry
*);
148 static int ipcacheParse(ipcache_entry
*, const rfc1035_rr
*, int, const char *error
);
149 static ipcache_entry
*ipcache_get(const char *);
150 static void ipcacheLockEntry(ipcache_entry
*);
151 static void ipcacheStatPrint(ipcache_entry
*, StoreEntry
*);
152 static void ipcacheUnlockEntry(ipcache_entry
*);
153 static void ipcacheRelease(ipcache_entry
*, bool dofree
= true);
155 /// \ingroup IPCacheInternal
156 static ipcache_addrs static_addrs
;
157 /// \ingroup IPCacheInternal
158 static hash_table
*ip_table
= NULL
;
160 /// \ingroup IPCacheInternal
161 static long ipcache_low
= 180;
162 /// \ingroup IPCacheInternal
163 static long ipcache_high
= 200;
165 #if LIBRESOLV_DNS_TTL_HACK
166 extern int _dns_ttl_
;
169 /// \ingroup IPCacheInternal
170 inline int ipcacheCount() { return ip_table
? ip_table
->count
: 0; }
173 ipcache_entry::age() const
175 return request_time
.tv_sec
? tvSubMsec(request_time
, current_time
) : -1;
179 \ingroup IPCacheInternal
181 * removes the given ipcache entry
184 ipcacheRelease(ipcache_entry
* i
, bool dofree
)
187 debugs(14, DBG_CRITICAL
, "ipcacheRelease: Releasing entry with i=<NULL>");
191 if (!i
|| !i
->hash
.key
) {
192 debugs(14, DBG_CRITICAL
, "ipcacheRelease: Releasing entry without hash link!");
196 debugs(14, 3, "ipcacheRelease: Releasing entry for '" << (const char *) i
->hash
.key
<< "'");
198 hash_remove_link(ip_table
, (hash_link
*) i
);
199 dlinkDelete(&i
->lru
, &lru_list
);
204 /// \ingroup IPCacheInternal
205 static ipcache_entry
*
206 ipcache_get(const char *name
)
208 if (ip_table
!= NULL
)
209 return (ipcache_entry
*) hash_lookup(ip_table
, name
);
214 /// \ingroup IPCacheInternal
216 ipcacheExpiredEntry(ipcache_entry
* i
)
218 /* all static entries are locked, so this takes care of them too */
223 if (i
->addrs
.count
== 0)
224 if (0 == i
->flags
.negcached
)
227 if (i
->expires
> squid_curtime
)
233 /// \ingroup IPCacheAPI
235 ipcache_purgelru(void *voidnotused
)
238 dlink_node
*prev
= NULL
;
241 eventAdd("ipcache_purgelru", ipcache_purgelru
, NULL
, 10.0, 1);
243 for (m
= lru_list
.tail
; m
; m
= prev
) {
244 if (ipcacheCount() < ipcache_low
)
249 i
= (ipcache_entry
*)m
->data
;
259 debugs(14, 9, "ipcache_purgelru: removed " << removed
<< " entries");
263 \ingroup IPCacheInternal
265 * purges entries added from /etc/hosts (or whatever).
268 purge_entries_fromhosts(void)
270 dlink_node
*m
= lru_list
.head
;
271 ipcache_entry
*i
= NULL
, *t
;
274 if (i
!= NULL
) { /* need to delay deletion */
275 ipcacheRelease(i
); /* we just override locks */
279 t
= (ipcache_entry
*)m
->data
;
281 if (t
->flags
.fromhosts
)
292 \ingroup IPCacheInternal
294 * create blank ipcache_entry
296 static ipcache_entry
*
297 ipcacheCreateEntry(const char *name
)
299 static ipcache_entry
*i
;
300 i
= (ipcache_entry
*)memAllocate(MEM_IPCACHE_ENTRY
);
301 i
->hash
.key
= xstrdup(name
);
302 Tolower(static_cast<char*>(i
->hash
.key
));
303 i
->expires
= squid_curtime
+ Config
.negativeDnsTtl
;
307 /// \ingroup IPCacheInternal
309 ipcacheAddEntry(ipcache_entry
* i
)
311 hash_link
*e
= (hash_link
*)hash_lookup(ip_table
, i
->hash
.key
);
314 /* avoid colission */
315 ipcache_entry
*q
= (ipcache_entry
*) e
;
319 hash_join(ip_table
, &i
->hash
);
320 dlinkAdd(i
, &i
->lru
, &lru_list
);
321 i
->lastref
= squid_curtime
;
325 \ingroup IPCacheInternal
327 * walks down the pending list, calling handlers
330 ipcacheCallback(ipcache_entry
*i
, int wait
)
332 IPH
*callback
= i
->handler
;
334 i
->lastref
= squid_curtime
;
341 callback
= i
->handler
;
345 if (cbdataReferenceValidDone(i
->handlerData
, &cbdata
)) {
346 const DnsLookupDetails
details(i
->error_message
, wait
);
347 callback((i
->addrs
.count
? &i
->addrs
: NULL
), details
, cbdata
);
350 ipcacheUnlockEntry(i
);
353 /// \ingroup IPCacheAPI
355 ipcacheParse(ipcache_entry
*i
, const rfc1035_rr
* answers
, int nr
, const char *error_message
)
361 const char *name
= (const char *)i
->hash
.key
;
364 i
->expires
= squid_curtime
+ Config
.negativeDnsTtl
;
365 i
->flags
.negcached
= true;
366 safe_free(i
->addrs
.in_addrs
);
367 assert(i
->addrs
.in_addrs
== NULL
);
368 safe_free(i
->addrs
.bad_mask
);
369 assert(i
->addrs
.bad_mask
== NULL
);
370 safe_free(i
->error_message
);
371 assert(i
->error_message
== NULL
);
375 debugs(14, 3, "ipcacheParse: Lookup failed '" << error_message
<< "' for '" << (const char *)i
->hash
.key
<< "'");
376 i
->error_message
= xstrdup(error_message
);
381 debugs(14, 3, "ipcacheParse: No DNS records in response to '" << name
<< "'");
382 i
->error_message
= xstrdup("No DNS records");
386 debugs(14, 3, "ipcacheParse: " << nr
<< " answers for '" << name
<< "'");
389 for (k
= 0; k
< nr
; ++k
) {
391 if (Ip::EnableIpv6
&& answers
[k
].type
== RFC1035_TYPE_AAAA
) {
392 if (answers
[k
].rdlength
!= sizeof(struct in6_addr
)) {
393 debugs(14, DBG_IMPORTANT
, "ipcacheParse: Invalid IPv6 address in response to '" << name
<< "'");
397 ++IpcacheStats
.rr_aaaa
;
401 if (answers
[k
].type
== RFC1035_TYPE_A
) {
402 if (answers
[k
].rdlength
!= sizeof(struct in_addr
)) {
403 debugs(14, DBG_IMPORTANT
, "ipcacheParse: Invalid IPv4 address in response to '" << name
<< "'");
411 /* With A and AAAA, the CNAME does not necessarily come with additional records to use. */
412 if (answers
[k
].type
== RFC1035_TYPE_CNAME
) {
414 ++IpcacheStats
.rr_cname
;
418 // otherwise its an unknown RR. debug at level 9 since we usually want to ignore these and they are common.
419 debugs(14, 9, HERE
<< "Unknown RR type received: type=" << answers
[k
].type
<< " starting at " << &(answers
[k
]) );
422 debugs(14, DBG_IMPORTANT
, "ipcacheParse: No Address records in response to '" << name
<< "'");
423 i
->error_message
= xstrdup("No Address records");
425 ++IpcacheStats
.cname_only
;
429 i
->addrs
.in_addrs
= static_cast<Ip::Address
*>(xcalloc(na
, sizeof(Ip::Address
)));
430 for (int l
= 0; l
< na
; ++l
)
431 i
->addrs
.in_addrs
[l
].setEmpty(); // perform same init actions as constructor would.
432 i
->addrs
.bad_mask
= (unsigned char *)xcalloc(na
, sizeof(unsigned char));
434 for (j
= 0, k
= 0; k
< nr
; ++k
) {
436 if (answers
[k
].type
== RFC1035_TYPE_A
) {
437 if (answers
[k
].rdlength
!= sizeof(struct in_addr
))
441 memcpy(&temp
, answers
[k
].rdata
, sizeof(struct in_addr
));
442 i
->addrs
.in_addrs
[j
] = temp
;
444 debugs(14, 3, "ipcacheParse: " << name
<< " #" << j
<< " " << i
->addrs
.in_addrs
[j
]);
447 } else if (Ip::EnableIpv6
&& answers
[k
].type
== RFC1035_TYPE_AAAA
) {
448 if (answers
[k
].rdlength
!= sizeof(struct in6_addr
))
451 struct in6_addr temp
;
452 memcpy(&temp
, answers
[k
].rdata
, sizeof(struct in6_addr
));
453 i
->addrs
.in_addrs
[j
] = temp
;
455 debugs(14, 3, "ipcacheParse: " << name
<< " #" << j
<< " " << i
->addrs
.in_addrs
[j
] );
458 if (ttl
== 0 || (int) answers
[k
].ttl
< ttl
)
459 ttl
= answers
[k
].ttl
;
465 i
->addrs
.count
= (unsigned char) na
;
467 i
->addrs
.count
= 255;
469 if (ttl
> Config
.positiveDnsTtl
)
470 ttl
= Config
.positiveDnsTtl
;
472 if (ttl
< Config
.negativeDnsTtl
)
473 ttl
= Config
.negativeDnsTtl
;
475 i
->expires
= squid_curtime
+ ttl
;
477 i
->flags
.negcached
= false;
479 return i
->addrs
.count
;
482 /// \ingroup IPCacheInternal
484 ipcacheHandleReply(void *data
, const rfc1035_rr
* answers
, int na
, const char *error_message
)
487 static_cast<generic_cbdata
*>(data
)->unwrap(&i
);
488 ++IpcacheStats
.replies
;
489 const int age
= i
->age();
490 statCounter
.dns
.svcTime
.count(age
);
492 int done
= ipcacheParse(i
, answers
, na
, error_message
);
494 /* If we have not produced either IPs or Error immediately, wait for recursion to finish. */
495 if (done
!= 0 || error_message
!= NULL
) {
497 ipcacheCallback(i
, age
);
504 \param name Host to resolve.
505 \param handler Pointer to the function to be called when the reply
506 * from the IP cache (or the DNS if the IP cache misses)
507 \param handlerData Information that is passed to the handler and does not affect the IP cache.
509 * XXX: on hits and some errors, the handler is called immediately instead
510 * of scheduling an async call. This reentrant behavior means that the
511 * user job must be extra careful after calling ipcache_nbgethostbyname,
512 * especially if the handler destroys the job. Moreover, the job has
513 * no way of knowing whether the reentrant call happened.
514 * Comm::Connection setup usually protects the job by scheduling an async call,
515 * but some user code calls ipcache_nbgethostbyname directly.
518 ipcache_nbgethostbyname(const char *name
, IPH
* handler
, void *handlerData
)
520 ipcache_entry
*i
= NULL
;
521 const ipcache_addrs
*addrs
= NULL
;
523 debugs(14, 4, "ipcache_nbgethostbyname: Name '" << name
<< "'.");
524 ++IpcacheStats
.requests
;
526 if (name
== NULL
|| name
[0] == '\0') {
527 debugs(14, 4, "ipcache_nbgethostbyname: Invalid name!");
528 ++IpcacheStats
.invalid
;
529 const DnsLookupDetails
details("Invalid hostname", -1); // error, no lookup
531 handler(NULL
, details
, handlerData
);
535 if ((addrs
= ipcacheCheckNumeric(name
))) {
536 debugs(14, 4, "ipcache_nbgethostbyname: BYPASS for '" << name
<< "' (already numeric)");
537 ++IpcacheStats
.numeric_hits
;
538 const DnsLookupDetails
details(NULL
, -1); // no error, no lookup
540 handler(addrs
, details
, handlerData
);
544 i
= ipcache_get(name
);
549 } else if (ipcacheExpiredEntry(i
)) {
550 /* hit, but expired -- bummer */
555 debugs(14, 4, "ipcache_nbgethostbyname: HIT for '" << name
<< "'");
557 if (i
->flags
.negcached
)
558 ++IpcacheStats
.negative_hits
;
562 i
->handler
= handler
;
564 i
->handlerData
= cbdataReference(handlerData
);
566 ipcacheCallback(i
, -1); // no lookup
571 debugs(14, 5, "ipcache_nbgethostbyname: MISS for '" << name
<< "'");
572 ++IpcacheStats
.misses
;
573 i
= ipcacheCreateEntry(name
);
574 i
->handler
= handler
;
575 i
->handlerData
= cbdataReference(handlerData
);
576 i
->request_time
= current_time
;
577 c
= new generic_cbdata(i
);
578 idnsALookup(hashKeyStr(&i
->hash
), ipcacheHandleReply
, c
);
581 /// \ingroup IPCacheInternal
583 ipcacheRegisterWithCacheManager(void)
585 Mgr::RegisterAction("ipcache",
586 "IP Cache Stats and Contents",
587 stat_ipcache_get
, 0, 1);
593 * Initialize the ipcache.
594 * Is called from mainInitialize() after disk initialization
595 * and prior to the reverse FQDNCache initialization
601 debugs(14, DBG_IMPORTANT
, "Initializing IP Cache...");
602 memset(&IpcacheStats
, '\0', sizeof(IpcacheStats
));
603 memset(&lru_list
, '\0', sizeof(lru_list
));
604 memset(&static_addrs
, '\0', sizeof(ipcache_addrs
));
606 static_addrs
.in_addrs
= static_cast<Ip::Address
*>(xcalloc(1, sizeof(Ip::Address
)));
607 static_addrs
.in_addrs
->setEmpty(); // properly setup the Ip::Address!
608 static_addrs
.bad_mask
= (unsigned char *)xcalloc(1, sizeof(unsigned char));
609 ipcache_high
= (long) (((float) Config
.ipcache
.size
*
610 (float) Config
.ipcache
.high
) / (float) 100);
611 ipcache_low
= (long) (((float) Config
.ipcache
.size
*
612 (float) Config
.ipcache
.low
) / (float) 100);
613 n
= hashPrime(ipcache_high
/ 4);
614 ip_table
= hash_create((HASHCMP
*) strcmp
, n
, hash4
);
615 memDataInit(MEM_IPCACHE_ENTRY
, "ipcache_entry", sizeof(ipcache_entry
), 0);
617 ipcacheRegisterWithCacheManager();
623 * Is different from ipcache_nbgethostbyname in that it only checks
624 * if an entry exists in the cache and does not by default contact the DNS,
625 * unless this is requested, by setting the flags.
627 \param name Host name to resolve.
628 \param flags Default is NULL, set to IP_LOOKUP_IF_MISS
629 * to explicitly perform DNS lookups.
631 \retval NULL An error occured during lookup
632 \retval NULL No results available in cache and no lookup specified
633 \retval * Pointer to the ipcahce_addrs structure containing the lookup results
635 const ipcache_addrs
*
636 ipcache_gethostbyname(const char *name
, int flags
)
638 ipcache_entry
*i
= NULL
;
639 ipcache_addrs
*addrs
;
641 debugs(14, 3, "ipcache_gethostbyname: '" << name
<< "', flags=" << std::hex
<< flags
);
642 ++IpcacheStats
.requests
;
643 i
= ipcache_get(name
);
647 } else if (ipcacheExpiredEntry(i
)) {
650 } else if (i
->flags
.negcached
) {
651 ++IpcacheStats
.negative_hits
;
652 // ignore i->error_message: the caller just checks IP cache presence
656 i
->lastref
= squid_curtime
;
657 // ignore i->error_message: the caller just checks IP cache presence
661 /* no entry [any more] */
663 if ((addrs
= ipcacheCheckNumeric(name
))) {
664 ++IpcacheStats
.numeric_hits
;
668 ++IpcacheStats
.misses
;
670 if (flags
& IP_LOOKUP_IF_MISS
)
671 ipcache_nbgethostbyname(name
, NULL
, NULL
);
676 /// \ingroup IPCacheInternal
678 ipcacheStatPrint(ipcache_entry
* i
, StoreEntry
* sentry
)
681 char buf
[MAX_IPSTRLEN
];
684 debugs(14, DBG_CRITICAL
, HERE
<< "CRITICAL: sentry is NULL!");
689 debugs(14, DBG_CRITICAL
, HERE
<< "CRITICAL: ipcache_entry is NULL!");
690 storeAppendPrintf(sentry
, "CRITICAL ERROR\n");
694 int count
= i
->addrs
.count
;
696 storeAppendPrintf(sentry
, " %-32.32s %c%c %6d %6d %2d(%2d)",
697 hashKeyStr(&i
->hash
),
698 i
->flags
.fromhosts
? 'H' : ' ',
699 i
->flags
.negcached
? 'N' : ' ',
700 (int) (squid_curtime
- i
->lastref
),
701 (int) ((i
->flags
.fromhosts
? -1 : i
->expires
- squid_curtime
)),
702 (int) i
->addrs
.count
,
703 (int) i
->addrs
.badcount
);
706 * Negative-cached entries have no IPs listed. */
707 if (i
->flags
.negcached
) {
708 storeAppendPrintf(sentry
, "\n");
713 * Cached entries have IPs listed with a BNF of: ip-address '-' ('OK'|'BAD') */
714 for (k
= 0; k
< count
; ++k
) {
715 /* Display tidy-up: IPv6 are so big make the list vertical */
717 storeAppendPrintf(sentry
, " %45.45s-%3s\n",
718 i
->addrs
.in_addrs
[k
].toStr(buf
,MAX_IPSTRLEN
),
719 i
->addrs
.bad_mask
[k
] ? "BAD" : "OK ");
721 storeAppendPrintf(sentry
, "%s %45.45s-%3s\n",
722 " ", /* blank-space indenting IP list */
723 i
->addrs
.in_addrs
[k
].toStr(buf
,MAX_IPSTRLEN
),
724 i
->addrs
.bad_mask
[k
] ? "BAD" : "OK ");
729 \ingroup IPCacheInternal
731 * process objects list
734 stat_ipcache_get(StoreEntry
* sentry
)
737 assert(ip_table
!= NULL
);
738 storeAppendPrintf(sentry
, "IP Cache Statistics:\n");
739 storeAppendPrintf(sentry
, "IPcache Entries In Use: %d\n",
740 memInUse(MEM_IPCACHE_ENTRY
));
741 storeAppendPrintf(sentry
, "IPcache Entries Cached: %d\n",
743 storeAppendPrintf(sentry
, "IPcache Requests: %d\n",
744 IpcacheStats
.requests
);
745 storeAppendPrintf(sentry
, "IPcache Hits: %d\n",
747 storeAppendPrintf(sentry
, "IPcache Negative Hits: %d\n",
748 IpcacheStats
.negative_hits
);
749 storeAppendPrintf(sentry
, "IPcache Numeric Hits: %d\n",
750 IpcacheStats
.numeric_hits
);
751 storeAppendPrintf(sentry
, "IPcache Misses: %d\n",
752 IpcacheStats
.misses
);
753 storeAppendPrintf(sentry
, "IPcache Retrieved A: %d\n",
755 storeAppendPrintf(sentry
, "IPcache Retrieved AAAA: %d\n",
756 IpcacheStats
.rr_aaaa
);
757 storeAppendPrintf(sentry
, "IPcache Retrieved CNAME: %d\n",
758 IpcacheStats
.rr_cname
);
759 storeAppendPrintf(sentry
, "IPcache CNAME-Only Response: %d\n",
760 IpcacheStats
.cname_only
);
761 storeAppendPrintf(sentry
, "IPcache Invalid Request: %d\n",
762 IpcacheStats
.invalid
);
763 storeAppendPrintf(sentry
, "\n\n");
764 storeAppendPrintf(sentry
, "IP Cache Contents:\n\n");
765 storeAppendPrintf(sentry
, " %-31.31s %3s %6s %6s %4s\n",
772 for (m
= lru_list
.head
; m
; m
= m
->next
) {
773 assert( m
->next
!= m
);
774 ipcacheStatPrint((ipcache_entry
*)m
->data
, sentry
);
778 /// \ingroup IPCacheAPI
780 ipcacheInvalidate(const char *name
)
784 if ((i
= ipcache_get(name
)) == NULL
)
787 i
->expires
= squid_curtime
;
790 * NOTE, don't call ipcacheRelease here because we might be here due
791 * to a thread started from a callback.
795 /// \ingroup IPCacheAPI
797 ipcacheInvalidateNegative(const char *name
)
801 if ((i
= ipcache_get(name
)) == NULL
)
804 if (i
->flags
.negcached
)
805 i
->expires
= squid_curtime
;
808 * NOTE, don't call ipcacheRelease here because we might be here due
809 * to a thread started from a callback.
813 /// \ingroup IPCacheAPI
815 ipcacheCheckNumeric(const char *name
)
818 /* check if it's already a IP address in text form. */
820 /* it may be IPv6-wrapped */
821 if (name
[0] == '[') {
822 char *tmp
= xstrdup(&name
[1]);
823 tmp
[strlen(tmp
)-1] = '\0';
829 } else if (!(ip
= name
))
832 debugs(14, 4, "ipcacheCheckNumeric: HIT_BYPASS for '" << name
<< "' == " << ip
);
834 static_addrs
.count
= 1;
836 static_addrs
.cur
= 0;
838 static_addrs
.in_addrs
[0] = ip
;
840 static_addrs
.bad_mask
[0] = FALSE
;
842 static_addrs
.badcount
= 0;
844 return &static_addrs
;
847 /// \ingroup IPCacheInternal
849 ipcacheLockEntry(ipcache_entry
* i
)
851 if (i
->locks
++ == 0) {
852 dlinkDelete(&i
->lru
, &lru_list
);
853 dlinkAdd(i
, &i
->lru
, &lru_list
);
857 /// \ingroup IPCacheInternal
859 ipcacheUnlockEntry(ipcache_entry
* i
)
862 debugs(14, DBG_IMPORTANT
, "WARNING: ipcacheEntry unlocked with no lock! locks=" << i
->locks
);
868 if (ipcacheExpiredEntry(i
))
872 /// \ingroup IPCacheAPI
874 ipcacheCycleAddr(const char *name
, ipcache_addrs
* ia
)
881 if ((i
= ipcache_get(name
)) == NULL
)
884 if (i
->flags
.negcached
)
890 for (k
= 0; k
< ia
->count
; ++k
) {
891 if (++ia
->cur
== ia
->count
)
894 if (!ia
->bad_mask
[ia
->cur
])
898 if (k
== ia
->count
) {
899 /* All bad, reset to All good */
900 debugs(14, 3, "ipcacheCycleAddr: Changing ALL " << name
<< " addrs from BAD to OK");
902 for (k
= 0; k
< ia
->count
; ++k
)
910 /* NP: zero-based so we increase the human-readable number of our position */
911 debugs(14, 3, "ipcacheCycleAddr: " << name
<< " now at " << ia
->in_addrs
[ia
->cur
] << " (" << (ia
->cur
+1) << " of " << ia
->count
<< ")");
917 \param name domain name to have an IP marked bad
918 \param addr specific addres to be marked bad
921 ipcacheMarkBadAddr(const char *name
, const Ip::Address
&addr
)
927 /** Does nothing if the domain name does not exist. */
928 if ((i
= ipcache_get(name
)) == NULL
)
933 for (k
= 0; k
< (int) ia
->count
; ++k
) {
934 if (addr
== ia
->in_addrs
[k
] )
938 /** Does nothing if the IP does not exist for the doamin. */
939 if (k
== (int) ia
->count
)
942 /** Marks the given address as BAD */
943 if (!ia
->bad_mask
[k
]) {
944 ia
->bad_mask
[k
] = TRUE
;
946 i
->expires
= min(squid_curtime
+ max((time_t)60, Config
.negativeDnsTtl
), i
->expires
);
947 debugs(14, 2, "ipcacheMarkBadAddr: " << name
<< " " << addr
);
950 /** then calls ipcacheCycleAddr() to advance the current pointer to the next OK address. */
951 ipcacheCycleAddr(name
, ia
);
954 /// \ingroup IPCacheAPI
956 ipcacheMarkAllGood(const char *name
)
962 if ((i
= ipcache_get(name
)) == NULL
)
967 /* All bad, reset to All good */
968 debugs(14, 3, "ipcacheMarkAllGood: Changing ALL " << name
<< " addrs to OK (" << ia
->badcount
<< "/" << ia
->count
<< " bad)");
970 for (k
= 0; k
< ia
->count
; ++k
)
976 /// \ingroup IPCacheAPI
978 ipcacheMarkGoodAddr(const char *name
, const Ip::Address
&addr
)
984 if ((i
= ipcache_get(name
)) == NULL
)
989 for (k
= 0; k
< (int) ia
->count
; ++k
) {
990 if (addr
== ia
->in_addrs
[k
])
994 if (k
== (int) ia
->count
) /* not found */
997 if (!ia
->bad_mask
[k
]) /* already OK */
1000 ia
->bad_mask
[k
] = FALSE
;
1004 debugs(14, 2, "ipcacheMarkGoodAddr: " << name
<< " " << addr
);
1007 /// \ingroup IPCacheInternal
1009 ipcacheFreeEntry(void *data
)
1011 ipcache_entry
*i
= (ipcache_entry
*)data
;
1012 safe_free(i
->addrs
.in_addrs
);
1013 safe_free(i
->addrs
.bad_mask
);
1014 safe_free(i
->hash
.key
);
1015 safe_free(i
->error_message
);
1016 memFree(i
, MEM_IPCACHE_ENTRY
);
1019 /// \ingroup IPCacheAPI
1021 ipcacheFreeMemory(void)
1023 hashFreeItems(ip_table
, ipcacheFreeEntry
);
1024 hashFreeMemory(ip_table
);
1031 * Recalculate IP cache size upon reconfigure.
1032 * Is called to clear the IPCache's data structures,
1033 * cancel all pending requests.
1036 ipcache_restart(void)
1038 ipcache_high
= (long) (((float) Config
.ipcache
.size
*
1039 (float) Config
.ipcache
.high
) / (float) 100);
1040 ipcache_low
= (long) (((float) Config
.ipcache
.size
*
1041 (float) Config
.ipcache
.low
) / (float) 100);
1042 purge_entries_fromhosts();
1048 * Adds a "static" entry from /etc/hosts
1050 \param name Hostname to be linked with IP
1051 \param ipaddr IP Address to be cached.
1054 \retval 1 IP address is invalid or other error.
1057 ipcacheAddEntryFromHosts(const char *name
, const char *ipaddr
)
1063 if (!(ip
= ipaddr
)) {
1064 if (strchr(ipaddr
, ':') && strspn(ipaddr
, "0123456789abcdefABCDEF:") == strlen(ipaddr
)) {
1065 debugs(14, 3, "ipcacheAddEntryFromHosts: Skipping IPv6 address '" << ipaddr
<< "'");
1067 debugs(14, DBG_IMPORTANT
, "ipcacheAddEntryFromHosts: Bad IP address '" << ipaddr
<< "'");
1073 if ((i
= ipcache_get(name
))) {
1074 if (1 == i
->flags
.fromhosts
) {
1075 ipcacheUnlockEntry(i
);
1076 } else if (i
->locks
> 0) {
1077 debugs(14, DBG_IMPORTANT
, "ipcacheAddEntryFromHosts: can't add static entry for locked name '" << name
<< "'");
1084 i
= ipcacheCreateEntry(name
);
1087 i
->addrs
.badcount
= 0;
1089 i
->addrs
.in_addrs
= static_cast<Ip::Address
*>(xcalloc(1, sizeof(Ip::Address
)));
1090 i
->addrs
.bad_mask
= (unsigned char *)xcalloc(1, sizeof(unsigned char));
1091 i
->addrs
.in_addrs
[0] = ip
;
1092 i
->addrs
.bad_mask
[0] = FALSE
;
1093 i
->flags
.fromhosts
= true;
1095 ipcacheLockEntry(i
);
1103 * The function to return the ip cache statistics to via SNMP
1106 snmp_netIpFn(variable_list
* Var
, snint
* ErrP
)
1108 variable_list
*Answer
= NULL
;
1110 debugs(49, 5, "snmp_netIpFn: Processing request:" << snmpDebugOid(Var
->name
, Var
->name_length
, tmp
));
1111 *ErrP
= SNMP_ERR_NOERROR
;
1113 switch (Var
->name
[LEN_SQ_NET
+ 1]) {
1116 Answer
= snmp_var_new_integer(Var
->name
, Var
->name_length
,
1122 Answer
= snmp_var_new_integer(Var
->name
, Var
->name_length
,
1123 IpcacheStats
.requests
,
1128 Answer
= snmp_var_new_integer(Var
->name
, Var
->name_length
,
1134 Answer
= snmp_var_new_integer(Var
->name
, Var
->name_length
,
1140 Answer
= snmp_var_new_integer(Var
->name
, Var
->name_length
,
1141 IpcacheStats
.negative_hits
,
1146 Answer
= snmp_var_new_integer(Var
->name
, Var
->name_length
,
1147 IpcacheStats
.misses
,
1152 Answer
= snmp_var_new_integer(Var
->name
, Var
->name_length
,
1158 Answer
= snmp_var_new_integer(Var
->name
, Var
->name_length
,
1164 *ErrP
= SNMP_ERR_NOSUCHNAME
;
1165 snmp_var_free(Answer
);
1172 #endif /*SQUID_SNMP */