File: caddrinfo.cc

package info (click to toggle)
apt-cacher-ng 0.7.27-1~bpo70+1
  • links: PTS
  • area: main
  • in suites: wheezy-backports
  • size: 1,740 kB
  • sloc: cpp: 13,987; sh: 519; perl: 442; ansic: 414; makefile: 77
file content (145 lines) | stat: -rw-r--r-- 3,235 bytes parent folder | download
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

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

using namespace MYSTD;

static map<string, CAddrInfo::SPtr> mapDnsCache;
static pthread_mutex_t lockDnsCache= PTHREAD_MUTEX_INITIALIZER;

CAddrInfo::CAddrInfo() :
	m_nExpTime(0), m_addrInfo(NULL), m_resolvedInfo(NULL)
{
}

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

	LPCSTR port = sPort.empty() ? NULL : 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, NULL, NULL, NULL };

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

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

	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.");
		return false;
	}

	LOG("Host resolved");

	for (struct addrinfo * pCur=m_resolvedInfo; pCur; pCur = pCur->ai_next)
	{
		if (pCur->ai_socktype == SOCK_STREAM&& pCur->ai_protocol == IPPROTO_TCP)
		{
			m_addrInfo=pCur;
			return true;
		}
	}
	LOG("couldn't find working DNS config");

	sErrorBuf="500 DNS resolution error";

	// TODO: remove me from the map
	return false;
}

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

CAddrInfo::SPtr CAddrInfo::CachedResolve(const string & sHostname, const string &sPort, string &sErrorMsgBuf)
{
	//time_t timeExpired=time(NULL)+acfg::dnscachetime;
	time_t now(time(0));
	mstring dnsKey=sHostname+":"+sPort;

	SPtr p;

	{
		lockguard g(&lockDnsCache);
		SPtr localEntry;
		SPtr & cand = acfg::dnscachetime>0 ? mapDnsCache[dnsKey] : localEntry;
		if(cand && cand->m_nExpTime >= now)
			return cand;
		cand.reset(new CAddrInfo);
		p=cand;
		// lock the internal class and keep it until we are done with preparations
		p->lock();
	}

	if(!p)
		return SPtr(); // weird...

	if (p->Resolve(sHostname, sPort, sErrorMsgBuf))
	{
		p->m_nExpTime = time(0) + acfg::dnscachetime;
#ifndef MINIBUILD
		g_victor.ScheduleFor(p->m_nExpTime, cleaner::TYPE_EXDNS);
#endif
		p->unlock();
	}
	else // not good, remove from the cache again
	{
		aclog::err( (tSS()<<"Error resolving "<<dnsKey<<": " <<sErrorMsgBuf).c_str());
		lockguard g(&lockDnsCache);
		mapDnsCache.erase(dnsKey);
		p->unlock();
		p.reset();
	}

	return p;
}

time_t CAddrInfo::BackgroundCleanup()
{
	lockguard g(&lockDnsCache);
	time_t now(GetTime()), ret(END_OF_TIME);

	for(map<string, CAddrInfo::SPtr>::iterator it=mapDnsCache.begin();
			it!=mapDnsCache.end(); )
	{
		if(it->second)
		{
			if(it->second->m_nExpTime<=now)
			{
				mapDnsCache.erase(it++);
				continue;
			}
			else
				ret=min(ret, it->second->m_nExpTime);
		}

		++it;
	}
	return ret;
}