PTLib  Version 2.18.8
 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 
27 #ifndef PTLIB_PDNS_H
28 #define PTLIB_PDNS_H
29 
30 #if P_DNS_RESOLVER
31 
32 #ifdef P_USE_PRAGMA
33 #pragma interface
34 #endif
35 
36 #include <ptlib/sockets.h>
37 
38 #include <ptclib/random.h>
39 #include <ptclib/url.h>
40 
41 #if defined(_WIN32)
42 
43  #include <windns.h>
44  #include <ntverp.h>
45 
46  // Accommodate spelling error in windns.h
47  enum { DnsSectionAdditional = DnsSectionAddtional };
48 
49  #if VER_PRODUCTBUILD < 6000
50  typedef struct
51  {
52  WORD wOrder;
53  WORD wPreference;
54  PSTR pFlags;
55  PSTR pService;
56  PSTR pRegularExpression;
57  PSTR pReplacement;
58  }
59  DNS_NAPTR_DATA;
60  #endif
61 
62 #else /* _WIN32 */
63 
64  #define P_HAS_RESOLV_H 1 // set if using Unix-style DNS routines
65  #include <arpa/nameser.h>
66  #include <resolv.h>
67  #if defined(P_MACOSX) || defined(P_IOS)
68  #include <arpa/nameser_compat.h>
69  #endif
70 
71 #endif // _WIN32
72 
73 
74 #ifdef P_HAS_RESOLV_H
75 
77 //
78 // these classes provide an emulation of the Microsoft DNS API
79 // on non-Window systems
80 //
81 
82 #ifndef T_SRV
83 #define T_SRV 33
84 #endif
85 
86 #ifndef T_NAPTR
87 #define T_NAPTR 35
88 #endif
89 
90 
91 #define DNS_STATUS int
92 #define DNS_TYPE_SRV T_SRV
93 #define DNS_TYPE_MX T_MX
94 #define DNS_TYPE_A T_A
95 #define DNS_TYPE_AAAA T_AAAA
96 #define DNS_TYPE_NAPTR T_NAPTR
97 #define DnsFreeRecordList 1
98 #define DNS_QUERY_STANDARD 0
99 #define DNS_QUERY_BYPASS_CACHE 0
100 
101 typedef struct _DnsAData {
102  DWORD IpAddress;
103 } DNS_A_DATA;
104 
105 typedef struct _DnsAAAAData {
106  DWORD Ip6Address[4];
107 } DNS_AAAA_DATA;
108 
109 typedef struct {
110  char pNameExchange[MAXDNAME];
112 } DNS_MX_DATA;
113 
114 typedef struct {
115  char pNameHost[MAXDNAME];
116 } DNS_PTR_DATA;
117 
118 typedef struct _DnsSRVData {
119  char pNameTarget[MAXDNAME];
120  WORD wPriority;
121  WORD wWeight;
122  WORD wPort;
123 } DNS_SRV_DATA;
124 
125 typedef struct _DnsNULLData {
126  DWORD dwByteCount;
127  char data[1];
128 } DNS_NULL_DATA;
129 
130 typedef struct _DnsRecordFlags
131 {
132  unsigned Section : 2;
133  unsigned Delete : 1;
134  unsigned CharSet : 2;
135  unsigned Unused : 3;
136  unsigned Reserved : 24;
138 
139 typedef enum _DnsSection
140 {
145 } DNS_SECTION;
146 
147 
148 class DnsRecord {
149  public:
151  char pName[MAXDNAME];
152  WORD wType;
154 
155  union {
156  DWORD DW;
158  } Flags;
159 
160  union {
167  } Data;
168 };
169 
172 
173 
174 extern void DnsRecordListFree(PDNS_RECORD rec, int FreeType);
176 
177 extern DNS_STATUS DnsQuery_A(const char * service,
178  WORD requestType,
179  DWORD options,
180  void *,
181  PDNS_RECORD * results,
182  void *);
183 
184 
185 #endif // P_HAS_RESOLV_H
186 
187 namespace PDNS {
188 
190 
192  const char * name,
193  WORD type,
194  DWORD options,
195  void * extra,
196  PDNS_RECORD * queryResults,
197  void * reserved
198 );
199 
200 
201 
203 {
204  public:
205  PDnsRecords() : m_records(NULL) { }
206  ~PDnsRecords();
207 
208  operator PDNS_RECORD() { return m_records; }
209  operator PDNS_RECORD*() { return &m_records; }
210 
211  protected:
213 };
214 
215 
217 //
218 // this template automates the creation of a list of records for
219 // a specific type of DNS lookup
220 //
221 
222 template <unsigned type, class RecordListType, class RecordType>
223 PBoolean Lookup(const PString & name, RecordListType & recordList)
224 {
225  if (name.IsEmpty())
226  return false;
227 
228  recordList.RemoveAll();
229 
230  PDnsRecords results;
231  DNS_STATUS status = Cached_DnsQuery((const char *)name,
232  type,
234  NULL,
235  results,
236  NULL);
237  if (status != 0)
238  return false;
239 
240  // find records matching the correct type
241  PDNS_RECORD dnsRecord = results;
242  while (dnsRecord != NULL) {
243  RecordType * record = recordList.HandleDNSRecord(dnsRecord, results);
244  if (record != NULL)
245  recordList.Append(record);
246  dnsRecord = dnsRecord->pNext;
247  }
248 
249  return recordList.GetSize() != 0;
250 }
251 
253 
254 class SRVRecord : public PObject
255 {
256  PCLASSINFO(SRVRecord, PObject);
257  public:
259  { used = false; }
260 
261  Comparison Compare(const PObject & obj) const;
262  void PrintOn(ostream & strm) const;
263 
267  WORD port;
268  WORD priority;
269  WORD weight;
270 };
271 
273  public:
274  void PrintOn(ostream & strm) const;
275 
276  SRVRecord * GetFirst();
277  SRVRecord * GetNext();
278 
279  PDNS::SRVRecord * HandleDNSRecord(PDNS_RECORD dnsRecord, PDNS_RECORD results);
280 
281  protected:
282  PINDEX priPos;
284 };
285 
290 inline PBoolean GetRecords(const PString & service, SRVRecordList & serviceList)
291 { return Lookup<DNS_TYPE_SRV, SRVRecordList, SRVRecord>(service, serviceList); }
292 
297  const PString & service,
298  SRVRecordList & serviceList
299 )
300 { return GetRecords(service, serviceList); }
301 
307  const PString & service,
308  const PString & type,
309  const PString & domain,
310  SRVRecordList & serviceList
311 );
312 
319  const PString & srvQuery,
320  WORD defaultPort,
322 );
323 
325  const PString & domain,
326  const PString & service,
327  WORD defaultPort,
329 );
330 
331 #if P_URL
333  const PURL & url,
334  const PString & service,
335  PStringList & returnStr
336 );
337 #endif
338 
340 
341 class MXRecord : public PObject
342 {
344  public:
346  { used = false; }
347  Comparison Compare(const PObject & obj) const;
348  void PrintOn(ostream & strm) const;
349 
354 };
355 
356 PDECLARE_SORTED_LIST(MXRecordList, PDNS::MXRecord)
357  public:
358  void PrintOn(ostream & strm) const;
359 
360  MXRecord * GetFirst();
361  MXRecord * GetNext();
362 
363  PDNS::MXRecord * HandleDNSRecord(PDNS_RECORD dnsRecord, PDNS_RECORD results);
364 
365  protected:
366  PINDEX lastIndex;
367 };
368 
373  const PString & domain,
374  MXRecordList & serviceList
375 )
376 { return Lookup<DNS_TYPE_MX, MXRecordList, MXRecord>(domain, serviceList); }
377 
382  const PString & domain,
383  MXRecordList & serviceList
384 )
385 {
386  return GetRecords(domain, serviceList);
387 }
388 
389 
390 }; // namespace PDNS
391 
392 #endif // P_DNS_RESOLVER
393 
394 #endif // PTLIB_PDNS_H
395 
396 
397 // End Of File ///////////////////////////////////////////////////////////////
#define DNS_STATUS
Definition: pdns.h:91
PDNS_RECORD DnsRecordSetCopy(PDNS_RECORD src)
Definition: pdns.h:141
DNS_AAAA_DATA AAAA
Definition: pdns.h:162
DnsRecord * PDNS_RECORD
Definition: pdns.h:171
PWORDArray priList
Definition: pdns.h:283
PINDEX lastIndex
Definition: pdns.h:366
WORD priority
Definition: pdns.h:268
WORD weight
Definition: pdns.h:269
DNS_PTR_DATA NS
Definition: pdns.h:164
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:2164
PINDEX priPos
Definition: pdns.h:282
Definition: pdns.h:125
#define DNS_QUERY_STANDARD
Definition: pdns.h:98
PBoolean Lookup(const PString &name, RecordListType &recordList)
Definition: pdns.h:223
Definition: pdns.h:143
DNS_RECORD_FLAGS S
flags as structure
Definition: pdns.h:157
DNS_MX_DATA MX
Definition: pdns.h:163
WORD wType
Definition: pdns.h:152
DNS_STATUS DnsQuery_A(const char *service, WORD requestType, DWORD options, void *, PDNS_RECORD *results, void *)
Definition: pdns.h:109
SRVRecord()
Definition: pdns.h:258
Comparison
Result of the comparison operation performed by the Compare() function.
Definition: object.h:2251
WORD wPriority
Definition: pdns.h:120
PString hostName
Definition: pdns.h:350
Definition: pdns.h:202
Definition: pdns.h:254
Definition: pdns.h:144
Definition: pdns.h:118
DWORD DW
flags as DWORD
Definition: pdns.h:156
PString hostName
Definition: pdns.h:264
PDNS_RECORD m_records
Definition: pdns.h:212
WORD wWeight
Definition: pdns.h:121
virtual PBoolean IsEmpty() const
Determine if the string is empty.
PBoolean used
Definition: pdns.h:352
MXRecord()
Definition: pdns.h:345
void PrintOn(ostream &strm) const
Output the contents of the object to the stream.
WORD port
Definition: pdns.h:267
WORD preference
Definition: pdns.h:353
union DnsRecord::@23 Data
DWORD IpAddress
Definition: pdns.h:102
Definition: pdns.h:114
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:170
DNS_A_DATA A
Definition: pdns.h:161
bool PBoolean
Definition: object.h:174
WORD wPreference
Definition: pdns.h:111
Comparison Compare(const PObject &obj) const
Compare the two objects and return their relative rank.
Definition: pdns.h:101
PBoolean GetMXRecords(const PString &domain, MXRecordList &serviceList)
provided for backwards compatibility
Definition: pdns.h:381
The character string class.
Definition: pstring.h:108
Definition: pdns.h:148
PIPSocket::Address hostAddress
Definition: pdns.h:265
DNS_SECTION
Definition: pdns.h:139
WORD wPort
Definition: pdns.h:122
PIPSocket::Address hostAddress
Definition: pdns.h:351
DWORD dwByteCount
Definition: pdns.h:126
DnsRecord * pNext
Definition: pdns.h:150
Definition: pdns.h:142
NAPTRRecord * GetNext(const char *service=NULL)
void PrintOn(ostream &strm) const
char pName[MAXDNAME]
Definition: pdns.h:151
PBoolean GetSRVRecords(const PString &service, SRVRecordList &serviceList)
provided for backwards compatibility
Definition: pdns.h:296
This is a list collection class of PString objects.
Definition: pstring.h:2562
std::vector< PIPSocket::AddressAndPort > PIPSocketAddressAndPortVector
Definition: ipsock.h:957
union DnsRecord::@22 Flags
A class describing an IP address.
Definition: ipsock.h:59
PDnsRecords()
Definition: pdns.h:205
DNS_SRV_DATA SRV
Definition: pdns.h:165
Definition: pdns.h:105
PBoolean used
Definition: pdns.h:266
NAPTRRecord * GetFirst(const char *service=NULL)
Definition: pdns.h:130
This template class maps the PAbstractArray to a specific element type.
Definition: array.h:504
PDNS::NAPTRRecord * HandleDNSRecord(PDNS_RECORD dnsRecord, PDNS_RECORD results)
PBoolean LookupSRV(const PString &srvQuery, WORD defaultPort, PIPSocketAddressAndPortVector &addrList)
Perform a DNS lookup of the specified service.
Definition: pdns.h:341
PBoolean GetRecords(const PString &domain, NAPTRRecordList &recordList)
Definition: enum.h:79
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
This class describes a Universal Resource Locator.
Definition: url.h:56
WORD wDataLength
Definition: pdns.h:153
DNS_NULL_DATA Null
Definition: pdns.h:166
#define PDECLARE_SORTED_LIST(cls, T)
Begin declaration of a sorted list class.
Definition: lists.h:1131