File: caddrinfo.cc

package info (click to toggle)
apt-cacher-ng 2-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,032 kB
  • ctags: 1,705
  • sloc: cpp: 16,869; sh: 536; ansic: 404; perl: 377; makefile: 124
file content (183 lines) | stat: -rw-r--r-- 4,252 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183

#include "meta.h"
#include "caddrinfo.h"
#include "sockio.h"
#include "acfg.h"
#include "debug.h"
#include "lockable.h"
#include "cleaner.h"

#include <queue>

using namespace std;

namespace acng
{
static const unsigned DNS_CACHE_MAX=255;

base_with_condition dnsCacheCv;
map<tStrPair,CAddrInfoPtr> dnsCache;

// simple prio-queue to track the oldest entries
typedef decltype(dnsCache)::iterator dnsIter;
struct tDnsCmpEarliest
{
	bool operator() (const dnsIter& x,const dnsIter& y)
	{
		return (x->second->m_nExpTime > y->second->m_nExpTime);
	}
};
priority_queue<dnsIter, vector<dnsIter>, tDnsCmpEarliest> dnsCleanupQ;

bool CAddrInfo::Resolve(const string & sHostname, const string &sPort,
		string & sErrorBuf)
{
	LOGSTART2("CAddrInfo::Resolve", "Resolving " << sHostname);

	m_bError = false;
	m_bResInProgress = true;

	LPCSTR port = sPort.empty() ? nullptr : sPort.c_str();

	static struct addrinfo hints =
	{
	// we provide numbers, no resolution needed; only supported addresses
			(port ? AI_NUMERICSERV:0) | AI_ADDRCONFIG,
			PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP,
			0, nullptr, nullptr, nullptr };

	// if only one family is specified, filter on this earlier
	if(cfg::conprotos[0] != PF_UNSPEC && cfg::conprotos[1] == PF_UNSPEC)
		hints.ai_family = cfg::conprotos[0];

	if (m_resolvedInfo)
	{
		freeaddrinfo(m_resolvedInfo);
		m_resolvedInfo=nullptr;
	}

	int r=getaddrinfo(sHostname.c_str(), port, &hints, &m_resolvedInfo);

	if (0!=r)
	{
		sErrorBuf=(tSS()<<"503 DNS error for hostname "<<sHostname<<": "<<gai_strerror(r)
				<<". If "<<sHostname<<" refers to a configured cache repository, "
				"please check the corresponding configuration file.");
		m_bError = true;
		m_bResInProgress = false;
		return false;
	}

	LOG("Host resolved");
	
	// find any suitable-looking entry and keep a pointer to it
	for (auto pCur=m_resolvedInfo; pCur && m_bResInProgress ; pCur = pCur->ai_next)
	{
		if (pCur->ai_socktype == SOCK_STREAM&& pCur->ai_protocol == IPPROTO_TCP)
		{
			m_addrInfo=pCur;
			m_bResInProgress = false;
			return true;
		}
	}

	if(!m_bResInProgress)
		return true;

	LOG("couldn't find working DNS config");
	m_bError = true;
	m_bResInProgress = false;
	sErrorBuf="500 DNS resolution error";
	return false;
}

CAddrInfo::~CAddrInfo()
{
	if (m_resolvedInfo)
		freeaddrinfo(m_resolvedInfo);
}

CAddrInfoPtr CAddrInfo::CachedResolve(const string & sHostname, const string &sPort, string &sErrorMsgBuf)
{
	// set if someone is responsible for cleaning it up later
	bool added2cache = false;
	auto dnsKey=make_pair(sHostname, sPort);

	CAddrInfoPtr job;

	if (!cfg::dnscachetime)
	{
		job.reset(new CAddrInfo);
		goto plain_resolve;
	}

	if(!cfg::dnscachetime)
		goto plain_resolve;

	{
		lockuniq lg(dnsCacheCv);

		auto now = GetTime();

		// every caller does little cleanup work first
		while(!dnsCleanupQ.empty())
		{
			auto p = dnsCleanupQ.top()->second;
			if(p->m_nExpTime >= now)
			{
				dnsCache.erase(dnsCleanupQ.top());
				dnsCleanupQ.pop();
			}
			else if(dnsCleanupQ.size() < DNS_CACHE_MAX) // remaining ones are fresh enough unless we need to shrink the cache
				break;
		}

		// observe a DNS process in progress if needed
		auto it = dnsCache.find(dnsKey);
		if(it != dnsCache.end())
		{
			auto p = it->second;
			while(p->m_bResInProgress)
				dnsCacheCv.wait(lg);
			if(p->m_bError)
			{
				sErrorMsgBuf = string("Error resolving ") + sHostname;
				return job; // still invalid pointer
			}
			// otherwise it's ok
			return p;
		}
		job = make_shared<CAddrInfo>();
		added2cache = dnsCache.size() < DNS_CACHE_MAX; // if looks like DOS, don't dirty the temp queue with it
		if(added2cache)
			dnsCache.insert(make_pair(dnsKey, job));
	}
	plain_resolve:

	bool ok = job->Resolve(sHostname, sPort, sErrorMsgBuf);

	// there is a slight risk that someone else expired our entry in the meantime, but it's unlikely and should be harmless
	if(added2cache)
	{
		lockguard lg(dnsCacheCv);
		dnsCacheCv.notifyAll();
		if(ok)
		{
			auto it = dnsCache.find(dnsKey);
			if(it != dnsCache.end())
				dnsCleanupQ.push(it);
		}
		else
			dnsCache.erase(dnsKey);
	}
	return ok ? job : CAddrInfoPtr();
}

void DropDnsCache()
{
	lockguard g(dnsCacheCv);
	dnsCache.clear();
	dnsCleanupQ = decltype(dnsCleanupQ)();
}

}