--------------------- PatchSet 1003 Date: 2000/12/30 09:15:19 Author: hno Branch: etc_hosts Tag: (none) Log: date: 2000/12/29 12:53:51; author: kinkie; state: Exp; lines: +98 -37 Added handling of /etc/hosts file (forward and reverse lookups). date: 2000/12/27 16:21:01; author: kinkie; state: Exp; lines: +77 -16 Added code to read and parse /etc/hosts upon start and reconfigure. This is especially useful when using the internal DNS code which blissfully ignores /etc/hosts. Members: src/fqdncache.c:1.5->1.5.8.1 Index: squid/src/fqdncache.c =================================================================== RCS file: /cvsroot/squid-sf//squid/src/fqdncache.c,v retrieving revision 1.5 retrieving revision 1.5.8.1 diff -u -r1.5 -r1.5.8.1 --- squid/src/fqdncache.c 12 Dec 2000 23:21:19 -0000 1.5 +++ squid/src/fqdncache.c 30 Dec 2000 09:15:19 -0000 1.5.8.1 @@ -1,6 +1,6 @@ /* - * $Id: fqdncache.c,v 1.5 2000/12/12 23:21:19 adri Exp $ + * $Id: fqdncache.c,v 1.5.8.1 2000/12/30 09:15:19 hno Exp $ * * DEBUG: section 35 FQDN Cache * AUTHOR: Harvest Derived @@ -54,6 +54,7 @@ unsigned short locks; struct { unsigned int negcached:1; + unsigned int fromhosts:1; } flags; }; @@ -125,11 +126,12 @@ static int fqdncacheExpiredEntry(const fqdncache_entry * f) { - if (f->locks != 0) - return 0; - if (f->expires > squid_curtime) - return 0; - return 1; + /* all static entries are locked, so this takes care of them too */ + if (f->locks != 0) + return 0; + if (f->expires > squid_curtime) + return 0; + return 1; } void @@ -153,6 +155,26 @@ debug(35, 9) ("fqdncache_purgelru: removed %d entries\n", removed); } +/* purges entries added from /etc/hosts (or whatever). */ +static void purge_entries_fromhosts (void) { + dlink_node *m=lru_list.head; + fqdncache_entry *i=NULL, *t; + + while (m) { + if (i!=NULL) { /* need to delay deletion */ + fqdncacheRelease(i); /* we just override locks */ + i=NULL; + } + t=(fqdncache_entry *)m->data; + if (t->flags.fromhosts) + i=t; + m=m->next; + } + if (i!=NULL) + fqdncacheRelease(i); +} + + /* create blank fqdncache_entry */ static fqdncache_entry * fqdncacheCreateEntry(const char *name) @@ -436,38 +458,41 @@ void fqdnStats(StoreEntry * sentry) { - fqdncache_entry *f = NULL; - int k; - int ttl; - if (fqdn_table == NULL) - return; - storeAppendPrintf(sentry, "FQDN Cache Statistics:\n"); - storeAppendPrintf(sentry, "FQDNcache Entries: %d\n", - memInUse(MEM_FQDNCACHE_ENTRY)); - storeAppendPrintf(sentry, "FQDNcache Requests: %d\n", - FqdncacheStats.requests); - storeAppendPrintf(sentry, "FQDNcache Hits: %d\n", - FqdncacheStats.hits); - storeAppendPrintf(sentry, "FQDNcache Negative Hits: %d\n", - FqdncacheStats.negative_hits); - storeAppendPrintf(sentry, "FQDNcache Misses: %d\n", - FqdncacheStats.misses); - storeAppendPrintf(sentry, "Blocking calls to gethostbyaddr(): %d\n", - FqdncacheStats.ghba_calls); - storeAppendPrintf(sentry, "FQDN Cache Contents:\n\n"); - - hash_first(fqdn_table); - while ((f = (fqdncache_entry *) hash_next(fqdn_table))) { - ttl = (f->expires - squid_curtime); - storeAppendPrintf(sentry, " %-32.32s %c %6d %d", - hashKeyStr(&f->hash), - f->flags.negcached ? 'N' : ' ', - ttl, - (int) f->name_count); - for (k = 0; k < (int) f->name_count; k++) + fqdncache_entry *f = NULL; + int k; + int ttl; + if (fqdn_table == NULL) + return; + storeAppendPrintf(sentry, "FQDN Cache Statistics:\n"); + storeAppendPrintf(sentry, "FQDNcache Entries: %d\n", + memInUse(MEM_FQDNCACHE_ENTRY)); + storeAppendPrintf(sentry, "FQDNcache Requests: %d\n", + FqdncacheStats.requests); + storeAppendPrintf(sentry, "FQDNcache Hits: %d\n", + FqdncacheStats.hits); + storeAppendPrintf(sentry, "FQDNcache Negative Hits: %d\n", + FqdncacheStats.negative_hits); + storeAppendPrintf(sentry, "FQDNcache Misses: %d\n", + FqdncacheStats.misses); + storeAppendPrintf(sentry, "Blocking calls to gethostbyaddr(): %d\n", + FqdncacheStats.ghba_calls); + storeAppendPrintf(sentry, "FQDN Cache Contents:\n\n"); + + storeAppendPrintf(sentry, "%-15.15s %3s %3s %3s %s\n", + "Address","Flg","TTL","Cnt","Hostnames"); + hash_first(fqdn_table); + while ((f = (fqdncache_entry *) hash_next(fqdn_table))) { + ttl = (f->flags.fromhosts?-1:(f->expires - squid_curtime)); + storeAppendPrintf(sentry, "%-15.15s %c%c %3.3d % 3d", + hashKeyStr(&f->hash), + f->flags.negcached ? 'N' : ' ', + f->flags.fromhosts ? 'H' : ' ', + ttl, + (int) f->name_count); + for (k = 0; k < (int) f->name_count; k++) storeAppendPrintf(sentry, " %s", f->names[k]); - storeAppendPrintf(sentry, "\n"); - } + storeAppendPrintf(sentry, "\n"); + } } static void @@ -533,6 +558,43 @@ (float) FQDN_HIGH_WATER) / (float) 100); fqdncache_low = (long) (((float) Config.fqdncache.size * (float) FQDN_LOW_WATER) / (float) 100); + purge_entries_fromhosts(); +} + + +/* adds a "static" entry from /etc/hosts */ +/* the worldist is to be managed by the caller, including pointed-to strings */ +void fqdncacheAddEntryFromHosts (char *addr, wordlist *hostnames) { + fqdncache_entry *fce; + int j=0; + + if ((fce=fqdncache_get(addr))) { + if (fce->flags.fromhosts==1) { + fqdncacheUnlockEntry(fce); + } else { + if (fce->locks > 0) { + debug(35,1)("fqdncacheAddEntryFromHosts: can't add static entry for " + "locked address '%s'\n", addr); + return; + } else { + fqdncacheRelease(fce); + } + } + } + + fce=fqdncacheCreateEntry(addr); + while (hostnames) { + fce->names[j]=xstrdup(hostnames->key); + j++; + hostnames=hostnames->next; + if (j >= FQDN_MAX_NAMES) + break; + } + fce->name_count=j; + fce->names[j]=NULL; /* it's safe */ + fce->flags.fromhosts=1; + fqdncacheAddEntry(fce); + fqdncacheLockEntry(fce); } #ifdef SQUID_SNMP