PTLib  Version 2.10.10
pdns.h
Go to the documentation of this file.
1 /*
2  * pdns.h
3  *
4  * PWLib library for DNS lookup services
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 2003 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Contributor(s): ______________________________________.
25  *
26  * $Revision: 28568 $
27  * $Author: rjongbloed $
28  * $Date: 2012-11-22 00:16:50 -0600 (Thu, 22 Nov 2012) $
29  */
30 
31 #ifndef PTLIB_PDNS_H
32 #define PTLIB_PDNS_H
33 
34 #if P_DNS
35 
36 #ifdef P_USE_PRAGMA
37 #pragma interface
38 #endif
39 
40 #include <ptlib/sockets.h>
41 
42 #include <ptclib/random.h>
43 #include <ptclib/url.h>
44 
45 #if defined(_WIN32)
46 
47  #include <windns.h>
48  #include <ntverp.h>
49 
50  // Accommodate spelling error in windns.h
51  enum { DnsSectionAdditional = DnsSectionAddtional };
52 
53  #if VER_PRODUCTBUILD < 6000
54  typedef struct
55  {
56  WORD wOrder;
57  WORD wPreference;
58  PSTR pFlags;
59  PSTR pService;
60  PSTR pRegularExpression;
61  PSTR pReplacement;
62  }
63  DNS_NAPTR_DATA;
64  #endif
65 
66 #else /* _WIN32 */
67 
68  #define P_HAS_RESOLVER 1 // set if using Unix-style DNS routines
69  #include <arpa/nameser.h>
70  #include <resolv.h>
71  #if defined(P_MACOSX) && (P_MACOSX >= 700)
72  #include <arpa/nameser_compat.h>
73  #endif
74 
75 #endif // _WIN32
76 
77 
78 #ifdef P_HAS_RESOLVER
79 
81 //
82 // these classes provide an emulation of the Microsoft DNS API
83 // on non-Window systems
84 //
85 
86 #ifndef T_SRV
87 #define T_SRV 33
88 #endif
89 
90 #ifndef T_NAPTR
91 #define T_NAPTR 35
92 #endif
93 
94 
95 #define DNS_STATUS int
96 #define DNS_TYPE_SRV T_SRV
97 #define DNS_TYPE_MX T_MX
98 #define DNS_TYPE_A T_A
99 #define DNS_TYPE_AAAA T_AAAA
100 #define DNS_TYPE_NAPTR T_NAPTR
101 #define DnsFreeRecordList 1
102 #define DNS_QUERY_STANDARD 0
103 #define DNS_QUERY_BYPASS_CACHE 0
104 
105 typedef struct _DnsAData {
106  DWORD IpAddress;
107 } DNS_A_DATA;
108 
109 typedef struct _DnsAAAAData {
110  DWORD Ip6Address[4];
111 } DNS_AAAA_DATA;
112 
113 typedef struct {
114  char pNameExchange[MAXDNAME];
116 } DNS_MX_DATA;
117 
118 typedef struct {
119  char pNameHost[MAXDNAME];
120 } DNS_PTR_DATA;
121 
122 typedef struct _DnsSRVData {
123  char pNameTarget[MAXDNAME];
124  WORD wPriority;
125  WORD wWeight;
126  WORD wPort;
127 } DNS_SRV_DATA;
128 
129 typedef struct _DnsNULLData {
130  DWORD dwByteCount;
131  char data[1];
132 } DNS_NULL_DATA;
133 
134 typedef struct _DnsRecordFlags
135 {
136  unsigned Section : 2;
137  unsigned Delete : 1;
138  unsigned CharSet : 2;
139  unsigned Unused : 3;
140  unsigned Reserved : 24;
142 
143 typedef enum _DnsSection
144 {
149 } DNS_SECTION;
150 
151 
152 class DnsRecord {
153  public:
155  char pName[MAXDNAME];
156  WORD wType;
158 
159  union {
160  DWORD DW;
162  } Flags;
163 
164  union {
171  } Data;
172 };
173 
176 
177 
178 extern void DnsRecordListFree(PDNS_RECORD rec, int FreeType);
180 
181 extern DNS_STATUS DnsQuery_A(const char * service,
182  WORD requestType,
183  DWORD options,
184  void *,
185  PDNS_RECORD * results,
186  void *);
187 
188 
189 #endif // P_HAS_RESOLVER
190 
191 namespace PDNS {
192 
194 
196  const char * name,
197  WORD type,
198  DWORD options,
199  void * extra,
200  PDNS_RECORD * queryResults,
201  void * reserved
202 );
203 
204 
205 
207 //
208 // this template automates the creation of a list of records for
209 // a specific type of DNS lookup
210 //
211 
212 template <unsigned type, class RecordListType, class RecordType>
213 PBoolean Lookup(const PString & name, RecordListType & recordList)
214 {
215  if (name.IsEmpty())
216  return false;
217 
218  recordList.RemoveAll();
219 
220  PDNS_RECORD results = NULL;
221  DNS_STATUS status = Cached_DnsQuery((const char *)name,
222  type,
224  NULL,
225  &results,
226  NULL);
227  if (status != 0)
228  return false;
229 
230  // find records matching the correct type
231  PDNS_RECORD dnsRecord = results;
232  while (dnsRecord != NULL) {
233  RecordType * record = recordList.HandleDNSRecord(dnsRecord, results);
234  if (record != NULL)
235  recordList.Append(record);
236  dnsRecord = dnsRecord->pNext;
237  }
238 
239  if (results != NULL)
241 
242  return recordList.GetSize() != 0;
243 }
244 
246 
247 class SRVRecord : public PObject
248 {
249  PCLASSINFO(SRVRecord, PObject);
250  public:
252  { used = false; }
253 
254  Comparison Compare(const PObject & obj) const;
255  void PrintOn(ostream & strm) const;
256 
260  WORD port;
261  WORD priority;
262  WORD weight;
263 };
264 
266  public:
267  void PrintOn(ostream & strm) const;
268 
269  SRVRecord * GetFirst();
270  SRVRecord * GetNext();
271 
272  PDNS::SRVRecord * HandleDNSRecord(PDNS_RECORD dnsRecord, PDNS_RECORD results);
273 
274  protected:
275  PINDEX priPos;
277 };
278 
283 inline PBoolean GetRecords(const PString & service, SRVRecordList & serviceList)
284 { return Lookup<DNS_TYPE_SRV, SRVRecordList, SRVRecord>(service, serviceList); }
285 
290  const PString & service,
291  SRVRecordList & serviceList
292 )
293 { return GetRecords(service, serviceList); }
294 
300  const PString & service,
301  const PString & type,
302  const PString & domain,
303  SRVRecordList & serviceList
304 );
305 
312  const PString & srvQuery,
313  WORD defaultPort,
315 );
316 
318  const PString & domain,
319  const PString & service,
320  WORD defaultPort,
322 );
323 
325  const PURL & url,
326  const PString & service,
327  PStringList & returnStr
328 );
329 
331 
332 class MXRecord : public PObject
333 {
335  public:
337  { used = false; }
338  Comparison Compare(const PObject & obj) const;
339  void PrintOn(ostream & strm) const;
340 
345 };
346 
347 PDECLARE_SORTED_LIST(MXRecordList, PDNS::MXRecord)
348  public:
349  void PrintOn(ostream & strm) const;
350 
351  MXRecord * GetFirst();
352  MXRecord * GetNext();
353 
354  PDNS::MXRecord * HandleDNSRecord(PDNS_RECORD dnsRecord, PDNS_RECORD results);
355 
356  protected:
357  PINDEX lastIndex;
358 };
359 
364  const PString & domain,
365  MXRecordList & serviceList
366 )
367 { return Lookup<DNS_TYPE_MX, MXRecordList, MXRecord>(domain, serviceList); }
368 
373  const PString & domain,
374  MXRecordList & serviceList
375 )
376 {
377  return GetRecords(domain, serviceList);
378 }
379 
380 
381 }; // namespace PDNS
382 
383 #endif // P_DNS
384 
385 #endif // PTLIB_PDNS_H
386 
387 
388 // End Of File ///////////////////////////////////////////////////////////////
#define DNS_STATUS
Definition: pdns.h:95
PDNS_RECORD DnsRecordSetCopy(PDNS_RECORD src)
Definition: pdns.h:145
DNS_AAAA_DATA AAAA
Definition: pdns.h:166
DnsRecord * PDNS_RECORD
Definition: pdns.h:175
PWORDArray priList
Definition: pdns.h:276
PINDEX lastIndex
Definition: pdns.h:357
WORD priority
Definition: pdns.h:261
WORD weight
Definition: pdns.h:262
DNS_PTR_DATA NS
Definition: pdns.h:168
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:1049
PINDEX priPos
Definition: pdns.h:275
Definition: pdns.h:129
#define DNS_QUERY_STANDARD
Definition: pdns.h:102
union DnsRecord::@25 Data
PBoolean Lookup(const PString &name, RecordListType &recordList)
Definition: pdns.h:213
Definition: pdns.h:147
DNS_RECORD_FLAGS S
flags as structure
Definition: pdns.h:161
DNS_MX_DATA MX
Definition: pdns.h:167
WORD wType
Definition: pdns.h:156
DNS_STATUS DnsQuery_A(const char *service, WORD requestType, DWORD options, void *, PDNS_RECORD *results, void *)
Definition: pdns.h:113
SRVRecord()
Definition: pdns.h:251
Definition: enum.h:42
Comparison
Result of the comparison operation performed by the Compare() function.
Definition: object.h:1184
WORD wPriority
Definition: pdns.h:124
PString hostName
Definition: pdns.h:341
Definition: pdns.h:247
Definition: pdns.h:148
Definition: pdns.h:122
DWORD DW
flags as DWORD
Definition: pdns.h:160
PString hostName
Definition: pdns.h:257
WORD wWeight
Definition: pdns.h:125
virtual PBoolean IsEmpty() const
Determine if the string is empty.
PBoolean used
Definition: pdns.h:343
MXRecord()
Definition: pdns.h:336
BOOL PBoolean
Definition: object.h:102
void PrintOn(ostream &strm) const
Output the contents of the object to the stream.
PBoolean GetRecords(const PString &service, SRVRecordList &serviceList)
return a list of DNS SRV record with the specified service type
Definition: pdns.h:283
WORD port
Definition: pdns.h:260
WORD preference
Definition: pdns.h:344
Array of unsigned short integers.
Definition: array.h:710
union DnsRecord::@24 Flags
DWORD IpAddress
Definition: pdns.h:106
Definition: pdns.h:118
DNS_STATUS Cached_DnsQuery(const char *name, WORD type, DWORD options, void *extra, PDNS_RECORD *queryResults, void *reserved)
void DnsRecordListFree(PDNS_RECORD rec, int FreeType)
DnsRecord DNS_RECORD
Definition: pdns.h:174
DNS_A_DATA A
Definition: pdns.h:165
WORD wPreference
Definition: pdns.h:115
Comparison Compare(const PObject &obj) const
Compare the two objects and return their relative rank.
Definition: pdns.h:105
PBoolean GetMXRecords(const PString &domain, MXRecordList &serviceList)
provided for backwards compatibility
Definition: pdns.h:372
The character string class.
Definition: pstring.h:108
Definition: pdns.h:152
std::vector< PIPSocketAddressAndPort > PIPSocketAddressAndPortVector
Definition: ipsock.h:796
PIPSocket::Address hostAddress
Definition: pdns.h:258
DNS_SECTION
Definition: pdns.h:143
WORD wPort
Definition: pdns.h:126
PIPSocket::Address hostAddress
Definition: pdns.h:342
DWORD dwByteCount
Definition: pdns.h:130
DnsRecord * pNext
Definition: pdns.h:154
Definition: pdns.h:146
NAPTRRecord * GetNext(const char *service=NULL)
void PrintOn(ostream &strm) const
char pName[MAXDNAME]
Definition: pdns.h:155
PBoolean GetSRVRecords(const PString &service, SRVRecordList &serviceList)
provided for backwards compatibility
Definition: pdns.h:289
This is a list collection class of PString objects.
Definition: pstring.h:2184
A class describing an IP address.
Definition: ipsock.h:75
DNS_SRV_DATA SRV
Definition: pdns.h:169
Definition: pdns.h:109
PBoolean used
Definition: pdns.h:259
NAPTRRecord * GetFirst(const char *service=NULL)
Definition: pdns.h:134
PDNS::NAPTRRecord * HandleDNSRecord(PDNS_RECORD dnsRecord, PDNS_RECORD results)
#define DnsFreeRecordList
Definition: pdns.h:101
PBoolean LookupSRV(const PString &srvQuery, WORD defaultPort, PIPSocketAddressAndPortVector &addrList)
Perform a DNS lookup of the specified service.
Definition: pdns.h:332
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
This class describes a Universal Resource Locator.
Definition: url.h:54
WORD wDataLength
Definition: pdns.h:157
DNS_NULL_DATA Null
Definition: pdns.h:170
#define PDECLARE_SORTED_LIST(cls, T)
Begin declaration of a sorted list class.
Definition: lists.h:1028