The InspIRCd Project
Home | Developers | Wiki | Forums | Bug Tracker | SVN | Download
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

DNS Class Reference

DNS is a singleton class used by the core to dispatch dns requests to the dns server, and route incoming dns replies back to Resolver objects, based upon the request ID. More...

#include <dns.h>

Inheritance diagram for DNS:

Inheritance graph
[legend]
Collaboration diagram for DNS:

Collaboration graph
[legend]
List of all members.

Public Member Functions

int GetIP (const char *name)
 Start the lookup of an ipv4 from a hostname.
int GetName (const irc::sockets::insp_inaddr *ip)
 Start the lookup of a hostname from an ip, always using the protocol inspircd is built for, e.g.
int GetNameForce (const char *ip, ForceProtocol fp)
 Start lookup of a hostname from an ip, but force a specific protocol to be used for the lookup for example to perform an ipv6 reverse lookup.
int GetIP6 (const char *name)
 Start lookup of an ipv6 from a hostname.
int GetCName (const char *alias)
 Start lookup of a CNAME from another hostname.
DNSResult GetResult (int resultnum)
 Fetch the result string (an ip or host) and/or an error message to go with it.
void HandleEvent (EventType et, int errornum=0)
 Handle a SocketEngine read event Inherited from EventHandler.
bool AddResolverClass (Resolver *r)
 Add a Resolver* to the list of active classes.
DNSRequestAddQuery (DNSHeader *header, int &id, const char *original)
 Add a query to the list to be sent.
 DNS (InspIRCd *Instance)
 The constructor initialises the dns socket, and clears the request lists.
void Rehash ()
 Re-initialize the DNS subsystem.
 ~DNS ()
 Destructor.
unsigned long PRNG ()
 Portable random number generator, generates its random number from the ircd stats counters, effective user id, time of day and the rollover counter (currid).
void CleanResolvers (Module *module)
 Clean out all dns resolvers owned by a particular module, to make unloading a module safe if there are dns requests currently in progress.
CachedQueryGetCache (const std::string &source)
 Return the cached value of an IP or hostname.
void DelCache (const std::string &source)
 Delete a cached item from the DNS cache.
int ClearCache ()
 Clear all items from the DNS cache immediately.
int PruneCache ()
 Prune the DNS cache, e.g.

Static Public Member Functions

static void FillResourceRecord (ResourceRecord *rr, const unsigned char *input)
 Fill an rr (resource record) with data from input.
static void FillHeader (DNSHeader *header, const unsigned char *input, const int length)
 Fill a header with data from input limited by a length.
static void EmptyHeader (unsigned char *output, const DNSHeader *header, const int length)
 Empty out a header into a data stream ready for transmission "on the wire".
static void MakeIP6Int (char *query, const in6_addr *ip)
 Turn an in6_addr into a .ip6.arpa domain.

Public Attributes

int socketfamily
 Server address being used currently.
in_addr myserver4
 IPV4 server address.
ResolverClasses [MAX_REQUEST_ID]
 Currently active Resolver classes.
DNSRequestrequests [MAX_REQUEST_ID]
 Requests that are currently 'in flight'.

Static Public Attributes

static const int QUERY_PORT = 53
 The port number DNS requests are made on, and replies have as a source-port number.

Private Member Functions

int MakePayload (const char *name, const QueryType rr, const unsigned short rr_class, unsigned char *payload)
 Build a dns packet payload.

Private Attributes

InspIRCdServerInstance
 Creator/Owner object.
int currid
 A counter used to form part of the pseudo-random id.
bool ip6munge
 We have to turn off a few checks on received packets when people are using 4in6 (e.g.
dnscachecache
 Currently cached items.
CacheTimerPruneTimer
 A timer which ticks every hour to remove expired items from the DNS cache.

Static Private Attributes

static const int MAX_REQUEST_ID = 0xFFFF
 The maximum value of a dns request id, 16 bits wide, 0xFFFF.

Detailed Description

DNS is a singleton class used by the core to dispatch dns requests to the dns server, and route incoming dns replies back to Resolver objects, based upon the request ID.

You should never use this class yourself.

Definition at line 329 of file dns.h.


Constructor & Destructor Documentation

DNS::DNS InspIRCd Instance  ) 
 

The constructor initialises the dns socket, and clears the request lists.

Definition at line 392 of file dns.cpp.

References TimerManager::AddTimer(), cache, Classes, currid, DEBUG, LogManager::Log(), InspIRCd::Logs, PruneTimer, Rehash(), requests, ServerInstance, EventHandler::SetFd(), and InspIRCd::Timers.

DNS::~DNS  ) 
 

Destructor.

Definition at line 927 of file dns.cpp.

References SocketEngine::Close(), TimerManager::DelTimer(), InspIRCd::SE, ServerInstance, SocketEngine::Shutdown(), and InspIRCd::Timers.


Member Function Documentation

DNSRequest * DNS::AddQuery DNSHeader header,
int &  id,
const char *  original
 

Add a query to the list to be sent.

Definition at line 248 of file dns.cpp.

References DNSHeader::ancount, DNSHeader::arcount, DNSHeader::flags1, DNSHeader::flags2, FLAGS_MASK_RD, DNSRequest::id, DNSHeader::id, MAX_REQUEST_ID, DNSHeader::nscount, PRNG(), DNSHeader::qdcount, requests, and ServerInstance.

Referenced by GetCName(), GetIP(), GetIP6(), GetName(), and GetNameForce().

bool DNS::AddResolverClass Resolver r  ) 
 

Add a Resolver* to the list of active classes.

Definition at line 1112 of file dns.cpp.

References Classes, DEBUG, Resolver::GetId(), LogManager::Log(), InspIRCd::Logs, and ServerInstance.

Referenced by InspIRCd::AddResolver().

void DNS::CleanResolvers Module module  ) 
 

Clean out all dns resolvers owned by a particular module, to make unloading a module safe if there are dns requests currently in progress.

Definition at line 1144 of file dns.cpp.

References Classes, MAX_REQUEST_ID, Resolver::OnError(), and RESLOVER_FORCEUNLOAD.

Referenced by ModuleManager::Unload().

int DNS::ClearCache  ) 
 

Clear all items from the DNS cache immediately.

Definition at line 281 of file dns.cpp.

References cache.

void DNS::DelCache const std::string source  ) 
 

Delete a cached item from the DNS cache.

Parameters:
source An IP or hostname to remove

Definition at line 943 of file dns.cpp.

References cache.

Referenced by Resolver::Resolver().

void DNS::EmptyHeader unsigned char *  output,
const DNSHeader header,
const int  length
[inline, static]
 

Empty out a header into a data stream ready for transmission "on the wire".

Definition at line 191 of file dns.cpp.

References DNSHeader::ancount, DNSHeader::arcount, DNSHeader::flags1, DNSHeader::flags2, DNSHeader::id, DNSHeader::nscount, DNSHeader::payload, and DNSHeader::qdcount.

Referenced by DNSRequest::SendRequests().

void DNS::FillHeader DNSHeader header,
const unsigned char *  input,
const int  length
[inline, static]
 

Fill a header with data from input limited by a length.

Definition at line 177 of file dns.cpp.

References DNSHeader::ancount, DNSHeader::arcount, DNSHeader::flags1, DNSHeader::flags2, DNSHeader::id, DNSHeader::nscount, DNSHeader::payload, and DNSHeader::qdcount.

Referenced by GetResult().

void DNS::FillResourceRecord ResourceRecord rr,
const unsigned char *  input
[inline, static]
 

Fill an rr (resource record) with data from input.

Definition at line 168 of file dns.cpp.

References ResourceRecord::rdlength, ResourceRecord::rr_class, ResourceRecord::ttl, and ResourceRecord::type.

Referenced by DNSRequest::ResultIsReady().

CachedQuery * DNS::GetCache const std::string source  ) 
 

Return the cached value of an IP or hostname.

Parameters:
source An IP or hostname to find in the cache.
Returns:
A pointer to a CachedQuery if the item exists, otherwise NULL.

Definition at line 934 of file dns.cpp.

References cache.

Referenced by Resolver::Resolver().

int DNS::GetCName const char *  alias  ) 
 

Start lookup of a CNAME from another hostname.

Definition at line 497 of file dns.cpp.

References AddQuery(), DNS_QUERY_CNAME, DNSHeader::payload, and DNSRequest::SendRequests().

Referenced by Resolver::Resolver().

int DNS::GetIP const char *  name  ) 
 

Start the lookup of an ipv4 from a hostname.

Definition at line 461 of file dns.cpp.

References AddQuery(), DNS_QUERY_A, DNSHeader::payload, and DNSRequest::SendRequests().

Referenced by Resolver::Resolver().

int DNS::GetIP6 const char *  name  ) 
 

Start lookup of an ipv6 from a hostname.

Definition at line 479 of file dns.cpp.

References AddQuery(), DNS_QUERY_AAAA, DNSHeader::payload, and DNSRequest::SendRequests().

Referenced by Resolver::Resolver().

int DNS::GetName const irc::sockets::insp_inaddr ip  ) 
 

Start the lookup of a hostname from an ip, always using the protocol inspircd is built for, e.g.

use ipv6 reverse lookup when built for ipv6, or ipv4 lookup when built for ipv4.

Definition at line 515 of file dns.cpp.

References AddQuery(), DNS_QUERY_PTR, irc::sockets::insp_ntoa(), MakeIP6Int(), DNSHeader::payload, and DNSRequest::SendRequests().

Referenced by Resolver::Resolver().

int DNS::GetNameForce const char *  ip,
ForceProtocol  fp
 

Start lookup of a hostname from an ip, but force a specific protocol to be used for the lookup for example to perform an ipv6 reverse lookup.

Definition at line 547 of file dns.cpp.

References AddQuery(), DNS_QUERY_PTR, MakeIP6Int(), DNSHeader::payload, PROTOCOL_IPV6, and DNSRequest::SendRequests().

Referenced by Resolver::Resolver().

DNSResult DNS::GetResult int  resultnum  ) 
 

Fetch the result string (an ip or host) and/or an error message to go with it.

Parameters:
resultnum Result number to fetch

Definition at line 613 of file dns.cpp.

References InspIRCd::Config, DNS_QUERY_A, DNS_QUERY_AAAA, DNS_QUERY_CNAME, DNS_QUERY_PTR, ServerConfig::DNSServer, ERROR_MASK, FillHeader(), DNSHeader::id, ip6munge, MAXBUF, DNSRequest::orig, QUERY_PORT, SocketEngine::RecvFrom(), requests, DNSRequest::ResultIsReady(), InspIRCd::SE, ServerInstance, socketfamily, DNSRequest::ttl, and DNSRequest::type.

Referenced by HandleEvent().

void DNS::HandleEvent EventType  et,
int  errornum = 0
[virtual]
 

Handle a SocketEngine read event Inherited from EventHandler.

Implements EventHandler.

Definition at line 1056 of file dns.cpp.

References cache, Classes, DEBUG, ERROR_MASK, GetResult(), DNSResult::id, LogManager::Log(), InspIRCd::Logs, Resolver::OnError(), Resolver::OnLookupComplete(), DNSResult::original, RESOLVER_NXDOMAIN, DNSResult::result, ServerInstance, InspIRCd::stats, serverstats::statsDns, serverstats::statsDnsBad, serverstats::statsDnsGood, and DNSResult::ttl.

void DNS::MakeIP6Int char *  query,
const in6_addr *  ip
[static]
 

Turn an in6_addr into a .ip6.arpa domain.

Definition at line 592 of file dns.cpp.

References irc::hex().

Referenced by GetName(), and GetNameForce().

int DNS::MakePayload const char *  name,
const QueryType  rr,
const unsigned short  rr_class,
unsigned char *  payload
[private]
 

Build a dns packet payload.

Definition at line 424 of file dns.cpp.

unsigned long DNS::PRNG  ) 
 

Portable random number generator, generates its random number from the ircd stats counters, effective user id, time of day and the rollover counter (currid).

Definition at line 1161 of file dns.cpp.

References InspIRCd::Config, currid, ServerConfig::ports, ServerInstance, InspIRCd::stats, serverstats::statsAccept, serverstats::statsCollisions, serverstats::statsConnects, serverstats::statsDnsBad, serverstats::statsDnsGood, serverstats::statsRecv, and serverstats::statsSent.

Referenced by AddQuery().

int DNS::PruneCache  ) 
 

Prune the DNS cache, e.g.

remove all expired items and rehash the cache buckets, but leave items in the hash which are still valid.

Definition at line 290 of file dns.cpp.

References cache.

Referenced by Rehash(), and CacheTimer::Tick().

void DNS::Rehash  ) 
 

Re-initialize the DNS subsystem.

Definition at line 306 of file dns.cpp.

References SocketEngine::AddFd(), InspIRCd::BindSocket(), cache, SocketEngine::Close(), InspIRCd::Config, DEBUG, DEFAULT, SocketEngine::DelFd(), ServerConfig::DNSServer, ip6munge, LogManager::Log(), InspIRCd::Logs, SocketEngine::NonBlocking(), irc::sockets::OpenTCPSocket(), PruneCache(), InspIRCd::SE, ServerInstance, EventHandler::SetFd(), SocketEngine::Shutdown(), and socketfamily.

Referenced by DNS(), and InspIRCd::Run().


Member Data Documentation

dnscache* DNS::cache [private]
 

Currently cached items.

Definition at line 360 of file dns.h.

Referenced by ClearCache(), DelCache(), DNS(), GetCache(), HandleEvent(), PruneCache(), and Rehash().

Resolver* DNS::Classes[MAX_REQUEST_ID]
 

Currently active Resolver classes.

Definition at line 393 of file dns.h.

Referenced by AddResolverClass(), CleanResolvers(), DNS(), HandleEvent(), and RequestTimeout::Tick().

int DNS::currid [private]
 

A counter used to form part of the pseudo-random id.

Definition at line 347 of file dns.h.

Referenced by DNS(), and PRNG().

bool DNS::ip6munge [private]
 

We have to turn off a few checks on received packets when people are using 4in6 (e.g.

ffff:xxxx). This is a temporary kludge, Please let me know if you know how to fix it.

Definition at line 355 of file dns.h.

Referenced by GetResult(), and Rehash().

const int DNS::MAX_REQUEST_ID = 0xFFFF [static, private]
 

The maximum value of a dns request id, 16 bits wide, 0xFFFF.

Definition at line 342 of file dns.h.

Referenced by AddQuery(), and CleanResolvers().

in_addr DNS::myserver4
 

IPV4 server address.

Definition at line 388 of file dns.h.

Referenced by DNSRequest::SendRequests().

class CacheTimer* DNS::PruneTimer [private]
 

A timer which ticks every hour to remove expired items from the DNS cache.

Definition at line 365 of file dns.h.

Referenced by DNS().

const int DNS::QUERY_PORT = 53 [static]
 

The port number DNS requests are made on, and replies have as a source-port number.

Definition at line 404 of file dns.h.

Referenced by GetResult(), and DNSRequest::SendRequests().

DNSRequest* DNS::requests[MAX_REQUEST_ID]
 

Requests that are currently 'in flight'.

Definition at line 398 of file dns.h.

Referenced by AddQuery(), DNS(), GetResult(), and RequestTimeout::Tick().

InspIRCd* DNS::ServerInstance [private]
 

Creator/Owner object.

Definition at line 336 of file dns.h.

Referenced by AddQuery(), AddResolverClass(), DNS(), GetResult(), HandleEvent(), PRNG(), Rehash(), and ~DNS().

int DNS::socketfamily
 

Server address being used currently.

Definition at line 377 of file dns.h.

Referenced by GetResult(), and Rehash().


The documentation for this class was generated from the following files: