PTLib Version 2.12.9

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

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: 28762 $
27 * $Author: rjongbloed $
28 * $Date: 2013-01-04 10:51:04 +1100 (Fri, 04 Jan 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) && (P_MACOSX >= 700)
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];
115 WORD wPreference;
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;
141 } DNS_RECORD_FLAGS;
142
143 typedef enum _DnsSection
144 {
145 DnsSectionQuestion,
146 DnsSectionAnswer,
147 DnsSectionAuthority,
148 DnsSectionAdditional,
149 } DNS_SECTION;
150
151
152 class DnsRecord {
153 public:
154 DnsRecord * pNext;
155 char pName[MAXDNAME];
156 WORD wType;
157 WORD wDataLength;
158
159 union {
160 DWORD DW;
161 DNS_RECORD_FLAGS S;
162 } Flags;
163
164 union {
165 DNS_A_DATA A;
166 DNS_AAAA_DATA AAAA;
167 DNS_MX_DATA MX;
168 DNS_PTR_DATA NS;
169 DNS_SRV_DATA SRV;
170 DNS_NULL_DATA Null;
171 } Data;
172 };
173
174 typedef DnsRecord DNS_RECORD;
175 typedef DnsRecord * PDNS_RECORD;
176
177
178 extern void DnsRecordListFree(PDNS_RECORD rec, int FreeType);
179 extern PDNS_RECORD DnsRecordSetCopy(PDNS_RECORD src);
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
195 DNS_STATUS Cached_DnsQuery(
196 const char * name,
197 WORD type,
198 DWORD options,
199 void * extra,
200 PDNS_RECORD * queryResults,
201 void * reserved
202 );
203
204
205
206 class PDnsRecords
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:
216 PDNS_RECORD m_records;
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,
237 DNS_QUERY_STANDARD,
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:
262 SRVRecord()
263 { used = false; }
264
265 Comparison Compare(const PObject & obj) const;
266 void PrintOn(ostream & strm) const;
267
268 PString hostName;
269 PIPSocket::Address hostAddress;
270 PBoolean used;
271 WORD port;
272 WORD priority;
273 WORD weight;
274 };
275
276 PDECLARE_SORTED_LIST(SRVRecordList, PDNS::SRVRecord)
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;
287 PWORDArray priList;
288 };
289
294 inline PBoolean GetRecords(const PString & service, SRVRecordList & serviceList)
295 { return Lookup<DNS_TYPE_SRV, SRVRecordList, SRVRecord>(service, serviceList); }
296
300 inline PBoolean GetSRVRecords(
301 const PString & service,
302 SRVRecordList & serviceList
303 )
304 { return GetRecords(service, serviceList); }
305
310 PBoolean GetSRVRecords(
311 const PString & service,
312 const PString & type,
313 const PString & domain,
314 SRVRecordList & serviceList
315 );
316
322 PBoolean LookupSRV(
323 const PString & srvQuery,
324 WORD defaultPort,
325 PIPSocketAddressAndPortVector & addrList
326 );
327
328 PBoolean LookupSRV(
329 const PString & domain,
330 const PString & service,
331 WORD defaultPort,
332 PIPSocketAddressAndPortVector & addrList
333 );
334
335 #if P_URL
336 PBoolean LookupSRV(
337 const PURL & url,
338 const PString & service,
339 PStringList & returnStr
340 );
341 #endif
342
344
345 class MXRecord : public PObject
346 {
347 PCLASSINFO(MXRecord, PObject);
348 public:
349 MXRecord()
350 { used = false; }
351 Comparison Compare(const PObject & obj) const;
352 void PrintOn(ostream & strm) const;
353
354 PString hostName;
355 PIPSocket::Address hostAddress;
356 PBoolean used;
357 WORD preference;
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
376 inline PBoolean GetRecords(
377 const PString & domain,
378 MXRecordList & serviceList
379 )
380 { return Lookup<DNS_TYPE_MX, MXRecordList, MXRecord>(domain, serviceList); }
381
385 inline PBoolean GetMXRecords(
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 ///////////////////////////////////////////////////////////////