]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSCore/DNSCommon.h
6a6ae706b0df81b439b0a52fb40fcc76dfc80c5d
[apple/mdnsresponder.git] / mDNSCore / DNSCommon.h
1 /*
2 * Copyright (c) 2002-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22
23 Change History (most recent first):
24
25 $Log: DNSCommon.h,v $
26 Revision 1.30 2005/01/19 03:12:44 cheshire
27 Move LocalRecordReady() macro from mDNS.c to DNSCommon.h
28
29 Revision 1.29 2004/12/15 02:11:22 ksekar
30 <rdar://problem/3917317> Don't check for Dynamic DNS hostname uniqueness
31
32 Revision 1.28 2004/12/06 21:15:22 ksekar
33 <rdar://problem/3884386> mDNSResponder crashed in CheckServiceRegistrations
34
35 Revision 1.27 2004/12/03 07:20:50 ksekar
36 <rdar://problem/3674208> Wide-Area: Registration of large TXT record fails
37
38 Revision 1.26 2004/12/03 05:18:33 ksekar
39 <rdar://problem/3810596> mDNSResponder needs to return more specific TSIG errors
40
41 Revision 1.25 2004/10/26 03:52:02 cheshire
42 Update checkin comments
43
44 Revision 1.24 2004/10/23 01:16:00 cheshire
45 <rdar://problem/3851677> uDNS operations not always reliable on multi-homed hosts
46
47 Revision 1.23 2004/10/03 23:18:58 cheshire
48 Move address comparison macros from DNSCommon.h to mDNSEmbeddedAPI.h
49
50 Revision 1.22 2004/09/30 00:24:56 ksekar
51 <rdar://problem/3695802> Dynamically update default registration domains on config change
52
53 Revision 1.21 2004/09/17 01:08:48 cheshire
54 Renamed mDNSClientAPI.h to mDNSEmbeddedAPI.h
55 The name "mDNSClientAPI.h" is misleading to new developers looking at this code. The interfaces
56 declared in that file are ONLY appropriate to single-address-space embedded applications.
57 For clients on general-purpose computers, the interfaces defined in dns_sd.h should be used.
58
59 Revision 1.20 2004/09/17 00:49:51 cheshire
60 Get rid of now-unused GetResourceRecord -- the correct (safe) routine to use
61 is GetLargeResourceRecord
62
63 Revision 1.19 2004/09/16 21:59:15 cheshire
64 For consistency with zerov6Addr, rename zeroIPAddr to zerov4Addr
65
66 Revision 1.18 2004/09/16 02:29:39 cheshire
67 Moved mDNS_Lock/mDNS_Unlock to DNSCommon.c; Added necessary locking around
68 uDNS_ReceiveMsg, uDNS_StartQuery, uDNS_UpdateRecord, uDNS_RegisterService
69
70 Revision 1.17 2004/09/14 23:27:46 cheshire
71 Fix compile errors
72
73 Revision 1.16 2004/08/13 23:46:58 cheshire
74 "asyncronous" -> "asynchronous"
75
76 Revision 1.15 2004/08/10 23:19:14 ksekar
77 <rdar://problem/3722542>: DNS Extension daemon for Wide Area Rendezvous
78 Moved routines/constants to allow extern access for garbage collection daemon
79
80 Revision 1.14 2004/05/28 23:42:36 ksekar
81 <rdar://problem/3258021>: Feature: DNS server->client notification on record changes (#7805)
82
83 Revision 1.13 2004/05/18 23:51:25 cheshire
84 Tidy up all checkin comments to use consistent "<rdar://problem/xxxxxxx>" format for bug numbers
85
86 Revision 1.12 2004/04/22 04:03:59 cheshire
87 Headers should use "extern" declarations, not "mDNSexport"
88
89 Revision 1.11 2004/04/14 23:09:28 ksekar
90 Support for TSIG signed dynamic updates.
91
92 Revision 1.10 2004/03/13 01:57:33 ksekar
93 <rdar://problem/3192546>: DynDNS: Dynamic update of service records
94
95 Revision 1.9 2004/02/21 08:56:58 bradley
96 Wrap prototypes with extern "C" for C++ builds.
97
98 Revision 1.8 2004/02/06 23:04:18 ksekar
99 Basic Dynamic Update support via mDNS_Register (dissabled via
100 UNICAST_REGISTRATION #define)
101
102 Revision 1.7 2004/02/03 19:47:36 ksekar
103 Added an asynchronous state machine mechanism to uDNS.c, including
104 calls to find the parent zone for a domain name. Changes include code
105 in repository previously dissabled via "#if 0 incomplete". Codepath
106 is currently unused, and will be called to create update records, etc.
107
108 Revision 1.6 2004/01/27 20:15:22 cheshire
109 <rdar://problem/3541288>: Time to prune obsolete code for listening on port 53
110
111 Revision 1.5 2004/01/24 03:40:56 cheshire
112 Move mDNSAddrIsDNSMulticast() from DNSCommon.h to mDNSEmbeddedAPI.h so embedded clients can use it
113
114 Revision 1.4 2004/01/24 03:38:27 cheshire
115 Fix minor syntactic error: Headers should use "extern" declarations, not "mDNSexport"
116
117 Revision 1.3 2004/01/23 23:23:14 ksekar
118 Added TCP support for truncated unicast messages.
119
120 Revision 1.2 2004/01/21 21:12:23 cheshire
121 Add missing newline at end of file to make Unix tools happier
122
123 Revision 1.1 2003/12/13 03:05:27 ksekar
124 <rdar://problem/3192548>: DynDNS: Unicast query of service records
125
126
127 */
128
129 #ifndef __DNSCOMMON_H_
130 #define __DNSCOMMON_H_
131
132 #include "mDNSEmbeddedAPI.h"
133
134 #ifdef __cplusplus
135 extern "C" {
136 #endif
137
138 // ***************************************************************************
139 #if COMPILER_LIKES_PRAGMA_MARK
140 #pragma mark - DNS Protocol Constants
141 #endif
142
143 typedef enum
144 {
145 kDNSFlag0_QR_Mask = 0x80, // Query or response?
146 kDNSFlag0_QR_Query = 0x00,
147 kDNSFlag0_QR_Response = 0x80,
148
149 kDNSFlag0_OP_Mask = 0x78, // Operation type
150 kDNSFlag0_OP_StdQuery = 0x00,
151 kDNSFlag0_OP_Iquery = 0x08,
152 kDNSFlag0_OP_Status = 0x10,
153 kDNSFlag0_OP_Unused3 = 0x18,
154 kDNSFlag0_OP_Notify = 0x20,
155 kDNSFlag0_OP_Update = 0x28,
156
157 kDNSFlag0_QROP_Mask = kDNSFlag0_QR_Mask | kDNSFlag0_OP_Mask,
158
159 kDNSFlag0_AA = 0x04, // Authoritative Answer?
160 kDNSFlag0_TC = 0x02, // Truncated?
161 kDNSFlag0_RD = 0x01, // Recursion Desired?
162 kDNSFlag1_RA = 0x80, // Recursion Available?
163
164 kDNSFlag1_Zero = 0x40, // Reserved; must be zero
165 kDNSFlag1_AD = 0x20, // Authentic Data [RFC 2535]
166 kDNSFlag1_CD = 0x10, // Checking Disabled [RFC 2535]
167
168 kDNSFlag1_RC = 0x0F, // Response code
169 kDNSFlag1_RC_NoErr = 0x00,
170 kDNSFlag1_RC_FmtErr = 0x01,
171 kDNSFlag1_RC_SrvErr = 0x02,
172 kDNSFlag1_RC_NXDomain = 0x03,
173 kDNSFlag1_RC_NotImpl = 0x04,
174 kDNSFlag1_RC_Refused = 0x05,
175 kDNSFlag1_RC_YXDomain = 0x06,
176 kDNSFlag1_RC_YXRRSet = 0x07,
177 kDNSFlag1_RC_NXRRSet = 0x08,
178 kDNSFlag1_RC_NotAuth = 0x09,
179 kDNSFlag1_RC_NotZone = 0x0A
180 } DNS_Flags;
181
182 typedef enum
183 {
184 TSIG_ErrBadSig = 16,
185 TSIG_ErrBadKey = 17,
186 TSIG_ErrBadTime = 18
187 } TSIG_ErrorCode;
188
189 // ***************************************************************************
190 #if COMPILER_LIKES_PRAGMA_MARK
191 #pragma mark -
192 #pragma mark - General Utility Functions
193 #endif
194
195 extern const NetworkInterfaceInfo *GetFirstActiveInterface(const NetworkInterfaceInfo *intf);
196 extern mDNSInterfaceID GetNextActiveInterfaceID(const NetworkInterfaceInfo *intf);
197
198 extern mDNSu32 mDNSRandom(mDNSu32 max);
199
200
201 // ***************************************************************************
202 #if COMPILER_LIKES_PRAGMA_MARK
203 #pragma mark -
204 #pragma mark - Domain Name Utility Functions
205 #endif
206
207 #define mdnsIsDigit(X) ((X) >= '0' && (X) <= '9')
208 #define mDNSIsUpperCase(X) ((X) >= 'A' && (X) <= 'Z')
209 #define mDNSIsLowerCase(X) ((X) >= 'a' && (X) <= 'z')
210 #define mdnsIsLetter(X) (mDNSIsUpperCase(X) || mDNSIsLowerCase(X))
211
212 #define mdnsValidHostChar(X, notfirst, notlast) (mdnsIsLetter(X) || mdnsIsDigit(X) || ((notfirst) && (notlast) && (X) == '-') )
213
214 extern mDNSu16 CompressedDomainNameLength(const domainname *const name, const domainname *parent);
215
216 extern mDNSBool LabelContainsSuffix(const domainlabel *const name, const mDNSBool RichText);
217 extern mDNSu32 RemoveLabelSuffix(domainlabel *name, mDNSBool RichText);
218 extern void AppendLabelSuffix(domainlabel *name, mDNSu32 val, mDNSBool RichText);
219 extern void mDNS_HostNameCallback(mDNS *const m, AuthRecord *const rr, mStatus result);
220 #define ValidateDomainName(N) (DomainNameLength(N) <= MAX_DOMAIN_NAME)
221
222
223 // ***************************************************************************
224 #if COMPILER_LIKES_PRAGMA_MARK
225 #pragma mark -
226 #pragma mark - Resource Record Utility Functions
227 #endif
228
229 extern mDNSu32 RDataHashValue(mDNSu16 const rdlength, const RDataBody *const rdb);
230
231 extern mDNSBool SameRData(const ResourceRecord *const r1, const ResourceRecord *const r2);
232
233 extern mDNSBool ResourceRecordAnswersQuestion(const ResourceRecord *const rr, const DNSQuestion *const q);
234
235 extern mDNSBool SameResourceRecord(ResourceRecord *r1, ResourceRecord *r2);
236
237 extern mDNSu16 GetRDLength(const ResourceRecord *const rr, mDNSBool estimate);
238
239 #define GetRRDomainNameTarget(RR) ( \
240 ((RR)->rrtype == kDNSType_CNAME || (RR)->rrtype == kDNSType_PTR || (RR)->rrtype == kDNSType_NS) \
241 ? &(RR)->rdata->u.name : \
242 ((RR)->rrtype == kDNSType_SRV ) ? &(RR)->rdata->u.srv.target : mDNSNULL )
243
244 extern mDNSBool ValidateRData(const mDNSu16 rrtype, const mDNSu16 rdlength, const RData *const rd);
245 #define LocalRecordReady(X) ((X)->resrec.RecordType != kDNSRecordTypeUnique && (X)->resrec.RecordType != kDNSRecordTypeDeregistering)
246
247
248 // ***************************************************************************
249 #if COMPILER_LIKES_PRAGMA_MARK
250 #pragma mark -
251 #pragma mark -
252 #pragma mark - DNS Message Creation Functions
253 #endif
254
255 extern void InitializeDNSMessage(DNSMessageHeader *h, mDNSOpaque16 id, mDNSOpaque16 flags);
256 extern const mDNSu8 *FindCompressionPointer(const mDNSu8 *const base, const mDNSu8 *const end, const mDNSu8 *const domname);
257
258 extern mDNSu8 *putDomainNameAsLabels(const DNSMessage *const msg, mDNSu8 *ptr, const mDNSu8 *const limit, const domainname *const name);
259
260 extern mDNSu8 *putRData(const DNSMessage *const msg, mDNSu8 *ptr, const mDNSu8 *const limit, ResourceRecord *rr);
261
262 // If we have a single large record to put in the packet, then we allow the packet to be up to 9K bytes,
263 // but in the normal case we try to keep the packets below 1500 to avoid IP fragmentation on standard Ethernet
264
265 extern mDNSu8 *PutResourceRecordTTLWithLimit(DNSMessage *const msg, mDNSu8 *ptr, mDNSu16 *count, ResourceRecord *rr, mDNSu32 ttl, const mDNSu8 *limit);
266
267 #define PutResourceRecordTTL(msg, ptr, count, rr, ttl) PutResourceRecordTTLWithLimit((msg), (ptr), (count), (rr), (ttl), \
268 ((msg)->h.numAnswers || (msg)->h.numAuthorities || (msg)->h.numAdditionals) ? (msg)->data + NormalMaxDNSMessageData : (msg)->data + AbsoluteMaxDNSMessageData)
269
270 #define PutResourceRecordTTLJumbo(msg, ptr, count, rr, ttl) PutResourceRecordTTLWithLimit((msg), (ptr), (count), (rr), (ttl), \
271 (msg)->data + AbsoluteMaxDNSMessageData)
272
273 extern mDNSu8 *PutResourceRecordCappedTTL(DNSMessage *const msg, mDNSu8 *ptr, mDNSu16 *count, ResourceRecord *rr, mDNSu32 maxttl);
274
275 extern mDNSu8 *putEmptyResourceRecord(DNSMessage *const msg, mDNSu8 *ptr, const mDNSu8 *const limit, mDNSu16 *count, const AuthRecord *rr);
276
277 extern mDNSu8 *putQuestion(DNSMessage *const msg, mDNSu8 *ptr, const mDNSu8 *const limit, const domainname *const name, mDNSu16 rrtype, mDNSu16 rrclass);
278
279 extern mDNSu8 *putZone(DNSMessage *const msg, mDNSu8 *ptr, mDNSu8 *limit, const domainname *zone, mDNSOpaque16 zoneClass);
280
281 extern mDNSu8 *putPrereqNameNotInUse(domainname *name, DNSMessage *msg, mDNSu8 *ptr, mDNSu8 *end);
282
283 extern mDNSu8 *putDeletionRecord(DNSMessage *msg, mDNSu8 *ptr, ResourceRecord *rr);
284
285 extern mDNSu8 *putDeleteRRSet(DNSMessage *msg, mDNSu8 *ptr, const domainname *name, mDNSu16 rrtype);
286
287 extern mDNSu8 *putDeleteAllRRSets(DNSMessage *msg, mDNSu8 *ptr, const domainname *name);
288
289 extern mDNSu8 *putUpdateLease(DNSMessage *msg, mDNSu8 *end, mDNSu32 lease);
290
291 #define PutResourceRecord(MSG, P, C, RR) PutResourceRecordTTL((MSG), (P), (C), (RR), (RR)->rroriginalttl)
292
293
294 // ***************************************************************************
295 #if COMPILER_LIKES_PRAGMA_MARK
296 #pragma mark -
297 #pragma mark - DNS Message Parsing Functions
298 #endif
299
300 extern mDNSu32 DomainNameHashValue(const domainname *const name);
301
302 extern void SetNewRData(ResourceRecord *const rr, RData *NewRData, mDNSu16 rdlength);
303
304
305 extern const mDNSu8 *skipDomainName(const DNSMessage *const msg, const mDNSu8 *ptr, const mDNSu8 *const end);
306
307 extern const mDNSu8 *getDomainName(const DNSMessage *const msg, const mDNSu8 *ptr, const mDNSu8 *const end,
308 domainname *const name);
309
310 extern const mDNSu8 *skipResourceRecord(const DNSMessage *msg, const mDNSu8 *ptr, const mDNSu8 *end);
311
312 extern const mDNSu8 *GetLargeResourceRecord(mDNS *const m, const DNSMessage * const msg, const mDNSu8 *ptr,
313 const mDNSu8 * const end, const mDNSInterfaceID InterfaceID, mDNSu8 RecordType, LargeCacheRecord *largecr);
314
315 extern const mDNSu8 *skipQuestion(const DNSMessage *msg, const mDNSu8 *ptr, const mDNSu8 *end);
316
317 extern const mDNSu8 *getQuestion(const DNSMessage *msg, const mDNSu8 *ptr, const mDNSu8 *end, const mDNSInterfaceID InterfaceID,
318 DNSQuestion *question);
319
320 extern const mDNSu8 *LocateAnswers(const DNSMessage *const msg, const mDNSu8 *const end);
321
322 extern const mDNSu8 *LocateAuthorities(const DNSMessage *const msg, const mDNSu8 *const end);
323
324 extern const mDNSu8 *LocateAdditionals(const DNSMessage *const msg, const mDNSu8 *const end);
325
326 // ***************************************************************************
327 #if COMPILER_LIKES_PRAGMA_MARK
328 #pragma mark -
329 #pragma mark -
330 #pragma mark - Packet Sending Functions
331 #endif
332
333 extern mStatus mDNSSendDNSMessage(const mDNS *const m, DNSMessage *const msg, mDNSu8 *end,
334 mDNSInterfaceID InterfaceID, const mDNSAddr *dst, mDNSIPPort dstport, int sd, uDNS_AuthInfo *authInfo);
335
336 // ***************************************************************************
337 #if COMPILER_LIKES_PRAGMA_MARK
338 #pragma mark -
339 #pragma mark - RR List Management & Task Management
340 #endif
341
342 extern void mDNS_Lock(mDNS *const m);
343 extern void mDNS_Unlock(mDNS *const m);
344
345 #ifdef __cplusplus
346 }
347 #endif
348
349 #endif // __DNSCOMMON_H_