All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pdns.h
Go to the documentation of this file.
/*
* pdns.h
*
* PWLib library for DNS lookup services
*
* Portable Windows Library
*
* Copyright (c) 2003 Equivalence Pty. Ltd.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Portable Windows Library.
*
* The Initial Developer of the Original Code is Equivalence Pty. Ltd.
*
* Contributor(s): ______________________________________.
*
* $Revision: 28568 $
* $Author: rjongbloed $
* $Date: 2012-11-22 00:16:50 -0600 (Thu, 22 Nov 2012) $
*/
#ifndef PTLIB_PDNS_H
#define PTLIB_PDNS_H
#if P_DNS
#ifdef P_USE_PRAGMA
#pragma interface
#endif
#include <ptlib/sockets.h>
#include <ptclib/random.h>
#include <ptclib/url.h>
#if defined(_WIN32)
#include <windns.h>
#include <ntverp.h>
// Accommodate spelling error in windns.h
enum { DnsSectionAdditional = DnsSectionAddtional };
#if VER_PRODUCTBUILD < 6000
typedef struct
{
WORD wOrder;
WORD wPreference;
PSTR pFlags;
PSTR pService;
PSTR pRegularExpression;
PSTR pReplacement;
}
DNS_NAPTR_DATA;
#endif
#else /* _WIN32 */
#define P_HAS_RESOLVER 1 // set if using Unix-style DNS routines
#include <arpa/nameser.h>
#include <resolv.h>
#if defined(P_MACOSX) && (P_MACOSX >= 700)
#include <arpa/nameser_compat.h>
#endif
#endif // _WIN32
#ifdef P_HAS_RESOLVER
//
// these classes provide an emulation of the Microsoft DNS API
// on non-Window systems
//
#ifndef T_SRV
#define T_SRV 33
#endif
#ifndef T_NAPTR
#define T_NAPTR 35
#endif
#define DNS_STATUS int
#define DNS_TYPE_SRV T_SRV
#define DNS_TYPE_MX T_MX
#define DNS_TYPE_A T_A
#define DNS_TYPE_AAAA T_AAAA
#define DNS_TYPE_NAPTR T_NAPTR
#define DnsFreeRecordList 1
#define DNS_QUERY_STANDARD 0
#define DNS_QUERY_BYPASS_CACHE 0
typedef struct _DnsAData {
DWORD IpAddress;
} DNS_A_DATA;
typedef struct _DnsAAAAData {
DWORD Ip6Address[4];
} DNS_AAAA_DATA;
typedef struct {
char pNameExchange[MAXDNAME];
WORD wPreference;
} DNS_MX_DATA;
typedef struct {
char pNameHost[MAXDNAME];
} DNS_PTR_DATA;
typedef struct _DnsSRVData {
char pNameTarget[MAXDNAME];
WORD wPriority;
WORD wWeight;
WORD wPort;
} DNS_SRV_DATA;
typedef struct _DnsNULLData {
DWORD dwByteCount;
char data[1];
} DNS_NULL_DATA;
typedef struct _DnsRecordFlags
{
unsigned Section : 2;
unsigned Delete : 1;
unsigned CharSet : 2;
unsigned Unused : 3;
unsigned Reserved : 24;
} DNS_RECORD_FLAGS;
typedef enum _DnsSection
{
DnsSectionQuestion,
DnsSectionAnswer,
DnsSectionAuthority,
DnsSectionAdditional,
} DNS_SECTION;
class DnsRecord {
public:
DnsRecord * pNext;
char pName[MAXDNAME];
WORD wType;
WORD wDataLength;
union {
DWORD DW;
DNS_RECORD_FLAGS S;
} Flags;
union {
DNS_A_DATA A;
DNS_AAAA_DATA AAAA;
DNS_MX_DATA MX;
DNS_PTR_DATA NS;
DNS_SRV_DATA SRV;
DNS_NULL_DATA Null;
} Data;
};
typedef DnsRecord DNS_RECORD;
typedef DnsRecord * PDNS_RECORD;
extern void DnsRecordListFree(PDNS_RECORD rec, int FreeType);
extern PDNS_RECORD DnsRecordSetCopy(PDNS_RECORD src);
extern DNS_STATUS DnsQuery_A(const char * service,
WORD requestType,
DWORD options,
void *,
PDNS_RECORD * results,
void *);
#endif // P_HAS_RESOLVER
namespace PDNS {
DNS_STATUS Cached_DnsQuery(
const char * name,
WORD type,
DWORD options,
void * extra,
PDNS_RECORD * queryResults,
void * reserved
);
//
// this template automates the creation of a list of records for
// a specific type of DNS lookup
//
template <unsigned type, class RecordListType, class RecordType>
PBoolean Lookup(const PString & name, RecordListType & recordList)
{
if (name.IsEmpty())
return false;
recordList.RemoveAll();
PDNS_RECORD results = NULL;
DNS_STATUS status = Cached_DnsQuery((const char *)name,
type,
DNS_QUERY_STANDARD,
NULL,
&results,
NULL);
if (status != 0)
return false;
// find records matching the correct type
PDNS_RECORD dnsRecord = results;
while (dnsRecord != NULL) {
RecordType * record = recordList.HandleDNSRecord(dnsRecord, results);
if (record != NULL)
recordList.Append(record);
dnsRecord = dnsRecord->pNext;
}
if (results != NULL)
DnsRecordListFree(results, DnsFreeRecordList);
return recordList.GetSize() != 0;
}
class SRVRecord : public PObject
{
PCLASSINFO(SRVRecord, PObject);
public:
SRVRecord()
{ used = false; }
Comparison Compare(const PObject & obj) const;
void PrintOn(ostream & strm) const;
PString hostName;
PIPSocket::Address hostAddress;
PBoolean used;
WORD port;
WORD priority;
WORD weight;
};
PDECLARE_SORTED_LIST(SRVRecordList, PDNS::SRVRecord)
public:
void PrintOn(ostream & strm) const;
SRVRecord * GetFirst();
SRVRecord * GetNext();
PDNS::SRVRecord * HandleDNSRecord(PDNS_RECORD dnsRecord, PDNS_RECORD results);
protected:
PINDEX priPos;
PWORDArray priList;
};
inline PBoolean GetRecords(const PString & service, SRVRecordList & serviceList)
{ return Lookup<DNS_TYPE_SRV, SRVRecordList, SRVRecord>(service, serviceList); }
inline PBoolean GetSRVRecords(
const PString & service,
SRVRecordList & serviceList
)
{ return GetRecords(service, serviceList); }
PBoolean GetSRVRecords(
const PString & service,
const PString & type,
const PString & domain,
SRVRecordList & serviceList
);
PBoolean LookupSRV(
const PString & srvQuery,
WORD defaultPort,
PIPSocketAddressAndPortVector & addrList
);
PBoolean LookupSRV(
const PString & domain,
const PString & service,
WORD defaultPort,
PIPSocketAddressAndPortVector & addrList
);
PBoolean LookupSRV(
const PURL & url,
const PString & service,
PStringList & returnStr
);
class MXRecord : public PObject
{
PCLASSINFO(MXRecord, PObject);
public:
MXRecord()
{ used = false; }
Comparison Compare(const PObject & obj) const;
void PrintOn(ostream & strm) const;
PString hostName;
PIPSocket::Address hostAddress;
PBoolean used;
WORD preference;
};
PDECLARE_SORTED_LIST(MXRecordList, PDNS::MXRecord)
public:
void PrintOn(ostream & strm) const;
MXRecord * GetFirst();
MXRecord * GetNext();
PDNS::MXRecord * HandleDNSRecord(PDNS_RECORD dnsRecord, PDNS_RECORD results);
protected:
PINDEX lastIndex;
};
inline PBoolean GetRecords(
const PString & domain,
MXRecordList & serviceList
)
{ return Lookup<DNS_TYPE_MX, MXRecordList, MXRecord>(domain, serviceList); }
inline PBoolean GetMXRecords(
const PString & domain,
MXRecordList & serviceList
)
{
return GetRecords(domain, serviceList);
}
}; // namespace PDNS
#endif // P_DNS
#endif // PTLIB_PDNS_H
// End Of File ///////////////////////////////////////////////////////////////
DNS_STATUS
#define DNS_STATUS
Definition: pdns.h:95
DnsRecordSetCopy
PDNS_RECORD DnsRecordSetCopy(PDNS_RECORD src)
DnsSectionQuestion
Definition: pdns.h:145
DnsRecord::AAAA
DNS_AAAA_DATA AAAA
Definition: pdns.h:166
PDNS_RECORD
DnsRecord * PDNS_RECORD
Definition: pdns.h:175
PDNS::priList
PWORDArray priList
Definition: pdns.h:276
lastIndex
PINDEX lastIndex
Definition: pdns.h:357
PDNS::SRVRecord::priority
WORD priority
Definition: pdns.h:261
PDNS::SRVRecord::weight
WORD weight
Definition: pdns.h:262
DnsRecord::NS
DNS_PTR_DATA NS
Definition: pdns.h:168
PCLASSINFO
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:1049
PDNS::priPos
PINDEX priPos
Definition: pdns.h:275
DNS_NULL_DATA
Definition: pdns.h:129
DNS_QUERY_STANDARD
#define DNS_QUERY_STANDARD
Definition: pdns.h:102
DnsRecord::Data
union DnsRecord::@25 Data
PDNS::Lookup
PBoolean Lookup(const PString &name, RecordListType &recordList)
Definition: pdns.h:213
DnsSectionAuthority
Definition: pdns.h:147
DnsRecord::S
DNS_RECORD_FLAGS S
flags as structure
Definition: pdns.h:161
DnsRecord::MX
DNS_MX_DATA MX
Definition: pdns.h:167
DnsRecord::wType
WORD wType
Definition: pdns.h:156
DnsQuery_A
DNS_STATUS DnsQuery_A(const char *service, WORD requestType, DWORD options, void *, PDNS_RECORD *results, void *)
DNS_MX_DATA
Definition: pdns.h:113
PDNS::SRVRecord::SRVRecord
SRVRecord()
Definition: pdns.h:251
PObject::Comparison
Comparison
Result of the comparison operation performed by the Compare() function.
Definition: object.h:1184
DNS_SRV_DATA::wPriority
WORD wPriority
Definition: pdns.h:124
MXRecord::hostName
PString hostName
Definition: pdns.h:341
PDNS::SRVRecord
Definition: pdns.h:247
DnsSectionAdditional
Definition: pdns.h:148
DNS_SRV_DATA
Definition: pdns.h:122
DnsRecord::DW
DWORD DW
flags as DWORD
Definition: pdns.h:160
PDNS::SRVRecord::hostName
PString hostName
Definition: pdns.h:257
DNS_SRV_DATA::wWeight
WORD wWeight
Definition: pdns.h:125
PString::IsEmpty
virtual PBoolean IsEmpty() const
Determine if the string is empty.
MXRecord::used
PBoolean used
Definition: pdns.h:343
MXRecord::MXRecord
MXRecord()
Definition: pdns.h:336
PBoolean
BOOL PBoolean
Definition: object.h:102
PDNS::SRVRecord::PrintOn
void PrintOn(ostream &strm) const
Output the contents of the object to the stream.
GetRecords
PBoolean GetRecords(const PString &service, SRVRecordList &serviceList)
return a list of DNS SRV record with the specified service type
Definition: pdns.h:283
PDNS::SRVRecord::port
WORD port
Definition: pdns.h:260
MXRecord::preference
WORD preference
Definition: pdns.h:344
PWORDArray
Array of unsigned short integers.
Definition: array.h:710
DnsRecord::Flags
union DnsRecord::@24 Flags
DNS_A_DATA::IpAddress
DWORD IpAddress
Definition: pdns.h:106
DNS_PTR_DATA
Definition: pdns.h:118
PDNS::Cached_DnsQuery
DNS_STATUS Cached_DnsQuery(const char *name, WORD type, DWORD options, void *extra, PDNS_RECORD *queryResults, void *reserved)
DnsRecordListFree
void DnsRecordListFree(PDNS_RECORD rec, int FreeType)
DNS_RECORD
DnsRecord DNS_RECORD
Definition: pdns.h:174
DnsRecord::A
DNS_A_DATA A
Definition: pdns.h:165
DNS_MX_DATA::wPreference
WORD wPreference
Definition: pdns.h:115
PDNS::SRVRecord::Compare
Comparison Compare(const PObject &obj) const
Compare the two objects and return their relative rank.
DNS_A_DATA
Definition: pdns.h:105
GetMXRecords
PBoolean GetMXRecords(const PString &domain, MXRecordList &serviceList)
provided for backwards compatibility
Definition: pdns.h:372
PString
The character string class.
Definition: pstring.h:108
DnsRecord
Definition: pdns.h:152
PIPSocketAddressAndPortVector
std::vector< PIPSocketAddressAndPort > PIPSocketAddressAndPortVector
Definition: ipsock.h:796
PDNS::SRVRecord::hostAddress
PIPSocket::Address hostAddress
Definition: pdns.h:258
DNS_SECTION
DNS_SECTION
Definition: pdns.h:143
DNS_SRV_DATA::wPort
WORD wPort
Definition: pdns.h:126
MXRecord::hostAddress
PIPSocket::Address hostAddress
Definition: pdns.h:342
DNS_NULL_DATA::dwByteCount
DWORD dwByteCount
Definition: pdns.h:130
DnsRecord::pNext
DnsRecord * pNext
Definition: pdns.h:154
DnsSectionAnswer
Definition: pdns.h:146
PDNS::GetNext
NAPTRRecord * GetNext(const char *service=NULL)
PDNS::PrintOn
void PrintOn(ostream &strm) const
DnsRecord::pName
char pName[MAXDNAME]
Definition: pdns.h:155
GetSRVRecords
PBoolean GetSRVRecords(const PString &service, SRVRecordList &serviceList)
provided for backwards compatibility
Definition: pdns.h:289
PStringList
This is a list collection class of PString objects.
Definition: pstring.h:2184
PIPSocket::Address
A class describing an IP address.
Definition: ipsock.h:75
DnsRecord::SRV
DNS_SRV_DATA SRV
Definition: pdns.h:169
DNS_AAAA_DATA
Definition: pdns.h:109
PDNS::SRVRecord::used
PBoolean used
Definition: pdns.h:259
PDNS::GetFirst
NAPTRRecord * GetFirst(const char *service=NULL)
DNS_RECORD_FLAGS
Definition: pdns.h:134
sockets.h
PDNS::HandleDNSRecord
PDNS::NAPTRRecord * HandleDNSRecord(PDNS_RECORD dnsRecord, PDNS_RECORD results)
random.h
DnsFreeRecordList
#define DnsFreeRecordList
Definition: pdns.h:101
LookupSRV
PBoolean LookupSRV(const PString &srvQuery, WORD defaultPort, PIPSocketAddressAndPortVector &addrList)
Perform a DNS lookup of the specified service.
MXRecord
Definition: pdns.h:332
url.h
PObject
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
PURL
This class describes a Universal Resource Locator.
Definition: url.h:54
DnsRecord::wDataLength
WORD wDataLength
Definition: pdns.h:157
DnsRecord::Null
DNS_NULL_DATA Null
Definition: pdns.h:170
PDECLARE_SORTED_LIST
#define PDECLARE_SORTED_LIST(cls, T)
Begin declaration of a sorted list class.
Definition: lists.h:1028