]> git.saurik.com Git - apple/mdnsresponder.git/blame - mDNSCore/DNSCommon.h
mDNSResponder-258.18.tar.gz
[apple/mdnsresponder.git] / mDNSCore / DNSCommon.h
CommitLineData
67c8f8a1
A
1/* -*- Mode: C; tab-width: 4 -*-
2 *
8e92c31c
A
3 * Copyright (c) 2002-2003 Apple Computer, Inc. All rights reserved.
4 *
67c8f8a1
A
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
8e92c31c 8 *
67c8f8a1 9 * http://www.apache.org/licenses/LICENSE-2.0
8e92c31c 10 *
67c8f8a1
A
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
8e92c31c 15 * limitations under the License.
263eeeab 16 */
8e92c31c
A
17
18#ifndef __DNSCOMMON_H_
19#define __DNSCOMMON_H_
20
7f0064bd 21#include "mDNSEmbeddedAPI.h"
8e92c31c
A
22
23#ifdef __cplusplus
24 extern "C" {
25#endif
26
5e65c77f
A
27//*************************************************************************************************************
28// Macros
29
30// Note: The C preprocessor stringify operator ('#') makes a string from its argument, without macro expansion
31// e.g. If "version" is #define'd to be "4", then STRINGIFY_AWE(version) will return the string "version", not "4"
32// To expand "version" to its value before making the string, use STRINGIFY(version) instead
33#define STRINGIFY_ARGUMENT_WITHOUT_EXPANSION(s) #s
34#define STRINGIFY(s) STRINGIFY_ARGUMENT_WITHOUT_EXPANSION(s)
35
8e92c31c
A
36// ***************************************************************************
37#if COMPILER_LIKES_PRAGMA_MARK
38#pragma mark - DNS Protocol Constants
39#endif
40
41typedef enum
42 {
43 kDNSFlag0_QR_Mask = 0x80, // Query or response?
44 kDNSFlag0_QR_Query = 0x00,
45 kDNSFlag0_QR_Response = 0x80,
67c8f8a1 46
8e92c31c
A
47 kDNSFlag0_OP_Mask = 0x78, // Operation type
48 kDNSFlag0_OP_StdQuery = 0x00,
49 kDNSFlag0_OP_Iquery = 0x08,
50 kDNSFlag0_OP_Status = 0x10,
51 kDNSFlag0_OP_Unused3 = 0x18,
52 kDNSFlag0_OP_Notify = 0x20,
53 kDNSFlag0_OP_Update = 0x28,
67c8f8a1 54
8e92c31c 55 kDNSFlag0_QROP_Mask = kDNSFlag0_QR_Mask | kDNSFlag0_OP_Mask,
67c8f8a1 56
8e92c31c
A
57 kDNSFlag0_AA = 0x04, // Authoritative Answer?
58 kDNSFlag0_TC = 0x02, // Truncated?
59 kDNSFlag0_RD = 0x01, // Recursion Desired?
60 kDNSFlag1_RA = 0x80, // Recursion Available?
67c8f8a1 61
8e92c31c
A
62 kDNSFlag1_Zero = 0x40, // Reserved; must be zero
63 kDNSFlag1_AD = 0x20, // Authentic Data [RFC 2535]
64 kDNSFlag1_CD = 0x10, // Checking Disabled [RFC 2535]
65
67c8f8a1 66 kDNSFlag1_RC_Mask = 0x0F, // Response code
8e92c31c 67 kDNSFlag1_RC_NoErr = 0x00,
32bb7e43
A
68 kDNSFlag1_RC_FormErr = 0x01,
69 kDNSFlag1_RC_ServFail = 0x02,
8e92c31c
A
70 kDNSFlag1_RC_NXDomain = 0x03,
71 kDNSFlag1_RC_NotImpl = 0x04,
72 kDNSFlag1_RC_Refused = 0x05,
73 kDNSFlag1_RC_YXDomain = 0x06,
74 kDNSFlag1_RC_YXRRSet = 0x07,
75 kDNSFlag1_RC_NXRRSet = 0x08,
76 kDNSFlag1_RC_NotAuth = 0x09,
77 kDNSFlag1_RC_NotZone = 0x0A
78 } DNS_Flags;
79
7f0064bd
A
80typedef enum
81 {
82 TSIG_ErrBadSig = 16,
83 TSIG_ErrBadKey = 17,
84 TSIG_ErrBadTime = 18
85 } TSIG_ErrorCode;
67c8f8a1 86
8e92c31c
A
87// ***************************************************************************
88#if COMPILER_LIKES_PRAGMA_MARK
89#pragma mark -
90#pragma mark - General Utility Functions
91#endif
92
32bb7e43 93extern NetworkInterfaceInfo *GetFirstActiveInterface(NetworkInterfaceInfo *intf);
8e92c31c
A
94extern mDNSInterfaceID GetNextActiveInterfaceID(const NetworkInterfaceInfo *intf);
95
67c8f8a1 96extern mDNSu32 mDNSRandom(mDNSu32 max); // Returns pseudo-random result from zero to max inclusive
8e92c31c
A
97
98// ***************************************************************************
99#if COMPILER_LIKES_PRAGMA_MARK
100#pragma mark -
101#pragma mark - Domain Name Utility Functions
102#endif
67c8f8a1 103
32bb7e43 104#define mDNSIsDigit(X) ((X) >= '0' && (X) <= '9')
8e92c31c
A
105#define mDNSIsUpperCase(X) ((X) >= 'A' && (X) <= 'Z')
106#define mDNSIsLowerCase(X) ((X) >= 'a' && (X) <= 'z')
32bb7e43 107#define mDNSIsLetter(X) (mDNSIsUpperCase(X) || mDNSIsLowerCase(X))
67c8f8a1 108
32bb7e43 109#define mDNSValidHostChar(X, notfirst, notlast) (mDNSIsLetter(X) || mDNSIsDigit(X) || ((notfirst) && (notlast) && (X) == '-') )
8e92c31c
A
110
111extern mDNSu16 CompressedDomainNameLength(const domainname *const name, const domainname *parent);
67c8f8a1
A
112extern int CountLabels(const domainname *d);
113extern const domainname *SkipLeadingLabels(const domainname *d, int skip);
8e92c31c 114
7cb34e5c 115extern mDNSu32 TruncateUTF8ToLength(mDNSu8 *string, mDNSu32 length, mDNSu32 max);
8e92c31c
A
116extern mDNSBool LabelContainsSuffix(const domainlabel *const name, const mDNSBool RichText);
117extern mDNSu32 RemoveLabelSuffix(domainlabel *name, mDNSBool RichText);
263eeeab 118extern void AppendLabelSuffix(domainlabel *const name, mDNSu32 val, const mDNSBool RichText);
8e92c31c
A
119#define ValidateDomainName(N) (DomainNameLength(N) <= MAX_DOMAIN_NAME)
120
8e92c31c
A
121// ***************************************************************************
122#if COMPILER_LIKES_PRAGMA_MARK
123#pragma mark -
124#pragma mark - Resource Record Utility Functions
125#endif
126
030b743d
A
127// IdenticalResourceRecord returns true if two resources records have
128// the same name, type, class, and identical rdata (InterfaceID and TTL may differ)
129
130// IdenticalSameNameRecord is the same, except it skips the expensive SameDomainName() check,
131// which is at its most expensive and least useful in cases where we know in advance that the names match
132
133// Note: The dominant use of IdenticalResourceRecord is from ProcessQuery(), handling known-answer lists. In this case
134// it's common to have a whole bunch or records with exactly the same name (e.g. "_http._tcp.local") but different RDATA.
135// The SameDomainName() check is expensive when the names match, and in this case *all* the names match, so we
136// used to waste a lot of CPU time verifying that the names match, only then to find that the RDATA is different.
137// We observed mDNSResponder spending 30% of its total CPU time on this single task alone.
138// By swapping the checks so that we check the RDATA first, we can quickly detect when it's different
139// (99% of the time) and then bail out before we waste time on the expensive SameDomainName() check.
140
141#define IdenticalResourceRecord(r1,r2) ( \
142 (r1)->rrtype == (r2)->rrtype && \
143 (r1)->rrclass == (r2)->rrclass && \
144 (r1)->namehash == (r2)->namehash && \
145 (r1)->rdlength == (r2)->rdlength && \
146 (r1)->rdatahash == (r2)->rdatahash && \
263eeeab 147 SameRDataBody((r1), &(r2)->rdata->u, SameDomainName) && \
030b743d
A
148 SameDomainName((r1)->name, (r2)->name))
149
150#define IdenticalSameNameRecord(r1,r2) ( \
151 (r1)->rrtype == (r2)->rrtype && \
152 (r1)->rrclass == (r2)->rrclass && \
153 (r1)->rdlength == (r2)->rdlength && \
154 (r1)->rdatahash == (r2)->rdatahash && \
263eeeab 155 SameRDataBody((r1), &(r2)->rdata->u, SameDomainName))
030b743d 156
32bb7e43
A
157// A given RRType answers a QuestionType if RRType is CNAME, or types match, or QuestionType is ANY,
158// or the RRType is NSEC and positively asserts the nonexistence of the type being requested
263eeeab 159#define RRTypeAnswersQuestionType(R,Q) ((R)->rrtype == kDNSType_CNAME || (R)->rrtype == (Q) || (Q) == kDNSQType_ANY || RRAssertsNonexistence((R),(Q)))
32bb7e43
A
160#define RRAssertsNonexistence(R,T) ((R)->rrtype == kDNSType_NSEC && (T) < kDNSQType_ANY && !((R)->rdata->u.nsec.bitmap[(T)>>3] & (128 >> ((T)&7))))
161
030b743d 162extern mDNSu32 RDataHashValue(const ResourceRecord *const rr);
263eeeab 163extern mDNSBool SameRDataBody(const ResourceRecord *const r1, const RDataBody *const r2, DomainNameComparisonFn *samename);
67c8f8a1 164extern mDNSBool SameNameRecordAnswersQuestion(const ResourceRecord *const rr, const DNSQuestion *const q);
32bb7e43
A
165extern mDNSBool ResourceRecordAnswersQuestion(const ResourceRecord *const rr, const DNSQuestion *const q);
166extern mDNSBool AnyTypeRecordAnswersQuestion (const ResourceRecord *const rr, const DNSQuestion *const q);
263eeeab 167extern mDNSBool UnicastResourceRecordAnswersQuestion(const ResourceRecord *const rr, const DNSQuestion *const q);
8e92c31c 168extern mDNSu16 GetRDLength(const ResourceRecord *const rr, mDNSBool estimate);
67c8f8a1 169extern mDNSBool ValidateRData(const mDNSu16 rrtype, const mDNSu16 rdlength, const RData *const rd);
8e92c31c
A
170
171#define GetRRDomainNameTarget(RR) ( \
030b743d
A
172 ((RR)->rrtype == kDNSType_NS || (RR)->rrtype == kDNSType_CNAME || (RR)->rrtype == kDNSType_PTR || (RR)->rrtype == kDNSType_DNAME) ? &(RR)->rdata->u.name : \
173 ((RR)->rrtype == kDNSType_MX || (RR)->rrtype == kDNSType_AFSDB || (RR)->rrtype == kDNSType_RT || (RR)->rrtype == kDNSType_KX ) ? &(RR)->rdata->u.mx.exchange : \
8e92c31c
A
174 ((RR)->rrtype == kDNSType_SRV ) ? &(RR)->rdata->u.srv.target : mDNSNULL )
175
263eeeab 176#define LocalRecordReady(X) ((X)->resrec.RecordType != kDNSRecordTypeUnique)
8e92c31c 177
8e92c31c
A
178// ***************************************************************************
179#if COMPILER_LIKES_PRAGMA_MARK
180#pragma mark -
8e92c31c
A
181#pragma mark - DNS Message Creation Functions
182#endif
183
184extern void InitializeDNSMessage(DNSMessageHeader *h, mDNSOpaque16 id, mDNSOpaque16 flags);
185extern const mDNSu8 *FindCompressionPointer(const mDNSu8 *const base, const mDNSu8 *const end, const mDNSu8 *const domname);
8e92c31c 186extern mDNSu8 *putDomainNameAsLabels(const DNSMessage *const msg, mDNSu8 *ptr, const mDNSu8 *const limit, const domainname *const name);
67c8f8a1 187extern mDNSu8 *putRData(const DNSMessage *const msg, mDNSu8 *ptr, const mDNSu8 *const limit, const ResourceRecord *const rr);
8e92c31c 188
7f0064bd 189// If we have a single large record to put in the packet, then we allow the packet to be up to 9K bytes,
67c8f8a1 190// but in the normal case we try to keep the packets below 1500 to avoid IP fragmentation on standard Ethernet
ca3eca6b
A
191
192#define AllowedRRSpace(msg) (((msg)->h.numAnswers || (msg)->h.numAuthorities || (msg)->h.numAdditionals) ? NormalMaxDNSMessageData : AbsoluteMaxDNSMessageData)
193
7f0064bd 194extern mDNSu8 *PutResourceRecordTTLWithLimit(DNSMessage *const msg, mDNSu8 *ptr, mDNSu16 *count, ResourceRecord *rr, mDNSu32 ttl, const mDNSu8 *limit);
32bb7e43
A
195
196#define PutResourceRecordTTL(msg, ptr, count, rr, ttl) \
ca3eca6b
A
197 PutResourceRecordTTLWithLimit((msg), (ptr), (count), (rr), (ttl), (msg)->data + AllowedRRSpace(msg))
198
32bb7e43
A
199#define PutResourceRecordTTLJumbo(msg, ptr, count, rr, ttl) \
200 PutResourceRecordTTLWithLimit((msg), (ptr), (count), (rr), (ttl), (msg)->data + AbsoluteMaxDNSMessageData)
ca3eca6b 201
32bb7e43 202#define PutResourceRecord(MSG, P, C, RR) PutResourceRecordTTL((MSG), (P), (C), (RR), (RR)->rroriginalttl)
8e92c31c 203
ca3eca6b
A
204// The PutRR_OS variants assume a local variable 'm', put build the packet at m->omsg,
205// and assume a local variable 'OwnerRecordSpace' indicating how many bytes (if any) to reserve to add an OWNER option at the end
206#define PutRR_OS_TTL(ptr, count, rr, ttl) \
207 PutResourceRecordTTLWithLimit(&m->omsg, (ptr), (count), (rr), (ttl), m->omsg.data + AllowedRRSpace(&m->omsg) - OwnerRecordSpace)
208
209#define PutRR_OS(P, C, RR) PutRR_OS_TTL((P), (C), (RR), (RR)->rroriginalttl)
210
8e92c31c 211extern mDNSu8 *putQuestion(DNSMessage *const msg, mDNSu8 *ptr, const mDNSu8 *const limit, const domainname *const name, mDNSu16 rrtype, mDNSu16 rrclass);
7f0064bd 212extern mDNSu8 *putZone(DNSMessage *const msg, mDNSu8 *ptr, mDNSu8 *limit, const domainname *zone, mDNSOpaque16 zoneClass);
263eeeab 213extern mDNSu8 *putPrereqNameNotInUse(const domainname *const name, DNSMessage *const msg, mDNSu8 *const ptr, mDNSu8 *const end);
7f0064bd 214extern mDNSu8 *putDeletionRecord(DNSMessage *msg, mDNSu8 *ptr, ResourceRecord *rr);
263eeeab
A
215extern mDNSu8 *putDeletionRecordWithLimit(DNSMessage *msg, mDNSu8 *ptr, ResourceRecord *rr, mDNSu8 *limit);
216extern mDNSu8 *putDeleteRRSetWithLimit(DNSMessage *msg, mDNSu8 *ptr, const domainname *name, mDNSu16 rrtype, mDNSu8 *limit);
7f0064bd 217extern mDNSu8 *putDeleteAllRRSets(DNSMessage *msg, mDNSu8 *ptr, const domainname *name);
7f0064bd 218extern mDNSu8 *putUpdateLease(DNSMessage *msg, mDNSu8 *end, mDNSu32 lease);
263eeeab 219extern mDNSu8 *putUpdateLeaseWithLimit(DNSMessage *msg, mDNSu8 *ptr, mDNSu32 lease, mDNSu8 *limit);
8e92c31c 220
263eeeab 221extern mDNSu8 *putHINFO(const mDNS *const m, DNSMessage *const msg, mDNSu8 *ptr, DomainAuthInfo *authInfo, mDNSu8 *limit);
5e65c77f 222
8e92c31c
A
223// ***************************************************************************
224#if COMPILER_LIKES_PRAGMA_MARK
225#pragma mark -
226#pragma mark - DNS Message Parsing Functions
227#endif
228
67c8f8a1 229#define HashSlot(X) (DomainNameHashValue(X) % CACHE_HASH_SLOTS)
8e92c31c 230extern mDNSu32 DomainNameHashValue(const domainname *const name);
8e92c31c 231extern void SetNewRData(ResourceRecord *const rr, RData *NewRData, mDNSu16 rdlength);
8e92c31c 232extern const mDNSu8 *skipDomainName(const DNSMessage *const msg, const mDNSu8 *ptr, const mDNSu8 *const end);
8e92c31c
A
233extern const mDNSu8 *getDomainName(const DNSMessage *const msg, const mDNSu8 *ptr, const mDNSu8 *const end,
234 domainname *const name);
8e92c31c 235extern const mDNSu8 *skipResourceRecord(const DNSMessage *msg, const mDNSu8 *ptr, const mDNSu8 *end);
67c8f8a1 236extern const mDNSu8 *GetLargeResourceRecord(mDNS *const m, const DNSMessage * const msg, const mDNSu8 *ptr,
263eeeab 237 const mDNSu8 * end, const mDNSInterfaceID InterfaceID, mDNSu8 RecordType, LargeCacheRecord *const largecr);
8e92c31c 238extern const mDNSu8 *skipQuestion(const DNSMessage *msg, const mDNSu8 *ptr, const mDNSu8 *end);
8e92c31c
A
239extern const mDNSu8 *getQuestion(const DNSMessage *msg, const mDNSu8 *ptr, const mDNSu8 *end, const mDNSInterfaceID InterfaceID,
240 DNSQuestion *question);
8e92c31c 241extern const mDNSu8 *LocateAnswers(const DNSMessage *const msg, const mDNSu8 *const end);
8e92c31c 242extern const mDNSu8 *LocateAuthorities(const DNSMessage *const msg, const mDNSu8 *const end);
8e92c31c 243extern const mDNSu8 *LocateAdditionals(const DNSMessage *const msg, const mDNSu8 *const end);
32bb7e43 244extern const mDNSu8 *LocateOptRR(const DNSMessage *const msg, const mDNSu8 *const end, int minsize);
67c8f8a1 245extern const rdataOPT *GetLLQOptData(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *const end);
67c8f8a1 246extern mDNSu32 GetPktLease(mDNS *m, DNSMessage *msg, const mDNSu8 *end);
32bb7e43 247extern void DumpPacket(mDNS *const m, mStatus status, mDNSBool sent, char *transport,
9f29194f
A
248 const mDNSAddr *srcaddr, mDNSIPPort srcport,
249 const mDNSAddr *dstaddr, mDNSIPPort dstport, const DNSMessage *const msg, const mDNSu8 *const end);
8e92c31c 250
8e92c31c
A
251// ***************************************************************************
252#if COMPILER_LIKES_PRAGMA_MARK
253#pragma mark -
8e92c31c
A
254#pragma mark - Packet Sending Functions
255#endif
256
67c8f8a1 257extern mStatus mDNSSendDNSMessage(mDNS *const m, DNSMessage *const msg, mDNSu8 *end,
9f29194f 258 mDNSInterfaceID InterfaceID, UDPSocket *src, const mDNSAddr *dst, mDNSIPPort dstport, TCPSocket *sock, DomainAuthInfo *authInfo);
7f0064bd
A
259
260// ***************************************************************************
261#if COMPILER_LIKES_PRAGMA_MARK
262#pragma mark -
263#pragma mark - RR List Management & Task Management
264#endif
265
32bb7e43 266extern void ShowTaskSchedulingError(mDNS *const m);
263eeeab
A
267extern void mDNS_Lock_(mDNS *const m, const char * const functionname);
268extern void mDNS_Unlock_(mDNS *const m, const char * const functionname);
67c8f8a1 269
32bb7e43
A
270#if defined(_WIN32)
271 #define __func__ __FUNCTION__
272#endif
273
263eeeab 274#define mDNS_Lock(X) mDNS_Lock_((X), __func__)
67c8f8a1 275
263eeeab 276#define mDNS_Unlock(X) mDNS_Unlock_((X), __func__)
67c8f8a1
A
277
278#define mDNS_DropLockBeforeCallback() do { m->mDNS_reentrancy++; \
279 if (m->mDNS_busy != m->mDNS_reentrancy) LogMsg("%s: Locking Failure! mDNS_busy (%ld) != mDNS_reentrancy (%ld)", __func__, m->mDNS_busy, m->mDNS_reentrancy); \
280 } while (0)
281
282#define mDNS_ReclaimLockAfterCallback() do { \
283 if (m->mDNS_busy != m->mDNS_reentrancy) LogMsg("%s: Unlocking Failure! mDNS_busy (%ld) != mDNS_reentrancy (%ld)", __func__, m->mDNS_busy, m->mDNS_reentrancy); \
284 m->mDNS_reentrancy--; } while (0)
283ee3ff 285
8e92c31c
A
286#ifdef __cplusplus
287 }
288#endif
289
290#endif // __DNSCOMMON_H_