Files

128 lines
3.4 KiB
C
Raw Permalink Normal View History

2013-08-02 13:12:24 -07:00
// Copyright Matt Wells Nov 2000
// . uses UDP only (we'll do the TCP part later)
// . derived from UdpServer
// . always sends lookup to fastest dns
// . periodically pings other servers
// . if our primary dns goes down it's speed will be changed in hostmap
// and we'll have a new primary
// . if a request times out we try the next dns
2016-03-08 22:14:30 +01:00
#ifndef GB_DNS_H
#define GB_DNS_H
2013-08-02 13:12:24 -07:00
#include "UdpServer.h"
#include "DnsProtocol.h"
#include "RdbCache.h"
#define MAX_DNS_IPS 32
struct DnsState;
2016-11-12 19:37:58 +01:00
class Host;
2013-08-02 13:12:24 -07:00
class Dns {
public:
Dns();
// reset the udp server and rdb cache
void reset();
// . we create our own udpServer in here since we can't share that
// because of the protocol differences
// . read our dns servers from the conf file
2016-11-12 16:51:54 +01:00
bool init(uint16_t clientPort);
2013-08-02 13:12:24 -07:00
// . check errno to on return or on callback to ensure no error
// . we set ip to 0 if not found
// . returns -1 and sets errno on error
// . returns 0 if transaction blocked, 1 if completed
2016-05-30 17:04:21 +02:00
bool getIp ( const char *hostname,
int32_t hostnameLen,
int32_t *ip,
void *state,
void (*callback)(void *state, int32_t ip))
{
return getIp(hostname,hostnameLen,ip,state,callback,
NULL, 60, false);
}
2013-08-02 13:12:24 -07:00
2016-11-12 16:51:54 +01:00
const UdpServer& getUdpServer() const { return m_udpServer; }
UdpServer& getUdpServer() { return m_udpServer; }
2016-05-19 18:37:26 +02:00
RdbCache *getCache () { return &m_rdbCache; }
RdbCache *getCacheLocal () { return &m_rdbCacheLocal; }
2013-08-02 13:12:24 -07:00
// returns true if in cache, and sets *ip
2016-11-12 16:51:54 +01:00
bool isInCache(key96_t key, int32_t *ip);
2013-08-02 13:12:24 -07:00
// add this hostnamekey/ip pair to the cache
2016-11-12 16:51:54 +01:00
void addToCache(key96_t hostnameKey, int32_t ip, int32_t ttl = -1);
2013-08-02 13:12:24 -07:00
// is it in the /etc/hosts file?
2016-11-12 16:51:54 +01:00
bool isInFile(key96_t key, int32_t *ip);
2013-08-02 13:12:24 -07:00
2016-11-12 16:51:54 +01:00
static key96_t getKey(const char *hostname, int32_t hostnameLen);
2013-08-02 13:12:24 -07:00
Host *getIPLookupHost(key96_t key);
2013-08-02 13:12:24 -07:00
2016-11-12 16:51:54 +01:00
private:
static void gotIpWrapper(void *state, UdpSlot *slot);
static void gotIpOfDNSWrapper(void *state , int32_t ip);
static void returnIp(DnsState *ds, int32_t ip);
2013-08-02 13:12:24 -07:00
2016-11-12 16:51:54 +01:00
bool loadFile();
2013-08-02 13:12:24 -07:00
2016-11-12 16:51:54 +01:00
bool sendToNextDNS(struct DnsState *ds);
bool getIpOfDNS(DnsState *ds);
// . pull the hostname out of a dns reply packet's query resource rec.
bool extractHostname(const char *dgram, const char *record, char *hostname);
bool getIp ( const char *hostname,
int32_t hostnameLen,
int32_t *ip,
void *state,
void (*callback)(void *state, int32_t ip),
DnsState *ds,
int32_t timeout,
bool dnsLookup);
2016-11-12 16:51:54 +01:00
// . returns the ip
// . called to parse the ip out of the reply in "slot"
// . must be public so gotIpWrapper() can call it
// . also update the timestamps in our private hostmap
// . returns -1 on error
// . returns 0 if ip does not exist
int32_t gotIp(UdpSlot *slot, struct DnsState *dnsState);
// . we have our own udp server
// . it contains our HostMap and DnsProtocol ptrs
// . keep public so gotIpWrapper() can use it to destroy the slot
UdpServer m_udpServer;
2013-08-02 13:12:24 -07:00
// . key is a hash of hostname
// . record/slot contains a 4 byte ip entry (if in key is in cache)
// . cache is shared with other dbs
RdbCache m_rdbCache;
RdbCache m_rdbCacheLocal;
DnsProtocol m_proto;
2014-11-10 14:45:11 -08:00
int16_t m_dnsClientPort;
2013-08-02 13:12:24 -07:00
// /etc/hosts in hashed into this table
2014-11-10 14:45:11 -08:00
int32_t *m_ips;
key96_t *m_keys;
2014-11-10 14:45:11 -08:00
int32_t m_numSlots;
2013-08-02 13:12:24 -07:00
};
//This stores the ip's for the machine where
//hash96(hostname) % g_hostdb.m_numHosts = cluster(group)
extern class Dns g_dns;
2016-03-08 22:14:30 +01:00
#endif // GB_DNS_H