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.
35 #include "CacheManager.h"
36 #include "DnsLookupDetails.h"
38 #include "ip/Address.h"
42 #include "mgr/Registration.h"
45 #include "SquidTime.h"
46 #include "StatCounters.h"
51 #include "snmp_core.h"
55 \defgroup IPCacheAPI IP Cache API
57 \section Introduction Introduction
59 * The IP cache is a built-in component of squid providing
60 * Hostname to IP-Number translation functionality and managing
61 * the involved data-structures. Efficiency concerns require
62 * mechanisms that allow non-blocking access to these mappings.
63 * The IP cache usually doesn't block on a request except for
64 * special cases where this is desired (see below).
66 \todo IP Cache should have its own API *.h header file.
70 \defgroup IPCacheInternal IP Cache Internals
72 \todo when IP cache is provided as a class. These sub-groups will be obsolete
73 * for now they are used to seperate the public and private functions.
74 * with the private ones all being in IPCachInternal and public in IPCacheAPI
76 \section InternalOperation Internal Operation
78 * Internally, the execution flow is as follows: On a miss,
79 * ipcache_getnbhostbyname checks whether a request for
80 * this name is already pending, and if positive, it creates
81 * a new entry using ipcacheAddNew with the IP_PENDING
82 * flag set . Then it calls ipcacheAddPending to add a
83 * request to the queue together with data and handler. Else,
84 * ipcache_dnsDispatch() is called to directly create a
85 * DNS query or to ipcacheEnqueue() if all no DNS port
86 * is free. ipcache_call_pending() is called regularly
87 * to walk down the pending list and call handlers. LRU clean-up
88 * is performed through ipcache_purgelru() according to
89 * the ipcache_high threshold.
95 * The data structure used for storing name-address mappings
96 * is a small hashtable (static hash_table *ip_table),
97 * where structures of type ipcache_entry whose most
98 * interesting members are:
103 hash_link hash
; /* must be first */
111 struct timeval request_time
;
113 unsigned short locks
;
115 unsigned int negcached
:1;
116 unsigned int fromhosts
:1;
119 int age() const; ///< time passed since request_time or -1 if unknown
122 /// \ingroup IPCacheInternal
123 static struct _ipcache_stats
{
137 /// \ingroup IPCacheInternal
138 static dlink_list lru_list
;
141 static void stat_ipcache_get(StoreEntry
*);
143 static FREE ipcacheFreeEntry
;
145 static HLPCB ipcacheHandleReply
;
147 static IDNSCB ipcacheHandleReply
;
149 static int ipcacheExpiredEntry(ipcache_entry
*);
151 static int ipcacheParse(ipcache_entry
*, const char *buf
);
153 static int ipcacheParse(ipcache_entry
*, const rfc1035_rr
*, int, const char *error
);
155 static ipcache_entry
*ipcache_get(const char *);
156 static void ipcacheLockEntry(ipcache_entry
*);
157 static void ipcacheStatPrint(ipcache_entry
*, StoreEntry
*);
158 static void ipcacheUnlockEntry(ipcache_entry
*);
159 static void ipcacheRelease(ipcache_entry
*, bool dofree
= true);
161 /// \ingroup IPCacheInternal
162 static ipcache_addrs static_addrs
;
163 /// \ingroup IPCacheInternal
164 static hash_table
*ip_table
= NULL
;
166 /// \ingroup IPCacheInternal
167 static long ipcache_low
= 180;
168 /// \ingroup IPCacheInternal
169 static long ipcache_high
= 200;
171 #if LIBRESOLV_DNS_TTL_HACK
172 extern int _dns_ttl_
;
175 /// \ingroup IPCacheInternal
176 inline int ipcacheCount() { return ip_table
? ip_table
->count
: 0; }
179 ipcache_entry::age() const
181 return request_time
.tv_sec
? tvSubMsec(request_time
, current_time
) : -1;
185 \ingroup IPCacheInternal
187 * removes the given ipcache entry
190 ipcacheRelease(ipcache_entry
* i
, bool dofree
)
193 debugs(14, DBG_CRITICAL
, "ipcacheRelease: Releasing entry with i=<NULL>");
197 if (!i
|| !i
->hash
.key
) {
198 debugs(14, DBG_CRITICAL
, "ipcacheRelease: Releasing entry without hash link!");
202 debugs(14, 3, "ipcacheRelease: Releasing entry for '" << (const char *) i
->hash
.key
<< "'");
204 hash_remove_link(ip_table
, (hash_link
*) i
);
205 dlinkDelete(&i
->lru
, &lru_list
);
210 /// \ingroup IPCacheInternal
211 static ipcache_entry
*
212 ipcache_get(const char *name
)
214 if (ip_table
!= NULL
)
215 return (ipcache_entry
*) hash_lookup(ip_table
, name
);
220 /// \ingroup IPCacheInternal
222 ipcacheExpiredEntry(ipcache_entry
* i
)
224 /* all static entries are locked, so this takes care of them too */
229 if (i
->addrs
.count
== 0)
230 if (0 == i
->flags
.negcached
)
233 if (i
->expires
> squid_curtime
)
239 /// \ingroup IPCacheAPI
241 ipcache_purgelru(void *voidnotused
)
244 dlink_node
*prev
= NULL
;
247 eventAdd("ipcache_purgelru", ipcache_purgelru
, NULL
, 10.0, 1);
249 for (m
= lru_list
.tail
; m
; m
= prev
) {
250 if (ipcacheCount() < ipcache_low
)
255 i
= (ipcache_entry
*)m
->data
;
265 debugs(14, 9, "ipcache_purgelru: removed " << removed
<< " entries");
269 \ingroup IPCacheInternal
271 * purges entries added from /etc/hosts (or whatever).
274 purge_entries_fromhosts(void)
276 dlink_node
*m
= lru_list
.head
;
277 ipcache_entry
*i
= NULL
, *t
;
280 if (i
!= NULL
) { /* need to delay deletion */
281 ipcacheRelease(i
); /* we just override locks */
285 t
= (ipcache_entry
*)m
->data
;
287 if (t
->flags
.fromhosts
)
298 \ingroup IPCacheInternal
300 * create blank ipcache_entry
302 static ipcache_entry
*
303 ipcacheCreateEntry(const char *name
)
305 static ipcache_entry
*i
;
306 i
= (ipcache_entry
*)memAllocate(MEM_IPCACHE_ENTRY
);
307 i
->hash
.key
= xstrdup(name
);
308 Tolower(static_cast<char*>(i
->hash
.key
));
309 i
->expires
= squid_curtime
+ Config
.negativeDnsTtl
;
313 /// \ingroup IPCacheInternal
315 ipcacheAddEntry(ipcache_entry
* i
)
317 hash_link
*e
= (hash_link
*)hash_lookup(ip_table
, i
->hash
.key
);
320 /* avoid colission */
321 ipcache_entry
*q
= (ipcache_entry
*) e
;
325 hash_join(ip_table
, &i
->hash
);
326 dlinkAdd(i
, &i
->lru
, &lru_list
);
327 i
->lastref
= squid_curtime
;
331 \ingroup IPCacheInternal
333 * walks down the pending list, calling handlers
336 ipcacheCallback(ipcache_entry
*i
, int wait
)
338 IPH
*callback
= i
->handler
;
340 i
->lastref
= squid_curtime
;
347 callback
= i
->handler
;
351 if (cbdataReferenceValidDone(i
->handlerData
, &cbdata
)) {
352 const DnsLookupDetails
details(i
->error_message
, wait
);
353 callback((i
->addrs
.count
? &i
->addrs
: NULL
), details
, cbdata
);
356 ipcacheUnlockEntry(i
);
359 /// \ingroup IPCacheAPI
362 ipcacheParse(ipcache_entry
*i
, const char *inbuf
)
364 LOCAL_ARRAY(char, buf
, DNS_INBUF_SZ
);
369 const char *name
= (const char *)i
->hash
.key
;
370 i
->expires
= squid_curtime
+ Config
.negativeDnsTtl
;
371 i
->flags
.negcached
= 1;
372 safe_free(i
->addrs
.in_addrs
);
373 safe_free(i
->addrs
.bad_mask
);
374 safe_free(i
->error_message
);
378 debugs(14, DBG_IMPORTANT
, "ipcacheParse: Got <NULL> reply");
379 i
->error_message
= xstrdup("Internal Error");
383 xstrncpy(buf
, inbuf
, DNS_INBUF_SZ
);
384 debugs(14, 5, "ipcacheParse: parsing: {" << buf
<< "}");
385 token
= strtok(buf
, w_space
);
388 debugs(14, DBG_IMPORTANT
, "ipcacheParse: expecting result, got '" << inbuf
<< "'");
390 i
->error_message
= xstrdup("Internal Error");
394 if (0 == strcmp(token
, "$fail")) {
395 token
= strtok(NULL
, "\n");
396 assert(NULL
!= token
);
397 i
->error_message
= xstrdup(token
);
401 if (0 != strcmp(token
, "$addr")) {
402 debugs(14, DBG_IMPORTANT
, "ipcacheParse: expecting '$addr', got '" << inbuf
<< "' in response to '" << name
<< "'");
404 i
->error_message
= xstrdup("Internal Error");
408 token
= strtok(NULL
, w_space
);
411 debugs(14, DBG_IMPORTANT
, "ipcacheParse: expecting TTL, got '" << inbuf
<< "' in response to '" << name
<< "'");
413 i
->error_message
= xstrdup("Internal Error");
419 while (NULL
!= (token
= strtok(NULL
, w_space
))) {
429 i
->addrs
.in_addrs
= static_cast<Ip::Address
*>(xcalloc(ipcount
, sizeof(Ip::Address
)));
430 for (int l
= 0; l
< ipcount
; ++l
)
431 i
->addrs
.in_addrs
[l
].SetEmpty(); // perform same init actions as constructor would.
432 i
->addrs
.bad_mask
= (unsigned char *)xcalloc(ipcount
, sizeof(unsigned char));
433 memset(i
->addrs
.bad_mask
, 0, sizeof(unsigned char) * ipcount
);
435 for (j
= 0, k
= 0; k
< ipcount
; ++k
) {
436 if ( i
->addrs
.in_addrs
[j
] = A
[k
] )
439 debugs(14, DBG_IMPORTANT
, "ipcacheParse: Invalid IP address '" << A
[k
] << "' in response to '" << name
<< "'");
442 i
->addrs
.count
= (unsigned char) j
;
445 if (i
->addrs
.count
<= 0) {
446 debugs(14, DBG_IMPORTANT
, "ipcacheParse: No addresses in response to '" << name
<< "'");
450 if (ttl
== 0 || ttl
> Config
.positiveDnsTtl
)
451 ttl
= Config
.positiveDnsTtl
;
453 if (ttl
< Config
.negativeDnsTtl
)
454 ttl
= Config
.negativeDnsTtl
;
456 i
->expires
= squid_curtime
+ ttl
;
458 i
->flags
.negcached
= 0;
460 return i
->addrs
.count
;
465 ipcacheParse(ipcache_entry
*i
, const rfc1035_rr
* answers
, int nr
, const char *error_message
)
471 const char *name
= (const char *)i
->hash
.key
;
474 i
->expires
= squid_curtime
+ Config
.negativeDnsTtl
;
475 i
->flags
.negcached
= 1;
476 safe_free(i
->addrs
.in_addrs
);
477 assert(i
->addrs
.in_addrs
== NULL
);
478 safe_free(i
->addrs
.bad_mask
);
479 assert(i
->addrs
.bad_mask
== NULL
);
480 safe_free(i
->error_message
);
481 assert(i
->error_message
== NULL
);
485 debugs(14, 3, "ipcacheParse: Lookup failed '" << error_message
<< "' for '" << (const char *)i
->hash
.key
<< "'");
486 i
->error_message
= xstrdup(error_message
);
491 debugs(14, 3, "ipcacheParse: No DNS records in response to '" << name
<< "'");
492 i
->error_message
= xstrdup("No DNS records");
496 debugs(14, 3, "ipcacheParse: " << nr
<< " answers for '" << name
<< "'");
499 for (k
= 0; k
< nr
; ++k
) {
501 if (Ip::EnableIpv6
&& answers
[k
].type
== RFC1035_TYPE_AAAA
) {
502 if (answers
[k
].rdlength
!= sizeof(struct in6_addr
)) {
503 debugs(14, DBG_IMPORTANT
, "ipcacheParse: Invalid IPv6 address in response to '" << name
<< "'");
507 ++IpcacheStats
.rr_aaaa
;
511 if (answers
[k
].type
== RFC1035_TYPE_A
) {
512 if (answers
[k
].rdlength
!= sizeof(struct in_addr
)) {
513 debugs(14, DBG_IMPORTANT
, "ipcacheParse: Invalid IPv4 address in response to '" << name
<< "'");
521 /* With A and AAAA, the CNAME does not necessarily come with additional records to use. */
522 if (answers
[k
].type
== RFC1035_TYPE_CNAME
) {
524 ++IpcacheStats
.rr_cname
;
528 // otherwise its an unknown RR. debug at level 9 since we usually want to ignore these and they are common.
529 debugs(14, 9, HERE
<< "Unknown RR type received: type=" << answers
[k
].type
<< " starting at " << &(answers
[k
]) );
532 debugs(14, DBG_IMPORTANT
, "ipcacheParse: No Address records in response to '" << name
<< "'");
533 i
->error_message
= xstrdup("No Address records");
535 ++IpcacheStats
.cname_only
;
539 i
->addrs
.in_addrs
= static_cast<Ip::Address
*>(xcalloc(na
, sizeof(Ip::Address
)));
540 for (int l
= 0; l
< na
; ++l
)
541 i
->addrs
.in_addrs
[l
].SetEmpty(); // perform same init actions as constructor would.
542 i
->addrs
.bad_mask
= (unsigned char *)xcalloc(na
, sizeof(unsigned char));
544 for (j
= 0, k
= 0; k
< nr
; ++k
) {
546 if (answers
[k
].type
== RFC1035_TYPE_A
) {
547 if (answers
[k
].rdlength
!= sizeof(struct in_addr
))
551 memcpy(&temp
, answers
[k
].rdata
, sizeof(struct in_addr
));
552 i
->addrs
.in_addrs
[j
] = temp
;
554 debugs(14, 3, "ipcacheParse: " << name
<< " #" << j
<< " " << i
->addrs
.in_addrs
[j
]);
557 } else if (Ip::EnableIpv6
&& answers
[k
].type
== RFC1035_TYPE_AAAA
) {
558 if (answers
[k
].rdlength
!= sizeof(struct in6_addr
))
561 struct in6_addr temp
;
562 memcpy(&temp
, answers
[k
].rdata
, sizeof(struct in6_addr
));
563 i
->addrs
.in_addrs
[j
] = temp
;
565 debugs(14, 3, "ipcacheParse: " << name
<< " #" << j
<< " " << i
->addrs
.in_addrs
[j
] );
568 if (ttl
== 0 || (int) answers
[k
].ttl
< ttl
)
569 ttl
= answers
[k
].ttl
;
575 i
->addrs
.count
= (unsigned char) na
;
577 i
->addrs
.count
= 255;
579 if (ttl
> Config
.positiveDnsTtl
)
580 ttl
= Config
.positiveDnsTtl
;
582 if (ttl
< Config
.negativeDnsTtl
)
583 ttl
= Config
.negativeDnsTtl
;
585 i
->expires
= squid_curtime
+ ttl
;
587 i
->flags
.negcached
= 0;
589 return i
->addrs
.count
;
594 /// \ingroup IPCacheInternal
597 ipcacheHandleReply(void *data
, char *reply
)
599 ipcacheHandleReply(void *data
, const rfc1035_rr
* answers
, int na
, const char *error_message
)
603 static_cast<generic_cbdata
*>(data
)->unwrap(&i
);
604 ++IpcacheStats
.replies
;
605 const int age
= i
->age();
606 statCounter
.dns
.svcTime
.count(age
);
609 ipcacheParse(i
, reply
);
612 int done
= ipcacheParse(i
, answers
, na
, error_message
);
614 /* If we have not produced either IPs or Error immediately, wait for recursion to finish. */
615 if (done
!= 0 || error_message
!= NULL
)
620 ipcacheCallback(i
, age
);
627 \param name Host to resolve.
628 \param handler Pointer to the function to be called when the reply
629 * from the IP cache (or the DNS if the IP cache misses)
630 \param handlerData Information that is passed to the handler and does not affect the IP cache.
632 * XXX: on hits and some errors, the handler is called immediately instead
633 * of scheduling an async call. This reentrant behavior means that the
634 * user job must be extra careful after calling ipcache_nbgethostbyname,
635 * especially if the handler destroys the job. Moreover, the job has
636 * no way of knowing whether the reentrant call happened.
637 * Comm::Connection setup usually protects the job by scheduling an async call,
638 * but some user code calls ipcache_nbgethostbyname directly.
641 ipcache_nbgethostbyname(const char *name
, IPH
* handler
, void *handlerData
)
643 ipcache_entry
*i
= NULL
;
644 const ipcache_addrs
*addrs
= NULL
;
646 debugs(14, 4, "ipcache_nbgethostbyname: Name '" << name
<< "'.");
647 ++IpcacheStats
.requests
;
649 if (name
== NULL
|| name
[0] == '\0') {
650 debugs(14, 4, "ipcache_nbgethostbyname: Invalid name!");
651 ++IpcacheStats
.invalid
;
652 const DnsLookupDetails
details("Invalid hostname", -1); // error, no lookup
654 handler(NULL
, details
, handlerData
);
658 if ((addrs
= ipcacheCheckNumeric(name
))) {
659 debugs(14, 4, "ipcache_nbgethostbyname: BYPASS for '" << name
<< "' (already numeric)");
660 ++IpcacheStats
.numeric_hits
;
661 const DnsLookupDetails
details(NULL
, -1); // no error, no lookup
663 handler(addrs
, details
, handlerData
);
667 i
= ipcache_get(name
);
672 } else if (ipcacheExpiredEntry(i
)) {
673 /* hit, but expired -- bummer */
678 debugs(14, 4, "ipcache_nbgethostbyname: HIT for '" << name
<< "'");
680 if (i
->flags
.negcached
)
681 ++IpcacheStats
.negative_hits
;
685 i
->handler
= handler
;
687 i
->handlerData
= cbdataReference(handlerData
);
689 ipcacheCallback(i
, -1); // no lookup
694 debugs(14, 5, "ipcache_nbgethostbyname: MISS for '" << name
<< "'");
695 ++IpcacheStats
.misses
;
696 i
= ipcacheCreateEntry(name
);
697 i
->handler
= handler
;
698 i
->handlerData
= cbdataReference(handlerData
);
699 i
->request_time
= current_time
;
700 c
= new generic_cbdata(i
);
702 dnsSubmit(hashKeyStr(&i
->hash
), ipcacheHandleReply
, c
);
704 idnsALookup(hashKeyStr(&i
->hash
), ipcacheHandleReply
, c
);
708 /// \ingroup IPCacheInternal
710 ipcacheRegisterWithCacheManager(void)
712 Mgr::RegisterAction("ipcache",
713 "IP Cache Stats and Contents",
714 stat_ipcache_get
, 0, 1);
720 * Initialize the ipcache.
721 * Is called from mainInitialize() after disk initialization
722 * and prior to the reverse FQDNCache initialization
728 debugs(14, DBG_IMPORTANT
, "Initializing IP Cache...");
729 memset(&IpcacheStats
, '\0', sizeof(IpcacheStats
));
730 memset(&lru_list
, '\0', sizeof(lru_list
));
731 memset(&static_addrs
, '\0', sizeof(ipcache_addrs
));
733 static_addrs
.in_addrs
= static_cast<Ip::Address
*>(xcalloc(1, sizeof(Ip::Address
)));
734 static_addrs
.in_addrs
->SetEmpty(); // properly setup the Ip::Address!
735 static_addrs
.bad_mask
= (unsigned char *)xcalloc(1, sizeof(unsigned char));
736 ipcache_high
= (long) (((float) Config
.ipcache
.size
*
737 (float) Config
.ipcache
.high
) / (float) 100);
738 ipcache_low
= (long) (((float) Config
.ipcache
.size
*
739 (float) Config
.ipcache
.low
) / (float) 100);
740 n
= hashPrime(ipcache_high
/ 4);
741 ip_table
= hash_create((HASHCMP
*) strcmp
, n
, hash4
);
742 memDataInit(MEM_IPCACHE_ENTRY
, "ipcache_entry", sizeof(ipcache_entry
), 0);
744 ipcacheRegisterWithCacheManager();
750 * Is different from ipcache_nbgethostbyname in that it only checks
751 * if an entry exists in the cache and does not by default contact the DNS,
752 * unless this is requested, by setting the flags.
754 \param name Host name to resolve.
755 \param flags Default is NULL, set to IP_LOOKUP_IF_MISS
756 * to explicitly perform DNS lookups.
758 \retval NULL An error occured during lookup
759 \retval NULL No results available in cache and no lookup specified
760 \retval * Pointer to the ipcahce_addrs structure containing the lookup results
762 const ipcache_addrs
*
763 ipcache_gethostbyname(const char *name
, int flags
)
765 ipcache_entry
*i
= NULL
;
766 ipcache_addrs
*addrs
;
768 debugs(14, 3, "ipcache_gethostbyname: '" << name
<< "', flags=" << std::hex
<< flags
);
769 ++IpcacheStats
.requests
;
770 i
= ipcache_get(name
);
774 } else if (ipcacheExpiredEntry(i
)) {
777 } else if (i
->flags
.negcached
) {
778 ++IpcacheStats
.negative_hits
;
779 // ignore i->error_message: the caller just checks IP cache presence
783 i
->lastref
= squid_curtime
;
784 // ignore i->error_message: the caller just checks IP cache presence
788 /* no entry [any more] */
790 if ((addrs
= ipcacheCheckNumeric(name
))) {
791 ++IpcacheStats
.numeric_hits
;
795 ++IpcacheStats
.misses
;
797 if (flags
& IP_LOOKUP_IF_MISS
)
798 ipcache_nbgethostbyname(name
, NULL
, NULL
);
803 /// \ingroup IPCacheInternal
805 ipcacheStatPrint(ipcache_entry
* i
, StoreEntry
* sentry
)
808 char buf
[MAX_IPSTRLEN
];
811 debugs(14, DBG_CRITICAL
, HERE
<< "CRITICAL: sentry is NULL!");
816 debugs(14, DBG_CRITICAL
, HERE
<< "CRITICAL: ipcache_entry is NULL!");
817 storeAppendPrintf(sentry
, "CRITICAL ERROR\n");
821 int count
= i
->addrs
.count
;
823 storeAppendPrintf(sentry
, " %-32.32s %c%c %6d %6d %2d(%2d)",
824 hashKeyStr(&i
->hash
),
825 i
->flags
.fromhosts
? 'H' : ' ',
826 i
->flags
.negcached
? 'N' : ' ',
827 (int) (squid_curtime
- i
->lastref
),
828 (int) ((i
->flags
.fromhosts
? -1 : i
->expires
- squid_curtime
)),
829 (int) i
->addrs
.count
,
830 (int) i
->addrs
.badcount
);
833 * Negative-cached entries have no IPs listed. */
834 if (i
->flags
.negcached
) {
835 storeAppendPrintf(sentry
, "\n");
840 * Cached entries have IPs listed with a BNF of: ip-address '-' ('OK'|'BAD') */
841 for (k
= 0; k
< count
; ++k
) {
842 /* Display tidy-up: IPv6 are so big make the list vertical */
844 storeAppendPrintf(sentry
, " %45.45s-%3s\n",
845 i
->addrs
.in_addrs
[k
].NtoA(buf
,MAX_IPSTRLEN
),
846 i
->addrs
.bad_mask
[k
] ? "BAD" : "OK ");
848 storeAppendPrintf(sentry
, "%s %45.45s-%3s\n",
849 " ", /* blank-space indenting IP list */
850 i
->addrs
.in_addrs
[k
].NtoA(buf
,MAX_IPSTRLEN
),
851 i
->addrs
.bad_mask
[k
] ? "BAD" : "OK ");
856 \ingroup IPCacheInternal
858 * process objects list
861 stat_ipcache_get(StoreEntry
* sentry
)
864 assert(ip_table
!= NULL
);
865 storeAppendPrintf(sentry
, "IP Cache Statistics:\n");
866 storeAppendPrintf(sentry
, "IPcache Entries In Use: %d\n",
867 memInUse(MEM_IPCACHE_ENTRY
));
868 storeAppendPrintf(sentry
, "IPcache Entries Cached: %d\n",
870 storeAppendPrintf(sentry
, "IPcache Requests: %d\n",
871 IpcacheStats
.requests
);
872 storeAppendPrintf(sentry
, "IPcache Hits: %d\n",
874 storeAppendPrintf(sentry
, "IPcache Negative Hits: %d\n",
875 IpcacheStats
.negative_hits
);
876 storeAppendPrintf(sentry
, "IPcache Numeric Hits: %d\n",
877 IpcacheStats
.numeric_hits
);
878 storeAppendPrintf(sentry
, "IPcache Misses: %d\n",
879 IpcacheStats
.misses
);
880 storeAppendPrintf(sentry
, "IPcache Retrieved A: %d\n",
882 storeAppendPrintf(sentry
, "IPcache Retrieved AAAA: %d\n",
883 IpcacheStats
.rr_aaaa
);
884 storeAppendPrintf(sentry
, "IPcache Retrieved CNAME: %d\n",
885 IpcacheStats
.rr_cname
);
886 storeAppendPrintf(sentry
, "IPcache CNAME-Only Response: %d\n",
887 IpcacheStats
.cname_only
);
888 storeAppendPrintf(sentry
, "IPcache Invalid Request: %d\n",
889 IpcacheStats
.invalid
);
890 storeAppendPrintf(sentry
, "\n\n");
891 storeAppendPrintf(sentry
, "IP Cache Contents:\n\n");
892 storeAppendPrintf(sentry
, " %-31.31s %3s %6s %6s %4s\n",
899 for (m
= lru_list
.head
; m
; m
= m
->next
) {
900 assert( m
->next
!= m
);
901 ipcacheStatPrint((ipcache_entry
*)m
->data
, sentry
);
905 /// \ingroup IPCacheAPI
907 ipcacheInvalidate(const char *name
)
911 if ((i
= ipcache_get(name
)) == NULL
)
914 i
->expires
= squid_curtime
;
917 * NOTE, don't call ipcacheRelease here because we might be here due
918 * to a thread started from a callback.
922 /// \ingroup IPCacheAPI
924 ipcacheInvalidateNegative(const char *name
)
928 if ((i
= ipcache_get(name
)) == NULL
)
931 if (i
->flags
.negcached
)
932 i
->expires
= squid_curtime
;
935 * NOTE, don't call ipcacheRelease here because we might be here due
936 * to a thread started from a callback.
940 /// \ingroup IPCacheAPI
942 ipcacheCheckNumeric(const char *name
)
945 /* check if it's already a IP address in text form. */
947 /* it may be IPv6-wrapped */
948 if (name
[0] == '[') {
949 char *tmp
= xstrdup(&name
[1]);
950 tmp
[strlen(tmp
)-1] = '\0';
956 } else if (!(ip
= name
))
959 debugs(14, 4, "ipcacheCheckNumeric: HIT_BYPASS for '" << name
<< "' == " << ip
);
961 static_addrs
.count
= 1;
963 static_addrs
.cur
= 0;
965 static_addrs
.in_addrs
[0] = ip
;
967 static_addrs
.bad_mask
[0] = FALSE
;
969 static_addrs
.badcount
= 0;
971 return &static_addrs
;
974 /// \ingroup IPCacheInternal
976 ipcacheLockEntry(ipcache_entry
* i
)
978 if (i
->locks
++ == 0) {
979 dlinkDelete(&i
->lru
, &lru_list
);
980 dlinkAdd(i
, &i
->lru
, &lru_list
);
984 /// \ingroup IPCacheInternal
986 ipcacheUnlockEntry(ipcache_entry
* i
)
989 debugs(14, DBG_IMPORTANT
, "WARNING: ipcacheEntry unlocked with no lock! locks=" << i
->locks
);
995 if (ipcacheExpiredEntry(i
))
999 /// \ingroup IPCacheAPI
1001 ipcacheCycleAddr(const char *name
, ipcache_addrs
* ia
)
1008 if ((i
= ipcache_get(name
)) == NULL
)
1011 if (i
->flags
.negcached
)
1017 for (k
= 0; k
< ia
->count
; ++k
) {
1018 if (++ia
->cur
== ia
->count
)
1021 if (!ia
->bad_mask
[ia
->cur
])
1025 if (k
== ia
->count
) {
1026 /* All bad, reset to All good */
1027 debugs(14, 3, "ipcacheCycleAddr: Changing ALL " << name
<< " addrs from BAD to OK");
1029 for (k
= 0; k
< ia
->count
; ++k
)
1030 ia
->bad_mask
[k
] = 0;
1037 /* NP: zero-based so we increase the human-readable number of our position */
1038 debugs(14, 3, "ipcacheCycleAddr: " << name
<< " now at " << ia
->in_addrs
[ia
->cur
] << " (" << (ia
->cur
+1) << " of " << ia
->count
<< ")");
1044 \param name domain name to have an IP marked bad
1045 \param addr specific addres to be marked bad
1048 ipcacheMarkBadAddr(const char *name
, const Ip::Address
&addr
)
1054 /** Does nothing if the domain name does not exist. */
1055 if ((i
= ipcache_get(name
)) == NULL
)
1060 for (k
= 0; k
< (int) ia
->count
; ++k
) {
1061 if (addr
== ia
->in_addrs
[k
] )
1065 /** Does nothing if the IP does not exist for the doamin. */
1066 if (k
== (int) ia
->count
)
1069 /** Marks the given address as BAD */
1070 if (!ia
->bad_mask
[k
]) {
1071 ia
->bad_mask
[k
] = TRUE
;
1073 i
->expires
= min(squid_curtime
+ max((time_t)60, Config
.negativeDnsTtl
), i
->expires
);
1074 debugs(14, 2, "ipcacheMarkBadAddr: " << name
<< " " << addr
);
1077 /** then calls ipcacheCycleAddr() to advance the current pointer to the next OK address. */
1078 ipcacheCycleAddr(name
, ia
);
1081 /// \ingroup IPCacheAPI
1083 ipcacheMarkAllGood(const char *name
)
1089 if ((i
= ipcache_get(name
)) == NULL
)
1094 /* All bad, reset to All good */
1095 debugs(14, 3, "ipcacheMarkAllGood: Changing ALL " << name
<< " addrs to OK (" << ia
->badcount
<< "/" << ia
->count
<< " bad)");
1097 for (k
= 0; k
< ia
->count
; ++k
)
1098 ia
->bad_mask
[k
] = 0;
1103 /// \ingroup IPCacheAPI
1105 ipcacheMarkGoodAddr(const char *name
, const Ip::Address
&addr
)
1111 if ((i
= ipcache_get(name
)) == NULL
)
1116 for (k
= 0; k
< (int) ia
->count
; ++k
) {
1117 if (addr
== ia
->in_addrs
[k
])
1121 if (k
== (int) ia
->count
) /* not found */
1124 if (!ia
->bad_mask
[k
]) /* already OK */
1127 ia
->bad_mask
[k
] = FALSE
;
1131 debugs(14, 2, "ipcacheMarkGoodAddr: " << name
<< " " << addr
);
1134 /// \ingroup IPCacheInternal
1136 ipcacheFreeEntry(void *data
)
1138 ipcache_entry
*i
= (ipcache_entry
*)data
;
1139 safe_free(i
->addrs
.in_addrs
);
1140 safe_free(i
->addrs
.bad_mask
);
1141 safe_free(i
->hash
.key
);
1142 safe_free(i
->error_message
);
1143 memFree(i
, MEM_IPCACHE_ENTRY
);
1146 /// \ingroup IPCacheAPI
1148 ipcacheFreeMemory(void)
1150 hashFreeItems(ip_table
, ipcacheFreeEntry
);
1151 hashFreeMemory(ip_table
);
1158 * Recalculate IP cache size upon reconfigure.
1159 * Is called to clear the IPCache's data structures,
1160 * cancel all pending requests.
1163 ipcache_restart(void)
1165 ipcache_high
= (long) (((float) Config
.ipcache
.size
*
1166 (float) Config
.ipcache
.high
) / (float) 100);
1167 ipcache_low
= (long) (((float) Config
.ipcache
.size
*
1168 (float) Config
.ipcache
.low
) / (float) 100);
1169 purge_entries_fromhosts();
1175 * Adds a "static" entry from /etc/hosts
1177 \param name Hostname to be linked with IP
1178 \param ipaddr IP Address to be cached.
1181 \retval 1 IP address is invalid or other error.
1184 ipcacheAddEntryFromHosts(const char *name
, const char *ipaddr
)
1190 if (!(ip
= ipaddr
)) {
1191 if (strchr(ipaddr
, ':') && strspn(ipaddr
, "0123456789abcdefABCDEF:") == strlen(ipaddr
)) {
1192 debugs(14, 3, "ipcacheAddEntryFromHosts: Skipping IPv6 address '" << ipaddr
<< "'");
1194 debugs(14, DBG_IMPORTANT
, "ipcacheAddEntryFromHosts: Bad IP address '" << ipaddr
<< "'");
1200 if ((i
= ipcache_get(name
))) {
1201 if (1 == i
->flags
.fromhosts
) {
1202 ipcacheUnlockEntry(i
);
1203 } else if (i
->locks
> 0) {
1204 debugs(14, DBG_IMPORTANT
, "ipcacheAddEntryFromHosts: can't add static entry for locked name '" << name
<< "'");
1211 i
= ipcacheCreateEntry(name
);
1214 i
->addrs
.badcount
= 0;
1216 i
->addrs
.in_addrs
= static_cast<Ip::Address
*>(xcalloc(1, sizeof(Ip::Address
)));
1217 i
->addrs
.bad_mask
= (unsigned char *)xcalloc(1, sizeof(unsigned char));
1218 i
->addrs
.in_addrs
[0] = ip
;
1219 i
->addrs
.bad_mask
[0] = FALSE
;
1220 i
->flags
.fromhosts
= 1;
1222 ipcacheLockEntry(i
);
1230 * The function to return the ip cache statistics to via SNMP
1233 snmp_netIpFn(variable_list
* Var
, snint
* ErrP
)
1235 variable_list
*Answer
= NULL
;
1237 debugs(49, 5, "snmp_netIpFn: Processing request:" << snmpDebugOid(Var
->name
, Var
->name_length
, tmp
));
1238 *ErrP
= SNMP_ERR_NOERROR
;
1240 switch (Var
->name
[LEN_SQ_NET
+ 1]) {
1243 Answer
= snmp_var_new_integer(Var
->name
, Var
->name_length
,
1249 Answer
= snmp_var_new_integer(Var
->name
, Var
->name_length
,
1250 IpcacheStats
.requests
,
1255 Answer
= snmp_var_new_integer(Var
->name
, Var
->name_length
,
1261 Answer
= snmp_var_new_integer(Var
->name
, Var
->name_length
,
1267 Answer
= snmp_var_new_integer(Var
->name
, Var
->name_length
,
1268 IpcacheStats
.negative_hits
,
1273 Answer
= snmp_var_new_integer(Var
->name
, Var
->name_length
,
1274 IpcacheStats
.misses
,
1279 Answer
= snmp_var_new_integer(Var
->name
, Var
->name_length
,
1285 Answer
= snmp_var_new_integer(Var
->name
, Var
->name_length
,
1291 *ErrP
= SNMP_ERR_NOSUCHNAME
;
1292 snmp_var_free(Answer
);
1299 #endif /*SQUID_SNMP */