diff --git a/src/dns/forward.h b/src/dns/forward.h index 9168b2ad723..b7f4689ada5 100644 --- a/src/dns/forward.h +++ b/src/dns/forward.h @@ -22,8 +22,6 @@ namespace Dns class LookupDetails; -void Init(void); - /// A DNS domain name as described in RFC 1034 and RFC 1035. /// /// The object creator is responsible for removing any encodings (e.g., URI diff --git a/src/dns_internal.cc b/src/dns_internal.cc index 259c080eea2..a6366c549bb 100644 --- a/src/dns_internal.cc +++ b/src/dns_internal.cc @@ -27,7 +27,9 @@ #include "event.h" #include "fd.h" #include "fde.h" +#include "fqdncache.h" #include "ip/tools.h" +#include "ipcache.h" #include "MemBuf.h" #include "mgr/Registration.h" #include "snmp_agent.h" @@ -199,22 +201,6 @@ class ns nsvc *vc = nullptr; }; -namespace Dns -{ - -/// manage DNS internal component -class ConfigRr : public RegisteredRunner -{ -public: - /* RegisteredRunner API */ - void startReconfigure() override; - void endingShutdown() override; -}; - -} // namespace Dns - -DefineRunnerRegistratorIn(Dns, ConfigRr); - struct _sp { char domain[NS_MAXDNAME]; int queries; @@ -397,6 +383,86 @@ idnsParseNameservers(void) return result; } +static void +idnsParseEtcHosts() +{ + char buf[1024]; + char buf2[512]; + + if (!Config.etcHostsPath) + return; + + if (strcmp(Config.etcHostsPath, "none") == 0) + return; + + FILE *fp = fopen(Config.etcHostsPath, "r"); + if (!fp) { + int xerrno = errno; + debugs(78, DBG_IMPORTANT, "'" << Config.etcHostsPath << "' : " << xstrerr(xerrno)); + return; + } + +#if _SQUID_WINDOWS_ + setmode(fileno(fp), O_TEXT); +#endif + + while (fgets(buf, 1024, fp)) { /* for each line */ + + if (buf[0] == '#') /* MS-windows likes to add comments */ + continue; + strtok(buf, "#"); /* chop everything following a comment marker */ + + auto lt = buf; + char *addr = buf; + debugs(78, 5, "etc_hosts: line is '" << buf << "'"); + + auto nt = strpbrk(lt, w_space); + if (!nt) /* empty line */ + continue; + *nt = '\0'; /* null-terminate the address */ + debugs(78, 5, "etc_hosts: address is '" << addr << "'"); + + lt = nt + 1; + SBufList hosts; + while ((nt = strpbrk(lt, w_space))) { + char *host = nullptr; + + if (nt == lt) { /* multiple spaces */ + debugs(78, 5, "etc_hosts: multiple spaces, skipping"); + lt = nt + 1; + continue; + } + *nt = '\0'; + debugs(78, 5, "etc_hosts: got hostname '" << lt << "'"); + + /* For IPV6 addresses also check for a colon */ + if (Config.appendDomain && !strchr(lt, '.') && !strchr(lt, ':')) { + /* I know it's ugly, but it's only at reconfig */ + strncpy(buf2, lt, sizeof(buf2)-1); + strncat(buf2, Config.appendDomain, sizeof(buf2) - strlen(lt) - 1); + buf2[sizeof(buf2)-1] = '\0'; + host = buf2; + } else { + host = lt; + } + + if (ipcacheAddEntryFromHosts(host, addr) != 0) { + /* invalid address, continuing is useless */ + hosts.clear(); + break; + } + hosts.emplace_back(SBuf(host)); + + lt = nt + 1; + } + + if (!hosts.empty()) + fqdncacheAddEntryFromHosts(addr, hosts); + } + + fclose(fp); +} + static bool idnsParseResolvConf(void) { @@ -1545,10 +1611,10 @@ idnsRcodeCount(int rcode, int attempt) ++ RcodeMatrix[rcode][attempt]; } -void -Dns::Init(void) +static void +idnsInitialize() { - static int init = 0; + idnsParseEtcHosts(); if (DnsSocketA < 0 && DnsSocketB < 0) { Ip::Address addrV6; // since we do not want to alter Config.Addrs.udp_* and do not have one of our own. @@ -1623,12 +1689,6 @@ Dns::Init(void) idnsAddNameserver("127.0.0.1"); } - if (!init) { - memset(RcodeMatrix, '\0', sizeof(RcodeMatrix)); - idns_lookup_hash = hash_create((HASHCMP *) strcmp, 103, hash_string); - ++init; - } - #if WHEN_EDNS_RESPONSES_ARE_PARSED if (Config.onoff.ignore_unknown_nameservers && max_shared_edns > 0) { debugs(0, DBG_IMPORTANT, "ERROR: cannot negotiate EDNS with unknown nameservers. Disabling"); @@ -1669,18 +1729,6 @@ idnsShutdownAndFreeState(const char *reason) idnsFreeSearchpath(); } -void -Dns::ConfigRr::endingShutdown() -{ - idnsShutdownAndFreeState("Shutdown"); -} - -void -Dns::ConfigRr::startReconfigure() -{ - idnsShutdownAndFreeState("Reconfigure"); -} - static int idnsCachedLookup(const char *key, IDNSCB * callback, void *data) { @@ -1900,3 +1948,41 @@ snmp_netDnsFn(variable_list * Var, snint * ErrP) #endif /*SQUID_SNMP */ +static void +idnsStartupSequence() +{ + ipcache_init(); + fqdncache_init(); + idnsInitialize(); +} + +static void +idnsReconfigureSequence() +{ + idnsInitialize(); + ipcache_restart(); /* clear stuck entries */ + fqdncache_restart(); /* sigh, fqdncache too */ +} + +namespace Dns +{ + +/// manage DNS internal component +class ConfigRr : public RegisteredRunner +{ +public: + /* RegisteredRunner API */ + void bootstrapConfig() override { + // XXX: replace globals + memset(RcodeMatrix, '\0', sizeof(RcodeMatrix)); + idns_lookup_hash = hash_create((HASHCMP *) strcmp, 103, hash_string); + } + void useConfig() override { idnsStartupSequence(); } + void startReconfigure() override { idnsShutdownAndFreeState("Reconfigure"); } + void syncConfig() override { idnsReconfigureSequence(); } + void endingShutdown() override { idnsShutdownAndFreeState("Shutdown"); } +}; + +} // namespace Dns + +DefineRunnerRegistratorIn(Dns, ConfigRr); diff --git a/src/fqdncache.cc b/src/fqdncache.cc index 6c8168f9612..797262a05c1 100644 --- a/src/fqdncache.cc +++ b/src/fqdncache.cc @@ -196,7 +196,7 @@ fqdncacheExpiredEntry(const fqdncache_entry * f) } /// \ingroup FQDNCacheAPI -void +static void fqdncache_purgelru(void *) { dlink_node *m; @@ -684,10 +684,6 @@ fqdncacheRegisterWithCacheManager(void) void fqdncache_init(void) { - int n; - - fqdncacheRegisterWithCacheManager(); - if (fqdn_table) return; @@ -702,9 +698,12 @@ fqdncache_init(void) fqdncache_low = (long) (((float) Config.fqdncache.size * (float) FQDN_LOW_WATER) / (float) 100); - n = hashPrime(fqdncache_high / 4); + auto n = hashPrime(fqdncache_high / 4); fqdn_table = hash_create((HASHCMP *) strcmp, n, hash4); + + fqdncacheRegisterWithCacheManager(); + fqdncache_purgelru(nullptr); } #if SQUID_SNMP diff --git a/src/fqdncache.h b/src/fqdncache.h index 49d22968ed8..4a8bd9b1f92 100644 --- a/src/fqdncache.h +++ b/src/fqdncache.h @@ -27,7 +27,6 @@ using FQDNH = void (const char *, const Dns::LookupDetails &, void *); void fqdncache_init(); void fqdnStats(StoreEntry *); void fqdncache_restart(); -void fqdncache_purgelru(void *); void fqdncacheAddEntryFromHosts(char *addr, SBufList &hostnames); const char *fqdncache_gethostbyaddr(const Ip::Address &, int flags); diff --git a/src/ipcache.cc b/src/ipcache.cc index cbcc715a19c..5c2e2b4344f 100644 --- a/src/ipcache.cc +++ b/src/ipcache.cc @@ -349,7 +349,7 @@ ipcacheExpiredEntry(ipcache_entry * i) } /// \ingroup IPCacheAPI -void +static void ipcache_purgelru(void *) { dlink_node *m; @@ -695,7 +695,6 @@ ipcacheRegisterWithCacheManager(void) void ipcache_init(void) { - int n; debugs(14, Important(24), "Initializing IP Cache..."); memset(&IpcacheStats, '\0', sizeof(IpcacheStats)); lru_list = dlink_list(); @@ -704,10 +703,11 @@ ipcache_init(void) (float) Config.ipcache.high) / (float) 100); ipcache_low = (long) (((float) Config.ipcache.size * (float) Config.ipcache.low) / (float) 100); - n = hashPrime(ipcache_high / 4); + auto n = hashPrime(ipcache_high / 4); ip_table = hash_create((HASHCMP *) strcmp, n, hash4); ipcacheRegisterWithCacheManager(); + ipcache_purgelru(nullptr); } /** diff --git a/src/ipcache.h b/src/ipcache.h index 420cb349368..060f954c6ce 100644 --- a/src/ipcache.h +++ b/src/ipcache.h @@ -226,7 +226,6 @@ typedef Dns::CachedIps ipcache_addrs; ///< deprecated alias typedef void IPH(const ipcache_addrs *, const Dns::LookupDetails &details, void *); -void ipcache_purgelru(void *); void ipcache_nbgethostbyname(const char *name, IPH * handler, void *handlerData); const ipcache_addrs *ipcache_gethostbyname(const char *, int flags); void ipcacheInvalidate(const char *); diff --git a/src/main.cc b/src/main.cc index c9ad17f905d..979f25063b6 100644 --- a/src/main.cc +++ b/src/main.cc @@ -30,14 +30,12 @@ #include "CpuAffinity.h" #include "debug/Messages.h" #include "DiskIO/DiskIOModule.h" -#include "dns/forward.h" #include "errorpage.h" #include "event.h" #include "EventLoop.h" #include "ExternalACL.h" #include "fd.h" #include "format/Token.h" -#include "fqdncache.h" #include "fs/Module.h" #include "fs_io.h" #include "FwdState.h" @@ -53,7 +51,6 @@ #include "ipc/Coordinator.h" #include "ipc/Kids.h" #include "ipc/Strand.h" -#include "ipcache.h" #include "mime.h" #include "neighbors.h" #include "parser/Tokenizer.h" @@ -878,9 +875,6 @@ mainReconfigureFinish(void *) setUmask(Config.umask); setEffectiveUser(); Debug::UseCacheLog(); - ipcache_restart(); /* clear stuck entries */ - fqdncache_restart(); /* sigh, fqdncache too */ - parseEtcHosts(); errorInitialize(); /* reload error pages */ accessLogInit(); @@ -906,7 +900,6 @@ mainReconfigureFinish(void *) icapLogOpen(); #endif storeLogOpen(); - Dns::Init(); #if USE_SSL_CRTD Ssl::Helper::Reconfigure(); #endif @@ -1083,14 +1076,6 @@ mainInitialize(void) #endif - ipcache_init(); - - fqdncache_init(); - - parseEtcHosts(); - - Dns::Init(); - #if USE_SSL_CRTD Ssl::Helper::Init(); #endif @@ -1211,10 +1196,6 @@ mainInitialize(void) eventAdd("storeMaintain", Store::Maintain, nullptr, 1.0, 1); - eventAdd("ipcache_purgelru", ipcache_purgelru, nullptr, 10.0, 1); - - eventAdd("fqdncache_purgelru", fqdncache_purgelru, nullptr, 15.0, 1); - eventAdd("memPoolCleanIdlePools", Mem::CleanIdlePools, nullptr, 15.0, 1); configured_once = 1; diff --git a/src/tests/stub_fqdncache.cc b/src/tests/stub_fqdncache.cc index 774249ec3cc..2d9d0d33a44 100644 --- a/src/tests/stub_fqdncache.cc +++ b/src/tests/stub_fqdncache.cc @@ -17,7 +17,6 @@ bool Dns::ResolveClientAddressesAsap = false; void fqdncache_init(void) STUB void fqdnStats(StoreEntry *) STUB void fqdncache_restart(void) STUB -void fqdncache_purgelru(void *) STUB void fqdncacheAddEntryFromHosts(char *, SBufList &) STUB const char *fqdncache_gethostbyaddr(const Ip::Address &, int) STUB_RETVAL(nullptr) void fqdncache_nbgethostbyaddr(const Ip::Address &, FQDNH *, void *) STUB diff --git a/src/tests/stub_ipcache.cc b/src/tests/stub_ipcache.cc index 33cc3cdcca0..0d0c4ca1383 100644 --- a/src/tests/stub_ipcache.cc +++ b/src/tests/stub_ipcache.cc @@ -12,7 +12,6 @@ #define STUB_API "ipcache.cc" #include "tests/STUB.h" -void ipcache_purgelru(void *) STUB void ipcache_nbgethostbyname(const char *, IPH *, void *) STUB const ipcache_addrs *ipcache_gethostbyname(const char *, int) STUB_RETVAL(nullptr) void ipcacheInvalidate(const char *) STUB diff --git a/src/tests/stub_tools.cc b/src/tests/stub_tools.cc index 19fb4c271fd..764986ca71c 100644 --- a/src/tests/stub_tools.cc +++ b/src/tests/stub_tools.cc @@ -64,7 +64,6 @@ void setSystemLimits(void) STUB void squid_signal(int, SIGHDLR *, int) STUB void logsFlush(void) STUB void debugObj(int, int, const char *, void *, ObjPackMethod) STUB -void parseEtcHosts(void) STUB int getMyPort(void) STUB_RETVAL(0) void setUmask(mode_t) STUB void strwordquote(MemBuf *, const char *) STUB diff --git a/src/tools.cc b/src/tools.cc index a828805581f..1e8fad6412d 100644 --- a/src/tools.cc +++ b/src/tools.cc @@ -946,98 +946,6 @@ debugObj(int section, int level, const char *label, void *obj, ObjPackMethod pm) mb.clean(); } -void -parseEtcHosts(void) -{ - char buf[1024]; - char buf2[512]; - char *nt = buf; - char *lt = buf; - - if (!Config.etcHostsPath) - return; - - if (0 == strcmp(Config.etcHostsPath, "none")) - return; - - FILE *fp = fopen(Config.etcHostsPath, "r"); - - if (!fp) { - int xerrno = errno; - debugs(1, DBG_IMPORTANT, "parseEtcHosts: '" << Config.etcHostsPath << "' : " << xstrerr(xerrno)); - return; - } - -#if _SQUID_WINDOWS_ - setmode(fileno(fp), O_TEXT); -#endif - - while (fgets(buf, 1024, fp)) { /* for each line */ - - if (buf[0] == '#') /* MS-windows likes to add comments */ - continue; - - strtok(buf, "#"); /* chop everything following a comment marker */ - - lt = buf; - - char *addr = buf; - - debugs(1, 5, "etc_hosts: line is '" << buf << "'"); - - nt = strpbrk(lt, w_space); - - if (nt == nullptr) /* empty line */ - continue; - - *nt = '\0'; /* null-terminate the address */ - - debugs(1, 5, "etc_hosts: address is '" << addr << "'"); - - lt = nt + 1; - - SBufList hosts; - - while ((nt = strpbrk(lt, w_space))) { - char *host = nullptr; - - if (nt == lt) { /* multiple spaces */ - debugs(1, 5, "etc_hosts: multiple spaces, skipping"); - lt = nt + 1; - continue; - } - - *nt = '\0'; - debugs(1, 5, "etc_hosts: got hostname '" << lt << "'"); - - /* For IPV6 addresses also check for a colon */ - if (Config.appendDomain && !strchr(lt, '.') && !strchr(lt, ':')) { - /* I know it's ugly, but it's only at reconfig */ - strncpy(buf2, lt, sizeof(buf2)-1); - strncat(buf2, Config.appendDomain, sizeof(buf2) - strlen(lt) - 1); - buf2[sizeof(buf2)-1] = '\0'; - host = buf2; - } else { - host = lt; - } - - if (ipcacheAddEntryFromHosts(host, addr) != 0) { - /* invalid address, continuing is useless */ - hosts.clear(); - break; - } - hosts.emplace_back(SBuf(host)); - - lt = nt + 1; - } - - if (!hosts.empty()) - fqdncacheAddEntryFromHosts(addr, hosts); - } - - fclose (fp); -} - int getMyPort(void) { diff --git a/src/tools.h b/src/tools.h index aa9d3c8223b..6c5ffaf3cba 100644 --- a/src/tools.h +++ b/src/tools.h @@ -22,7 +22,6 @@ extern int DebugSignal; /// Default is APP_SHORTNAME ('squid'). extern SBuf service_name; -void parseEtcHosts(void); int getMyPort(void); void setUmask(mode_t mask); void strwordquote(MemBuf * mb, const char *str);