xref: /titanic_51/usr/src/cmd/cmd-inet/usr.lib/mdnsd/DNSCommon.c (revision 1e194cd1a618eb48f311652742895fc33026c470)
1 /* -*- Mode: C; tab-width: 4 -*-
2  *
3  * Copyright (c) 2002-2013 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 #include "CryptoAlg.h"
22 #include "anonymous.h"
23 
24 // Disable certain benign warnings with Microsoft compilers
25 #if (defined(_MSC_VER))
26 // Disable "conditional expression is constant" warning for debug macros.
27 // Otherwise, this generates warnings for the perfectly natural construct "while(1)"
28 // If someone knows a variant way of writing "while(1)" that doesn't generate warning messages, please let us know
29     #pragma warning(disable:4127)
30 // Disable "array is too small to include a terminating null character" warning
31 // -- domain labels have an initial length byte, not a terminating null character
32     #pragma warning(disable:4295)
33 #endif
34 
35 // ***************************************************************************
36 #if COMPILER_LIKES_PRAGMA_MARK
37 #pragma mark - Program Constants
38 #endif
39 
40 mDNSexport const mDNSInterfaceID mDNSInterface_Any       = 0;
41 mDNSexport const mDNSInterfaceID mDNSInterfaceMark       = (mDNSInterfaceID)-1;
42 mDNSexport const mDNSInterfaceID mDNSInterface_LocalOnly = (mDNSInterfaceID)-2;
43 mDNSexport const mDNSInterfaceID mDNSInterface_Unicast   = (mDNSInterfaceID)-3;
44 mDNSexport const mDNSInterfaceID mDNSInterface_P2P       = (mDNSInterfaceID)-4;
45 mDNSexport const mDNSInterfaceID uDNSInterfaceMark       = (mDNSInterfaceID)-5;
46 
47 // Note: Microsoft's proposed "Link Local Multicast Name Resolution Protocol" (LLMNR) is essentially a limited version of
48 // Multicast DNS, using the same packet formats, naming syntax, and record types as Multicast DNS, but on a different UDP
49 // port and multicast address, which means it won't interoperate with the existing installed base of Multicast DNS responders.
50 // LLMNR uses IPv4 multicast address 224.0.0.252, IPv6 multicast address FF02::0001:0003, and UDP port 5355.
51 // Uncomment the appropriate lines below to build a special Multicast DNS responder for testing interoperability
52 // with Microsoft's LLMNR client code.
53 
54 #define   DiscardPortAsNumber               9
55 #define   SSHPortAsNumber                  22
56 #define   UnicastDNSPortAsNumber           53
57 #define   SSDPPortAsNumber               1900
58 #define   IPSECPortAsNumber              4500
59 #define   NSIPCPortAsNumber              5030       // Port used for dnsextd to talk to local nameserver bound to loopback
60 #define   NATPMPAnnouncementPortAsNumber 5350
61 #define   NATPMPPortAsNumber             5351
62 #define   DNSEXTPortAsNumber             5352       // Port used for end-to-end DNS operations like LLQ, Updates with Leases, etc.
63 #define   MulticastDNSPortAsNumber       5353
64 #define   LoopbackIPCPortAsNumber        5354
65 //#define MulticastDNSPortAsNumber       5355		// LLMNR
66 #define   PrivateDNSPortAsNumber         5533
67 
68 mDNSexport const mDNSIPPort DiscardPort            = { { DiscardPortAsNumber            >> 8, DiscardPortAsNumber            & 0xFF } };
69 mDNSexport const mDNSIPPort SSHPort                = { { SSHPortAsNumber                >> 8, SSHPortAsNumber                & 0xFF } };
70 mDNSexport const mDNSIPPort UnicastDNSPort         = { { UnicastDNSPortAsNumber         >> 8, UnicastDNSPortAsNumber         & 0xFF } };
71 mDNSexport const mDNSIPPort SSDPPort               = { { SSDPPortAsNumber               >> 8, SSDPPortAsNumber               & 0xFF } };
72 mDNSexport const mDNSIPPort IPSECPort              = { { IPSECPortAsNumber              >> 8, IPSECPortAsNumber              & 0xFF } };
73 mDNSexport const mDNSIPPort NSIPCPort              = { { NSIPCPortAsNumber              >> 8, NSIPCPortAsNumber              & 0xFF } };
74 mDNSexport const mDNSIPPort NATPMPAnnouncementPort = { { NATPMPAnnouncementPortAsNumber >> 8, NATPMPAnnouncementPortAsNumber & 0xFF } };
75 mDNSexport const mDNSIPPort NATPMPPort             = { { NATPMPPortAsNumber             >> 8, NATPMPPortAsNumber             & 0xFF } };
76 mDNSexport const mDNSIPPort DNSEXTPort             = { { DNSEXTPortAsNumber             >> 8, DNSEXTPortAsNumber             & 0xFF } };
77 mDNSexport const mDNSIPPort MulticastDNSPort       = { { MulticastDNSPortAsNumber       >> 8, MulticastDNSPortAsNumber       & 0xFF } };
78 mDNSexport const mDNSIPPort LoopbackIPCPort        = { { LoopbackIPCPortAsNumber        >> 8, LoopbackIPCPortAsNumber        & 0xFF } };
79 mDNSexport const mDNSIPPort PrivateDNSPort         = { { PrivateDNSPortAsNumber         >> 8, PrivateDNSPortAsNumber         & 0xFF } };
80 
81 mDNSexport const OwnerOptData zeroOwner         = { 0, 0, { { 0 } }, { { 0 } }, { { 0 } } };
82 
83 mDNSexport const mDNSIPPort zeroIPPort        = { { 0 } };
84 mDNSexport const mDNSv4Addr zerov4Addr        = { { 0 } };
85 mDNSexport const mDNSv6Addr zerov6Addr        = { { 0 } };
86 mDNSexport const mDNSEthAddr zeroEthAddr       = { { 0 } };
87 mDNSexport const mDNSv4Addr onesIPv4Addr      = { { 255, 255, 255, 255 } };
88 mDNSexport const mDNSv6Addr onesIPv6Addr      = { { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 } };
89 mDNSexport const mDNSEthAddr onesEthAddr       = { { 255, 255, 255, 255, 255, 255 } };
90 mDNSexport const mDNSAddr zeroAddr          = { mDNSAddrType_None, {{{ 0 }}} };
91 
92 mDNSexport const mDNSv4Addr AllDNSAdminGroup   = { { 239, 255, 255, 251 } };
93 mDNSexport const mDNSv4Addr AllHosts_v4        = { { 224,   0,   0,   1 } };  // For NAT-PMP & PCP Annoucements
94 mDNSexport const mDNSv6Addr AllHosts_v6        = { { 0xFF,0x02,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x01 } };
95 mDNSexport const mDNSv6Addr NDP_prefix         = { { 0xFF,0x02,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x01, 0xFF,0x00,0x00,0xFB } };  // FF02:0:0:0:0:1:FF00::/104
96 mDNSexport const mDNSEthAddr AllHosts_v6_Eth    = { { 0x33, 0x33, 0x00, 0x00, 0x00, 0x01 } };
97 mDNSexport const mDNSAddr AllDNSLinkGroup_v4 = { mDNSAddrType_IPv4, { { { 224,   0,   0, 251 } } } };
98 //mDNSexport const mDNSAddr  AllDNSLinkGroup_v4 = { mDNSAddrType_IPv4, { { { 224,   0,   0, 252 } } } }; // LLMNR
99 mDNSexport const mDNSAddr AllDNSLinkGroup_v6 = { mDNSAddrType_IPv6, { { { 0xFF,0x02,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0xFB } } } };
100 //mDNSexport const mDNSAddr  AllDNSLinkGroup_v6 = { mDNSAddrType_IPv6, { { { 0xFF,0x02,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x01,0x00,0x03 } } } }; // LLMNR
101 
102 mDNSexport const mDNSOpaque16 zeroID          = { { 0, 0 } };
103 mDNSexport const mDNSOpaque16 onesID          = { { 255, 255 } };
104 mDNSexport const mDNSOpaque16 QueryFlags      = { { kDNSFlag0_QR_Query    | kDNSFlag0_OP_StdQuery,                0 } };
105 mDNSexport const mDNSOpaque16 uQueryFlags     = { { kDNSFlag0_QR_Query    | kDNSFlag0_OP_StdQuery | kDNSFlag0_RD, 0 } };
106 mDNSexport const mDNSOpaque16 DNSSecQFlags    = { { kDNSFlag0_QR_Query    | kDNSFlag0_OP_StdQuery | kDNSFlag0_RD, kDNSFlag1_CD } };
107 mDNSexport const mDNSOpaque16 ResponseFlags   = { { kDNSFlag0_QR_Response | kDNSFlag0_OP_StdQuery | kDNSFlag0_AA, 0 } };
108 mDNSexport const mDNSOpaque16 UpdateReqFlags  = { { kDNSFlag0_QR_Query    | kDNSFlag0_OP_Update,                  0 } };
109 mDNSexport const mDNSOpaque16 UpdateRespFlags = { { kDNSFlag0_QR_Response | kDNSFlag0_OP_Update,                  0 } };
110 
111 mDNSexport const mDNSOpaque64 zeroOpaque64    = { { 0 } };
112 
113 // ***************************************************************************
114 #if COMPILER_LIKES_PRAGMA_MARK
115 #pragma mark -
116 #pragma mark - General Utility Functions
117 #endif
118 
119 // return true for RFC1918 private addresses
120 mDNSexport mDNSBool mDNSv4AddrIsRFC1918(const mDNSv4Addr * const addr)
121 {
122     return ((addr->b[0] == 10) ||                                 // 10/8 prefix
123             (addr->b[0] == 172 && (addr->b[1] & 0xF0) == 16) ||   // 172.16/12
124             (addr->b[0] == 192 && addr->b[1] == 168));            // 192.168/16
125 }
126 
127 mDNSexport void mDNSAddrMapIPv4toIPv6(mDNSv4Addr* in, mDNSv6Addr* out)
128 {
129     out->l[0] = 0;
130     out->l[1] = 0;
131     out->w[4] = 0;
132     out->w[5] = 0xffff;
133     out->b[12] = in->b[0];
134     out->b[13] = in->b[1];
135     out->b[14] = in->b[2];
136     out->b[15] = in->b[3];
137 }
138 
139 mDNSexport mDNSBool mDNSAddrIPv4FromMappedIPv6(mDNSv6Addr *in, mDNSv4Addr* out)
140 {
141     if (in->l[0] != 0 || in->l[1] != 0 || in->w[4] != 0 || in->w[5] != 0xffff)
142         return mDNSfalse;
143 
144     out->NotAnInteger = in->l[3];
145     return mDNStrue;
146 }
147 
148 mDNSexport NetworkInterfaceInfo *GetFirstActiveInterface(NetworkInterfaceInfo *intf)
149 {
150     while (intf && !intf->InterfaceActive) intf = intf->next;
151     return(intf);
152 }
153 
154 mDNSexport mDNSInterfaceID GetNextActiveInterfaceID(const NetworkInterfaceInfo *intf)
155 {
156     const NetworkInterfaceInfo *next = GetFirstActiveInterface(intf->next);
157     if (next) return(next->InterfaceID);else return(mDNSNULL);
158 }
159 
160 mDNSexport mDNSu32 NumCacheRecordsForInterfaceID(const mDNS *const m, mDNSInterfaceID id)
161 {
162     mDNSu32 slot, used = 0;
163     CacheGroup *cg;
164     const CacheRecord *rr;
165     FORALL_CACHERECORDS(slot, cg, rr)
166     {
167         if (rr->resrec.InterfaceID == id)
168             used++;
169     }
170     return(used);
171 }
172 
173 mDNSexport char *DNSTypeName(mDNSu16 rrtype)
174 {
175     switch (rrtype)
176     {
177     case kDNSType_A:    return("Addr");
178     case kDNSType_NS:   return("NS");
179     case kDNSType_CNAME: return("CNAME");
180     case kDNSType_SOA:  return("SOA");
181     case kDNSType_NULL: return("NULL");
182     case kDNSType_PTR:  return("PTR");
183     case kDNSType_HINFO: return("HINFO");
184     case kDNSType_TXT:  return("TXT");
185     case kDNSType_AAAA: return("AAAA");
186     case kDNSType_SRV:  return("SRV");
187     case kDNSType_OPT:  return("OPT");
188     case kDNSType_NSEC: return("NSEC");
189     case kDNSType_NSEC3: return("NSEC3");
190     case kDNSType_NSEC3PARAM: return("NSEC3PARAM");
191     case kDNSType_TSIG: return("TSIG");
192     case kDNSType_RRSIG: return("RRSIG");
193     case kDNSType_DNSKEY: return("DNSKEY");
194     case kDNSType_DS: return("DS");
195     case kDNSQType_ANY: return("ANY");
196     default:            {
197         static char buffer[16];
198         mDNS_snprintf(buffer, sizeof(buffer), "TYPE%d", rrtype);
199         return(buffer);
200     }
201     }
202 }
203 
204 mDNSlocal char *DNSSECAlgName(mDNSu8 alg)
205 {
206     switch (alg)
207     {
208     case CRYPTO_RSA_SHA1: return "RSA_SHA1";
209     case CRYPTO_DSA_NSEC3_SHA1: return "DSA_NSEC3_SHA1";
210     case CRYPTO_RSA_NSEC3_SHA1: return "RSA_NSEC3_SHA1";
211     case CRYPTO_RSA_SHA256: return "RSA_SHA256";
212     case CRYPTO_RSA_SHA512: return "RSA_SHA512";
213     default: {
214         static char algbuffer[16];
215         mDNS_snprintf(algbuffer, sizeof(algbuffer), "ALG%d", alg);
216         return(algbuffer);
217     }
218     }
219 }
220 
221 mDNSlocal char *DNSSECDigestName(mDNSu8 digest)
222 {
223     switch (digest)
224     {
225     case SHA1_DIGEST_TYPE: return "SHA1";
226     case SHA256_DIGEST_TYPE: return "SHA256";
227     default:
228         {
229         static char digbuffer[16];
230         mDNS_snprintf(digbuffer, sizeof(digbuffer), "DIG%d", digest);
231         return(digbuffer);
232         }
233     }
234 }
235 
236 mDNSexport mDNSu32 swap32(mDNSu32 x)
237 {
238     mDNSu8 *ptr = (mDNSu8 *)&x;
239     return (mDNSu32)((mDNSu32)ptr[0] << 24 | (mDNSu32)ptr[1] << 16 | (mDNSu32)ptr[2] << 8 | ptr[3]);
240 }
241 
242 mDNSexport mDNSu16 swap16(mDNSu16 x)
243 {
244     mDNSu8 *ptr = (mDNSu8 *)&x;
245     return (mDNSu16)((mDNSu16)ptr[0] << 8 | ptr[1]);
246 }
247 
248 // RFC 4034 Appendix B: Get the keyid of a DNS KEY. It is not transmitted
249 // explicitly on the wire.
250 //
251 // Note: This just helps narrow down the list of keys to look at. It is possible
252 // for two DNS keys to have the same ID i.e., key ID is not a unqiue tag. We ignore
253 // MD5 keys.
254 //
255 // 1st argument - the RDATA part of the DNSKEY RR
256 // 2nd argument - the RDLENGTH
257 //
258 mDNSlocal mDNSu32 keytag(mDNSu8 *key, mDNSu32 keysize)
259 {
260     unsigned long ac;
261     unsigned int i;
262 
263     for (ac = 0, i = 0; i < keysize; ++i)
264         ac += (i & 1) ? key[i] : key[i] << 8;
265     ac += (ac >> 16) & 0xFFFF;
266     return ac & 0xFFFF;
267 }
268 
269 mDNSexport int baseEncode(char *buffer, int blen, const mDNSu8 *data, int len, int encAlg)
270 {
271     AlgContext *ctx;
272     mDNSu8 *outputBuffer;
273     int length;
274 
275     ctx = AlgCreate(ENC_ALG, encAlg);
276     if (!ctx)
277     {
278         LogMsg("baseEncode: AlgCreate failed\n");
279         return 0;
280     }
281     AlgAdd(ctx, data, len);
282     outputBuffer = AlgEncode(ctx);
283     length = 0;
284     if (outputBuffer)
285     {
286         // Note: don't include any spaces in the format string below. This
287         // is also used by NSEC3 code for proving non-existence where it
288         // needs the base32 encoding without any spaces etc.
289         length = mDNS_snprintf(buffer, blen, "%s", outputBuffer);
290     }
291     AlgDestroy(ctx);
292     return length;
293 }
294 
295 mDNSlocal void PrintTypeBitmap(const mDNSu8 *bmap, int bitmaplen, char *const buffer, mDNSu32 length)
296 {
297     int win, wlen, type;
298 
299     while (bitmaplen > 0)
300     {
301         int i;
302 
303         if (bitmaplen < 3)
304         {
305             LogMsg("PrintTypeBitmap: malformed bitmap, bitmaplen %d short", bitmaplen);
306             break;
307         }
308 
309         win = *bmap++;
310         wlen = *bmap++;
311         bitmaplen -= 2;
312         if (bitmaplen < wlen || wlen < 1 || wlen > 32)
313         {
314             LogInfo("PrintTypeBitmap: malformed nsec, bitmaplen %d wlen %d", bitmaplen, wlen);
315             break;
316         }
317         if (win < 0 || win >= 256)
318         {
319             LogInfo("PrintTypeBitmap: malformed nsec, bad window win %d", win);
320             break;
321         }
322         type = win * 256;
323         for (i = 0; i < wlen * 8; i++)
324         {
325             if (bmap[i>>3] & (128 >> (i&7)))
326                 length += mDNS_snprintf(buffer+length, (MaxMsg - 1) - length, "%s ", DNSTypeName(type + i));
327         }
328         bmap += wlen;
329         bitmaplen -= wlen;
330     }
331 }
332 
333 // Parse the fields beyond the base header. NSEC3 should have been validated.
334 mDNSexport void NSEC3Parse(const ResourceRecord *const rr, mDNSu8 **salt, int *hashLength, mDNSu8 **nxtName, int *bitmaplen, mDNSu8 **bitmap)
335 {
336 	const RDataBody2 *const rdb = (RDataBody2 *)rr->rdata->u.data;
337 	rdataNSEC3 *nsec3 = (rdataNSEC3 *)rdb->data;
338     mDNSu8 *p = (mDNSu8 *)&nsec3->salt;
339     int hlen;
340 
341     if (salt)
342     {
343         if (nsec3->saltLength)
344             *salt = p;
345         else
346             *salt = mDNSNULL;
347     }
348     p += nsec3->saltLength;
349     // p is pointing at hashLength
350     hlen = (int)*p;
351     if (hashLength)
352         *hashLength = hlen;
353     p++;
354     if (nxtName)
355         *nxtName = p;
356     p += hlen;
357     if (bitmaplen)
358         *bitmaplen = rr->rdlength - (int)(p - rdb->data);
359     if (bitmap)
360         *bitmap = p;
361 }
362 
363 // Note slight bug: this code uses the rdlength from the ResourceRecord object, to display
364 // the rdata from the RDataBody object. Sometimes this could be the wrong length -- but as
365 // long as this routine is only used for debugging messages, it probably isn't a big problem.
366 mDNSexport char *GetRRDisplayString_rdb(const ResourceRecord *const rr, const RDataBody *const rd1, char *const buffer)
367 {
368     const RDataBody2 *const rd = (RDataBody2 *)rd1;
369     #define RemSpc (MaxMsg-1-length)
370     char *ptr = buffer;
371     mDNSu32 length = mDNS_snprintf(buffer, MaxMsg-1, "%4d %##s %s ", rr->rdlength, rr->name->c, DNSTypeName(rr->rrtype));
372     if (rr->RecordType == kDNSRecordTypePacketNegative) return(buffer);
373     if (!rr->rdlength && rr->rrtype != kDNSType_OPT) { mDNS_snprintf(buffer+length, RemSpc, "<< ZERO RDATA LENGTH >>"); return(buffer); }
374 
375     switch (rr->rrtype)
376     {
377     case kDNSType_A:    mDNS_snprintf(buffer+length, RemSpc, "%.4a", &rd->ipv4);          break;
378 
379     case kDNSType_NS:       // Same as PTR
380     case kDNSType_CNAME:    // Same as PTR
381     case kDNSType_PTR:  mDNS_snprintf(buffer+length, RemSpc, "%##s", rd->name.c);       break;
382 
383     case kDNSType_SOA:  mDNS_snprintf(buffer+length, RemSpc, "%##s %##s %d %d %d %d %d",
384                                       rd->soa.mname.c, rd->soa.rname.c,
385                                       rd->soa.serial, rd->soa.refresh, rd->soa.retry, rd->soa.expire, rd->soa.min);
386         break;
387 
388     case kDNSType_HINFO:    // Display this the same as TXT (show all constituent strings)
389     case kDNSType_TXT:  {
390         const mDNSu8 *t = rd->txt.c;
391         while (t < rd->txt.c + rr->rdlength)
392         {
393             length += mDNS_snprintf(buffer+length, RemSpc, "%s%#s", t > rd->txt.c ? "¦" : "", t);
394             t += 1 + t[0];
395         }
396     } break;
397 
398     case kDNSType_AAAA: mDNS_snprintf(buffer+length, RemSpc, "%.16a", &rd->ipv6);       break;
399     case kDNSType_SRV:  mDNS_snprintf(buffer+length, RemSpc, "%u %u %u %##s",
400                                       rd->srv.priority, rd->srv.weight, mDNSVal16(rd->srv.port), rd->srv.target.c); break;
401 
402     case kDNSType_OPT:  {
403         const rdataOPT *opt;
404         const rdataOPT *const end = (const rdataOPT *)&rd->data[rr->rdlength];
405         length += mDNS_snprintf(buffer+length, RemSpc, "Max %d", rr->rrclass);
406         for (opt = &rd->opt[0]; opt < end; opt++)
407         {
408             switch(opt->opt)
409             {
410             case kDNSOpt_LLQ:
411                 length += mDNS_snprintf(buffer+length, RemSpc, " LLQ");
412                 length += mDNS_snprintf(buffer+length, RemSpc, " Vers %d",     opt->u.llq.vers);
413                 length += mDNS_snprintf(buffer+length, RemSpc, " Op %d",       opt->u.llq.llqOp);
414                 length += mDNS_snprintf(buffer+length, RemSpc, " Err/Port %d", opt->u.llq.err);
415                 length += mDNS_snprintf(buffer+length, RemSpc, " ID %08X%08X", opt->u.llq.id.l[0], opt->u.llq.id.l[1]);
416                 length += mDNS_snprintf(buffer+length, RemSpc, " Lease %d",    opt->u.llq.llqlease);
417                 break;
418             case kDNSOpt_Lease:
419                 length += mDNS_snprintf(buffer+length, RemSpc, " Lease %d",    opt->u.updatelease);
420                 break;
421             case kDNSOpt_Owner:
422                 length += mDNS_snprintf(buffer+length, RemSpc, " Owner");
423                 length += mDNS_snprintf(buffer+length, RemSpc, " Vers %d",     opt->u.owner.vers);
424                 length += mDNS_snprintf(buffer+length, RemSpc, " Seq %3d", (mDNSu8)opt->u.owner.seq);                           // Display as unsigned
425                 length += mDNS_snprintf(buffer+length, RemSpc, " MAC %.6a",    opt->u.owner.HMAC.b);
426                 if (opt->optlen >= DNSOpt_OwnerData_ID_Wake_Space-4)
427                 {
428                     length += mDNS_snprintf(buffer+length, RemSpc, " I-MAC %.6a", opt->u.owner.IMAC.b);
429                     if (opt->optlen > DNSOpt_OwnerData_ID_Wake_Space-4)
430                         length += mDNS_snprintf(buffer+length, RemSpc, " Password %.6a", opt->u.owner.password.b);
431                 }
432                 break;
433             case kDNSOpt_Trace:
434                 length += mDNS_snprintf(buffer+length, RemSpc, " Trace");
435                 length += mDNS_snprintf(buffer+length, RemSpc, " Platform %d",    opt->u.tracer.platf);
436                 length += mDNS_snprintf(buffer+length, RemSpc, " mDNSVers %d",    opt->u.tracer.mDNSv);
437                 break;
438             default:
439                 length += mDNS_snprintf(buffer+length, RemSpc, " Unknown %d",  opt->opt);
440                 break;
441             }
442         }
443     }
444     break;
445 
446     case kDNSType_NSEC: {
447         domainname *next = (domainname *)rd->data;
448         int len, bitmaplen;
449         mDNSu8 *bmap;
450         len = DomainNameLength(next);
451         bitmaplen = rr->rdlength - len;
452         bmap = (mDNSu8 *)((mDNSu8 *)next + len);
453 
454         if (UNICAST_NSEC(rr))
455             length += mDNS_snprintf(buffer+length, RemSpc, "%##s ", next->c);
456         PrintTypeBitmap(bmap, bitmaplen, buffer, length);
457 
458     }
459     break;
460     case kDNSType_NSEC3: {
461         rdataNSEC3 *nsec3 = (rdataNSEC3 *)rd->data;
462         const mDNSu8 *p = (mDNSu8 *)&nsec3->salt;
463         int hashLength, bitmaplen, i;
464 
465         length += mDNS_snprintf(buffer+length, RemSpc, "\t%s  %d  %d ",
466                                 DNSSECDigestName(nsec3->alg), nsec3->flags, swap16(nsec3->iterations));
467 
468         if (!nsec3->saltLength)
469         {
470             length += mDNS_snprintf(buffer+length, RemSpc, "-");
471         }
472         else
473         {
474             for (i = 0; i < nsec3->saltLength; i++)
475             {
476                 length += mDNS_snprintf(buffer+length, RemSpc, "%x", p[i]);
477             }
478         }
479 
480         // put a space at the end
481         length += mDNS_snprintf(buffer+length, RemSpc, " ");
482 
483         p += nsec3->saltLength;
484         // p is pointing at hashLength
485         hashLength = (int)*p++;
486 
487         length += baseEncode(buffer + length, RemSpc, p, hashLength, ENC_BASE32);
488 
489         // put a space at the end
490         length += mDNS_snprintf(buffer+length, RemSpc, " ");
491 
492         p += hashLength;
493         bitmaplen = rr->rdlength - (int)(p - rd->data);
494         PrintTypeBitmap(p, bitmaplen, buffer, length);
495     }
496     break;
497     case kDNSType_RRSIG:    {
498         rdataRRSig *rrsig = (rdataRRSig *)rd->data;
499         mDNSu8 expTimeBuf[64];
500         mDNSu8 inceptTimeBuf[64];
501         unsigned long inceptClock;
502         unsigned long expClock;
503         int len;
504 
505         expClock = (unsigned long)swap32(rrsig->sigExpireTime);
506         mDNSPlatformFormatTime(expClock, expTimeBuf, sizeof(expTimeBuf));
507 
508         inceptClock = (unsigned long)swap32(rrsig->sigInceptTime);
509         mDNSPlatformFormatTime(inceptClock, inceptTimeBuf, sizeof(inceptTimeBuf));
510 
511         length += mDNS_snprintf(buffer+length, RemSpc, "\t%s  %s  %d  %d  %s  %s  %d  %##s ",
512                                 DNSTypeName(swap16(rrsig->typeCovered)), DNSSECAlgName(rrsig->alg), rrsig->labels, swap32(rrsig->origTTL),
513                                 expTimeBuf, inceptTimeBuf, swap16(rrsig->keyTag), ((domainname *)(&rrsig->signerName))->c);
514 
515         len = DomainNameLength((domainname *)&rrsig->signerName);
516         length += baseEncode(buffer + length, RemSpc, (const mDNSu8 *)(rd->data + len + RRSIG_FIXED_SIZE),
517                                rr->rdlength - (len + RRSIG_FIXED_SIZE), ENC_BASE64);
518     }
519     break;
520     case kDNSType_DNSKEY:   {
521         rdataDNSKey *rrkey = (rdataDNSKey *)rd->data;
522         length += mDNS_snprintf(buffer+length, RemSpc, "\t%d  %d  %s  %u ", swap16(rrkey->flags), rrkey->proto,
523                                 DNSSECAlgName(rrkey->alg), (unsigned int)keytag((mDNSu8 *)rrkey, rr->rdlength));
524         length += baseEncode(buffer + length, RemSpc, (const mDNSu8 *)(rd->data + DNSKEY_FIXED_SIZE),
525                                rr->rdlength - DNSKEY_FIXED_SIZE, ENC_BASE64);
526     }
527     break;
528     case kDNSType_DS:       {
529         mDNSu8 *p;
530         int i;
531         rdataDS *rrds = (rdataDS *)rd->data;
532 
533         length += mDNS_snprintf(buffer+length, RemSpc, "\t%s\t%d\t%s ", DNSSECAlgName(rrds->alg), swap16(rrds->keyTag),
534                                 DNSSECDigestName(rrds->digestType));
535 
536         p = (mDNSu8 *)(rd->data + DS_FIXED_SIZE);
537         for (i = 0; i < (rr->rdlength - DS_FIXED_SIZE); i++)
538         {
539             length += mDNS_snprintf(buffer+length, RemSpc, "%x", p[i]);
540         }
541     }
542     break;
543 
544     default:            mDNS_snprintf(buffer+length, RemSpc, "RDLen %d: %s", rr->rdlength, rd->data);
545         // Really should scan buffer to check if text is valid UTF-8 and only replace with dots if not
546         for (ptr = buffer; *ptr; ptr++) if (*ptr < ' ') *ptr = '.';
547         break;
548     }
549     return(buffer);
550 }
551 
552 // See comments in mDNSEmbeddedAPI.h
553 #if _PLATFORM_HAS_STRONG_PRNG_
554 #define mDNSRandomNumber mDNSPlatformRandomNumber
555 #else
556 mDNSlocal mDNSu32 mDNSRandomFromSeed(mDNSu32 seed)
557 {
558     return seed * 21 + 1;
559 }
560 
561 mDNSlocal mDNSu32 mDNSMixRandomSeed(mDNSu32 seed, mDNSu8 iteration)
562 {
563     return iteration ? mDNSMixRandomSeed(mDNSRandomFromSeed(seed), --iteration) : seed;
564 }
565 
566 mDNSlocal mDNSu32 mDNSRandomNumber()
567 {
568     static mDNSBool seeded = mDNSfalse;
569     static mDNSu32 seed = 0;
570     if (!seeded)
571     {
572         seed = mDNSMixRandomSeed(mDNSPlatformRandomSeed(), 100);
573         seeded = mDNStrue;
574     }
575     return (seed = mDNSRandomFromSeed(seed));
576 }
577 #endif // ! _PLATFORM_HAS_STRONG_PRNG_
578 
579 mDNSexport mDNSu32 mDNSRandom(mDNSu32 max)      // Returns pseudo-random result from zero to max inclusive
580 {
581     mDNSu32 ret = 0;
582     mDNSu32 mask = 1;
583 
584     while (mask < max) mask = (mask << 1) | 1;
585 
586     do ret = mDNSRandomNumber() & mask;
587     while (ret > max);
588 
589     return ret;
590 }
591 
592 mDNSexport mDNSBool mDNSSameAddress(const mDNSAddr *ip1, const mDNSAddr *ip2)
593 {
594     if (ip1->type == ip2->type)
595     {
596         switch (ip1->type)
597         {
598         case mDNSAddrType_None: return(mDNStrue);      // Empty addresses have no data and are therefore always equal
599         case mDNSAddrType_IPv4: return (mDNSBool)(mDNSSameIPv4Address(ip1->ip.v4, ip2->ip.v4));
600         case mDNSAddrType_IPv6: return (mDNSBool)(mDNSSameIPv6Address(ip1->ip.v6, ip2->ip.v6));
601         }
602     }
603     return(mDNSfalse);
604 }
605 
606 mDNSexport mDNSBool mDNSAddrIsDNSMulticast(const mDNSAddr *ip)
607 {
608     switch(ip->type)
609     {
610     case mDNSAddrType_IPv4: return (mDNSBool)(mDNSSameIPv4Address(ip->ip.v4, AllDNSLinkGroup_v4.ip.v4));
611     case mDNSAddrType_IPv6: return (mDNSBool)(mDNSSameIPv6Address(ip->ip.v6, AllDNSLinkGroup_v6.ip.v6));
612     default: return(mDNSfalse);
613     }
614 }
615 
616 // ***************************************************************************
617 #if COMPILER_LIKES_PRAGMA_MARK
618 #pragma mark -
619 #pragma mark - Domain Name Utility Functions
620 #endif
621 
622 mDNSexport mDNSBool SameDomainLabel(const mDNSu8 *a, const mDNSu8 *b)
623 {
624     int i;
625     const int len = *a++;
626 
627     if (len > MAX_DOMAIN_LABEL)
628     { debugf("Malformed label (too long)"); return(mDNSfalse); }
629 
630     if (len != *b++) return(mDNSfalse);
631     for (i=0; i<len; i++)
632     {
633         mDNSu8 ac = *a++;
634         mDNSu8 bc = *b++;
635         if (mDNSIsUpperCase(ac)) ac += 'a' - 'A';
636         if (mDNSIsUpperCase(bc)) bc += 'a' - 'A';
637         if (ac != bc) return(mDNSfalse);
638     }
639     return(mDNStrue);
640 }
641 
642 mDNSexport mDNSBool SameDomainName(const domainname *const d1, const domainname *const d2)
643 {
644     const mDNSu8 *      a   = d1->c;
645     const mDNSu8 *      b   = d2->c;
646     const mDNSu8 *const max = d1->c + MAX_DOMAIN_NAME;          // Maximum that's valid
647 
648     while (*a || *b)
649     {
650         if (a + 1 + *a >= max)
651         { debugf("Malformed domain name (more than 256 characters)"); return(mDNSfalse); }
652         if (!SameDomainLabel(a, b)) return(mDNSfalse);
653         a += 1 + *a;
654         b += 1 + *b;
655     }
656 
657     return(mDNStrue);
658 }
659 
660 mDNSexport mDNSBool SameDomainNameCS(const domainname *const d1, const domainname *const d2)
661 {
662     mDNSu16 l1 = DomainNameLength(d1);
663     mDNSu16 l2 = DomainNameLength(d2);
664     return(l1 <= MAX_DOMAIN_NAME && l1 == l2 && mDNSPlatformMemSame(d1, d2, l1));
665 }
666 
667 mDNSexport mDNSBool IsLocalDomain(const domainname *d)
668 {
669     // Domains that are defined to be resolved via link-local multicast are:
670     // local., 254.169.in-addr.arpa., and {8,9,A,B}.E.F.ip6.arpa.
671     static const domainname *nL = (const domainname*)"\x5" "local";
672     static const domainname *nR = (const domainname*)"\x3" "254" "\x3" "169"         "\x7" "in-addr" "\x4" "arpa";
673     static const domainname *n8 = (const domainname*)"\x1" "8"   "\x1" "e" "\x1" "f" "\x3" "ip6"     "\x4" "arpa";
674     static const domainname *n9 = (const domainname*)"\x1" "9"   "\x1" "e" "\x1" "f" "\x3" "ip6"     "\x4" "arpa";
675     static const domainname *nA = (const domainname*)"\x1" "a"   "\x1" "e" "\x1" "f" "\x3" "ip6"     "\x4" "arpa";
676     static const domainname *nB = (const domainname*)"\x1" "b"   "\x1" "e" "\x1" "f" "\x3" "ip6"     "\x4" "arpa";
677 
678     const domainname *d1, *d2, *d3, *d4, *d5;   // Top-level domain, second-level domain, etc.
679     d1 = d2 = d3 = d4 = d5 = mDNSNULL;
680     while (d->c[0])
681     {
682         d5 = d4; d4 = d3; d3 = d2; d2 = d1; d1 = d;
683         d = (const domainname*)(d->c + 1 + d->c[0]);
684     }
685 
686     if (d1 && SameDomainName(d1, nL)) return(mDNStrue);
687     if (d4 && SameDomainName(d4, nR)) return(mDNStrue);
688     if (d5 && SameDomainName(d5, n8)) return(mDNStrue);
689     if (d5 && SameDomainName(d5, n9)) return(mDNStrue);
690     if (d5 && SameDomainName(d5, nA)) return(mDNStrue);
691     if (d5 && SameDomainName(d5, nB)) return(mDNStrue);
692     return(mDNSfalse);
693 }
694 
695 mDNSexport const mDNSu8 *LastLabel(const domainname *d)
696 {
697     const mDNSu8 *p = d->c;
698     while (d->c[0])
699     {
700         p = d->c;
701         d = (const domainname*)(d->c + 1 + d->c[0]);
702     }
703     return(p);
704 }
705 
706 // Returns length of a domain name INCLUDING the byte for the final null label
707 // e.g. for the root label "." it returns one
708 // For the FQDN "com." it returns 5 (length byte, three data bytes, final zero)
709 // Legal results are 1 (just root label) to 256 (MAX_DOMAIN_NAME)
710 // If the given domainname is invalid, result is 257 (MAX_DOMAIN_NAME+1)
711 mDNSexport mDNSu16 DomainNameLengthLimit(const domainname *const name, const mDNSu8 *limit)
712 {
713     const mDNSu8 *src = name->c;
714     while (src < limit && *src <= MAX_DOMAIN_LABEL)
715     {
716         if (*src == 0) return((mDNSu16)(src - name->c + 1));
717         src += 1 + *src;
718     }
719     return(MAX_DOMAIN_NAME+1);
720 }
721 
722 // CompressedDomainNameLength returns the length of a domain name INCLUDING the byte
723 // for the final null label, e.g. for the root label "." it returns one.
724 // E.g. for the FQDN "foo.com." it returns 9
725 // (length, three data bytes, length, three more data bytes, final zero).
726 // In the case where a parent domain name is provided, and the given name is a child
727 // of that parent, CompressedDomainNameLength returns the length of the prefix portion
728 // of the child name, plus TWO bytes for the compression pointer.
729 // E.g. for the name "foo.com." with parent "com.", it returns 6
730 // (length, three data bytes, two-byte compression pointer).
731 mDNSexport mDNSu16 CompressedDomainNameLength(const domainname *const name, const domainname *parent)
732 {
733     const mDNSu8 *src = name->c;
734     if (parent && parent->c[0] == 0) parent = mDNSNULL;
735     while (*src)
736     {
737         if (*src > MAX_DOMAIN_LABEL) return(MAX_DOMAIN_NAME+1);
738         if (parent && SameDomainName((const domainname *)src, parent)) return((mDNSu16)(src - name->c + 2));
739         src += 1 + *src;
740         if (src - name->c >= MAX_DOMAIN_NAME) return(MAX_DOMAIN_NAME+1);
741     }
742     return((mDNSu16)(src - name->c + 1));
743 }
744 
745 // CountLabels() returns number of labels in name, excluding final root label
746 // (e.g. for "apple.com." CountLabels returns 2.)
747 mDNSexport int CountLabels(const domainname *d)
748 {
749     int count = 0;
750     const mDNSu8 *ptr;
751     for (ptr = d->c; *ptr; ptr = ptr + ptr[0] + 1) count++;
752     return count;
753 }
754 
755 // SkipLeadingLabels skips over the first 'skip' labels in the domainname,
756 // returning a pointer to the suffix with 'skip' labels removed.
757 mDNSexport const domainname *SkipLeadingLabels(const domainname *d, int skip)
758 {
759     while (skip > 0 && d->c[0]) { d = (const domainname *)(d->c + 1 + d->c[0]); skip--; }
760     return(d);
761 }
762 
763 // AppendLiteralLabelString appends a single label to an existing (possibly empty) domainname.
764 // The C string contains the label as-is, with no escaping, etc.
765 // Any dots in the name are literal dots, not label separators
766 // If successful, AppendLiteralLabelString returns a pointer to the next unused byte
767 // in the domainname bufer (i.e. the next byte after the terminating zero).
768 // If unable to construct a legal domain name (i.e. label more than 63 bytes, or total more than 256 bytes)
769 // AppendLiteralLabelString returns mDNSNULL.
770 mDNSexport mDNSu8 *AppendLiteralLabelString(domainname *const name, const char *cstr)
771 {
772     mDNSu8       *      ptr  = name->c + DomainNameLength(name) - 1;    // Find end of current name
773     const mDNSu8 *const lim1 = name->c + MAX_DOMAIN_NAME - 1;           // Limit of how much we can add (not counting final zero)
774     const mDNSu8 *const lim2 = ptr + 1 + MAX_DOMAIN_LABEL;
775     const mDNSu8 *const lim  = (lim1 < lim2) ? lim1 : lim2;
776     mDNSu8       *lengthbyte = ptr++;                                   // Record where the length is going to go
777 
778     while (*cstr && ptr < lim) *ptr++ = (mDNSu8)*cstr++;    // Copy the data
779     *lengthbyte = (mDNSu8)(ptr - lengthbyte - 1);           // Fill in the length byte
780     *ptr++ = 0;                                             // Put the null root label on the end
781     if (*cstr) return(mDNSNULL);                            // Failure: We didn't successfully consume all input
782     else return(ptr);                                       // Success: return new value of ptr
783 }
784 
785 // AppendDNSNameString appends zero or more labels to an existing (possibly empty) domainname.
786 // The C string is in conventional DNS syntax:
787 // Textual labels, escaped as necessary using the usual DNS '\' notation, separated by dots.
788 // If successful, AppendDNSNameString returns a pointer to the next unused byte
789 // in the domainname bufer (i.e. the next byte after the terminating zero).
790 // If unable to construct a legal domain name (i.e. label more than 63 bytes, or total more than 256 bytes)
791 // AppendDNSNameString returns mDNSNULL.
792 mDNSexport mDNSu8 *AppendDNSNameString(domainname *const name, const char *cstring)
793 {
794     const char   *cstr      = cstring;
795     mDNSu8       *      ptr = name->c + DomainNameLength(name) - 1; // Find end of current name
796     const mDNSu8 *const lim = name->c + MAX_DOMAIN_NAME - 1;        // Limit of how much we can add (not counting final zero)
797     while (*cstr && ptr < lim)                                      // While more characters, and space to put them...
798     {
799         mDNSu8 *lengthbyte = ptr++;                                 // Record where the length is going to go
800         if (*cstr == '.') { LogMsg("AppendDNSNameString: Illegal empty label in name \"%s\"", cstring); return(mDNSNULL); }
801         while (*cstr && *cstr != '.' && ptr < lim)                  // While we have characters in the label...
802         {
803             mDNSu8 c = (mDNSu8)*cstr++;                             // Read the character
804             if (c == '\\')                                          // If escape character, check next character
805             {
806                 c = (mDNSu8)*cstr++;                                // Assume we'll just take the next character
807                 if (mDNSIsDigit(cstr[-1]) && mDNSIsDigit(cstr[0]) && mDNSIsDigit(cstr[1]))
808                 {                                                   // If three decimal digits,
809                     int v0 = cstr[-1] - '0';                        // then interpret as three-digit decimal
810                     int v1 = cstr[ 0] - '0';
811                     int v2 = cstr[ 1] - '0';
812                     int val = v0 * 100 + v1 * 10 + v2;
813                     if (val <= 255) { c = (mDNSu8)val; cstr += 2; } // If valid three-digit decimal value, use it
814                 }
815             }
816             *ptr++ = c;                                             // Write the character
817         }
818         if (*cstr) cstr++;                                          // Skip over the trailing dot (if present)
819         if (ptr - lengthbyte - 1 > MAX_DOMAIN_LABEL)                // If illegal label, abort
820             return(mDNSNULL);
821         *lengthbyte = (mDNSu8)(ptr - lengthbyte - 1);               // Fill in the length byte
822     }
823 
824     *ptr++ = 0;                                                     // Put the null root label on the end
825     if (*cstr) return(mDNSNULL);                                    // Failure: We didn't successfully consume all input
826     else return(ptr);                                               // Success: return new value of ptr
827 }
828 
829 // AppendDomainLabel appends a single label to a name.
830 // If successful, AppendDomainLabel returns a pointer to the next unused byte
831 // in the domainname bufer (i.e. the next byte after the terminating zero).
832 // If unable to construct a legal domain name (i.e. label more than 63 bytes, or total more than 256 bytes)
833 // AppendDomainLabel returns mDNSNULL.
834 mDNSexport mDNSu8 *AppendDomainLabel(domainname *const name, const domainlabel *const label)
835 {
836     int i;
837     mDNSu8 *ptr = name->c + DomainNameLength(name) - 1;
838 
839     // Check label is legal
840     if (label->c[0] > MAX_DOMAIN_LABEL) return(mDNSNULL);
841 
842     // Check that ptr + length byte + data bytes + final zero does not exceed our limit
843     if (ptr + 1 + label->c[0] + 1 > name->c + MAX_DOMAIN_NAME) return(mDNSNULL);
844 
845     for (i=0; i<=label->c[0]; i++) *ptr++ = label->c[i];    // Copy the label data
846     *ptr++ = 0;                             // Put the null root label on the end
847     return(ptr);
848 }
849 
850 mDNSexport mDNSu8 *AppendDomainName(domainname *const name, const domainname *const append)
851 {
852     mDNSu8       *      ptr = name->c + DomainNameLength(name) - 1; // Find end of current name
853     const mDNSu8 *const lim = name->c + MAX_DOMAIN_NAME - 1;        // Limit of how much we can add (not counting final zero)
854     const mDNSu8 *      src = append->c;
855     while (src[0])
856     {
857         int i;
858         if (ptr + 1 + src[0] > lim) return(mDNSNULL);
859         for (i=0; i<=src[0]; i++) *ptr++ = src[i];
860         *ptr = 0;   // Put the null root label on the end
861         src += i;
862     }
863     return(ptr);
864 }
865 
866 // MakeDomainLabelFromLiteralString makes a single domain label from a single literal C string (with no escaping).
867 // If successful, MakeDomainLabelFromLiteralString returns mDNStrue.
868 // If unable to convert the whole string to a legal domain label (i.e. because length is more than 63 bytes) then
869 // MakeDomainLabelFromLiteralString makes a legal domain label from the first 63 bytes of the string and returns mDNSfalse.
870 // In some cases silently truncated oversized names to 63 bytes is acceptable, so the return result may be ignored.
871 // In other cases silent truncation may not be acceptable, so in those cases the calling function needs to check the return result.
872 mDNSexport mDNSBool MakeDomainLabelFromLiteralString(domainlabel *const label, const char *cstr)
873 {
874     mDNSu8       *      ptr   = label->c + 1;                       // Where we're putting it
875     const mDNSu8 *const limit = label->c + 1 + MAX_DOMAIN_LABEL;    // The maximum we can put
876     while (*cstr && ptr < limit) *ptr++ = (mDNSu8)*cstr++;          // Copy the label
877     label->c[0] = (mDNSu8)(ptr - label->c - 1);                     // Set the length byte
878     return(*cstr == 0);                                             // Return mDNStrue if we successfully consumed all input
879 }
880 
881 // MakeDomainNameFromDNSNameString makes a native DNS-format domainname from a C string.
882 // The C string is in conventional DNS syntax:
883 // Textual labels, escaped as necessary using the usual DNS '\' notation, separated by dots.
884 // If successful, MakeDomainNameFromDNSNameString returns a pointer to the next unused byte
885 // in the domainname bufer (i.e. the next byte after the terminating zero).
886 // If unable to construct a legal domain name (i.e. label more than 63 bytes, or total more than 256 bytes)
887 // MakeDomainNameFromDNSNameString returns mDNSNULL.
888 mDNSexport mDNSu8 *MakeDomainNameFromDNSNameString(domainname *const name, const char *cstr)
889 {
890     name->c[0] = 0;                                 // Make an empty domain name
891     return(AppendDNSNameString(name, cstr));        // And then add this string to it
892 }
893 
894 mDNSexport char *ConvertDomainLabelToCString_withescape(const domainlabel *const label, char *ptr, char esc)
895 {
896     const mDNSu8 *      src = label->c;                         // Domain label we're reading
897     const mDNSu8 len = *src++;                                  // Read length of this (non-null) label
898     const mDNSu8 *const end = src + len;                        // Work out where the label ends
899     if (len > MAX_DOMAIN_LABEL) return(mDNSNULL);               // If illegal label, abort
900     while (src < end)                                           // While we have characters in the label
901     {
902         mDNSu8 c = *src++;
903         if (esc)
904         {
905             if (c == '.' || c == esc)                           // If character is a dot or the escape character
906                 *ptr++ = esc;                                   // Output escape character
907             else if (c <= ' ')                                  // If non-printing ascii,
908             {                                                   // Output decimal escape sequence
909                 *ptr++ = esc;
910                 *ptr++ = (char)  ('0' + (c / 100)     );
911                 *ptr++ = (char)  ('0' + (c /  10) % 10);
912                 c      = (mDNSu8)('0' + (c      ) % 10);
913             }
914         }
915         *ptr++ = (char)c;                                       // Copy the character
916     }
917     *ptr = 0;                                                   // Null-terminate the string
918     return(ptr);                                                // and return
919 }
920 
921 // Note: To guarantee that there will be no possible overrun, cstr must be at least MAX_ESCAPED_DOMAIN_NAME (1009 bytes)
922 mDNSexport char *ConvertDomainNameToCString_withescape(const domainname *const name, char *ptr, char esc)
923 {
924     const mDNSu8 *src         = name->c;                            // Domain name we're reading
925     const mDNSu8 *const max   = name->c + MAX_DOMAIN_NAME;          // Maximum that's valid
926 
927     if (*src == 0) *ptr++ = '.';                                    // Special case: For root, just write a dot
928 
929     while (*src)                                                    // While more characters in the domain name
930     {
931         if (src + 1 + *src >= max) return(mDNSNULL);
932         ptr = ConvertDomainLabelToCString_withescape((const domainlabel *)src, ptr, esc);
933         if (!ptr) return(mDNSNULL);
934         src += 1 + *src;
935         *ptr++ = '.';                                               // Write the dot after the label
936     }
937 
938     *ptr++ = 0;                                                     // Null-terminate the string
939     return(ptr);                                                    // and return
940 }
941 
942 // RFC 1034 rules:
943 // Host names must start with a letter, end with a letter or digit,
944 // and have as interior characters only letters, digits, and hyphen.
945 // This was subsequently modified in RFC 1123 to allow the first character to be either a letter or a digit
946 
947 mDNSexport void ConvertUTF8PstringToRFC1034HostLabel(const mDNSu8 UTF8Name[], domainlabel *const hostlabel)
948 {
949     const mDNSu8 *      src  = &UTF8Name[1];
950     const mDNSu8 *const end  = &UTF8Name[1] + UTF8Name[0];
951     mDNSu8 *      ptr  = &hostlabel->c[1];
952     const mDNSu8 *const lim  = &hostlabel->c[1] + MAX_DOMAIN_LABEL;
953     while (src < end)
954     {
955         // Delete apostrophes from source name
956         if (src[0] == '\'') { src++; continue; }        // Standard straight single quote
957         if (src + 2 < end && src[0] == 0xE2 && src[1] == 0x80 && src[2] == 0x99)
958         { src += 3; continue; }     // Unicode curly apostrophe
959         if (ptr < lim)
960         {
961             if (mDNSValidHostChar(*src, (ptr > &hostlabel->c[1]), (src < end-1))) *ptr++ = *src;
962             else if (ptr > &hostlabel->c[1] && ptr[-1] != '-') *ptr++ = '-';
963         }
964         src++;
965     }
966     while (ptr > &hostlabel->c[1] && ptr[-1] == '-') ptr--; // Truncate trailing '-' marks
967     hostlabel->c[0] = (mDNSu8)(ptr - &hostlabel->c[1]);
968 }
969 
970 #define ValidTransportProtocol(X) ( (X)[0] == 4 && (X)[1] == '_' && \
971                                     ((((X)[2] | 0x20) == 'u' && ((X)[3] | 0x20) == 'd') || (((X)[2] | 0x20) == 't' && ((X)[3] | 0x20) == 'c')) && \
972                                     ((X)[4] | 0x20) == 'p')
973 
974 mDNSexport mDNSu8 *ConstructServiceName(domainname *const fqdn,
975                                         const domainlabel *name, const domainname *type, const domainname *const domain)
976 {
977     int i, len;
978     mDNSu8 *dst = fqdn->c;
979     const mDNSu8 *src;
980     const char *errormsg;
981 #if APPLE_OSX_mDNSResponder
982     mDNSBool loggedUnderscore = mDNSfalse;
983     static char typeBuf[MAX_ESCAPED_DOMAIN_NAME];
984 #endif
985 
986     // In the case where there is no name (and ONLY in that case),
987     // a single-label subtype is allowed as the first label of a three-part "type"
988     if (!name && type)
989     {
990         const mDNSu8 *s0 = type->c;
991         if (s0[0] && s0[0] < 0x40)      // If legal first label (at least one character, and no more than 63)
992         {
993             const mDNSu8 * s1 = s0 + 1 + s0[0];
994             if (s1[0] && s1[0] < 0x40)  // and legal second label (at least one character, and no more than 63)
995             {
996                 const mDNSu8 *s2 = s1 + 1 + s1[0];
997                 if (s2[0] && s2[0] < 0x40 && s2[1+s2[0]] == 0)  // and we have three and only three labels
998                 {
999                     static const mDNSu8 SubTypeLabel[5] = mDNSSubTypeLabel;
1000                     src = s0;                                   // Copy the first label
1001                     len = *src;
1002                     for (i=0; i <= len;                      i++) *dst++ = *src++;
1003                     for (i=0; i < (int)sizeof(SubTypeLabel); i++) *dst++ = SubTypeLabel[i];
1004                     type = (const domainname *)s1;
1005 
1006                     // Special support to enable the DNSServiceBrowse call made by Bonjour Browser
1007                     // For these queries, we retract the "._sub" we just added between the subtype and the main type
1008                     // Remove after Bonjour Browser is updated to use DNSServiceQueryRecord instead of DNSServiceBrowse
1009                     if (SameDomainName((domainname*)s0, (const domainname*)"\x09_services\x07_dns-sd\x04_udp"))
1010                         dst -= sizeof(SubTypeLabel);
1011                 }
1012             }
1013         }
1014     }
1015 
1016     if (name && name->c[0])
1017     {
1018         src = name->c;                                  // Put the service name into the domain name
1019         len = *src;
1020         if (len >= 0x40) { errormsg = "Service instance name too long"; goto fail; }
1021         for (i=0; i<=len; i++) *dst++ = *src++;
1022     }
1023     else
1024         name = (domainlabel*)"";    // Set this up to be non-null, to avoid errors if we have to call LogMsg() below
1025 
1026     src = type->c;                                      // Put the service type into the domain name
1027     len = *src;
1028     if (len < 2 || len > 16)
1029     {
1030         LogMsg("Bad service type in %#s.%##s%##s Application protocol name must be underscore plus 1-15 characters. "
1031                "See <http://www.dns-sd.org/ServiceTypes.html>", name->c, type->c, domain->c);
1032 #if APPLE_OSX_mDNSResponder
1033         ConvertDomainNameToCString(type, typeBuf);
1034         mDNSASLLog(mDNSNULL, "serviceType.nameTooLong", "noop", typeBuf, "");
1035 #endif
1036     }
1037     if (len < 2 || len >= 0x40 || (len > 16 && !SameDomainName(domain, &localdomain))) return(mDNSNULL);
1038     if (src[1] != '_') { errormsg = "Application protocol name must begin with underscore"; goto fail; }
1039     for (i=2; i<=len; i++)
1040     {
1041         // Letters and digits are allowed anywhere
1042         if (mDNSIsLetter(src[i]) || mDNSIsDigit(src[i])) continue;
1043         // Hyphens are only allowed as interior characters
1044         // Underscores are not supposed to be allowed at all, but for backwards compatibility with some old products we do allow them,
1045         // with the same rule as hyphens
1046         if ((src[i] == '-' || src[i] == '_') && i > 2 && i < len)
1047         {
1048 #if APPLE_OSX_mDNSResponder
1049             if (src[i] == '_' && loggedUnderscore == mDNSfalse)
1050             {
1051                 ConvertDomainNameToCString(type, typeBuf);
1052                 mDNSASLLog(mDNSNULL, "serviceType.nameWithUnderscore", "noop", typeBuf, "");
1053                 loggedUnderscore = mDNStrue;
1054             }
1055 #endif
1056             continue;
1057         }
1058         errormsg = "Application protocol name must contain only letters, digits, and hyphens";
1059 #if APPLE_OSX_mDNSResponder
1060         {
1061             ConvertDomainNameToCString(type, typeBuf);
1062             mDNSASLLog(mDNSNULL, "serviceType.nameWithIllegalCharacters", "noop", typeBuf, "");
1063         }
1064 #endif
1065         goto fail;
1066     }
1067     for (i=0; i<=len; i++) *dst++ = *src++;
1068 
1069     len = *src;
1070     if (!ValidTransportProtocol(src)) { errormsg = "Transport protocol name must be _udp or _tcp"; goto fail; }
1071     for (i=0; i<=len; i++) *dst++ = *src++;
1072 
1073     if (*src) { errormsg = "Service type must have only two labels"; goto fail; }
1074 
1075     *dst = 0;
1076     if (!domain->c[0]) { errormsg = "Service domain must be non-empty"; goto fail; }
1077     if (SameDomainName(domain, (const domainname*)"\x05" "local" "\x04" "arpa"))
1078     { errormsg = "Illegal domain \"local.arpa.\" Use \"local.\" (or empty string)"; goto fail; }
1079     dst = AppendDomainName(fqdn, domain);
1080     if (!dst) { errormsg = "Service domain too long"; goto fail; }
1081     return(dst);
1082 
1083 fail:
1084     LogMsg("ConstructServiceName: %s: %#s.%##s%##s", errormsg, name->c, type->c, domain->c);
1085     return(mDNSNULL);
1086 }
1087 
1088 // A service name has the form: instance.application-protocol.transport-protocol.domain
1089 // DeconstructServiceName is currently fairly forgiving: It doesn't try to enforce character
1090 // set or length limits for the protocol names, and the final domain is allowed to be empty.
1091 // However, if the given FQDN doesn't contain at least three labels,
1092 // DeconstructServiceName will reject it and return mDNSfalse.
1093 mDNSexport mDNSBool DeconstructServiceName(const domainname *const fqdn,
1094                                            domainlabel *const name, domainname *const type, domainname *const domain)
1095 {
1096     int i, len;
1097     const mDNSu8 *src = fqdn->c;
1098     const mDNSu8 *max = fqdn->c + MAX_DOMAIN_NAME;
1099     mDNSu8 *dst;
1100 
1101     dst = name->c;                                      // Extract the service name
1102     len = *src;
1103     if (!len)         { debugf("DeconstructServiceName: FQDN empty!");                             return(mDNSfalse); }
1104     if (len >= 0x40)  { debugf("DeconstructServiceName: Instance name too long");                  return(mDNSfalse); }
1105     for (i=0; i<=len; i++) *dst++ = *src++;
1106 
1107     dst = type->c;                                      // Extract the service type
1108     len = *src;
1109     if (!len)         { debugf("DeconstructServiceName: FQDN contains only one label!");           return(mDNSfalse); }
1110     if (len >= 0x40)  { debugf("DeconstructServiceName: Application protocol name too long");      return(mDNSfalse); }
1111     if (src[1] != '_') { debugf("DeconstructServiceName: No _ at start of application protocol");   return(mDNSfalse); }
1112     for (i=0; i<=len; i++) *dst++ = *src++;
1113 
1114     len = *src;
1115     if (!len)         { debugf("DeconstructServiceName: FQDN contains only two labels!");          return(mDNSfalse); }
1116     if (!ValidTransportProtocol(src))
1117     { debugf("DeconstructServiceName: Transport protocol must be _udp or _tcp"); return(mDNSfalse); }
1118     for (i=0; i<=len; i++) *dst++ = *src++;
1119     *dst++ = 0;                                         // Put terminator on the end of service type
1120 
1121     dst = domain->c;                                    // Extract the service domain
1122     while (*src)
1123     {
1124         len = *src;
1125         if (len >= 0x40)
1126         { debugf("DeconstructServiceName: Label in service domain too long"); return(mDNSfalse); }
1127         if (src + 1 + len + 1 >= max)
1128         { debugf("DeconstructServiceName: Total service domain too long"); return(mDNSfalse); }
1129         for (i=0; i<=len; i++) *dst++ = *src++;
1130     }
1131     *dst++ = 0;     // Put the null root label on the end
1132 
1133     return(mDNStrue);
1134 }
1135 
1136 mDNSexport mStatus DNSNameToLowerCase(domainname *d, domainname *result)
1137 {
1138     const mDNSu8 *a = d->c;
1139     mDNSu8 *b = result->c;
1140     const mDNSu8 *const max = d->c + MAX_DOMAIN_NAME;
1141     int i, len;
1142 
1143     while (*a)
1144     {
1145         if (a + 1 + *a >= max)
1146         {
1147             LogMsg("DNSNameToLowerCase: ERROR!! Malformed Domain name");
1148             return mStatus_BadParamErr;
1149         }
1150         len = *a++;
1151         *b++ = len;
1152         for (i = 0; i < len; i++)
1153         {
1154             mDNSu8 ac = *a++;
1155             if (mDNSIsUpperCase(ac)) ac += 'a' - 'A';
1156             *b++ = ac;
1157         }
1158     }
1159     *b = 0;
1160 
1161     return mStatus_NoError;
1162 }
1163 
1164 mDNSexport const mDNSu8 *NSEC3HashName(const domainname *name, rdataNSEC3 *nsec3, const mDNSu8 *AnonData, int AnonDataLen,
1165     const mDNSu8 hash[NSEC3_MAX_HASH_LEN], int *dlen)
1166 {
1167     AlgContext *ctx;
1168     int i;
1169     domainname lname;
1170     mDNSu8 *p = (mDNSu8 *)&nsec3->salt;
1171     const mDNSu8 *digest;
1172     int digestlen;
1173     mDNSBool first = mDNStrue;
1174 
1175     if (DNSNameToLowerCase((domainname *)name, &lname) != mStatus_NoError)
1176     {
1177         LogMsg("NSEC3HashName: ERROR!! DNSNameToLowerCase failed");
1178         return mDNSNULL;
1179     }
1180 
1181     digest = lname.c;
1182     digestlen = DomainNameLength(&lname);
1183 
1184     // Note that it is "i <=". The first iteration is for digesting the name and salt.
1185     // The iteration count does not include that.
1186     for (i = 0; i <= swap16(nsec3->iterations); i++)
1187     {
1188         ctx = AlgCreate(DIGEST_ALG, nsec3->alg);
1189         if (!ctx)
1190         {
1191             LogMsg("NSEC3HashName: ERROR!! Cannot allocate context");
1192             return mDNSNULL;
1193         }
1194 
1195         AlgAdd(ctx, digest, digestlen);
1196         if (nsec3->saltLength)
1197             AlgAdd(ctx, p, nsec3->saltLength);
1198         if (AnonDataLen)
1199             AlgAdd(ctx, AnonData, AnonDataLen);
1200         if (first)
1201         {
1202             first = mDNSfalse;
1203             digest = hash;
1204             digestlen = AlgLength(ctx);
1205         }
1206         AlgFinal(ctx, (void *)digest, digestlen);
1207         AlgDestroy(ctx);
1208     }
1209     *dlen = digestlen;
1210     return digest;
1211 }
1212 
1213 // Notes on UTF-8:
1214 // 0xxxxxxx represents a 7-bit ASCII value from 0x00 to 0x7F
1215 // 10xxxxxx is a continuation byte of a multi-byte character
1216 // 110xxxxx is the first byte of a 2-byte character (11 effective bits; values 0x     80 - 0x     800-1)
1217 // 1110xxxx is the first byte of a 3-byte character (16 effective bits; values 0x    800 - 0x   10000-1)
1218 // 11110xxx is the first byte of a 4-byte character (21 effective bits; values 0x  10000 - 0x  200000-1)
1219 // 111110xx is the first byte of a 5-byte character (26 effective bits; values 0x 200000 - 0x 4000000-1)
1220 // 1111110x is the first byte of a 6-byte character (31 effective bits; values 0x4000000 - 0x80000000-1)
1221 //
1222 // UTF-16 surrogate pairs are used in UTF-16 to encode values larger than 0xFFFF.
1223 // Although UTF-16 surrogate pairs are not supposed to appear in legal UTF-8, we want to be defensive
1224 // about that too. (See <http://www.unicode.org/faq/utf_bom.html#34>, "What are surrogates?")
1225 // The first of pair is a UTF-16 value in the range 0xD800-0xDBFF (11101101 1010xxxx 10xxxxxx in UTF-8),
1226 // and the second    is a UTF-16 value in the range 0xDC00-0xDFFF (11101101 1011xxxx 10xxxxxx in UTF-8).
1227 
1228 mDNSexport mDNSu32 TruncateUTF8ToLength(mDNSu8 *string, mDNSu32 length, mDNSu32 max)
1229 {
1230     if (length > max)
1231     {
1232         mDNSu8 c1 = string[max];                                        // First byte after cut point
1233         mDNSu8 c2 = (max+1 < length) ? string[max+1] : (mDNSu8)0xB0;    // Second byte after cut point
1234         length = max;   // Trim length down
1235         while (length > 0)
1236         {
1237             // Check if the byte right after the chop point is a UTF-8 continuation byte,
1238             // or if the character right after the chop point is the second of a UTF-16 surrogate pair.
1239             // If so, then we continue to chop more bytes until we get to a legal chop point.
1240             mDNSBool continuation    = ((c1 & 0xC0) == 0x80);
1241             mDNSBool secondsurrogate = (c1 == 0xED && (c2 & 0xF0) == 0xB0);
1242             if (!continuation && !secondsurrogate) break;
1243             c2 = c1;
1244             c1 = string[--length];
1245         }
1246         // Having truncated characters off the end of our string, also cut off any residual white space
1247         while (length > 0 && string[length-1] <= ' ') length--;
1248     }
1249     return(length);
1250 }
1251 
1252 // Returns true if a rich text label ends in " (nnn)", or if an RFC 1034
1253 // name ends in "-nnn", where n is some decimal number.
1254 mDNSexport mDNSBool LabelContainsSuffix(const domainlabel *const name, const mDNSBool RichText)
1255 {
1256     mDNSu16 l = name->c[0];
1257 
1258     if (RichText)
1259     {
1260         if (l < 4) return mDNSfalse;                            // Need at least " (2)"
1261         if (name->c[l--] != ')') return mDNSfalse;              // Last char must be ')'
1262         if (!mDNSIsDigit(name->c[l])) return mDNSfalse;         // Preceeded by a digit
1263         l--;
1264         while (l > 2 && mDNSIsDigit(name->c[l])) l--;           // Strip off digits
1265         return (name->c[l] == '(' && name->c[l - 1] == ' ');
1266     }
1267     else
1268     {
1269         if (l < 2) return mDNSfalse;                            // Need at least "-2"
1270         if (!mDNSIsDigit(name->c[l])) return mDNSfalse;         // Last char must be a digit
1271         l--;
1272         while (l > 2 && mDNSIsDigit(name->c[l])) l--;           // Strip off digits
1273         return (name->c[l] == '-');
1274     }
1275 }
1276 
1277 // removes an auto-generated suffix (appended on a name collision) from a label.  caller is
1278 // responsible for ensuring that the label does indeed contain a suffix.  returns the number
1279 // from the suffix that was removed.
1280 mDNSexport mDNSu32 RemoveLabelSuffix(domainlabel *name, mDNSBool RichText)
1281 {
1282     mDNSu32 val = 0, multiplier = 1;
1283 
1284     // Chop closing parentheses from RichText suffix
1285     if (RichText && name->c[0] >= 1 && name->c[name->c[0]] == ')') name->c[0]--;
1286 
1287     // Get any existing numerical suffix off the name
1288     while (mDNSIsDigit(name->c[name->c[0]]))
1289     { val += (name->c[name->c[0]] - '0') * multiplier; multiplier *= 10; name->c[0]--; }
1290 
1291     // Chop opening parentheses or dash from suffix
1292     if (RichText)
1293     {
1294         if (name->c[0] >= 2 && name->c[name->c[0]] == '(' && name->c[name->c[0]-1] == ' ') name->c[0] -= 2;
1295     }
1296     else
1297     {
1298         if (name->c[0] >= 1 && name->c[name->c[0]] == '-') name->c[0] -= 1;
1299     }
1300 
1301     return(val);
1302 }
1303 
1304 // appends a numerical suffix to a label, with the number following a whitespace and enclosed
1305 // in parentheses (rich text) or following two consecutive hyphens (RFC 1034 domain label).
1306 mDNSexport void AppendLabelSuffix(domainlabel *const name, mDNSu32 val, const mDNSBool RichText)
1307 {
1308     mDNSu32 divisor = 1, chars = 2; // Shortest possible RFC1034 name suffix is 2 characters ("-2")
1309     if (RichText) chars = 4;        // Shortest possible RichText suffix is 4 characters (" (2)")
1310 
1311     // Truncate trailing spaces from RichText names
1312     if (RichText) while (name->c[name->c[0]] == ' ') name->c[0]--;
1313 
1314     while (divisor < 0xFFFFFFFFUL/10 && val >= divisor * 10) { divisor *= 10; chars++; }
1315 
1316     name->c[0] = (mDNSu8) TruncateUTF8ToLength(name->c+1, name->c[0], MAX_DOMAIN_LABEL - chars);
1317 
1318     if (RichText) { name->c[++name->c[0]] = ' '; name->c[++name->c[0]] = '('; }
1319     else          { name->c[++name->c[0]] = '-'; }
1320 
1321     while (divisor)
1322     {
1323         name->c[++name->c[0]] = (mDNSu8)('0' + val / divisor);
1324         val     %= divisor;
1325         divisor /= 10;
1326     }
1327 
1328     if (RichText) name->c[++name->c[0]] = ')';
1329 }
1330 
1331 mDNSexport void IncrementLabelSuffix(domainlabel *name, mDNSBool RichText)
1332 {
1333     mDNSu32 val = 0;
1334 
1335     if (LabelContainsSuffix(name, RichText))
1336         val = RemoveLabelSuffix(name, RichText);
1337 
1338     // If no existing suffix, start by renaming "Foo" as "Foo (2)" or "Foo-2" as appropriate.
1339     // If existing suffix in the range 2-9, increment it.
1340     // If we've had ten conflicts already, there are probably too many hosts trying to use the same name,
1341     // so add a random increment to improve the chances of finding an available name next time.
1342     if      (val == 0) val = 2;
1343     else if (val < 10) val++;
1344     else val += 1 + mDNSRandom(99);
1345 
1346     AppendLabelSuffix(name, val, RichText);
1347 }
1348 
1349 // ***************************************************************************
1350 #if COMPILER_LIKES_PRAGMA_MARK
1351 #pragma mark -
1352 #pragma mark - Resource Record Utility Functions
1353 #endif
1354 
1355 // Set up a AuthRecord with sensible default values.
1356 // These defaults may be overwritten with new values before mDNS_Register is called
1357 mDNSexport void mDNS_SetupResourceRecord(AuthRecord *rr, RData *RDataStorage, mDNSInterfaceID InterfaceID,
1358                                          mDNSu16 rrtype, mDNSu32 ttl, mDNSu8 RecordType, AuthRecType artype, mDNSRecordCallback Callback, void *Context)
1359 {
1360     //
1361     // LocalOnly auth record can be created with LocalOnly InterfaceID or a valid InterfaceID.
1362     // Most of the applications normally create with LocalOnly InterfaceID and we store them as
1363     // such, so that we can deliver the response to questions that specify LocalOnly InterfaceID.
1364     // LocalOnly resource records can also be created with valid InterfaceID which happens today
1365     // when we create LocalOnly records for /etc/hosts.
1366 
1367     if (InterfaceID == mDNSInterface_LocalOnly && artype != AuthRecordLocalOnly)
1368     {
1369         LogMsg("mDNS_SetupResourceRecord: ERROR!! Mismatch LocalOnly record InterfaceID %p called with artype %d", InterfaceID, artype);
1370         return;
1371     }
1372     else if (InterfaceID == mDNSInterface_P2P && artype != AuthRecordP2P)
1373     {
1374         LogMsg("mDNS_SetupResourceRecord: ERROR!! Mismatch P2P record InterfaceID %p called with artype %d", InterfaceID, artype);
1375         return;
1376     }
1377     else if (!InterfaceID && (artype == AuthRecordP2P || artype == AuthRecordLocalOnly))
1378     {
1379         LogMsg("mDNS_SetupResourceRecord: ERROR!! Mismatch InterfaceAny record InterfaceID %p called with artype %d", InterfaceID, artype);
1380         return;
1381     }
1382 
1383     // Don't try to store a TTL bigger than we can represent in platform time units
1384     if (ttl > 0x7FFFFFFFUL / mDNSPlatformOneSecond)
1385         ttl = 0x7FFFFFFFUL / mDNSPlatformOneSecond;
1386     else if (ttl == 0)      // And Zero TTL is illegal
1387         ttl = DefaultTTLforRRType(rrtype);
1388 
1389     // Field Group 1: The actual information pertaining to this resource record
1390     rr->resrec.RecordType        = RecordType;
1391     rr->resrec.InterfaceID       = InterfaceID;
1392     rr->resrec.name              = &rr->namestorage;
1393     rr->resrec.rrtype            = rrtype;
1394     rr->resrec.rrclass           = kDNSClass_IN;
1395     rr->resrec.rroriginalttl     = ttl;
1396     rr->resrec.rDNSServer        = mDNSNULL;
1397     rr->resrec.AnonInfo          = mDNSNULL;
1398 //	rr->resrec.rdlength          = MUST set by client and/or in mDNS_Register_internal
1399 //	rr->resrec.rdestimate        = set in mDNS_Register_internal
1400 //	rr->resrec.rdata             = MUST be set by client
1401 
1402     if (RDataStorage)
1403         rr->resrec.rdata = RDataStorage;
1404     else
1405     {
1406         rr->resrec.rdata = &rr->rdatastorage;
1407         rr->resrec.rdata->MaxRDLength = sizeof(RDataBody);
1408     }
1409 
1410     // Field Group 2: Persistent metadata for Authoritative Records
1411     rr->Additional1       = mDNSNULL;
1412     rr->Additional2       = mDNSNULL;
1413     rr->DependentOn       = mDNSNULL;
1414     rr->RRSet             = mDNSNULL;
1415     rr->RecordCallback    = Callback;
1416     rr->RecordContext     = Context;
1417 
1418     rr->AutoTarget        = Target_Manual;
1419     rr->AllowRemoteQuery  = mDNSfalse;
1420     rr->ForceMCast        = mDNSfalse;
1421 
1422     rr->WakeUp            = zeroOwner;
1423     rr->AddressProxy      = zeroAddr;
1424     rr->TimeRcvd          = 0;
1425     rr->TimeExpire        = 0;
1426     rr->ARType            = artype;
1427     rr->AuthFlags         = 0;
1428 
1429     // Field Group 3: Transient state for Authoritative Records (set in mDNS_Register_internal)
1430     // Field Group 4: Transient uDNS state for Authoritative Records (set in mDNS_Register_internal)
1431 
1432     // For now, until the uDNS code is fully integrated, it's helpful to zero the uDNS state fields here too, just in case
1433     // (e.g. uDNS_RegisterService short-circuits the usual mDNS_Register_internal record registration calls, so a bunch
1434     // of fields don't get set up properly. In particular, if we don't zero rr->QueuedRData then the uDNS code crashes.)
1435     rr->state             = regState_Zero;
1436     rr->uselease          = 0;
1437     rr->expire            = 0;
1438     rr->Private           = 0;
1439     rr->updateid          = zeroID;
1440     rr->zone              = rr->resrec.name;
1441     rr->nta               = mDNSNULL;
1442     rr->tcp               = mDNSNULL;
1443     rr->OrigRData         = 0;
1444     rr->OrigRDLen         = 0;
1445     rr->InFlightRData     = 0;
1446     rr->InFlightRDLen     = 0;
1447     rr->QueuedRData       = 0;
1448     rr->QueuedRDLen       = 0;
1449     mDNSPlatformMemZero(&rr->NATinfo, sizeof(rr->NATinfo));
1450     rr->SRVChanged = mDNSfalse;
1451     rr->mState = mergeState_Zero;
1452 
1453     rr->namestorage.c[0]  = 0;      // MUST be set by client before calling mDNS_Register()
1454 }
1455 
1456 mDNSexport void mDNS_SetupQuestion(DNSQuestion *const q, const mDNSInterfaceID InterfaceID, const domainname *const name,
1457                                    const mDNSu16 qtype, mDNSQuestionCallback *const callback, void *const context)
1458 {
1459     q->InterfaceID         = InterfaceID;
1460     q->flags               = 0;
1461     q->Target              = zeroAddr;
1462     AssignDomainName(&q->qname, name);
1463     q->qtype               = qtype;
1464     q->qclass              = kDNSClass_IN;
1465     q->LongLived           = (qtype == kDNSType_PTR);
1466     q->ExpectUnique        = (qtype != kDNSType_PTR);
1467     q->ForceMCast          = mDNSfalse;
1468     q->ReturnIntermed      = mDNSfalse;
1469     q->SuppressUnusable    = mDNSfalse;
1470     q->DenyOnCellInterface = mDNSfalse;
1471     q->DenyOnExpInterface  = mDNSfalse;
1472     q->SearchListIndex     = 0;
1473     q->AppendSearchDomains = 0;
1474     q->RetryWithSearchDomains = mDNSfalse;
1475     q->TimeoutQuestion     = 0;
1476     q->WakeOnResolve       = 0;
1477     q->UseBackgroundTrafficClass = mDNSfalse;
1478     q->ValidationRequired  = 0;
1479     q->ValidatingResponse  = 0;
1480     q->ProxyQuestion       = 0;
1481     q->qnameOrig           = mDNSNULL;
1482     q->AnonInfo            = mDNSNULL;
1483     q->pid                 = mDNSPlatformGetPID();
1484     q->DisallowPID         = mDNSfalse;
1485     q->ServiceID           = -1;
1486     q->QuestionCallback    = callback;
1487     q->QuestionContext     = context;
1488 }
1489 
1490 mDNSexport mDNSu32 RDataHashValue(const ResourceRecord *const rr)
1491 {
1492     int len = rr->rdlength;
1493     const RDataBody2 *const rdb = (RDataBody2 *)rr->rdata->u.data;
1494     const mDNSu8 *ptr = rdb->data;
1495     mDNSu32 sum = 0;
1496 
1497     switch(rr->rrtype)
1498     {
1499     case kDNSType_NS:
1500     case kDNSType_MD:
1501     case kDNSType_MF:
1502     case kDNSType_CNAME:
1503     case kDNSType_MB:
1504     case kDNSType_MG:
1505     case kDNSType_MR:
1506     case kDNSType_PTR:
1507     case kDNSType_NSAP_PTR:
1508     case kDNSType_DNAME: return DomainNameHashValue(&rdb->name);
1509 
1510     case kDNSType_SOA:   return rdb->soa.serial  +
1511                rdb->soa.refresh +
1512                rdb->soa.retry   +
1513                rdb->soa.expire  +
1514                rdb->soa.min     +
1515                DomainNameHashValue(&rdb->soa.mname) +
1516                DomainNameHashValue(&rdb->soa.rname);
1517 
1518     case kDNSType_MX:
1519     case kDNSType_AFSDB:
1520     case kDNSType_RT:
1521     case kDNSType_KX:    return DomainNameHashValue(&rdb->mx.exchange);
1522 
1523     case kDNSType_MINFO:
1524     case kDNSType_RP:    return DomainNameHashValue(&rdb->rp.mbox)   + DomainNameHashValue(&rdb->rp.txt);
1525 
1526     case kDNSType_PX:    return DomainNameHashValue(&rdb->px.map822) + DomainNameHashValue(&rdb->px.mapx400);
1527 
1528     case kDNSType_SRV:   return DomainNameHashValue(&rdb->srv.target);
1529 
1530     case kDNSType_OPT:   return 0;      // OPT is a pseudo-RR container structure; makes no sense to compare
1531 
1532     case kDNSType_NSEC: {
1533         int dlen;
1534         dlen = DomainNameLength((domainname *)rdb->data);
1535         sum = DomainNameHashValue((domainname *)rdb->data);
1536         ptr += dlen;
1537         len -= dlen;
1538         /* FALLTHROUGH */
1539     }
1540 
1541     default:
1542     {
1543         int i;
1544         for (i=0; i+1 < len; i+=2)
1545         {
1546             sum += (((mDNSu32)(ptr[i])) << 8) | ptr[i+1];
1547             sum = (sum<<3) | (sum>>29);
1548         }
1549         if (i < len)
1550         {
1551             sum += ((mDNSu32)(ptr[i])) << 8;
1552         }
1553         return(sum);
1554     }
1555     }
1556 }
1557 
1558 // r1 has to be a full ResourceRecord including rrtype and rdlength
1559 // r2 is just a bare RDataBody, which MUST be the same rrtype and rdlength as r1
1560 mDNSexport mDNSBool SameRDataBody(const ResourceRecord *const r1, const RDataBody *const r2, DomainNameComparisonFn *samename)
1561 {
1562     const RDataBody2 *const b1 = (RDataBody2 *)r1->rdata->u.data;
1563     const RDataBody2 *const b2 = (RDataBody2 *)r2;
1564     switch(r1->rrtype)
1565     {
1566     case kDNSType_NS:
1567     case kDNSType_MD:
1568     case kDNSType_MF:
1569     case kDNSType_CNAME:
1570     case kDNSType_MB:
1571     case kDNSType_MG:
1572     case kDNSType_MR:
1573     case kDNSType_PTR:
1574     case kDNSType_NSAP_PTR:
1575     case kDNSType_DNAME: return(SameDomainName(&b1->name, &b2->name));
1576 
1577     case kDNSType_SOA:  return (mDNSBool)(   b1->soa.serial   == b2->soa.serial             &&
1578                                              b1->soa.refresh  == b2->soa.refresh            &&
1579                                              b1->soa.retry    == b2->soa.retry              &&
1580                                              b1->soa.expire   == b2->soa.expire             &&
1581                                              b1->soa.min      == b2->soa.min                &&
1582                                              samename(&b1->soa.mname, &b2->soa.mname) &&
1583                                              samename(&b1->soa.rname, &b2->soa.rname));
1584 
1585     case kDNSType_MX:
1586     case kDNSType_AFSDB:
1587     case kDNSType_RT:
1588     case kDNSType_KX:   return (mDNSBool)(   b1->mx.preference == b2->mx.preference &&
1589                                              samename(&b1->mx.exchange, &b2->mx.exchange));
1590 
1591     case kDNSType_MINFO:
1592     case kDNSType_RP:   return (mDNSBool)(   samename(&b1->rp.mbox, &b2->rp.mbox) &&
1593                                              samename(&b1->rp.txt,  &b2->rp.txt));
1594 
1595     case kDNSType_PX:   return (mDNSBool)(   b1->px.preference == b2->px.preference          &&
1596                                              samename(&b1->px.map822,  &b2->px.map822) &&
1597                                              samename(&b1->px.mapx400, &b2->px.mapx400));
1598 
1599     case kDNSType_SRV:  return (mDNSBool)(   b1->srv.priority == b2->srv.priority       &&
1600                                              b1->srv.weight   == b2->srv.weight         &&
1601                                              mDNSSameIPPort(b1->srv.port, b2->srv.port) &&
1602                                              samename(&b1->srv.target, &b2->srv.target));
1603 
1604     case kDNSType_OPT:  return mDNSfalse;       // OPT is a pseudo-RR container structure; makes no sense to compare
1605     case kDNSType_NSEC: {
1606         // If the "nxt" name changes in case, we want to delete the old
1607         // and store just the new one. If the caller passes in SameDomainCS for "samename",
1608         // we would return "false" when the only change between the two rdata is the case
1609         // change in "nxt".
1610         //
1611         // Note: rdlength of both the RData are same (ensured by the caller) and hence we can
1612         // use just r1->rdlength below
1613 
1614         int dlen1 = DomainNameLength((domainname *)b1->data);
1615         int dlen2 = DomainNameLength((domainname *)b2->data);
1616         return (mDNSBool)(dlen1 == dlen2 &&
1617                           samename((domainname *)b1->data, (domainname *)b2->data) &&
1618                           mDNSPlatformMemSame(b1->data + dlen1, b2->data + dlen2, r1->rdlength - dlen1));
1619     }
1620 
1621     default:            return(mDNSPlatformMemSame(b1->data, b2->data, r1->rdlength));
1622     }
1623 }
1624 
1625 mDNSexport mDNSBool BitmapTypeCheck(mDNSu8 *bmap, int bitmaplen, mDNSu16 type)
1626 {
1627     int win, wlen;
1628     int wintype;
1629 
1630     // The window that this type belongs to. NSEC has 256 windows that
1631     // comprises of 256 types.
1632     wintype = type >> 8;
1633 
1634     while (bitmaplen > 0)
1635     {
1636         if (bitmaplen < 3)
1637         {
1638             LogInfo("BitmapTypeCheck: malformed nsec, bitmaplen %d short", bitmaplen);
1639             return mDNSfalse;
1640         }
1641 
1642         win = *bmap++;
1643         wlen = *bmap++;
1644         bitmaplen -= 2;
1645         if (bitmaplen < wlen || wlen < 1 || wlen > 32)
1646         {
1647             LogInfo("BitmapTypeCheck: malformed nsec, bitmaplen %d wlen %d, win %d", bitmaplen, wlen, win);
1648             return mDNSfalse;
1649         }
1650         if (win < 0 || win >= 256)
1651         {
1652             LogInfo("BitmapTypeCheck: malformed nsec, wlen %d", wlen);
1653             return mDNSfalse;
1654         }
1655         if (win == wintype)
1656         {
1657             // First byte in the window serves 0 to 7, the next one serves 8 to 15 and so on.
1658             // Calculate the right byte offset first.
1659             int boff = (type & 0xff ) >> 3;
1660             if (wlen <= boff)
1661                 return mDNSfalse;
1662             // The last three bits values 0 to 7 corresponds to bit positions
1663             // within the byte.
1664             return (bmap[boff] & (0x80 >> (type & 7)));
1665         }
1666         else
1667         {
1668             // If the windows are ordered, then we could check to see
1669             // if wintype > win and then return early.
1670             bmap += wlen;
1671             bitmaplen -= wlen;
1672         }
1673     }
1674     return mDNSfalse;
1675 }
1676 
1677 // Don't call this function if the resource record is not NSEC. It will return false
1678 // which means that the type does not exist.
1679 mDNSexport mDNSBool RRAssertsExistence(const ResourceRecord *const rr, mDNSu16 type)
1680 {
1681     const RDataBody2 *const rdb = (RDataBody2 *)rr->rdata->u.data;
1682     mDNSu8 *nsec = (mDNSu8 *)rdb->data;
1683     int len, bitmaplen;
1684     mDNSu8 *bmap;
1685 
1686     if (rr->rrtype != kDNSType_NSEC) return mDNSfalse;
1687 
1688     len = DomainNameLength((domainname *)nsec);
1689 
1690     bitmaplen = rr->rdlength - len;
1691     bmap = nsec + len;
1692     return (BitmapTypeCheck(bmap, bitmaplen, type));
1693 }
1694 
1695 // Don't call this function if the resource record is not NSEC. It will return false
1696 // which means that the type exists.
1697 mDNSexport mDNSBool RRAssertsNonexistence(const ResourceRecord *const rr, mDNSu16 type)
1698 {
1699     if (rr->rrtype != kDNSType_NSEC) return mDNSfalse;
1700 
1701     return !RRAssertsExistence(rr, type);
1702 }
1703 
1704 // Checks whether the RRSIG or NSEC record answers the question "q".
1705 mDNSlocal mDNSBool DNSSECRecordAnswersQuestion(const ResourceRecord *const rr, const DNSQuestion *const q, mDNSBool *checkType)
1706 {
1707     *checkType = mDNStrue;
1708 
1709     // This function is called for all questions and as long as the type matches,
1710     // return true. For the types (RRSIG and NSEC) that are specifically checked in
1711     // this function, returning true still holds good.
1712     if (q->qtype == rr->rrtype)
1713         return mDNStrue;
1714 
1715     // For DS and DNSKEY questions, the types should match i.e., don't answer using CNAME
1716     // records as it answers any question type.
1717     //
1718     // - DS record comes from the parent zone where CNAME record cannot coexist and hence
1719     //  cannot possibly answer it.
1720     //
1721     // - For DNSKEY, one could potentially follow CNAME but there could be a DNSKEY at
1722     //   the "qname" itself. To keep it simple, we don't follow CNAME.
1723 
1724     if ((q->qtype == kDNSType_DS || q->qtype == kDNSType_DNSKEY) && (q->qtype != rr->rrtype))
1725     {
1726         debugf("DNSSECRecordAnswersQuestion: %d type resource record matched question %##s (%s), ignoring", rr->rrtype,
1727             q->qname.c, DNSTypeName(q->qtype));
1728         return mDNSfalse;
1729     }
1730 
1731     // If we are validating a response using DNSSEC, we might already have the records
1732     // for the "q->qtype" in the cache but we issued a query with DO bit set
1733     // to get the RRSIGs e.g., if you have two questions one of which does not require
1734     // DNSSEC validation. When the RRSIG is added to the cache, we need to deliver
1735     // the response to the question. The RRSIG type won't match the q->qtype and hence
1736     // we need to bypass the check in that case.
1737     if (rr->rrtype == kDNSType_RRSIG && q->ValidatingResponse)
1738     {
1739         const RDataBody2 *const rdb = (RDataBody2 *)rr->rdata->u.data;
1740         rdataRRSig *rrsig = (rdataRRSig *)rdb->data;
1741         mDNSu16 typeCovered = swap16(rrsig->typeCovered);
1742         debugf("DNSSECRecordAnswersQuestion: Matching RRSIG typeCovered %s", DNSTypeName(typeCovered));
1743         if (typeCovered != kDNSType_CNAME && typeCovered != q->qtype)
1744         {
1745             debugf("DNSSECRecordAnswersQuestion: RRSIG did not match question %##s (%s)", q->qname.c,
1746                     DNSTypeName(q->qtype));
1747             return mDNSfalse;
1748         }
1749         LogInfo("DNSSECRecordAnswersQuestion: RRSIG matched question %##s (%s)", q->qname.c,
1750                 DNSTypeName(q->qtype));
1751         *checkType = mDNSfalse;
1752         return mDNStrue;
1753     }
1754     // If the NSEC record asserts the non-existence of a name looked up by the question, we would
1755     // typically answer that e.g., the bitmap asserts that q->qtype does not exist. If we have
1756     // to prove the non-existence as required by ValidatingResponse and ValidationRequired question,
1757     // then we should not answer that as it may not be the right one always. We may need more than
1758     // one NSEC to prove the non-existence.
1759     if (rr->rrtype == kDNSType_NSEC && DNSSECQuestion(q))
1760     {
1761         debugf("DNSSECRecordAnswersQuestion: Question %##s (%s) matched record %##s (NSEC)", q->qname.c,
1762                 DNSTypeName(q->qtype), rr->name->c);
1763         return mDNSfalse;
1764     }
1765     return mDNStrue;
1766 }
1767 
1768 // ResourceRecordAnswersQuestion returns mDNStrue if the given resource record is a valid answer to the given question.
1769 // SameNameRecordAnswersQuestion is the same, except it skips the expensive SameDomainName() call.
1770 // SameDomainName() is generally cheap when the names don't match, but expensive when they do match,
1771 // because it has to check all the way to the end of the names to be sure.
1772 // In cases where we know in advance that the names match it's especially advantageous to skip the
1773 // SameDomainName() call because that's precisely the time when it's most expensive and least useful.
1774 
1775 mDNSexport mDNSBool SameNameRecordAnswersQuestion(const ResourceRecord *const rr, const DNSQuestion *const q)
1776 {
1777     mDNSBool checkType = mDNStrue;
1778 
1779     // LocalOnly/P2P questions can be answered with AuthRecordAny in this function. LocalOnly/P2P records
1780     // are handled in LocalOnlyRecordAnswersQuestion
1781     if ((rr->InterfaceID == mDNSInterface_LocalOnly) || (rr->InterfaceID == mDNSInterface_P2P))
1782     {
1783         LogMsg("SameNameRecordAnswersQuestion: ERROR!! called with LocalOnly ResourceRecord %p, Question %p", rr->InterfaceID, q->InterfaceID);
1784         return mDNSfalse;
1785     }
1786     if (QuerySuppressed(q))
1787         return mDNSfalse;
1788 
1789     if (rr->InterfaceID &&
1790         q->InterfaceID && q->InterfaceID != mDNSInterface_LocalOnly &&
1791         rr->InterfaceID != q->InterfaceID) return(mDNSfalse);
1792 
1793     // Resource record received via unicast, the resolver group ID should match ?
1794     if (!rr->InterfaceID)
1795     {
1796         mDNSu16 idr = (rr->rDNSServer ? rr->rDNSServer->resGroupID : 0);
1797         mDNSu16 idq = (q->qDNSServer ? q->qDNSServer->resGroupID : 0);
1798         if (idr != idq) return(mDNSfalse);
1799         if (!DNSSECRecordAnswersQuestion(rr, q, &checkType)) return mDNSfalse;
1800     }
1801 
1802     // If ResourceRecord received via multicast, but question was unicast, then shouldn't use record to answer this question
1803     if (rr->InterfaceID && !mDNSOpaque16IsZero(q->TargetQID)) return(mDNSfalse);
1804 
1805     // CNAME answers question of any type and a negative cache record should not prevent us from querying other
1806     // valid types at the same name.
1807     if (rr->rrtype == kDNSType_CNAME && rr->RecordType == kDNSRecordTypePacketNegative && rr->rrtype != q->qtype)
1808         return mDNSfalse;
1809 
1810     // RR type CNAME matches any query type. QTYPE ANY matches any RR type. QCLASS ANY matches any RR class.
1811     if (checkType && !RRTypeAnswersQuestionType(rr,q->qtype)) return(mDNSfalse);
1812     if (rr->rrclass != q->qclass && q->qclass != kDNSQClass_ANY) return(mDNSfalse);
1813 
1814 #if APPLE_OSX_mDNSResponder
1815     if (!mDNSPlatformValidRecordForQuestion(rr, q))
1816         return mDNSfalse;
1817 #endif // APPLE_OSX_mDNSResponder
1818 
1819     if (!AnonInfoAnswersQuestion(rr, q))
1820         return mDNSfalse;
1821 
1822     return(mDNStrue);
1823 }
1824 
1825 mDNSexport mDNSBool ResourceRecordAnswersQuestion(const ResourceRecord *const rr, const DNSQuestion *const q)
1826 {
1827     if (!SameNameRecordAnswersQuestion(rr, q))
1828         return mDNSfalse;
1829 
1830     return(rr->namehash == q->qnamehash && SameDomainName(rr->name, &q->qname));
1831 }
1832 
1833 // We have a separate function to handle LocalOnly AuthRecords because they can be created with
1834 // a valid InterfaceID (e.g., scoped /etc/hosts) and can be used to answer unicast questions unlike
1835 // multicast resource records (which has a valid InterfaceID) which can't be used to answer
1836 // unicast questions. ResourceRecordAnswersQuestion/SameNameRecordAnswersQuestion can't tell whether
1837 // a resource record is multicast or LocalOnly by just looking at the ResourceRecord because
1838 // LocalOnly records are truly identified by ARType in the AuthRecord.  As P2P and LocalOnly record
1839 // are kept in the same hash table, we use the same function to make it easy for the callers when
1840 // they walk the hash table to answer LocalOnly/P2P questions
1841 //
1842 mDNSexport mDNSBool LocalOnlyRecordAnswersQuestion(AuthRecord *const ar, const DNSQuestion *const q)
1843 {
1844     ResourceRecord *rr = &ar->resrec;
1845 
1846     // mDNSInterface_Any questions can be answered with LocalOnly/P2P records in this function. AuthRecord_Any
1847     // records are handled in ResourceRecordAnswersQuestion/SameNameRecordAnswersQuestion
1848     if (RRAny(ar))
1849     {
1850         LogMsg("LocalOnlyRecordAnswersQuestion: ERROR!! called with regular AuthRecordAny %##s", rr->name->c);
1851         return mDNSfalse;
1852     }
1853 
1854     // Questions with mDNSInterface_LocalOnly InterfaceID should be answered with all resource records that are
1855     // *local* to the machine. These include resource records that have InterfaceID set to mDNSInterface_LocalOnly,
1856     // mDNSInterface_Any and any other real InterfaceID. Hence, LocalOnly questions should not be checked against
1857     // the InterfaceID in the resource record.
1858     //
1859     // mDNSInterface_Unicast does not indicate any scope and hence treat them like mDNSInterface_Any.
1860 
1861     if (rr->InterfaceID &&
1862         q->InterfaceID && q->InterfaceID != mDNSInterface_LocalOnly && q->InterfaceID != mDNSInterface_Unicast &&
1863         rr->InterfaceID != q->InterfaceID) return(mDNSfalse);
1864 
1865     // Entries in /etc/hosts are added as LocalOnly resource records. The LocalOnly resource records
1866     // may have a scope e.g., fe80::1%en0. The question may be scoped or not: the InterfaceID may be set
1867     // to mDNSInterface_Any, mDNSInterface_LocalOnly or a real InterfaceID (scoped).
1868     //
1869     // 1) Question: Any, LocalOnly Record: no scope. This question should be answered with this record.
1870     //
1871     // 2) Question: Any, LocalOnly Record: scoped.  This question should be answered with the record because
1872     //    traditionally applications never specify scope e.g., getaddrinfo, but need to be able
1873     //    to get to /etc/hosts entries.
1874     //
1875     // 3) Question: Scoped (LocalOnly or InterfaceID), LocalOnly Record: no scope. This is the inverse of (2).
1876     //    If we register a LocalOnly record, we need to answer a LocalOnly question. If the /etc/hosts has a
1877     //    non scoped entry, it may not make sense to answer a scoped question. But we can't tell these two
1878     //    cases apart. As we currently answer LocalOnly question with LocalOnly record, we continue to do so.
1879     //
1880     // 4) Question: Scoped (LocalOnly or InterfaceID), LocalOnly Record: scoped. LocalOnly questions should be
1881     //    answered with any resource record where as if it has a valid InterfaceID, the scope should match.
1882     //
1883     // (1) and (2) is bypassed because we check for a non-NULL InterfaceID above. For (3), the InterfaceID is NULL
1884     // and hence bypassed above. For (4) we bypassed LocalOnly questions and checked the scope of the record
1885     // against the question.
1886     //
1887     // For P2P, InterfaceIDs of the question and the record should match.
1888 
1889     // If ResourceRecord received via multicast, but question was unicast, then shouldn't use record to answer this question.
1890     // LocalOnly authoritative answers are exempt. LocalOnly authoritative answers are used for /etc/host entries.
1891     // We don't want a local process to be able to create a fake LocalOnly address record for "www.bigbank.com" which would then
1892     // cause other applications (e.g. Safari) to connect to the wrong address. The rpc to register records filters out records
1893     // with names that don't end in local and have mDNSInterface_LocalOnly set.
1894     //
1895     // Note: The check is bypassed for LocalOnly and for P2P it is not needed as only .local records are registered and for
1896     // a question to match its names, it also has to end in .local and that question can't be a unicast question (See
1897     // Question_uDNS macro and its usage). As P2P does not enforce .local only registrations we still make this check
1898     // and also makes it future proof.
1899 
1900     if (ar->ARType != AuthRecordLocalOnly && rr->InterfaceID && !mDNSOpaque16IsZero(q->TargetQID)) return(mDNSfalse);
1901 
1902     // RR type CNAME matches any query type. QTYPE ANY matches any RR type. QCLASS ANY matches any RR class.
1903     if (!RRTypeAnswersQuestionType(rr,q->qtype)) return(mDNSfalse);
1904     if (rr->rrclass != q->qclass && q->qclass != kDNSQClass_ANY) return(mDNSfalse);
1905 
1906     if (!AnonInfoAnswersQuestion(rr, q))
1907         return mDNSfalse;
1908 
1909     return(rr->namehash == q->qnamehash && SameDomainName(rr->name, &q->qname));
1910 }
1911 
1912 mDNSexport mDNSBool AnyTypeRecordAnswersQuestion(const ResourceRecord *const rr, const DNSQuestion *const q)
1913 {
1914     // LocalOnly/P2P questions can be answered with AuthRecordAny in this function. LocalOnly/P2P records
1915     // are handled in LocalOnlyRecordAnswersQuestion
1916     if ((rr->InterfaceID == mDNSInterface_LocalOnly) || (rr->InterfaceID == mDNSInterface_P2P))
1917     {
1918         LogMsg("AnyTypeRecordAnswersQuestion: ERROR!! called with LocalOnly ResourceRecord %p, Question %p", rr->InterfaceID, q->InterfaceID);
1919         return mDNSfalse;
1920     }
1921     if (rr->InterfaceID &&
1922         q->InterfaceID && q->InterfaceID != mDNSInterface_LocalOnly &&
1923         rr->InterfaceID != q->InterfaceID) return(mDNSfalse);
1924 
1925     // Resource record received via unicast, the resolver group ID should match ?
1926     // Note that Auth Records are normally setup with NULL InterfaceID and
1927     // both the DNSServers are assumed to be NULL in that case
1928     if (!rr->InterfaceID)
1929     {
1930         mDNSu16 idr = (rr->rDNSServer ? rr->rDNSServer->resGroupID : 0);
1931         mDNSu16 idq = (q->qDNSServer ? q->qDNSServer->resGroupID : 0);
1932         if (idr != idq) return(mDNSfalse);
1933     }
1934 
1935     // If ResourceRecord received via multicast, but question was unicast, then shouldn't use record to answer this question
1936     if (rr->InterfaceID && !mDNSOpaque16IsZero(q->TargetQID)) return(mDNSfalse);
1937 
1938     if (rr->rrclass != q->qclass && q->qclass != kDNSQClass_ANY) return(mDNSfalse);
1939 
1940     if (!AnonInfoAnswersQuestion(rr, q))
1941         return mDNSfalse;
1942 
1943     return(rr->namehash == q->qnamehash && SameDomainName(rr->name, &q->qname));
1944 }
1945 
1946 // This is called with both unicast resource record and multicast resource record. The question that
1947 // received the unicast response could be the regular unicast response from a DNS server or a response
1948 // to a mDNS QU query. The main reason we need this function is that we can't compare DNSServers between the
1949 // question and the resource record because the resource record is not completely initialized in
1950 // mDNSCoreReceiveResponse when this function is called.
1951 mDNSexport mDNSBool ResourceRecordAnswersUnicastResponse(const ResourceRecord *const rr, const DNSQuestion *const q)
1952 {
1953     mDNSBool checkType = mDNStrue;
1954 
1955     if (QuerySuppressed(q))
1956         return mDNSfalse;
1957 
1958     // For resource records created using multicast, the InterfaceIDs have to match
1959     if (rr->InterfaceID &&
1960         q->InterfaceID && rr->InterfaceID != q->InterfaceID) return(mDNSfalse);
1961 
1962     // If ResourceRecord received via multicast, but question was unicast, then shouldn't use record to answer this question.
1963     if (rr->InterfaceID && !mDNSOpaque16IsZero(q->TargetQID)) return(mDNSfalse);
1964 
1965     if (!DNSSECRecordAnswersQuestion(rr, q, &checkType)) return mDNSfalse;
1966 
1967     // RR type CNAME matches any query type. QTYPE ANY matches any RR type. QCLASS ANY matches any RR class.
1968     if (checkType && !RRTypeAnswersQuestionType(rr,q->qtype)) return(mDNSfalse);
1969 
1970     if (rr->rrclass != q->qclass && q->qclass != kDNSQClass_ANY) return(mDNSfalse);
1971 
1972     return(rr->namehash == q->qnamehash && SameDomainName(rr->name, &q->qname));
1973 }
1974 
1975 mDNSexport mDNSu16 GetRDLength(const ResourceRecord *const rr, mDNSBool estimate)
1976 {
1977     const RDataBody2 *const rd = (RDataBody2 *)rr->rdata->u.data;
1978     const domainname *const name = estimate ? rr->name : mDNSNULL;
1979     if (rr->rrclass == kDNSQClass_ANY) return(rr->rdlength);    // Used in update packets to mean "Delete An RRset" (RFC 2136)
1980     else switch (rr->rrtype)
1981         {
1982         case kDNSType_A:    return(sizeof(rd->ipv4));
1983 
1984         case kDNSType_NS:
1985         case kDNSType_CNAME:
1986         case kDNSType_PTR:
1987         case kDNSType_DNAME: return(CompressedDomainNameLength(&rd->name, name));
1988 
1989         case kDNSType_SOA:  return (mDNSu16)(CompressedDomainNameLength(&rd->soa.mname, name) +
1990                                              CompressedDomainNameLength(&rd->soa.rname, name) +
1991                                              5 * sizeof(mDNSOpaque32));
1992 
1993         case kDNSType_NULL:
1994         case kDNSType_TSIG:
1995         case kDNSType_TXT:
1996         case kDNSType_X25:
1997         case kDNSType_ISDN:
1998         case kDNSType_LOC:
1999         case kDNSType_DHCID: return(rr->rdlength); // Not self-describing, so have to just trust rdlength
2000 
2001         case kDNSType_HINFO: return (mDNSu16)(2 + (int)rd->data[0] + (int)rd->data[1 + (int)rd->data[0]]);
2002 
2003         case kDNSType_MX:
2004         case kDNSType_AFSDB:
2005         case kDNSType_RT:
2006         case kDNSType_KX:   return (mDNSu16)(2 + CompressedDomainNameLength(&rd->mx.exchange, name));
2007 
2008         case kDNSType_RP:   return (mDNSu16)(CompressedDomainNameLength(&rd->rp.mbox, name) +
2009                                              CompressedDomainNameLength(&rd->rp.txt, name));
2010 
2011         case kDNSType_PX:   return (mDNSu16)(2 + CompressedDomainNameLength(&rd->px.map822, name) +
2012                                              CompressedDomainNameLength(&rd->px.mapx400, name));
2013 
2014         case kDNSType_AAAA: return(sizeof(rd->ipv6));
2015 
2016         case kDNSType_SRV:  return (mDNSu16)(6 + CompressedDomainNameLength(&rd->srv.target, name));
2017 
2018         case kDNSType_OPT:  return(rr->rdlength);
2019 
2020         case kDNSType_NSEC: {
2021             domainname *next = (domainname *)rd->data;
2022             int dlen = DomainNameLength(next);
2023             //
2024             if (UNICAST_NSEC(rr))
2025                 return (mDNSu16)(CompressedDomainNameLength(next, name) + rr->rdlength - dlen);
2026             else
2027                 return (mDNSu16)((estimate ? 2 : dlen) + rr->rdlength - dlen);
2028         }
2029 
2030         default:            debugf("Warning! Don't know how to get length of resource type %d", rr->rrtype);
2031             return(rr->rdlength);
2032         }
2033 }
2034 
2035 // When a local client registers (or updates) a record, we use this routine to do some simple validation checks
2036 // to help reduce the risk of bogus malformed data on the network
2037 mDNSexport mDNSBool ValidateRData(const mDNSu16 rrtype, const mDNSu16 rdlength, const RData *const rd)
2038 {
2039     mDNSu16 len;
2040 
2041     switch(rrtype)
2042     {
2043     case kDNSType_A:    return(rdlength == sizeof(mDNSv4Addr));
2044 
2045     case kDNSType_NS:       // Same as PTR
2046     case kDNSType_MD:       // Same as PTR
2047     case kDNSType_MF:       // Same as PTR
2048     case kDNSType_CNAME:    // Same as PTR
2049     //case kDNSType_SOA not checked
2050     case kDNSType_MB:       // Same as PTR
2051     case kDNSType_MG:       // Same as PTR
2052     case kDNSType_MR:       // Same as PTR
2053     //case kDNSType_NULL not checked (no specified format, so always valid)
2054     //case kDNSType_WKS not checked
2055     case kDNSType_PTR:  len = DomainNameLengthLimit(&rd->u.name, rd->u.data + rdlength);
2056         return(len <= MAX_DOMAIN_NAME && rdlength == len);
2057 
2058     case kDNSType_HINFO:    // Same as TXT (roughly)
2059     case kDNSType_MINFO:    // Same as TXT (roughly)
2060     case kDNSType_TXT:  if (!rdlength) return(mDNSfalse);     // TXT record has to be at least one byte (RFC 1035)
2061         {
2062             const mDNSu8 *ptr = rd->u.txt.c;
2063             const mDNSu8 *end = rd->u.txt.c + rdlength;
2064             while (ptr < end) ptr += 1 + ptr[0];
2065             return (ptr == end);
2066         }
2067 
2068     case kDNSType_AAAA: return(rdlength == sizeof(mDNSv6Addr));
2069 
2070     case kDNSType_MX:       // Must be at least two-byte preference, plus domainname
2071                             // Call to DomainNameLengthLimit() implicitly enforces both requirements for us
2072         len = DomainNameLengthLimit(&rd->u.mx.exchange, rd->u.data + rdlength);
2073         return(len <= MAX_DOMAIN_NAME && rdlength == 2+len);
2074 
2075     case kDNSType_SRV:      // Must be at least priority+weight+port, plus domainname
2076                             // Call to DomainNameLengthLimit() implicitly enforces both requirements for us
2077         len = DomainNameLengthLimit(&rd->u.srv.target, rd->u.data + rdlength);
2078         return(len <= MAX_DOMAIN_NAME && rdlength == 6+len);
2079 
2080     //case kDNSType_NSEC not checked
2081 
2082     default:            return(mDNStrue);       // Allow all other types without checking
2083     }
2084 }
2085 
2086 // ***************************************************************************
2087 #if COMPILER_LIKES_PRAGMA_MARK
2088 #pragma mark -
2089 #pragma mark - DNS Message Creation Functions
2090 #endif
2091 
2092 mDNSexport void InitializeDNSMessage(DNSMessageHeader *h, mDNSOpaque16 id, mDNSOpaque16 flags)
2093 {
2094     h->id             = id;
2095     h->flags          = flags;
2096     h->numQuestions   = 0;
2097     h->numAnswers     = 0;
2098     h->numAuthorities = 0;
2099     h->numAdditionals = 0;
2100 }
2101 
2102 mDNSexport const mDNSu8 *FindCompressionPointer(const mDNSu8 *const base, const mDNSu8 *const end, const mDNSu8 *const domname)
2103 {
2104     const mDNSu8 *result = end - *domname - 1;
2105 
2106     if (*domname == 0) return(mDNSNULL);    // There's no point trying to match just the root label
2107 
2108     // This loop examines each possible starting position in packet, starting end of the packet and working backwards
2109     while (result >= base)
2110     {
2111         // If the length byte and first character of the label match, then check further to see
2112         // if this location in the packet will yield a useful name compression pointer.
2113         if (result[0] == domname[0] && result[1] == domname[1])
2114         {
2115             const mDNSu8 *name = domname;
2116             const mDNSu8 *targ = result;
2117             while (targ + *name < end)
2118             {
2119                 // First see if this label matches
2120                 int i;
2121                 const mDNSu8 *pointertarget;
2122                 for (i=0; i <= *name; i++) if (targ[i] != name[i]) break;
2123                 if (i <= *name) break;                          // If label did not match, bail out
2124                 targ += 1 + *name;                              // Else, did match, so advance target pointer
2125                 name += 1 + *name;                              // and proceed to check next label
2126                 if (*name == 0 && *targ == 0) return(result);   // If no more labels, we found a match!
2127                 if (*name == 0) break;                          // If no more labels to match, we failed, so bail out
2128 
2129                 // The label matched, so now follow the pointer (if appropriate) and then see if the next label matches
2130                 if (targ[0] < 0x40) continue;                   // If length value, continue to check next label
2131                 if (targ[0] < 0xC0) break;                      // If 40-BF, not valid
2132                 if (targ+1 >= end) break;                       // Second byte not present!
2133                 pointertarget = base + (((mDNSu16)(targ[0] & 0x3F)) << 8) + targ[1];
2134                 if (targ < pointertarget) break;                // Pointertarget must point *backwards* in the packet
2135                 if (pointertarget[0] >= 0x40) break;            // Pointertarget must point to a valid length byte
2136                 targ = pointertarget;
2137             }
2138         }
2139         result--;   // We failed to match at this search position, so back up the tentative result pointer and try again
2140     }
2141     return(mDNSNULL);
2142 }
2143 
2144 // Put a string of dot-separated labels as length-prefixed labels
2145 // domainname is a fully-qualified name (i.e. assumed to be ending in a dot, even if it doesn't)
2146 // msg points to the message we're building (pass mDNSNULL if we don't want to use compression pointers)
2147 // end points to the end of the message so far
2148 // ptr points to where we want to put the name
2149 // limit points to one byte past the end of the buffer that we must not overrun
2150 // domainname is the name to put
2151 mDNSexport mDNSu8 *putDomainNameAsLabels(const DNSMessage *const msg,
2152                                          mDNSu8 *ptr, const mDNSu8 *const limit, const domainname *const name)
2153 {
2154     const mDNSu8 *const base        = (const mDNSu8 *)msg;
2155     const mDNSu8 *      np          = name->c;
2156     const mDNSu8 *const max         = name->c + MAX_DOMAIN_NAME;    // Maximum that's valid
2157     const mDNSu8 *      pointer     = mDNSNULL;
2158     const mDNSu8 *const searchlimit = ptr;
2159 
2160     if (!ptr) { LogMsg("putDomainNameAsLabels %##s ptr is null", name->c); return(mDNSNULL); }
2161 
2162     if (!*np)       // If just writing one-byte root label, make sure we have space for that
2163     {
2164         if (ptr >= limit) return(mDNSNULL);
2165     }
2166     else            // else, loop through writing labels and/or a compression offset
2167     {
2168         do  {
2169             if (*np > MAX_DOMAIN_LABEL)
2170             { LogMsg("Malformed domain name %##s (label more than 63 bytes)", name->c); return(mDNSNULL); }
2171 
2172             // This check correctly allows for the final trailing root label:
2173             // e.g.
2174             // Suppose our domain name is exactly 256 bytes long, including the final trailing root label.
2175             // Suppose np is now at name->c[249], and we're about to write our last non-null label ("local").
2176             // We know that max will be at name->c[256]
2177             // That means that np + 1 + 5 == max - 1, so we (just) pass the "if" test below, write our
2178             // six bytes, then exit the loop, write the final terminating root label, and the domain
2179             // name we've written is exactly 256 bytes long, exactly at the correct legal limit.
2180             // If the name is one byte longer, then we fail the "if" test below, and correctly bail out.
2181             if (np + 1 + *np >= max)
2182             { LogMsg("Malformed domain name %##s (more than 256 bytes)", name->c); return(mDNSNULL); }
2183 
2184             if (base) pointer = FindCompressionPointer(base, searchlimit, np);
2185             if (pointer)                    // Use a compression pointer if we can
2186             {
2187                 const mDNSu16 offset = (mDNSu16)(pointer - base);
2188                 if (ptr+2 > limit) return(mDNSNULL);    // If we don't have two bytes of space left, give up
2189                 *ptr++ = (mDNSu8)(0xC0 | (offset >> 8));
2190                 *ptr++ = (mDNSu8)(        offset &  0xFF);
2191                 return(ptr);
2192             }
2193             else                            // Else copy one label and try again
2194             {
2195                 int i;
2196                 mDNSu8 len = *np++;
2197                 // If we don't at least have enough space for this label *plus* a terminating zero on the end, give up
2198                 if (ptr + 1 + len >= limit) return(mDNSNULL);
2199                 *ptr++ = len;
2200                 for (i=0; i<len; i++) *ptr++ = *np++;
2201             }
2202         } while (*np);                      // While we've got characters remaining in the name, continue
2203     }
2204 
2205     *ptr++ = 0;     // Put the final root label
2206     return(ptr);
2207 }
2208 
2209 mDNSlocal mDNSu8 *putVal16(mDNSu8 *ptr, mDNSu16 val)
2210 {
2211     ptr[0] = (mDNSu8)((val >> 8 ) & 0xFF);
2212     ptr[1] = (mDNSu8)((val      ) & 0xFF);
2213     return ptr + sizeof(mDNSOpaque16);
2214 }
2215 
2216 mDNSlocal mDNSu8 *putVal32(mDNSu8 *ptr, mDNSu32 val)
2217 {
2218     ptr[0] = (mDNSu8)((val >> 24) & 0xFF);
2219     ptr[1] = (mDNSu8)((val >> 16) & 0xFF);
2220     ptr[2] = (mDNSu8)((val >>  8) & 0xFF);
2221     ptr[3] = (mDNSu8)((val      ) & 0xFF);
2222     return ptr + sizeof(mDNSu32);
2223 }
2224 
2225 // Copy the RDATA information. The actual in memory storage for the data might be bigger than what the rdlength
2226 // says. Hence, the only way to copy out the data from a resource record is to use putRData.
2227 // msg points to the message we're building (pass mDNSNULL for "msg" if we don't want to use compression pointers)
2228 mDNSexport mDNSu8 *putRData(const DNSMessage *const msg, mDNSu8 *ptr, const mDNSu8 *const limit, const ResourceRecord *const rr)
2229 {
2230     const RDataBody2 *const rdb = (RDataBody2 *)rr->rdata->u.data;
2231     switch (rr->rrtype)
2232     {
2233     case kDNSType_A:    if (rr->rdlength != 4)
2234         { debugf("putRData: Illegal length %d for kDNSType_A", rr->rdlength); return(mDNSNULL); }
2235         if (ptr + 4 > limit) return(mDNSNULL);
2236         *ptr++ = rdb->ipv4.b[0];
2237         *ptr++ = rdb->ipv4.b[1];
2238         *ptr++ = rdb->ipv4.b[2];
2239         *ptr++ = rdb->ipv4.b[3];
2240         return(ptr);
2241 
2242     case kDNSType_NS:
2243     case kDNSType_CNAME:
2244     case kDNSType_PTR:
2245     case kDNSType_DNAME: return(putDomainNameAsLabels(msg, ptr, limit, &rdb->name));
2246 
2247     case kDNSType_SOA:  ptr = putDomainNameAsLabels(msg, ptr, limit, &rdb->soa.mname);
2248         if (!ptr) return(mDNSNULL);
2249         ptr = putDomainNameAsLabels(msg, ptr, limit, &rdb->soa.rname);
2250         if (!ptr || ptr + 20 > limit) return(mDNSNULL);
2251         ptr = putVal32(ptr, rdb->soa.serial);
2252         ptr = putVal32(ptr, rdb->soa.refresh);
2253         ptr = putVal32(ptr, rdb->soa.retry);
2254         ptr = putVal32(ptr, rdb->soa.expire);
2255         ptr = putVal32(ptr, rdb->soa.min);
2256         return(ptr);
2257 
2258     case kDNSType_NULL:
2259     case kDNSType_HINFO:
2260     case kDNSType_TSIG:
2261     case kDNSType_TXT:
2262     case kDNSType_X25:
2263     case kDNSType_ISDN:
2264     case kDNSType_LOC:
2265     case kDNSType_DHCID: if (ptr + rr->rdlength > limit) return(mDNSNULL);
2266         mDNSPlatformMemCopy(ptr, rdb->data, rr->rdlength);
2267         return(ptr + rr->rdlength);
2268 
2269     case kDNSType_MX:
2270     case kDNSType_AFSDB:
2271     case kDNSType_RT:
2272     case kDNSType_KX:   if (ptr + 3 > limit) return(mDNSNULL);
2273         ptr = putVal16(ptr, rdb->mx.preference);
2274         return(putDomainNameAsLabels(msg, ptr, limit, &rdb->mx.exchange));
2275 
2276     case kDNSType_RP:   ptr = putDomainNameAsLabels(msg, ptr, limit, &rdb->rp.mbox);
2277         if (!ptr) return(mDNSNULL);
2278         ptr = putDomainNameAsLabels(msg, ptr, limit, &rdb->rp.txt);
2279         return(ptr);
2280 
2281     case kDNSType_PX:   if (ptr + 5 > limit) return(mDNSNULL);
2282         ptr = putVal16(ptr, rdb->px.preference);
2283         ptr = putDomainNameAsLabels(msg, ptr, limit, &rdb->px.map822);
2284         if (!ptr) return(mDNSNULL);
2285         ptr = putDomainNameAsLabels(msg, ptr, limit, &rdb->px.mapx400);
2286         return(ptr);
2287 
2288     case kDNSType_AAAA: if (rr->rdlength != sizeof(rdb->ipv6))
2289         { debugf("putRData: Illegal length %d for kDNSType_AAAA", rr->rdlength); return(mDNSNULL); }
2290         if (ptr + sizeof(rdb->ipv6) > limit) return(mDNSNULL);
2291         mDNSPlatformMemCopy(ptr, &rdb->ipv6, sizeof(rdb->ipv6));
2292         return(ptr + sizeof(rdb->ipv6));
2293 
2294     case kDNSType_SRV:  if (ptr + 7 > limit) return(mDNSNULL);
2295         *ptr++ = (mDNSu8)(rdb->srv.priority >> 8);
2296         *ptr++ = (mDNSu8)(rdb->srv.priority &  0xFF);
2297         *ptr++ = (mDNSu8)(rdb->srv.weight   >> 8);
2298         *ptr++ = (mDNSu8)(rdb->srv.weight   &  0xFF);
2299         *ptr++ = rdb->srv.port.b[0];
2300         *ptr++ = rdb->srv.port.b[1];
2301         return(putDomainNameAsLabels(msg, ptr, limit, &rdb->srv.target));
2302 
2303     case kDNSType_OPT:  {
2304         int len = 0;
2305         const rdataOPT *opt;
2306         const rdataOPT *const end = (const rdataOPT *)&rr->rdata->u.data[rr->rdlength];
2307         for (opt = &rr->rdata->u.opt[0]; opt < end; opt++)
2308             len += DNSOpt_Data_Space(opt);
2309         if (ptr + len > limit)
2310         {
2311             LogMsg("ERROR: putOptRData - out of space");
2312             return mDNSNULL;
2313         }
2314         for (opt = &rr->rdata->u.opt[0]; opt < end; opt++)
2315         {
2316             const int space = DNSOpt_Data_Space(opt);
2317             ptr = putVal16(ptr, opt->opt);
2318             ptr = putVal16(ptr, (mDNSu16)space - 4);
2319             switch (opt->opt)
2320             {
2321             case kDNSOpt_LLQ:
2322                 ptr = putVal16(ptr, opt->u.llq.vers);
2323                 ptr = putVal16(ptr, opt->u.llq.llqOp);
2324                 ptr = putVal16(ptr, opt->u.llq.err);
2325                 mDNSPlatformMemCopy(ptr, opt->u.llq.id.b, 8);                          // 8-byte id
2326                 ptr += 8;
2327                 ptr = putVal32(ptr, opt->u.llq.llqlease);
2328                 break;
2329             case kDNSOpt_Lease:
2330                 ptr = putVal32(ptr, opt->u.updatelease);
2331                 break;
2332             case kDNSOpt_Owner:
2333                 *ptr++ = opt->u.owner.vers;
2334                 *ptr++ = opt->u.owner.seq;
2335                 mDNSPlatformMemCopy(ptr, opt->u.owner.HMAC.b, 6);                          // 6-byte Host identifier
2336                 ptr += 6;
2337                 if (space >= DNSOpt_OwnerData_ID_Wake_Space)
2338                 {
2339                     mDNSPlatformMemCopy(ptr, opt->u.owner.IMAC.b, 6);                           // 6-byte interface MAC
2340                     ptr += 6;
2341                     if (space > DNSOpt_OwnerData_ID_Wake_Space)
2342                     {
2343                         mDNSPlatformMemCopy(ptr, opt->u.owner.password.b, space - DNSOpt_OwnerData_ID_Wake_Space);
2344                         ptr += space - DNSOpt_OwnerData_ID_Wake_Space;
2345                     }
2346                 }
2347                 break;
2348             case kDNSOpt_Trace:
2349                 *ptr++ = opt->u.tracer.platf;
2350                 ptr    = putVal32(ptr, opt->u.tracer.mDNSv);
2351                 break;
2352             }
2353         }
2354         return ptr;
2355     }
2356 
2357     case kDNSType_NSEC: {
2358         // For NSEC records, rdlength represents the exact number of bytes
2359         // of in memory storage.
2360         int len = rr->rdlength;
2361         mDNSu8 *nsec = (mDNSu8 *)rdb->data;
2362         domainname *name = (domainname *)nsec;
2363         int dlen;
2364 
2365         dlen = DomainNameLength(name);
2366         len -= dlen;
2367         nsec += dlen;
2368         // This function is called when we are sending a NSEC record as part of mDNS,
2369         // or to copy the data to any other buffer needed which could be a mDNS or uDNS
2370         // NSEC record. The only time compression is used that when we are sending it
2371         // in mDNS (indicated by non-NULL "msg") and hence we handle mDNS case
2372         // separately.
2373         if (!UNICAST_NSEC(rr))
2374         {
2375             mDNSu8 *save = ptr;
2376             int i, j, wlen;
2377             wlen = *(nsec + 1);
2378             nsec += 2;                     // Skip the window number and len
2379             len -= 2;
2380 
2381             // For our simplified use of NSEC synthetic records:
2382             //
2383             // nextname is always the record's own name,
2384             // the block number is always 0,
2385             // the count byte is a value in the range 1-32,
2386             // followed by the 1-32 data bytes
2387             //
2388             // Note: When we send the NSEC record in mDNS, the window size is set to 32.
2389             // We need to find out what the last non-NULL byte is.  If we are copying out
2390             // from an RDATA, we have the right length. As we need to handle both the case,
2391             // we loop to find the right value instead of blindly using len to copy.
2392 
2393             for (i=wlen; i>0; i--) if (nsec[i-1]) break;
2394 
2395             ptr = putDomainNameAsLabels(msg, ptr, limit, rr->name);
2396             if (!ptr) { LogInfo("putRData: Can't put name, Length %d, record %##s", limit - save, rr->name->c); return(mDNSNULL); }
2397             if (i)                          // Only put a block if at least one type exists for this name
2398             {
2399                 if (ptr + 2 + i > limit) { LogInfo("putRData: Can't put window, Length %d, i %d, record %##s", limit - ptr, i, rr->name->c); return(mDNSNULL); }
2400                 *ptr++ = 0;
2401                 *ptr++ = (mDNSu8)i;
2402                 for (j=0; j<i; j++) *ptr++ = nsec[j];
2403             }
2404             return ptr;
2405         }
2406         else
2407         {
2408             int win, wlen;
2409 
2410             // Sanity check whether the bitmap is good
2411             while (len)
2412             {
2413                 if (len < 3)
2414                 { LogMsg("putRData: invalid length %d", len); return mDNSNULL; }
2415 
2416                 win = *nsec++;
2417                 wlen = *nsec++;
2418                 len -= 2;
2419                 if (len < wlen || wlen < 1 || wlen > 32)
2420                 { LogMsg("putRData: invalid window length %d", wlen); return mDNSNULL; }
2421                 if (win < 0 || win >= 256)
2422                 { LogMsg("putRData: invalid window %d", win); return mDNSNULL; }
2423 
2424                 nsec += wlen;
2425                 len -= wlen;
2426             }
2427             if (ptr + rr->rdlength > limit) { LogMsg("putRData: NSEC rdlength beyond limit %##s (%s), ptr %p, rdlength %d, limit %p", rr->name->c, DNSTypeName(rr->rrtype), ptr, rr->rdlength, limit); return(mDNSNULL);}
2428 
2429             // No compression allowed for "nxt", just copy the data.
2430             mDNSPlatformMemCopy(ptr, rdb->data, rr->rdlength);
2431             return(ptr + rr->rdlength);
2432         }
2433     }
2434 
2435     default:            debugf("putRData: Warning! Writing unknown resource type %d as raw data", rr->rrtype);
2436         if (ptr + rr->rdlength > limit) return(mDNSNULL);
2437         mDNSPlatformMemCopy(ptr, rdb->data, rr->rdlength);
2438         return(ptr + rr->rdlength);
2439     }
2440 }
2441 
2442 #define IsUnicastUpdate(X) (!mDNSOpaque16IsZero((X)->h.id) && ((X)->h.flags.b[0] & kDNSFlag0_OP_Mask) == kDNSFlag0_OP_Update)
2443 
2444 mDNSexport mDNSu8 *PutResourceRecordTTLWithLimit(DNSMessage *const msg, mDNSu8 *ptr, mDNSu16 *count, ResourceRecord *rr, mDNSu32 ttl, const mDNSu8 *limit)
2445 {
2446     mDNSu8 *endofrdata;
2447     mDNSu16 actualLength;
2448     // When sending SRV to conventional DNS server (i.e. in DNS update requests) we should not do name compression on the rdata (RFC 2782)
2449     const DNSMessage *const rdatacompressionbase = (IsUnicastUpdate(msg) && rr->rrtype == kDNSType_SRV) ? mDNSNULL : msg;
2450 
2451     if (rr->RecordType == kDNSRecordTypeUnregistered)
2452     {
2453         LogMsg("PutResourceRecordTTLWithLimit ERROR! Attempt to put kDNSRecordTypeUnregistered %##s (%s)", rr->name->c, DNSTypeName(rr->rrtype));
2454         return(ptr);
2455     }
2456 
2457     if (!ptr)
2458     {
2459         LogMsg("PutResourceRecordTTLWithLimit ptr is null %##s (%s)", rr->name->c, DNSTypeName(rr->rrtype));
2460         return(mDNSNULL);
2461     }
2462 
2463     ptr = putDomainNameAsLabels(msg, ptr, limit, rr->name);
2464     // If we're out-of-space, return mDNSNULL
2465     if (!ptr || ptr + 10 >= limit)
2466     {
2467         LogInfo("PutResourceRecordTTLWithLimit: can't put name, out of space %##s (%s), ptr %p, limit %p", rr->name->c,
2468             DNSTypeName(rr->rrtype), ptr, limit);
2469         return(mDNSNULL);
2470     }
2471     ptr[0] = (mDNSu8)(rr->rrtype  >> 8);
2472     ptr[1] = (mDNSu8)(rr->rrtype  &  0xFF);
2473     ptr[2] = (mDNSu8)(rr->rrclass >> 8);
2474     ptr[3] = (mDNSu8)(rr->rrclass &  0xFF);
2475     ptr[4] = (mDNSu8)((ttl >> 24) &  0xFF);
2476     ptr[5] = (mDNSu8)((ttl >> 16) &  0xFF);
2477     ptr[6] = (mDNSu8)((ttl >>  8) &  0xFF);
2478     ptr[7] = (mDNSu8)( ttl        &  0xFF);
2479     // ptr[8] and ptr[9] filled in *after* we find out how much space the rdata takes
2480 
2481     endofrdata = putRData(rdatacompressionbase, ptr+10, limit, rr);
2482     if (!endofrdata)
2483     {
2484         LogInfo("PutResourceRecordTTLWithLimit: Ran out of space in PutResourceRecord for %##s (%s), ptr %p, limit %p", rr->name->c,
2485             DNSTypeName(rr->rrtype), ptr+10, limit);
2486         return(mDNSNULL);
2487     }
2488 
2489     // Go back and fill in the actual number of data bytes we wrote
2490     // (actualLength can be less than rdlength when domain name compression is used)
2491     actualLength = (mDNSu16)(endofrdata - ptr - 10);
2492     ptr[8] = (mDNSu8)(actualLength >> 8);
2493     ptr[9] = (mDNSu8)(actualLength &  0xFF);
2494 
2495     if (count) (*count)++;
2496     else LogMsg("PutResourceRecordTTL: ERROR: No target count to update for %##s (%s)", rr->name->c, DNSTypeName(rr->rrtype));
2497     return(endofrdata);
2498 }
2499 
2500 mDNSlocal mDNSu8 *putEmptyResourceRecord(DNSMessage *const msg, mDNSu8 *ptr, const mDNSu8 *const limit, mDNSu16 *count, const AuthRecord *rr)
2501 {
2502     ptr = putDomainNameAsLabels(msg, ptr, limit, rr->resrec.name);
2503     if (!ptr || ptr + 10 > limit) return(mDNSNULL);     // If we're out-of-space, return mDNSNULL
2504     ptr[0] = (mDNSu8)(rr->resrec.rrtype  >> 8);             // Put type
2505     ptr[1] = (mDNSu8)(rr->resrec.rrtype  &  0xFF);
2506     ptr[2] = (mDNSu8)(rr->resrec.rrclass >> 8);             // Put class
2507     ptr[3] = (mDNSu8)(rr->resrec.rrclass &  0xFF);
2508     ptr[4] = ptr[5] = ptr[6] = ptr[7] = 0;              // TTL is zero
2509     ptr[8] = ptr[9] = 0;                                // RDATA length is zero
2510     (*count)++;
2511     return(ptr + 10);
2512 }
2513 
2514 mDNSexport mDNSu8 *putQuestion(DNSMessage *const msg, mDNSu8 *ptr, const mDNSu8 *const limit, const domainname *const name, mDNSu16 rrtype, mDNSu16 rrclass)
2515 {
2516     ptr = putDomainNameAsLabels(msg, ptr, limit, name);
2517     if (!ptr || ptr+4 >= limit) return(mDNSNULL);           // If we're out-of-space, return mDNSNULL
2518     ptr[0] = (mDNSu8)(rrtype  >> 8);
2519     ptr[1] = (mDNSu8)(rrtype  &  0xFF);
2520     ptr[2] = (mDNSu8)(rrclass >> 8);
2521     ptr[3] = (mDNSu8)(rrclass &  0xFF);
2522     msg->h.numQuestions++;
2523     return(ptr+4);
2524 }
2525 
2526 // for dynamic updates
2527 mDNSexport mDNSu8 *putZone(DNSMessage *const msg, mDNSu8 *ptr, mDNSu8 *limit, const domainname *zone, mDNSOpaque16 zoneClass)
2528 {
2529     ptr = putDomainNameAsLabels(msg, ptr, limit, zone);
2530     if (!ptr || ptr + 4 > limit) return mDNSNULL;       // If we're out-of-space, return NULL
2531     *ptr++ = (mDNSu8)(kDNSType_SOA  >> 8);
2532     *ptr++ = (mDNSu8)(kDNSType_SOA  &  0xFF);
2533     *ptr++ = zoneClass.b[0];
2534     *ptr++ = zoneClass.b[1];
2535     msg->h.mDNS_numZones++;
2536     return ptr;
2537 }
2538 
2539 // for dynamic updates
2540 mDNSexport mDNSu8 *putPrereqNameNotInUse(const domainname *const name, DNSMessage *const msg, mDNSu8 *const ptr, mDNSu8 *const end)
2541 {
2542     AuthRecord prereq;
2543     mDNS_SetupResourceRecord(&prereq, mDNSNULL, mDNSInterface_Any, kDNSQType_ANY, kStandardTTL, 0, AuthRecordAny, mDNSNULL, mDNSNULL);
2544     AssignDomainName(&prereq.namestorage, name);
2545     prereq.resrec.rrtype = kDNSQType_ANY;
2546     prereq.resrec.rrclass = kDNSClass_NONE;
2547     return putEmptyResourceRecord(msg, ptr, end, &msg->h.mDNS_numPrereqs, &prereq);
2548 }
2549 
2550 // for dynamic updates
2551 mDNSexport mDNSu8 *putDeletionRecord(DNSMessage *msg, mDNSu8 *ptr, ResourceRecord *rr)
2552 {
2553     // deletion: specify record w/ TTL 0, class NONE
2554     const mDNSu16 origclass = rr->rrclass;
2555     rr->rrclass = kDNSClass_NONE;
2556     ptr = PutResourceRecordTTLJumbo(msg, ptr, &msg->h.mDNS_numUpdates, rr, 0);
2557     rr->rrclass = origclass;
2558     return ptr;
2559 }
2560 
2561 // for dynamic updates
2562 mDNSexport mDNSu8 *putDeletionRecordWithLimit(DNSMessage *msg, mDNSu8 *ptr, ResourceRecord *rr, mDNSu8 *limit)
2563 {
2564     // deletion: specify record w/ TTL 0, class NONE
2565     const mDNSu16 origclass = rr->rrclass;
2566     rr->rrclass = kDNSClass_NONE;
2567     ptr = PutResourceRecordTTLWithLimit(msg, ptr, &msg->h.mDNS_numUpdates, rr, 0, limit);
2568     rr->rrclass = origclass;
2569     return ptr;
2570 }
2571 
2572 mDNSexport mDNSu8 *putDeleteRRSetWithLimit(DNSMessage *msg, mDNSu8 *ptr, const domainname *name, mDNSu16 rrtype, mDNSu8 *limit)
2573 {
2574     mDNSu16 class = kDNSQClass_ANY;
2575 
2576     ptr = putDomainNameAsLabels(msg, ptr, limit, name);
2577     if (!ptr || ptr + 10 >= limit) return mDNSNULL; // If we're out-of-space, return mDNSNULL
2578     ptr[0] = (mDNSu8)(rrtype  >> 8);
2579     ptr[1] = (mDNSu8)(rrtype  &  0xFF);
2580     ptr[2] = (mDNSu8)(class >> 8);
2581     ptr[3] = (mDNSu8)(class &  0xFF);
2582     ptr[4] = ptr[5] = ptr[6] = ptr[7] = 0; // zero ttl
2583     ptr[8] = ptr[9] = 0; // zero rdlength/rdata
2584 
2585     msg->h.mDNS_numUpdates++;
2586     return ptr + 10;
2587 }
2588 
2589 // for dynamic updates
2590 mDNSexport mDNSu8 *putDeleteAllRRSets(DNSMessage *msg, mDNSu8 *ptr, const domainname *name)
2591 {
2592     const mDNSu8 *limit = msg->data + AbsoluteMaxDNSMessageData;
2593     mDNSu16 class = kDNSQClass_ANY;
2594     mDNSu16 rrtype = kDNSQType_ANY;
2595 
2596     ptr = putDomainNameAsLabels(msg, ptr, limit, name);
2597     if (!ptr || ptr + 10 >= limit) return mDNSNULL; // If we're out-of-space, return mDNSNULL
2598     ptr[0] = (mDNSu8)(rrtype >> 8);
2599     ptr[1] = (mDNSu8)(rrtype &  0xFF);
2600     ptr[2] = (mDNSu8)(class >> 8);
2601     ptr[3] = (mDNSu8)(class &  0xFF);
2602     ptr[4] = ptr[5] = ptr[6] = ptr[7] = 0; // zero ttl
2603     ptr[8] = ptr[9] = 0; // zero rdlength/rdata
2604 
2605     msg->h.mDNS_numUpdates++;
2606     return ptr + 10;
2607 }
2608 
2609 // for dynamic updates
2610 mDNSexport mDNSu8 *putUpdateLease(DNSMessage *msg, mDNSu8 *end, mDNSu32 lease)
2611 {
2612     AuthRecord rr;
2613     mDNS_SetupResourceRecord(&rr, mDNSNULL, mDNSInterface_Any, kDNSType_OPT, kStandardTTL, kDNSRecordTypeKnownUnique, AuthRecordAny, mDNSNULL, mDNSNULL);
2614     rr.resrec.rrclass    = NormalMaxDNSMessageData;
2615     rr.resrec.rdlength   = sizeof(rdataOPT);    // One option in this OPT record
2616     rr.resrec.rdestimate = sizeof(rdataOPT);
2617     rr.resrec.rdata->u.opt[0].opt           = kDNSOpt_Lease;
2618     rr.resrec.rdata->u.opt[0].u.updatelease = lease;
2619     end = PutResourceRecordTTLJumbo(msg, end, &msg->h.numAdditionals, &rr.resrec, 0);
2620     if (!end) { LogMsg("ERROR: putUpdateLease - PutResourceRecordTTL"); return mDNSNULL; }
2621     return end;
2622 }
2623 
2624 // for dynamic updates
2625 mDNSexport mDNSu8 *putUpdateLeaseWithLimit(DNSMessage *msg, mDNSu8 *end, mDNSu32 lease, mDNSu8 *limit)
2626 {
2627     AuthRecord rr;
2628     mDNS_SetupResourceRecord(&rr, mDNSNULL, mDNSInterface_Any, kDNSType_OPT, kStandardTTL, kDNSRecordTypeKnownUnique, AuthRecordAny, mDNSNULL, mDNSNULL);
2629     rr.resrec.rrclass    = NormalMaxDNSMessageData;
2630     rr.resrec.rdlength   = sizeof(rdataOPT);    // One option in this OPT record
2631     rr.resrec.rdestimate = sizeof(rdataOPT);
2632     rr.resrec.rdata->u.opt[0].opt           = kDNSOpt_Lease;
2633     rr.resrec.rdata->u.opt[0].u.updatelease = lease;
2634     end = PutResourceRecordTTLWithLimit(msg, end, &msg->h.numAdditionals, &rr.resrec, 0, limit);
2635     if (!end) { LogMsg("ERROR: putUpdateLease - PutResourceRecordTTLWithLimit"); return mDNSNULL; }
2636     return end;
2637 }
2638 
2639 mDNSexport mDNSu8 *putDNSSECOption(DNSMessage *msg, mDNSu8 *end, mDNSu8 *limit)
2640 {
2641     AuthRecord rr;
2642     mDNSu32 ttl = 0;
2643 
2644     mDNS_SetupResourceRecord(&rr, mDNSNULL, mDNSInterface_Any, kDNSType_OPT, kStandardTTL, kDNSRecordTypeKnownUnique, AuthRecordAny, mDNSNULL, mDNSNULL);
2645     // It is still not clear what the right size is. We will have to fine tune this once we do
2646     // a lot of testing with DNSSEC.
2647     rr.resrec.rrclass    = 4096;
2648     rr.resrec.rdlength   = 0;
2649     rr.resrec.rdestimate = 0;
2650     // set the DO bit
2651     ttl |= 0x8000;
2652     end = PutResourceRecordTTLWithLimit(msg, end, &msg->h.numAdditionals, &rr.resrec, ttl, limit);
2653     if (!end) { LogMsg("ERROR: putUpdateLease - PutResourceRecordTTLWithLimit"); return mDNSNULL; }
2654     return end;
2655 }
2656 
2657 mDNSexport mDNSu8 *putHINFO(const mDNS *const m, DNSMessage *const msg, mDNSu8 *end, DomainAuthInfo *authInfo, mDNSu8 *limit)
2658 {
2659     if (authInfo && authInfo->AutoTunnel)
2660     {
2661         AuthRecord hinfo;
2662         mDNSu8 *h = hinfo.rdatastorage.u.data;
2663         mDNSu16 len = 2 + m->HIHardware.c[0] + m->HISoftware.c[0];
2664         mDNSu8 *newptr;
2665         mDNS_SetupResourceRecord(&hinfo, mDNSNULL, mDNSInterface_Any, kDNSType_HINFO, 0, kDNSRecordTypeUnique, AuthRecordAny, mDNSNULL, mDNSNULL);
2666         AppendDomainLabel(&hinfo.namestorage, &m->hostlabel);
2667         AppendDomainName (&hinfo.namestorage, &authInfo->domain);
2668         hinfo.resrec.rroriginalttl = 0;
2669         mDNSPlatformMemCopy(h, &m->HIHardware, 1 + (mDNSu32)m->HIHardware.c[0]);
2670         h += 1 + (int)h[0];
2671         mDNSPlatformMemCopy(h, &m->HISoftware, 1 + (mDNSu32)m->HISoftware.c[0]);
2672         hinfo.resrec.rdlength   = len;
2673         hinfo.resrec.rdestimate = len;
2674         newptr = PutResourceRecordTTLWithLimit(msg, end, &msg->h.numAdditionals, &hinfo.resrec, 0, limit);
2675         return newptr;
2676     }
2677     else
2678         return end;
2679 }
2680 
2681 // ***************************************************************************
2682 #if COMPILER_LIKES_PRAGMA_MARK
2683 #pragma mark -
2684 #pragma mark - DNS Message Parsing Functions
2685 #endif
2686 
2687 mDNSexport mDNSu32 DomainNameHashValue(const domainname *const name)
2688 {
2689     mDNSu32 sum = 0;
2690     const mDNSu8 *c;
2691 
2692     for (c = name->c; c[0] != 0 && c[1] != 0; c += 2)
2693     {
2694         sum += ((mDNSIsUpperCase(c[0]) ? c[0] + 'a' - 'A' : c[0]) << 8) |
2695                (mDNSIsUpperCase(c[1]) ? c[1] + 'a' - 'A' : c[1]);
2696         sum = (sum<<3) | (sum>>29);
2697     }
2698     if (c[0]) sum += ((mDNSIsUpperCase(c[0]) ? c[0] + 'a' - 'A' : c[0]) << 8);
2699     return(sum);
2700 }
2701 
2702 mDNSexport void SetNewRData(ResourceRecord *const rr, RData *NewRData, mDNSu16 rdlength)
2703 {
2704     domainname *target;
2705     if (NewRData)
2706     {
2707         rr->rdata    = NewRData;
2708         rr->rdlength = rdlength;
2709     }
2710     // Must not try to get target pointer until after updating rr->rdata
2711     target = GetRRDomainNameTarget(rr);
2712     rr->rdlength   = GetRDLength(rr, mDNSfalse);
2713     rr->rdestimate = GetRDLength(rr, mDNStrue);
2714     rr->rdatahash  = target ? DomainNameHashValue(target) : RDataHashValue(rr);
2715 }
2716 
2717 mDNSexport const mDNSu8 *skipDomainName(const DNSMessage *const msg, const mDNSu8 *ptr, const mDNSu8 *const end)
2718 {
2719     mDNSu16 total = 0;
2720 
2721     if (ptr < (mDNSu8*)msg || ptr >= end)
2722     { debugf("skipDomainName: Illegal ptr not within packet boundaries"); return(mDNSNULL); }
2723 
2724     while (1)                       // Read sequence of labels
2725     {
2726         const mDNSu8 len = *ptr++;  // Read length of this label
2727         if (len == 0) return(ptr);  // If length is zero, that means this name is complete
2728         switch (len & 0xC0)
2729         {
2730         case 0x00:  if (ptr + len >= end)                       // Remember: expect at least one more byte for the root label
2731             { debugf("skipDomainName: Malformed domain name (overruns packet end)"); return(mDNSNULL); }
2732             if (total + 1 + len >= MAX_DOMAIN_NAME)             // Remember: expect at least one more byte for the root label
2733             { debugf("skipDomainName: Malformed domain name (more than 256 characters)"); return(mDNSNULL); }
2734             ptr += len;
2735             total += 1 + len;
2736             break;
2737 
2738         case 0x40:  debugf("skipDomainName: Extended EDNS0 label types 0x%X not supported", len); return(mDNSNULL);
2739         case 0x80:  debugf("skipDomainName: Illegal label length 0x%X", len); return(mDNSNULL);
2740         case 0xC0:  return(ptr+1);
2741         }
2742     }
2743 }
2744 
2745 // Routine to fetch an FQDN from the DNS message, following compression pointers if necessary.
2746 mDNSexport const mDNSu8 *getDomainName(const DNSMessage *const msg, const mDNSu8 *ptr, const mDNSu8 *const end,
2747                                        domainname *const name)
2748 {
2749     const mDNSu8 *nextbyte = mDNSNULL;                  // Record where we got to before we started following pointers
2750     mDNSu8       *np = name->c;                         // Name pointer
2751     const mDNSu8 *const limit = np + MAX_DOMAIN_NAME;   // Limit so we don't overrun buffer
2752 
2753     if (ptr < (mDNSu8*)msg || ptr >= end)
2754     { debugf("getDomainName: Illegal ptr not within packet boundaries"); return(mDNSNULL); }
2755 
2756     *np = 0;                        // Tentatively place the root label here (may be overwritten if we have more labels)
2757 
2758     while (1)                       // Read sequence of labels
2759     {
2760         const mDNSu8 len = *ptr++;  // Read length of this label
2761         if (len == 0) break;        // If length is zero, that means this name is complete
2762         switch (len & 0xC0)
2763         {
2764             int i;
2765             mDNSu16 offset;
2766 
2767         case 0x00:  if (ptr + len >= end)           // Remember: expect at least one more byte for the root label
2768             { debugf("getDomainName: Malformed domain name (overruns packet end)"); return(mDNSNULL); }
2769             if (np + 1 + len >= limit)              // Remember: expect at least one more byte for the root label
2770             { debugf("getDomainName: Malformed domain name (more than 256 characters)"); return(mDNSNULL); }
2771             *np++ = len;
2772             for (i=0; i<len; i++) *np++ = *ptr++;
2773             *np = 0;                // Tentatively place the root label here (may be overwritten if we have more labels)
2774             break;
2775 
2776         case 0x40:  debugf("getDomainName: Extended EDNS0 label types 0x%X not supported in name %##s", len, name->c);
2777             return(mDNSNULL);
2778 
2779         case 0x80:  debugf("getDomainName: Illegal label length 0x%X in domain name %##s", len, name->c); return(mDNSNULL);
2780 
2781         case 0xC0:  offset = (mDNSu16)((((mDNSu16)(len & 0x3F)) << 8) | *ptr++);
2782             if (!nextbyte) nextbyte = ptr;              // Record where we got to before we started following pointers
2783             ptr = (mDNSu8 *)msg + offset;
2784             if (ptr < (mDNSu8*)msg || ptr >= end)
2785             { debugf("getDomainName: Illegal compression pointer not within packet boundaries"); return(mDNSNULL); }
2786             if (*ptr & 0xC0)
2787             { debugf("getDomainName: Compression pointer must point to real label"); return(mDNSNULL); }
2788             break;
2789         }
2790     }
2791 
2792     if (nextbyte) return(nextbyte);
2793     else return(ptr);
2794 }
2795 
2796 mDNSexport const mDNSu8 *skipResourceRecord(const DNSMessage *msg, const mDNSu8 *ptr, const mDNSu8 *end)
2797 {
2798     mDNSu16 pktrdlength;
2799 
2800     ptr = skipDomainName(msg, ptr, end);
2801     if (!ptr) { debugf("skipResourceRecord: Malformed RR name"); return(mDNSNULL); }
2802 
2803     if (ptr + 10 > end) { debugf("skipResourceRecord: Malformed RR -- no type/class/ttl/len!"); return(mDNSNULL); }
2804     pktrdlength = (mDNSu16)((mDNSu16)ptr[8] <<  8 | ptr[9]);
2805     ptr += 10;
2806     if (ptr + pktrdlength > end) { debugf("skipResourceRecord: RDATA exceeds end of packet"); return(mDNSNULL); }
2807 
2808     return(ptr + pktrdlength);
2809 }
2810 
2811 // Sanity check whether the NSEC/NSEC3 bitmap is good
2812 mDNSlocal mDNSu8 *SanityCheckBitMap(const mDNSu8 *bmap, const mDNSu8 *end, int len)
2813 {
2814     int win, wlen;
2815 
2816     while (bmap < end)
2817     {
2818         if (len < 3)
2819         {
2820             LogInfo("SanityCheckBitMap: invalid length %d", len);
2821             return mDNSNULL;
2822         }
2823 
2824         win = *bmap++;
2825         wlen = *bmap++;
2826         len -= 2;
2827         if (len < wlen || wlen < 1 || wlen > 32)
2828         {
2829             LogInfo("SanityCheckBitMap: invalid window length %d", wlen);
2830             return mDNSNULL;
2831         }
2832         if (win < 0 || win >= 256)
2833         {
2834             LogInfo("SanityCheckBitMap: invalid window %d", win);
2835             return mDNSNULL;
2836         }
2837 
2838         bmap += wlen;
2839         len -= wlen;
2840     }
2841     return (mDNSu8 *)bmap;
2842 }
2843 
2844 // This function is called with "msg" when we receive a DNS message and needs to parse a single resource record
2845 // pointed to by "ptr". Some resource records like SOA, SRV are converted to host order and also expanded
2846 // (domainnames are expanded to 255 bytes) when stored in memory.
2847 //
2848 // This function can also be called with "NULL" msg to parse a single resource record pointed to by ptr.
2849 // The caller can do this only if the names in the resource records are compressed and validity of the
2850 // resource record has already been done before. DNSSEC currently uses it this way.
2851 mDNSexport mDNSBool SetRData(const DNSMessage *const msg, const mDNSu8 *ptr, const mDNSu8 *end,
2852     LargeCacheRecord *const largecr, mDNSu16 rdlength)
2853 {
2854     CacheRecord *const rr = &largecr->r;
2855     RDataBody2 *const rdb = (RDataBody2 *)rr->smallrdatastorage.data;
2856 
2857     switch (rr->resrec.rrtype)
2858     {
2859     case kDNSType_A:
2860         if (rdlength != sizeof(mDNSv4Addr))
2861             goto fail;
2862         rdb->ipv4.b[0] = ptr[0];
2863         rdb->ipv4.b[1] = ptr[1];
2864         rdb->ipv4.b[2] = ptr[2];
2865         rdb->ipv4.b[3] = ptr[3];
2866         break;
2867 
2868     case kDNSType_NS:
2869     case kDNSType_MD:
2870     case kDNSType_MF:
2871     case kDNSType_CNAME:
2872     case kDNSType_MB:
2873     case kDNSType_MG:
2874     case kDNSType_MR:
2875     case kDNSType_PTR:
2876     case kDNSType_NSAP_PTR:
2877     case kDNSType_DNAME:
2878         if (msg)
2879         {
2880             ptr = getDomainName(msg, ptr, end, &rdb->name);
2881         }
2882         else
2883         {
2884             AssignDomainName(&rdb->name, (domainname *)ptr);
2885             ptr += DomainNameLength(&rdb->name);
2886         }
2887         if (ptr != end)
2888         {
2889             debugf("SetRData: Malformed CNAME/PTR RDATA name");
2890             goto fail;
2891         }
2892         break;
2893 
2894     case kDNSType_SOA:
2895         if (msg)
2896         {
2897             ptr = getDomainName(msg, ptr, end, &rdb->soa.mname);
2898         }
2899         else
2900         {
2901             AssignDomainName(&rdb->soa.mname, (domainname *)ptr);
2902             ptr += DomainNameLength(&rdb->soa.mname);
2903         }
2904         if (!ptr)
2905         {
2906             debugf("SetRData: Malformed SOA RDATA mname");
2907             goto fail;
2908         }
2909         if (msg)
2910         {
2911             ptr = getDomainName(msg, ptr, end, &rdb->soa.rname);
2912         }
2913         else
2914         {
2915             AssignDomainName(&rdb->soa.rname, (domainname *)ptr);
2916             ptr += DomainNameLength(&rdb->soa.rname);
2917         }
2918         if (!ptr)
2919         {
2920             debugf("SetRData: Malformed SOA RDATA rname");
2921             goto fail;
2922         }
2923         if (ptr + 0x14 != end)
2924         {
2925             debugf("SetRData: Malformed SOA RDATA");
2926             goto fail;
2927         }
2928         rdb->soa.serial  = (mDNSs32) ((mDNSs32)ptr[0x00] << 24 | (mDNSs32)ptr[0x01] << 16 | (mDNSs32)ptr[0x02] << 8 | ptr[0x03]);
2929         rdb->soa.refresh = (mDNSu32) ((mDNSu32)ptr[0x04] << 24 | (mDNSu32)ptr[0x05] << 16 | (mDNSu32)ptr[0x06] << 8 | ptr[0x07]);
2930         rdb->soa.retry   = (mDNSu32) ((mDNSu32)ptr[0x08] << 24 | (mDNSu32)ptr[0x09] << 16 | (mDNSu32)ptr[0x0A] << 8 | ptr[0x0B]);
2931         rdb->soa.expire  = (mDNSu32) ((mDNSu32)ptr[0x0C] << 24 | (mDNSu32)ptr[0x0D] << 16 | (mDNSu32)ptr[0x0E] << 8 | ptr[0x0F]);
2932         rdb->soa.min     = (mDNSu32) ((mDNSu32)ptr[0x10] << 24 | (mDNSu32)ptr[0x11] << 16 | (mDNSu32)ptr[0x12] << 8 | ptr[0x13]);
2933         break;
2934 
2935     case kDNSType_NULL:
2936     case kDNSType_HINFO:
2937     case kDNSType_TXT:
2938     case kDNSType_X25:
2939     case kDNSType_ISDN:
2940     case kDNSType_LOC:
2941     case kDNSType_DHCID:
2942         rr->resrec.rdlength = rdlength;
2943         mDNSPlatformMemCopy(rdb->data, ptr, rdlength);
2944         break;
2945 
2946     case kDNSType_MX:
2947     case kDNSType_AFSDB:
2948     case kDNSType_RT:
2949     case kDNSType_KX:
2950         // Preference + domainname
2951         if (rdlength < 3)
2952             goto fail;
2953         rdb->mx.preference = (mDNSu16)((mDNSu16)ptr[0] <<  8 | ptr[1]);
2954         ptr += 2;
2955         if (msg)
2956         {
2957             ptr = getDomainName(msg, ptr, end, &rdb->mx.exchange);
2958         }
2959         else
2960         {
2961             AssignDomainName(&rdb->mx.exchange, (domainname *)ptr);
2962             ptr += DomainNameLength(&rdb->mx.exchange);
2963         }
2964         if (ptr != end)
2965         {
2966             debugf("SetRData: Malformed MX name");
2967             goto fail;
2968         }
2969         break;
2970 
2971     case kDNSType_MINFO:
2972     case kDNSType_RP:
2973         // Domainname + domainname
2974         if (msg)
2975         {
2976             ptr = getDomainName(msg, ptr, end, &rdb->rp.mbox);
2977         }
2978         else
2979         {
2980             AssignDomainName(&rdb->rp.mbox, (domainname *)ptr);
2981             ptr += DomainNameLength(&rdb->rp.mbox);
2982         }
2983         if (!ptr)
2984         {
2985             debugf("SetRData: Malformed RP mbox");
2986             goto fail;
2987         }
2988         if (msg)
2989         {
2990             ptr = getDomainName(msg, ptr, end, &rdb->rp.txt);
2991         }
2992         else
2993         {
2994             AssignDomainName(&rdb->rp.txt, (domainname *)ptr);
2995             ptr += DomainNameLength(&rdb->rp.txt);
2996         }
2997         if (ptr != end)
2998         {
2999             debugf("SetRData: Malformed RP txt");
3000             goto fail;
3001         }
3002         break;
3003 
3004     case kDNSType_PX:
3005         // Preference + domainname + domainname
3006         if (rdlength < 4)
3007             goto fail;
3008         rdb->px.preference = (mDNSu16)((mDNSu16)ptr[0] <<  8 | ptr[1]);
3009         ptr += 2;
3010         if (msg)
3011         {
3012             ptr = getDomainName(msg, ptr, end, &rdb->px.map822);
3013         }
3014         else
3015         {
3016             AssignDomainName(&rdb->px.map822, (domainname *)ptr);
3017             ptr += DomainNameLength(&rdb->px.map822);
3018         }
3019         if (!ptr)
3020         {
3021             debugf("SetRData: Malformed PX map822");
3022             goto fail;
3023         }
3024         if (msg)
3025         {
3026             ptr = getDomainName(msg, ptr, end, &rdb->px.mapx400);
3027         }
3028         else
3029         {
3030             AssignDomainName(&rdb->px.mapx400, (domainname *)ptr);
3031             ptr += DomainNameLength(&rdb->px.mapx400);
3032         }
3033         if (ptr != end)
3034         {
3035             debugf("SetRData: Malformed PX mapx400");
3036             goto fail;
3037         }
3038         break;
3039 
3040     case kDNSType_AAAA:
3041         if (rdlength != sizeof(mDNSv6Addr))
3042             goto fail;
3043         mDNSPlatformMemCopy(&rdb->ipv6, ptr, sizeof(rdb->ipv6));
3044         break;
3045 
3046     case kDNSType_SRV:
3047         // Priority + weight + port + domainname
3048         if (rdlength < 7)
3049             goto fail;
3050         rdb->srv.priority = (mDNSu16)((mDNSu16)ptr[0] <<  8 | ptr[1]);
3051         rdb->srv.weight   = (mDNSu16)((mDNSu16)ptr[2] <<  8 | ptr[3]);
3052         rdb->srv.port.b[0] = ptr[4];
3053         rdb->srv.port.b[1] = ptr[5];
3054         ptr += 6;
3055         if (msg)
3056         {
3057             ptr = getDomainName(msg, ptr, end, &rdb->srv.target);
3058         }
3059         else
3060         {
3061             AssignDomainName(&rdb->srv.target, (domainname *)ptr);
3062             ptr += DomainNameLength(&rdb->srv.target);
3063         }
3064         if (ptr != end)
3065         {
3066             debugf("SetRData: Malformed SRV RDATA name");
3067             goto fail;
3068         }
3069         break;
3070 
3071     case kDNSType_NAPTR:
3072     {
3073         int savelen, len;
3074         domainname name;
3075         const mDNSu8 *orig = ptr;
3076 
3077         // Make sure the data is parseable and within the limits. DNSSEC code looks at
3078         // the domain name in the end for a valid domainname.
3079         //
3080         // Fixed length: Order, preference (4 bytes)
3081         // Variable length: flags, service, regexp, domainname
3082 
3083         if (rdlength < 8)
3084             goto fail;
3085         // Order, preference.
3086         ptr += 4;
3087         // Parse flags, Service and Regexp
3088         // length in the first byte does not include the length byte itself
3089         len = *ptr + 1;
3090         ptr += len;
3091         if (ptr >= end)
3092         {
3093             LogInfo("SetRData: Malformed NAPTR flags");
3094             goto fail;
3095         }
3096 
3097         // Service
3098         len = *ptr + 1;
3099         ptr += len;
3100         if (ptr >= end)
3101         {
3102             LogInfo("SetRData: Malformed NAPTR service");
3103             goto fail;
3104         }
3105 
3106         // Regexp
3107         len = *ptr + 1;
3108         ptr += len;
3109         if (ptr >= end)
3110         {
3111             LogInfo("SetRData: Malformed NAPTR regexp");
3112             goto fail;
3113         }
3114 
3115         savelen = ptr - orig;
3116 
3117         // RFC 2915 states that name compression is not allowed for this field. But RFC 3597
3118         // states that for NAPTR we should decompress. We make sure that we store the full
3119         // name rather than the compressed name
3120         if (msg)
3121         {
3122             ptr = getDomainName(msg, ptr, end, &name);
3123         }
3124         else
3125         {
3126             AssignDomainName(&name, (domainname *)ptr);
3127             ptr += DomainNameLength(&name);
3128         }
3129         if (ptr != end)
3130         {
3131             LogInfo("SetRData: Malformed NAPTR RDATA name");
3132             goto fail;
3133         }
3134 
3135         rr->resrec.rdlength = savelen + DomainNameLength(&name);
3136         // The uncompressed size should not exceed the limits
3137         if (rr->resrec.rdlength > MaximumRDSize)
3138         {
3139             LogInfo("SetRData: Malformed NAPTR rdlength %d, rr->resrec.rdlength %d, "
3140                     "bmaplen %d, name %##s", rdlength, rr->resrec.rdlength, name.c);
3141             goto fail;
3142         }
3143         mDNSPlatformMemCopy(rdb->data, orig, savelen);
3144         AssignDomainName((domainname *)(rdb->data + savelen), &name);
3145         break;
3146     }
3147     case kDNSType_OPT:  {
3148         mDNSu8 *dataend     = rr->resrec.rdata->u.data;
3149         rdataOPT *opt = rr->resrec.rdata->u.opt;
3150         rr->resrec.rdlength = 0;
3151         while (ptr < end && (mDNSu8 *)(opt+1) < &dataend[MaximumRDSize])
3152         {
3153             const rdataOPT *const currentopt = opt;
3154             if (ptr + 4 > end) { LogInfo("SetRData: OPT RDATA ptr + 4 > end"); goto fail; }
3155             opt->opt    = (mDNSu16)((mDNSu16)ptr[0] <<  8 | ptr[1]);
3156             opt->optlen = (mDNSu16)((mDNSu16)ptr[2] <<  8 | ptr[3]);
3157             ptr += 4;
3158             if (ptr + opt->optlen > end) { LogInfo("SetRData: ptr + opt->optlen > end"); goto fail; }
3159             switch (opt->opt)
3160             {
3161             case kDNSOpt_LLQ:
3162                 if (opt->optlen == DNSOpt_LLQData_Space - 4)
3163                 {
3164                     opt->u.llq.vers  = (mDNSu16)((mDNSu16)ptr[0] <<  8 | ptr[1]);
3165                     opt->u.llq.llqOp = (mDNSu16)((mDNSu16)ptr[2] <<  8 | ptr[3]);
3166                     opt->u.llq.err   = (mDNSu16)((mDNSu16)ptr[4] <<  8 | ptr[5]);
3167                     mDNSPlatformMemCopy(opt->u.llq.id.b, ptr+6, 8);
3168                     opt->u.llq.llqlease = (mDNSu32) ((mDNSu32)ptr[14] << 24 | (mDNSu32)ptr[15] << 16 | (mDNSu32)ptr[16] << 8 | ptr[17]);
3169                     if (opt->u.llq.llqlease > 0x70000000UL / mDNSPlatformOneSecond)
3170                         opt->u.llq.llqlease = 0x70000000UL / mDNSPlatformOneSecond;
3171                     opt++;
3172                 }
3173                 break;
3174             case kDNSOpt_Lease:
3175                 if (opt->optlen == DNSOpt_LeaseData_Space - 4)
3176                 {
3177                     opt->u.updatelease = (mDNSu32) ((mDNSu32)ptr[0] << 24 | (mDNSu32)ptr[1] << 16 | (mDNSu32)ptr[2] << 8 | ptr[3]);
3178                     if (opt->u.updatelease > 0x70000000UL / mDNSPlatformOneSecond)
3179                         opt->u.updatelease = 0x70000000UL / mDNSPlatformOneSecond;
3180                     opt++;
3181                 }
3182                 break;
3183             case kDNSOpt_Owner:
3184                 if (ValidOwnerLength(opt->optlen))
3185                 {
3186                     opt->u.owner.vers = ptr[0];
3187                     opt->u.owner.seq  = ptr[1];
3188                     mDNSPlatformMemCopy(opt->u.owner.HMAC.b, ptr+2, 6);                         // 6-byte MAC address
3189                     mDNSPlatformMemCopy(opt->u.owner.IMAC.b, ptr+2, 6);                         // 6-byte MAC address
3190                     opt->u.owner.password = zeroEthAddr;
3191                     if (opt->optlen >= DNSOpt_OwnerData_ID_Wake_Space-4)
3192                     {
3193                         mDNSPlatformMemCopy(opt->u.owner.IMAC.b, ptr+8, 6);                     // 6-byte MAC address
3194                         // This mDNSPlatformMemCopy is safe because the ValidOwnerLength(opt->optlen) check above
3195                         // ensures that opt->optlen is no more than DNSOpt_OwnerData_ID_Wake_PW6_Space - 4
3196                         if (opt->optlen > DNSOpt_OwnerData_ID_Wake_Space-4)
3197                             mDNSPlatformMemCopy(opt->u.owner.password.b, ptr+14, opt->optlen - (DNSOpt_OwnerData_ID_Wake_Space-4));
3198                     }
3199                     opt++;
3200                 }
3201                 break;
3202             case kDNSOpt_Trace:
3203                 if (opt->optlen == DNSOpt_TraceData_Space - 4)
3204                 {
3205                     opt->u.tracer.platf   = ptr[0];
3206                     opt->u.tracer.mDNSv   = (mDNSu32) ((mDNSu32)ptr[1] << 24 | (mDNSu32)ptr[2] << 16 | (mDNSu32)ptr[3] << 8 | ptr[4]);
3207                     opt++;
3208                 }
3209                 else
3210                 {
3211                     opt->u.tracer.platf   = 0xFF;
3212                     opt->u.tracer.mDNSv   = 0xFFFFFFFF;
3213                     opt++;
3214                 }
3215                 break;
3216             }
3217             ptr += currentopt->optlen;
3218         }
3219         rr->resrec.rdlength = (mDNSu16)((mDNSu8*)opt - rr->resrec.rdata->u.data);
3220         if (ptr != end) { LogInfo("SetRData: Malformed OptRdata"); goto fail; }
3221         break;
3222     }
3223 
3224     case kDNSType_NSEC: {
3225         domainname name;
3226         int len = rdlength;
3227         int bmaplen, dlen;
3228         const mDNSu8 *orig = ptr;
3229         const mDNSu8 *bmap;
3230 
3231         if (msg)
3232         {
3233             ptr = getDomainName(msg, ptr, end, &name);
3234         }
3235         else
3236         {
3237             AssignDomainName(&name, (domainname *)ptr);
3238             ptr += DomainNameLength(&name);
3239         }
3240         if (!ptr)
3241         {
3242             LogInfo("SetRData: Malformed NSEC nextname");
3243             goto fail;
3244         }
3245 
3246         dlen = DomainNameLength(&name);
3247 
3248         // Multicast NSECs use name compression for this field unlike the unicast case which
3249         // does not use compression. And multicast case always succeeds in compression. So,
3250         // the rdlength includes only the compressed space in that case. So, can't
3251         // use the DomainNameLength of name to reduce the length here.
3252         len -= (ptr - orig);
3253         bmaplen = len;                  // Save the length of the bitmap
3254         bmap = ptr;
3255         ptr = SanityCheckBitMap(bmap, end, len);
3256         if (!ptr)
3257             goto fail;
3258         if (ptr != end)
3259         {
3260             LogInfo("SetRData: Malformed NSEC length not right");
3261             goto fail;
3262         }
3263 
3264         // Initialize the right length here. When we call SetNewRData below which in turn calls
3265         // GetRDLength and for NSEC case, it assumes that rdlength is intitialized
3266         rr->resrec.rdlength = DomainNameLength(&name) + bmaplen;
3267 
3268         // Do we have space after the name expansion ?
3269         if (rr->resrec.rdlength > MaximumRDSize)
3270         {
3271             LogInfo("SetRData: Malformed NSEC rdlength %d, rr->resrec.rdlength %d, "
3272                     "bmaplen %d, name %##s", rdlength, rr->resrec.rdlength, name.c);
3273             goto fail;
3274         }
3275         AssignDomainName((domainname *)rdb->data, &name);
3276         mDNSPlatformMemCopy(rdb->data + dlen, bmap, bmaplen);
3277         break;
3278     }
3279     case kDNSType_NSEC3:
3280     {
3281         rdataNSEC3 *nsec3 = (rdataNSEC3 *)ptr;
3282         mDNSu8 *p = (mDNSu8 *)&nsec3->salt;
3283         int hashLength, bitmaplen;
3284 
3285         if (rdlength < NSEC3_FIXED_SIZE + 1)
3286         {
3287             LogInfo("SetRData: NSEC3 too small length %d", rdlength);
3288             goto fail;
3289         }
3290         if (nsec3->alg != SHA1_DIGEST_TYPE)
3291         {
3292             LogInfo("SetRData: nsec3 alg %d not supported", nsec3->alg);
3293             goto fail;
3294         }
3295         if (swap16(nsec3->iterations) > NSEC3_MAX_ITERATIONS)
3296         {
3297             LogInfo("SetRData: nsec3 iteration count %d too big", swap16(nsec3->iterations));
3298             goto fail;
3299         }
3300         p += nsec3->saltLength;
3301         // There should at least be one byte beyond saltLength
3302         if (p >= end)
3303         {
3304             LogInfo("SetRData: nsec3 too small, at saltlength %d, p %p, end %p", nsec3->saltLength, p, end);
3305             goto fail;
3306         }
3307         // p is pointing at hashLength
3308         hashLength = (int)*p++;
3309         if (!hashLength)
3310         {
3311             LogInfo("SetRData: hashLength zero");
3312             goto fail;
3313         }
3314         p += hashLength;
3315         if (p > end)
3316         {
3317             LogInfo("SetRData: nsec3 too small, at hashLength %d, p %p, end %p", hashLength, p, end);
3318             goto fail;
3319         }
3320 
3321         bitmaplen = rdlength - (int)(p - ptr);
3322         p = SanityCheckBitMap(p, end, bitmaplen);
3323         if (!p)
3324             goto fail;
3325         rr->resrec.rdlength = rdlength;
3326         mDNSPlatformMemCopy(rdb->data, ptr, rdlength);
3327         break;
3328     }
3329     case kDNSType_TKEY:
3330     case kDNSType_TSIG:
3331     {
3332         domainname name;
3333         int dlen, rlen;
3334 
3335         // The name should not be compressed. But we take the conservative approach
3336         // and uncompress the name before we store it.
3337         if (msg)
3338         {
3339             ptr = getDomainName(msg, ptr, end, &name);
3340         }
3341         else
3342         {
3343             AssignDomainName(&name, (domainname *)ptr);
3344             ptr += DomainNameLength(&name);
3345         }
3346         if (!ptr)
3347         {
3348             LogInfo("SetRData: Malformed name for TSIG/TKEY type %d", rr->resrec.rrtype);
3349             goto fail;
3350         }
3351         dlen = DomainNameLength(&name);
3352         rlen = end - ptr;
3353         rr->resrec.rdlength = dlen + rlen;
3354         AssignDomainName((domainname *)rdb->data, &name);
3355         mDNSPlatformMemCopy(rdb->data + dlen, ptr, rlen);
3356         break;
3357     }
3358     case kDNSType_RRSIG:
3359     {
3360         const mDNSu8 *sig = ptr + RRSIG_FIXED_SIZE;
3361         const mDNSu8 *orig = sig;
3362         domainname name;
3363         if (rdlength < RRSIG_FIXED_SIZE + 1)
3364         {
3365             LogInfo("SetRData: RRSIG too small length %d", rdlength);
3366             goto fail;
3367         }
3368         if (msg)
3369         {
3370             sig = getDomainName(msg, sig, end, &name);
3371         }
3372         else
3373         {
3374             AssignDomainName(&name, (domainname *)sig);
3375             sig += DomainNameLength(&name);
3376         }
3377         if (!sig)
3378         {
3379             LogInfo("SetRData: Malformed RRSIG record");
3380             goto fail;
3381         }
3382 
3383         if ((sig - orig) != DomainNameLength(&name))
3384         {
3385             LogInfo("SetRData: Malformed RRSIG record, signer name compression");
3386             goto fail;
3387         }
3388         // Just ensure that we have at least one byte of the signature
3389         if (sig + 1 >= end)
3390         {
3391             LogInfo("SetRData: Not enough bytes for signature type %d", rr->resrec.rrtype);
3392             goto fail;
3393         }
3394         rr->resrec.rdlength = rdlength;
3395         mDNSPlatformMemCopy(rdb->data, ptr, rdlength);
3396         break;
3397     }
3398     case kDNSType_DNSKEY:
3399     {
3400         if (rdlength < DNSKEY_FIXED_SIZE + 1)
3401         {
3402             LogInfo("SetRData: DNSKEY too small length %d", rdlength);
3403             goto fail;
3404         }
3405         rr->resrec.rdlength = rdlength;
3406         mDNSPlatformMemCopy(rdb->data, ptr, rdlength);
3407         break;
3408     }
3409     case kDNSType_DS:
3410     {
3411         if (rdlength < DS_FIXED_SIZE + 1)
3412         {
3413             LogInfo("SetRData: DS too small length %d", rdlength);
3414             goto fail;
3415         }
3416         rr->resrec.rdlength = rdlength;
3417         mDNSPlatformMemCopy(rdb->data, ptr, rdlength);
3418         break;
3419     }
3420     default:
3421         debugf("SetRData: Warning! Reading resource type %d (%s) as opaque data",
3422                rr->resrec.rrtype, DNSTypeName(rr->resrec.rrtype));
3423         // Note: Just because we don't understand the record type, that doesn't
3424         // mean we fail. The DNS protocol specifies rdlength, so we can
3425         // safely skip over unknown records and ignore them.
3426         // We also grab a binary copy of the rdata anyway, since the caller
3427         // might know how to interpret it even if we don't.
3428         rr->resrec.rdlength = rdlength;
3429         mDNSPlatformMemCopy(rdb->data, ptr, rdlength);
3430         break;
3431     }
3432     return mDNStrue;
3433 fail:
3434     return mDNSfalse;
3435 }
3436 
3437 mDNSexport const mDNSu8 *GetLargeResourceRecord(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *ptr,
3438                                                 const mDNSu8 *end, const mDNSInterfaceID InterfaceID, mDNSu8 RecordType, LargeCacheRecord *const largecr)
3439 {
3440     CacheRecord *const rr = &largecr->r;
3441     mDNSu16 pktrdlength;
3442 
3443     if (largecr == &m->rec && m->rec.r.resrec.RecordType)
3444     {
3445         LogMsg("GetLargeResourceRecord: m->rec appears to be already in use for %s", CRDisplayString(m, &m->rec.r));
3446 #if ForceAlerts
3447         *(long*)0 = 0;
3448 #endif
3449     }
3450 
3451     rr->next              = mDNSNULL;
3452     rr->resrec.name       = &largecr->namestorage;
3453 
3454     rr->NextInKAList      = mDNSNULL;
3455     rr->TimeRcvd          = m ? m->timenow : 0;
3456     rr->DelayDelivery     = 0;
3457     rr->NextRequiredQuery = m ? m->timenow : 0;     // Will be updated to the real value when we call SetNextCacheCheckTimeForRecord()
3458     rr->LastUsed          = m ? m->timenow : 0;
3459     rr->CRActiveQuestion  = mDNSNULL;
3460     rr->UnansweredQueries = 0;
3461     rr->LastUnansweredTime= 0;
3462 #if ENABLE_MULTI_PACKET_QUERY_SNOOPING
3463     rr->MPUnansweredQ     = 0;
3464     rr->MPLastUnansweredQT= 0;
3465     rr->MPUnansweredKA    = 0;
3466     rr->MPExpectingKA     = mDNSfalse;
3467 #endif
3468     rr->NextInCFList      = mDNSNULL;
3469 
3470     rr->resrec.InterfaceID       = InterfaceID;
3471     rr->resrec.rDNSServer = mDNSNULL;
3472 
3473     ptr = getDomainName(msg, ptr, end, &largecr->namestorage);      // Will bail out correctly if ptr is NULL
3474     if (!ptr) { debugf("GetLargeResourceRecord: Malformed RR name"); return(mDNSNULL); }
3475     rr->resrec.namehash = DomainNameHashValue(rr->resrec.name);
3476 
3477     if (ptr + 10 > end) { debugf("GetLargeResourceRecord: Malformed RR -- no type/class/ttl/len!"); return(mDNSNULL); }
3478 
3479     rr->resrec.rrtype            = (mDNSu16) ((mDNSu16)ptr[0] <<  8 | ptr[1]);
3480     rr->resrec.rrclass           = (mDNSu16)(((mDNSu16)ptr[2] <<  8 | ptr[3]) & kDNSClass_Mask);
3481     rr->resrec.rroriginalttl     = (mDNSu32) ((mDNSu32)ptr[4] << 24 | (mDNSu32)ptr[5] << 16 | (mDNSu32)ptr[6] << 8 | ptr[7]);
3482     if (rr->resrec.rroriginalttl > 0x70000000UL / mDNSPlatformOneSecond && (mDNSs32)rr->resrec.rroriginalttl != -1)
3483         rr->resrec.rroriginalttl = 0x70000000UL / mDNSPlatformOneSecond;
3484     // Note: We don't have to adjust m->NextCacheCheck here -- this is just getting a record into memory for
3485     // us to look at. If we decide to copy it into the cache, then we'll update m->NextCacheCheck accordingly.
3486     pktrdlength           = (mDNSu16)((mDNSu16)ptr[8] <<  8 | ptr[9]);
3487 
3488     // If mDNS record has cache-flush bit set, we mark it unique
3489     // For uDNS records, all are implicitly deemed unique (a single DNS server is always
3490     // authoritative for the entire RRSet), unless this is a truncated response
3491     if (ptr[2] & (kDNSClass_UniqueRRSet >> 8) || (!InterfaceID && !(msg->h.flags.b[0] & kDNSFlag0_TC)))
3492         RecordType |= kDNSRecordTypePacketUniqueMask;
3493     ptr += 10;
3494     if (ptr + pktrdlength > end) { debugf("GetLargeResourceRecord: RDATA exceeds end of packet"); return(mDNSNULL); }
3495     end = ptr + pktrdlength;        // Adjust end to indicate the end of the rdata for this resource record
3496 
3497     rr->resrec.rdata = (RData*)&rr->smallrdatastorage;
3498     rr->resrec.rdata->MaxRDLength = MaximumRDSize;
3499 
3500     if (pktrdlength > MaximumRDSize)
3501     {
3502         LogInfo("GetLargeResourceRecord: %s rdata size (%d) exceeds storage (%d)",
3503                 DNSTypeName(rr->resrec.rrtype), pktrdlength, rr->resrec.rdata->MaxRDLength);
3504         goto fail;
3505     }
3506 
3507     if (!RecordType) LogMsg("GetLargeResourceRecord: No RecordType for %##s", rr->resrec.name->c);
3508 
3509     // IMPORTANT: Any record type we understand and unpack into a structure containing domainnames needs to have corresponding
3510     // cases in SameRDataBody() and RDataHashValue() to do a semantic comparison (or checksum) of the structure instead of a blind
3511     // bitwise memory compare (or sum). This is because a domainname is a fixed size structure holding variable-length data.
3512     // Any bytes past the logical end of the name are undefined, and a blind bitwise memory compare may indicate that
3513     // two domainnames are different when semantically they are the same name and it's only the unused bytes that differ.
3514     if (rr->resrec.rrclass == kDNSQClass_ANY && pktrdlength == 0)   // Used in update packets to mean "Delete An RRset" (RFC 2136)
3515         rr->resrec.rdlength = 0;
3516     else if (!SetRData(msg, ptr, end, largecr, pktrdlength))
3517         goto fail;
3518 
3519     SetNewRData(&rr->resrec, mDNSNULL, 0);      // Sets rdlength, rdestimate, rdatahash for us
3520 
3521     // Success! Now fill in RecordType to show this record contains valid data
3522     rr->resrec.RecordType = RecordType;
3523     return(end);
3524 
3525 fail:
3526     // If we were unable to parse the rdata in this record, we indicate that by
3527     // returing a 'kDNSRecordTypePacketNegative' record with rdlength set to zero
3528     rr->resrec.RecordType = kDNSRecordTypePacketNegative;
3529     rr->resrec.rdlength   = 0;
3530     rr->resrec.rdestimate = 0;
3531     rr->resrec.rdatahash  = 0;
3532     return(end);
3533 }
3534 
3535 mDNSexport const mDNSu8 *skipQuestion(const DNSMessage *msg, const mDNSu8 *ptr, const mDNSu8 *end)
3536 {
3537     ptr = skipDomainName(msg, ptr, end);
3538     if (!ptr) { debugf("skipQuestion: Malformed domain name in DNS question section"); return(mDNSNULL); }
3539     if (ptr+4 > end) { debugf("skipQuestion: Malformed DNS question section -- no query type and class!"); return(mDNSNULL); }
3540     return(ptr+4);
3541 }
3542 
3543 mDNSexport const mDNSu8 *getQuestion(const DNSMessage *msg, const mDNSu8 *ptr, const mDNSu8 *end, const mDNSInterfaceID InterfaceID,
3544                                      DNSQuestion *question)
3545 {
3546     mDNSPlatformMemZero(question, sizeof(*question));
3547     question->InterfaceID = InterfaceID;
3548     if (!InterfaceID) question->TargetQID = onesID; // In DNSQuestions we use TargetQID as the indicator of whether it's unicast or multicast
3549     ptr = getDomainName(msg, ptr, end, &question->qname);
3550     if (!ptr) { debugf("Malformed domain name in DNS question section"); return(mDNSNULL); }
3551     if (ptr+4 > end) { debugf("Malformed DNS question section -- no query type and class!"); return(mDNSNULL); }
3552 
3553     question->qnamehash = DomainNameHashValue(&question->qname);
3554     question->qtype  = (mDNSu16)((mDNSu16)ptr[0] << 8 | ptr[1]);            // Get type
3555     question->qclass = (mDNSu16)((mDNSu16)ptr[2] << 8 | ptr[3]);            // and class
3556     return(ptr+4);
3557 }
3558 
3559 mDNSexport const mDNSu8 *LocateAnswers(const DNSMessage *const msg, const mDNSu8 *const end)
3560 {
3561     int i;
3562     const mDNSu8 *ptr = msg->data;
3563     for (i = 0; i < msg->h.numQuestions && ptr; i++) ptr = skipQuestion(msg, ptr, end);
3564     return(ptr);
3565 }
3566 
3567 mDNSexport const mDNSu8 *LocateAuthorities(const DNSMessage *const msg, const mDNSu8 *const end)
3568 {
3569     int i;
3570     const mDNSu8 *ptr = LocateAnswers(msg, end);
3571     for (i = 0; i < msg->h.numAnswers && ptr; i++) ptr = skipResourceRecord(msg, ptr, end);
3572     return(ptr);
3573 }
3574 
3575 mDNSexport const mDNSu8 *LocateAdditionals(const DNSMessage *const msg, const mDNSu8 *const end)
3576 {
3577     int i;
3578     const mDNSu8 *ptr = LocateAuthorities(msg, end);
3579     for (i = 0; i < msg->h.numAuthorities; i++) ptr = skipResourceRecord(msg, ptr, end);
3580     return (ptr);
3581 }
3582 
3583 mDNSexport const mDNSu8 *LocateOptRR(const DNSMessage *const msg, const mDNSu8 *const end, int minsize)
3584 {
3585     int i;
3586     const mDNSu8 *ptr = LocateAdditionals(msg, end);
3587 
3588     // Locate the OPT record.
3589     // According to RFC 2671, "One OPT pseudo-RR can be added to the additional data section of either a request or a response."
3590     // This implies that there may be *at most* one OPT record per DNS message, in the Additional Section,
3591     // but not necessarily the *last* entry in the Additional Section.
3592     for (i = 0; ptr && i < msg->h.numAdditionals; i++)
3593     {
3594         if (ptr + DNSOpt_Header_Space + minsize <= end &&   // Make sure we have 11+minsize bytes of data
3595             ptr[0] == 0                                &&   // Name must be root label
3596             ptr[1] == (kDNSType_OPT >> 8  )            &&   // rrtype OPT
3597             ptr[2] == (kDNSType_OPT & 0xFF)            &&
3598             ((mDNSu16)ptr[9] << 8 | (mDNSu16)ptr[10]) >= (mDNSu16)minsize)
3599             return(ptr);
3600         else
3601             ptr = skipResourceRecord(msg, ptr, end);
3602     }
3603     return(mDNSNULL);
3604 }
3605 
3606 // On success, GetLLQOptData returns pointer to storage within shared "m->rec";
3607 // it is caller's responsibilty to clear m->rec.r.resrec.RecordType after use
3608 // Note: An OPT RDataBody actually contains one or more variable-length rdataOPT objects packed together
3609 // The code that currently calls this assumes there's only one, instead of iterating through the set
3610 mDNSexport const rdataOPT *GetLLQOptData(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *const end)
3611 {
3612     const mDNSu8 *ptr = LocateOptRR(msg, end, DNSOpt_LLQData_Space);
3613     if (ptr)
3614     {
3615         ptr = GetLargeResourceRecord(m, msg, ptr, end, 0, kDNSRecordTypePacketAdd, &m->rec);
3616         if (ptr && m->rec.r.resrec.RecordType != kDNSRecordTypePacketNegative) return(&m->rec.r.resrec.rdata->u.opt[0]);
3617     }
3618     return(mDNSNULL);
3619 }
3620 
3621 // Get the lease life of records in a dynamic update
3622 // returns 0 on error or if no lease present
3623 mDNSexport mDNSu32 GetPktLease(mDNS *m, DNSMessage *msg, const mDNSu8 *end)
3624 {
3625     mDNSu32 result = 0;
3626     const mDNSu8 *ptr = LocateOptRR(msg, end, DNSOpt_LeaseData_Space);
3627     if (ptr) ptr = GetLargeResourceRecord(m, msg, ptr, end, 0, kDNSRecordTypePacketAdd, &m->rec);
3628     if (ptr && m->rec.r.resrec.rdlength >= DNSOpt_LeaseData_Space && m->rec.r.resrec.rdata->u.opt[0].opt == kDNSOpt_Lease)
3629         result = m->rec.r.resrec.rdata->u.opt[0].u.updatelease;
3630     m->rec.r.resrec.RecordType = 0;     // Clear RecordType to show we're not still using it
3631     return(result);
3632 }
3633 
3634 mDNSlocal const mDNSu8 *DumpRecords(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *ptr, const mDNSu8 *const end, int count, char *label)
3635 {
3636     int i;
3637     LogMsg("%2d %s", count, label);
3638     for (i = 0; i < count && ptr; i++)
3639     {
3640         // This puts a LargeCacheRecord on the stack instead of using the shared m->rec storage,
3641         // but since it's only used for debugging (and probably only on OS X, not on
3642         // embedded systems) putting a 9kB object on the stack isn't a big problem.
3643         LargeCacheRecord largecr;
3644         ptr = GetLargeResourceRecord(m, msg, ptr, end, mDNSInterface_Any, kDNSRecordTypePacketAns, &largecr);
3645         if (ptr) LogMsg("%2d TTL%8d %s", i, largecr.r.resrec.rroriginalttl, CRDisplayString(m, &largecr.r));
3646     }
3647     if (!ptr) LogMsg("DumpRecords: ERROR: Premature end of packet data");
3648     return(ptr);
3649 }
3650 
3651 #define DNS_OP_Name(X) (                              \
3652         (X) == kDNSFlag0_OP_StdQuery ? ""         :       \
3653         (X) == kDNSFlag0_OP_Iquery   ? "Iquery "  :       \
3654         (X) == kDNSFlag0_OP_Status   ? "Status "  :       \
3655         (X) == kDNSFlag0_OP_Unused3  ? "Unused3 " :       \
3656         (X) == kDNSFlag0_OP_Notify   ? "Notify "  :       \
3657         (X) == kDNSFlag0_OP_Update   ? "Update "  : "?? " )
3658 
3659 #define DNS_RC_Name(X) (                             \
3660         (X) == kDNSFlag1_RC_NoErr    ? "NoErr"    :      \
3661         (X) == kDNSFlag1_RC_FormErr  ? "FormErr"  :      \
3662         (X) == kDNSFlag1_RC_ServFail ? "ServFail" :      \
3663         (X) == kDNSFlag1_RC_NXDomain ? "NXDomain" :      \
3664         (X) == kDNSFlag1_RC_NotImpl  ? "NotImpl"  :      \
3665         (X) == kDNSFlag1_RC_Refused  ? "Refused"  :      \
3666         (X) == kDNSFlag1_RC_YXDomain ? "YXDomain" :      \
3667         (X) == kDNSFlag1_RC_YXRRSet  ? "YXRRSet"  :      \
3668         (X) == kDNSFlag1_RC_NXRRSet  ? "NXRRSet"  :      \
3669         (X) == kDNSFlag1_RC_NotAuth  ? "NotAuth"  :      \
3670         (X) == kDNSFlag1_RC_NotZone  ? "NotZone"  : "??" )
3671 
3672 // Note: DumpPacket expects the packet header fields in host byte order, not network byte order
3673 mDNSexport void DumpPacket(mDNS *const m, mStatus status, mDNSBool sent, char *transport,
3674                            const mDNSAddr *srcaddr, mDNSIPPort srcport,
3675                            const mDNSAddr *dstaddr, mDNSIPPort dstport, const DNSMessage *const msg, const mDNSu8 *const end)
3676 {
3677     mDNSBool IsUpdate = ((msg->h.flags.b[0] & kDNSFlag0_OP_Mask) == kDNSFlag0_OP_Update);
3678     const mDNSu8 *ptr = msg->data;
3679     int i;
3680     DNSQuestion q;
3681     char tbuffer[64], sbuffer[64], dbuffer[64] = "";
3682     if (!status) tbuffer[mDNS_snprintf(tbuffer, sizeof(tbuffer), sent ? "Sent" : "Received"                        )] = 0;
3683     else tbuffer[mDNS_snprintf(tbuffer, sizeof(tbuffer), "ERROR %d %sing", status, sent ? "Send" : "Receive")] = 0;
3684     if (sent) sbuffer[mDNS_snprintf(sbuffer, sizeof(sbuffer), "port "        )] = 0;
3685     else sbuffer[mDNS_snprintf(sbuffer, sizeof(sbuffer), "%#a:", srcaddr)] = 0;
3686     if (dstaddr || !mDNSIPPortIsZero(dstport))
3687         dbuffer[mDNS_snprintf(dbuffer, sizeof(dbuffer), " to %#a:%d", dstaddr, mDNSVal16(dstport))] = 0;
3688 
3689     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 --",
3690            tbuffer, transport,
3691            DNS_OP_Name(msg->h.flags.b[0] & kDNSFlag0_OP_Mask),
3692            msg->h.flags.b[0] & kDNSFlag0_QR_Response ? "Response" : "Query",
3693            msg->h.flags.b[0], msg->h.flags.b[1],
3694            DNS_RC_Name(msg->h.flags.b[1] & kDNSFlag1_RC_Mask),
3695            msg->h.flags.b[1] & kDNSFlag1_RC_Mask,
3696            msg->h.flags.b[0] & kDNSFlag0_AA ? "AA " : "",
3697            msg->h.flags.b[0] & kDNSFlag0_TC ? "TC " : "",
3698            msg->h.flags.b[0] & kDNSFlag0_RD ? "RD " : "",
3699            msg->h.flags.b[1] & kDNSFlag1_RA ? "RA " : "",
3700            msg->h.flags.b[1] & kDNSFlag1_AD ? "AD " : "",
3701            msg->h.flags.b[1] & kDNSFlag1_CD ? "CD " : "",
3702            mDNSVal16(msg->h.id),
3703            end - msg->data,
3704            sbuffer, mDNSVal16(srcport), dbuffer,
3705            (msg->h.flags.b[0] & kDNSFlag0_TC) ? " (truncated)" : ""
3706            );
3707 
3708     LogMsg("%2d %s", msg->h.numQuestions, IsUpdate ? "Zone" : "Questions");
3709     for (i = 0; i < msg->h.numQuestions && ptr; i++)
3710     {
3711         ptr = getQuestion(msg, ptr, end, mDNSInterface_Any, &q);
3712         if (ptr) LogMsg("%2d %##s %s", i, q.qname.c, DNSTypeName(q.qtype));
3713     }
3714     ptr = DumpRecords(m, msg, ptr, end, msg->h.numAnswers,     IsUpdate ? "Prerequisites" : "Answers");
3715     ptr = DumpRecords(m, msg, ptr, end, msg->h.numAuthorities, IsUpdate ? "Updates"       : "Authorities");
3716     ptr = DumpRecords(m, msg, ptr, end, msg->h.numAdditionals, "Additionals");
3717     LogMsg("--------------");
3718 }
3719 
3720 // ***************************************************************************
3721 #if COMPILER_LIKES_PRAGMA_MARK
3722 #pragma mark -
3723 #pragma mark - Packet Sending Functions
3724 #endif
3725 
3726 // Stub definition of TCPSocket_struct so we can access flags field. (Rest of TCPSocket_struct is platform-dependent.)
3727 struct TCPSocket_struct { TCPSocketFlags flags; /* ... */ };
3728 
3729 struct UDPSocket_struct
3730 {
3731     mDNSIPPort port; // MUST BE FIRST FIELD -- mDNSCoreReceive expects every UDPSocket_struct to begin with mDNSIPPort port
3732 };
3733 
3734 // Note: When we sign a DNS message using DNSDigest_SignMessage(), the current real-time clock value is used, which
3735 // is why we generally defer signing until we send the message, to ensure the signature is as fresh as possible.
3736 mDNSexport mStatus mDNSSendDNSMessage(mDNS *const m, DNSMessage *const msg, mDNSu8 *end,
3737                                       mDNSInterfaceID InterfaceID, UDPSocket *src, const mDNSAddr *dst,
3738                                       mDNSIPPort dstport, TCPSocket *sock, DomainAuthInfo *authInfo,
3739                                       mDNSBool useBackgroundTrafficClass)
3740 {
3741     mStatus status = mStatus_NoError;
3742     const mDNSu16 numAdditionals = msg->h.numAdditionals;
3743     mDNSu8 *newend;
3744     mDNSu8 *limit = msg->data + AbsoluteMaxDNSMessageData;
3745 
3746 #if APPLE_OSX_mDNSResponder
3747     // maintain outbound packet statistics
3748     if (mDNSOpaque16IsZero(msg->h.id))
3749         m->MulticastPacketsSent++;
3750     else
3751         m->UnicastPacketsSent++;
3752 #endif // APPLE_OSX_mDNSResponder
3753 
3754     // Zero-length message data is okay (e.g. for a DNS Update ack, where all we need is an ID and an error code
3755     if (end < msg->data || end - msg->data > AbsoluteMaxDNSMessageData)
3756     {
3757         LogMsg("mDNSSendDNSMessage: invalid message %p %p %d", msg->data, end, end - msg->data);
3758         return mStatus_BadParamErr;
3759     }
3760 
3761     newend = putHINFO(m, msg, end, authInfo, limit);
3762     if (!newend) LogMsg("mDNSSendDNSMessage: putHINFO failed msg %p end %p, limit %p", msg->data, end, limit); // Not fatal
3763     else end = newend;
3764 
3765     // Put all the integer values in IETF byte-order (MSB first, LSB second)
3766     SwapDNSHeaderBytes(msg);
3767 
3768     if (authInfo) DNSDigest_SignMessage(msg, &end, authInfo, 0);    // DNSDigest_SignMessage operates on message in network byte order
3769     if (!end) { LogMsg("mDNSSendDNSMessage: DNSDigest_SignMessage failed"); status = mStatus_NoMemoryErr; }
3770     else
3771     {
3772         // Send the packet on the wire
3773         if (!sock)
3774             status = mDNSPlatformSendUDP(m, msg, end, InterfaceID, src, dst, dstport, useBackgroundTrafficClass);
3775         else
3776         {
3777             mDNSu16 msglen = (mDNSu16)(end - (mDNSu8 *)msg);
3778             mDNSu8 lenbuf[2] = { (mDNSu8)(msglen >> 8), (mDNSu8)(msglen & 0xFF) };
3779             char *buf;
3780             long nsent;
3781 
3782             // Try to send them in one packet if we can allocate enough memory
3783             buf = mDNSPlatformMemAllocate(msglen + 2);
3784             if (buf)
3785             {
3786                 buf[0] = lenbuf[0];
3787                 buf[1] = lenbuf[1];
3788                 mDNSPlatformMemCopy(buf+2, msg, msglen);
3789                 nsent = mDNSPlatformWriteTCP(sock, buf, msglen+2);
3790                 if (nsent != (msglen + 2))
3791                 {
3792                     LogMsg("mDNSSendDNSMessage: write message failed %d/%d", nsent, msglen);
3793                     status = mStatus_ConnFailed;
3794                 }
3795                 mDNSPlatformMemFree(buf);
3796             }
3797             else
3798             {
3799                 nsent = mDNSPlatformWriteTCP(sock, (char*)lenbuf, 2);
3800                 if (nsent != 2)
3801                 {
3802                     LogMsg("mDNSSendDNSMessage: write msg length failed %d/%d", nsent, 2);
3803                     status = mStatus_ConnFailed;
3804                 }
3805                 else
3806                 {
3807                     nsent = mDNSPlatformWriteTCP(sock, (char *)msg, msglen);
3808                     if (nsent != msglen)
3809                     {
3810                         LogMsg("mDNSSendDNSMessage: write msg body failed %d/%d", nsent, msglen);
3811                         status = mStatus_ConnFailed;
3812                     }
3813                 }
3814             }
3815         }
3816     }
3817 
3818     // Swap the integer values back the way they were (remember that numAdditionals may have been changed by putHINFO and/or SignMessage)
3819     SwapDNSHeaderBytes(msg);
3820 
3821     // Dump the packet with the HINFO and TSIG
3822     if (mDNS_PacketLoggingEnabled && !mDNSOpaque16IsZero(msg->h.id)) {
3823 	mDNSIPPort port = MulticastDNSPort;
3824         DumpPacket(m, status, mDNStrue, sock &&
3825 	(sock->flags & kTCPSocketFlags_UseTLS) ?
3826 	"TLS" : sock ? "TCP" : "UDP", mDNSNULL,
3827 	src ? src->port : port, dst, dstport, msg, end);
3828     }
3829 
3830     // put the number of additionals back the way it was
3831     msg->h.numAdditionals = numAdditionals;
3832 
3833     return(status);
3834 }
3835 
3836 // ***************************************************************************
3837 #if COMPILER_LIKES_PRAGMA_MARK
3838 #pragma mark -
3839 #pragma mark - RR List Management & Task Management
3840 #endif
3841 
3842 mDNSexport void mDNS_Lock_(mDNS *const m, const char * const functionname)
3843 {
3844     // MUST grab the platform lock FIRST!
3845     mDNSPlatformLock(m);
3846 
3847     // Normally, mDNS_reentrancy is zero and so is mDNS_busy
3848     // However, when we call a client callback mDNS_busy is one, and we increment mDNS_reentrancy too
3849     // If that client callback does mDNS API calls, mDNS_reentrancy and mDNS_busy will both be one
3850     // If mDNS_busy != mDNS_reentrancy that's a bad sign
3851     if (m->mDNS_busy != m->mDNS_reentrancy)
3852     {
3853         LogMsg("%s: mDNS_Lock: Locking failure! mDNS_busy (%ld) != mDNS_reentrancy (%ld)", functionname, m->mDNS_busy, m->mDNS_reentrancy);
3854 #if ForceAlerts
3855         *(long*)0 = 0;
3856 #endif
3857     }
3858 
3859     // If this is an initial entry into the mDNSCore code, set m->timenow
3860     // else, if this is a re-entrant entry into the mDNSCore code, m->timenow should already be set
3861     if (m->mDNS_busy == 0)
3862     {
3863         if (m->timenow)
3864             LogMsg("%s: mDNS_Lock: m->timenow already set (%ld/%ld)", functionname, m->timenow, mDNS_TimeNow_NoLock(m));
3865         m->timenow = mDNS_TimeNow_NoLock(m);
3866         if (m->timenow == 0) m->timenow = 1;
3867     }
3868     else if (m->timenow == 0)
3869     {
3870         LogMsg("%s: mDNS_Lock: m->mDNS_busy is %ld but m->timenow not set", functionname, m->mDNS_busy);
3871         m->timenow = mDNS_TimeNow_NoLock(m);
3872         if (m->timenow == 0) m->timenow = 1;
3873     }
3874 
3875     if (m->timenow_last - m->timenow > 0)
3876     {
3877         m->timenow_adjust += m->timenow_last - m->timenow;
3878         LogMsg("%s: mDNSPlatformRawTime went backwards by %ld ticks; setting correction factor to %ld", functionname, m->timenow_last - m->timenow, m->timenow_adjust);
3879         m->timenow = m->timenow_last;
3880     }
3881     m->timenow_last = m->timenow;
3882 
3883     // Increment mDNS_busy so we'll recognise re-entrant calls
3884     m->mDNS_busy++;
3885 }
3886 
3887 mDNSlocal AuthRecord *AnyLocalRecordReady(const mDNS *const m)
3888 {
3889     AuthRecord *rr;
3890     for (rr = m->NewLocalRecords; rr; rr = rr->next)
3891         if (LocalRecordReady(rr)) return rr;
3892     return mDNSNULL;
3893 }
3894 
3895 mDNSlocal mDNSs32 GetNextScheduledEvent(const mDNS *const m)
3896 {
3897     mDNSs32 e = m->timenow + 0x78000000;
3898     if (m->mDNSPlatformStatus != mStatus_NoError) return(e);
3899     if (m->NewQuestions)
3900     {
3901         if (m->NewQuestions->DelayAnswering) e = m->NewQuestions->DelayAnswering;
3902         else return(m->timenow);
3903     }
3904     if (m->NewLocalOnlyQuestions) return(m->timenow);
3905     if (m->NewLocalRecords && AnyLocalRecordReady(m)) return(m->timenow);
3906     if (m->NewLocalOnlyRecords) return(m->timenow);
3907     if (m->SPSProxyListChanged) return(m->timenow);
3908     if (m->LocalRemoveEvents) return(m->timenow);
3909 
3910 #ifndef UNICAST_DISABLED
3911     if (e - m->NextuDNSEvent         > 0) e = m->NextuDNSEvent;
3912     if (e - m->NextScheduledNATOp    > 0) e = m->NextScheduledNATOp;
3913     if (m->NextSRVUpdate && e - m->NextSRVUpdate > 0) e = m->NextSRVUpdate;
3914 #endif
3915 
3916     if (e - m->NextCacheCheck        > 0) e = m->NextCacheCheck;
3917     if (e - m->NextScheduledSPS      > 0) e = m->NextScheduledSPS;
3918     if (e - m->NextScheduledKA       > 0) e = m->NextScheduledKA;
3919 
3920     // NextScheduledSPRetry only valid when DelaySleep not set
3921     if (!m->DelaySleep && m->SleepLimit && e - m->NextScheduledSPRetry > 0) e = m->NextScheduledSPRetry;
3922     if (m->DelaySleep && e - m->DelaySleep > 0) e = m->DelaySleep;
3923 
3924     if (m->SuppressSending)
3925     {
3926         if (e - m->SuppressSending       > 0) e = m->SuppressSending;
3927     }
3928     else
3929     {
3930         if (e - m->NextScheduledQuery    > 0) e = m->NextScheduledQuery;
3931         if (e - m->NextScheduledProbe    > 0) e = m->NextScheduledProbe;
3932         if (e - m->NextScheduledResponse > 0) e = m->NextScheduledResponse;
3933     }
3934     if (e - m->NextScheduledStopTime > 0) e = m->NextScheduledStopTime;
3935     return(e);
3936 }
3937 
3938 mDNSexport void ShowTaskSchedulingError(mDNS *const m)
3939 {
3940     AuthRecord *rr;
3941     mDNS_Lock(m);
3942 
3943     LogMsg("Task Scheduling Error: Continuously busy for more than a second");
3944 
3945     // Note: To accurately diagnose *why* we're busy, the debugging code here needs to mirror the logic in GetNextScheduledEvent above
3946 
3947     if (m->NewQuestions && (!m->NewQuestions->DelayAnswering || m->timenow - m->NewQuestions->DelayAnswering >= 0))
3948         LogMsg("Task Scheduling Error: NewQuestion %##s (%s)",
3949                m->NewQuestions->qname.c, DNSTypeName(m->NewQuestions->qtype));
3950 
3951     if (m->NewLocalOnlyQuestions)
3952         LogMsg("Task Scheduling Error: NewLocalOnlyQuestions %##s (%s)",
3953                m->NewLocalOnlyQuestions->qname.c, DNSTypeName(m->NewLocalOnlyQuestions->qtype));
3954 
3955     if (m->NewLocalRecords)
3956     {
3957         rr = AnyLocalRecordReady(m);
3958         if (rr) LogMsg("Task Scheduling Error: NewLocalRecords %s", ARDisplayString(m, rr));
3959     }
3960 
3961     if (m->NewLocalOnlyRecords) LogMsg("Task Scheduling Error: NewLocalOnlyRecords");
3962 
3963     if (m->SPSProxyListChanged) LogMsg("Task Scheduling Error: SPSProxyListChanged");
3964     if (m->LocalRemoveEvents) LogMsg("Task Scheduling Error: LocalRemoveEvents");
3965 
3966     if (m->timenow - m->NextScheduledEvent    >= 0)
3967         LogMsg("Task Scheduling Error: m->NextScheduledEvent %d",    m->timenow - m->NextScheduledEvent);
3968 
3969 #ifndef UNICAST_DISABLED
3970     if (m->timenow - m->NextuDNSEvent         >= 0)
3971         LogMsg("Task Scheduling Error: m->NextuDNSEvent %d",         m->timenow - m->NextuDNSEvent);
3972     if (m->timenow - m->NextScheduledNATOp    >= 0)
3973         LogMsg("Task Scheduling Error: m->NextScheduledNATOp %d",    m->timenow - m->NextScheduledNATOp);
3974     if (m->NextSRVUpdate && m->timenow - m->NextSRVUpdate >= 0)
3975         LogMsg("Task Scheduling Error: m->NextSRVUpdate %d",         m->timenow - m->NextSRVUpdate);
3976 #endif
3977 
3978     if (m->timenow - m->NextCacheCheck        >= 0)
3979         LogMsg("Task Scheduling Error: m->NextCacheCheck %d",        m->timenow - m->NextCacheCheck);
3980     if (m->timenow - m->NextScheduledSPS      >= 0)
3981         LogMsg("Task Scheduling Error: m->NextScheduledSPS %d",      m->timenow - m->NextScheduledSPS);
3982     if (m->timenow - m->NextScheduledKA       >= 0)
3983         LogMsg("Task Scheduling Error: m->NextScheduledKA %d",      m->timenow - m->NextScheduledKA);
3984     if (!m->DelaySleep && m->SleepLimit && m->timenow - m->NextScheduledSPRetry >= 0)
3985         LogMsg("Task Scheduling Error: m->NextScheduledSPRetry %d",  m->timenow - m->NextScheduledSPRetry);
3986     if (m->DelaySleep && m->timenow - m->DelaySleep >= 0)
3987         LogMsg("Task Scheduling Error: m->DelaySleep %d",            m->timenow - m->DelaySleep);
3988 
3989     if (m->SuppressSending && m->timenow - m->SuppressSending >= 0)
3990         LogMsg("Task Scheduling Error: m->SuppressSending %d",       m->timenow - m->SuppressSending);
3991     if (m->timenow - m->NextScheduledQuery    >= 0)
3992         LogMsg("Task Scheduling Error: m->NextScheduledQuery %d",    m->timenow - m->NextScheduledQuery);
3993     if (m->timenow - m->NextScheduledProbe    >= 0)
3994         LogMsg("Task Scheduling Error: m->NextScheduledProbe %d",    m->timenow - m->NextScheduledProbe);
3995     if (m->timenow - m->NextScheduledResponse >= 0)
3996         LogMsg("Task Scheduling Error: m->NextScheduledResponse %d", m->timenow - m->NextScheduledResponse);
3997 
3998     mDNS_Unlock(m);
3999 }
4000 
4001 mDNSexport void mDNS_Unlock_(mDNS *const m, const char * const functionname)
4002 {
4003     // Decrement mDNS_busy
4004     m->mDNS_busy--;
4005 
4006     // Check for locking failures
4007     if (m->mDNS_busy != m->mDNS_reentrancy)
4008     {
4009         LogMsg("%s: mDNS_Unlock: Locking failure! mDNS_busy (%ld) != mDNS_reentrancy (%ld)", functionname, m->mDNS_busy, m->mDNS_reentrancy);
4010 #if ForceAlerts
4011         *(long*)0 = 0;
4012 #endif
4013     }
4014 
4015     // If this is a final exit from the mDNSCore code, set m->NextScheduledEvent and clear m->timenow
4016     if (m->mDNS_busy == 0)
4017     {
4018         m->NextScheduledEvent = GetNextScheduledEvent(m);
4019         if (m->timenow == 0) LogMsg("%s: mDNS_Unlock: ERROR! m->timenow aready zero", functionname);
4020         m->timenow = 0;
4021     }
4022 
4023     // MUST release the platform lock LAST!
4024     mDNSPlatformUnlock(m);
4025 }
4026 
4027 // ***************************************************************************
4028 #if COMPILER_LIKES_PRAGMA_MARK
4029 #pragma mark -
4030 #pragma mark - Specialized mDNS version of vsnprintf
4031 #endif
4032 
4033 static const struct mDNSprintf_format
4034 {
4035     unsigned leftJustify : 1;
4036     unsigned forceSign : 1;
4037     unsigned zeroPad : 1;
4038     unsigned havePrecision : 1;
4039     unsigned hSize : 1;
4040     unsigned lSize : 1;
4041     char altForm;
4042     char sign;              // +, - or space
4043     unsigned int fieldWidth;
4044     unsigned int precision;
4045 } mDNSprintf_format_default = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
4046 
4047 mDNSexport mDNSu32 mDNS_vsnprintf(char *sbuffer, mDNSu32 buflen, const char *fmt, va_list arg)
4048 {
4049     mDNSu32 nwritten = 0;
4050     int c;
4051     if (buflen == 0) return(0);
4052     buflen--;       // Pre-reserve one space in the buffer for the terminating null
4053     if (buflen == 0) goto exit;
4054 
4055     for (c = *fmt; c != 0; c = *++fmt)
4056     {
4057         if (c != '%')
4058         {
4059             *sbuffer++ = (char)c;
4060             if (++nwritten >= buflen) goto exit;
4061         }
4062         else
4063         {
4064             unsigned int i=0, j;
4065             // The mDNS Vsprintf Argument Conversion Buffer is used as a temporary holding area for
4066             // generating decimal numbers, hexdecimal numbers, IP addresses, domain name strings, etc.
4067             // The size needs to be enough for a 256-byte domain name plus some error text.
4068             #define mDNS_VACB_Size 300
4069             char mDNS_VACB[mDNS_VACB_Size];
4070             #define mDNS_VACB_Lim (&mDNS_VACB[mDNS_VACB_Size])
4071             #define mDNS_VACB_Remain(s) ((mDNSu32)(mDNS_VACB_Lim - s))
4072             char *s = mDNS_VACB_Lim, *digits;
4073             struct mDNSprintf_format F = mDNSprintf_format_default;
4074 
4075             while (1)   //  decode flags
4076             {
4077                 c = *++fmt;
4078                 if      (c == '-') F.leftJustify = 1;
4079                 else if (c == '+') F.forceSign = 1;
4080                 else if (c == ' ') F.sign = ' ';
4081                 else if (c == '#') F.altForm++;
4082                 else if (c == '0') F.zeroPad = 1;
4083                 else break;
4084             }
4085 
4086             if (c == '*')   //  decode field width
4087             {
4088                 int f = va_arg(arg, int);
4089                 if (f < 0) { f = -f; F.leftJustify = 1; }
4090                 F.fieldWidth = (unsigned int)f;
4091                 c = *++fmt;
4092             }
4093             else
4094             {
4095                 for (; c >= '0' && c <= '9'; c = *++fmt)
4096                     F.fieldWidth = (10 * F.fieldWidth) + (c - '0');
4097             }
4098 
4099             if (c == '.')   //  decode precision
4100             {
4101                 if ((c = *++fmt) == '*')
4102                 { F.precision = va_arg(arg, unsigned int); c = *++fmt; }
4103                 else for (; c >= '0' && c <= '9'; c = *++fmt)
4104                         F.precision = (10 * F.precision) + (c - '0');
4105                 F.havePrecision = 1;
4106             }
4107 
4108             if (F.leftJustify) F.zeroPad = 0;
4109 
4110 conv:
4111             switch (c)  //  perform appropriate conversion
4112             {
4113                 unsigned long n;
4114             case 'h':  F.hSize = 1; c = *++fmt; goto conv;
4115             case 'l':       // fall through
4116             case 'L':  F.lSize = 1; c = *++fmt; goto conv;
4117             case 'd':
4118             case 'i':  if (F.lSize) n = (unsigned long)va_arg(arg, long);
4119                 else n = (unsigned long)va_arg(arg, int);
4120                 if (F.hSize) n = (short) n;
4121                 if ((long) n < 0) { n = (unsigned long)-(long)n; F.sign = '-'; }
4122                 else if (F.forceSign) F.sign = '+';
4123                 goto decimal;
4124             case 'u':  if (F.lSize) n = va_arg(arg, unsigned long);
4125                 else n = va_arg(arg, unsigned int);
4126                 if (F.hSize) n = (unsigned short) n;
4127                 F.sign = 0;
4128                 goto decimal;
4129 decimal:    if (!F.havePrecision)
4130                 {
4131                     if (F.zeroPad)
4132                     {
4133                         F.precision = F.fieldWidth;
4134                         if (F.sign) --F.precision;
4135                     }
4136                     if (F.precision < 1) F.precision = 1;
4137                 }
4138                 if (F.precision > mDNS_VACB_Size - 1)
4139                     F.precision = mDNS_VACB_Size - 1;
4140                 for (i = 0; n; n /= 10, i++) *--s = (char)(n % 10 + '0');
4141                 for (; i < F.precision; i++) *--s = '0';
4142                 if (F.sign) { *--s = F.sign; i++; }
4143                 break;
4144 
4145             case 'o':  if (F.lSize) n = va_arg(arg, unsigned long);
4146                 else n = va_arg(arg, unsigned int);
4147                 if (F.hSize) n = (unsigned short) n;
4148                 if (!F.havePrecision)
4149                 {
4150                     if (F.zeroPad) F.precision = F.fieldWidth;
4151                     if (F.precision < 1) F.precision = 1;
4152                 }
4153                 if (F.precision > mDNS_VACB_Size - 1)
4154                     F.precision = mDNS_VACB_Size - 1;
4155                 for (i = 0; n; n /= 8, i++) *--s = (char)(n % 8 + '0');
4156                 if (F.altForm && i && *s != '0') { *--s = '0'; i++; }
4157                 for (; i < F.precision; i++) *--s = '0';
4158                 break;
4159 
4160             case 'a':  {
4161                 unsigned char *a = va_arg(arg, unsigned char *);
4162                 if (!a) { static char emsg[] = "<<NULL>>"; s = emsg; i = sizeof(emsg)-1; }
4163                 else
4164                 {
4165                     s = mDNS_VACB;              // Adjust s to point to the start of the buffer, not the end
4166                     if (F.altForm)
4167                     {
4168                         mDNSAddr *ip = (mDNSAddr*)a;
4169                         switch (ip->type)
4170                         {
4171                         case mDNSAddrType_IPv4: F.precision =  4; a = (unsigned char *)&ip->ip.v4; break;
4172                         case mDNSAddrType_IPv6: F.precision = 16; a = (unsigned char *)&ip->ip.v6; break;
4173                         default:                F.precision =  0; break;
4174                         }
4175                     }
4176                     if (F.altForm && !F.precision)
4177                         i = mDNS_snprintf(mDNS_VACB, sizeof(mDNS_VACB), "«ZERO ADDRESS»");
4178                     else switch (F.precision)
4179                         {
4180                         case  4: i = mDNS_snprintf(mDNS_VACB, sizeof(mDNS_VACB), "%d.%d.%d.%d",
4181                                                    a[0], a[1], a[2], a[3]); break;
4182                         case  6: i = mDNS_snprintf(mDNS_VACB, sizeof(mDNS_VACB), "%02X:%02X:%02X:%02X:%02X:%02X",
4183                                                    a[0], a[1], a[2], a[3], a[4], a[5]); break;
4184                         case 16: i = mDNS_snprintf(mDNS_VACB, sizeof(mDNS_VACB),
4185                                                    "%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X",
4186                                                    a[0x0], a[0x1], a[0x2], a[0x3], a[0x4], a[0x5], a[0x6], a[0x7],
4187                                                    a[0x8], a[0x9], a[0xA], a[0xB], a[0xC], a[0xD], a[0xE], a[0xF]); break;
4188                         default: i = mDNS_snprintf(mDNS_VACB, sizeof(mDNS_VACB), "%s", "<< ERROR: Must specify"
4189                                                    " address size (i.e. %.4a=IPv4, %.6a=Ethernet, %.16a=IPv6) >>"); break;
4190                         }
4191                 }
4192             }
4193             break;
4194 
4195             case 'p':  F.havePrecision = F.lSize = 1;
4196                 F.precision = sizeof(void*) * 2;                // 8 characters on 32-bit; 16 characters on 64-bit
4197             case 'X':  digits = "0123456789ABCDEF";
4198                 goto hexadecimal;
4199             case 'x':  digits = "0123456789abcdef";
4200 hexadecimal: if (F.lSize) n = va_arg(arg, unsigned long);
4201                 else n = va_arg(arg, unsigned int);
4202                 if (F.hSize) n = (unsigned short) n;
4203                 if (!F.havePrecision)
4204                 {
4205                     if (F.zeroPad)
4206                     {
4207                         F.precision = F.fieldWidth;
4208                         if (F.altForm) F.precision -= 2;
4209                     }
4210                     if (F.precision < 1) F.precision = 1;
4211                 }
4212                 if (F.precision > mDNS_VACB_Size - 1)
4213                     F.precision = mDNS_VACB_Size - 1;
4214                 for (i = 0; n; n /= 16, i++) *--s = digits[n % 16];
4215                 for (; i < F.precision; i++) *--s = '0';
4216                 if (F.altForm) { *--s = (char)c; *--s = '0'; i += 2; }
4217                 break;
4218 
4219             case 'c':  *--s = (char)va_arg(arg, int); i = 1; break;
4220 
4221             case 's':  s = va_arg(arg, char *);
4222                 if (!s) { static char emsg[] = "<<NULL>>"; s = emsg; i = sizeof(emsg)-1; }
4223                 else switch (F.altForm)
4224                     {
4225                     case 0: i=0;
4226                         if (!F.havePrecision)                               // C string
4227                             while (s[i]) i++;
4228                         else
4229                         {
4230                             while ((i < F.precision) && s[i]) i++;
4231                             // Make sure we don't truncate in the middle of a UTF-8 character
4232                             // If last character we got was any kind of UTF-8 multi-byte character,
4233                             // then see if we have to back up.
4234                             // This is not as easy as the similar checks below, because
4235                             // here we can't assume it's safe to examine the *next* byte, so we
4236                             // have to confine ourselves to working only backwards in the string.
4237                             j = i;                      // Record where we got to
4238                             // Now, back up until we find first non-continuation-char
4239                             while (i>0 && (s[i-1] & 0xC0) == 0x80) i--;
4240                             // Now s[i-1] is the first non-continuation-char
4241                             // and (j-i) is the number of continuation-chars we found
4242                             if (i>0 && (s[i-1] & 0xC0) == 0xC0)                 // If we found a start-char
4243                             {
4244                                 i--;                        // Tentatively eliminate this start-char as well
4245                                 // Now (j-i) is the number of characters we're considering eliminating.
4246                                 // To be legal UTF-8, the start-char must contain (j-i) one-bits,
4247                                 // followed by a zero bit. If we shift it right by (7-(j-i)) bits
4248                                 // (with sign extension) then the result has to be 0xFE.
4249                                 // If this is right, then we reinstate the tentatively eliminated bytes.
4250                                 if (((j-i) < 7) && (((s[i] >> (7-(j-i))) & 0xFF) == 0xFE)) i = j;
4251                             }
4252                         }
4253                         break;
4254                     case 1: i = (unsigned char) *s++; break;                // Pascal string
4255                     case 2: {                                               // DNS label-sequence name
4256                         unsigned char *a = (unsigned char *)s;
4257                         s = mDNS_VACB;                  // Adjust s to point to the start of the buffer, not the end
4258                         if (*a == 0) *s++ = '.';                    // Special case for root DNS name
4259                         while (*a)
4260                         {
4261                             char buf[63*4+1];
4262                             if (*a > 63)
4263                             { s += mDNS_snprintf(s, mDNS_VACB_Remain(s), "<<INVALID LABEL LENGTH %u>>", *a); break; }
4264                             if (s + *a >= &mDNS_VACB[254])
4265                             { s += mDNS_snprintf(s, mDNS_VACB_Remain(s), "<<NAME TOO LONG>>"); break; }
4266                             // Need to use ConvertDomainLabelToCString to do proper escaping here,
4267                             // so it's clear what's a literal dot and what's a label separator
4268                             ConvertDomainLabelToCString((domainlabel*)a, buf);
4269                             s += mDNS_snprintf(s, mDNS_VACB_Remain(s), "%s.", buf);
4270                             a += 1 + *a;
4271                         }
4272                         i = (mDNSu32)(s - mDNS_VACB);
4273                         s = mDNS_VACB;                  // Reset s back to the start of the buffer
4274                         break;
4275                     }
4276                     }
4277                 // Make sure we don't truncate in the middle of a UTF-8 character (see similar comment below)
4278                 if (F.havePrecision && i > F.precision)
4279                 { i = F.precision; while (i>0 && (s[i] & 0xC0) == 0x80) i--;}
4280                 break;
4281 
4282             case 'n':  s = va_arg(arg, char *);
4283                 if      (F.hSize) *(short *) s = (short)nwritten;
4284                 else if (F.lSize) *(long  *) s = (long)nwritten;
4285                 else *(int   *) s = (int)nwritten;
4286                 continue;
4287 
4288             default:    s = mDNS_VACB;
4289                 i = mDNS_snprintf(mDNS_VACB, sizeof(mDNS_VACB), "<<UNKNOWN FORMAT CONVERSION CODE %%%c>>", c);
4290 
4291             case '%':  *sbuffer++ = (char)c;
4292                 if (++nwritten >= buflen) goto exit;
4293                 break;
4294             }
4295 
4296             if (i < F.fieldWidth && !F.leftJustify)         // Pad on the left
4297                 do  {
4298                     *sbuffer++ = ' ';
4299                     if (++nwritten >= buflen) goto exit;
4300                 } while (i < --F.fieldWidth);
4301 
4302             // Make sure we don't truncate in the middle of a UTF-8 character.
4303             // Note: s[i] is the first eliminated character; i.e. the next character *after* the last character of the
4304             // allowed output. If s[i] is a UTF-8 continuation character, then we've cut a unicode character in half,
4305             // so back up 'i' until s[i] is no longer a UTF-8 continuation character. (if the input was proprly
4306             // formed, s[i] will now be the UTF-8 start character of the multi-byte character we just eliminated).
4307             if (i > buflen - nwritten)
4308             { i = buflen - nwritten; while (i>0 && (s[i] & 0xC0) == 0x80) i--;}
4309             for (j=0; j<i; j++) *sbuffer++ = *s++;          // Write the converted result
4310             nwritten += i;
4311             if (nwritten >= buflen) goto exit;
4312 
4313             for (; i < F.fieldWidth; i++)                   // Pad on the right
4314             {
4315                 *sbuffer++ = ' ';
4316                 if (++nwritten >= buflen) goto exit;
4317             }
4318         }
4319     }
4320 exit:
4321     *sbuffer++ = 0;
4322     return(nwritten);
4323 }
4324 
4325 mDNSexport mDNSu32 mDNS_snprintf(char *sbuffer, mDNSu32 buflen, const char *fmt, ...)
4326 {
4327     mDNSu32 length;
4328 
4329     va_list ptr;
4330     va_start(ptr,fmt);
4331     length = mDNS_vsnprintf(sbuffer, buflen, fmt, ptr);
4332     va_end(ptr);
4333 
4334     return(length);
4335 }
4336