]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSCore/DNSCommon.c
mDNSResponder-258.21.tar.gz
[apple/mdnsresponder.git] / mDNSCore / DNSCommon.c
1 /* -*- Mode: C; tab-width: 4 -*-
2 *
3 * Copyright (c) 2002-2003 Apple Computer, Inc. All rights reserved.
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18 // Set mDNS_InstantiateInlines to tell mDNSEmbeddedAPI.h to instantiate inline functions, if necessary
19 #define mDNS_InstantiateInlines 1
20 #include "DNSCommon.h"
21
22 // Disable certain benign warnings with Microsoft compilers
23 #if (defined(_MSC_VER))
24 // Disable "conditional expression is constant" warning for debug macros.
25 // Otherwise, this generates warnings for the perfectly natural construct "while(1)"
26 // If someone knows a variant way of writing "while(1)" that doesn't generate warning messages, please let us know
27 #pragma warning(disable:4127)
28 // Disable "array is too small to include a terminating null character" warning
29 // -- domain labels have an initial length byte, not a terminating null character
30 #pragma warning(disable:4295)
31 #endif
32
33 // ***************************************************************************
34 #if COMPILER_LIKES_PRAGMA_MARK
35 #pragma mark - Program Constants
36 #endif
37
38 mDNSexport const mDNSInterfaceID mDNSInterface_Any = 0;
39 mDNSexport const mDNSInterfaceID mDNSInterfaceMark = (mDNSInterfaceID)-1;
40 mDNSexport const mDNSInterfaceID mDNSInterface_LocalOnly = (mDNSInterfaceID)-2;
41 mDNSexport const mDNSInterfaceID mDNSInterface_Unicast = (mDNSInterfaceID)-3;
42 mDNSexport const mDNSInterfaceID mDNSInterface_P2P = (mDNSInterfaceID)-4;
43
44 // Note: Microsoft's proposed "Link Local Multicast Name Resolution Protocol" (LLMNR) is essentially a limited version of
45 // Multicast DNS, using the same packet formats, naming syntax, and record types as Multicast DNS, but on a different UDP
46 // port and multicast address, which means it won't interoperate with the existing installed base of Multicast DNS responders.
47 // LLMNR uses IPv4 multicast address 224.0.0.252, IPv6 multicast address FF02::0001:0003, and UDP port 5355.
48 // Uncomment the appropriate lines below to build a special Multicast DNS responder for testing interoperability
49 // with Microsoft's LLMNR client code.
50
51 #define DiscardPortAsNumber 9
52 #define SSHPortAsNumber 22
53 #define UnicastDNSPortAsNumber 53
54 #define SSDPPortAsNumber 1900
55 #define IPSECPortAsNumber 4500
56 #define NSIPCPortAsNumber 5030 // Port used for dnsextd to talk to local nameserver bound to loopback
57 #define NATPMPAnnouncementPortAsNumber 5350
58 #define NATPMPPortAsNumber 5351
59 #define DNSEXTPortAsNumber 5352 // Port used for end-to-end DNS operations like LLQ, Updates with Leases, etc.
60 #define MulticastDNSPortAsNumber 5353
61 #define LoopbackIPCPortAsNumber 5354
62 //#define MulticastDNSPortAsNumber 5355 // LLMNR
63 #define PrivateDNSPortAsNumber 5533
64
65 mDNSexport const mDNSIPPort DiscardPort = { { DiscardPortAsNumber >> 8, DiscardPortAsNumber & 0xFF } };
66 mDNSexport const mDNSIPPort SSHPort = { { SSHPortAsNumber >> 8, SSHPortAsNumber & 0xFF } };
67 mDNSexport const mDNSIPPort UnicastDNSPort = { { UnicastDNSPortAsNumber >> 8, UnicastDNSPortAsNumber & 0xFF } };
68 mDNSexport const mDNSIPPort SSDPPort = { { SSDPPortAsNumber >> 8, SSDPPortAsNumber & 0xFF } };
69 mDNSexport const mDNSIPPort IPSECPort = { { IPSECPortAsNumber >> 8, IPSECPortAsNumber & 0xFF } };
70 mDNSexport const mDNSIPPort NSIPCPort = { { NSIPCPortAsNumber >> 8, NSIPCPortAsNumber & 0xFF } };
71 mDNSexport const mDNSIPPort NATPMPAnnouncementPort = { { NATPMPAnnouncementPortAsNumber >> 8, NATPMPAnnouncementPortAsNumber & 0xFF } };
72 mDNSexport const mDNSIPPort NATPMPPort = { { NATPMPPortAsNumber >> 8, NATPMPPortAsNumber & 0xFF } };
73 mDNSexport const mDNSIPPort DNSEXTPort = { { DNSEXTPortAsNumber >> 8, DNSEXTPortAsNumber & 0xFF } };
74 mDNSexport const mDNSIPPort MulticastDNSPort = { { MulticastDNSPortAsNumber >> 8, MulticastDNSPortAsNumber & 0xFF } };
75 mDNSexport const mDNSIPPort LoopbackIPCPort = { { LoopbackIPCPortAsNumber >> 8, LoopbackIPCPortAsNumber & 0xFF } };
76 mDNSexport const mDNSIPPort PrivateDNSPort = { { PrivateDNSPortAsNumber >> 8, PrivateDNSPortAsNumber & 0xFF } };
77
78 mDNSexport const OwnerOptData zeroOwner = { 0, 0, { { 0 } }, { { 0 } }, { { 0 } } };
79
80 mDNSexport const mDNSIPPort zeroIPPort = { { 0 } };
81 mDNSexport const mDNSv4Addr zerov4Addr = { { 0 } };
82 mDNSexport const mDNSv6Addr zerov6Addr = { { 0 } };
83 mDNSexport const mDNSEthAddr zeroEthAddr = { { 0 } };
84 mDNSexport const mDNSv4Addr onesIPv4Addr = { { 255, 255, 255, 255 } };
85 mDNSexport const mDNSv6Addr onesIPv6Addr = { { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 } };
86 mDNSexport const mDNSEthAddr onesEthAddr = { { 255, 255, 255, 255, 255, 255 } };
87 mDNSexport const mDNSAddr zeroAddr = { mDNSAddrType_None, {{{ 0 }}} };
88
89 mDNSexport const mDNSv4Addr AllDNSAdminGroup = { { 239, 255, 255, 251 } };
90 mDNSexport const mDNSv4Addr AllHosts_v4 = { { 224, 0, 0, 1 } }; // For NAT-PMP Annoucements
91 mDNSexport const mDNSv6Addr AllHosts_v6 = { { 0xFF,0x02,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x01 } };
92 mDNSexport const mDNSv6Addr NDP_prefix = { { 0xFF,0x02,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x01, 0xFF,0x00,0x00,0xFB } }; // FF02:0:0:0:0:1:FF00::/104
93 mDNSexport const mDNSEthAddr AllHosts_v6_Eth = { { 0x33, 0x33, 0x00, 0x00, 0x00, 0x01 } };
94 mDNSexport const mDNSAddr AllDNSLinkGroup_v4 = { mDNSAddrType_IPv4, { { { 224, 0, 0, 251 } } } };
95 //mDNSexport const mDNSAddr AllDNSLinkGroup_v4 = { mDNSAddrType_IPv4, { { { 224, 0, 0, 252 } } } }; // LLMNR
96 mDNSexport const mDNSAddr AllDNSLinkGroup_v6 = { mDNSAddrType_IPv6, { { { 0xFF,0x02,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0xFB } } } };
97 //mDNSexport const mDNSAddr AllDNSLinkGroup_v6 = { mDNSAddrType_IPv6, { { { 0xFF,0x02,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x01,0x00,0x03 } } } }; // LLMNR
98
99 mDNSexport const mDNSOpaque16 zeroID = { { 0, 0 } };
100 mDNSexport const mDNSOpaque16 onesID = { { 255, 255 } };
101 mDNSexport const mDNSOpaque16 QueryFlags = { { kDNSFlag0_QR_Query | kDNSFlag0_OP_StdQuery, 0 } };
102 mDNSexport const mDNSOpaque16 uQueryFlags = { { kDNSFlag0_QR_Query | kDNSFlag0_OP_StdQuery | kDNSFlag0_RD, 0 } };
103 mDNSexport const mDNSOpaque16 ResponseFlags = { { kDNSFlag0_QR_Response | kDNSFlag0_OP_StdQuery | kDNSFlag0_AA, 0 } };
104 mDNSexport const mDNSOpaque16 UpdateReqFlags = { { kDNSFlag0_QR_Query | kDNSFlag0_OP_Update, 0 } };
105 mDNSexport const mDNSOpaque16 UpdateRespFlags = { { kDNSFlag0_QR_Response | kDNSFlag0_OP_Update, 0 } };
106
107 mDNSexport const mDNSOpaque64 zeroOpaque64 = { { 0 } };
108
109 // ***************************************************************************
110 #if COMPILER_LIKES_PRAGMA_MARK
111 #pragma mark -
112 #pragma mark - General Utility Functions
113 #endif
114
115 // return true for RFC1918 private addresses
116 mDNSexport mDNSBool mDNSv4AddrIsRFC1918(mDNSv4Addr *addr)
117 {
118 return ((addr->b[0] == 10) || // 10/8 prefix
119 (addr->b[0] == 172 && (addr->b[1] & 0xF0) == 16) || // 172.16/12
120 (addr->b[0] == 192 && addr->b[1] == 168)); // 192.168/16
121 }
122
123 mDNSexport NetworkInterfaceInfo *GetFirstActiveInterface(NetworkInterfaceInfo *intf)
124 {
125 while (intf && !intf->InterfaceActive) intf = intf->next;
126 return(intf);
127 }
128
129 mDNSexport mDNSInterfaceID GetNextActiveInterfaceID(const NetworkInterfaceInfo *intf)
130 {
131 const NetworkInterfaceInfo *next = GetFirstActiveInterface(intf->next);
132 if (next) return(next->InterfaceID); else return(mDNSNULL);
133 }
134
135 mDNSexport mDNSu32 NumCacheRecordsForInterfaceID(const mDNS *const m, mDNSInterfaceID id)
136 {
137 mDNSu32 slot, used = 0;
138 CacheGroup *cg;
139 const CacheRecord *rr;
140 FORALL_CACHERECORDS(slot, cg, rr)
141 if (rr->resrec.InterfaceID == id) used++;
142 return(used);
143 }
144
145 mDNSexport char *DNSTypeName(mDNSu16 rrtype)
146 {
147 switch (rrtype)
148 {
149 case kDNSType_A: return("Addr");
150 case kDNSType_NS: return("NS");
151 case kDNSType_CNAME:return("CNAME");
152 case kDNSType_SOA: return("SOA");
153 case kDNSType_NULL: return("NULL");
154 case kDNSType_PTR: return("PTR");
155 case kDNSType_HINFO:return("HINFO");
156 case kDNSType_TXT: return("TXT");
157 case kDNSType_AAAA: return("AAAA");
158 case kDNSType_SRV: return("SRV");
159 case kDNSType_OPT: return("OPT");
160 case kDNSType_NSEC: return("NSEC");
161 case kDNSType_TSIG: return("TSIG");
162 case kDNSQType_ANY: return("ANY");
163 default: {
164 static char buffer[16];
165 mDNS_snprintf(buffer, sizeof(buffer), "(%d)", rrtype);
166 return(buffer);
167 }
168 }
169 }
170
171 // Note slight bug: this code uses the rdlength from the ResourceRecord object, to display
172 // the rdata from the RDataBody object. Sometimes this could be the wrong length -- but as
173 // long as this routine is only used for debugging messages, it probably isn't a big problem.
174 mDNSexport char *GetRRDisplayString_rdb(const ResourceRecord *const rr, const RDataBody *const rd1, char *const buffer)
175 {
176 const RDataBody2 *const rd = (RDataBody2 *)rd1;
177 #define RemSpc (MaxMsg-1-length)
178 char *ptr = buffer;
179 mDNSu32 length = mDNS_snprintf(buffer, MaxMsg-1, "%4d %##s %s ", rr->rdlength, rr->name->c, DNSTypeName(rr->rrtype));
180 if (rr->RecordType == kDNSRecordTypePacketNegative) return(buffer);
181 if (!rr->rdlength) { mDNS_snprintf(buffer+length, RemSpc, "<< ZERO RDATA LENGTH >>"); return(buffer); }
182
183 switch (rr->rrtype)
184 {
185 case kDNSType_A: mDNS_snprintf(buffer+length, RemSpc, "%.4a", &rd->ipv4); break;
186
187 case kDNSType_NS: // Same as PTR
188 case kDNSType_CNAME:// Same as PTR
189 case kDNSType_PTR: mDNS_snprintf(buffer+length, RemSpc, "%##s", rd->name.c); break;
190
191 case kDNSType_SOA: mDNS_snprintf(buffer+length, RemSpc, "%##s %##s %d %d %d %d %d",
192 rd->soa.mname.c, rd->soa.rname.c,
193 rd->soa.serial, rd->soa.refresh, rd->soa.retry, rd->soa.expire, rd->soa.min);
194 break;
195
196 case kDNSType_HINFO:// Display this the same as TXT (show all constituent strings)
197 case kDNSType_TXT: {
198 const mDNSu8 *t = rd->txt.c;
199 while (t < rd->txt.c + rr->rdlength)
200 {
201 length += mDNS_snprintf(buffer+length, RemSpc, "%s%#s", t > rd->txt.c ? "¦" : "", t);
202 t += 1 + t[0];
203 }
204 } break;
205
206 case kDNSType_AAAA: mDNS_snprintf(buffer+length, RemSpc, "%.16a", &rd->ipv6); break;
207 case kDNSType_SRV: mDNS_snprintf(buffer+length, RemSpc, "%u %u %u %##s",
208 rd->srv.priority, rd->srv.weight, mDNSVal16(rd->srv.port), rd->srv.target.c); break;
209
210 case kDNSType_OPT: {
211 const rdataOPT *opt;
212 const rdataOPT *const end = (const rdataOPT *)&rd->data[rr->rdlength];
213 length += mDNS_snprintf(buffer+length, RemSpc, "Max %d", rr->rrclass);
214 for (opt = &rd->opt[0]; opt < end; opt++)
215 {
216 switch(opt->opt)
217 {
218 case kDNSOpt_LLQ:
219 length += mDNS_snprintf(buffer+length, RemSpc, " Vers %d", opt->u.llq.vers);
220 length += mDNS_snprintf(buffer+length, RemSpc, " Op %d", opt->u.llq.llqOp);
221 length += mDNS_snprintf(buffer+length, RemSpc, " Err/Port %d", opt->u.llq.err);
222 length += mDNS_snprintf(buffer+length, RemSpc, " ID %08X%08X", opt->u.llq.id.l[0], opt->u.llq.id.l[1]);
223 length += mDNS_snprintf(buffer+length, RemSpc, " Lease %d", opt->u.llq.llqlease);
224 break;
225 case kDNSOpt_Lease:
226 length += mDNS_snprintf(buffer+length, RemSpc, " Lease %d", opt->u.updatelease);
227 break;
228 case kDNSOpt_Owner:
229 length += mDNS_snprintf(buffer+length, RemSpc, " Vers %d", opt->u.owner.vers);
230 length += mDNS_snprintf(buffer+length, RemSpc, " Seq %3d", (mDNSu8)opt->u.owner.seq); // Display as unsigned
231 length += mDNS_snprintf(buffer+length, RemSpc, " MAC %.6a", opt->u.owner.HMAC.b);
232 if (opt->optlen >= DNSOpt_OwnerData_ID_Wake_Space-4)
233 {
234 length += mDNS_snprintf(buffer+length, RemSpc, " I-MAC %.6a", opt->u.owner.IMAC.b);
235 if (opt->optlen > DNSOpt_OwnerData_ID_Wake_Space-4)
236 length += mDNS_snprintf(buffer+length, RemSpc, " Password %.6a", opt->u.owner.password.b);
237 }
238 break;
239 default:
240 length += mDNS_snprintf(buffer+length, RemSpc, " Unknown %d", opt->opt);
241 break;
242 }
243 }
244 }
245 break;
246
247 case kDNSType_NSEC: {
248 mDNSu16 i;
249 for (i=0; i<255; i++)
250 if (rd->nsec.bitmap[i>>3] & (128 >> (i&7)))
251 length += mDNS_snprintf(buffer+length, RemSpc, "%s ", DNSTypeName(i));
252 }
253 break;
254
255 default: mDNS_snprintf(buffer+length, RemSpc, "RDLen %d: %s", rr->rdlength, rd->data);
256 // Really should scan buffer to check if text is valid UTF-8 and only replace with dots if not
257 for (ptr = buffer; *ptr; ptr++) if (*ptr < ' ') *ptr = '.';
258 break;
259 }
260 return(buffer);
261 }
262
263 // See comments in mDNSEmbeddedAPI.h
264 #if _PLATFORM_HAS_STRONG_PRNG_
265 #define mDNSRandomNumber mDNSPlatformRandomNumber
266 #else
267 mDNSlocal mDNSu32 mDNSRandomFromSeed(mDNSu32 seed)
268 {
269 return seed * 21 + 1;
270 }
271
272 mDNSlocal mDNSu32 mDNSMixRandomSeed(mDNSu32 seed, mDNSu8 iteration)
273 {
274 return iteration ? mDNSMixRandomSeed(mDNSRandomFromSeed(seed), --iteration) : seed;
275 }
276
277 mDNSlocal mDNSu32 mDNSRandomNumber()
278 {
279 static mDNSBool seeded = mDNSfalse;
280 static mDNSu32 seed = 0;
281 if (!seeded)
282 {
283 seed = mDNSMixRandomSeed(mDNSPlatformRandomSeed(), 100);
284 seeded = mDNStrue;
285 }
286 return (seed = mDNSRandomFromSeed(seed));
287 }
288 #endif // ! _PLATFORM_HAS_STRONG_PRNG_
289
290 mDNSexport mDNSu32 mDNSRandom(mDNSu32 max) // Returns pseudo-random result from zero to max inclusive
291 {
292 mDNSu32 ret = 0;
293 mDNSu32 mask = 1;
294
295 while (mask < max) mask = (mask << 1) | 1;
296
297 do ret = mDNSRandomNumber() & mask;
298 while (ret > max);
299
300 return ret;
301 }
302
303 mDNSexport mDNSBool mDNSSameAddress(const mDNSAddr *ip1, const mDNSAddr *ip2)
304 {
305 if (ip1->type == ip2->type)
306 {
307 switch (ip1->type)
308 {
309 case mDNSAddrType_None : return(mDNStrue); // Empty addresses have no data and are therefore always equal
310 case mDNSAddrType_IPv4 : return(mDNSBool)(mDNSSameIPv4Address(ip1->ip.v4, ip2->ip.v4));
311 case mDNSAddrType_IPv6 : return(mDNSBool)(mDNSSameIPv6Address(ip1->ip.v6, ip2->ip.v6));
312 }
313 }
314 return(mDNSfalse);
315 }
316
317 mDNSexport mDNSBool mDNSAddrIsDNSMulticast(const mDNSAddr *ip)
318 {
319 switch(ip->type)
320 {
321 case mDNSAddrType_IPv4: return(mDNSBool)(mDNSSameIPv4Address(ip->ip.v4, AllDNSLinkGroup_v4.ip.v4));
322 case mDNSAddrType_IPv6: return(mDNSBool)(mDNSSameIPv6Address(ip->ip.v6, AllDNSLinkGroup_v6.ip.v6));
323 default: return(mDNSfalse);
324 }
325 }
326
327 // ***************************************************************************
328 #if COMPILER_LIKES_PRAGMA_MARK
329 #pragma mark -
330 #pragma mark - Domain Name Utility Functions
331 #endif
332
333 mDNSexport mDNSBool SameDomainLabel(const mDNSu8 *a, const mDNSu8 *b)
334 {
335 int i;
336 const int len = *a++;
337
338 if (len > MAX_DOMAIN_LABEL)
339 { debugf("Malformed label (too long)"); return(mDNSfalse); }
340
341 if (len != *b++) return(mDNSfalse);
342 for (i=0; i<len; i++)
343 {
344 mDNSu8 ac = *a++;
345 mDNSu8 bc = *b++;
346 if (mDNSIsUpperCase(ac)) ac += 'a' - 'A';
347 if (mDNSIsUpperCase(bc)) bc += 'a' - 'A';
348 if (ac != bc) return(mDNSfalse);
349 }
350 return(mDNStrue);
351 }
352
353 mDNSexport mDNSBool SameDomainName(const domainname *const d1, const domainname *const d2)
354 {
355 const mDNSu8 * a = d1->c;
356 const mDNSu8 * b = d2->c;
357 const mDNSu8 *const max = d1->c + MAX_DOMAIN_NAME; // Maximum that's valid
358
359 while (*a || *b)
360 {
361 if (a + 1 + *a >= max)
362 { debugf("Malformed domain name (more than 256 characters)"); return(mDNSfalse); }
363 if (!SameDomainLabel(a, b)) return(mDNSfalse);
364 a += 1 + *a;
365 b += 1 + *b;
366 }
367
368 return(mDNStrue);
369 }
370
371 mDNSexport mDNSBool SameDomainNameCS(const domainname *const d1, const domainname *const d2)
372 {
373 mDNSu16 l1 = DomainNameLength(d1);
374 mDNSu16 l2 = DomainNameLength(d2);
375 return(l1 <= MAX_DOMAIN_NAME && l1 == l2 && mDNSPlatformMemSame(d1, d2, l1));
376 }
377
378 mDNSexport mDNSBool IsLocalDomain(const domainname *d)
379 {
380 // Domains that are defined to be resolved via link-local multicast are:
381 // local., 254.169.in-addr.arpa., and {8,9,A,B}.E.F.ip6.arpa.
382 static const domainname *nL = (const domainname*)"\x5" "local";
383 static const domainname *nR = (const domainname*)"\x3" "254" "\x3" "169" "\x7" "in-addr" "\x4" "arpa";
384 static const domainname *n8 = (const domainname*)"\x1" "8" "\x1" "e" "\x1" "f" "\x3" "ip6" "\x4" "arpa";
385 static const domainname *n9 = (const domainname*)"\x1" "9" "\x1" "e" "\x1" "f" "\x3" "ip6" "\x4" "arpa";
386 static const domainname *nA = (const domainname*)"\x1" "a" "\x1" "e" "\x1" "f" "\x3" "ip6" "\x4" "arpa";
387 static const domainname *nB = (const domainname*)"\x1" "b" "\x1" "e" "\x1" "f" "\x3" "ip6" "\x4" "arpa";
388
389 const domainname *d1, *d2, *d3, *d4, *d5; // Top-level domain, second-level domain, etc.
390 d1 = d2 = d3 = d4 = d5 = mDNSNULL;
391 while (d->c[0])
392 {
393 d5 = d4; d4 = d3; d3 = d2; d2 = d1; d1 = d;
394 d = (const domainname*)(d->c + 1 + d->c[0]);
395 }
396
397 if (d1 && SameDomainName(d1, nL)) return(mDNStrue);
398 if (d4 && SameDomainName(d4, nR)) return(mDNStrue);
399 if (d5 && SameDomainName(d5, n8)) return(mDNStrue);
400 if (d5 && SameDomainName(d5, n9)) return(mDNStrue);
401 if (d5 && SameDomainName(d5, nA)) return(mDNStrue);
402 if (d5 && SameDomainName(d5, nB)) return(mDNStrue);
403 return(mDNSfalse);
404 }
405
406 mDNSexport const mDNSu8 *LastLabel(const domainname *d)
407 {
408 const mDNSu8 *p = d->c;
409 while (d->c[0])
410 {
411 p = d->c;
412 d = (const domainname*)(d->c + 1 + d->c[0]);
413 }
414 return(p);
415 }
416
417 // Returns length of a domain name INCLUDING the byte for the final null label
418 // e.g. for the root label "." it returns one
419 // For the FQDN "com." it returns 5 (length byte, three data bytes, final zero)
420 // Legal results are 1 (just root label) to 256 (MAX_DOMAIN_NAME)
421 // If the given domainname is invalid, result is 257 (MAX_DOMAIN_NAME+1)
422 mDNSexport mDNSu16 DomainNameLengthLimit(const domainname *const name, const mDNSu8 *limit)
423 {
424 const mDNSu8 *src = name->c;
425 while (src < limit && *src <= MAX_DOMAIN_LABEL)
426 {
427 if (*src == 0) return((mDNSu16)(src - name->c + 1));
428 src += 1 + *src;
429 }
430 return(MAX_DOMAIN_NAME+1);
431 }
432
433 // CompressedDomainNameLength returns the length of a domain name INCLUDING the byte
434 // for the final null label, e.g. for the root label "." it returns one.
435 // E.g. for the FQDN "foo.com." it returns 9
436 // (length, three data bytes, length, three more data bytes, final zero).
437 // In the case where a parent domain name is provided, and the given name is a child
438 // of that parent, CompressedDomainNameLength returns the length of the prefix portion
439 // of the child name, plus TWO bytes for the compression pointer.
440 // E.g. for the name "foo.com." with parent "com.", it returns 6
441 // (length, three data bytes, two-byte compression pointer).
442 mDNSexport mDNSu16 CompressedDomainNameLength(const domainname *const name, const domainname *parent)
443 {
444 const mDNSu8 *src = name->c;
445 if (parent && parent->c[0] == 0) parent = mDNSNULL;
446 while (*src)
447 {
448 if (*src > MAX_DOMAIN_LABEL) return(MAX_DOMAIN_NAME+1);
449 if (parent && SameDomainName((const domainname *)src, parent)) return((mDNSu16)(src - name->c + 2));
450 src += 1 + *src;
451 if (src - name->c >= MAX_DOMAIN_NAME) return(MAX_DOMAIN_NAME+1);
452 }
453 return((mDNSu16)(src - name->c + 1));
454 }
455
456 // CountLabels() returns number of labels in name, excluding final root label
457 // (e.g. for "apple.com." CountLabels returns 2.)
458 mDNSexport int CountLabels(const domainname *d)
459 {
460 int count = 0;
461 const mDNSu8 *ptr;
462 for (ptr = d->c; *ptr; ptr = ptr + ptr[0] + 1) count++;
463 return count;
464 }
465
466 // SkipLeadingLabels skips over the first 'skip' labels in the domainname,
467 // returning a pointer to the suffix with 'skip' labels removed.
468 mDNSexport const domainname *SkipLeadingLabels(const domainname *d, int skip)
469 {
470 while (skip > 0 && d->c[0]) { d = (const domainname *)(d->c + 1 + d->c[0]); skip--; }
471 return(d);
472 }
473
474 // AppendLiteralLabelString appends a single label to an existing (possibly empty) domainname.
475 // The C string contains the label as-is, with no escaping, etc.
476 // Any dots in the name are literal dots, not label separators
477 // If successful, AppendLiteralLabelString returns a pointer to the next unused byte
478 // in the domainname bufer (i.e. the next byte after the terminating zero).
479 // If unable to construct a legal domain name (i.e. label more than 63 bytes, or total more than 256 bytes)
480 // AppendLiteralLabelString returns mDNSNULL.
481 mDNSexport mDNSu8 *AppendLiteralLabelString(domainname *const name, const char *cstr)
482 {
483 mDNSu8 * ptr = name->c + DomainNameLength(name) - 1; // Find end of current name
484 const mDNSu8 *const lim1 = name->c + MAX_DOMAIN_NAME - 1; // Limit of how much we can add (not counting final zero)
485 const mDNSu8 *const lim2 = ptr + 1 + MAX_DOMAIN_LABEL;
486 const mDNSu8 *const lim = (lim1 < lim2) ? lim1 : lim2;
487 mDNSu8 *lengthbyte = ptr++; // Record where the length is going to go
488
489 while (*cstr && ptr < lim) *ptr++ = (mDNSu8)*cstr++; // Copy the data
490 *lengthbyte = (mDNSu8)(ptr - lengthbyte - 1); // Fill in the length byte
491 *ptr++ = 0; // Put the null root label on the end
492 if (*cstr) return(mDNSNULL); // Failure: We didn't successfully consume all input
493 else return(ptr); // Success: return new value of ptr
494 }
495
496 // AppendDNSNameString appends zero or more labels to an existing (possibly empty) domainname.
497 // The C string is in conventional DNS syntax:
498 // Textual labels, escaped as necessary using the usual DNS '\' notation, separated by dots.
499 // If successful, AppendDNSNameString returns a pointer to the next unused byte
500 // in the domainname bufer (i.e. the next byte after the terminating zero).
501 // If unable to construct a legal domain name (i.e. label more than 63 bytes, or total more than 256 bytes)
502 // AppendDNSNameString returns mDNSNULL.
503 mDNSexport mDNSu8 *AppendDNSNameString(domainname *const name, const char *cstring)
504 {
505 const char *cstr = cstring;
506 mDNSu8 * ptr = name->c + DomainNameLength(name) - 1; // Find end of current name
507 const mDNSu8 *const lim = name->c + MAX_DOMAIN_NAME - 1; // Limit of how much we can add (not counting final zero)
508 while (*cstr && ptr < lim) // While more characters, and space to put them...
509 {
510 mDNSu8 *lengthbyte = ptr++; // Record where the length is going to go
511 if (*cstr == '.') { LogMsg("AppendDNSNameString: Illegal empty label in name \"%s\"", cstring); return(mDNSNULL); }
512 while (*cstr && *cstr != '.' && ptr < lim) // While we have characters in the label...
513 {
514 mDNSu8 c = (mDNSu8)*cstr++; // Read the character
515 if (c == '\\') // If escape character, check next character
516 {
517 c = (mDNSu8)*cstr++; // Assume we'll just take the next character
518 if (mDNSIsDigit(cstr[-1]) && mDNSIsDigit(cstr[0]) && mDNSIsDigit(cstr[1]))
519 { // If three decimal digits,
520 int v0 = cstr[-1] - '0'; // then interpret as three-digit decimal
521 int v1 = cstr[ 0] - '0';
522 int v2 = cstr[ 1] - '0';
523 int val = v0 * 100 + v1 * 10 + v2;
524 if (val <= 255) { c = (mDNSu8)val; cstr += 2; } // If valid three-digit decimal value, use it
525 }
526 }
527 *ptr++ = c; // Write the character
528 }
529 if (*cstr) cstr++; // Skip over the trailing dot (if present)
530 if (ptr - lengthbyte - 1 > MAX_DOMAIN_LABEL) // If illegal label, abort
531 return(mDNSNULL);
532 *lengthbyte = (mDNSu8)(ptr - lengthbyte - 1); // Fill in the length byte
533 }
534
535 *ptr++ = 0; // Put the null root label on the end
536 if (*cstr) return(mDNSNULL); // Failure: We didn't successfully consume all input
537 else return(ptr); // Success: return new value of ptr
538 }
539
540 // AppendDomainLabel appends a single label to a name.
541 // If successful, AppendDomainLabel returns a pointer to the next unused byte
542 // in the domainname bufer (i.e. the next byte after the terminating zero).
543 // If unable to construct a legal domain name (i.e. label more than 63 bytes, or total more than 256 bytes)
544 // AppendDomainLabel returns mDNSNULL.
545 mDNSexport mDNSu8 *AppendDomainLabel(domainname *const name, const domainlabel *const label)
546 {
547 int i;
548 mDNSu8 *ptr = name->c + DomainNameLength(name) - 1;
549
550 // Check label is legal
551 if (label->c[0] > MAX_DOMAIN_LABEL) return(mDNSNULL);
552
553 // Check that ptr + length byte + data bytes + final zero does not exceed our limit
554 if (ptr + 1 + label->c[0] + 1 > name->c + MAX_DOMAIN_NAME) return(mDNSNULL);
555
556 for (i=0; i<=label->c[0]; i++) *ptr++ = label->c[i]; // Copy the label data
557 *ptr++ = 0; // Put the null root label on the end
558 return(ptr);
559 }
560
561 mDNSexport mDNSu8 *AppendDomainName(domainname *const name, const domainname *const append)
562 {
563 mDNSu8 * ptr = name->c + DomainNameLength(name) - 1; // Find end of current name
564 const mDNSu8 *const lim = name->c + MAX_DOMAIN_NAME - 1; // Limit of how much we can add (not counting final zero)
565 const mDNSu8 * src = append->c;
566 while (src[0])
567 {
568 int i;
569 if (ptr + 1 + src[0] > lim) return(mDNSNULL);
570 for (i=0; i<=src[0]; i++) *ptr++ = src[i];
571 *ptr = 0; // Put the null root label on the end
572 src += i;
573 }
574 return(ptr);
575 }
576
577 // MakeDomainLabelFromLiteralString makes a single domain label from a single literal C string (with no escaping).
578 // If successful, MakeDomainLabelFromLiteralString returns mDNStrue.
579 // If unable to convert the whole string to a legal domain label (i.e. because length is more than 63 bytes) then
580 // MakeDomainLabelFromLiteralString makes a legal domain label from the first 63 bytes of the string and returns mDNSfalse.
581 // In some cases silently truncated oversized names to 63 bytes is acceptable, so the return result may be ignored.
582 // In other cases silent truncation may not be acceptable, so in those cases the calling function needs to check the return result.
583 mDNSexport mDNSBool MakeDomainLabelFromLiteralString(domainlabel *const label, const char *cstr)
584 {
585 mDNSu8 * ptr = label->c + 1; // Where we're putting it
586 const mDNSu8 *const limit = label->c + 1 + MAX_DOMAIN_LABEL; // The maximum we can put
587 while (*cstr && ptr < limit) *ptr++ = (mDNSu8)*cstr++; // Copy the label
588 label->c[0] = (mDNSu8)(ptr - label->c - 1); // Set the length byte
589 return(*cstr == 0); // Return mDNStrue if we successfully consumed all input
590 }
591
592 // MakeDomainNameFromDNSNameString makes a native DNS-format domainname from a C string.
593 // The C string is in conventional DNS syntax:
594 // Textual labels, escaped as necessary using the usual DNS '\' notation, separated by dots.
595 // If successful, MakeDomainNameFromDNSNameString returns a pointer to the next unused byte
596 // in the domainname bufer (i.e. the next byte after the terminating zero).
597 // If unable to construct a legal domain name (i.e. label more than 63 bytes, or total more than 256 bytes)
598 // MakeDomainNameFromDNSNameString returns mDNSNULL.
599 mDNSexport mDNSu8 *MakeDomainNameFromDNSNameString(domainname *const name, const char *cstr)
600 {
601 name->c[0] = 0; // Make an empty domain name
602 return(AppendDNSNameString(name, cstr)); // And then add this string to it
603 }
604
605 mDNSexport char *ConvertDomainLabelToCString_withescape(const domainlabel *const label, char *ptr, char esc)
606 {
607 const mDNSu8 * src = label->c; // Domain label we're reading
608 const mDNSu8 len = *src++; // Read length of this (non-null) label
609 const mDNSu8 *const end = src + len; // Work out where the label ends
610 if (len > MAX_DOMAIN_LABEL) return(mDNSNULL); // If illegal label, abort
611 while (src < end) // While we have characters in the label
612 {
613 mDNSu8 c = *src++;
614 if (esc)
615 {
616 if (c == '.' || c == esc) // If character is a dot or the escape character
617 *ptr++ = esc; // Output escape character
618 else if (c <= ' ') // If non-printing ascii,
619 { // Output decimal escape sequence
620 *ptr++ = esc;
621 *ptr++ = (char) ('0' + (c / 100) );
622 *ptr++ = (char) ('0' + (c / 10) % 10);
623 c = (mDNSu8)('0' + (c ) % 10);
624 }
625 }
626 *ptr++ = (char)c; // Copy the character
627 }
628 *ptr = 0; // Null-terminate the string
629 return(ptr); // and return
630 }
631
632 // Note: To guarantee that there will be no possible overrun, cstr must be at least MAX_ESCAPED_DOMAIN_NAME (1009 bytes)
633 mDNSexport char *ConvertDomainNameToCString_withescape(const domainname *const name, char *ptr, char esc)
634 {
635 const mDNSu8 *src = name->c; // Domain name we're reading
636 const mDNSu8 *const max = name->c + MAX_DOMAIN_NAME; // Maximum that's valid
637
638 if (*src == 0) *ptr++ = '.'; // Special case: For root, just write a dot
639
640 while (*src) // While more characters in the domain name
641 {
642 if (src + 1 + *src >= max) return(mDNSNULL);
643 ptr = ConvertDomainLabelToCString_withescape((const domainlabel *)src, ptr, esc);
644 if (!ptr) return(mDNSNULL);
645 src += 1 + *src;
646 *ptr++ = '.'; // Write the dot after the label
647 }
648
649 *ptr++ = 0; // Null-terminate the string
650 return(ptr); // and return
651 }
652
653 // RFC 1034 rules:
654 // Host names must start with a letter, end with a letter or digit,
655 // and have as interior characters only letters, digits, and hyphen.
656 // This was subsequently modified in RFC 1123 to allow the first character to be either a letter or a digit
657
658 mDNSexport void ConvertUTF8PstringToRFC1034HostLabel(const mDNSu8 UTF8Name[], domainlabel *const hostlabel)
659 {
660 const mDNSu8 * src = &UTF8Name[1];
661 const mDNSu8 *const end = &UTF8Name[1] + UTF8Name[0];
662 mDNSu8 * ptr = &hostlabel->c[1];
663 const mDNSu8 *const lim = &hostlabel->c[1] + MAX_DOMAIN_LABEL;
664 while (src < end)
665 {
666 // Delete apostrophes from source name
667 if (src[0] == '\'') { src++; continue; } // Standard straight single quote
668 if (src + 2 < end && src[0] == 0xE2 && src[1] == 0x80 && src[2] == 0x99)
669 { src += 3; continue; } // Unicode curly apostrophe
670 if (ptr < lim)
671 {
672 if (mDNSValidHostChar(*src, (ptr > &hostlabel->c[1]), (src < end-1))) *ptr++ = *src;
673 else if (ptr > &hostlabel->c[1] && ptr[-1] != '-') *ptr++ = '-';
674 }
675 src++;
676 }
677 while (ptr > &hostlabel->c[1] && ptr[-1] == '-') ptr--; // Truncate trailing '-' marks
678 hostlabel->c[0] = (mDNSu8)(ptr - &hostlabel->c[1]);
679 }
680
681 #define ValidTransportProtocol(X) ( (X)[0] == 4 && (X)[1] == '_' && \
682 ((((X)[2] | 0x20) == 'u' && ((X)[3] | 0x20) == 'd') || (((X)[2] | 0x20) == 't' && ((X)[3] | 0x20) == 'c')) && \
683 ((X)[4] | 0x20) == 'p')
684
685 mDNSexport mDNSu8 *ConstructServiceName(domainname *const fqdn,
686 const domainlabel *name, const domainname *type, const domainname *const domain)
687 {
688 int i, len;
689 mDNSu8 *dst = fqdn->c;
690 const mDNSu8 *src;
691 const char *errormsg;
692 #if APPLE_OSX_mDNSResponder
693 mDNSBool loggedUnderscore = mDNSfalse;
694 static char typeBuf[MAX_ESCAPED_DOMAIN_NAME];
695 #endif
696
697 // In the case where there is no name (and ONLY in that case),
698 // a single-label subtype is allowed as the first label of a three-part "type"
699 if (!name && type)
700 {
701 const mDNSu8 *s0 = type->c;
702 if (s0[0] && s0[0] < 0x40) // If legal first label (at least one character, and no more than 63)
703 {
704 const mDNSu8 * s1 = s0 + 1 + s0[0];
705 if (s1[0] && s1[0] < 0x40) // and legal second label (at least one character, and no more than 63)
706 {
707 const mDNSu8 *s2 = s1 + 1 + s1[0];
708 if (s2[0] && s2[0] < 0x40 && s2[1+s2[0]] == 0) // and we have three and only three labels
709 {
710 static const mDNSu8 SubTypeLabel[5] = "\x04_sub";
711 src = s0; // Copy the first label
712 len = *src;
713 for (i=0; i <= len; i++) *dst++ = *src++;
714 for (i=0; i < (int)sizeof(SubTypeLabel); i++) *dst++ = SubTypeLabel[i];
715 type = (const domainname *)s1;
716
717 // Special support to enable the DNSServiceBrowse call made by Bonjour Browser
718 // For these queries, we retract the "._sub" we just added between the subtype and the main type
719 // Remove after Bonjour Browser is updated to use DNSServiceQueryRecord instead of DNSServiceBrowse
720 if (SameDomainName((domainname*)s0, (const domainname*)"\x09_services\x07_dns-sd\x04_udp"))
721 dst -= sizeof(SubTypeLabel);
722 }
723 }
724 }
725 }
726
727 if (name && name->c[0])
728 {
729 src = name->c; // Put the service name into the domain name
730 len = *src;
731 if (len >= 0x40) { errormsg = "Service instance name too long"; goto fail; }
732 for (i=0; i<=len; i++) *dst++ = *src++;
733 }
734 else
735 name = (domainlabel*)""; // Set this up to be non-null, to avoid errors if we have to call LogMsg() below
736
737 src = type->c; // Put the service type into the domain name
738 len = *src;
739 if (len < 2 || len > 16)
740 {
741 LogMsg("Bad service type in %#s.%##s%##s Application protocol name must be underscore plus 1-15 characters. "
742 "See <http://www.dns-sd.org/ServiceTypes.html>", name->c, type->c, domain->c);
743 #if APPLE_OSX_mDNSResponder
744 ConvertDomainNameToCString(type, typeBuf);
745 mDNSASLLog(mDNSNULL, "serviceType.nameTooLong", "noop", typeBuf, "");
746 #endif
747 }
748 if (len < 2 || len >= 0x40 || (len > 16 && !SameDomainName(domain, &localdomain))) return(mDNSNULL);
749 if (src[1] != '_') { errormsg = "Application protocol name must begin with underscore"; goto fail; }
750 for (i=2; i<=len; i++)
751 {
752 // Letters and digits are allowed anywhere
753 if (mDNSIsLetter(src[i]) || mDNSIsDigit(src[i])) continue;
754 // Hyphens are only allowed as interior characters
755 // Underscores are not supposed to be allowed at all, but for backwards compatibility with some old products we do allow them,
756 // with the same rule as hyphens
757 if ((src[i] == '-' || src[i] == '_') && i > 2 && i < len)
758 {
759 #if APPLE_OSX_mDNSResponder
760 if (src[i] == '_' && loggedUnderscore == mDNSfalse)
761 {
762 ConvertDomainNameToCString(type, typeBuf);
763 mDNSASLLog(mDNSNULL, "serviceType.nameWithUnderscore", "noop", typeBuf, "");
764 loggedUnderscore = mDNStrue;
765 }
766 #endif
767 continue;
768 }
769 errormsg = "Application protocol name must contain only letters, digits, and hyphens";
770 #if APPLE_OSX_mDNSResponder
771 {
772 ConvertDomainNameToCString(type, typeBuf);
773 mDNSASLLog(mDNSNULL, "serviceType.nameWithIllegalCharacters", "noop", typeBuf, "");
774 }
775 #endif
776 goto fail;
777 }
778 for (i=0; i<=len; i++) *dst++ = *src++;
779
780 len = *src;
781 if (!ValidTransportProtocol(src)) { errormsg = "Transport protocol name must be _udp or _tcp"; goto fail; }
782 for (i=0; i<=len; i++) *dst++ = *src++;
783
784 if (*src) { errormsg = "Service type must have only two labels"; goto fail; }
785
786 *dst = 0;
787 if (!domain->c[0]) { errormsg = "Service domain must be non-empty"; goto fail; }
788 if (SameDomainName(domain, (const domainname*)"\x05" "local" "\x04" "arpa"))
789 { errormsg = "Illegal domain \"local.arpa.\" Use \"local.\" (or empty string)"; goto fail; }
790 dst = AppendDomainName(fqdn, domain);
791 if (!dst) { errormsg = "Service domain too long"; goto fail; }
792 return(dst);
793
794 fail:
795 LogMsg("ConstructServiceName: %s: %#s.%##s%##s", errormsg, name->c, type->c, domain->c);
796 return(mDNSNULL);
797 }
798
799 // A service name has the form: instance.application-protocol.transport-protocol.domain
800 // DeconstructServiceName is currently fairly forgiving: It doesn't try to enforce character
801 // set or length limits for the protocol names, and the final domain is allowed to be empty.
802 // However, if the given FQDN doesn't contain at least three labels,
803 // DeconstructServiceName will reject it and return mDNSfalse.
804 mDNSexport mDNSBool DeconstructServiceName(const domainname *const fqdn,
805 domainlabel *const name, domainname *const type, domainname *const domain)
806 {
807 int i, len;
808 const mDNSu8 *src = fqdn->c;
809 const mDNSu8 *max = fqdn->c + MAX_DOMAIN_NAME;
810 mDNSu8 *dst;
811
812 dst = name->c; // Extract the service name
813 len = *src;
814 if (!len) { debugf("DeconstructServiceName: FQDN empty!"); return(mDNSfalse); }
815 if (len >= 0x40) { debugf("DeconstructServiceName: Instance name too long"); return(mDNSfalse); }
816 for (i=0; i<=len; i++) *dst++ = *src++;
817
818 dst = type->c; // Extract the service type
819 len = *src;
820 if (!len) { debugf("DeconstructServiceName: FQDN contains only one label!"); return(mDNSfalse); }
821 if (len >= 0x40) { debugf("DeconstructServiceName: Application protocol name too long"); return(mDNSfalse); }
822 if (src[1] != '_'){ debugf("DeconstructServiceName: No _ at start of application protocol"); return(mDNSfalse); }
823 for (i=0; i<=len; i++) *dst++ = *src++;
824
825 len = *src;
826 if (!len) { debugf("DeconstructServiceName: FQDN contains only two labels!"); return(mDNSfalse); }
827 if (!ValidTransportProtocol(src))
828 { debugf("DeconstructServiceName: Transport protocol must be _udp or _tcp"); return(mDNSfalse); }
829 for (i=0; i<=len; i++) *dst++ = *src++;
830 *dst++ = 0; // Put terminator on the end of service type
831
832 dst = domain->c; // Extract the service domain
833 while (*src)
834 {
835 len = *src;
836 if (len >= 0x40)
837 { debugf("DeconstructServiceName: Label in service domain too long"); return(mDNSfalse); }
838 if (src + 1 + len + 1 >= max)
839 { debugf("DeconstructServiceName: Total service domain too long"); return(mDNSfalse); }
840 for (i=0; i<=len; i++) *dst++ = *src++;
841 }
842 *dst++ = 0; // Put the null root label on the end
843
844 return(mDNStrue);
845 }
846
847 // Notes on UTF-8:
848 // 0xxxxxxx represents a 7-bit ASCII value from 0x00 to 0x7F
849 // 10xxxxxx is a continuation byte of a multi-byte character
850 // 110xxxxx is the first byte of a 2-byte character (11 effective bits; values 0x 80 - 0x 800-1)
851 // 1110xxxx is the first byte of a 3-byte character (16 effective bits; values 0x 800 - 0x 10000-1)
852 // 11110xxx is the first byte of a 4-byte character (21 effective bits; values 0x 10000 - 0x 200000-1)
853 // 111110xx is the first byte of a 5-byte character (26 effective bits; values 0x 200000 - 0x 4000000-1)
854 // 1111110x is the first byte of a 6-byte character (31 effective bits; values 0x4000000 - 0x80000000-1)
855 //
856 // UTF-16 surrogate pairs are used in UTF-16 to encode values larger than 0xFFFF.
857 // Although UTF-16 surrogate pairs are not supposed to appear in legal UTF-8, we want to be defensive
858 // about that too. (See <http://www.unicode.org/faq/utf_bom.html#34>, "What are surrogates?")
859 // The first of pair is a UTF-16 value in the range 0xD800-0xDBFF (11101101 1010xxxx 10xxxxxx in UTF-8),
860 // and the second is a UTF-16 value in the range 0xDC00-0xDFFF (11101101 1011xxxx 10xxxxxx in UTF-8).
861
862 mDNSexport mDNSu32 TruncateUTF8ToLength(mDNSu8 *string, mDNSu32 length, mDNSu32 max)
863 {
864 if (length > max)
865 {
866 mDNSu8 c1 = string[max]; // First byte after cut point
867 mDNSu8 c2 = (max+1 < length) ? string[max+1] : (mDNSu8)0xB0; // Second byte after cut point
868 length = max; // Trim length down
869 while (length > 0)
870 {
871 // Check if the byte right after the chop point is a UTF-8 continuation byte,
872 // or if the character right after the chop point is the second of a UTF-16 surrogate pair.
873 // If so, then we continue to chop more bytes until we get to a legal chop point.
874 mDNSBool continuation = ((c1 & 0xC0) == 0x80);
875 mDNSBool secondsurrogate = (c1 == 0xED && (c2 & 0xF0) == 0xB0);
876 if (!continuation && !secondsurrogate) break;
877 c2 = c1;
878 c1 = string[--length];
879 }
880 // Having truncated characters off the end of our string, also cut off any residual white space
881 while (length > 0 && string[length-1] <= ' ') length--;
882 }
883 return(length);
884 }
885
886 // Returns true if a rich text label ends in " (nnn)", or if an RFC 1034
887 // name ends in "-nnn", where n is some decimal number.
888 mDNSexport mDNSBool LabelContainsSuffix(const domainlabel *const name, const mDNSBool RichText)
889 {
890 mDNSu16 l = name->c[0];
891
892 if (RichText)
893 {
894 if (l < 4) return mDNSfalse; // Need at least " (2)"
895 if (name->c[l--] != ')') return mDNSfalse; // Last char must be ')'
896 if (!mDNSIsDigit(name->c[l])) return mDNSfalse; // Preceeded by a digit
897 l--;
898 while (l > 2 && mDNSIsDigit(name->c[l])) l--; // Strip off digits
899 return (name->c[l] == '(' && name->c[l - 1] == ' ');
900 }
901 else
902 {
903 if (l < 2) return mDNSfalse; // Need at least "-2"
904 if (!mDNSIsDigit(name->c[l])) return mDNSfalse; // Last char must be a digit
905 l--;
906 while (l > 2 && mDNSIsDigit(name->c[l])) l--; // Strip off digits
907 return (name->c[l] == '-');
908 }
909 }
910
911 // removes an auto-generated suffix (appended on a name collision) from a label. caller is
912 // responsible for ensuring that the label does indeed contain a suffix. returns the number
913 // from the suffix that was removed.
914 mDNSexport mDNSu32 RemoveLabelSuffix(domainlabel *name, mDNSBool RichText)
915 {
916 mDNSu32 val = 0, multiplier = 1;
917
918 // Chop closing parentheses from RichText suffix
919 if (RichText && name->c[0] >= 1 && name->c[name->c[0]] == ')') name->c[0]--;
920
921 // Get any existing numerical suffix off the name
922 while (mDNSIsDigit(name->c[name->c[0]]))
923 { val += (name->c[name->c[0]] - '0') * multiplier; multiplier *= 10; name->c[0]--; }
924
925 // Chop opening parentheses or dash from suffix
926 if (RichText)
927 {
928 if (name->c[0] >= 2 && name->c[name->c[0]] == '(' && name->c[name->c[0]-1] == ' ') name->c[0] -= 2;
929 }
930 else
931 {
932 if (name->c[0] >= 1 && name->c[name->c[0]] == '-') name->c[0] -= 1;
933 }
934
935 return(val);
936 }
937
938 // appends a numerical suffix to a label, with the number following a whitespace and enclosed
939 // in parentheses (rich text) or following two consecutive hyphens (RFC 1034 domain label).
940 mDNSexport void AppendLabelSuffix(domainlabel *const name, mDNSu32 val, const mDNSBool RichText)
941 {
942 mDNSu32 divisor = 1, chars = 2; // Shortest possible RFC1034 name suffix is 2 characters ("-2")
943 if (RichText) chars = 4; // Shortest possible RichText suffix is 4 characters (" (2)")
944
945 // Truncate trailing spaces from RichText names
946 if (RichText) while (name->c[name->c[0]] == ' ') name->c[0]--;
947
948 while (divisor < 0xFFFFFFFFUL/10 && val >= divisor * 10) { divisor *= 10; chars++; }
949
950 name->c[0] = (mDNSu8) TruncateUTF8ToLength(name->c+1, name->c[0], MAX_DOMAIN_LABEL - chars);
951
952 if (RichText) { name->c[++name->c[0]] = ' '; name->c[++name->c[0]] = '('; }
953 else { name->c[++name->c[0]] = '-'; }
954
955 while (divisor)
956 {
957 name->c[++name->c[0]] = (mDNSu8)('0' + val / divisor);
958 val %= divisor;
959 divisor /= 10;
960 }
961
962 if (RichText) name->c[++name->c[0]] = ')';
963 }
964
965 mDNSexport void IncrementLabelSuffix(domainlabel *name, mDNSBool RichText)
966 {
967 mDNSu32 val = 0;
968
969 if (LabelContainsSuffix(name, RichText))
970 val = RemoveLabelSuffix(name, RichText);
971
972 // If no existing suffix, start by renaming "Foo" as "Foo (2)" or "Foo-2" as appropriate.
973 // If existing suffix in the range 2-9, increment it.
974 // If we've had ten conflicts already, there are probably too many hosts trying to use the same name,
975 // so add a random increment to improve the chances of finding an available name next time.
976 if (val == 0) val = 2;
977 else if (val < 10) val++;
978 else val += 1 + mDNSRandom(99);
979
980 AppendLabelSuffix(name, val, RichText);
981 }
982
983 // ***************************************************************************
984 #if COMPILER_LIKES_PRAGMA_MARK
985 #pragma mark -
986 #pragma mark - Resource Record Utility Functions
987 #endif
988
989 // Set up a AuthRecord with sensible default values.
990 // These defaults may be overwritten with new values before mDNS_Register is called
991 mDNSexport void mDNS_SetupResourceRecord(AuthRecord *rr, RData *RDataStorage, mDNSInterfaceID InterfaceID,
992 mDNSu16 rrtype, mDNSu32 ttl, mDNSu8 RecordType, mDNSRecordCallback Callback, void *Context)
993 {
994 // Don't try to store a TTL bigger than we can represent in platform time units
995 if (ttl > 0x7FFFFFFFUL / mDNSPlatformOneSecond)
996 ttl = 0x7FFFFFFFUL / mDNSPlatformOneSecond;
997 else if (ttl == 0) // And Zero TTL is illegal
998 ttl = DefaultTTLforRRType(rrtype);
999
1000 // Field Group 1: The actual information pertaining to this resource record
1001 rr->resrec.RecordType = RecordType;
1002 rr->resrec.InterfaceID = InterfaceID;
1003 rr->resrec.name = &rr->namestorage;
1004 rr->resrec.rrtype = rrtype;
1005 rr->resrec.rrclass = kDNSClass_IN;
1006 rr->resrec.rroriginalttl = ttl;
1007 rr->resrec.rDNSServer = mDNSNULL;
1008 // rr->resrec.rdlength = MUST set by client and/or in mDNS_Register_internal
1009 // rr->resrec.rdestimate = set in mDNS_Register_internal
1010 // rr->resrec.rdata = MUST be set by client
1011
1012 if (RDataStorage)
1013 rr->resrec.rdata = RDataStorage;
1014 else
1015 {
1016 rr->resrec.rdata = &rr->rdatastorage;
1017 rr->resrec.rdata->MaxRDLength = sizeof(RDataBody);
1018 }
1019
1020 // Field Group 2: Persistent metadata for Authoritative Records
1021 rr->Additional1 = mDNSNULL;
1022 rr->Additional2 = mDNSNULL;
1023 rr->DependentOn = mDNSNULL;
1024 rr->RRSet = mDNSNULL;
1025 rr->RecordCallback = Callback;
1026 rr->RecordContext = Context;
1027
1028 rr->AutoTarget = Target_Manual;
1029 rr->AllowRemoteQuery = mDNSfalse;
1030 rr->ForceMCast = mDNSfalse;
1031
1032 rr->WakeUp = zeroOwner;
1033 rr->AddressProxy = zeroAddr;
1034 rr->TimeRcvd = 0;
1035 rr->TimeExpire = 0;
1036
1037 // Field Group 3: Transient state for Authoritative Records (set in mDNS_Register_internal)
1038 // Field Group 4: Transient uDNS state for Authoritative Records (set in mDNS_Register_internal)
1039
1040 // For now, until the uDNS code is fully integrated, it's helpful to zero the uDNS state fields here too, just in case
1041 // (e.g. uDNS_RegisterService short-circuits the usual mDNS_Register_internal record registration calls, so a bunch
1042 // of fields don't get set up properly. In particular, if we don't zero rr->QueuedRData then the uDNS code crashes.)
1043 rr->state = regState_Zero;
1044 rr->uselease = 0;
1045 rr->expire = 0;
1046 rr->Private = 0;
1047 rr->updateid = zeroID;
1048 rr->zone = rr->resrec.name;
1049 rr->nta = mDNSNULL;
1050 rr->tcp = mDNSNULL;
1051 rr->OrigRData = 0;
1052 rr->OrigRDLen = 0;
1053 rr->InFlightRData = 0;
1054 rr->InFlightRDLen = 0;
1055 rr->QueuedRData = 0;
1056 rr->QueuedRDLen = 0;
1057 mDNSPlatformMemZero(&rr->NATinfo, sizeof(rr->NATinfo));
1058 rr->SRVChanged = mDNSfalse;
1059 rr->mState = mergeState_Zero;
1060
1061 rr->namestorage.c[0] = 0; // MUST be set by client before calling mDNS_Register()
1062 }
1063
1064 mDNSexport void mDNS_SetupQuestion(DNSQuestion *const q, const mDNSInterfaceID InterfaceID, const domainname *const name,
1065 const mDNSu16 qtype, mDNSQuestionCallback *const callback, void *const context)
1066 {
1067 q->InterfaceID = InterfaceID;
1068 q->Target = zeroAddr;
1069 AssignDomainName(&q->qname, name);
1070 q->qtype = qtype;
1071 q->qclass = kDNSClass_IN;
1072 q->LongLived = (qtype == kDNSType_PTR);
1073 q->ExpectUnique = (qtype != kDNSType_PTR);
1074 q->ForceMCast = mDNSfalse;
1075 q->ReturnIntermed = mDNSfalse;
1076 q->SuppressUnusable = mDNSfalse;
1077 q->WakeOnResolve = mDNSfalse;
1078 q->QuestionCallback = callback;
1079 q->QuestionContext = context;
1080 }
1081
1082 mDNSexport mDNSu32 RDataHashValue(const ResourceRecord *const rr)
1083 {
1084 int len = rr->rdlength;
1085 const RDataBody2 *const rdb = (RDataBody2 *)rr->rdata->u.data;
1086 switch(rr->rrtype)
1087 {
1088 case kDNSType_NS:
1089 case kDNSType_CNAME:
1090 case kDNSType_PTR:
1091 case kDNSType_DNAME: return DomainNameHashValue(&rdb->name);
1092
1093 case kDNSType_SOA: return rdb->soa.serial +
1094 rdb->soa.refresh +
1095 rdb->soa.retry +
1096 rdb->soa.expire +
1097 rdb->soa.min +
1098 DomainNameHashValue(&rdb->soa.mname) +
1099 DomainNameHashValue(&rdb->soa.rname);
1100
1101 case kDNSType_MX:
1102 case kDNSType_AFSDB:
1103 case kDNSType_RT:
1104 case kDNSType_KX: return DomainNameHashValue(&rdb->mx.exchange);
1105
1106 case kDNSType_RP: return DomainNameHashValue(&rdb->rp.mbox) + DomainNameHashValue(&rdb->rp.txt);
1107
1108 case kDNSType_PX: return DomainNameHashValue(&rdb->px.map822) + DomainNameHashValue(&rdb->px.mapx400);
1109
1110 case kDNSType_SRV: return DomainNameHashValue(&rdb->srv.target);
1111
1112 case kDNSType_OPT: return 0; // OPT is a pseudo-RR container structure; makes no sense to compare
1113
1114 case kDNSType_NSEC: len = sizeof(rdataNSEC); // Use in-memory length of 32, and fall through default checksum computation below
1115
1116 default:
1117 {
1118 mDNSu32 sum = 0;
1119 int i;
1120 for (i=0; i+1 < len; i+=2)
1121 {
1122 sum += (((mDNSu32)(rdb->data[i])) << 8) | rdb->data[i+1];
1123 sum = (sum<<3) | (sum>>29);
1124 }
1125 if (i < len)
1126 {
1127 sum += ((mDNSu32)(rdb->data[i])) << 8;
1128 }
1129 return(sum);
1130 }
1131 }
1132 }
1133
1134 // r1 has to be a full ResourceRecord including rrtype and rdlength
1135 // r2 is just a bare RDataBody, which MUST be the same rrtype and rdlength as r1
1136 mDNSexport mDNSBool SameRDataBody(const ResourceRecord *const r1, const RDataBody *const r2, DomainNameComparisonFn *samename)
1137 {
1138 const RDataBody2 *const b1 = (RDataBody2 *)r1->rdata->u.data;
1139 const RDataBody2 *const b2 = (RDataBody2 *)r2;
1140 switch(r1->rrtype)
1141 {
1142 case kDNSType_NS:
1143 case kDNSType_CNAME:
1144 case kDNSType_PTR:
1145 case kDNSType_DNAME:return(SameDomainName(&b1->name, &b2->name));
1146
1147 case kDNSType_SOA: return(mDNSBool)( b1->soa.serial == b2->soa.serial &&
1148 b1->soa.refresh == b2->soa.refresh &&
1149 b1->soa.retry == b2->soa.retry &&
1150 b1->soa.expire == b2->soa.expire &&
1151 b1->soa.min == b2->soa.min &&
1152 samename(&b1->soa.mname, &b2->soa.mname) &&
1153 samename(&b1->soa.rname, &b2->soa.rname));
1154
1155 case kDNSType_MX:
1156 case kDNSType_AFSDB:
1157 case kDNSType_RT:
1158 case kDNSType_KX: return(mDNSBool)( b1->mx.preference == b2->mx.preference &&
1159 samename(&b1->mx.exchange, &b2->mx.exchange));
1160
1161 case kDNSType_RP: return(mDNSBool)( samename(&b1->rp.mbox, &b2->rp.mbox) &&
1162 samename(&b1->rp.txt, &b2->rp.txt));
1163
1164 case kDNSType_PX: return(mDNSBool)( b1->px.preference == b2->px.preference &&
1165 samename(&b1->px.map822, &b2->px.map822) &&
1166 samename(&b1->px.mapx400, &b2->px.mapx400));
1167
1168 case kDNSType_SRV: return(mDNSBool)( b1->srv.priority == b2->srv.priority &&
1169 b1->srv.weight == b2->srv.weight &&
1170 mDNSSameIPPort(b1->srv.port, b2->srv.port) &&
1171 samename(&b1->srv.target, &b2->srv.target));
1172
1173 case kDNSType_OPT: return mDNSfalse; // OPT is a pseudo-RR container structure; makes no sense to compare
1174
1175 case kDNSType_NSEC: return(mDNSPlatformMemSame(b1->data, b2->data, sizeof(rdataNSEC)));
1176
1177 default: return(mDNSPlatformMemSame(b1->data, b2->data, r1->rdlength));
1178 }
1179 }
1180
1181 // ResourceRecordAnswersQuestion returns mDNStrue if the given resource record is a valid answer to the given question.
1182 // SameNameRecordAnswersQuestion is the same, except it skips the expensive SameDomainName() call.
1183 // SameDomainName() is generally cheap when the names don't match, but expensive when they do match,
1184 // because it has to check all the way to the end of the names to be sure.
1185 // In cases where we know in advance that the names match it's especially advantageous to skip the
1186 // SameDomainName() call because that's precisely the time when it's most expensive and least useful.
1187
1188 mDNSexport mDNSBool SameNameRecordAnswersQuestion(const ResourceRecord *const rr, const DNSQuestion *const q)
1189 {
1190 if (rr->InterfaceID &&
1191 q ->InterfaceID && q->InterfaceID != mDNSInterface_LocalOnly &&
1192 rr->InterfaceID != q->InterfaceID) return(mDNSfalse);
1193
1194 // Resource record received via unicast, the DNSServer entries should match ?
1195 if (!rr->InterfaceID && rr->rDNSServer != q->qDNSServer) return(mDNSfalse);
1196
1197 // If ResourceRecord received via multicast, but question was unicast, then shouldn't use record to answer this question
1198 if (rr->InterfaceID && !mDNSOpaque16IsZero(q->TargetQID)) return(mDNSfalse);
1199
1200 // RR type CNAME matches any query type. QTYPE ANY matches any RR type. QCLASS ANY matches any RR class.
1201 if (!RRTypeAnswersQuestionType(rr,q->qtype)) return(mDNSfalse);
1202 if (rr->rrclass != q->qclass && q->qclass != kDNSQClass_ANY) return(mDNSfalse);
1203
1204 return(mDNStrue);
1205 }
1206
1207 mDNSexport mDNSBool ResourceRecordAnswersQuestion(const ResourceRecord *const rr, const DNSQuestion *const q)
1208 {
1209 if (rr->InterfaceID &&
1210 q ->InterfaceID && q->InterfaceID != mDNSInterface_LocalOnly &&
1211 rr->InterfaceID != q->InterfaceID) return(mDNSfalse);
1212
1213 // Resource record received via unicast, the DNSServer entries should match ?
1214 if (!rr->InterfaceID && rr->rDNSServer != q->qDNSServer) return(mDNSfalse);
1215
1216 // If ResourceRecord received via multicast, but question was unicast, then shouldn't use record to answer this question.
1217 // This also covers the case where the ResourceRecord is mDNSInterface_LocalOnly and the question is expecting a unicast
1218 // DNS response. We don't want a local process to be able to create a fake LocalOnly address record for "www.bigbank.com"
1219 // which would then cause other applications (e.g. Safari) to connect to the wrong address. If we decide to support this later,
1220 // the restrictions need to be at least as strict as the restrictions on who can edit /etc/hosts and put fake addresses there.
1221 if (rr->InterfaceID && !mDNSOpaque16IsZero(q->TargetQID)) return(mDNSfalse);
1222
1223 // RR type CNAME matches any query type. QTYPE ANY matches any RR type. QCLASS ANY matches any RR class.
1224 if (!RRTypeAnswersQuestionType(rr,q->qtype)) return(mDNSfalse);
1225 if (rr->rrclass != q->qclass && q->qclass != kDNSQClass_ANY) return(mDNSfalse);
1226
1227 return(rr->namehash == q->qnamehash && SameDomainName(rr->name, &q->qname));
1228 }
1229
1230 mDNSexport mDNSBool AnyTypeRecordAnswersQuestion(const ResourceRecord *const rr, const DNSQuestion *const q)
1231 {
1232 if (rr->InterfaceID &&
1233 q ->InterfaceID && q->InterfaceID != mDNSInterface_LocalOnly &&
1234 rr->InterfaceID != q->InterfaceID) return(mDNSfalse);
1235
1236 // Resource record received via unicast, the DNSServer entries should match ?
1237 // Note that Auth Records are normally setup with NULL InterfaceID and
1238 // both the DNSServers are assumed to be NULL in that case
1239 if (!rr->InterfaceID && rr->rDNSServer != q->qDNSServer) return(mDNSfalse);
1240
1241 // If ResourceRecord received via multicast, but question was unicast, then shouldn't use record to answer this question
1242 if (rr->InterfaceID && !mDNSOpaque16IsZero(q->TargetQID)) return(mDNSfalse);
1243
1244 if (rr->rrclass != q->qclass && q->qclass != kDNSQClass_ANY) return(mDNSfalse);
1245
1246 return(rr->namehash == q->qnamehash && SameDomainName(rr->name, &q->qname));
1247 }
1248
1249 // This is called only when the caller knows that it is a Unicast Resource Record and it is a Unicast Question
1250 // and hence we don't need InterfaceID checks like above. Though this may not be a big optimization, the main
1251 // reason we need this is that we can't compare DNSServers between the question and the resource record because
1252 // the resource record may not be completely initialized e.g., mDNSCoreReceiveResponse
1253 mDNSexport mDNSBool UnicastResourceRecordAnswersQuestion(const ResourceRecord *const rr, const DNSQuestion *const q)
1254 {
1255 // RR type CNAME matches any query type. QTYPE ANY matches any RR type. QCLASS ANY matches any RR class.
1256 if (!RRTypeAnswersQuestionType(rr,q->qtype)) return(mDNSfalse);
1257
1258 if (rr->rrclass != q->qclass && q->qclass != kDNSQClass_ANY) return(mDNSfalse);
1259
1260 return(rr->namehash == q->qnamehash && SameDomainName(rr->name, &q->qname));
1261 }
1262
1263 mDNSexport mDNSu16 GetRDLength(const ResourceRecord *const rr, mDNSBool estimate)
1264 {
1265 const RDataBody2 *const rd = (RDataBody2 *)rr->rdata->u.data;
1266 const domainname *const name = estimate ? rr->name : mDNSNULL;
1267 if (rr->rrclass == kDNSQClass_ANY) return(rr->rdlength); // Used in update packets to mean "Delete An RRset" (RFC 2136)
1268 else switch (rr->rrtype)
1269 {
1270 case kDNSType_A: return(sizeof(rd->ipv4));
1271
1272 case kDNSType_NS:
1273 case kDNSType_CNAME:
1274 case kDNSType_PTR:
1275 case kDNSType_DNAME:return(CompressedDomainNameLength(&rd->name, name));
1276
1277 case kDNSType_SOA: return(mDNSu16)(CompressedDomainNameLength(&rd->soa.mname, name) +
1278 CompressedDomainNameLength(&rd->soa.rname, name) +
1279 5 * sizeof(mDNSOpaque32));
1280
1281 case kDNSType_NULL:
1282 case kDNSType_TSIG:
1283 case kDNSType_TXT:
1284 case kDNSType_X25:
1285 case kDNSType_ISDN:
1286 case kDNSType_LOC:
1287 case kDNSType_DHCID:return(rr->rdlength); // Not self-describing, so have to just trust rdlength
1288
1289 case kDNSType_HINFO:return(mDNSu16)(2 + (int)rd->data[0] + (int)rd->data[1 + (int)rd->data[0]]);
1290
1291 case kDNSType_MX:
1292 case kDNSType_AFSDB:
1293 case kDNSType_RT:
1294 case kDNSType_KX: return(mDNSu16)(2 + CompressedDomainNameLength(&rd->mx.exchange, name));
1295
1296 case kDNSType_RP: return(mDNSu16)(CompressedDomainNameLength(&rd->rp.mbox, name) +
1297 CompressedDomainNameLength(&rd->rp.txt, name));
1298
1299 case kDNSType_PX: return(mDNSu16)(2 + CompressedDomainNameLength(&rd->px.map822, name) +
1300 CompressedDomainNameLength(&rd->px.mapx400, name));
1301
1302 case kDNSType_AAAA: return(sizeof(rd->ipv6));
1303
1304 case kDNSType_SRV: return(mDNSu16)(6 + CompressedDomainNameLength(&rd->srv.target, name));
1305
1306 case kDNSType_OPT: return(rr->rdlength);
1307
1308 case kDNSType_NSEC: {
1309 int i;
1310 for (i=sizeof(rdataNSEC); i>0; i--) if (rd->nsec.bitmap[i-1]) break;
1311 // For our simplified use of NSEC synthetic records:
1312 // nextname is always the record's own name,
1313 // and if we have at least one record type that exists,
1314 // - the block number is always 0,
1315 // - the count byte is a value in the range 1-32,
1316 // - followed by the 1-32 data bytes
1317 return(mDNSu16)((estimate ? 2 : DomainNameLength(rr->name)) + (i ? (2 + i) : 0));
1318 }
1319
1320 default: debugf("Warning! Don't know how to get length of resource type %d", rr->rrtype);
1321 return(rr->rdlength);
1322 }
1323 }
1324
1325 // When a local client registers (or updates) a record, we use this routine to do some simple validation checks
1326 // to help reduce the risk of bogus malformed data on the network
1327 mDNSexport mDNSBool ValidateRData(const mDNSu16 rrtype, const mDNSu16 rdlength, const RData *const rd)
1328 {
1329 mDNSu16 len;
1330
1331 switch(rrtype)
1332 {
1333 case kDNSType_A: return(rdlength == sizeof(mDNSv4Addr));
1334
1335 case kDNSType_NS: // Same as PTR
1336 case kDNSType_MD: // Same as PTR
1337 case kDNSType_MF: // Same as PTR
1338 case kDNSType_CNAME:// Same as PTR
1339 //case kDNSType_SOA not checked
1340 case kDNSType_MB: // Same as PTR
1341 case kDNSType_MG: // Same as PTR
1342 case kDNSType_MR: // Same as PTR
1343 //case kDNSType_NULL not checked (no specified format, so always valid)
1344 //case kDNSType_WKS not checked
1345 case kDNSType_PTR: len = DomainNameLengthLimit(&rd->u.name, rd->u.data + rdlength);
1346 return(len <= MAX_DOMAIN_NAME && rdlength == len);
1347
1348 case kDNSType_HINFO:// Same as TXT (roughly)
1349 case kDNSType_MINFO:// Same as TXT (roughly)
1350 case kDNSType_TXT: if (!rdlength) return(mDNSfalse); // TXT record has to be at least one byte (RFC 1035)
1351 {
1352 const mDNSu8 *ptr = rd->u.txt.c;
1353 const mDNSu8 *end = rd->u.txt.c + rdlength;
1354 while (ptr < end) ptr += 1 + ptr[0];
1355 return (ptr == end);
1356 }
1357
1358 case kDNSType_AAAA: return(rdlength == sizeof(mDNSv6Addr));
1359
1360 case kDNSType_MX: // Must be at least two-byte preference, plus domainname
1361 // Call to DomainNameLengthLimit() implicitly enforces both requirements for us
1362 len = DomainNameLengthLimit(&rd->u.mx.exchange, rd->u.data + rdlength);
1363 return(len <= MAX_DOMAIN_NAME && rdlength == 2+len);
1364
1365 case kDNSType_SRV: // Must be at least priority+weight+port, plus domainname
1366 // Call to DomainNameLengthLimit() implicitly enforces both requirements for us
1367 len = DomainNameLengthLimit(&rd->u.srv.target, rd->u.data + rdlength);
1368 return(len <= MAX_DOMAIN_NAME && rdlength == 6+len);
1369
1370 //case kDNSType_NSEC not checked
1371
1372 default: return(mDNStrue); // Allow all other types without checking
1373 }
1374 }
1375
1376 // ***************************************************************************
1377 #if COMPILER_LIKES_PRAGMA_MARK
1378 #pragma mark -
1379 #pragma mark - DNS Message Creation Functions
1380 #endif
1381
1382 mDNSexport void InitializeDNSMessage(DNSMessageHeader *h, mDNSOpaque16 id, mDNSOpaque16 flags)
1383 {
1384 h->id = id;
1385 h->flags = flags;
1386 h->numQuestions = 0;
1387 h->numAnswers = 0;
1388 h->numAuthorities = 0;
1389 h->numAdditionals = 0;
1390 }
1391
1392 mDNSexport const mDNSu8 *FindCompressionPointer(const mDNSu8 *const base, const mDNSu8 *const end, const mDNSu8 *const domname)
1393 {
1394 const mDNSu8 *result = end - *domname - 1;
1395
1396 if (*domname == 0) return(mDNSNULL); // There's no point trying to match just the root label
1397
1398 // This loop examines each possible starting position in packet, starting end of the packet and working backwards
1399 while (result >= base)
1400 {
1401 // If the length byte and first character of the label match, then check further to see
1402 // if this location in the packet will yield a useful name compression pointer.
1403 if (result[0] == domname[0] && result[1] == domname[1])
1404 {
1405 const mDNSu8 *name = domname;
1406 const mDNSu8 *targ = result;
1407 while (targ + *name < end)
1408 {
1409 // First see if this label matches
1410 int i;
1411 const mDNSu8 *pointertarget;
1412 for (i=0; i <= *name; i++) if (targ[i] != name[i]) break;
1413 if (i <= *name) break; // If label did not match, bail out
1414 targ += 1 + *name; // Else, did match, so advance target pointer
1415 name += 1 + *name; // and proceed to check next label
1416 if (*name == 0 && *targ == 0) return(result); // If no more labels, we found a match!
1417 if (*name == 0) break; // If no more labels to match, we failed, so bail out
1418
1419 // The label matched, so now follow the pointer (if appropriate) and then see if the next label matches
1420 if (targ[0] < 0x40) continue; // If length value, continue to check next label
1421 if (targ[0] < 0xC0) break; // If 40-BF, not valid
1422 if (targ+1 >= end) break; // Second byte not present!
1423 pointertarget = base + (((mDNSu16)(targ[0] & 0x3F)) << 8) + targ[1];
1424 if (targ < pointertarget) break; // Pointertarget must point *backwards* in the packet
1425 if (pointertarget[0] >= 0x40) break; // Pointertarget must point to a valid length byte
1426 targ = pointertarget;
1427 }
1428 }
1429 result--; // We failed to match at this search position, so back up the tentative result pointer and try again
1430 }
1431 return(mDNSNULL);
1432 }
1433
1434 // Put a string of dot-separated labels as length-prefixed labels
1435 // domainname is a fully-qualified name (i.e. assumed to be ending in a dot, even if it doesn't)
1436 // msg points to the message we're building (pass mDNSNULL if we don't want to use compression pointers)
1437 // end points to the end of the message so far
1438 // ptr points to where we want to put the name
1439 // limit points to one byte past the end of the buffer that we must not overrun
1440 // domainname is the name to put
1441 mDNSexport mDNSu8 *putDomainNameAsLabels(const DNSMessage *const msg,
1442 mDNSu8 *ptr, const mDNSu8 *const limit, const domainname *const name)
1443 {
1444 const mDNSu8 *const base = (const mDNSu8 *)msg;
1445 const mDNSu8 * np = name->c;
1446 const mDNSu8 *const max = name->c + MAX_DOMAIN_NAME; // Maximum that's valid
1447 const mDNSu8 * pointer = mDNSNULL;
1448 const mDNSu8 *const searchlimit = ptr;
1449
1450 if (!ptr) { LogMsg("putDomainNameAsLabels %##s ptr is null", name->c); return(mDNSNULL); }
1451
1452 if (!*np) // If just writing one-byte root label, make sure we have space for that
1453 {
1454 if (ptr >= limit) return(mDNSNULL);
1455 }
1456 else // else, loop through writing labels and/or a compression offset
1457 {
1458 do {
1459 if (*np > MAX_DOMAIN_LABEL)
1460 { LogMsg("Malformed domain name %##s (label more than 63 bytes)", name->c); return(mDNSNULL); }
1461
1462 // This check correctly allows for the final trailing root label:
1463 // e.g.
1464 // Suppose our domain name is exactly 256 bytes long, including the final trailing root label.
1465 // Suppose np is now at name->c[249], and we're about to write our last non-null label ("local").
1466 // We know that max will be at name->c[256]
1467 // That means that np + 1 + 5 == max - 1, so we (just) pass the "if" test below, write our
1468 // six bytes, then exit the loop, write the final terminating root label, and the domain
1469 // name we've written is exactly 256 bytes long, exactly at the correct legal limit.
1470 // If the name is one byte longer, then we fail the "if" test below, and correctly bail out.
1471 if (np + 1 + *np >= max)
1472 { LogMsg("Malformed domain name %##s (more than 256 bytes)", name->c); return(mDNSNULL); }
1473
1474 if (base) pointer = FindCompressionPointer(base, searchlimit, np);
1475 if (pointer) // Use a compression pointer if we can
1476 {
1477 const mDNSu16 offset = (mDNSu16)(pointer - base);
1478 if (ptr+2 > limit) return(mDNSNULL); // If we don't have two bytes of space left, give up
1479 *ptr++ = (mDNSu8)(0xC0 | (offset >> 8));
1480 *ptr++ = (mDNSu8)( offset & 0xFF);
1481 return(ptr);
1482 }
1483 else // Else copy one label and try again
1484 {
1485 int i;
1486 mDNSu8 len = *np++;
1487 // If we don't at least have enough space for this label *plus* a terminating zero on the end, give up
1488 if (ptr + 1 + len >= limit) return(mDNSNULL);
1489 *ptr++ = len;
1490 for (i=0; i<len; i++) *ptr++ = *np++;
1491 }
1492 } while (*np); // While we've got characters remaining in the name, continue
1493 }
1494
1495 *ptr++ = 0; // Put the final root label
1496 return(ptr);
1497 }
1498
1499 mDNSlocal mDNSu8 *putVal16(mDNSu8 *ptr, mDNSu16 val)
1500 {
1501 ptr[0] = (mDNSu8)((val >> 8 ) & 0xFF);
1502 ptr[1] = (mDNSu8)((val ) & 0xFF);
1503 return ptr + sizeof(mDNSOpaque16);
1504 }
1505
1506 mDNSlocal mDNSu8 *putVal32(mDNSu8 *ptr, mDNSu32 val)
1507 {
1508 ptr[0] = (mDNSu8)((val >> 24) & 0xFF);
1509 ptr[1] = (mDNSu8)((val >> 16) & 0xFF);
1510 ptr[2] = (mDNSu8)((val >> 8) & 0xFF);
1511 ptr[3] = (mDNSu8)((val ) & 0xFF);
1512 return ptr + sizeof(mDNSu32);
1513 }
1514
1515 // msg points to the message we're building (pass mDNSNULL if we don't want to use compression pointers)
1516 mDNSexport mDNSu8 *putRData(const DNSMessage *const msg, mDNSu8 *ptr, const mDNSu8 *const limit, const ResourceRecord *const rr)
1517 {
1518 const RDataBody2 *const rdb = (RDataBody2 *)rr->rdata->u.data;
1519 switch (rr->rrtype)
1520 {
1521 case kDNSType_A: if (rr->rdlength != 4)
1522 { debugf("putRData: Illegal length %d for kDNSType_A", rr->rdlength); return(mDNSNULL); }
1523 if (ptr + 4 > limit) return(mDNSNULL);
1524 *ptr++ = rdb->ipv4.b[0];
1525 *ptr++ = rdb->ipv4.b[1];
1526 *ptr++ = rdb->ipv4.b[2];
1527 *ptr++ = rdb->ipv4.b[3];
1528 return(ptr);
1529
1530 case kDNSType_NS:
1531 case kDNSType_CNAME:
1532 case kDNSType_PTR:
1533 case kDNSType_DNAME:return(putDomainNameAsLabels(msg, ptr, limit, &rdb->name));
1534
1535 case kDNSType_SOA: ptr = putDomainNameAsLabels(msg, ptr, limit, &rdb->soa.mname);
1536 if (!ptr) return(mDNSNULL);
1537 ptr = putDomainNameAsLabels(msg, ptr, limit, &rdb->soa.rname);
1538 if (!ptr || ptr + 20 > limit) return(mDNSNULL);
1539 ptr = putVal32(ptr, rdb->soa.serial);
1540 ptr = putVal32(ptr, rdb->soa.refresh);
1541 ptr = putVal32(ptr, rdb->soa.retry);
1542 ptr = putVal32(ptr, rdb->soa.expire);
1543 ptr = putVal32(ptr, rdb->soa.min);
1544 return(ptr);
1545
1546 case kDNSType_NULL:
1547 case kDNSType_HINFO:
1548 case kDNSType_TSIG:
1549 case kDNSType_TXT:
1550 case kDNSType_X25:
1551 case kDNSType_ISDN:
1552 case kDNSType_LOC:
1553 case kDNSType_DHCID:if (ptr + rr->rdlength > limit) return(mDNSNULL);
1554 mDNSPlatformMemCopy(ptr, rdb->data, rr->rdlength);
1555 return(ptr + rr->rdlength);
1556
1557 case kDNSType_MX:
1558 case kDNSType_AFSDB:
1559 case kDNSType_RT:
1560 case kDNSType_KX: if (ptr + 3 > limit) return(mDNSNULL);
1561 ptr = putVal16(ptr, rdb->mx.preference);
1562 return(putDomainNameAsLabels(msg, ptr, limit, &rdb->mx.exchange));
1563
1564 case kDNSType_RP: ptr = putDomainNameAsLabels(msg, ptr, limit, &rdb->rp.mbox);
1565 if (!ptr) return(mDNSNULL);
1566 ptr = putDomainNameAsLabels(msg, ptr, limit, &rdb->rp.txt);
1567 return(ptr);
1568
1569 case kDNSType_PX: if (ptr + 5 > limit) return(mDNSNULL);
1570 ptr = putVal16(ptr, rdb->px.preference);
1571 ptr = putDomainNameAsLabels(msg, ptr, limit, &rdb->px.map822);
1572 if (!ptr) return(mDNSNULL);
1573 ptr = putDomainNameAsLabels(msg, ptr, limit, &rdb->px.mapx400);
1574 return(ptr);
1575
1576 case kDNSType_AAAA: if (rr->rdlength != sizeof(rdb->ipv6))
1577 { debugf("putRData: Illegal length %d for kDNSType_AAAA", rr->rdlength); return(mDNSNULL); }
1578 if (ptr + sizeof(rdb->ipv6) > limit) return(mDNSNULL);
1579 mDNSPlatformMemCopy(ptr, &rdb->ipv6, sizeof(rdb->ipv6));
1580 return(ptr + sizeof(rdb->ipv6));
1581
1582 case kDNSType_SRV: if (ptr + 7 > limit) return(mDNSNULL);
1583 *ptr++ = (mDNSu8)(rdb->srv.priority >> 8);
1584 *ptr++ = (mDNSu8)(rdb->srv.priority & 0xFF);
1585 *ptr++ = (mDNSu8)(rdb->srv.weight >> 8);
1586 *ptr++ = (mDNSu8)(rdb->srv.weight & 0xFF);
1587 *ptr++ = rdb->srv.port.b[0];
1588 *ptr++ = rdb->srv.port.b[1];
1589 return(putDomainNameAsLabels(msg, ptr, limit, &rdb->srv.target));
1590
1591 case kDNSType_OPT: {
1592 int len = 0;
1593 const rdataOPT *opt;
1594 const rdataOPT *const end = (const rdataOPT *)&rr->rdata->u.data[rr->rdlength];
1595 for (opt = &rr->rdata->u.opt[0]; opt < end; opt++) len += DNSOpt_Data_Space(opt);
1596 if (ptr + len > limit) { LogMsg("ERROR: putOptRData - out of space"); return mDNSNULL; }
1597
1598 for (opt = &rr->rdata->u.opt[0]; opt < end; opt++)
1599 {
1600 const int space = DNSOpt_Data_Space(opt);
1601 ptr = putVal16(ptr, opt->opt);
1602 ptr = putVal16(ptr, (mDNSu16)space - 4);
1603 switch (opt->opt)
1604 {
1605 case kDNSOpt_LLQ:
1606 ptr = putVal16(ptr, opt->u.llq.vers);
1607 ptr = putVal16(ptr, opt->u.llq.llqOp);
1608 ptr = putVal16(ptr, opt->u.llq.err);
1609 mDNSPlatformMemCopy(ptr, opt->u.llq.id.b, 8); // 8-byte id
1610 ptr += 8;
1611 ptr = putVal32(ptr, opt->u.llq.llqlease);
1612 break;
1613 case kDNSOpt_Lease:
1614 ptr = putVal32(ptr, opt->u.updatelease);
1615 break;
1616 case kDNSOpt_Owner:
1617 *ptr++ = opt->u.owner.vers;
1618 *ptr++ = opt->u.owner.seq;
1619 mDNSPlatformMemCopy(ptr, opt->u.owner.HMAC.b, 6); // 6-byte Host identifier
1620 ptr += 6;
1621 if (space >= DNSOpt_OwnerData_ID_Wake_Space)
1622 {
1623 mDNSPlatformMemCopy(ptr, opt->u.owner.IMAC.b, 6); // 6-byte interface MAC
1624 ptr += 6;
1625 if (space > DNSOpt_OwnerData_ID_Wake_Space)
1626 {
1627 mDNSPlatformMemCopy(ptr, opt->u.owner.password.b, space - DNSOpt_OwnerData_ID_Wake_Space);
1628 ptr += space - DNSOpt_OwnerData_ID_Wake_Space;
1629 }
1630 }
1631 break;
1632 }
1633 }
1634 return ptr;
1635 }
1636
1637 case kDNSType_NSEC: {
1638 // For our simplified use of NSEC synthetic records:
1639 // nextname is always the record's own name,
1640 // the block number is always 0,
1641 // the count byte is a value in the range 1-32,
1642 // followed by the 1-32 data bytes
1643 int i, j;
1644 for (i=sizeof(rdataNSEC); i>0; i--) if (rdb->nsec.bitmap[i-1]) break;
1645 ptr = putDomainNameAsLabels(msg, ptr, limit, rr->name);
1646 if (!ptr) return(mDNSNULL);
1647 if (i) // Only put a block if at least one type exists for this name
1648 {
1649 if (ptr + 2 + i > limit) return(mDNSNULL);
1650 *ptr++ = 0;
1651 *ptr++ = (mDNSu8)i;
1652 for (j=0; j<i; j++) *ptr++ = rdb->nsec.bitmap[j];
1653 }
1654 return ptr;
1655 }
1656
1657 default: debugf("putRData: Warning! Writing unknown resource type %d as raw data", rr->rrtype);
1658 if (ptr + rr->rdlength > limit) return(mDNSNULL);
1659 mDNSPlatformMemCopy(ptr, rdb->data, rr->rdlength);
1660 return(ptr + rr->rdlength);
1661 }
1662 }
1663
1664 #define IsUnicastUpdate(X) (!mDNSOpaque16IsZero((X)->h.id) && ((X)->h.flags.b[0] & kDNSFlag0_OP_Mask) == kDNSFlag0_OP_Update)
1665
1666 mDNSexport mDNSu8 *PutResourceRecordTTLWithLimit(DNSMessage *const msg, mDNSu8 *ptr, mDNSu16 *count, ResourceRecord *rr, mDNSu32 ttl, const mDNSu8 *limit)
1667 {
1668 mDNSu8 *endofrdata;
1669 mDNSu16 actualLength;
1670 // When sending SRV to conventional DNS server (i.e. in DNS update requests) we should not do name compression on the rdata (RFC 2782)
1671 const DNSMessage *const rdatacompressionbase = (IsUnicastUpdate(msg) && rr->rrtype == kDNSType_SRV) ? mDNSNULL : msg;
1672
1673 if (rr->RecordType == kDNSRecordTypeUnregistered)
1674 {
1675 LogMsg("PutResourceRecord ERROR! Attempt to put kDNSRecordTypeUnregistered %##s (%s)", rr->name->c, DNSTypeName(rr->rrtype));
1676 return(ptr);
1677 }
1678
1679 if (!ptr) { LogMsg("PutResourceRecordTTLWithLimit ptr is null"); return(mDNSNULL); }
1680
1681 ptr = putDomainNameAsLabels(msg, ptr, limit, rr->name);
1682 if (!ptr || ptr + 10 >= limit) return(mDNSNULL); // If we're out-of-space, return mDNSNULL
1683 ptr[0] = (mDNSu8)(rr->rrtype >> 8);
1684 ptr[1] = (mDNSu8)(rr->rrtype & 0xFF);
1685 ptr[2] = (mDNSu8)(rr->rrclass >> 8);
1686 ptr[3] = (mDNSu8)(rr->rrclass & 0xFF);
1687 ptr[4] = (mDNSu8)((ttl >> 24) & 0xFF);
1688 ptr[5] = (mDNSu8)((ttl >> 16) & 0xFF);
1689 ptr[6] = (mDNSu8)((ttl >> 8) & 0xFF);
1690 ptr[7] = (mDNSu8)( ttl & 0xFF);
1691 // ptr[8] and ptr[9] filled in *after* we find out how much space the rdata takes
1692
1693 endofrdata = putRData(rdatacompressionbase, ptr+10, limit, rr);
1694 if (!endofrdata) { verbosedebugf("Ran out of space in PutResourceRecord for %##s (%s)", rr->name->c, DNSTypeName(rr->rrtype)); return(mDNSNULL); }
1695
1696 // Go back and fill in the actual number of data bytes we wrote
1697 // (actualLength can be less than rdlength when domain name compression is used)
1698 actualLength = (mDNSu16)(endofrdata - ptr - 10);
1699 ptr[8] = (mDNSu8)(actualLength >> 8);
1700 ptr[9] = (mDNSu8)(actualLength & 0xFF);
1701
1702 if (count) (*count)++;
1703 else LogMsg("PutResourceRecordTTL: ERROR: No target count to update for %##s (%s)", rr->name->c, DNSTypeName(rr->rrtype));
1704 return(endofrdata);
1705 }
1706
1707 mDNSlocal mDNSu8 *putEmptyResourceRecord(DNSMessage *const msg, mDNSu8 *ptr, const mDNSu8 *const limit, mDNSu16 *count, const AuthRecord *rr)
1708 {
1709 ptr = putDomainNameAsLabels(msg, ptr, limit, rr->resrec.name);
1710 if (!ptr || ptr + 10 > limit) return(mDNSNULL); // If we're out-of-space, return mDNSNULL
1711 ptr[0] = (mDNSu8)(rr->resrec.rrtype >> 8); // Put type
1712 ptr[1] = (mDNSu8)(rr->resrec.rrtype & 0xFF);
1713 ptr[2] = (mDNSu8)(rr->resrec.rrclass >> 8); // Put class
1714 ptr[3] = (mDNSu8)(rr->resrec.rrclass & 0xFF);
1715 ptr[4] = ptr[5] = ptr[6] = ptr[7] = 0; // TTL is zero
1716 ptr[8] = ptr[9] = 0; // RDATA length is zero
1717 (*count)++;
1718 return(ptr + 10);
1719 }
1720
1721 mDNSexport mDNSu8 *putQuestion(DNSMessage *const msg, mDNSu8 *ptr, const mDNSu8 *const limit, const domainname *const name, mDNSu16 rrtype, mDNSu16 rrclass)
1722 {
1723 ptr = putDomainNameAsLabels(msg, ptr, limit, name);
1724 if (!ptr || ptr+4 >= limit) return(mDNSNULL); // If we're out-of-space, return mDNSNULL
1725 ptr[0] = (mDNSu8)(rrtype >> 8);
1726 ptr[1] = (mDNSu8)(rrtype & 0xFF);
1727 ptr[2] = (mDNSu8)(rrclass >> 8);
1728 ptr[3] = (mDNSu8)(rrclass & 0xFF);
1729 msg->h.numQuestions++;
1730 return(ptr+4);
1731 }
1732
1733 // for dynamic updates
1734 mDNSexport mDNSu8 *putZone(DNSMessage *const msg, mDNSu8 *ptr, mDNSu8 *limit, const domainname *zone, mDNSOpaque16 zoneClass)
1735 {
1736 ptr = putDomainNameAsLabels(msg, ptr, limit, zone);
1737 if (!ptr || ptr + 4 > limit) return mDNSNULL; // If we're out-of-space, return NULL
1738 *ptr++ = (mDNSu8)(kDNSType_SOA >> 8);
1739 *ptr++ = (mDNSu8)(kDNSType_SOA & 0xFF);
1740 *ptr++ = zoneClass.b[0];
1741 *ptr++ = zoneClass.b[1];
1742 msg->h.mDNS_numZones++;
1743 return ptr;
1744 }
1745
1746 // for dynamic updates
1747 mDNSexport mDNSu8 *putPrereqNameNotInUse(const domainname *const name, DNSMessage *const msg, mDNSu8 *const ptr, mDNSu8 *const end)
1748 {
1749 AuthRecord prereq;
1750 mDNS_SetupResourceRecord(&prereq, mDNSNULL, mDNSInterface_Any, kDNSQType_ANY, kStandardTTL, 0, mDNSNULL, mDNSNULL);
1751 AssignDomainName(&prereq.namestorage, name);
1752 prereq.resrec.rrtype = kDNSQType_ANY;
1753 prereq.resrec.rrclass = kDNSClass_NONE;
1754 return putEmptyResourceRecord(msg, ptr, end, &msg->h.mDNS_numPrereqs, &prereq);
1755 }
1756
1757 // for dynamic updates
1758 mDNSexport mDNSu8 *putDeletionRecord(DNSMessage *msg, mDNSu8 *ptr, ResourceRecord *rr)
1759 {
1760 // deletion: specify record w/ TTL 0, class NONE
1761 const mDNSu16 origclass = rr->rrclass;
1762 rr->rrclass = kDNSClass_NONE;
1763 ptr = PutResourceRecordTTLJumbo(msg, ptr, &msg->h.mDNS_numUpdates, rr, 0);
1764 rr->rrclass = origclass;
1765 return ptr;
1766 }
1767
1768 // for dynamic updates
1769 mDNSexport mDNSu8 *putDeletionRecordWithLimit(DNSMessage *msg, mDNSu8 *ptr, ResourceRecord *rr, mDNSu8 *limit)
1770 {
1771 // deletion: specify record w/ TTL 0, class NONE
1772 const mDNSu16 origclass = rr->rrclass;
1773 rr->rrclass = kDNSClass_NONE;
1774 ptr = PutResourceRecordTTLWithLimit(msg, ptr, &msg->h.mDNS_numUpdates, rr, 0, limit);
1775 rr->rrclass = origclass;
1776 return ptr;
1777 }
1778
1779 mDNSexport mDNSu8 *putDeleteRRSetWithLimit(DNSMessage *msg, mDNSu8 *ptr, const domainname *name, mDNSu16 rrtype, mDNSu8 *limit)
1780 {
1781 mDNSu16 class = kDNSQClass_ANY;
1782
1783 ptr = putDomainNameAsLabels(msg, ptr, limit, name);
1784 if (!ptr || ptr + 10 >= limit) return mDNSNULL; // If we're out-of-space, return mDNSNULL
1785 ptr[0] = (mDNSu8)(rrtype >> 8);
1786 ptr[1] = (mDNSu8)(rrtype & 0xFF);
1787 ptr[2] = (mDNSu8)(class >> 8);
1788 ptr[3] = (mDNSu8)(class & 0xFF);
1789 ptr[4] = ptr[5] = ptr[6] = ptr[7] = 0; // zero ttl
1790 ptr[8] = ptr[9] = 0; // zero rdlength/rdata
1791
1792 msg->h.mDNS_numUpdates++;
1793 return ptr + 10;
1794 }
1795
1796 // for dynamic updates
1797 mDNSexport mDNSu8 *putDeleteAllRRSets(DNSMessage *msg, mDNSu8 *ptr, const domainname *name)
1798 {
1799 const mDNSu8 *limit = msg->data + AbsoluteMaxDNSMessageData;
1800 mDNSu16 class = kDNSQClass_ANY;
1801 mDNSu16 rrtype = kDNSQType_ANY;
1802
1803 ptr = putDomainNameAsLabels(msg, ptr, limit, name);
1804 if (!ptr || ptr + 10 >= limit) return mDNSNULL; // If we're out-of-space, return mDNSNULL
1805 ptr[0] = (mDNSu8)(rrtype >> 8);
1806 ptr[1] = (mDNSu8)(rrtype & 0xFF);
1807 ptr[2] = (mDNSu8)(class >> 8);
1808 ptr[3] = (mDNSu8)(class & 0xFF);
1809 ptr[4] = ptr[5] = ptr[6] = ptr[7] = 0; // zero ttl
1810 ptr[8] = ptr[9] = 0; // zero rdlength/rdata
1811
1812 msg->h.mDNS_numUpdates++;
1813 return ptr + 10;
1814 }
1815
1816 // for dynamic updates
1817 mDNSexport mDNSu8 *putUpdateLease(DNSMessage *msg, mDNSu8 *end, mDNSu32 lease)
1818 {
1819 AuthRecord rr;
1820 mDNS_SetupResourceRecord(&rr, mDNSNULL, mDNSInterface_Any, kDNSType_OPT, kStandardTTL, kDNSRecordTypeKnownUnique, mDNSNULL, mDNSNULL);
1821 rr.resrec.rrclass = NormalMaxDNSMessageData;
1822 rr.resrec.rdlength = sizeof(rdataOPT); // One option in this OPT record
1823 rr.resrec.rdestimate = sizeof(rdataOPT);
1824 rr.resrec.rdata->u.opt[0].opt = kDNSOpt_Lease;
1825 rr.resrec.rdata->u.opt[0].u.updatelease = lease;
1826 end = PutResourceRecordTTLJumbo(msg, end, &msg->h.numAdditionals, &rr.resrec, 0);
1827 if (!end) { LogMsg("ERROR: putUpdateLease - PutResourceRecordTTL"); return mDNSNULL; }
1828 return end;
1829 }
1830
1831 // for dynamic updates
1832 mDNSexport mDNSu8 *putUpdateLeaseWithLimit(DNSMessage *msg, mDNSu8 *end, mDNSu32 lease, mDNSu8 *limit)
1833 {
1834 AuthRecord rr;
1835 mDNS_SetupResourceRecord(&rr, mDNSNULL, mDNSInterface_Any, kDNSType_OPT, kStandardTTL, kDNSRecordTypeKnownUnique, mDNSNULL, mDNSNULL);
1836 rr.resrec.rrclass = NormalMaxDNSMessageData;
1837 rr.resrec.rdlength = sizeof(rdataOPT); // One option in this OPT record
1838 rr.resrec.rdestimate = sizeof(rdataOPT);
1839 rr.resrec.rdata->u.opt[0].opt = kDNSOpt_Lease;
1840 rr.resrec.rdata->u.opt[0].u.updatelease = lease;
1841 end = PutResourceRecordTTLWithLimit(msg, end, &msg->h.numAdditionals, &rr.resrec, 0, limit);
1842 if (!end) { LogMsg("ERROR: putUpdateLease - PutResourceRecordTTLWithLimit"); return mDNSNULL; }
1843 return end;
1844 }
1845
1846 mDNSexport mDNSu8 *putHINFO(const mDNS *const m, DNSMessage *const msg, mDNSu8 *end, DomainAuthInfo *authInfo, mDNSu8 *limit)
1847 {
1848 if (authInfo && authInfo->AutoTunnel)
1849 {
1850 AuthRecord hinfo;
1851 mDNSu8 *h = hinfo.rdatastorage.u.data;
1852 mDNSu16 len = 2 + m->HIHardware.c[0] + m->HISoftware.c[0];
1853 mDNSu8 *newptr;
1854 mDNS_SetupResourceRecord(&hinfo, mDNSNULL, mDNSInterface_Any, kDNSType_HINFO, 0, kDNSRecordTypeUnique, mDNSNULL, mDNSNULL);
1855 AppendDomainLabel(&hinfo.namestorage, &m->hostlabel);
1856 AppendDomainName (&hinfo.namestorage, &authInfo->domain);
1857 hinfo.resrec.rroriginalttl = 0;
1858 mDNSPlatformMemCopy(h, &m->HIHardware, 1 + (mDNSu32)m->HIHardware.c[0]);
1859 h += 1 + (int)h[0];
1860 mDNSPlatformMemCopy(h, &m->HISoftware, 1 + (mDNSu32)m->HISoftware.c[0]);
1861 hinfo.resrec.rdlength = len;
1862 hinfo.resrec.rdestimate = len;
1863 newptr = PutResourceRecordTTLWithLimit(msg, end, &msg->h.numAdditionals, &hinfo.resrec, 0, limit);
1864 return newptr;
1865 }
1866 else
1867 return end;
1868 }
1869
1870 // ***************************************************************************
1871 #if COMPILER_LIKES_PRAGMA_MARK
1872 #pragma mark -
1873 #pragma mark - DNS Message Parsing Functions
1874 #endif
1875
1876 mDNSexport mDNSu32 DomainNameHashValue(const domainname *const name)
1877 {
1878 mDNSu32 sum = 0;
1879 const mDNSu8 *c;
1880
1881 for (c = name->c; c[0] != 0 && c[1] != 0; c += 2)
1882 {
1883 sum += ((mDNSIsUpperCase(c[0]) ? c[0] + 'a' - 'A' : c[0]) << 8) |
1884 (mDNSIsUpperCase(c[1]) ? c[1] + 'a' - 'A' : c[1]);
1885 sum = (sum<<3) | (sum>>29);
1886 }
1887 if (c[0]) sum += ((mDNSIsUpperCase(c[0]) ? c[0] + 'a' - 'A' : c[0]) << 8);
1888 return(sum);
1889 }
1890
1891 mDNSexport void SetNewRData(ResourceRecord *const rr, RData *NewRData, mDNSu16 rdlength)
1892 {
1893 domainname *target;
1894 if (NewRData)
1895 {
1896 rr->rdata = NewRData;
1897 rr->rdlength = rdlength;
1898 }
1899 // Must not try to get target pointer until after updating rr->rdata
1900 target = GetRRDomainNameTarget(rr);
1901 rr->rdlength = GetRDLength(rr, mDNSfalse);
1902 rr->rdestimate = GetRDLength(rr, mDNStrue);
1903 rr->rdatahash = target ? DomainNameHashValue(target) : RDataHashValue(rr);
1904 }
1905
1906 mDNSexport const mDNSu8 *skipDomainName(const DNSMessage *const msg, const mDNSu8 *ptr, const mDNSu8 *const end)
1907 {
1908 mDNSu16 total = 0;
1909
1910 if (ptr < (mDNSu8*)msg || ptr >= end)
1911 { debugf("skipDomainName: Illegal ptr not within packet boundaries"); return(mDNSNULL); }
1912
1913 while (1) // Read sequence of labels
1914 {
1915 const mDNSu8 len = *ptr++; // Read length of this label
1916 if (len == 0) return(ptr); // If length is zero, that means this name is complete
1917 switch (len & 0xC0)
1918 {
1919 case 0x00: if (ptr + len >= end) // Remember: expect at least one more byte for the root label
1920 { debugf("skipDomainName: Malformed domain name (overruns packet end)"); return(mDNSNULL); }
1921 if (total + 1 + len >= MAX_DOMAIN_NAME) // Remember: expect at least one more byte for the root label
1922 { debugf("skipDomainName: Malformed domain name (more than 256 characters)"); return(mDNSNULL); }
1923 ptr += len;
1924 total += 1 + len;
1925 break;
1926
1927 case 0x40: debugf("skipDomainName: Extended EDNS0 label types 0x%X not supported", len); return(mDNSNULL);
1928 case 0x80: debugf("skipDomainName: Illegal label length 0x%X", len); return(mDNSNULL);
1929 case 0xC0: return(ptr+1);
1930 }
1931 }
1932 }
1933
1934 // Routine to fetch an FQDN from the DNS message, following compression pointers if necessary.
1935 mDNSexport const mDNSu8 *getDomainName(const DNSMessage *const msg, const mDNSu8 *ptr, const mDNSu8 *const end,
1936 domainname *const name)
1937 {
1938 const mDNSu8 *nextbyte = mDNSNULL; // Record where we got to before we started following pointers
1939 mDNSu8 *np = name->c; // Name pointer
1940 const mDNSu8 *const limit = np + MAX_DOMAIN_NAME; // Limit so we don't overrun buffer
1941
1942 if (ptr < (mDNSu8*)msg || ptr >= end)
1943 { debugf("getDomainName: Illegal ptr not within packet boundaries"); return(mDNSNULL); }
1944
1945 *np = 0; // Tentatively place the root label here (may be overwritten if we have more labels)
1946
1947 while (1) // Read sequence of labels
1948 {
1949 const mDNSu8 len = *ptr++; // Read length of this label
1950 if (len == 0) break; // If length is zero, that means this name is complete
1951 switch (len & 0xC0)
1952 {
1953 int i;
1954 mDNSu16 offset;
1955
1956 case 0x00: if (ptr + len >= end) // Remember: expect at least one more byte for the root label
1957 { debugf("getDomainName: Malformed domain name (overruns packet end)"); return(mDNSNULL); }
1958 if (np + 1 + len >= limit) // Remember: expect at least one more byte for the root label
1959 { debugf("getDomainName: Malformed domain name (more than 256 characters)"); return(mDNSNULL); }
1960 *np++ = len;
1961 for (i=0; i<len; i++) *np++ = *ptr++;
1962 *np = 0; // Tentatively place the root label here (may be overwritten if we have more labels)
1963 break;
1964
1965 case 0x40: debugf("getDomainName: Extended EDNS0 label types 0x%X not supported in name %##s", len, name->c);
1966 return(mDNSNULL);
1967
1968 case 0x80: debugf("getDomainName: Illegal label length 0x%X in domain name %##s", len, name->c); return(mDNSNULL);
1969
1970 case 0xC0: offset = (mDNSu16)((((mDNSu16)(len & 0x3F)) << 8) | *ptr++);
1971 if (!nextbyte) nextbyte = ptr; // Record where we got to before we started following pointers
1972 ptr = (mDNSu8 *)msg + offset;
1973 if (ptr < (mDNSu8*)msg || ptr >= end)
1974 { debugf("getDomainName: Illegal compression pointer not within packet boundaries"); return(mDNSNULL); }
1975 if (*ptr & 0xC0)
1976 { debugf("getDomainName: Compression pointer must point to real label"); return(mDNSNULL); }
1977 break;
1978 }
1979 }
1980
1981 if (nextbyte) return(nextbyte);
1982 else return(ptr);
1983 }
1984
1985 mDNSexport const mDNSu8 *skipResourceRecord(const DNSMessage *msg, const mDNSu8 *ptr, const mDNSu8 *end)
1986 {
1987 mDNSu16 pktrdlength;
1988
1989 ptr = skipDomainName(msg, ptr, end);
1990 if (!ptr) { debugf("skipResourceRecord: Malformed RR name"); return(mDNSNULL); }
1991
1992 if (ptr + 10 > end) { debugf("skipResourceRecord: Malformed RR -- no type/class/ttl/len!"); return(mDNSNULL); }
1993 pktrdlength = (mDNSu16)((mDNSu16)ptr[8] << 8 | ptr[9]);
1994 ptr += 10;
1995 if (ptr + pktrdlength > end) { debugf("skipResourceRecord: RDATA exceeds end of packet"); return(mDNSNULL); }
1996
1997 return(ptr + pktrdlength);
1998 }
1999
2000 mDNSexport const mDNSu8 *GetLargeResourceRecord(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *ptr,
2001 const mDNSu8 *end, const mDNSInterfaceID InterfaceID, mDNSu8 RecordType, LargeCacheRecord *const largecr)
2002 {
2003 CacheRecord *const rr = &largecr->r;
2004 RDataBody2 *const rdb = (RDataBody2 *)rr->smallrdatastorage.data;
2005 mDNSu16 pktrdlength;
2006
2007 if (largecr == &m->rec && m->rec.r.resrec.RecordType)
2008 {
2009 LogMsg("GetLargeResourceRecord: m->rec appears to be already in use for %s", CRDisplayString(m, &m->rec.r));
2010 #if ForceAlerts
2011 *(long*)0 = 0;
2012 #endif
2013 }
2014
2015 rr->next = mDNSNULL;
2016 rr->resrec.name = &largecr->namestorage;
2017
2018 rr->NextInKAList = mDNSNULL;
2019 rr->TimeRcvd = m ? m->timenow : 0;
2020 rr->DelayDelivery = 0;
2021 rr->NextRequiredQuery = m ? m->timenow : 0; // Will be updated to the real value when we call SetNextCacheCheckTimeForRecord()
2022 rr->LastUsed = m ? m->timenow : 0;
2023 rr->CRActiveQuestion = mDNSNULL;
2024 rr->UnansweredQueries = 0;
2025 rr->LastUnansweredTime= 0;
2026 #if ENABLE_MULTI_PACKET_QUERY_SNOOPING
2027 rr->MPUnansweredQ = 0;
2028 rr->MPLastUnansweredQT= 0;
2029 rr->MPUnansweredKA = 0;
2030 rr->MPExpectingKA = mDNSfalse;
2031 #endif
2032 rr->NextInCFList = mDNSNULL;
2033
2034 rr->resrec.InterfaceID = InterfaceID;
2035 rr->resrec.rDNSServer = mDNSNULL;
2036
2037 ptr = getDomainName(msg, ptr, end, &largecr->namestorage); // Will bail out correctly if ptr is NULL
2038 if (!ptr) { debugf("GetLargeResourceRecord: Malformed RR name"); return(mDNSNULL); }
2039 rr->resrec.namehash = DomainNameHashValue(rr->resrec.name);
2040
2041 if (ptr + 10 > end) { debugf("GetLargeResourceRecord: Malformed RR -- no type/class/ttl/len!"); return(mDNSNULL); }
2042
2043 rr->resrec.rrtype = (mDNSu16) ((mDNSu16)ptr[0] << 8 | ptr[1]);
2044 rr->resrec.rrclass = (mDNSu16)(((mDNSu16)ptr[2] << 8 | ptr[3]) & kDNSClass_Mask);
2045 rr->resrec.rroriginalttl = (mDNSu32) ((mDNSu32)ptr[4] << 24 | (mDNSu32)ptr[5] << 16 | (mDNSu32)ptr[6] << 8 | ptr[7]);
2046 if (rr->resrec.rroriginalttl > 0x70000000UL / mDNSPlatformOneSecond && (mDNSs32)rr->resrec.rroriginalttl != -1)
2047 rr->resrec.rroriginalttl = 0x70000000UL / mDNSPlatformOneSecond;
2048 // Note: We don't have to adjust m->NextCacheCheck here -- this is just getting a record into memory for
2049 // us to look at. If we decide to copy it into the cache, then we'll update m->NextCacheCheck accordingly.
2050 pktrdlength = (mDNSu16)((mDNSu16)ptr[8] << 8 | ptr[9]);
2051
2052 // If mDNS record has cache-flush bit set, we mark it unique
2053 // For uDNS records, all are implicitly deemed unique (a single DNS server is always
2054 // authoritative for the entire RRSet), unless this is a truncated response
2055 if (ptr[2] & (kDNSClass_UniqueRRSet >> 8) || (!InterfaceID && !(msg->h.flags.b[0] & kDNSFlag0_TC)))
2056 RecordType |= kDNSRecordTypePacketUniqueMask;
2057 ptr += 10;
2058 if (ptr + pktrdlength > end) { debugf("GetLargeResourceRecord: RDATA exceeds end of packet"); return(mDNSNULL); }
2059 end = ptr + pktrdlength; // Adjust end to indicate the end of the rdata for this resource record
2060
2061 rr->resrec.rdata = (RData*)&rr->smallrdatastorage;
2062 rr->resrec.rdata->MaxRDLength = MaximumRDSize;
2063
2064 if (!RecordType) LogMsg("GetLargeResourceRecord: No RecordType for %##s", rr->resrec.name->c);
2065
2066 // IMPORTANT: Any record type we understand and unpack into a structure containing domainnames needs to have corresponding
2067 // cases in SameRDataBody() and RDataHashValue() to do a semantic comparison (or checksum) of the structure instead of a blind
2068 // bitwise memory compare (or sum). This is because a domainname is a fixed size structure holding variable-length data.
2069 // Any bytes past the logical end of the name are undefined, and a blind bitwise memory compare may indicate that
2070 // two domainnames are different when semantically they are the same name and it's only the unused bytes that differ.
2071 if (rr->resrec.rrclass == kDNSQClass_ANY && pktrdlength == 0) // Used in update packets to mean "Delete An RRset" (RFC 2136)
2072 rr->resrec.rdlength = 0;
2073 else switch (rr->resrec.rrtype)
2074 {
2075 case kDNSType_A: if (pktrdlength != sizeof(mDNSv4Addr)) goto fail;
2076 rdb->ipv4.b[0] = ptr[0];
2077 rdb->ipv4.b[1] = ptr[1];
2078 rdb->ipv4.b[2] = ptr[2];
2079 rdb->ipv4.b[3] = ptr[3];
2080 break;
2081
2082 case kDNSType_NS:
2083 case kDNSType_CNAME:
2084 case kDNSType_PTR:
2085 case kDNSType_DNAME:ptr = getDomainName(msg, ptr, end, &rdb->name);
2086 if (ptr != end) { debugf("GetLargeResourceRecord: Malformed CNAME/PTR RDATA name"); goto fail; }
2087 //debugf("%##s PTR %##s rdlen %d", rr->resrec.name.c, rdb->name.c, pktrdlength);
2088 break;
2089
2090 case kDNSType_SOA: ptr = getDomainName(msg, ptr, end, &rdb->soa.mname);
2091 if (!ptr) { debugf("GetLargeResourceRecord: Malformed SOA RDATA mname"); goto fail; }
2092 ptr = getDomainName(msg, ptr, end, &rdb->soa.rname);
2093 if (!ptr) { debugf("GetLargeResourceRecord: Malformed SOA RDATA rname"); goto fail; }
2094 if (ptr + 0x14 != end) { debugf("GetLargeResourceRecord: Malformed SOA RDATA"); goto fail; }
2095 rdb->soa.serial = (mDNSs32) ((mDNSs32)ptr[0x00] << 24 | (mDNSs32)ptr[0x01] << 16 | (mDNSs32)ptr[0x02] << 8 | ptr[0x03]);
2096 rdb->soa.refresh = (mDNSu32) ((mDNSu32)ptr[0x04] << 24 | (mDNSu32)ptr[0x05] << 16 | (mDNSu32)ptr[0x06] << 8 | ptr[0x07]);
2097 rdb->soa.retry = (mDNSu32) ((mDNSu32)ptr[0x08] << 24 | (mDNSu32)ptr[0x09] << 16 | (mDNSu32)ptr[0x0A] << 8 | ptr[0x0B]);
2098 rdb->soa.expire = (mDNSu32) ((mDNSu32)ptr[0x0C] << 24 | (mDNSu32)ptr[0x0D] << 16 | (mDNSu32)ptr[0x0E] << 8 | ptr[0x0F]);
2099 rdb->soa.min = (mDNSu32) ((mDNSu32)ptr[0x10] << 24 | (mDNSu32)ptr[0x11] << 16 | (mDNSu32)ptr[0x12] << 8 | ptr[0x13]);
2100 break;
2101
2102 case kDNSType_NULL:
2103 case kDNSType_HINFO:
2104 case kDNSType_TSIG:
2105 case kDNSType_TXT:
2106 case kDNSType_X25:
2107 case kDNSType_ISDN:
2108 case kDNSType_LOC:
2109 case kDNSType_DHCID:if (pktrdlength > rr->resrec.rdata->MaxRDLength)
2110 {
2111 debugf("GetLargeResourceRecord: %s rdata size (%d) exceeds storage (%d)",
2112 DNSTypeName(rr->resrec.rrtype), pktrdlength, rr->resrec.rdata->MaxRDLength);
2113 goto fail;
2114 }
2115 rr->resrec.rdlength = pktrdlength;
2116 mDNSPlatformMemCopy(rdb->data, ptr, pktrdlength);
2117 break;
2118
2119 case kDNSType_MX:
2120 case kDNSType_AFSDB:
2121 case kDNSType_RT:
2122 case kDNSType_KX: if (pktrdlength < 3) goto fail; // Preference + domainname
2123 rdb->mx.preference = (mDNSu16)((mDNSu16)ptr[0] << 8 | ptr[1]);
2124 ptr = getDomainName(msg, ptr+2, end, &rdb->mx.exchange);
2125 if (ptr != end) { debugf("GetLargeResourceRecord: Malformed MX name"); goto fail; }
2126 //debugf("%##s SRV %##s rdlen %d", rr->resrec.name.c, rdb->srv.target.c, pktrdlength);
2127 break;
2128
2129 case kDNSType_RP: ptr = getDomainName(msg, ptr, end, &rdb->rp.mbox); // Domainname + domainname
2130 if (!ptr) { debugf("GetLargeResourceRecord: Malformed RP mbox"); goto fail; }
2131 ptr = getDomainName(msg, ptr, end, &rdb->rp.txt);
2132 if (ptr != end) { debugf("GetLargeResourceRecord: Malformed RP txt"); goto fail; }
2133 break;
2134
2135 case kDNSType_PX: if (pktrdlength < 4) goto fail; // Preference + domainname + domainname
2136 rdb->px.preference = (mDNSu16)((mDNSu16)ptr[0] << 8 | ptr[1]);
2137 ptr = getDomainName(msg, ptr, end, &rdb->px.map822);
2138 if (!ptr) { debugf("GetLargeResourceRecord: Malformed PX map822"); goto fail; }
2139 ptr = getDomainName(msg, ptr, end, &rdb->px.mapx400);
2140 if (ptr != end) { debugf("GetLargeResourceRecord: Malformed PX mapx400"); goto fail; }
2141 break;
2142
2143 case kDNSType_AAAA: if (pktrdlength != sizeof(mDNSv6Addr)) goto fail;
2144 mDNSPlatformMemCopy(&rdb->ipv6, ptr, sizeof(rdb->ipv6));
2145 break;
2146
2147 case kDNSType_SRV: if (pktrdlength < 7) goto fail; // Priority + weight + port + domainname
2148 rdb->srv.priority = (mDNSu16)((mDNSu16)ptr[0] << 8 | ptr[1]);
2149 rdb->srv.weight = (mDNSu16)((mDNSu16)ptr[2] << 8 | ptr[3]);
2150 rdb->srv.port.b[0] = ptr[4];
2151 rdb->srv.port.b[1] = ptr[5];
2152 ptr = getDomainName(msg, ptr+6, end, &rdb->srv.target);
2153 if (ptr != end) { debugf("GetLargeResourceRecord: Malformed SRV RDATA name"); goto fail; }
2154 //debugf("%##s SRV %##s rdlen %d", rr->resrec.name.c, rdb->srv.target.c, pktrdlength);
2155 break;
2156
2157 case kDNSType_OPT: {
2158 rdataOPT *opt = rr->resrec.rdata->u.opt;
2159 rr->resrec.rdlength = 0;
2160 while (ptr < end && (mDNSu8 *)(opt+1) < &rr->resrec.rdata->u.data[MaximumRDSize])
2161 {
2162 const rdataOPT *const currentopt = opt;
2163 if (ptr + 4 > end) { LogInfo("GetLargeResourceRecord: OPT RDATA ptr + 4 > end"); goto fail; }
2164 opt->opt = (mDNSu16)((mDNSu16)ptr[0] << 8 | ptr[1]);
2165 opt->optlen = (mDNSu16)((mDNSu16)ptr[2] << 8 | ptr[3]);
2166 ptr += 4;
2167 if (ptr + opt->optlen > end) { LogInfo("GetLargeResourceRecord: ptr + opt->optlen > end"); goto fail; }
2168 switch (opt->opt)
2169 {
2170 case kDNSOpt_LLQ:
2171 if (opt->optlen == DNSOpt_LLQData_Space - 4)
2172 {
2173 opt->u.llq.vers = (mDNSu16)((mDNSu16)ptr[0] << 8 | ptr[1]);
2174 opt->u.llq.llqOp = (mDNSu16)((mDNSu16)ptr[2] << 8 | ptr[3]);
2175 opt->u.llq.err = (mDNSu16)((mDNSu16)ptr[4] << 8 | ptr[5]);
2176 mDNSPlatformMemCopy(opt->u.llq.id.b, ptr+6, 8);
2177 opt->u.llq.llqlease = (mDNSu32) ((mDNSu32)ptr[14] << 24 | (mDNSu32)ptr[15] << 16 | (mDNSu32)ptr[16] << 8 | ptr[17]);
2178 if (opt->u.llq.llqlease > 0x70000000UL / mDNSPlatformOneSecond)
2179 opt->u.llq.llqlease = 0x70000000UL / mDNSPlatformOneSecond;
2180 opt++;
2181 }
2182 break;
2183 case kDNSOpt_Lease:
2184 if (opt->optlen == DNSOpt_LeaseData_Space - 4)
2185 {
2186 opt->u.updatelease = (mDNSu32) ((mDNSu32)ptr[0] << 24 | (mDNSu32)ptr[1] << 16 | (mDNSu32)ptr[2] << 8 | ptr[3]);
2187 if (opt->u.updatelease > 0x70000000UL / mDNSPlatformOneSecond)
2188 opt->u.updatelease = 0x70000000UL / mDNSPlatformOneSecond;
2189 opt++;
2190 }
2191 break;
2192 case kDNSOpt_Owner:
2193 if (ValidOwnerLength(opt->optlen))
2194 {
2195 opt->u.owner.vers = ptr[0];
2196 opt->u.owner.seq = ptr[1];
2197 mDNSPlatformMemCopy(opt->u.owner.HMAC.b, ptr+2, 6); // 6-byte MAC address
2198 mDNSPlatformMemCopy(opt->u.owner.IMAC.b, ptr+2, 6); // 6-byte MAC address
2199 opt->u.owner.password = zeroEthAddr;
2200 if (opt->optlen >= DNSOpt_OwnerData_ID_Wake_Space-4)
2201 {
2202 mDNSPlatformMemCopy(opt->u.owner.IMAC.b, ptr+8, 6); // 6-byte MAC address
2203 // This mDNSPlatformMemCopy is safe because the ValidOwnerLength(opt->optlen) check above
2204 // ensures that opt->optlen is no more than DNSOpt_OwnerData_ID_Wake_PW6_Space - 4
2205 if (opt->optlen > DNSOpt_OwnerData_ID_Wake_Space-4)
2206 mDNSPlatformMemCopy(opt->u.owner.password.b, ptr+14, opt->optlen - (DNSOpt_OwnerData_ID_Wake_Space-4));
2207 }
2208 opt++;
2209 }
2210 break;
2211 }
2212 ptr += currentopt->optlen;
2213 }
2214 rr->resrec.rdlength = (mDNSu16)((mDNSu8*)opt - rr->resrec.rdata->u.data);
2215 if (ptr != end) { LogInfo("GetLargeResourceRecord: Malformed OptRdata"); goto fail; }
2216 break;
2217 }
2218
2219 case kDNSType_NSEC: {
2220 unsigned int i, j;
2221 domainname d;
2222 ptr = getDomainName(msg, ptr, end, &d); // Ignored for our simplified use of NSEC synthetic records
2223 if (!ptr) { LogInfo("GetLargeResourceRecord: Malformed NSEC nextname"); goto fail; }
2224 mDNSPlatformMemZero(rdb->nsec.bitmap, sizeof(rdb->nsec.bitmap));
2225 if (ptr < end)
2226 {
2227 if (*ptr++ != 0) { debugf("GetLargeResourceRecord: We only handle block zero NSECs"); goto fail; }
2228 i = *ptr++;
2229 if (i > sizeof(rdataNSEC)) { debugf("GetLargeResourceRecord: invalid block length %d", i); goto fail; }
2230 for (j=0; j<i; j++) rdb->nsec.bitmap[j] = *ptr++;
2231 }
2232 if (ptr != end) { debugf("GetLargeResourceRecord: Malformed NSEC"); goto fail; }
2233 break;
2234 }
2235
2236 default: if (pktrdlength > rr->resrec.rdata->MaxRDLength)
2237 {
2238 debugf("GetLargeResourceRecord: rdata %d (%s) size (%d) exceeds storage (%d)",
2239 rr->resrec.rrtype, DNSTypeName(rr->resrec.rrtype), pktrdlength, rr->resrec.rdata->MaxRDLength);
2240 goto fail;
2241 }
2242 debugf("GetLargeResourceRecord: Warning! Reading resource type %d (%s) as opaque data",
2243 rr->resrec.rrtype, DNSTypeName(rr->resrec.rrtype));
2244 // Note: Just because we don't understand the record type, that doesn't
2245 // mean we fail. The DNS protocol specifies rdlength, so we can
2246 // safely skip over unknown records and ignore them.
2247 // We also grab a binary copy of the rdata anyway, since the caller
2248 // might know how to interpret it even if we don't.
2249 rr->resrec.rdlength = pktrdlength;
2250 mDNSPlatformMemCopy(rdb->data, ptr, pktrdlength);
2251 break;
2252 }
2253
2254 SetNewRData(&rr->resrec, mDNSNULL, 0); // Sets rdlength, rdestimate, rdatahash for us
2255
2256 // Success! Now fill in RecordType to show this record contains valid data
2257 rr->resrec.RecordType = RecordType;
2258 return(end);
2259
2260 fail:
2261 // If we were unable to parse the rdata in this record, we indicate that by
2262 // returing a 'kDNSRecordTypePacketNegative' record with rdlength set to zero
2263 rr->resrec.RecordType = kDNSRecordTypePacketNegative;
2264 rr->resrec.rdlength = 0;
2265 rr->resrec.rdestimate = 0;
2266 rr->resrec.rdatahash = 0;
2267 return(end);
2268 }
2269
2270 mDNSexport const mDNSu8 *skipQuestion(const DNSMessage *msg, const mDNSu8 *ptr, const mDNSu8 *end)
2271 {
2272 ptr = skipDomainName(msg, ptr, end);
2273 if (!ptr) { debugf("skipQuestion: Malformed domain name in DNS question section"); return(mDNSNULL); }
2274 if (ptr+4 > end) { debugf("skipQuestion: Malformed DNS question section -- no query type and class!"); return(mDNSNULL); }
2275 return(ptr+4);
2276 }
2277
2278 mDNSexport const mDNSu8 *getQuestion(const DNSMessage *msg, const mDNSu8 *ptr, const mDNSu8 *end, const mDNSInterfaceID InterfaceID,
2279 DNSQuestion *question)
2280 {
2281 mDNSPlatformMemZero(question, sizeof(*question));
2282 question->InterfaceID = InterfaceID;
2283 if (!InterfaceID) question->TargetQID = onesID; // In DNSQuestions we use TargetQID as the indicator of whether it's unicast or multicast
2284 ptr = getDomainName(msg, ptr, end, &question->qname);
2285 if (!ptr) { debugf("Malformed domain name in DNS question section"); return(mDNSNULL); }
2286 if (ptr+4 > end) { debugf("Malformed DNS question section -- no query type and class!"); return(mDNSNULL); }
2287
2288 question->qnamehash = DomainNameHashValue(&question->qname);
2289 question->qtype = (mDNSu16)((mDNSu16)ptr[0] << 8 | ptr[1]); // Get type
2290 question->qclass = (mDNSu16)((mDNSu16)ptr[2] << 8 | ptr[3]); // and class
2291 return(ptr+4);
2292 }
2293
2294 mDNSexport const mDNSu8 *LocateAnswers(const DNSMessage *const msg, const mDNSu8 *const end)
2295 {
2296 int i;
2297 const mDNSu8 *ptr = msg->data;
2298 for (i = 0; i < msg->h.numQuestions && ptr; i++) ptr = skipQuestion(msg, ptr, end);
2299 return(ptr);
2300 }
2301
2302 mDNSexport const mDNSu8 *LocateAuthorities(const DNSMessage *const msg, const mDNSu8 *const end)
2303 {
2304 int i;
2305 const mDNSu8 *ptr = LocateAnswers(msg, end);
2306 for (i = 0; i < msg->h.numAnswers && ptr; i++) ptr = skipResourceRecord(msg, ptr, end);
2307 return(ptr);
2308 }
2309
2310 mDNSexport const mDNSu8 *LocateAdditionals(const DNSMessage *const msg, const mDNSu8 *const end)
2311 {
2312 int i;
2313 const mDNSu8 *ptr = LocateAuthorities(msg, end);
2314 for (i = 0; i < msg->h.numAuthorities; i++) ptr = skipResourceRecord(msg, ptr, end);
2315 return (ptr);
2316 }
2317
2318 mDNSexport const mDNSu8 *LocateOptRR(const DNSMessage *const msg, const mDNSu8 *const end, int minsize)
2319 {
2320 int i;
2321 const mDNSu8 *ptr = LocateAdditionals(msg, end);
2322
2323 // Locate the OPT record.
2324 // According to RFC 2671, "One OPT pseudo-RR can be added to the additional data section of either a request or a response."
2325 // This implies that there may be *at most* one OPT record per DNS message, in the Additional Section,
2326 // but not necessarily the *last* entry in the Additional Section.
2327 for (i = 0; ptr && i < msg->h.numAdditionals; i++)
2328 {
2329 if (ptr + DNSOpt_Header_Space + minsize <= end && // Make sure we have 11+minsize bytes of data
2330 ptr[0] == 0 && // Name must be root label
2331 ptr[1] == (kDNSType_OPT >> 8 ) && // rrtype OPT
2332 ptr[2] == (kDNSType_OPT & 0xFF) &&
2333 ((mDNSu16)ptr[9] << 8 | (mDNSu16)ptr[10]) >= (mDNSu16)minsize)
2334 return(ptr);
2335 else
2336 ptr = skipResourceRecord(msg, ptr, end);
2337 }
2338 return(mDNSNULL);
2339 }
2340
2341 // On success, GetLLQOptData returns pointer to storage within shared "m->rec";
2342 // it is caller's responsibilty to clear m->rec.r.resrec.RecordType after use
2343 // Note: An OPT RDataBody actually contains one or more variable-length rdataOPT objects packed together
2344 // The code that currently calls this assumes there's only one, instead of iterating through the set
2345 mDNSexport const rdataOPT *GetLLQOptData(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *const end)
2346 {
2347 const mDNSu8 *ptr = LocateOptRR(msg, end, DNSOpt_LLQData_Space);
2348 if (ptr)
2349 {
2350 ptr = GetLargeResourceRecord(m, msg, ptr, end, 0, kDNSRecordTypePacketAdd, &m->rec);
2351 if (ptr && m->rec.r.resrec.RecordType != kDNSRecordTypePacketNegative) return(&m->rec.r.resrec.rdata->u.opt[0]);
2352 }
2353 return(mDNSNULL);
2354 }
2355
2356 // Get the lease life of records in a dynamic update
2357 // returns 0 on error or if no lease present
2358 mDNSexport mDNSu32 GetPktLease(mDNS *m, DNSMessage *msg, const mDNSu8 *end)
2359 {
2360 mDNSu32 result = 0;
2361 const mDNSu8 *ptr = LocateOptRR(msg, end, DNSOpt_LeaseData_Space);
2362 if (ptr) ptr = GetLargeResourceRecord(m, msg, ptr, end, 0, kDNSRecordTypePacketAdd, &m->rec);
2363 if (ptr && m->rec.r.resrec.rdlength >= DNSOpt_LeaseData_Space && m->rec.r.resrec.rdata->u.opt[0].opt == kDNSOpt_Lease)
2364 result = m->rec.r.resrec.rdata->u.opt[0].u.updatelease;
2365 m->rec.r.resrec.RecordType = 0; // Clear RecordType to show we're not still using it
2366 return(result);
2367 }
2368
2369 mDNSlocal const mDNSu8 *DumpRecords(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *ptr, const mDNSu8 *const end, int count, char *label)
2370 {
2371 int i;
2372 LogMsg("%2d %s", count, label);
2373 for (i = 0; i < count && ptr; i++)
2374 {
2375 // This puts a LargeCacheRecord on the stack instead of using the shared m->rec storage,
2376 // but since it's only used for debugging (and probably only on OS X, not on
2377 // embedded systems) putting a 9kB object on the stack isn't a big problem.
2378 LargeCacheRecord largecr;
2379 ptr = GetLargeResourceRecord(m, msg, ptr, end, mDNSInterface_Any, kDNSRecordTypePacketAns, &largecr);
2380 if (ptr) LogMsg("%2d TTL%8d %s", i, largecr.r.resrec.rroriginalttl, CRDisplayString(m, &largecr.r));
2381 }
2382 if (!ptr) LogMsg("ERROR: Premature end of packet data");
2383 return(ptr);
2384 }
2385
2386 #define DNS_OP_Name(X) ( \
2387 (X) == kDNSFlag0_OP_StdQuery ? "" : \
2388 (X) == kDNSFlag0_OP_Iquery ? "Iquery " : \
2389 (X) == kDNSFlag0_OP_Status ? "Status " : \
2390 (X) == kDNSFlag0_OP_Unused3 ? "Unused3 " : \
2391 (X) == kDNSFlag0_OP_Notify ? "Notify " : \
2392 (X) == kDNSFlag0_OP_Update ? "Update " : "?? " )
2393
2394 #define DNS_RC_Name(X) ( \
2395 (X) == kDNSFlag1_RC_NoErr ? "NoErr" : \
2396 (X) == kDNSFlag1_RC_FormErr ? "FormErr" : \
2397 (X) == kDNSFlag1_RC_ServFail ? "ServFail" : \
2398 (X) == kDNSFlag1_RC_NXDomain ? "NXDomain" : \
2399 (X) == kDNSFlag1_RC_NotImpl ? "NotImpl" : \
2400 (X) == kDNSFlag1_RC_Refused ? "Refused" : \
2401 (X) == kDNSFlag1_RC_YXDomain ? "YXDomain" : \
2402 (X) == kDNSFlag1_RC_YXRRSet ? "YXRRSet" : \
2403 (X) == kDNSFlag1_RC_NXRRSet ? "NXRRSet" : \
2404 (X) == kDNSFlag1_RC_NotAuth ? "NotAuth" : \
2405 (X) == kDNSFlag1_RC_NotZone ? "NotZone" : "??" )
2406
2407 // Note: DumpPacket expects the packet header fields in host byte order, not network byte order
2408 mDNSexport void DumpPacket(mDNS *const m, mStatus status, mDNSBool sent, char *transport,
2409 const mDNSAddr *srcaddr, mDNSIPPort srcport,
2410 const mDNSAddr *dstaddr, mDNSIPPort dstport, const DNSMessage *const msg, const mDNSu8 *const end)
2411 {
2412 mDNSBool IsUpdate = ((msg->h.flags.b[0] & kDNSFlag0_OP_Mask) == kDNSFlag0_OP_Update);
2413 const mDNSu8 *ptr = msg->data;
2414 int i;
2415 DNSQuestion q;
2416 char tbuffer[64], sbuffer[64], dbuffer[64] = "";
2417 if (!status) tbuffer[mDNS_snprintf(tbuffer, sizeof(tbuffer), sent ? "Sent" : "Received" )] = 0;
2418 else tbuffer[mDNS_snprintf(tbuffer, sizeof(tbuffer), "ERROR %d %sing", status, sent ? "Send" : "Receiv")] = 0;
2419 if (sent) sbuffer[mDNS_snprintf(sbuffer, sizeof(sbuffer), "port " )] = 0;
2420 else sbuffer[mDNS_snprintf(sbuffer, sizeof(sbuffer), "%#a:", srcaddr)] = 0;
2421 if (dstaddr || !mDNSIPPortIsZero(dstport))
2422 dbuffer[mDNS_snprintf(dbuffer, sizeof(dbuffer), " to %#a:%d", dstaddr, mDNSVal16(dstport))] = 0;
2423
2424 LogMsg("-- %s %s DNS %s%s (flags %02X%02X) RCODE: %s (%d) %s%s%s%s%s%sID: %d %d bytes from %s%d%s%s --",
2425 tbuffer, transport,
2426 DNS_OP_Name(msg->h.flags.b[0] & kDNSFlag0_OP_Mask),
2427 msg->h.flags.b[0] & kDNSFlag0_QR_Response ? "Response" : "Query",
2428 msg->h.flags.b[0], msg->h.flags.b[1],
2429 DNS_RC_Name(msg->h.flags.b[1] & kDNSFlag1_RC_Mask),
2430 msg->h.flags.b[1] & kDNSFlag1_RC_Mask,
2431 msg->h.flags.b[0] & kDNSFlag0_AA ? "AA " : "",
2432 msg->h.flags.b[0] & kDNSFlag0_TC ? "TC " : "",
2433 msg->h.flags.b[0] & kDNSFlag0_RD ? "RD " : "",
2434 msg->h.flags.b[1] & kDNSFlag1_RA ? "RA " : "",
2435 msg->h.flags.b[1] & kDNSFlag1_AD ? "AD " : "",
2436 msg->h.flags.b[1] & kDNSFlag1_CD ? "CD " : "",
2437 mDNSVal16(msg->h.id),
2438 end - msg->data,
2439 sbuffer, mDNSVal16(srcport), dbuffer,
2440 (msg->h.flags.b[0] & kDNSFlag0_TC) ? " (truncated)" : ""
2441 );
2442
2443 LogMsg("%2d %s", msg->h.numQuestions, IsUpdate ? "Zone" : "Questions");
2444 for (i = 0; i < msg->h.numQuestions && ptr; i++)
2445 {
2446 ptr = getQuestion(msg, ptr, end, mDNSInterface_Any, &q);
2447 if (ptr) LogMsg("%2d %##s %s", i, q.qname.c, DNSTypeName(q.qtype));
2448 }
2449 ptr = DumpRecords(m, msg, ptr, end, msg->h.numAnswers, IsUpdate ? "Prerequisites" : "Answers");
2450 ptr = DumpRecords(m, msg, ptr, end, msg->h.numAuthorities, IsUpdate ? "Updates" : "Authorities");
2451 ptr = DumpRecords(m, msg, ptr, end, msg->h.numAdditionals, "Additionals");
2452 LogMsg("--------------");
2453 }
2454
2455 // ***************************************************************************
2456 #if COMPILER_LIKES_PRAGMA_MARK
2457 #pragma mark -
2458 #pragma mark - Packet Sending Functions
2459 #endif
2460
2461 // Stub definition of TCPSocket_struct so we can access flags field. (Rest of TCPSocket_struct is platform-dependent.)
2462 struct TCPSocket_struct { TCPSocketFlags flags; /* ... */ };
2463
2464 struct UDPSocket_struct
2465 {
2466 mDNSIPPort port; // MUST BE FIRST FIELD -- mDNSCoreReceive expects every UDPSocket_struct to begin with mDNSIPPort port
2467 };
2468
2469 // Note: When we sign a DNS message using DNSDigest_SignMessage(), the current real-time clock value is used, which
2470 // is why we generally defer signing until we send the message, to ensure the signature is as fresh as possible.
2471 mDNSexport mStatus mDNSSendDNSMessage(mDNS *const m, DNSMessage *const msg, mDNSu8 *end,
2472 mDNSInterfaceID InterfaceID, UDPSocket *src, const mDNSAddr *dst, mDNSIPPort dstport, TCPSocket *sock, DomainAuthInfo *authInfo)
2473 {
2474 mStatus status = mStatus_NoError;
2475 const mDNSu16 numAdditionals = msg->h.numAdditionals;
2476 mDNSu8 *newend;
2477 mDNSu8 *limit = msg->data + AbsoluteMaxDNSMessageData;
2478
2479 // Zero-length message data is okay (e.g. for a DNS Update ack, where all we need is an ID and an error code
2480 if (end < msg->data || end - msg->data > AbsoluteMaxDNSMessageData)
2481 {
2482 LogMsg("mDNSSendDNSMessage: invalid message %p %p %d", msg->data, end, end - msg->data);
2483 return mStatus_BadParamErr;
2484 }
2485
2486 newend = putHINFO(m, msg, end, authInfo, limit);
2487 if (!newend) LogMsg("mDNSSendDNSMessage: putHINFO failed msg %p end %p, limit %p", msg->data, end, limit); // Not fatal
2488 else end = newend;
2489
2490 // Put all the integer values in IETF byte-order (MSB first, LSB second)
2491 SwapDNSHeaderBytes(msg);
2492
2493 if (authInfo) DNSDigest_SignMessage(msg, &end, authInfo, 0); // DNSDigest_SignMessage operates on message in network byte order
2494 if (!end) { LogMsg("mDNSSendDNSMessage: DNSDigest_SignMessage failed"); status = mStatus_NoMemoryErr; }
2495 else
2496 {
2497 // Send the packet on the wire
2498 if (!sock)
2499 status = mDNSPlatformSendUDP(m, msg, end, InterfaceID, src, dst, dstport);
2500 else
2501 {
2502 mDNSu16 msglen = (mDNSu16)(end - (mDNSu8 *)msg);
2503 mDNSu8 lenbuf[2] = { (mDNSu8)(msglen >> 8), (mDNSu8)(msglen & 0xFF) };
2504 long nsent = mDNSPlatformWriteTCP(sock, (char*)lenbuf, 2); // Should do scatter/gather here -- this is probably going out as two packets
2505 if (nsent != 2) { LogMsg("mDNSSendDNSMessage: write msg length failed %d/%d", nsent, 2); status = mStatus_ConnFailed; }
2506 else
2507 {
2508 nsent = mDNSPlatformWriteTCP(sock, (char *)msg, msglen);
2509 if (nsent != msglen) { LogMsg("mDNSSendDNSMessage: write msg body failed %d/%d", nsent, msglen); status = mStatus_ConnFailed; }
2510 }
2511 }
2512 }
2513
2514 // Swap the integer values back the way they were (remember that numAdditionals may have been changed by putHINFO and/or SignMessage)
2515 SwapDNSHeaderBytes(msg);
2516
2517 // Dump the packet with the HINFO and TSIG
2518 if (mDNS_PacketLoggingEnabled && !mDNSOpaque16IsZero(msg->h.id))
2519 DumpPacket(m, status, mDNStrue, sock && (sock->flags & kTCPSocketFlags_UseTLS) ? "TLS" : sock ? "TCP" : "UDP", mDNSNULL, src ? src->port : MulticastDNSPort, dst, dstport, msg, end);
2520
2521 // put the number of additionals back the way it was
2522 msg->h.numAdditionals = numAdditionals;
2523
2524 return(status);
2525 }
2526
2527 // ***************************************************************************
2528 #if COMPILER_LIKES_PRAGMA_MARK
2529 #pragma mark -
2530 #pragma mark - RR List Management & Task Management
2531 #endif
2532
2533 mDNSexport void mDNS_Lock_(mDNS *const m, const char * const functionname)
2534 {
2535 // MUST grab the platform lock FIRST!
2536 mDNSPlatformLock(m);
2537
2538 // Normally, mDNS_reentrancy is zero and so is mDNS_busy
2539 // However, when we call a client callback mDNS_busy is one, and we increment mDNS_reentrancy too
2540 // If that client callback does mDNS API calls, mDNS_reentrancy and mDNS_busy will both be one
2541 // If mDNS_busy != mDNS_reentrancy that's a bad sign
2542 if (m->mDNS_busy != m->mDNS_reentrancy)
2543 {
2544 LogMsg("%s: mDNS_Lock: Locking failure! mDNS_busy (%ld) != mDNS_reentrancy (%ld)", functionname, m->mDNS_busy, m->mDNS_reentrancy);
2545 #if ForceAlerts
2546 *(long*)0 = 0;
2547 #endif
2548 }
2549
2550 // If this is an initial entry into the mDNSCore code, set m->timenow
2551 // else, if this is a re-entrant entry into the mDNSCore code, m->timenow should already be set
2552 if (m->mDNS_busy == 0)
2553 {
2554 if (m->timenow)
2555 LogMsg("%s: mDNS_Lock: m->timenow already set (%ld/%ld)", functionname, m->timenow, mDNS_TimeNow_NoLock(m));
2556 m->timenow = mDNS_TimeNow_NoLock(m);
2557 if (m->timenow == 0) m->timenow = 1;
2558 }
2559 else if (m->timenow == 0)
2560 {
2561 LogMsg("%s: mDNS_Lock: m->mDNS_busy is %ld but m->timenow not set", functionname, m->mDNS_busy);
2562 m->timenow = mDNS_TimeNow_NoLock(m);
2563 if (m->timenow == 0) m->timenow = 1;
2564 }
2565
2566 if (m->timenow_last - m->timenow > 0)
2567 {
2568 m->timenow_adjust += m->timenow_last - m->timenow;
2569 LogMsg("%s: mDNSPlatformRawTime went backwards by %ld ticks; setting correction factor to %ld", functionname, m->timenow_last - m->timenow, m->timenow_adjust);
2570 m->timenow = m->timenow_last;
2571 }
2572 m->timenow_last = m->timenow;
2573
2574 // Increment mDNS_busy so we'll recognise re-entrant calls
2575 m->mDNS_busy++;
2576 }
2577
2578 mDNSlocal AuthRecord *AnyLocalRecordReady(const mDNS *const m)
2579 {
2580 AuthRecord *rr;
2581 for (rr = m->NewLocalRecords; rr; rr = rr->next)
2582 if (LocalRecordReady(rr)) return rr;
2583 return mDNSNULL;
2584 }
2585
2586 mDNSlocal mDNSs32 GetNextScheduledEvent(const mDNS *const m)
2587 {
2588 mDNSs32 e = m->timenow + 0x78000000;
2589 if (m->mDNSPlatformStatus != mStatus_NoError) return(e);
2590 if (m->NewQuestions)
2591 {
2592 if (m->NewQuestions->DelayAnswering) e = m->NewQuestions->DelayAnswering;
2593 else return(m->timenow);
2594 }
2595 if (m->NewLocalOnlyQuestions) return(m->timenow);
2596 if (m->NewLocalRecords && AnyLocalRecordReady(m)) return(m->timenow);
2597 if (m->SPSProxyListChanged) return(m->timenow);
2598 if (m->LocalRemoveEvents) return(m->timenow);
2599
2600 #ifndef UNICAST_DISABLED
2601 if (e - m->NextuDNSEvent > 0) e = m->NextuDNSEvent;
2602 if (e - m->NextScheduledNATOp > 0) e = m->NextScheduledNATOp;
2603 if (m->NextSRVUpdate && e - m->NextSRVUpdate > 0) e = m->NextSRVUpdate;
2604 #endif
2605
2606 if (e - m->NextCacheCheck > 0) e = m->NextCacheCheck;
2607 if (e - m->NextScheduledSPS > 0) e = m->NextScheduledSPS;
2608 // NextScheduledSPRetry only valid when DelaySleep not set
2609 if (!m->DelaySleep && m->SleepLimit && e - m->NextScheduledSPRetry > 0) e = m->NextScheduledSPRetry;
2610 if (m->DelaySleep && e - m->DelaySleep > 0) e = m->DelaySleep;
2611
2612 if (m->SuppressSending)
2613 {
2614 if (e - m->SuppressSending > 0) e = m->SuppressSending;
2615 }
2616 else
2617 {
2618 if (e - m->NextScheduledQuery > 0) e = m->NextScheduledQuery;
2619 if (e - m->NextScheduledProbe > 0) e = m->NextScheduledProbe;
2620 if (e - m->NextScheduledResponse > 0) e = m->NextScheduledResponse;
2621 }
2622
2623 return(e);
2624 }
2625
2626 mDNSexport void ShowTaskSchedulingError(mDNS *const m)
2627 {
2628 mDNS_Lock(m);
2629
2630 LogMsg("Task Scheduling Error: Continuously busy for more than a second");
2631
2632 // Note: To accurately diagnose *why* we're busy, the debugging code here needs to mirror the logic in GetNextScheduledEvent above
2633
2634 if (m->NewQuestions && (!m->NewQuestions->DelayAnswering || m->timenow - m->NewQuestions->DelayAnswering >= 0))
2635 LogMsg("Task Scheduling Error: NewQuestion %##s (%s)",
2636 m->NewQuestions->qname.c, DNSTypeName(m->NewQuestions->qtype));
2637
2638 if (m->NewLocalOnlyQuestions)
2639 LogMsg("Task Scheduling Error: NewLocalOnlyQuestions %##s (%s)",
2640 m->NewLocalOnlyQuestions->qname.c, DNSTypeName(m->NewLocalOnlyQuestions->qtype));
2641
2642 if (m->NewLocalRecords)
2643 {
2644 AuthRecord *rr = AnyLocalRecordReady(m);
2645 if (rr) LogMsg("Task Scheduling Error: NewLocalRecords %s", ARDisplayString(m, rr));
2646 }
2647
2648 if (m->SPSProxyListChanged) LogMsg("Task Scheduling Error: SPSProxyListChanged");
2649 if (m->LocalRemoveEvents) LogMsg("Task Scheduling Error: LocalRemoveEvents");
2650
2651 if (m->timenow - m->NextScheduledEvent >= 0)
2652 LogMsg("Task Scheduling Error: m->NextScheduledEvent %d", m->timenow - m->NextScheduledEvent);
2653
2654 #ifndef UNICAST_DISABLED
2655 if (m->timenow - m->NextuDNSEvent >= 0)
2656 LogMsg("Task Scheduling Error: m->NextuDNSEvent %d", m->timenow - m->NextuDNSEvent);
2657 if (m->timenow - m->NextScheduledNATOp >= 0)
2658 LogMsg("Task Scheduling Error: m->NextScheduledNATOp %d", m->timenow - m->NextScheduledNATOp);
2659 if (m->NextSRVUpdate && m->timenow - m->NextSRVUpdate >= 0)
2660 LogMsg("Task Scheduling Error: m->NextSRVUpdate %d", m->timenow - m->NextSRVUpdate);
2661 #endif
2662
2663 if (m->timenow - m->NextCacheCheck >= 0)
2664 LogMsg("Task Scheduling Error: m->NextCacheCheck %d", m->timenow - m->NextCacheCheck);
2665 if (m->timenow - m->NextScheduledSPS >= 0)
2666 LogMsg("Task Scheduling Error: m->NextScheduledSPS %d", m->timenow - m->NextScheduledSPS);
2667 if (!m->DelaySleep && m->SleepLimit && m->timenow - m->NextScheduledSPRetry >= 0)
2668 LogMsg("Task Scheduling Error: m->NextScheduledSPRetry %d", m->timenow - m->NextScheduledSPRetry);
2669 if (m->DelaySleep && m->timenow - m->DelaySleep >= 0)
2670 LogMsg("Task Scheduling Error: m->DelaySleep %d", m->timenow - m->DelaySleep);
2671
2672 if (m->SuppressSending && m->timenow - m->SuppressSending >= 0)
2673 LogMsg("Task Scheduling Error: m->SuppressSending %d", m->timenow - m->SuppressSending);
2674 if (m->timenow - m->NextScheduledQuery >= 0)
2675 LogMsg("Task Scheduling Error: m->NextScheduledQuery %d", m->timenow - m->NextScheduledQuery);
2676 if (m->timenow - m->NextScheduledProbe >= 0)
2677 LogMsg("Task Scheduling Error: m->NextScheduledProbe %d", m->timenow - m->NextScheduledProbe);
2678 if (m->timenow - m->NextScheduledResponse >= 0)
2679 LogMsg("Task Scheduling Error: m->NextScheduledResponse %d", m->timenow - m->NextScheduledResponse);
2680
2681 mDNS_Unlock(m);
2682 }
2683
2684 mDNSexport void mDNS_Unlock_(mDNS *const m, const char * const functionname)
2685 {
2686 // Decrement mDNS_busy
2687 m->mDNS_busy--;
2688
2689 // Check for locking failures
2690 if (m->mDNS_busy != m->mDNS_reentrancy)
2691 {
2692 LogMsg("%s: mDNS_Unlock: Locking failure! mDNS_busy (%ld) != mDNS_reentrancy (%ld)", functionname, m->mDNS_busy, m->mDNS_reentrancy);
2693 #if ForceAlerts
2694 *(long*)0 = 0;
2695 #endif
2696 }
2697
2698 // If this is a final exit from the mDNSCore code, set m->NextScheduledEvent and clear m->timenow
2699 if (m->mDNS_busy == 0)
2700 {
2701 m->NextScheduledEvent = GetNextScheduledEvent(m);
2702 if (m->timenow == 0) LogMsg("%s: mDNS_Unlock: ERROR! m->timenow aready zero", functionname);
2703 m->timenow = 0;
2704 }
2705
2706 // MUST release the platform lock LAST!
2707 mDNSPlatformUnlock(m);
2708 }
2709
2710 // ***************************************************************************
2711 #if COMPILER_LIKES_PRAGMA_MARK
2712 #pragma mark -
2713 #pragma mark - Specialized mDNS version of vsnprintf
2714 #endif
2715
2716 static const struct mDNSprintf_format
2717 {
2718 unsigned leftJustify : 1;
2719 unsigned forceSign : 1;
2720 unsigned zeroPad : 1;
2721 unsigned havePrecision : 1;
2722 unsigned hSize : 1;
2723 unsigned lSize : 1;
2724 char altForm;
2725 char sign; // +, - or space
2726 unsigned int fieldWidth;
2727 unsigned int precision;
2728 } mDNSprintf_format_default = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
2729
2730 mDNSexport mDNSu32 mDNS_vsnprintf(char *sbuffer, mDNSu32 buflen, const char *fmt, va_list arg)
2731 {
2732 mDNSu32 nwritten = 0;
2733 int c;
2734 if (buflen == 0) return(0);
2735 buflen--; // Pre-reserve one space in the buffer for the terminating null
2736 if (buflen == 0) goto exit;
2737
2738 for (c = *fmt; c != 0; c = *++fmt)
2739 {
2740 if (c != '%')
2741 {
2742 *sbuffer++ = (char)c;
2743 if (++nwritten >= buflen) goto exit;
2744 }
2745 else
2746 {
2747 unsigned int i=0, j;
2748 // The mDNS Vsprintf Argument Conversion Buffer is used as a temporary holding area for
2749 // generating decimal numbers, hexdecimal numbers, IP addresses, domain name strings, etc.
2750 // The size needs to be enough for a 256-byte domain name plus some error text.
2751 #define mDNS_VACB_Size 300
2752 char mDNS_VACB[mDNS_VACB_Size];
2753 #define mDNS_VACB_Lim (&mDNS_VACB[mDNS_VACB_Size])
2754 #define mDNS_VACB_Remain(s) ((mDNSu32)(mDNS_VACB_Lim - s))
2755 char *s = mDNS_VACB_Lim, *digits;
2756 struct mDNSprintf_format F = mDNSprintf_format_default;
2757
2758 while (1) // decode flags
2759 {
2760 c = *++fmt;
2761 if (c == '-') F.leftJustify = 1;
2762 else if (c == '+') F.forceSign = 1;
2763 else if (c == ' ') F.sign = ' ';
2764 else if (c == '#') F.altForm++;
2765 else if (c == '0') F.zeroPad = 1;
2766 else break;
2767 }
2768
2769 if (c == '*') // decode field width
2770 {
2771 int f = va_arg(arg, int);
2772 if (f < 0) { f = -f; F.leftJustify = 1; }
2773 F.fieldWidth = (unsigned int)f;
2774 c = *++fmt;
2775 }
2776 else
2777 {
2778 for (; c >= '0' && c <= '9'; c = *++fmt)
2779 F.fieldWidth = (10 * F.fieldWidth) + (c - '0');
2780 }
2781
2782 if (c == '.') // decode precision
2783 {
2784 if ((c = *++fmt) == '*')
2785 { F.precision = va_arg(arg, unsigned int); c = *++fmt; }
2786 else for (; c >= '0' && c <= '9'; c = *++fmt)
2787 F.precision = (10 * F.precision) + (c - '0');
2788 F.havePrecision = 1;
2789 }
2790
2791 if (F.leftJustify) F.zeroPad = 0;
2792
2793 conv:
2794 switch (c) // perform appropriate conversion
2795 {
2796 unsigned long n;
2797 case 'h' : F.hSize = 1; c = *++fmt; goto conv;
2798 case 'l' : // fall through
2799 case 'L' : F.lSize = 1; c = *++fmt; goto conv;
2800 case 'd' :
2801 case 'i' : if (F.lSize) n = (unsigned long)va_arg(arg, long);
2802 else n = (unsigned long)va_arg(arg, int);
2803 if (F.hSize) n = (short) n;
2804 if ((long) n < 0) { n = (unsigned long)-(long)n; F.sign = '-'; }
2805 else if (F.forceSign) F.sign = '+';
2806 goto decimal;
2807 case 'u' : if (F.lSize) n = va_arg(arg, unsigned long);
2808 else n = va_arg(arg, unsigned int);
2809 if (F.hSize) n = (unsigned short) n;
2810 F.sign = 0;
2811 goto decimal;
2812 decimal: if (!F.havePrecision)
2813 {
2814 if (F.zeroPad)
2815 {
2816 F.precision = F.fieldWidth;
2817 if (F.sign) --F.precision;
2818 }
2819 if (F.precision < 1) F.precision = 1;
2820 }
2821 if (F.precision > mDNS_VACB_Size - 1)
2822 F.precision = mDNS_VACB_Size - 1;
2823 for (i = 0; n; n /= 10, i++) *--s = (char)(n % 10 + '0');
2824 for (; i < F.precision; i++) *--s = '0';
2825 if (F.sign) { *--s = F.sign; i++; }
2826 break;
2827
2828 case 'o' : if (F.lSize) n = va_arg(arg, unsigned long);
2829 else n = va_arg(arg, unsigned int);
2830 if (F.hSize) n = (unsigned short) n;
2831 if (!F.havePrecision)
2832 {
2833 if (F.zeroPad) F.precision = F.fieldWidth;
2834 if (F.precision < 1) F.precision = 1;
2835 }
2836 if (F.precision > mDNS_VACB_Size - 1)
2837 F.precision = mDNS_VACB_Size - 1;
2838 for (i = 0; n; n /= 8, i++) *--s = (char)(n % 8 + '0');
2839 if (F.altForm && i && *s != '0') { *--s = '0'; i++; }
2840 for (; i < F.precision; i++) *--s = '0';
2841 break;
2842
2843 case 'a' : {
2844 unsigned char *a = va_arg(arg, unsigned char *);
2845 if (!a) { static char emsg[] = "<<NULL>>"; s = emsg; i = sizeof(emsg)-1; }
2846 else
2847 {
2848 s = mDNS_VACB; // Adjust s to point to the start of the buffer, not the end
2849 if (F.altForm)
2850 {
2851 mDNSAddr *ip = (mDNSAddr*)a;
2852 switch (ip->type)
2853 {
2854 case mDNSAddrType_IPv4: F.precision = 4; a = (unsigned char *)&ip->ip.v4; break;
2855 case mDNSAddrType_IPv6: F.precision = 16; a = (unsigned char *)&ip->ip.v6; break;
2856 default: F.precision = 0; break;
2857 }
2858 }
2859 if (F.altForm && !F.precision)
2860 i = mDNS_snprintf(mDNS_VACB, sizeof(mDNS_VACB), "«ZERO ADDRESS»");
2861 else switch (F.precision)
2862 {
2863 case 4: i = mDNS_snprintf(mDNS_VACB, sizeof(mDNS_VACB), "%d.%d.%d.%d",
2864 a[0], a[1], a[2], a[3]); break;
2865 case 6: i = mDNS_snprintf(mDNS_VACB, sizeof(mDNS_VACB), "%02X:%02X:%02X:%02X:%02X:%02X",
2866 a[0], a[1], a[2], a[3], a[4], a[5]); break;
2867 case 16: i = mDNS_snprintf(mDNS_VACB, sizeof(mDNS_VACB),
2868 "%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X",
2869 a[0x0], a[0x1], a[0x2], a[0x3], a[0x4], a[0x5], a[0x6], a[0x7],
2870 a[0x8], a[0x9], a[0xA], a[0xB], a[0xC], a[0xD], a[0xE], a[0xF]); break;
2871 default: i = mDNS_snprintf(mDNS_VACB, sizeof(mDNS_VACB), "%s", "<< ERROR: Must specify"
2872 " address size (i.e. %.4a=IPv4, %.6a=Ethernet, %.16a=IPv6) >>"); break;
2873 }
2874 }
2875 }
2876 break;
2877
2878 case 'p' : F.havePrecision = F.lSize = 1;
2879 F.precision = sizeof(void*) * 2; // 8 characters on 32-bit; 16 characters on 64-bit
2880 case 'X' : digits = "0123456789ABCDEF";
2881 goto hexadecimal;
2882 case 'x' : digits = "0123456789abcdef";
2883 hexadecimal:if (F.lSize) n = va_arg(arg, unsigned long);
2884 else n = va_arg(arg, unsigned int);
2885 if (F.hSize) n = (unsigned short) n;
2886 if (!F.havePrecision)
2887 {
2888 if (F.zeroPad)
2889 {
2890 F.precision = F.fieldWidth;
2891 if (F.altForm) F.precision -= 2;
2892 }
2893 if (F.precision < 1) F.precision = 1;
2894 }
2895 if (F.precision > mDNS_VACB_Size - 1)
2896 F.precision = mDNS_VACB_Size - 1;
2897 for (i = 0; n; n /= 16, i++) *--s = digits[n % 16];
2898 for (; i < F.precision; i++) *--s = '0';
2899 if (F.altForm) { *--s = (char)c; *--s = '0'; i += 2; }
2900 break;
2901
2902 case 'c' : *--s = (char)va_arg(arg, int); i = 1; break;
2903
2904 case 's' : s = va_arg(arg, char *);
2905 if (!s) { static char emsg[] = "<<NULL>>"; s = emsg; i = sizeof(emsg)-1; }
2906 else switch (F.altForm)
2907 {
2908 case 0: i=0;
2909 if (!F.havePrecision) // C string
2910 while (s[i]) i++;
2911 else
2912 {
2913 while ((i < F.precision) && s[i]) i++;
2914 // Make sure we don't truncate in the middle of a UTF-8 character
2915 // If last character we got was any kind of UTF-8 multi-byte character,
2916 // then see if we have to back up.
2917 // This is not as easy as the similar checks below, because
2918 // here we can't assume it's safe to examine the *next* byte, so we
2919 // have to confine ourselves to working only backwards in the string.
2920 j = i; // Record where we got to
2921 // Now, back up until we find first non-continuation-char
2922 while (i>0 && (s[i-1] & 0xC0) == 0x80) i--;
2923 // Now s[i-1] is the first non-continuation-char
2924 // and (j-i) is the number of continuation-chars we found
2925 if (i>0 && (s[i-1] & 0xC0) == 0xC0) // If we found a start-char
2926 {
2927 i--; // Tentatively eliminate this start-char as well
2928 // Now (j-i) is the number of characters we're considering eliminating.
2929 // To be legal UTF-8, the start-char must contain (j-i) one-bits,
2930 // followed by a zero bit. If we shift it right by (7-(j-i)) bits
2931 // (with sign extension) then the result has to be 0xFE.
2932 // If this is right, then we reinstate the tentatively eliminated bytes.
2933 if (((j-i) < 7) && (((s[i] >> (7-(j-i))) & 0xFF) == 0xFE)) i = j;
2934 }
2935 }
2936 break;
2937 case 1: i = (unsigned char) *s++; break; // Pascal string
2938 case 2: { // DNS label-sequence name
2939 unsigned char *a = (unsigned char *)s;
2940 s = mDNS_VACB; // Adjust s to point to the start of the buffer, not the end
2941 if (*a == 0) *s++ = '.'; // Special case for root DNS name
2942 while (*a)
2943 {
2944 char buf[63*4+1];
2945 if (*a > 63)
2946 { s += mDNS_snprintf(s, mDNS_VACB_Remain(s), "<<INVALID LABEL LENGTH %u>>", *a); break; }
2947 if (s + *a >= &mDNS_VACB[254])
2948 { s += mDNS_snprintf(s, mDNS_VACB_Remain(s), "<<NAME TOO LONG>>"); break; }
2949 // Need to use ConvertDomainLabelToCString to do proper escaping here,
2950 // so it's clear what's a literal dot and what's a label separator
2951 ConvertDomainLabelToCString((domainlabel*)a, buf);
2952 s += mDNS_snprintf(s, mDNS_VACB_Remain(s), "%s.", buf);
2953 a += 1 + *a;
2954 }
2955 i = (mDNSu32)(s - mDNS_VACB);
2956 s = mDNS_VACB; // Reset s back to the start of the buffer
2957 break;
2958 }
2959 }
2960 // Make sure we don't truncate in the middle of a UTF-8 character (see similar comment below)
2961 if (F.havePrecision && i > F.precision)
2962 { i = F.precision; while (i>0 && (s[i] & 0xC0) == 0x80) i--; }
2963 break;
2964
2965 case 'n' : s = va_arg(arg, char *);
2966 if (F.hSize) * (short *) s = (short)nwritten;
2967 else if (F.lSize) * (long *) s = (long)nwritten;
2968 else * (int *) s = (int)nwritten;
2969 continue;
2970
2971 default: s = mDNS_VACB;
2972 i = mDNS_snprintf(mDNS_VACB, sizeof(mDNS_VACB), "<<UNKNOWN FORMAT CONVERSION CODE %%%c>>", c);
2973
2974 case '%' : *sbuffer++ = (char)c;
2975 if (++nwritten >= buflen) goto exit;
2976 break;
2977 }
2978
2979 if (i < F.fieldWidth && !F.leftJustify) // Pad on the left
2980 do {
2981 *sbuffer++ = ' ';
2982 if (++nwritten >= buflen) goto exit;
2983 } while (i < --F.fieldWidth);
2984
2985 // Make sure we don't truncate in the middle of a UTF-8 character.
2986 // Note: s[i] is the first eliminated character; i.e. the next character *after* the last character of the
2987 // allowed output. If s[i] is a UTF-8 continuation character, then we've cut a unicode character in half,
2988 // so back up 'i' until s[i] is no longer a UTF-8 continuation character. (if the input was proprly
2989 // formed, s[i] will now be the UTF-8 start character of the multi-byte character we just eliminated).
2990 if (i > buflen - nwritten)
2991 { i = buflen - nwritten; while (i>0 && (s[i] & 0xC0) == 0x80) i--; }
2992 for (j=0; j<i; j++) *sbuffer++ = *s++; // Write the converted result
2993 nwritten += i;
2994 if (nwritten >= buflen) goto exit;
2995
2996 for (; i < F.fieldWidth; i++) // Pad on the right
2997 {
2998 *sbuffer++ = ' ';
2999 if (++nwritten >= buflen) goto exit;
3000 }
3001 }
3002 }
3003 exit:
3004 *sbuffer++ = 0;
3005 return(nwritten);
3006 }
3007
3008 mDNSexport mDNSu32 mDNS_snprintf(char *sbuffer, mDNSu32 buflen, const char *fmt, ...)
3009 {
3010 mDNSu32 length;
3011
3012 va_list ptr;
3013 va_start(ptr,fmt);
3014 length = mDNS_vsnprintf(sbuffer, buflen, fmt, ptr);
3015 va_end(ptr);
3016
3017 return(length);
3018 }