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
38mDNSexport const mDNSInterfaceID mDNSInterface_Any       = 0;
39mDNSexport const mDNSInterfaceID mDNSInterfaceMark       = (mDNSInterfaceID)-1;
40mDNSexport const mDNSInterfaceID mDNSInterface_LocalOnly = (mDNSInterfaceID)-2;
41mDNSexport const mDNSInterfaceID mDNSInterface_Unicast   = (mDNSInterfaceID)-3;
42mDNSexport 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
65mDNSexport const mDNSIPPort DiscardPort            = { { DiscardPortAsNumber            >> 8, DiscardPortAsNumber            & 0xFF } };
66mDNSexport const mDNSIPPort SSHPort                = { { SSHPortAsNumber                >> 8, SSHPortAsNumber                & 0xFF } };
67mDNSexport const mDNSIPPort UnicastDNSPort         = { { UnicastDNSPortAsNumber         >> 8, UnicastDNSPortAsNumber         & 0xFF } };
68mDNSexport const mDNSIPPort SSDPPort               = { { SSDPPortAsNumber               >> 8, SSDPPortAsNumber               & 0xFF } };
69mDNSexport const mDNSIPPort IPSECPort              = { { IPSECPortAsNumber              >> 8, IPSECPortAsNumber              & 0xFF } };
70mDNSexport const mDNSIPPort NSIPCPort              = { { NSIPCPortAsNumber              >> 8, NSIPCPortAsNumber              & 0xFF } };
71mDNSexport const mDNSIPPort NATPMPAnnouncementPort = { { NATPMPAnnouncementPortAsNumber >> 8, NATPMPAnnouncementPortAsNumber & 0xFF } };
72mDNSexport const mDNSIPPort NATPMPPort             = { { NATPMPPortAsNumber             >> 8, NATPMPPortAsNumber             & 0xFF } };
73mDNSexport const mDNSIPPort DNSEXTPort             = { { DNSEXTPortAsNumber             >> 8, DNSEXTPortAsNumber             & 0xFF } };
74mDNSexport const mDNSIPPort MulticastDNSPort       = { { MulticastDNSPortAsNumber       >> 8, MulticastDNSPortAsNumber       & 0xFF } };
75mDNSexport const mDNSIPPort LoopbackIPCPort        = { { LoopbackIPCPortAsNumber        >> 8, LoopbackIPCPortAsNumber        & 0xFF } };
76mDNSexport const mDNSIPPort PrivateDNSPort         = { { PrivateDNSPortAsNumber         >> 8, PrivateDNSPortAsNumber         & 0xFF } };
77
78mDNSexport const OwnerOptData    zeroOwner         = { 0, 0, { { 0 } }, { { 0 } }, { { 0 } } };
79
80mDNSexport const mDNSIPPort      zeroIPPort        = { { 0 } };
81mDNSexport const mDNSv4Addr      zerov4Addr        = { { 0 } };
82mDNSexport const mDNSv6Addr      zerov6Addr        = { { 0 } };
83mDNSexport const mDNSEthAddr     zeroEthAddr       = { { 0 } };
84mDNSexport const mDNSv4Addr      onesIPv4Addr      = { { 255, 255, 255, 255 } };
85mDNSexport const mDNSv6Addr      onesIPv6Addr      = { { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 } };
86mDNSexport const mDNSEthAddr     onesEthAddr       = { { 255, 255, 255, 255, 255, 255 } };
87mDNSexport const mDNSAddr        zeroAddr          = { mDNSAddrType_None, {{{ 0 }}} };
88
89mDNSexport const mDNSv4Addr  AllDNSAdminGroup   = { { 239, 255, 255, 251 } };
90mDNSexport const mDNSv4Addr  AllHosts_v4        = { { 224,   0,   0,   1 } }; // For NAT-PMP Annoucements
91mDNSexport const mDNSv6Addr  AllHosts_v6        = { { 0xFF,0x02,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x01 } };
92mDNSexport 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
93mDNSexport const mDNSEthAddr AllHosts_v6_Eth    = { { 0x33, 0x33, 0x00, 0x00, 0x00, 0x01 } };
94mDNSexport const mDNSAddr    AllDNSLinkGroup_v4 = { mDNSAddrType_IPv4, { { { 224,   0,   0, 251 } } } };
95//mDNSexport const mDNSAddr  AllDNSLinkGroup_v4 = { mDNSAddrType_IPv4, { { { 224,   0,   0, 252 } } } }; // LLMNR
96mDNSexport 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
99mDNSexport const mDNSOpaque16 zeroID          = { { 0, 0 } };
100mDNSexport const mDNSOpaque16 onesID          = { { 255, 255 } };
101mDNSexport const mDNSOpaque16 QueryFlags      = { { kDNSFlag0_QR_Query    | kDNSFlag0_OP_StdQuery,                0 } };
102mDNSexport const mDNSOpaque16 uQueryFlags     = { { kDNSFlag0_QR_Query    | kDNSFlag0_OP_StdQuery | kDNSFlag0_RD, 0 } };
103mDNSexport const mDNSOpaque16 ResponseFlags   = { { kDNSFlag0_QR_Response | kDNSFlag0_OP_StdQuery | kDNSFlag0_AA, 0 } };
104mDNSexport const mDNSOpaque16 UpdateReqFlags  = { { kDNSFlag0_QR_Query    | kDNSFlag0_OP_Update,                  0 } };
105mDNSexport const mDNSOpaque16 UpdateRespFlags = { { kDNSFlag0_QR_Response | kDNSFlag0_OP_Update,                  0 } };
106
107mDNSexport 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
116mDNSexport 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
123mDNSexport NetworkInterfaceInfo *GetFirstActiveInterface(NetworkInterfaceInfo *intf)
124	{
125	while (intf && !intf->InterfaceActive) intf = intf->next;
126	return(intf);
127	}
128
129mDNSexport mDNSInterfaceID GetNextActiveInterfaceID(const NetworkInterfaceInfo *intf)
130	{
131	const NetworkInterfaceInfo *next = GetFirstActiveInterface(intf->next);
132	if (next) return(next->InterfaceID); else return(mDNSNULL);
133	}
134
135mDNSexport 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
145mDNSexport 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.
174mDNSexport 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
267mDNSlocal mDNSu32 mDNSRandomFromSeed(mDNSu32 seed)
268	{
269	return seed * 21 + 1;
270	}
271
272mDNSlocal mDNSu32 mDNSMixRandomSeed(mDNSu32 seed, mDNSu8 iteration)
273	{
274	return iteration ? mDNSMixRandomSeed(mDNSRandomFromSeed(seed), --iteration) : seed;
275	}
276
277mDNSlocal 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
290mDNSexport 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
303mDNSexport 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
317mDNSexport 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
333mDNSexport 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
353mDNSexport 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
371mDNSexport 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
378mDNSexport 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
406mDNSexport 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)
422mDNSexport 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).
442mDNSexport 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.)
458mDNSexport 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.
468mDNSexport 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.
481mDNSexport 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.
503mDNSexport 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.
545mDNSexport 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
561mDNSexport 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.
583mDNSexport 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.
599mDNSexport 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
605mDNSexport 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)
633mDNSexport 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
658mDNSexport 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
685mDNSexport 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
794fail:
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.
804mDNSexport 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
862mDNSexport 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.
888mDNSexport 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.
914mDNSexport 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).
940mDNSexport 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
965mDNSexport 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
991mDNSexport void mDNS_SetupResourceRecord(AuthRecord *rr, RData *RDataStorage, mDNSInterfaceID InterfaceID,
992	mDNSu16 rrtype, mDNSu32 ttl, mDNSu8 RecordType, AuthRecType artype, mDNSRecordCallback Callback, void *Context)
993	{
994	//
995	// LocalOnly auth record can be created with LocalOnly InterfaceID or a valid InterfaceID.
996	// Most of the applications normally create with LocalOnly InterfaceID and we store them as
997	// such, so that we can deliver the response to questions that specify LocalOnly InterfaceID.
998	// LocalOnly resource records can also be created with valid InterfaceID which happens today
999	// when we create LocalOnly records for /etc/hosts.
1000
1001	if (InterfaceID == mDNSInterface_LocalOnly && artype != AuthRecordLocalOnly)
1002		{
1003		LogMsg("mDNS_SetupResourceRecord: ERROR!! Mismatch LocalOnly record InterfaceID %p called with artype %d", InterfaceID, artype);
1004		return;
1005		}
1006	else if (InterfaceID == mDNSInterface_P2P && artype != AuthRecordP2P)
1007		{
1008		LogMsg("mDNS_SetupResourceRecord: ERROR!! Mismatch P2P record InterfaceID %p called with artype %d", InterfaceID, artype);
1009		return;
1010		}
1011	else if (!InterfaceID && (artype == AuthRecordP2P || artype == AuthRecordLocalOnly))
1012		{
1013		LogMsg("mDNS_SetupResourceRecord: ERROR!! Mismatch InterfaceAny record InterfaceID %p called with artype %d", InterfaceID, artype);
1014		return;
1015		}
1016
1017	// Don't try to store a TTL bigger than we can represent in platform time units
1018	if (ttl > 0x7FFFFFFFUL / mDNSPlatformOneSecond)
1019		ttl = 0x7FFFFFFFUL / mDNSPlatformOneSecond;
1020	else if (ttl == 0)		// And Zero TTL is illegal
1021		ttl = DefaultTTLforRRType(rrtype);
1022
1023	// Field Group 1: The actual information pertaining to this resource record
1024	rr->resrec.RecordType        = RecordType;
1025	rr->resrec.InterfaceID       = InterfaceID;
1026	rr->resrec.name              = &rr->namestorage;
1027	rr->resrec.rrtype            = rrtype;
1028	rr->resrec.rrclass           = kDNSClass_IN;
1029	rr->resrec.rroriginalttl     = ttl;
1030	rr->resrec.rDNSServer		 = mDNSNULL;
1031//	rr->resrec.rdlength          = MUST set by client and/or in mDNS_Register_internal
1032//	rr->resrec.rdestimate        = set in mDNS_Register_internal
1033//	rr->resrec.rdata             = MUST be set by client
1034
1035	if (RDataStorage)
1036		rr->resrec.rdata = RDataStorage;
1037	else
1038		{
1039		rr->resrec.rdata = &rr->rdatastorage;
1040		rr->resrec.rdata->MaxRDLength = sizeof(RDataBody);
1041		}
1042
1043	// Field Group 2: Persistent metadata for Authoritative Records
1044	rr->Additional1       = mDNSNULL;
1045	rr->Additional2       = mDNSNULL;
1046	rr->DependentOn       = mDNSNULL;
1047	rr->RRSet             = mDNSNULL;
1048	rr->RecordCallback    = Callback;
1049	rr->RecordContext     = Context;
1050
1051	rr->AutoTarget        = Target_Manual;
1052	rr->AllowRemoteQuery  = mDNSfalse;
1053	rr->ForceMCast        = mDNSfalse;
1054
1055	rr->WakeUp            = zeroOwner;
1056	rr->AddressProxy      = zeroAddr;
1057	rr->TimeRcvd          = 0;
1058	rr->TimeExpire        = 0;
1059	rr->ARType            = artype;
1060
1061	// Field Group 3: Transient state for Authoritative Records (set in mDNS_Register_internal)
1062	// Field Group 4: Transient uDNS state for Authoritative Records (set in mDNS_Register_internal)
1063
1064	// For now, until the uDNS code is fully integrated, it's helpful to zero the uDNS state fields here too, just in case
1065	// (e.g. uDNS_RegisterService short-circuits the usual mDNS_Register_internal record registration calls, so a bunch
1066	// of fields don't get set up properly. In particular, if we don't zero rr->QueuedRData then the uDNS code crashes.)
1067	rr->state             = regState_Zero;
1068	rr->uselease          = 0;
1069	rr->expire            = 0;
1070	rr->Private           = 0;
1071	rr->updateid          = zeroID;
1072	rr->zone              = rr->resrec.name;
1073	rr->nta               = mDNSNULL;
1074	rr->tcp               = mDNSNULL;
1075	rr->OrigRData         = 0;
1076	rr->OrigRDLen         = 0;
1077	rr->InFlightRData     = 0;
1078	rr->InFlightRDLen     = 0;
1079	rr->QueuedRData       = 0;
1080	rr->QueuedRDLen       = 0;
1081	mDNSPlatformMemZero(&rr->NATinfo, sizeof(rr->NATinfo));
1082	rr->SRVChanged = mDNSfalse;
1083	rr->mState = mergeState_Zero;
1084
1085	rr->namestorage.c[0]  = 0;		// MUST be set by client before calling mDNS_Register()
1086	}
1087
1088mDNSexport void mDNS_SetupQuestion(DNSQuestion *const q, const mDNSInterfaceID InterfaceID, const domainname *const name,
1089               const mDNSu16 qtype, mDNSQuestionCallback *const callback, void *const context)
1090	{
1091	q->InterfaceID         = InterfaceID;
1092	q->Target              = zeroAddr;
1093	AssignDomainName(&q->qname, name);
1094	q->qtype               = qtype;
1095	q->qclass              = kDNSClass_IN;
1096	q->LongLived           = (qtype == kDNSType_PTR);
1097	q->ExpectUnique        = (qtype != kDNSType_PTR);
1098	q->ForceMCast          = mDNSfalse;
1099	q->ReturnIntermed      = mDNSfalse;
1100	q->SuppressUnusable    = mDNSfalse;
1101	q->SearchListIndex     = 0;
1102	q->AppendSearchDomains = 0;
1103	q->RetryWithSearchDomains = mDNSfalse;
1104	q->TimeoutQuestion     = 0;
1105	q->WakeOnResolve       = 0;
1106	q->qnameOrig           = mDNSNULL;
1107	q->QuestionCallback    = callback;
1108	q->QuestionContext     = context;
1109	}
1110
1111mDNSexport mDNSu32 RDataHashValue(const ResourceRecord *const rr)
1112	{
1113	int len = rr->rdlength;
1114	const RDataBody2 *const rdb = (RDataBody2 *)rr->rdata->u.data;
1115	switch(rr->rrtype)
1116		{
1117		case kDNSType_NS:
1118		case kDNSType_CNAME:
1119		case kDNSType_PTR:
1120		case kDNSType_DNAME: return DomainNameHashValue(&rdb->name);
1121
1122		case kDNSType_SOA:   return rdb->soa.serial  +
1123									rdb->soa.refresh +
1124									rdb->soa.retry   +
1125									rdb->soa.expire  +
1126									rdb->soa.min     +
1127									DomainNameHashValue(&rdb->soa.mname) +
1128									DomainNameHashValue(&rdb->soa.rname);
1129
1130		case kDNSType_MX:
1131		case kDNSType_AFSDB:
1132		case kDNSType_RT:
1133		case kDNSType_KX:	 return DomainNameHashValue(&rdb->mx.exchange);
1134
1135		case kDNSType_RP:	 return DomainNameHashValue(&rdb->rp.mbox)   + DomainNameHashValue(&rdb->rp.txt);
1136
1137		case kDNSType_PX:	 return DomainNameHashValue(&rdb->px.map822) + DomainNameHashValue(&rdb->px.mapx400);
1138
1139		case kDNSType_SRV:	 return DomainNameHashValue(&rdb->srv.target);
1140
1141		case kDNSType_OPT:	 return 0;	// OPT is a pseudo-RR container structure; makes no sense to compare
1142
1143		case kDNSType_NSEC:	 len = sizeof(rdataNSEC);	// Use in-memory length of 32, and fall through default checksum computation below
1144
1145		default:
1146			{
1147			mDNSu32 sum = 0;
1148			int i;
1149			for (i=0; i+1 < len; i+=2)
1150				{
1151				sum += (((mDNSu32)(rdb->data[i])) << 8) | rdb->data[i+1];
1152				sum = (sum<<3) | (sum>>29);
1153				}
1154			if (i < len)
1155				{
1156				sum += ((mDNSu32)(rdb->data[i])) << 8;
1157				}
1158			return(sum);
1159			}
1160		}
1161	}
1162
1163// r1 has to be a full ResourceRecord including rrtype and rdlength
1164// r2 is just a bare RDataBody, which MUST be the same rrtype and rdlength as r1
1165mDNSexport mDNSBool SameRDataBody(const ResourceRecord *const r1, const RDataBody *const r2, DomainNameComparisonFn *samename)
1166	{
1167	const RDataBody2 *const b1 = (RDataBody2 *)r1->rdata->u.data;
1168	const RDataBody2 *const b2 = (RDataBody2 *)r2;
1169	switch(r1->rrtype)
1170		{
1171		case kDNSType_NS:
1172		case kDNSType_CNAME:
1173		case kDNSType_PTR:
1174		case kDNSType_DNAME:return(SameDomainName(&b1->name, &b2->name));
1175
1176		case kDNSType_SOA:	return(mDNSBool)(  	b1->soa.serial   == b2->soa.serial             &&
1177												b1->soa.refresh  == b2->soa.refresh            &&
1178												b1->soa.retry    == b2->soa.retry              &&
1179												b1->soa.expire   == b2->soa.expire             &&
1180												b1->soa.min      == b2->soa.min                &&
1181												samename(&b1->soa.mname, &b2->soa.mname) &&
1182												samename(&b1->soa.rname, &b2->soa.rname));
1183
1184		case kDNSType_MX:
1185		case kDNSType_AFSDB:
1186		case kDNSType_RT:
1187		case kDNSType_KX:	return(mDNSBool)(  	b1->mx.preference == b2->mx.preference &&
1188												samename(&b1->mx.exchange, &b2->mx.exchange));
1189
1190		case kDNSType_RP:	return(mDNSBool)(  	samename(&b1->rp.mbox, &b2->rp.mbox) &&
1191												samename(&b1->rp.txt,  &b2->rp.txt));
1192
1193		case kDNSType_PX:	return(mDNSBool)(  	b1->px.preference == b2->px.preference          &&
1194												samename(&b1->px.map822,  &b2->px.map822) &&
1195												samename(&b1->px.mapx400, &b2->px.mapx400));
1196
1197		case kDNSType_SRV:	return(mDNSBool)(  	b1->srv.priority == b2->srv.priority       &&
1198												b1->srv.weight   == b2->srv.weight         &&
1199												mDNSSameIPPort(b1->srv.port, b2->srv.port) &&
1200												samename(&b1->srv.target, &b2->srv.target));
1201
1202		case kDNSType_OPT:	return mDNSfalse;	// OPT is a pseudo-RR container structure; makes no sense to compare
1203
1204		case kDNSType_NSEC: return(mDNSPlatformMemSame(b1->data, b2->data, sizeof(rdataNSEC)));
1205
1206		default:			return(mDNSPlatformMemSame(b1->data, b2->data, r1->rdlength));
1207		}
1208	}
1209
1210// ResourceRecordAnswersQuestion returns mDNStrue if the given resource record is a valid answer to the given question.
1211// SameNameRecordAnswersQuestion is the same, except it skips the expensive SameDomainName() call.
1212// SameDomainName() is generally cheap when the names don't match, but expensive when they do match,
1213// because it has to check all the way to the end of the names to be sure.
1214// In cases where we know in advance that the names match it's especially advantageous to skip the
1215// SameDomainName() call because that's precisely the time when it's most expensive and least useful.
1216
1217mDNSexport mDNSBool SameNameRecordAnswersQuestion(const ResourceRecord *const rr, const DNSQuestion *const q)
1218	{
1219	// LocalOnly/P2P questions can be answered with AuthRecordAny in this function. LocalOnly/P2P records
1220	// are handled in LocalOnlyRecordAnswersQuestion
1221	if ((rr->InterfaceID == mDNSInterface_LocalOnly) || (rr->InterfaceID == mDNSInterface_P2P))
1222		{
1223		LogMsg("SameNameRecordAnswersQuestion: ERROR!! called with LocalOnly ResourceRecord %p, Question %p", rr->InterfaceID, q->InterfaceID);
1224		return mDNSfalse;
1225		}
1226	if (rr->InterfaceID &&
1227		q ->InterfaceID && q->InterfaceID != mDNSInterface_LocalOnly &&
1228		rr->InterfaceID != q->InterfaceID) return(mDNSfalse);
1229
1230	// Resource record received via unicast, the DNSServer entries should match ?
1231	if (!rr->InterfaceID && rr->rDNSServer != q->qDNSServer) return(mDNSfalse);
1232
1233	// If ResourceRecord received via multicast, but question was unicast, then shouldn't use record to answer this question
1234	if (rr->InterfaceID && !mDNSOpaque16IsZero(q->TargetQID)) return(mDNSfalse);
1235
1236	// RR type CNAME matches any query type. QTYPE ANY matches any RR type. QCLASS ANY matches any RR class.
1237	if (!RRTypeAnswersQuestionType(rr,q->qtype)) return(mDNSfalse);
1238	if (rr->rrclass != q->qclass && q->qclass != kDNSQClass_ANY) return(mDNSfalse);
1239
1240	return(mDNStrue);
1241	}
1242
1243mDNSexport mDNSBool ResourceRecordAnswersQuestion(const ResourceRecord *const rr, const DNSQuestion *const q)
1244	{
1245	// LocalOnly/P2P questions can be answered with AuthRecordAny in this function. LocalOnly/P2P records
1246	// are handled in LocalOnlyRecordAnswersQuestion
1247	if ((rr->InterfaceID == mDNSInterface_LocalOnly) || (rr->InterfaceID == mDNSInterface_P2P))
1248		{
1249		LogMsg("ResourceRecordAnswersQuestion: ERROR!! called with LocalOnly/P2P ResourceRecord %p, Question %p", rr->InterfaceID, q->InterfaceID);
1250		return mDNSfalse;
1251		}
1252
1253	if (rr->InterfaceID &&
1254		q ->InterfaceID && q->InterfaceID != mDNSInterface_LocalOnly &&
1255		rr->InterfaceID != q->InterfaceID) return(mDNSfalse);
1256
1257	// Resource record received via unicast, the DNSServer entries should match ?
1258	if (!rr->InterfaceID && rr->rDNSServer != q->qDNSServer) return(mDNSfalse);
1259
1260	// If ResourceRecord received via multicast, but question was unicast, then shouldn't use record to answer this question.
1261	if (rr->InterfaceID && !mDNSOpaque16IsZero(q->TargetQID)) return(mDNSfalse);
1262
1263	// RR type CNAME matches any query type. QTYPE ANY matches any RR type. QCLASS ANY matches any RR class.
1264	if (!RRTypeAnswersQuestionType(rr,q->qtype)) return(mDNSfalse);
1265	if (rr->rrclass != q->qclass && q->qclass != kDNSQClass_ANY) return(mDNSfalse);
1266
1267	return(rr->namehash == q->qnamehash && SameDomainName(rr->name, &q->qname));
1268	}
1269
1270// We have a separate function to handle LocalOnly AuthRecords because they can be created with
1271// a valid InterfaceID (e.g., scoped /etc/hosts) and can be used to answer unicast questions unlike
1272// multicast resource records (which has a valid InterfaceID) which can't be used to answer
1273// unicast questions. ResourceRecordAnswersQuestion/SameNameRecordAnswersQuestion can't tell whether
1274// a resource record is multicast or LocalOnly by just looking at the ResourceRecord because
1275// LocalOnly records are truly identified by ARType in the AuthRecord.  As P2P and LocalOnly record
1276// are kept in the same hash table, we use the same function to make it easy for the callers when
1277// they walk the hash table to answer LocalOnly/P2P questions
1278//
1279mDNSexport mDNSBool LocalOnlyRecordAnswersQuestion(AuthRecord *const ar, const DNSQuestion *const q)
1280	{
1281	ResourceRecord *rr = &ar->resrec;
1282
1283	// mDNSInterface_Any questions can be answered with LocalOnly/P2P records in this function. AuthRecord_Any
1284	// records are handled in ResourceRecordAnswersQuestion/SameNameRecordAnswersQuestion
1285	if (RRAny(ar))
1286		{
1287		LogMsg("LocalOnlyRecordAnswersQuestion: ERROR!! called with regular AuthRecordAny %##s", rr->name->c);
1288		return mDNSfalse;
1289		}
1290
1291	// Questions with mDNSInterface_LocalOnly InterfaceID should be answered with all resource records that are
1292	// *local* to the machine. These include resource records that have InterfaceID set to mDNSInterface_LocalOnly,
1293	// mDNSInterface_Any and any other real InterfaceID. Hence, LocalOnly questions should not be checked against
1294	// the InterfaceID in the resource record.
1295	//
1296	// mDNSInterface_Unicast does not indicate any scope and hence treat them like mDNSInterface_Any.
1297
1298	if (rr->InterfaceID &&
1299		q->InterfaceID && q->InterfaceID != mDNSInterface_LocalOnly && q->InterfaceID != mDNSInterface_Unicast &&
1300		rr->InterfaceID != q->InterfaceID) return(mDNSfalse);
1301
1302	// Entries in /etc/hosts are added as LocalOnly resource records. The LocalOnly resource records
1303	// may have a scope e.g., fe80::1%en0. The question may be scoped or not: the InterfaceID may be set
1304	// to mDNSInterface_Any, mDNSInterface_LocalOnly or a real InterfaceID (scoped).
1305	//
1306	// 1) Question: Any, LocalOnly Record: no scope. This question should be answered with this record.
1307	//
1308	// 2) Question: Any, LocalOnly Record: scoped.  This question should be answered with the record because
1309	//    traditionally applications never specify scope e.g., getaddrinfo, but need to be able
1310	//    to get to /etc/hosts entries.
1311	//
1312	// 3) Question: Scoped (LocalOnly or InterfaceID), LocalOnly Record: no scope. This is the inverse of (2).
1313	//    If we register a LocalOnly record, we need to answer a LocalOnly question. If the /etc/hosts has a
1314	//    non scoped entry, it may not make sense to answer a scoped question. But we can't tell these two
1315	//    cases apart. As we currently answer LocalOnly question with LocalOnly record, we continue to do so.
1316	//
1317	// 4) Question: Scoped (LocalOnly or InterfaceID), LocalOnly Record: scoped. LocalOnly questions should be
1318	//    answered with any resource record where as if it has a valid InterfaceID, the scope should match.
1319	//
1320	// (1) and (2) is bypassed because we check for a non-NULL InterfaceID above. For (3), the InterfaceID is NULL
1321	// and hence bypassed above. For (4) we bypassed LocalOnly questions and checked the scope of the record
1322	// against the question.
1323	//
1324	// For P2P, InterfaceIDs of the question and the record should match.
1325
1326	// If ResourceRecord received via multicast, but question was unicast, then shouldn't use record to answer this question.
1327	// LocalOnly authoritative answers are exempt. LocalOnly authoritative answers are used for /etc/host entries.
1328	// We don't want a local process to be able to create a fake LocalOnly address record for "www.bigbank.com" which would then
1329	// cause other applications (e.g. Safari) to connect to the wrong address. The rpc to register records filters out records
1330	// with names that don't end in local and have mDNSInterface_LocalOnly set.
1331	//
1332	// Note: The check is bypassed for LocalOnly and for P2P it is not needed as only .local records are registered and for
1333	// a question to match its names, it also has to end in .local and that question can't be a unicast question (See
1334	// Question_uDNS macro and its usage). As P2P does not enforce .local only registrations we still make this check
1335	// and also makes it future proof.
1336
1337	if (ar->ARType != AuthRecordLocalOnly && rr->InterfaceID && !mDNSOpaque16IsZero(q->TargetQID)) return(mDNSfalse);
1338
1339	// RR type CNAME matches any query type. QTYPE ANY matches any RR type. QCLASS ANY matches any RR class.
1340	if (!RRTypeAnswersQuestionType(rr,q->qtype)) return(mDNSfalse);
1341	if (rr->rrclass != q->qclass && q->qclass != kDNSQClass_ANY) return(mDNSfalse);
1342
1343	return(rr->namehash == q->qnamehash && SameDomainName(rr->name, &q->qname));
1344	}
1345
1346mDNSexport mDNSBool AnyTypeRecordAnswersQuestion(const ResourceRecord *const rr, const DNSQuestion *const q)
1347	{
1348	// LocalOnly/P2P questions can be answered with AuthRecordAny in this function. LocalOnly/P2P records
1349	// are handled in LocalOnlyRecordAnswersQuestion
1350	if ((rr->InterfaceID == mDNSInterface_LocalOnly) || (rr->InterfaceID == mDNSInterface_P2P))
1351		{
1352		LogMsg("AnyTypeRecordAnswersQuestion: ERROR!! called with LocalOnly ResourceRecord %p, Question %p", rr->InterfaceID, q->InterfaceID);
1353		return mDNSfalse;
1354		}
1355	if (rr->InterfaceID &&
1356		q ->InterfaceID && q->InterfaceID != mDNSInterface_LocalOnly &&
1357		rr->InterfaceID != q->InterfaceID) return(mDNSfalse);
1358
1359	// Resource record received via unicast, the DNSServer entries should match ?
1360	// Note that Auth Records are normally setup with NULL InterfaceID and
1361	// both the DNSServers are assumed to be NULL in that case
1362	if (!rr->InterfaceID && rr->rDNSServer != q->qDNSServer) return(mDNSfalse);
1363
1364	// If ResourceRecord received via multicast, but question was unicast, then shouldn't use record to answer this question
1365	if (rr->InterfaceID && !mDNSOpaque16IsZero(q->TargetQID)) return(mDNSfalse);
1366
1367	if (rr->rrclass != q->qclass && q->qclass != kDNSQClass_ANY) return(mDNSfalse);
1368
1369	return(rr->namehash == q->qnamehash && SameDomainName(rr->name, &q->qname));
1370	}
1371
1372// This is called with both unicast resource record and multicast resource record. The question that
1373// received the unicast response could be the regular unicast response from a DNS server or a response
1374// to a mDNS QU query. The main reason we need this function is that we can't compare DNSServers between the
1375// question and the resource record because the resource record is not completely initialized in
1376// mDNSCoreReceiveResponse when this function is called.
1377mDNSexport mDNSBool ResourceRecordAnswersUnicastResponse(const ResourceRecord *const rr, const DNSQuestion *const q)
1378	{
1379	// For resource records created using multicast, the InterfaceIDs have to match
1380	if (rr->InterfaceID &&
1381		q->InterfaceID && rr->InterfaceID != q->InterfaceID) return(mDNSfalse);
1382
1383	// If ResourceRecord received via multicast, but question was unicast, then shouldn't use record to answer this question.
1384	if (rr->InterfaceID && !mDNSOpaque16IsZero(q->TargetQID)) return(mDNSfalse);
1385
1386	// RR type CNAME matches any query type. QTYPE ANY matches any RR type. QCLASS ANY matches any RR class.
1387	if (!RRTypeAnswersQuestionType(rr,q->qtype)) return(mDNSfalse);
1388
1389	if (rr->rrclass != q->qclass && q->qclass != kDNSQClass_ANY) return(mDNSfalse);
1390
1391	return(rr->namehash == q->qnamehash && SameDomainName(rr->name, &q->qname));
1392	}
1393
1394mDNSexport mDNSu16 GetRDLength(const ResourceRecord *const rr, mDNSBool estimate)
1395	{
1396	const RDataBody2 *const rd = (RDataBody2 *)rr->rdata->u.data;
1397	const domainname *const name = estimate ? rr->name : mDNSNULL;
1398	if (rr->rrclass == kDNSQClass_ANY) return(rr->rdlength);	// Used in update packets to mean "Delete An RRset" (RFC 2136)
1399	else switch (rr->rrtype)
1400		{
1401		case kDNSType_A:	return(sizeof(rd->ipv4));
1402
1403		case kDNSType_NS:
1404		case kDNSType_CNAME:
1405		case kDNSType_PTR:
1406		case kDNSType_DNAME:return(CompressedDomainNameLength(&rd->name, name));
1407
1408		case kDNSType_SOA:  return(mDNSu16)(CompressedDomainNameLength(&rd->soa.mname, name) +
1409											CompressedDomainNameLength(&rd->soa.rname, name) +
1410											5 * sizeof(mDNSOpaque32));
1411
1412		case kDNSType_NULL:
1413		case kDNSType_TSIG:
1414		case kDNSType_TXT:
1415		case kDNSType_X25:
1416		case kDNSType_ISDN:
1417		case kDNSType_LOC:
1418		case kDNSType_DHCID:return(rr->rdlength); // Not self-describing, so have to just trust rdlength
1419
1420		case kDNSType_HINFO:return(mDNSu16)(2 + (int)rd->data[0] + (int)rd->data[1 + (int)rd->data[0]]);
1421
1422		case kDNSType_MX:
1423		case kDNSType_AFSDB:
1424		case kDNSType_RT:
1425		case kDNSType_KX:	return(mDNSu16)(2 + CompressedDomainNameLength(&rd->mx.exchange, name));
1426
1427		case kDNSType_RP:	return(mDNSu16)(CompressedDomainNameLength(&rd->rp.mbox, name) +
1428											CompressedDomainNameLength(&rd->rp.txt, name));
1429
1430		case kDNSType_PX:	return(mDNSu16)(2 + CompressedDomainNameLength(&rd->px.map822, name) +
1431												CompressedDomainNameLength(&rd->px.mapx400, name));
1432
1433		case kDNSType_AAAA:	return(sizeof(rd->ipv6));
1434
1435		case kDNSType_SRV:	return(mDNSu16)(6 + CompressedDomainNameLength(&rd->srv.target, name));
1436
1437		case kDNSType_OPT:  return(rr->rdlength);
1438
1439		case kDNSType_NSEC: {
1440							int i;
1441							for (i=sizeof(rdataNSEC); i>0; i--) if (rd->nsec.bitmap[i-1]) break;
1442							// For our simplified use of NSEC synthetic records:
1443							// nextname is always the record's own name,
1444							// and if we have at least one record type that exists,
1445							//  - the block number is always 0,
1446							//  - the count byte is a value in the range 1-32,
1447							//  - followed by the 1-32 data bytes
1448							return(mDNSu16)((estimate ? 2 : DomainNameLength(rr->name)) + (i ? (2 + i) : 0));
1449							}
1450
1451		default:			debugf("Warning! Don't know how to get length of resource type %d", rr->rrtype);
1452							return(rr->rdlength);
1453		}
1454	}
1455
1456// When a local client registers (or updates) a record, we use this routine to do some simple validation checks
1457// to help reduce the risk of bogus malformed data on the network
1458mDNSexport mDNSBool ValidateRData(const mDNSu16 rrtype, const mDNSu16 rdlength, const RData *const rd)
1459	{
1460	mDNSu16 len;
1461
1462	switch(rrtype)
1463		{
1464		case kDNSType_A:	return(rdlength == sizeof(mDNSv4Addr));
1465
1466		case kDNSType_NS:	// Same as PTR
1467		case kDNSType_MD:	// Same as PTR
1468		case kDNSType_MF:	// Same as PTR
1469		case kDNSType_CNAME:// Same as PTR
1470		//case kDNSType_SOA not checked
1471		case kDNSType_MB:	// Same as PTR
1472		case kDNSType_MG:	// Same as PTR
1473		case kDNSType_MR:	// Same as PTR
1474		//case kDNSType_NULL not checked (no specified format, so always valid)
1475		//case kDNSType_WKS not checked
1476		case kDNSType_PTR:	len = DomainNameLengthLimit(&rd->u.name, rd->u.data + rdlength);
1477							return(len <= MAX_DOMAIN_NAME && rdlength == len);
1478
1479		case kDNSType_HINFO:// Same as TXT (roughly)
1480		case kDNSType_MINFO:// Same as TXT (roughly)
1481		case kDNSType_TXT:  if (!rdlength) return(mDNSfalse); // TXT record has to be at least one byte (RFC 1035)
1482							{
1483							const mDNSu8 *ptr = rd->u.txt.c;
1484							const mDNSu8 *end = rd->u.txt.c + rdlength;
1485							while (ptr < end) ptr += 1 + ptr[0];
1486							return (ptr == end);
1487							}
1488
1489		case kDNSType_AAAA:	return(rdlength == sizeof(mDNSv6Addr));
1490
1491		case kDNSType_MX:   // Must be at least two-byte preference, plus domainname
1492							// Call to DomainNameLengthLimit() implicitly enforces both requirements for us
1493							len = DomainNameLengthLimit(&rd->u.mx.exchange, rd->u.data + rdlength);
1494							return(len <= MAX_DOMAIN_NAME && rdlength == 2+len);
1495
1496		case kDNSType_SRV:	// Must be at least priority+weight+port, plus domainname
1497							// Call to DomainNameLengthLimit() implicitly enforces both requirements for us
1498							len = DomainNameLengthLimit(&rd->u.srv.target, rd->u.data + rdlength);
1499							return(len <= MAX_DOMAIN_NAME && rdlength == 6+len);
1500
1501		//case kDNSType_NSEC not checked
1502
1503		default:			return(mDNStrue);	// Allow all other types without checking
1504		}
1505	}
1506
1507// ***************************************************************************
1508#if COMPILER_LIKES_PRAGMA_MARK
1509#pragma mark -
1510#pragma mark - DNS Message Creation Functions
1511#endif
1512
1513mDNSexport void InitializeDNSMessage(DNSMessageHeader *h, mDNSOpaque16 id, mDNSOpaque16 flags)
1514	{
1515	h->id             = id;
1516	h->flags          = flags;
1517	h->numQuestions   = 0;
1518	h->numAnswers     = 0;
1519	h->numAuthorities = 0;
1520	h->numAdditionals = 0;
1521	}
1522
1523mDNSexport const mDNSu8 *FindCompressionPointer(const mDNSu8 *const base, const mDNSu8 *const end, const mDNSu8 *const domname)
1524	{
1525	const mDNSu8 *result = end - *domname - 1;
1526
1527	if (*domname == 0) return(mDNSNULL);	// There's no point trying to match just the root label
1528
1529	// This loop examines each possible starting position in packet, starting end of the packet and working backwards
1530	while (result >= base)
1531		{
1532		// If the length byte and first character of the label match, then check further to see
1533		// if this location in the packet will yield a useful name compression pointer.
1534		if (result[0] == domname[0] && result[1] == domname[1])
1535			{
1536			const mDNSu8 *name = domname;
1537			const mDNSu8 *targ = result;
1538			while (targ + *name < end)
1539				{
1540				// First see if this label matches
1541				int i;
1542				const mDNSu8 *pointertarget;
1543				for (i=0; i <= *name; i++) if (targ[i] != name[i]) break;
1544				if (i <= *name) break;							// If label did not match, bail out
1545				targ += 1 + *name;								// Else, did match, so advance target pointer
1546				name += 1 + *name;								// and proceed to check next label
1547				if (*name == 0 && *targ == 0) return(result);	// If no more labels, we found a match!
1548				if (*name == 0) break;							// If no more labels to match, we failed, so bail out
1549
1550				// The label matched, so now follow the pointer (if appropriate) and then see if the next label matches
1551				if (targ[0] < 0x40) continue;					// If length value, continue to check next label
1552				if (targ[0] < 0xC0) break;						// If 40-BF, not valid
1553				if (targ+1 >= end) break;						// Second byte not present!
1554				pointertarget = base + (((mDNSu16)(targ[0] & 0x3F)) << 8) + targ[1];
1555				if (targ < pointertarget) break;				// Pointertarget must point *backwards* in the packet
1556				if (pointertarget[0] >= 0x40) break;			// Pointertarget must point to a valid length byte
1557				targ = pointertarget;
1558				}
1559			}
1560		result--;	// We failed to match at this search position, so back up the tentative result pointer and try again
1561		}
1562	return(mDNSNULL);
1563	}
1564
1565// Put a string of dot-separated labels as length-prefixed labels
1566// domainname is a fully-qualified name (i.e. assumed to be ending in a dot, even if it doesn't)
1567// msg points to the message we're building (pass mDNSNULL if we don't want to use compression pointers)
1568// end points to the end of the message so far
1569// ptr points to where we want to put the name
1570// limit points to one byte past the end of the buffer that we must not overrun
1571// domainname is the name to put
1572mDNSexport mDNSu8 *putDomainNameAsLabels(const DNSMessage *const msg,
1573	mDNSu8 *ptr, const mDNSu8 *const limit, const domainname *const name)
1574	{
1575	const mDNSu8 *const base        = (const mDNSu8 *)msg;
1576	const mDNSu8 *      np          = name->c;
1577	const mDNSu8 *const max         = name->c + MAX_DOMAIN_NAME;	// Maximum that's valid
1578	const mDNSu8 *      pointer     = mDNSNULL;
1579	const mDNSu8 *const searchlimit = ptr;
1580
1581	if (!ptr) { LogMsg("putDomainNameAsLabels %##s ptr is null", name->c); return(mDNSNULL); }
1582
1583	if (!*np)		// If just writing one-byte root label, make sure we have space for that
1584		{
1585		if (ptr >= limit) return(mDNSNULL);
1586		}
1587	else			// else, loop through writing labels and/or a compression offset
1588		{
1589		do	{
1590			if (*np > MAX_DOMAIN_LABEL)
1591				{ LogMsg("Malformed domain name %##s (label more than 63 bytes)", name->c); return(mDNSNULL); }
1592
1593			// This check correctly allows for the final trailing root label:
1594			// e.g.
1595			// Suppose our domain name is exactly 256 bytes long, including the final trailing root label.
1596			// Suppose np is now at name->c[249], and we're about to write our last non-null label ("local").
1597			// We know that max will be at name->c[256]
1598			// That means that np + 1 + 5 == max - 1, so we (just) pass the "if" test below, write our
1599			// six bytes, then exit the loop, write the final terminating root label, and the domain
1600			// name we've written is exactly 256 bytes long, exactly at the correct legal limit.
1601			// If the name is one byte longer, then we fail the "if" test below, and correctly bail out.
1602			if (np + 1 + *np >= max)
1603				{ LogMsg("Malformed domain name %##s (more than 256 bytes)", name->c); return(mDNSNULL); }
1604
1605			if (base) pointer = FindCompressionPointer(base, searchlimit, np);
1606			if (pointer)					// Use a compression pointer if we can
1607				{
1608				const mDNSu16 offset = (mDNSu16)(pointer - base);
1609				if (ptr+2 > limit) return(mDNSNULL);	// If we don't have two bytes of space left, give up
1610				*ptr++ = (mDNSu8)(0xC0 | (offset >> 8));
1611				*ptr++ = (mDNSu8)(        offset &  0xFF);
1612				return(ptr);
1613				}
1614			else							// Else copy one label and try again
1615				{
1616				int i;
1617				mDNSu8 len = *np++;
1618				// If we don't at least have enough space for this label *plus* a terminating zero on the end, give up
1619				if (ptr + 1 + len >= limit) return(mDNSNULL);
1620				*ptr++ = len;
1621				for (i=0; i<len; i++) *ptr++ = *np++;
1622				}
1623			} while (*np);					// While we've got characters remaining in the name, continue
1624		}
1625
1626	*ptr++ = 0;		// Put the final root label
1627	return(ptr);
1628	}
1629
1630mDNSlocal mDNSu8 *putVal16(mDNSu8 *ptr, mDNSu16 val)
1631	{
1632	ptr[0] = (mDNSu8)((val >> 8 ) & 0xFF);
1633	ptr[1] = (mDNSu8)((val      ) & 0xFF);
1634	return ptr + sizeof(mDNSOpaque16);
1635	}
1636
1637mDNSlocal mDNSu8 *putVal32(mDNSu8 *ptr, mDNSu32 val)
1638	{
1639	ptr[0] = (mDNSu8)((val >> 24) & 0xFF);
1640	ptr[1] = (mDNSu8)((val >> 16) & 0xFF);
1641	ptr[2] = (mDNSu8)((val >>  8) & 0xFF);
1642	ptr[3] = (mDNSu8)((val      ) & 0xFF);
1643	return ptr + sizeof(mDNSu32);
1644	}
1645
1646// msg points to the message we're building (pass mDNSNULL if we don't want to use compression pointers)
1647mDNSexport mDNSu8 *putRData(const DNSMessage *const msg, mDNSu8 *ptr, const mDNSu8 *const limit, const ResourceRecord *const rr)
1648	{
1649	const RDataBody2 *const rdb = (RDataBody2 *)rr->rdata->u.data;
1650	switch (rr->rrtype)
1651		{
1652		case kDNSType_A:	if (rr->rdlength != 4)
1653								{ debugf("putRData: Illegal length %d for kDNSType_A", rr->rdlength); return(mDNSNULL); }
1654							if (ptr + 4 > limit) return(mDNSNULL);
1655							*ptr++ = rdb->ipv4.b[0];
1656							*ptr++ = rdb->ipv4.b[1];
1657							*ptr++ = rdb->ipv4.b[2];
1658							*ptr++ = rdb->ipv4.b[3];
1659							return(ptr);
1660
1661		case kDNSType_NS:
1662		case kDNSType_CNAME:
1663		case kDNSType_PTR:
1664		case kDNSType_DNAME:return(putDomainNameAsLabels(msg, ptr, limit, &rdb->name));
1665
1666		case kDNSType_SOA:  ptr = putDomainNameAsLabels(msg, ptr, limit, &rdb->soa.mname);
1667							if (!ptr) return(mDNSNULL);
1668							ptr = putDomainNameAsLabels(msg, ptr, limit, &rdb->soa.rname);
1669							if (!ptr || ptr + 20 > limit) return(mDNSNULL);
1670							ptr = putVal32(ptr, rdb->soa.serial);
1671							ptr = putVal32(ptr, rdb->soa.refresh);
1672							ptr = putVal32(ptr, rdb->soa.retry);
1673							ptr = putVal32(ptr, rdb->soa.expire);
1674							ptr = putVal32(ptr, rdb->soa.min);
1675			                return(ptr);
1676
1677		case kDNSType_NULL:
1678		case kDNSType_HINFO:
1679		case kDNSType_TSIG:
1680		case kDNSType_TXT:
1681		case kDNSType_X25:
1682		case kDNSType_ISDN:
1683		case kDNSType_LOC:
1684		case kDNSType_DHCID:if (ptr + rr->rdlength > limit) return(mDNSNULL);
1685							mDNSPlatformMemCopy(ptr, rdb->data, rr->rdlength);
1686							return(ptr + rr->rdlength);
1687
1688		case kDNSType_MX:
1689		case kDNSType_AFSDB:
1690		case kDNSType_RT:
1691		case kDNSType_KX:	if (ptr + 3 > limit) return(mDNSNULL);
1692							ptr = putVal16(ptr, rdb->mx.preference);
1693							return(putDomainNameAsLabels(msg, ptr, limit, &rdb->mx.exchange));
1694
1695		case kDNSType_RP:	ptr = putDomainNameAsLabels(msg, ptr, limit, &rdb->rp.mbox);
1696							if (!ptr) return(mDNSNULL);
1697							ptr = putDomainNameAsLabels(msg, ptr, limit, &rdb->rp.txt);
1698			                return(ptr);
1699
1700		case kDNSType_PX:	if (ptr + 5 > limit) return(mDNSNULL);
1701							ptr = putVal16(ptr, rdb->px.preference);
1702							ptr = putDomainNameAsLabels(msg, ptr, limit, &rdb->px.map822);
1703							if (!ptr) return(mDNSNULL);
1704							ptr = putDomainNameAsLabels(msg, ptr, limit, &rdb->px.mapx400);
1705			                return(ptr);
1706
1707		case kDNSType_AAAA:	if (rr->rdlength != sizeof(rdb->ipv6))
1708								{ debugf("putRData: Illegal length %d for kDNSType_AAAA", rr->rdlength); return(mDNSNULL); }
1709							if (ptr + sizeof(rdb->ipv6) > limit) return(mDNSNULL);
1710							mDNSPlatformMemCopy(ptr, &rdb->ipv6, sizeof(rdb->ipv6));
1711							return(ptr + sizeof(rdb->ipv6));
1712
1713		case kDNSType_SRV:	if (ptr + 7 > limit) return(mDNSNULL);
1714							*ptr++ = (mDNSu8)(rdb->srv.priority >> 8);
1715							*ptr++ = (mDNSu8)(rdb->srv.priority &  0xFF);
1716							*ptr++ = (mDNSu8)(rdb->srv.weight   >> 8);
1717							*ptr++ = (mDNSu8)(rdb->srv.weight   &  0xFF);
1718							*ptr++ = rdb->srv.port.b[0];
1719							*ptr++ = rdb->srv.port.b[1];
1720							return(putDomainNameAsLabels(msg, ptr, limit, &rdb->srv.target));
1721
1722		case kDNSType_OPT:	{
1723							int len = 0;
1724							const rdataOPT *opt;
1725							const rdataOPT *const end = (const rdataOPT *)&rr->rdata->u.data[rr->rdlength];
1726							for (opt = &rr->rdata->u.opt[0]; opt < end; opt++) len += DNSOpt_Data_Space(opt);
1727							if (ptr + len > limit) { LogMsg("ERROR: putOptRData - out of space"); return mDNSNULL; }
1728
1729							for (opt = &rr->rdata->u.opt[0]; opt < end; opt++)
1730								{
1731								const int space = DNSOpt_Data_Space(opt);
1732								ptr = putVal16(ptr, opt->opt);
1733								ptr = putVal16(ptr, (mDNSu16)space - 4);
1734								switch (opt->opt)
1735									{
1736									case kDNSOpt_LLQ:
1737										ptr = putVal16(ptr, opt->u.llq.vers);
1738										ptr = putVal16(ptr, opt->u.llq.llqOp);
1739										ptr = putVal16(ptr, opt->u.llq.err);
1740										mDNSPlatformMemCopy(ptr, opt->u.llq.id.b, 8);  // 8-byte id
1741										ptr += 8;
1742										ptr = putVal32(ptr, opt->u.llq.llqlease);
1743										break;
1744									case kDNSOpt_Lease:
1745										ptr = putVal32(ptr, opt->u.updatelease);
1746										break;
1747									case kDNSOpt_Owner:
1748										*ptr++ = opt->u.owner.vers;
1749										*ptr++ = opt->u.owner.seq;
1750										mDNSPlatformMemCopy(ptr, opt->u.owner.HMAC.b, 6);  // 6-byte Host identifier
1751										ptr += 6;
1752										if (space >= DNSOpt_OwnerData_ID_Wake_Space)
1753											{
1754											mDNSPlatformMemCopy(ptr, opt->u.owner.IMAC.b, 6);	// 6-byte interface MAC
1755											ptr += 6;
1756											if (space > DNSOpt_OwnerData_ID_Wake_Space)
1757												{
1758												mDNSPlatformMemCopy(ptr, opt->u.owner.password.b, space - DNSOpt_OwnerData_ID_Wake_Space);
1759												ptr += space - DNSOpt_OwnerData_ID_Wake_Space;
1760												}
1761											}
1762										break;
1763									}
1764								}
1765							return ptr;
1766							}
1767
1768		case kDNSType_NSEC: {
1769							// For our simplified use of NSEC synthetic records:
1770							// nextname is always the record's own name,
1771							// the block number is always 0,
1772							// the count byte is a value in the range 1-32,
1773							// followed by the 1-32 data bytes
1774							int i, j;
1775							for (i=sizeof(rdataNSEC); i>0; i--) if (rdb->nsec.bitmap[i-1]) break;
1776							ptr = putDomainNameAsLabels(msg, ptr, limit, rr->name);
1777							if (!ptr) return(mDNSNULL);
1778							if (i)		// Only put a block if at least one type exists for this name
1779								{
1780								if (ptr + 2 + i > limit) return(mDNSNULL);
1781								*ptr++ = 0;
1782								*ptr++ = (mDNSu8)i;
1783								for (j=0; j<i; j++) *ptr++ = rdb->nsec.bitmap[j];
1784								}
1785							return ptr;
1786							}
1787
1788		default:			debugf("putRData: Warning! Writing unknown resource type %d as raw data", rr->rrtype);
1789							if (ptr + rr->rdlength > limit) return(mDNSNULL);
1790							mDNSPlatformMemCopy(ptr, rdb->data, rr->rdlength);
1791							return(ptr + rr->rdlength);
1792		}
1793	}
1794
1795#define IsUnicastUpdate(X) (!mDNSOpaque16IsZero((X)->h.id) && ((X)->h.flags.b[0] & kDNSFlag0_OP_Mask) == kDNSFlag0_OP_Update)
1796
1797mDNSexport mDNSu8 *PutResourceRecordTTLWithLimit(DNSMessage *const msg, mDNSu8 *ptr, mDNSu16 *count, ResourceRecord *rr, mDNSu32 ttl, const mDNSu8 *limit)
1798	{
1799	mDNSu8 *endofrdata;
1800	mDNSu16 actualLength;
1801	// When sending SRV to conventional DNS server (i.e. in DNS update requests) we should not do name compression on the rdata (RFC 2782)
1802	const DNSMessage *const rdatacompressionbase = (IsUnicastUpdate(msg) && rr->rrtype == kDNSType_SRV) ? mDNSNULL : msg;
1803
1804	if (rr->RecordType == kDNSRecordTypeUnregistered)
1805		{
1806		LogMsg("PutResourceRecord ERROR! Attempt to put kDNSRecordTypeUnregistered %##s (%s)", rr->name->c, DNSTypeName(rr->rrtype));
1807		return(ptr);
1808		}
1809
1810	if (!ptr) { LogMsg("PutResourceRecordTTLWithLimit ptr is null"); return(mDNSNULL); }
1811
1812	ptr = putDomainNameAsLabels(msg, ptr, limit, rr->name);
1813	if (!ptr || ptr + 10 >= limit) return(mDNSNULL);	// If we're out-of-space, return mDNSNULL
1814	ptr[0] = (mDNSu8)(rr->rrtype  >> 8);
1815	ptr[1] = (mDNSu8)(rr->rrtype  &  0xFF);
1816	ptr[2] = (mDNSu8)(rr->rrclass >> 8);
1817	ptr[3] = (mDNSu8)(rr->rrclass &  0xFF);
1818	ptr[4] = (mDNSu8)((ttl >> 24) &  0xFF);
1819	ptr[5] = (mDNSu8)((ttl >> 16) &  0xFF);
1820	ptr[6] = (mDNSu8)((ttl >>  8) &  0xFF);
1821	ptr[7] = (mDNSu8)( ttl        &  0xFF);
1822	// ptr[8] and ptr[9] filled in *after* we find out how much space the rdata takes
1823
1824	endofrdata = putRData(rdatacompressionbase, ptr+10, limit, rr);
1825	if (!endofrdata) { verbosedebugf("Ran out of space in PutResourceRecord for %##s (%s)", rr->name->c, DNSTypeName(rr->rrtype)); return(mDNSNULL); }
1826
1827	// Go back and fill in the actual number of data bytes we wrote
1828	// (actualLength can be less than rdlength when domain name compression is used)
1829	actualLength = (mDNSu16)(endofrdata - ptr - 10);
1830	ptr[8] = (mDNSu8)(actualLength >> 8);
1831	ptr[9] = (mDNSu8)(actualLength &  0xFF);
1832
1833	if (count) (*count)++;
1834	else LogMsg("PutResourceRecordTTL: ERROR: No target count to update for %##s (%s)", rr->name->c, DNSTypeName(rr->rrtype));
1835	return(endofrdata);
1836	}
1837
1838mDNSlocal mDNSu8 *putEmptyResourceRecord(DNSMessage *const msg, mDNSu8 *ptr, const mDNSu8 *const limit, mDNSu16 *count, const AuthRecord *rr)
1839	{
1840	ptr = putDomainNameAsLabels(msg, ptr, limit, rr->resrec.name);
1841	if (!ptr || ptr + 10 > limit) return(mDNSNULL);		// If we're out-of-space, return mDNSNULL
1842	ptr[0] = (mDNSu8)(rr->resrec.rrtype  >> 8);				// Put type
1843	ptr[1] = (mDNSu8)(rr->resrec.rrtype  &  0xFF);
1844	ptr[2] = (mDNSu8)(rr->resrec.rrclass >> 8);				// Put class
1845	ptr[3] = (mDNSu8)(rr->resrec.rrclass &  0xFF);
1846	ptr[4] = ptr[5] = ptr[6] = ptr[7] = 0;				// TTL is zero
1847	ptr[8] = ptr[9] = 0;								// RDATA length is zero
1848	(*count)++;
1849	return(ptr + 10);
1850	}
1851
1852mDNSexport mDNSu8 *putQuestion(DNSMessage *const msg, mDNSu8 *ptr, const mDNSu8 *const limit, const domainname *const name, mDNSu16 rrtype, mDNSu16 rrclass)
1853	{
1854	ptr = putDomainNameAsLabels(msg, ptr, limit, name);
1855	if (!ptr || ptr+4 >= limit) return(mDNSNULL);			// If we're out-of-space, return mDNSNULL
1856	ptr[0] = (mDNSu8)(rrtype  >> 8);
1857	ptr[1] = (mDNSu8)(rrtype  &  0xFF);
1858	ptr[2] = (mDNSu8)(rrclass >> 8);
1859	ptr[3] = (mDNSu8)(rrclass &  0xFF);
1860	msg->h.numQuestions++;
1861	return(ptr+4);
1862	}
1863
1864// for dynamic updates
1865mDNSexport mDNSu8 *putZone(DNSMessage *const msg, mDNSu8 *ptr, mDNSu8 *limit, const domainname *zone, mDNSOpaque16 zoneClass)
1866	{
1867	ptr = putDomainNameAsLabels(msg, ptr, limit, zone);
1868	if (!ptr || ptr + 4 > limit) return mDNSNULL;		// If we're out-of-space, return NULL
1869	*ptr++ = (mDNSu8)(kDNSType_SOA  >> 8);
1870	*ptr++ = (mDNSu8)(kDNSType_SOA  &  0xFF);
1871	*ptr++ = zoneClass.b[0];
1872	*ptr++ = zoneClass.b[1];
1873	msg->h.mDNS_numZones++;
1874	return ptr;
1875	}
1876
1877// for dynamic updates
1878mDNSexport mDNSu8 *putPrereqNameNotInUse(const domainname *const name, DNSMessage *const msg, mDNSu8 *const ptr, mDNSu8 *const end)
1879	{
1880	AuthRecord prereq;
1881	mDNS_SetupResourceRecord(&prereq, mDNSNULL, mDNSInterface_Any, kDNSQType_ANY, kStandardTTL, 0, AuthRecordAny, mDNSNULL, mDNSNULL);
1882	AssignDomainName(&prereq.namestorage, name);
1883	prereq.resrec.rrtype = kDNSQType_ANY;
1884	prereq.resrec.rrclass = kDNSClass_NONE;
1885	return putEmptyResourceRecord(msg, ptr, end, &msg->h.mDNS_numPrereqs, &prereq);
1886	}
1887
1888// for dynamic updates
1889mDNSexport mDNSu8 *putDeletionRecord(DNSMessage *msg, mDNSu8 *ptr, ResourceRecord *rr)
1890	{
1891	// deletion: specify record w/ TTL 0, class NONE
1892	const mDNSu16 origclass = rr->rrclass;
1893	rr->rrclass = kDNSClass_NONE;
1894	ptr = PutResourceRecordTTLJumbo(msg, ptr, &msg->h.mDNS_numUpdates, rr, 0);
1895	rr->rrclass = origclass;
1896	return ptr;
1897	}
1898
1899// for dynamic updates
1900mDNSexport mDNSu8 *putDeletionRecordWithLimit(DNSMessage *msg, mDNSu8 *ptr, ResourceRecord *rr, mDNSu8 *limit)
1901	{
1902	// deletion: specify record w/ TTL 0, class NONE
1903	const mDNSu16 origclass = rr->rrclass;
1904	rr->rrclass = kDNSClass_NONE;
1905	ptr = PutResourceRecordTTLWithLimit(msg, ptr, &msg->h.mDNS_numUpdates, rr, 0, limit);
1906	rr->rrclass = origclass;
1907	return ptr;
1908	}
1909
1910mDNSexport mDNSu8 *putDeleteRRSetWithLimit(DNSMessage *msg, mDNSu8 *ptr, const domainname *name, mDNSu16 rrtype, mDNSu8 *limit)
1911	{
1912	mDNSu16 class = kDNSQClass_ANY;
1913
1914	ptr = putDomainNameAsLabels(msg, ptr, limit, name);
1915	if (!ptr || ptr + 10 >= limit) return mDNSNULL;	// If we're out-of-space, return mDNSNULL
1916	ptr[0] = (mDNSu8)(rrtype  >> 8);
1917	ptr[1] = (mDNSu8)(rrtype  &  0xFF);
1918	ptr[2] = (mDNSu8)(class >> 8);
1919	ptr[3] = (mDNSu8)(class &  0xFF);
1920	ptr[4] = ptr[5] = ptr[6] = ptr[7] = 0; // zero ttl
1921	ptr[8] = ptr[9] = 0; // zero rdlength/rdata
1922
1923	msg->h.mDNS_numUpdates++;
1924	return ptr + 10;
1925	}
1926
1927// for dynamic updates
1928mDNSexport mDNSu8 *putDeleteAllRRSets(DNSMessage *msg, mDNSu8 *ptr, const domainname *name)
1929	{
1930	const mDNSu8 *limit = msg->data + AbsoluteMaxDNSMessageData;
1931	mDNSu16 class = kDNSQClass_ANY;
1932	mDNSu16 rrtype = kDNSQType_ANY;
1933
1934	ptr = putDomainNameAsLabels(msg, ptr, limit, name);
1935	if (!ptr || ptr + 10 >= limit) return mDNSNULL;	// If we're out-of-space, return mDNSNULL
1936	ptr[0] = (mDNSu8)(rrtype >> 8);
1937	ptr[1] = (mDNSu8)(rrtype &  0xFF);
1938	ptr[2] = (mDNSu8)(class  >> 8);
1939	ptr[3] = (mDNSu8)(class  &  0xFF);
1940	ptr[4] = ptr[5] = ptr[6] = ptr[7] = 0; // zero ttl
1941	ptr[8] = ptr[9] = 0; // zero rdlength/rdata
1942
1943	msg->h.mDNS_numUpdates++;
1944	return ptr + 10;
1945	}
1946
1947// for dynamic updates
1948mDNSexport mDNSu8 *putUpdateLease(DNSMessage *msg, mDNSu8 *end, mDNSu32 lease)
1949	{
1950	AuthRecord rr;
1951	mDNS_SetupResourceRecord(&rr, mDNSNULL, mDNSInterface_Any, kDNSType_OPT, kStandardTTL, kDNSRecordTypeKnownUnique, AuthRecordAny, mDNSNULL, mDNSNULL);
1952	rr.resrec.rrclass    = NormalMaxDNSMessageData;
1953	rr.resrec.rdlength   = sizeof(rdataOPT);	// One option in this OPT record
1954	rr.resrec.rdestimate = sizeof(rdataOPT);
1955	rr.resrec.rdata->u.opt[0].opt           = kDNSOpt_Lease;
1956	rr.resrec.rdata->u.opt[0].u.updatelease = lease;
1957	end = PutResourceRecordTTLJumbo(msg, end, &msg->h.numAdditionals, &rr.resrec, 0);
1958	if (!end) { LogMsg("ERROR: putUpdateLease - PutResourceRecordTTL"); return mDNSNULL; }
1959	return end;
1960	}
1961
1962// for dynamic updates
1963mDNSexport mDNSu8 *putUpdateLeaseWithLimit(DNSMessage *msg, mDNSu8 *end, mDNSu32 lease, mDNSu8 *limit)
1964	{
1965	AuthRecord rr;
1966	mDNS_SetupResourceRecord(&rr, mDNSNULL, mDNSInterface_Any, kDNSType_OPT, kStandardTTL, kDNSRecordTypeKnownUnique, AuthRecordAny, mDNSNULL, mDNSNULL);
1967	rr.resrec.rrclass    = NormalMaxDNSMessageData;
1968	rr.resrec.rdlength   = sizeof(rdataOPT);	// One option in this OPT record
1969	rr.resrec.rdestimate = sizeof(rdataOPT);
1970	rr.resrec.rdata->u.opt[0].opt           = kDNSOpt_Lease;
1971	rr.resrec.rdata->u.opt[0].u.updatelease = lease;
1972	end = PutResourceRecordTTLWithLimit(msg, end, &msg->h.numAdditionals, &rr.resrec, 0, limit);
1973	if (!end) { LogMsg("ERROR: putUpdateLease - PutResourceRecordTTLWithLimit"); return mDNSNULL; }
1974	return end;
1975	}
1976
1977mDNSexport mDNSu8 *putHINFO(const mDNS *const m, DNSMessage *const msg, mDNSu8 *end, DomainAuthInfo *authInfo, mDNSu8 *limit)
1978	{
1979	if (authInfo && authInfo->AutoTunnel)
1980		{
1981		AuthRecord hinfo;
1982		mDNSu8 *h = hinfo.rdatastorage.u.data;
1983		mDNSu16 len = 2 + m->HIHardware.c[0] + m->HISoftware.c[0];
1984		mDNSu8 *newptr;
1985		mDNS_SetupResourceRecord(&hinfo, mDNSNULL, mDNSInterface_Any, kDNSType_HINFO, 0, kDNSRecordTypeUnique, AuthRecordAny, mDNSNULL, mDNSNULL);
1986		AppendDomainLabel(&hinfo.namestorage, &m->hostlabel);
1987		AppendDomainName (&hinfo.namestorage, &authInfo->domain);
1988		hinfo.resrec.rroriginalttl = 0;
1989		mDNSPlatformMemCopy(h, &m->HIHardware, 1 + (mDNSu32)m->HIHardware.c[0]);
1990		h += 1 + (int)h[0];
1991		mDNSPlatformMemCopy(h, &m->HISoftware, 1 + (mDNSu32)m->HISoftware.c[0]);
1992		hinfo.resrec.rdlength   = len;
1993		hinfo.resrec.rdestimate = len;
1994		newptr = PutResourceRecordTTLWithLimit(msg, end, &msg->h.numAdditionals, &hinfo.resrec, 0, limit);
1995		return newptr;
1996		}
1997	else
1998		return end;
1999	}
2000
2001// ***************************************************************************
2002#if COMPILER_LIKES_PRAGMA_MARK
2003#pragma mark -
2004#pragma mark - DNS Message Parsing Functions
2005#endif
2006
2007mDNSexport mDNSu32 DomainNameHashValue(const domainname *const name)
2008	{
2009	mDNSu32 sum = 0;
2010	const mDNSu8 *c;
2011
2012	for (c = name->c; c[0] != 0 && c[1] != 0; c += 2)
2013		{
2014		sum += ((mDNSIsUpperCase(c[0]) ? c[0] + 'a' - 'A' : c[0]) << 8) |
2015				(mDNSIsUpperCase(c[1]) ? c[1] + 'a' - 'A' : c[1]);
2016		sum = (sum<<3) | (sum>>29);
2017		}
2018	if (c[0]) sum += ((mDNSIsUpperCase(c[0]) ? c[0] + 'a' - 'A' : c[0]) << 8);
2019	return(sum);
2020	}
2021
2022mDNSexport void SetNewRData(ResourceRecord *const rr, RData *NewRData, mDNSu16 rdlength)
2023	{
2024	domainname *target;
2025	if (NewRData)
2026		{
2027		rr->rdata    = NewRData;
2028		rr->rdlength = rdlength;
2029		}
2030	// Must not try to get target pointer until after updating rr->rdata
2031	target = GetRRDomainNameTarget(rr);
2032	rr->rdlength   = GetRDLength(rr, mDNSfalse);
2033	rr->rdestimate = GetRDLength(rr, mDNStrue);
2034	rr->rdatahash  = target ? DomainNameHashValue(target) : RDataHashValue(rr);
2035	}
2036
2037mDNSexport const mDNSu8 *skipDomainName(const DNSMessage *const msg, const mDNSu8 *ptr, const mDNSu8 *const end)
2038	{
2039	mDNSu16 total = 0;
2040
2041	if (ptr < (mDNSu8*)msg || ptr >= end)
2042		{ debugf("skipDomainName: Illegal ptr not within packet boundaries"); return(mDNSNULL); }
2043
2044	while (1)						// Read sequence of labels
2045		{
2046		const mDNSu8 len = *ptr++;	// Read length of this label
2047		if (len == 0) return(ptr);	// If length is zero, that means this name is complete
2048		switch (len & 0xC0)
2049			{
2050			case 0x00:	if (ptr + len >= end)					// Remember: expect at least one more byte for the root label
2051							{ debugf("skipDomainName: Malformed domain name (overruns packet end)"); return(mDNSNULL); }
2052						if (total + 1 + len >= MAX_DOMAIN_NAME)	// Remember: expect at least one more byte for the root label
2053							{ debugf("skipDomainName: Malformed domain name (more than 256 characters)"); return(mDNSNULL); }
2054						ptr += len;
2055						total += 1 + len;
2056						break;
2057
2058			case 0x40:	debugf("skipDomainName: Extended EDNS0 label types 0x%X not supported", len); return(mDNSNULL);
2059			case 0x80:	debugf("skipDomainName: Illegal label length 0x%X", len); return(mDNSNULL);
2060			case 0xC0:	return(ptr+1);
2061			}
2062		}
2063	}
2064
2065// Routine to fetch an FQDN from the DNS message, following compression pointers if necessary.
2066mDNSexport const mDNSu8 *getDomainName(const DNSMessage *const msg, const mDNSu8 *ptr, const mDNSu8 *const end,
2067	domainname *const name)
2068	{
2069	const mDNSu8 *nextbyte = mDNSNULL;					// Record where we got to before we started following pointers
2070	mDNSu8       *np = name->c;							// Name pointer
2071	const mDNSu8 *const limit = np + MAX_DOMAIN_NAME;	// Limit so we don't overrun buffer
2072
2073	if (ptr < (mDNSu8*)msg || ptr >= end)
2074		{ debugf("getDomainName: Illegal ptr not within packet boundaries"); return(mDNSNULL); }
2075
2076	*np = 0;						// Tentatively place the root label here (may be overwritten if we have more labels)
2077
2078	while (1)						// Read sequence of labels
2079		{
2080		const mDNSu8 len = *ptr++;	// Read length of this label
2081		if (len == 0) break;		// If length is zero, that means this name is complete
2082		switch (len & 0xC0)
2083			{
2084			int i;
2085			mDNSu16 offset;
2086
2087			case 0x00:	if (ptr + len >= end)		// Remember: expect at least one more byte for the root label
2088							{ debugf("getDomainName: Malformed domain name (overruns packet end)"); return(mDNSNULL); }
2089						if (np + 1 + len >= limit)	// Remember: expect at least one more byte for the root label
2090							{ debugf("getDomainName: Malformed domain name (more than 256 characters)"); return(mDNSNULL); }
2091						*np++ = len;
2092						for (i=0; i<len; i++) *np++ = *ptr++;
2093						*np = 0;	// Tentatively place the root label here (may be overwritten if we have more labels)
2094						break;
2095
2096			case 0x40:	debugf("getDomainName: Extended EDNS0 label types 0x%X not supported in name %##s", len, name->c);
2097						return(mDNSNULL);
2098
2099			case 0x80:	debugf("getDomainName: Illegal label length 0x%X in domain name %##s", len, name->c); return(mDNSNULL);
2100
2101			case 0xC0:	offset = (mDNSu16)((((mDNSu16)(len & 0x3F)) << 8) | *ptr++);
2102						if (!nextbyte) nextbyte = ptr;	// Record where we got to before we started following pointers
2103						ptr = (mDNSu8 *)msg + offset;
2104						if (ptr < (mDNSu8*)msg || ptr >= end)
2105							{ debugf("getDomainName: Illegal compression pointer not within packet boundaries"); return(mDNSNULL); }
2106						if (*ptr & 0xC0)
2107							{ debugf("getDomainName: Compression pointer must point to real label"); return(mDNSNULL); }
2108						break;
2109			}
2110		}
2111
2112	if (nextbyte) return(nextbyte);
2113	else return(ptr);
2114	}
2115
2116mDNSexport const mDNSu8 *skipResourceRecord(const DNSMessage *msg, const mDNSu8 *ptr, const mDNSu8 *end)
2117	{
2118	mDNSu16 pktrdlength;
2119
2120	ptr = skipDomainName(msg, ptr, end);
2121	if (!ptr) { debugf("skipResourceRecord: Malformed RR name"); return(mDNSNULL); }
2122
2123	if (ptr + 10 > end) { debugf("skipResourceRecord: Malformed RR -- no type/class/ttl/len!"); return(mDNSNULL); }
2124	pktrdlength = (mDNSu16)((mDNSu16)ptr[8] <<  8 | ptr[9]);
2125	ptr += 10;
2126	if (ptr + pktrdlength > end) { debugf("skipResourceRecord: RDATA exceeds end of packet"); return(mDNSNULL); }
2127
2128	return(ptr + pktrdlength);
2129	}
2130
2131mDNSexport const mDNSu8 *GetLargeResourceRecord(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *ptr,
2132    const mDNSu8 *end, const mDNSInterfaceID InterfaceID, mDNSu8 RecordType, LargeCacheRecord *const largecr)
2133	{
2134	CacheRecord *const rr = &largecr->r;
2135	RDataBody2 *const rdb = (RDataBody2 *)rr->smallrdatastorage.data;
2136	mDNSu16 pktrdlength;
2137
2138	if (largecr == &m->rec && m->rec.r.resrec.RecordType)
2139		{
2140		LogMsg("GetLargeResourceRecord: m->rec appears to be already in use for %s", CRDisplayString(m, &m->rec.r));
2141#if ForceAlerts
2142		*(long*)0 = 0;
2143#endif
2144		}
2145
2146	rr->next              = mDNSNULL;
2147	rr->resrec.name       = &largecr->namestorage;
2148
2149	rr->NextInKAList      = mDNSNULL;
2150	rr->TimeRcvd          = m ? m->timenow : 0;
2151	rr->DelayDelivery     = 0;
2152	rr->NextRequiredQuery = m ? m->timenow : 0;		// Will be updated to the real value when we call SetNextCacheCheckTimeForRecord()
2153	rr->LastUsed          = m ? m->timenow : 0;
2154	rr->CRActiveQuestion  = mDNSNULL;
2155	rr->UnansweredQueries = 0;
2156	rr->LastUnansweredTime= 0;
2157#if ENABLE_MULTI_PACKET_QUERY_SNOOPING
2158	rr->MPUnansweredQ     = 0;
2159	rr->MPLastUnansweredQT= 0;
2160	rr->MPUnansweredKA    = 0;
2161	rr->MPExpectingKA     = mDNSfalse;
2162#endif
2163	rr->NextInCFList      = mDNSNULL;
2164
2165	rr->resrec.InterfaceID       = InterfaceID;
2166	rr->resrec.rDNSServer = mDNSNULL;
2167
2168	ptr = getDomainName(msg, ptr, end, &largecr->namestorage);		// Will bail out correctly if ptr is NULL
2169	if (!ptr) { debugf("GetLargeResourceRecord: Malformed RR name"); return(mDNSNULL); }
2170	rr->resrec.namehash = DomainNameHashValue(rr->resrec.name);
2171
2172	if (ptr + 10 > end) { debugf("GetLargeResourceRecord: Malformed RR -- no type/class/ttl/len!"); return(mDNSNULL); }
2173
2174	rr->resrec.rrtype            = (mDNSu16) ((mDNSu16)ptr[0] <<  8 | ptr[1]);
2175	rr->resrec.rrclass           = (mDNSu16)(((mDNSu16)ptr[2] <<  8 | ptr[3]) & kDNSClass_Mask);
2176	rr->resrec.rroriginalttl     = (mDNSu32) ((mDNSu32)ptr[4] << 24 | (mDNSu32)ptr[5] << 16 | (mDNSu32)ptr[6] << 8 | ptr[7]);
2177	if (rr->resrec.rroriginalttl > 0x70000000UL / mDNSPlatformOneSecond && (mDNSs32)rr->resrec.rroriginalttl != -1)
2178		rr->resrec.rroriginalttl = 0x70000000UL / mDNSPlatformOneSecond;
2179	// Note: We don't have to adjust m->NextCacheCheck here -- this is just getting a record into memory for
2180	// us to look at. If we decide to copy it into the cache, then we'll update m->NextCacheCheck accordingly.
2181	pktrdlength           = (mDNSu16)((mDNSu16)ptr[8] <<  8 | ptr[9]);
2182
2183	// If mDNS record has cache-flush bit set, we mark it unique
2184	// For uDNS records, all are implicitly deemed unique (a single DNS server is always
2185	// authoritative for the entire RRSet), unless this is a truncated response
2186	if (ptr[2] & (kDNSClass_UniqueRRSet >> 8) || (!InterfaceID && !(msg->h.flags.b[0] & kDNSFlag0_TC)))
2187		RecordType |= kDNSRecordTypePacketUniqueMask;
2188	ptr += 10;
2189	if (ptr + pktrdlength > end) { debugf("GetLargeResourceRecord: RDATA exceeds end of packet"); return(mDNSNULL); }
2190	end = ptr + pktrdlength;		// Adjust end to indicate the end of the rdata for this resource record
2191
2192	rr->resrec.rdata = (RData*)&rr->smallrdatastorage;
2193	rr->resrec.rdata->MaxRDLength = MaximumRDSize;
2194
2195	if (!RecordType) LogMsg("GetLargeResourceRecord: No RecordType for %##s", rr->resrec.name->c);
2196
2197	// IMPORTANT: Any record type we understand and unpack into a structure containing domainnames needs to have corresponding
2198	// cases in SameRDataBody() and RDataHashValue() to do a semantic comparison (or checksum) of the structure instead of a blind
2199	// bitwise memory compare (or sum). This is because a domainname is a fixed size structure holding variable-length data.
2200	// Any bytes past the logical end of the name are undefined, and a blind bitwise memory compare may indicate that
2201	// two domainnames are different when semantically they are the same name and it's only the unused bytes that differ.
2202	if (rr->resrec.rrclass == kDNSQClass_ANY && pktrdlength == 0)	// Used in update packets to mean "Delete An RRset" (RFC 2136)
2203		rr->resrec.rdlength = 0;
2204	else switch (rr->resrec.rrtype)
2205		{
2206		case kDNSType_A:	if (pktrdlength != sizeof(mDNSv4Addr)) goto fail;
2207							rdb->ipv4.b[0] = ptr[0];
2208							rdb->ipv4.b[1] = ptr[1];
2209							rdb->ipv4.b[2] = ptr[2];
2210							rdb->ipv4.b[3] = ptr[3];
2211							break;
2212
2213		case kDNSType_NS:
2214		case kDNSType_CNAME:
2215		case kDNSType_PTR:
2216		case kDNSType_DNAME:ptr = getDomainName(msg, ptr, end, &rdb->name);
2217							if (ptr != end) { debugf("GetLargeResourceRecord: Malformed CNAME/PTR RDATA name"); goto fail; }
2218							//debugf("%##s PTR %##s rdlen %d", rr->resrec.name.c, rdb->name.c, pktrdlength);
2219							break;
2220
2221		case kDNSType_SOA:  ptr = getDomainName(msg, ptr, end, &rdb->soa.mname);
2222							if (!ptr)              { debugf("GetLargeResourceRecord: Malformed SOA RDATA mname"); goto fail; }
2223							ptr = getDomainName(msg, ptr, end, &rdb->soa.rname);
2224							if (!ptr)              { debugf("GetLargeResourceRecord: Malformed SOA RDATA rname"); goto fail; }
2225							if (ptr + 0x14 != end) { debugf("GetLargeResourceRecord: Malformed SOA RDATA");       goto fail; }
2226							rdb->soa.serial  = (mDNSs32) ((mDNSs32)ptr[0x00] << 24 | (mDNSs32)ptr[0x01] << 16 | (mDNSs32)ptr[0x02] << 8 | ptr[0x03]);
2227							rdb->soa.refresh = (mDNSu32) ((mDNSu32)ptr[0x04] << 24 | (mDNSu32)ptr[0x05] << 16 | (mDNSu32)ptr[0x06] << 8 | ptr[0x07]);
2228							rdb->soa.retry   = (mDNSu32) ((mDNSu32)ptr[0x08] << 24 | (mDNSu32)ptr[0x09] << 16 | (mDNSu32)ptr[0x0A] << 8 | ptr[0x0B]);
2229							rdb->soa.expire  = (mDNSu32) ((mDNSu32)ptr[0x0C] << 24 | (mDNSu32)ptr[0x0D] << 16 | (mDNSu32)ptr[0x0E] << 8 | ptr[0x0F]);
2230							rdb->soa.min     = (mDNSu32) ((mDNSu32)ptr[0x10] << 24 | (mDNSu32)ptr[0x11] << 16 | (mDNSu32)ptr[0x12] << 8 | ptr[0x13]);
2231							break;
2232
2233		case kDNSType_NULL:
2234		case kDNSType_HINFO:
2235		case kDNSType_TSIG:
2236		case kDNSType_TXT:
2237		case kDNSType_X25:
2238		case kDNSType_ISDN:
2239		case kDNSType_LOC:
2240		case kDNSType_DHCID:if (pktrdlength > rr->resrec.rdata->MaxRDLength)
2241								{
2242								debugf("GetLargeResourceRecord: %s rdata size (%d) exceeds storage (%d)",
2243									DNSTypeName(rr->resrec.rrtype), pktrdlength, rr->resrec.rdata->MaxRDLength);
2244								goto fail;
2245								}
2246							rr->resrec.rdlength = pktrdlength;
2247							mDNSPlatformMemCopy(rdb->data, ptr, pktrdlength);
2248							break;
2249
2250		case kDNSType_MX:
2251		case kDNSType_AFSDB:
2252		case kDNSType_RT:
2253		case kDNSType_KX:	if (pktrdlength < 3) goto fail;	// Preference + domainname
2254							rdb->mx.preference = (mDNSu16)((mDNSu16)ptr[0] <<  8 | ptr[1]);
2255							ptr = getDomainName(msg, ptr+2, end, &rdb->mx.exchange);
2256							if (ptr != end) { debugf("GetLargeResourceRecord: Malformed MX name"); goto fail; }
2257							//debugf("%##s SRV %##s rdlen %d", rr->resrec.name.c, rdb->srv.target.c, pktrdlength);
2258							break;
2259
2260		case kDNSType_RP:	ptr = getDomainName(msg, ptr, end, &rdb->rp.mbox);	// Domainname + domainname
2261							if (!ptr)       { debugf("GetLargeResourceRecord: Malformed RP mbox"); goto fail; }
2262							ptr = getDomainName(msg, ptr, end, &rdb->rp.txt);
2263							if (ptr != end) { debugf("GetLargeResourceRecord: Malformed RP txt"); goto fail; }
2264							break;
2265
2266		case kDNSType_PX:	if (pktrdlength < 4) goto fail;	// Preference + domainname + domainname
2267							rdb->px.preference = (mDNSu16)((mDNSu16)ptr[0] <<  8 | ptr[1]);
2268							ptr = getDomainName(msg, ptr, end, &rdb->px.map822);
2269							if (!ptr)       { debugf("GetLargeResourceRecord: Malformed PX map822"); goto fail; }
2270							ptr = getDomainName(msg, ptr, end, &rdb->px.mapx400);
2271							if (ptr != end) { debugf("GetLargeResourceRecord: Malformed PX mapx400"); goto fail; }
2272							break;
2273
2274		case kDNSType_AAAA:	if (pktrdlength != sizeof(mDNSv6Addr)) goto fail;
2275							mDNSPlatformMemCopy(&rdb->ipv6, ptr, sizeof(rdb->ipv6));
2276							break;
2277
2278		case kDNSType_SRV:	if (pktrdlength < 7) goto fail;	// Priority + weight + port + domainname
2279							rdb->srv.priority = (mDNSu16)((mDNSu16)ptr[0] <<  8 | ptr[1]);
2280							rdb->srv.weight   = (mDNSu16)((mDNSu16)ptr[2] <<  8 | ptr[3]);
2281							rdb->srv.port.b[0] = ptr[4];
2282							rdb->srv.port.b[1] = ptr[5];
2283							ptr = getDomainName(msg, ptr+6, end, &rdb->srv.target);
2284							if (ptr != end) { debugf("GetLargeResourceRecord: Malformed SRV RDATA name"); goto fail; }
2285							//debugf("%##s SRV %##s rdlen %d", rr->resrec.name.c, rdb->srv.target.c, pktrdlength);
2286							break;
2287
2288		case kDNSType_OPT:	{
2289							rdataOPT *opt = rr->resrec.rdata->u.opt;
2290							rr->resrec.rdlength = 0;
2291							while (ptr < end && (mDNSu8 *)(opt+1) < &rr->resrec.rdata->u.data[MaximumRDSize])
2292								{
2293								const rdataOPT *const currentopt = opt;
2294								if (ptr + 4 > end) { LogInfo("GetLargeResourceRecord: OPT RDATA ptr + 4 > end"); goto fail; }
2295								opt->opt    = (mDNSu16)((mDNSu16)ptr[0] <<  8 | ptr[1]);
2296								opt->optlen = (mDNSu16)((mDNSu16)ptr[2] <<  8 | ptr[3]);
2297								ptr += 4;
2298								if (ptr + opt->optlen > end) { LogInfo("GetLargeResourceRecord: ptr + opt->optlen > end"); goto fail; }
2299								switch (opt->opt)
2300									{
2301									case kDNSOpt_LLQ:
2302										if (opt->optlen == DNSOpt_LLQData_Space - 4)
2303											{
2304											opt->u.llq.vers  = (mDNSu16)((mDNSu16)ptr[0] <<  8 | ptr[1]);
2305											opt->u.llq.llqOp = (mDNSu16)((mDNSu16)ptr[2] <<  8 | ptr[3]);
2306											opt->u.llq.err   = (mDNSu16)((mDNSu16)ptr[4] <<  8 | ptr[5]);
2307											mDNSPlatformMemCopy(opt->u.llq.id.b, ptr+6, 8);
2308											opt->u.llq.llqlease = (mDNSu32) ((mDNSu32)ptr[14] << 24 | (mDNSu32)ptr[15] << 16 | (mDNSu32)ptr[16] << 8 | ptr[17]);
2309											if (opt->u.llq.llqlease > 0x70000000UL / mDNSPlatformOneSecond)
2310												opt->u.llq.llqlease = 0x70000000UL / mDNSPlatformOneSecond;
2311											opt++;
2312											}
2313										break;
2314									case kDNSOpt_Lease:
2315										if (opt->optlen == DNSOpt_LeaseData_Space - 4)
2316											{
2317											opt->u.updatelease = (mDNSu32) ((mDNSu32)ptr[0] << 24 | (mDNSu32)ptr[1] << 16 | (mDNSu32)ptr[2] << 8 | ptr[3]);
2318											if (opt->u.updatelease > 0x70000000UL / mDNSPlatformOneSecond)
2319												opt->u.updatelease = 0x70000000UL / mDNSPlatformOneSecond;
2320											opt++;
2321											}
2322										break;
2323									case kDNSOpt_Owner:
2324										if (ValidOwnerLength(opt->optlen))
2325											{
2326											opt->u.owner.vers = ptr[0];
2327											opt->u.owner.seq  = ptr[1];
2328											mDNSPlatformMemCopy(opt->u.owner.HMAC.b, ptr+2, 6);		// 6-byte MAC address
2329											mDNSPlatformMemCopy(opt->u.owner.IMAC.b, ptr+2, 6);		// 6-byte MAC address
2330											opt->u.owner.password = zeroEthAddr;
2331											if (opt->optlen >= DNSOpt_OwnerData_ID_Wake_Space-4)
2332												{
2333												mDNSPlatformMemCopy(opt->u.owner.IMAC.b, ptr+8, 6);	// 6-byte MAC address
2334												// This mDNSPlatformMemCopy is safe because the ValidOwnerLength(opt->optlen) check above
2335												// ensures that opt->optlen is no more than DNSOpt_OwnerData_ID_Wake_PW6_Space - 4
2336												if (opt->optlen > DNSOpt_OwnerData_ID_Wake_Space-4)
2337													mDNSPlatformMemCopy(opt->u.owner.password.b, ptr+14, opt->optlen - (DNSOpt_OwnerData_ID_Wake_Space-4));
2338												}
2339											opt++;
2340											}
2341										break;
2342									}
2343								ptr += currentopt->optlen;
2344								}
2345							rr->resrec.rdlength = (mDNSu16)((mDNSu8*)opt - rr->resrec.rdata->u.data);
2346							if (ptr != end) { LogInfo("GetLargeResourceRecord: Malformed OptRdata"); goto fail; }
2347							break;
2348							}
2349
2350		case kDNSType_NSEC: {
2351							unsigned int i, j;
2352							domainname d;
2353							ptr = getDomainName(msg, ptr, end, &d);		// Ignored for our simplified use of NSEC synthetic records
2354							if (!ptr) { LogInfo("GetLargeResourceRecord: Malformed NSEC nextname"); goto fail; }
2355							mDNSPlatformMemZero(rdb->nsec.bitmap, sizeof(rdb->nsec.bitmap));
2356							if (ptr < end)
2357								{
2358								if (*ptr++ != 0) { debugf("GetLargeResourceRecord: We only handle block zero NSECs"); goto fail; }
2359								i = *ptr++;
2360								if (i > sizeof(rdataNSEC)) { debugf("GetLargeResourceRecord: invalid block length %d", i); goto fail; }
2361								for (j=0; j<i; j++) rdb->nsec.bitmap[j] = *ptr++;
2362								}
2363							if (ptr != end) { debugf("GetLargeResourceRecord: Malformed NSEC"); goto fail; }
2364							break;
2365							}
2366
2367		default:			if (pktrdlength > rr->resrec.rdata->MaxRDLength)
2368								{
2369								debugf("GetLargeResourceRecord: rdata %d (%s) size (%d) exceeds storage (%d)",
2370									rr->resrec.rrtype, DNSTypeName(rr->resrec.rrtype), pktrdlength, rr->resrec.rdata->MaxRDLength);
2371								goto fail;
2372								}
2373							debugf("GetLargeResourceRecord: Warning! Reading resource type %d (%s) as opaque data",
2374								rr->resrec.rrtype, DNSTypeName(rr->resrec.rrtype));
2375							// Note: Just because we don't understand the record type, that doesn't
2376							// mean we fail. The DNS protocol specifies rdlength, so we can
2377							// safely skip over unknown records and ignore them.
2378							// We also grab a binary copy of the rdata anyway, since the caller
2379							// might know how to interpret it even if we don't.
2380							rr->resrec.rdlength = pktrdlength;
2381							mDNSPlatformMemCopy(rdb->data, ptr, pktrdlength);
2382							break;
2383		}
2384
2385	SetNewRData(&rr->resrec, mDNSNULL, 0);		// Sets rdlength, rdestimate, rdatahash for us
2386
2387	// Success! Now fill in RecordType to show this record contains valid data
2388	rr->resrec.RecordType = RecordType;
2389	return(end);
2390
2391fail:
2392	// If we were unable to parse the rdata in this record, we indicate that by
2393	// returing a 'kDNSRecordTypePacketNegative' record with rdlength set to zero
2394	rr->resrec.RecordType = kDNSRecordTypePacketNegative;
2395	rr->resrec.rdlength   = 0;
2396	rr->resrec.rdestimate = 0;
2397	rr->resrec.rdatahash  = 0;
2398	return(end);
2399	}
2400
2401mDNSexport const mDNSu8 *skipQuestion(const DNSMessage *msg, const mDNSu8 *ptr, const mDNSu8 *end)
2402	{
2403	ptr = skipDomainName(msg, ptr, end);
2404	if (!ptr) { debugf("skipQuestion: Malformed domain name in DNS question section"); return(mDNSNULL); }
2405	if (ptr+4 > end) { debugf("skipQuestion: Malformed DNS question section -- no query type and class!"); return(mDNSNULL); }
2406	return(ptr+4);
2407	}
2408
2409mDNSexport const mDNSu8 *getQuestion(const DNSMessage *msg, const mDNSu8 *ptr, const mDNSu8 *end, const mDNSInterfaceID InterfaceID,
2410	DNSQuestion *question)
2411	{
2412	mDNSPlatformMemZero(question, sizeof(*question));
2413	question->InterfaceID = InterfaceID;
2414	if (!InterfaceID) question->TargetQID = onesID;	// In DNSQuestions we use TargetQID as the indicator of whether it's unicast or multicast
2415	ptr = getDomainName(msg, ptr, end, &question->qname);
2416	if (!ptr) { debugf("Malformed domain name in DNS question section"); return(mDNSNULL); }
2417	if (ptr+4 > end) { debugf("Malformed DNS question section -- no query type and class!"); return(mDNSNULL); }
2418
2419	question->qnamehash = DomainNameHashValue(&question->qname);
2420	question->qtype  = (mDNSu16)((mDNSu16)ptr[0] << 8 | ptr[1]);			// Get type
2421	question->qclass = (mDNSu16)((mDNSu16)ptr[2] << 8 | ptr[3]);			// and class
2422	return(ptr+4);
2423	}
2424
2425mDNSexport const mDNSu8 *LocateAnswers(const DNSMessage *const msg, const mDNSu8 *const end)
2426	{
2427	int i;
2428	const mDNSu8 *ptr = msg->data;
2429	for (i = 0; i < msg->h.numQuestions && ptr; i++) ptr = skipQuestion(msg, ptr, end);
2430	return(ptr);
2431	}
2432
2433mDNSexport const mDNSu8 *LocateAuthorities(const DNSMessage *const msg, const mDNSu8 *const end)
2434	{
2435	int i;
2436	const mDNSu8 *ptr = LocateAnswers(msg, end);
2437	for (i = 0; i < msg->h.numAnswers && ptr; i++) ptr = skipResourceRecord(msg, ptr, end);
2438	return(ptr);
2439	}
2440
2441mDNSexport const mDNSu8 *LocateAdditionals(const DNSMessage *const msg, const mDNSu8 *const end)
2442	{
2443	int i;
2444	const mDNSu8 *ptr = LocateAuthorities(msg, end);
2445	for (i = 0; i < msg->h.numAuthorities; i++) ptr = skipResourceRecord(msg, ptr, end);
2446	return (ptr);
2447	}
2448
2449mDNSexport const mDNSu8 *LocateOptRR(const DNSMessage *const msg, const mDNSu8 *const end, int minsize)
2450	{
2451	int i;
2452	const mDNSu8 *ptr = LocateAdditionals(msg, end);
2453
2454	// Locate the OPT record.
2455	// According to RFC 2671, "One OPT pseudo-RR can be added to the additional data section of either a request or a response."
2456	// This implies that there may be *at most* one OPT record per DNS message, in the Additional Section,
2457	// but not necessarily the *last* entry in the Additional Section.
2458	for (i = 0; ptr && i < msg->h.numAdditionals; i++)
2459		{
2460		if (ptr + DNSOpt_Header_Space + minsize <= end &&	// Make sure we have 11+minsize bytes of data
2461			ptr[0] == 0                                &&	// Name must be root label
2462			ptr[1] == (kDNSType_OPT >> 8  )            &&	// rrtype OPT
2463			ptr[2] == (kDNSType_OPT & 0xFF)            &&
2464			((mDNSu16)ptr[9] << 8 | (mDNSu16)ptr[10]) >= (mDNSu16)minsize)
2465			return(ptr);
2466		else
2467			ptr = skipResourceRecord(msg, ptr, end);
2468		}
2469	return(mDNSNULL);
2470	}
2471
2472// On success, GetLLQOptData returns pointer to storage within shared "m->rec";
2473// it is caller's responsibilty to clear m->rec.r.resrec.RecordType after use
2474// Note: An OPT RDataBody actually contains one or more variable-length rdataOPT objects packed together
2475// The code that currently calls this assumes there's only one, instead of iterating through the set
2476mDNSexport const rdataOPT *GetLLQOptData(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *const end)
2477	{
2478	const mDNSu8 *ptr = LocateOptRR(msg, end, DNSOpt_LLQData_Space);
2479	if (ptr)
2480		{
2481		ptr = GetLargeResourceRecord(m, msg, ptr, end, 0, kDNSRecordTypePacketAdd, &m->rec);
2482		if (ptr && m->rec.r.resrec.RecordType != kDNSRecordTypePacketNegative) return(&m->rec.r.resrec.rdata->u.opt[0]);
2483		}
2484	return(mDNSNULL);
2485	}
2486
2487// Get the lease life of records in a dynamic update
2488// returns 0 on error or if no lease present
2489mDNSexport mDNSu32 GetPktLease(mDNS *m, DNSMessage *msg, const mDNSu8 *end)
2490	{
2491	mDNSu32 result = 0;
2492	const mDNSu8 *ptr = LocateOptRR(msg, end, DNSOpt_LeaseData_Space);
2493	if (ptr) ptr = GetLargeResourceRecord(m, msg, ptr, end, 0, kDNSRecordTypePacketAdd, &m->rec);
2494	if (ptr && m->rec.r.resrec.rdlength >= DNSOpt_LeaseData_Space && m->rec.r.resrec.rdata->u.opt[0].opt == kDNSOpt_Lease)
2495		result = m->rec.r.resrec.rdata->u.opt[0].u.updatelease;
2496	m->rec.r.resrec.RecordType = 0;		// Clear RecordType to show we're not still using it
2497	return(result);
2498	}
2499
2500mDNSlocal const mDNSu8 *DumpRecords(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *ptr, const mDNSu8 *const end, int count, char *label)
2501	{
2502	int i;
2503	LogMsg("%2d %s", count, label);
2504	for (i = 0; i < count && ptr; i++)
2505		{
2506		// This puts a LargeCacheRecord on the stack instead of using the shared m->rec storage,
2507		// but since it's only used for debugging (and probably only on OS X, not on
2508		// embedded systems) putting a 9kB object on the stack isn't a big problem.
2509		LargeCacheRecord largecr;
2510		ptr = GetLargeResourceRecord(m, msg, ptr, end, mDNSInterface_Any, kDNSRecordTypePacketAns, &largecr);
2511		if (ptr) LogMsg("%2d TTL%8d %s", i, largecr.r.resrec.rroriginalttl, CRDisplayString(m, &largecr.r));
2512		}
2513	if (!ptr) LogMsg("ERROR: Premature end of packet data");
2514	return(ptr);
2515	}
2516
2517#define DNS_OP_Name(X) (                              \
2518	(X) == kDNSFlag0_OP_StdQuery ? ""         :       \
2519	(X) == kDNSFlag0_OP_Iquery   ? "Iquery "  :       \
2520	(X) == kDNSFlag0_OP_Status   ? "Status "  :       \
2521	(X) == kDNSFlag0_OP_Unused3  ? "Unused3 " :       \
2522	(X) == kDNSFlag0_OP_Notify   ? "Notify "  :       \
2523	(X) == kDNSFlag0_OP_Update   ? "Update "  : "?? " )
2524
2525#define DNS_RC_Name(X) (                             \
2526	(X) == kDNSFlag1_RC_NoErr    ? "NoErr"    :      \
2527	(X) == kDNSFlag1_RC_FormErr  ? "FormErr"  :      \
2528	(X) == kDNSFlag1_RC_ServFail ? "ServFail" :      \
2529	(X) == kDNSFlag1_RC_NXDomain ? "NXDomain" :      \
2530	(X) == kDNSFlag1_RC_NotImpl  ? "NotImpl"  :      \
2531	(X) == kDNSFlag1_RC_Refused  ? "Refused"  :      \
2532	(X) == kDNSFlag1_RC_YXDomain ? "YXDomain" :      \
2533	(X) == kDNSFlag1_RC_YXRRSet  ? "YXRRSet"  :      \
2534	(X) == kDNSFlag1_RC_NXRRSet  ? "NXRRSet"  :      \
2535	(X) == kDNSFlag1_RC_NotAuth  ? "NotAuth"  :      \
2536	(X) == kDNSFlag1_RC_NotZone  ? "NotZone"  : "??" )
2537
2538// Note: DumpPacket expects the packet header fields in host byte order, not network byte order
2539mDNSexport void DumpPacket(mDNS *const m, mStatus status, mDNSBool sent, char *transport,
2540	const mDNSAddr *srcaddr, mDNSIPPort srcport,
2541	const mDNSAddr *dstaddr, mDNSIPPort dstport, const DNSMessage *const msg, const mDNSu8 *const end)
2542	{
2543	mDNSBool IsUpdate = ((msg->h.flags.b[0] & kDNSFlag0_OP_Mask) == kDNSFlag0_OP_Update);
2544	const mDNSu8 *ptr = msg->data;
2545	int i;
2546	DNSQuestion q;
2547	char tbuffer[64], sbuffer[64], dbuffer[64] = "";
2548	if (!status) tbuffer[mDNS_snprintf(tbuffer, sizeof(tbuffer), sent ? "Sent" : "Received"                        )] = 0;
2549	else         tbuffer[mDNS_snprintf(tbuffer, sizeof(tbuffer), "ERROR %d %sing", status, sent ? "Send" : "Receiv")] = 0;
2550	if (sent) sbuffer[mDNS_snprintf(sbuffer, sizeof(sbuffer), "port "        )] = 0;
2551	else      sbuffer[mDNS_snprintf(sbuffer, sizeof(sbuffer), "%#a:", srcaddr)] = 0;
2552	if (dstaddr || !mDNSIPPortIsZero(dstport))
2553		dbuffer[mDNS_snprintf(dbuffer, sizeof(dbuffer), " to %#a:%d", dstaddr, mDNSVal16(dstport))] = 0;
2554
2555	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 --",
2556		tbuffer, transport,
2557		DNS_OP_Name(msg->h.flags.b[0] & kDNSFlag0_OP_Mask),
2558		msg->h.flags.b[0] & kDNSFlag0_QR_Response ? "Response" : "Query",
2559		msg->h.flags.b[0], msg->h.flags.b[1],
2560		DNS_RC_Name(msg->h.flags.b[1] & kDNSFlag1_RC_Mask),
2561		msg->h.flags.b[1] & kDNSFlag1_RC_Mask,
2562		msg->h.flags.b[0] & kDNSFlag0_AA ? "AA " : "",
2563		msg->h.flags.b[0] & kDNSFlag0_TC ? "TC " : "",
2564		msg->h.flags.b[0] & kDNSFlag0_RD ? "RD " : "",
2565		msg->h.flags.b[1] & kDNSFlag1_RA ? "RA " : "",
2566		msg->h.flags.b[1] & kDNSFlag1_AD ? "AD " : "",
2567		msg->h.flags.b[1] & kDNSFlag1_CD ? "CD " : "",
2568		mDNSVal16(msg->h.id),
2569		end - msg->data,
2570		sbuffer, mDNSVal16(srcport), dbuffer,
2571		(msg->h.flags.b[0] & kDNSFlag0_TC) ? " (truncated)" : ""
2572		);
2573
2574	LogMsg("%2d %s", msg->h.numQuestions, IsUpdate ? "Zone" : "Questions");
2575	for (i = 0; i < msg->h.numQuestions && ptr; i++)
2576		{
2577		ptr = getQuestion(msg, ptr, end, mDNSInterface_Any, &q);
2578		if (ptr) LogMsg("%2d %##s %s", i, q.qname.c, DNSTypeName(q.qtype));
2579		}
2580	ptr = DumpRecords(m, msg, ptr, end, msg->h.numAnswers,     IsUpdate ? "Prerequisites" : "Answers");
2581	ptr = DumpRecords(m, msg, ptr, end, msg->h.numAuthorities, IsUpdate ? "Updates"       : "Authorities");
2582	ptr = DumpRecords(m, msg, ptr, end, msg->h.numAdditionals, "Additionals");
2583	LogMsg("--------------");
2584	}
2585
2586// ***************************************************************************
2587#if COMPILER_LIKES_PRAGMA_MARK
2588#pragma mark -
2589#pragma mark - Packet Sending Functions
2590#endif
2591
2592// Stub definition of TCPSocket_struct so we can access flags field. (Rest of TCPSocket_struct is platform-dependent.)
2593struct TCPSocket_struct { TCPSocketFlags flags; /* ... */ };
2594
2595struct UDPSocket_struct
2596	{
2597	mDNSIPPort port; // MUST BE FIRST FIELD -- mDNSCoreReceive expects every UDPSocket_struct to begin with mDNSIPPort port
2598	};
2599
2600// Note: When we sign a DNS message using DNSDigest_SignMessage(), the current real-time clock value is used, which
2601// is why we generally defer signing until we send the message, to ensure the signature is as fresh as possible.
2602mDNSexport mStatus mDNSSendDNSMessage(mDNS *const m, DNSMessage *const msg, mDNSu8 *end,
2603    mDNSInterfaceID InterfaceID, UDPSocket *src, const mDNSAddr *dst, mDNSIPPort dstport, TCPSocket *sock, DomainAuthInfo *authInfo)
2604	{
2605	mStatus status = mStatus_NoError;
2606	const mDNSu16 numAdditionals = msg->h.numAdditionals;
2607	mDNSu8 *newend;
2608	mDNSu8 *limit = msg->data + AbsoluteMaxDNSMessageData;
2609
2610	// Zero-length message data is okay (e.g. for a DNS Update ack, where all we need is an ID and an error code
2611	if (end < msg->data || end - msg->data > AbsoluteMaxDNSMessageData)
2612		{
2613		LogMsg("mDNSSendDNSMessage: invalid message %p %p %d", msg->data, end, end - msg->data);
2614		return mStatus_BadParamErr;
2615		}
2616
2617	newend = putHINFO(m, msg, end, authInfo, limit);
2618	if (!newend) LogMsg("mDNSSendDNSMessage: putHINFO failed msg %p end %p, limit %p", msg->data, end, limit); // Not fatal
2619	else end = newend;
2620
2621	// Put all the integer values in IETF byte-order (MSB first, LSB second)
2622	SwapDNSHeaderBytes(msg);
2623
2624	if (authInfo) DNSDigest_SignMessage(msg, &end, authInfo, 0);	// DNSDigest_SignMessage operates on message in network byte order
2625	if (!end) { LogMsg("mDNSSendDNSMessage: DNSDigest_SignMessage failed"); status = mStatus_NoMemoryErr; }
2626	else
2627		{
2628		// Send the packet on the wire
2629		if (!sock)
2630			status = mDNSPlatformSendUDP(m, msg, end, InterfaceID, src, dst, dstport);
2631		else
2632			{
2633			mDNSu16 msglen = (mDNSu16)(end - (mDNSu8 *)msg);
2634			mDNSu8 lenbuf[2] = { (mDNSu8)(msglen >> 8), (mDNSu8)(msglen & 0xFF) };
2635			long nsent = mDNSPlatformWriteTCP(sock, (char*)lenbuf, 2);		// Should do scatter/gather here -- this is probably going out as two packets
2636			if (nsent != 2) { LogMsg("mDNSSendDNSMessage: write msg length failed %d/%d", nsent, 2); status = mStatus_ConnFailed; }
2637			else
2638				{
2639				nsent = mDNSPlatformWriteTCP(sock, (char *)msg, msglen);
2640				if (nsent != msglen) { LogMsg("mDNSSendDNSMessage: write msg body failed %d/%d", nsent, msglen); status = mStatus_ConnFailed; }
2641				}
2642			}
2643		}
2644
2645	// Swap the integer values back the way they were (remember that numAdditionals may have been changed by putHINFO and/or SignMessage)
2646	SwapDNSHeaderBytes(msg);
2647
2648	// Dump the packet with the HINFO and TSIG
2649	if (mDNS_PacketLoggingEnabled && !mDNSOpaque16IsZero(msg->h.id))
2650		DumpPacket(m, status, mDNStrue, sock && (sock->flags & kTCPSocketFlags_UseTLS) ? "TLS" : sock ? "TCP" : "UDP", mDNSNULL, src ? src->port : MulticastDNSPort, dst, dstport, msg, end);
2651
2652	// put the number of additionals back the way it was
2653	msg->h.numAdditionals = numAdditionals;
2654
2655	return(status);
2656	}
2657
2658// ***************************************************************************
2659#if COMPILER_LIKES_PRAGMA_MARK
2660#pragma mark -
2661#pragma mark - RR List Management & Task Management
2662#endif
2663
2664mDNSexport void mDNS_Lock_(mDNS *const m, const char * const functionname)
2665	{
2666	// MUST grab the platform lock FIRST!
2667	mDNSPlatformLock(m);
2668
2669	// Normally, mDNS_reentrancy is zero and so is mDNS_busy
2670	// However, when we call a client callback mDNS_busy is one, and we increment mDNS_reentrancy too
2671	// If that client callback does mDNS API calls, mDNS_reentrancy and mDNS_busy will both be one
2672	// If mDNS_busy != mDNS_reentrancy that's a bad sign
2673	if (m->mDNS_busy != m->mDNS_reentrancy)
2674		{
2675		LogMsg("%s: mDNS_Lock: Locking failure! mDNS_busy (%ld) != mDNS_reentrancy (%ld)", functionname, m->mDNS_busy, m->mDNS_reentrancy);
2676#if ForceAlerts
2677		*(long*)0 = 0;
2678#endif
2679		}
2680
2681	// If this is an initial entry into the mDNSCore code, set m->timenow
2682	// else, if this is a re-entrant entry into the mDNSCore code, m->timenow should already be set
2683	if (m->mDNS_busy == 0)
2684		{
2685		if (m->timenow)
2686			LogMsg("%s: mDNS_Lock: m->timenow already set (%ld/%ld)", functionname, m->timenow, mDNS_TimeNow_NoLock(m));
2687		m->timenow = mDNS_TimeNow_NoLock(m);
2688		if (m->timenow == 0) m->timenow = 1;
2689		}
2690	else if (m->timenow == 0)
2691		{
2692		LogMsg("%s: mDNS_Lock: m->mDNS_busy is %ld but m->timenow not set", functionname, m->mDNS_busy);
2693		m->timenow = mDNS_TimeNow_NoLock(m);
2694		if (m->timenow == 0) m->timenow = 1;
2695		}
2696
2697	if (m->timenow_last - m->timenow > 0)
2698		{
2699		m->timenow_adjust += m->timenow_last - m->timenow;
2700		LogMsg("%s: mDNSPlatformRawTime went backwards by %ld ticks; setting correction factor to %ld", functionname, m->timenow_last - m->timenow, m->timenow_adjust);
2701		m->timenow = m->timenow_last;
2702		}
2703	m->timenow_last = m->timenow;
2704
2705	// Increment mDNS_busy so we'll recognise re-entrant calls
2706	m->mDNS_busy++;
2707	}
2708
2709mDNSlocal AuthRecord *AnyLocalRecordReady(const mDNS *const m)
2710	{
2711	AuthRecord *rr;
2712	for (rr = m->NewLocalRecords; rr; rr = rr->next)
2713		if (LocalRecordReady(rr)) return rr;
2714	return mDNSNULL;
2715	}
2716
2717mDNSlocal mDNSs32 GetNextScheduledEvent(const mDNS *const m)
2718	{
2719	mDNSs32 e = m->timenow + 0x78000000;
2720	if (m->mDNSPlatformStatus != mStatus_NoError) return(e);
2721	if (m->NewQuestions)
2722		{
2723		if (m->NewQuestions->DelayAnswering) e = m->NewQuestions->DelayAnswering;
2724		else return(m->timenow);
2725		}
2726	if (m->NewLocalOnlyQuestions)                     return(m->timenow);
2727	if (m->NewLocalRecords && AnyLocalRecordReady(m)) return(m->timenow);
2728	if (m->NewLocalOnlyRecords)                       return(m->timenow);
2729	if (m->SPSProxyListChanged)                       return(m->timenow);
2730	if (m->LocalRemoveEvents)                         return(m->timenow);
2731
2732#ifndef UNICAST_DISABLED
2733	if (e - m->NextuDNSEvent         > 0) e = m->NextuDNSEvent;
2734	if (e - m->NextScheduledNATOp    > 0) e = m->NextScheduledNATOp;
2735	if (m->NextSRVUpdate && e - m->NextSRVUpdate > 0) e = m->NextSRVUpdate;
2736#endif
2737
2738	if (e - m->NextCacheCheck        > 0) e = m->NextCacheCheck;
2739	if (e - m->NextScheduledSPS      > 0) e = m->NextScheduledSPS;
2740	// NextScheduledSPRetry only valid when DelaySleep not set
2741	if (!m->DelaySleep && m->SleepLimit && e - m->NextScheduledSPRetry > 0) e = m->NextScheduledSPRetry;
2742	if (m->DelaySleep && e - m->DelaySleep > 0) e = m->DelaySleep;
2743
2744	if (m->SuppressSending)
2745		{
2746		if (e - m->SuppressSending       > 0) e = m->SuppressSending;
2747		}
2748	else
2749		{
2750		if (e - m->NextScheduledQuery    > 0) e = m->NextScheduledQuery;
2751		if (e - m->NextScheduledProbe    > 0) e = m->NextScheduledProbe;
2752		if (e - m->NextScheduledResponse > 0) e = m->NextScheduledResponse;
2753		}
2754	if (e - m->NextScheduledStopTime > 0) e = m->NextScheduledStopTime;
2755	return(e);
2756	}
2757
2758mDNSexport void ShowTaskSchedulingError(mDNS *const m)
2759	{
2760	AuthRecord *rr;
2761	mDNS_Lock(m);
2762
2763	LogMsg("Task Scheduling Error: Continuously busy for more than a second");
2764
2765	// Note: To accurately diagnose *why* we're busy, the debugging code here needs to mirror the logic in GetNextScheduledEvent above
2766
2767	if (m->NewQuestions && (!m->NewQuestions->DelayAnswering || m->timenow - m->NewQuestions->DelayAnswering >= 0))
2768		LogMsg("Task Scheduling Error: NewQuestion %##s (%s)",
2769			m->NewQuestions->qname.c, DNSTypeName(m->NewQuestions->qtype));
2770
2771	if (m->NewLocalOnlyQuestions)
2772		LogMsg("Task Scheduling Error: NewLocalOnlyQuestions %##s (%s)",
2773			m->NewLocalOnlyQuestions->qname.c, DNSTypeName(m->NewLocalOnlyQuestions->qtype));
2774
2775	if (m->NewLocalRecords)
2776		{
2777		rr = AnyLocalRecordReady(m);
2778		if (rr) LogMsg("Task Scheduling Error: NewLocalRecords %s", ARDisplayString(m, rr));
2779		}
2780
2781	if (m->NewLocalOnlyRecords) LogMsg("Task Scheduling Error: NewLocalOnlyRecords");
2782
2783	if (m->SPSProxyListChanged) LogMsg("Task Scheduling Error: SPSProxyListChanged");
2784	if (m->LocalRemoveEvents)   LogMsg("Task Scheduling Error: LocalRemoveEvents");
2785
2786	if (m->timenow - m->NextScheduledEvent    >= 0)
2787		LogMsg("Task Scheduling Error: m->NextScheduledEvent %d",    m->timenow - m->NextScheduledEvent);
2788
2789#ifndef UNICAST_DISABLED
2790	if (m->timenow - m->NextuDNSEvent         >= 0)
2791		LogMsg("Task Scheduling Error: m->NextuDNSEvent %d",         m->timenow - m->NextuDNSEvent);
2792	if (m->timenow - m->NextScheduledNATOp    >= 0)
2793		LogMsg("Task Scheduling Error: m->NextScheduledNATOp %d",    m->timenow - m->NextScheduledNATOp);
2794	if (m->NextSRVUpdate && m->timenow - m->NextSRVUpdate >= 0)
2795		LogMsg("Task Scheduling Error: m->NextSRVUpdate %d",         m->timenow - m->NextSRVUpdate);
2796#endif
2797
2798	if (m->timenow - m->NextCacheCheck        >= 0)
2799		LogMsg("Task Scheduling Error: m->NextCacheCheck %d",        m->timenow - m->NextCacheCheck);
2800	if (m->timenow - m->NextScheduledSPS      >= 0)
2801		LogMsg("Task Scheduling Error: m->NextScheduledSPS %d",      m->timenow - m->NextScheduledSPS);
2802	if (!m->DelaySleep && m->SleepLimit && m->timenow - m->NextScheduledSPRetry >= 0)
2803		LogMsg("Task Scheduling Error: m->NextScheduledSPRetry %d",  m->timenow - m->NextScheduledSPRetry);
2804	if (m->DelaySleep && m->timenow - m->DelaySleep >= 0)
2805		LogMsg("Task Scheduling Error: m->DelaySleep %d",            m->timenow - m->DelaySleep);
2806
2807	if (m->SuppressSending && m->timenow - m->SuppressSending >= 0)
2808		LogMsg("Task Scheduling Error: m->SuppressSending %d",       m->timenow - m->SuppressSending);
2809	if (m->timenow - m->NextScheduledQuery    >= 0)
2810		LogMsg("Task Scheduling Error: m->NextScheduledQuery %d",    m->timenow - m->NextScheduledQuery);
2811	if (m->timenow - m->NextScheduledProbe    >= 0)
2812		LogMsg("Task Scheduling Error: m->NextScheduledProbe %d",    m->timenow - m->NextScheduledProbe);
2813	if (m->timenow - m->NextScheduledResponse >= 0)
2814		LogMsg("Task Scheduling Error: m->NextScheduledResponse %d", m->timenow - m->NextScheduledResponse);
2815
2816	mDNS_Unlock(m);
2817	}
2818
2819mDNSexport void mDNS_Unlock_(mDNS *const m, const char * const functionname)
2820	{
2821	// Decrement mDNS_busy
2822	m->mDNS_busy--;
2823
2824	// Check for locking failures
2825	if (m->mDNS_busy != m->mDNS_reentrancy)
2826		{
2827		LogMsg("%s: mDNS_Unlock: Locking failure! mDNS_busy (%ld) != mDNS_reentrancy (%ld)", functionname, m->mDNS_busy, m->mDNS_reentrancy);
2828#if ForceAlerts
2829		*(long*)0 = 0;
2830#endif
2831		}
2832
2833	// If this is a final exit from the mDNSCore code, set m->NextScheduledEvent and clear m->timenow
2834	if (m->mDNS_busy == 0)
2835		{
2836		m->NextScheduledEvent = GetNextScheduledEvent(m);
2837		if (m->timenow == 0) LogMsg("%s: mDNS_Unlock: ERROR! m->timenow aready zero", functionname);
2838		m->timenow = 0;
2839		}
2840
2841	// MUST release the platform lock LAST!
2842	mDNSPlatformUnlock(m);
2843	}
2844
2845// ***************************************************************************
2846#if COMPILER_LIKES_PRAGMA_MARK
2847#pragma mark -
2848#pragma mark - Specialized mDNS version of vsnprintf
2849#endif
2850
2851static const struct mDNSprintf_format
2852	{
2853	unsigned      leftJustify : 1;
2854	unsigned      forceSign : 1;
2855	unsigned      zeroPad : 1;
2856	unsigned      havePrecision : 1;
2857	unsigned      hSize : 1;
2858	unsigned      lSize : 1;
2859	char          altForm;
2860	char          sign;		// +, - or space
2861	unsigned int  fieldWidth;
2862	unsigned int  precision;
2863	} mDNSprintf_format_default = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
2864
2865mDNSexport mDNSu32 mDNS_vsnprintf(char *sbuffer, mDNSu32 buflen, const char *fmt, va_list arg)
2866	{
2867	mDNSu32 nwritten = 0;
2868	int c;
2869	if (buflen == 0) return(0);
2870	buflen--;		// Pre-reserve one space in the buffer for the terminating null
2871	if (buflen == 0) goto exit;
2872
2873	for (c = *fmt; c != 0; c = *++fmt)
2874		{
2875		if (c != '%')
2876			{
2877			*sbuffer++ = (char)c;
2878			if (++nwritten >= buflen) goto exit;
2879			}
2880		else
2881			{
2882			unsigned int i=0, j;
2883			// The mDNS Vsprintf Argument Conversion Buffer is used as a temporary holding area for
2884			// generating decimal numbers, hexdecimal numbers, IP addresses, domain name strings, etc.
2885			// The size needs to be enough for a 256-byte domain name plus some error text.
2886			#define mDNS_VACB_Size 300
2887			char mDNS_VACB[mDNS_VACB_Size];
2888			#define mDNS_VACB_Lim (&mDNS_VACB[mDNS_VACB_Size])
2889			#define mDNS_VACB_Remain(s) ((mDNSu32)(mDNS_VACB_Lim - s))
2890			char *s = mDNS_VACB_Lim, *digits;
2891			struct mDNSprintf_format F = mDNSprintf_format_default;
2892
2893			while (1)	//  decode flags
2894				{
2895				c = *++fmt;
2896				if      (c == '-') F.leftJustify = 1;
2897				else if (c == '+') F.forceSign = 1;
2898				else if (c == ' ') F.sign = ' ';
2899				else if (c == '#') F.altForm++;
2900				else if (c == '0') F.zeroPad = 1;
2901				else break;
2902				}
2903
2904			if (c == '*')	//  decode field width
2905				{
2906				int f = va_arg(arg, int);
2907				if (f < 0) { f = -f; F.leftJustify = 1; }
2908				F.fieldWidth = (unsigned int)f;
2909				c = *++fmt;
2910				}
2911			else
2912				{
2913				for (; c >= '0' && c <= '9'; c = *++fmt)
2914					F.fieldWidth = (10 * F.fieldWidth) + (c - '0');
2915				}
2916
2917			if (c == '.')	//  decode precision
2918				{
2919				if ((c = *++fmt) == '*')
2920					{ F.precision = va_arg(arg, unsigned int); c = *++fmt; }
2921				else for (; c >= '0' && c <= '9'; c = *++fmt)
2922						F.precision = (10 * F.precision) + (c - '0');
2923				F.havePrecision = 1;
2924				}
2925
2926			if (F.leftJustify) F.zeroPad = 0;
2927
2928			conv:
2929			switch (c)	//  perform appropriate conversion
2930				{
2931				unsigned long n;
2932				case 'h' :	F.hSize = 1; c = *++fmt; goto conv;
2933				case 'l' :	// fall through
2934				case 'L' :	F.lSize = 1; c = *++fmt; goto conv;
2935				case 'd' :
2936				case 'i' :	if (F.lSize) n = (unsigned long)va_arg(arg, long);
2937							else n = (unsigned long)va_arg(arg, int);
2938							if (F.hSize) n = (short) n;
2939							if ((long) n < 0) { n = (unsigned long)-(long)n; F.sign = '-'; }
2940							else if (F.forceSign) F.sign = '+';
2941							goto decimal;
2942				case 'u' :	if (F.lSize) n = va_arg(arg, unsigned long);
2943							else n = va_arg(arg, unsigned int);
2944							if (F.hSize) n = (unsigned short) n;
2945							F.sign = 0;
2946							goto decimal;
2947				decimal:	if (!F.havePrecision)
2948								{
2949								if (F.zeroPad)
2950									{
2951									F.precision = F.fieldWidth;
2952									if (F.sign) --F.precision;
2953									}
2954								if (F.precision < 1) F.precision = 1;
2955								}
2956							if (F.precision > mDNS_VACB_Size - 1)
2957								F.precision = mDNS_VACB_Size - 1;
2958							for (i = 0; n; n /= 10, i++) *--s = (char)(n % 10 + '0');
2959							for (; i < F.precision; i++) *--s = '0';
2960							if (F.sign) { *--s = F.sign; i++; }
2961							break;
2962
2963				case 'o' :	if (F.lSize) n = va_arg(arg, unsigned long);
2964							else n = va_arg(arg, unsigned int);
2965							if (F.hSize) n = (unsigned short) n;
2966							if (!F.havePrecision)
2967								{
2968								if (F.zeroPad) F.precision = F.fieldWidth;
2969								if (F.precision < 1) F.precision = 1;
2970								}
2971							if (F.precision > mDNS_VACB_Size - 1)
2972								F.precision = mDNS_VACB_Size - 1;
2973							for (i = 0; n; n /= 8, i++) *--s = (char)(n % 8 + '0');
2974							if (F.altForm && i && *s != '0') { *--s = '0'; i++; }
2975							for (; i < F.precision; i++) *--s = '0';
2976							break;
2977
2978				case 'a' :	{
2979							unsigned char *a = va_arg(arg, unsigned char *);
2980							if (!a) { static char emsg[] = "<<NULL>>"; s = emsg; i = sizeof(emsg)-1; }
2981							else
2982								{
2983								s = mDNS_VACB;	// Adjust s to point to the start of the buffer, not the end
2984								if (F.altForm)
2985									{
2986									mDNSAddr *ip = (mDNSAddr*)a;
2987									switch (ip->type)
2988										{
2989										case mDNSAddrType_IPv4: F.precision =  4; a = (unsigned char *)&ip->ip.v4; break;
2990										case mDNSAddrType_IPv6: F.precision = 16; a = (unsigned char *)&ip->ip.v6; break;
2991										default:                F.precision =  0; break;
2992										}
2993									}
2994								if (F.altForm && !F.precision)
2995									i = mDNS_snprintf(mDNS_VACB, sizeof(mDNS_VACB), "«ZERO ADDRESS»");
2996								else switch (F.precision)
2997									{
2998									case  4: i = mDNS_snprintf(mDNS_VACB, sizeof(mDNS_VACB), "%d.%d.%d.%d",
2999														a[0], a[1], a[2], a[3]); break;
3000									case  6: i = mDNS_snprintf(mDNS_VACB, sizeof(mDNS_VACB), "%02X:%02X:%02X:%02X:%02X:%02X",
3001														a[0], a[1], a[2], a[3], a[4], a[5]); break;
3002									case 16: i = mDNS_snprintf(mDNS_VACB, sizeof(mDNS_VACB),
3003														"%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X",
3004														a[0x0], a[0x1], a[0x2], a[0x3], a[0x4], a[0x5], a[0x6], a[0x7],
3005														a[0x8], a[0x9], a[0xA], a[0xB], a[0xC], a[0xD], a[0xE], a[0xF]); break;
3006									default: i = mDNS_snprintf(mDNS_VACB, sizeof(mDNS_VACB), "%s", "<< ERROR: Must specify"
3007														" address size (i.e. %.4a=IPv4, %.6a=Ethernet, %.16a=IPv6) >>"); break;
3008									}
3009								}
3010							}
3011							break;
3012
3013				case 'p' :	F.havePrecision = F.lSize = 1;
3014							F.precision = sizeof(void*) * 2;	// 8 characters on 32-bit; 16 characters on 64-bit
3015				case 'X' :	digits = "0123456789ABCDEF";
3016							goto hexadecimal;
3017				case 'x' :	digits = "0123456789abcdef";
3018				hexadecimal:if (F.lSize) n = va_arg(arg, unsigned long);
3019							else n = va_arg(arg, unsigned int);
3020							if (F.hSize) n = (unsigned short) n;
3021							if (!F.havePrecision)
3022								{
3023								if (F.zeroPad)
3024									{
3025									F.precision = F.fieldWidth;
3026									if (F.altForm) F.precision -= 2;
3027									}
3028								if (F.precision < 1) F.precision = 1;
3029								}
3030							if (F.precision > mDNS_VACB_Size - 1)
3031								F.precision = mDNS_VACB_Size - 1;
3032							for (i = 0; n; n /= 16, i++) *--s = digits[n % 16];
3033							for (; i < F.precision; i++) *--s = '0';
3034							if (F.altForm) { *--s = (char)c; *--s = '0'; i += 2; }
3035							break;
3036
3037				case 'c' :	*--s = (char)va_arg(arg, int); i = 1; break;
3038
3039				case 's' :	s = va_arg(arg, char *);
3040							if (!s) { static char emsg[] = "<<NULL>>"; s = emsg; i = sizeof(emsg)-1; }
3041							else switch (F.altForm)
3042								{
3043								case 0: i=0;
3044										if (!F.havePrecision)				// C string
3045											while (s[i]) i++;
3046										else
3047											{
3048											while ((i < F.precision) && s[i]) i++;
3049											// Make sure we don't truncate in the middle of a UTF-8 character
3050											// If last character we got was any kind of UTF-8 multi-byte character,
3051											// then see if we have to back up.
3052											// This is not as easy as the similar checks below, because
3053											// here we can't assume it's safe to examine the *next* byte, so we
3054											// have to confine ourselves to working only backwards in the string.
3055											j = i;		// Record where we got to
3056											// Now, back up until we find first non-continuation-char
3057											while (i>0 && (s[i-1] & 0xC0) == 0x80) i--;
3058											// Now s[i-1] is the first non-continuation-char
3059											// and (j-i) is the number of continuation-chars we found
3060											if (i>0 && (s[i-1] & 0xC0) == 0xC0)	// If we found a start-char
3061												{
3062												i--;		// Tentatively eliminate this start-char as well
3063												// Now (j-i) is the number of characters we're considering eliminating.
3064												// To be legal UTF-8, the start-char must contain (j-i) one-bits,
3065												// followed by a zero bit. If we shift it right by (7-(j-i)) bits
3066												// (with sign extension) then the result has to be 0xFE.
3067												// If this is right, then we reinstate the tentatively eliminated bytes.
3068												if (((j-i) < 7) && (((s[i] >> (7-(j-i))) & 0xFF) == 0xFE)) i = j;
3069												}
3070											}
3071										break;
3072								case 1: i = (unsigned char) *s++; break;	// Pascal string
3073								case 2: {									// DNS label-sequence name
3074										unsigned char *a = (unsigned char *)s;
3075										s = mDNS_VACB;	// Adjust s to point to the start of the buffer, not the end
3076										if (*a == 0) *s++ = '.';	// Special case for root DNS name
3077										while (*a)
3078											{
3079											char buf[63*4+1];
3080											if (*a > 63)
3081												{ s += mDNS_snprintf(s, mDNS_VACB_Remain(s), "<<INVALID LABEL LENGTH %u>>", *a); break; }
3082											if (s + *a >= &mDNS_VACB[254])
3083												{ s += mDNS_snprintf(s, mDNS_VACB_Remain(s), "<<NAME TOO LONG>>"); break; }
3084											// Need to use ConvertDomainLabelToCString to do proper escaping here,
3085											// so it's clear what's a literal dot and what's a label separator
3086											ConvertDomainLabelToCString((domainlabel*)a, buf);
3087											s += mDNS_snprintf(s, mDNS_VACB_Remain(s), "%s.", buf);
3088											a += 1 + *a;
3089											}
3090										i = (mDNSu32)(s - mDNS_VACB);
3091										s = mDNS_VACB;	// Reset s back to the start of the buffer
3092										break;
3093										}
3094								}
3095							// Make sure we don't truncate in the middle of a UTF-8 character (see similar comment below)
3096							if (F.havePrecision && i > F.precision)
3097								{ i = F.precision; while (i>0 && (s[i] & 0xC0) == 0x80) i--; }
3098							break;
3099
3100				case 'n' :	s = va_arg(arg, char *);
3101							if      (F.hSize) * (short *) s = (short)nwritten;
3102							else if (F.lSize) * (long  *) s = (long)nwritten;
3103							else              * (int   *) s = (int)nwritten;
3104							continue;
3105
3106				default:	s = mDNS_VACB;
3107							i = mDNS_snprintf(mDNS_VACB, sizeof(mDNS_VACB), "<<UNKNOWN FORMAT CONVERSION CODE %%%c>>", c);
3108
3109				case '%' :	*sbuffer++ = (char)c;
3110							if (++nwritten >= buflen) goto exit;
3111							break;
3112				}
3113
3114			if (i < F.fieldWidth && !F.leftJustify)			// Pad on the left
3115				do	{
3116					*sbuffer++ = ' ';
3117					if (++nwritten >= buflen) goto exit;
3118					} while (i < --F.fieldWidth);
3119
3120			// Make sure we don't truncate in the middle of a UTF-8 character.
3121			// Note: s[i] is the first eliminated character; i.e. the next character *after* the last character of the
3122			// allowed output. If s[i] is a UTF-8 continuation character, then we've cut a unicode character in half,
3123			// so back up 'i' until s[i] is no longer a UTF-8 continuation character. (if the input was proprly
3124			// formed, s[i] will now be the UTF-8 start character of the multi-byte character we just eliminated).
3125			if (i > buflen - nwritten)
3126				{ i = buflen - nwritten; while (i>0 && (s[i] & 0xC0) == 0x80) i--; }
3127			for (j=0; j<i; j++) *sbuffer++ = *s++;			// Write the converted result
3128			nwritten += i;
3129			if (nwritten >= buflen) goto exit;
3130
3131			for (; i < F.fieldWidth; i++)					// Pad on the right
3132				{
3133				*sbuffer++ = ' ';
3134				if (++nwritten >= buflen) goto exit;
3135				}
3136			}
3137		}
3138	exit:
3139	*sbuffer++ = 0;
3140	return(nwritten);
3141	}
3142
3143mDNSexport mDNSu32 mDNS_snprintf(char *sbuffer, mDNSu32 buflen, const char *fmt, ...)
3144	{
3145	mDNSu32 length;
3146
3147	va_list ptr;
3148	va_start(ptr,fmt);
3149	length = mDNS_vsnprintf(sbuffer, buflen, fmt, ptr);
3150	va_end(ptr);
3151
3152	return(length);
3153	}
3154