1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2002-2003 Apple Computer, Inc. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 // Set mDNS_InstantiateInlines to tell mDNSEmbeddedAPI.h to instantiate inline functions, if necessary
19 #define mDNS_InstantiateInlines 1
20 #include "DNSCommon.h"
22 // Disable certain benign warnings with Microsoft compilers
23 #if (defined(_MSC_VER))
24 // Disable "conditional expression is constant" warning for debug macros.
25 // Otherwise, this generates warnings for the perfectly natural construct "while(1)"
26 // If someone knows a variant way of writing "while(1)" that doesn't generate warning messages, please let us know
27 #pragma warning(disable:4127)
28 // Disable "array is too small to include a terminating null character" warning
29 // -- domain labels have an initial length byte, not a terminating null character
30 #pragma warning(disable:4295)
33 // ***************************************************************************
34 #if COMPILER_LIKES_PRAGMA_MARK
35 #pragma mark - Program Constants
38 mDNSexport
const mDNSInterfaceID mDNSInterface_Any
= 0;
39 mDNSexport
const mDNSInterfaceID mDNSInterfaceMark
= (mDNSInterfaceID
)-1;
40 mDNSexport
const mDNSInterfaceID mDNSInterface_LocalOnly
= (mDNSInterfaceID
)-2;
41 mDNSexport
const mDNSInterfaceID mDNSInterface_Unicast
= (mDNSInterfaceID
)-3;
42 mDNSexport
const mDNSInterfaceID mDNSInterface_P2P
= (mDNSInterfaceID
)-4;
44 // Note: Microsoft's proposed "Link Local Multicast Name Resolution Protocol" (LLMNR) is essentially a limited version of
45 // Multicast DNS, using the same packet formats, naming syntax, and record types as Multicast DNS, but on a different UDP
46 // port and multicast address, which means it won't interoperate with the existing installed base of Multicast DNS responders.
47 // LLMNR uses IPv4 multicast address 224.0.0.252, IPv6 multicast address FF02::0001:0003, and UDP port 5355.
48 // Uncomment the appropriate lines below to build a special Multicast DNS responder for testing interoperability
49 // with Microsoft's LLMNR client code.
51 #define DiscardPortAsNumber 9
52 #define SSHPortAsNumber 22
53 #define UnicastDNSPortAsNumber 53
54 #define SSDPPortAsNumber 1900
55 #define IPSECPortAsNumber 4500
56 #define NSIPCPortAsNumber 5030 // Port used for dnsextd to talk to local nameserver bound to loopback
57 #define NATPMPAnnouncementPortAsNumber 5350
58 #define NATPMPPortAsNumber 5351
59 #define DNSEXTPortAsNumber 5352 // Port used for end-to-end DNS operations like LLQ, Updates with Leases, etc.
60 #define MulticastDNSPortAsNumber 5353
61 #define LoopbackIPCPortAsNumber 5354
62 //#define MulticastDNSPortAsNumber 5355 // LLMNR
63 #define PrivateDNSPortAsNumber 5533
65 mDNSexport
const mDNSIPPort DiscardPort
= { { DiscardPortAsNumber
>> 8, DiscardPortAsNumber
& 0xFF } };
66 mDNSexport
const mDNSIPPort SSHPort
= { { SSHPortAsNumber
>> 8, SSHPortAsNumber
& 0xFF } };
67 mDNSexport
const mDNSIPPort UnicastDNSPort
= { { UnicastDNSPortAsNumber
>> 8, UnicastDNSPortAsNumber
& 0xFF } };
68 mDNSexport
const mDNSIPPort SSDPPort
= { { SSDPPortAsNumber
>> 8, SSDPPortAsNumber
& 0xFF } };
69 mDNSexport
const mDNSIPPort IPSECPort
= { { IPSECPortAsNumber
>> 8, IPSECPortAsNumber
& 0xFF } };
70 mDNSexport
const mDNSIPPort NSIPCPort
= { { NSIPCPortAsNumber
>> 8, NSIPCPortAsNumber
& 0xFF } };
71 mDNSexport
const mDNSIPPort NATPMPAnnouncementPort
= { { NATPMPAnnouncementPortAsNumber
>> 8, NATPMPAnnouncementPortAsNumber
& 0xFF } };
72 mDNSexport
const mDNSIPPort NATPMPPort
= { { NATPMPPortAsNumber
>> 8, NATPMPPortAsNumber
& 0xFF } };
73 mDNSexport
const mDNSIPPort DNSEXTPort
= { { DNSEXTPortAsNumber
>> 8, DNSEXTPortAsNumber
& 0xFF } };
74 mDNSexport
const mDNSIPPort MulticastDNSPort
= { { MulticastDNSPortAsNumber
>> 8, MulticastDNSPortAsNumber
& 0xFF } };
75 mDNSexport
const mDNSIPPort LoopbackIPCPort
= { { LoopbackIPCPortAsNumber
>> 8, LoopbackIPCPortAsNumber
& 0xFF } };
76 mDNSexport
const mDNSIPPort PrivateDNSPort
= { { PrivateDNSPortAsNumber
>> 8, PrivateDNSPortAsNumber
& 0xFF } };
78 mDNSexport
const OwnerOptData zeroOwner
= { 0, 0, { { 0 } }, { { 0 } }, { { 0 } } };
80 mDNSexport
const mDNSIPPort zeroIPPort
= { { 0 } };
81 mDNSexport
const mDNSv4Addr zerov4Addr
= { { 0 } };
82 mDNSexport
const mDNSv6Addr zerov6Addr
= { { 0 } };
83 mDNSexport
const mDNSEthAddr zeroEthAddr
= { { 0 } };
84 mDNSexport
const mDNSv4Addr onesIPv4Addr
= { { 255, 255, 255, 255 } };
85 mDNSexport
const mDNSv6Addr onesIPv6Addr
= { { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 } };
86 mDNSexport
const mDNSEthAddr onesEthAddr
= { { 255, 255, 255, 255, 255, 255 } };
87 mDNSexport
const mDNSAddr zeroAddr
= { mDNSAddrType_None
, {{{ 0 }}} };
89 mDNSexport
const mDNSv4Addr AllDNSAdminGroup
= { { 239, 255, 255, 251 } };
90 mDNSexport
const mDNSv4Addr AllHosts_v4
= { { 224, 0, 0, 1 } }; // For NAT-PMP Annoucements
91 mDNSexport
const mDNSv6Addr AllHosts_v6
= { { 0xFF,0x02,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x01 } };
92 mDNSexport
const mDNSv6Addr NDP_prefix
= { { 0xFF,0x02,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x01, 0xFF,0x00,0x00,0xFB } }; // FF02:0:0:0:0:1:FF00::/104
93 mDNSexport
const mDNSEthAddr AllHosts_v6_Eth
= { { 0x33, 0x33, 0x00, 0x00, 0x00, 0x01 } };
94 mDNSexport
const mDNSAddr AllDNSLinkGroup_v4
= { mDNSAddrType_IPv4
, { { { 224, 0, 0, 251 } } } };
95 //mDNSexport const mDNSAddr AllDNSLinkGroup_v4 = { mDNSAddrType_IPv4, { { { 224, 0, 0, 252 } } } }; // LLMNR
96 mDNSexport
const mDNSAddr AllDNSLinkGroup_v6
= { mDNSAddrType_IPv6
, { { { 0xFF,0x02,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0xFB } } } };
97 //mDNSexport const mDNSAddr AllDNSLinkGroup_v6 = { mDNSAddrType_IPv6, { { { 0xFF,0x02,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x01,0x00,0x03 } } } }; // LLMNR
99 mDNSexport
const mDNSOpaque16 zeroID
= { { 0, 0 } };
100 mDNSexport
const mDNSOpaque16 onesID
= { { 255, 255 } };
101 mDNSexport
const mDNSOpaque16 QueryFlags
= { { kDNSFlag0_QR_Query
| kDNSFlag0_OP_StdQuery
, 0 } };
102 mDNSexport
const mDNSOpaque16 uQueryFlags
= { { kDNSFlag0_QR_Query
| kDNSFlag0_OP_StdQuery
| kDNSFlag0_RD
, 0 } };
103 mDNSexport
const mDNSOpaque16 ResponseFlags
= { { kDNSFlag0_QR_Response
| kDNSFlag0_OP_StdQuery
| kDNSFlag0_AA
, 0 } };
104 mDNSexport
const mDNSOpaque16 UpdateReqFlags
= { { kDNSFlag0_QR_Query
| kDNSFlag0_OP_Update
, 0 } };
105 mDNSexport
const mDNSOpaque16 UpdateRespFlags
= { { kDNSFlag0_QR_Response
| kDNSFlag0_OP_Update
, 0 } };
107 mDNSexport
const mDNSOpaque64 zeroOpaque64
= { { 0 } };
109 // ***************************************************************************
110 #if COMPILER_LIKES_PRAGMA_MARK
112 #pragma mark - General Utility Functions
115 // return true for RFC1918 private addresses
116 mDNSexport mDNSBool
mDNSv4AddrIsRFC1918(mDNSv4Addr
*addr
)
118 return ((addr
->b
[0] == 10) || // 10/8 prefix
119 (addr
->b
[0] == 172 && (addr
->b
[1] & 0xF0) == 16) || // 172.16/12
120 (addr
->b
[0] == 192 && addr
->b
[1] == 168)); // 192.168/16
123 mDNSexport NetworkInterfaceInfo
*GetFirstActiveInterface(NetworkInterfaceInfo
*intf
)
125 while (intf
&& !intf
->InterfaceActive
) intf
= intf
->next
;
129 mDNSexport mDNSInterfaceID
GetNextActiveInterfaceID(const NetworkInterfaceInfo
*intf
)
131 const NetworkInterfaceInfo
*next
= GetFirstActiveInterface(intf
->next
);
132 if (next
) return(next
->InterfaceID
); else return(mDNSNULL
);
135 mDNSexport mDNSu32
NumCacheRecordsForInterfaceID(const mDNS
*const m
, mDNSInterfaceID id
)
137 mDNSu32 slot
, used
= 0;
139 const CacheRecord
*rr
;
140 FORALL_CACHERECORDS(slot
, cg
, rr
)
141 if (rr
->resrec
.InterfaceID
== id
) used
++;
145 mDNSexport
char *DNSTypeName(mDNSu16 rrtype
)
149 case kDNSType_A
: return("Addr");
150 case kDNSType_NS
: return("NS");
151 case kDNSType_CNAME
:return("CNAME");
152 case kDNSType_SOA
: return("SOA");
153 case kDNSType_NULL
: return("NULL");
154 case kDNSType_PTR
: return("PTR");
155 case kDNSType_HINFO
:return("HINFO");
156 case kDNSType_TXT
: return("TXT");
157 case kDNSType_AAAA
: return("AAAA");
158 case kDNSType_SRV
: return("SRV");
159 case kDNSType_OPT
: return("OPT");
160 case kDNSType_NSEC
: return("NSEC");
161 case kDNSType_TSIG
: return("TSIG");
162 case kDNSQType_ANY
: return("ANY");
164 static char buffer
[16];
165 mDNS_snprintf(buffer
, sizeof(buffer
), "(%d)", rrtype
);
171 // Note slight bug: this code uses the rdlength from the ResourceRecord object, to display
172 // the rdata from the RDataBody object. Sometimes this could be the wrong length -- but as
173 // long as this routine is only used for debugging messages, it probably isn't a big problem.
174 mDNSexport
char *GetRRDisplayString_rdb(const ResourceRecord
*const rr
, const RDataBody
*const rd1
, char *const buffer
)
176 const RDataBody2
*const rd
= (RDataBody2
*)rd1
;
177 #define RemSpc (MaxMsg-1-length)
179 mDNSu32 length
= mDNS_snprintf(buffer
, MaxMsg
-1, "%4d %##s %s ", rr
->rdlength
, rr
->name
->c
, DNSTypeName(rr
->rrtype
));
180 if (rr
->RecordType
== kDNSRecordTypePacketNegative
) return(buffer
);
181 if (!rr
->rdlength
) { mDNS_snprintf(buffer
+length
, RemSpc
, "<< ZERO RDATA LENGTH >>"); return(buffer
); }
185 case kDNSType_A
: mDNS_snprintf(buffer
+length
, RemSpc
, "%.4a", &rd
->ipv4
); break;
187 case kDNSType_NS
: // Same as PTR
188 case kDNSType_CNAME
:// Same as PTR
189 case kDNSType_PTR
: mDNS_snprintf(buffer
+length
, RemSpc
, "%##s", rd
->name
.c
); break;
191 case kDNSType_SOA
: mDNS_snprintf(buffer
+length
, RemSpc
, "%##s %##s %d %d %d %d %d",
192 rd
->soa
.mname
.c
, rd
->soa
.rname
.c
,
193 rd
->soa
.serial
, rd
->soa
.refresh
, rd
->soa
.retry
, rd
->soa
.expire
, rd
->soa
.min
);
196 case kDNSType_HINFO
:// Display this the same as TXT (show all constituent strings)
198 const mDNSu8
*t
= rd
->txt
.c
;
199 while (t
< rd
->txt
.c
+ rr
->rdlength
)
201 length
+= mDNS_snprintf(buffer
+length
, RemSpc
, "%s%#s", t
> rd
->txt
.c
? "¦" : "", t
);
206 case kDNSType_AAAA
: mDNS_snprintf(buffer
+length
, RemSpc
, "%.16a", &rd
->ipv6
); break;
207 case kDNSType_SRV
: mDNS_snprintf(buffer
+length
, RemSpc
, "%u %u %u %##s",
208 rd
->srv
.priority
, rd
->srv
.weight
, mDNSVal16(rd
->srv
.port
), rd
->srv
.target
.c
); break;
212 const rdataOPT
*const end
= (const rdataOPT
*)&rd
->data
[rr
->rdlength
];
213 length
+= mDNS_snprintf(buffer
+length
, RemSpc
, "Max %d", rr
->rrclass
);
214 for (opt
= &rd
->opt
[0]; opt
< end
; opt
++)
219 length
+= mDNS_snprintf(buffer
+length
, RemSpc
, " Vers %d", opt
->u
.llq
.vers
);
220 length
+= mDNS_snprintf(buffer
+length
, RemSpc
, " Op %d", opt
->u
.llq
.llqOp
);
221 length
+= mDNS_snprintf(buffer
+length
, RemSpc
, " Err/Port %d", opt
->u
.llq
.err
);
222 length
+= mDNS_snprintf(buffer
+length
, RemSpc
, " ID %08X%08X", opt
->u
.llq
.id
.l
[0], opt
->u
.llq
.id
.l
[1]);
223 length
+= mDNS_snprintf(buffer
+length
, RemSpc
, " Lease %d", opt
->u
.llq
.llqlease
);
226 length
+= mDNS_snprintf(buffer
+length
, RemSpc
, " Lease %d", opt
->u
.updatelease
);
229 length
+= mDNS_snprintf(buffer
+length
, RemSpc
, " Vers %d", opt
->u
.owner
.vers
);
230 length
+= mDNS_snprintf(buffer
+length
, RemSpc
, " Seq %3d", (mDNSu8
)opt
->u
.owner
.seq
); // Display as unsigned
231 length
+= mDNS_snprintf(buffer
+length
, RemSpc
, " MAC %.6a", opt
->u
.owner
.HMAC
.b
);
232 if (opt
->optlen
>= DNSOpt_OwnerData_ID_Wake_Space
-4)
234 length
+= mDNS_snprintf(buffer
+length
, RemSpc
, " I-MAC %.6a", opt
->u
.owner
.IMAC
.b
);
235 if (opt
->optlen
> DNSOpt_OwnerData_ID_Wake_Space
-4)
236 length
+= mDNS_snprintf(buffer
+length
, RemSpc
, " Password %.6a", opt
->u
.owner
.password
.b
);
240 length
+= mDNS_snprintf(buffer
+length
, RemSpc
, " Unknown %d", opt
->opt
);
247 case kDNSType_NSEC
: {
249 for (i
=0; i
<255; i
++)
250 if (rd
->nsec
.bitmap
[i
>>3] & (128 >> (i
&7)))
251 length
+= mDNS_snprintf(buffer
+length
, RemSpc
, "%s ", DNSTypeName(i
));
255 default: mDNS_snprintf(buffer
+length
, RemSpc
, "RDLen %d: %s", rr
->rdlength
, rd
->data
);
256 // Really should scan buffer to check if text is valid UTF-8 and only replace with dots if not
257 for (ptr
= buffer
; *ptr
; ptr
++) if (*ptr
< ' ') *ptr
= '.';
263 // See comments in mDNSEmbeddedAPI.h
264 #if _PLATFORM_HAS_STRONG_PRNG_
265 #define mDNSRandomNumber mDNSPlatformRandomNumber
267 mDNSlocal mDNSu32
mDNSRandomFromSeed(mDNSu32 seed
)
269 return seed
* 21 + 1;
272 mDNSlocal mDNSu32
mDNSMixRandomSeed(mDNSu32 seed
, mDNSu8 iteration
)
274 return iteration
? mDNSMixRandomSeed(mDNSRandomFromSeed(seed
), --iteration
) : seed
;
277 mDNSlocal mDNSu32
mDNSRandomNumber()
279 static mDNSBool seeded
= mDNSfalse
;
280 static mDNSu32 seed
= 0;
283 seed
= mDNSMixRandomSeed(mDNSPlatformRandomSeed(), 100);
286 return (seed
= mDNSRandomFromSeed(seed
));
288 #endif // ! _PLATFORM_HAS_STRONG_PRNG_
290 mDNSexport mDNSu32
mDNSRandom(mDNSu32 max
) // Returns pseudo-random result from zero to max inclusive
295 while (mask
< max
) mask
= (mask
<< 1) | 1;
297 do ret
= mDNSRandomNumber() & mask
;
303 mDNSexport mDNSBool
mDNSSameAddress(const mDNSAddr
*ip1
, const mDNSAddr
*ip2
)
305 if (ip1
->type
== ip2
->type
)
309 case mDNSAddrType_None
: return(mDNStrue
); // Empty addresses have no data and are therefore always equal
310 case mDNSAddrType_IPv4
: return(mDNSBool
)(mDNSSameIPv4Address(ip1
->ip
.v4
, ip2
->ip
.v4
));
311 case mDNSAddrType_IPv6
: return(mDNSBool
)(mDNSSameIPv6Address(ip1
->ip
.v6
, ip2
->ip
.v6
));
317 mDNSexport mDNSBool
mDNSAddrIsDNSMulticast(const mDNSAddr
*ip
)
321 case mDNSAddrType_IPv4
: return(mDNSBool
)(mDNSSameIPv4Address(ip
->ip
.v4
, AllDNSLinkGroup_v4
.ip
.v4
));
322 case mDNSAddrType_IPv6
: return(mDNSBool
)(mDNSSameIPv6Address(ip
->ip
.v6
, AllDNSLinkGroup_v6
.ip
.v6
));
323 default: return(mDNSfalse
);
327 // ***************************************************************************
328 #if COMPILER_LIKES_PRAGMA_MARK
330 #pragma mark - Domain Name Utility Functions
333 mDNSexport mDNSBool
SameDomainLabel(const mDNSu8
*a
, const mDNSu8
*b
)
336 const int len
= *a
++;
338 if (len
> MAX_DOMAIN_LABEL
)
339 { debugf("Malformed label (too long)"); return(mDNSfalse
); }
341 if (len
!= *b
++) return(mDNSfalse
);
342 for (i
=0; i
<len
; i
++)
346 if (mDNSIsUpperCase(ac
)) ac
+= 'a' - 'A';
347 if (mDNSIsUpperCase(bc
)) bc
+= 'a' - 'A';
348 if (ac
!= bc
) return(mDNSfalse
);
353 mDNSexport mDNSBool
SameDomainName(const domainname
*const d1
, const domainname
*const d2
)
355 const mDNSu8
* a
= d1
->c
;
356 const mDNSu8
* b
= d2
->c
;
357 const mDNSu8
*const max
= d1
->c
+ MAX_DOMAIN_NAME
; // Maximum that's valid
361 if (a
+ 1 + *a
>= max
)
362 { debugf("Malformed domain name (more than 256 characters)"); return(mDNSfalse
); }
363 if (!SameDomainLabel(a
, b
)) return(mDNSfalse
);
371 mDNSexport mDNSBool
SameDomainNameCS(const domainname
*const d1
, const domainname
*const d2
)
373 mDNSu16 l1
= DomainNameLength(d1
);
374 mDNSu16 l2
= DomainNameLength(d2
);
375 return(l1
<= MAX_DOMAIN_NAME
&& l1
== l2
&& mDNSPlatformMemSame(d1
, d2
, l1
));
378 mDNSexport mDNSBool
IsLocalDomain(const domainname
*d
)
380 // Domains that are defined to be resolved via link-local multicast are:
381 // local., 254.169.in-addr.arpa., and {8,9,A,B}.E.F.ip6.arpa.
382 static const domainname
*nL
= (const domainname
*)"\x5" "local";
383 static const domainname
*nR
= (const domainname
*)"\x3" "254" "\x3" "169" "\x7" "in-addr" "\x4" "arpa";
384 static const domainname
*n8
= (const domainname
*)"\x1" "8" "\x1" "e" "\x1" "f" "\x3" "ip6" "\x4" "arpa";
385 static const domainname
*n9
= (const domainname
*)"\x1" "9" "\x1" "e" "\x1" "f" "\x3" "ip6" "\x4" "arpa";
386 static const domainname
*nA
= (const domainname
*)"\x1" "a" "\x1" "e" "\x1" "f" "\x3" "ip6" "\x4" "arpa";
387 static const domainname
*nB
= (const domainname
*)"\x1" "b" "\x1" "e" "\x1" "f" "\x3" "ip6" "\x4" "arpa";
389 const domainname
*d1
, *d2
, *d3
, *d4
, *d5
; // Top-level domain, second-level domain, etc.
390 d1
= d2
= d3
= d4
= d5
= mDNSNULL
;
393 d5
= d4
; d4
= d3
; d3
= d2
; d2
= d1
; d1
= d
;
394 d
= (const domainname
*)(d
->c
+ 1 + d
->c
[0]);
397 if (d1
&& SameDomainName(d1
, nL
)) return(mDNStrue
);
398 if (d4
&& SameDomainName(d4
, nR
)) return(mDNStrue
);
399 if (d5
&& SameDomainName(d5
, n8
)) return(mDNStrue
);
400 if (d5
&& SameDomainName(d5
, n9
)) return(mDNStrue
);
401 if (d5
&& SameDomainName(d5
, nA
)) return(mDNStrue
);
402 if (d5
&& SameDomainName(d5
, nB
)) return(mDNStrue
);
406 mDNSexport
const mDNSu8
*LastLabel(const domainname
*d
)
408 const mDNSu8
*p
= d
->c
;
412 d
= (const domainname
*)(d
->c
+ 1 + d
->c
[0]);
417 // Returns length of a domain name INCLUDING the byte for the final null label
418 // e.g. for the root label "." it returns one
419 // For the FQDN "com." it returns 5 (length byte, three data bytes, final zero)
420 // Legal results are 1 (just root label) to 256 (MAX_DOMAIN_NAME)
421 // If the given domainname is invalid, result is 257 (MAX_DOMAIN_NAME+1)
422 mDNSexport mDNSu16
DomainNameLengthLimit(const domainname
*const name
, const mDNSu8
*limit
)
424 const mDNSu8
*src
= name
->c
;
425 while (src
< limit
&& *src
<= MAX_DOMAIN_LABEL
)
427 if (*src
== 0) return((mDNSu16
)(src
- name
->c
+ 1));
430 return(MAX_DOMAIN_NAME
+1);
433 // CompressedDomainNameLength returns the length of a domain name INCLUDING the byte
434 // for the final null label, e.g. for the root label "." it returns one.
435 // E.g. for the FQDN "foo.com." it returns 9
436 // (length, three data bytes, length, three more data bytes, final zero).
437 // In the case where a parent domain name is provided, and the given name is a child
438 // of that parent, CompressedDomainNameLength returns the length of the prefix portion
439 // of the child name, plus TWO bytes for the compression pointer.
440 // E.g. for the name "foo.com." with parent "com.", it returns 6
441 // (length, three data bytes, two-byte compression pointer).
442 mDNSexport mDNSu16
CompressedDomainNameLength(const domainname
*const name
, const domainname
*parent
)
444 const mDNSu8
*src
= name
->c
;
445 if (parent
&& parent
->c
[0] == 0) parent
= mDNSNULL
;
448 if (*src
> MAX_DOMAIN_LABEL
) return(MAX_DOMAIN_NAME
+1);
449 if (parent
&& SameDomainName((const domainname
*)src
, parent
)) return((mDNSu16
)(src
- name
->c
+ 2));
451 if (src
- name
->c
>= MAX_DOMAIN_NAME
) return(MAX_DOMAIN_NAME
+1);
453 return((mDNSu16
)(src
- name
->c
+ 1));
456 // CountLabels() returns number of labels in name, excluding final root label
457 // (e.g. for "apple.com." CountLabels returns 2.)
458 mDNSexport
int CountLabels(const domainname
*d
)
462 for (ptr
= d
->c
; *ptr
; ptr
= ptr
+ ptr
[0] + 1) count
++;
466 // SkipLeadingLabels skips over the first 'skip' labels in the domainname,
467 // returning a pointer to the suffix with 'skip' labels removed.
468 mDNSexport
const domainname
*SkipLeadingLabels(const domainname
*d
, int skip
)
470 while (skip
> 0 && d
->c
[0]) { d
= (const domainname
*)(d
->c
+ 1 + d
->c
[0]); skip
--; }
474 // AppendLiteralLabelString appends a single label to an existing (possibly empty) domainname.
475 // The C string contains the label as-is, with no escaping, etc.
476 // Any dots in the name are literal dots, not label separators
477 // If successful, AppendLiteralLabelString returns a pointer to the next unused byte
478 // in the domainname bufer (i.e. the next byte after the terminating zero).
479 // If unable to construct a legal domain name (i.e. label more than 63 bytes, or total more than 256 bytes)
480 // AppendLiteralLabelString returns mDNSNULL.
481 mDNSexport mDNSu8
*AppendLiteralLabelString(domainname
*const name
, const char *cstr
)
483 mDNSu8
* ptr
= name
->c
+ DomainNameLength(name
) - 1; // Find end of current name
484 const mDNSu8
*const lim1
= name
->c
+ MAX_DOMAIN_NAME
- 1; // Limit of how much we can add (not counting final zero)
485 const mDNSu8
*const lim2
= ptr
+ 1 + MAX_DOMAIN_LABEL
;
486 const mDNSu8
*const lim
= (lim1
< lim2
) ? lim1
: lim2
;
487 mDNSu8
*lengthbyte
= ptr
++; // Record where the length is going to go
489 while (*cstr
&& ptr
< lim
) *ptr
++ = (mDNSu8
)*cstr
++; // Copy the data
490 *lengthbyte
= (mDNSu8
)(ptr
- lengthbyte
- 1); // Fill in the length byte
491 *ptr
++ = 0; // Put the null root label on the end
492 if (*cstr
) return(mDNSNULL
); // Failure: We didn't successfully consume all input
493 else return(ptr
); // Success: return new value of ptr
496 // AppendDNSNameString appends zero or more labels to an existing (possibly empty) domainname.
497 // The C string is in conventional DNS syntax:
498 // Textual labels, escaped as necessary using the usual DNS '\' notation, separated by dots.
499 // If successful, AppendDNSNameString returns a pointer to the next unused byte
500 // in the domainname bufer (i.e. the next byte after the terminating zero).
501 // If unable to construct a legal domain name (i.e. label more than 63 bytes, or total more than 256 bytes)
502 // AppendDNSNameString returns mDNSNULL.
503 mDNSexport mDNSu8
*AppendDNSNameString(domainname
*const name
, const char *cstring
)
505 const char *cstr
= cstring
;
506 mDNSu8
* ptr
= name
->c
+ DomainNameLength(name
) - 1; // Find end of current name
507 const mDNSu8
*const lim
= name
->c
+ MAX_DOMAIN_NAME
- 1; // Limit of how much we can add (not counting final zero)
508 while (*cstr
&& ptr
< lim
) // While more characters, and space to put them...
510 mDNSu8
*lengthbyte
= ptr
++; // Record where the length is going to go
511 if (*cstr
== '.') { LogMsg("AppendDNSNameString: Illegal empty label in name \"%s\"", cstring
); return(mDNSNULL
); }
512 while (*cstr
&& *cstr
!= '.' && ptr
< lim
) // While we have characters in the label...
514 mDNSu8 c
= (mDNSu8
)*cstr
++; // Read the character
515 if (c
== '\\') // If escape character, check next character
517 c
= (mDNSu8
)*cstr
++; // Assume we'll just take the next character
518 if (mDNSIsDigit(cstr
[-1]) && mDNSIsDigit(cstr
[0]) && mDNSIsDigit(cstr
[1]))
519 { // If three decimal digits,
520 int v0
= cstr
[-1] - '0'; // then interpret as three-digit decimal
521 int v1
= cstr
[ 0] - '0';
522 int v2
= cstr
[ 1] - '0';
523 int val
= v0
* 100 + v1
* 10 + v2
;
524 if (val
<= 255) { c
= (mDNSu8
)val
; cstr
+= 2; } // If valid three-digit decimal value, use it
527 *ptr
++ = c
; // Write the character
529 if (*cstr
) cstr
++; // Skip over the trailing dot (if present)
530 if (ptr
- lengthbyte
- 1 > MAX_DOMAIN_LABEL
) // If illegal label, abort
532 *lengthbyte
= (mDNSu8
)(ptr
- lengthbyte
- 1); // Fill in the length byte
535 *ptr
++ = 0; // Put the null root label on the end
536 if (*cstr
) return(mDNSNULL
); // Failure: We didn't successfully consume all input
537 else return(ptr
); // Success: return new value of ptr
540 // AppendDomainLabel appends a single label to a name.
541 // If successful, AppendDomainLabel returns a pointer to the next unused byte
542 // in the domainname bufer (i.e. the next byte after the terminating zero).
543 // If unable to construct a legal domain name (i.e. label more than 63 bytes, or total more than 256 bytes)
544 // AppendDomainLabel returns mDNSNULL.
545 mDNSexport mDNSu8
*AppendDomainLabel(domainname
*const name
, const domainlabel
*const label
)
548 mDNSu8
*ptr
= name
->c
+ DomainNameLength(name
) - 1;
550 // Check label is legal
551 if (label
->c
[0] > MAX_DOMAIN_LABEL
) return(mDNSNULL
);
553 // Check that ptr + length byte + data bytes + final zero does not exceed our limit
554 if (ptr
+ 1 + label
->c
[0] + 1 > name
->c
+ MAX_DOMAIN_NAME
) return(mDNSNULL
);
556 for (i
=0; i
<=label
->c
[0]; i
++) *ptr
++ = label
->c
[i
]; // Copy the label data
557 *ptr
++ = 0; // Put the null root label on the end
561 mDNSexport mDNSu8
*AppendDomainName(domainname
*const name
, const domainname
*const append
)
563 mDNSu8
* ptr
= name
->c
+ DomainNameLength(name
) - 1; // Find end of current name
564 const mDNSu8
*const lim
= name
->c
+ MAX_DOMAIN_NAME
- 1; // Limit of how much we can add (not counting final zero)
565 const mDNSu8
* src
= append
->c
;
569 if (ptr
+ 1 + src
[0] > lim
) return(mDNSNULL
);
570 for (i
=0; i
<=src
[0]; i
++) *ptr
++ = src
[i
];
571 *ptr
= 0; // Put the null root label on the end
577 // MakeDomainLabelFromLiteralString makes a single domain label from a single literal C string (with no escaping).
578 // If successful, MakeDomainLabelFromLiteralString returns mDNStrue.
579 // If unable to convert the whole string to a legal domain label (i.e. because length is more than 63 bytes) then
580 // MakeDomainLabelFromLiteralString makes a legal domain label from the first 63 bytes of the string and returns mDNSfalse.
581 // In some cases silently truncated oversized names to 63 bytes is acceptable, so the return result may be ignored.
582 // In other cases silent truncation may not be acceptable, so in those cases the calling function needs to check the return result.
583 mDNSexport mDNSBool
MakeDomainLabelFromLiteralString(domainlabel
*const label
, const char *cstr
)
585 mDNSu8
* ptr
= label
->c
+ 1; // Where we're putting it
586 const mDNSu8
*const limit
= label
->c
+ 1 + MAX_DOMAIN_LABEL
; // The maximum we can put
587 while (*cstr
&& ptr
< limit
) *ptr
++ = (mDNSu8
)*cstr
++; // Copy the label
588 label
->c
[0] = (mDNSu8
)(ptr
- label
->c
- 1); // Set the length byte
589 return(*cstr
== 0); // Return mDNStrue if we successfully consumed all input
592 // MakeDomainNameFromDNSNameString makes a native DNS-format domainname from a C string.
593 // The C string is in conventional DNS syntax:
594 // Textual labels, escaped as necessary using the usual DNS '\' notation, separated by dots.
595 // If successful, MakeDomainNameFromDNSNameString returns a pointer to the next unused byte
596 // in the domainname bufer (i.e. the next byte after the terminating zero).
597 // If unable to construct a legal domain name (i.e. label more than 63 bytes, or total more than 256 bytes)
598 // MakeDomainNameFromDNSNameString returns mDNSNULL.
599 mDNSexport mDNSu8
*MakeDomainNameFromDNSNameString(domainname
*const name
, const char *cstr
)
601 name
->c
[0] = 0; // Make an empty domain name
602 return(AppendDNSNameString(name
, cstr
)); // And then add this string to it
605 mDNSexport
char *ConvertDomainLabelToCString_withescape(const domainlabel
*const label
, char *ptr
, char esc
)
607 const mDNSu8
* src
= label
->c
; // Domain label we're reading
608 const mDNSu8 len
= *src
++; // Read length of this (non-null) label
609 const mDNSu8
*const end
= src
+ len
; // Work out where the label ends
610 if (len
> MAX_DOMAIN_LABEL
) return(mDNSNULL
); // If illegal label, abort
611 while (src
< end
) // While we have characters in the label
616 if (c
== '.' || c
== esc
) // If character is a dot or the escape character
617 *ptr
++ = esc
; // Output escape character
618 else if (c
<= ' ') // If non-printing ascii,
619 { // Output decimal escape sequence
621 *ptr
++ = (char) ('0' + (c
/ 100) );
622 *ptr
++ = (char) ('0' + (c
/ 10) % 10);
623 c
= (mDNSu8
)('0' + (c
) % 10);
626 *ptr
++ = (char)c
; // Copy the character
628 *ptr
= 0; // Null-terminate the string
629 return(ptr
); // and return
632 // Note: To guarantee that there will be no possible overrun, cstr must be at least MAX_ESCAPED_DOMAIN_NAME (1009 bytes)
633 mDNSexport
char *ConvertDomainNameToCString_withescape(const domainname
*const name
, char *ptr
, char esc
)
635 const mDNSu8
*src
= name
->c
; // Domain name we're reading
636 const mDNSu8
*const max
= name
->c
+ MAX_DOMAIN_NAME
; // Maximum that's valid
638 if (*src
== 0) *ptr
++ = '.'; // Special case: For root, just write a dot
640 while (*src
) // While more characters in the domain name
642 if (src
+ 1 + *src
>= max
) return(mDNSNULL
);
643 ptr
= ConvertDomainLabelToCString_withescape((const domainlabel
*)src
, ptr
, esc
);
644 if (!ptr
) return(mDNSNULL
);
646 *ptr
++ = '.'; // Write the dot after the label
649 *ptr
++ = 0; // Null-terminate the string
650 return(ptr
); // and return
654 // Host names must start with a letter, end with a letter or digit,
655 // and have as interior characters only letters, digits, and hyphen.
656 // This was subsequently modified in RFC 1123 to allow the first character to be either a letter or a digit
658 mDNSexport
void ConvertUTF8PstringToRFC1034HostLabel(const mDNSu8 UTF8Name
[], domainlabel
*const hostlabel
)
660 const mDNSu8
* src
= &UTF8Name
[1];
661 const mDNSu8
*const end
= &UTF8Name
[1] + UTF8Name
[0];
662 mDNSu8
* ptr
= &hostlabel
->c
[1];
663 const mDNSu8
*const lim
= &hostlabel
->c
[1] + MAX_DOMAIN_LABEL
;
666 // Delete apostrophes from source name
667 if (src
[0] == '\'') { src
++; continue; } // Standard straight single quote
668 if (src
+ 2 < end
&& src
[0] == 0xE2 && src
[1] == 0x80 && src
[2] == 0x99)
669 { src
+= 3; continue; } // Unicode curly apostrophe
672 if (mDNSValidHostChar(*src
, (ptr
> &hostlabel
->c
[1]), (src
< end
-1))) *ptr
++ = *src
;
673 else if (ptr
> &hostlabel
->c
[1] && ptr
[-1] != '-') *ptr
++ = '-';
677 while (ptr
> &hostlabel
->c
[1] && ptr
[-1] == '-') ptr
--; // Truncate trailing '-' marks
678 hostlabel
->c
[0] = (mDNSu8
)(ptr
- &hostlabel
->c
[1]);
681 #define ValidTransportProtocol(X) ( (X)[0] == 4 && (X)[1] == '_' && \
682 ((((X)[2] | 0x20) == 'u' && ((X)[3] | 0x20) == 'd') || (((X)[2] | 0x20) == 't' && ((X)[3] | 0x20) == 'c')) && \
683 ((X)[4] | 0x20) == 'p')
685 mDNSexport mDNSu8
*ConstructServiceName(domainname
*const fqdn
,
686 const domainlabel
*name
, const domainname
*type
, const domainname
*const domain
)
689 mDNSu8
*dst
= fqdn
->c
;
691 const char *errormsg
;
692 #if APPLE_OSX_mDNSResponder
693 mDNSBool loggedUnderscore
= mDNSfalse
;
694 static char typeBuf
[MAX_ESCAPED_DOMAIN_NAME
];
697 // In the case where there is no name (and ONLY in that case),
698 // a single-label subtype is allowed as the first label of a three-part "type"
701 const mDNSu8
*s0
= type
->c
;
702 if (s0
[0] && s0
[0] < 0x40) // If legal first label (at least one character, and no more than 63)
704 const mDNSu8
* s1
= s0
+ 1 + s0
[0];
705 if (s1
[0] && s1
[0] < 0x40) // and legal second label (at least one character, and no more than 63)
707 const mDNSu8
*s2
= s1
+ 1 + s1
[0];
708 if (s2
[0] && s2
[0] < 0x40 && s2
[1+s2
[0]] == 0) // and we have three and only three labels
710 static const mDNSu8 SubTypeLabel
[5] = "\x04_sub";
711 src
= s0
; // Copy the first label
713 for (i
=0; i
<= len
; i
++) *dst
++ = *src
++;
714 for (i
=0; i
< (int)sizeof(SubTypeLabel
); i
++) *dst
++ = SubTypeLabel
[i
];
715 type
= (const domainname
*)s1
;
717 // Special support to enable the DNSServiceBrowse call made by Bonjour Browser
718 // For these queries, we retract the "._sub" we just added between the subtype and the main type
719 // Remove after Bonjour Browser is updated to use DNSServiceQueryRecord instead of DNSServiceBrowse
720 if (SameDomainName((domainname
*)s0
, (const domainname
*)"\x09_services\x07_dns-sd\x04_udp"))
721 dst
-= sizeof(SubTypeLabel
);
727 if (name
&& name
->c
[0])
729 src
= name
->c
; // Put the service name into the domain name
731 if (len
>= 0x40) { errormsg
= "Service instance name too long"; goto fail
; }
732 for (i
=0; i
<=len
; i
++) *dst
++ = *src
++;
735 name
= (domainlabel
*)""; // Set this up to be non-null, to avoid errors if we have to call LogMsg() below
737 src
= type
->c
; // Put the service type into the domain name
739 if (len
< 2 || len
> 16)
741 LogMsg("Bad service type in %#s.%##s%##s Application protocol name must be underscore plus 1-15 characters. "
742 "See <http://www.dns-sd.org/ServiceTypes.html>", name
->c
, type
->c
, domain
->c
);
743 #if APPLE_OSX_mDNSResponder
744 ConvertDomainNameToCString(type
, typeBuf
);
745 mDNSASLLog(mDNSNULL
, "serviceType.nameTooLong", "noop", typeBuf
, "");
748 if (len
< 2 || len
>= 0x40 || (len
> 16 && !SameDomainName(domain
, &localdomain
))) return(mDNSNULL
);
749 if (src
[1] != '_') { errormsg
= "Application protocol name must begin with underscore"; goto fail
; }
750 for (i
=2; i
<=len
; i
++)
752 // Letters and digits are allowed anywhere
753 if (mDNSIsLetter(src
[i
]) || mDNSIsDigit(src
[i
])) continue;
754 // Hyphens are only allowed as interior characters
755 // Underscores are not supposed to be allowed at all, but for backwards compatibility with some old products we do allow them,
756 // with the same rule as hyphens
757 if ((src
[i
] == '-' || src
[i
] == '_') && i
> 2 && i
< len
)
759 #if APPLE_OSX_mDNSResponder
760 if (src
[i
] == '_' && loggedUnderscore
== mDNSfalse
)
762 ConvertDomainNameToCString(type
, typeBuf
);
763 mDNSASLLog(mDNSNULL
, "serviceType.nameWithUnderscore", "noop", typeBuf
, "");
764 loggedUnderscore
= mDNStrue
;
769 errormsg
= "Application protocol name must contain only letters, digits, and hyphens";
770 #if APPLE_OSX_mDNSResponder
772 ConvertDomainNameToCString(type
, typeBuf
);
773 mDNSASLLog(mDNSNULL
, "serviceType.nameWithIllegalCharacters", "noop", typeBuf
, "");
778 for (i
=0; i
<=len
; i
++) *dst
++ = *src
++;
781 if (!ValidTransportProtocol(src
)) { errormsg
= "Transport protocol name must be _udp or _tcp"; goto fail
; }
782 for (i
=0; i
<=len
; i
++) *dst
++ = *src
++;
784 if (*src
) { errormsg
= "Service type must have only two labels"; goto fail
; }
787 if (!domain
->c
[0]) { errormsg
= "Service domain must be non-empty"; goto fail
; }
788 if (SameDomainName(domain
, (const domainname
*)"\x05" "local" "\x04" "arpa"))
789 { errormsg
= "Illegal domain \"local.arpa.\" Use \"local.\" (or empty string)"; goto fail
; }
790 dst
= AppendDomainName(fqdn
, domain
);
791 if (!dst
) { errormsg
= "Service domain too long"; goto fail
; }
795 LogMsg("ConstructServiceName: %s: %#s.%##s%##s", errormsg
, name
->c
, type
->c
, domain
->c
);
799 // A service name has the form: instance.application-protocol.transport-protocol.domain
800 // DeconstructServiceName is currently fairly forgiving: It doesn't try to enforce character
801 // set or length limits for the protocol names, and the final domain is allowed to be empty.
802 // However, if the given FQDN doesn't contain at least three labels,
803 // DeconstructServiceName will reject it and return mDNSfalse.
804 mDNSexport mDNSBool
DeconstructServiceName(const domainname
*const fqdn
,
805 domainlabel
*const name
, domainname
*const type
, domainname
*const domain
)
808 const mDNSu8
*src
= fqdn
->c
;
809 const mDNSu8
*max
= fqdn
->c
+ MAX_DOMAIN_NAME
;
812 dst
= name
->c
; // Extract the service name
814 if (!len
) { debugf("DeconstructServiceName: FQDN empty!"); return(mDNSfalse
); }
815 if (len
>= 0x40) { debugf("DeconstructServiceName: Instance name too long"); return(mDNSfalse
); }
816 for (i
=0; i
<=len
; i
++) *dst
++ = *src
++;
818 dst
= type
->c
; // Extract the service type
820 if (!len
) { debugf("DeconstructServiceName: FQDN contains only one label!"); return(mDNSfalse
); }
821 if (len
>= 0x40) { debugf("DeconstructServiceName: Application protocol name too long"); return(mDNSfalse
); }
822 if (src
[1] != '_'){ debugf("DeconstructServiceName: No _ at start of application protocol"); return(mDNSfalse
); }
823 for (i
=0; i
<=len
; i
++) *dst
++ = *src
++;
826 if (!len
) { debugf("DeconstructServiceName: FQDN contains only two labels!"); return(mDNSfalse
); }
827 if (!ValidTransportProtocol(src
))
828 { debugf("DeconstructServiceName: Transport protocol must be _udp or _tcp"); return(mDNSfalse
); }
829 for (i
=0; i
<=len
; i
++) *dst
++ = *src
++;
830 *dst
++ = 0; // Put terminator on the end of service type
832 dst
= domain
->c
; // Extract the service domain
837 { debugf("DeconstructServiceName: Label in service domain too long"); return(mDNSfalse
); }
838 if (src
+ 1 + len
+ 1 >= max
)
839 { debugf("DeconstructServiceName: Total service domain too long"); return(mDNSfalse
); }
840 for (i
=0; i
<=len
; i
++) *dst
++ = *src
++;
842 *dst
++ = 0; // Put the null root label on the end
848 // 0xxxxxxx represents a 7-bit ASCII value from 0x00 to 0x7F
849 // 10xxxxxx is a continuation byte of a multi-byte character
850 // 110xxxxx is the first byte of a 2-byte character (11 effective bits; values 0x 80 - 0x 800-1)
851 // 1110xxxx is the first byte of a 3-byte character (16 effective bits; values 0x 800 - 0x 10000-1)
852 // 11110xxx is the first byte of a 4-byte character (21 effective bits; values 0x 10000 - 0x 200000-1)
853 // 111110xx is the first byte of a 5-byte character (26 effective bits; values 0x 200000 - 0x 4000000-1)
854 // 1111110x is the first byte of a 6-byte character (31 effective bits; values 0x4000000 - 0x80000000-1)
856 // UTF-16 surrogate pairs are used in UTF-16 to encode values larger than 0xFFFF.
857 // Although UTF-16 surrogate pairs are not supposed to appear in legal UTF-8, we want to be defensive
858 // about that too. (See <http://www.unicode.org/faq/utf_bom.html#34>, "What are surrogates?")
859 // The first of pair is a UTF-16 value in the range 0xD800-0xDBFF (11101101 1010xxxx 10xxxxxx in UTF-8),
860 // and the second is a UTF-16 value in the range 0xDC00-0xDFFF (11101101 1011xxxx 10xxxxxx in UTF-8).
862 mDNSexport mDNSu32
TruncateUTF8ToLength(mDNSu8
*string
, mDNSu32 length
, mDNSu32 max
)
866 mDNSu8 c1
= string
[max
]; // First byte after cut point
867 mDNSu8 c2
= (max
+1 < length
) ? string
[max
+1] : (mDNSu8
)0xB0; // Second byte after cut point
868 length
= max
; // Trim length down
871 // Check if the byte right after the chop point is a UTF-8 continuation byte,
872 // or if the character right after the chop point is the second of a UTF-16 surrogate pair.
873 // If so, then we continue to chop more bytes until we get to a legal chop point.
874 mDNSBool continuation
= ((c1
& 0xC0) == 0x80);
875 mDNSBool secondsurrogate
= (c1
== 0xED && (c2
& 0xF0) == 0xB0);
876 if (!continuation
&& !secondsurrogate
) break;
878 c1
= string
[--length
];
880 // Having truncated characters off the end of our string, also cut off any residual white space
881 while (length
> 0 && string
[length
-1] <= ' ') length
--;
886 // Returns true if a rich text label ends in " (nnn)", or if an RFC 1034
887 // name ends in "-nnn", where n is some decimal number.
888 mDNSexport mDNSBool
LabelContainsSuffix(const domainlabel
*const name
, const mDNSBool RichText
)
890 mDNSu16 l
= name
->c
[0];
894 if (l
< 4) return mDNSfalse
; // Need at least " (2)"
895 if (name
->c
[l
--] != ')') return mDNSfalse
; // Last char must be ')'
896 if (!mDNSIsDigit(name
->c
[l
])) return mDNSfalse
; // Preceeded by a digit
898 while (l
> 2 && mDNSIsDigit(name
->c
[l
])) l
--; // Strip off digits
899 return (name
->c
[l
] == '(' && name
->c
[l
- 1] == ' ');
903 if (l
< 2) return mDNSfalse
; // Need at least "-2"
904 if (!mDNSIsDigit(name
->c
[l
])) return mDNSfalse
; // Last char must be a digit
906 while (l
> 2 && mDNSIsDigit(name
->c
[l
])) l
--; // Strip off digits
907 return (name
->c
[l
] == '-');
911 // removes an auto-generated suffix (appended on a name collision) from a label. caller is
912 // responsible for ensuring that the label does indeed contain a suffix. returns the number
913 // from the suffix that was removed.
914 mDNSexport mDNSu32
RemoveLabelSuffix(domainlabel
*name
, mDNSBool RichText
)
916 mDNSu32 val
= 0, multiplier
= 1;
918 // Chop closing parentheses from RichText suffix
919 if (RichText
&& name
->c
[0] >= 1 && name
->c
[name
->c
[0]] == ')') name
->c
[0]--;
921 // Get any existing numerical suffix off the name
922 while (mDNSIsDigit(name
->c
[name
->c
[0]]))
923 { val
+= (name
->c
[name
->c
[0]] - '0') * multiplier
; multiplier
*= 10; name
->c
[0]--; }
925 // Chop opening parentheses or dash from suffix
928 if (name
->c
[0] >= 2 && name
->c
[name
->c
[0]] == '(' && name
->c
[name
->c
[0]-1] == ' ') name
->c
[0] -= 2;
932 if (name
->c
[0] >= 1 && name
->c
[name
->c
[0]] == '-') name
->c
[0] -= 1;
938 // appends a numerical suffix to a label, with the number following a whitespace and enclosed
939 // in parentheses (rich text) or following two consecutive hyphens (RFC 1034 domain label).
940 mDNSexport
void AppendLabelSuffix(domainlabel
*const name
, mDNSu32 val
, const mDNSBool RichText
)
942 mDNSu32 divisor
= 1, chars
= 2; // Shortest possible RFC1034 name suffix is 2 characters ("-2")
943 if (RichText
) chars
= 4; // Shortest possible RichText suffix is 4 characters (" (2)")
945 // Truncate trailing spaces from RichText names
946 if (RichText
) while (name
->c
[name
->c
[0]] == ' ') name
->c
[0]--;
948 while (divisor
< 0xFFFFFFFFUL
/10 && val
>= divisor
* 10) { divisor
*= 10; chars
++; }
950 name
->c
[0] = (mDNSu8
) TruncateUTF8ToLength(name
->c
+1, name
->c
[0], MAX_DOMAIN_LABEL
- chars
);
952 if (RichText
) { name
->c
[++name
->c
[0]] = ' '; name
->c
[++name
->c
[0]] = '('; }
953 else { name
->c
[++name
->c
[0]] = '-'; }
957 name
->c
[++name
->c
[0]] = (mDNSu8
)('0' + val
/ divisor
);
962 if (RichText
) name
->c
[++name
->c
[0]] = ')';
965 mDNSexport
void IncrementLabelSuffix(domainlabel
*name
, mDNSBool RichText
)
969 if (LabelContainsSuffix(name
, RichText
))
970 val
= RemoveLabelSuffix(name
, RichText
);
972 // If no existing suffix, start by renaming "Foo" as "Foo (2)" or "Foo-2" as appropriate.
973 // If existing suffix in the range 2-9, increment it.
974 // If we've had ten conflicts already, there are probably too many hosts trying to use the same name,
975 // so add a random increment to improve the chances of finding an available name next time.
976 if (val
== 0) val
= 2;
977 else if (val
< 10) val
++;
978 else val
+= 1 + mDNSRandom(99);
980 AppendLabelSuffix(name
, val
, RichText
);
983 // ***************************************************************************
984 #if COMPILER_LIKES_PRAGMA_MARK
986 #pragma mark - Resource Record Utility Functions
989 // Set up a AuthRecord with sensible default values.
990 // These defaults may be overwritten with new values before mDNS_Register is called
991 mDNSexport
void mDNS_SetupResourceRecord(AuthRecord
*rr
, RData
*RDataStorage
, mDNSInterfaceID InterfaceID
,
992 mDNSu16 rrtype
, mDNSu32 ttl
, mDNSu8 RecordType
, AuthRecType artype
, mDNSRecordCallback Callback
, void *Context
)
995 // LocalOnly auth record can be created with LocalOnly InterfaceID or a valid InterfaceID.
996 // Most of the applications normally create with LocalOnly InterfaceID and we store them as
997 // such, so that we can deliver the response to questions that specify LocalOnly InterfaceID.
998 // LocalOnly resource records can also be created with valid InterfaceID which happens today
999 // when we create LocalOnly records for /etc/hosts.
1001 if (InterfaceID
== mDNSInterface_LocalOnly
&& artype
!= AuthRecordLocalOnly
)
1003 LogMsg("mDNS_SetupResourceRecord: ERROR!! Mismatch LocalOnly record InterfaceID %p called with artype %d", InterfaceID
, artype
);
1006 else if (InterfaceID
== mDNSInterface_P2P
&& artype
!= AuthRecordP2P
)
1008 LogMsg("mDNS_SetupResourceRecord: ERROR!! Mismatch P2P record InterfaceID %p called with artype %d", InterfaceID
, artype
);
1011 else if (!InterfaceID
&& (artype
== AuthRecordP2P
|| artype
== AuthRecordLocalOnly
))
1013 LogMsg("mDNS_SetupResourceRecord: ERROR!! Mismatch InterfaceAny record InterfaceID %p called with artype %d", InterfaceID
, artype
);
1017 // Don't try to store a TTL bigger than we can represent in platform time units
1018 if (ttl
> 0x7FFFFFFFUL
/ mDNSPlatformOneSecond
)
1019 ttl
= 0x7FFFFFFFUL
/ mDNSPlatformOneSecond
;
1020 else if (ttl
== 0) // And Zero TTL is illegal
1021 ttl
= DefaultTTLforRRType(rrtype
);
1023 // Field Group 1: The actual information pertaining to this resource record
1024 rr
->resrec
.RecordType
= RecordType
;
1025 rr
->resrec
.InterfaceID
= InterfaceID
;
1026 rr
->resrec
.name
= &rr
->namestorage
;
1027 rr
->resrec
.rrtype
= rrtype
;
1028 rr
->resrec
.rrclass
= kDNSClass_IN
;
1029 rr
->resrec
.rroriginalttl
= ttl
;
1030 rr
->resrec
.rDNSServer
= mDNSNULL
;
1031 // rr->resrec.rdlength = MUST set by client and/or in mDNS_Register_internal
1032 // rr->resrec.rdestimate = set in mDNS_Register_internal
1033 // rr->resrec.rdata = MUST be set by client
1036 rr
->resrec
.rdata
= RDataStorage
;
1039 rr
->resrec
.rdata
= &rr
->rdatastorage
;
1040 rr
->resrec
.rdata
->MaxRDLength
= sizeof(RDataBody
);
1043 // Field Group 2: Persistent metadata for Authoritative Records
1044 rr
->Additional1
= mDNSNULL
;
1045 rr
->Additional2
= mDNSNULL
;
1046 rr
->DependentOn
= mDNSNULL
;
1047 rr
->RRSet
= mDNSNULL
;
1048 rr
->RecordCallback
= Callback
;
1049 rr
->RecordContext
= Context
;
1051 rr
->AutoTarget
= Target_Manual
;
1052 rr
->AllowRemoteQuery
= mDNSfalse
;
1053 rr
->ForceMCast
= mDNSfalse
;
1055 rr
->WakeUp
= zeroOwner
;
1056 rr
->AddressProxy
= zeroAddr
;
1059 rr
->ARType
= artype
;
1061 // Field Group 3: Transient state for Authoritative Records (set in mDNS_Register_internal)
1062 // Field Group 4: Transient uDNS state for Authoritative Records (set in mDNS_Register_internal)
1064 // For now, until the uDNS code is fully integrated, it's helpful to zero the uDNS state fields here too, just in case
1065 // (e.g. uDNS_RegisterService short-circuits the usual mDNS_Register_internal record registration calls, so a bunch
1066 // of fields don't get set up properly. In particular, if we don't zero rr->QueuedRData then the uDNS code crashes.)
1067 rr
->state
= regState_Zero
;
1071 rr
->updateid
= zeroID
;
1072 rr
->zone
= rr
->resrec
.name
;
1077 rr
->InFlightRData
= 0;
1078 rr
->InFlightRDLen
= 0;
1079 rr
->QueuedRData
= 0;
1080 rr
->QueuedRDLen
= 0;
1081 mDNSPlatformMemZero(&rr
->NATinfo
, sizeof(rr
->NATinfo
));
1082 rr
->SRVChanged
= mDNSfalse
;
1083 rr
->mState
= mergeState_Zero
;
1085 rr
->namestorage
.c
[0] = 0; // MUST be set by client before calling mDNS_Register()
1088 mDNSexport
void mDNS_SetupQuestion(DNSQuestion
*const q
, const mDNSInterfaceID InterfaceID
, const domainname
*const name
,
1089 const mDNSu16 qtype
, mDNSQuestionCallback
*const callback
, void *const context
)
1091 q
->InterfaceID
= InterfaceID
;
1092 q
->Target
= zeroAddr
;
1093 AssignDomainName(&q
->qname
, name
);
1095 q
->qclass
= kDNSClass_IN
;
1096 q
->LongLived
= (qtype
== kDNSType_PTR
);
1097 q
->ExpectUnique
= (qtype
!= kDNSType_PTR
);
1098 q
->ForceMCast
= mDNSfalse
;
1099 q
->ReturnIntermed
= mDNSfalse
;
1100 q
->SuppressUnusable
= mDNSfalse
;
1101 q
->SearchListIndex
= 0;
1102 q
->AppendSearchDomains
= 0;
1103 q
->RetryWithSearchDomains
= mDNSfalse
;
1104 q
->TimeoutQuestion
= 0;
1105 q
->WakeOnResolve
= 0;
1106 q
->qnameOrig
= mDNSNULL
;
1107 q
->QuestionCallback
= callback
;
1108 q
->QuestionContext
= context
;
1111 mDNSexport mDNSu32
RDataHashValue(const ResourceRecord
*const rr
)
1113 int len
= rr
->rdlength
;
1114 const RDataBody2
*const rdb
= (RDataBody2
*)rr
->rdata
->u
.data
;
1118 case kDNSType_CNAME
:
1120 case kDNSType_DNAME
: return DomainNameHashValue(&rdb
->name
);
1122 case kDNSType_SOA
: return rdb
->soa
.serial
+
1127 DomainNameHashValue(&rdb
->soa
.mname
) +
1128 DomainNameHashValue(&rdb
->soa
.rname
);
1131 case kDNSType_AFSDB
:
1133 case kDNSType_KX
: return DomainNameHashValue(&rdb
->mx
.exchange
);
1135 case kDNSType_RP
: return DomainNameHashValue(&rdb
->rp
.mbox
) + DomainNameHashValue(&rdb
->rp
.txt
);
1137 case kDNSType_PX
: return DomainNameHashValue(&rdb
->px
.map822
) + DomainNameHashValue(&rdb
->px
.mapx400
);
1139 case kDNSType_SRV
: return DomainNameHashValue(&rdb
->srv
.target
);
1141 case kDNSType_OPT
: return 0; // OPT is a pseudo-RR container structure; makes no sense to compare
1143 case kDNSType_NSEC
: len
= sizeof(rdataNSEC
); // Use in-memory length of 32, and fall through default checksum computation below
1149 for (i
=0; i
+1 < len
; i
+=2)
1151 sum
+= (((mDNSu32
)(rdb
->data
[i
])) << 8) | rdb
->data
[i
+1];
1152 sum
= (sum
<<3) | (sum
>>29);
1156 sum
+= ((mDNSu32
)(rdb
->data
[i
])) << 8;
1163 // r1 has to be a full ResourceRecord including rrtype and rdlength
1164 // r2 is just a bare RDataBody, which MUST be the same rrtype and rdlength as r1
1165 mDNSexport mDNSBool
SameRDataBody(const ResourceRecord
*const r1
, const RDataBody
*const r2
, DomainNameComparisonFn
*samename
)
1167 const RDataBody2
*const b1
= (RDataBody2
*)r1
->rdata
->u
.data
;
1168 const RDataBody2
*const b2
= (RDataBody2
*)r2
;
1172 case kDNSType_CNAME
:
1174 case kDNSType_DNAME
:return(SameDomainName(&b1
->name
, &b2
->name
));
1176 case kDNSType_SOA
: return(mDNSBool
)( b1
->soa
.serial
== b2
->soa
.serial
&&
1177 b1
->soa
.refresh
== b2
->soa
.refresh
&&
1178 b1
->soa
.retry
== b2
->soa
.retry
&&
1179 b1
->soa
.expire
== b2
->soa
.expire
&&
1180 b1
->soa
.min
== b2
->soa
.min
&&
1181 samename(&b1
->soa
.mname
, &b2
->soa
.mname
) &&
1182 samename(&b1
->soa
.rname
, &b2
->soa
.rname
));
1185 case kDNSType_AFSDB
:
1187 case kDNSType_KX
: return(mDNSBool
)( b1
->mx
.preference
== b2
->mx
.preference
&&
1188 samename(&b1
->mx
.exchange
, &b2
->mx
.exchange
));
1190 case kDNSType_RP
: return(mDNSBool
)( samename(&b1
->rp
.mbox
, &b2
->rp
.mbox
) &&
1191 samename(&b1
->rp
.txt
, &b2
->rp
.txt
));
1193 case kDNSType_PX
: return(mDNSBool
)( b1
->px
.preference
== b2
->px
.preference
&&
1194 samename(&b1
->px
.map822
, &b2
->px
.map822
) &&
1195 samename(&b1
->px
.mapx400
, &b2
->px
.mapx400
));
1197 case kDNSType_SRV
: return(mDNSBool
)( b1
->srv
.priority
== b2
->srv
.priority
&&
1198 b1
->srv
.weight
== b2
->srv
.weight
&&
1199 mDNSSameIPPort(b1
->srv
.port
, b2
->srv
.port
) &&
1200 samename(&b1
->srv
.target
, &b2
->srv
.target
));
1202 case kDNSType_OPT
: return mDNSfalse
; // OPT is a pseudo-RR container structure; makes no sense to compare
1204 case kDNSType_NSEC
: return(mDNSPlatformMemSame(b1
->data
, b2
->data
, sizeof(rdataNSEC
)));
1206 default: return(mDNSPlatformMemSame(b1
->data
, b2
->data
, r1
->rdlength
));
1210 // ResourceRecordAnswersQuestion returns mDNStrue if the given resource record is a valid answer to the given question.
1211 // SameNameRecordAnswersQuestion is the same, except it skips the expensive SameDomainName() call.
1212 // SameDomainName() is generally cheap when the names don't match, but expensive when they do match,
1213 // because it has to check all the way to the end of the names to be sure.
1214 // In cases where we know in advance that the names match it's especially advantageous to skip the
1215 // SameDomainName() call because that's precisely the time when it's most expensive and least useful.
1217 mDNSexport mDNSBool
SameNameRecordAnswersQuestion(const ResourceRecord
*const rr
, const DNSQuestion
*const q
)
1219 // LocalOnly/P2P questions can be answered with AuthRecordAny in this function. LocalOnly/P2P records
1220 // are handled in LocalOnlyRecordAnswersQuestion
1221 if ((rr
->InterfaceID
== mDNSInterface_LocalOnly
) || (rr
->InterfaceID
== mDNSInterface_P2P
))
1223 LogMsg("SameNameRecordAnswersQuestion: ERROR!! called with LocalOnly ResourceRecord %p, Question %p", rr
->InterfaceID
, q
->InterfaceID
);
1226 if (rr
->InterfaceID
&&
1227 q
->InterfaceID
&& q
->InterfaceID
!= mDNSInterface_LocalOnly
&&
1228 rr
->InterfaceID
!= q
->InterfaceID
) return(mDNSfalse
);
1230 // Resource record received via unicast, the DNSServer entries should match ?
1231 if (!rr
->InterfaceID
&& rr
->rDNSServer
!= q
->qDNSServer
) return(mDNSfalse
);
1233 // If ResourceRecord received via multicast, but question was unicast, then shouldn't use record to answer this question
1234 if (rr
->InterfaceID
&& !mDNSOpaque16IsZero(q
->TargetQID
)) return(mDNSfalse
);
1236 // RR type CNAME matches any query type. QTYPE ANY matches any RR type. QCLASS ANY matches any RR class.
1237 if (!RRTypeAnswersQuestionType(rr
,q
->qtype
)) return(mDNSfalse
);
1238 if (rr
->rrclass
!= q
->qclass
&& q
->qclass
!= kDNSQClass_ANY
) return(mDNSfalse
);
1243 mDNSexport mDNSBool
ResourceRecordAnswersQuestion(const ResourceRecord
*const rr
, const DNSQuestion
*const q
)
1245 // LocalOnly/P2P questions can be answered with AuthRecordAny in this function. LocalOnly/P2P records
1246 // are handled in LocalOnlyRecordAnswersQuestion
1247 if ((rr
->InterfaceID
== mDNSInterface_LocalOnly
) || (rr
->InterfaceID
== mDNSInterface_P2P
))
1249 LogMsg("ResourceRecordAnswersQuestion: ERROR!! called with LocalOnly/P2P ResourceRecord %p, Question %p", rr
->InterfaceID
, q
->InterfaceID
);
1253 if (rr
->InterfaceID
&&
1254 q
->InterfaceID
&& q
->InterfaceID
!= mDNSInterface_LocalOnly
&&
1255 rr
->InterfaceID
!= q
->InterfaceID
) return(mDNSfalse
);
1257 // Resource record received via unicast, the DNSServer entries should match ?
1258 if (!rr
->InterfaceID
&& rr
->rDNSServer
!= q
->qDNSServer
) return(mDNSfalse
);
1260 // If ResourceRecord received via multicast, but question was unicast, then shouldn't use record to answer this question.
1261 if (rr
->InterfaceID
&& !mDNSOpaque16IsZero(q
->TargetQID
)) return(mDNSfalse
);
1263 // RR type CNAME matches any query type. QTYPE ANY matches any RR type. QCLASS ANY matches any RR class.
1264 if (!RRTypeAnswersQuestionType(rr
,q
->qtype
)) return(mDNSfalse
);
1265 if (rr
->rrclass
!= q
->qclass
&& q
->qclass
!= kDNSQClass_ANY
) return(mDNSfalse
);
1267 return(rr
->namehash
== q
->qnamehash
&& SameDomainName(rr
->name
, &q
->qname
));
1270 // We have a separate function to handle LocalOnly AuthRecords because they can be created with
1271 // a valid InterfaceID (e.g., scoped /etc/hosts) and can be used to answer unicast questions unlike
1272 // multicast resource records (which has a valid InterfaceID) which can't be used to answer
1273 // unicast questions. ResourceRecordAnswersQuestion/SameNameRecordAnswersQuestion can't tell whether
1274 // a resource record is multicast or LocalOnly by just looking at the ResourceRecord because
1275 // LocalOnly records are truly identified by ARType in the AuthRecord. As P2P and LocalOnly record
1276 // are kept in the same hash table, we use the same function to make it easy for the callers when
1277 // they walk the hash table to answer LocalOnly/P2P questions
1279 mDNSexport mDNSBool
LocalOnlyRecordAnswersQuestion(AuthRecord
*const ar
, const DNSQuestion
*const q
)
1281 ResourceRecord
*rr
= &ar
->resrec
;
1283 // mDNSInterface_Any questions can be answered with LocalOnly/P2P records in this function. AuthRecord_Any
1284 // records are handled in ResourceRecordAnswersQuestion/SameNameRecordAnswersQuestion
1287 LogMsg("LocalOnlyRecordAnswersQuestion: ERROR!! called with regular AuthRecordAny %##s", rr
->name
->c
);
1291 // Questions with mDNSInterface_LocalOnly InterfaceID should be answered with all resource records that are
1292 // *local* to the machine. These include resource records that have InterfaceID set to mDNSInterface_LocalOnly,
1293 // mDNSInterface_Any and any other real InterfaceID. Hence, LocalOnly questions should not be checked against
1294 // the InterfaceID in the resource record.
1296 // mDNSInterface_Unicast does not indicate any scope and hence treat them like mDNSInterface_Any.
1298 if (rr
->InterfaceID
&&
1299 q
->InterfaceID
&& q
->InterfaceID
!= mDNSInterface_LocalOnly
&& q
->InterfaceID
!= mDNSInterface_Unicast
&&
1300 rr
->InterfaceID
!= q
->InterfaceID
) return(mDNSfalse
);
1302 // Entries in /etc/hosts are added as LocalOnly resource records. The LocalOnly resource records
1303 // may have a scope e.g., fe80::1%en0. The question may be scoped or not: the InterfaceID may be set
1304 // to mDNSInterface_Any, mDNSInterface_LocalOnly or a real InterfaceID (scoped).
1306 // 1) Question: Any, LocalOnly Record: no scope. This question should be answered with this record.
1308 // 2) Question: Any, LocalOnly Record: scoped. This question should be answered with the record because
1309 // traditionally applications never specify scope e.g., getaddrinfo, but need to be able
1310 // to get to /etc/hosts entries.
1312 // 3) Question: Scoped (LocalOnly or InterfaceID), LocalOnly Record: no scope. This is the inverse of (2).
1313 // If we register a LocalOnly record, we need to answer a LocalOnly question. If the /etc/hosts has a
1314 // non scoped entry, it may not make sense to answer a scoped question. But we can't tell these two
1315 // cases apart. As we currently answer LocalOnly question with LocalOnly record, we continue to do so.
1317 // 4) Question: Scoped (LocalOnly or InterfaceID), LocalOnly Record: scoped. LocalOnly questions should be
1318 // answered with any resource record where as if it has a valid InterfaceID, the scope should match.
1320 // (1) and (2) is bypassed because we check for a non-NULL InterfaceID above. For (3), the InterfaceID is NULL
1321 // and hence bypassed above. For (4) we bypassed LocalOnly questions and checked the scope of the record
1322 // against the question.
1324 // For P2P, InterfaceIDs of the question and the record should match.
1326 // If ResourceRecord received via multicast, but question was unicast, then shouldn't use record to answer this question.
1327 // LocalOnly authoritative answers are exempt. LocalOnly authoritative answers are used for /etc/host entries.
1328 // We don't want a local process to be able to create a fake LocalOnly address record for "www.bigbank.com" which would then
1329 // cause other applications (e.g. Safari) to connect to the wrong address. The rpc to register records filters out records
1330 // with names that don't end in local and have mDNSInterface_LocalOnly set.
1332 // Note: The check is bypassed for LocalOnly and for P2P it is not needed as only .local records are registered and for
1333 // a question to match its names, it also has to end in .local and that question can't be a unicast question (See
1334 // Question_uDNS macro and its usage). As P2P does not enforce .local only registrations we still make this check
1335 // and also makes it future proof.
1337 if (ar
->ARType
!= AuthRecordLocalOnly
&& rr
->InterfaceID
&& !mDNSOpaque16IsZero(q
->TargetQID
)) return(mDNSfalse
);
1339 // RR type CNAME matches any query type. QTYPE ANY matches any RR type. QCLASS ANY matches any RR class.
1340 if (!RRTypeAnswersQuestionType(rr
,q
->qtype
)) return(mDNSfalse
);
1341 if (rr
->rrclass
!= q
->qclass
&& q
->qclass
!= kDNSQClass_ANY
) return(mDNSfalse
);
1343 return(rr
->namehash
== q
->qnamehash
&& SameDomainName(rr
->name
, &q
->qname
));
1346 mDNSexport mDNSBool
AnyTypeRecordAnswersQuestion(const ResourceRecord
*const rr
, const DNSQuestion
*const q
)
1348 // LocalOnly/P2P questions can be answered with AuthRecordAny in this function. LocalOnly/P2P records
1349 // are handled in LocalOnlyRecordAnswersQuestion
1350 if ((rr
->InterfaceID
== mDNSInterface_LocalOnly
) || (rr
->InterfaceID
== mDNSInterface_P2P
))
1352 LogMsg("AnyTypeRecordAnswersQuestion: ERROR!! called with LocalOnly ResourceRecord %p, Question %p", rr
->InterfaceID
, q
->InterfaceID
);
1355 if (rr
->InterfaceID
&&
1356 q
->InterfaceID
&& q
->InterfaceID
!= mDNSInterface_LocalOnly
&&
1357 rr
->InterfaceID
!= q
->InterfaceID
) return(mDNSfalse
);
1359 // Resource record received via unicast, the DNSServer entries should match ?
1360 // Note that Auth Records are normally setup with NULL InterfaceID and
1361 // both the DNSServers are assumed to be NULL in that case
1362 if (!rr
->InterfaceID
&& rr
->rDNSServer
!= q
->qDNSServer
) return(mDNSfalse
);
1364 // If ResourceRecord received via multicast, but question was unicast, then shouldn't use record to answer this question
1365 if (rr
->InterfaceID
&& !mDNSOpaque16IsZero(q
->TargetQID
)) return(mDNSfalse
);
1367 if (rr
->rrclass
!= q
->qclass
&& q
->qclass
!= kDNSQClass_ANY
) return(mDNSfalse
);
1369 return(rr
->namehash
== q
->qnamehash
&& SameDomainName(rr
->name
, &q
->qname
));
1372 // This is called only when the caller knows that it is a Unicast Resource Record and it is a Unicast Question
1373 // and hence we don't need InterfaceID checks like above. Though this may not be a big optimization, the main
1374 // reason we need this is that we can't compare DNSServers between the question and the resource record because
1375 // the resource record may not be completely initialized e.g., mDNSCoreReceiveResponse
1376 mDNSexport mDNSBool
UnicastResourceRecordAnswersQuestion(const ResourceRecord
*const rr
, const DNSQuestion
*const q
)
1378 // RR type CNAME matches any query type. QTYPE ANY matches any RR type. QCLASS ANY matches any RR class.
1379 if (!RRTypeAnswersQuestionType(rr
,q
->qtype
)) return(mDNSfalse
);
1381 if (rr
->rrclass
!= q
->qclass
&& q
->qclass
!= kDNSQClass_ANY
) return(mDNSfalse
);
1383 return(rr
->namehash
== q
->qnamehash
&& SameDomainName(rr
->name
, &q
->qname
));
1386 mDNSexport mDNSu16
GetRDLength(const ResourceRecord
*const rr
, mDNSBool estimate
)
1388 const RDataBody2
*const rd
= (RDataBody2
*)rr
->rdata
->u
.data
;
1389 const domainname
*const name
= estimate
? rr
->name
: mDNSNULL
;
1390 if (rr
->rrclass
== kDNSQClass_ANY
) return(rr
->rdlength
); // Used in update packets to mean "Delete An RRset" (RFC 2136)
1391 else switch (rr
->rrtype
)
1393 case kDNSType_A
: return(sizeof(rd
->ipv4
));
1396 case kDNSType_CNAME
:
1398 case kDNSType_DNAME
:return(CompressedDomainNameLength(&rd
->name
, name
));
1400 case kDNSType_SOA
: return(mDNSu16
)(CompressedDomainNameLength(&rd
->soa
.mname
, name
) +
1401 CompressedDomainNameLength(&rd
->soa
.rname
, name
) +
1402 5 * sizeof(mDNSOpaque32
));
1410 case kDNSType_DHCID
:return(rr
->rdlength
); // Not self-describing, so have to just trust rdlength
1412 case kDNSType_HINFO
:return(mDNSu16
)(2 + (int)rd
->data
[0] + (int)rd
->data
[1 + (int)rd
->data
[0]]);
1415 case kDNSType_AFSDB
:
1417 case kDNSType_KX
: return(mDNSu16
)(2 + CompressedDomainNameLength(&rd
->mx
.exchange
, name
));
1419 case kDNSType_RP
: return(mDNSu16
)(CompressedDomainNameLength(&rd
->rp
.mbox
, name
) +
1420 CompressedDomainNameLength(&rd
->rp
.txt
, name
));
1422 case kDNSType_PX
: return(mDNSu16
)(2 + CompressedDomainNameLength(&rd
->px
.map822
, name
) +
1423 CompressedDomainNameLength(&rd
->px
.mapx400
, name
));
1425 case kDNSType_AAAA
: return(sizeof(rd
->ipv6
));
1427 case kDNSType_SRV
: return(mDNSu16
)(6 + CompressedDomainNameLength(&rd
->srv
.target
, name
));
1429 case kDNSType_OPT
: return(rr
->rdlength
);
1431 case kDNSType_NSEC
: {
1433 for (i
=sizeof(rdataNSEC
); i
>0; i
--) if (rd
->nsec
.bitmap
[i
-1]) break;
1434 // For our simplified use of NSEC synthetic records:
1435 // nextname is always the record's own name,
1436 // and if we have at least one record type that exists,
1437 // - the block number is always 0,
1438 // - the count byte is a value in the range 1-32,
1439 // - followed by the 1-32 data bytes
1440 return(mDNSu16
)((estimate
? 2 : DomainNameLength(rr
->name
)) + (i
? (2 + i
) : 0));
1443 default: debugf("Warning! Don't know how to get length of resource type %d", rr
->rrtype
);
1444 return(rr
->rdlength
);
1448 // When a local client registers (or updates) a record, we use this routine to do some simple validation checks
1449 // to help reduce the risk of bogus malformed data on the network
1450 mDNSexport mDNSBool
ValidateRData(const mDNSu16 rrtype
, const mDNSu16 rdlength
, const RData
*const rd
)
1456 case kDNSType_A
: return(rdlength
== sizeof(mDNSv4Addr
));
1458 case kDNSType_NS
: // Same as PTR
1459 case kDNSType_MD
: // Same as PTR
1460 case kDNSType_MF
: // Same as PTR
1461 case kDNSType_CNAME
:// Same as PTR
1462 //case kDNSType_SOA not checked
1463 case kDNSType_MB
: // Same as PTR
1464 case kDNSType_MG
: // Same as PTR
1465 case kDNSType_MR
: // Same as PTR
1466 //case kDNSType_NULL not checked (no specified format, so always valid)
1467 //case kDNSType_WKS not checked
1468 case kDNSType_PTR
: len
= DomainNameLengthLimit(&rd
->u
.name
, rd
->u
.data
+ rdlength
);
1469 return(len
<= MAX_DOMAIN_NAME
&& rdlength
== len
);
1471 case kDNSType_HINFO
:// Same as TXT (roughly)
1472 case kDNSType_MINFO
:// Same as TXT (roughly)
1473 case kDNSType_TXT
: if (!rdlength
) return(mDNSfalse
); // TXT record has to be at least one byte (RFC 1035)
1475 const mDNSu8
*ptr
= rd
->u
.txt
.c
;
1476 const mDNSu8
*end
= rd
->u
.txt
.c
+ rdlength
;
1477 while (ptr
< end
) ptr
+= 1 + ptr
[0];
1478 return (ptr
== end
);
1481 case kDNSType_AAAA
: return(rdlength
== sizeof(mDNSv6Addr
));
1483 case kDNSType_MX
: // Must be at least two-byte preference, plus domainname
1484 // Call to DomainNameLengthLimit() implicitly enforces both requirements for us
1485 len
= DomainNameLengthLimit(&rd
->u
.mx
.exchange
, rd
->u
.data
+ rdlength
);
1486 return(len
<= MAX_DOMAIN_NAME
&& rdlength
== 2+len
);
1488 case kDNSType_SRV
: // Must be at least priority+weight+port, plus domainname
1489 // Call to DomainNameLengthLimit() implicitly enforces both requirements for us
1490 len
= DomainNameLengthLimit(&rd
->u
.srv
.target
, rd
->u
.data
+ rdlength
);
1491 return(len
<= MAX_DOMAIN_NAME
&& rdlength
== 6+len
);
1493 //case kDNSType_NSEC not checked
1495 default: return(mDNStrue
); // Allow all other types without checking
1499 // ***************************************************************************
1500 #if COMPILER_LIKES_PRAGMA_MARK
1502 #pragma mark - DNS Message Creation Functions
1505 mDNSexport
void InitializeDNSMessage(DNSMessageHeader
*h
, mDNSOpaque16 id
, mDNSOpaque16 flags
)
1509 h
->numQuestions
= 0;
1511 h
->numAuthorities
= 0;
1512 h
->numAdditionals
= 0;
1515 mDNSexport
const mDNSu8
*FindCompressionPointer(const mDNSu8
*const base
, const mDNSu8
*const end
, const mDNSu8
*const domname
)
1517 const mDNSu8
*result
= end
- *domname
- 1;
1519 if (*domname
== 0) return(mDNSNULL
); // There's no point trying to match just the root label
1521 // This loop examines each possible starting position in packet, starting end of the packet and working backwards
1522 while (result
>= base
)
1524 // If the length byte and first character of the label match, then check further to see
1525 // if this location in the packet will yield a useful name compression pointer.
1526 if (result
[0] == domname
[0] && result
[1] == domname
[1])
1528 const mDNSu8
*name
= domname
;
1529 const mDNSu8
*targ
= result
;
1530 while (targ
+ *name
< end
)
1532 // First see if this label matches
1534 const mDNSu8
*pointertarget
;
1535 for (i
=0; i
<= *name
; i
++) if (targ
[i
] != name
[i
]) break;
1536 if (i
<= *name
) break; // If label did not match, bail out
1537 targ
+= 1 + *name
; // Else, did match, so advance target pointer
1538 name
+= 1 + *name
; // and proceed to check next label
1539 if (*name
== 0 && *targ
== 0) return(result
); // If no more labels, we found a match!
1540 if (*name
== 0) break; // If no more labels to match, we failed, so bail out
1542 // The label matched, so now follow the pointer (if appropriate) and then see if the next label matches
1543 if (targ
[0] < 0x40) continue; // If length value, continue to check next label
1544 if (targ
[0] < 0xC0) break; // If 40-BF, not valid
1545 if (targ
+1 >= end
) break; // Second byte not present!
1546 pointertarget
= base
+ (((mDNSu16
)(targ
[0] & 0x3F)) << 8) + targ
[1];
1547 if (targ
< pointertarget
) break; // Pointertarget must point *backwards* in the packet
1548 if (pointertarget
[0] >= 0x40) break; // Pointertarget must point to a valid length byte
1549 targ
= pointertarget
;
1552 result
--; // We failed to match at this search position, so back up the tentative result pointer and try again
1557 // Put a string of dot-separated labels as length-prefixed labels
1558 // domainname is a fully-qualified name (i.e. assumed to be ending in a dot, even if it doesn't)
1559 // msg points to the message we're building (pass mDNSNULL if we don't want to use compression pointers)
1560 // end points to the end of the message so far
1561 // ptr points to where we want to put the name
1562 // limit points to one byte past the end of the buffer that we must not overrun
1563 // domainname is the name to put
1564 mDNSexport mDNSu8
*putDomainNameAsLabels(const DNSMessage
*const msg
,
1565 mDNSu8
*ptr
, const mDNSu8
*const limit
, const domainname
*const name
)
1567 const mDNSu8
*const base
= (const mDNSu8
*)msg
;
1568 const mDNSu8
* np
= name
->c
;
1569 const mDNSu8
*const max
= name
->c
+ MAX_DOMAIN_NAME
; // Maximum that's valid
1570 const mDNSu8
* pointer
= mDNSNULL
;
1571 const mDNSu8
*const searchlimit
= ptr
;
1573 if (!ptr
) { LogMsg("putDomainNameAsLabels %##s ptr is null", name
->c
); return(mDNSNULL
); }
1575 if (!*np
) // If just writing one-byte root label, make sure we have space for that
1577 if (ptr
>= limit
) return(mDNSNULL
);
1579 else // else, loop through writing labels and/or a compression offset
1582 if (*np
> MAX_DOMAIN_LABEL
)
1583 { LogMsg("Malformed domain name %##s (label more than 63 bytes)", name
->c
); return(mDNSNULL
); }
1585 // This check correctly allows for the final trailing root label:
1587 // Suppose our domain name is exactly 256 bytes long, including the final trailing root label.
1588 // Suppose np is now at name->c[249], and we're about to write our last non-null label ("local").
1589 // We know that max will be at name->c[256]
1590 // That means that np + 1 + 5 == max - 1, so we (just) pass the "if" test below, write our
1591 // six bytes, then exit the loop, write the final terminating root label, and the domain
1592 // name we've written is exactly 256 bytes long, exactly at the correct legal limit.
1593 // If the name is one byte longer, then we fail the "if" test below, and correctly bail out.
1594 if (np
+ 1 + *np
>= max
)
1595 { LogMsg("Malformed domain name %##s (more than 256 bytes)", name
->c
); return(mDNSNULL
); }
1597 if (base
) pointer
= FindCompressionPointer(base
, searchlimit
, np
);
1598 if (pointer
) // Use a compression pointer if we can
1600 const mDNSu16 offset
= (mDNSu16
)(pointer
- base
);
1601 if (ptr
+2 > limit
) return(mDNSNULL
); // If we don't have two bytes of space left, give up
1602 *ptr
++ = (mDNSu8
)(0xC0 | (offset
>> 8));
1603 *ptr
++ = (mDNSu8
)( offset
& 0xFF);
1606 else // Else copy one label and try again
1610 // If we don't at least have enough space for this label *plus* a terminating zero on the end, give up
1611 if (ptr
+ 1 + len
>= limit
) return(mDNSNULL
);
1613 for (i
=0; i
<len
; i
++) *ptr
++ = *np
++;
1615 } while (*np
); // While we've got characters remaining in the name, continue
1618 *ptr
++ = 0; // Put the final root label
1622 mDNSlocal mDNSu8
*putVal16(mDNSu8
*ptr
, mDNSu16 val
)
1624 ptr
[0] = (mDNSu8
)((val
>> 8 ) & 0xFF);
1625 ptr
[1] = (mDNSu8
)((val
) & 0xFF);
1626 return ptr
+ sizeof(mDNSOpaque16
);
1629 mDNSlocal mDNSu8
*putVal32(mDNSu8
*ptr
, mDNSu32 val
)
1631 ptr
[0] = (mDNSu8
)((val
>> 24) & 0xFF);
1632 ptr
[1] = (mDNSu8
)((val
>> 16) & 0xFF);
1633 ptr
[2] = (mDNSu8
)((val
>> 8) & 0xFF);
1634 ptr
[3] = (mDNSu8
)((val
) & 0xFF);
1635 return ptr
+ sizeof(mDNSu32
);
1638 // msg points to the message we're building (pass mDNSNULL if we don't want to use compression pointers)
1639 mDNSexport mDNSu8
*putRData(const DNSMessage
*const msg
, mDNSu8
*ptr
, const mDNSu8
*const limit
, const ResourceRecord
*const rr
)
1641 const RDataBody2
*const rdb
= (RDataBody2
*)rr
->rdata
->u
.data
;
1644 case kDNSType_A
: if (rr
->rdlength
!= 4)
1645 { debugf("putRData: Illegal length %d for kDNSType_A", rr
->rdlength
); return(mDNSNULL
); }
1646 if (ptr
+ 4 > limit
) return(mDNSNULL
);
1647 *ptr
++ = rdb
->ipv4
.b
[0];
1648 *ptr
++ = rdb
->ipv4
.b
[1];
1649 *ptr
++ = rdb
->ipv4
.b
[2];
1650 *ptr
++ = rdb
->ipv4
.b
[3];
1654 case kDNSType_CNAME
:
1656 case kDNSType_DNAME
:return(putDomainNameAsLabels(msg
, ptr
, limit
, &rdb
->name
));
1658 case kDNSType_SOA
: ptr
= putDomainNameAsLabels(msg
, ptr
, limit
, &rdb
->soa
.mname
);
1659 if (!ptr
) return(mDNSNULL
);
1660 ptr
= putDomainNameAsLabels(msg
, ptr
, limit
, &rdb
->soa
.rname
);
1661 if (!ptr
|| ptr
+ 20 > limit
) return(mDNSNULL
);
1662 ptr
= putVal32(ptr
, rdb
->soa
.serial
);
1663 ptr
= putVal32(ptr
, rdb
->soa
.refresh
);
1664 ptr
= putVal32(ptr
, rdb
->soa
.retry
);
1665 ptr
= putVal32(ptr
, rdb
->soa
.expire
);
1666 ptr
= putVal32(ptr
, rdb
->soa
.min
);
1670 case kDNSType_HINFO
:
1676 case kDNSType_DHCID
:if (ptr
+ rr
->rdlength
> limit
) return(mDNSNULL
);
1677 mDNSPlatformMemCopy(ptr
, rdb
->data
, rr
->rdlength
);
1678 return(ptr
+ rr
->rdlength
);
1681 case kDNSType_AFSDB
:
1683 case kDNSType_KX
: if (ptr
+ 3 > limit
) return(mDNSNULL
);
1684 ptr
= putVal16(ptr
, rdb
->mx
.preference
);
1685 return(putDomainNameAsLabels(msg
, ptr
, limit
, &rdb
->mx
.exchange
));
1687 case kDNSType_RP
: ptr
= putDomainNameAsLabels(msg
, ptr
, limit
, &rdb
->rp
.mbox
);
1688 if (!ptr
) return(mDNSNULL
);
1689 ptr
= putDomainNameAsLabels(msg
, ptr
, limit
, &rdb
->rp
.txt
);
1692 case kDNSType_PX
: if (ptr
+ 5 > limit
) return(mDNSNULL
);
1693 ptr
= putVal16(ptr
, rdb
->px
.preference
);
1694 ptr
= putDomainNameAsLabels(msg
, ptr
, limit
, &rdb
->px
.map822
);
1695 if (!ptr
) return(mDNSNULL
);
1696 ptr
= putDomainNameAsLabels(msg
, ptr
, limit
, &rdb
->px
.mapx400
);
1699 case kDNSType_AAAA
: if (rr
->rdlength
!= sizeof(rdb
->ipv6
))
1700 { debugf("putRData: Illegal length %d for kDNSType_AAAA", rr
->rdlength
); return(mDNSNULL
); }
1701 if (ptr
+ sizeof(rdb
->ipv6
) > limit
) return(mDNSNULL
);
1702 mDNSPlatformMemCopy(ptr
, &rdb
->ipv6
, sizeof(rdb
->ipv6
));
1703 return(ptr
+ sizeof(rdb
->ipv6
));
1705 case kDNSType_SRV
: if (ptr
+ 7 > limit
) return(mDNSNULL
);
1706 *ptr
++ = (mDNSu8
)(rdb
->srv
.priority
>> 8);
1707 *ptr
++ = (mDNSu8
)(rdb
->srv
.priority
& 0xFF);
1708 *ptr
++ = (mDNSu8
)(rdb
->srv
.weight
>> 8);
1709 *ptr
++ = (mDNSu8
)(rdb
->srv
.weight
& 0xFF);
1710 *ptr
++ = rdb
->srv
.port
.b
[0];
1711 *ptr
++ = rdb
->srv
.port
.b
[1];
1712 return(putDomainNameAsLabels(msg
, ptr
, limit
, &rdb
->srv
.target
));
1714 case kDNSType_OPT
: {
1716 const rdataOPT
*opt
;
1717 const rdataOPT
*const end
= (const rdataOPT
*)&rr
->rdata
->u
.data
[rr
->rdlength
];
1718 for (opt
= &rr
->rdata
->u
.opt
[0]; opt
< end
; opt
++) len
+= DNSOpt_Data_Space(opt
);
1719 if (ptr
+ len
> limit
) { LogMsg("ERROR: putOptRData - out of space"); return mDNSNULL
; }
1721 for (opt
= &rr
->rdata
->u
.opt
[0]; opt
< end
; opt
++)
1723 const int space
= DNSOpt_Data_Space(opt
);
1724 ptr
= putVal16(ptr
, opt
->opt
);
1725 ptr
= putVal16(ptr
, (mDNSu16
)space
- 4);
1729 ptr
= putVal16(ptr
, opt
->u
.llq
.vers
);
1730 ptr
= putVal16(ptr
, opt
->u
.llq
.llqOp
);
1731 ptr
= putVal16(ptr
, opt
->u
.llq
.err
);
1732 mDNSPlatformMemCopy(ptr
, opt
->u
.llq
.id
.b
, 8); // 8-byte id
1734 ptr
= putVal32(ptr
, opt
->u
.llq
.llqlease
);
1737 ptr
= putVal32(ptr
, opt
->u
.updatelease
);
1740 *ptr
++ = opt
->u
.owner
.vers
;
1741 *ptr
++ = opt
->u
.owner
.seq
;
1742 mDNSPlatformMemCopy(ptr
, opt
->u
.owner
.HMAC
.b
, 6); // 6-byte Host identifier
1744 if (space
>= DNSOpt_OwnerData_ID_Wake_Space
)
1746 mDNSPlatformMemCopy(ptr
, opt
->u
.owner
.IMAC
.b
, 6); // 6-byte interface MAC
1748 if (space
> DNSOpt_OwnerData_ID_Wake_Space
)
1750 mDNSPlatformMemCopy(ptr
, opt
->u
.owner
.password
.b
, space
- DNSOpt_OwnerData_ID_Wake_Space
);
1751 ptr
+= space
- DNSOpt_OwnerData_ID_Wake_Space
;
1760 case kDNSType_NSEC
: {
1761 // For our simplified use of NSEC synthetic records:
1762 // nextname is always the record's own name,
1763 // the block number is always 0,
1764 // the count byte is a value in the range 1-32,
1765 // followed by the 1-32 data bytes
1767 for (i
=sizeof(rdataNSEC
); i
>0; i
--) if (rdb
->nsec
.bitmap
[i
-1]) break;
1768 ptr
= putDomainNameAsLabels(msg
, ptr
, limit
, rr
->name
);
1769 if (!ptr
) return(mDNSNULL
);
1770 if (i
) // Only put a block if at least one type exists for this name
1772 if (ptr
+ 2 + i
> limit
) return(mDNSNULL
);
1775 for (j
=0; j
<i
; j
++) *ptr
++ = rdb
->nsec
.bitmap
[j
];
1780 default: debugf("putRData: Warning! Writing unknown resource type %d as raw data", rr
->rrtype
);
1781 if (ptr
+ rr
->rdlength
> limit
) return(mDNSNULL
);
1782 mDNSPlatformMemCopy(ptr
, rdb
->data
, rr
->rdlength
);
1783 return(ptr
+ rr
->rdlength
);
1787 #define IsUnicastUpdate(X) (!mDNSOpaque16IsZero((X)->h.id) && ((X)->h.flags.b[0] & kDNSFlag0_OP_Mask) == kDNSFlag0_OP_Update)
1789 mDNSexport mDNSu8
*PutResourceRecordTTLWithLimit(DNSMessage
*const msg
, mDNSu8
*ptr
, mDNSu16
*count
, ResourceRecord
*rr
, mDNSu32 ttl
, const mDNSu8
*limit
)
1792 mDNSu16 actualLength
;
1793 // When sending SRV to conventional DNS server (i.e. in DNS update requests) we should not do name compression on the rdata (RFC 2782)
1794 const DNSMessage
*const rdatacompressionbase
= (IsUnicastUpdate(msg
) && rr
->rrtype
== kDNSType_SRV
) ? mDNSNULL
: msg
;
1796 if (rr
->RecordType
== kDNSRecordTypeUnregistered
)
1798 LogMsg("PutResourceRecord ERROR! Attempt to put kDNSRecordTypeUnregistered %##s (%s)", rr
->name
->c
, DNSTypeName(rr
->rrtype
));
1802 if (!ptr
) { LogMsg("PutResourceRecordTTLWithLimit ptr is null"); return(mDNSNULL
); }
1804 ptr
= putDomainNameAsLabels(msg
, ptr
, limit
, rr
->name
);
1805 if (!ptr
|| ptr
+ 10 >= limit
) return(mDNSNULL
); // If we're out-of-space, return mDNSNULL
1806 ptr
[0] = (mDNSu8
)(rr
->rrtype
>> 8);
1807 ptr
[1] = (mDNSu8
)(rr
->rrtype
& 0xFF);
1808 ptr
[2] = (mDNSu8
)(rr
->rrclass
>> 8);
1809 ptr
[3] = (mDNSu8
)(rr
->rrclass
& 0xFF);
1810 ptr
[4] = (mDNSu8
)((ttl
>> 24) & 0xFF);
1811 ptr
[5] = (mDNSu8
)((ttl
>> 16) & 0xFF);
1812 ptr
[6] = (mDNSu8
)((ttl
>> 8) & 0xFF);
1813 ptr
[7] = (mDNSu8
)( ttl
& 0xFF);
1814 // ptr[8] and ptr[9] filled in *after* we find out how much space the rdata takes
1816 endofrdata
= putRData(rdatacompressionbase
, ptr
+10, limit
, rr
);
1817 if (!endofrdata
) { verbosedebugf("Ran out of space in PutResourceRecord for %##s (%s)", rr
->name
->c
, DNSTypeName(rr
->rrtype
)); return(mDNSNULL
); }
1819 // Go back and fill in the actual number of data bytes we wrote
1820 // (actualLength can be less than rdlength when domain name compression is used)
1821 actualLength
= (mDNSu16
)(endofrdata
- ptr
- 10);
1822 ptr
[8] = (mDNSu8
)(actualLength
>> 8);
1823 ptr
[9] = (mDNSu8
)(actualLength
& 0xFF);
1825 if (count
) (*count
)++;
1826 else LogMsg("PutResourceRecordTTL: ERROR: No target count to update for %##s (%s)", rr
->name
->c
, DNSTypeName(rr
->rrtype
));
1830 mDNSlocal mDNSu8
*putEmptyResourceRecord(DNSMessage
*const msg
, mDNSu8
*ptr
, const mDNSu8
*const limit
, mDNSu16
*count
, const AuthRecord
*rr
)
1832 ptr
= putDomainNameAsLabels(msg
, ptr
, limit
, rr
->resrec
.name
);
1833 if (!ptr
|| ptr
+ 10 > limit
) return(mDNSNULL
); // If we're out-of-space, return mDNSNULL
1834 ptr
[0] = (mDNSu8
)(rr
->resrec
.rrtype
>> 8); // Put type
1835 ptr
[1] = (mDNSu8
)(rr
->resrec
.rrtype
& 0xFF);
1836 ptr
[2] = (mDNSu8
)(rr
->resrec
.rrclass
>> 8); // Put class
1837 ptr
[3] = (mDNSu8
)(rr
->resrec
.rrclass
& 0xFF);
1838 ptr
[4] = ptr
[5] = ptr
[6] = ptr
[7] = 0; // TTL is zero
1839 ptr
[8] = ptr
[9] = 0; // RDATA length is zero
1844 mDNSexport mDNSu8
*putQuestion(DNSMessage
*const msg
, mDNSu8
*ptr
, const mDNSu8
*const limit
, const domainname
*const name
, mDNSu16 rrtype
, mDNSu16 rrclass
)
1846 ptr
= putDomainNameAsLabels(msg
, ptr
, limit
, name
);
1847 if (!ptr
|| ptr
+4 >= limit
) return(mDNSNULL
); // If we're out-of-space, return mDNSNULL
1848 ptr
[0] = (mDNSu8
)(rrtype
>> 8);
1849 ptr
[1] = (mDNSu8
)(rrtype
& 0xFF);
1850 ptr
[2] = (mDNSu8
)(rrclass
>> 8);
1851 ptr
[3] = (mDNSu8
)(rrclass
& 0xFF);
1852 msg
->h
.numQuestions
++;
1856 // for dynamic updates
1857 mDNSexport mDNSu8
*putZone(DNSMessage
*const msg
, mDNSu8
*ptr
, mDNSu8
*limit
, const domainname
*zone
, mDNSOpaque16 zoneClass
)
1859 ptr
= putDomainNameAsLabels(msg
, ptr
, limit
, zone
);
1860 if (!ptr
|| ptr
+ 4 > limit
) return mDNSNULL
; // If we're out-of-space, return NULL
1861 *ptr
++ = (mDNSu8
)(kDNSType_SOA
>> 8);
1862 *ptr
++ = (mDNSu8
)(kDNSType_SOA
& 0xFF);
1863 *ptr
++ = zoneClass
.b
[0];
1864 *ptr
++ = zoneClass
.b
[1];
1865 msg
->h
.mDNS_numZones
++;
1869 // for dynamic updates
1870 mDNSexport mDNSu8
*putPrereqNameNotInUse(const domainname
*const name
, DNSMessage
*const msg
, mDNSu8
*const ptr
, mDNSu8
*const end
)
1873 mDNS_SetupResourceRecord(&prereq
, mDNSNULL
, mDNSInterface_Any
, kDNSQType_ANY
, kStandardTTL
, 0, AuthRecordAny
, mDNSNULL
, mDNSNULL
);
1874 AssignDomainName(&prereq
.namestorage
, name
);
1875 prereq
.resrec
.rrtype
= kDNSQType_ANY
;
1876 prereq
.resrec
.rrclass
= kDNSClass_NONE
;
1877 return putEmptyResourceRecord(msg
, ptr
, end
, &msg
->h
.mDNS_numPrereqs
, &prereq
);
1880 // for dynamic updates
1881 mDNSexport mDNSu8
*putDeletionRecord(DNSMessage
*msg
, mDNSu8
*ptr
, ResourceRecord
*rr
)
1883 // deletion: specify record w/ TTL 0, class NONE
1884 const mDNSu16 origclass
= rr
->rrclass
;
1885 rr
->rrclass
= kDNSClass_NONE
;
1886 ptr
= PutResourceRecordTTLJumbo(msg
, ptr
, &msg
->h
.mDNS_numUpdates
, rr
, 0);
1887 rr
->rrclass
= origclass
;
1891 // for dynamic updates
1892 mDNSexport mDNSu8
*putDeletionRecordWithLimit(DNSMessage
*msg
, mDNSu8
*ptr
, ResourceRecord
*rr
, mDNSu8
*limit
)
1894 // deletion: specify record w/ TTL 0, class NONE
1895 const mDNSu16 origclass
= rr
->rrclass
;
1896 rr
->rrclass
= kDNSClass_NONE
;
1897 ptr
= PutResourceRecordTTLWithLimit(msg
, ptr
, &msg
->h
.mDNS_numUpdates
, rr
, 0, limit
);
1898 rr
->rrclass
= origclass
;
1902 mDNSexport mDNSu8
*putDeleteRRSetWithLimit(DNSMessage
*msg
, mDNSu8
*ptr
, const domainname
*name
, mDNSu16 rrtype
, mDNSu8
*limit
)
1904 mDNSu16
class = kDNSQClass_ANY
;
1906 ptr
= putDomainNameAsLabels(msg
, ptr
, limit
, name
);
1907 if (!ptr
|| ptr
+ 10 >= limit
) return mDNSNULL
; // If we're out-of-space, return mDNSNULL
1908 ptr
[0] = (mDNSu8
)(rrtype
>> 8);
1909 ptr
[1] = (mDNSu8
)(rrtype
& 0xFF);
1910 ptr
[2] = (mDNSu8
)(class >> 8);
1911 ptr
[3] = (mDNSu8
)(class & 0xFF);
1912 ptr
[4] = ptr
[5] = ptr
[6] = ptr
[7] = 0; // zero ttl
1913 ptr
[8] = ptr
[9] = 0; // zero rdlength/rdata
1915 msg
->h
.mDNS_numUpdates
++;
1919 // for dynamic updates
1920 mDNSexport mDNSu8
*putDeleteAllRRSets(DNSMessage
*msg
, mDNSu8
*ptr
, const domainname
*name
)
1922 const mDNSu8
*limit
= msg
->data
+ AbsoluteMaxDNSMessageData
;
1923 mDNSu16
class = kDNSQClass_ANY
;
1924 mDNSu16 rrtype
= kDNSQType_ANY
;
1926 ptr
= putDomainNameAsLabels(msg
, ptr
, limit
, name
);
1927 if (!ptr
|| ptr
+ 10 >= limit
) return mDNSNULL
; // If we're out-of-space, return mDNSNULL
1928 ptr
[0] = (mDNSu8
)(rrtype
>> 8);
1929 ptr
[1] = (mDNSu8
)(rrtype
& 0xFF);
1930 ptr
[2] = (mDNSu8
)(class >> 8);
1931 ptr
[3] = (mDNSu8
)(class & 0xFF);
1932 ptr
[4] = ptr
[5] = ptr
[6] = ptr
[7] = 0; // zero ttl
1933 ptr
[8] = ptr
[9] = 0; // zero rdlength/rdata
1935 msg
->h
.mDNS_numUpdates
++;
1939 // for dynamic updates
1940 mDNSexport mDNSu8
*putUpdateLease(DNSMessage
*msg
, mDNSu8
*end
, mDNSu32 lease
)
1943 mDNS_SetupResourceRecord(&rr
, mDNSNULL
, mDNSInterface_Any
, kDNSType_OPT
, kStandardTTL
, kDNSRecordTypeKnownUnique
, AuthRecordAny
, mDNSNULL
, mDNSNULL
);
1944 rr
.resrec
.rrclass
= NormalMaxDNSMessageData
;
1945 rr
.resrec
.rdlength
= sizeof(rdataOPT
); // One option in this OPT record
1946 rr
.resrec
.rdestimate
= sizeof(rdataOPT
);
1947 rr
.resrec
.rdata
->u
.opt
[0].opt
= kDNSOpt_Lease
;
1948 rr
.resrec
.rdata
->u
.opt
[0].u
.updatelease
= lease
;
1949 end
= PutResourceRecordTTLJumbo(msg
, end
, &msg
->h
.numAdditionals
, &rr
.resrec
, 0);
1950 if (!end
) { LogMsg("ERROR: putUpdateLease - PutResourceRecordTTL"); return mDNSNULL
; }
1954 // for dynamic updates
1955 mDNSexport mDNSu8
*putUpdateLeaseWithLimit(DNSMessage
*msg
, mDNSu8
*end
, mDNSu32 lease
, mDNSu8
*limit
)
1958 mDNS_SetupResourceRecord(&rr
, mDNSNULL
, mDNSInterface_Any
, kDNSType_OPT
, kStandardTTL
, kDNSRecordTypeKnownUnique
, AuthRecordAny
, mDNSNULL
, mDNSNULL
);
1959 rr
.resrec
.rrclass
= NormalMaxDNSMessageData
;
1960 rr
.resrec
.rdlength
= sizeof(rdataOPT
); // One option in this OPT record
1961 rr
.resrec
.rdestimate
= sizeof(rdataOPT
);
1962 rr
.resrec
.rdata
->u
.opt
[0].opt
= kDNSOpt_Lease
;
1963 rr
.resrec
.rdata
->u
.opt
[0].u
.updatelease
= lease
;
1964 end
= PutResourceRecordTTLWithLimit(msg
, end
, &msg
->h
.numAdditionals
, &rr
.resrec
, 0, limit
);
1965 if (!end
) { LogMsg("ERROR: putUpdateLease - PutResourceRecordTTLWithLimit"); return mDNSNULL
; }
1969 mDNSexport mDNSu8
*putHINFO(const mDNS
*const m
, DNSMessage
*const msg
, mDNSu8
*end
, DomainAuthInfo
*authInfo
, mDNSu8
*limit
)
1971 if (authInfo
&& authInfo
->AutoTunnel
)
1974 mDNSu8
*h
= hinfo
.rdatastorage
.u
.data
;
1975 mDNSu16 len
= 2 + m
->HIHardware
.c
[0] + m
->HISoftware
.c
[0];
1977 mDNS_SetupResourceRecord(&hinfo
, mDNSNULL
, mDNSInterface_Any
, kDNSType_HINFO
, 0, kDNSRecordTypeUnique
, AuthRecordAny
, mDNSNULL
, mDNSNULL
);
1978 AppendDomainLabel(&hinfo
.namestorage
, &m
->hostlabel
);
1979 AppendDomainName (&hinfo
.namestorage
, &authInfo
->domain
);
1980 hinfo
.resrec
.rroriginalttl
= 0;
1981 mDNSPlatformMemCopy(h
, &m
->HIHardware
, 1 + (mDNSu32
)m
->HIHardware
.c
[0]);
1983 mDNSPlatformMemCopy(h
, &m
->HISoftware
, 1 + (mDNSu32
)m
->HISoftware
.c
[0]);
1984 hinfo
.resrec
.rdlength
= len
;
1985 hinfo
.resrec
.rdestimate
= len
;
1986 newptr
= PutResourceRecordTTLWithLimit(msg
, end
, &msg
->h
.numAdditionals
, &hinfo
.resrec
, 0, limit
);
1993 // ***************************************************************************
1994 #if COMPILER_LIKES_PRAGMA_MARK
1996 #pragma mark - DNS Message Parsing Functions
1999 mDNSexport mDNSu32
DomainNameHashValue(const domainname
*const name
)
2004 for (c
= name
->c
; c
[0] != 0 && c
[1] != 0; c
+= 2)
2006 sum
+= ((mDNSIsUpperCase(c
[0]) ? c
[0] + 'a' - 'A' : c
[0]) << 8) |
2007 (mDNSIsUpperCase(c
[1]) ? c
[1] + 'a' - 'A' : c
[1]);
2008 sum
= (sum
<<3) | (sum
>>29);
2010 if (c
[0]) sum
+= ((mDNSIsUpperCase(c
[0]) ? c
[0] + 'a' - 'A' : c
[0]) << 8);
2014 mDNSexport
void SetNewRData(ResourceRecord
*const rr
, RData
*NewRData
, mDNSu16 rdlength
)
2019 rr
->rdata
= NewRData
;
2020 rr
->rdlength
= rdlength
;
2022 // Must not try to get target pointer until after updating rr->rdata
2023 target
= GetRRDomainNameTarget(rr
);
2024 rr
->rdlength
= GetRDLength(rr
, mDNSfalse
);
2025 rr
->rdestimate
= GetRDLength(rr
, mDNStrue
);
2026 rr
->rdatahash
= target
? DomainNameHashValue(target
) : RDataHashValue(rr
);
2029 mDNSexport
const mDNSu8
*skipDomainName(const DNSMessage
*const msg
, const mDNSu8
*ptr
, const mDNSu8
*const end
)
2033 if (ptr
< (mDNSu8
*)msg
|| ptr
>= end
)
2034 { debugf("skipDomainName: Illegal ptr not within packet boundaries"); return(mDNSNULL
); }
2036 while (1) // Read sequence of labels
2038 const mDNSu8 len
= *ptr
++; // Read length of this label
2039 if (len
== 0) return(ptr
); // If length is zero, that means this name is complete
2042 case 0x00: if (ptr
+ len
>= end
) // Remember: expect at least one more byte for the root label
2043 { debugf("skipDomainName: Malformed domain name (overruns packet end)"); return(mDNSNULL
); }
2044 if (total
+ 1 + len
>= MAX_DOMAIN_NAME
) // Remember: expect at least one more byte for the root label
2045 { debugf("skipDomainName: Malformed domain name (more than 256 characters)"); return(mDNSNULL
); }
2050 case 0x40: debugf("skipDomainName: Extended EDNS0 label types 0x%X not supported", len
); return(mDNSNULL
);
2051 case 0x80: debugf("skipDomainName: Illegal label length 0x%X", len
); return(mDNSNULL
);
2052 case 0xC0: return(ptr
+1);
2057 // Routine to fetch an FQDN from the DNS message, following compression pointers if necessary.
2058 mDNSexport
const mDNSu8
*getDomainName(const DNSMessage
*const msg
, const mDNSu8
*ptr
, const mDNSu8
*const end
,
2059 domainname
*const name
)
2061 const mDNSu8
*nextbyte
= mDNSNULL
; // Record where we got to before we started following pointers
2062 mDNSu8
*np
= name
->c
; // Name pointer
2063 const mDNSu8
*const limit
= np
+ MAX_DOMAIN_NAME
; // Limit so we don't overrun buffer
2065 if (ptr
< (mDNSu8
*)msg
|| ptr
>= end
)
2066 { debugf("getDomainName: Illegal ptr not within packet boundaries"); return(mDNSNULL
); }
2068 *np
= 0; // Tentatively place the root label here (may be overwritten if we have more labels)
2070 while (1) // Read sequence of labels
2072 const mDNSu8 len
= *ptr
++; // Read length of this label
2073 if (len
== 0) break; // If length is zero, that means this name is complete
2079 case 0x00: if (ptr
+ len
>= end
) // Remember: expect at least one more byte for the root label
2080 { debugf("getDomainName: Malformed domain name (overruns packet end)"); return(mDNSNULL
); }
2081 if (np
+ 1 + len
>= limit
) // Remember: expect at least one more byte for the root label
2082 { debugf("getDomainName: Malformed domain name (more than 256 characters)"); return(mDNSNULL
); }
2084 for (i
=0; i
<len
; i
++) *np
++ = *ptr
++;
2085 *np
= 0; // Tentatively place the root label here (may be overwritten if we have more labels)
2088 case 0x40: debugf("getDomainName: Extended EDNS0 label types 0x%X not supported in name %##s", len
, name
->c
);
2091 case 0x80: debugf("getDomainName: Illegal label length 0x%X in domain name %##s", len
, name
->c
); return(mDNSNULL
);
2093 case 0xC0: offset
= (mDNSu16
)((((mDNSu16
)(len
& 0x3F)) << 8) | *ptr
++);
2094 if (!nextbyte
) nextbyte
= ptr
; // Record where we got to before we started following pointers
2095 ptr
= (mDNSu8
*)msg
+ offset
;
2096 if (ptr
< (mDNSu8
*)msg
|| ptr
>= end
)
2097 { debugf("getDomainName: Illegal compression pointer not within packet boundaries"); return(mDNSNULL
); }
2099 { debugf("getDomainName: Compression pointer must point to real label"); return(mDNSNULL
); }
2104 if (nextbyte
) return(nextbyte
);
2108 mDNSexport
const mDNSu8
*skipResourceRecord(const DNSMessage
*msg
, const mDNSu8
*ptr
, const mDNSu8
*end
)
2110 mDNSu16 pktrdlength
;
2112 ptr
= skipDomainName(msg
, ptr
, end
);
2113 if (!ptr
) { debugf("skipResourceRecord: Malformed RR name"); return(mDNSNULL
); }
2115 if (ptr
+ 10 > end
) { debugf("skipResourceRecord: Malformed RR -- no type/class/ttl/len!"); return(mDNSNULL
); }
2116 pktrdlength
= (mDNSu16
)((mDNSu16
)ptr
[8] << 8 | ptr
[9]);
2118 if (ptr
+ pktrdlength
> end
) { debugf("skipResourceRecord: RDATA exceeds end of packet"); return(mDNSNULL
); }
2120 return(ptr
+ pktrdlength
);
2123 mDNSexport
const mDNSu8
*GetLargeResourceRecord(mDNS
*const m
, const DNSMessage
*const msg
, const mDNSu8
*ptr
,
2124 const mDNSu8
*end
, const mDNSInterfaceID InterfaceID
, mDNSu8 RecordType
, LargeCacheRecord
*const largecr
)
2126 CacheRecord
*const rr
= &largecr
->r
;
2127 RDataBody2
*const rdb
= (RDataBody2
*)rr
->smallrdatastorage
.data
;
2128 mDNSu16 pktrdlength
;
2130 if (largecr
== &m
->rec
&& m
->rec
.r
.resrec
.RecordType
)
2132 LogMsg("GetLargeResourceRecord: m->rec appears to be already in use for %s", CRDisplayString(m
, &m
->rec
.r
));
2138 rr
->next
= mDNSNULL
;
2139 rr
->resrec
.name
= &largecr
->namestorage
;
2141 rr
->NextInKAList
= mDNSNULL
;
2142 rr
->TimeRcvd
= m
? m
->timenow
: 0;
2143 rr
->DelayDelivery
= 0;
2144 rr
->NextRequiredQuery
= m
? m
->timenow
: 0; // Will be updated to the real value when we call SetNextCacheCheckTimeForRecord()
2145 rr
->LastUsed
= m
? m
->timenow
: 0;
2146 rr
->CRActiveQuestion
= mDNSNULL
;
2147 rr
->UnansweredQueries
= 0;
2148 rr
->LastUnansweredTime
= 0;
2149 #if ENABLE_MULTI_PACKET_QUERY_SNOOPING
2150 rr
->MPUnansweredQ
= 0;
2151 rr
->MPLastUnansweredQT
= 0;
2152 rr
->MPUnansweredKA
= 0;
2153 rr
->MPExpectingKA
= mDNSfalse
;
2155 rr
->NextInCFList
= mDNSNULL
;
2157 rr
->resrec
.InterfaceID
= InterfaceID
;
2158 rr
->resrec
.rDNSServer
= mDNSNULL
;
2160 ptr
= getDomainName(msg
, ptr
, end
, &largecr
->namestorage
); // Will bail out correctly if ptr is NULL
2161 if (!ptr
) { debugf("GetLargeResourceRecord: Malformed RR name"); return(mDNSNULL
); }
2162 rr
->resrec
.namehash
= DomainNameHashValue(rr
->resrec
.name
);
2164 if (ptr
+ 10 > end
) { debugf("GetLargeResourceRecord: Malformed RR -- no type/class/ttl/len!"); return(mDNSNULL
); }
2166 rr
->resrec
.rrtype
= (mDNSu16
) ((mDNSu16
)ptr
[0] << 8 | ptr
[1]);
2167 rr
->resrec
.rrclass
= (mDNSu16
)(((mDNSu16
)ptr
[2] << 8 | ptr
[3]) & kDNSClass_Mask
);
2168 rr
->resrec
.rroriginalttl
= (mDNSu32
) ((mDNSu32
)ptr
[4] << 24 | (mDNSu32
)ptr
[5] << 16 | (mDNSu32
)ptr
[6] << 8 | ptr
[7]);
2169 if (rr
->resrec
.rroriginalttl
> 0x70000000UL
/ mDNSPlatformOneSecond
&& (mDNSs32
)rr
->resrec
.rroriginalttl
!= -1)
2170 rr
->resrec
.rroriginalttl
= 0x70000000UL
/ mDNSPlatformOneSecond
;
2171 // Note: We don't have to adjust m->NextCacheCheck here -- this is just getting a record into memory for
2172 // us to look at. If we decide to copy it into the cache, then we'll update m->NextCacheCheck accordingly.
2173 pktrdlength
= (mDNSu16
)((mDNSu16
)ptr
[8] << 8 | ptr
[9]);
2175 // If mDNS record has cache-flush bit set, we mark it unique
2176 // For uDNS records, all are implicitly deemed unique (a single DNS server is always
2177 // authoritative for the entire RRSet), unless this is a truncated response
2178 if (ptr
[2] & (kDNSClass_UniqueRRSet
>> 8) || (!InterfaceID
&& !(msg
->h
.flags
.b
[0] & kDNSFlag0_TC
)))
2179 RecordType
|= kDNSRecordTypePacketUniqueMask
;
2181 if (ptr
+ pktrdlength
> end
) { debugf("GetLargeResourceRecord: RDATA exceeds end of packet"); return(mDNSNULL
); }
2182 end
= ptr
+ pktrdlength
; // Adjust end to indicate the end of the rdata for this resource record
2184 rr
->resrec
.rdata
= (RData
*)&rr
->smallrdatastorage
;
2185 rr
->resrec
.rdata
->MaxRDLength
= MaximumRDSize
;
2187 if (!RecordType
) LogMsg("GetLargeResourceRecord: No RecordType for %##s", rr
->resrec
.name
->c
);
2189 // IMPORTANT: Any record type we understand and unpack into a structure containing domainnames needs to have corresponding
2190 // cases in SameRDataBody() and RDataHashValue() to do a semantic comparison (or checksum) of the structure instead of a blind
2191 // bitwise memory compare (or sum). This is because a domainname is a fixed size structure holding variable-length data.
2192 // Any bytes past the logical end of the name are undefined, and a blind bitwise memory compare may indicate that
2193 // two domainnames are different when semantically they are the same name and it's only the unused bytes that differ.
2194 if (rr
->resrec
.rrclass
== kDNSQClass_ANY
&& pktrdlength
== 0) // Used in update packets to mean "Delete An RRset" (RFC 2136)
2195 rr
->resrec
.rdlength
= 0;
2196 else switch (rr
->resrec
.rrtype
)
2198 case kDNSType_A
: if (pktrdlength
!= sizeof(mDNSv4Addr
)) goto fail
;
2199 rdb
->ipv4
.b
[0] = ptr
[0];
2200 rdb
->ipv4
.b
[1] = ptr
[1];
2201 rdb
->ipv4
.b
[2] = ptr
[2];
2202 rdb
->ipv4
.b
[3] = ptr
[3];
2206 case kDNSType_CNAME
:
2208 case kDNSType_DNAME
:ptr
= getDomainName(msg
, ptr
, end
, &rdb
->name
);
2209 if (ptr
!= end
) { debugf("GetLargeResourceRecord: Malformed CNAME/PTR RDATA name"); goto fail
; }
2210 //debugf("%##s PTR %##s rdlen %d", rr->resrec.name.c, rdb->name.c, pktrdlength);
2213 case kDNSType_SOA
: ptr
= getDomainName(msg
, ptr
, end
, &rdb
->soa
.mname
);
2214 if (!ptr
) { debugf("GetLargeResourceRecord: Malformed SOA RDATA mname"); goto fail
; }
2215 ptr
= getDomainName(msg
, ptr
, end
, &rdb
->soa
.rname
);
2216 if (!ptr
) { debugf("GetLargeResourceRecord: Malformed SOA RDATA rname"); goto fail
; }
2217 if (ptr
+ 0x14 != end
) { debugf("GetLargeResourceRecord: Malformed SOA RDATA"); goto fail
; }
2218 rdb
->soa
.serial
= (mDNSs32
) ((mDNSs32
)ptr
[0x00] << 24 | (mDNSs32
)ptr
[0x01] << 16 | (mDNSs32
)ptr
[0x02] << 8 | ptr
[0x03]);
2219 rdb
->soa
.refresh
= (mDNSu32
) ((mDNSu32
)ptr
[0x04] << 24 | (mDNSu32
)ptr
[0x05] << 16 | (mDNSu32
)ptr
[0x06] << 8 | ptr
[0x07]);
2220 rdb
->soa
.retry
= (mDNSu32
) ((mDNSu32
)ptr
[0x08] << 24 | (mDNSu32
)ptr
[0x09] << 16 | (mDNSu32
)ptr
[0x0A] << 8 | ptr
[0x0B]);
2221 rdb
->soa
.expire
= (mDNSu32
) ((mDNSu32
)ptr
[0x0C] << 24 | (mDNSu32
)ptr
[0x0D] << 16 | (mDNSu32
)ptr
[0x0E] << 8 | ptr
[0x0F]);
2222 rdb
->soa
.min
= (mDNSu32
) ((mDNSu32
)ptr
[0x10] << 24 | (mDNSu32
)ptr
[0x11] << 16 | (mDNSu32
)ptr
[0x12] << 8 | ptr
[0x13]);
2226 case kDNSType_HINFO
:
2232 case kDNSType_DHCID
:if (pktrdlength
> rr
->resrec
.rdata
->MaxRDLength
)
2234 debugf("GetLargeResourceRecord: %s rdata size (%d) exceeds storage (%d)",
2235 DNSTypeName(rr
->resrec
.rrtype
), pktrdlength
, rr
->resrec
.rdata
->MaxRDLength
);
2238 rr
->resrec
.rdlength
= pktrdlength
;
2239 mDNSPlatformMemCopy(rdb
->data
, ptr
, pktrdlength
);
2243 case kDNSType_AFSDB
:
2245 case kDNSType_KX
: if (pktrdlength
< 3) goto fail
; // Preference + domainname
2246 rdb
->mx
.preference
= (mDNSu16
)((mDNSu16
)ptr
[0] << 8 | ptr
[1]);
2247 ptr
= getDomainName(msg
, ptr
+2, end
, &rdb
->mx
.exchange
);
2248 if (ptr
!= end
) { debugf("GetLargeResourceRecord: Malformed MX name"); goto fail
; }
2249 //debugf("%##s SRV %##s rdlen %d", rr->resrec.name.c, rdb->srv.target.c, pktrdlength);
2252 case kDNSType_RP
: ptr
= getDomainName(msg
, ptr
, end
, &rdb
->rp
.mbox
); // Domainname + domainname
2253 if (!ptr
) { debugf("GetLargeResourceRecord: Malformed RP mbox"); goto fail
; }
2254 ptr
= getDomainName(msg
, ptr
, end
, &rdb
->rp
.txt
);
2255 if (ptr
!= end
) { debugf("GetLargeResourceRecord: Malformed RP txt"); goto fail
; }
2258 case kDNSType_PX
: if (pktrdlength
< 4) goto fail
; // Preference + domainname + domainname
2259 rdb
->px
.preference
= (mDNSu16
)((mDNSu16
)ptr
[0] << 8 | ptr
[1]);
2260 ptr
= getDomainName(msg
, ptr
, end
, &rdb
->px
.map822
);
2261 if (!ptr
) { debugf("GetLargeResourceRecord: Malformed PX map822"); goto fail
; }
2262 ptr
= getDomainName(msg
, ptr
, end
, &rdb
->px
.mapx400
);
2263 if (ptr
!= end
) { debugf("GetLargeResourceRecord: Malformed PX mapx400"); goto fail
; }
2266 case kDNSType_AAAA
: if (pktrdlength
!= sizeof(mDNSv6Addr
)) goto fail
;
2267 mDNSPlatformMemCopy(&rdb
->ipv6
, ptr
, sizeof(rdb
->ipv6
));
2270 case kDNSType_SRV
: if (pktrdlength
< 7) goto fail
; // Priority + weight + port + domainname
2271 rdb
->srv
.priority
= (mDNSu16
)((mDNSu16
)ptr
[0] << 8 | ptr
[1]);
2272 rdb
->srv
.weight
= (mDNSu16
)((mDNSu16
)ptr
[2] << 8 | ptr
[3]);
2273 rdb
->srv
.port
.b
[0] = ptr
[4];
2274 rdb
->srv
.port
.b
[1] = ptr
[5];
2275 ptr
= getDomainName(msg
, ptr
+6, end
, &rdb
->srv
.target
);
2276 if (ptr
!= end
) { debugf("GetLargeResourceRecord: Malformed SRV RDATA name"); goto fail
; }
2277 //debugf("%##s SRV %##s rdlen %d", rr->resrec.name.c, rdb->srv.target.c, pktrdlength);
2280 case kDNSType_OPT
: {
2281 rdataOPT
*opt
= rr
->resrec
.rdata
->u
.opt
;
2282 rr
->resrec
.rdlength
= 0;
2283 while (ptr
< end
&& (mDNSu8
*)(opt
+1) < &rr
->resrec
.rdata
->u
.data
[MaximumRDSize
])
2285 const rdataOPT
*const currentopt
= opt
;
2286 if (ptr
+ 4 > end
) { LogInfo("GetLargeResourceRecord: OPT RDATA ptr + 4 > end"); goto fail
; }
2287 opt
->opt
= (mDNSu16
)((mDNSu16
)ptr
[0] << 8 | ptr
[1]);
2288 opt
->optlen
= (mDNSu16
)((mDNSu16
)ptr
[2] << 8 | ptr
[3]);
2290 if (ptr
+ opt
->optlen
> end
) { LogInfo("GetLargeResourceRecord: ptr + opt->optlen > end"); goto fail
; }
2294 if (opt
->optlen
== DNSOpt_LLQData_Space
- 4)
2296 opt
->u
.llq
.vers
= (mDNSu16
)((mDNSu16
)ptr
[0] << 8 | ptr
[1]);
2297 opt
->u
.llq
.llqOp
= (mDNSu16
)((mDNSu16
)ptr
[2] << 8 | ptr
[3]);
2298 opt
->u
.llq
.err
= (mDNSu16
)((mDNSu16
)ptr
[4] << 8 | ptr
[5]);
2299 mDNSPlatformMemCopy(opt
->u
.llq
.id
.b
, ptr
+6, 8);
2300 opt
->u
.llq
.llqlease
= (mDNSu32
) ((mDNSu32
)ptr
[14] << 24 | (mDNSu32
)ptr
[15] << 16 | (mDNSu32
)ptr
[16] << 8 | ptr
[17]);
2301 if (opt
->u
.llq
.llqlease
> 0x70000000UL
/ mDNSPlatformOneSecond
)
2302 opt
->u
.llq
.llqlease
= 0x70000000UL
/ mDNSPlatformOneSecond
;
2307 if (opt
->optlen
== DNSOpt_LeaseData_Space
- 4)
2309 opt
->u
.updatelease
= (mDNSu32
) ((mDNSu32
)ptr
[0] << 24 | (mDNSu32
)ptr
[1] << 16 | (mDNSu32
)ptr
[2] << 8 | ptr
[3]);
2310 if (opt
->u
.updatelease
> 0x70000000UL
/ mDNSPlatformOneSecond
)
2311 opt
->u
.updatelease
= 0x70000000UL
/ mDNSPlatformOneSecond
;
2316 if (ValidOwnerLength(opt
->optlen
))
2318 opt
->u
.owner
.vers
= ptr
[0];
2319 opt
->u
.owner
.seq
= ptr
[1];
2320 mDNSPlatformMemCopy(opt
->u
.owner
.HMAC
.b
, ptr
+2, 6); // 6-byte MAC address
2321 mDNSPlatformMemCopy(opt
->u
.owner
.IMAC
.b
, ptr
+2, 6); // 6-byte MAC address
2322 opt
->u
.owner
.password
= zeroEthAddr
;
2323 if (opt
->optlen
>= DNSOpt_OwnerData_ID_Wake_Space
-4)
2325 mDNSPlatformMemCopy(opt
->u
.owner
.IMAC
.b
, ptr
+8, 6); // 6-byte MAC address
2326 // This mDNSPlatformMemCopy is safe because the ValidOwnerLength(opt->optlen) check above
2327 // ensures that opt->optlen is no more than DNSOpt_OwnerData_ID_Wake_PW6_Space - 4
2328 if (opt
->optlen
> DNSOpt_OwnerData_ID_Wake_Space
-4)
2329 mDNSPlatformMemCopy(opt
->u
.owner
.password
.b
, ptr
+14, opt
->optlen
- (DNSOpt_OwnerData_ID_Wake_Space
-4));
2335 ptr
+= currentopt
->optlen
;
2337 rr
->resrec
.rdlength
= (mDNSu16
)((mDNSu8
*)opt
- rr
->resrec
.rdata
->u
.data
);
2338 if (ptr
!= end
) { LogInfo("GetLargeResourceRecord: Malformed OptRdata"); goto fail
; }
2342 case kDNSType_NSEC
: {
2345 ptr
= getDomainName(msg
, ptr
, end
, &d
); // Ignored for our simplified use of NSEC synthetic records
2346 if (!ptr
) { LogInfo("GetLargeResourceRecord: Malformed NSEC nextname"); goto fail
; }
2347 mDNSPlatformMemZero(rdb
->nsec
.bitmap
, sizeof(rdb
->nsec
.bitmap
));
2350 if (*ptr
++ != 0) { debugf("GetLargeResourceRecord: We only handle block zero NSECs"); goto fail
; }
2352 if (i
> sizeof(rdataNSEC
)) { debugf("GetLargeResourceRecord: invalid block length %d", i
); goto fail
; }
2353 for (j
=0; j
<i
; j
++) rdb
->nsec
.bitmap
[j
] = *ptr
++;
2355 if (ptr
!= end
) { debugf("GetLargeResourceRecord: Malformed NSEC"); goto fail
; }
2359 default: if (pktrdlength
> rr
->resrec
.rdata
->MaxRDLength
)
2361 debugf("GetLargeResourceRecord: rdata %d (%s) size (%d) exceeds storage (%d)",
2362 rr
->resrec
.rrtype
, DNSTypeName(rr
->resrec
.rrtype
), pktrdlength
, rr
->resrec
.rdata
->MaxRDLength
);
2365 debugf("GetLargeResourceRecord: Warning! Reading resource type %d (%s) as opaque data",
2366 rr
->resrec
.rrtype
, DNSTypeName(rr
->resrec
.rrtype
));
2367 // Note: Just because we don't understand the record type, that doesn't
2368 // mean we fail. The DNS protocol specifies rdlength, so we can
2369 // safely skip over unknown records and ignore them.
2370 // We also grab a binary copy of the rdata anyway, since the caller
2371 // might know how to interpret it even if we don't.
2372 rr
->resrec
.rdlength
= pktrdlength
;
2373 mDNSPlatformMemCopy(rdb
->data
, ptr
, pktrdlength
);
2377 SetNewRData(&rr
->resrec
, mDNSNULL
, 0); // Sets rdlength, rdestimate, rdatahash for us
2379 // Success! Now fill in RecordType to show this record contains valid data
2380 rr
->resrec
.RecordType
= RecordType
;
2384 // If we were unable to parse the rdata in this record, we indicate that by
2385 // returing a 'kDNSRecordTypePacketNegative' record with rdlength set to zero
2386 rr
->resrec
.RecordType
= kDNSRecordTypePacketNegative
;
2387 rr
->resrec
.rdlength
= 0;
2388 rr
->resrec
.rdestimate
= 0;
2389 rr
->resrec
.rdatahash
= 0;
2393 mDNSexport
const mDNSu8
*skipQuestion(const DNSMessage
*msg
, const mDNSu8
*ptr
, const mDNSu8
*end
)
2395 ptr
= skipDomainName(msg
, ptr
, end
);
2396 if (!ptr
) { debugf("skipQuestion: Malformed domain name in DNS question section"); return(mDNSNULL
); }
2397 if (ptr
+4 > end
) { debugf("skipQuestion: Malformed DNS question section -- no query type and class!"); return(mDNSNULL
); }
2401 mDNSexport
const mDNSu8
*getQuestion(const DNSMessage
*msg
, const mDNSu8
*ptr
, const mDNSu8
*end
, const mDNSInterfaceID InterfaceID
,
2402 DNSQuestion
*question
)
2404 mDNSPlatformMemZero(question
, sizeof(*question
));
2405 question
->InterfaceID
= InterfaceID
;
2406 if (!InterfaceID
) question
->TargetQID
= onesID
; // In DNSQuestions we use TargetQID as the indicator of whether it's unicast or multicast
2407 ptr
= getDomainName(msg
, ptr
, end
, &question
->qname
);
2408 if (!ptr
) { debugf("Malformed domain name in DNS question section"); return(mDNSNULL
); }
2409 if (ptr
+4 > end
) { debugf("Malformed DNS question section -- no query type and class!"); return(mDNSNULL
); }
2411 question
->qnamehash
= DomainNameHashValue(&question
->qname
);
2412 question
->qtype
= (mDNSu16
)((mDNSu16
)ptr
[0] << 8 | ptr
[1]); // Get type
2413 question
->qclass
= (mDNSu16
)((mDNSu16
)ptr
[2] << 8 | ptr
[3]); // and class
2417 mDNSexport
const mDNSu8
*LocateAnswers(const DNSMessage
*const msg
, const mDNSu8
*const end
)
2420 const mDNSu8
*ptr
= msg
->data
;
2421 for (i
= 0; i
< msg
->h
.numQuestions
&& ptr
; i
++) ptr
= skipQuestion(msg
, ptr
, end
);
2425 mDNSexport
const mDNSu8
*LocateAuthorities(const DNSMessage
*const msg
, const mDNSu8
*const end
)
2428 const mDNSu8
*ptr
= LocateAnswers(msg
, end
);
2429 for (i
= 0; i
< msg
->h
.numAnswers
&& ptr
; i
++) ptr
= skipResourceRecord(msg
, ptr
, end
);
2433 mDNSexport
const mDNSu8
*LocateAdditionals(const DNSMessage
*const msg
, const mDNSu8
*const end
)
2436 const mDNSu8
*ptr
= LocateAuthorities(msg
, end
);
2437 for (i
= 0; i
< msg
->h
.numAuthorities
; i
++) ptr
= skipResourceRecord(msg
, ptr
, end
);
2441 mDNSexport
const mDNSu8
*LocateOptRR(const DNSMessage
*const msg
, const mDNSu8
*const end
, int minsize
)
2444 const mDNSu8
*ptr
= LocateAdditionals(msg
, end
);
2446 // Locate the OPT record.
2447 // According to RFC 2671, "One OPT pseudo-RR can be added to the additional data section of either a request or a response."
2448 // This implies that there may be *at most* one OPT record per DNS message, in the Additional Section,
2449 // but not necessarily the *last* entry in the Additional Section.
2450 for (i
= 0; ptr
&& i
< msg
->h
.numAdditionals
; i
++)
2452 if (ptr
+ DNSOpt_Header_Space
+ minsize
<= end
&& // Make sure we have 11+minsize bytes of data
2453 ptr
[0] == 0 && // Name must be root label
2454 ptr
[1] == (kDNSType_OPT
>> 8 ) && // rrtype OPT
2455 ptr
[2] == (kDNSType_OPT
& 0xFF) &&
2456 ((mDNSu16
)ptr
[9] << 8 | (mDNSu16
)ptr
[10]) >= (mDNSu16
)minsize
)
2459 ptr
= skipResourceRecord(msg
, ptr
, end
);
2464 // On success, GetLLQOptData returns pointer to storage within shared "m->rec";
2465 // it is caller's responsibilty to clear m->rec.r.resrec.RecordType after use
2466 // Note: An OPT RDataBody actually contains one or more variable-length rdataOPT objects packed together
2467 // The code that currently calls this assumes there's only one, instead of iterating through the set
2468 mDNSexport
const rdataOPT
*GetLLQOptData(mDNS
*const m
, const DNSMessage
*const msg
, const mDNSu8
*const end
)
2470 const mDNSu8
*ptr
= LocateOptRR(msg
, end
, DNSOpt_LLQData_Space
);
2473 ptr
= GetLargeResourceRecord(m
, msg
, ptr
, end
, 0, kDNSRecordTypePacketAdd
, &m
->rec
);
2474 if (ptr
&& m
->rec
.r
.resrec
.RecordType
!= kDNSRecordTypePacketNegative
) return(&m
->rec
.r
.resrec
.rdata
->u
.opt
[0]);
2479 // Get the lease life of records in a dynamic update
2480 // returns 0 on error or if no lease present
2481 mDNSexport mDNSu32
GetPktLease(mDNS
*m
, DNSMessage
*msg
, const mDNSu8
*end
)
2484 const mDNSu8
*ptr
= LocateOptRR(msg
, end
, DNSOpt_LeaseData_Space
);
2485 if (ptr
) ptr
= GetLargeResourceRecord(m
, msg
, ptr
, end
, 0, kDNSRecordTypePacketAdd
, &m
->rec
);
2486 if (ptr
&& m
->rec
.r
.resrec
.rdlength
>= DNSOpt_LeaseData_Space
&& m
->rec
.r
.resrec
.rdata
->u
.opt
[0].opt
== kDNSOpt_Lease
)
2487 result
= m
->rec
.r
.resrec
.rdata
->u
.opt
[0].u
.updatelease
;
2488 m
->rec
.r
.resrec
.RecordType
= 0; // Clear RecordType to show we're not still using it
2492 mDNSlocal
const mDNSu8
*DumpRecords(mDNS
*const m
, const DNSMessage
*const msg
, const mDNSu8
*ptr
, const mDNSu8
*const end
, int count
, char *label
)
2495 LogMsg("%2d %s", count
, label
);
2496 for (i
= 0; i
< count
&& ptr
; i
++)
2498 // This puts a LargeCacheRecord on the stack instead of using the shared m->rec storage,
2499 // but since it's only used for debugging (and probably only on OS X, not on
2500 // embedded systems) putting a 9kB object on the stack isn't a big problem.
2501 LargeCacheRecord largecr
;
2502 ptr
= GetLargeResourceRecord(m
, msg
, ptr
, end
, mDNSInterface_Any
, kDNSRecordTypePacketAns
, &largecr
);
2503 if (ptr
) LogMsg("%2d TTL%8d %s", i
, largecr
.r
.resrec
.rroriginalttl
, CRDisplayString(m
, &largecr
.r
));
2505 if (!ptr
) LogMsg("ERROR: Premature end of packet data");
2509 #define DNS_OP_Name(X) ( \
2510 (X) == kDNSFlag0_OP_StdQuery ? "" : \
2511 (X) == kDNSFlag0_OP_Iquery ? "Iquery " : \
2512 (X) == kDNSFlag0_OP_Status ? "Status " : \
2513 (X) == kDNSFlag0_OP_Unused3 ? "Unused3 " : \
2514 (X) == kDNSFlag0_OP_Notify ? "Notify " : \
2515 (X) == kDNSFlag0_OP_Update ? "Update " : "?? " )
2517 #define DNS_RC_Name(X) ( \
2518 (X) == kDNSFlag1_RC_NoErr ? "NoErr" : \
2519 (X) == kDNSFlag1_RC_FormErr ? "FormErr" : \
2520 (X) == kDNSFlag1_RC_ServFail ? "ServFail" : \
2521 (X) == kDNSFlag1_RC_NXDomain ? "NXDomain" : \
2522 (X) == kDNSFlag1_RC_NotImpl ? "NotImpl" : \
2523 (X) == kDNSFlag1_RC_Refused ? "Refused" : \
2524 (X) == kDNSFlag1_RC_YXDomain ? "YXDomain" : \
2525 (X) == kDNSFlag1_RC_YXRRSet ? "YXRRSet" : \
2526 (X) == kDNSFlag1_RC_NXRRSet ? "NXRRSet" : \
2527 (X) == kDNSFlag1_RC_NotAuth ? "NotAuth" : \
2528 (X) == kDNSFlag1_RC_NotZone ? "NotZone" : "??" )
2530 // Note: DumpPacket expects the packet header fields in host byte order, not network byte order
2531 mDNSexport
void DumpPacket(mDNS
*const m
, mStatus status
, mDNSBool sent
, char *transport
,
2532 const mDNSAddr
*srcaddr
, mDNSIPPort srcport
,
2533 const mDNSAddr
*dstaddr
, mDNSIPPort dstport
, const DNSMessage
*const msg
, const mDNSu8
*const end
)
2535 mDNSBool IsUpdate
= ((msg
->h
.flags
.b
[0] & kDNSFlag0_OP_Mask
) == kDNSFlag0_OP_Update
);
2536 const mDNSu8
*ptr
= msg
->data
;
2539 char tbuffer
[64], sbuffer
[64], dbuffer
[64] = "";
2540 if (!status
) tbuffer
[mDNS_snprintf(tbuffer
, sizeof(tbuffer
), sent
? "Sent" : "Received" )] = 0;
2541 else tbuffer
[mDNS_snprintf(tbuffer
, sizeof(tbuffer
), "ERROR %d %sing", status
, sent
? "Send" : "Receiv")] = 0;
2542 if (sent
) sbuffer
[mDNS_snprintf(sbuffer
, sizeof(sbuffer
), "port " )] = 0;
2543 else sbuffer
[mDNS_snprintf(sbuffer
, sizeof(sbuffer
), "%#a:", srcaddr
)] = 0;
2544 if (dstaddr
|| !mDNSIPPortIsZero(dstport
))
2545 dbuffer
[mDNS_snprintf(dbuffer
, sizeof(dbuffer
), " to %#a:%d", dstaddr
, mDNSVal16(dstport
))] = 0;
2547 LogMsg("-- %s %s DNS %s%s (flags %02X%02X) RCODE: %s (%d) %s%s%s%s%s%sID: %d %d bytes from %s%d%s%s --",
2549 DNS_OP_Name(msg
->h
.flags
.b
[0] & kDNSFlag0_OP_Mask
),
2550 msg
->h
.flags
.b
[0] & kDNSFlag0_QR_Response
? "Response" : "Query",
2551 msg
->h
.flags
.b
[0], msg
->h
.flags
.b
[1],
2552 DNS_RC_Name(msg
->h
.flags
.b
[1] & kDNSFlag1_RC_Mask
),
2553 msg
->h
.flags
.b
[1] & kDNSFlag1_RC_Mask
,
2554 msg
->h
.flags
.b
[0] & kDNSFlag0_AA
? "AA " : "",
2555 msg
->h
.flags
.b
[0] & kDNSFlag0_TC
? "TC " : "",
2556 msg
->h
.flags
.b
[0] & kDNSFlag0_RD
? "RD " : "",
2557 msg
->h
.flags
.b
[1] & kDNSFlag1_RA
? "RA " : "",
2558 msg
->h
.flags
.b
[1] & kDNSFlag1_AD
? "AD " : "",
2559 msg
->h
.flags
.b
[1] & kDNSFlag1_CD
? "CD " : "",
2560 mDNSVal16(msg
->h
.id
),
2562 sbuffer
, mDNSVal16(srcport
), dbuffer
,
2563 (msg
->h
.flags
.b
[0] & kDNSFlag0_TC
) ? " (truncated)" : ""
2566 LogMsg("%2d %s", msg
->h
.numQuestions
, IsUpdate
? "Zone" : "Questions");
2567 for (i
= 0; i
< msg
->h
.numQuestions
&& ptr
; i
++)
2569 ptr
= getQuestion(msg
, ptr
, end
, mDNSInterface_Any
, &q
);
2570 if (ptr
) LogMsg("%2d %##s %s", i
, q
.qname
.c
, DNSTypeName(q
.qtype
));
2572 ptr
= DumpRecords(m
, msg
, ptr
, end
, msg
->h
.numAnswers
, IsUpdate
? "Prerequisites" : "Answers");
2573 ptr
= DumpRecords(m
, msg
, ptr
, end
, msg
->h
.numAuthorities
, IsUpdate
? "Updates" : "Authorities");
2574 ptr
= DumpRecords(m
, msg
, ptr
, end
, msg
->h
.numAdditionals
, "Additionals");
2575 LogMsg("--------------");
2578 // ***************************************************************************
2579 #if COMPILER_LIKES_PRAGMA_MARK
2581 #pragma mark - Packet Sending Functions
2584 // Stub definition of TCPSocket_struct so we can access flags field. (Rest of TCPSocket_struct is platform-dependent.)
2585 struct TCPSocket_struct
{ TCPSocketFlags flags
; /* ... */ };
2587 struct UDPSocket_struct
2589 mDNSIPPort port
; // MUST BE FIRST FIELD -- mDNSCoreReceive expects every UDPSocket_struct to begin with mDNSIPPort port
2592 // Note: When we sign a DNS message using DNSDigest_SignMessage(), the current real-time clock value is used, which
2593 // is why we generally defer signing until we send the message, to ensure the signature is as fresh as possible.
2594 mDNSexport mStatus
mDNSSendDNSMessage(mDNS
*const m
, DNSMessage
*const msg
, mDNSu8
*end
,
2595 mDNSInterfaceID InterfaceID
, UDPSocket
*src
, const mDNSAddr
*dst
, mDNSIPPort dstport
, TCPSocket
*sock
, DomainAuthInfo
*authInfo
)
2597 mStatus status
= mStatus_NoError
;
2598 const mDNSu16 numAdditionals
= msg
->h
.numAdditionals
;
2600 mDNSu8
*limit
= msg
->data
+ AbsoluteMaxDNSMessageData
;
2602 // Zero-length message data is okay (e.g. for a DNS Update ack, where all we need is an ID and an error code
2603 if (end
< msg
->data
|| end
- msg
->data
> AbsoluteMaxDNSMessageData
)
2605 LogMsg("mDNSSendDNSMessage: invalid message %p %p %d", msg
->data
, end
, end
- msg
->data
);
2606 return mStatus_BadParamErr
;
2609 newend
= putHINFO(m
, msg
, end
, authInfo
, limit
);
2610 if (!newend
) LogMsg("mDNSSendDNSMessage: putHINFO failed msg %p end %p, limit %p", msg
->data
, end
, limit
); // Not fatal
2613 // Put all the integer values in IETF byte-order (MSB first, LSB second)
2614 SwapDNSHeaderBytes(msg
);
2616 if (authInfo
) DNSDigest_SignMessage(msg
, &end
, authInfo
, 0); // DNSDigest_SignMessage operates on message in network byte order
2617 if (!end
) { LogMsg("mDNSSendDNSMessage: DNSDigest_SignMessage failed"); status
= mStatus_NoMemoryErr
; }
2620 // Send the packet on the wire
2622 status
= mDNSPlatformSendUDP(m
, msg
, end
, InterfaceID
, src
, dst
, dstport
);
2625 mDNSu16 msglen
= (mDNSu16
)(end
- (mDNSu8
*)msg
);
2626 mDNSu8 lenbuf
[2] = { (mDNSu8
)(msglen
>> 8), (mDNSu8
)(msglen
& 0xFF) };
2627 long nsent
= mDNSPlatformWriteTCP(sock
, (char*)lenbuf
, 2); // Should do scatter/gather here -- this is probably going out as two packets
2628 if (nsent
!= 2) { LogMsg("mDNSSendDNSMessage: write msg length failed %d/%d", nsent
, 2); status
= mStatus_ConnFailed
; }
2631 nsent
= mDNSPlatformWriteTCP(sock
, (char *)msg
, msglen
);
2632 if (nsent
!= msglen
) { LogMsg("mDNSSendDNSMessage: write msg body failed %d/%d", nsent
, msglen
); status
= mStatus_ConnFailed
; }
2637 // Swap the integer values back the way they were (remember that numAdditionals may have been changed by putHINFO and/or SignMessage)
2638 SwapDNSHeaderBytes(msg
);
2640 // Dump the packet with the HINFO and TSIG
2641 if (mDNS_PacketLoggingEnabled
&& !mDNSOpaque16IsZero(msg
->h
.id
))
2642 DumpPacket(m
, status
, mDNStrue
, sock
&& (sock
->flags
& kTCPSocketFlags_UseTLS
) ? "TLS" : sock
? "TCP" : "UDP", mDNSNULL
, src
? src
->port
: MulticastDNSPort
, dst
, dstport
, msg
, end
);
2644 // put the number of additionals back the way it was
2645 msg
->h
.numAdditionals
= numAdditionals
;
2650 // ***************************************************************************
2651 #if COMPILER_LIKES_PRAGMA_MARK
2653 #pragma mark - RR List Management & Task Management
2656 mDNSexport
void mDNS_Lock_(mDNS
*const m
, const char * const functionname
)
2658 // MUST grab the platform lock FIRST!
2659 mDNSPlatformLock(m
);
2661 // Normally, mDNS_reentrancy is zero and so is mDNS_busy
2662 // However, when we call a client callback mDNS_busy is one, and we increment mDNS_reentrancy too
2663 // If that client callback does mDNS API calls, mDNS_reentrancy and mDNS_busy will both be one
2664 // If mDNS_busy != mDNS_reentrancy that's a bad sign
2665 if (m
->mDNS_busy
!= m
->mDNS_reentrancy
)
2667 LogMsg("%s: mDNS_Lock: Locking failure! mDNS_busy (%ld) != mDNS_reentrancy (%ld)", functionname
, m
->mDNS_busy
, m
->mDNS_reentrancy
);
2673 // If this is an initial entry into the mDNSCore code, set m->timenow
2674 // else, if this is a re-entrant entry into the mDNSCore code, m->timenow should already be set
2675 if (m
->mDNS_busy
== 0)
2678 LogMsg("%s: mDNS_Lock: m->timenow already set (%ld/%ld)", functionname
, m
->timenow
, mDNS_TimeNow_NoLock(m
));
2679 m
->timenow
= mDNS_TimeNow_NoLock(m
);
2680 if (m
->timenow
== 0) m
->timenow
= 1;
2682 else if (m
->timenow
== 0)
2684 LogMsg("%s: mDNS_Lock: m->mDNS_busy is %ld but m->timenow not set", functionname
, m
->mDNS_busy
);
2685 m
->timenow
= mDNS_TimeNow_NoLock(m
);
2686 if (m
->timenow
== 0) m
->timenow
= 1;
2689 if (m
->timenow_last
- m
->timenow
> 0)
2691 m
->timenow_adjust
+= m
->timenow_last
- m
->timenow
;
2692 LogMsg("%s: mDNSPlatformRawTime went backwards by %ld ticks; setting correction factor to %ld", functionname
, m
->timenow_last
- m
->timenow
, m
->timenow_adjust
);
2693 m
->timenow
= m
->timenow_last
;
2695 m
->timenow_last
= m
->timenow
;
2697 // Increment mDNS_busy so we'll recognise re-entrant calls
2701 mDNSlocal AuthRecord
*AnyLocalRecordReady(const mDNS
*const m
)
2704 for (rr
= m
->NewLocalRecords
; rr
; rr
= rr
->next
)
2705 if (LocalRecordReady(rr
)) return rr
;
2709 mDNSlocal mDNSs32
GetNextScheduledEvent(const mDNS
*const m
)
2711 mDNSs32 e
= m
->timenow
+ 0x78000000;
2712 if (m
->mDNSPlatformStatus
!= mStatus_NoError
) return(e
);
2713 if (m
->NewQuestions
)
2715 if (m
->NewQuestions
->DelayAnswering
) e
= m
->NewQuestions
->DelayAnswering
;
2716 else return(m
->timenow
);
2718 if (m
->NewLocalOnlyQuestions
) return(m
->timenow
);
2719 if (m
->NewLocalRecords
&& AnyLocalRecordReady(m
)) return(m
->timenow
);
2720 if (m
->NewLocalOnlyRecords
) return(m
->timenow
);
2721 if (m
->SPSProxyListChanged
) return(m
->timenow
);
2722 if (m
->LocalRemoveEvents
) return(m
->timenow
);
2724 #ifndef UNICAST_DISABLED
2725 if (e
- m
->NextuDNSEvent
> 0) e
= m
->NextuDNSEvent
;
2726 if (e
- m
->NextScheduledNATOp
> 0) e
= m
->NextScheduledNATOp
;
2727 if (m
->NextSRVUpdate
&& e
- m
->NextSRVUpdate
> 0) e
= m
->NextSRVUpdate
;
2730 if (e
- m
->NextCacheCheck
> 0) e
= m
->NextCacheCheck
;
2731 if (e
- m
->NextScheduledSPS
> 0) e
= m
->NextScheduledSPS
;
2732 // NextScheduledSPRetry only valid when DelaySleep not set
2733 if (!m
->DelaySleep
&& m
->SleepLimit
&& e
- m
->NextScheduledSPRetry
> 0) e
= m
->NextScheduledSPRetry
;
2734 if (m
->DelaySleep
&& e
- m
->DelaySleep
> 0) e
= m
->DelaySleep
;
2736 if (m
->SuppressSending
)
2738 if (e
- m
->SuppressSending
> 0) e
= m
->SuppressSending
;
2742 if (e
- m
->NextScheduledQuery
> 0) e
= m
->NextScheduledQuery
;
2743 if (e
- m
->NextScheduledProbe
> 0) e
= m
->NextScheduledProbe
;
2744 if (e
- m
->NextScheduledResponse
> 0) e
= m
->NextScheduledResponse
;
2746 if (e
- m
->NextScheduledStopTime
> 0) e
= m
->NextScheduledStopTime
;
2750 mDNSexport
void ShowTaskSchedulingError(mDNS
*const m
)
2755 LogMsg("Task Scheduling Error: Continuously busy for more than a second");
2757 // Note: To accurately diagnose *why* we're busy, the debugging code here needs to mirror the logic in GetNextScheduledEvent above
2759 if (m
->NewQuestions
&& (!m
->NewQuestions
->DelayAnswering
|| m
->timenow
- m
->NewQuestions
->DelayAnswering
>= 0))
2760 LogMsg("Task Scheduling Error: NewQuestion %##s (%s)",
2761 m
->NewQuestions
->qname
.c
, DNSTypeName(m
->NewQuestions
->qtype
));
2763 if (m
->NewLocalOnlyQuestions
)
2764 LogMsg("Task Scheduling Error: NewLocalOnlyQuestions %##s (%s)",
2765 m
->NewLocalOnlyQuestions
->qname
.c
, DNSTypeName(m
->NewLocalOnlyQuestions
->qtype
));
2767 if (m
->NewLocalRecords
)
2769 rr
= AnyLocalRecordReady(m
);
2770 if (rr
) LogMsg("Task Scheduling Error: NewLocalRecords %s", ARDisplayString(m
, rr
));
2773 if (m
->NewLocalOnlyRecords
) LogMsg("Task Scheduling Error: NewLocalOnlyRecords");
2775 if (m
->SPSProxyListChanged
) LogMsg("Task Scheduling Error: SPSProxyListChanged");
2776 if (m
->LocalRemoveEvents
) LogMsg("Task Scheduling Error: LocalRemoveEvents");
2778 if (m
->timenow
- m
->NextScheduledEvent
>= 0)
2779 LogMsg("Task Scheduling Error: m->NextScheduledEvent %d", m
->timenow
- m
->NextScheduledEvent
);
2781 #ifndef UNICAST_DISABLED
2782 if (m
->timenow
- m
->NextuDNSEvent
>= 0)
2783 LogMsg("Task Scheduling Error: m->NextuDNSEvent %d", m
->timenow
- m
->NextuDNSEvent
);
2784 if (m
->timenow
- m
->NextScheduledNATOp
>= 0)
2785 LogMsg("Task Scheduling Error: m->NextScheduledNATOp %d", m
->timenow
- m
->NextScheduledNATOp
);
2786 if (m
->NextSRVUpdate
&& m
->timenow
- m
->NextSRVUpdate
>= 0)
2787 LogMsg("Task Scheduling Error: m->NextSRVUpdate %d", m
->timenow
- m
->NextSRVUpdate
);
2790 if (m
->timenow
- m
->NextCacheCheck
>= 0)
2791 LogMsg("Task Scheduling Error: m->NextCacheCheck %d", m
->timenow
- m
->NextCacheCheck
);
2792 if (m
->timenow
- m
->NextScheduledSPS
>= 0)
2793 LogMsg("Task Scheduling Error: m->NextScheduledSPS %d", m
->timenow
- m
->NextScheduledSPS
);
2794 if (!m
->DelaySleep
&& m
->SleepLimit
&& m
->timenow
- m
->NextScheduledSPRetry
>= 0)
2795 LogMsg("Task Scheduling Error: m->NextScheduledSPRetry %d", m
->timenow
- m
->NextScheduledSPRetry
);
2796 if (m
->DelaySleep
&& m
->timenow
- m
->DelaySleep
>= 0)
2797 LogMsg("Task Scheduling Error: m->DelaySleep %d", m
->timenow
- m
->DelaySleep
);
2799 if (m
->SuppressSending
&& m
->timenow
- m
->SuppressSending
>= 0)
2800 LogMsg("Task Scheduling Error: m->SuppressSending %d", m
->timenow
- m
->SuppressSending
);
2801 if (m
->timenow
- m
->NextScheduledQuery
>= 0)
2802 LogMsg("Task Scheduling Error: m->NextScheduledQuery %d", m
->timenow
- m
->NextScheduledQuery
);
2803 if (m
->timenow
- m
->NextScheduledProbe
>= 0)
2804 LogMsg("Task Scheduling Error: m->NextScheduledProbe %d", m
->timenow
- m
->NextScheduledProbe
);
2805 if (m
->timenow
- m
->NextScheduledResponse
>= 0)
2806 LogMsg("Task Scheduling Error: m->NextScheduledResponse %d", m
->timenow
- m
->NextScheduledResponse
);
2811 mDNSexport
void mDNS_Unlock_(mDNS
*const m
, const char * const functionname
)
2813 // Decrement mDNS_busy
2816 // Check for locking failures
2817 if (m
->mDNS_busy
!= m
->mDNS_reentrancy
)
2819 LogMsg("%s: mDNS_Unlock: Locking failure! mDNS_busy (%ld) != mDNS_reentrancy (%ld)", functionname
, m
->mDNS_busy
, m
->mDNS_reentrancy
);
2825 // If this is a final exit from the mDNSCore code, set m->NextScheduledEvent and clear m->timenow
2826 if (m
->mDNS_busy
== 0)
2828 m
->NextScheduledEvent
= GetNextScheduledEvent(m
);
2829 if (m
->timenow
== 0) LogMsg("%s: mDNS_Unlock: ERROR! m->timenow aready zero", functionname
);
2833 // MUST release the platform lock LAST!
2834 mDNSPlatformUnlock(m
);
2837 // ***************************************************************************
2838 #if COMPILER_LIKES_PRAGMA_MARK
2840 #pragma mark - Specialized mDNS version of vsnprintf
2843 static const struct mDNSprintf_format
2845 unsigned leftJustify
: 1;
2846 unsigned forceSign
: 1;
2847 unsigned zeroPad
: 1;
2848 unsigned havePrecision
: 1;
2852 char sign
; // +, - or space
2853 unsigned int fieldWidth
;
2854 unsigned int precision
;
2855 } mDNSprintf_format_default
= { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
2857 mDNSexport mDNSu32
mDNS_vsnprintf(char *sbuffer
, mDNSu32 buflen
, const char *fmt
, va_list arg
)
2859 mDNSu32 nwritten
= 0;
2861 if (buflen
== 0) return(0);
2862 buflen
--; // Pre-reserve one space in the buffer for the terminating null
2863 if (buflen
== 0) goto exit
;
2865 for (c
= *fmt
; c
!= 0; c
= *++fmt
)
2869 *sbuffer
++ = (char)c
;
2870 if (++nwritten
>= buflen
) goto exit
;
2874 unsigned int i
=0, j
;
2875 // The mDNS Vsprintf Argument Conversion Buffer is used as a temporary holding area for
2876 // generating decimal numbers, hexdecimal numbers, IP addresses, domain name strings, etc.
2877 // The size needs to be enough for a 256-byte domain name plus some error text.
2878 #define mDNS_VACB_Size 300
2879 char mDNS_VACB
[mDNS_VACB_Size
];
2880 #define mDNS_VACB_Lim (&mDNS_VACB[mDNS_VACB_Size])
2881 #define mDNS_VACB_Remain(s) ((mDNSu32)(mDNS_VACB_Lim - s))
2882 char *s
= mDNS_VACB_Lim
, *digits
;
2883 struct mDNSprintf_format F
= mDNSprintf_format_default
;
2885 while (1) // decode flags
2888 if (c
== '-') F
.leftJustify
= 1;
2889 else if (c
== '+') F
.forceSign
= 1;
2890 else if (c
== ' ') F
.sign
= ' ';
2891 else if (c
== '#') F
.altForm
++;
2892 else if (c
== '0') F
.zeroPad
= 1;
2896 if (c
== '*') // decode field width
2898 int f
= va_arg(arg
, int);
2899 if (f
< 0) { f
= -f
; F
.leftJustify
= 1; }
2900 F
.fieldWidth
= (unsigned int)f
;
2905 for (; c
>= '0' && c
<= '9'; c
= *++fmt
)
2906 F
.fieldWidth
= (10 * F
.fieldWidth
) + (c
- '0');
2909 if (c
== '.') // decode precision
2911 if ((c
= *++fmt
) == '*')
2912 { F
.precision
= va_arg(arg
, unsigned int); c
= *++fmt
; }
2913 else for (; c
>= '0' && c
<= '9'; c
= *++fmt
)
2914 F
.precision
= (10 * F
.precision
) + (c
- '0');
2915 F
.havePrecision
= 1;
2918 if (F
.leftJustify
) F
.zeroPad
= 0;
2921 switch (c
) // perform appropriate conversion
2924 case 'h' : F
.hSize
= 1; c
= *++fmt
; goto conv
;
2925 case 'l' : // fall through
2926 case 'L' : F
.lSize
= 1; c
= *++fmt
; goto conv
;
2928 case 'i' : if (F
.lSize
) n
= (unsigned long)va_arg(arg
, long);
2929 else n
= (unsigned long)va_arg(arg
, int);
2930 if (F
.hSize
) n
= (short) n
;
2931 if ((long) n
< 0) { n
= (unsigned long)-(long)n
; F
.sign
= '-'; }
2932 else if (F
.forceSign
) F
.sign
= '+';
2934 case 'u' : if (F
.lSize
) n
= va_arg(arg
, unsigned long);
2935 else n
= va_arg(arg
, unsigned int);
2936 if (F
.hSize
) n
= (unsigned short) n
;
2939 decimal
: if (!F
.havePrecision
)
2943 F
.precision
= F
.fieldWidth
;
2944 if (F
.sign
) --F
.precision
;
2946 if (F
.precision
< 1) F
.precision
= 1;
2948 if (F
.precision
> mDNS_VACB_Size
- 1)
2949 F
.precision
= mDNS_VACB_Size
- 1;
2950 for (i
= 0; n
; n
/= 10, i
++) *--s
= (char)(n
% 10 + '0');
2951 for (; i
< F
.precision
; i
++) *--s
= '0';
2952 if (F
.sign
) { *--s
= F
.sign
; i
++; }
2955 case 'o' : if (F
.lSize
) n
= va_arg(arg
, unsigned long);
2956 else n
= va_arg(arg
, unsigned int);
2957 if (F
.hSize
) n
= (unsigned short) n
;
2958 if (!F
.havePrecision
)
2960 if (F
.zeroPad
) F
.precision
= F
.fieldWidth
;
2961 if (F
.precision
< 1) F
.precision
= 1;
2963 if (F
.precision
> mDNS_VACB_Size
- 1)
2964 F
.precision
= mDNS_VACB_Size
- 1;
2965 for (i
= 0; n
; n
/= 8, i
++) *--s
= (char)(n
% 8 + '0');
2966 if (F
.altForm
&& i
&& *s
!= '0') { *--s
= '0'; i
++; }
2967 for (; i
< F
.precision
; i
++) *--s
= '0';
2971 unsigned char *a
= va_arg(arg
, unsigned char *);
2972 if (!a
) { static char emsg
[] = "<<NULL>>"; s
= emsg
; i
= sizeof(emsg
)-1; }
2975 s
= mDNS_VACB
; // Adjust s to point to the start of the buffer, not the end
2978 mDNSAddr
*ip
= (mDNSAddr
*)a
;
2981 case mDNSAddrType_IPv4
: F
.precision
= 4; a
= (unsigned char *)&ip
->ip
.v4
; break;
2982 case mDNSAddrType_IPv6
: F
.precision
= 16; a
= (unsigned char *)&ip
->ip
.v6
; break;
2983 default: F
.precision
= 0; break;
2986 if (F
.altForm
&& !F
.precision
)
2987 i
= mDNS_snprintf(mDNS_VACB
, sizeof(mDNS_VACB
), "«ZERO ADDRESS»");
2988 else switch (F
.precision
)
2990 case 4: i
= mDNS_snprintf(mDNS_VACB
, sizeof(mDNS_VACB
), "%d.%d.%d.%d",
2991 a
[0], a
[1], a
[2], a
[3]); break;
2992 case 6: i
= mDNS_snprintf(mDNS_VACB
, sizeof(mDNS_VACB
), "%02X:%02X:%02X:%02X:%02X:%02X",
2993 a
[0], a
[1], a
[2], a
[3], a
[4], a
[5]); break;
2994 case 16: i
= mDNS_snprintf(mDNS_VACB
, sizeof(mDNS_VACB
),
2995 "%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X",
2996 a
[0x0], a
[0x1], a
[0x2], a
[0x3], a
[0x4], a
[0x5], a
[0x6], a
[0x7],
2997 a
[0x8], a
[0x9], a
[0xA], a
[0xB], a
[0xC], a
[0xD], a
[0xE], a
[0xF]); break;
2998 default: i
= mDNS_snprintf(mDNS_VACB
, sizeof(mDNS_VACB
), "%s", "<< ERROR: Must specify"
2999 " address size (i.e. %.4a=IPv4, %.6a=Ethernet, %.16a=IPv6) >>"); break;
3005 case 'p' : F
.havePrecision
= F
.lSize
= 1;
3006 F
.precision
= sizeof(void*) * 2; // 8 characters on 32-bit; 16 characters on 64-bit
3007 case 'X' : digits
= "0123456789ABCDEF";
3009 case 'x' : digits
= "0123456789abcdef";
3010 hexadecimal
:if (F
.lSize
) n
= va_arg(arg
, unsigned long);
3011 else n
= va_arg(arg
, unsigned int);
3012 if (F
.hSize
) n
= (unsigned short) n
;
3013 if (!F
.havePrecision
)
3017 F
.precision
= F
.fieldWidth
;
3018 if (F
.altForm
) F
.precision
-= 2;
3020 if (F
.precision
< 1) F
.precision
= 1;
3022 if (F
.precision
> mDNS_VACB_Size
- 1)
3023 F
.precision
= mDNS_VACB_Size
- 1;
3024 for (i
= 0; n
; n
/= 16, i
++) *--s
= digits
[n
% 16];
3025 for (; i
< F
.precision
; i
++) *--s
= '0';
3026 if (F
.altForm
) { *--s
= (char)c
; *--s
= '0'; i
+= 2; }
3029 case 'c' : *--s
= (char)va_arg(arg
, int); i
= 1; break;
3031 case 's' : s
= va_arg(arg
, char *);
3032 if (!s
) { static char emsg
[] = "<<NULL>>"; s
= emsg
; i
= sizeof(emsg
)-1; }
3033 else switch (F
.altForm
)
3036 if (!F
.havePrecision
) // C string
3040 while ((i
< F
.precision
) && s
[i
]) i
++;
3041 // Make sure we don't truncate in the middle of a UTF-8 character
3042 // If last character we got was any kind of UTF-8 multi-byte character,
3043 // then see if we have to back up.
3044 // This is not as easy as the similar checks below, because
3045 // here we can't assume it's safe to examine the *next* byte, so we
3046 // have to confine ourselves to working only backwards in the string.
3047 j
= i
; // Record where we got to
3048 // Now, back up until we find first non-continuation-char
3049 while (i
>0 && (s
[i
-1] & 0xC0) == 0x80) i
--;
3050 // Now s[i-1] is the first non-continuation-char
3051 // and (j-i) is the number of continuation-chars we found
3052 if (i
>0 && (s
[i
-1] & 0xC0) == 0xC0) // If we found a start-char
3054 i
--; // Tentatively eliminate this start-char as well
3055 // Now (j-i) is the number of characters we're considering eliminating.
3056 // To be legal UTF-8, the start-char must contain (j-i) one-bits,
3057 // followed by a zero bit. If we shift it right by (7-(j-i)) bits
3058 // (with sign extension) then the result has to be 0xFE.
3059 // If this is right, then we reinstate the tentatively eliminated bytes.
3060 if (((j
-i
) < 7) && (((s
[i
] >> (7-(j
-i
))) & 0xFF) == 0xFE)) i
= j
;
3064 case 1: i
= (unsigned char) *s
++; break; // Pascal string
3065 case 2: { // DNS label-sequence name
3066 unsigned char *a
= (unsigned char *)s
;
3067 s
= mDNS_VACB
; // Adjust s to point to the start of the buffer, not the end
3068 if (*a
== 0) *s
++ = '.'; // Special case for root DNS name
3073 { s
+= mDNS_snprintf(s
, mDNS_VACB_Remain(s
), "<<INVALID LABEL LENGTH %u>>", *a
); break; }
3074 if (s
+ *a
>= &mDNS_VACB
[254])
3075 { s
+= mDNS_snprintf(s
, mDNS_VACB_Remain(s
), "<<NAME TOO LONG>>"); break; }
3076 // Need to use ConvertDomainLabelToCString to do proper escaping here,
3077 // so it's clear what's a literal dot and what's a label separator
3078 ConvertDomainLabelToCString((domainlabel
*)a
, buf
);
3079 s
+= mDNS_snprintf(s
, mDNS_VACB_Remain(s
), "%s.", buf
);
3082 i
= (mDNSu32
)(s
- mDNS_VACB
);
3083 s
= mDNS_VACB
; // Reset s back to the start of the buffer
3087 // Make sure we don't truncate in the middle of a UTF-8 character (see similar comment below)
3088 if (F
.havePrecision
&& i
> F
.precision
)
3089 { i
= F
.precision
; while (i
>0 && (s
[i
] & 0xC0) == 0x80) i
--; }
3092 case 'n' : s
= va_arg(arg
, char *);
3093 if (F
.hSize
) * (short *) s
= (short)nwritten
;
3094 else if (F
.lSize
) * (long *) s
= (long)nwritten
;
3095 else * (int *) s
= (int)nwritten
;
3098 default: s
= mDNS_VACB
;
3099 i
= mDNS_snprintf(mDNS_VACB
, sizeof(mDNS_VACB
), "<<UNKNOWN FORMAT CONVERSION CODE %%%c>>", c
);
3101 case '%' : *sbuffer
++ = (char)c
;
3102 if (++nwritten
>= buflen
) goto exit
;
3106 if (i
< F
.fieldWidth
&& !F
.leftJustify
) // Pad on the left
3109 if (++nwritten
>= buflen
) goto exit
;
3110 } while (i
< --F
.fieldWidth
);
3112 // Make sure we don't truncate in the middle of a UTF-8 character.
3113 // Note: s[i] is the first eliminated character; i.e. the next character *after* the last character of the
3114 // allowed output. If s[i] is a UTF-8 continuation character, then we've cut a unicode character in half,
3115 // so back up 'i' until s[i] is no longer a UTF-8 continuation character. (if the input was proprly
3116 // formed, s[i] will now be the UTF-8 start character of the multi-byte character we just eliminated).
3117 if (i
> buflen
- nwritten
)
3118 { i
= buflen
- nwritten
; while (i
>0 && (s
[i
] & 0xC0) == 0x80) i
--; }
3119 for (j
=0; j
<i
; j
++) *sbuffer
++ = *s
++; // Write the converted result
3121 if (nwritten
>= buflen
) goto exit
;
3123 for (; i
< F
.fieldWidth
; i
++) // Pad on the right
3126 if (++nwritten
>= buflen
) goto exit
;
3135 mDNSexport mDNSu32
mDNS_snprintf(char *sbuffer
, mDNSu32 buflen
, const char *fmt
, ...)
3141 length
= mDNS_vsnprintf(sbuffer
, buflen
, fmt
, ptr
);