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