File: caddrinfo.cc

package info (click to toggle)
apt-cacher-ng 3.3.1-2~bpo10+1
  • links: PTS, VCS
  • area: main
  • in suites: buster-backports
  • size: 2,040 kB
  • sloc: cpp: 17,564; sh: 553; ansic: 401; perl: 377; makefile: 126
file content (202 lines) | stat: -rw-r--r-- 4,926 bytes parent folder | download | duplicates (2)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

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

#include <deque>
#include <memory>
#include <thread>
using namespace std;

#define DNS_MAX_PAR 8

namespace acng
{
static const unsigned DNS_CACHE_MAX=255;

static string make_dns_key(const string & sHostname, const string &sPort)
{
	return sHostname + "/" + sPort;
}

base_with_condition dnsCacheCv;

map<string,CAddrInfoPtr> dnsCache;
deque<decltype(dnsCache)::iterator> dnsAddSeq;

bool CAddrInfo::ResolveTcpTarget(const string & sHostname, const string &sPort,
		string & sErrorBuf,
		const evutil_addrinfo* pHints,
		bool & bTransientError)
{
	LOGSTART2("CAddrInfo::Resolve", "Resolving " << sHostname);

	sErrorBuf.clear();
	auto filter_specific = (cfg::conprotos[0] != PF_UNSPEC && cfg::conprotos[1] == PF_UNSPEC);
	evutil_addrinfo default_connect_hints =
	{
		// we provide plain port numbers, no resolution needed
		// also return only probably working addresses
		AI_NUMERICSERV | AI_ADDRCONFIG,
		filter_specific ? cfg::conprotos[0] : PF_UNSPEC,
		SOCK_STREAM, IPPROTO_TCP,
		0, nullptr, nullptr, nullptr
	};

	auto ret_error = [&](const char* sfx, int rc)
			{
		// XXX: maybe also evaluate errno in case of EAI_SYSTEM
		sErrorBuf = "503 DNS error for " + sHostname + ":" + sPort + " : " + evutil_gai_strerror(rc);
		if(sfx)	sErrorBuf += string("(") + sfx + ")";
		LOG(sfx);
		return false;
	};

	Reset();

	static atomic_int dns_overload_limiter(0);
	while(dns_overload_limiter > DNS_MAX_PAR)
		this_thread::sleep_for(1s);
	dns_overload_limiter++;
	int r = evutil_getaddrinfo(sHostname.empty() ? nullptr : sHostname.c_str(),
			sPort.empty() ? nullptr : sPort.c_str(),
			pHints ? pHints : &default_connect_hints,
			&m_rawInfo);
	dns_overload_limiter--;

	switch(r)
	{
	case 0: break;
	case EAI_AGAIN:
	case EAI_MEMORY:
	case EAI_SYSTEM:
		bTransientError = true;
		__just_fall_through;
	default:
		return ret_error("If this refers to a configured cache repository, please check the corresponding configuration file", r);
	}
#ifdef DEBUG
	for(auto p=m_rawInfo; p; p=p->ai_next)
		std::cerr << formatIpPort(p) << std::endl;
#endif
	// find any suitable-looking entry and keep a pointer to it faster lookup
	for (auto pCur=m_rawInfo; pCur; pCur = pCur->ai_next)
	{
		if (pCur->ai_socktype != SOCK_STREAM || pCur->ai_protocol != IPPROTO_TCP)
			continue;
		m_tcpAddrInfo = pCur;
		return true;
	}

	return ret_error("no suitable target service", EAI_SYSTEM);
}

void CAddrInfo::Reset()
{
	if (m_rawInfo)
		evutil_freeaddrinfo(m_rawInfo);
	m_tcpAddrInfo = m_rawInfo = nullptr;
}

CAddrInfo::~CAddrInfo()
{
	Reset();
}

CAddrInfoPtr CAddrInfo::CachedResolve(const string & sHostname, const string &sPort, string &sErrorMsgBuf)
{
	bool dummy_run = sHostname.empty() && sPort.empty();
	bool bTransientError = false;
	auto resolve_now = [&]()
			{
		auto ret = make_shared<CAddrInfo>();
		if(! ret->ResolveTcpTarget(sHostname, sPort, sErrorMsgBuf, nullptr, bTransientError))
			ret.reset();
		return ret;
	};
	if (!cfg::dnscachetime && !dummy_run)
		return resolve_now();

	auto dnsKey = make_dns_key(sHostname, sPort);
	auto now(GetTime());

	lockuniq lg(dnsCacheCv);

	// clean all expired entries
	unsigned n(0);
	while(!dnsAddSeq.empty() && dnsAddSeq.front()->second && dnsAddSeq.front()->second->m_expTime <= now)
	{
		// just to be safe in case anyone waits for it
		if(!dnsAddSeq.front()->second->m_sError)
			dnsAddSeq.front()->second->m_sError.reset(new string("504 DNS Cache Timeout"));
		dnsCache.erase(dnsAddSeq.front());
		dnsAddSeq.pop_front();
		++n;
	}
	if(n) dnsCacheCv.notifyAll();

	if (dummy_run)
		return CAddrInfoPtr();

	if (dnsCache.size() >= DNS_CACHE_MAX)
	{
		// something is fishy, too long exp. time? Just pass through then
		lg.unLock();
		return resolve_now();
	}

	auto insres = dnsCache.emplace(dnsKey, make_shared<CAddrInfo>());
	auto p = insres.first->second;
	auto is_ours(insres.second);
	if (is_ours)
	{
		dnsAddSeq.push_back(insres.first);
		p->m_expTime = now + cfg::dnscachetime;
	}
	else // reuse the results from another thread
	{
		while(true)
		{
			if(p->m_sError)
			{
				sErrorMsgBuf = * p->m_sError;
				return CAddrInfoPtr();
			}
			if(p->m_expTime <= MAX_VAL(time_t))
				return p;
			dnsCacheCv.wait(lg);
		}
	}
	lg.unLock();
	auto resret = p->ResolveTcpTarget(sHostname, sPort, sErrorMsgBuf, nullptr, bTransientError);
	lg.reLock();
	if(resret)
	{
		p->m_expTime = now + cfg::dnscachetime;
		dnsCacheCv.notifyAll();
		return p;
	}
	// or handle errors, keep them for observers
	p->m_sError.reset(new string(sErrorMsgBuf));
	// for permanent failures, also keep them in cache for a while
	if(!bTransientError)
		p->m_expTime = now + cfg::dnscachetime;
	dnsCacheCv.notifyAll();
	return p;
}

#if 0
void DropDnsCache()
{
#warning check all usage
	lockguard g(dnsCacheCv);
	dnsCleanupQ.clear();
	dnsCache.clear();
}
#endif

}