1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2002-2003 Apple Computer, Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 Change History (most recent first):
20 Revision 1.59 2008/03/14 19:58:38 mcguire
21 <rdar://problem/5500969> BTMM: Need ability to identify version of mDNSResponder client
22 Make sure we add the record when sending LLQ refreshes
24 Revision 1.58 2008/03/06 21:26:10 cheshire
25 Moved duplicated STRINGIFY macro from individual C files to DNSCommon.h
27 Revision 1.57 2007/12/13 20:20:17 cheshire
28 Minor efficiency tweaks -- converted IdenticalResourceRecord, IdenticalSameNameRecord, and
29 SameRData from functions to macros, which allows the code to be inlined (the compiler can't
30 inline a function defined in a different compilation unit) and therefore optimized better.
32 Revision 1.56 2007/12/13 00:13:03 cheshire
33 Simplified RDataHashValue to take a single ResourceRecord pointer, instead of separate rdlength and RDataBody
35 Revision 1.55 2007/12/13 00:09:28 cheshire
36 For completeness added MX, AFSDB, RT, KX to list of RRTYPES that are considered to have a target domainname in their rdata
38 Revision 1.54 2007/10/05 17:56:10 cheshire
39 Move CountLabels and SkipLeadingLabels to DNSCommon.c so they're callable from other files
41 Revision 1.53 2007/09/27 17:42:49 cheshire
42 Fix naming: for consistency, "kDNSFlag1_RC" should be "kDNSFlag1_RC_Mask"
44 Revision 1.52 2007/09/26 00:49:46 cheshire
45 Improve packet logging to show sent and received packets,
46 transport protocol (UDP/TCP/TLS) and source/destination address:port
48 Revision 1.51 2007/09/21 21:12:36 cheshire
49 <rdar://problem/5498009> BTMM: Need to log updates and query packet contents
51 Revision 1.50 2007/09/20 01:12:06 cheshire
52 Moved HashSlot(X) from mDNS.c to DNSCommon.h so it's usable in other files
54 Revision 1.49 2007/08/30 00:31:20 cheshire
55 Improve "locking failure" debugging messages to show function name using __func__ macro
57 Revision 1.48 2007/05/25 00:25:44 cheshire
58 <rdar://problem/5227737> Need to enhance putRData to output all current known types
60 Revision 1.47 2007/05/01 21:46:31 cheshire
61 Move GetLLQOptData/GetPktLease from uDNS.c into DNSCommon.c so that dnsextd can use them
63 Revision 1.46 2007/04/22 20:18:10 cheshire
64 Add comment about mDNSRandom()
66 Revision 1.45 2007/04/22 06:02:02 cheshire
67 <rdar://problem/4615977> Query should immediately return failure when no server
69 Revision 1.44 2007/03/28 01:20:05 cheshire
70 <rdar://problem/4883206> Improve/create logging for secure browse
72 Revision 1.43 2007/03/20 17:07:15 cheshire
73 Rename "struct uDNS_TCPSocket_struct" to "TCPSocket", "struct uDNS_UDPSocket_struct" to "UDPSocket"
75 Revision 1.42 2007/03/10 03:26:44 cheshire
76 <rdar://problem/4961667> uDNS: LLQ refresh response packet causes cached records to be removed from cache
78 Revision 1.41 2007/01/18 23:18:17 cheshire
79 Source code tidying: Delete extraneous white space
81 Revision 1.40 2007/01/05 08:30:40 cheshire
82 Trim excessive "$Log" checkin history from before 2006
83 (checkin history still available via "cvs log ..." of course)
85 Revision 1.39 2007/01/04 21:45:20 cheshire
86 Added mDNS_DropLockBeforeCallback/mDNS_ReclaimLockAfterCallback macros,
87 to do additional lock sanity checking around callback invocations
89 Revision 1.38 2006/12/22 20:59:49 cheshire
90 <rdar://problem/4742742> Read *all* DNS keys from keychain,
91 not just key for the system-wide default registration domain
93 Revision 1.37 2006/08/14 23:24:22 cheshire
94 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
96 Revision 1.36 2006/07/05 22:56:07 cheshire
97 <rdar://problem/4472014> Add Private DNS client functionality to mDNSResponder
98 Update mDNSSendDNSMessage() to use uDNS_TCPSocket type instead of "int"
100 Revision 1.35 2006/06/29 07:42:14 cheshire
101 <rdar://problem/3922989> Performance: Remove unnecessary SameDomainName() checks
103 Revision 1.34 2006/03/18 21:47:56 cheshire
104 <rdar://problem/4073825> Improve logic for delaying packets after repeated interface transitions
106 Revision 1.33 2006/03/10 21:51:41 cheshire
107 <rdar://problem/4111464> After record update, old record sometimes remains in cache
108 Split out SameRDataBody() into a separate routine so it can be called from other code
112 #ifndef __DNSCOMMON_H_
113 #define __DNSCOMMON_H_
115 #include "mDNSEmbeddedAPI.h"
121 //*************************************************************************************************************
124 // Note: The C preprocessor stringify operator ('#') makes a string from its argument, without macro expansion
125 // e.g. If "version" is #define'd to be "4", then STRINGIFY_AWE(version) will return the string "version", not "4"
126 // To expand "version" to its value before making the string, use STRINGIFY(version) instead
127 #define STRINGIFY_ARGUMENT_WITHOUT_EXPANSION(s) #s
128 #define STRINGIFY(s) STRINGIFY_ARGUMENT_WITHOUT_EXPANSION(s)
130 // ***************************************************************************
131 #if COMPILER_LIKES_PRAGMA_MARK
132 #pragma mark - DNS Protocol Constants
137 kDNSFlag0_QR_Mask
= 0x80, // Query or response?
138 kDNSFlag0_QR_Query
= 0x00,
139 kDNSFlag0_QR_Response
= 0x80,
141 kDNSFlag0_OP_Mask
= 0x78, // Operation type
142 kDNSFlag0_OP_StdQuery
= 0x00,
143 kDNSFlag0_OP_Iquery
= 0x08,
144 kDNSFlag0_OP_Status
= 0x10,
145 kDNSFlag0_OP_Unused3
= 0x18,
146 kDNSFlag0_OP_Notify
= 0x20,
147 kDNSFlag0_OP_Update
= 0x28,
149 kDNSFlag0_QROP_Mask
= kDNSFlag0_QR_Mask
| kDNSFlag0_OP_Mask
,
151 kDNSFlag0_AA
= 0x04, // Authoritative Answer?
152 kDNSFlag0_TC
= 0x02, // Truncated?
153 kDNSFlag0_RD
= 0x01, // Recursion Desired?
154 kDNSFlag1_RA
= 0x80, // Recursion Available?
156 kDNSFlag1_Zero
= 0x40, // Reserved; must be zero
157 kDNSFlag1_AD
= 0x20, // Authentic Data [RFC 2535]
158 kDNSFlag1_CD
= 0x10, // Checking Disabled [RFC 2535]
160 kDNSFlag1_RC_Mask
= 0x0F, // Response code
161 kDNSFlag1_RC_NoErr
= 0x00,
162 kDNSFlag1_RC_FmtErr
= 0x01,
163 kDNSFlag1_RC_SrvErr
= 0x02,
164 kDNSFlag1_RC_NXDomain
= 0x03,
165 kDNSFlag1_RC_NotImpl
= 0x04,
166 kDNSFlag1_RC_Refused
= 0x05,
167 kDNSFlag1_RC_YXDomain
= 0x06,
168 kDNSFlag1_RC_YXRRSet
= 0x07,
169 kDNSFlag1_RC_NXRRSet
= 0x08,
170 kDNSFlag1_RC_NotAuth
= 0x09,
171 kDNSFlag1_RC_NotZone
= 0x0A
181 // ***************************************************************************
182 #if COMPILER_LIKES_PRAGMA_MARK
184 #pragma mark - General Utility Functions
187 extern const NetworkInterfaceInfo
*GetFirstActiveInterface(const NetworkInterfaceInfo
*intf
);
188 extern mDNSInterfaceID
GetNextActiveInterfaceID(const NetworkInterfaceInfo
*intf
);
190 extern mDNSu32
mDNSRandom(mDNSu32 max
); // Returns pseudo-random result from zero to max inclusive
191 extern mDNSu32
mDNSRandomFromFixedSeed(mDNSu32 seed
, mDNSu32 max
);
193 // ***************************************************************************
194 #if COMPILER_LIKES_PRAGMA_MARK
196 #pragma mark - Domain Name Utility Functions
199 #define mdnsIsDigit(X) ((X) >= '0' && (X) <= '9')
200 #define mDNSIsUpperCase(X) ((X) >= 'A' && (X) <= 'Z')
201 #define mDNSIsLowerCase(X) ((X) >= 'a' && (X) <= 'z')
202 #define mdnsIsLetter(X) (mDNSIsUpperCase(X) || mDNSIsLowerCase(X))
204 #define mdnsValidHostChar(X, notfirst, notlast) (mdnsIsLetter(X) || mdnsIsDigit(X) || ((notfirst) && (notlast) && (X) == '-') )
206 extern mDNSu16
CompressedDomainNameLength(const domainname
*const name
, const domainname
*parent
);
207 extern int CountLabels(const domainname
*d
);
208 extern const domainname
*SkipLeadingLabels(const domainname
*d
, int skip
);
210 extern mDNSu32
TruncateUTF8ToLength(mDNSu8
*string
, mDNSu32 length
, mDNSu32 max
);
211 extern mDNSBool
LabelContainsSuffix(const domainlabel
*const name
, const mDNSBool RichText
);
212 extern mDNSu32
RemoveLabelSuffix(domainlabel
*name
, mDNSBool RichText
);
213 extern void AppendLabelSuffix(domainlabel
*name
, mDNSu32 val
, mDNSBool RichText
);
214 #define ValidateDomainName(N) (DomainNameLength(N) <= MAX_DOMAIN_NAME)
216 // ***************************************************************************
217 #if COMPILER_LIKES_PRAGMA_MARK
219 #pragma mark - Resource Record Utility Functions
222 // IdenticalResourceRecord returns true if two resources records have
223 // the same name, type, class, and identical rdata (InterfaceID and TTL may differ)
225 // IdenticalSameNameRecord is the same, except it skips the expensive SameDomainName() check,
226 // which is at its most expensive and least useful in cases where we know in advance that the names match
228 // Note: The dominant use of IdenticalResourceRecord is from ProcessQuery(), handling known-answer lists. In this case
229 // it's common to have a whole bunch or records with exactly the same name (e.g. "_http._tcp.local") but different RDATA.
230 // The SameDomainName() check is expensive when the names match, and in this case *all* the names match, so we
231 // used to waste a lot of CPU time verifying that the names match, only then to find that the RDATA is different.
232 // We observed mDNSResponder spending 30% of its total CPU time on this single task alone.
233 // By swapping the checks so that we check the RDATA first, we can quickly detect when it's different
234 // (99% of the time) and then bail out before we waste time on the expensive SameDomainName() check.
236 #define IdenticalResourceRecord(r1,r2) ( \
237 (r1)->rrtype == (r2)->rrtype && \
238 (r1)->rrclass == (r2)->rrclass && \
239 (r1)->namehash == (r2)->namehash && \
240 (r1)->rdlength == (r2)->rdlength && \
241 (r1)->rdatahash == (r2)->rdatahash && \
242 SameRDataBody((r1), &(r2)->rdata->u) && \
243 SameDomainName((r1)->name, (r2)->name))
245 #define IdenticalSameNameRecord(r1,r2) ( \
246 (r1)->rrtype == (r2)->rrtype && \
247 (r1)->rrclass == (r2)->rrclass && \
248 (r1)->rdlength == (r2)->rdlength && \
249 (r1)->rdatahash == (r2)->rdatahash && \
250 SameRDataBody((r1), &(r2)->rdata->u))
252 extern mDNSu32
RDataHashValue(const ResourceRecord
*const rr
);
253 extern mDNSBool
SameRDataBody(const ResourceRecord
*const r1
, const RDataBody
*const r2
);
254 #define SameRData(r1,r2) ((r1)->rrtype == (r2)->rrtype && (r1)->rdlength == (r2)->rdlength && (r1)->rdatahash == (r2)->rdatahash && SameRDataBody((r1), &(r2)->rdata->u))
255 extern mDNSBool
ResourceRecordAnswersQuestion(const ResourceRecord
*const rr
, const DNSQuestion
*const q
);
256 extern mDNSBool
SameNameRecordAnswersQuestion(const ResourceRecord
*const rr
, const DNSQuestion
*const q
);
257 extern mDNSu16
GetRDLength(const ResourceRecord
*const rr
, mDNSBool estimate
);
258 extern mDNSBool
ValidateRData(const mDNSu16 rrtype
, const mDNSu16 rdlength
, const RData
*const rd
);
260 #define GetRRDomainNameTarget(RR) ( \
261 ((RR)->rrtype == kDNSType_NS || (RR)->rrtype == kDNSType_CNAME || (RR)->rrtype == kDNSType_PTR || (RR)->rrtype == kDNSType_DNAME) ? &(RR)->rdata->u.name : \
262 ((RR)->rrtype == kDNSType_MX || (RR)->rrtype == kDNSType_AFSDB || (RR)->rrtype == kDNSType_RT || (RR)->rrtype == kDNSType_KX ) ? &(RR)->rdata->u.mx.exchange : \
263 ((RR)->rrtype == kDNSType_SRV ) ? &(RR)->rdata->u.srv.target : mDNSNULL )
265 #define LocalRecordReady(X) ((X)->resrec.RecordType != kDNSRecordTypeUnique && (X)->resrec.RecordType != kDNSRecordTypeDeregistering)
267 // ***************************************************************************
268 #if COMPILER_LIKES_PRAGMA_MARK
270 #pragma mark - DNS Message Creation Functions
273 extern void InitializeDNSMessage(DNSMessageHeader
*h
, mDNSOpaque16 id
, mDNSOpaque16 flags
);
274 extern const mDNSu8
*FindCompressionPointer(const mDNSu8
*const base
, const mDNSu8
*const end
, const mDNSu8
*const domname
);
275 extern mDNSu8
*putDomainNameAsLabels(const DNSMessage
*const msg
, mDNSu8
*ptr
, const mDNSu8
*const limit
, const domainname
*const name
);
276 extern mDNSu8
*putRData(const DNSMessage
*const msg
, mDNSu8
*ptr
, const mDNSu8
*const limit
, const ResourceRecord
*const rr
);
278 // If we have a single large record to put in the packet, then we allow the packet to be up to 9K bytes,
279 // but in the normal case we try to keep the packets below 1500 to avoid IP fragmentation on standard Ethernet
280 extern mDNSu8
*PutResourceRecordTTLWithLimit(DNSMessage
*const msg
, mDNSu8
*ptr
, mDNSu16
*count
, ResourceRecord
*rr
, mDNSu32 ttl
, const mDNSu8
*limit
);
281 #define PutResourceRecordTTL(msg, ptr, count, rr, ttl) PutResourceRecordTTLWithLimit((msg), (ptr), (count), (rr), (ttl), \
282 ((msg)->h.numAnswers || (msg)->h.numAuthorities || (msg)->h.numAdditionals) ? (msg)->data + NormalMaxDNSMessageData : (msg)->data + AbsoluteMaxDNSMessageData)
283 #define PutResourceRecordTTLJumbo(msg, ptr, count, rr, ttl) PutResourceRecordTTLWithLimit((msg), (ptr), (count), (rr), (ttl), \
284 (msg)->data + AbsoluteMaxDNSMessageData)
285 extern mDNSu8
*PutResourceRecordCappedTTL(DNSMessage
*const msg
, mDNSu8
*ptr
, mDNSu16
*count
, ResourceRecord
*rr
, mDNSu32 maxttl
);
286 extern mDNSu8
*putEmptyResourceRecord(DNSMessage
*const msg
, mDNSu8
*ptr
, const mDNSu8
*const limit
, mDNSu16
*count
, const AuthRecord
*rr
);
288 extern mDNSu8
*putQuestion(DNSMessage
*const msg
, mDNSu8
*ptr
, const mDNSu8
*const limit
, const domainname
*const name
, mDNSu16 rrtype
, mDNSu16 rrclass
);
289 extern mDNSu8
*putZone(DNSMessage
*const msg
, mDNSu8
*ptr
, mDNSu8
*limit
, const domainname
*zone
, mDNSOpaque16 zoneClass
);
290 extern mDNSu8
*putPrereqNameNotInUse(const domainname
*const name
, DNSMessage
*msg
, mDNSu8
*ptr
, mDNSu8
*end
);
291 extern mDNSu8
*putDeletionRecord(DNSMessage
*msg
, mDNSu8
*ptr
, ResourceRecord
*rr
);
292 extern mDNSu8
*putDeleteRRSet(DNSMessage
*msg
, mDNSu8
*ptr
, const domainname
*name
, mDNSu16 rrtype
);
293 extern mDNSu8
*putDeleteAllRRSets(DNSMessage
*msg
, mDNSu8
*ptr
, const domainname
*name
);
294 extern mDNSu8
*putUpdateLease(DNSMessage
*msg
, mDNSu8
*end
, mDNSu32 lease
);
295 #define PutResourceRecord(MSG, P, C, RR) PutResourceRecordTTL((MSG), (P), (C), (RR), (RR)->rroriginalttl)
297 extern mDNSu8
*putHINFO(const mDNS
*const m
, DNSMessage
*const msg
, mDNSu8
*end
, DomainAuthInfo
*authInfo
);
299 // ***************************************************************************
300 #if COMPILER_LIKES_PRAGMA_MARK
302 #pragma mark - DNS Message Parsing Functions
305 #define HashSlot(X) (DomainNameHashValue(X) % CACHE_HASH_SLOTS)
306 extern mDNSu32
DomainNameHashValue(const domainname
*const name
);
307 extern void SetNewRData(ResourceRecord
*const rr
, RData
*NewRData
, mDNSu16 rdlength
);
308 extern const mDNSu8
*skipDomainName(const DNSMessage
*const msg
, const mDNSu8
*ptr
, const mDNSu8
*const end
);
309 extern const mDNSu8
*getDomainName(const DNSMessage
*const msg
, const mDNSu8
*ptr
, const mDNSu8
*const end
,
310 domainname
*const name
);
311 extern const mDNSu8
*skipResourceRecord(const DNSMessage
*msg
, const mDNSu8
*ptr
, const mDNSu8
*end
);
312 extern const mDNSu8
*GetLargeResourceRecord(mDNS
*const m
, const DNSMessage
* const msg
, const mDNSu8
*ptr
,
313 const mDNSu8
* end
, const mDNSInterfaceID InterfaceID
, mDNSu8 RecordType
, LargeCacheRecord
*largecr
);
314 extern const mDNSu8
*skipQuestion(const DNSMessage
*msg
, const mDNSu8
*ptr
, const mDNSu8
*end
);
315 extern const mDNSu8
*getQuestion(const DNSMessage
*msg
, const mDNSu8
*ptr
, const mDNSu8
*end
, const mDNSInterfaceID InterfaceID
,
316 DNSQuestion
*question
);
317 extern const mDNSu8
*LocateAnswers(const DNSMessage
*const msg
, const mDNSu8
*const end
);
318 extern const mDNSu8
*LocateAuthorities(const DNSMessage
*const msg
, const mDNSu8
*const end
);
319 extern const mDNSu8
*LocateAdditionals(const DNSMessage
*const msg
, const mDNSu8
*const end
);
320 extern const mDNSu8
*LocateLLQOptData(const DNSMessage
*const msg
, const mDNSu8
*const end
);
321 extern const rdataOPT
*GetLLQOptData(mDNS
*const m
, const DNSMessage
*const msg
, const mDNSu8
*const end
);
322 extern const mDNSu8
*LocateLeaseOptData(const DNSMessage
*const msg
, const mDNSu8
*const end
);
323 extern mDNSu32
GetPktLease(mDNS
*m
, DNSMessage
*msg
, const mDNSu8
*end
);
324 extern void DumpPacket(mDNS
*const m
, mDNSBool sent
, char *type
, const mDNSAddr
*addr
, mDNSIPPort port
, const DNSMessage
*const msg
, const mDNSu8
*const end
);
326 // ***************************************************************************
327 #if COMPILER_LIKES_PRAGMA_MARK
329 #pragma mark - Packet Sending Functions
332 extern mStatus
mDNSSendDNSMessage(mDNS
*const m
, DNSMessage
*const msg
, mDNSu8
*end
,
333 mDNSInterfaceID InterfaceID
, const mDNSAddr
*dst
, mDNSIPPort dstport
, TCPSocket
*sock
, DomainAuthInfo
*authInfo
);
335 // ***************************************************************************
336 #if COMPILER_LIKES_PRAGMA_MARK
338 #pragma mark - RR List Management & Task Management
341 extern void mDNS_Lock_(mDNS
*const m
);
342 extern void mDNS_Unlock_(mDNS
*const m
);
344 #define mDNS_Lock(X) do { \
345 if ((X)->mDNS_busy != (X)->mDNS_reentrancy) LogMsg("%s: mDNS_Lock locking failure! mDNS_busy (%ld) != mDNS_reentrancy (%ld)", __func__, (X)->mDNS_busy, (X)->mDNS_reentrancy); \
346 mDNS_Lock_(X); } while (0)
348 #define mDNS_Unlock(X) do { mDNS_Unlock_(X); \
349 if ((X)->mDNS_busy != (X)->mDNS_reentrancy) LogMsg("%s: mDNS_Unlock locking failure! mDNS_busy (%ld) != mDNS_reentrancy (%ld)", __func__, (X)->mDNS_busy, (X)->mDNS_reentrancy); \
352 #define mDNS_DropLockBeforeCallback() do { m->mDNS_reentrancy++; \
353 if (m->mDNS_busy != m->mDNS_reentrancy) LogMsg("%s: Locking Failure! mDNS_busy (%ld) != mDNS_reentrancy (%ld)", __func__, m->mDNS_busy, m->mDNS_reentrancy); \
356 #define mDNS_ReclaimLockAfterCallback() do { \
357 if (m->mDNS_busy != m->mDNS_reentrancy) LogMsg("%s: Unlocking Failure! mDNS_busy (%ld) != mDNS_reentrancy (%ld)", __func__, m->mDNS_busy, m->mDNS_reentrancy); \
358 m->mDNS_reentrancy--; } while (0)
364 #endif // __DNSCOMMON_H_