PTLib  Version 2.14.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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: 29756 $
27  * $Author: rjongbloed $
28  * $Date: 2013-05-21 18:02:45 +1000 (Tue, 21 May 2013) $
29  */
30 
31 #ifndef PTLIB_PDNS_H
32 #define PTLIB_PDNS_H
33 
34 #if P_DNS_RESOLVER
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_RESOLV_H 1 // set if using Unix-style DNS routines
69  #include <arpa/nameser.h>
70  #include <resolv.h>
71  #if defined(P_MACOSX) || defined(P_IOS)
72  #include <arpa/nameser_compat.h>
73  #endif
74 
75 #endif // _WIN32
76 
77 
78 #ifdef P_HAS_RESOLV_H
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_RESOLV_H
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  public:
209  PDnsRecords() : m_records(NULL) { }
210  ~PDnsRecords();
211 
212  operator PDNS_RECORD() { return m_records; }
213  operator PDNS_RECORD*() { return &m_records; }
214 
215  protected:
217 };
218 
219 
221 //
222 // this template automates the creation of a list of records for
223 // a specific type of DNS lookup
224 //
225 
226 template <unsigned type, class RecordListType, class RecordType>
227 PBoolean Lookup(const PString & name, RecordListType & recordList)
228 {
229  if (name.IsEmpty())
230  return false;
231 
232  recordList.RemoveAll();
233 
234  PDnsRecords results;
235  DNS_STATUS status = Cached_DnsQuery((const char *)name,
236  type,
238  NULL,
239  results,
240  NULL);
241  if (status != 0)
242  return false;
243 
244  // find records matching the correct type
245  PDNS_RECORD dnsRecord = results;
246  while (dnsRecord != NULL) {
247  RecordType * record = recordList.HandleDNSRecord(dnsRecord, results);
248  if (record != NULL)
249  recordList.Append(record);
250  dnsRecord = dnsRecord->pNext;
251  }
252 
253  return recordList.GetSize() != 0;
254 }
255 
257 
258 class SRVRecord : public PObject
259 {
260  PCLASSINFO(SRVRecord, PObject);
261  public:
263  { used = false; }
264 
265  Comparison Compare(const PObject & obj) const;
266  void PrintOn(ostream & strm) const;
267 
271  WORD port;
272  WORD priority;
273  WORD weight;
274 };
275 
277  public:
278  void PrintOn(ostream & strm) const;
279 
280  SRVRecord * GetFirst();
281  SRVRecord * GetNext();
282 
283  PDNS::SRVRecord * HandleDNSRecord(PDNS_RECORD dnsRecord, PDNS_RECORD results);
284 
285  protected:
286  PINDEX priPos;
288 };
289 
294 inline PBoolean GetRecords(const PString & service, SRVRecordList & serviceList)
295 { return Lookup<DNS_TYPE_SRV, SRVRecordList, SRVRecord>(service, serviceList); }
296 
301  const PString & service,
302  SRVRecordList & serviceList
303 )
304 { return GetRecords(service, serviceList); }
305 
311  const PString & service,
312  const PString & type,
313  const PString & domain,
314  SRVRecordList & serviceList
315 );
316 
323  const PString & srvQuery,
324  WORD defaultPort,
326 );
327 
329  const PString & domain,
330  const PString & service,
331  WORD defaultPort,
333 );
334 
335 #if P_URL
337  const PURL & url,
338  const PString & service,
339  PStringList & returnStr
340 );
341 #endif
342 
344 
345 class MXRecord : public PObject
346 {
348  public:
350  { used = false; }
351  Comparison Compare(const PObject & obj) const;
352  void PrintOn(ostream & strm) const;
353 
358 };
359 
360 PDECLARE_SORTED_LIST(MXRecordList, PDNS::MXRecord)
361  public:
362  void PrintOn(ostream & strm) const;
363 
364  MXRecord * GetFirst();
365  MXRecord * GetNext();
366 
367  PDNS::MXRecord * HandleDNSRecord(PDNS_RECORD dnsRecord, PDNS_RECORD results);
368 
369  protected:
370  PINDEX lastIndex;
371 };
372 
377  const PString & domain,
378  MXRecordList & serviceList
379 )
380 { return Lookup<DNS_TYPE_MX, MXRecordList, MXRecord>(domain, serviceList); }
381 
386  const PString & domain,
387  MXRecordList & serviceList
388 )
389 {
390  return GetRecords(domain, serviceList);
391 }
392 
393 
394 }; // namespace PDNS
395 
396 #endif // P_DNS_RESOLVER
397 
398 #endif // PTLIB_PDNS_H
399 
400 
401 // End Of File ///////////////////////////////////////////////////////////////