Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/dns/forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
160 changes: 123 additions & 37 deletions src/dns_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -1545,10 +1611,10 @@ idnsRcodeCount(int rcode, int attempt)
++ RcodeMatrix[rcode][attempt];
}

void
Dns::Init(void)
static void
idnsInitialize()
{
static int init = 0;
idnsParseEtcHosts();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When working on improving this code, please do not add more configuration parsing steps into socket opening code. Squid should parse/validate configuration and then (if validation is successful) apply it (e.g., by closing and opening configuration-dependent sockets).


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.
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These initialization steps are not necessary for configuration parsing and, hence, do not belong to bootstrapConfig() event. Most likely, idns_lookup_hash should be created like the other two hashes affected by this PR scope (fqdn_table and ip_table).

}
void useConfig() override { idnsStartupSequence(); }
void startReconfigure() override { idnsShutdownAndFreeState("Reconfigure"); }
void syncConfig() override { idnsReconfigureSequence(); }
void endingShutdown() override { idnsShutdownAndFreeState("Shutdown"); }
};

} // namespace Dns

DefineRunnerRegistratorIn(Dns, ConfigRr);
11 changes: 5 additions & 6 deletions src/fqdncache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ fqdncacheExpiredEntry(const fqdncache_entry * f)
}

/// \ingroup FQDNCacheAPI
void
static void
fqdncache_purgelru(void *)
{
dlink_node *m;
Expand Down Expand Up @@ -684,10 +684,6 @@ fqdncacheRegisterWithCacheManager(void)
void
fqdncache_init(void)
{
int n;

fqdncacheRegisterWithCacheManager();

if (fqdn_table)
return;

Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/fqdncache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/ipcache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ ipcacheExpiredEntry(ipcache_entry * i)
}

/// \ingroup IPCacheAPI
void
static void
ipcache_purgelru(void *)
{
dlink_node *m;
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are polishing how n is declared, please improve its const-correctness. The same applies to another function where PR changes how n is declared.

ip_table = hash_create((HASHCMP *) strcmp, n, hash4);

ipcacheRegisterWithCacheManager();
ipcache_purgelru(nullptr);
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/ipcache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);
Expand Down
19 changes: 0 additions & 19 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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();

Expand All @@ -906,7 +900,6 @@ mainReconfigureFinish(void *)
icapLogOpen();
#endif
storeLogOpen();
Dns::Init();
#if USE_SSL_CRTD
Ssl::Helper::Reconfigure();
#endif
Expand Down Expand Up @@ -1083,14 +1076,6 @@ mainInitialize(void)

#endif

ipcache_init();

fqdncache_init();

parseEtcHosts();

Dns::Init();

#if USE_SSL_CRTD
Ssl::Helper::Init();
#endif
Expand Down Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/tests/stub_fqdncache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion src/tests/stub_ipcache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/tests/stub_tools.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading
Loading