added DNS::reload()

This commit is contained in:
Guenter Obiltschnig 2013-02-14 16:35:04 +01:00
parent b3a3ece54e
commit 0ab5c9dcf6
2 changed files with 37 additions and 5 deletions

View File

@ -44,7 +44,6 @@
#include "Poco/Net/SocketDefs.h"
#include "Poco/Net/IPAddress.h"
#include "Poco/Net/HostEntry.h"
#include "Poco/Mutex.h"
namespace Poco {
@ -115,6 +114,13 @@ public:
///
/// Throws an IOException in case of any other error.
static void reload();
/// Reloads the resolver configuration.
///
/// This method will call res_init() if the Net library
/// has been compiled with -DPOCO_HAVE_LIBRESOLV. Otherwise
/// it will do nothing.
//@ deprecated
static void flushCache();
/// Flushes the internal DNS cache.

View File

@ -39,11 +39,15 @@
#include "Poco/Net/SocketAddress.h"
#include "Poco/Environment.h"
#include "Poco/NumberFormatter.h"
#include "Poco/AtomicCounter.h"
#include "Poco/RWLock.h"
#include <cstring>
using Poco::FastMutex;
#if defined(POCO_HAVE_LIBRESOLV)
#include <resolv.h>
#endif
using Poco::Environment;
using Poco::NumberFormatter;
using Poco::IOException;
@ -53,8 +57,17 @@ namespace Poco {
namespace Net {
#if defined(POCO_HAVE_LIBRESOLV)
static Poco::RWLock resolverLock;
#endif
HostEntry DNS::hostByName(const std::string& hostname)
{
#if defined(POCO_HAVE_LIBRESOLV)
Poco::ScopedReadRWLock readLock(resolverLock);
#endif
#if defined(POCO_HAVE_IPv6) || defined(POCO_HAVE_ADDRINFO)
struct addrinfo* pAI;
struct addrinfo hints;
@ -91,6 +104,10 @@ HostEntry DNS::hostByName(const std::string& hostname)
HostEntry DNS::hostByAddress(const IPAddress& address)
{
#if defined(POCO_HAVE_LIBRESOLV)
Poco::ScopedReadRWLock readLock(resolverLock);
#endif
#if defined(POCO_HAVE_IPv6) || defined(POCO_HAVE_ADDRINFO)
SocketAddress sa(address, 0);
static char fqname[1024];
@ -162,6 +179,15 @@ HostEntry DNS::thisHost()
}
void DNS::reload()
{
#if defined(POCO_HAVE_LIBRESOLV)
Poco::ScopedWriteRWLock writeLock(resolverLock);
res_init();
#endif
}
void DNS::flushCache()
{
}
@ -214,7 +240,7 @@ void DNS::error(int code, const std::string& arg)
void DNS::aierror(int code, const std::string& arg)
{
#if defined(POCO_HAVE_IPv6)
#if defined(POCO_HAVE_IPv6) || defined(POCO_HAVE_ADDRINFO)
switch (code)
{
case EAI_AGAIN:
@ -241,7 +267,7 @@ void DNS::aierror(int code, const std::string& arg)
default:
throw DNSException("EAI", NumberFormatter::format(code));
}
#endif // POCO_HAVE_IPv6
#endif // POCO_HAVE_IPv6 || defined(POCO_HAVE_ADDRINFO)
}