1/* -*- Mode: C; tab-width: 4 -*-
2 *
3 * Copyright (c) 2002-2017 Apple Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16
17 * To Do:
18 * Elimate all mDNSPlatformMemAllocate/mDNSPlatformMemFree from this code -- the core code
19 * is supposed to be malloc-free so that it runs in constant memory determined at compile-time.
20 * Any dynamic run-time requirements should be handled by the platform layer below or client layer above
21 */
22
23#if APPLE_OSX_mDNSResponder
24#include <TargetConditionals.h>
25#endif
26#include "uDNS.h"
27
28#if AWD_METRICS
29#include "Metrics.h"
30#endif
31
32#if (defined(_MSC_VER))
33// Disable "assignment within conditional expression".
34// Other compilers understand the convention that if you place the assignment expression within an extra pair
35// of parentheses, this signals to the compiler that you really intended an assignment and no warning is necessary.
36// The Microsoft compiler doesn't understand this convention, so in the absense of any other way to signal
37// to the compiler that the assignment is intentional, we have to just turn this warning off completely.
38    #pragma warning(disable:4706)
39#endif
40
41// For domain enumeration and automatic browsing
42// This is the user's DNS search list.
43// In each of these domains we search for our special pointer records (lb._dns-sd._udp.<domain>, etc.)
44// to discover recommended domains for domain enumeration (browse, default browse, registration,
45// default registration) and possibly one or more recommended automatic browsing domains.
46mDNSexport SearchListElem *SearchList = mDNSNULL;
47
48// The value can be set to true by the Platform code e.g., MacOSX uses the plist mechanism
49mDNSBool StrictUnicastOrdering = mDNSfalse;
50
51// We keep track of the number of unicast DNS servers and log a message when we exceed 64.
52// Currently the unicast queries maintain a 128 bit map to track the valid DNS servers for that
53// question. Bit position is the index into the DNS server list. This is done so to try all
54// the servers exactly once before giving up. If we could allocate memory in the core, then
55// arbitrary limitation of 128 DNSServers can be removed.
56mDNSu8 NumUnicastDNSServers = 0;
57#define MAX_UNICAST_DNS_SERVERS 128
58#if APPLE_OSX_mDNSResponder
59mDNSu8 NumUnreachableDNSServers = 0;
60#endif
61
62#define SetNextuDNSEvent(m, rr) { \
63        if ((m)->NextuDNSEvent - ((rr)->LastAPTime + (rr)->ThisAPInterval) >= 0)                                                                              \
64            (m)->NextuDNSEvent = ((rr)->LastAPTime + (rr)->ThisAPInterval);                                                                         \
65}
66
67#ifndef UNICAST_DISABLED
68
69// ***************************************************************************
70#if COMPILER_LIKES_PRAGMA_MARK
71#pragma mark - General Utility Functions
72#endif
73
74// set retry timestamp for record with exponential backoff
75mDNSlocal void SetRecordRetry(mDNS *const m, AuthRecord *rr, mDNSu32 random)
76{
77    rr->LastAPTime = m->timenow;
78
79    if (rr->expire && rr->refreshCount < MAX_UPDATE_REFRESH_COUNT)
80    {
81        mDNSs32 remaining = rr->expire - m->timenow;
82        rr->refreshCount++;
83        if (remaining > MIN_UPDATE_REFRESH_TIME)
84        {
85            // Refresh at 70% + random (currently it is 0 to 10%)
86            rr->ThisAPInterval =  7 * (remaining/10) + (random ? random : mDNSRandom(remaining/10));
87            // Don't update more often than 5 minutes
88            if (rr->ThisAPInterval < MIN_UPDATE_REFRESH_TIME)
89                rr->ThisAPInterval = MIN_UPDATE_REFRESH_TIME;
90            LogInfo("SetRecordRetry refresh in %d of %d for %s",
91                    rr->ThisAPInterval/mDNSPlatformOneSecond, (rr->expire - m->timenow)/mDNSPlatformOneSecond, ARDisplayString(m, rr));
92        }
93        else
94        {
95            rr->ThisAPInterval = MIN_UPDATE_REFRESH_TIME;
96            LogInfo("SetRecordRetry clamping to min refresh in %d of %d for %s",
97                    rr->ThisAPInterval/mDNSPlatformOneSecond, (rr->expire - m->timenow)/mDNSPlatformOneSecond, ARDisplayString(m, rr));
98        }
99        return;
100    }
101
102    rr->expire = 0;
103
104    rr->ThisAPInterval = rr->ThisAPInterval * QuestionIntervalStep; // Same Retry logic as Unicast Queries
105    if (rr->ThisAPInterval < INIT_RECORD_REG_INTERVAL)
106        rr->ThisAPInterval = INIT_RECORD_REG_INTERVAL;
107    if (rr->ThisAPInterval > MAX_RECORD_REG_INTERVAL)
108        rr->ThisAPInterval = MAX_RECORD_REG_INTERVAL;
109
110    LogInfo("SetRecordRetry retry in %d ms for %s", rr->ThisAPInterval, ARDisplayString(m, rr));
111}
112
113// ***************************************************************************
114#if COMPILER_LIKES_PRAGMA_MARK
115#pragma mark - Name Server List Management
116#endif
117
118#define TrueFalseStr(X) ((X) ? "true" : "false")
119
120mDNSexport DNSServer *mDNS_AddDNSServer(mDNS *const m, const domainname *d, const mDNSInterfaceID interface, const mDNSs32 serviceID, const mDNSAddr *addr,
121                                        const mDNSIPPort port, mDNSu32 scoped, mDNSu32 timeout, mDNSBool cellIntf, mDNSBool isExpensive, mDNSBool isCLAT46,
122                                        mDNSu16 resGroupID, mDNSBool reqA, mDNSBool reqAAAA, mDNSBool reqDO)
123{
124    DNSServer **p = &m->DNSServers;
125    DNSServer *tmp = mDNSNULL;
126
127    if ((NumUnicastDNSServers + 1) > MAX_UNICAST_DNS_SERVERS)
128    {
129        LogMsg("mDNS_AddDNSServer: DNS server limit of %d reached, not adding this server", MAX_UNICAST_DNS_SERVERS);
130        return mDNSNULL;
131    }
132
133    if (!d)
134        d = (const domainname *)"";
135
136    LogInfo("mDNS_AddDNSServer(%d): Adding %#a for %##s, InterfaceID %p, serviceID %u, scoped %d, resGroupID %d req_A %s, req_AAAA %s, cell %s, expensive %s, CLAT46 %s, req_DO %s",
137        NumUnicastDNSServers, addr, d->c, interface, serviceID, scoped, resGroupID,
138        TrueFalseStr(reqA), TrueFalseStr(reqAAAA), TrueFalseStr(cellIntf), TrueFalseStr(isExpensive), TrueFalseStr(isCLAT46), TrueFalseStr(reqDO));
139
140    while (*p)  // Check if we already have this {interface,address,port,domain} tuple registered + reqA/reqAAAA bits
141    {
142        if ((*p)->scoped == scoped && (*p)->interface == interface && (*p)->serviceID == serviceID &&
143            mDNSSameAddress(&(*p)->addr, addr) && mDNSSameIPPort((*p)->port, port) && SameDomainName(&(*p)->domain, d) &&
144            (*p)->req_A == reqA && (*p)->req_AAAA == reqAAAA)
145        {
146            if (!((*p)->flags & DNSServer_FlagDelete))
147                debugf("Note: DNS Server %#a:%d for domain %##s (%p) registered more than once", addr, mDNSVal16(port), d->c, interface);
148            tmp = *p;
149            *p = tmp->next;
150            tmp->next = mDNSNULL;
151        }
152        else
153        {
154            p=&(*p)->next;
155        }
156    }
157
158    // NumUnicastDNSServers is the count of active DNS servers i.e., ones that are not marked
159    // with DNSServer_FlagDelete. We should increment it:
160    //
161    // 1) When we add a new DNS server
162    // 2) When we resurrect a old DNS server that is marked with DNSServer_FlagDelete
163    //
164    // Don't increment when we resurrect a DNS server that is not marked with DNSServer_FlagDelete.
165    // We have already accounted for it when it was added for the first time. This case happens when
166    // we add DNS servers with the same address multiple times (mis-configuration).
167
168    if (!tmp || (tmp->flags & DNSServer_FlagDelete))
169        NumUnicastDNSServers++;
170
171
172    if (tmp)
173    {
174#if APPLE_OSX_mDNSResponder
175        if (tmp->flags & DNSServer_FlagDelete)
176        {
177            tmp->flags &= ~DNSServer_FlagUnreachable;
178        }
179#endif
180        tmp->flags &= ~DNSServer_FlagDelete;
181        *p = tmp; // move to end of list, to ensure ordering from platform layer
182    }
183    else
184    {
185        // allocate, add to list
186        *p = mDNSPlatformMemAllocate(sizeof(**p));
187        if (!*p)
188        {
189            LogMsg("Error: mDNS_AddDNSServer - malloc");
190        }
191        else
192        {
193            (*p)->scoped      = scoped;
194            (*p)->interface   = interface;
195            (*p)->serviceID   = serviceID;
196            (*p)->addr        = *addr;
197            (*p)->port        = port;
198            (*p)->flags       = DNSServer_FlagNew;
199            (*p)->timeout     = timeout;
200            (*p)->cellIntf    = cellIntf;
201            (*p)->isExpensive = isExpensive;
202            (*p)->isCLAT46    = isCLAT46;
203            (*p)->req_A       = reqA;
204            (*p)->req_AAAA    = reqAAAA;
205            (*p)->req_DO      = reqDO;
206            // We start off assuming that the DNS server is not DNSSEC aware and
207            // when we receive the first response to a DNSSEC question, we set
208            // it to true.
209            (*p)->DNSSECAware = mDNSfalse;
210            (*p)->retransDO = 0;
211            AssignDomainName(&(*p)->domain, d);
212            (*p)->next = mDNSNULL;
213        }
214    }
215    if (*p) {
216        (*p)->penaltyTime = 0;
217        // We always update the ID (not just when we allocate a new instance) because we could
218        // be adding a new non-scoped resolver with a new ID and we want all the non-scoped
219        // resolvers belong to the same group.
220        (*p)->resGroupID  = resGroupID;
221    }
222    return(*p);
223}
224
225// PenalizeDNSServer is called when the number of queries to the unicast
226// DNS server exceeds MAX_UCAST_UNANSWERED_QUERIES or when we receive an
227// error e.g., SERV_FAIL from DNS server.
228mDNSexport void PenalizeDNSServer(mDNS *const m, DNSQuestion *q, mDNSOpaque16 responseFlags)
229{
230    DNSServer *new;
231    DNSServer *orig = q->qDNSServer;
232    mDNSu8 rcode = '\0';
233
234    mDNS_CheckLock(m);
235
236    LogInfo("PenalizeDNSServer: Penalizing DNS server %#a question for question %p %##s (%s) SuppressUnusable %d",
237            (q->qDNSServer ? &q->qDNSServer->addr : mDNSNULL), q, q->qname.c, DNSTypeName(q->qtype), q->SuppressUnusable);
238
239    // If we get error from any DNS server, remember the error. If all of the servers,
240    // return the error, then return the first error.
241    if (mDNSOpaque16IsZero(q->responseFlags))
242        q->responseFlags = responseFlags;
243
244    rcode = (mDNSu8)(responseFlags.b[1] & kDNSFlag1_RC_Mask);
245
246    // After we reset the qDNSServer to NULL, we could get more SERV_FAILS that might end up
247    // penalizing again.
248    if (!q->qDNSServer)
249        goto end;
250
251    // If strict ordering of unicast servers needs to be preserved, we just lookup
252    // the next best match server below
253    //
254    // If strict ordering is not required which is the default behavior, we penalize the server
255    // for DNSSERVER_PENALTY_TIME. We may also use additional logic e.g., don't penalize for PTR
256    // in the future.
257
258    if (!StrictUnicastOrdering)
259    {
260        LogInfo("PenalizeDNSServer: Strict Unicast Ordering is FALSE");
261        // We penalize the server so that new queries don't pick this server for DNSSERVER_PENALTY_TIME
262        // XXX Include other logic here to see if this server should really be penalized
263        //
264        if (q->qtype == kDNSType_PTR)
265        {
266            LogInfo("PenalizeDNSServer: Not Penalizing PTR question");
267        }
268        else if ((rcode == kDNSFlag1_RC_FormErr) || (rcode == kDNSFlag1_RC_ServFail) || (rcode == kDNSFlag1_RC_NotImpl) || (rcode == kDNSFlag1_RC_Refused))
269        {
270            LogInfo("PenalizeDNSServer: Not Penalizing DNS Server since it at least responded with rcode %d", rcode);
271        }
272        else
273        {
274            LogInfo("PenalizeDNSServer: Penalizing question type %d", q->qtype);
275            q->qDNSServer->penaltyTime = NonZeroTime(m->timenow + DNSSERVER_PENALTY_TIME);
276        }
277    }
278    else
279    {
280        LogInfo("PenalizeDNSServer: Strict Unicast Ordering is TRUE");
281    }
282
283end:
284    new = GetServerForQuestion(m, q);
285
286    if (new == orig)
287    {
288        if (new)
289        {
290            LogMsg("PenalizeDNSServer: ERROR!! GetServerForQuestion returned the same server %#a:%d", &new->addr,
291                   mDNSVal16(new->port));
292            q->ThisQInterval = 0;   // Inactivate this question so that we dont bombard the network
293        }
294        else
295        {
296            // When we have no more DNS servers, we might end up calling PenalizeDNSServer multiple
297            // times when we receive SERVFAIL from delayed packets in the network e.g., DNS server
298            // is slow in responding and we have sent three queries. When we repeatedly call, it is
299            // okay to receive the same NULL DNS server. Next time we try to send the query, we will
300            // realize and re-initialize the DNS servers.
301            LogInfo("PenalizeDNSServer: GetServerForQuestion returned the same server NULL");
302        }
303    }
304    else
305    {
306        // The new DNSServer is set in DNSServerChangeForQuestion
307        DNSServerChangeForQuestion(m, q, new);
308
309        if (new)
310        {
311            LogInfo("PenalizeDNSServer: Server for %##s (%s) changed to %#a:%d (%##s)",
312                    q->qname.c, DNSTypeName(q->qtype), &q->qDNSServer->addr, mDNSVal16(q->qDNSServer->port), q->qDNSServer->domain.c);
313            // We want to try the next server immediately. As the question may already have backed off, reset
314            // the interval. We do this only the first time when we try all the DNS servers. Once we reached the end of
315            // list and retrying all the servers again e.g., at least one server failed to respond in the previous try, we
316            // use the normal backoff which is done in uDNS_CheckCurrentQuestion when we send the packet out.
317            if (!q->triedAllServersOnce)
318            {
319                q->ThisQInterval = InitialQuestionInterval;
320                q->LastQTime  = m->timenow - q->ThisQInterval;
321                SetNextQueryTime(m, q);
322            }
323        }
324        else
325        {
326            // We don't have any more DNS servers for this question. If some server in the list did not return
327            // any response, we need to keep retrying till we get a response. uDNS_CheckCurrentQuestion handles
328            // this case.
329            //
330            // If all servers responded with a negative response, We need to do two things. First, generate a
331            // negative response so that applications get a reply. We also need to reinitialize the DNS servers
332            // so that when the cache expires, we can restart the query.  We defer this up until we generate
333            // a negative cache response in uDNS_CheckCurrentQuestion.
334            //
335            // Be careful not to touch the ThisQInterval here. For a normal question, when we answer the question
336            // in AnswerCurrentQuestionWithResourceRecord will set ThisQInterval to MaxQuestionInterval and hence
337            // the next query will not happen until cache expiry. If it is a long lived question,
338            // AnswerCurrentQuestionWithResourceRecord will not set it to MaxQuestionInterval. In that case,
339            // we want the normal backoff to work.
340            LogInfo("PenalizeDNSServer: Server for %p, %##s (%s) changed to NULL, Interval %d", q, q->qname.c, DNSTypeName(q->qtype), q->ThisQInterval);
341        }
342        q->unansweredQueries = 0;
343
344    }
345}
346
347// ***************************************************************************
348#if COMPILER_LIKES_PRAGMA_MARK
349#pragma mark - authorization management
350#endif
351
352mDNSlocal DomainAuthInfo *GetAuthInfoForName_direct(mDNS *m, const domainname *const name)
353{
354    const domainname *n = name;
355    while (n->c[0])
356    {
357        DomainAuthInfo *ptr;
358        for (ptr = m->AuthInfoList; ptr; ptr = ptr->next)
359            if (SameDomainName(&ptr->domain, n))
360            {
361                debugf("GetAuthInfoForName %##s Matched %##s Key name %##s", name->c, ptr->domain.c, ptr->keyname.c);
362                return(ptr);
363            }
364        n = (const domainname *)(n->c + 1 + n->c[0]);
365    }
366    //LogInfo("GetAuthInfoForName none found for %##s", name->c);
367    return mDNSNULL;
368}
369
370// MUST be called with lock held
371mDNSexport DomainAuthInfo *GetAuthInfoForName_internal(mDNS *m, const domainname *const name)
372{
373    DomainAuthInfo **p = &m->AuthInfoList;
374
375    mDNS_CheckLock(m);
376
377    // First purge any dead keys from the list
378    while (*p)
379    {
380        if ((*p)->deltime && m->timenow - (*p)->deltime >= 0 && AutoTunnelUnregistered(*p))
381        {
382            DNSQuestion *q;
383            DomainAuthInfo *info = *p;
384            LogInfo("GetAuthInfoForName_internal deleting expired key %##s %##s", info->domain.c, info->keyname.c);
385            *p = info->next;    // Cut DomainAuthInfo from list *before* scanning our question list updating AuthInfo pointers
386            for (q = m->Questions; q; q=q->next)
387                if (q->AuthInfo == info)
388                {
389                    q->AuthInfo = GetAuthInfoForName_direct(m, &q->qname);
390                    debugf("GetAuthInfoForName_internal updated q->AuthInfo from %##s to %##s for %##s (%s)",
391                           info->domain.c, q->AuthInfo ? q->AuthInfo->domain.c : mDNSNULL, q->qname.c, DNSTypeName(q->qtype));
392                }
393
394            // Probably not essential, but just to be safe, zero out the secret key data
395            // so we don't leave it hanging around in memory
396            // (where it could potentially get exposed via some other bug)
397            mDNSPlatformMemZero(info, sizeof(*info));
398            mDNSPlatformMemFree(info);
399        }
400        else
401            p = &(*p)->next;
402    }
403
404    return(GetAuthInfoForName_direct(m, name));
405}
406
407mDNSexport DomainAuthInfo *GetAuthInfoForName(mDNS *m, const domainname *const name)
408{
409    DomainAuthInfo *d;
410    mDNS_Lock(m);
411    d = GetAuthInfoForName_internal(m, name);
412    mDNS_Unlock(m);
413    return(d);
414}
415
416// MUST be called with the lock held
417mDNSexport mStatus mDNS_SetSecretForDomain(mDNS *m, DomainAuthInfo *info,
418                                           const domainname *domain, const domainname *keyname, const char *b64keydata, const domainname *hostname, mDNSIPPort *port, mDNSBool autoTunnel)
419{
420    DNSQuestion *q;
421    DomainAuthInfo **p = &m->AuthInfoList;
422    if (!info || !b64keydata) { LogMsg("mDNS_SetSecretForDomain: ERROR: info %p b64keydata %p", info, b64keydata); return(mStatus_BadParamErr); }
423
424    LogInfo("mDNS_SetSecretForDomain: domain %##s key %##s%s", domain->c, keyname->c, autoTunnel ? " AutoTunnel" : "");
425
426    info->AutoTunnel = autoTunnel;
427    AssignDomainName(&info->domain,  domain);
428    AssignDomainName(&info->keyname, keyname);
429    if (hostname)
430        AssignDomainName(&info->hostname, hostname);
431    else
432        info->hostname.c[0] = 0;
433    if (port)
434        info->port = *port;
435    else
436        info->port = zeroIPPort;
437    mDNS_snprintf(info->b64keydata, sizeof(info->b64keydata), "%s", b64keydata);
438
439    if (DNSDigest_ConstructHMACKeyfromBase64(info, b64keydata) < 0)
440    {
441        LogMsg("mDNS_SetSecretForDomain: ERROR: Could not convert shared secret from base64: domain %##s key %##s %s", domain->c, keyname->c, mDNS_LoggingEnabled ? b64keydata : "");
442        return(mStatus_BadParamErr);
443    }
444
445    // Don't clear deltime until after we've ascertained that b64keydata is valid
446    info->deltime = 0;
447
448    while (*p && (*p) != info) p=&(*p)->next;
449    if (*p) {LogInfo("mDNS_SetSecretForDomain: Domain %##s Already in list", (*p)->domain.c); return(mStatus_AlreadyRegistered);}
450
451    // Caution: Only zero AutoTunnelHostRecord.namestorage AFTER we've determined that this is a NEW DomainAuthInfo
452    // being added to the list. Otherwise we risk smashing our AutoTunnel host records that are already active and in use.
453    info->AutoTunnelHostRecord.resrec.RecordType = kDNSRecordTypeUnregistered;
454    info->AutoTunnelHostRecord.namestorage.c[0] = 0;
455    info->AutoTunnelTarget.resrec.RecordType = kDNSRecordTypeUnregistered;
456    info->AutoTunnelDeviceInfo.resrec.RecordType = kDNSRecordTypeUnregistered;
457    info->AutoTunnelService.resrec.RecordType = kDNSRecordTypeUnregistered;
458    info->AutoTunnel6Record.resrec.RecordType = kDNSRecordTypeUnregistered;
459    info->AutoTunnelServiceStarted = mDNSfalse;
460    info->AutoTunnelInnerAddress = zerov6Addr;
461    info->next = mDNSNULL;
462    *p = info;
463
464    // Check to see if adding this new DomainAuthInfo has changed the credentials for any of our questions
465    for (q = m->Questions; q; q=q->next)
466    {
467        DomainAuthInfo *newinfo = GetAuthInfoForQuestion(m, q);
468        if (q->AuthInfo != newinfo)
469        {
470            debugf("mDNS_SetSecretForDomain updating q->AuthInfo from %##s to %##s for %##s (%s)",
471                   q->AuthInfo ? q->AuthInfo->domain.c : mDNSNULL,
472                   newinfo     ? newinfo->domain.c : mDNSNULL, q->qname.c, DNSTypeName(q->qtype));
473            q->AuthInfo = newinfo;
474        }
475    }
476
477    return(mStatus_NoError);
478}
479
480// ***************************************************************************
481#if COMPILER_LIKES_PRAGMA_MARK
482#pragma mark -
483#pragma mark - NAT Traversal
484#endif
485
486// Keep track of when to request/refresh the external address using NAT-PMP or UPnP/IGD,
487// and do so when necessary
488mDNSlocal mStatus uDNS_RequestAddress(mDNS *m)
489{
490    mStatus err = mStatus_NoError;
491
492    if (!m->NATTraversals)
493    {
494        m->retryGetAddr = NonZeroTime(m->timenow + FutureTime);
495        LogInfo("uDNS_RequestAddress: Setting retryGetAddr to future");
496    }
497    else if (m->timenow - m->retryGetAddr >= 0)
498    {
499        if (mDNSv4AddrIsRFC1918(&m->Router.ip.v4))
500        {
501            static NATAddrRequest req = {NATMAP_VERS, NATOp_AddrRequest};
502            static mDNSu8* start = (mDNSu8*)&req;
503            mDNSu8* end = start + sizeof(NATAddrRequest);
504            err = mDNSPlatformSendUDP(m, start, end, 0, mDNSNULL, &m->Router, NATPMPPort, mDNSfalse);
505            debugf("uDNS_RequestAddress: Sent NAT-PMP external address request %d", err);
506
507#ifdef _LEGACY_NAT_TRAVERSAL_
508            if (mDNSIPPortIsZero(m->UPnPRouterPort) || mDNSIPPortIsZero(m->UPnPSOAPPort))
509            {
510                LNT_SendDiscoveryMsg(m);
511                debugf("uDNS_RequestAddress: LNT_SendDiscoveryMsg");
512            }
513            else
514            {
515                mStatus lnterr = LNT_GetExternalAddress(m);
516                if (lnterr)
517                    LogMsg("uDNS_RequestAddress: LNT_GetExternalAddress returned error %d", lnterr);
518
519                err = err ? err : lnterr; // NAT-PMP error takes precedence
520            }
521#endif // _LEGACY_NAT_TRAVERSAL_
522        }
523
524        // Always update the interval and retry time, so that even if we fail to send the
525        // packet, we won't spin in an infinite loop repeatedly failing to send the packet
526        if (m->retryIntervalGetAddr < NATMAP_INIT_RETRY)
527        {
528            m->retryIntervalGetAddr = NATMAP_INIT_RETRY;
529        }
530        else if (m->retryIntervalGetAddr < NATMAP_MAX_RETRY_INTERVAL / 2)
531        {
532            m->retryIntervalGetAddr *= 2;
533        }
534        else
535        {
536            m->retryIntervalGetAddr = NATMAP_MAX_RETRY_INTERVAL;
537        }
538
539        m->retryGetAddr = NonZeroTime(m->timenow + m->retryIntervalGetAddr);
540    }
541    else
542    {
543        debugf("uDNS_RequestAddress: Not time to send address request");
544    }
545
546    // Always update NextScheduledNATOp, even if we didn't change retryGetAddr, so we'll
547    // be called when we need to send the request(s)
548    if (m->NextScheduledNATOp - m->retryGetAddr > 0)
549        m->NextScheduledNATOp = m->retryGetAddr;
550
551    return err;
552}
553
554mDNSlocal mStatus uDNS_SendNATMsg(mDNS *m, NATTraversalInfo *info, mDNSBool usePCP, mDNSBool unmapping)
555{
556    mStatus err = mStatus_NoError;
557
558    if (!info)
559    {
560        LogMsg("uDNS_SendNATMsg called unexpectedly with NULL info");
561        return mStatus_BadParamErr;
562    }
563
564    // send msg if the router's address is private (which means it's non-zero)
565    if (mDNSv4AddrIsRFC1918(&m->Router.ip.v4))
566    {
567        if (!usePCP)
568        {
569            if (!info->sentNATPMP)
570            {
571                if (info->Protocol)
572                {
573                    static NATPortMapRequest NATPortReq;
574                    static const mDNSu8* end = (mDNSu8 *)&NATPortReq + sizeof(NATPortMapRequest);
575                    mDNSu8 *p = (mDNSu8 *)&NATPortReq.NATReq_lease;
576
577                    NATPortReq.vers    = NATMAP_VERS;
578                    NATPortReq.opcode  = info->Protocol;
579                    NATPortReq.unused  = zeroID;
580                    NATPortReq.intport = info->IntPort;
581                    NATPortReq.extport = info->RequestedPort;
582                    p[0] = (mDNSu8)((info->NATLease >> 24) &  0xFF);
583                    p[1] = (mDNSu8)((info->NATLease >> 16) &  0xFF);
584                    p[2] = (mDNSu8)((info->NATLease >>  8) &  0xFF);
585                    p[3] = (mDNSu8)( info->NATLease        &  0xFF);
586
587                    err = mDNSPlatformSendUDP(m, (mDNSu8 *)&NATPortReq, end, 0, mDNSNULL, &m->Router, NATPMPPort, mDNSfalse);
588                    debugf("uDNS_SendNATMsg: Sent NAT-PMP mapping request %d", err);
589                }
590
591                // In case the address request already went out for another NAT-T,
592                // set the NewAddress to the currently known global external address, so
593                // Address-only operations will get the callback immediately
594                info->NewAddress = m->ExtAddress;
595
596                // Remember that we just sent a NAT-PMP packet, so we won't resend one later.
597                // We do this because the NAT-PMP "Unsupported Version" response has no
598                // information about the (PCP) request that triggered it, so we must send
599                // NAT-PMP requests for all operations. Without this, we'll send n PCP
600                // requests for n operations, receive n NAT-PMP "Unsupported Version"
601                // responses, and send n NAT-PMP requests for each of those responses,
602                // resulting in (n + n^2) packets sent. We only want to send 2n packets:
603                // n PCP requests followed by n NAT-PMP requests.
604                info->sentNATPMP = mDNStrue;
605            }
606        }
607        else
608        {
609            PCPMapRequest req;
610            mDNSu8* start = (mDNSu8*)&req;
611            mDNSu8* end = start + sizeof(req);
612            mDNSu8* p = (mDNSu8*)&req.lifetime;
613
614            req.version = PCP_VERS;
615            req.opCode = PCPOp_Map;
616            req.reserved = zeroID;
617
618            p[0] = (mDNSu8)((info->NATLease >> 24) &  0xFF);
619            p[1] = (mDNSu8)((info->NATLease >> 16) &  0xFF);
620            p[2] = (mDNSu8)((info->NATLease >>  8) &  0xFF);
621            p[3] = (mDNSu8)( info->NATLease        &  0xFF);
622
623            mDNSAddrMapIPv4toIPv6(&m->AdvertisedV4.ip.v4, &req.clientAddr);
624
625            req.nonce[0] = m->PCPNonce[0];
626            req.nonce[1] = m->PCPNonce[1];
627            req.nonce[2] = m->PCPNonce[2];
628
629            req.protocol = (info->Protocol == NATOp_MapUDP ? PCPProto_UDP : PCPProto_TCP);
630
631            req.reservedMapOp[0] = 0;
632            req.reservedMapOp[1] = 0;
633            req.reservedMapOp[2] = 0;
634
635            req.intPort = info->Protocol ? info->IntPort : DiscardPort;
636            req.extPort = info->RequestedPort;
637
638            // Since we only support IPv4, even if using the all-zeros address, map it, so
639            // the PCP gateway will give us an IPv4 address & not an IPv6 address.
640            mDNSAddrMapIPv4toIPv6(&info->NewAddress, &req.extAddress);
641
642            err = mDNSPlatformSendUDP(m, start, end, 0, mDNSNULL, &m->Router, NATPMPPort, mDNSfalse);
643            debugf("uDNS_SendNATMsg: Sent PCP Mapping request %d", err);
644
645            // Unset the sentNATPMP flag, so that we'll send a NAT-PMP packet if we
646            // receive a NAT-PMP "Unsupported Version" packet. This will result in every
647            // renewal, retransmission, etc. being tried first as PCP, then if a NAT-PMP
648            // "Unsupported Version" response is received, fall-back & send the request
649            // using NAT-PMP.
650            info->sentNATPMP = mDNSfalse;
651
652#ifdef _LEGACY_NAT_TRAVERSAL_
653            // If an unmapping is being performed, then don't send an LNT discovery message or an LNT port map request.
654            if (!unmapping)
655            {
656                if (mDNSIPPortIsZero(m->UPnPRouterPort) || mDNSIPPortIsZero(m->UPnPSOAPPort))
657                {
658                    LNT_SendDiscoveryMsg(m);
659                    debugf("uDNS_SendNATMsg: LNT_SendDiscoveryMsg");
660                }
661                else
662                {
663                    mStatus lnterr = LNT_MapPort(m, info);
664                    if (lnterr)
665                        LogMsg("uDNS_SendNATMsg: LNT_MapPort returned error %d", lnterr);
666
667                    err = err ? err : lnterr; // PCP error takes precedence
668                }
669            }
670#else
671            (void)unmapping; // Unused
672#endif // _LEGACY_NAT_TRAVERSAL_
673        }
674    }
675
676    return(err);
677}
678
679mDNSexport void RecreateNATMappings(mDNS *const m, const mDNSu32 waitTicks)
680{
681    mDNSu32 when = NonZeroTime(m->timenow + waitTicks);
682    NATTraversalInfo *n;
683    for (n = m->NATTraversals; n; n=n->next)
684    {
685        n->ExpiryTime    = 0;       // Mark this mapping as expired
686        n->retryInterval = NATMAP_INIT_RETRY;
687        n->retryPortMap  = when;
688        n->lastSuccessfulProtocol = NATTProtocolNone;
689        if (!n->Protocol) n->NewResult = mStatus_NoError;
690#ifdef _LEGACY_NAT_TRAVERSAL_
691        if (n->tcpInfo.sock) { mDNSPlatformTCPCloseConnection(n->tcpInfo.sock); n->tcpInfo.sock = mDNSNULL; }
692#endif // _LEGACY_NAT_TRAVERSAL_
693    }
694
695    m->PCPNonce[0] = mDNSRandom(-1);
696    m->PCPNonce[1] = mDNSRandom(-1);
697    m->PCPNonce[2] = mDNSRandom(-1);
698    m->retryIntervalGetAddr = 0;
699    m->retryGetAddr = when;
700
701#ifdef _LEGACY_NAT_TRAVERSAL_
702    LNT_ClearState(m);
703#endif // _LEGACY_NAT_TRAVERSAL_
704
705    m->NextScheduledNATOp = m->timenow;     // Need to send packets immediately
706}
707
708mDNSexport void natTraversalHandleAddressReply(mDNS *const m, mDNSu16 err, mDNSv4Addr ExtAddr)
709{
710    static mDNSu16 last_err = 0;
711    NATTraversalInfo *n;
712
713    if (err)
714    {
715        if (err != last_err) LogMsg("Error getting external address %d", err);
716        ExtAddr = zerov4Addr;
717    }
718    else
719    {
720        LogInfo("Received external IP address %.4a from NAT", &ExtAddr);
721        if (mDNSv4AddrIsRFC1918(&ExtAddr))
722            LogMsg("Double NAT (external NAT gateway address %.4a is also a private RFC 1918 address)", &ExtAddr);
723        if (mDNSIPv4AddressIsZero(ExtAddr))
724            err = NATErr_NetFail; // fake error to handle routers that pathologically report success with the zero address
725    }
726
727    // Globally remember the most recently discovered address, so it can be used in each
728    // new NATTraversal structure
729    m->ExtAddress = ExtAddr;
730
731    if (!err) // Success, back-off to maximum interval
732        m->retryIntervalGetAddr = NATMAP_MAX_RETRY_INTERVAL;
733    else if (!last_err) // Failure after success, retry quickly (then back-off exponentially)
734        m->retryIntervalGetAddr = NATMAP_INIT_RETRY;
735    // else back-off normally in case of pathological failures
736
737    m->retryGetAddr = m->timenow + m->retryIntervalGetAddr;
738    if (m->NextScheduledNATOp - m->retryGetAddr > 0)
739        m->NextScheduledNATOp = m->retryGetAddr;
740
741    last_err = err;
742
743    for (n = m->NATTraversals; n; n=n->next)
744    {
745        // We should change n->NewAddress only when n is one of:
746        // 1) a mapping operation that most recently succeeded using NAT-PMP or UPnP/IGD,
747        //    because such an operation needs the update now. If the lastSuccessfulProtocol
748        //    is currently none, then natTraversalHandlePortMapReplyWithAddress() will be
749        //    called should NAT-PMP or UPnP/IGD succeed in the future.
750        // 2) an address-only operation that did not succeed via PCP, because when such an
751        //    operation succeeds via PCP, it's for the TCP discard port just to learn the
752        //    address. And that address may be different than the external address
753        //    discovered via NAT-PMP or UPnP/IGD. If the lastSuccessfulProtocol
754        //    is currently none, we must update the NewAddress as PCP may not succeed.
755        if (!mDNSSameIPv4Address(n->NewAddress, ExtAddr) &&
756             (n->Protocol ?
757               (n->lastSuccessfulProtocol == NATTProtocolNATPMP || n->lastSuccessfulProtocol == NATTProtocolUPNPIGD) :
758               (n->lastSuccessfulProtocol != NATTProtocolPCP)))
759        {
760            // Needs an update immediately
761            n->NewAddress    = ExtAddr;
762            n->ExpiryTime    = 0;
763            n->retryInterval = NATMAP_INIT_RETRY;
764            n->retryPortMap  = m->timenow;
765#ifdef _LEGACY_NAT_TRAVERSAL_
766            if (n->tcpInfo.sock) { mDNSPlatformTCPCloseConnection(n->tcpInfo.sock); n->tcpInfo.sock = mDNSNULL; }
767#endif // _LEGACY_NAT_TRAVERSAL_
768
769            m->NextScheduledNATOp = m->timenow;     // Need to send packets immediately
770        }
771    }
772}
773
774// Both places that call NATSetNextRenewalTime() update m->NextScheduledNATOp correctly afterwards
775mDNSlocal void NATSetNextRenewalTime(mDNS *const m, NATTraversalInfo *n)
776{
777    n->retryInterval = (n->ExpiryTime - m->timenow)/2;
778    if (n->retryInterval < NATMAP_MIN_RETRY_INTERVAL)   // Min retry interval is 2 seconds
779        n->retryInterval = NATMAP_MIN_RETRY_INTERVAL;
780    n->retryPortMap = m->timenow + n->retryInterval;
781}
782
783mDNSlocal void natTraversalHandlePortMapReplyWithAddress(mDNS *const m, NATTraversalInfo *n, const mDNSInterfaceID InterfaceID, mDNSu16 err, mDNSv4Addr extaddr, mDNSIPPort extport, mDNSu32 lease, NATTProtocol protocol)
784{
785    const char *prot = n->Protocol == 0 ? "Add" : n->Protocol == NATOp_MapUDP ? "UDP" : n->Protocol == NATOp_MapTCP ? "TCP" : "???";
786    (void)prot;
787    n->NewResult = err;
788    if (err || lease == 0 || mDNSIPPortIsZero(extport))
789    {
790        LogInfo("natTraversalHandlePortMapReplyWithAddress: %p Response %s Port %5d External %.4a:%d lease %d error %d",
791                n, prot, mDNSVal16(n->IntPort), &extaddr, mDNSVal16(extport), lease, err);
792        n->retryInterval = NATMAP_MAX_RETRY_INTERVAL;
793        n->retryPortMap = m->timenow + NATMAP_MAX_RETRY_INTERVAL;
794        // No need to set m->NextScheduledNATOp here, since we're only ever extending the m->retryPortMap time
795        if      (err == NATErr_Refused) n->NewResult = mStatus_NATPortMappingDisabled;
796        else if (err > NATErr_None && err <= NATErr_Opcode) n->NewResult = mStatus_NATPortMappingUnsupported;
797    }
798    else
799    {
800        if (lease > 999999999UL / mDNSPlatformOneSecond)
801            lease = 999999999UL / mDNSPlatformOneSecond;
802        n->ExpiryTime = NonZeroTime(m->timenow + lease * mDNSPlatformOneSecond);
803
804        if (!mDNSSameIPv4Address(n->NewAddress, extaddr) || !mDNSSameIPPort(n->RequestedPort, extport))
805            LogInfo("natTraversalHandlePortMapReplyWithAddress: %p %s Response %s Port %5d External %.4a:%d changed to %.4a:%d lease %d",
806                    n,
807                    (n->lastSuccessfulProtocol == NATTProtocolNone    ? "None    " :
808                     n->lastSuccessfulProtocol == NATTProtocolNATPMP  ? "NAT-PMP " :
809                     n->lastSuccessfulProtocol == NATTProtocolUPNPIGD ? "UPnP/IGD" :
810                     n->lastSuccessfulProtocol == NATTProtocolPCP     ? "PCP     " :
811                     /* else */                                         "Unknown " ),
812                    prot, mDNSVal16(n->IntPort), &n->NewAddress, mDNSVal16(n->RequestedPort),
813                    &extaddr, mDNSVal16(extport), lease);
814
815        n->InterfaceID   = InterfaceID;
816        n->NewAddress    = extaddr;
817        if (n->Protocol) n->RequestedPort = extport; // Don't report the (PCP) external port to address-only operations
818        n->lastSuccessfulProtocol = protocol;
819
820        NATSetNextRenewalTime(m, n);            // Got our port mapping; now set timer to renew it at halfway point
821        m->NextScheduledNATOp = m->timenow;     // May need to invoke client callback immediately
822    }
823}
824
825// To be called for NAT-PMP or UPnP/IGD mappings, to use currently discovered (global) address
826mDNSexport void natTraversalHandlePortMapReply(mDNS *const m, NATTraversalInfo *n, const mDNSInterfaceID InterfaceID, mDNSu16 err, mDNSIPPort extport, mDNSu32 lease, NATTProtocol protocol)
827{
828    natTraversalHandlePortMapReplyWithAddress(m, n, InterfaceID, err, m->ExtAddress, extport, lease, protocol);
829}
830
831// Must be called with the mDNS_Lock held
832mDNSexport mStatus mDNS_StartNATOperation_internal(mDNS *const m, NATTraversalInfo *traversal)
833{
834    NATTraversalInfo **n;
835
836    LogInfo("mDNS_StartNATOperation_internal %p Protocol %d IntPort %d RequestedPort %d NATLease %d", traversal,
837            traversal->Protocol, mDNSVal16(traversal->IntPort), mDNSVal16(traversal->RequestedPort), traversal->NATLease);
838
839    // Note: It important that new traversal requests are appended at the *end* of the list, not prepended at the start
840    for (n = &m->NATTraversals; *n; n=&(*n)->next)
841    {
842        if (traversal == *n)
843        {
844            LogFatalError("Error! Tried to add a NAT traversal that's already in the active list: request %p Prot %d Int %d TTL %d",
845                   traversal, traversal->Protocol, mDNSVal16(traversal->IntPort), traversal->NATLease);
846            return(mStatus_AlreadyRegistered);
847        }
848        if (traversal->Protocol && traversal->Protocol == (*n)->Protocol && mDNSSameIPPort(traversal->IntPort, (*n)->IntPort) &&
849            !mDNSSameIPPort(traversal->IntPort, SSHPort))
850            LogMsg("Warning: Created port mapping request %p Prot %d Int %d TTL %d "
851                   "duplicates existing port mapping request %p Prot %d Int %d TTL %d",
852                   traversal, traversal->Protocol, mDNSVal16(traversal->IntPort), traversal->NATLease,
853                   *n,        (*n)->Protocol, mDNSVal16((*n)->IntPort), (*n)->NATLease);
854    }
855
856    // Initialize necessary fields
857    traversal->next            = mDNSNULL;
858    traversal->ExpiryTime      = 0;
859    traversal->retryInterval   = NATMAP_INIT_RETRY;
860    traversal->retryPortMap    = m->timenow;
861    traversal->NewResult       = mStatus_NoError;
862    traversal->lastSuccessfulProtocol = NATTProtocolNone;
863    traversal->sentNATPMP      = mDNSfalse;
864    traversal->ExternalAddress = onesIPv4Addr;
865    traversal->NewAddress      = zerov4Addr;
866    traversal->ExternalPort    = zeroIPPort;
867    traversal->Lifetime        = 0;
868    traversal->Result          = mStatus_NoError;
869
870    // set default lease if necessary
871    if (!traversal->NATLease) traversal->NATLease = NATMAP_DEFAULT_LEASE;
872
873#ifdef _LEGACY_NAT_TRAVERSAL_
874    mDNSPlatformMemZero(&traversal->tcpInfo, sizeof(traversal->tcpInfo));
875#endif // _LEGACY_NAT_TRAVERSAL_
876
877    if (!m->NATTraversals)      // If this is our first NAT request, kick off an address request too
878    {
879        m->retryGetAddr         = m->timenow;
880        m->retryIntervalGetAddr = NATMAP_INIT_RETRY;
881    }
882
883    // If this is an address-only operation, initialize to the current global address,
884    // or (in non-PCP environments) we won't know the address until the next external
885    // address request/response.
886    if (!traversal->Protocol)
887    {
888        traversal->NewAddress = m->ExtAddress;
889    }
890
891    m->NextScheduledNATOp = m->timenow; // This will always trigger sending the packet ASAP, and generate client callback if necessary
892
893    *n = traversal;     // Append new NATTraversalInfo to the end of our list
894
895    return(mStatus_NoError);
896}
897
898// Must be called with the mDNS_Lock held
899mDNSexport mStatus mDNS_StopNATOperation_internal(mDNS *m, NATTraversalInfo *traversal)
900{
901    mDNSBool unmap = mDNStrue;
902    NATTraversalInfo *p;
903    NATTraversalInfo **ptr = &m->NATTraversals;
904
905    while (*ptr && *ptr != traversal) ptr=&(*ptr)->next;
906    if (*ptr) *ptr = (*ptr)->next;      // If we found it, cut this NATTraversalInfo struct from our list
907    else
908    {
909        LogMsg("mDNS_StopNATOperation_internal: NATTraversalInfo %p not found in list", traversal);
910        return(mStatus_BadReferenceErr);
911    }
912
913    LogInfo("mDNS_StopNATOperation_internal %p %d %d %d %d", traversal,
914            traversal->Protocol, mDNSVal16(traversal->IntPort), mDNSVal16(traversal->RequestedPort), traversal->NATLease);
915
916    if (m->CurrentNATTraversal == traversal)
917        m->CurrentNATTraversal = m->CurrentNATTraversal->next;
918
919    // If there is a match for the operation being stopped, don't send a deletion request (unmap)
920    for (p = m->NATTraversals; p; p=p->next)
921    {
922        if (traversal->Protocol ?
923            ((traversal->Protocol == p->Protocol && mDNSSameIPPort(traversal->IntPort, p->IntPort)) ||
924             (!p->Protocol && traversal->Protocol == NATOp_MapTCP && mDNSSameIPPort(traversal->IntPort, DiscardPort))) :
925            (!p->Protocol || (p->Protocol == NATOp_MapTCP && mDNSSameIPPort(p->IntPort, DiscardPort))))
926        {
927            LogInfo("Warning: Removed port mapping request %p Prot %d Int %d TTL %d "
928                    "duplicates existing port mapping request %p Prot %d Int %d TTL %d",
929                    traversal, traversal->Protocol, mDNSVal16(traversal->IntPort), traversal->NATLease,
930                            p,         p->Protocol, mDNSVal16(        p->IntPort),         p->NATLease);
931            unmap = mDNSfalse;
932        }
933    }
934
935    // Even if we DIDN'T make a successful UPnP mapping yet, we might still have a partially-open TCP connection we need to clean up
936    // Before zeroing traversal->RequestedPort below, perform the LNT unmapping, which requires the mapping's external port,
937    // held by the traversal->RequestedPort variable.
938    #ifdef _LEGACY_NAT_TRAVERSAL_
939    {
940        mStatus err = LNT_UnmapPort(m, traversal);
941        if (err) LogMsg("Legacy NAT Traversal - unmap request failed with error %d", err);
942    }
943    #endif // _LEGACY_NAT_TRAVERSAL_
944
945    if (traversal->ExpiryTime && unmap)
946    {
947        traversal->NATLease = 0;
948        traversal->retryInterval = 0;
949
950        // In case we most recently sent NAT-PMP, we need to set sentNATPMP to false so
951        // that we'll send a NAT-PMP request to destroy the mapping. We do this because
952        // the NATTraversal struct has already been cut from the list, and the client
953        // layer will destroy the memory upon returning from this function, so we can't
954        // try PCP first and then fall-back to NAT-PMP. That is, if we most recently
955        // created/renewed the mapping using NAT-PMP, we need to destroy it using NAT-PMP
956        // now, because we won't get a chance later.
957        traversal->sentNATPMP = mDNSfalse;
958
959        // Both NAT-PMP & PCP RFCs state that the suggested port in deletion requests
960        // should be zero. And for PCP, the suggested external address should also be
961        // zero, specifically, the all-zeros IPv4-mapped address, since we would only
962        // would have requested an IPv4 address.
963        traversal->RequestedPort = zeroIPPort;
964        traversal->NewAddress = zerov4Addr;
965
966        uDNS_SendNATMsg(m, traversal, traversal->lastSuccessfulProtocol != NATTProtocolNATPMP, mDNStrue);
967    }
968
969    return(mStatus_NoError);
970}
971
972mDNSexport mStatus mDNS_StartNATOperation(mDNS *const m, NATTraversalInfo *traversal)
973{
974    mStatus status;
975    mDNS_Lock(m);
976    status = mDNS_StartNATOperation_internal(m, traversal);
977    mDNS_Unlock(m);
978    return(status);
979}
980
981mDNSexport mStatus mDNS_StopNATOperation(mDNS *const m, NATTraversalInfo *traversal)
982{
983    mStatus status;
984    mDNS_Lock(m);
985    status = mDNS_StopNATOperation_internal(m, traversal);
986    mDNS_Unlock(m);
987    return(status);
988}
989
990// ***************************************************************************
991#if COMPILER_LIKES_PRAGMA_MARK
992#pragma mark -
993#pragma mark - Long-Lived Queries
994#endif
995
996// Lock must be held -- otherwise m->timenow is undefined
997mDNSlocal void StartLLQPolling(mDNS *const m, DNSQuestion *q)
998{
999    debugf("StartLLQPolling: %##s", q->qname.c);
1000    q->state = LLQ_Poll;
1001    q->ThisQInterval = INIT_UCAST_POLL_INTERVAL;
1002    // We want to send our poll query ASAP, but the "+ 1" is because if we set the time to now,
1003    // we risk causing spurious "SendQueries didn't send all its queries" log messages
1004    q->LastQTime     = m->timenow - q->ThisQInterval + 1;
1005    SetNextQueryTime(m, q);
1006#if APPLE_OSX_mDNSResponder
1007    UpdateAutoTunnelDomainStatuses(m);
1008#endif
1009}
1010
1011mDNSlocal mDNSu8 *putLLQ(DNSMessage *const msg, mDNSu8 *ptr, const DNSQuestion *const question, const LLQOptData *const data)
1012{
1013    AuthRecord rr;
1014    ResourceRecord *opt = &rr.resrec;
1015    rdataOPT *optRD;
1016
1017    //!!!KRS when we implement multiple llqs per message, we'll need to memmove anything past the question section
1018    ptr = putQuestion(msg, ptr, msg->data + AbsoluteMaxDNSMessageData, &question->qname, question->qtype, question->qclass);
1019    if (!ptr) { LogMsg("ERROR: putLLQ - putQuestion"); return mDNSNULL; }
1020
1021    // locate OptRR if it exists, set pointer to end
1022    // !!!KRS implement me
1023
1024    // format opt rr (fields not specified are zero-valued)
1025    mDNS_SetupResourceRecord(&rr, mDNSNULL, mDNSInterface_Any, kDNSType_OPT, kStandardTTL, kDNSRecordTypeKnownUnique, AuthRecordAny, mDNSNULL, mDNSNULL);
1026    opt->rrclass    = NormalMaxDNSMessageData;
1027    opt->rdlength   = sizeof(rdataOPT); // One option in this OPT record
1028    opt->rdestimate = sizeof(rdataOPT);
1029
1030    optRD = &rr.resrec.rdata->u.opt[0];
1031    optRD->opt = kDNSOpt_LLQ;
1032    optRD->u.llq = *data;
1033    ptr = PutResourceRecordTTLJumbo(msg, ptr, &msg->h.numAdditionals, opt, 0);
1034    if (!ptr) { LogMsg("ERROR: putLLQ - PutResourceRecordTTLJumbo"); return mDNSNULL; }
1035
1036    return ptr;
1037}
1038
1039// Normally we'd just request event packets be sent directly to m->LLQNAT.ExternalPort, except...
1040// with LLQs over TLS/TCP we're doing a weird thing where instead of requesting packets be sent to ExternalAddress:ExternalPort
1041// we're requesting that packets be sent to ExternalPort, but at the source address of our outgoing TCP connection.
1042// Normally, after going through the NAT gateway, the source address of our outgoing TCP connection is the same as ExternalAddress,
1043// so this is fine, except when the TCP connection ends up going over a VPN tunnel instead.
1044// To work around this, if we find that the source address for our TCP connection is not a private address, we tell the Dot Mac
1045// LLQ server to send events to us directly at port 5353 on that address, instead of at our mapped external NAT port.
1046
1047mDNSlocal mDNSu16 GetLLQEventPort(const mDNS *const m, const mDNSAddr *const dst)
1048{
1049    mDNSAddr src;
1050    mDNSPlatformSourceAddrForDest(&src, dst);
1051    //LogMsg("GetLLQEventPort: src %#a for dst %#a (%d)", &src, dst, mDNSv4AddrIsRFC1918(&src.ip.v4) ? mDNSVal16(m->LLQNAT.ExternalPort) : 0);
1052    return(mDNSv4AddrIsRFC1918(&src.ip.v4) ? mDNSVal16(m->LLQNAT.ExternalPort) : mDNSVal16(MulticastDNSPort));
1053}
1054
1055// Normally called with llq set.
1056// May be called with llq NULL, when retransmitting a lost Challenge Response
1057mDNSlocal void sendChallengeResponse(mDNS *const m, DNSQuestion *const q, const LLQOptData *llq)
1058{
1059    mDNSu8 *responsePtr = m->omsg.data;
1060    LLQOptData llqBuf;
1061
1062    if (q->tcp) { LogMsg("sendChallengeResponse: ERROR!!: question %##s (%s) tcp non-NULL", q->qname.c, DNSTypeName(q->qtype)); return; }
1063
1064    if (PrivateQuery(q)) { LogMsg("sendChallengeResponse: ERROR!!: Private Query %##s (%s)", q->qname.c, DNSTypeName(q->qtype)); return; }
1065
1066    if (q->ntries++ == kLLQ_MAX_TRIES)
1067    {
1068        LogMsg("sendChallengeResponse: %d failed attempts for LLQ %##s", kLLQ_MAX_TRIES, q->qname.c);
1069        StartLLQPolling(m,q);
1070        return;
1071    }
1072
1073    if (!llq)       // Retransmission: need to make a new LLQOptData
1074    {
1075        llqBuf.vers     = kLLQ_Vers;
1076        llqBuf.llqOp    = kLLQOp_Setup;
1077        llqBuf.err      = LLQErr_NoError;   // Don't need to tell server UDP notification port when sending over UDP
1078        llqBuf.id       = q->id;
1079        llqBuf.llqlease = q->ReqLease;
1080        llq = &llqBuf;
1081    }
1082
1083    q->LastQTime     = m->timenow;
1084    q->ThisQInterval = q->tcp ? 0 : (kLLQ_INIT_RESEND * q->ntries * mDNSPlatformOneSecond);     // If using TCP, don't need to retransmit
1085    SetNextQueryTime(m, q);
1086
1087    // To simulate loss of challenge response packet, uncomment line below
1088    //if (q->ntries == 1) return;
1089
1090    InitializeDNSMessage(&m->omsg.h, q->TargetQID, uQueryFlags);
1091    responsePtr = putLLQ(&m->omsg, responsePtr, q, llq);
1092    if (responsePtr)
1093    {
1094        mStatus err = mDNSSendDNSMessage(m, &m->omsg, responsePtr, mDNSInterface_Any, q->LocalSocket, &q->servAddr, q->servPort, mDNSNULL, mDNSNULL, mDNSfalse);
1095        if (err) { LogMsg("sendChallengeResponse: mDNSSendDNSMessage%s failed: %d", q->tcp ? " (TCP)" : "", err); }
1096    }
1097    else StartLLQPolling(m,q);
1098}
1099
1100mDNSlocal void SetLLQTimer(mDNS *const m, DNSQuestion *const q, const LLQOptData *const llq)
1101{
1102    mDNSs32 lease = (mDNSs32)llq->llqlease * mDNSPlatformOneSecond;
1103    q->ReqLease      = llq->llqlease;
1104    q->LastQTime     = m->timenow;
1105    q->expire        = m->timenow + lease;
1106    q->ThisQInterval = lease/2 + mDNSRandom(lease/10);
1107    debugf("SetLLQTimer setting %##s (%s) to %d %d", q->qname.c, DNSTypeName(q->qtype), lease/mDNSPlatformOneSecond, q->ThisQInterval/mDNSPlatformOneSecond);
1108    SetNextQueryTime(m, q);
1109}
1110
1111mDNSlocal void recvSetupResponse(mDNS *const m, mDNSu8 rcode, DNSQuestion *const q, const LLQOptData *const llq)
1112{
1113    if (rcode && rcode != kDNSFlag1_RC_NXDomain)
1114    { LogMsg("ERROR: recvSetupResponse %##s (%s) - rcode && rcode != kDNSFlag1_RC_NXDomain", q->qname.c, DNSTypeName(q->qtype)); return; }
1115
1116    if (llq->llqOp != kLLQOp_Setup)
1117    { LogMsg("ERROR: recvSetupResponse %##s (%s) - bad op %d", q->qname.c, DNSTypeName(q->qtype), llq->llqOp); return; }
1118
1119    if (llq->vers != kLLQ_Vers)
1120    { LogMsg("ERROR: recvSetupResponse %##s (%s) - bad vers %d", q->qname.c, DNSTypeName(q->qtype), llq->vers); return; }
1121
1122    if (q->state == LLQ_InitialRequest)
1123    {
1124        //LogInfo("Got LLQ_InitialRequest");
1125
1126        if (llq->err) { LogMsg("recvSetupResponse - received llq->err %d from server", llq->err); StartLLQPolling(m,q); return; }
1127
1128        if (q->ReqLease != llq->llqlease)
1129            debugf("recvSetupResponse: requested lease %lu, granted lease %lu", q->ReqLease, llq->llqlease);
1130
1131        // cache expiration in case we go to sleep before finishing setup
1132        q->ReqLease = llq->llqlease;
1133        q->expire = m->timenow + ((mDNSs32)llq->llqlease * mDNSPlatformOneSecond);
1134
1135        // update state
1136        q->state  = LLQ_SecondaryRequest;
1137        q->id     = llq->id;
1138        q->ntries = 0; // first attempt to send response
1139        sendChallengeResponse(m, q, llq);
1140    }
1141    else if (q->state == LLQ_SecondaryRequest)
1142    {
1143        //LogInfo("Got LLQ_SecondaryRequest");
1144
1145        // Fix this immediately if not sooner.  Copy the id from the LLQOptData into our DNSQuestion struct.  This is only
1146        // an issue for private LLQs, because we skip parts 2 and 3 of the handshake.  This is related to a bigger
1147        // problem of the current implementation of TCP LLQ setup: we're not handling state transitions correctly
1148        // if the server sends back SERVFULL or STATIC.
1149        if (PrivateQuery(q))
1150        {
1151            LogInfo("Private LLQ_SecondaryRequest; copying id %08X%08X", llq->id.l[0], llq->id.l[1]);
1152            q->id = llq->id;
1153        }
1154
1155        if (llq->err) { LogMsg("ERROR: recvSetupResponse %##s (%s) code %d from server", q->qname.c, DNSTypeName(q->qtype), llq->err); StartLLQPolling(m,q); return; }
1156        if (!mDNSSameOpaque64(&q->id, &llq->id))
1157        { LogMsg("recvSetupResponse - ID changed.  discarding"); return; }     // this can happen rarely (on packet loss + reordering)
1158        q->state         = LLQ_Established;
1159        q->ntries        = 0;
1160        SetLLQTimer(m, q, llq);
1161#if APPLE_OSX_mDNSResponder
1162        UpdateAutoTunnelDomainStatuses(m);
1163#endif
1164    }
1165}
1166
1167mDNSexport uDNS_LLQType uDNS_recvLLQResponse(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *const end,
1168                                             const mDNSAddr *const srcaddr, const mDNSIPPort srcport, DNSQuestion **matchQuestion)
1169{
1170    DNSQuestion pktQ, *q;
1171    if (msg->h.numQuestions && getQuestion(msg, msg->data, end, 0, &pktQ))
1172    {
1173        const rdataOPT *opt = GetLLQOptData(m, msg, end);
1174
1175        for (q = m->Questions; q; q = q->next)
1176        {
1177            if (!mDNSOpaque16IsZero(q->TargetQID) && q->LongLived && q->qtype == pktQ.qtype && q->qnamehash == pktQ.qnamehash && SameDomainName(&q->qname, &pktQ.qname))
1178            {
1179                debugf("uDNS_recvLLQResponse found %##s (%s) %d %#a %#a %X %X %X %X %d",
1180                       q->qname.c, DNSTypeName(q->qtype), q->state, srcaddr, &q->servAddr,
1181                       opt ? opt->u.llq.id.l[0] : 0, opt ? opt->u.llq.id.l[1] : 0, q->id.l[0], q->id.l[1], opt ? opt->u.llq.llqOp : 0);
1182                if (q->state == LLQ_Poll) debugf("uDNS_LLQ_Events: q->state == LLQ_Poll msg->h.id %d q->TargetQID %d", mDNSVal16(msg->h.id), mDNSVal16(q->TargetQID));
1183                if (q->state == LLQ_Poll && mDNSSameOpaque16(msg->h.id, q->TargetQID))
1184                {
1185                    m->rec.r.resrec.RecordType = 0;     // Clear RecordType to show we're not still using it
1186
1187                    // Don't reset the state to IntialRequest as we may write that to the dynamic store
1188                    // and PrefPane might wrongly think that we are "Starting" instead of "Polling". If
1189                    // we are in polling state because of PCP/NAT-PMP disabled or DoubleNAT, next LLQNATCallback
1190                    // would kick us back to LLQInitialRequest. So, resetting the state here may not be useful.
1191                    //
1192                    // If we have a good NAT (neither PCP/NAT-PMP disabled nor Double-NAT), then we should not be
1193                    // possibly in polling state. To be safe, we want to retry from the start in that case
1194                    // as there may not be another LLQNATCallback
1195                    //
1196                    // NOTE: We can be in polling state if we cannot resolve the SOA record i.e, servAddr is set to
1197                    // all ones. In that case, we would set it in LLQ_InitialRequest as it overrides the PCP/NAT-PMP or
1198                    // Double-NAT state.
1199                    if (!mDNSAddressIsOnes(&q->servAddr) && !mDNSIPPortIsZero(m->LLQNAT.ExternalPort) &&
1200                        !m->LLQNAT.Result)
1201                    {
1202                        debugf("uDNS_recvLLQResponse got poll response; moving to LLQ_InitialRequest for %##s (%s)", q->qname.c, DNSTypeName(q->qtype));
1203                        q->state         = LLQ_InitialRequest;
1204                    }
1205                    q->servPort      = zeroIPPort;      // Clear servPort so that startLLQHandshake will retry the GetZoneData processing
1206                    q->ThisQInterval = LLQ_POLL_INTERVAL + mDNSRandom(LLQ_POLL_INTERVAL/10);    // Retry LLQ setup in approx 15 minutes
1207                    q->LastQTime     = m->timenow;
1208                    SetNextQueryTime(m, q);
1209                    *matchQuestion = q;
1210                    return uDNS_LLQ_Entire;     // uDNS_LLQ_Entire means flush stale records; assume a large effective TTL
1211                }
1212                // Note: In LLQ Event packets, the msg->h.id does not match our q->TargetQID, because in that case the msg->h.id nonce is selected by the server
1213                else if (opt && q->state == LLQ_Established && opt->u.llq.llqOp == kLLQOp_Event && mDNSSameOpaque64(&opt->u.llq.id, &q->id))
1214                {
1215                    mDNSu8 *ackEnd;
1216                    //debugf("Sending LLQ ack for %##s (%s)", q->qname.c, DNSTypeName(q->qtype));
1217                    InitializeDNSMessage(&m->omsg.h, msg->h.id, ResponseFlags);
1218                    ackEnd = putLLQ(&m->omsg, m->omsg.data, q, &opt->u.llq);
1219                    if (ackEnd) mDNSSendDNSMessage(m, &m->omsg, ackEnd, mDNSInterface_Any, q->LocalSocket, srcaddr, srcport, mDNSNULL, mDNSNULL, mDNSfalse);
1220                    m->rec.r.resrec.RecordType = 0;     // Clear RecordType to show we're not still using it
1221                    debugf("uDNS_LLQ_Events: q->state == LLQ_Established msg->h.id %d q->TargetQID %d", mDNSVal16(msg->h.id), mDNSVal16(q->TargetQID));
1222                    *matchQuestion = q;
1223                    return uDNS_LLQ_Events;
1224                }
1225                if (opt && mDNSSameOpaque16(msg->h.id, q->TargetQID))
1226                {
1227                    if (q->state == LLQ_Established && opt->u.llq.llqOp == kLLQOp_Refresh && mDNSSameOpaque64(&opt->u.llq.id, &q->id) && msg->h.numAdditionals && !msg->h.numAnswers)
1228                    {
1229                        if (opt->u.llq.err != LLQErr_NoError) LogMsg("recvRefreshReply: received error %d from server", opt->u.llq.err);
1230                        else
1231                        {
1232                            //LogInfo("Received refresh confirmation ntries %d for %##s (%s)", q->ntries, q->qname.c, DNSTypeName(q->qtype));
1233                            // If we're waiting to go to sleep, then this LLQ deletion may have been the thing
1234                            // we were waiting for, so schedule another check to see if we can sleep now.
1235                            if (opt->u.llq.llqlease == 0 && m->SleepLimit) m->NextScheduledSPRetry = m->timenow;
1236                            GrantCacheExtensions(m, q, opt->u.llq.llqlease);
1237                            SetLLQTimer(m, q, &opt->u.llq);
1238                            q->ntries = 0;
1239                        }
1240                        m->rec.r.resrec.RecordType = 0;     // Clear RecordType to show we're not still using it
1241                        *matchQuestion = q;
1242                        return uDNS_LLQ_Ignore;
1243                    }
1244                    if (q->state < LLQ_Established && mDNSSameAddress(srcaddr, &q->servAddr))
1245                    {
1246                        LLQ_State oldstate = q->state;
1247                        recvSetupResponse(m, msg->h.flags.b[1] & kDNSFlag1_RC_Mask, q, &opt->u.llq);
1248                        m->rec.r.resrec.RecordType = 0;     // Clear RecordType to show we're not still using it
1249                        // We have a protocol anomaly here in the LLQ definition.
1250                        // Both the challenge packet from the server and the ack+answers packet have opt->u.llq.llqOp == kLLQOp_Setup.
1251                        // However, we need to treat them differently:
1252                        // The challenge packet has no answers in it, and tells us nothing about whether our cache entries
1253                        // are still valid, so this packet should not cause us to do anything that messes with our cache.
1254                        // The ack+answers packet gives us the whole truth, so we should handle it by updating our cache
1255                        // to match the answers in the packet, and only the answers in the packet.
1256                        *matchQuestion = q;
1257                        return (oldstate == LLQ_SecondaryRequest ? uDNS_LLQ_Entire : uDNS_LLQ_Ignore);
1258                    }
1259                }
1260            }
1261        }
1262        m->rec.r.resrec.RecordType = 0;     // Clear RecordType to show we're not still using it
1263    }
1264    *matchQuestion = mDNSNULL;
1265    return uDNS_LLQ_Not;
1266}
1267
1268// Stub definition of TCPSocket_struct so we can access flags field. (Rest of TCPSocket_struct is platform-dependent.)
1269struct TCPSocket_struct { TCPSocketFlags flags; /* ... */ };
1270
1271// tcpCallback is called to handle events (e.g. connection opening and data reception) on TCP connections for
1272// Private DNS operations -- private queries, private LLQs, private record updates and private service updates
1273mDNSlocal void tcpCallback(TCPSocket *sock, void *context, mDNSBool ConnectionEstablished, mStatus err)
1274{
1275    tcpInfo_t *tcpInfo = (tcpInfo_t *)context;
1276    mDNSBool closed  = mDNSfalse;
1277    mDNS      *m       = tcpInfo->m;
1278    DNSQuestion *const q = tcpInfo->question;
1279    tcpInfo_t **backpointer =
1280        q                 ? &q->tcp :
1281        tcpInfo->rr       ? &tcpInfo->rr->tcp : mDNSNULL;
1282    if (backpointer && *backpointer != tcpInfo)
1283        LogMsg("tcpCallback: %d backpointer %p incorrect tcpInfo %p question %p rr %p",
1284               mDNSPlatformTCPGetFD(tcpInfo->sock), *backpointer, tcpInfo, q, tcpInfo->rr);
1285
1286    if (err) goto exit;
1287
1288    if (ConnectionEstablished)
1289    {
1290        mDNSu8    *end = ((mDNSu8*) &tcpInfo->request) + tcpInfo->requestLen;
1291        DomainAuthInfo *AuthInfo;
1292
1293        // Defensive coding for <rdar://problem/5546824> Crash in mDNSResponder at GetAuthInfoForName_internal + 366
1294        // Don't know yet what's causing this, but at least we can be cautious and try to avoid crashing if we find our pointers in an unexpected state
1295        if (tcpInfo->rr && tcpInfo->rr->resrec.name != &tcpInfo->rr->namestorage)
1296            LogMsg("tcpCallback: ERROR: tcpInfo->rr->resrec.name %p != &tcpInfo->rr->namestorage %p",
1297                   tcpInfo->rr->resrec.name, &tcpInfo->rr->namestorage);
1298        if (tcpInfo->rr  && tcpInfo->rr->resrec.name != &tcpInfo->rr->namestorage) return;
1299
1300        AuthInfo =  tcpInfo->rr  ? GetAuthInfoForName(m, tcpInfo->rr->resrec.name)         : mDNSNULL;
1301
1302        // connection is established - send the message
1303        if (q && q->LongLived && q->state == LLQ_Established)
1304        {
1305            // Lease renewal over TCP, resulting from opening a TCP connection in sendLLQRefresh
1306            end = ((mDNSu8*) &tcpInfo->request) + tcpInfo->requestLen;
1307        }
1308        else if (q && q->LongLived && q->state != LLQ_Poll && !mDNSIPPortIsZero(m->LLQNAT.ExternalPort) && !mDNSIPPortIsZero(q->servPort))
1309        {
1310            // Notes:
1311            // If we have a NAT port mapping, ExternalPort is the external port
1312            // If we have a routable address so we don't need a port mapping, ExternalPort is the same as our own internal port
1313            // If we need a NAT port mapping but can't get one, then ExternalPort is zero
1314            LLQOptData llqData;         // set llq rdata
1315            llqData.vers  = kLLQ_Vers;
1316            llqData.llqOp = kLLQOp_Setup;
1317            llqData.err   = GetLLQEventPort(m, &tcpInfo->Addr); // We're using TCP; tell server what UDP port to send notifications to
1318            LogInfo("tcpCallback: eventPort %d", llqData.err);
1319            llqData.id    = zeroOpaque64;
1320            llqData.llqlease = kLLQ_DefLease;
1321            InitializeDNSMessage(&tcpInfo->request.h, q->TargetQID, uQueryFlags);
1322            end = putLLQ(&tcpInfo->request, tcpInfo->request.data, q, &llqData);
1323            if (!end) { LogMsg("ERROR: tcpCallback - putLLQ"); err = mStatus_UnknownErr; goto exit; }
1324            AuthInfo = q->AuthInfo;     // Need to add TSIG to this message
1325            q->ntries = 0; // Reset ntries so that tcp/tls connection failures don't affect sendChallengeResponse failures
1326        }
1327        else if (q)
1328        {
1329            // LLQ Polling mode or non-LLQ uDNS over TCP
1330            InitializeDNSMessage(&tcpInfo->request.h, q->TargetQID, (DNSSECQuestion(q) ? DNSSecQFlags : uQueryFlags));
1331            end = putQuestion(&tcpInfo->request, tcpInfo->request.data, tcpInfo->request.data + AbsoluteMaxDNSMessageData, &q->qname, q->qtype, q->qclass);
1332            if (DNSSECQuestion(q) && q->qDNSServer && !q->qDNSServer->cellIntf)
1333            {
1334                if (q->ProxyQuestion)
1335                    end = DNSProxySetAttributes(q, &tcpInfo->request.h, &tcpInfo->request, end, tcpInfo->request.data + AbsoluteMaxDNSMessageData);
1336                else
1337                    end = putDNSSECOption(&tcpInfo->request, end, tcpInfo->request.data + AbsoluteMaxDNSMessageData);
1338            }
1339
1340            AuthInfo = q->AuthInfo;     // Need to add TSIG to this message
1341        }
1342
1343        err = mDNSSendDNSMessage(m, &tcpInfo->request, end, mDNSInterface_Any, mDNSNULL, &tcpInfo->Addr, tcpInfo->Port, sock, AuthInfo, mDNSfalse);
1344        if (err) { debugf("ERROR: tcpCallback: mDNSSendDNSMessage - %d", err); err = mStatus_UnknownErr; goto exit; }
1345#if AWD_METRICS
1346        if (mDNSSameIPPort(tcpInfo->Port, UnicastDNSPort))
1347        {
1348            MetricsUpdateDNSQuerySize((mDNSu32)(end - (mDNSu8 *)&tcpInfo->request));
1349        }
1350#endif
1351
1352        // Record time we sent this question
1353        if (q)
1354        {
1355            mDNS_Lock(m);
1356            q->LastQTime = m->timenow;
1357            if (q->ThisQInterval < (256 * mDNSPlatformOneSecond))   // Now we have a TCP connection open, make sure we wait at least 256 seconds before retrying
1358                q->ThisQInterval = (256 * mDNSPlatformOneSecond);
1359            SetNextQueryTime(m, q);
1360            mDNS_Unlock(m);
1361        }
1362    }
1363    else
1364    {
1365        long n;
1366        const mDNSBool Read_replylen = (tcpInfo->nread < 2);  // Do we need to read the replylen field first?
1367        if (Read_replylen)         // First read the two-byte length preceeding the DNS message
1368        {
1369            mDNSu8 *lenptr = (mDNSu8 *)&tcpInfo->replylen;
1370            n = mDNSPlatformReadTCP(sock, lenptr + tcpInfo->nread, 2 - tcpInfo->nread, &closed);
1371            if (n < 0)
1372            {
1373                LogMsg("ERROR: tcpCallback - attempt to read message length failed (%d)", n);
1374                err = mStatus_ConnFailed;
1375                goto exit;
1376            }
1377            else if (closed)
1378            {
1379                // It's perfectly fine for this socket to close after the first reply. The server might
1380                // be sending gratuitous replies using UDP and doesn't have a need to leave the TCP socket open.
1381                // We'll only log this event if we've never received a reply before.
1382                // BIND 9 appears to close an idle connection after 30 seconds.
1383                if (tcpInfo->numReplies == 0)
1384                {
1385                    LogMsg("ERROR: socket closed prematurely tcpInfo->nread = %d", tcpInfo->nread);
1386                    err = mStatus_ConnFailed;
1387                    goto exit;
1388                }
1389                else
1390                {
1391                    // Note that we may not be doing the best thing if an error occurs after we've sent a second request
1392                    // over this tcp connection.  That is, we only track whether we've received at least one response
1393                    // which may have been to a previous request sent over this tcp connection.
1394                    if (backpointer) *backpointer = mDNSNULL; // Clear client backpointer FIRST so we don't risk double-disposing our tcpInfo_t
1395                    DisposeTCPConn(tcpInfo);
1396                    return;
1397                }
1398            }
1399
1400            tcpInfo->nread += n;
1401            if (tcpInfo->nread < 2) goto exit;
1402
1403            tcpInfo->replylen = (mDNSu16)((mDNSu16)lenptr[0] << 8 | lenptr[1]);
1404            if (tcpInfo->replylen < sizeof(DNSMessageHeader))
1405            { LogMsg("ERROR: tcpCallback - length too short (%d bytes)", tcpInfo->replylen); err = mStatus_UnknownErr; goto exit; }
1406
1407            tcpInfo->reply = mDNSPlatformMemAllocate(tcpInfo->replylen);
1408            if (!tcpInfo->reply) { LogMsg("ERROR: tcpCallback - malloc failed"); err = mStatus_NoMemoryErr; goto exit; }
1409        }
1410
1411        n = mDNSPlatformReadTCP(sock, ((char *)tcpInfo->reply) + (tcpInfo->nread - 2), tcpInfo->replylen - (tcpInfo->nread - 2), &closed);
1412
1413        if (n < 0)
1414        {
1415            // If this is our only read for this invokation, and it fails, then that's bad.
1416            // But if we did successfully read some or all of the replylen field this time through,
1417            // and this is now our second read from the socket, then it's expected that sometimes
1418            // there may be no more data present, and that's perfectly okay.
1419            // Assuming failure of the second read is a problem is what caused this bug:
1420            // <rdar://problem/15043194> mDNSResponder fails to read DNS over TCP packet correctly
1421            if (!Read_replylen) { LogMsg("ERROR: tcpCallback - read returned %d", n); err = mStatus_ConnFailed; }
1422            goto exit;
1423        }
1424        else if (closed)
1425        {
1426            if (tcpInfo->numReplies == 0)
1427            {
1428                LogMsg("ERROR: socket closed prematurely tcpInfo->nread = %d", tcpInfo->nread);
1429                err = mStatus_ConnFailed;
1430                goto exit;
1431            }
1432            else
1433            {
1434                // Note that we may not be doing the best thing if an error occurs after we've sent a second request
1435                // over this tcp connection.  That is, we only track whether we've received at least one response
1436                // which may have been to a previous request sent over this tcp connection.
1437                if (backpointer) *backpointer = mDNSNULL; // Clear client backpointer FIRST so we don't risk double-disposing our tcpInfo_t
1438                DisposeTCPConn(tcpInfo);
1439                return;
1440            }
1441        }
1442
1443        tcpInfo->nread += n;
1444
1445        if ((tcpInfo->nread - 2) == tcpInfo->replylen)
1446        {
1447            mDNSBool tls;
1448            DNSMessage *reply = tcpInfo->reply;
1449            mDNSu8     *end   = (mDNSu8 *)tcpInfo->reply + tcpInfo->replylen;
1450            mDNSAddr Addr  = tcpInfo->Addr;
1451            mDNSIPPort Port  = tcpInfo->Port;
1452            mDNSIPPort srcPort = zeroIPPort;
1453            tcpInfo->numReplies++;
1454            tcpInfo->reply    = mDNSNULL;   // Detach reply buffer from tcpInfo_t, to make sure client callback can't cause it to be disposed
1455            tcpInfo->nread    = 0;
1456            tcpInfo->replylen = 0;
1457
1458            // If we're going to dispose this connection, do it FIRST, before calling client callback
1459            // Note: Sleep code depends on us clearing *backpointer here -- it uses the clearing of rr->tcp
1460            // as the signal that the DNS deregistration operation with the server has completed, and the machine may now sleep
1461            // If we clear the tcp pointer in the question, mDNSCoreReceiveResponse cannot find a matching question. Hence
1462            // we store the minimal information i.e., the source port of the connection in the question itself.
1463            // Dereference sock before it is disposed in DisposeTCPConn below.
1464
1465            if (sock->flags & kTCPSocketFlags_UseTLS) tls = mDNStrue;
1466            else tls = mDNSfalse;
1467
1468            if (q && q->tcp) {srcPort = q->tcp->SrcPort; q->tcpSrcPort = srcPort;}
1469
1470            if (backpointer)
1471                if (!q || !q->LongLived || m->SleepState)
1472                { *backpointer = mDNSNULL; DisposeTCPConn(tcpInfo); }
1473
1474            mDNSCoreReceive(m, reply, end, &Addr, Port, tls ? (mDNSAddr *)1 : mDNSNULL, srcPort, 0);
1475            // USE CAUTION HERE: Invoking mDNSCoreReceive may have caused the environment to change, including canceling this operation itself
1476
1477            mDNSPlatformMemFree(reply);
1478            return;
1479        }
1480    }
1481
1482exit:
1483
1484    if (err)
1485    {
1486        // Clear client backpointer FIRST -- that way if one of the callbacks cancels its operation
1487        // we won't end up double-disposing our tcpInfo_t
1488        if (backpointer) *backpointer = mDNSNULL;
1489
1490        mDNS_Lock(m);       // Need to grab the lock to get m->timenow
1491
1492        if (q)
1493        {
1494            if (q->ThisQInterval == 0)
1495            {
1496                // We get here when we fail to establish a new TCP/TLS connection that would have been used for a new LLQ request or an LLQ renewal.
1497                // Note that ThisQInterval is also zero when sendChallengeResponse resends the LLQ request on an extant TCP/TLS connection.
1498                q->LastQTime = m->timenow;
1499                if (q->LongLived)
1500                {
1501                    // We didn't get the chance to send our request packet before the TCP/TLS connection failed.
1502                    // We want to retry quickly, but want to back off exponentially in case the server is having issues.
1503                    // Since ThisQInterval was 0, we can't just multiply by QuestionIntervalStep, we must track the number
1504                    // of TCP/TLS connection failures using ntries.
1505                    mDNSu32 count = q->ntries + 1; // want to wait at least 1 second before retrying
1506
1507                    q->ThisQInterval = InitialQuestionInterval;
1508
1509                    for (; count; count--)
1510                        q->ThisQInterval *= QuestionIntervalStep;
1511
1512                    if (q->ThisQInterval > LLQ_POLL_INTERVAL)
1513                        q->ThisQInterval = LLQ_POLL_INTERVAL;
1514                    else
1515                        q->ntries++;
1516
1517                    LogMsg("tcpCallback: stream connection for LLQ %##s (%s) failed %d times, retrying in %d ms", q->qname.c, DNSTypeName(q->qtype), q->ntries, q->ThisQInterval);
1518                }
1519                else
1520                {
1521                    q->ThisQInterval = MAX_UCAST_POLL_INTERVAL;
1522                    LogMsg("tcpCallback: stream connection for %##s (%s) failed, retrying in %d ms", q->qname.c, DNSTypeName(q->qtype), q->ThisQInterval);
1523                }
1524                SetNextQueryTime(m, q);
1525            }
1526            else if (NextQSendTime(q) - m->timenow > (q->LongLived ? LLQ_POLL_INTERVAL : MAX_UCAST_POLL_INTERVAL))
1527            {
1528                // If we get an error and our next scheduled query for this question is more than the max interval from now,
1529                // reset the next query to ensure we wait no longer the maximum interval from now before trying again.
1530                q->LastQTime     = m->timenow;
1531                q->ThisQInterval = q->LongLived ? LLQ_POLL_INTERVAL : MAX_UCAST_POLL_INTERVAL;
1532                SetNextQueryTime(m, q);
1533                LogMsg("tcpCallback: stream connection for %##s (%s) failed, retrying in %d ms", q->qname.c, DNSTypeName(q->qtype), q->ThisQInterval);
1534            }
1535
1536            // We're about to dispose of the TCP connection, so we must reset the state to retry over TCP/TLS
1537            // because sendChallengeResponse will send the query via UDP if we don't have a tcp pointer.
1538            // Resetting to LLQ_InitialRequest will cause uDNS_CheckCurrentQuestion to call startLLQHandshake, which
1539            // will attempt to establish a new tcp connection.
1540            if (q->LongLived && q->state == LLQ_SecondaryRequest)
1541                q->state = LLQ_InitialRequest;
1542
1543            // ConnFailed may happen if the server sends a TCP reset or TLS fails, in which case we want to retry establishing the LLQ
1544            // quickly rather than switching to polling mode.  This case is handled by the above code to set q->ThisQInterval just above.
1545            // If the error isn't ConnFailed, then the LLQ is in bad shape, so we switch to polling mode.
1546            if (err != mStatus_ConnFailed)
1547            {
1548                if (q->LongLived && q->state != LLQ_Poll) StartLLQPolling(m, q);
1549            }
1550        }
1551
1552        mDNS_Unlock(m);
1553
1554        DisposeTCPConn(tcpInfo);
1555    }
1556}
1557
1558mDNSlocal tcpInfo_t *MakeTCPConn(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *const end,
1559                                 TCPSocketFlags flags, const mDNSAddr *const Addr, const mDNSIPPort Port, domainname *hostname,
1560                                 DNSQuestion *const question, AuthRecord *const rr)
1561{
1562    mStatus err;
1563    mDNSIPPort srcport = zeroIPPort;
1564    tcpInfo_t *info;
1565    mDNSBool useBackgroundTrafficClass;
1566
1567    useBackgroundTrafficClass = question ? question->UseBackgroundTrafficClass : mDNSfalse;
1568
1569    if ((flags & kTCPSocketFlags_UseTLS) && (!hostname || !hostname->c[0]))
1570    { LogMsg("MakeTCPConn: TLS connection being setup with NULL hostname"); return mDNSNULL; }
1571
1572    info = (tcpInfo_t *)mDNSPlatformMemAllocate(sizeof(tcpInfo_t));
1573    if (!info) { LogMsg("ERROR: MakeTCP - memallocate failed"); return(mDNSNULL); }
1574    mDNSPlatformMemZero(info, sizeof(tcpInfo_t));
1575
1576    info->m          = m;
1577    info->sock       = mDNSPlatformTCPSocket(flags, &srcport, useBackgroundTrafficClass);
1578    info->requestLen = 0;
1579    info->question   = question;
1580    info->rr         = rr;
1581    info->Addr       = *Addr;
1582    info->Port       = Port;
1583    info->reply      = mDNSNULL;
1584    info->replylen   = 0;
1585    info->nread      = 0;
1586    info->numReplies = 0;
1587    info->SrcPort = srcport;
1588
1589    if (msg)
1590    {
1591        info->requestLen = (int) (end - ((mDNSu8*)msg));
1592        mDNSPlatformMemCopy(&info->request, msg, info->requestLen);
1593    }
1594
1595    if (!info->sock) { LogMsg("MakeTCPConn: unable to create TCP socket"); mDNSPlatformMemFree(info); return(mDNSNULL); }
1596    mDNSPlatformSetSocktOpt(info->sock, mDNSTransport_TCP, Addr->type, question);
1597    err = mDNSPlatformTCPConnect(info->sock, Addr, Port, hostname, (question ? question->InterfaceID : mDNSNULL), tcpCallback, info);
1598
1599    // Probably suboptimal here.
1600    // Instead of returning mDNSNULL here on failure, we should probably invoke the callback with an error code.
1601    // That way clients can put all the error handling and retry/recovery code in one place,
1602    // instead of having to handle immediate errors in one place and async errors in another.
1603    // Also: "err == mStatus_ConnEstablished" probably never happens.
1604
1605    // Don't need to log "connection failed" in customer builds -- it happens quite often during sleep, wake, configuration changes, etc.
1606    if      (err == mStatus_ConnEstablished) { tcpCallback(info->sock, info, mDNStrue, mStatus_NoError); }
1607    else if (err != mStatus_ConnPending    ) { LogInfo("MakeTCPConn: connection failed"); DisposeTCPConn(info); return(mDNSNULL); }
1608    return(info);
1609}
1610
1611mDNSexport void DisposeTCPConn(struct tcpInfo_t *tcp)
1612{
1613    mDNSPlatformTCPCloseConnection(tcp->sock);
1614    if (tcp->reply) mDNSPlatformMemFree(tcp->reply);
1615    mDNSPlatformMemFree(tcp);
1616}
1617
1618// Lock must be held
1619mDNSexport void startLLQHandshake(mDNS *m, DNSQuestion *q)
1620{
1621    if (m->LLQNAT.clientContext != mDNSNULL) // LLQNAT just started, give it some time
1622    {
1623        LogInfo("startLLQHandshake: waiting for NAT status for %##s (%s)", q->qname.c, DNSTypeName(q->qtype));
1624        q->ThisQInterval = LLQ_POLL_INTERVAL + mDNSRandom(LLQ_POLL_INTERVAL/10);    // Retry in approx 15 minutes
1625        q->LastQTime = m->timenow;
1626        SetNextQueryTime(m, q);
1627        return;
1628    }
1629
1630    // Either we don't have {PCP, NAT-PMP, UPnP/IGD} support (ExternalPort is zero) or behind a Double NAT that may or
1631    // may not have {PCP, NAT-PMP, UPnP/IGD} support (NATResult is non-zero)
1632    if (mDNSIPPortIsZero(m->LLQNAT.ExternalPort) || m->LLQNAT.Result)
1633    {
1634        LogInfo("startLLQHandshake: Cannot receive inbound packets; will poll for %##s (%s) External Port %d, NAT Result %d",
1635                q->qname.c, DNSTypeName(q->qtype), mDNSVal16(m->LLQNAT.ExternalPort), m->LLQNAT.Result);
1636        StartLLQPolling(m, q);
1637        return;
1638    }
1639
1640    if (mDNSIPPortIsZero(q->servPort))
1641    {
1642        debugf("startLLQHandshake: StartGetZoneData for %##s (%s)", q->qname.c, DNSTypeName(q->qtype));
1643        q->ThisQInterval = LLQ_POLL_INTERVAL + mDNSRandom(LLQ_POLL_INTERVAL/10);    // Retry in approx 15 minutes
1644        q->LastQTime     = m->timenow;
1645        SetNextQueryTime(m, q);
1646        q->servAddr = zeroAddr;
1647        // We know q->servPort is zero because of check above
1648        if (q->nta) CancelGetZoneData(m, q->nta);
1649        q->nta = StartGetZoneData(m, &q->qname, ZoneServiceLLQ, LLQGotZoneData, q);
1650        return;
1651    }
1652
1653    if (PrivateQuery(q))
1654    {
1655        if (q->tcp) LogInfo("startLLQHandshake: Disposing existing TCP connection for %##s (%s)", q->qname.c, DNSTypeName(q->qtype));
1656        if (q->tcp) { DisposeTCPConn(q->tcp); q->tcp = mDNSNULL; }
1657        if (!q->nta)
1658        {
1659            // Normally we lookup the zone data and then call this function. And we never free the zone data
1660            // for "PrivateQuery". But sometimes this can happen due to some race conditions. When we
1661            // switch networks, we might end up "Polling" the network e.g., we are behind a Double NAT.
1662            // When we poll, we free the zone information as we send the query to the server (See
1663            // PrivateQueryGotZoneData). The NAT callback (LLQNATCallback) may happen soon after that. If we
1664            // are still behind Double NAT, we would have returned early in this function. But we could
1665            // have switched to a network with no NATs and we should get the zone data again.
1666            LogInfo("startLLQHandshake: nta is NULL for %##s (%s)", q->qname.c, DNSTypeName(q->qtype));
1667            q->nta = StartGetZoneData(m, &q->qname, ZoneServiceLLQ, LLQGotZoneData, q);
1668            return;
1669        }
1670        else if (!q->nta->Host.c[0])
1671        {
1672            // This should not happen. If it happens, we print a log and MakeTCPConn will fail if it can't find a hostname
1673            LogMsg("startLLQHandshake: ERROR!!: nta non NULL for %##s (%s) but HostName %d NULL, LongLived %d", q->qname.c, DNSTypeName(q->qtype), q->nta->Host.c[0], q->LongLived);
1674        }
1675        q->tcp = MakeTCPConn(m, mDNSNULL, mDNSNULL, kTCPSocketFlags_UseTLS, &q->servAddr, q->servPort, &q->nta->Host, q, mDNSNULL);
1676        if (!q->tcp)
1677            q->ThisQInterval = mDNSPlatformOneSecond * 5;   // If TCP failed (transient networking glitch) try again in five seconds
1678        else
1679        {
1680            q->state         = LLQ_SecondaryRequest;        // Right now, for private DNS, we skip the four-way LLQ handshake
1681            q->ReqLease      = kLLQ_DefLease;
1682            q->ThisQInterval = 0;
1683        }
1684        q->LastQTime     = m->timenow;
1685        SetNextQueryTime(m, q);
1686    }
1687    else
1688    {
1689        debugf("startLLQHandshake: m->AdvertisedV4 %#a%s Server %#a:%d%s %##s (%s)",
1690               &m->AdvertisedV4,                     mDNSv4AddrIsRFC1918(&m->AdvertisedV4.ip.v4) ? " (RFC 1918)" : "",
1691               &q->servAddr, mDNSVal16(q->servPort), mDNSAddrIsRFC1918(&q->servAddr)             ? " (RFC 1918)" : "",
1692               q->qname.c, DNSTypeName(q->qtype));
1693
1694        if (q->ntries++ >= kLLQ_MAX_TRIES)
1695        {
1696            LogMsg("startLLQHandshake: %d failed attempts for LLQ %##s Polling.", kLLQ_MAX_TRIES, q->qname.c);
1697            StartLLQPolling(m, q);
1698        }
1699        else
1700        {
1701            mDNSu8 *end;
1702            LLQOptData llqData;
1703
1704            // set llq rdata
1705            llqData.vers  = kLLQ_Vers;
1706            llqData.llqOp = kLLQOp_Setup;
1707            llqData.err   = LLQErr_NoError; // Don't need to tell server UDP notification port when sending over UDP
1708            llqData.id    = zeroOpaque64;
1709            llqData.llqlease = kLLQ_DefLease;
1710
1711            InitializeDNSMessage(&m->omsg.h, q->TargetQID, uQueryFlags);
1712            end = putLLQ(&m->omsg, m->omsg.data, q, &llqData);
1713            if (!end) { LogMsg("ERROR: startLLQHandshake - putLLQ"); StartLLQPolling(m,q); return; }
1714
1715            mDNSSendDNSMessage(m, &m->omsg, end, mDNSInterface_Any, q->LocalSocket, &q->servAddr, q->servPort, mDNSNULL, mDNSNULL, mDNSfalse);
1716
1717            // update question state
1718            q->state         = LLQ_InitialRequest;
1719            q->ReqLease      = kLLQ_DefLease;
1720            q->ThisQInterval = (kLLQ_INIT_RESEND * mDNSPlatformOneSecond);
1721            q->LastQTime     = m->timenow;
1722            SetNextQueryTime(m, q);
1723        }
1724    }
1725}
1726
1727
1728// forward declaration so GetServiceTarget can do reverse lookup if needed
1729mDNSlocal void GetStaticHostname(mDNS *m);
1730
1731mDNSexport const domainname *GetServiceTarget(mDNS *m, AuthRecord *const rr)
1732{
1733    debugf("GetServiceTarget %##s", rr->resrec.name->c);
1734
1735    if (!rr->AutoTarget)        // If not automatically tracking this host's current name, just return the existing target
1736        return(&rr->resrec.rdata->u.srv.target);
1737    else
1738    {
1739#if APPLE_OSX_mDNSResponder
1740        DomainAuthInfo *AuthInfo = GetAuthInfoForName_internal(m, rr->resrec.name);
1741        if (AuthInfo && AuthInfo->AutoTunnel)
1742        {
1743            StartServerTunnel(AuthInfo);
1744            if (AuthInfo->AutoTunnelHostRecord.namestorage.c[0] == 0) return(mDNSNULL);
1745            debugf("GetServiceTarget: Returning %##s", AuthInfo->AutoTunnelHostRecord.namestorage.c);
1746            return(&AuthInfo->AutoTunnelHostRecord.namestorage);
1747        }
1748        else
1749#endif // APPLE_OSX_mDNSResponder
1750        {
1751            const int srvcount = CountLabels(rr->resrec.name);
1752            HostnameInfo *besthi = mDNSNULL, *hi;
1753            int best = 0;
1754            for (hi = m->Hostnames; hi; hi = hi->next)
1755                if (hi->arv4.state == regState_Registered || hi->arv4.state == regState_Refresh ||
1756                    hi->arv6.state == regState_Registered || hi->arv6.state == regState_Refresh)
1757                {
1758                    int x, hostcount = CountLabels(&hi->fqdn);
1759                    for (x = hostcount < srvcount ? hostcount : srvcount; x > 0 && x > best; x--)
1760                        if (SameDomainName(SkipLeadingLabels(rr->resrec.name, srvcount - x), SkipLeadingLabels(&hi->fqdn, hostcount - x)))
1761                        { best = x; besthi = hi; }
1762                }
1763
1764            if (besthi) return(&besthi->fqdn);
1765        }
1766        if (m->StaticHostname.c[0]) return(&m->StaticHostname);
1767        else GetStaticHostname(m); // asynchronously do reverse lookup for primary IPv4 address
1768        LogInfo("GetServiceTarget: Returning NULL for %s", ARDisplayString(m, rr));
1769        return(mDNSNULL);
1770    }
1771}
1772
1773mDNSlocal const domainname *PUBLIC_UPDATE_SERVICE_TYPE  = (const domainname*)"\x0B_dns-update"     "\x04_udp";
1774mDNSlocal const domainname *PUBLIC_LLQ_SERVICE_TYPE     = (const domainname*)"\x08_dns-llq"        "\x04_udp";
1775
1776mDNSlocal const domainname *PRIVATE_UPDATE_SERVICE_TYPE = (const domainname*)"\x0F_dns-update-tls" "\x04_tcp";
1777mDNSlocal const domainname *PRIVATE_QUERY_SERVICE_TYPE  = (const domainname*)"\x0E_dns-query-tls"  "\x04_tcp";
1778mDNSlocal const domainname *PRIVATE_LLQ_SERVICE_TYPE    = (const domainname*)"\x0C_dns-llq-tls"    "\x04_tcp";
1779mDNSlocal const domainname *DNS_PUSH_NOTIFICATION_SERVICE_TYPE = (const domainname*)"\x0C_dns-push-tls"    "\x04_tcp";
1780
1781#define ZoneDataSRV(X) ( \
1782        (X)->ZoneService == ZoneServiceUpdate  ? ((X)->ZonePrivate ? PRIVATE_UPDATE_SERVICE_TYPE : PUBLIC_UPDATE_SERVICE_TYPE) : \
1783        (X)->ZoneService == ZoneServiceQuery   ? ((X)->ZonePrivate ? PRIVATE_QUERY_SERVICE_TYPE  : (const domainname*)""     ) : \
1784        (X)->ZoneService == ZoneServiceLLQ     ? ((X)->ZonePrivate ? PRIVATE_LLQ_SERVICE_TYPE    : PUBLIC_LLQ_SERVICE_TYPE   ) : \
1785        (X)->ZoneService == ZoneServiceDNSPush ? DNS_PUSH_NOTIFICATION_SERVICE_TYPE : (const domainname*)"")
1786
1787// Forward reference: GetZoneData_StartQuery references GetZoneData_QuestionCallback, and
1788// GetZoneData_QuestionCallback calls GetZoneData_StartQuery
1789mDNSlocal mStatus GetZoneData_StartQuery(mDNS *const m, ZoneData *zd, mDNSu16 qtype);
1790
1791// GetZoneData_QuestionCallback is called from normal client callback context (core API calls allowed)
1792mDNSlocal void GetZoneData_QuestionCallback(mDNS *const m, DNSQuestion *question, const ResourceRecord *const answer, QC_result AddRecord)
1793{
1794    ZoneData *zd = (ZoneData*)question->QuestionContext;
1795
1796    debugf("GetZoneData_QuestionCallback: %s %s", AddRecord ? "Add" : "Rmv", RRDisplayString(m, answer));
1797
1798    if (!AddRecord) return;                                             // Don't care about REMOVE events
1799    if (AddRecord == QC_addnocache && answer->rdlength == 0) return;    // Don't care about transient failure indications
1800    if (answer->rrtype != question->qtype) return;                      // Don't care about CNAMEs
1801
1802    if (answer->rrtype == kDNSType_SOA)
1803    {
1804        debugf("GetZoneData GOT SOA %s", RRDisplayString(m, answer));
1805        mDNS_StopQuery(m, question);
1806        if (question->ThisQInterval != -1)
1807            LogMsg("GetZoneData_QuestionCallback: Question %##s (%s) ThisQInterval %d not -1", question->qname.c, DNSTypeName(question->qtype), question->ThisQInterval);
1808        if (answer->rdlength)
1809        {
1810            AssignDomainName(&zd->ZoneName, answer->name);
1811            zd->ZoneClass = answer->rrclass;
1812            AssignDomainName(&zd->question.qname, &zd->ZoneName);
1813            GetZoneData_StartQuery(m, zd, kDNSType_SRV);
1814        }
1815        else if (zd->CurrentSOA->c[0])
1816        {
1817            DomainAuthInfo *AuthInfo = GetAuthInfoForName(m, zd->CurrentSOA);
1818            if (AuthInfo && AuthInfo->AutoTunnel)
1819            {
1820                // To keep the load on the server down, we don't chop down on
1821                // SOA lookups for AutoTunnels
1822                LogInfo("GetZoneData_QuestionCallback: not chopping labels for %##s", zd->CurrentSOA->c);
1823                zd->ZoneDataCallback(m, mStatus_NoSuchNameErr, zd);
1824            }
1825            else
1826            {
1827                zd->CurrentSOA = (domainname *)(zd->CurrentSOA->c + zd->CurrentSOA->c[0]+1);
1828                AssignDomainName(&zd->question.qname, zd->CurrentSOA);
1829                GetZoneData_StartQuery(m, zd, kDNSType_SOA);
1830            }
1831        }
1832        else
1833        {
1834            LogInfo("GetZoneData recursed to root label of %##s without finding SOA", zd->ChildName.c);
1835            zd->ZoneDataCallback(m, mStatus_NoSuchNameErr, zd);
1836        }
1837    }
1838    else if (answer->rrtype == kDNSType_SRV)
1839    {
1840        debugf("GetZoneData GOT SRV %s", RRDisplayString(m, answer));
1841        mDNS_StopQuery(m, question);
1842        if (question->ThisQInterval != -1)
1843            LogMsg("GetZoneData_QuestionCallback: Question %##s (%s) ThisQInterval %d not -1", question->qname.c, DNSTypeName(question->qtype), question->ThisQInterval);
1844// Right now we don't want to fail back to non-encrypted operations
1845// If the AuthInfo has the AutoTunnel field set, then we want private or nothing
1846// <rdar://problem/5687667> BTMM: Don't fallback to unencrypted operations when SRV lookup fails
1847#if 0
1848        if (!answer->rdlength && zd->ZonePrivate && zd->ZoneService != ZoneServiceQuery)
1849        {
1850            zd->ZonePrivate = mDNSfalse;    // Causes ZoneDataSRV() to yield a different SRV name when building the query
1851            GetZoneData_StartQuery(m, zd, kDNSType_SRV);        // Try again, non-private this time
1852        }
1853        else
1854#endif
1855        {
1856            if (answer->rdlength)
1857            {
1858                AssignDomainName(&zd->Host, &answer->rdata->u.srv.target);
1859                zd->Port = answer->rdata->u.srv.port;
1860                AssignDomainName(&zd->question.qname, &zd->Host);
1861                GetZoneData_StartQuery(m, zd, kDNSType_A);
1862            }
1863            else
1864            {
1865                zd->ZonePrivate = mDNSfalse;
1866                zd->Host.c[0] = 0;
1867                zd->Port = zeroIPPort;
1868                zd->Addr = zeroAddr;
1869                zd->ZoneDataCallback(m, mStatus_NoError, zd);
1870            }
1871        }
1872    }
1873    else if (answer->rrtype == kDNSType_A)
1874    {
1875        debugf("GetZoneData GOT A %s", RRDisplayString(m, answer));
1876        mDNS_StopQuery(m, question);
1877        if (question->ThisQInterval != -1)
1878            LogMsg("GetZoneData_QuestionCallback: Question %##s (%s) ThisQInterval %d not -1", question->qname.c, DNSTypeName(question->qtype), question->ThisQInterval);
1879        zd->Addr.type  = mDNSAddrType_IPv4;
1880	if (answer->rdlength == 4)
1881            zd->Addr.ip.v4 = answer->rdata->u.ipv4;
1882	else
1883            zd->Addr.ip.v4 = zerov4Addr;
1884        // In order to simulate firewalls blocking our outgoing TCP connections, returning immediate ICMP errors or TCP resets,
1885        // the code below will make us try to connect to loopback, resulting in an immediate "port unreachable" failure.
1886        // This helps us test to make sure we handle this case gracefully
1887        // <rdar://problem/5607082> BTMM: mDNSResponder taking 100 percent CPU after upgrading to 10.5.1
1888#if 0
1889        zd->Addr.ip.v4.b[0] = 127;
1890        zd->Addr.ip.v4.b[1] = 0;
1891        zd->Addr.ip.v4.b[2] = 0;
1892        zd->Addr.ip.v4.b[3] = 1;
1893#endif
1894        // The caller needs to free the memory when done with zone data
1895        zd->ZoneDataCallback(m, mStatus_NoError, zd);
1896    }
1897}
1898
1899// GetZoneData_StartQuery is called from normal client context (lock not held, or client callback)
1900mDNSlocal mStatus GetZoneData_StartQuery(mDNS *const m, ZoneData *zd, mDNSu16 qtype)
1901{
1902    if (qtype == kDNSType_SRV)
1903    {
1904        AssignDomainName(&zd->question.qname, ZoneDataSRV(zd));
1905        AppendDomainName(&zd->question.qname, &zd->ZoneName);
1906        debugf("lookupDNSPort %##s", zd->question.qname.c);
1907    }
1908
1909    // CancelGetZoneData can get called at any time. We should stop the question if it has not been
1910    // stopped already. A value of -1 for ThisQInterval indicates that the question is not active
1911    // yet.
1912    zd->question.ThisQInterval       = -1;
1913    zd->question.InterfaceID         = mDNSInterface_Any;
1914    zd->question.flags               = 0;
1915    zd->question.Target              = zeroAddr;
1916    //zd->question.qname.c[0]        = 0;           // Already set
1917    zd->question.qtype               = qtype;
1918    zd->question.qclass              = kDNSClass_IN;
1919    zd->question.LongLived           = mDNSfalse;
1920    zd->question.ExpectUnique        = mDNStrue;
1921    zd->question.ForceMCast          = mDNSfalse;
1922    zd->question.ReturnIntermed      = mDNStrue;
1923    zd->question.SuppressUnusable    = mDNSfalse;
1924    zd->question.SearchListIndex     = 0;
1925    zd->question.AppendSearchDomains = 0;
1926    zd->question.RetryWithSearchDomains = mDNSfalse;
1927    zd->question.TimeoutQuestion     = 0;
1928    zd->question.WakeOnResolve       = 0;
1929    zd->question.UseBackgroundTrafficClass = mDNSfalse;
1930    zd->question.ValidationRequired = 0;
1931    zd->question.ValidatingResponse = 0;
1932    zd->question.ProxyQuestion      = 0;
1933    zd->question.qnameOrig           = mDNSNULL;
1934    zd->question.AnonInfo            = mDNSNULL;
1935    zd->question.pid                 = mDNSPlatformGetPID();
1936    zd->question.euid                = 0;
1937    zd->question.QuestionCallback    = GetZoneData_QuestionCallback;
1938    zd->question.QuestionContext     = zd;
1939
1940    //LogMsg("GetZoneData_StartQuery %##s (%s) %p", zd->question.qname.c, DNSTypeName(zd->question.qtype), zd->question.Private);
1941    return(mDNS_StartQuery(m, &zd->question));
1942}
1943
1944// StartGetZoneData is an internal routine (i.e. must be called with the lock already held)
1945mDNSexport ZoneData *StartGetZoneData(mDNS *const m, const domainname *const name, const ZoneService target, ZoneDataCallback callback, void *ZoneDataContext)
1946{
1947    DomainAuthInfo *AuthInfo = GetAuthInfoForName_internal(m, name);
1948    int initialskip = (AuthInfo && AuthInfo->AutoTunnel) ? DomainNameLength(name) - DomainNameLength(&AuthInfo->domain) : 0;
1949    ZoneData *zd = (ZoneData*)mDNSPlatformMemAllocate(sizeof(ZoneData));
1950    if (!zd) { LogMsg("ERROR: StartGetZoneData - mDNSPlatformMemAllocate failed"); return mDNSNULL; }
1951    mDNSPlatformMemZero(zd, sizeof(ZoneData));
1952    AssignDomainName(&zd->ChildName, name);
1953    zd->ZoneService      = target;
1954    zd->CurrentSOA       = (domainname *)(&zd->ChildName.c[initialskip]);
1955    zd->ZoneName.c[0]    = 0;
1956    zd->ZoneClass        = 0;
1957    zd->Host.c[0]        = 0;
1958    zd->Port             = zeroIPPort;
1959    zd->Addr             = zeroAddr;
1960    zd->ZonePrivate      = AuthInfo && AuthInfo->AutoTunnel ? mDNStrue : mDNSfalse;
1961    zd->ZoneDataCallback = callback;
1962    zd->ZoneDataContext  = ZoneDataContext;
1963
1964    zd->question.QuestionContext = zd;
1965
1966    mDNS_DropLockBeforeCallback();      // GetZoneData_StartQuery expects to be called from a normal callback, so we emulate that here
1967    if (AuthInfo && AuthInfo->AutoTunnel && !mDNSIPPortIsZero(AuthInfo->port))
1968    {
1969        LogInfo("StartGetZoneData: Bypassing SOA, SRV query for %##s", AuthInfo->domain.c);
1970        // We bypass SOA and SRV queries if we know the hostname and port already from the configuration.
1971        // Today this is only true for AutoTunnel. As we bypass, we need to infer a few things:
1972        //
1973        // 1. Zone name is the same as the AuthInfo domain
1974        // 2. ZoneClass is kDNSClass_IN which should be a safe assumption
1975        //
1976        // If we want to make this bypass mechanism work for non-AutoTunnels also, (1) has to hold
1977        // good. Otherwise, it has to be configured also.
1978
1979        AssignDomainName(&zd->ZoneName, &AuthInfo->domain);
1980        zd->ZoneClass = kDNSClass_IN;
1981        AssignDomainName(&zd->Host, &AuthInfo->hostname);
1982        zd->Port = AuthInfo->port;
1983        AssignDomainName(&zd->question.qname, &zd->Host);
1984        GetZoneData_StartQuery(m, zd, kDNSType_A);
1985    }
1986    else
1987    {
1988        if (AuthInfo && AuthInfo->AutoTunnel) LogInfo("StartGetZoneData: Not Bypassing SOA, SRV query for %##s", AuthInfo->domain.c);
1989        AssignDomainName(&zd->question.qname, zd->CurrentSOA);
1990        GetZoneData_StartQuery(m, zd, kDNSType_SOA);
1991    }
1992    mDNS_ReclaimLockAfterCallback();
1993
1994    return zd;
1995}
1996
1997// Returns if the question is a GetZoneData question. These questions are special in
1998// that they are created internally while resolving a private query or LLQs.
1999mDNSexport mDNSBool IsGetZoneDataQuestion(DNSQuestion *q)
2000{
2001    if (q->QuestionCallback == GetZoneData_QuestionCallback) return(mDNStrue);
2002    else return(mDNSfalse);
2003}
2004
2005// GetZoneData queries are a special case -- even if we have a key for them, we don't do them privately,
2006// because that would result in an infinite loop (i.e. to do a private query we first need to get
2007// the _dns-query-tls SRV record for the zone, and we can't do *that* privately because to do so
2008// we'd need to already know the _dns-query-tls SRV record.
2009// Also, as a general rule, we never do SOA queries privately
2010mDNSexport DomainAuthInfo *GetAuthInfoForQuestion(mDNS *m, const DNSQuestion *const q)  // Must be called with lock held
2011{
2012    if (q->QuestionCallback == GetZoneData_QuestionCallback) return(mDNSNULL);
2013    if (q->qtype            == kDNSType_SOA                ) return(mDNSNULL);
2014    return(GetAuthInfoForName_internal(m, &q->qname));
2015}
2016
2017// ***************************************************************************
2018#if COMPILER_LIKES_PRAGMA_MARK
2019#pragma mark - host name and interface management
2020#endif
2021
2022mDNSlocal void SendRecordRegistration(mDNS *const m, AuthRecord *rr);
2023mDNSlocal void SendRecordDeregistration(mDNS *m, AuthRecord *rr);
2024mDNSlocal mDNSBool IsRecordMergeable(mDNS *const m, AuthRecord *rr, mDNSs32 time);
2025
2026// When this function is called, service record is already deregistered. We just
2027// have to deregister the PTR and TXT records.
2028mDNSlocal void UpdateAllServiceRecords(mDNS *const m, AuthRecord *rr, mDNSBool reg)
2029{
2030    AuthRecord *r, *srvRR;
2031
2032    if (rr->resrec.rrtype != kDNSType_SRV) { LogMsg("UpdateAllServiceRecords:ERROR!! ResourceRecord not a service record %s", ARDisplayString(m, rr)); return; }
2033
2034    if (reg && rr->state == regState_NoTarget) { LogMsg("UpdateAllServiceRecords:ERROR!! SRV record %s in noTarget state during registration", ARDisplayString(m, rr)); return; }
2035
2036    LogInfo("UpdateAllServiceRecords: ResourceRecord %s", ARDisplayString(m, rr));
2037
2038    for (r = m->ResourceRecords; r; r=r->next)
2039    {
2040        if (!AuthRecord_uDNS(r)) continue;
2041        srvRR = mDNSNULL;
2042        if (r->resrec.rrtype == kDNSType_PTR)
2043            srvRR = r->Additional1;
2044        else if (r->resrec.rrtype == kDNSType_TXT)
2045            srvRR = r->DependentOn;
2046        if (srvRR && srvRR->resrec.rrtype != kDNSType_SRV)
2047            LogMsg("UpdateAllServiceRecords: ERROR!! Resource record %s wrong, expecting SRV type", ARDisplayString(m, srvRR));
2048        if (srvRR == rr)
2049        {
2050            if (!reg)
2051            {
2052                LogInfo("UpdateAllServiceRecords: deregistering %s", ARDisplayString(m, r));
2053                r->SRVChanged = mDNStrue;
2054                r->ThisAPInterval = INIT_RECORD_REG_INTERVAL;
2055                r->LastAPTime = m->timenow - INIT_RECORD_REG_INTERVAL;
2056                r->state = regState_DeregPending;
2057            }
2058            else
2059            {
2060                // Clearing SRVchanged is a safety measure. If our pevious dereg never
2061                // came back and we had a target change, we are starting fresh
2062                r->SRVChanged = mDNSfalse;
2063                // if it is already registered or in the process of registering, then don't
2064                // bother re-registering. This happens today for non-BTMM domains where the
2065                // TXT and PTR get registered before SRV records because of the delay in
2066                // getting the port mapping. There is no point in re-registering the TXT
2067                // and PTR records.
2068                if ((r->state == regState_Registered) ||
2069                    (r->state == regState_Pending && r->nta && !mDNSIPv4AddressIsZero(r->nta->Addr.ip.v4)))
2070                    LogInfo("UpdateAllServiceRecords: not registering %s, state %d", ARDisplayString(m, r), r->state);
2071                else
2072                {
2073                    LogInfo("UpdateAllServiceRecords: registering %s, state %d", ARDisplayString(m, r), r->state);
2074                    ActivateUnicastRegistration(m, r);
2075                }
2076            }
2077        }
2078    }
2079}
2080
2081// Called in normal client context (lock not held)
2082// Currently only supports SRV records for nat mapping
2083mDNSlocal void CompleteRecordNatMap(mDNS *m, NATTraversalInfo *n)
2084{
2085    const domainname *target;
2086    domainname *srvt;
2087    AuthRecord *rr = (AuthRecord *)n->clientContext;
2088    debugf("SRVNatMap complete %.4a IntPort %u ExternalPort %u NATLease %u", &n->ExternalAddress, mDNSVal16(n->IntPort), mDNSVal16(n->ExternalPort), n->NATLease);
2089
2090    if (!rr) { LogMsg("CompleteRecordNatMap called with unknown AuthRecord object"); return; }
2091    if (!n->NATLease) { LogMsg("CompleteRecordNatMap No NATLease for %s", ARDisplayString(m, rr)); return; }
2092
2093    if (rr->resrec.rrtype != kDNSType_SRV) {LogMsg("CompleteRecordNatMap: Not a service record %s", ARDisplayString(m, rr)); return; }
2094
2095    if (rr->resrec.RecordType == kDNSRecordTypeDeregistering) { LogInfo("CompleteRecordNatMap called for %s, Service deregistering", ARDisplayString(m, rr)); return; }
2096
2097    if (rr->state == regState_DeregPending) { LogInfo("CompleteRecordNatMap called for %s, record in DeregPending", ARDisplayString(m, rr)); return; }
2098
2099    // As we free the zone info after registering/deregistering with the server (See hndlRecordUpdateReply),
2100    // we need to restart the get zone data and nat mapping request to get the latest mapping result as we can't handle it
2101    // at this moment. Restart from the beginning.
2102    if (!rr->nta || mDNSIPv4AddressIsZero(rr->nta->Addr.ip.v4))
2103    {
2104        LogInfo("CompleteRecordNatMap called for %s but no zone information!", ARDisplayString(m, rr));
2105        // We need to clear out the NATinfo state so that it will result in re-acquiring the mapping
2106        // and hence this callback called again.
2107        if (rr->NATinfo.clientContext)
2108        {
2109            mDNS_StopNATOperation_internal(m, &rr->NATinfo);
2110            rr->NATinfo.clientContext = mDNSNULL;
2111        }
2112        rr->state = regState_Pending;
2113        rr->ThisAPInterval = INIT_RECORD_REG_INTERVAL;
2114        rr->LastAPTime = m->timenow - INIT_RECORD_REG_INTERVAL;
2115        return;
2116    }
2117
2118    mDNS_Lock(m);
2119    // Reevaluate the target always as Target could have changed while
2120    // we were getting the port mapping (See UpdateOneSRVRecord)
2121    target = GetServiceTarget(m, rr);
2122    srvt = GetRRDomainNameTarget(&rr->resrec);
2123    if (!target || target->c[0] == 0 || mDNSIPPortIsZero(n->ExternalPort))
2124    {
2125        if (target && target->c[0])
2126            LogInfo("CompleteRecordNatMap - Target %##s for ResourceRecord %##s, ExternalPort %d", target->c, rr->resrec.name->c, mDNSVal16(n->ExternalPort));
2127        else
2128            LogInfo("CompleteRecordNatMap - no target for %##s, ExternalPort %d", rr->resrec.name->c, mDNSVal16(n->ExternalPort));
2129        if (srvt) srvt->c[0] = 0;
2130        rr->state = regState_NoTarget;
2131        rr->resrec.rdlength = rr->resrec.rdestimate = 0;
2132        mDNS_Unlock(m);
2133        UpdateAllServiceRecords(m, rr, mDNSfalse);
2134        return;
2135    }
2136    LogInfo("CompleteRecordNatMap - Target %##s for ResourceRecord %##s, ExternalPort %d", target->c, rr->resrec.name->c, mDNSVal16(n->ExternalPort));
2137    // This function might get called multiple times during a network transition event. Previosuly, we could
2138    // have put the SRV record in NoTarget state above and deregistered all the other records. When this
2139    // function gets called again with a non-zero ExternalPort, we need to set the target and register the
2140    // other records again.
2141    if (srvt && !SameDomainName(srvt, target))
2142    {
2143        AssignDomainName(srvt, target);
2144        SetNewRData(&rr->resrec, mDNSNULL, 0);      // Update rdlength, rdestimate, rdatahash
2145    }
2146
2147    // SRVChanged is set when when the target of the SRV record changes (See UpdateOneSRVRecord).
2148    // As a result of the target change, we might register just that SRV Record if it was
2149    // previously registered and we have a new target OR deregister SRV (and the associated
2150    // PTR/TXT records) if we don't have a target anymore. When we get a response from the server,
2151    // SRVChanged state tells that we registered/deregistered because of a target change
2152    // and hence handle accordingly e.g., if we deregistered, put the records in NoTarget state OR
2153    // if we registered then put it in Registered state.
2154    //
2155    // Here, we are registering all the records again from the beginning. Treat this as first time
2156    // registration rather than a temporary target change.
2157    rr->SRVChanged = mDNSfalse;
2158
2159    // We want IsRecordMergeable to check whether it is a record whose update can be
2160    // sent with others. We set the time before we call IsRecordMergeable, so that
2161    // it does not fail this record based on time. We are interested in other checks
2162    // at this time
2163    rr->state = regState_Pending;
2164    rr->ThisAPInterval = INIT_RECORD_REG_INTERVAL;
2165    rr->LastAPTime = m->timenow - INIT_RECORD_REG_INTERVAL;
2166    if (IsRecordMergeable(m, rr, m->timenow + MERGE_DELAY_TIME))
2167        // Delay the record registration by MERGE_DELAY_TIME so that we can merge them
2168        // into one update
2169        rr->LastAPTime += MERGE_DELAY_TIME;
2170    mDNS_Unlock(m);
2171    // We call this always even though it may not be necessary always e.g., normal registration
2172    // process where TXT and PTR gets registered followed by the SRV record after it gets
2173    // the port mapping. In that case, UpdateAllServiceRecords handles the optimization. The
2174    // update of TXT and PTR record is required if we entered noTargetState before as explained
2175    // above.
2176    UpdateAllServiceRecords(m, rr, mDNStrue);
2177}
2178
2179mDNSlocal void StartRecordNatMap(mDNS *m, AuthRecord *rr)
2180{
2181    const mDNSu8 *p;
2182    mDNSu8 protocol;
2183
2184    if (rr->resrec.rrtype != kDNSType_SRV)
2185    {
2186        LogInfo("StartRecordNatMap: Resource Record %##s type %d, not supported", rr->resrec.name->c, rr->resrec.rrtype);
2187        return;
2188    }
2189    p = rr->resrec.name->c;
2190    //Assume <Service Instance>.<App Protocol>.<Transport protocol>.<Name>
2191    // Skip the first two labels to get to the transport protocol
2192    if (p[0]) p += 1 + p[0];
2193    if (p[0]) p += 1 + p[0];
2194    if      (SameDomainLabel(p, (mDNSu8 *)"\x4" "_tcp")) protocol = NATOp_MapTCP;
2195    else if (SameDomainLabel(p, (mDNSu8 *)"\x4" "_udp")) protocol = NATOp_MapUDP;
2196    else { LogMsg("StartRecordNatMap: could not determine transport protocol of service %##s", rr->resrec.name->c); return; }
2197
2198    //LogMsg("StartRecordNatMap: clientContext %p IntPort %d srv.port %d %s",
2199    //  rr->NATinfo.clientContext, mDNSVal16(rr->NATinfo.IntPort), mDNSVal16(rr->resrec.rdata->u.srv.port), ARDisplayString(m, rr));
2200    if (rr->NATinfo.clientContext) mDNS_StopNATOperation_internal(m, &rr->NATinfo);
2201    rr->NATinfo.Protocol       = protocol;
2202
2203    // Shouldn't be trying to set IntPort here --
2204    // BuildUpdateMessage overwrites srs->RR_SRV.resrec.rdata->u.srv.port with external (mapped) port number
2205    rr->NATinfo.IntPort        = rr->resrec.rdata->u.srv.port;
2206    rr->NATinfo.RequestedPort  = rr->resrec.rdata->u.srv.port;
2207    rr->NATinfo.NATLease       = 0;     // Request default lease
2208    rr->NATinfo.clientCallback = CompleteRecordNatMap;
2209    rr->NATinfo.clientContext  = rr;
2210    mDNS_StartNATOperation_internal(m, &rr->NATinfo);
2211}
2212
2213// Unlink an Auth Record from the m->ResourceRecords list.
2214// When a resource record enters regState_NoTarget initially, mDNS_Register_internal
2215// does not initialize completely e.g., it cannot check for duplicates etc. The resource
2216// record is temporarily left in the ResourceRecords list so that we can initialize later
2217// when the target is resolvable. Similarly, when host name changes, we enter regState_NoTarget
2218// and we do the same.
2219
2220// This UnlinkResourceRecord routine is very worrying. It bypasses all the normal cleanup performed
2221// by mDNS_Deregister_internal and just unceremoniously cuts the record from the active list.
2222// This is why re-regsitering this record was producing syslog messages like this:
2223// "Error! Tried to add a NAT traversal that's already in the active list"
2224// Right now UnlinkResourceRecord is fortunately only called by RegisterAllServiceRecords,
2225// which then immediately calls mDNS_Register_internal to re-register the record, which probably
2226// masked more serious problems. Any other use of UnlinkResourceRecord is likely to lead to crashes.
2227// For now we'll workaround that specific problem by explicitly calling mDNS_StopNATOperation_internal,
2228// but long-term we should either stop cancelling the record registration and then re-registering it,
2229// or if we really do need to do this for some reason it should be done via the usual
2230// mDNS_Deregister_internal path instead of just cutting the record from the list.
2231
2232mDNSlocal mStatus UnlinkResourceRecord(mDNS *const m, AuthRecord *const rr)
2233{
2234    AuthRecord **list = &m->ResourceRecords;
2235    while (*list && *list != rr) list = &(*list)->next;
2236    if (*list)
2237    {
2238        *list = rr->next;
2239        rr->next = mDNSNULL;
2240
2241        // Temporary workaround to cancel any active NAT mapping operation
2242        if (rr->NATinfo.clientContext)
2243        {
2244            mDNS_StopNATOperation_internal(m, &rr->NATinfo);
2245            rr->NATinfo.clientContext = mDNSNULL;
2246            if (rr->resrec.rrtype == kDNSType_SRV) rr->resrec.rdata->u.srv.port = rr->NATinfo.IntPort;
2247        }
2248
2249        return(mStatus_NoError);
2250    }
2251    LogMsg("UnlinkResourceRecord:ERROR!! - no such active record %##s", rr->resrec.name->c);
2252    return(mStatus_NoSuchRecord);
2253}
2254
2255// We need to go through mDNS_Register again as we did not complete the
2256// full initialization last time e.g., duplicate checks.
2257// After we register, we will be in regState_GetZoneData.
2258mDNSlocal void RegisterAllServiceRecords(mDNS *const m, AuthRecord *rr)
2259{
2260    LogInfo("RegisterAllServiceRecords: Service Record %##s", rr->resrec.name->c);
2261    // First Register the service record, we do this differently from other records because
2262    // when it entered NoTarget state, it did not go through complete initialization
2263    rr->SRVChanged = mDNSfalse;
2264    UnlinkResourceRecord(m, rr);
2265    mDNS_Register_internal(m, rr);
2266    // Register the other records
2267    UpdateAllServiceRecords(m, rr, mDNStrue);
2268}
2269
2270// Called with lock held
2271mDNSlocal void UpdateOneSRVRecord(mDNS *m, AuthRecord *rr)
2272{
2273    // Target change if:
2274    // We have a target and were previously waiting for one, or
2275    // We had a target and no longer do, or
2276    // The target has changed
2277
2278    domainname *curtarget = &rr->resrec.rdata->u.srv.target;
2279    const domainname *const nt = GetServiceTarget(m, rr);
2280    const domainname *const newtarget = nt ? nt : (domainname*)"";
2281    mDNSBool TargetChanged = (newtarget->c[0] && rr->state == regState_NoTarget) || !SameDomainName(curtarget, newtarget);
2282    mDNSBool HaveZoneData  = rr->nta && !mDNSIPv4AddressIsZero(rr->nta->Addr.ip.v4);
2283
2284    // Nat state change if:
2285    // We were behind a NAT, and now we are behind a new NAT, or
2286    // We're not behind a NAT but our port was previously mapped to a different external port
2287    // We were not behind a NAT and now we are
2288
2289    mDNSIPPort port        = rr->resrec.rdata->u.srv.port;
2290    mDNSBool NowNeedNATMAP = (rr->AutoTarget == Target_AutoHostAndNATMAP && !mDNSIPPortIsZero(port) && mDNSv4AddrIsRFC1918(&m->AdvertisedV4.ip.v4) && rr->nta && !mDNSAddrIsRFC1918(&rr->nta->Addr));
2291    mDNSBool WereBehindNAT = (rr->NATinfo.clientContext != mDNSNULL);
2292    mDNSBool PortWasMapped = (rr->NATinfo.clientContext && !mDNSSameIPPort(rr->NATinfo.RequestedPort, port));       // I think this is always false -- SC Sept 07
2293    mDNSBool NATChanged    = (!WereBehindNAT && NowNeedNATMAP) || (!NowNeedNATMAP && PortWasMapped);
2294
2295    (void)HaveZoneData; //unused
2296
2297    LogInfo("UpdateOneSRVRecord: Resource Record %s TargetChanged %d, NewTarget %##s", ARDisplayString(m, rr), TargetChanged, nt->c);
2298
2299    debugf("UpdateOneSRVRecord: %##s newtarget %##s TargetChanged %d HaveZoneData %d port %d NowNeedNATMAP %d WereBehindNAT %d PortWasMapped %d NATChanged %d",
2300           rr->resrec.name->c, newtarget,
2301           TargetChanged, HaveZoneData, mDNSVal16(port), NowNeedNATMAP, WereBehindNAT, PortWasMapped, NATChanged);
2302
2303    mDNS_CheckLock(m);
2304
2305    if (!TargetChanged && !NATChanged) return;
2306
2307    // If we are deregistering the record, then ignore any NAT/Target change.
2308    if (rr->resrec.RecordType == kDNSRecordTypeDeregistering)
2309    {
2310        LogInfo("UpdateOneSRVRecord: Deregistering record, Ignoring TargetChanged %d, NATChanged %d for %##s, state %d", TargetChanged, NATChanged,
2311                rr->resrec.name->c, rr->state);
2312        return;
2313    }
2314
2315    if (newtarget)
2316        LogInfo("UpdateOneSRVRecord: TargetChanged %d, NATChanged %d for %##s, state %d, newtarget %##s", TargetChanged, NATChanged, rr->resrec.name->c, rr->state, newtarget->c);
2317    else
2318        LogInfo("UpdateOneSRVRecord: TargetChanged %d, NATChanged %d for %##s, state %d, null newtarget", TargetChanged, NATChanged, rr->resrec.name->c, rr->state);
2319    switch(rr->state)
2320    {
2321    case regState_NATMap:
2322        // In these states, the SRV has either not yet been registered (it will get up-to-date information when it is)
2323        // or is in the process of, or has already been, deregistered. This assumes that whenever we transition out
2324        // of this state, we need to look at the target again.
2325        return;
2326
2327    case regState_UpdatePending:
2328        // We are getting a Target change/NAT change while the SRV record is being updated ?
2329        // let us not do anything for now.
2330        return;
2331
2332    case regState_NATError:
2333        if (!NATChanged) return;
2334	// if nat changed, register if we have a target (below)
2335	/* FALLTHROUGH */
2336
2337    case regState_NoTarget:
2338        if (!newtarget->c[0])
2339        {
2340            LogInfo("UpdateOneSRVRecord: No target yet for Resource Record %s", ARDisplayString(m, rr));
2341            return;
2342        }
2343        RegisterAllServiceRecords(m, rr);
2344        return;
2345    case regState_DeregPending:
2346    // We are in DeregPending either because the service was deregistered from above or we handled
2347    // a NAT/Target change before and sent the deregistration below. There are a few race conditions
2348    // possible
2349    //
2350    // 1. We are handling a second NAT/Target change while the first dereg is in progress. It is possible
2351    //    that first dereg never made it through because there was no network connectivity e.g., disconnecting
2352    //    from network triggers this function due to a target change and later connecting to the network
2353    //    retriggers this function but the deregistration never made it through yet. Just fall through.
2354    //    If there is a target register otherwise deregister.
2355    //
2356    // 2. While we sent the dereg during a previous NAT/Target change, uDNS_DeregisterRecord gets
2357    //    called as part of service deregistration. When the response comes back, we call
2358    //    CompleteDeregistration rather than handle NAT/Target change because the record is in
2359    //    kDNSRecordTypeDeregistering state.
2360    //
2361    // 3. If the upper layer deregisters the service, we check for kDNSRecordTypeDeregistering both
2362    //    here in this function to avoid handling NAT/Target change and in hndlRecordUpdateReply to call
2363    //    CompleteDeregistration instead of handling NAT/Target change. Hence, we are not concerned
2364    //    about that case here.
2365    //
2366    // We just handle case (1) by falling through
2367    case regState_Pending:
2368    case regState_Refresh:
2369    case regState_Registered:
2370        // target or nat changed.  deregister service.  upon completion, we'll look for a new target
2371        rr->SRVChanged = mDNStrue;
2372        rr->ThisAPInterval = INIT_RECORD_REG_INTERVAL;
2373        rr->LastAPTime = m->timenow - INIT_RECORD_REG_INTERVAL;
2374        if (newtarget->c[0])
2375        {
2376            LogInfo("UpdateOneSRVRecord: SRV record changed for service %##s, registering with new target %##s",
2377                    rr->resrec.name->c, newtarget->c);
2378            rr->state = regState_Pending;
2379        }
2380        else
2381        {
2382            LogInfo("UpdateOneSRVRecord: SRV record changed for service %##s de-registering", rr->resrec.name->c);
2383            rr->state = regState_DeregPending;
2384            UpdateAllServiceRecords(m, rr, mDNSfalse);
2385        }
2386        return;
2387    case regState_Unregistered:
2388    default: LogMsg("UpdateOneSRVRecord: Unknown state %d for %##s", rr->state, rr->resrec.name->c);
2389    }
2390}
2391
2392mDNSexport void UpdateAllSRVRecords(mDNS *m)
2393{
2394    m->NextSRVUpdate = 0;
2395    LogInfo("UpdateAllSRVRecords %d", m->SleepState);
2396
2397    if (m->CurrentRecord)
2398        LogMsg("UpdateAllSRVRecords ERROR m->CurrentRecord already set %s", ARDisplayString(m, m->CurrentRecord));
2399    m->CurrentRecord = m->ResourceRecords;
2400    while (m->CurrentRecord)
2401    {
2402        AuthRecord *rptr = m->CurrentRecord;
2403        m->CurrentRecord = m->CurrentRecord->next;
2404        if (AuthRecord_uDNS(rptr) && rptr->resrec.rrtype == kDNSType_SRV)
2405            UpdateOneSRVRecord(m, rptr);
2406    }
2407}
2408
2409// Forward reference: AdvertiseHostname references HostnameCallback, and HostnameCallback calls AdvertiseHostname
2410mDNSlocal void HostnameCallback(mDNS *const m, AuthRecord *const rr, mStatus result);
2411
2412// Called in normal client context (lock not held)
2413mDNSlocal void hostnameGetPublicAddressCallback(mDNS *m, NATTraversalInfo *n)
2414{
2415    HostnameInfo *h = (HostnameInfo *)n->clientContext;
2416
2417    if (!h) { LogMsg("RegisterHostnameRecord: registration cancelled"); return; }
2418
2419    if (!n->Result)
2420    {
2421        if (mDNSIPv4AddressIsZero(n->ExternalAddress) || mDNSv4AddrIsRFC1918(&n->ExternalAddress)) return;
2422
2423        if (h->arv4.resrec.RecordType)
2424        {
2425            if (mDNSSameIPv4Address(h->arv4.resrec.rdata->u.ipv4, n->ExternalAddress)) return;  // If address unchanged, do nothing
2426            LogInfo("Updating hostname %p %##s IPv4 from %.4a to %.4a (NAT gateway's external address)",n,
2427                    h->arv4.resrec.name->c, &h->arv4.resrec.rdata->u.ipv4, &n->ExternalAddress);
2428            mDNS_Deregister(m, &h->arv4);   // mStatus_MemFree callback will re-register with new address
2429        }
2430        else
2431        {
2432            LogInfo("Advertising hostname %##s IPv4 %.4a (NAT gateway's external address)", h->arv4.resrec.name->c, &n->ExternalAddress);
2433            h->arv4.resrec.RecordType = kDNSRecordTypeKnownUnique;
2434            h->arv4.resrec.rdata->u.ipv4 = n->ExternalAddress;
2435            mDNS_Register(m, &h->arv4);
2436        }
2437    }
2438}
2439
2440// register record or begin NAT traversal
2441mDNSlocal void AdvertiseHostname(mDNS *m, HostnameInfo *h)
2442{
2443    if (!mDNSIPv4AddressIsZero(m->AdvertisedV4.ip.v4) && h->arv4.resrec.RecordType == kDNSRecordTypeUnregistered)
2444    {
2445        mDNS_SetupResourceRecord(&h->arv4, mDNSNULL, mDNSInterface_Any, kDNSType_A, kHostNameTTL, kDNSRecordTypeUnregistered, AuthRecordAny, HostnameCallback, h);
2446        AssignDomainName(&h->arv4.namestorage, &h->fqdn);
2447        h->arv4.resrec.rdata->u.ipv4 = m->AdvertisedV4.ip.v4;
2448        h->arv4.state = regState_Unregistered;
2449        if (mDNSv4AddrIsRFC1918(&m->AdvertisedV4.ip.v4))
2450        {
2451            // If we already have a NAT query active, stop it and restart it to make sure we get another callback
2452            if (h->natinfo.clientContext) mDNS_StopNATOperation_internal(m, &h->natinfo);
2453            h->natinfo.Protocol         = 0;
2454            h->natinfo.IntPort          = zeroIPPort;
2455            h->natinfo.RequestedPort    = zeroIPPort;
2456            h->natinfo.NATLease         = 0;
2457            h->natinfo.clientCallback   = hostnameGetPublicAddressCallback;
2458            h->natinfo.clientContext    = h;
2459            mDNS_StartNATOperation_internal(m, &h->natinfo);
2460        }
2461        else
2462        {
2463            LogInfo("Advertising hostname %##s IPv4 %.4a", h->arv4.resrec.name->c, &m->AdvertisedV4.ip.v4);
2464            h->arv4.resrec.RecordType = kDNSRecordTypeKnownUnique;
2465            mDNS_Register_internal(m, &h->arv4);
2466        }
2467    }
2468
2469    if (!mDNSIPv6AddressIsZero(m->AdvertisedV6.ip.v6) && h->arv6.resrec.RecordType == kDNSRecordTypeUnregistered)
2470    {
2471        mDNS_SetupResourceRecord(&h->arv6, mDNSNULL, mDNSInterface_Any, kDNSType_AAAA, kHostNameTTL, kDNSRecordTypeKnownUnique, AuthRecordAny, HostnameCallback, h);
2472        AssignDomainName(&h->arv6.namestorage, &h->fqdn);
2473        h->arv6.resrec.rdata->u.ipv6 = m->AdvertisedV6.ip.v6;
2474        h->arv6.state = regState_Unregistered;
2475        LogInfo("Advertising hostname %##s IPv6 %.16a", h->arv6.resrec.name->c, &m->AdvertisedV6.ip.v6);
2476        mDNS_Register_internal(m, &h->arv6);
2477    }
2478}
2479
2480mDNSlocal void HostnameCallback(mDNS *const m, AuthRecord *const rr, mStatus result)
2481{
2482    HostnameInfo *hi = (HostnameInfo *)rr->RecordContext;
2483
2484    if (result == mStatus_MemFree)
2485    {
2486        if (hi)
2487        {
2488            // If we're still in the Hostnames list, update to new address
2489            HostnameInfo *i;
2490            LogInfo("HostnameCallback: Got mStatus_MemFree for %p %p %s", hi, rr, ARDisplayString(m, rr));
2491            for (i = m->Hostnames; i; i = i->next)
2492                if (rr == &i->arv4 || rr == &i->arv6)
2493                { mDNS_Lock(m); AdvertiseHostname(m, i); mDNS_Unlock(m); return; }
2494
2495            // Else, we're not still in the Hostnames list, so free the memory
2496            if (hi->arv4.resrec.RecordType == kDNSRecordTypeUnregistered &&
2497                hi->arv6.resrec.RecordType == kDNSRecordTypeUnregistered)
2498            {
2499                if (hi->natinfo.clientContext) mDNS_StopNATOperation_internal(m, &hi->natinfo);
2500                hi->natinfo.clientContext = mDNSNULL;
2501                mDNSPlatformMemFree(hi);    // free hi when both v4 and v6 AuthRecs deallocated
2502            }
2503        }
2504        return;
2505    }
2506
2507    if (result)
2508    {
2509        // don't unlink or free - we can retry when we get a new address/router
2510        if (rr->resrec.rrtype == kDNSType_A)
2511            LogMsg("HostnameCallback: Error %d for registration of %##s IP %.4a", result, rr->resrec.name->c, &rr->resrec.rdata->u.ipv4);
2512        else
2513            LogMsg("HostnameCallback: Error %d for registration of %##s IP %.16a", result, rr->resrec.name->c, &rr->resrec.rdata->u.ipv6);
2514        if (!hi) { mDNSPlatformMemFree(rr); return; }
2515        if (rr->state != regState_Unregistered) LogMsg("Error: HostnameCallback invoked with error code for record not in regState_Unregistered!");
2516
2517        if (hi->arv4.state == regState_Unregistered &&
2518            hi->arv6.state == regState_Unregistered)
2519        {
2520            // only deliver status if both v4 and v6 fail
2521            rr->RecordContext = (void *)hi->StatusContext;
2522            if (hi->StatusCallback)
2523                hi->StatusCallback(m, rr, result); // client may NOT make API calls here
2524            rr->RecordContext = (void *)hi;
2525        }
2526        return;
2527    }
2528
2529    // register any pending services that require a target
2530    mDNS_Lock(m);
2531    m->NextSRVUpdate = NonZeroTime(m->timenow);
2532    mDNS_Unlock(m);
2533
2534    // Deliver success to client
2535    if (!hi) { LogMsg("HostnameCallback invoked with orphaned address record"); return; }
2536    if (rr->resrec.rrtype == kDNSType_A)
2537        LogInfo("Registered hostname %##s IP %.4a", rr->resrec.name->c, &rr->resrec.rdata->u.ipv4);
2538    else
2539        LogInfo("Registered hostname %##s IP %.16a", rr->resrec.name->c, &rr->resrec.rdata->u.ipv6);
2540
2541    rr->RecordContext = (void *)hi->StatusContext;
2542    if (hi->StatusCallback)
2543        hi->StatusCallback(m, rr, result); // client may NOT make API calls here
2544    rr->RecordContext = (void *)hi;
2545}
2546
2547mDNSlocal void FoundStaticHostname(mDNS *const m, DNSQuestion *question, const ResourceRecord *const answer, QC_result AddRecord)
2548{
2549    const domainname *pktname = &answer->rdata->u.name;
2550    domainname *storedname = &m->StaticHostname;
2551    HostnameInfo *h = m->Hostnames;
2552
2553    (void)question;
2554
2555    if (answer->rdlength != 0)
2556        LogInfo("FoundStaticHostname: question %##s -> answer %##s (%s)", question->qname.c, answer->rdata->u.name.c, AddRecord ? "ADD" : "RMV");
2557    else
2558        LogInfo("FoundStaticHostname: question %##s -> answer NULL (%s)", question->qname.c, AddRecord ? "ADD" : "RMV");
2559
2560    if (AddRecord && answer->rdlength != 0 && !SameDomainName(pktname, storedname))
2561    {
2562        AssignDomainName(storedname, pktname);
2563        while (h)
2564        {
2565            if (h->arv4.state == regState_Pending || h->arv4.state == regState_NATMap || h->arv6.state == regState_Pending)
2566            {
2567                // if we're in the process of registering a dynamic hostname, delay SRV update so we don't have to reregister services if the dynamic name succeeds
2568                m->NextSRVUpdate = NonZeroTime(m->timenow + 5 * mDNSPlatformOneSecond);
2569                debugf("FoundStaticHostname: NextSRVUpdate in %d %d", m->NextSRVUpdate - m->timenow, m->timenow);
2570                return;
2571            }
2572            h = h->next;
2573        }
2574        mDNS_Lock(m);
2575        m->NextSRVUpdate = NonZeroTime(m->timenow);
2576        mDNS_Unlock(m);
2577    }
2578    else if (!AddRecord && SameDomainName(pktname, storedname))
2579    {
2580        mDNS_Lock(m);
2581        storedname->c[0] = 0;
2582        m->NextSRVUpdate = NonZeroTime(m->timenow);
2583        mDNS_Unlock(m);
2584    }
2585}
2586
2587// Called with lock held
2588mDNSlocal void GetStaticHostname(mDNS *m)
2589{
2590    char buf[MAX_REVERSE_MAPPING_NAME_V4];
2591    DNSQuestion *q = &m->ReverseMap;
2592    mDNSu8 *ip = m->AdvertisedV4.ip.v4.b;
2593    mStatus err;
2594
2595    if (m->ReverseMap.ThisQInterval != -1) return; // already running
2596    if (mDNSIPv4AddressIsZero(m->AdvertisedV4.ip.v4)) return;
2597
2598    mDNSPlatformMemZero(q, sizeof(*q));
2599    // Note: This is reverse order compared to a normal dotted-decimal IP address, so we can't use our customary "%.4a" format code
2600    mDNS_snprintf(buf, sizeof(buf), "%d.%d.%d.%d.in-addr.arpa.", ip[3], ip[2], ip[1], ip[0]);
2601    if (!MakeDomainNameFromDNSNameString(&q->qname, buf)) { LogMsg("Error: GetStaticHostname - bad name %s", buf); return; }
2602
2603    q->InterfaceID      = mDNSInterface_Any;
2604    q->flags            = 0;
2605    q->Target           = zeroAddr;
2606    q->qtype            = kDNSType_PTR;
2607    q->qclass           = kDNSClass_IN;
2608    q->LongLived        = mDNSfalse;
2609    q->ExpectUnique     = mDNSfalse;
2610    q->ForceMCast       = mDNSfalse;
2611    q->ReturnIntermed   = mDNStrue;
2612    q->SuppressUnusable = mDNSfalse;
2613    q->SearchListIndex  = 0;
2614    q->AppendSearchDomains = 0;
2615    q->RetryWithSearchDomains = mDNSfalse;
2616    q->TimeoutQuestion  = 0;
2617    q->WakeOnResolve    = 0;
2618    q->UseBackgroundTrafficClass = mDNSfalse;
2619    q->ValidationRequired = 0;
2620    q->ValidatingResponse = 0;
2621    q->ProxyQuestion      = 0;
2622    q->qnameOrig        = mDNSNULL;
2623    q->AnonInfo         = mDNSNULL;
2624    q->pid              = mDNSPlatformGetPID();
2625    q->euid             = 0;
2626    q->QuestionCallback = FoundStaticHostname;
2627    q->QuestionContext  = mDNSNULL;
2628
2629    LogInfo("GetStaticHostname: %##s (%s)", q->qname.c, DNSTypeName(q->qtype));
2630    err = mDNS_StartQuery_internal(m, q);
2631    if (err) LogMsg("Error: GetStaticHostname - StartQuery returned error %d", err);
2632}
2633
2634mDNSexport void mDNS_AddDynDNSHostName(mDNS *m, const domainname *fqdn, mDNSRecordCallback *StatusCallback, const void *StatusContext)
2635{
2636    HostnameInfo **ptr = &m->Hostnames;
2637
2638    LogInfo("mDNS_AddDynDNSHostName %##s", fqdn);
2639
2640    while (*ptr && !SameDomainName(fqdn, &(*ptr)->fqdn)) ptr = &(*ptr)->next;
2641    if (*ptr) { LogMsg("DynDNSHostName %##s already in list", fqdn->c); return; }
2642
2643    // allocate and format new address record
2644    *ptr = mDNSPlatformMemAllocate(sizeof(**ptr));
2645    if (!*ptr) { LogMsg("ERROR: mDNS_AddDynDNSHostName - malloc"); return; }
2646
2647    mDNSPlatformMemZero(*ptr, sizeof(**ptr));
2648    AssignDomainName(&(*ptr)->fqdn, fqdn);
2649    (*ptr)->arv4.state     = regState_Unregistered;
2650    (*ptr)->arv6.state     = regState_Unregistered;
2651    (*ptr)->StatusCallback = StatusCallback;
2652    (*ptr)->StatusContext  = StatusContext;
2653
2654    AdvertiseHostname(m, *ptr);
2655}
2656
2657mDNSexport void mDNS_RemoveDynDNSHostName(mDNS *m, const domainname *fqdn)
2658{
2659    HostnameInfo **ptr = &m->Hostnames;
2660
2661    LogInfo("mDNS_RemoveDynDNSHostName %##s", fqdn);
2662
2663    while (*ptr && !SameDomainName(fqdn, &(*ptr)->fqdn)) ptr = &(*ptr)->next;
2664    if (!*ptr) LogMsg("mDNS_RemoveDynDNSHostName: no such domainname %##s", fqdn->c);
2665    else
2666    {
2667        HostnameInfo *hi = *ptr;
2668        // We do it this way because, if we have no active v6 record, the "mDNS_Deregister_internal(m, &hi->arv4);"
2669        // below could free the memory, and we have to make sure we don't touch hi fields after that.
2670        mDNSBool f4 = hi->arv4.resrec.RecordType != kDNSRecordTypeUnregistered && hi->arv4.state != regState_Unregistered;
2671        mDNSBool f6 = hi->arv6.resrec.RecordType != kDNSRecordTypeUnregistered && hi->arv6.state != regState_Unregistered;
2672        *ptr = (*ptr)->next; // unlink
2673        if (f4 || f6)
2674        {
2675            if (f4)
2676            {
2677                LogInfo("mDNS_RemoveDynDNSHostName removing v4 %##s", fqdn);
2678                mDNS_Deregister_internal(m, &hi->arv4, mDNS_Dereg_normal);
2679            }
2680            if (f6)
2681            {
2682                LogInfo("mDNS_RemoveDynDNSHostName removing v6 %##s", fqdn);
2683                mDNS_Deregister_internal(m, &hi->arv6, mDNS_Dereg_normal);
2684            }
2685            // When both deregistrations complete we'll free the memory in the mStatus_MemFree callback
2686        }
2687        else
2688        {
2689            if (hi->natinfo.clientContext)
2690            {
2691                mDNS_StopNATOperation_internal(m, &hi->natinfo);
2692                hi->natinfo.clientContext = mDNSNULL;
2693            }
2694            mDNSPlatformMemFree(hi);
2695        }
2696    }
2697    mDNS_CheckLock(m);
2698    m->NextSRVUpdate = NonZeroTime(m->timenow);
2699}
2700
2701// Currently called without holding the lock
2702// Maybe we should change that?
2703mDNSexport void mDNS_SetPrimaryInterfaceInfo(mDNS *m, const mDNSAddr *v4addr, const mDNSAddr *v6addr, const mDNSAddr *router)
2704{
2705    mDNSBool v4Changed, v6Changed, RouterChanged;
2706    mDNSv6Addr v6;
2707
2708    if (m->mDNS_busy != m->mDNS_reentrancy)
2709        LogMsg("mDNS_SetPrimaryInterfaceInfo: mDNS_busy (%ld) != mDNS_reentrancy (%ld)", m->mDNS_busy, m->mDNS_reentrancy);
2710
2711    if (v4addr && v4addr->type != mDNSAddrType_IPv4) { LogMsg("mDNS_SetPrimaryInterfaceInfo v4 address - incorrect type.  Discarding. %#a", v4addr); return; }
2712    if (v6addr && v6addr->type != mDNSAddrType_IPv6) { LogMsg("mDNS_SetPrimaryInterfaceInfo v6 address - incorrect type.  Discarding. %#a", v6addr); return; }
2713    if (router && router->type != mDNSAddrType_IPv4) { LogMsg("mDNS_SetPrimaryInterfaceInfo passed non-v4 router.  Discarding. %#a",        router); return; }
2714
2715    mDNS_Lock(m);
2716
2717    v4Changed     = !mDNSSameIPv4Address(m->AdvertisedV4.ip.v4, v4addr ? v4addr->ip.v4 : zerov4Addr);
2718    if (v6addr)
2719	v6 = v6addr->ip.v6;
2720    else
2721	v6 = zerov6Addr;
2722    v6Changed     = !mDNSSameIPv6Address(m->AdvertisedV6.ip.v6, v6);
2723    RouterChanged = !mDNSSameIPv4Address(m->Router.ip.v4,       router ? router->ip.v4 : zerov4Addr);
2724
2725    if (v4addr && (v4Changed || RouterChanged))
2726        debugf("mDNS_SetPrimaryInterfaceInfo: address changed from %#a to %#a", &m->AdvertisedV4, v4addr);
2727
2728    if (v4addr) m->AdvertisedV4 = *v4addr;else m->AdvertisedV4.ip.v4 = zerov4Addr;
2729    if (v6addr) m->AdvertisedV6 = *v6addr;else m->AdvertisedV6.ip.v6 = zerov6Addr;
2730    if (router) m->Router       = *router;else m->Router.ip.v4 = zerov4Addr;
2731    // setting router to zero indicates that nat mappings must be reestablished when router is reset
2732
2733    if (v4Changed || RouterChanged || v6Changed)
2734    {
2735        HostnameInfo *i;
2736        LogInfo("mDNS_SetPrimaryInterfaceInfo: %s%s%s%#a %#a %#a",
2737                v4Changed     ? "v4Changed "     : "",
2738                RouterChanged ? "RouterChanged " : "",
2739                v6Changed     ? "v6Changed "     : "", v4addr, v6addr, router);
2740
2741        for (i = m->Hostnames; i; i = i->next)
2742        {
2743            LogInfo("mDNS_SetPrimaryInterfaceInfo updating host name registrations for %##s", i->fqdn.c);
2744
2745            if (i->arv4.resrec.RecordType > kDNSRecordTypeDeregistering &&
2746                !mDNSSameIPv4Address(i->arv4.resrec.rdata->u.ipv4, m->AdvertisedV4.ip.v4))
2747            {
2748                LogInfo("mDNS_SetPrimaryInterfaceInfo deregistering %s", ARDisplayString(m, &i->arv4));
2749                mDNS_Deregister_internal(m, &i->arv4, mDNS_Dereg_normal);
2750            }
2751
2752            if (i->arv6.resrec.RecordType > kDNSRecordTypeDeregistering &&
2753                !mDNSSameIPv6Address(i->arv6.resrec.rdata->u.ipv6, m->AdvertisedV6.ip.v6))
2754            {
2755                LogInfo("mDNS_SetPrimaryInterfaceInfo deregistering %s", ARDisplayString(m, &i->arv6));
2756                mDNS_Deregister_internal(m, &i->arv6, mDNS_Dereg_normal);
2757            }
2758
2759            // AdvertiseHostname will only register new address records.
2760            // For records still in the process of deregistering it will ignore them, and let the mStatus_MemFree callback handle them.
2761            AdvertiseHostname(m, i);
2762        }
2763
2764        if (v4Changed || RouterChanged)
2765        {
2766            // If we have a non-zero IPv4 address, we should try immediately to see if we have a NAT gateway
2767            // If we have no IPv4 address, we don't want to be in quite such a hurry to report failures to our clients
2768            // <rdar://problem/6935929> Sleeping server sometimes briefly disappears over Back to My Mac after it wakes up
2769            mDNSu32 waitSeconds = v4addr ? 0 : 5;
2770            NATTraversalInfo *n;
2771            m->ExtAddress           = zerov4Addr;
2772            m->LastNATMapResultCode = NATErr_None;
2773
2774            RecreateNATMappings(m, mDNSPlatformOneSecond * waitSeconds);
2775
2776            for (n = m->NATTraversals; n; n=n->next)
2777                n->NewAddress = zerov4Addr;
2778
2779            LogInfo("mDNS_SetPrimaryInterfaceInfo:%s%s: recreating NAT mappings in %d seconds",
2780                    v4Changed     ? " v4Changed"     : "",
2781                    RouterChanged ? " RouterChanged" : "",
2782                    waitSeconds);
2783        }
2784
2785        if (m->ReverseMap.ThisQInterval != -1) mDNS_StopQuery_internal(m, &m->ReverseMap);
2786        m->StaticHostname.c[0] = 0;
2787
2788        m->NextSRVUpdate = NonZeroTime(m->timenow);
2789
2790#if APPLE_OSX_mDNSResponder
2791        UpdateAutoTunnelDomainStatuses(m);
2792#endif
2793    }
2794
2795    mDNS_Unlock(m);
2796}
2797
2798// ***************************************************************************
2799#if COMPILER_LIKES_PRAGMA_MARK
2800#pragma mark - Incoming Message Processing
2801#endif
2802
2803mDNSlocal mStatus ParseTSIGError(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *const end, const domainname *const displayname)
2804{
2805    const mDNSu8 *ptr;
2806    mStatus err = mStatus_NoError;
2807    int i;
2808
2809    ptr = LocateAdditionals(msg, end);
2810    if (!ptr) goto finish;
2811
2812    for (i = 0; i < msg->h.numAdditionals; i++)
2813    {
2814        ptr = GetLargeResourceRecord(m, msg, ptr, end, 0, kDNSRecordTypePacketAdd, &m->rec);
2815        if (!ptr) goto finish;
2816        if (m->rec.r.resrec.RecordType != kDNSRecordTypePacketNegative && m->rec.r.resrec.rrtype == kDNSType_TSIG)
2817        {
2818            mDNSu32 macsize;
2819            mDNSu8 *rd = m->rec.r.resrec.rdata->u.data;
2820            mDNSu8 *rdend = rd + m->rec.r.resrec.rdlength;
2821            int alglen = DomainNameLengthLimit(&m->rec.r.resrec.rdata->u.name, rdend);
2822            if (alglen > MAX_DOMAIN_NAME) goto finish;
2823            rd += alglen;                                       // algorithm name
2824            if (rd + 6 > rdend) goto finish;
2825            rd += 6;                                            // 48-bit timestamp
2826            if (rd + sizeof(mDNSOpaque16) > rdend) goto finish;
2827            rd += sizeof(mDNSOpaque16);                         // fudge
2828            if (rd + sizeof(mDNSOpaque16) > rdend) goto finish;
2829            macsize = mDNSVal16(*(mDNSOpaque16 *)rd);
2830            rd += sizeof(mDNSOpaque16);                         // MAC size
2831            if (rd + macsize > rdend) goto finish;
2832            rd += macsize;
2833            if (rd + sizeof(mDNSOpaque16) > rdend) goto finish;
2834            rd += sizeof(mDNSOpaque16);                         // orig id
2835            if (rd + sizeof(mDNSOpaque16) > rdend) goto finish;
2836            err = mDNSVal16(*(mDNSOpaque16 *)rd);               // error code
2837
2838            if      (err == TSIG_ErrBadSig)  { LogMsg("%##s: bad signature", displayname->c);              err = mStatus_BadSig;     }
2839            else if (err == TSIG_ErrBadKey)  { LogMsg("%##s: bad key", displayname->c);                    err = mStatus_BadKey;     }
2840            else if (err == TSIG_ErrBadTime) { LogMsg("%##s: bad time", displayname->c);                   err = mStatus_BadTime;    }
2841            else if (err)                    { LogMsg("%##s: unknown tsig error %d", displayname->c, err); err = mStatus_UnknownErr; }
2842            goto finish;
2843        }
2844        m->rec.r.resrec.RecordType = 0;     // Clear RecordType to show we're not still using it
2845    }
2846
2847finish:
2848    m->rec.r.resrec.RecordType = 0;     // Clear RecordType to show we're not still using it
2849    return err;
2850}
2851
2852mDNSlocal mStatus checkUpdateResult(mDNS *const m, const domainname *const displayname, const mDNSu8 rcode, const DNSMessage *const msg, const mDNSu8 *const end)
2853{
2854    (void)msg;  // currently unused, needed for TSIG errors
2855    if (!rcode) return mStatus_NoError;
2856    else if (rcode == kDNSFlag1_RC_YXDomain)
2857    {
2858        debugf("name in use: %##s", displayname->c);
2859        return mStatus_NameConflict;
2860    }
2861    else if (rcode == kDNSFlag1_RC_Refused)
2862    {
2863        LogMsg("Update %##s refused", displayname->c);
2864        return mStatus_Refused;
2865    }
2866    else if (rcode == kDNSFlag1_RC_NXRRSet)
2867    {
2868        LogMsg("Reregister refused (NXRRSET): %##s", displayname->c);
2869        return mStatus_NoSuchRecord;
2870    }
2871    else if (rcode == kDNSFlag1_RC_NotAuth)
2872    {
2873        // TSIG errors should come with FormErr as per RFC 2845, but BIND 9 sends them with NotAuth so we look here too
2874        mStatus tsigerr = ParseTSIGError(m, msg, end, displayname);
2875        if (!tsigerr)
2876        {
2877            LogMsg("Permission denied (NOAUTH): %##s", displayname->c);
2878            return mStatus_UnknownErr;
2879        }
2880        else return tsigerr;
2881    }
2882    else if (rcode == kDNSFlag1_RC_FormErr)
2883    {
2884        mStatus tsigerr = ParseTSIGError(m, msg, end, displayname);
2885        if (!tsigerr)
2886        {
2887            LogMsg("Format Error: %##s", displayname->c);
2888            return mStatus_UnknownErr;
2889        }
2890        else return tsigerr;
2891    }
2892    else
2893    {
2894        LogMsg("Update %##s failed with rcode %d", displayname->c, rcode);
2895        return mStatus_UnknownErr;
2896    }
2897}
2898
2899// We add three Additional Records for unicast resource record registrations
2900// which is a function of AuthInfo and AutoTunnel properties
2901mDNSlocal mDNSu32 RRAdditionalSize(mDNS *const m, DomainAuthInfo *AuthInfo)
2902{
2903    mDNSu32 leaseSize, hinfoSize, tsigSize;
2904    mDNSu32 rr_base_size = 10; // type (2) class (2) TTL (4) rdlength (2)
2905
2906    // OPT RR : Emptyname(.) + base size + rdataOPT
2907    leaseSize = 1 + rr_base_size + sizeof(rdataOPT);
2908
2909    // HINFO: Resource Record Name + base size + RDATA
2910    // HINFO is added only for autotunnels
2911    hinfoSize = 0;
2912    if (AuthInfo && AuthInfo->AutoTunnel)
2913        hinfoSize = (m->hostlabel.c[0] + 1) + DomainNameLength(&AuthInfo->domain) +
2914                    rr_base_size + (2 + m->HIHardware.c[0] + m->HISoftware.c[0]);
2915
2916    //TSIG: Resource Record Name + base size + RDATA
2917    // RDATA:
2918    //  Algorithm name: hmac-md5.sig-alg.reg.int (8+7+3+3 + 5 bytes for length = 26 bytes)
2919    //  Time: 6 bytes
2920    //  Fudge: 2 bytes
2921    //  Mac Size: 2 bytes
2922    //  Mac: 16 bytes
2923    //  ID: 2 bytes
2924    //  Error: 2 bytes
2925    //  Len: 2 bytes
2926    //  Total: 58 bytes
2927    tsigSize = 0;
2928    if (AuthInfo) tsigSize = DomainNameLength(&AuthInfo->keyname) + rr_base_size + 58;
2929
2930    return (leaseSize + hinfoSize + tsigSize);
2931}
2932
2933//Note: Make sure that RREstimatedSize is updated accordingly if anything that is done here
2934//would modify rdlength/rdestimate
2935mDNSlocal mDNSu8* BuildUpdateMessage(mDNS *const m, mDNSu8 *ptr, AuthRecord *rr, mDNSu8 *limit)
2936{
2937    //If this record is deregistering, then just send the deletion record
2938    if (rr->state == regState_DeregPending)
2939    {
2940        rr->expire = 0;     // Indicate that we have no active registration any more
2941        ptr = putDeletionRecordWithLimit(&m->omsg, ptr, &rr->resrec, limit);
2942        if (!ptr) goto exit;
2943        return ptr;
2944    }
2945
2946    // This is a common function to both sending an update in a group or individual
2947    // records separately. Hence, we change the state here.
2948    if (rr->state == regState_Registered) rr->state = regState_Refresh;
2949    if (rr->state != regState_Refresh && rr->state != regState_UpdatePending)
2950        rr->state = regState_Pending;
2951
2952    // For Advisory records like e.g., _services._dns-sd, which is shared, don't send goodbyes as multiple
2953    // host might be registering records and deregistering from one does not make sense
2954    if (rr->resrec.RecordType != kDNSRecordTypeAdvisory) rr->RequireGoodbye = mDNStrue;
2955
2956    if ((rr->resrec.rrtype == kDNSType_SRV) && (rr->AutoTarget == Target_AutoHostAndNATMAP) &&
2957        !mDNSIPPortIsZero(rr->NATinfo.ExternalPort))
2958    {
2959        rr->resrec.rdata->u.srv.port = rr->NATinfo.ExternalPort;
2960    }
2961
2962    if (rr->state == regState_UpdatePending)
2963    {
2964        // delete old RData
2965        SetNewRData(&rr->resrec, rr->OrigRData, rr->OrigRDLen);
2966        if (!(ptr = putDeletionRecordWithLimit(&m->omsg, ptr, &rr->resrec, limit))) goto exit; // delete old rdata
2967
2968        // add new RData
2969        SetNewRData(&rr->resrec, rr->InFlightRData, rr->InFlightRDLen);
2970        if (!(ptr = PutResourceRecordTTLWithLimit(&m->omsg, ptr, &m->omsg.h.mDNS_numUpdates, &rr->resrec, rr->resrec.rroriginalttl, limit))) goto exit;
2971    }
2972    else
2973    {
2974        if (rr->resrec.RecordType == kDNSRecordTypeKnownUnique || rr->resrec.RecordType == kDNSRecordTypeVerified)
2975        {
2976            // KnownUnique : Delete any previous value
2977            // For Unicast registrations, we don't verify that it is unique, but set to verified and hence we want to
2978            // delete any previous value
2979            ptr = putDeleteRRSetWithLimit(&m->omsg, ptr, rr->resrec.name, rr->resrec.rrtype, limit);
2980            if (!ptr) goto exit;
2981        }
2982        else if (rr->resrec.RecordType != kDNSRecordTypeShared)
2983        {
2984            // For now don't do this, until we have the logic for intelligent grouping of individual records into logical service record sets
2985            //ptr = putPrereqNameNotInUse(rr->resrec.name, &m->omsg, ptr, end);
2986            if (!ptr) goto exit;
2987        }
2988
2989        ptr = PutResourceRecordTTLWithLimit(&m->omsg, ptr, &m->omsg.h.mDNS_numUpdates, &rr->resrec, rr->resrec.rroriginalttl, limit);
2990        if (!ptr) goto exit;
2991    }
2992
2993    return ptr;
2994exit:
2995    LogMsg("BuildUpdateMessage: Error formatting message for %s", ARDisplayString(m, rr));
2996    return mDNSNULL;
2997}
2998
2999// Called with lock held
3000mDNSlocal void SendRecordRegistration(mDNS *const m, AuthRecord *rr)
3001{
3002    mDNSu8 *ptr = m->omsg.data;
3003    mStatus err = mStatus_UnknownErr;
3004    mDNSu8 *limit;
3005    DomainAuthInfo *AuthInfo;
3006
3007    // For the ability to register large TXT records, we limit the single record registrations
3008    // to AbsoluteMaxDNSMessageData
3009    limit = ptr + AbsoluteMaxDNSMessageData;
3010
3011    AuthInfo = GetAuthInfoForName_internal(m, rr->resrec.name);
3012    limit -= RRAdditionalSize(m, AuthInfo);
3013
3014    mDNS_CheckLock(m);
3015
3016    if (!rr->nta || mDNSIPv4AddressIsZero(rr->nta->Addr.ip.v4))
3017    {
3018        // We never call this function when there is no zone information . Log a message if it ever happens.
3019        LogMsg("SendRecordRegistration: No Zone information, should not happen %s", ARDisplayString(m, rr));
3020        return;
3021    }
3022
3023    rr->updateid = mDNS_NewMessageID(m);
3024    InitializeDNSMessage(&m->omsg.h, rr->updateid, UpdateReqFlags);
3025
3026    // set zone
3027    ptr = putZone(&m->omsg, ptr, limit, rr->zone, mDNSOpaque16fromIntVal(rr->resrec.rrclass));
3028    if (!ptr) goto exit;
3029
3030    if (!(ptr = BuildUpdateMessage(m, ptr, rr, limit))) goto exit;
3031
3032    if (rr->uselease)
3033    {
3034        ptr = putUpdateLeaseWithLimit(&m->omsg, ptr, DEFAULT_UPDATE_LEASE, limit);
3035        if (!ptr) goto exit;
3036    }
3037    if (rr->Private)
3038    {
3039        LogInfo("SendRecordRegistration TCP %p %s", rr->tcp, ARDisplayString(m, rr));
3040        if (rr->tcp) LogInfo("SendRecordRegistration: Disposing existing TCP connection for %s", ARDisplayString(m, rr));
3041        if (rr->tcp) { DisposeTCPConn(rr->tcp); rr->tcp = mDNSNULL; }
3042        if (!rr->nta) { LogMsg("SendRecordRegistration:Private:ERROR!! nta is NULL for %s", ARDisplayString(m, rr)); return; }
3043        rr->tcp = MakeTCPConn(m, &m->omsg, ptr, kTCPSocketFlags_UseTLS, &rr->nta->Addr, rr->nta->Port, &rr->nta->Host, mDNSNULL, rr);
3044    }
3045    else
3046    {
3047        LogInfo("SendRecordRegistration UDP %s", ARDisplayString(m, rr));
3048        if (!rr->nta) { LogMsg("SendRecordRegistration:ERROR!! nta is NULL for %s", ARDisplayString(m, rr)); return; }
3049        err = mDNSSendDNSMessage(m, &m->omsg, ptr, mDNSInterface_Any, mDNSNULL, &rr->nta->Addr, rr->nta->Port, mDNSNULL, GetAuthInfoForName_internal(m, rr->resrec.name), mDNSfalse);
3050        if (err) debugf("ERROR: SendRecordRegistration - mDNSSendDNSMessage - %d", err);
3051    }
3052
3053    SetRecordRetry(m, rr, 0);
3054    return;
3055exit:
3056    LogMsg("SendRecordRegistration: Error formatting message for %s, disabling further updates", ARDisplayString(m, rr));
3057    // Disable this record from future updates
3058    rr->state = regState_NoTarget;
3059}
3060
3061// Is the given record "rr" eligible for merging ?
3062mDNSlocal mDNSBool IsRecordMergeable(mDNS *const m, AuthRecord *rr, mDNSs32 time)
3063{
3064    DomainAuthInfo *info;
3065    // A record is eligible for merge, if the following properties are met.
3066    //
3067    // 1. uDNS Resource Record
3068    // 2. It is time to send them now
3069    // 3. It is in proper state
3070    // 4. Update zone has been resolved
3071    // 5. if DomainAuthInfo exists for the zone, it should not be soon deleted
3072    // 6. Zone information is present
3073    // 7. Update server is not zero
3074    // 8. It has a non-null zone
3075    // 9. It uses a lease option
3076    // 10. DontMerge is not set
3077    //
3078    // Following code is implemented as separate "if" statements instead of one "if" statement
3079    // is for better debugging purposes e.g., we know exactly what failed if debugging turned on.
3080
3081    if (!AuthRecord_uDNS(rr)) return mDNSfalse;
3082
3083    if (rr->LastAPTime + rr->ThisAPInterval - time > 0)
3084    { debugf("IsRecordMergeable: Time %d not reached for %s", rr->LastAPTime + rr->ThisAPInterval - m->timenow, ARDisplayString(m, rr)); return mDNSfalse; }
3085
3086    if (!rr->zone) return mDNSfalse;
3087
3088    info = GetAuthInfoForName_internal(m, rr->zone);
3089
3090    if (info && info->deltime && m->timenow - info->deltime >= 0) {debugf("IsRecordMergeable: Domain %##s will be deleted soon", info->domain.c); return mDNSfalse;}
3091
3092    if (rr->state != regState_DeregPending && rr->state != regState_Pending && rr->state != regState_Registered && rr->state != regState_Refresh && rr->state != regState_UpdatePending)
3093    { debugf("IsRecordMergeable: state %d not right  %s", rr->state, ARDisplayString(m, rr)); return mDNSfalse; }
3094
3095    if (!rr->nta || mDNSIPv4AddressIsZero(rr->nta->Addr.ip.v4)) return mDNSfalse;
3096
3097    if (!rr->uselease) return mDNSfalse;
3098
3099    if (rr->mState == mergeState_DontMerge) {debugf("IsRecordMergeable Dontmerge true %s", ARDisplayString(m, rr)); return mDNSfalse;}
3100    debugf("IsRecordMergeable: Returning true for %s", ARDisplayString(m, rr));
3101    return mDNStrue;
3102}
3103
3104// Is the resource record "rr" eligible to merge to with "currentRR" ?
3105mDNSlocal mDNSBool AreRecordsMergeable(mDNS *const m, AuthRecord *currentRR, AuthRecord *rr, mDNSs32 time)
3106{
3107    // A record is eligible to merge with another record as long it is eligible for merge in itself
3108    // and it has the same zone information as the other record
3109    if (!IsRecordMergeable(m, rr, time)) return mDNSfalse;
3110
3111    if (!SameDomainName(currentRR->zone, rr->zone))
3112    { debugf("AreRecordMergeable zone mismatch current rr Zone %##s, rr zone  %##s", currentRR->zone->c, rr->zone->c); return mDNSfalse; }
3113
3114    if (!mDNSSameIPv4Address(currentRR->nta->Addr.ip.v4, rr->nta->Addr.ip.v4)) return mDNSfalse;
3115
3116    if (!mDNSSameIPPort(currentRR->nta->Port, rr->nta->Port)) return mDNSfalse;
3117
3118    debugf("AreRecordsMergeable: Returning true for %s", ARDisplayString(m, rr));
3119    return mDNStrue;
3120}
3121
3122// If we can't build the message successfully because of problems in pre-computing
3123// the space, we disable merging for all the current records
3124mDNSlocal void RRMergeFailure(mDNS *const m)
3125{
3126    AuthRecord *rr;
3127    for (rr = m->ResourceRecords; rr; rr = rr->next)
3128    {
3129        rr->mState = mergeState_DontMerge;
3130        rr->SendRNow = mDNSNULL;
3131        // Restarting the registration is much simpler than saving and restoring
3132        // the exact time
3133        ActivateUnicastRegistration(m, rr);
3134    }
3135}
3136
3137mDNSlocal void SendGroupRRMessage(mDNS *const m, AuthRecord *anchorRR, mDNSu8 *ptr, DomainAuthInfo *info)
3138{
3139    mDNSu8 *limit;
3140    if (!anchorRR) {debugf("SendGroupRRMessage: Could not merge records"); return;}
3141
3142    if (info && info->AutoTunnel) limit = m->omsg.data + AbsoluteMaxDNSMessageData;
3143    else limit = m->omsg.data + NormalMaxDNSMessageData;
3144
3145    // This has to go in the additional section and hence need to be done last
3146    ptr = putUpdateLeaseWithLimit(&m->omsg, ptr, DEFAULT_UPDATE_LEASE, limit);
3147    if (!ptr)
3148    {
3149        LogMsg("SendGroupRRMessage: ERROR: Could not put lease option, failing the group registration");
3150        // if we can't put the lease, we need to undo the merge
3151        RRMergeFailure(m);
3152        return;
3153    }
3154    if (anchorRR->Private)
3155    {
3156        if (anchorRR->tcp) debugf("SendGroupRRMessage: Disposing existing TCP connection for %s", ARDisplayString(m, anchorRR));
3157        if (anchorRR->tcp) { DisposeTCPConn(anchorRR->tcp); anchorRR->tcp = mDNSNULL; }
3158        if (!anchorRR->nta) { LogMsg("SendGroupRRMessage:ERROR!! nta is NULL for %s", ARDisplayString(m, anchorRR)); return; }
3159        anchorRR->tcp = MakeTCPConn(m, &m->omsg, ptr, kTCPSocketFlags_UseTLS, &anchorRR->nta->Addr, anchorRR->nta->Port, &anchorRR->nta->Host, mDNSNULL, anchorRR);
3160        if (!anchorRR->tcp) LogInfo("SendGroupRRMessage: Cannot establish TCP connection for %s", ARDisplayString(m, anchorRR));
3161        else LogInfo("SendGroupRRMessage: Sent a group update ID: %d start %p, end %p, limit %p", mDNSVal16(m->omsg.h.id), m->omsg.data, ptr, limit);
3162    }
3163    else
3164    {
3165        mStatus err = mDNSSendDNSMessage(m, &m->omsg, ptr, mDNSInterface_Any, mDNSNULL, &anchorRR->nta->Addr, anchorRR->nta->Port, mDNSNULL, info, mDNSfalse);
3166        if (err) LogInfo("SendGroupRRMessage: Cannot send UDP message for %s", ARDisplayString(m, anchorRR));
3167        else LogInfo("SendGroupRRMessage: Sent a group UDP update ID: %d start %p, end %p, limit %p", mDNSVal16(m->omsg.h.id), m->omsg.data, ptr, limit);
3168    }
3169    return;
3170}
3171
3172// As we always include the zone information and the resource records contain zone name
3173// at the end, it will get compressed. Hence, we subtract zoneSize and add two bytes for
3174// the compression pointer
3175mDNSlocal mDNSu32 RREstimatedSize(AuthRecord *rr, int zoneSize)
3176{
3177    int rdlength;
3178
3179    // Note: Estimation of the record size has to mirror the logic in BuildUpdateMessage, otherwise estimation
3180    // would be wrong. Currently BuildUpdateMessage calls SetNewRData in UpdatePending case. Hence, we need
3181    // to account for that here. Otherwise, we might under estimate the size.
3182    if (rr->state == regState_UpdatePending)
3183        // old RData that will be deleted
3184        // new RData that will be added
3185        rdlength = rr->OrigRDLen + rr->InFlightRDLen;
3186    else
3187        rdlength = rr->resrec.rdestimate;
3188
3189    if (rr->state == regState_DeregPending)
3190    {
3191        debugf("RREstimatedSize: ResourceRecord %##s (%s), DomainNameLength %d, zoneSize %d, rdestimate %d",
3192               rr->resrec.name->c, DNSTypeName(rr->resrec.rrtype), DomainNameLength(rr->resrec.name), zoneSize, rdlength);
3193        return DomainNameLength(rr->resrec.name) - zoneSize + 2 + 10 + rdlength;
3194    }
3195
3196    // For SRV, TXT, AAAA etc. that are Unique/Verified, we also send a Deletion Record
3197    if (rr->resrec.RecordType == kDNSRecordTypeKnownUnique || rr->resrec.RecordType == kDNSRecordTypeVerified)
3198    {
3199        // Deletion Record: Resource Record Name + Base size (10) + 0
3200        // Record: Resource Record Name (Compressed = 2) + Base size (10) + rdestimate
3201
3202        debugf("RREstimatedSize: ResourceRecord %##s (%s), DomainNameLength %d, zoneSize %d, rdestimate %d",
3203               rr->resrec.name->c, DNSTypeName(rr->resrec.rrtype), DomainNameLength(rr->resrec.name), zoneSize, rdlength);
3204        return DomainNameLength(rr->resrec.name) - zoneSize + 2 + 10 + 2 + 10 + rdlength;
3205    }
3206    else
3207    {
3208        return DomainNameLength(rr->resrec.name) - zoneSize + 2 + 10 + rdlength;
3209    }
3210}
3211
3212mDNSlocal AuthRecord *MarkRRForSending(mDNS *const m)
3213{
3214    AuthRecord *rr;
3215    AuthRecord *firstRR = mDNSNULL;
3216
3217    // Look for records that needs to be sent in the next two seconds (MERGE_DELAY_TIME is set to 1 second).
3218    // The logic is as follows.
3219    //
3220    // 1. Record 1 finishes getting zone data and its registration gets delayed by 1 second
3221    // 2. Record 2 comes 0.1 second later, finishes getting its zone data and its registration is also delayed by
3222    //    1 second which is now scheduled at 1.1 second
3223    //
3224    // By looking for 1 second into the future (m->timenow + MERGE_DELAY_TIME below does that) we have merged both
3225    // of the above records. Note that we can't look for records too much into the future as this will affect the
3226    // retry logic. The first retry is scheduled at 3 seconds. Hence, we should always look smaller than that.
3227    // Anything more than one second will affect the first retry to happen sooner.
3228    //
3229    // Note: As a side effect of looking one second into the future to facilitate merging, the retries happen
3230    // one second sooner.
3231    for (rr = m->ResourceRecords; rr; rr = rr->next)
3232    {
3233        if (!firstRR)
3234        {
3235            if (!IsRecordMergeable(m, rr, m->timenow + MERGE_DELAY_TIME)) continue;
3236            firstRR = rr;
3237        }
3238        else if (!AreRecordsMergeable(m, firstRR, rr, m->timenow + MERGE_DELAY_TIME)) continue;
3239
3240        if (rr->SendRNow) LogMsg("MarkRRForSending: Resourcerecord %s already marked for sending", ARDisplayString(m, rr));
3241        rr->SendRNow = uDNSInterfaceMark;
3242    }
3243
3244    // We parsed through all records and found something to send. The services/records might
3245    // get registered at different times but we want the refreshes to be all merged and sent
3246    // as one update. Hence, we accelerate some of the records so that they will sync up in
3247    // the future. Look at the records excluding the ones that we have already sent in the
3248    // previous pass. If it half way through its scheduled refresh/retransmit, merge them
3249    // into this packet.
3250    //
3251    // Note that we only look at Registered/Refresh state to keep it simple. As we don't know
3252    // whether the current update will fit into one or more packets, merging a resource record
3253    // (which is in a different state) that has been scheduled for retransmit would trigger
3254    // sending more packets.
3255    if (firstRR)
3256    {
3257        int acc = 0;
3258        for (rr = m->ResourceRecords; rr; rr = rr->next)
3259        {
3260            if ((rr->state != regState_Registered && rr->state != regState_Refresh) ||
3261                (rr->SendRNow == uDNSInterfaceMark) ||
3262                (!AreRecordsMergeable(m, firstRR, rr, m->timenow + rr->ThisAPInterval/2)))
3263                continue;
3264            rr->SendRNow = uDNSInterfaceMark;
3265            acc++;
3266        }
3267        if (acc) LogInfo("MarkRRForSending: Accelereated %d records", acc);
3268    }
3269    return firstRR;
3270}
3271
3272mDNSlocal mDNSBool SendGroupUpdates(mDNS *const m)
3273{
3274    mDNSOpaque16 msgid;
3275    mDNSs32 spaceleft = 0;
3276    mDNSs32 zoneSize, rrSize;
3277    mDNSu8 *oldnext; // for debugging
3278    mDNSu8 *next = m->omsg.data;
3279    AuthRecord *rr;
3280    AuthRecord *anchorRR = mDNSNULL;
3281    int nrecords = 0;
3282    AuthRecord *startRR = m->ResourceRecords;
3283    mDNSu8 *limit = mDNSNULL;
3284    DomainAuthInfo *AuthInfo = mDNSNULL;
3285    mDNSBool sentallRecords = mDNStrue;
3286
3287
3288    // We try to fit as many ResourceRecords as possible in AbsoluteNormal/MaxDNSMessageData. Before we start
3289    // putting in resource records, we need to reserve space for a few things. Every group/packet should
3290    // have the following.
3291    //
3292    // 1) Needs space for the Zone information (which needs to be at the beginning)
3293    // 2) Additional section MUST have space for lease option, HINFO and TSIG option (which needs to
3294    //    to be at the end)
3295    //
3296    // In future we need to reserve space for the pre-requisites which also goes at the beginning.
3297    // To accomodate pre-requisites in the future, first we walk the whole list marking records
3298    // that can be sent in this packet and computing the space needed for these records.
3299    // For TXT and SRV records, we delete the previous record if any by sending the same
3300    // resource record with ANY RDATA and zero rdlen. Hence, we need to have space for both of them.
3301
3302    while (startRR)
3303    {
3304        AuthInfo = mDNSNULL;
3305        anchorRR = mDNSNULL;
3306        nrecords = 0;
3307        zoneSize = 0;
3308        for (rr = startRR; rr; rr = rr->next)
3309        {
3310            if (rr->SendRNow != uDNSInterfaceMark) continue;
3311
3312            rr->SendRNow = mDNSNULL;
3313
3314            if (!anchorRR)
3315            {
3316                AuthInfo = GetAuthInfoForName_internal(m, rr->zone);
3317
3318                // Though we allow single record registrations for UDP to be AbsoluteMaxDNSMessageData (See
3319                // SendRecordRegistration) to handle large TXT records, to avoid fragmentation we limit UDP
3320                // message to NormalMaxDNSMessageData
3321                if (AuthInfo && AuthInfo->AutoTunnel) spaceleft = AbsoluteMaxDNSMessageData;
3322                else spaceleft = NormalMaxDNSMessageData;
3323
3324                next = m->omsg.data;
3325                spaceleft -= RRAdditionalSize(m, AuthInfo);
3326                if (spaceleft <= 0)
3327                {
3328                    LogMsg("SendGroupUpdates: ERROR!!: spaceleft is zero at the beginning");
3329                    RRMergeFailure(m);
3330                    return mDNSfalse;
3331                }
3332                limit = next + spaceleft;
3333
3334                // Build the initial part of message before putting in the other records
3335                msgid = mDNS_NewMessageID(m);
3336                InitializeDNSMessage(&m->omsg.h, msgid, UpdateReqFlags);
3337
3338                // We need zone information at the beginning of the packet. Length: ZNAME, ZTYPE(2), ZCLASS(2)
3339                // zone has to be non-NULL for a record to be mergeable, hence it is safe to set/ examine zone
3340                //without checking for NULL.
3341                zoneSize = DomainNameLength(rr->zone) + 4;
3342                spaceleft -= zoneSize;
3343                if (spaceleft <= 0)
3344                {
3345                    LogMsg("SendGroupUpdates: ERROR no space for zone information, disabling merge");
3346                    RRMergeFailure(m);
3347                    return mDNSfalse;
3348                }
3349                next = putZone(&m->omsg, next, limit, rr->zone, mDNSOpaque16fromIntVal(rr->resrec.rrclass));
3350                if (!next)
3351                {
3352                    LogMsg("SendGroupUpdates: ERROR! Cannot put zone, disabling merge");
3353                    RRMergeFailure(m);
3354                    return mDNSfalse;
3355                }
3356                anchorRR = rr;
3357            }
3358
3359            rrSize = RREstimatedSize(rr, zoneSize - 4);
3360
3361            if ((spaceleft - rrSize) < 0)
3362            {
3363                // If we can't fit even a single message, skip it, it will be sent separately
3364                // in CheckRecordUpdates
3365                if (!nrecords)
3366                {
3367                    LogInfo("SendGroupUpdates: Skipping message %s, spaceleft %d, rrSize %d", ARDisplayString(m, rr), spaceleft, rrSize);
3368                    // Mark this as not sent so that the caller knows about it
3369                    rr->SendRNow = uDNSInterfaceMark;
3370                    // We need to remove the merge delay so that we can send it immediately
3371                    rr->ThisAPInterval = INIT_RECORD_REG_INTERVAL;
3372                    rr->LastAPTime = m->timenow - INIT_RECORD_REG_INTERVAL;
3373                    rr = rr->next;
3374                    anchorRR = mDNSNULL;
3375                    sentallRecords = mDNSfalse;
3376                }
3377                else
3378                {
3379                    LogInfo("SendGroupUpdates:1: Parsed %d records and sending using %s, spaceleft %d, rrSize %d", nrecords, ARDisplayString(m, anchorRR), spaceleft, rrSize);
3380                    SendGroupRRMessage(m, anchorRR, next, AuthInfo);
3381                }
3382                break;      // breaks out of for loop
3383            }
3384            spaceleft -= rrSize;
3385            oldnext = next;
3386            LogInfo("SendGroupUpdates: Building a message with resource record %s, next %p, state %d, ttl %d", ARDisplayString(m, rr), next, rr->state, rr->resrec.rroriginalttl);
3387            if (!(next = BuildUpdateMessage(m, next, rr, limit)))
3388            {
3389                // We calculated the space and if we can't fit in, we had some bug in the calculation,
3390                // disable merge completely.
3391                LogMsg("SendGroupUpdates: ptr NULL while building message with %s", ARDisplayString(m, rr));
3392                RRMergeFailure(m);
3393                return mDNSfalse;
3394            }
3395            // If our estimate was higher, adjust to the actual size
3396            if ((next - oldnext) > rrSize)
3397                LogMsg("SendGroupUpdates: ERROR!! Record size estimation is wrong for %s, Estimate %d, Actual %d, state %d", ARDisplayString(m, rr), rrSize, next - oldnext, rr->state);
3398            else { spaceleft += rrSize; spaceleft -= (next - oldnext); }
3399
3400            nrecords++;
3401            // We could have sent an update earlier with this "rr" as anchorRR for which we never got a response.
3402            // To preserve ordering, we blow away the previous connection before sending this.
3403            if (rr->tcp) { DisposeTCPConn(rr->tcp); rr->tcp = mDNSNULL;}
3404            rr->updateid = msgid;
3405
3406            // By setting the retry time interval here, we will not be looking at these records
3407            // again when we return to CheckGroupRecordUpdates.
3408            SetRecordRetry(m, rr, 0);
3409        }
3410        // Either we have parsed all the records or stopped at "rr" above due to lack of space
3411        startRR = rr;
3412    }
3413
3414    if (anchorRR)
3415    {
3416        LogInfo("SendGroupUpdates: Parsed %d records and sending using %s", nrecords, ARDisplayString(m, anchorRR));
3417        SendGroupRRMessage(m, anchorRR, next, AuthInfo);
3418    }
3419    return sentallRecords;
3420}
3421
3422// Merge the record registrations and send them as a group only if they
3423// have same DomainAuthInfo and hence the same key to put the TSIG
3424mDNSlocal void CheckGroupRecordUpdates(mDNS *const m)
3425{
3426    AuthRecord *rr, *nextRR;
3427    // Keep sending as long as there is at least one record to be sent
3428    while (MarkRRForSending(m))
3429    {
3430        if (!SendGroupUpdates(m))
3431        {
3432            // if everything that was marked was not sent, send them out individually
3433            for (rr = m->ResourceRecords; rr; rr = nextRR)
3434            {
3435                // SendRecordRegistrtion might delete the rr from list, hence
3436                // dereference nextRR before calling the function
3437                nextRR = rr->next;
3438                if (rr->SendRNow == uDNSInterfaceMark)
3439                {
3440                    // Any records marked for sending should be eligible to be sent out
3441                    // immediately. Just being cautious
3442                    if (rr->LastAPTime + rr->ThisAPInterval - m->timenow > 0)
3443                    { LogMsg("CheckGroupRecordUpdates: ERROR!! Resourcerecord %s not ready", ARDisplayString(m, rr)); continue; }
3444                    rr->SendRNow = mDNSNULL;
3445                    SendRecordRegistration(m, rr);
3446                }
3447            }
3448        }
3449    }
3450
3451    debugf("CheckGroupRecordUpdates: No work, returning");
3452    return;
3453}
3454
3455mDNSlocal void hndlSRVChanged(mDNS *const m, AuthRecord *rr)
3456{
3457    // Reevaluate the target always as NAT/Target could have changed while
3458    // we were registering/deeregistering
3459    domainname *dt;
3460    const domainname *target = GetServiceTarget(m, rr);
3461    if (!target || target->c[0] == 0)
3462    {
3463        // we don't have a target, if we just derregistered, then we don't have to do anything
3464        if (rr->state == regState_DeregPending)
3465        {
3466            LogInfo("hndlSRVChanged: SRVChanged, No Target, SRV Deregistered for %##s, state %d", rr->resrec.name->c,
3467                    rr->state);
3468            rr->SRVChanged = mDNSfalse;
3469            dt = GetRRDomainNameTarget(&rr->resrec);
3470            if (dt) dt->c[0] = 0;
3471            rr->state = regState_NoTarget;  // Wait for the next target change
3472            rr->resrec.rdlength = rr->resrec.rdestimate = 0;
3473            return;
3474        }
3475
3476        // we don't have a target, if we just registered, we need to deregister
3477        if (rr->state == regState_Pending)
3478        {
3479            LogInfo("hndlSRVChanged: SRVChanged, No Target, Deregistering again %##s, state %d", rr->resrec.name->c, rr->state);
3480            rr->ThisAPInterval = INIT_RECORD_REG_INTERVAL;
3481            rr->LastAPTime = m->timenow - INIT_RECORD_REG_INTERVAL;
3482            rr->state = regState_DeregPending;
3483            return;
3484        }
3485        LogInfo("hndlSRVChanged: Not in DeregPending or RegPending state %##s, state %d", rr->resrec.name->c, rr->state);
3486    }
3487    else
3488    {
3489        // If we were in registered state and SRV changed to NULL, we deregister and come back here
3490        // if we have a target, we need to register again.
3491        //
3492        // if we just registered check to see if it is same. If it is different just re-register the
3493        // SRV and its assoicated records
3494        //
3495        // UpdateOneSRVRecord takes care of re-registering all service records
3496        if ((rr->state == regState_DeregPending) ||
3497            (rr->state == regState_Pending && !SameDomainName(target, &rr->resrec.rdata->u.srv.target)))
3498        {
3499            dt = GetRRDomainNameTarget(&rr->resrec);
3500            if (dt) dt->c[0] = 0;
3501            rr->state = regState_NoTarget;  // NoTarget will allow us to pick up new target OR nat traversal state
3502            rr->resrec.rdlength = rr->resrec.rdestimate = 0;
3503            LogInfo("hndlSRVChanged: SRVChanged, Valid Target %##s, Registering all records for %##s, state %d",
3504                    target->c, rr->resrec.name->c, rr->state);
3505            rr->SRVChanged = mDNSfalse;
3506            UpdateOneSRVRecord(m, rr);
3507            return;
3508        }
3509        // Target did not change while this record was registering. Hence, we go to
3510        // Registered state - the state we started from.
3511        if (rr->state == regState_Pending) rr->state = regState_Registered;
3512    }
3513
3514    rr->SRVChanged = mDNSfalse;
3515}
3516
3517// Called with lock held
3518mDNSlocal void hndlRecordUpdateReply(mDNS *m, AuthRecord *rr, mStatus err, mDNSu32 random)
3519{
3520    mDNSBool InvokeCallback = mDNStrue;
3521    mDNSIPPort UpdatePort = zeroIPPort;
3522
3523    mDNS_CheckLock(m);
3524
3525    LogInfo("hndlRecordUpdateReply: err %d ID %d state %d %s(%p)", err, mDNSVal16(rr->updateid), rr->state, ARDisplayString(m, rr), rr);
3526
3527    rr->updateError = err;
3528#if APPLE_OSX_mDNSResponder
3529    if (err == mStatus_BadSig || err == mStatus_BadKey || err == mStatus_BadTime) UpdateAutoTunnelDomainStatuses(m);
3530#endif
3531
3532    SetRecordRetry(m, rr, random);
3533
3534    rr->updateid = zeroID;  // Make sure that this is not considered as part of a group anymore
3535    // Later when need to send an update, we will get the zone data again. Thus we avoid
3536    // using stale information.
3537    //
3538    // Note: By clearing out the zone info here, it also helps better merging of records
3539    // in some cases. For example, when we get out regState_NoTarget state e.g., move out
3540    // of Double NAT, we want all the records to be in one update. Some BTMM records like
3541    // _autotunnel6 and host records are registered/deregistered when NAT state changes.
3542    // As they are re-registered the zone information is cleared out. To merge with other
3543    // records that might be possibly going out, clearing out the information here helps
3544    // as all of them try to get the zone data.
3545    if (rr->nta)
3546    {
3547        // We always expect the question to be stopped when we get a valid response from the server.
3548        // If the zone info tries to change during this time, updateid would be different and hence
3549        // this response should not have been accepted.
3550        if (rr->nta->question.ThisQInterval != -1)
3551            LogMsg("hndlRecordUpdateReply: ResourceRecord %s, zone info question %##s (%s) interval %d not -1",
3552                   ARDisplayString(m, rr), rr->nta->question.qname.c, DNSTypeName(rr->nta->question.qtype), rr->nta->question.ThisQInterval);
3553        UpdatePort = rr->nta->Port;
3554        CancelGetZoneData(m, rr->nta);
3555        rr->nta = mDNSNULL;
3556    }
3557
3558    // If we are deregistering the record, then complete the deregistration. Ignore any NAT/SRV change
3559    // that could have happened during that time.
3560    if (rr->resrec.RecordType == kDNSRecordTypeDeregistering && rr->state == regState_DeregPending)
3561    {
3562        debugf("hndlRecordUpdateReply: Received reply for deregister record %##s type %d", rr->resrec.name->c, rr->resrec.rrtype);
3563        if (err) LogMsg("ERROR: Deregistration of record %##s type %d failed with error %d",
3564                        rr->resrec.name->c, rr->resrec.rrtype, err);
3565        rr->state = regState_Unregistered;
3566        CompleteDeregistration(m, rr);
3567        return;
3568    }
3569
3570    // We are returning early without updating the state. When we come back from sleep we will re-register after
3571    // re-initializing all the state as though it is a first registration. If the record can't be registered e.g.,
3572    // no target, it will be deregistered. Hence, the updating to the right state should not matter when going
3573    // to sleep.
3574    if (m->SleepState)
3575    {
3576        // Need to set it to NoTarget state so that RecordReadyForSleep knows that
3577        // we are done
3578        if (rr->resrec.rrtype == kDNSType_SRV && rr->state == regState_DeregPending)
3579            rr->state = regState_NoTarget;
3580        return;
3581    }
3582
3583    if (rr->state == regState_UpdatePending)
3584    {
3585        if (err) LogMsg("Update record failed for %##s (err %d)", rr->resrec.name->c, err);
3586        rr->state = regState_Registered;
3587        // deallocate old RData
3588        if (rr->UpdateCallback) rr->UpdateCallback(m, rr, rr->OrigRData, rr->OrigRDLen);
3589        SetNewRData(&rr->resrec, rr->InFlightRData, rr->InFlightRDLen);
3590        rr->OrigRData = mDNSNULL;
3591        rr->InFlightRData = mDNSNULL;
3592    }
3593
3594    if (rr->SRVChanged)
3595    {
3596        if (rr->resrec.rrtype == kDNSType_SRV)
3597            hndlSRVChanged(m, rr);
3598        else
3599        {
3600            LogInfo("hndlRecordUpdateReply: Deregistered %##s (%s), state %d", rr->resrec.name->c, DNSTypeName(rr->resrec.rrtype), rr->state);
3601            rr->SRVChanged = mDNSfalse;
3602            if (rr->state != regState_DeregPending) LogMsg("hndlRecordUpdateReply: ResourceRecord %s not in DeregPending state %d", ARDisplayString(m, rr), rr->state);
3603            rr->state = regState_NoTarget;  // Wait for the next target change
3604        }
3605        return;
3606    }
3607
3608    if (rr->state == regState_Pending || rr->state == regState_Refresh)
3609    {
3610        if (!err)
3611        {
3612            if (rr->state == regState_Refresh) InvokeCallback = mDNSfalse;
3613            rr->state = regState_Registered;
3614        }
3615        else
3616        {
3617            // Retry without lease only for non-Private domains
3618            LogMsg("hndlRecordUpdateReply: Registration of record %##s type %d failed with error %d", rr->resrec.name->c, rr->resrec.rrtype, err);
3619            if (!rr->Private && rr->uselease && err == mStatus_UnknownErr && mDNSSameIPPort(UpdatePort, UnicastDNSPort))
3620            {
3621                LogMsg("hndlRecordUpdateReply: Will retry update of record %##s without lease option", rr->resrec.name->c);
3622                rr->uselease = mDNSfalse;
3623                rr->ThisAPInterval = INIT_RECORD_REG_INTERVAL;
3624                rr->LastAPTime = m->timenow - INIT_RECORD_REG_INTERVAL;
3625                SetNextuDNSEvent(m, rr);
3626                return;
3627            }
3628            // Communicate the error to the application in the callback below
3629        }
3630    }
3631
3632    if (rr->QueuedRData && rr->state == regState_Registered)
3633    {
3634        rr->state = regState_UpdatePending;
3635        rr->InFlightRData = rr->QueuedRData;
3636        rr->InFlightRDLen = rr->QueuedRDLen;
3637        rr->OrigRData = rr->resrec.rdata;
3638        rr->OrigRDLen = rr->resrec.rdlength;
3639        rr->QueuedRData = mDNSNULL;
3640        rr->ThisAPInterval = INIT_RECORD_REG_INTERVAL;
3641        rr->LastAPTime = m->timenow - INIT_RECORD_REG_INTERVAL;
3642        SetNextuDNSEvent(m, rr);
3643        return;
3644    }
3645
3646    // Don't invoke the callback on error as this may not be useful to the client.
3647    // The client may potentially delete the resource record on error which we normally
3648    // delete during deregistration
3649    if (!err && InvokeCallback && rr->RecordCallback)
3650    {
3651        LogInfo("hndlRecordUpdateReply: Calling record callback on %##s", rr->resrec.name->c);
3652        mDNS_DropLockBeforeCallback();
3653        rr->RecordCallback(m, rr, err);
3654        mDNS_ReclaimLockAfterCallback();
3655    }
3656    // CAUTION: MUST NOT do anything more with rr after calling rr->Callback(), because the client's callback function
3657    // is allowed to do anything, including starting/stopping queries, registering/deregistering records, etc.
3658}
3659
3660mDNSlocal void uDNS_ReceiveNATPMPPacket(mDNS *m, const mDNSInterfaceID InterfaceID, mDNSu8 *pkt, mDNSu16 len)
3661{
3662    NATTraversalInfo *ptr;
3663    NATAddrReply     *AddrReply    = (NATAddrReply    *)pkt;
3664    NATPortMapReply  *PortMapReply = (NATPortMapReply *)pkt;
3665    mDNSu32 nat_elapsed, our_elapsed;
3666
3667    // Minimum NAT-PMP packet is vers (1) opcode (1) + err (2) = 4 bytes
3668    if (len < 4) { LogMsg("NAT-PMP message too short (%d bytes)", len); return; }
3669
3670    // Read multi-byte error value (field is identical in a NATPortMapReply)
3671    AddrReply->err = (mDNSu16) ((mDNSu16)pkt[2] << 8 | pkt[3]);
3672
3673    if (AddrReply->err == NATErr_Vers)
3674    {
3675        NATTraversalInfo *n;
3676        LogInfo("NAT-PMP version unsupported message received");
3677        for (n = m->NATTraversals; n; n=n->next)
3678        {
3679            // Send a NAT-PMP request for this operation as needed
3680            // and update the state variables
3681            uDNS_SendNATMsg(m, n, mDNSfalse, mDNSfalse);
3682        }
3683
3684        m->NextScheduledNATOp = m->timenow;
3685
3686        return;
3687    }
3688
3689    // The minimum reasonable NAT-PMP packet length is vers (1) + opcode (1) + err (2) + upseconds (4) = 8 bytes
3690    // If it's not at least this long, bail before we byte-swap the upseconds field & overrun our buffer.
3691    // The retry timer will ensure we converge to correctness.
3692    if (len < 8)
3693    {
3694        LogMsg("NAT-PMP message too short (%d bytes) 0x%X 0x%X", len, AddrReply->opcode, AddrReply->err);
3695        return;
3696    }
3697
3698    // Read multi-byte upseconds value (field is identical in a NATPortMapReply)
3699    AddrReply->upseconds = (mDNSs32) ((mDNSs32)pkt[4] << 24 | (mDNSs32)pkt[5] << 16 | (mDNSs32)pkt[6] << 8 | pkt[7]);
3700
3701    nat_elapsed = AddrReply->upseconds - m->LastNATupseconds;
3702    our_elapsed = (m->timenow - m->LastNATReplyLocalTime) / mDNSPlatformOneSecond;
3703    debugf("uDNS_ReceiveNATPMPPacket %X upseconds %u nat_elapsed %d our_elapsed %d", AddrReply->opcode, AddrReply->upseconds, nat_elapsed, our_elapsed);
3704
3705    // We compute a conservative estimate of how much the NAT gateways's clock should have advanced
3706    // 1. We subtract 12.5% from our own measured elapsed time, to allow for NAT gateways that have an inacurate clock that runs slowly
3707    // 2. We add a two-second safety margin to allow for rounding errors: e.g.
3708    //    -- if NAT gateway sends a packet at t=2.000 seconds, then one at t=7.999, that's approximately 6 real seconds,
3709    //       but based on the values in the packet (2,7) the apparent difference according to the packet is only 5 seconds
3710    //    -- if we're slow handling packets and/or we have coarse clock granularity,
3711    //       we could receive the t=2 packet at our t=1.999 seconds, which we round down to 1
3712    //       and the t=7.999 packet at our t=8.000 seconds, which we record as 8,
3713    //       giving an apparent local time difference of 7 seconds
3714    //    The two-second safety margin coves this possible calculation discrepancy
3715    if (AddrReply->upseconds < m->LastNATupseconds || nat_elapsed + 2 < our_elapsed - our_elapsed/8)
3716    { LogMsg("NAT-PMP epoch time check failed: assuming NAT gateway %#a rebooted", &m->Router); RecreateNATMappings(m, 0); }
3717
3718    m->LastNATupseconds      = AddrReply->upseconds;
3719    m->LastNATReplyLocalTime = m->timenow;
3720#ifdef _LEGACY_NAT_TRAVERSAL_
3721    LNT_ClearState(m);
3722#endif // _LEGACY_NAT_TRAVERSAL_
3723
3724    if (AddrReply->opcode == NATOp_AddrResponse)
3725    {
3726#if APPLE_OSX_mDNSResponder
3727        LogInfo("uDNS_ReceiveNATPMPPacket: AddressRequest %s error %d", AddrReply->err ? "failure" : "success", AddrReply->err);
3728#endif
3729        if (!AddrReply->err && len < sizeof(NATAddrReply)) { LogMsg("NAT-PMP AddrResponse message too short (%d bytes)", len); return; }
3730        natTraversalHandleAddressReply(m, AddrReply->err, AddrReply->ExtAddr);
3731    }
3732    else if (AddrReply->opcode == NATOp_MapUDPResponse || AddrReply->opcode == NATOp_MapTCPResponse)
3733    {
3734        mDNSu8 Protocol = AddrReply->opcode & 0x7F;
3735#if APPLE_OSX_mDNSResponder
3736        LogInfo("uDNS_ReceiveNATPMPPacket: PortMapRequest %s %s - error %d",
3737            PortMapReply->err ? "failure" : "success", (AddrReply->opcode == NATOp_MapUDPResponse) ? "UDP" : "TCP", PortMapReply->err);
3738#endif
3739        if (!PortMapReply->err)
3740        {
3741            if (len < sizeof(NATPortMapReply)) { LogMsg("NAT-PMP PortMapReply message too short (%d bytes)", len); return; }
3742            PortMapReply->NATRep_lease = (mDNSu32) ((mDNSu32)pkt[12] << 24 | (mDNSu32)pkt[13] << 16 | (mDNSu32)pkt[14] << 8 | pkt[15]);
3743        }
3744
3745        // Since some NAT-PMP server implementations don't return the requested internal port in
3746        // the reply, we can't associate this reply with a particular NATTraversalInfo structure.
3747        // We globally keep track of the most recent error code for mappings.
3748        m->LastNATMapResultCode = PortMapReply->err;
3749
3750        for (ptr = m->NATTraversals; ptr; ptr=ptr->next)
3751            if (ptr->Protocol == Protocol && mDNSSameIPPort(ptr->IntPort, PortMapReply->intport))
3752                natTraversalHandlePortMapReply(m, ptr, InterfaceID, PortMapReply->err, PortMapReply->extport, PortMapReply->NATRep_lease, NATTProtocolNATPMP);
3753    }
3754    else { LogMsg("Received NAT-PMP response with unknown opcode 0x%X", AddrReply->opcode); return; }
3755
3756    // Don't need an SSDP socket if we get a NAT-PMP packet
3757    if (m->SSDPSocket) { debugf("uDNS_ReceiveNATPMPPacket destroying SSDPSocket %p", &m->SSDPSocket); mDNSPlatformUDPClose(m->SSDPSocket); m->SSDPSocket = mDNSNULL; }
3758}
3759
3760mDNSlocal void uDNS_ReceivePCPPacket(mDNS *m, const mDNSInterfaceID InterfaceID, mDNSu8 *pkt, mDNSu16 len)
3761{
3762    NATTraversalInfo *ptr;
3763    PCPMapReply *reply = (PCPMapReply*)pkt;
3764    mDNSu32 client_delta, server_delta;
3765    mDNSBool checkEpochValidity = m->LastNATupseconds != 0;
3766    mDNSu8 strippedOpCode;
3767    mDNSv4Addr mappedAddress = zerov4Addr;
3768    mDNSu8 protocol = 0;
3769    mDNSIPPort intport = zeroIPPort;
3770    mDNSIPPort extport = zeroIPPort;
3771
3772    // Minimum PCP packet is 24 bytes
3773    if (len < 24)
3774    {
3775        LogMsg("uDNS_ReceivePCPPacket: message too short (%d bytes)", len);
3776        return;
3777    }
3778
3779    strippedOpCode = reply->opCode & 0x7f;
3780
3781    if ((reply->opCode & 0x80) == 0x00 || (strippedOpCode != PCPOp_Announce && strippedOpCode != PCPOp_Map))
3782    {
3783        LogMsg("uDNS_ReceivePCPPacket: unhandled opCode %u", reply->opCode);
3784        return;
3785    }
3786
3787    // Read multi-byte values
3788    reply->lifetime = (mDNSs32)((mDNSs32)pkt[4] << 24 | (mDNSs32)pkt[5] << 16 | (mDNSs32)pkt[ 6] << 8 | pkt[ 7]);
3789    reply->epoch    = (mDNSs32)((mDNSs32)pkt[8] << 24 | (mDNSs32)pkt[9] << 16 | (mDNSs32)pkt[10] << 8 | pkt[11]);
3790
3791    client_delta = (m->timenow - m->LastNATReplyLocalTime) / mDNSPlatformOneSecond;
3792    server_delta = reply->epoch - m->LastNATupseconds;
3793    debugf("uDNS_ReceivePCPPacket: %X %X upseconds %u client_delta %d server_delta %d", reply->opCode, reply->result, reply->epoch, client_delta, server_delta);
3794
3795    // If seconds since the epoch is 0, use 1 so we'll check epoch validity next time
3796    m->LastNATupseconds      = reply->epoch ? reply->epoch : 1;
3797    m->LastNATReplyLocalTime = m->timenow;
3798
3799#ifdef _LEGACY_NAT_TRAVERSAL_
3800    LNT_ClearState(m);
3801#endif // _LEGACY_NAT_TRAVERSAL_
3802
3803    // Don't need an SSDP socket if we get a PCP packet
3804    if (m->SSDPSocket) { debugf("uDNS_ReceivePCPPacket: destroying SSDPSocket %p", &m->SSDPSocket); mDNSPlatformUDPClose(m->SSDPSocket); m->SSDPSocket = mDNSNULL; }
3805
3806    if (checkEpochValidity && (client_delta + 2 < server_delta - server_delta / 16 || server_delta + 2 < client_delta - client_delta / 16))
3807    {
3808        // If this is an ANNOUNCE packet, wait a random interval up to 5 seconds
3809        // otherwise, refresh immediately
3810        mDNSu32 waitTicks = strippedOpCode ? 0 : mDNSRandom(PCP_WAITSECS_AFTER_EPOCH_INVALID * mDNSPlatformOneSecond);
3811        LogMsg("uDNS_ReceivePCPPacket: Epoch invalid, %#a likely rebooted, waiting %u ticks", &m->Router, waitTicks);
3812        RecreateNATMappings(m, waitTicks);
3813        // we can ignore the rest of this packet, as new requests are about to go out
3814        return;
3815    }
3816
3817    if (strippedOpCode == PCPOp_Announce)
3818        return;
3819
3820    // We globally keep track of the most recent error code for mappings.
3821    // This seems bad to do with PCP, but best not change it now.
3822    m->LastNATMapResultCode = reply->result;
3823
3824    if (!reply->result)
3825    {
3826        if (len < sizeof(PCPMapReply))
3827        {
3828            LogMsg("uDNS_ReceivePCPPacket: mapping response too short (%d bytes)", len);
3829            return;
3830        }
3831
3832        // Check the nonce
3833        if (reply->nonce[0] != m->PCPNonce[0] || reply->nonce[1] != m->PCPNonce[1] || reply->nonce[2] != m->PCPNonce[2])
3834        {
3835            LogMsg("uDNS_ReceivePCPPacket: invalid nonce, ignoring. received { %x %x %x } expected { %x %x %x }",
3836                   reply->nonce[0], reply->nonce[1], reply->nonce[2],
3837                    m->PCPNonce[0],  m->PCPNonce[1],  m->PCPNonce[2]);
3838            return;
3839        }
3840
3841        // Get the values
3842        protocol = reply->protocol;
3843        intport = reply->intPort;
3844        extport = reply->extPort;
3845
3846        // Get the external address, which should be mapped, since we only support IPv4
3847        if (!mDNSAddrIPv4FromMappedIPv6(&reply->extAddress, &mappedAddress))
3848        {
3849            LogMsg("uDNS_ReceivePCPPacket: unexpected external address: %.16a", &reply->extAddress);
3850            reply->result = NATErr_NetFail;
3851            // fall through to report the error
3852        }
3853        else if (mDNSIPv4AddressIsZero(mappedAddress))
3854        {
3855            // If this is the deletion case, we will have sent the zero IPv4-mapped address
3856            // in our request, and the server should reflect it in the response, so we
3857            // should not log about receiving a zero address. And in this case, we no
3858            // longer have a NATTraversal to report errors back to, so it's ok to set the
3859            // result here.
3860            // In other cases, a zero address is an error, and we will have a NATTraversal
3861            // to report back to, so set an error and fall through to report it.
3862            // CheckNATMappings will log the error.
3863            reply->result = NATErr_NetFail;
3864        }
3865    }
3866    else
3867    {
3868        LogInfo("uDNS_ReceivePCPPacket: error received from server. opcode %X result %X lifetime %X epoch %X",
3869                reply->opCode, reply->result, reply->lifetime, reply->epoch);
3870
3871        // If the packet is long enough, get the protocol & intport for matching to report
3872        // the error
3873        if (len >= sizeof(PCPMapReply))
3874        {
3875            protocol = reply->protocol;
3876            intport = reply->intPort;
3877        }
3878    }
3879
3880    for (ptr = m->NATTraversals; ptr; ptr=ptr->next)
3881    {
3882        mDNSu8 ptrProtocol = ((ptr->Protocol & NATOp_MapTCP) == NATOp_MapTCP ? PCPProto_TCP : PCPProto_UDP);
3883        if ((protocol == ptrProtocol && mDNSSameIPPort(ptr->IntPort, intport)) ||
3884            (!ptr->Protocol && protocol == PCPProto_TCP && mDNSSameIPPort(DiscardPort, intport)))
3885        {
3886            natTraversalHandlePortMapReplyWithAddress(m, ptr, InterfaceID, reply->result ? NATErr_NetFail : NATErr_None, mappedAddress, extport, reply->lifetime, NATTProtocolPCP);
3887        }
3888    }
3889}
3890
3891mDNSexport void uDNS_ReceiveNATPacket(mDNS *m, const mDNSInterfaceID InterfaceID, mDNSu8 *pkt, mDNSu16 len)
3892{
3893    if (len == 0)
3894        LogMsg("uDNS_ReceiveNATPacket: zero length packet");
3895    else if (pkt[0] == PCP_VERS)
3896        uDNS_ReceivePCPPacket(m, InterfaceID, pkt, len);
3897    else if (pkt[0] == NATMAP_VERS)
3898        uDNS_ReceiveNATPMPPacket(m, InterfaceID, pkt, len);
3899    else
3900        LogMsg("uDNS_ReceiveNATPacket: packet with version %u (expected %u or %u)", pkt[0], PCP_VERS, NATMAP_VERS);
3901}
3902
3903// Called from mDNSCoreReceive with the lock held
3904mDNSexport void uDNS_ReceiveMsg(mDNS *const m, DNSMessage *const msg, const mDNSu8 *const end, const mDNSAddr *const srcaddr, const mDNSIPPort srcport)
3905{
3906    DNSQuestion *qptr;
3907    mStatus err = mStatus_NoError;
3908
3909    mDNSu8 StdR    = kDNSFlag0_QR_Response | kDNSFlag0_OP_StdQuery;
3910    mDNSu8 UpdateR = kDNSFlag0_QR_Response | kDNSFlag0_OP_Update;
3911    mDNSu8 QR_OP   = (mDNSu8)(msg->h.flags.b[0] & kDNSFlag0_QROP_Mask);
3912    mDNSu8 rcode   = (mDNSu8)(msg->h.flags.b[1] & kDNSFlag1_RC_Mask);
3913
3914    (void)srcport; // Unused
3915
3916    debugf("uDNS_ReceiveMsg from %#-15a with "
3917           "%2d Question%s %2d Answer%s %2d Authorit%s %2d Additional%s %d bytes",
3918           srcaddr,
3919           msg->h.numQuestions,   msg->h.numQuestions   == 1 ? ", "   : "s,",
3920           msg->h.numAnswers,     msg->h.numAnswers     == 1 ? ", "   : "s,",
3921           msg->h.numAuthorities, msg->h.numAuthorities == 1 ? "y,  " : "ies,",
3922           msg->h.numAdditionals, msg->h.numAdditionals == 1 ? ""     : "s", end - msg->data);
3923#if APPLE_OSX_mDNSResponder
3924    if (NumUnreachableDNSServers > 0)
3925        SymptomReporterDNSServerReachable(m, srcaddr);
3926#endif
3927
3928    if (QR_OP == StdR)
3929    {
3930        //if (srcaddr && recvLLQResponse(m, msg, end, srcaddr, srcport)) return;
3931        for (qptr = m->Questions; qptr; qptr = qptr->next)
3932            if (msg->h.flags.b[0] & kDNSFlag0_TC && mDNSSameOpaque16(qptr->TargetQID, msg->h.id) && m->timenow - qptr->LastQTime < RESPONSE_WINDOW)
3933            {
3934                if (!srcaddr) LogMsg("uDNS_ReceiveMsg: TCP DNS response had TC bit set: ignoring");
3935                else          uDNS_RestartQuestionAsTCP(m, qptr, srcaddr, srcport);
3936            }
3937    }
3938
3939    if (QR_OP == UpdateR)
3940    {
3941        mDNSu32 pktlease = 0;
3942        mDNSBool gotlease = GetPktLease(m, msg, end, &pktlease);
3943        mDNSu32 lease = gotlease ? pktlease : 60 * 60; // If lease option missing, assume one hour
3944        mDNSs32 expire = m->timenow + (mDNSs32)lease * mDNSPlatformOneSecond;
3945        mDNSu32 random = mDNSRandom((mDNSs32)lease * mDNSPlatformOneSecond/10);
3946
3947        //rcode = kDNSFlag1_RC_ServFail;    // Simulate server failure (rcode 2)
3948
3949        // Walk through all the records that matches the messageID. There could be multiple
3950        // records if we had sent them in a group
3951        if (m->CurrentRecord)
3952            LogMsg("uDNS_ReceiveMsg ERROR m->CurrentRecord already set %s", ARDisplayString(m, m->CurrentRecord));
3953        m->CurrentRecord = m->ResourceRecords;
3954        while (m->CurrentRecord)
3955        {
3956            AuthRecord *rptr = m->CurrentRecord;
3957            m->CurrentRecord = m->CurrentRecord->next;
3958            if (AuthRecord_uDNS(rptr) && mDNSSameOpaque16(rptr->updateid, msg->h.id))
3959            {
3960                err = checkUpdateResult(m, rptr->resrec.name, rcode, msg, end);
3961                if (!err && rptr->uselease && lease)
3962                    if (rptr->expire - expire >= 0 || rptr->state != regState_UpdatePending)
3963                    {
3964                        rptr->expire = expire;
3965                        rptr->refreshCount = 0;
3966                    }
3967                // We pass the random value to make sure that if we update multiple
3968                // records, they all get the same random value
3969                hndlRecordUpdateReply(m, rptr, err, random);
3970            }
3971        }
3972    }
3973    debugf("Received unexpected response: ID %d matches no active records", mDNSVal16(msg->h.id));
3974}
3975
3976// ***************************************************************************
3977#if COMPILER_LIKES_PRAGMA_MARK
3978#pragma mark - Query Routines
3979#endif
3980
3981mDNSexport void sendLLQRefresh(mDNS *m, DNSQuestion *q)
3982{
3983    mDNSu8 *end;
3984    LLQOptData llq;
3985    mDNSu8 *limit = m->omsg.data + AbsoluteMaxDNSMessageData;
3986
3987    if (q->ReqLease)
3988        if ((q->state == LLQ_Established && q->ntries >= kLLQ_MAX_TRIES) || q->expire - m->timenow < 0)
3989        {
3990            LogMsg("Unable to refresh LLQ %##s (%s) - will retry in %d seconds", q->qname.c, DNSTypeName(q->qtype), LLQ_POLL_INTERVAL / mDNSPlatformOneSecond);
3991            StartLLQPolling(m,q);
3992            return;
3993        }
3994
3995    llq.vers     = kLLQ_Vers;
3996    llq.llqOp    = kLLQOp_Refresh;
3997    llq.err      = q->tcp ? GetLLQEventPort(m, &q->servAddr) : LLQErr_NoError;  // If using TCP tell server what UDP port to send notifications to
3998    llq.id       = q->id;
3999    llq.llqlease = q->ReqLease;
4000
4001    InitializeDNSMessage(&m->omsg.h, q->TargetQID, uQueryFlags);
4002    end = putLLQ(&m->omsg, m->omsg.data, q, &llq);
4003    if (!end) { LogMsg("sendLLQRefresh: putLLQ failed %##s (%s)", q->qname.c, DNSTypeName(q->qtype)); return; }
4004
4005    // Note that we (conditionally) add HINFO and TSIG here, since the question might be going away,
4006    // so we may not be able to reference it (most importantly it's AuthInfo) when we actually send the message
4007    end = putHINFO(m, &m->omsg, end, q->AuthInfo, limit);
4008    if (!end) { LogMsg("sendLLQRefresh: putHINFO failed %##s (%s)", q->qname.c, DNSTypeName(q->qtype)); return; }
4009
4010    if (PrivateQuery(q))
4011    {
4012        DNSDigest_SignMessageHostByteOrder(&m->omsg, &end, q->AuthInfo);
4013        if (!end) { LogMsg("sendLLQRefresh: DNSDigest_SignMessage failed %##s (%s)", q->qname.c, DNSTypeName(q->qtype)); return; }
4014    }
4015
4016    if (PrivateQuery(q) && !q->tcp)
4017    {
4018        LogInfo("sendLLQRefresh setting up new TLS session %##s (%s)", q->qname.c, DNSTypeName(q->qtype));
4019        if (!q->nta)
4020        {
4021            // Note: If a question is in LLQ_Established state, we never free the zone data for the
4022            // question (PrivateQuery). If we free, we reset the state to something other than LLQ_Established.
4023            // This function is called only if the query is in LLQ_Established state and hence nta should
4024            // never be NULL. In spite of that, we have seen q->nta being NULL in the field. Just refetch the
4025            // zone data in that case.
4026            q->nta = StartGetZoneData(m, &q->qname, ZoneServiceLLQ, LLQGotZoneData, q);
4027            return;
4028            // ThisQInterval is not adjusted when we return from here which means that we will get called back
4029            // again immediately. As q->servAddr and q->servPort are still valid and the nta->Host is initialized
4030            // without any additional discovery for PrivateQuery, things work.
4031        }
4032        q->tcp = MakeTCPConn(m, &m->omsg, end, kTCPSocketFlags_UseTLS, &q->servAddr, q->servPort, &q->nta->Host, q, mDNSNULL);
4033    }
4034    else
4035    {
4036        mStatus err;
4037
4038        // if AuthInfo and AuthInfo->AutoTunnel is set, we use the TCP socket but don't need to pass the AuthInfo as
4039        // we already protected the message above.
4040        LogInfo("sendLLQRefresh: using existing %s session %##s (%s)", PrivateQuery(q) ? "TLS" : "UDP",
4041                q->qname.c, DNSTypeName(q->qtype));
4042
4043        err = mDNSSendDNSMessage(m, &m->omsg, end, mDNSInterface_Any, q->LocalSocket, &q->servAddr, q->servPort, q->tcp ? q->tcp->sock : mDNSNULL, mDNSNULL, mDNSfalse);
4044        if (err)
4045        {
4046            LogMsg("sendLLQRefresh: mDNSSendDNSMessage%s failed: %d", q->tcp ? " (TCP)" : "", err);
4047            if (q->tcp) { DisposeTCPConn(q->tcp); q->tcp = mDNSNULL; }
4048        }
4049    }
4050
4051    q->ntries++;
4052
4053    debugf("sendLLQRefresh ntries %d %##s (%s)", q->ntries, q->qname.c, DNSTypeName(q->qtype));
4054
4055    q->LastQTime = m->timenow;
4056    SetNextQueryTime(m, q);
4057}
4058
4059mDNSexport void LLQGotZoneData(mDNS *const m, mStatus err, const ZoneData *zoneInfo)
4060{
4061    DNSQuestion *q = (DNSQuestion *)zoneInfo->ZoneDataContext;
4062
4063    mDNS_Lock(m);
4064
4065    // If we get here it means that the GetZoneData operation has completed.
4066    // We hold on to the zone data if it is AutoTunnel as we use the hostname
4067    // in zoneInfo during the TLS connection setup.
4068    q->servAddr = zeroAddr;
4069    q->servPort = zeroIPPort;
4070
4071    if (!err && !mDNSIPPortIsZero(zoneInfo->Port) && !mDNSAddressIsZero(&zoneInfo->Addr) && zoneInfo->Host.c[0])
4072    {
4073        q->servAddr = zoneInfo->Addr;
4074        q->servPort = zoneInfo->Port;
4075        if (!PrivateQuery(q))
4076        {
4077            // We don't need the zone data as we use it only for the Host information which we
4078            // don't need if we are not going to use TLS connections.
4079            if (q->nta)
4080            {
4081                if (q->nta != zoneInfo) LogMsg("LLQGotZoneData: nta (%p) != zoneInfo (%p)  %##s (%s)", q->nta, zoneInfo, q->qname.c, DNSTypeName(q->qtype));
4082                CancelGetZoneData(m, q->nta);
4083                q->nta = mDNSNULL;
4084            }
4085        }
4086        q->ntries = 0;
4087        debugf("LLQGotZoneData %#a:%d", &q->servAddr, mDNSVal16(q->servPort));
4088        startLLQHandshake(m, q);
4089    }
4090    else
4091    {
4092        if (q->nta)
4093        {
4094            if (q->nta != zoneInfo) LogMsg("LLQGotZoneData: nta (%p) != zoneInfo (%p)  %##s (%s)", q->nta, zoneInfo, q->qname.c, DNSTypeName(q->qtype));
4095            CancelGetZoneData(m, q->nta);
4096            q->nta = mDNSNULL;
4097        }
4098        StartLLQPolling(m,q);
4099        if (err == mStatus_NoSuchNameErr)
4100        {
4101            // this actually failed, so mark it by setting address to all ones
4102            q->servAddr.type = mDNSAddrType_IPv4;
4103            q->servAddr.ip.v4 = onesIPv4Addr;
4104        }
4105    }
4106
4107    mDNS_Unlock(m);
4108}
4109
4110#ifdef DNS_PUSH_ENABLED
4111mDNSexport void DNSPushNotificationGotZoneData(mDNS *const m, mStatus err, const ZoneData *zoneInfo)
4112{
4113    DNSQuestion *q = (DNSQuestion *)zoneInfo->ZoneDataContext;
4114    mDNS_Lock(m);
4115
4116    // If we get here it means that the GetZoneData operation has completed.
4117    // We hold on to the zone data if it is AutoTunnel as we use the hostname
4118    // in zoneInfo during the TLS connection setup.
4119    q->servAddr = zeroAddr;
4120    q->servPort = zeroIPPort;
4121    if (!err && zoneInfo && !mDNSIPPortIsZero(zoneInfo->Port) && !mDNSAddressIsZero(&zoneInfo->Addr) && zoneInfo->Host.c[0])
4122    {
4123        q->dnsPushState      = DNSPUSH_SERVERFOUND;
4124        q->dnsPushServerAddr = zoneInfo->Addr;
4125        q->dnsPushServerPort = zoneInfo->Port;
4126        q->ntries            = 0;
4127        LogInfo("DNSPushNotificationGotZoneData %#a:%d", &q->dnsPushServerAddr, mDNSVal16(q->dnsPushServerPort));
4128        SubscribeToDNSPushNotificationServer(m,q);
4129    }
4130    else
4131    {
4132        q->dnsPushState = DNSPUSH_NOSERVER;
4133        StartLLQPolling(m,q);
4134        if (err == mStatus_NoSuchNameErr)
4135        {
4136            // this actually failed, so mark it by setting address to all ones
4137            q->servAddr.type  = mDNSAddrType_IPv4;
4138            q->servAddr.ip.v4 = onesIPv4Addr;
4139        }
4140    }
4141    mDNS_Unlock(m);
4142}
4143#endif // DNS_PUSH_ENABLED
4144
4145// Called in normal callback context (i.e. mDNS_busy and mDNS_reentrancy are both 1)
4146mDNSlocal void PrivateQueryGotZoneData(mDNS *const m, mStatus err, const ZoneData *zoneInfo)
4147{
4148    DNSQuestion *q = (DNSQuestion *) zoneInfo->ZoneDataContext;
4149
4150    LogInfo("PrivateQueryGotZoneData %##s (%s) err %d Zone %##s Private %d", q->qname.c, DNSTypeName(q->qtype), err, zoneInfo->ZoneName.c, zoneInfo->ZonePrivate);
4151
4152    if (q->nta != zoneInfo) LogMsg("PrivateQueryGotZoneData:ERROR!!: nta (%p) != zoneInfo (%p)  %##s (%s)", q->nta, zoneInfo, q->qname.c, DNSTypeName(q->qtype));
4153
4154    if (err || !zoneInfo || mDNSAddressIsZero(&zoneInfo->Addr) || mDNSIPPortIsZero(zoneInfo->Port) || !zoneInfo->Host.c[0])
4155    {
4156        LogInfo("PrivateQueryGotZoneData: ERROR!! %##s (%s) invoked with error code %d %p %#a:%d",
4157                q->qname.c, DNSTypeName(q->qtype), err, zoneInfo,
4158                zoneInfo ? &zoneInfo->Addr : mDNSNULL,
4159                zoneInfo ? mDNSVal16(zoneInfo->Port) : 0);
4160        CancelGetZoneData(m, q->nta);
4161        q->nta = mDNSNULL;
4162        return;
4163    }
4164
4165    if (!zoneInfo->ZonePrivate)
4166    {
4167        debugf("Private port lookup failed -- retrying without TLS -- %##s (%s)", q->qname.c, DNSTypeName(q->qtype));
4168        q->AuthInfo      = mDNSNULL;        // Clear AuthInfo so we try again non-private
4169        q->ThisQInterval = InitialQuestionInterval;
4170        q->LastQTime     = m->timenow - q->ThisQInterval;
4171        CancelGetZoneData(m, q->nta);
4172        q->nta = mDNSNULL;
4173        mDNS_Lock(m);
4174        SetNextQueryTime(m, q);
4175        mDNS_Unlock(m);
4176        return;
4177        // Next call to uDNS_CheckCurrentQuestion() will do this as a non-private query
4178    }
4179
4180    if (!PrivateQuery(q))
4181    {
4182        LogMsg("PrivateQueryGotZoneData: ERROR!! Not a private query %##s (%s) AuthInfo %p", q->qname.c, DNSTypeName(q->qtype), q->AuthInfo);
4183        CancelGetZoneData(m, q->nta);
4184        q->nta = mDNSNULL;
4185        return;
4186    }
4187
4188    q->TargetQID = mDNS_NewMessageID(m);
4189    if (q->tcp) { DisposeTCPConn(q->tcp); q->tcp = mDNSNULL; }
4190    if (!q->nta) { LogMsg("PrivateQueryGotZoneData:ERROR!! nta is NULL for %##s (%s)", q->qname.c, DNSTypeName(q->qtype)); return; }
4191    q->tcp = MakeTCPConn(m, mDNSNULL, mDNSNULL, kTCPSocketFlags_UseTLS, &zoneInfo->Addr, zoneInfo->Port, &q->nta->Host, q, mDNSNULL);
4192    if (q->nta) { CancelGetZoneData(m, q->nta); q->nta = mDNSNULL; }
4193}
4194
4195// ***************************************************************************
4196#if COMPILER_LIKES_PRAGMA_MARK
4197#pragma mark - Dynamic Updates
4198#endif
4199
4200// Called in normal callback context (i.e. mDNS_busy and mDNS_reentrancy are both 1)
4201mDNSexport void RecordRegistrationGotZoneData(mDNS *const m, mStatus err, const ZoneData *zoneData)
4202{
4203    AuthRecord *newRR;
4204    AuthRecord *ptr;
4205    int c1, c2;
4206
4207    if (!zoneData) { LogMsg("ERROR: RecordRegistrationGotZoneData invoked with NULL result and no error"); return; }
4208
4209    newRR = (AuthRecord*)zoneData->ZoneDataContext;
4210
4211    if (newRR->nta != zoneData)
4212        LogMsg("RecordRegistrationGotZoneData: nta (%p) != zoneData (%p)  %##s (%s)", newRR->nta, zoneData, newRR->resrec.name->c, DNSTypeName(newRR->resrec.rrtype));
4213
4214    if (m->mDNS_busy != m->mDNS_reentrancy)
4215        LogMsg("RecordRegistrationGotZoneData: mDNS_busy (%ld) != mDNS_reentrancy (%ld)", m->mDNS_busy, m->mDNS_reentrancy);
4216
4217    // make sure record is still in list (!!!)
4218    for (ptr = m->ResourceRecords; ptr; ptr = ptr->next) if (ptr == newRR) break;
4219    if (!ptr)
4220    {
4221        LogMsg("RecordRegistrationGotZoneData - RR no longer in list.  Discarding.");
4222        CancelGetZoneData(m, newRR->nta);
4223        newRR->nta = mDNSNULL;
4224        return;
4225    }
4226
4227    // check error/result
4228    if (err)
4229    {
4230        if (err != mStatus_NoSuchNameErr) LogMsg("RecordRegistrationGotZoneData: error %d", err);
4231        CancelGetZoneData(m, newRR->nta);
4232        newRR->nta = mDNSNULL;
4233        return;
4234    }
4235
4236    if (newRR->resrec.rrclass != zoneData->ZoneClass)
4237    {
4238        LogMsg("ERROR: New resource record's class (%d) does not match zone class (%d)", newRR->resrec.rrclass, zoneData->ZoneClass);
4239        CancelGetZoneData(m, newRR->nta);
4240        newRR->nta = mDNSNULL;
4241        return;
4242    }
4243
4244    // Don't try to do updates to the root name server.
4245    // We might be tempted also to block updates to any single-label name server (e.g. com, edu, net, etc.) but some
4246    // organizations use their own private pseudo-TLD, like ".home", etc, and we don't want to block that.
4247    if (zoneData->ZoneName.c[0] == 0)
4248    {
4249        LogInfo("RecordRegistrationGotZoneData: No name server found claiming responsibility for \"%##s\"!", newRR->resrec.name->c);
4250        CancelGetZoneData(m, newRR->nta);
4251        newRR->nta = mDNSNULL;
4252        return;
4253    }
4254
4255    // Store discovered zone data
4256    c1 = CountLabels(newRR->resrec.name);
4257    c2 = CountLabels(&zoneData->ZoneName);
4258    if (c2 > c1)
4259    {
4260        LogMsg("RecordRegistrationGotZoneData: Zone \"%##s\" is longer than \"%##s\"", zoneData->ZoneName.c, newRR->resrec.name->c);
4261        CancelGetZoneData(m, newRR->nta);
4262        newRR->nta = mDNSNULL;
4263        return;
4264    }
4265    newRR->zone = SkipLeadingLabels(newRR->resrec.name, c1-c2);
4266    if (!SameDomainName(newRR->zone, &zoneData->ZoneName))
4267    {
4268        LogMsg("RecordRegistrationGotZoneData: Zone \"%##s\" does not match \"%##s\" for \"%##s\"", newRR->zone->c, zoneData->ZoneName.c, newRR->resrec.name->c);
4269        CancelGetZoneData(m, newRR->nta);
4270        newRR->nta = mDNSNULL;
4271        return;
4272    }
4273
4274    if (mDNSIPPortIsZero(zoneData->Port) || mDNSAddressIsZero(&zoneData->Addr) || !zoneData->Host.c[0])
4275    {
4276        LogInfo("RecordRegistrationGotZoneData: No _dns-update._udp service found for \"%##s\"!", newRR->resrec.name->c);
4277        CancelGetZoneData(m, newRR->nta);
4278        newRR->nta = mDNSNULL;
4279        return;
4280    }
4281
4282    newRR->Private      = zoneData->ZonePrivate;
4283    debugf("RecordRegistrationGotZoneData: Set zone information for %##s %##s to %#a:%d",
4284           newRR->resrec.name->c, zoneData->ZoneName.c, &zoneData->Addr, mDNSVal16(zoneData->Port));
4285
4286    // If we are deregistering, uDNS_DeregisterRecord will do that as it has the zone data now.
4287    if (newRR->state == regState_DeregPending)
4288    {
4289        mDNS_Lock(m);
4290        uDNS_DeregisterRecord(m, newRR);
4291        mDNS_Unlock(m);
4292        return;
4293    }
4294
4295    if (newRR->resrec.rrtype == kDNSType_SRV)
4296    {
4297        const domainname *target;
4298        // Reevaluate the target always as NAT/Target could have changed while
4299        // we were fetching zone data.
4300        mDNS_Lock(m);
4301        target = GetServiceTarget(m, newRR);
4302        mDNS_Unlock(m);
4303        if (!target || target->c[0] == 0)
4304        {
4305            domainname *t = GetRRDomainNameTarget(&newRR->resrec);
4306            LogInfo("RecordRegistrationGotZoneData - no target for %##s", newRR->resrec.name->c);
4307            if (t) t->c[0] = 0;
4308            newRR->resrec.rdlength = newRR->resrec.rdestimate = 0;
4309            newRR->state = regState_NoTarget;
4310            CancelGetZoneData(m, newRR->nta);
4311            newRR->nta = mDNSNULL;
4312            return;
4313        }
4314    }
4315    // If we have non-zero service port (always?)
4316    // and a private address, and update server is non-private
4317    // and this service is AutoTarget
4318    // then initiate a NAT mapping request. On completion it will do SendRecordRegistration() for us
4319    if (newRR->resrec.rrtype == kDNSType_SRV && !mDNSIPPortIsZero(newRR->resrec.rdata->u.srv.port) &&
4320        mDNSv4AddrIsRFC1918(&m->AdvertisedV4.ip.v4) && newRR->nta && !mDNSAddrIsRFC1918(&newRR->nta->Addr) &&
4321        newRR->AutoTarget == Target_AutoHostAndNATMAP)
4322    {
4323        DomainAuthInfo *AuthInfo;
4324        AuthInfo = GetAuthInfoForName(m, newRR->resrec.name);
4325        if (AuthInfo && AuthInfo->AutoTunnel)
4326        {
4327            domainname *t = GetRRDomainNameTarget(&newRR->resrec);
4328            LogMsg("RecordRegistrationGotZoneData: ERROR!! AutoTunnel has Target_AutoHostAndNATMAP for %s", ARDisplayString(m, newRR));
4329            if (t) t->c[0] = 0;
4330            newRR->resrec.rdlength = newRR->resrec.rdestimate = 0;
4331            newRR->state = regState_NoTarget;
4332            CancelGetZoneData(m, newRR->nta);
4333            newRR->nta = mDNSNULL;
4334            return;
4335        }
4336        // During network transitions, we are called multiple times in different states. Setup NAT
4337        // state just once for this record.
4338        if (!newRR->NATinfo.clientContext)
4339        {
4340            LogInfo("RecordRegistrationGotZoneData StartRecordNatMap %s", ARDisplayString(m, newRR));
4341            newRR->state = regState_NATMap;
4342            StartRecordNatMap(m, newRR);
4343            return;
4344        }
4345        else LogInfo("RecordRegistrationGotZoneData: StartRecordNatMap for %s, state %d, context %p", ARDisplayString(m, newRR), newRR->state, newRR->NATinfo.clientContext);
4346    }
4347    mDNS_Lock(m);
4348    // We want IsRecordMergeable to check whether it is a record whose update can be
4349    // sent with others. We set the time before we call IsRecordMergeable, so that
4350    // it does not fail this record based on time. We are interested in other checks
4351    // at this time. If a previous update resulted in error, then don't reset the
4352    // interval. Preserve the back-off so that we don't keep retrying aggressively.
4353    if (newRR->updateError == mStatus_NoError)
4354    {
4355        newRR->ThisAPInterval = INIT_RECORD_REG_INTERVAL;
4356        newRR->LastAPTime = m->timenow - INIT_RECORD_REG_INTERVAL;
4357    }
4358    if (IsRecordMergeable(m, newRR, m->timenow + MERGE_DELAY_TIME))
4359    {
4360        // Delay the record registration by MERGE_DELAY_TIME so that we can merge them
4361        // into one update
4362        LogInfo("RecordRegistrationGotZoneData: Delayed registration for %s", ARDisplayString(m, newRR));
4363        newRR->LastAPTime += MERGE_DELAY_TIME;
4364    }
4365    mDNS_Unlock(m);
4366}
4367
4368mDNSlocal void SendRecordDeregistration(mDNS *m, AuthRecord *rr)
4369{
4370    mDNSu8 *ptr = m->omsg.data;
4371    mDNSu8 *limit;
4372    DomainAuthInfo *AuthInfo;
4373
4374    mDNS_CheckLock(m);
4375
4376    if (!rr->nta || mDNSIPv4AddressIsZero(rr->nta->Addr.ip.v4))
4377    {
4378        LogMsg("SendRecordDeRegistration: No zone info for Resource record %s RecordType %d", ARDisplayString(m, rr), rr->resrec.RecordType);
4379        return;
4380    }
4381
4382    limit = ptr + AbsoluteMaxDNSMessageData;
4383    AuthInfo = GetAuthInfoForName_internal(m, rr->resrec.name);
4384    limit -= RRAdditionalSize(m, AuthInfo);
4385
4386    rr->updateid = mDNS_NewMessageID(m);
4387    InitializeDNSMessage(&m->omsg.h, rr->updateid, UpdateReqFlags);
4388
4389    // set zone
4390    ptr = putZone(&m->omsg, ptr, limit, rr->zone, mDNSOpaque16fromIntVal(rr->resrec.rrclass));
4391    if (!ptr) goto exit;
4392
4393    ptr = BuildUpdateMessage(m, ptr, rr, limit);
4394
4395    if (!ptr) goto exit;
4396
4397    if (rr->Private)
4398    {
4399        LogInfo("SendRecordDeregistration TCP %p %s", rr->tcp, ARDisplayString(m, rr));
4400        if (rr->tcp) LogInfo("SendRecordDeregistration: Disposing existing TCP connection for %s", ARDisplayString(m, rr));
4401        if (rr->tcp) { DisposeTCPConn(rr->tcp); rr->tcp = mDNSNULL; }
4402        if (!rr->nta) { LogMsg("SendRecordDeregistration:Private:ERROR!! nta is NULL for %s", ARDisplayString(m, rr)); return; }
4403        rr->tcp = MakeTCPConn(m, &m->omsg, ptr, kTCPSocketFlags_UseTLS, &rr->nta->Addr, rr->nta->Port, &rr->nta->Host, mDNSNULL, rr);
4404    }
4405    else
4406    {
4407        mStatus err;
4408        LogInfo("SendRecordDeregistration UDP %s", ARDisplayString(m, rr));
4409        if (!rr->nta) { LogMsg("SendRecordDeregistration:ERROR!! nta is NULL for %s", ARDisplayString(m, rr)); return; }
4410        err = mDNSSendDNSMessage(m, &m->omsg, ptr, mDNSInterface_Any, mDNSNULL, &rr->nta->Addr, rr->nta->Port, mDNSNULL, GetAuthInfoForName_internal(m, rr->resrec.name), mDNSfalse);
4411        if (err) debugf("ERROR: SendRecordDeregistration - mDNSSendDNSMessage - %d", err);
4412        //if (rr->state == regState_DeregPending) CompleteDeregistration(m, rr);        // Don't touch rr after this
4413    }
4414    SetRecordRetry(m, rr, 0);
4415    return;
4416exit:
4417    LogMsg("SendRecordDeregistration: Error formatting message for %s", ARDisplayString(m, rr));
4418}
4419
4420mDNSexport mStatus uDNS_DeregisterRecord(mDNS *const m, AuthRecord *const rr)
4421{
4422    DomainAuthInfo *info;
4423
4424    LogInfo("uDNS_DeregisterRecord: Resource Record %s, state %d", ARDisplayString(m, rr), rr->state);
4425
4426    switch (rr->state)
4427    {
4428    case regState_Refresh:
4429    case regState_Pending:
4430    case regState_UpdatePending:
4431    case regState_Registered: break;
4432    case regState_DeregPending: break;
4433
4434    case regState_NATError:
4435    case regState_NATMap:
4436    // A record could be in NoTarget to start with if the corresponding SRV record could not find a target.
4437    // It is also possible to reenter the NoTarget state when we move to a network with a NAT that has
4438    // no {PCP, NAT-PMP, UPnP/IGD} support. In that case before we entered NoTarget, we already deregistered with
4439    // the server.
4440    case regState_NoTarget:
4441    case regState_Unregistered:
4442    case regState_Zero:
4443    default:
4444        LogInfo("uDNS_DeregisterRecord: State %d for %##s type %s", rr->state, rr->resrec.name->c, DNSTypeName(rr->resrec.rrtype));
4445        // This function may be called during sleep when there are no sleep proxy servers
4446        if (rr->resrec.RecordType == kDNSRecordTypeDeregistering) CompleteDeregistration(m, rr);
4447        return mStatus_NoError;
4448    }
4449
4450    // if unsent rdata is queued, free it.
4451    //
4452    // The data may be queued in QueuedRData or InFlightRData.
4453    //
4454    // 1) If the record is in Registered state, we store it in InFlightRData and copy the same in "rdata"
4455    //   *just* before sending the update to the server. Till we get the response, InFlightRData and "rdata"
4456    //   in the resource record are same. We don't want to free in that case. It will be freed when "rdata"
4457    //   is freed. If they are not same, the update has not been sent and we should free it here.
4458    //
4459    // 2) If the record is in UpdatePending state, we queue the update in QueuedRData. When the previous update
4460    //   comes back from the server, we copy it from QueuedRData to InFlightRData and repeat (1). This implies
4461    //   that QueuedRData can never be same as "rdata" in the resource record. As long as we have something
4462    //   left in QueuedRData, we should free it here.
4463
4464    if (rr->InFlightRData && rr->UpdateCallback)
4465    {
4466        if (rr->InFlightRData != rr->resrec.rdata)
4467        {
4468            LogInfo("uDNS_DeregisterRecord: Freeing InFlightRData for %s", ARDisplayString(m, rr));
4469            rr->UpdateCallback(m, rr, rr->InFlightRData, rr->InFlightRDLen);
4470            rr->InFlightRData = mDNSNULL;
4471        }
4472        else
4473            LogInfo("uDNS_DeregisterRecord: InFlightRData same as rdata for %s", ARDisplayString(m, rr));
4474    }
4475
4476    if (rr->QueuedRData && rr->UpdateCallback)
4477    {
4478        if (rr->QueuedRData == rr->resrec.rdata)
4479            LogMsg("uDNS_DeregisterRecord: ERROR!! QueuedRData same as rdata for %s", ARDisplayString(m, rr));
4480        else
4481        {
4482            LogInfo("uDNS_DeregisterRecord: Freeing QueuedRData for %s", ARDisplayString(m, rr));
4483            rr->UpdateCallback(m, rr, rr->QueuedRData, rr->QueuedRDLen);
4484            rr->QueuedRData = mDNSNULL;
4485        }
4486    }
4487
4488    // If a current group registration is pending, we can't send this deregisration till that registration
4489    // has reached the server i.e., the ordering is important. Previously, if we did not send this
4490    // registration in a group, then the previous connection will be torn down as part of sending the
4491    // deregistration. If we send this in a group, we need to locate the resource record that was used
4492    // to send this registration and terminate that connection. This means all the updates on that might
4493    // be lost (assuming the response is not waiting for us at the socket) and the retry will send the
4494    // update again sometime in the near future.
4495    //
4496    // NOTE: SSL handshake failures normally free the TCP connection immediately. Hence, you may not
4497    // find the TCP below there. This case can happen only when tcp is trying to actively retransmit
4498    // the request or SSL negotiation taking time i.e resource record is actively trying to get the
4499    // message to the server. During that time a deregister has to happen.
4500
4501    if (!mDNSOpaque16IsZero(rr->updateid))
4502    {
4503        AuthRecord *anchorRR;
4504        mDNSBool found = mDNSfalse;
4505        for (anchorRR = m->ResourceRecords; anchorRR; anchorRR = anchorRR->next)
4506        {
4507            if (AuthRecord_uDNS(rr) && mDNSSameOpaque16(anchorRR->updateid, rr->updateid) && anchorRR->tcp)
4508            {
4509                LogInfo("uDNS_DeregisterRecord: Found Anchor RR %s terminated", ARDisplayString(m, anchorRR));
4510                if (found)
4511                    LogMsg("uDNS_DeregisterRecord: ERROR: Another anchorRR %s found", ARDisplayString(m, anchorRR));
4512                DisposeTCPConn(anchorRR->tcp);
4513                anchorRR->tcp = mDNSNULL;
4514                found = mDNStrue;
4515            }
4516        }
4517        if (!found) LogInfo("uDNSDeregisterRecord: Cannot find the anchor Resource Record for %s, not an error", ARDisplayString(m, rr));
4518    }
4519
4520    // Retry logic for deregistration should be no different from sending registration the first time.
4521    // Currently ThisAPInterval most likely is set to the refresh interval
4522    rr->state          = regState_DeregPending;
4523    rr->ThisAPInterval = INIT_RECORD_REG_INTERVAL;
4524    rr->LastAPTime     = m->timenow - INIT_RECORD_REG_INTERVAL;
4525    info = GetAuthInfoForName_internal(m, rr->resrec.name);
4526    if (IsRecordMergeable(m, rr, m->timenow + MERGE_DELAY_TIME))
4527    {
4528        // Delay the record deregistration by MERGE_DELAY_TIME so that we can merge them
4529        // into one update. If the domain is being deleted, delay by 2 * MERGE_DELAY_TIME
4530        // so that we can merge all the AutoTunnel records and the service records in
4531        // one update (they get deregistered a little apart)
4532        if (info && info->deltime) rr->LastAPTime += (2 * MERGE_DELAY_TIME);
4533        else rr->LastAPTime += MERGE_DELAY_TIME;
4534    }
4535    // IsRecordMergeable could have returned false for several reasons e.g., DontMerge is set or
4536    // no zone information. Most likely it is the latter, CheckRecordUpdates will fetch the zone
4537    // data when it encounters this record.
4538
4539    if (m->NextuDNSEvent - (rr->LastAPTime + rr->ThisAPInterval) >= 0)
4540        m->NextuDNSEvent = (rr->LastAPTime + rr->ThisAPInterval);
4541
4542    return mStatus_NoError;
4543}
4544
4545mDNSexport mStatus uDNS_UpdateRecord(mDNS *m, AuthRecord *rr)
4546{
4547    LogInfo("uDNS_UpdateRecord: Resource Record %##s, state %d", rr->resrec.name->c, rr->state);
4548    switch(rr->state)
4549    {
4550    case regState_DeregPending:
4551    case regState_Unregistered:
4552        // not actively registered
4553        goto unreg_error;
4554
4555    case regState_NATMap:
4556    case regState_NoTarget:
4557        // change rdata directly since it hasn't been sent yet
4558        if (rr->UpdateCallback) rr->UpdateCallback(m, rr, rr->resrec.rdata, rr->resrec.rdlength);
4559        SetNewRData(&rr->resrec, rr->NewRData, rr->newrdlength);
4560        rr->NewRData = mDNSNULL;
4561        return mStatus_NoError;
4562
4563    case regState_Pending:
4564    case regState_Refresh:
4565    case regState_UpdatePending:
4566        // registration in-flight. queue rdata and return
4567        if (rr->QueuedRData && rr->UpdateCallback)
4568            // if unsent rdata is already queued, free it before we replace it
4569            rr->UpdateCallback(m, rr, rr->QueuedRData, rr->QueuedRDLen);
4570        rr->QueuedRData = rr->NewRData;
4571        rr->QueuedRDLen = rr->newrdlength;
4572        rr->NewRData = mDNSNULL;
4573        return mStatus_NoError;
4574
4575    case regState_Registered:
4576        rr->OrigRData = rr->resrec.rdata;
4577        rr->OrigRDLen = rr->resrec.rdlength;
4578        rr->InFlightRData = rr->NewRData;
4579        rr->InFlightRDLen = rr->newrdlength;
4580        rr->NewRData = mDNSNULL;
4581        rr->state = regState_UpdatePending;
4582        rr->ThisAPInterval = INIT_RECORD_REG_INTERVAL;
4583        rr->LastAPTime = m->timenow - INIT_RECORD_REG_INTERVAL;
4584        SetNextuDNSEvent(m, rr);
4585        return mStatus_NoError;
4586
4587    case regState_NATError:
4588        LogMsg("ERROR: uDNS_UpdateRecord called for record %##s with bad state regState_NATError", rr->resrec.name->c);
4589        return mStatus_UnknownErr;      // states for service records only
4590
4591    default: LogMsg("uDNS_UpdateRecord: Unknown state %d for %##s", rr->state, rr->resrec.name->c);
4592    }
4593
4594unreg_error:
4595    LogMsg("uDNS_UpdateRecord: Requested update of record %##s type %d, in erroneous state %d",
4596           rr->resrec.name->c, rr->resrec.rrtype, rr->state);
4597    return mStatus_Invalid;
4598}
4599
4600// ***************************************************************************
4601#if COMPILER_LIKES_PRAGMA_MARK
4602#pragma mark - Periodic Execution Routines
4603#endif
4604
4605mDNSlocal void handle_unanswered_query(mDNS *const m)
4606{
4607    DNSQuestion *q = m->CurrentQuestion;
4608
4609    if (q->unansweredQueries >= MAX_DNSSEC_UNANSWERED_QUERIES && DNSSECOptionalQuestion(q))
4610    {
4611        // If we are not receiving any responses for DNSSEC question, it could be due to
4612        // a broken middlebox or a DNS server that does not understand the EDNS0/DOK option that
4613        // silently drops the packets. Also as per RFC 5625 there are certain buggy DNS Proxies
4614        // that are known to drop these pkts. To handle this, we turn off sending the EDNS0/DOK
4615        // option if we have not received any responses indicating that the server or
4616        // the middlebox is DNSSEC aware. If we receive at least one response to a DNSSEC
4617        // question, we don't turn off validation. Also, we wait for MAX_DNSSEC_RETRANSMISSIONS
4618        // before turning off validation to accomodate packet loss.
4619        //
4620        // Note: req_DO affects only DNSSEC_VALIDATION_SECURE_OPTIONAL questions;
4621        // DNSSEC_VALIDATION_SECURE questions ignores req_DO.
4622
4623        if (!q->qDNSServer->DNSSECAware && q->qDNSServer->req_DO)
4624        {
4625            q->qDNSServer->retransDO++;
4626            if (q->qDNSServer->retransDO == MAX_DNSSEC_RETRANSMISSIONS)
4627            {
4628                LogInfo("handle_unanswered_query: setting req_DO false for %#a", &q->qDNSServer->addr);
4629                q->qDNSServer->req_DO = mDNSfalse;
4630            }
4631        }
4632
4633        if (!q->qDNSServer->req_DO)
4634        {
4635            q->ValidationState     = DNSSECValNotRequired;
4636            q->ValidationRequired  = DNSSEC_VALIDATION_NONE;
4637
4638            if (q->ProxyQuestion)
4639                q->ProxyDNSSECOK = mDNSfalse;
4640            LogInfo("handle_unanswered_query: unanswered query for %##s (%s), so turned off validation for %#a",
4641                q->qname.c, DNSTypeName(q->qtype), &q->qDNSServer->addr);
4642        }
4643    }
4644}
4645
4646mDNSlocal void uDNS_HandleLLQState(mDNS *const m, DNSQuestion *q)
4647{
4648#ifdef DNS_PUSH_ENABLED
4649    // First attempt to use DNS Push Notification.
4650    if (q->dnsPushState == DNSPUSH_INIT)
4651        DiscoverDNSPushNotificationServer(m, q);
4652#endif // DNS_PUSH_ENABLED
4653    switch (q->state)
4654    {
4655        case LLQ_InitialRequest:   startLLQHandshake(m, q); break;
4656        case LLQ_SecondaryRequest:
4657            // For PrivateQueries, we need to start the handshake again as we don't do the Challenge/Response step
4658            if (PrivateQuery(q))   startLLQHandshake(m, q);
4659            else                   sendChallengeResponse(m, q, mDNSNULL);
4660            break;
4661        case LLQ_Established:      sendLLQRefresh(m, q); break;
4662        case LLQ_Poll:             break;       // Do nothing (handled below)
4663    }
4664}
4665
4666// The question to be checked is not passed in as an explicit parameter;
4667// instead it is implicit that the question to be checked is m->CurrentQuestion.
4668mDNSexport void uDNS_CheckCurrentQuestion(mDNS *const m)
4669{
4670    DNSQuestion *q = m->CurrentQuestion;
4671    if (m->timenow - NextQSendTime(q) < 0) return;
4672
4673    if (q->LongLived)
4674    {
4675        uDNS_HandleLLQState(m,q);
4676    }
4677
4678    handle_unanswered_query(m);
4679    // We repeat the check above (rather than just making this the "else" case) because startLLQHandshake can change q->state to LLQ_Poll
4680    if (!(q->LongLived && q->state != LLQ_Poll))
4681    {
4682        if (q->unansweredQueries >= MAX_UCAST_UNANSWERED_QUERIES)
4683        {
4684            DNSServer *orig = q->qDNSServer;
4685            if (orig)
4686                LogInfo("uDNS_CheckCurrentQuestion: Sent %d unanswered queries for %##s (%s) to %#a:%d (%##s)",
4687                        q->unansweredQueries, q->qname.c, DNSTypeName(q->qtype), &orig->addr, mDNSVal16(orig->port), orig->domain.c);
4688
4689#if APPLE_OSX_mDNSResponder
4690            SymptomReporterDNSServerUnreachable(orig);
4691#endif
4692            PenalizeDNSServer(m, q, zeroID);
4693            q->noServerResponse = 1;
4694        }
4695        // There are two cases here.
4696        //
4697        // 1. We have only one DNS server for this question. It is not responding even after we sent MAX_UCAST_UNANSWERED_QUERIES.
4698        //    In that case, we need to keep retrying till we get a response. But we need to backoff as we retry. We set
4699        //    noServerResponse in the block above and below we do not touch the question interval. When we come here, we
4700        //    already waited for the response. We need to send another query right at this moment. We do that below by
4701        //    reinitializing dns servers and reissuing the query.
4702        //
4703        // 2. We have more than one DNS server. If at least one server did not respond, we would have set noServerResponse
4704        //    either now (the last server in the list) or before (non-last server in the list). In either case, if we have
4705        //    reached the end of DNS server list, we need to try again from the beginning. Ideally we should try just the
4706        //    servers that did not respond, but for simplicity we try all the servers. Once we reached the end of list, we
4707        //    set triedAllServersOnce so that we don't try all the servers aggressively. See PenalizeDNSServer.
4708        if (!q->qDNSServer && q->noServerResponse)
4709        {
4710            DNSServer *new;
4711            DNSQuestion *qptr;
4712            q->triedAllServersOnce = 1;
4713            // Re-initialize all DNS servers for this question. If we have a DNSServer, DNSServerChangeForQuestion will
4714            // handle all the work including setting the new DNS server.
4715            SetValidDNSServers(m, q);
4716            new = GetServerForQuestion(m, q);
4717            if (new)
4718            {
4719		mDNSIPPort zp = zeroIPPort;
4720                LogInfo("uDNS_checkCurrentQuestion: Retrying question %p %##s (%s) DNS Server %#a:%d ThisQInterval %d",
4721                        q, q->qname.c, DNSTypeName(q->qtype), new ? &new->addr : mDNSNULL, mDNSVal16(new? new->port : zp), q->ThisQInterval);
4722                DNSServerChangeForQuestion(m, q, new);
4723            }
4724            for (qptr = q->next ; qptr; qptr = qptr->next)
4725                if (qptr->DuplicateOf == q) { qptr->validDNSServers = q->validDNSServers; qptr->qDNSServer = q->qDNSServer; }
4726        }
4727        if (q->qDNSServer)
4728        {
4729            mDNSu8 *end;
4730            mStatus err = mStatus_NoError;
4731            mDNSBool private = mDNSfalse;
4732
4733            InitializeDNSMessage(&m->omsg.h, q->TargetQID, (DNSSECQuestion(q) ? DNSSecQFlags : uQueryFlags));
4734
4735            end = putQuestion(&m->omsg, m->omsg.data, m->omsg.data + AbsoluteMaxDNSMessageData, &q->qname, q->qtype, q->qclass);
4736            if (DNSSECQuestion(q) && !q->qDNSServer->cellIntf)
4737            {
4738                if (q->ProxyQuestion)
4739                    end = DNSProxySetAttributes(q, &m->omsg.h, &m->omsg, end, m->omsg.data + AbsoluteMaxDNSMessageData);
4740                else
4741                    end = putDNSSECOption(&m->omsg, end, m->omsg.data + AbsoluteMaxDNSMessageData);
4742            }
4743            private = PrivateQuery(q);
4744
4745            if (end > m->omsg.data)
4746            {
4747                //LogMsg("uDNS_CheckCurrentQuestion %p %d %p %##s (%s)", q, NextQSendTime(q) - m->timenow, private, q->qname.c, DNSTypeName(q->qtype));
4748                if (private)
4749                {
4750                    if (q->nta) CancelGetZoneData(m, q->nta);
4751                    q->nta = StartGetZoneData(m, &q->qname, q->LongLived ? ZoneServiceLLQ : ZoneServiceQuery, PrivateQueryGotZoneData, q);
4752                    if (q->state == LLQ_Poll) q->ThisQInterval = (LLQ_POLL_INTERVAL + mDNSRandom(LLQ_POLL_INTERVAL/10)) / QuestionIntervalStep;
4753                }
4754                else
4755                {
4756		    mDNSIPPort zp = zeroIPPort;
4757                    debugf("uDNS_CheckCurrentQuestion sending %p %##s (%s) %#a:%d UnansweredQueries %d",
4758                           q, q->qname.c, DNSTypeName(q->qtype),
4759                           q->qDNSServer ? &q->qDNSServer->addr : mDNSNULL, mDNSVal16(q->qDNSServer ? q->qDNSServer->port : zeroIPPort), q->unansweredQueries);
4760#if APPLE_OSX_mDNSResponder
4761                    // When a DNS proxy network extension initiates the close of a UDP flow (this usually happens when a DNS
4762                    // proxy gets disabled or crashes), mDNSResponder's corresponding UDP socket will be marked with the
4763                    // SS_CANTRCVMORE state flag. Reading from such a socket is no longer possible, so close the current
4764                    // socket pair so that we can create a new pair.
4765                    if (q->LocalSocket && mDNSPlatformUDPSocketEncounteredEOF(q->LocalSocket))
4766                    {
4767                        mDNSPlatformUDPClose(q->LocalSocket);
4768                        q->LocalSocket = mDNSNULL;
4769                    }
4770#endif
4771                    if (!q->LocalSocket)
4772                    {
4773                        q->LocalSocket = mDNSPlatformUDPSocket(zeroIPPort);
4774                        if (q->LocalSocket)
4775                        {
4776                            mDNSPlatformSetSocktOpt(q->LocalSocket, mDNSTransport_UDP, mDNSAddrType_IPv4, q);
4777                            mDNSPlatformSetSocktOpt(q->LocalSocket, mDNSTransport_UDP, mDNSAddrType_IPv6, q);
4778                        }
4779                    }
4780                    if (!q->LocalSocket) err = mStatus_NoMemoryErr; // If failed to make socket (should be very rare), we'll try again next time
4781                    else
4782                    {
4783                        err = mDNSSendDNSMessage(m, &m->omsg, end, q->qDNSServer->interface, q->LocalSocket, &q->qDNSServer->addr, q->qDNSServer->port, mDNSNULL, mDNSNULL, q->UseBackgroundTrafficClass);
4784#if AWD_METRICS
4785                        if (!err)
4786                        {
4787                            MetricsUpdateDNSQuerySize((mDNSu32)(end - (mDNSu8 *)&m->omsg));
4788                            if (q->metrics.answered)
4789                            {
4790                                q->metrics.querySendCount = 0;
4791                                q->metrics.answered       = mDNSfalse;
4792                            }
4793                            if (q->metrics.querySendCount++ == 0)
4794                            {
4795                                q->metrics.firstQueryTime = m->timenow;
4796                            }
4797                        }
4798#endif
4799                    }
4800                }
4801            }
4802
4803            if (err == mStatus_HostUnreachErr)
4804            {
4805                DNSServer *newServer;
4806
4807                LogInfo("uDNS_CheckCurrentQuestion: host unreachable error for DNS server %#a for question [%p] %##s (%s)",
4808                    &q->qDNSServer->addr, q, q->qname.c, DNSTypeName(q->qtype));
4809
4810                if (!StrictUnicastOrdering)
4811                {
4812                    q->qDNSServer->penaltyTime = NonZeroTime(m->timenow + DNSSERVER_PENALTY_TIME);
4813                }
4814
4815                newServer = GetServerForQuestion(m, q);
4816                if (!newServer)
4817                {
4818                    q->triedAllServersOnce = 1;
4819                    SetValidDNSServers(m, q);
4820                    newServer = GetServerForQuestion(m, q);
4821                }
4822                if (newServer)
4823                {
4824                    LogInfo("uDNS_checkCurrentQuestion: Retrying question %p %##s (%s) DNS Server %#a:%u ThisQInterval %d",
4825                        q, q->qname.c, DNSTypeName(q->qtype), newServer ? &newServer->addr : mDNSNULL, mDNSVal16(newServer ? newServer->port : zeroIPPort), q->ThisQInterval);
4826                    DNSServerChangeForQuestion(m, q, newServer);
4827                }
4828                if (q->triedAllServersOnce)
4829                {
4830                    q->LastQTime = m->timenow;
4831                }
4832                else
4833                {
4834                    q->ThisQInterval = InitialQuestionInterval;
4835                    q->LastQTime     = m->timenow - q->ThisQInterval;
4836                }
4837                q-><