2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 14 IP Cache */
12 #include "CacheManager.h"
15 #include "dns/LookupDetails.h"
16 #include "dns/rfc3596.h"
18 #include "ip/Address.h"
21 #include "mgr/Registration.h"
22 #include "SquidConfig.h"
23 #include "SquidTime.h"
24 #include "StatCounters.h"
30 #include "snmp_core.h"
34 \defgroup IPCacheAPI IP Cache API
36 \section Introduction Introduction
38 * The IP cache is a built-in component of squid providing
39 * Hostname to IP-Number translation functionality and managing
40 * the involved data-structures. Efficiency concerns require
41 * mechanisms that allow non-blocking access to these mappings.
42 * The IP cache usually doesn't block on a request except for
43 * special cases where this is desired (see below).
45 \todo IP Cache should have its own API *.h header file.
49 \defgroup IPCacheInternal IP Cache Internals
51 \todo when IP cache is provided as a class. These sub-groups will be obsolete
52 * for now they are used to seperate the public and private functions.
53 * with the private ones all being in IPCachInternal and public in IPCacheAPI
55 \section InternalOperation Internal Operation
57 * Internally, the execution flow is as follows: On a miss,
58 * ipcache_getnbhostbyname checks whether a request for
59 * this name is already pending, and if positive, it creates
60 * a new entry using ipcacheAddNew with the IP_PENDING
61 * flag set . Then it calls ipcacheAddPending to add a
62 * request to the queue together with data and handler. Else,
63 * ipcache_dnsDispatch() is called to directly create a
64 * DNS query or to ipcacheEnqueue() if all no DNS port
65 * is free. ipcache_call_pending() is called regularly
66 * to walk down the pending list and call handlers. LRU clean-up
67 * is performed through ipcache_purgelru() according to
68 * the ipcache_high threshold.
74 * The data structure used for storing name-address mappings
75 * is a small hashtable (static hash_table *ip_table),
76 * where structures of type ipcache_entry whose most
77 * interesting members are:
82 hash_link hash
; /* must be first */
90 struct timeval request_time
;
98 int age() const; ///< time passed since request_time or -1 if unknown
101 /// \ingroup IPCacheInternal
102 static struct _ipcache_stats
{
116 /// \ingroup IPCacheInternal
117 static dlink_list lru_list
;
120 static void stat_ipcache_get(StoreEntry
*);
122 static FREE ipcacheFreeEntry
;
123 static IDNSCB ipcacheHandleReply
;
124 static int ipcacheExpiredEntry(ipcache_entry
*);
125 static int ipcacheParse(ipcache_entry
*, const rfc1035_rr
*, int, const char *error
);
126 static ipcache_entry
*ipcache_get(const char *);
127 static void ipcacheLockEntry(ipcache_entry
*);
128 static void ipcacheStatPrint(ipcache_entry
*, StoreEntry
*);
129 static void ipcacheUnlockEntry(ipcache_entry
*);
130 static void ipcacheRelease(ipcache_entry
*, bool dofree
= true);
132 /// \ingroup IPCacheInternal
133 static ipcache_addrs static_addrs
;
134 /// \ingroup IPCacheInternal
135 static hash_table
*ip_table
= NULL
;
137 /// \ingroup IPCacheInternal
138 static long ipcache_low
= 180;
139 /// \ingroup IPCacheInternal
140 static long ipcache_high
= 200;
142 #if LIBRESOLV_DNS_TTL_HACK
143 extern int _dns_ttl_
;
146 /// \ingroup IPCacheInternal
147 inline int ipcacheCount() { return ip_table
? ip_table
->count
: 0; }
150 ipcache_entry::age() const
152 return request_time
.tv_sec
? tvSubMsec(request_time
, current_time
) : -1;
156 \ingroup IPCacheInternal
158 * removes the given ipcache entry
161 ipcacheRelease(ipcache_entry
* i
, bool dofree
)
164 debugs(14, DBG_CRITICAL
, "ipcacheRelease: Releasing entry with i=<NULL>");
168 if (!i
|| !i
->hash
.key
) {
169 debugs(14, DBG_CRITICAL
, "ipcacheRelease: Releasing entry without hash link!");
173 debugs(14, 3, "ipcacheRelease: Releasing entry for '" << (const char *) i
->hash
.key
<< "'");
175 hash_remove_link(ip_table
, (hash_link
*) i
);
176 dlinkDelete(&i
->lru
, &lru_list
);
181 /// \ingroup IPCacheInternal
182 static ipcache_entry
*
183 ipcache_get(const char *name
)
185 if (ip_table
!= NULL
)
186 return (ipcache_entry
*) hash_lookup(ip_table
, name
);
191 /// \ingroup IPCacheInternal
193 ipcacheExpiredEntry(ipcache_entry
* i
)
195 /* all static entries are locked, so this takes care of them too */
200 if (i
->addrs
.count
== 0)
201 if (0 == i
->flags
.negcached
)
204 if (i
->expires
> squid_curtime
)
210 /// \ingroup IPCacheAPI
212 ipcache_purgelru(void *)
215 dlink_node
*prev
= NULL
;
218 eventAdd("ipcache_purgelru", ipcache_purgelru
, NULL
, 10.0, 1);
220 for (m
= lru_list
.tail
; m
; m
= prev
) {
221 if (ipcacheCount() < ipcache_low
)
226 i
= (ipcache_entry
*)m
->data
;
236 debugs(14, 9, "ipcache_purgelru: removed " << removed
<< " entries");
240 \ingroup IPCacheInternal
242 * purges entries added from /etc/hosts (or whatever).
245 purge_entries_fromhosts(void)
247 dlink_node
*m
= lru_list
.head
;
248 ipcache_entry
*i
= NULL
, *t
;
251 if (i
!= NULL
) { /* need to delay deletion */
252 ipcacheRelease(i
); /* we just override locks */
256 t
= (ipcache_entry
*)m
->data
;
258 if (t
->flags
.fromhosts
)
269 \ingroup IPCacheInternal
271 * create blank ipcache_entry
273 static ipcache_entry
*
274 ipcacheCreateEntry(const char *name
)
276 static ipcache_entry
*i
;
277 i
= (ipcache_entry
*)memAllocate(MEM_IPCACHE_ENTRY
);
278 i
->hash
.key
= xstrdup(name
);
279 Tolower(static_cast<char*>(i
->hash
.key
));
280 i
->expires
= squid_curtime
+ Config
.negativeDnsTtl
;
284 /// \ingroup IPCacheInternal
286 ipcacheAddEntry(ipcache_entry
* i
)
288 hash_link
*e
= (hash_link
*)hash_lookup(ip_table
, i
->hash
.key
);
291 /* avoid colission */
292 ipcache_entry
*q
= (ipcache_entry
*) e
;
296 hash_join(ip_table
, &i
->hash
);
297 dlinkAdd(i
, &i
->lru
, &lru_list
);
298 i
->lastref
= squid_curtime
;
302 \ingroup IPCacheInternal
304 * walks down the pending list, calling handlers
307 ipcacheCallback(ipcache_entry
*i
, int wait
)
309 IPH
*callback
= i
->handler
;
311 i
->lastref
= squid_curtime
;
318 callback
= i
->handler
;
322 if (cbdataReferenceValidDone(i
->handlerData
, &cbdata
)) {
323 const Dns::LookupDetails
details(i
->error_message
, wait
);
324 callback((i
->addrs
.count
? &i
->addrs
: NULL
), details
, cbdata
);
327 ipcacheUnlockEntry(i
);
330 /// \ingroup IPCacheAPI
332 ipcacheParse(ipcache_entry
*i
, const rfc1035_rr
* answers
, int nr
, const char *error_message
)
338 const char *name
= (const char *)i
->hash
.key
;
341 i
->expires
= squid_curtime
+ Config
.negativeDnsTtl
;
342 i
->flags
.negcached
= true;
343 safe_free(i
->addrs
.in_addrs
);
344 assert(i
->addrs
.in_addrs
== NULL
);
345 safe_free(i
->addrs
.bad_mask
);
346 assert(i
->addrs
.bad_mask
== NULL
);
347 safe_free(i
->error_message
);
348 assert(i
->error_message
== NULL
);
352 debugs(14, 3, "ipcacheParse: Lookup failed '" << error_message
<< "' for '" << (const char *)i
->hash
.key
<< "'");
353 i
->error_message
= xstrdup(error_message
);
358 debugs(14, 3, "ipcacheParse: No DNS records in response to '" << name
<< "'");
359 i
->error_message
= xstrdup("No DNS records");
363 debugs(14, 3, "ipcacheParse: " << nr
<< " answers for '" << name
<< "'");
366 for (k
= 0; k
< nr
; ++k
) {
368 if (Ip::EnableIpv6
&& answers
[k
].type
== RFC1035_TYPE_AAAA
) {
369 if (answers
[k
].rdlength
!= sizeof(struct in6_addr
)) {
370 debugs(14, DBG_IMPORTANT
, "ipcacheParse: Invalid IPv6 address in response to '" << name
<< "'");
374 ++IpcacheStats
.rr_aaaa
;
378 if (answers
[k
].type
== RFC1035_TYPE_A
) {
379 if (answers
[k
].rdlength
!= sizeof(struct in_addr
)) {
380 debugs(14, DBG_IMPORTANT
, "ipcacheParse: Invalid IPv4 address in response to '" << name
<< "'");
388 /* With A and AAAA, the CNAME does not necessarily come with additional records to use. */
389 if (answers
[k
].type
== RFC1035_TYPE_CNAME
) {
391 ++IpcacheStats
.rr_cname
;
395 // otherwise its an unknown RR. debug at level 9 since we usually want to ignore these and they are common.
396 debugs(14, 9, HERE
<< "Unknown RR type received: type=" << answers
[k
].type
<< " starting at " << &(answers
[k
]) );
399 debugs(14, DBG_IMPORTANT
, "ipcacheParse: No Address records in response to '" << name
<< "'");
400 i
->error_message
= xstrdup("No Address records");
402 ++IpcacheStats
.cname_only
;
406 i
->addrs
.in_addrs
= static_cast<Ip::Address
*>(xcalloc(na
, sizeof(Ip::Address
)));
407 for (int l
= 0; l
< na
; ++l
)
408 i
->addrs
.in_addrs
[l
].setEmpty(); // perform same init actions as constructor would.
409 i
->addrs
.bad_mask
= (unsigned char *)xcalloc(na
, sizeof(unsigned char));
411 for (j
= 0, k
= 0; k
< nr
; ++k
) {
413 if (answers
[k
].type
== RFC1035_TYPE_A
) {
414 if (answers
[k
].rdlength
!= sizeof(struct in_addr
))
418 memcpy(&temp
, answers
[k
].rdata
, sizeof(struct in_addr
));
419 i
->addrs
.in_addrs
[j
] = temp
;
421 debugs(14, 3, "ipcacheParse: " << name
<< " #" << j
<< " " << i
->addrs
.in_addrs
[j
]);
424 } else if (Ip::EnableIpv6
&& answers
[k
].type
== RFC1035_TYPE_AAAA
) {
425 if (answers
[k
].rdlength
!= sizeof(struct in6_addr
))
428 struct in6_addr temp
;
429 memcpy(&temp
, answers
[k
].rdata
, sizeof(struct in6_addr
));
430 i
->addrs
.in_addrs
[j
] = temp
;
432 debugs(14, 3, "ipcacheParse: " << name
<< " #" << j
<< " " << i
->addrs
.in_addrs
[j
] );
435 if (ttl
== 0 || (int) answers
[k
].ttl
< ttl
)
436 ttl
= answers
[k
].ttl
;
442 i
->addrs
.count
= (unsigned char) na
;
444 i
->addrs
.count
= 255;
446 if (ttl
> Config
.positiveDnsTtl
)
447 ttl
= Config
.positiveDnsTtl
;
449 if (ttl
< Config
.negativeDnsTtl
)
450 ttl
= Config
.negativeDnsTtl
;
452 i
->expires
= squid_curtime
+ ttl
;
454 i
->flags
.negcached
= false;
456 return i
->addrs
.count
;
459 /// \ingroup IPCacheInternal
461 ipcacheHandleReply(void *data
, const rfc1035_rr
* answers
, int na
, const char *error_message
)
464 static_cast<generic_cbdata
*>(data
)->unwrap(&i
);
465 ++IpcacheStats
.replies
;
466 const int age
= i
->age();
467 statCounter
.dns
.svcTime
.count(age
);
469 int done
= ipcacheParse(i
, answers
, na
, error_message
);
471 /* If we have not produced either IPs or Error immediately, wait for recursion to finish. */
472 if (done
!= 0 || error_message
!= NULL
) {
474 ipcacheCallback(i
, age
);
481 \param name Host to resolve.
482 \param handler Pointer to the function to be called when the reply
483 * from the IP cache (or the DNS if the IP cache misses)
484 \param handlerData Information that is passed to the handler and does not affect the IP cache.
486 * XXX: on hits and some errors, the handler is called immediately instead
487 * of scheduling an async call. This reentrant behavior means that the
488 * user job must be extra careful after calling ipcache_nbgethostbyname,
489 * especially if the handler destroys the job. Moreover, the job has
490 * no way of knowing whether the reentrant call happened.
491 * Comm::Connection setup usually protects the job by scheduling an async call,
492 * but some user code calls ipcache_nbgethostbyname directly.
495 ipcache_nbgethostbyname(const char *name
, IPH
* handler
, void *handlerData
)
497 ipcache_entry
*i
= NULL
;
498 const ipcache_addrs
*addrs
= NULL
;
500 debugs(14, 4, "ipcache_nbgethostbyname: Name '" << name
<< "'.");
501 ++IpcacheStats
.requests
;
503 if (name
== NULL
|| name
[0] == '\0') {
504 debugs(14, 4, "ipcache_nbgethostbyname: Invalid name!");
505 ++IpcacheStats
.invalid
;
506 const Dns::LookupDetails
details("Invalid hostname", -1); // error, no lookup
508 handler(NULL
, details
, handlerData
);
512 if ((addrs
= ipcacheCheckNumeric(name
))) {
513 debugs(14, 4, "ipcache_nbgethostbyname: BYPASS for '" << name
<< "' (already numeric)");
514 ++IpcacheStats
.numeric_hits
;
515 const Dns::LookupDetails details
; // no error, no lookup
517 handler(addrs
, details
, handlerData
);
521 i
= ipcache_get(name
);
526 } else if (ipcacheExpiredEntry(i
)) {
527 /* hit, but expired -- bummer */
532 debugs(14, 4, "ipcache_nbgethostbyname: HIT for '" << name
<< "'");
534 if (i
->flags
.negcached
)
535 ++IpcacheStats
.negative_hits
;
539 i
->handler
= handler
;
541 i
->handlerData
= cbdataReference(handlerData
);
543 ipcacheCallback(i
, -1); // no lookup
548 debugs(14, 5, "ipcache_nbgethostbyname: MISS for '" << name
<< "'");
549 ++IpcacheStats
.misses
;
550 i
= ipcacheCreateEntry(name
);
551 i
->handler
= handler
;
552 i
->handlerData
= cbdataReference(handlerData
);
553 i
->request_time
= current_time
;
554 c
= new generic_cbdata(i
);
555 idnsALookup(hashKeyStr(&i
->hash
), ipcacheHandleReply
, c
);
558 /// \ingroup IPCacheInternal
560 ipcacheRegisterWithCacheManager(void)
562 Mgr::RegisterAction("ipcache",
563 "IP Cache Stats and Contents",
564 stat_ipcache_get
, 0, 1);
570 * Initialize the ipcache.
571 * Is called from mainInitialize() after disk initialization
572 * and prior to the reverse FQDNCache initialization
578 debugs(14, DBG_IMPORTANT
, "Initializing IP Cache...");
579 memset(&IpcacheStats
, '\0', sizeof(IpcacheStats
));
580 memset(&lru_list
, '\0', sizeof(lru_list
));
581 memset(&static_addrs
, '\0', sizeof(ipcache_addrs
));
583 static_addrs
.in_addrs
= static_cast<Ip::Address
*>(xcalloc(1, sizeof(Ip::Address
)));
584 static_addrs
.in_addrs
->setEmpty(); // properly setup the Ip::Address!
585 static_addrs
.bad_mask
= (unsigned char *)xcalloc(1, sizeof(unsigned char));
586 ipcache_high
= (long) (((float) Config
.ipcache
.size
*
587 (float) Config
.ipcache
.high
) / (float) 100);
588 ipcache_low
= (long) (((float) Config
.ipcache
.size
*
589 (float) Config
.ipcache
.low
) / (float) 100);
590 n
= hashPrime(ipcache_high
/ 4);
591 ip_table
= hash_create((HASHCMP
*) strcmp
, n
, hash4
);
592 memDataInit(MEM_IPCACHE_ENTRY
, "ipcache_entry", sizeof(ipcache_entry
), 0);
594 ipcacheRegisterWithCacheManager();
600 * Is different from ipcache_nbgethostbyname in that it only checks
601 * if an entry exists in the cache and does not by default contact the DNS,
602 * unless this is requested, by setting the flags.
604 \param name Host name to resolve.
605 \param flags Default is NULL, set to IP_LOOKUP_IF_MISS
606 * to explicitly perform DNS lookups.
608 \retval NULL An error occured during lookup
609 \retval NULL No results available in cache and no lookup specified
610 \retval * Pointer to the ipcahce_addrs structure containing the lookup results
612 const ipcache_addrs
*
613 ipcache_gethostbyname(const char *name
, int flags
)
615 ipcache_entry
*i
= NULL
;
616 ipcache_addrs
*addrs
;
618 debugs(14, 3, "ipcache_gethostbyname: '" << name
<< "', flags=" << std::hex
<< flags
);
619 ++IpcacheStats
.requests
;
620 i
= ipcache_get(name
);
624 } else if (ipcacheExpiredEntry(i
)) {
627 } else if (i
->flags
.negcached
) {
628 ++IpcacheStats
.negative_hits
;
629 // ignore i->error_message: the caller just checks IP cache presence
633 i
->lastref
= squid_curtime
;
634 // ignore i->error_message: the caller just checks IP cache presence
638 /* no entry [any more] */
640 if ((addrs
= ipcacheCheckNumeric(name
))) {
641 ++IpcacheStats
.numeric_hits
;
645 ++IpcacheStats
.misses
;
647 if (flags
& IP_LOOKUP_IF_MISS
)
648 ipcache_nbgethostbyname(name
, NULL
, NULL
);
653 /// \ingroup IPCacheInternal
655 ipcacheStatPrint(ipcache_entry
* i
, StoreEntry
* sentry
)
658 char buf
[MAX_IPSTRLEN
];
661 debugs(14, DBG_CRITICAL
, HERE
<< "CRITICAL: sentry is NULL!");
666 debugs(14, DBG_CRITICAL
, HERE
<< "CRITICAL: ipcache_entry is NULL!");
667 storeAppendPrintf(sentry
, "CRITICAL ERROR\n");
671 int count
= i
->addrs
.count
;
673 storeAppendPrintf(sentry
, " %-32.32s %c%c %6d %6d %2d(%2d)",
674 hashKeyStr(&i
->hash
),
675 i
->flags
.fromhosts
? 'H' : ' ',
676 i
->flags
.negcached
? 'N' : ' ',
677 (int) (squid_curtime
- i
->lastref
),
678 (int) ((i
->flags
.fromhosts
? -1 : i
->expires
- squid_curtime
)),
679 (int) i
->addrs
.count
,
680 (int) i
->addrs
.badcount
);
683 * Negative-cached entries have no IPs listed. */
684 if (i
->flags
.negcached
) {
685 storeAppendPrintf(sentry
, "\n");
690 * Cached entries have IPs listed with a BNF of: ip-address '-' ('OK'|'BAD') */
691 for (k
= 0; k
< count
; ++k
) {
692 /* Display tidy-up: IPv6 are so big make the list vertical */
694 storeAppendPrintf(sentry
, " %45.45s-%3s\n",
695 i
->addrs
.in_addrs
[k
].toStr(buf
,MAX_IPSTRLEN
),
696 i
->addrs
.bad_mask
[k
] ? "BAD" : "OK ");
698 storeAppendPrintf(sentry
, "%s %45.45s-%3s\n",
699 " ", /* blank-space indenting IP list */
700 i
->addrs
.in_addrs
[k
].toStr(buf
,MAX_IPSTRLEN
),
701 i
->addrs
.bad_mask
[k
] ? "BAD" : "OK ");
706 \ingroup IPCacheInternal
708 * process objects list
711 stat_ipcache_get(StoreEntry
* sentry
)
714 assert(ip_table
!= NULL
);
715 storeAppendPrintf(sentry
, "IP Cache Statistics:\n");
716 storeAppendPrintf(sentry
, "IPcache Entries In Use: %d\n",
717 memInUse(MEM_IPCACHE_ENTRY
));
718 storeAppendPrintf(sentry
, "IPcache Entries Cached: %d\n",
720 storeAppendPrintf(sentry
, "IPcache Requests: %d\n",
721 IpcacheStats
.requests
);
722 storeAppendPrintf(sentry
, "IPcache Hits: %d\n",
724 storeAppendPrintf(sentry
, "IPcache Negative Hits: %d\n",
725 IpcacheStats
.negative_hits
);
726 storeAppendPrintf(sentry
, "IPcache Numeric Hits: %d\n",
727 IpcacheStats
.numeric_hits
);
728 storeAppendPrintf(sentry
, "IPcache Misses: %d\n",
729 IpcacheStats
.misses
);
730 storeAppendPrintf(sentry
, "IPcache Retrieved A: %d\n",
732 storeAppendPrintf(sentry
, "IPcache Retrieved AAAA: %d\n",
733 IpcacheStats
.rr_aaaa
);
734 storeAppendPrintf(sentry
, "IPcache Retrieved CNAME: %d\n",
735 IpcacheStats
.rr_cname
);
736 storeAppendPrintf(sentry
, "IPcache CNAME-Only Response: %d\n",
737 IpcacheStats
.cname_only
);
738 storeAppendPrintf(sentry
, "IPcache Invalid Request: %d\n",
739 IpcacheStats
.invalid
);
740 storeAppendPrintf(sentry
, "\n\n");
741 storeAppendPrintf(sentry
, "IP Cache Contents:\n\n");
742 storeAppendPrintf(sentry
, " %-31.31s %3s %6s %6s %4s\n",
749 for (m
= lru_list
.head
; m
; m
= m
->next
) {
750 assert( m
->next
!= m
);
751 ipcacheStatPrint((ipcache_entry
*)m
->data
, sentry
);
755 /// \ingroup IPCacheAPI
757 ipcacheInvalidate(const char *name
)
761 if ((i
= ipcache_get(name
)) == NULL
)
764 i
->expires
= squid_curtime
;
767 * NOTE, don't call ipcacheRelease here because we might be here due
768 * to a thread started from a callback.
772 /// \ingroup IPCacheAPI
774 ipcacheInvalidateNegative(const char *name
)
778 if ((i
= ipcache_get(name
)) == NULL
)
781 if (i
->flags
.negcached
)
782 i
->expires
= squid_curtime
;
785 * NOTE, don't call ipcacheRelease here because we might be here due
786 * to a thread started from a callback.
790 /// \ingroup IPCacheAPI
792 ipcacheCheckNumeric(const char *name
)
795 /* check if it's already a IP address in text form. */
797 /* it may be IPv6-wrapped */
798 if (name
[0] == '[') {
799 char *tmp
= xstrdup(&name
[1]);
800 tmp
[strlen(tmp
)-1] = '\0';
806 } else if (!(ip
= name
))
809 debugs(14, 4, "ipcacheCheckNumeric: HIT_BYPASS for '" << name
<< "' == " << ip
);
811 static_addrs
.count
= 1;
813 static_addrs
.cur
= 0;
815 static_addrs
.in_addrs
[0] = ip
;
817 static_addrs
.bad_mask
[0] = FALSE
;
819 static_addrs
.badcount
= 0;
821 return &static_addrs
;
824 /// \ingroup IPCacheInternal
826 ipcacheLockEntry(ipcache_entry
* i
)
828 if (i
->locks
++ == 0) {
829 dlinkDelete(&i
->lru
, &lru_list
);
830 dlinkAdd(i
, &i
->lru
, &lru_list
);
834 /// \ingroup IPCacheInternal
836 ipcacheUnlockEntry(ipcache_entry
* i
)
839 debugs(14, DBG_IMPORTANT
, "WARNING: ipcacheEntry unlocked with no lock! locks=" << i
->locks
);
845 if (ipcacheExpiredEntry(i
))
849 /// \ingroup IPCacheAPI
851 ipcacheCycleAddr(const char *name
, ipcache_addrs
* ia
)
858 if ((i
= ipcache_get(name
)) == NULL
)
861 if (i
->flags
.negcached
)
867 for (k
= 0; k
< ia
->count
; ++k
) {
868 if (++ia
->cur
== ia
->count
)
871 if (!ia
->bad_mask
[ia
->cur
])
875 if (k
== ia
->count
) {
876 /* All bad, reset to All good */
877 debugs(14, 3, "ipcacheCycleAddr: Changing ALL " << name
<< " addrs from BAD to OK");
879 for (k
= 0; k
< ia
->count
; ++k
)
887 /* NP: zero-based so we increase the human-readable number of our position */
888 debugs(14, 3, "ipcacheCycleAddr: " << name
<< " now at " << ia
->in_addrs
[ia
->cur
] << " (" << (ia
->cur
+1) << " of " << ia
->count
<< ")");
894 \param name domain name to have an IP marked bad
895 \param addr specific addres to be marked bad
898 ipcacheMarkBadAddr(const char *name
, const Ip::Address
&addr
)
904 /** Does nothing if the domain name does not exist. */
905 if ((i
= ipcache_get(name
)) == NULL
)
910 for (k
= 0; k
< (int) ia
->count
; ++k
) {
911 if (addr
== ia
->in_addrs
[k
] )
915 /** Does nothing if the IP does not exist for the doamin. */
916 if (k
== (int) ia
->count
)
919 /** Marks the given address as BAD */
920 if (!ia
->bad_mask
[k
]) {
921 ia
->bad_mask
[k
] = TRUE
;
923 debugs(14, 2, "ipcacheMarkBadAddr: " << name
<< " " << addr
);
926 /** then calls ipcacheCycleAddr() to advance the current pointer to the next OK address. */
927 ipcacheCycleAddr(name
, ia
);
930 /// \ingroup IPCacheAPI
932 ipcacheMarkAllGood(const char *name
)
938 if ((i
= ipcache_get(name
)) == NULL
)
943 /* All bad, reset to All good */
944 debugs(14, 3, "ipcacheMarkAllGood: Changing ALL " << name
<< " addrs to OK (" << ia
->badcount
<< "/" << ia
->count
<< " bad)");
946 for (k
= 0; k
< ia
->count
; ++k
)
952 /// \ingroup IPCacheAPI
954 ipcacheMarkGoodAddr(const char *name
, const Ip::Address
&addr
)
960 if ((i
= ipcache_get(name
)) == NULL
)
965 for (k
= 0; k
< (int) ia
->count
; ++k
) {
966 if (addr
== ia
->in_addrs
[k
])
970 if (k
== (int) ia
->count
) /* not found */
973 if (!ia
->bad_mask
[k
]) /* already OK */
976 ia
->bad_mask
[k
] = FALSE
;
980 debugs(14, 2, "ipcacheMarkGoodAddr: " << name
<< " " << addr
);
983 /// \ingroup IPCacheInternal
985 ipcacheFreeEntry(void *data
)
987 ipcache_entry
*i
= (ipcache_entry
*)data
;
988 safe_free(i
->addrs
.in_addrs
);
989 safe_free(i
->addrs
.bad_mask
);
990 safe_free(i
->hash
.key
);
991 safe_free(i
->error_message
);
992 memFree(i
, MEM_IPCACHE_ENTRY
);
995 /// \ingroup IPCacheAPI
997 ipcacheFreeMemory(void)
999 hashFreeItems(ip_table
, ipcacheFreeEntry
);
1000 hashFreeMemory(ip_table
);
1007 * Recalculate IP cache size upon reconfigure.
1008 * Is called to clear the IPCache's data structures,
1009 * cancel all pending requests.
1012 ipcache_restart(void)
1014 ipcache_high
= (long) (((float) Config
.ipcache
.size
*
1015 (float) Config
.ipcache
.high
) / (float) 100);
1016 ipcache_low
= (long) (((float) Config
.ipcache
.size
*
1017 (float) Config
.ipcache
.low
) / (float) 100);
1018 purge_entries_fromhosts();
1024 * Adds a "static" entry from /etc/hosts
1026 \param name Hostname to be linked with IP
1027 \param ipaddr IP Address to be cached.
1030 \retval 1 IP address is invalid or other error.
1033 ipcacheAddEntryFromHosts(const char *name
, const char *ipaddr
)
1039 if (!(ip
= ipaddr
)) {
1040 if (strchr(ipaddr
, ':') && strspn(ipaddr
, "0123456789abcdefABCDEF:") == strlen(ipaddr
)) {
1041 debugs(14, 3, "ipcacheAddEntryFromHosts: Skipping IPv6 address '" << ipaddr
<< "'");
1043 debugs(14, DBG_IMPORTANT
, "ipcacheAddEntryFromHosts: Bad IP address '" << ipaddr
<< "'");
1049 if ((i
= ipcache_get(name
))) {
1050 if (1 == i
->flags
.fromhosts
) {
1051 ipcacheUnlockEntry(i
);
1052 } else if (i
->locks
> 0) {
1053 debugs(14, DBG_IMPORTANT
, "ipcacheAddEntryFromHosts: can't add static entry for locked name '" << name
<< "'");
1060 i
= ipcacheCreateEntry(name
);
1063 i
->addrs
.badcount
= 0;
1065 i
->addrs
.in_addrs
= static_cast<Ip::Address
*>(xcalloc(1, sizeof(Ip::Address
)));
1066 i
->addrs
.bad_mask
= (unsigned char *)xcalloc(1, sizeof(unsigned char));
1067 i
->addrs
.in_addrs
[0] = ip
;
1068 i
->addrs
.bad_mask
[0] = FALSE
;
1069 i
->flags
.fromhosts
= true;
1071 ipcacheLockEntry(i
);
1079 * The function to return the ip cache statistics to via SNMP
1082 snmp_netIpFn(variable_list
* Var
, snint
* ErrP
)
1084 variable_list
*Answer
= NULL
;
1086 debugs(49, 5, "snmp_netIpFn: Processing request:" << snmpDebugOid(Var
->name
, Var
->name_length
, tmp
));
1087 *ErrP
= SNMP_ERR_NOERROR
;
1089 switch (Var
->name
[LEN_SQ_NET
+ 1]) {
1092 Answer
= snmp_var_new_integer(Var
->name
, Var
->name_length
,
1098 Answer
= snmp_var_new_integer(Var
->name
, Var
->name_length
,
1099 IpcacheStats
.requests
,
1104 Answer
= snmp_var_new_integer(Var
->name
, Var
->name_length
,
1110 Answer
= snmp_var_new_integer(Var
->name
, Var
->name_length
,
1116 Answer
= snmp_var_new_integer(Var
->name
, Var
->name_length
,
1117 IpcacheStats
.negative_hits
,
1122 Answer
= snmp_var_new_integer(Var
->name
, Var
->name_length
,
1123 IpcacheStats
.misses
,
1128 Answer
= snmp_var_new_integer(Var
->name
, Var
->name_length
,
1134 Answer
= snmp_var_new_integer(Var
->name
, Var
->name_length
,
1140 *ErrP
= SNMP_ERR_NOSUCHNAME
;
1141 snmp_var_free(Answer
);
1148 #endif /*SQUID_SNMP */