blob: 47559a4bbc1fe4084a1e9b2693d45e8da93853bf [file] [log] [blame]
/* -*- Mode: C; tab-width: 4 -*-
*
* Copyright (c) 2002-2006 Apple Computer, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* To Do:
* Elimate all mDNSPlatformMemAllocate/mDNSPlatformMemFree from this code -- the core code
* is supposed to be malloc-free so that it runs in constant memory determined at compile-time.
* Any dynamic run-time requirements should be handled by the platform layer below or client layer above
*/
#if APPLE_OSX_mDNSResponder
#include <TargetConditionals.h>
#endif
#include "uDNS.h"
#if(defined(_MSC_VER))
// Disable "assignment within conditional expression".
// Other compilers understand the convention that if you place the assignment expression within an extra pair
// of parentheses, this signals to the compiler that you really intended an assignment and no warning is necessary.
// The Microsoft compiler doesn't understand this convention, so in the absense of any other way to signal
// to the compiler that the assignment is intentional, we have to just turn this warning off completely.
#pragma warning(disable:4706)
#endif
// For domain enumeration and automatic browsing
// This is the user's DNS search list.
// In each of these domains we search for our special pointer records (lb._dns-sd._udp.<domain>, etc.)
// to discover recommended domains for domain enumeration (browse, default browse, registration,
// default registration) and possibly one or more recommended automatic browsing domains.
mDNSexport SearchListElem *SearchList = mDNSNULL;
// The value can be set to true by the Platform code e.g., MacOSX uses the plist mechanism
mDNSBool StrictUnicastOrdering = mDNSfalse;
// We keep track of the number of unicast DNS servers and log a message when we exceed 64.
// Currently the unicast queries maintain a 64 bit map to track the valid DNS servers for that
// question. Bit position is the index into the DNS server list. This is done so to try all
// the servers exactly once before giving up. If we could allocate memory in the core, then
// arbitrary limitation of 64 DNSServers can be removed.
mDNSu8 NumUnicastDNSServers = 0;
#define MAX_UNICAST_DNS_SERVERS 64
// ***************************************************************************
#if COMPILER_LIKES_PRAGMA_MARK
#pragma mark - General Utility Functions
#endif
// set retry timestamp for record with exponential backoff
mDNSlocal void SetRecordRetry(mDNS *const m, AuthRecord *rr, mDNSu32 random)
{
rr->LastAPTime = m->timenow;
if (rr->expire && rr->refreshCount < MAX_UPDATE_REFRESH_COUNT)
{
mDNSs32 remaining = rr->expire - m->timenow;
rr->refreshCount++;
if (remaining > MIN_UPDATE_REFRESH_TIME)
{
// Refresh at 70% + random (currently it is 0 to 10%)
rr->ThisAPInterval = 7 * (remaining/10) + (random ? random : mDNSRandom(remaining/10));
// Don't update more often than 5 minutes
if (rr->ThisAPInterval < MIN_UPDATE_REFRESH_TIME)
rr->ThisAPInterval = MIN_UPDATE_REFRESH_TIME;
LogInfo("SetRecordRetry refresh in %d of %d for %s",
rr->ThisAPInterval/mDNSPlatformOneSecond, (rr->expire - m->timenow)/mDNSPlatformOneSecond, ARDisplayString(m, rr));
}
else
{
rr->ThisAPInterval = MIN_UPDATE_REFRESH_TIME;
LogInfo("SetRecordRetry clamping to min refresh in %d of %d for %s",
rr->ThisAPInterval/mDNSPlatformOneSecond, (rr->expire - m->timenow)/mDNSPlatformOneSecond, ARDisplayString(m, rr));
}
return;
}
rr->expire = 0;
rr->ThisAPInterval = rr->ThisAPInterval * QuestionIntervalStep; // Same Retry logic as Unicast Queries
if (rr->ThisAPInterval < INIT_RECORD_REG_INTERVAL)
rr->ThisAPInterval = INIT_RECORD_REG_INTERVAL;
if (rr->ThisAPInterval > MAX_RECORD_REG_INTERVAL)
rr->ThisAPInterval = MAX_RECORD_REG_INTERVAL;
LogInfo("SetRecordRetry retry in %d ms for %s", rr->ThisAPInterval, ARDisplayString(m, rr));
}
// ***************************************************************************
#if COMPILER_LIKES_PRAGMA_MARK
#pragma mark - Name Server List Management
#endif
mDNSexport DNSServer *mDNS_AddDNSServer(mDNS *const m, const domainname *d, const mDNSInterfaceID interface, const mDNSAddr *addr, const mDNSIPPort port, mDNSBool scoped, mDNSu32 timeout)
{
DNSServer **p = &m->DNSServers;
DNSServer *tmp = mDNSNULL;
if ((NumUnicastDNSServers + 1) > MAX_UNICAST_DNS_SERVERS)
{
LogMsg("mDNS_AddDNSServer: DNS server limit of %d reached, not adding this server", MAX_UNICAST_DNS_SERVERS);
return mDNSNULL;
}
if (!d) d = (const domainname *)"";
LogInfo("mDNS_AddDNSServer: Adding %#a for %##s, InterfaceID %p, scoped %d", addr, d->c, interface, scoped);
if (m->mDNS_busy != m->mDNS_reentrancy+1)
LogMsg("mDNS_AddDNSServer: Lock not held! mDNS_busy (%ld) mDNS_reentrancy (%ld)", m->mDNS_busy, m->mDNS_reentrancy);
while (*p) // Check if we already have this {interface,address,port,domain} tuple registered
{
if ((*p)->scoped == scoped && (*p)->interface == interface && (*p)->teststate != DNSServer_Disabled &&
mDNSSameAddress(&(*p)->addr, addr) && mDNSSameIPPort((*p)->port, port) && SameDomainName(&(*p)->domain, d))
{
if (!((*p)->flags & DNSServer_FlagDelete)) debugf("Note: DNS Server %#a:%d for domain %##s (%p) registered more than once", addr, mDNSVal16(port), d->c, interface);
(*p)->flags &= ~DNSServer_FlagDelete;
tmp = *p;
*p = tmp->next;
tmp->next = mDNSNULL;
}
else
p=&(*p)->next;
}
if (tmp) *p = tmp; // move to end of list, to ensure ordering from platform layer
else
{
// allocate, add to list
*p = mDNSPlatformMemAllocate(sizeof(**p));
if (!*p) LogMsg("Error: mDNS_AddDNSServer - malloc");
else
{
NumUnicastDNSServers++;
(*p)->scoped = scoped;
(*p)->interface = interface;
(*p)->addr = *addr;
(*p)->port = port;
(*p)->flags = DNSServer_FlagNew;
(*p)->teststate = /* DNSServer_Untested */ DNSServer_Passed;
(*p)->lasttest = m->timenow - INIT_UCAST_POLL_INTERVAL;
(*p)->timeout = timeout;
AssignDomainName(&(*p)->domain, d);
(*p)->next = mDNSNULL;
}
}
(*p)->penaltyTime = 0;
return(*p);
}
// PenalizeDNSServer is called when the number of queries to the unicast
// DNS server exceeds MAX_UCAST_UNANSWERED_QUERIES or when we receive an
// error e.g., SERV_FAIL from DNS server.
mDNSexport void PenalizeDNSServer(mDNS *const m, DNSQuestion *q)
{
DNSServer *new;
DNSServer *orig = q->qDNSServer;
if (m->mDNS_busy != m->mDNS_reentrancy+1)
LogMsg("PenalizeDNSServer: Lock not held! mDNS_busy (%ld) mDNS_reentrancy (%ld)", m->mDNS_busy, m->mDNS_reentrancy);
// This should never happen. Whenever we change DNS server, we change the ID on the question and hence
// we should never accept a response after we penalize a DNS server e.g., send two queries, no response,
// penalize DNS server and no new servers to pick for the question and hence qDNSServer is NULL. If we
// receive a response now, the DNS server can be NULL. But we won't because the ID already has been
// changed.
if (!q->qDNSServer)
{
LogMsg("PenalizeDNSServer: ERROR!! Null DNS server for %##s (%s) %d", q->qname.c, DNSTypeName(q->qtype), q->unansweredQueries);
goto end;
}
LogInfo("PenalizeDNSServer: Penalizing DNS server %#a:%d question (%##s) for question %p %##s (%s) SuppressUnusable %d",
&q->qDNSServer->addr, mDNSVal16(q->qDNSServer->port), q->qDNSServer->domain.c, q, q->qname.c, DNSTypeName(q->qtype),
q->SuppressUnusable);
// If strict ordering of unicast servers needs to be preserved, we just lookup
// the next best match server below
//
// If strict ordering is not required which is the default behavior, we penalize the server
// for DNSSERVER_PENALTY_TIME. We may also use additional logic e.g., don't penalize for PTR
// in the future.
if (!StrictUnicastOrdering)
{
LogInfo("PenalizeDNSServer: Strict Unicast Ordering is FALSE");
// We penalize the server so that new queries don't pick this server for DNSSERVER_PENALTY_TIME
// XXX Include other logic here to see if this server should really be penalized
//
if (q->qtype == kDNSType_PTR)
{
LogInfo("PenalizeDNSServer: Not Penalizing PTR question");
}
else
{
LogInfo("PenalizeDNSServer: Penalizing question type %d", q->qtype);
q->qDNSServer->penaltyTime = NonZeroTime(m->timenow + DNSSERVER_PENALTY_TIME);
}
}
else
{
LogInfo("PenalizeDNSServer: Strict Unicast Ordering is TRUE");
}
end:
new = GetServerForQuestion(m, q);
if (new == orig)
{
if (new)
LogMsg("PenalizeDNSServer: ERROR!! GetServerForQuestion returned the same server %#a:%d", &new->addr,
mDNSVal16(new->port));
else
LogMsg("PenalizeDNSServer: ERROR!! GetServerForQuestion returned the same server NULL");
q->ThisQInterval = 0; // Inactivate this question so that we dont bombard the network
}
else
{
// The new DNSServer is set in DNSServerChangeForQuestion
DNSServerChangeForQuestion(m, q, new);
if (new)
{
LogInfo("PenalizeDNSServer: Server for %##s (%s) changed to %#a:%d (%##s)",
q->qname.c, DNSTypeName(q->qtype), &q->qDNSServer->addr, mDNSVal16(q->qDNSServer->port), q->qDNSServer->domain.c);
// We want to try the next server immediately. As the question may already have backed off, reset
// the interval. We do this only the first time when we try all the DNS servers. Once we reached the end of
// list and retrying all the servers again e.g., at least one server failed to respond in the previous try, we
// use the normal backoff which is done in uDNS_CheckCurrentQuestion when we send the packet out.
if (!q->triedAllServersOnce)
{
q->ThisQInterval = InitialQuestionInterval;
q->LastQTime = m->timenow - q->ThisQInterval;
SetNextQueryTime(m, q);
}
}
else
{
// We don't have any more DNS servers for this question. If some server in the list did not return
// any response, we need to keep retrying till we get a response. uDNS_CheckCurrentQuestion handles
// this case.
//
// If all servers responded with a negative response, We need to do two things. First, generate a
// negative response so that applications get a reply. We also need to reinitialize the DNS servers
// so that when the cache expires, we can restart the query.
//
// Negative response may be generated in two ways.
//
// 1. AnswerQuestionForDNSServerChanges (called from DNSServerChangedForQuestion) might find some
// cache entries and answer this question.
// 2. uDNS_CheckCurrentQuestion will create a new cache entry and answer this question
//
// For (1), it might be okay to reinitialize the DNS servers here. But for (2), we can't do it here
// because uDNS_CheckCurrentQuestion will try resending the queries. Hence, to be consistent, we
// defer reintializing the DNS servers up until generating a negative cache response.
//
// Be careful not to touch the ThisQInterval here. For a normal question, when we answer the question
// in AnswerCurrentQuestionWithResourceRecord will set ThisQInterval to MaxQuestionInterval and hence
// the next query will not happen until cache expiry. If it is a long lived question,
// AnswerCurrentQuestionWithResourceRecord will not set it to MaxQuestionInterval. In that case,
// we want the normal backoff to work.
LogInfo("PenalizeDNSServer: Server for %p, %##s (%s) changed to NULL, Interval %d", q, q->qname.c, DNSTypeName(q->qtype), q->ThisQInterval);
}
q->unansweredQueries = 0;
}
}
// ***************************************************************************
#if COMPILER_LIKES_PRAGMA_MARK
#pragma mark - authorization management
#endif
mDNSlocal DomainAuthInfo *GetAuthInfoForName_direct(mDNS *m, const domainname *const name)
{
const domainname *n = name;
while (n->c[0])
{
DomainAuthInfo *ptr;
for (ptr = m->AuthInfoList; ptr; ptr = ptr->next)
if (SameDomainName(&ptr->domain, n))
{
debugf("GetAuthInfoForName %##s Matched %##s Key name %##s", name->c, ptr->domain.c, ptr->keyname.c);
return(ptr);
}
n = (const domainname *)(n->c + 1 + n->c[0]);
}
//LogInfo("GetAuthInfoForName none found for %##s", name->c);
return mDNSNULL;
}
// MUST be called with lock held
mDNSexport DomainAuthInfo *GetAuthInfoForName_internal(mDNS *m, const domainname *const name)
{
DomainAuthInfo **p = &m->AuthInfoList;
if (m->mDNS_busy != m->mDNS_reentrancy+1)
LogMsg("GetAuthInfoForName_internal: Lock not held! mDNS_busy (%ld) mDNS_reentrancy (%ld)", m->mDNS_busy, m->mDNS_reentrancy);
// First purge any dead keys from the list
while (*p)
{
if ((*p)->deltime && m->timenow - (*p)->deltime >= 0 && AutoTunnelUnregistered(*p))
{
DNSQuestion *q;
DomainAuthInfo *info = *p;
LogInfo("GetAuthInfoForName_internal deleting expired key %##s %##s", info->domain.c, info->keyname.c);
*p = info->next; // Cut DomainAuthInfo from list *before* scanning our question list updating AuthInfo pointers
for (q = m->Questions; q; q=q->next)
if (q->AuthInfo == info)
{
q->AuthInfo = GetAuthInfoForName_direct(m, &q->qname);
debugf("GetAuthInfoForName_internal updated q->AuthInfo from %##s to %##s for %##s (%s)",
info->domain.c, q->AuthInfo ? q->AuthInfo->domain.c : mDNSNULL, q->qname.c, DNSTypeName(q->qtype));
}
// Probably not essential, but just to be safe, zero out the secret key data
// so we don't leave it hanging around in memory
// (where it could potentially get exposed via some other bug)
mDNSPlatformMemZero(info, sizeof(*info));
mDNSPlatformMemFree(info);
}
else
p = &(*p)->next;
}
return(GetAuthInfoForName_direct(m, name));
}
mDNSexport DomainAuthInfo *GetAuthInfoForName(mDNS *m, const domainname *const name)
{
DomainAuthInfo *d;
mDNS_Lock(m);
d = GetAuthInfoForName_internal(m, name);
mDNS_Unlock(m);
return(d);
}
// MUST be called with the lock held
mDNSexport mStatus mDNS_SetSecretForDomain(mDNS *m, DomainAuthInfo *info,
const domainname *domain, const domainname *keyname, const char *b64keydata, const domainname *hostname, mDNSIPPort *port, const char *autoTunnelPrefix)
{
DNSQuestion *q;
DomainAuthInfo **p = &m->AuthInfoList;
if (!info || !b64keydata) { LogMsg("mDNS_SetSecretForDomain: ERROR: info %p b64keydata %p", info, b64keydata); return(mStatus_BadParamErr); }
LogInfo("mDNS_SetSecretForDomain: domain %##s key %##s%s%s", domain->c, keyname->c, autoTunnelPrefix ? " prefix " : "", autoTunnelPrefix ? autoTunnelPrefix : "");
info->AutoTunnel = autoTunnelPrefix;
AssignDomainName(&info->domain, domain);
AssignDomainName(&info->keyname, keyname);
if (hostname)
AssignDomainName(&info->hostname, hostname);
else
info->hostname.c[0] = 0;
if (port)
info->port = *port;
else
info->port = zeroIPPort;
mDNS_snprintf(info->b64keydata, sizeof(info->b64keydata), "%s", b64keydata);
if (DNSDigest_ConstructHMACKeyfromBase64(info, b64keydata) < 0)
{
LogMsg("mDNS_SetSecretForDomain: ERROR: Could not convert shared secret from base64: domain %##s key %##s %s", domain->c, keyname->c, mDNS_LoggingEnabled ? b64keydata : "");
return(mStatus_BadParamErr);
}
// Don't clear deltime until after we've ascertained that b64keydata is valid
info->deltime = 0;
while (*p && (*p) != info) p=&(*p)->next;
if (*p) {LogInfo("mDNS_SetSecretForDomain: Domain %##s Already in list", (*p)->domain.c); return(mStatus_AlreadyRegistered);}
// Caution: Only zero AutoTunnelHostRecord.namestorage and AutoTunnelNAT.clientContext AFTER we've determined that this is a NEW DomainAuthInfo
// being added to the list. Otherwise we risk smashing our AutoTunnel host records and NATOperation that are already active and in use.
info->AutoTunnelHostRecord.resrec.RecordType = kDNSRecordTypeUnregistered;
info->AutoTunnelHostRecord.namestorage.c[0] = 0;
info->AutoTunnelTarget .resrec.RecordType = kDNSRecordTypeUnregistered;
info->AutoTunnelDeviceInfo.resrec.RecordType = kDNSRecordTypeUnregistered;
info->AutoTunnelService .resrec.RecordType = kDNSRecordTypeUnregistered;
info->AutoTunnel6Record .resrec.RecordType = kDNSRecordTypeUnregistered;
info->AutoTunnelNAT.clientContext = mDNSNULL;
info->next = mDNSNULL;
*p = info;
// Check to see if adding this new DomainAuthInfo has changed the credentials for any of our questions
for (q = m->Questions; q; q=q->next)
{
DomainAuthInfo *newinfo = GetAuthInfoForQuestion(m, q);
if (q->AuthInfo != newinfo)
{
debugf("mDNS_SetSecretForDomain updating q->AuthInfo from %##s to %##s for %##s (%s)",
q->AuthInfo ? q->AuthInfo->domain.c : mDNSNULL,
newinfo ? newinfo ->domain.c : mDNSNULL, q->qname.c, DNSTypeName(q->qtype));
q->AuthInfo = newinfo;
}
}
return(mStatus_NoError);
}
// ***************************************************************************
#if COMPILER_LIKES_PRAGMA_MARK
#pragma mark -
#pragma mark - NAT Traversal
#endif
mDNSlocal mStatus uDNS_SendNATMsg(mDNS *m, NATTraversalInfo *info)
{
mStatus err = mStatus_NoError;
// send msg if we have a router and it is a private address
if (!mDNSIPv4AddressIsZero(m->Router.ip.v4) && mDNSv4AddrIsRFC1918(&m->Router.ip.v4))
{
union { NATAddrRequest NATAddrReq; NATPortMapRequest NATPortReq; } u = { { NATMAP_VERS, NATOp_AddrRequest } } ;
const mDNSu8 *end = (mDNSu8 *)&u + sizeof(NATAddrRequest);
if (info) // For NATOp_MapUDP and NATOp_MapTCP, fill in additional fields
{
mDNSu8 *p = (mDNSu8 *)&u.NATPortReq.NATReq_lease;
u.NATPortReq.opcode = info->Protocol;
u.NATPortReq.unused = zeroID;
u.NATPortReq.intport = info->IntPort;
u.NATPortReq.extport = info->RequestedPort;
p[0] = (mDNSu8)((info->NATLease >> 24) & 0xFF);
p[1] = (mDNSu8)((info->NATLease >> 16) & 0xFF);
p[2] = (mDNSu8)((info->NATLease >> 8) & 0xFF);
p[3] = (mDNSu8)( info->NATLease & 0xFF);
end = (mDNSu8 *)&u + sizeof(NATPortMapRequest);
}
err = mDNSPlatformSendUDP(m, (mDNSu8 *)&u, end, 0, mDNSNULL, &m->Router, NATPMPPort);
#ifdef _LEGACY_NAT_TRAVERSAL_
if (mDNSIPPortIsZero(m->UPnPRouterPort) || mDNSIPPortIsZero(m->UPnPSOAPPort)) LNT_SendDiscoveryMsg(m);
else if (info) err = LNT_MapPort(m, info);
else err = LNT_GetExternalAddress(m);
#endif // _LEGACY_NAT_TRAVERSAL_
}
return(err);
}
mDNSexport void RecreateNATMappings(mDNS *const m)
{
NATTraversalInfo *n;
for (n = m->NATTraversals; n; n=n->next)
{
n->ExpiryTime = 0; // Mark this mapping as expired
n->retryInterval = NATMAP_INIT_RETRY;
n->retryPortMap = m->timenow;
#ifdef _LEGACY_NAT_TRAVERSAL_
if (n->tcpInfo.sock) { mDNSPlatformTCPCloseConnection(n->tcpInfo.sock); n->tcpInfo.sock = mDNSNULL; }
#endif // _LEGACY_NAT_TRAVERSAL_
}
m->NextScheduledNATOp = m->timenow; // Need to send packets immediately
}
mDNSexport void natTraversalHandleAddressReply(mDNS *const m, mDNSu16 err, mDNSv4Addr ExtAddr)
{
static mDNSu16 last_err = 0;
if (err)
{
if (err != last_err) LogMsg("Error getting external address %d", err);
ExtAddr = zerov4Addr;
}
else
{
LogInfo("Received external IP address %.4a from NAT", &ExtAddr);
if (mDNSv4AddrIsRFC1918(&ExtAddr))
LogMsg("Double NAT (external NAT gateway address %.4a is also a private RFC 1918 address)", &ExtAddr);
if (mDNSIPv4AddressIsZero(ExtAddr))
err = NATErr_NetFail; // fake error to handle routers that pathologically report success with the zero address
}
if (!mDNSSameIPv4Address(m->ExternalAddress, ExtAddr))
{
m->ExternalAddress = ExtAddr;
RecreateNATMappings(m); // Also sets NextScheduledNATOp for us
}
if (!err) // Success, back-off to maximum interval
m->retryIntervalGetAddr = NATMAP_MAX_RETRY_INTERVAL;
else if (!last_err) // Failure after success, retry quickly (then back-off exponentially)
m->retryIntervalGetAddr = NATMAP_INIT_RETRY;
// else back-off normally in case of pathological failures
m->retryGetAddr = m->timenow + m->retryIntervalGetAddr;
if (m->NextScheduledNATOp - m->retryIntervalGetAddr > 0)
m->NextScheduledNATOp = m->retryIntervalGetAddr;
last_err = err;
}
// Both places that call NATSetNextRenewalTime() update m->NextScheduledNATOp correctly afterwards
mDNSlocal void NATSetNextRenewalTime(mDNS *const m, NATTraversalInfo *n)
{
n->retryInterval = (n->ExpiryTime - m->timenow)/2;
if (n->retryInterval < NATMAP_MIN_RETRY_INTERVAL) // Min retry interval is 2 seconds
n->retryInterval = NATMAP_MIN_RETRY_INTERVAL;
n->retryPortMap = m->timenow + n->retryInterval;
}
// Note: When called from handleLNTPortMappingResponse() only pkt->err, pkt->extport and pkt->NATRep_lease fields are filled in
mDNSexport void natTraversalHandlePortMapReply(mDNS *const m, NATTraversalInfo *n, const mDNSInterfaceID InterfaceID, mDNSu16 err, mDNSIPPort extport, mDNSu32 lease)
{
const char *prot = n->Protocol == NATOp_MapUDP ? "UDP" : n->Protocol == NATOp_MapTCP ? "TCP" : "?";
(void)prot;
n->NewResult = err;
if (err || lease == 0 || mDNSIPPortIsZero(extport))
{
LogInfo("natTraversalHandlePortMapReply: %p Response %s Port %5d External Port %5d lease %d error %d",
n, prot, mDNSVal16(n->IntPort), mDNSVal16(extport), lease, err);
n->retryInterval = NATMAP_MAX_RETRY_INTERVAL;
n->retryPortMap = m->timenow + NATMAP_MAX_RETRY_INTERVAL;
// No need to set m->NextScheduledNATOp here, since we're only ever extending the m->retryPortMap time
if (err == NATErr_Refused) n->NewResult = mStatus_NATPortMappingDisabled;
else if (err > NATErr_None && err <= NATErr_Opcode) n->NewResult = mStatus_NATPortMappingUnsupported;
}
else
{
if (lease > 999999999UL / mDNSPlatformOneSecond)
lease = 999999999UL / mDNSPlatformOneSecond;
n->ExpiryTime = NonZeroTime(m->timenow + lease * mDNSPlatformOneSecond);
if (!mDNSSameIPPort(n->RequestedPort, extport))
LogInfo("natTraversalHandlePortMapReply: %p Response %s Port %5d External Port %5d changed to %5d",
n, prot, mDNSVal16(n->IntPort), mDNSVal16(n->RequestedPort), mDNSVal16(extport));
n->InterfaceID = InterfaceID;
n->RequestedPort = extport;
LogInfo("natTraversalHandlePortMapReply: %p Response %s Port %5d External Port %5d lease %d",
n, prot, mDNSVal16(n->IntPort), mDNSVal16(extport), lease);
NATSetNextRenewalTime(m, n); // Got our port mapping; now set timer to renew it at halfway point
m->NextScheduledNATOp = m->timenow; // May need to invoke client callback immediately
}
}
// Must be called with the mDNS_Lock held
mDNSexport mStatus mDNS_StartNATOperation_internal(mDNS *const m, NATTraversalInfo *traversal)
{
NATTraversalInfo **n;
LogInfo("mDNS_StartNATOperation_internal %p Protocol %d IntPort %d RequestedPort %d NATLease %d", traversal,
traversal->Protocol, mDNSVal16(traversal->IntPort), mDNSVal16(traversal->RequestedPort), traversal->NATLease);
// Note: It important that new traversal requests are appended at the *end* of the list, not prepended at the start
for (n = &m->NATTraversals; *n; n=&(*n)->next)
{
if (traversal == *n)
{
LogMsg("Error! Tried to add a NAT traversal that's already in the active list: request %p Prot %d Int %d TTL %d",
traversal, traversal->Protocol, mDNSVal16(traversal->IntPort), traversal->NATLease);
#if ForceAlerts
*(long*)0 = 0;
#endif
return(mStatus_AlreadyRegistered);
}
if (traversal->Protocol && traversal->Protocol == (*n)->Protocol && mDNSSameIPPort(traversal->IntPort, (*n)->IntPort) &&
!mDNSSameIPPort(traversal->IntPort, SSHPort))
LogMsg("Warning: Created port mapping request %p Prot %d Int %d TTL %d "
"duplicates existing port mapping request %p Prot %d Int %d TTL %d",
traversal, traversal->Protocol, mDNSVal16(traversal->IntPort), traversal->NATLease,
*n, (*n) ->Protocol, mDNSVal16((*n) ->IntPort), (*n) ->NATLease);
}
// Initialize necessary fields
traversal->next = mDNSNULL;
traversal->ExpiryTime = 0;
traversal->retryInterval = NATMAP_INIT_RETRY;
traversal->retryPortMap = m->timenow;
traversal->NewResult = mStatus_NoError;
traversal->ExternalAddress = onesIPv4Addr;
traversal->ExternalPort = zeroIPPort;
traversal->Lifetime = 0;
traversal->Result = mStatus_NoError;
// set default lease if necessary
if (!traversal->NATLease) traversal->NATLease = NATMAP_DEFAULT_LEASE;
#ifdef _LEGACY_NAT_TRAVERSAL_
mDNSPlatformMemZero(&traversal->tcpInfo, sizeof(traversal->tcpInfo));
#endif // _LEGACY_NAT_TRAVERSAL_
if (!m->NATTraversals) // If this is our first NAT request, kick off an address request too
{
m->retryGetAddr = m->timenow;
m->retryIntervalGetAddr = NATMAP_INIT_RETRY;
}
m->NextScheduledNATOp = m->timenow; // This will always trigger sending the packet ASAP, and generate client callback if necessary
*n = traversal; // Append new NATTraversalInfo to the end of our list
return(mStatus_NoError);
}
// Must be called with the mDNS_Lock held
mDNSexport mStatus mDNS_StopNATOperation_internal(mDNS *m, NATTraversalInfo *traversal)
{
mDNSBool unmap = mDNStrue;
NATTraversalInfo *p;
NATTraversalInfo **ptr = &m->NATTraversals;
while (*ptr && *ptr != traversal) ptr=&(*ptr)->next;
if (*ptr) *ptr = (*ptr)->next; // If we found it, cut this NATTraversalInfo struct from our list
else
{
LogMsg("mDNS_StopNATOperation_internal: NATTraversalInfo %p not found in list", traversal);
return(mStatus_BadReferenceErr);
}
LogInfo("mDNS_StopNATOperation_internal %p %d %d %d %d", traversal,
traversal->Protocol, mDNSVal16(traversal->IntPort), mDNSVal16(traversal->RequestedPort), traversal->NATLease);
if (m->CurrentNATTraversal == traversal)
m->CurrentNATTraversal = m->CurrentNATTraversal->next;
if (traversal->Protocol)
for (p = m->NATTraversals; p; p=p->next)
if (traversal->Protocol == p->Protocol && mDNSSameIPPort(traversal->IntPort, p->IntPort))
{
if (!mDNSSameIPPort(traversal->IntPort, SSHPort))
LogMsg("Warning: Removed port mapping request %p Prot %d Int %d TTL %d "
"duplicates existing port mapping request %p Prot %d Int %d TTL %d",
traversal, traversal->Protocol, mDNSVal16(traversal->IntPort), traversal->NATLease,
p, p ->Protocol, mDNSVal16(p ->IntPort), p ->NATLease);
unmap = mDNSfalse;
}
if (traversal->ExpiryTime && unmap)
{
traversal->NATLease = 0;
traversal->retryInterval = 0;
uDNS_SendNATMsg(m, traversal);
}
// 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
#ifdef _LEGACY_NAT_TRAVERSAL_
{
mStatus err = LNT_UnmapPort(m, traversal);
if (err) LogMsg("Legacy NAT Traversal - unmap request failed with error %d", err);
}
#endif // _LEGACY_NAT_TRAVERSAL_
return(mStatus_NoError);
}
mDNSexport mStatus mDNS_StartNATOperation(mDNS *const m, NATTraversalInfo *traversal)
{
mStatus status;
mDNS_Lock(m);
status = mDNS_StartNATOperation_internal(m, traversal);
mDNS_Unlock(m);
return(status);
}
mDNSexport mStatus mDNS_StopNATOperation(mDNS *const m, NATTraversalInfo *traversal)
{
mStatus status;
mDNS_Lock(m);
status = mDNS_StopNATOperation_internal(m, traversal);
mDNS_Unlock(m);
return(status);
}
// ***************************************************************************
#if COMPILER_LIKES_PRAGMA_MARK
#pragma mark -
#pragma mark - Long-Lived Queries
#endif
// Lock must be held -- otherwise m->timenow is undefined
mDNSlocal void StartLLQPolling(mDNS *const m, DNSQuestion *q)
{
debugf("StartLLQPolling: %##s", q->qname.c);
q->state = LLQ_Poll;
q->ThisQInterval = INIT_UCAST_POLL_INTERVAL;
// We want to send our poll query ASAP, but the "+ 1" is because if we set the time to now,
// we risk causing spurious "SendQueries didn't send all its queries" log messages
q->LastQTime = m->timenow - q->ThisQInterval + 1;
SetNextQueryTime(m, q);
#if APPLE_OSX_mDNSResponder
UpdateAutoTunnelDomainStatuses(m);
#endif
}
mDNSlocal mDNSu8 *putLLQ(DNSMessage *const msg, mDNSu8 *ptr, const DNSQuestion *const question, const LLQOptData *const data)
{
AuthRecord rr;
ResourceRecord *opt = &rr.resrec;
rdataOPT *optRD;
//!!!KRS when we implement multiple llqs per message, we'll need to memmove anything past the question section
ptr = putQuestion(msg, ptr, msg->data + AbsoluteMaxDNSMessageData, &question->qname, question->qtype, question->qclass);
if (!ptr) { LogMsg("ERROR: putLLQ - putQuestion"); return mDNSNULL; }
// locate OptRR if it exists, set pointer to end
// !!!KRS implement me
// format opt rr (fields not specified are zero-valued)
mDNS_SetupResourceRecord(&rr, mDNSNULL, mDNSInterface_Any, kDNSType_OPT, kStandardTTL, kDNSRecordTypeKnownUnique, AuthRecordAny, mDNSNULL, mDNSNULL);
opt->rrclass = NormalMaxDNSMessageData;
opt->rdlength = sizeof(rdataOPT); // One option in this OPT record
opt->rdestimate = sizeof(rdataOPT);
optRD = &rr.resrec.rdata->u.opt[0];
optRD->opt = kDNSOpt_LLQ;
optRD->u.llq = *data;
ptr = PutResourceRecordTTLJumbo(msg, ptr, &msg->h.numAdditionals, opt, 0);
if (!ptr) { LogMsg("ERROR: putLLQ - PutResourceRecordTTLJumbo"); return mDNSNULL; }
return ptr;
}
// Normally we'd just request event packets be sent directly to m->LLQNAT.ExternalPort, except...
// with LLQs over TLS/TCP we're doing a weird thing where instead of requesting packets be sent to ExternalAddress:ExternalPort
// we're requesting that packets be sent to ExternalPort, but at the source address of our outgoing TCP connection.
// Normally, after going through the NAT gateway, the source address of our outgoing TCP connection is the same as ExternalAddress,
// so this is fine, except when the TCP connection ends up going over a VPN tunnel instead.
// 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
// LLQ server to send events to us directly at port 5353 on that address, instead of at our mapped external NAT port.
mDNSlocal mDNSu16 GetLLQEventPort(const mDNS *const m, const mDNSAddr *const dst)
{
mDNSAddr src;
mDNSPlatformSourceAddrForDest(&src, dst);
//LogMsg("GetLLQEventPort: src %#a for dst %#a (%d)", &src, dst, mDNSv4AddrIsRFC1918(&src.ip.v4) ? mDNSVal16(m->LLQNAT.ExternalPort) : 0);
return(mDNSv4AddrIsRFC1918(&src.ip.v4) ? mDNSVal16(m->LLQNAT.ExternalPort) : mDNSVal16(MulticastDNSPort));
}
// Normally called with llq set.
// May be called with llq NULL, when retransmitting a lost Challenge Response
mDNSlocal void sendChallengeResponse(mDNS *const m, DNSQuestion *const q, const LLQOptData *llq)
{
mDNSu8 *responsePtr = m->omsg.data;
LLQOptData llqBuf;
if (q->tcp) { LogMsg("sendChallengeResponse: ERROR!!: question %##s (%s) tcp non-NULL", q->qname.c, DNSTypeName(q->qtype)); return; }
if (PrivateQuery(q)) { LogMsg("sendChallengeResponse: ERROR!!: Private Query %##s (%s)", q->qname.c, DNSTypeName(q->qtype)); return; }
if (q->ntries++ == kLLQ_MAX_TRIES)
{
LogMsg("sendChallengeResponse: %d failed attempts for LLQ %##s", kLLQ_MAX_TRIES, q->qname.c);
StartLLQPolling(m,q);
return;
}
if (!llq) // Retransmission: need to make a new LLQOptData
{
llqBuf.vers = kLLQ_Vers;
llqBuf.llqOp = kLLQOp_Setup;
llqBuf.err = LLQErr_NoError; // Don't need to tell server UDP notification port when sending over UDP
llqBuf.id = q->id;
llqBuf.llqlease = q->ReqLease;
llq = &llqBuf;
}
q->LastQTime = m->timenow;
q->ThisQInterval = q->tcp ? 0 : (kLLQ_INIT_RESEND * q->ntries * mDNSPlatformOneSecond); // If using TCP, don't need to retransmit
SetNextQueryTime(m, q);
// To simulate loss of challenge response packet, uncomment line below
//if (q->ntries == 1) return;
InitializeDNSMessage(&m->omsg.h, q->TargetQID, uQueryFlags);
responsePtr = putLLQ(&m->omsg, responsePtr, q, llq);
if (responsePtr)
{
mStatus err = mDNSSendDNSMessage(m, &m->omsg, responsePtr, mDNSInterface_Any, q->LocalSocket, &q->servAddr, q->servPort, mDNSNULL, mDNSNULL);
if (err) { LogMsg("sendChallengeResponse: mDNSSendDNSMessage%s failed: %d", q->tcp ? " (TCP)" : "", err); }
}
else StartLLQPolling(m,q);
}
mDNSlocal void SetLLQTimer(mDNS *const m, DNSQuestion *const q, const LLQOptData *const llq)
{
mDNSs32 lease = (mDNSs32)llq->llqlease * mDNSPlatformOneSecond;
q->ReqLease = llq->llqlease;
q->LastQTime = m->timenow;
q->expire = m->timenow + lease;
q->ThisQInterval = lease/2 + mDNSRandom(lease/10);
debugf("SetLLQTimer setting %##s (%s) to %d %d", q->qname.c, DNSTypeName(q->qtype), lease/mDNSPlatformOneSecond, q->ThisQInterval/mDNSPlatformOneSecond);
SetNextQueryTime(m, q);
}
mDNSlocal void recvSetupResponse(mDNS *const m, mDNSu8 rcode, DNSQuestion *const q, const LLQOptData *const llq)
{
if (rcode && rcode != kDNSFlag1_RC_NXDomain)
{ LogMsg("ERROR: recvSetupResponse %##s (%s) - rcode && rcode != kDNSFlag1_RC_NXDomain", q->qname.c, DNSTypeName(q->qtype)); return; }
if (llq->llqOp != kLLQOp_Setup)
{ LogMsg("ERROR: recvSetupResponse %##s (%s) - bad op %d", q->qname.c, DNSTypeName(q->qtype), llq->llqOp); return; }
if (llq->vers != kLLQ_Vers)
{ LogMsg("ERROR: recvSetupResponse %##s (%s) - bad vers %d", q->qname.c, DNSTypeName(q->qtype), llq->vers); return; }
if (q->state == LLQ_InitialRequest)
{
//LogInfo("Got LLQ_InitialRequest");
if (llq->err) { LogMsg("recvSetupResponse - received llq->err %d from server", llq->err); StartLLQPolling(m,q); return; }
if (q->ReqLease != llq->llqlease)
debugf("recvSetupResponse: requested lease %lu, granted lease %lu", q->ReqLease, llq->llqlease);
// cache expiration in case we go to sleep before finishing setup
q->ReqLease = llq->llqlease;
q->expire = m->timenow + ((mDNSs32)llq->llqlease * mDNSPlatformOneSecond);
// update state
q->state = LLQ_SecondaryRequest;
q->id = llq->id;
q->ntries = 0; // first attempt to send response
sendChallengeResponse(m, q, llq);
}
else if (q->state == LLQ_SecondaryRequest)
{
//LogInfo("Got LLQ_SecondaryRequest");
// Fix this immediately if not sooner. Copy the id from the LLQOptData into our DNSQuestion struct. This is only
// an issue for private LLQs, because we skip parts 2 and 3 of the handshake. This is related to a bigger
// problem of the current implementation of TCP LLQ setup: we're not handling state transitions correctly
// if the server sends back SERVFULL or STATIC.
if (PrivateQuery(q))
{
LogInfo("Private LLQ_SecondaryRequest; copying id %08X%08X", llq->id.l[0], llq->id.l[1]);
q->id = llq->id;
}
if (llq->err) { LogMsg("ERROR: recvSetupResponse %##s (%s) code %d from server", q->qname.c, DNSTypeName(q->qtype), llq->err); StartLLQPolling(m,q); return; }
if (!mDNSSameOpaque64(&q->id, &llq->id))
{ LogMsg("recvSetupResponse - ID changed. discarding"); return; } // this can happen rarely (on packet loss + reordering)
q->state = LLQ_Established;
q->ntries = 0;
SetLLQTimer(m, q, llq);
#if APPLE_OSX_mDNSResponder
UpdateAutoTunnelDomainStatuses(m);
#endif
}
}
mDNSexport uDNS_LLQType uDNS_recvLLQResponse(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *const end,
const mDNSAddr *const srcaddr, const mDNSIPPort srcport, DNSQuestion **matchQuestion)
{
DNSQuestion pktQ, *q;
if (msg->h.numQuestions && getQuestion(msg, msg->data, end, 0, &pktQ))
{
const rdataOPT *opt = GetLLQOptData(m, msg, end);
for (q = m->Questions; q; q = q->next)
{
if (!mDNSOpaque16IsZero(q->TargetQID) && q->LongLived && q->qtype == pktQ.qtype && q->qnamehash == pktQ.qnamehash && SameDomainName(&q->qname, &pktQ.qname))
{
debugf("uDNS_recvLLQResponse found %##s (%s) %d %#a %#a %X %X %X %X %d",
q->qname.c, DNSTypeName(q->qtype), q->state, srcaddr, &q->servAddr,
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);
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));
if (q->state == LLQ_Poll && mDNSSameOpaque16(msg->h.id, q->TargetQID))
{
m->rec.r.resrec.RecordType = 0; // Clear RecordType to show we're not still using it
// Don't reset the state to IntialRequest as we may write that to the dynamic store
// and PrefPane might wrongly think that we are "Starting" instead of "Polling". If
// we are in polling state because of NAT-PMP disabled or DoubleNAT, next LLQNATCallback
// would kick us back to LLQInitialRequest. So, resetting the state here may not be useful.
//
// If we have a good NAT (neither NAT-PMP disabled nor Double-NAT), then we should not be
// possibly in polling state. To be safe, we want to retry from the start in that case
// as there may not be another LLQNATCallback
//
// NOTE: We can be in polling state if we cannot resolve the SOA record i.e, servAddr is set to
// all ones. In that case, we would set it in LLQ_InitialRequest as it overrides the NAT-PMP or
// Double-NAT state.
if (!mDNSAddressIsOnes(&q->servAddr) && !mDNSIPPortIsZero(m->LLQNAT.ExternalPort) &&
!m->LLQNAT.Result)
{
debugf("uDNS_recvLLQResponse got poll response; moving to LLQ_InitialRequest for %##s (%s)", q->qname.c, DNSTypeName(q->qtype));
q->state = LLQ_InitialRequest;
}
q->servPort = zeroIPPort; // Clear servPort so that startLLQHandshake will retry the GetZoneData processing
q->ThisQInterval = LLQ_POLL_INTERVAL + mDNSRandom(LLQ_POLL_INTERVAL/10); // Retry LLQ setup in approx 15 minutes
q->LastQTime = m->timenow;
SetNextQueryTime(m, q);
*matchQuestion = q;
return uDNS_LLQ_Entire; // uDNS_LLQ_Entire means flush stale records; assume a large effective TTL
}
// 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
else if (opt && q->state == LLQ_Established && opt->u.llq.llqOp == kLLQOp_Event && mDNSSameOpaque64(&opt->u.llq.id, &q->id))
{
mDNSu8 *ackEnd;
//debugf("Sending LLQ ack for %##s (%s)", q->qname.c, DNSTypeName(q->qtype));
InitializeDNSMessage(&m->omsg.h, msg->h.id, ResponseFlags);
ackEnd = putLLQ(&m->omsg, m->omsg.data, q, &opt->u.llq);
if (ackEnd) mDNSSendDNSMessage(m, &m->omsg, ackEnd, mDNSInterface_Any, q->LocalSocket, srcaddr, srcport, mDNSNULL, mDNSNULL);
m->rec.r.resrec.RecordType = 0; // Clear RecordType to show we're not still using it
debugf("uDNS_LLQ_Events: q->state == LLQ_Established msg->h.id %d q->TargetQID %d", mDNSVal16(msg->h.id), mDNSVal16(q->TargetQID));
*matchQuestion = q;
return uDNS_LLQ_Events;
}
if (opt && mDNSSameOpaque16(msg->h.id, q->TargetQID))
{
if (q->state == LLQ_Established && opt->u.llq.llqOp == kLLQOp_Refresh && mDNSSameOpaque64(&opt->u.llq.id, &q->id) && msg->h.numAdditionals && !msg->h.numAnswers)
{
if (opt->u.llq.err != LLQErr_NoError) LogMsg("recvRefreshReply: received error %d from server", opt->u.llq.err);
else
{
//LogInfo("Received refresh confirmation ntries %d for %##s (%s)", q->ntries, q->qname.c, DNSTypeName(q->qtype));
// If we're waiting to go to sleep, then this LLQ deletion may have been the thing
// we were waiting for, so schedule another check to see if we can sleep now.
if (opt->u.llq.llqlease == 0 && m->SleepLimit) m->NextScheduledSPRetry = m->timenow;
GrantCacheExtensions(m, q, opt->u.llq.llqlease);
SetLLQTimer(m, q, &opt->u.llq);
q->ntries = 0;
}
m->rec.r.resrec.RecordType = 0; // Clear RecordType to show we're not still using it
*matchQuestion = q;
return uDNS_LLQ_Ignore;
}
if (q->state < LLQ_Established && mDNSSameAddress(srcaddr, &q->servAddr))
{
LLQ_State oldstate = q->state;
recvSetupResponse(m, msg->h.flags.b[1] & kDNSFlag1_RC_Mask, q, &opt->u.llq);
m->rec.r.resrec.RecordType = 0; // Clear RecordType to show we're not still using it
// We have a protocol anomaly here in the LLQ definition.
// Both the challenge packet from the server and the ack+answers packet have opt->u.llq.llqOp == kLLQOp_Setup.
// However, we need to treat them differently:
// The challenge packet has no answers in it, and tells us nothing about whether our cache entries
// are still valid, so this packet should not cause us to do anything that messes with our cache.
// The ack+answers packet gives us the whole truth, so we should handle it by updating our cache
// to match the answers in the packet, and only the answers in the packet.
*matchQuestion = q;
return (oldstate == LLQ_SecondaryRequest ? uDNS_LLQ_Entire : uDNS_LLQ_Ignore);
}
}
}
}
m->rec.r.resrec.RecordType = 0; // Clear RecordType to show we're not still using it
}
*matchQuestion = mDNSNULL;
return uDNS_LLQ_Not;
}
// Stub definition of TCPSocket_struct so we can access flags field. (Rest of TCPSocket_struct is platform-dependent.)
struct TCPSocket_struct { TCPSocketFlags flags; /* ... */ };
// tcpCallback is called to handle events (e.g. connection opening and data reception) on TCP connections for
// Private DNS operations -- private queries, private LLQs, private record updates and private service updates
mDNSlocal void tcpCallback(TCPSocket *sock, void *context, mDNSBool ConnectionEstablished, mStatus err)
{
tcpInfo_t *tcpInfo = (tcpInfo_t *)context;
mDNSBool closed = mDNSfalse;
mDNS *m = tcpInfo->m;
DNSQuestion *const q = tcpInfo->question;
tcpInfo_t **backpointer =
q ? &q ->tcp :
tcpInfo->rr ? &tcpInfo->rr ->tcp : mDNSNULL;
if (backpointer && *backpointer != tcpInfo)
LogMsg("tcpCallback: %d backpointer %p incorrect tcpInfo %p question %p rr %p",
mDNSPlatformTCPGetFD(tcpInfo->sock), *backpointer, tcpInfo, q, tcpInfo->rr);
if (err) goto exit;
if (ConnectionEstablished)
{
mDNSu8 *end = ((mDNSu8*) &tcpInfo->request) + tcpInfo->requestLen;
DomainAuthInfo *AuthInfo;
// Defensive coding for <rdar://problem/5546824> Crash in mDNSResponder at GetAuthInfoForName_internal + 366
// 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
if (tcpInfo->rr && tcpInfo->rr->resrec.name != &tcpInfo->rr->namestorage)
LogMsg("tcpCallback: ERROR: tcpInfo->rr->resrec.name %p != &tcpInfo->rr->namestorage %p",
tcpInfo->rr->resrec.name, &tcpInfo->rr->namestorage);
if (tcpInfo->rr && tcpInfo->rr-> resrec.name != &tcpInfo->rr-> namestorage) return;
AuthInfo = tcpInfo->rr ? GetAuthInfoForName(m, tcpInfo->rr->resrec.name) : mDNSNULL;
// connection is established - send the message
if (q && q->LongLived && q->state == LLQ_Established)
{
// Lease renewal over TCP, resulting from opening a TCP connection in sendLLQRefresh
end = ((mDNSu8*) &tcpInfo->request) + tcpInfo->requestLen;
}
else if (q && q->LongLived && q->state != LLQ_Poll && !mDNSIPPortIsZero(m->LLQNAT.ExternalPort) && !mDNSIPPortIsZero(q->servPort))
{
// Notes:
// If we have a NAT port mapping, ExternalPort is the external port
// If we have a routable address so we don't need a port mapping, ExternalPort is the same as our own internal port
// If we need a NAT port mapping but can't get one, then ExternalPort is zero
LLQOptData llqData; // set llq rdata
llqData.vers = kLLQ_Vers;
llqData.llqOp = kLLQOp_Setup;
llqData.err = GetLLQEventPort(m, &tcpInfo->Addr); // We're using TCP; tell server what UDP port to send notifications to
LogInfo("tcpCallback: eventPort %d", llqData.err);
llqData.id = zeroOpaque64;
llqData.llqlease = kLLQ_DefLease;
InitializeDNSMessage(&tcpInfo->request.h, q->TargetQID, uQueryFlags);
end = putLLQ(&tcpInfo->request, tcpInfo->request.data, q, &llqData);
if (!end) { LogMsg("ERROR: tcpCallback - putLLQ"); err = mStatus_UnknownErr; goto exit; }
AuthInfo = q->AuthInfo; // Need to add TSIG to this message
q->ntries = 0; // Reset ntries so that tcp/tls connection failures don't affect sendChallengeResponse failures
}
else if (q)
{
// LLQ Polling mode or non-LLQ uDNS over TCP
InitializeDNSMessage(&tcpInfo->request.h, q->TargetQID, uQueryFlags);
end = putQuestion(&tcpInfo->request, tcpInfo->request.data, tcpInfo->request.data + AbsoluteMaxDNSMessageData, &q->qname, q->qtype, q->qclass);
AuthInfo = q->AuthInfo; // Need to add TSIG to this message
}
err = mDNSSendDNSMessage(m, &tcpInfo->request, end, mDNSInterface_Any, mDNSNULL, &tcpInfo->Addr, tcpInfo->Port, sock, AuthInfo);
if (err) { debugf("ERROR: tcpCallback: mDNSSendDNSMessage - %d", err); err = mStatus_UnknownErr; goto exit; }
// Record time we sent this question
if (q)
{
mDNS_Lock(m);
q->LastQTime = m->timenow;
if (q->ThisQInterval < (256 * mDNSPlatformOneSecond)) // Now we have a TCP connection open, make sure we wait at least 256 seconds before retrying
q->ThisQInterval = (256 * mDNSPlatformOneSecond);
SetNextQueryTime(m, q);
mDNS_Unlock(m);
}
}
else
{
long n;
if (tcpInfo->nread < 2) // First read the two-byte length preceeding the DNS message
{
mDNSu8 *lenptr = (mDNSu8 *)&tcpInfo->replylen;
n = mDNSPlatformReadTCP(sock, lenptr + tcpInfo->nread, 2 - tcpInfo->nread, &closed);
if (n < 0)
{
LogMsg("ERROR: tcpCallback - attempt to read message length failed (%d)", n);
err = mStatus_ConnFailed;
goto exit;
}
else if (closed)
{
// It's perfectly fine for this socket to close after the first reply. The server might
// be sending gratuitous replies using UDP and doesn't have a need to leave the TCP socket open.
// We'll only log this event if we've never received a reply before.
// BIND 9 appears to close an idle connection after 30 seconds.
if (tcpInfo->numReplies == 0)
{
LogMsg("ERROR: socket closed prematurely tcpInfo->nread = %d", tcpInfo->nread);
err = mStatus_ConnFailed;
goto exit;
}
else
{
// Note that we may not be doing the best thing if an error occurs after we've sent a second request
// over this tcp connection. That is, we only track whether we've received at least one response
// which may have been to a previous request sent over this tcp connection.
if (backpointer) *backpointer = mDNSNULL; // Clear client backpointer FIRST so we don't risk double-disposing our tcpInfo_t
DisposeTCPConn(tcpInfo);
return;
}
}
tcpInfo->nread += n;
if (tcpInfo->nread < 2) goto exit;
tcpInfo->replylen = (mDNSu16)((mDNSu16)lenptr[0] << 8 | lenptr[1]);
if (tcpInfo->replylen < sizeof(DNSMessageHeader))
{ LogMsg("ERROR: tcpCallback - length too short (%d bytes)", tcpInfo->replylen); err = mStatus_UnknownErr; goto exit; }
tcpInfo->reply = mDNSPlatformMemAllocate(tcpInfo->replylen);
if (!tcpInfo->reply) { LogMsg("ERROR: tcpCallback - malloc failed"); err = mStatus_NoMemoryErr; goto exit; }
}
n = mDNSPlatformReadTCP(sock, ((char *)tcpInfo->reply) + (tcpInfo->nread - 2), tcpInfo->replylen - (tcpInfo->nread - 2), &closed);
if (n < 0)
{
LogMsg("ERROR: tcpCallback - read returned %d", n);
err = mStatus_ConnFailed;
goto exit;
}
else if (closed)
{
if (tcpInfo->numReplies == 0)
{
LogMsg("ERROR: socket closed prematurely tcpInfo->nread = %d", tcpInfo->nread);
err = mStatus_ConnFailed;
goto exit;
}
else
{
// Note that we may not be doing the best thing if an error occurs after we've sent a second request
// over this tcp connection. That is, we only track whether we've received at least one response
// which may have been to a previous request sent over this tcp connection.
if (backpointer) *backpointer = mDNSNULL; // Clear client backpointer FIRST so we don't risk double-disposing our tcpInfo_t
DisposeTCPConn(tcpInfo);
return;
}
}
tcpInfo->nread += n;
if ((tcpInfo->nread - 2) == tcpInfo->replylen)
{
mDNSBool tls;
DNSMessage *reply = tcpInfo->reply;
mDNSu8 *end = (mDNSu8 *)tcpInfo->reply + tcpInfo->replylen;
mDNSAddr Addr = tcpInfo->Addr;
mDNSIPPort Port = tcpInfo->Port;
mDNSIPPort srcPort = zeroIPPort;
tcpInfo->numReplies++;
tcpInfo->reply = mDNSNULL; // Detach reply buffer from tcpInfo_t, to make sure client callback can't cause it to be disposed
tcpInfo->nread = 0;
tcpInfo->replylen = 0;
// If we're going to dispose this connection, do it FIRST, before calling client callback
// Note: Sleep code depends on us clearing *backpointer here -- it uses the clearing of rr->tcp
// as the signal that the DNS deregistration operation with the server has completed, and the machine may now sleep
// If we clear the tcp pointer in the question, mDNSCoreReceiveResponse cannot find a matching question. Hence
// we store the minimal information i.e., the source port of the connection in the question itself.
// Dereference sock before it is disposed in DisposeTCPConn below.
if (sock->flags & kTCPSocketFlags_UseTLS) tls = mDNStrue;
else tls = mDNSfalse;
if (q && q->tcp) {srcPort = q->tcp->SrcPort; q->tcpSrcPort = srcPort;}
if (backpointer)
if (!q || !q->LongLived || m->SleepState)
{ *backpointer = mDNSNULL; DisposeTCPConn(tcpInfo); }
mDNSCoreReceive(m, reply, end, &Addr, Port, tls ? (mDNSAddr *)1 : mDNSNULL, srcPort, 0);
// USE CAUTION HERE: Invoking mDNSCoreReceive may have caused the environment to change, including canceling this operation itself
mDNSPlatformMemFree(reply);
return;
}
}
exit:
if (err)
{
// Clear client backpointer FIRST -- that way if one of the callbacks cancels its operation
// we won't end up double-disposing our tcpInfo_t
if (backpointer) *backpointer = mDNSNULL;
mDNS_Lock(m); // Need to grab the lock to get m->timenow
if (q)
{
if (q->ThisQInterval == 0)
{
// 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.
// Note that ThisQInterval is also zero when sendChallengeResponse resends the LLQ request on an extant TCP/TLS connection.
q->LastQTime = m->timenow;
if (q->LongLived)
{
// We didn't get the chance to send our request packet before the TCP/TLS connection failed.
// We want to retry quickly, but want to back off exponentially in case the server is having issues.
// Since ThisQInterval was 0, we can't just multiply by QuestionIntervalStep, we must track the number
// of TCP/TLS connection failures using ntries.
mDNSu32 count = q->ntries + 1; // want to wait at least 1 second before retrying
q->ThisQInterval = InitialQuestionInterval;
for (;count;count--)
q->ThisQInterval *= QuestionIntervalStep;
if (q->ThisQInterval > LLQ_POLL_INTERVAL)
q->ThisQInterval = LLQ_POLL_INTERVAL;
else
q->ntries++;
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);
}
else
{
q->ThisQInterval = MAX_UCAST_POLL_INTERVAL;
LogMsg("tcpCallback: stream connection for %##s (%s) failed, retrying in %d ms", q->qname.c, DNSTypeName(q->qtype), q->ThisQInterval);
}
SetNextQueryTime(m, q);
}
else if (NextQSendTime(q) - m->timenow > (q->LongLived ? LLQ_POLL_INTERVAL : MAX_UCAST_POLL_INTERVAL))
{
// If we get an error and our next scheduled query for this question is more than the max interval from now,
// reset the next query to ensure we wait no longer the maximum interval from now before trying again.
q->LastQTime = m->timenow;
q->ThisQInterval = q->LongLived ? LLQ_POLL_INTERVAL : MAX_UCAST_POLL_INTERVAL;
SetNextQueryTime(m, q);
LogMsg("tcpCallback: stream connection for %##s (%s) failed, retrying in %d ms", q->qname.c, DNSTypeName(q->qtype), q->ThisQInterval);
}
// We're about to dispose of the TCP connection, so we must reset the state to retry over TCP/TLS
// because sendChallengeResponse will send the query via UDP if we don't have a tcp pointer.
// Resetting to LLQ_InitialRequest will cause uDNS_CheckCurrentQuestion to call startLLQHandshake, which
// will attempt to establish a new tcp connection.
if (q->LongLived && q->state == LLQ_SecondaryRequest)
q->state = LLQ_InitialRequest;
// ConnFailed may happen if the server sends a TCP reset or TLS fails, in which case we want to retry establishing the LLQ
// quickly rather than switching to polling mode. This case is handled by the above code to set q->ThisQInterval just above.
// If the error isn't ConnFailed, then the LLQ is in bad shape, so we switch to polling mode.
if (err != mStatus_ConnFailed)
{
if (q->LongLived && q->state != LLQ_Poll) StartLLQPolling(m, q);
}
}
mDNS_Unlock(m);
DisposeTCPConn(tcpInfo);
}
}
mDNSlocal tcpInfo_t *MakeTCPConn(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *const end,
TCPSocketFlags flags, const mDNSAddr *const Addr, const mDNSIPPort Port, domainname *hostname,
DNSQuestion *const question, AuthRecord *const rr)
{
mStatus err;
mDNSIPPort srcport = zeroIPPort;
tcpInfo_t *info;
if ((flags & kTCPSocketFlags_UseTLS) && (!hostname || !hostname->c[0]))
{ LogMsg("MakeTCPConn: TLS connection being setup with NULL hostname"); return mDNSNULL; }
info = (tcpInfo_t *)mDNSPlatformMemAllocate(sizeof(tcpInfo_t));
if (!info) { LogMsg("ERROR: MakeTCP - memallocate failed"); return(mDNSNULL); }
mDNSPlatformMemZero(info, sizeof(tcpInfo_t));
info->m = m;
info->sock = mDNSPlatformTCPSocket(m, flags, &srcport);
info->requestLen = 0;
info->question = question;
info->rr = rr;
info->Addr = *Addr;
info->Port = Port;
info->reply = mDNSNULL;
info->replylen = 0;
info->nread = 0;
info->numReplies = 0;
info->SrcPort = srcport;
if (msg)
{
info->requestLen = (int) (end - ((mDNSu8*)msg));
mDNSPlatformMemCopy(&info->request, msg, info->requestLen);
}
if (!info->sock) { LogMsg("MakeTCPConn: unable to create TCP socket"); mDNSPlatformMemFree(info); return(mDNSNULL); }
err = mDNSPlatformTCPConnect(info->sock, Addr, Port, hostname, (question ? question->InterfaceID : mDNSNULL), tcpCallback, info);
// Probably suboptimal here.
// Instead of returning mDNSNULL here on failure, we should probably invoke the callback with an error code.
// That way clients can put all the error handling and retry/recovery code in one place,
// instead of having to handle immediate errors in one place and async errors in another.
// Also: "err == mStatus_ConnEstablished" probably never happens.
// Don't need to log "connection failed" in customer builds -- it happens quite often during sleep, wake, configuration changes, etc.
if (err == mStatus_ConnEstablished) { tcpCallback(info->sock, info, mDNStrue, mStatus_NoError); }
else if (err != mStatus_ConnPending ) { LogInfo("MakeTCPConn: connection failed"); DisposeTCPConn(info); return(mDNSNULL); }
return(info);
}
mDNSexport void DisposeTCPConn(struct tcpInfo_t *tcp)
{
mDNSPlatformTCPCloseConnection(tcp->sock);
if (tcp->reply) mDNSPlatformMemFree(tcp->reply);
mDNSPlatformMemFree(tcp);
}
// Lock must be held
mDNSexport void startLLQHandshake(mDNS *m, DNSQuestion *q)
{
if (mDNSIPv4AddressIsOnes(m->LLQNAT.ExternalAddress))
{
LogInfo("startLLQHandshake: waiting for NAT status for %##s (%s)", q->qname.c, DNSTypeName(q->qtype));
q->ThisQInterval = LLQ_POLL_INTERVAL + mDNSRandom(LLQ_POLL_INTERVAL/10); // Retry in approx 15 minutes
q->LastQTime = m->timenow;
SetNextQueryTime(m, q);
return;
}
// Either we don't have NAT-PMP support (ExternalPort is zero) or behind a Double NAT that may or
// may not have NAT-PMP support (NATResult is non-zero)
if (mDNSIPPortIsZero(m->LLQNAT.ExternalPort) || m->LLQNAT.Result)
{
LogInfo("startLLQHandshake: Cannot receive inbound packets; will poll for %##s (%s) External Port %d, NAT Result %d",
q->qname.c, DNSTypeName(q->qtype), mDNSVal16(m->LLQNAT.ExternalPort), m->LLQNAT.Result);
StartLLQPolling(m, q);
return;
}
if (mDNSIPPortIsZero(q->servPort))
{
debugf("startLLQHandshake: StartGetZoneData for %##s (%s)", q->qname.c, DNSTypeName(q->qtype));
q->ThisQInterval = LLQ_POLL_INTERVAL + mDNSRandom(LLQ_POLL_INTERVAL/10); // Retry in approx 15 minutes
q->LastQTime = m->timenow;
SetNextQueryTime(m, q);
q->servAddr = zeroAddr;
// We know q->servPort is zero because of check above
if (q->nta) CancelGetZoneData(m, q->nta);
q->nta = StartGetZoneData(m, &q->qname, ZoneServiceLLQ, LLQGotZoneData, q);
return;
}
if (PrivateQuery(q))
{
if (q->tcp) LogInfo("startLLQHandshake: Disposing existing TCP connection for %##s (%s)", q->qname.c, DNSTypeName(q->qtype));
if (q->tcp) { DisposeTCPConn(q->tcp); q->tcp = mDNSNULL; }
if (!q->nta)
{
// Normally we lookup the zone data and then call this function. And we never free the zone data
// for "PrivateQuery". But sometimes this can happen due to some race conditions. When we
// switch networks, we might end up "Polling" the network e.g., we are behind a Double NAT.
// When we poll, we free the zone information as we send the query to the server (See
// PrivateQueryGotZoneData). The NAT callback (LLQNATCallback) may happen soon after that. If we
// are still behind Double NAT, we would have returned early in this function. But we could
// have switched to a network with no NATs and we should get the zone data again.
LogInfo("startLLQHandshake: nta is NULL for %##s (%s)", q->qname.c, DNSTypeName(q->qtype));
q->nta = StartGetZoneData(m, &q->qname, ZoneServiceLLQ, LLQGotZoneData, q);
return;
}
else if (!q->nta->Host.c[0])
{
// This should not happen. If it happens, we print a log and MakeTCPConn will fail if it can't find a hostname
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);
}
q->tcp = MakeTCPConn(m, mDNSNULL, mDNSNULL, kTCPSocketFlags_UseTLS, &q->servAddr, q->servPort, &q->nta->Host, q, mDNSNULL);
if (!q->tcp)
q->ThisQInterval = mDNSPlatformOneSecond * 5; // If TCP failed (transient networking glitch) try again in five seconds
else
{
q->state = LLQ_SecondaryRequest; // Right now, for private DNS, we skip the four-way LLQ handshake
q->ReqLease = kLLQ_DefLease;
q->ThisQInterval = 0;
}
q->LastQTime = m->timenow;
SetNextQueryTime(m, q);
}
else
{
debugf("startLLQHandshake: m->AdvertisedV4 %#a%s Server %#a:%d%s %##s (%s)",
&m->AdvertisedV4, mDNSv4AddrIsRFC1918(&m->AdvertisedV4.ip.v4) ? " (RFC 1918)" : "",
&q->servAddr, mDNSVal16(q->servPort), mDNSAddrIsRFC1918(&q->servAddr) ? " (RFC 1918)" : "",
q->qname.c, DNSTypeName(q->qtype));
if (q->ntries++ >= kLLQ_MAX_TRIES)
{
LogMsg("startLLQHandshake: %d failed attempts for LLQ %##s Polling.", kLLQ_MAX_TRIES, q->qname.c);
StartLLQPolling(m, q);
}
else
{
mDNSu8 *end;
LLQOptData llqData;
// set llq rdata
llqData.vers = kLLQ_Vers;
llqData.llqOp = kLLQOp_Setup;
llqData.err = LLQErr_NoError; // Don't need to tell server UDP notification port when sending over UDP
llqData.id = zeroOpaque64;
llqData.llqlease = kLLQ_DefLease;
InitializeDNSMessage(&m->omsg.h, q->TargetQID, uQueryFlags);
end = putLLQ(&m->omsg, m->omsg.data, q, &llqData);
if (!end) { LogMsg("ERROR: startLLQHandshake - putLLQ"); StartLLQPolling(m,q); return; }
mDNSSendDNSMessage(m, &m->omsg, end, mDNSInterface_Any, q->LocalSocket, &q->servAddr, q->servPort, mDNSNULL, mDNSNULL);
// update question state
q->state = LLQ_InitialRequest;
q->ReqLease = kLLQ_DefLease;
q->ThisQInterval = (kLLQ_INIT_RESEND * mDNSPlatformOneSecond);
q->LastQTime = m->timenow;
SetNextQueryTime(m, q);
}
}
}
// forward declaration so GetServiceTarget can do reverse lookup if needed
mDNSlocal void GetStaticHostname(mDNS *m);
mDNSexport const domainname *GetServiceTarget(mDNS *m, AuthRecord *const rr)
{
debugf("GetServiceTarget %##s", rr->resrec.name->c);
if (!rr->AutoTarget) // If not automatically tracking this host's current name, just return the existing target
return(&rr->resrec.rdata->u.srv.target);
else
{
#if APPLE_OSX_mDNSResponder
DomainAuthInfo *AuthInfo = GetAuthInfoForName_internal(m, rr->resrec.name);
if (AuthInfo && AuthInfo->AutoTunnel)
{
// If this AutoTunnel is not yet active, start it now (which entails activating its NAT Traversal request,
// which will subsequently advertise the appropriate records when the NAT Traversal returns a result)
if (!AuthInfo->AutoTunnelNAT.clientContext && m->AutoTunnelHostAddr.b[0])
{
LogInfo("GetServiceTarget: Calling SetupLocalAutoTunnelInterface_internal");
SetupLocalAutoTunnelInterface_internal(m, mDNStrue);
}
if (AuthInfo->AutoTunnelHostRecord.namestorage.c[0] == 0) return(mDNSNULL);
debugf("GetServiceTarget: Returning %##s", AuthInfo->AutoTunnelHostRecord.namestorage.c);
return(&AuthInfo->AutoTunnelHostRecord.namestorage);
}
else
#endif // APPLE_OSX_mDNSResponder
{
const int srvcount = CountLabels(rr->resrec.name);
HostnameInfo *besthi = mDNSNULL, *hi;
int best = 0;
for (hi = m->Hostnames; hi; hi = hi->next)
if (hi->arv4.state == regState_Registered || hi->arv4.state == regState_Refresh ||
hi->arv6.state == regState_Registered || hi->arv6.state == regState_Refresh)
{
int x, hostcount = CountLabels(&hi->fqdn);
for (x = hostcount < srvcount ? hostcount : srvcount; x > 0 && x > best; x--)
if (SameDomainName(SkipLeadingLabels(rr->resrec.name, srvcount - x), SkipLeadingLabels(&hi->fqdn, hostcount - x)))
{ best = x; besthi = hi; }
}
if (besthi) return(&besthi->fqdn);
}
if (m->StaticHostname.c[0]) return(&m->StaticHostname);
else GetStaticHostname(m); // asynchronously do reverse lookup for primary IPv4 address
LogInfo("GetServiceTarget: Returning NULL for %s", ARDisplayString(m, rr));
return(mDNSNULL);
}
}
mDNSlocal const domainname *PUBLIC_UPDATE_SERVICE_TYPE = (const domainname*)"\x0B_dns-update" "\x04_udp";
mDNSlocal const domainname *PUBLIC_LLQ_SERVICE_TYPE = (const domainname*)"\x08_dns-llq" "\x04_udp";
mDNSlocal const domainname *PRIVATE_UPDATE_SERVICE_TYPE = (const domainname*)"\x0F_dns-update-tls" "\x04_tcp";
mDNSlocal const domainname *PRIVATE_QUERY_SERVICE_TYPE = (const domainname*)"\x0E_dns-query-tls" "\x04_tcp";
mDNSlocal const domainname *PRIVATE_LLQ_SERVICE_TYPE = (const domainname*)"\x0C_dns-llq-tls" "\x04_tcp";
#define ZoneDataSRV(X) (\
(X)->ZoneService == ZoneServiceUpdate ? ((X)->ZonePrivate ? PRIVATE_UPDATE_SERVICE_TYPE : PUBLIC_UPDATE_SERVICE_TYPE) : \
(X)->ZoneService == ZoneServiceQuery ? ((X)->ZonePrivate ? PRIVATE_QUERY_SERVICE_TYPE : (const domainname*)"" ) : \
(X)->ZoneService == ZoneServiceLLQ ? ((X)->ZonePrivate ? PRIVATE_LLQ_SERVICE_TYPE : PUBLIC_LLQ_SERVICE_TYPE ) : (const domainname*)"")
// Forward reference: GetZoneData_StartQuery references GetZoneData_QuestionCallback, and
// GetZoneData_QuestionCallback calls GetZoneData_StartQuery
mDNSlocal mStatus GetZoneData_StartQuery(mDNS *const m, ZoneData *zd, mDNSu16 qtype);
// GetZoneData_QuestionCallback is called from normal client callback context (core API calls allowed)
mDNSlocal void GetZoneData_QuestionCallback(mDNS *const m, DNSQuestion *question, const ResourceRecord *const answer, QC_result AddRecord)
{
ZoneData *zd = (ZoneData*)question->QuestionContext;
debugf("GetZoneData_QuestionCallback: %s %s", AddRecord ? "Add" : "Rmv", RRDisplayString(m, answer));
if (!AddRecord) return; // Don't care about REMOVE events
if (AddRecord == QC_addnocache && answer->rdlength == 0) return; // Don't care about transient failure indications
if (answer->rrtype != question->qtype) return; // Don't care about CNAMEs
if (answer->rrtype == kDNSType_SOA)
{
debugf("GetZoneData GOT SOA %s", RRDisplayString(m, answer));
mDNS_StopQuery(m, question);
if (question->ThisQInterval != -1)
LogMsg("GetZoneData_QuestionCallback: Question %##s (%s) ThisQInterval %d not -1", question->qname.c, DNSTypeName(question->qtype), question->ThisQInterval);
if (answer->rdlength)
{
AssignDomainName(&zd->ZoneName, answer->name);
zd->ZoneClass = answer->rrclass;
AssignDomainName(&zd->question.qname, &zd->ZoneName);
GetZoneData_StartQuery(m, zd, kDNSType_SRV);
}
else if (zd->CurrentSOA->c[0])
{
DomainAuthInfo *AuthInfo = GetAuthInfoForName(m, zd->CurrentSOA);
if (AuthInfo && AuthInfo->AutoTunnel)
{
// To keep the load on the server down, we don't chop down on
// SOA lookups for AutoTunnels
LogInfo("GetZoneData_QuestionCallback: not chopping labels for %##s", zd->CurrentSOA->c);
zd->ZoneDataCallback(m, mStatus_NoSuchNameErr, zd);
}
else
{
zd->CurrentSOA = (domainname *)(zd->CurrentSOA->c + zd->CurrentSOA->c[0]+1);
AssignDomainName(&zd->question.qname, zd->CurrentSOA);
GetZoneData_StartQuery(m, zd, kDNSType_SOA);
}
}
else
{
LogInfo("GetZoneData recursed to root label of %##s without finding SOA", zd->ChildName.c);
zd->ZoneDataCallback(m, mStatus_NoSuchNameErr, zd);
}
}
else if (answer->rrtype == kDNSType_SRV)
{
debugf("GetZoneData GOT SRV %s", RRDisplayString(m, answer));
mDNS_StopQuery(m, question);
if (question->ThisQInterval != -1)
LogMsg("GetZoneData_QuestionCallback: Question %##s (%s) ThisQInterval %d not -1", question->qname.c, DNSTypeName(question->qtype), question->ThisQInterval);
// Right now we don't want to fail back to non-encrypted operations
// If the AuthInfo has the AutoTunnel field set, then we want private or nothing
// <rdar://problem/5687667> BTMM: Don't fallback to unencrypted operations when SRV lookup fails
#if 0
if (!answer->rdlength && zd->ZonePrivate && zd->ZoneService != ZoneServiceQuery)
{
zd->ZonePrivate = mDNSfalse; // Causes ZoneDataSRV() to yield a different SRV name when building the query
GetZoneData_StartQuery(m, zd, kDNSType_SRV); // Try again, non-private this time
}
else
#endif
{
if (answer->rdlength)
{
AssignDomainName(&zd->Host, &answer->rdata->u.srv.target);
zd->Port = answer->rdata->u.srv.port;
AssignDomainName(&zd->question.qname, &zd->Host);
GetZoneData_StartQuery(m, zd, kDNSType_A);
}
else
{
zd->ZonePrivate = mDNSfalse;
zd->Host.c[0] = 0;
zd->Port = zeroIPPort;
zd->Addr = zeroAddr;
zd->ZoneDataCallback(m, mStatus_NoError, zd);
}
}
}
else if (answer->rrtype == kDNSType_A)
{
debugf("GetZoneData GOT A %s", RRDisplayString(m, answer));
mDNS_StopQuery(m, question);
if (question->ThisQInterval != -1)
LogMsg("GetZoneData_QuestionCallback: Question %##s (%s) ThisQInterval %d not -1", question->qname.c, DNSTypeName(question->qtype), question->ThisQInterval);
zd->Addr.type = mDNSAddrType_IPv4;
zd->Addr.ip.v4 = (answer->rdlength == 4) ? answer->rdata->u.ipv4 : zerov4Addr;
// In order to simulate firewalls blocking our outgoing TCP connections, returning immediate ICMP errors or TCP resets,
// the code below will make us try to connect to loopback, resulting in an immediate "port unreachable" failure.
// This helps us test to make sure we handle this case gracefully
// <rdar://problem/5607082> BTMM: mDNSResponder taking 100 percent CPU after upgrading to 10.5.1
#if 0
zd->Addr.ip.v4.b[0] = 127;
zd->Addr.ip.v4.b[1] = 0;
zd->Addr.ip.v4.b[2] = 0;
zd->Addr.ip.v4.b[3] = 1;
#endif
// The caller needs to free the memory when done with zone data
zd->ZoneDataCallback(m, mStatus_NoError, zd);
}
}
// GetZoneData_StartQuery is called from normal client context (lock not held, or client callback)
mDNSlocal mStatus GetZoneData_StartQuery(mDNS *const m, ZoneData *zd, mDNSu16 qtype)
{
if (qtype == kDNSType_SRV)
{
AssignDomainName(&zd->question.qname, ZoneDataSRV(zd));
AppendDomainName(&zd->question.qname, &zd->ZoneName);
debugf("lookupDNSPort %##s", zd->question.qname.c);
}
// CancelGetZoneData can get called at any time. We should stop the question if it has not been
// stopped already. A value of -1 for ThisQInterval indicates that the question is not active
// yet.
zd->question.ThisQInterval = -1;
zd->question.InterfaceID = mDNSInterface_Any;
zd->question.Target = zeroAddr;
//zd->question.qname.c[0] = 0; // Already set
zd->question.qtype = qtype;
zd->question.qclass = kDNSClass_IN;
zd->question.LongLived = mDNSfalse;
zd->question.ExpectUnique = mDNStrue;
zd->question.ForceMCast = mDNSfalse;
zd->question.ReturnIntermed = mDNStrue;
zd->question.SuppressUnusable = mDNSfalse;
zd->question.SearchListIndex = 0;
zd->question.AppendSearchDomains = 0;
zd->question.RetryWithSearchDomains = mDNSfalse;
zd->question.TimeoutQuestion = 0;
zd->question.WakeOnResolve = 0;
zd->question.qnameOrig = mDNSNULL;
zd->question.QuestionCallback = GetZoneData_QuestionCallback;
zd->question.QuestionContext = zd;
//LogMsg("GetZoneData_StartQuery %##s (%s) %p", zd->question.qname.c, DNSTypeName(zd->question.qtype), zd->question.Private);
return(mDNS_StartQuery(m, &zd->question));
}
// StartGetZoneData is an internal routine (i.e. must be called with the lock already held)
mDNSexport ZoneData *StartGetZoneData(mDNS *const m, const domainname *const name, const ZoneService target, ZoneDataCallback callback, void *ZoneDataContext)
{
DomainAuthInfo *AuthInfo = GetAuthInfoForName_internal(m, name);
int initialskip = (AuthInfo && AuthInfo->AutoTunnel) ? DomainNameLength(name) - DomainNameLength(&AuthInfo->domain) : 0;
ZoneData *zd = (ZoneData*)mDNSPlatformMemAllocate(sizeof(ZoneData));
if (!zd) { LogMsg("ERROR: StartGetZoneData - mDNSPlatformMemAllocate failed"); return mDNSNULL; }
mDNSPlatformMemZero(zd, sizeof(ZoneData));
AssignDomainName(&zd->ChildName, name);
zd->ZoneService = target;
zd->CurrentSOA = (domainname *)(&zd->ChildName.c[initialskip]);
zd->ZoneName.c[0] = 0;
zd->ZoneClass = 0;
zd->Host.c[0] = 0;
zd->Port = zeroIPPort;
zd->Addr = zeroAddr;
zd->ZonePrivate = AuthInfo && AuthInfo->AutoTunnel ? mDNStrue : mDNSfalse;
zd->ZoneDataCallback = callback;
zd->ZoneDataContext = ZoneDataContext;
zd->question.QuestionContext = zd;
mDNS_DropLockBeforeCallback(); // GetZoneData_StartQuery expects to be called from a normal callback, so we emulate that here
if (AuthInfo && AuthInfo->AutoTunnel && !mDNSIPPortIsZero(AuthInfo->port))
{
LogInfo("StartGetZoneData: Bypassing SOA, SRV query for %##s", AuthInfo->domain.c);
// We bypass SOA and SRV queries if we know the hostname and port already from the configuration.
// Today this is only true for AutoTunnel. As we bypass, we need to infer a few things:
//
// 1. Zone name is the same as the AuthInfo domain
// 2. ZoneClass is kDNSClass_IN which should be a safe assumption
//
// If we want to make this bypass mechanism work for non-AutoTunnels also, (1) has to hold
// good. Otherwise, it has to be configured also.
AssignDomainName(&zd->ZoneName, &AuthInfo->domain);
zd->ZoneClass = kDNSClass_IN;
AssignDomainName(&zd->Host, &AuthInfo->hostname);
zd->Port = AuthInfo->port;
AssignDomainName(&zd->question.qname, &zd->Host);
GetZoneData_StartQuery(m, zd, kDNSType_A);
}
else
{
if (AuthInfo && AuthInfo->AutoTunnel) LogInfo("StartGetZoneData: Not Bypassing SOA, SRV query for %##s", AuthInfo->domain.c);
AssignDomainName(&zd->question.qname, zd->CurrentSOA);
GetZoneData_StartQuery(m, zd, kDNSType_SOA);
}
mDNS_ReclaimLockAfterCallback();
return zd;
}
// Returns if the question is a GetZoneData question. These questions are special in
// that they are created internally while resolving a private query or LLQs.
mDNSexport mDNSBool IsGetZoneDataQuestion(DNSQuestion *q)
{
if (q->QuestionCallback == GetZoneData_QuestionCallback) return(mDNStrue);
else return(mDNSfalse);
}
// GetZoneData queries are a special case -- even if we have a key for them, we don't do them privately,
// because that would result in an infinite loop (i.e. to do a private query we first need to get
// the _dns-query-tls SRV record for the zone, and we can't do *that* privately because to do so
// we'd need to already know the _dns-query-tls SRV record.
// Also, as a general rule, we never do SOA queries privately
mDNSexport DomainAuthInfo *GetAuthInfoForQuestion(mDNS *m, const DNSQuestion *const q) // Must be called with lock held
{
if (q->QuestionCallback == GetZoneData_QuestionCallback) return(mDNSNULL);
if (q->qtype == kDNSType_SOA ) return(mDNSNULL);
return(GetAuthInfoForName_internal(m, &q->qname));
}
// ***************************************************************************
#if COMPILER_LIKES_PRAGMA_MARK
#pragma mark - host name and interface management
#endif
mDNSlocal void SendRecordRegistration(mDNS *const m, AuthRecord *rr);
mDNSlocal void SendRecordDeregistration(mDNS *m, AuthRecord *rr);
mDNSlocal mDNSBool IsRecordMergeable(mDNS *const m, AuthRecord *rr, mDNSs32 time);
// When this function is called, service record is already deregistered. We just
// have to deregister the PTR and TXT records.
mDNSlocal void UpdateAllServiceRecords(mDNS *const m, AuthRecord *rr, mDNSBool reg)
{
AuthRecord *r, *srvRR;
if (rr->resrec.rrtype != kDNSType_SRV) { LogMsg("UpdateAllServiceRecords:ERROR!! ResourceRecord not a service record %s", ARDisplayString(m, rr)); return; }
if (reg && rr->state == regState_NoTarget) { LogMsg("UpdateAllServiceRecords:ERROR!! SRV record %s in noTarget state during registration", ARDisplayString(m, rr)); return; }
LogInfo("UpdateAllServiceRecords: ResourceRecord %s", ARDisplayString(m, rr));
for (r = m->ResourceRecords; r; r=r->next)
{
if (!AuthRecord_uDNS(r)) continue;
srvRR = mDNSNULL;
if (r->resrec.rrtype == kDNSType_PTR)
srvRR = r->Additional1;
else if (r->resrec.rrtype == kDNSType_TXT)
srvRR = r->DependentOn;
if (srvRR && srvRR->resrec.rrtype != kDNSType_SRV)
LogMsg("UpdateAllServiceRecords: ERROR!! Resource record %s wrong, expecting SRV type", ARDisplayString(m, srvRR));
if (srvRR == rr)
{
if (!reg)
{
LogInfo("UpdateAllServiceRecords: deregistering %s", ARDisplayString(m, r));
r->SRVChanged = mDNStrue;
r->ThisAPInterval = INIT_RECORD_REG_INTERVAL;
r->LastAPTime = m->timenow - INIT_RECORD_REG_INTERVAL;
r->state = regState_DeregPending;
}
else
{
// Clearing SRVchanged is a safety measure. If our pevious dereg never
// came back and we had a target change, we are starting fresh
r->SRVChanged = mDNSfalse;
// if it is already registered or in the process of registering, then don't
// bother re-registering. This happens today for non-BTMM domains where the
// TXT and PTR get registered before SRV records because of the delay in
// getting the port mapping. There is no point in re-registering the TXT
// and PTR records.
if ((r->state == regState_Registered) ||
(r->state == regState_Pending && r->nta && !mDNSIPv4AddressIsZero(r->nta->Addr.ip.v4)))
LogInfo("UpdateAllServiceRecords: not registering %s, state %d", ARDisplayString(m, r), r->state);
else
{
LogInfo("UpdateAllServiceRecords: registering %s, state %d", ARDisplayString(m, r), r->state);
ActivateUnicastRegistration(m, r);
}
}
}
}
}
// Called in normal client context (lock not held)
// Currently only supports SRV records for nat mapping
mDNSlocal void CompleteRecordNatMap(mDNS *m, NATTraversalInfo *n)
{
const domainname *target;
domainname *srvt;
AuthRecord *rr = (AuthRecord *)n->clientContext;
debugf("SRVNatMap complete %.4a IntPort %u ExternalPort %u NATLease %u", &n->ExternalAddress, mDNSVal16(n->IntPort), mDNSVal16(n->ExternalPort), n->NATLease);
if (!rr) { LogMsg("CompleteRecordNatMap called with unknown AuthRecord object"); return; }
if (!n->NATLease) { LogMsg("CompleteRecordNatMap No NATLease for %s", ARDisplayString(m, rr)); return; }
if (rr->resrec.rrtype != kDNSType_SRV) {LogMsg("CompleteRecordNatMap: Not a service record %s", ARDisplayString(m, rr)); return; }
if (rr->resrec.RecordType == kDNSRecordTypeDeregistering) { LogInfo("CompleteRecordNatMap called for %s, Service deregistering", ARDisplayString(m, rr)); return; }
if (rr->state == regState_DeregPending) { LogInfo("CompleteRecordNatMap called for %s, record in DeregPending", ARDisplayString(m, rr)); return; }
// As we free the zone info after registering/deregistering with the server (See hndlRecordUpdateReply),
// we need to restart the get zone data and nat mapping request to get the latest mapping result as we can't handle it
// at this moment. Restart from the beginning.
if (!rr->nta || mDNSIPv4AddressIsZero(rr->nta->Addr.ip.v4))
{
LogInfo("CompleteRecordNatMap called for %s but no zone information!", ARDisplayString(m, rr));
// We need to clear out the NATinfo state so that it will result in re-acquiring the mapping
// and hence this callback called again.
if (rr->NATinfo.clientContext)
{
mDNS_StopNATOperation_internal(m, &rr->NATinfo);
rr->NATinfo.clientContext = mDNSNULL;
}
rr->state = regState_Pending;
rr->ThisAPInterval = INIT_RECORD_REG_INTERVAL;
rr->LastAPTime = m->timenow - INIT_RECORD_REG_INTERVAL;
return;
}
mDNS_Lock(m);
// Reevaluate the target always as Target could have changed while
// we were getting the port mapping (See UpdateOneSRVRecord)
target = GetServiceTarget(m, rr);
srvt = GetRRDomainNameTarget(&rr->resrec);
if (!target || target->c[0] == 0 || mDNSIPPortIsZero(n->ExternalPort))
{
if (target && target->c[0])
LogInfo("CompleteRecordNatMap - Target %##s for ResourceRecord %##s, ExternalPort %d", target->c, rr->resrec.name->c, mDNSVal16(n->ExternalPort));
else
LogInfo("CompleteRecordNatMap - no target for %##s, ExternalPort %d", rr->resrec.name->c, mDNSVal16(n->ExternalPort));
if (srvt) srvt->c[0] = 0;
rr->state = regState_NoTarget;
rr->resrec.rdlength = rr->resrec.rdestimate = 0;
mDNS_Unlock(m);
UpdateAllServiceRecords(m, rr, mDNSfalse);
return;
}
LogInfo("CompleteRecordNatMap - Target %##s for ResourceRecord %##s, ExternalPort %d", target->c, rr->resrec.name->c, mDNSVal16(n->ExternalPort));
// This function might get called multiple times during a network transition event. Previosuly, we could
// have put the SRV record in NoTarget state above and deregistered all the other records. When this
// function gets called again with a non-zero ExternalPort, we need to set the target and register the
// other records again.
if (srvt && !SameDomainName(srvt, target))
{
AssignDomainName(srvt, target);
SetNewRData(&rr->resrec, mDNSNULL, 0); // Update rdlength, rdestimate, rdatahash
}
// SRVChanged is set when when the target of the SRV record changes (See UpdateOneSRVRecord).
// As a result of the target change, we might register just that SRV Record if it was
// previously registered and we have a new target OR deregister SRV (and the associated
// PTR/TXT records) if we don't have a target anymore. When we get a response from the server,
// SRVChanged state tells that we registered/deregistered because of a target change
// and hence handle accordingly e.g., if we deregistered, put the records in NoTarget state OR
// if we registered then put it in Registered state.
//
// Here, we are registering all the records again from the beginning. Treat this as first time
// registration rather than a temporary target change.
rr->SRVChanged = mDNSfalse;
// We want IsRecordMergeable to check whether it is a record whose update can be
// sent with others. We set the time before we call IsRecordMergeable, so that
// it does not fail this record based on time. We are interested in other checks
// at this time
rr->state = regState_Pending;
rr->ThisAPInterval = INIT_RECORD_REG_INTERVAL;
rr->LastAPTime = m->timenow - INIT_RECORD_REG_INTERVAL;
if (IsRecordMergeable(m, rr, m->timenow + MERGE_DELAY_TIME))
// Delay the record registration by MERGE_DELAY_TIME so that we can merge them
// into one update
rr->LastAPTime += MERGE_DELAY_TIME;
mDNS_Unlock(m);
// We call this always even though it may not be necessary always e.g., normal registration
// process where TXT and PTR gets registered followed by the SRV record after it gets
// the port mapping. In that case, UpdateAllServiceRecords handles the optimization. The
// update of TXT and PTR record is required if we entered noTargetState before as explained
// above.
UpdateAllServiceRecords(m, rr, mDNStrue);
}
mDNSlocal void StartRecordNatMap(mDNS *m, AuthRecord *rr)
{
const mDNSu8 *p;
mDNSu8 protocol;
if (rr->resrec.rrtype != kDNSType_SRV)
{
LogInfo("StartRecordNatMap: Resource Record %##s type %d, not supported", rr->resrec.name->c, rr->resrec.rrtype);
return;
}
p = rr->resrec.name->c;
//Assume <Service Instance>.<App Protocol>.<Transport protocol>.<Name>
// Skip the first two labels to get to the transport protocol
if (p[0]) p += 1 + p[0];
if (p[0]) p += 1 + p[0];
if (SameDomainLabel(p, (mDNSu8 *)"\x4" "_tcp")) protocol = NATOp_MapTCP;
else if (SameDomainLabel(p, (mDNSu8 *)"\x4" "_udp")) protocol = NATOp_MapUDP;
else { LogMsg("StartRecordNatMap: could not determine transport protocol of service %##s", rr->resrec.name->c); return; }
//LogMsg("StartRecordNatMap: clientContext %p IntPort %d srv.port %d %s",
// rr->NATinfo.clientContext, mDNSVal16(rr->NATinfo.IntPort), mDNSVal16(rr->resrec.rdata->u.srv.port), ARDisplayString(m, rr));
if (rr->NATinfo.clientContext) mDNS_StopNATOperation_internal(m, &rr->NATinfo);
rr->NATinfo.Protocol = protocol;
// Shouldn't be trying to set IntPort here --
// BuildUpdateMessage overwrites srs->RR_SRV.resrec.rdata->u.srv.port with external (mapped) port number
rr->NATinfo.IntPort = rr->resrec.rdata->u.srv.port;
rr->NATinfo.RequestedPort = rr->resrec.rdata->u.srv.port;
rr->NATinfo.NATLease = 0; // Request default lease
rr->NATinfo.clientCallback = CompleteRecordNatMap;
rr->NATinfo.clientContext = rr;
mDNS_StartNATOperation_internal(m, &rr->NATinfo);
}
// Unlink an Auth Record from the m->ResourceRecords list.
// When a resource record enters regState_NoTarget initially, mDNS_Register_internal
// does not initialize completely e.g., it cannot check for duplicates etc. The resource
// record is temporarily left in the ResourceRecords list so that we can initialize later
// when the target is resolvable. Similarly, when host name changes, we enter regState_NoTarget
// and we do the same.
// This UnlinkResourceRecord routine is very worrying. It bypasses all the normal cleanup performed
// by mDNS_Deregister_internal and just unceremoniously cuts the record from the active list.
// This is why re-regsitering this record was producing syslog messages like this:
// "Error! Tried to add a NAT traversal that's already in the active list"
// Right now UnlinkResourceRecord is fortunately only called by RegisterAllServiceRecords,
// which then immediately calls mDNS_Register_internal to re-register the record, which probably
// masked more serious problems. Any other use of UnlinkResourceRecord is likely to lead to crashes.
// For now we'll workaround that specific problem by explicitly calling mDNS_StopNATOperation_internal,
// but long-term we should either stop cancelling the record registration and then re-registering it,
// or if we really do need to do this for some reason it should be done via the usual
// mDNS_Deregister_internal path instead of just cutting the record from the list.
mDNSlocal mStatus UnlinkResourceRecord(mDNS *const m, AuthRecord *const rr)
{
AuthRecord **list = &m->ResourceRecords;
while (*list && *list != rr) list = &(*list)->next;
if (*list)
{
*list = rr->next;
rr->next = mDNSNULL;
// Temporary workaround to cancel any active NAT mapping operation
if (rr->NATinfo.clientContext)
{
mDNS_StopNATOperation_internal(m, &rr->NATinfo);
rr->NATinfo.clientContext = mDNSNULL;
if (rr->resrec.rrtype == kDNSType_SRV) rr->resrec.rdata->u.srv.port = rr->NATinfo.IntPort;
}
return(mStatus_NoError);
}
LogMsg("UnlinkResourceRecord:ERROR!! - no such active record %##s", rr->resrec.name->c);
return(mStatus_NoSuchRecord);
}
// We need to go through mDNS_Register again as we did not complete the
// full initialization last time e.g., duplicate checks.
// After we register, we will be in regState_GetZoneData.
mDNSlocal void RegisterAllServiceRecords(mDNS *const m, AuthRecord *rr)
{
LogInfo("RegisterAllServiceRecords: Service Record %##s", rr->resrec.name->c);
// First Register the service record, we do this differently from other records because
// when it entered NoTarget state, it did not go through complete initialization
rr->SRVChanged = mDNSfalse;
UnlinkResourceRecord(m, rr);
mDNS_Register_internal(m, rr);
// Register the other records
UpdateAllServiceRecords(m, rr, mDNStrue);
}
// Called with lock held
mDNSlocal void UpdateOneSRVRecord(mDNS *m, AuthRecord *rr)
{
// Target change if:
// We have a target and were previously waiting for one, or
// We had a target and no longer do, or
// The target has changed
domainname *curtarget = &rr->resrec.rdata->u.srv.target;
const domainname *const nt = GetServiceTarget(m, rr);
const domainname *const newtarget = nt ? nt : (domainname*)"";
mDNSBool TargetChanged = (newtarget->c[0] && rr->state == regState_NoTarget) || !SameDomainName(curtarget, newtarget);
mDNSBool HaveZoneData = rr->nta && !mDNSIPv4AddressIsZero(rr->nta->Addr.ip.v4);
// Nat state change if:
// We were behind a NAT, and now we are behind a new NAT, or
// We're not behind a NAT but our port was previously mapped to a different external port
// We were not behind a NAT and now we are
mDNSIPPort port = rr->resrec.rdata->u.srv.port;
mDNSBool NowNeedNATMAP = (rr->AutoTarget == Target_AutoHostAndNATMAP && !mDNSIPPortIsZero(port) && mDNSv4AddrIsRFC1918(&m->AdvertisedV4.ip.v4) && rr->nta && !mDNSAddrIsRFC1918(&rr->nta->Addr));
mDNSBool WereBehindNAT = (rr->NATinfo.clientContext != mDNSNULL);
mDNSBool PortWasMapped = (rr->NATinfo.clientContext && !mDNSSameIPPort(rr->NATinfo.RequestedPort, port)); // I think this is always false -- SC Sept 07
mDNSBool NATChanged = (!WereBehindNAT && NowNeedNATMAP) || (!NowNeedNATMAP && PortWasMapped);
(void)HaveZoneData; //unused
LogInfo("UpdateOneSRVRecord: Resource Record %s TargetChanged %d, NewTarget %##s", ARDisplayString(m, rr), TargetChanged, nt->c);
debugf("UpdateOneSRVRecord: %##s newtarget %##s TargetChanged %d HaveZoneData %d port %d NowNeedNATMAP %d WereBehindNAT %d PortWasMapped %d NATChanged %d",
rr->resrec.name->c, newtarget,
TargetChanged, HaveZoneData, mDNSVal16(port), NowNeedNATMAP, WereBehindNAT, PortWasMapped, NATChanged);
if (m->mDNS_busy != m->mDNS_reentrancy+1)
LogMsg("UpdateOneSRVRecord: Lock not held! mDNS_busy (%ld) mDNS_reentrancy (%ld)", m->mDNS_busy, m->mDNS_reentrancy);
if (!TargetChanged && !NATChanged) return;
// If we are deregistering the record, then ignore any NAT/Target change.
if (rr->resrec.RecordType == kDNSRecordTypeDeregistering)
{
LogInfo("UpdateOneSRVRecord: Deregistering record, Ignoring TargetChanged %d, NATChanged %d for %##s, state %d", TargetChanged, NATChanged,
rr->resrec.name->c, rr->state);
return;
}
if (newtarget)
LogInfo("UpdateOneSRVRecord: TargetChanged %d, NATChanged %d for %##s, state %d, newtarget %##s", TargetChanged, NATChanged, rr->resrec.name->c, rr->state, newtarget->c);
else
LogInfo("UpdateOneSRVRecord: TargetChanged %d, NATChanged %d for %##s, state %d, null newtarget", TargetChanged, NATChanged, rr->resrec.name->c, rr->state);
switch(rr->state)
{
case regState_NATMap:
// In these states, the SRV has either not yet been registered (it will get up-to-date information when it is)
// or is in the process of, or has already been, deregistered. This assumes that whenever we transition out
// of this state, we need to look at the target again.
return;
case regState_UpdatePending:
// We are getting a Target change/NAT change while the SRV record is being updated ?
// let us not do anything for now.
return;
case regState_NATError:
if (!NATChanged) return;
// if nat changed, register if we have a target (below)
case regState_NoTarget:
if (!newtarget->c[0])
{
LogInfo("UpdateOneSRVRecord: No target yet for Resource Record %s", ARDisplayString(m, rr));
return;
}
RegisterAllServiceRecords(m , rr);
return;
case regState_DeregPending:
// We are in DeregPending either because the service was deregistered from above or we handled
// a NAT/Target change before and sent the deregistration below. There are a few race conditions
// possible
//
// 1. We are handling a second NAT/Target change while the first dereg is in progress. It is possible
// that first dereg never made it through because there was no network connectivity e.g., disconnecting
// from network triggers this function due to a target change and later connecting to the network
// retriggers this function but the deregistration never made it through yet. Just fall through.
// If there is a target register otherwise deregister.
//
// 2. While we sent the dereg during a previous NAT/Target change, uDNS_DeregisterRecord gets
// called as part of service deregistration. When the response comes back, we call
// CompleteDeregistration rather than handle NAT/Target change because the record is in
// kDNSRecordTypeDeregistering state.
//
// 3. If the upper layer deregisters the service, we check for kDNSRecordTypeDeregistering both
// here in this function to avoid handling NAT/Target change and in hndlRecordUpdateReply to call
// CompleteDeregistration instead of handling NAT/Target change. Hence, we are not concerned
// about that case here.
//
// We just handle case (1) by falling through
case regState_Pending:
case regState_Refresh:
case regState_Registered:
// target or nat changed. deregister service. upon completion, we'll look for a new target
rr->SRVChanged = mDNStrue;
rr->ThisAPInterval = INIT_RECORD_REG_INTERVAL;
rr->LastAPTime = m->timenow - INIT_RECORD_REG_INTERVAL;
if (newtarget->c[0])
{
LogInfo("UpdateOneSRVRecord: SRV record changed for service %##s, registering with new target %##s",
rr->resrec.name->c, newtarget->c);
rr->state = regState_Pending;
}
else
{
LogInfo("UpdateOneSRVRecord: SRV record changed for service %##s de-registering", rr->resrec.name->c);
rr->state = regState_DeregPending;
UpdateAllServiceRecords(m, rr, mDNSfalse);
}
return;
case regState_Unregistered:
default: LogMsg("UpdateOneSRVRecord: Unknown state %d for %##s", rr->state, rr->resrec.name->c);
}
}
mDNSexport void UpdateAllSRVRecords(mDNS *m)
{
m->NextSRVUpdate = 0;
LogInfo("UpdateAllSRVRecords %d", m->SleepState);
if (m->CurrentRecord)
LogMsg("UpdateAllSRVRecords ERROR m->CurrentRecord already set %s", ARDisplayString(m, m->CurrentRecord));
m->CurrentRecord = m->ResourceRecords;
while (m->CurrentRecord)
{
AuthRecord *rptr = m->CurrentRecord;
m->CurrentRecord = m->CurrentRecord->next;
if (AuthRecord_uDNS(rptr) && rptr->resrec.rrtype == kDNSType_SRV)
UpdateOneSRVRecord(m, rptr);
}
}
// Forward reference: AdvertiseHostname references HostnameCallback, and HostnameCallback calls AdvertiseHostname
mDNSlocal void HostnameCallback(mDNS *const m, AuthRecord *const rr, mStatus result);
// Called in normal client context (lock not held)
mDNSlocal void hostnameGetPublicAddressCallback(mDNS *m, NATTraversalInfo *n)
{
HostnameInfo *h = (HostnameInfo *)n->clientContext;
if (!h) { LogMsg("RegisterHostnameRecord: registration cancelled"); return; }
if (!n->Result)
{
if (mDNSIPv4AddressIsZero(n->ExternalAddress) || mDNSv4AddrIsRFC1918(&n->ExternalAddress)) return;
if (h->arv4.resrec.RecordType)
{
if (mDNSSameIPv4Address(h->arv4.resrec.rdata->u.ipv4, n->ExternalAddress)) return; // If address unchanged, do nothing
LogInfo("Updating hostname %p %##s IPv4 from %.4a to %.4a (NAT gateway's external address)",n,
h->arv4.resrec.name->c, &h->arv4.resrec.rdata->u.ipv4, &n->ExternalAddress);
mDNS_Deregister(m, &h->arv4); // mStatus_MemFree callback will re-register with new address
}
else
{
LogInfo("Advertising hostname %##s IPv4 %.4a (NAT gateway's external address)", h->arv4.resrec.name->c, &n->ExternalAddress);
h->arv4.resrec.RecordType = kDNSRecordTypeKnownUnique;
h->arv4.resrec.rdata->u.ipv4 = n->ExternalAddress;
mDNS_Register(m, &h->arv4);
}
}
}
// register record or begin NAT traversal
mDNSlocal void AdvertiseHostname(mDNS *m, HostnameInfo *h)
{
if (!mDNSIPv4AddressIsZero(m->AdvertisedV4.ip.v4) && h->arv4.resrec.RecordType == kDNSRecordTypeUnregistered)
{
mDNS_SetupResourceRecord(&h->arv4, mDNSNULL, mDNSInterface_Any, kDNSType_A, kHostNameTTL, kDNSRecordTypeUnregistered, AuthRecordAny, HostnameCallback, h);
AssignDomainName(&h->arv4.namestorage, &h->fqdn);
h->arv4.resrec.rdata->u.ipv4 = m->AdvertisedV4.ip.v4;
h->arv4.state = regState_Unregistered;
if (mDNSv4AddrIsRFC1918(&m->AdvertisedV4.ip.v4))
{
// If we already have a NAT query active, stop it and restart it to make sure we get another callback
if (h->natinfo.clientContext) mDNS_StopNATOperation_internal(m, &h->natinfo);
h->natinfo.Protocol = 0;
h->natinfo.IntPort = zeroIPPort;
h->natinfo.RequestedPort = zeroIPPort;
h->natinfo.NATLease = 0;
h->natinfo.clientCallback = hostnameGetPublicAddressCallback;
h->natinfo.clientContext = h;
mDNS_StartNATOperation_internal(m, &h->natinfo);
}
else
{
LogInfo("Advertising hostname %##s IPv4 %.4a", h->arv4.resrec.name->c, &m->AdvertisedV4.ip.v4);
h->arv4.resrec.RecordType = kDNSRecordTypeKnownUnique;
mDNS_Register_internal(m, &h->arv4);
}
}
if (!mDNSIPv6AddressIsZero(m->AdvertisedV6.ip.v6) && h->arv6.resrec.RecordType == kDNSRecordTypeUnregistered)
{
mDNS_SetupResourceRecord(&h->arv6, mDNSNULL, mDNSInterface_Any, kDNSType_AAAA, kHostNameTTL, kDNSRecordTypeKnownUnique, AuthRecordAny, HostnameCallback, h);
AssignDomainName(&h->arv6.namestorage, &h->fqdn);
h->arv6.resrec.rdata->u.ipv6 = m->AdvertisedV6.ip.v6;
h->arv6.state = regState_Unregistered;
LogInfo("Advertising hostname %##s IPv6 %.16a", h->arv6.resrec.name->c, &m->AdvertisedV6.ip.v6);
mDNS_Register_internal(m, &h->arv6);
}
}
mDNSlocal void HostnameCallback(mDNS *const m, AuthRecord *const rr, mStatus result)
{
HostnameInfo *hi = (HostnameInfo *)rr->RecordContext;
if (result == mStatus_MemFree)
{
if (hi)
{
// If we're still in the Hostnames list, update to new address
HostnameInfo *i;
LogInfo("HostnameCallback: Got mStatus_MemFree for %p %p %s", hi, rr, ARDisplayString(m, rr));
for (i = m->Hostnames; i; i = i->next)
if (rr == &i->arv4 || rr == &i->arv6)
{ mDNS_Lock(m); AdvertiseHostname(m, i); mDNS_Unlock(m); return; }
// Else, we're not still in the Hostnames list, so free the memory
if (hi->arv4.resrec.RecordType == kDNSRecordTypeUnregistered &&
hi->arv6.resrec.RecordType == kDNSRecordTypeUnregistered)
{
if (hi->natinfo.clientContext) mDNS_StopNATOperation_internal(m, &hi->natinfo);
hi->natinfo.clientContext = mDNSNULL;
mDNSPlatformMemFree(hi); // free hi when both v4 and v6 AuthRecs deallocated
}
}
return;
}
if (result)
{
// don't unlink or free - we can retry when we get a new address/router
if (rr->resrec.rrtype == kDNSType_A)
LogMsg("HostnameCallback: Error %d for registration of %##s IP %.4a", result, rr->resrec.name->c, &rr->resrec.rdata->u.ipv4);
else
LogMsg("HostnameCallback: Error %d for registration of %##s IP %.16a", result, rr->resrec.name->c, &rr->resrec.rdata->u.ipv6);
if (!hi) { mDNSPlatformMemFree(rr); return; }
if (rr->state != regState_Unregistered) LogMsg("Error: HostnameCallback invoked with error code for record not in regState_Unregistered!");
if (hi->arv4.state == regState_Unregistered &&
hi->arv6.state == regState_Unregistered)
{
// only deliver status if both v4 and v6 fail
rr->RecordContext = (void *)hi->StatusContext;
if (hi->StatusCallback)
hi->StatusCallback(m, rr, result); // client may NOT make API calls here
rr->RecordContext = (void *)hi;
}
return;
}
// register any pending services that require a target
mDNS_Lock(m);
m->NextSRVUpdate = NonZeroTime(m->timenow);
mDNS_Unlock(m);
// Deliver success to client
if (!hi) { LogMsg("HostnameCallback invoked with orphaned address record"); return; }
if (rr->resrec.rrtype == kDNSType_A)
LogInfo("Registered hostname %##s IP %.4a", rr->resrec.name->c, &rr->resrec.rdata->u.ipv4);
else
LogInfo("Registered hostname %##s IP %.16a", rr->resrec.name->c, &rr->resrec.rdata->u.ipv6);
rr->RecordContext = (void *)hi->StatusContext;
if (hi->StatusCallback)
hi->StatusCallback(m, rr, result); // client may NOT make API calls here
rr->RecordContext = (void *)hi;
}
mDNSlocal void FoundStaticHostname(mDNS *const m, DNSQuestion *question, const ResourceRecord *const answer, QC_result AddRecord)
{
const domainname *pktname = &answer->rdata->u.name;
domainname *storedname = &m->StaticHostname;
HostnameInfo *h = m->Hostnames;
(void)question;
if (answer->rdlength != 0)
LogInfo("FoundStaticHostname: question %##s -> answer %##s (%s)", question->qname.c, answer->rdata->u.name.c, AddRecord ? "ADD" : "RMV");
else
LogInfo("FoundStaticHostname: question %##s -> answer NULL (%s)", question->qname.c, AddRecord ? "ADD" : "RMV");
if (AddRecord && answer->rdlength != 0 && !SameDomainName(pktname, storedname))
{
AssignDomainName(storedname, pktname);
while (h)
{
if (h->arv4.state == regState_Pending || h->arv4.state == regState_NATMap || h->arv6.state == regState_Pending)
{
// 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
m->NextSRVUpdate = NonZeroTime(m->timenow + 5 * mDNSPlatformOneSecond);
debugf("FoundStaticHostname: NextSRVUpdate in %d %d", m->NextSRVUpdate - m->timenow, m->timenow);
return;
}
h = h->next;
}
mDNS_Lock(m);
m->NextSRVUpdate = NonZeroTime(m->timenow);
mDNS_Unlock(m);
}
else if (!AddRecord && SameDomainName(pktname, storedname))
{
mDNS_Lock(m);
storedname->c[0] = 0;
m->NextSRVUpdate = NonZeroTime(m->timenow);
mDNS_Unlock(m);
}
}
// Called with lock held
mDNSlocal void GetStaticHostname(mDNS *m)
{
char buf[MAX_REVERSE_MAPPING_NAME_V4];
DNSQuestion *q = &m->ReverseMap;
mDNSu8 *ip = m->AdvertisedV4.ip.v4.b;
mStatus err;
if (m->ReverseMap.ThisQInterval != -1) return; // already running
if (mDNSIPv4AddressIsZero(m->AdvertisedV4.ip.v4)) return;
mDNSPlatformMemZero(q, sizeof(*q));
// Note: This is reverse order compared to a normal dotted-decimal IP address, so we can't use our customary "%.4a" format code
mDNS_snprintf(buf, sizeof(buf), "%d.%d.%d.%d.in-addr.arpa.", ip[3], ip[2], ip[1], ip[0]);
if (!MakeDomainNameFromDNSNameString(&q->qname, buf)) { LogMsg("Error: GetStaticHostname - bad name %s", buf); return; }
q->InterfaceID = mDNSInterface_Any;
q->Target = zeroAddr;
q->qtype = kDNSType_PTR;
q->qclass = kDNSClass_IN;
q->LongLived = mDNSfalse;
q->ExpectUnique = mDNSfalse;
q->ForceMCast = mDNSfalse;
q->ReturnIntermed = mDNStrue;
q->SuppressUnusable = mDNSfalse;
q->SearchListIndex = 0;
q->AppendSearchDomains = 0;
q->RetryWithSearchDomains = mDNSfalse;
q->TimeoutQuestion = 0;
q->WakeOnResolve = 0;
q->qnameOrig = mDNSNULL;
q->QuestionCallback = FoundStaticHostname;
q->QuestionContext = mDNSNULL;
LogInfo("GetStaticHostname: %##s (%s)", q->qname.c, DNSTypeName(q->qtype));
err = mDNS_StartQuery_internal(m, q);
if (err) LogMsg("Error: GetStaticHostname - StartQuery returned error %d", err);
}
mDNSexport void mDNS_AddDynDNSHostName(mDNS *m, const domainname *fqdn, mDNSRecordCallback *StatusCallback, const void *StatusContext)
{
HostnameInfo **ptr = &m->Hostnames;
LogInfo("mDNS_AddDynDNSHostName %##s", fqdn);
while (*ptr && !SameDomainName(fqdn, &(*ptr)->fqdn)) ptr = &(*ptr)->next;
if (*ptr) { LogMsg("DynDNSHostName %##s already in list", fqdn->c); return; }
// allocate and format new address record
*ptr = mDNSPlatformMemAllocate(sizeof(**ptr));
if (!*ptr) { LogMsg("ERROR: mDNS_AddDynDNSHostName - malloc"); return; }
mDNSPlatformMemZero(*ptr, sizeof(**ptr));
AssignDomainName(&(*ptr)->fqdn, fqdn);
(*ptr)->arv4.state = regState_Unregistered;
(*ptr)->arv6.state = regState_Unregistered;
(*ptr)->StatusCallback = StatusCallback;
(*ptr)->StatusContext = StatusContext;
AdvertiseHostname(m, *ptr);
}
mDNSexport void mDNS_RemoveDynDNSHostName(mDNS *m, const domainname *fqdn)
{
HostnameInfo **ptr = &m->Hostnames;
LogInfo("mDNS_RemoveDynDNSHostName %##s", fqdn);
while (*ptr && !SameDomainName(fqdn, &(*ptr)->fqdn)) ptr = &(*ptr)->next;
if (!*ptr) LogMsg("mDNS_RemoveDynDNSHostName: no such domainname %##s", fqdn->c);
else
{