uDNS.c revision 5ffb0c9b03b5149ff4f5821a62be4a52408ada2a
/* -*- Mode: C; tab-width: 4 -*-
*
* Copyright (c) 2002-2013 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
*
*
* 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
*/
#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.
#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.
// The value can be set to true by the Platform code e.g., MacOSX uses the plist mechanism
// 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.
#define MAX_UNICAST_DNS_SERVERS 64
#define SetNextuDNSEvent(m, rr) { \
}
#ifndef UNICAST_DISABLED
// ***************************************************************************
#endif
// set retry timestamp for record with exponential backoff
{
{
rr->refreshCount++;
if (remaining > MIN_UPDATE_REFRESH_TIME)
{
// Refresh at 70% + random (currently it is 0 to 10%)
// Don't update more often than 5 minutes
LogInfo("SetRecordRetry refresh in %d of %d for %s",
rr->ThisAPInterval/mDNSPlatformOneSecond, (rr->expire - m->timenow)/mDNSPlatformOneSecond, ARDisplayString(m, rr));
}
else
{
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->ThisAPInterval = rr->ThisAPInterval * QuestionIntervalStep; // Same Retry logic as Unicast Queries
}
// ***************************************************************************
#endif
mDNSexport DNSServer *mDNS_AddDNSServer(mDNS *const m, const domainname *d, const mDNSInterfaceID interface, const mDNSs32 serviceID, const mDNSAddr *addr,
const mDNSIPPort port, mDNSu32 scoped, mDNSu32 timeout, mDNSBool cellIntf, mDNSu16 resGroupID, mDNSBool reqA,
{
DNSServer **p = &m->DNSServers;
{
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(%d): Adding %#a for %##s, InterfaceID %p, serviceID %u, scoped %d, resGroupID %d req_A is %s req_AAAA is %s cell %s req_DO is %s",
NumUnicastDNSServers, addr, d->c, interface, serviceID, scoped, resGroupID, reqA ? "True" : "False", reqAAAA ? "True" : "False",
while (*p) // Check if we already have this {interface,address,port,domain} tuple registered + reqA/reqAAAA bits
{
if ((*p)->scoped == scoped && (*p)->interface == interface && (*p)->serviceID == serviceID && (*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);
tmp = *p;
}
else
{
p=&(*p)->next;
}
}
// NumUnicastDNSServers is the count of active DNS servers i.e., ones that are not marked
// with DNSServer_FlagDelete. We should increment it:
//
// 1) When we add a new DNS server
// 2) When we resurrect a old DNS server that is marked with DNSServer_FlagDelete
//
// Don't increment when we resurrect a DNS server that is not marked with DNSServer_FlagDelete.
// We have already accounted for it when it was added for the first time. This case happens when
// we add DNS servers with the same address multiple times (mis-configuration).
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
{
(*p)->flags = DNSServer_FlagNew;
// We start off assuming that the DNS server is not DNSSEC aware and
// when we receive the first response to a DNSSEC question, we set
// it to true.
(*p)->DNSSECAware = mDNSfalse;
(*p)->retransDO = 0;
AssignDomainName(&(*p)->domain, d);
}
}
(*p)->penaltyTime = 0;
// We always update the ID (not just when we allocate a new instance) because we could
// be adding a new non-scoped resolver with a new ID and we want all the non-scoped
// resolvers belong to the same group.
(*p)->resGroupID = resGroupID;
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.
{
mDNS_CheckLock(m);
LogInfo("PenalizeDNSServer: Penalizing DNS server %#a question for question %p %##s (%s) SuppressUnusable %d",
(q->qDNSServer ? &q->qDNSServer->addr : mDNSNULL), q, q->qname.c, DNSTypeName(q->qtype), q->SuppressUnusable);
// If we get error from any DNS server, remember the error. If all of the servers,
// return the error, then return the first error.
if (mDNSOpaque16IsZero(q->responseFlags))
q->responseFlags = responseFlags;
// After we reset the qDNSServer to NULL, we could get more SERV_FAILS that might end up
// peanlizing again.
if (!q->qDNSServer) goto end;
// 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
{
}
}
else
{
LogInfo("PenalizeDNSServer: Strict Unicast Ordering is TRUE");
}
end:
new = GetServerForQuestion(m, q);
{
if (new)
{
LogMsg("PenalizeDNSServer: ERROR!! GetServerForQuestion returned the same server %#a:%d", &new->addr,
q->ThisQInterval = 0; // Inactivate this question so that we dont bombard the network
}
else
{
// When we have no more DNS servers, we might end up calling PenalizeDNSServer multiple
// times when we receive SERVFAIL from delayed packets in the network e.g., DNS server
// is slow in responding and we have sent three queries. When we repeatedly call, it is
// okay to receive the same NULL DNS server. Next time we try to send the query, we will
// realize and re-initialize the DNS servers.
LogInfo("PenalizeDNSServer: GetServerForQuestion returned the same server NULL");
}
}
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)
{
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. We defer this up until we generate
// a negative cache response in uDNS_CheckCurrentQuestion.
//
// 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;
}
}
// ***************************************************************************
#endif
{
const domainname *n = name;
while (n->c[0])
{
{
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
{
DomainAuthInfo **p = &m->AuthInfoList;
mDNS_CheckLock(m);
// First purge any dead keys from the list
while (*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
{
debugf("GetAuthInfoForName_internal updated q->AuthInfo from %##s to %##s for %##s (%s)",
}
// 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)
}
else
p = &(*p)->next;
}
return(GetAuthInfoForName_direct(m, name));
}
{
DomainAuthInfo *d;
mDNS_Lock(m);
d = GetAuthInfoForName_internal(m, name);
mDNS_Unlock(m);
return(d);
}
// MUST be called with the lock held
const domainname *domain, const domainname *keyname, const char *b64keydata, const domainname *hostname, mDNSIPPort *port, mDNSBool autoTunnel)
{
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", domain->c, keyname->c, autoTunnel ? " AutoTunnel" : "");
if (hostname)
else
if (port)
else
{
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
if (*p) {LogInfo("mDNS_SetSecretForDomain: Domain %##s Already in list", (*p)->domain.c); return(mStatus_AlreadyRegistered);}
// Caution: Only zero AutoTunnelHostRecord.namestorage AFTER we've determined that this is a NEW DomainAuthInfo
// being added to the list. Otherwise we risk smashing our AutoTunnel host records that are already active and in use.
*p = info;
// Check to see if adding this new DomainAuthInfo has changed the credentials for any of our questions
{
{
debugf("mDNS_SetSecretForDomain updating q->AuthInfo from %##s to %##s for %##s (%s)",
}
}
return(mStatus_NoError);
}
// ***************************************************************************
#pragma mark -
#endif
// and do so when necessary
{
if (!m->NATTraversals)
{
LogInfo("uDNS_RequestAddress: Setting retryGetAddr to future");
}
else if (m->timenow - m->retryGetAddr >= 0)
{
{
#ifdef _LEGACY_NAT_TRAVERSAL_
{
debugf("uDNS_RequestAddress: LNT_SendDiscoveryMsg");
}
else
{
if (lnterr)
}
#endif // _LEGACY_NAT_TRAVERSAL_
}
// Always update the interval and retry time, so that even if we fail to send the
// packet, we won't spin in an infinite loop repeatedly failing to send the packet
if (m->retryIntervalGetAddr < NATMAP_INIT_RETRY)
{
}
{
m->retryIntervalGetAddr *= 2;
}
else
{
}
}
else
{
debugf("uDNS_RequestAddress: Not time to send address request");
}
// Always update NextScheduledNATOp, even if we didn't change retryGetAddr, so we'll
// be called when we need to send the request(s)
if (m->NextScheduledNATOp - m->retryGetAddr > 0)
m->NextScheduledNATOp = m->retryGetAddr;
return err;
}
{
if (!info)
{
LogMsg("uDNS_SendNATMsg called unexpectedly with NULL info");
return mStatus_BadParamErr;
}
// send msg if the router's address is private (which means it's non-zero)
{
if (!usePCP)
{
if (!info->sentNATPMP)
{
{
static NATPortMapRequest NATPortReq;
err = mDNSPlatformSendUDP(m, (mDNSu8 *)&NATPortReq, end, 0, mDNSNULL, &m->Router, NATPMPPort, mDNSfalse);
}
// In case the address request already went out for another NAT-T,
// set the NewAddress to the currently known global external address, so
// Address-only operations will get the callback immediately
// Remember that we just sent a NAT-PMP packet, so we won't resend one later.
// We do this because the NAT-PMP "Unsupported Version" response has no
// information about the (PCP) request that triggered it, so we must send
// NAT-PMP requests for all operations. Without this, we'll send n PCP
// requests for n operations, receive n NAT-PMP "Unsupported Version"
// responses, and send n NAT-PMP requests for each of those responses,
// resulting in (n + n^2) packets sent. We only want to send 2n packets:
// n PCP requests followed by n NAT-PMP requests.
}
}
else
{
req.reservedMapOp[0] = 0;
else
// Since we only support IPv4, even if using the all-zeros address, map it, so
// the PCP gateway will give us an IPv4 address & not an IPv6 address.
// Unset the sentNATPMP flag, so that we'll send a NAT-PMP packet if we
// receive a NAT-PMP "Unsupported Version" packet. This will result in every
// renewal, retransmission, etc. being tried first as PCP, then if a NAT-PMP
// "Unsupported Version" response is received, fall-back & send the request
// using NAT-PMP.
#ifdef _LEGACY_NAT_TRAVERSAL_
{
debugf("uDNS_SendNATMsg: LNT_SendDiscoveryMsg");
}
else
{
if (lnterr)
}
#endif // _LEGACY_NAT_TRAVERSAL_
}
}
return(err);
}
{
NATTraversalInfo *n;
for (n = m->NATTraversals; n; n=n->next)
{
n->ExpiryTime = 0; // Mark this mapping as expired
n->retryPortMap = when;
#ifdef _LEGACY_NAT_TRAVERSAL_
if (n->tcpInfo.sock) { mDNSPlatformTCPCloseConnection(n->tcpInfo.sock); n->tcpInfo.sock = mDNSNULL; }
#endif // _LEGACY_NAT_TRAVERSAL_
}
m->retryIntervalGetAddr = 0;
m->retryGetAddr = when;
#ifdef _LEGACY_NAT_TRAVERSAL_
LNT_ClearState(m);
#endif // _LEGACY_NAT_TRAVERSAL_
}
{
NATTraversalInfo *n;
if (err)
{
}
else
{
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
}
// Globally remember the most recently discovered address, so it can be used in each
// new NATTraversal structure
m->ExtAddress = ExtAddr;
if (!err) // Success, back-off to maximum interval
else if (!last_err) // Failure after success, retry quickly (then back-off exponentially)
// else back-off normally in case of pathological failures
if (m->NextScheduledNATOp - m->retryGetAddr > 0)
m->NextScheduledNATOp = m->retryGetAddr;
for (n = m->NATTraversals; n; n=n->next)
{
// We should change n->NewAddress only when n is one of:
// because such an operation needs the update now. If the lastSuccessfulProtocol
// is currently none, then natTraversalHandlePortMapReplyWithAddress() will be
// 2) an address-only operation that did not succeed via PCP, because when such an
// operation succeeds via PCP, it's for the TCP discard port just to learn the
// address. And that address may be different than the external address
// is currently none, we must update the NewAddress as PCP may not succeed.
(n->Protocol ?
(n->lastSuccessfulProtocol == NATTProtocolNATPMP || n->lastSuccessfulProtocol == NATTProtocolUPNPIGD) :
(n->lastSuccessfulProtocol != NATTProtocolPCP)))
{
// Needs an update immediately
n->NewAddress = ExtAddr;
n->ExpiryTime = 0;
n->retryPortMap = m->timenow;
#ifdef _LEGACY_NAT_TRAVERSAL_
if (n->tcpInfo.sock) { mDNSPlatformTCPCloseConnection(n->tcpInfo.sock); n->tcpInfo.sock = mDNSNULL; }
#endif // _LEGACY_NAT_TRAVERSAL_
}
}
}
// Both places that call NATSetNextRenewalTime() update m->NextScheduledNATOp correctly afterwards
{
}
mDNSlocal void natTraversalHandlePortMapReplyWithAddress(mDNS *const m, NATTraversalInfo *n, const mDNSInterfaceID InterfaceID, mDNSu16 err, mDNSv4Addr extaddr, mDNSIPPort extport, mDNSu32 lease, NATTProtocol protocol)
{
const char *prot = n->Protocol == 0 ? "Add" : n->Protocol == NATOp_MapUDP ? "UDP" : n->Protocol == NATOp_MapTCP ? "TCP" : "???";
(void)prot;
{
LogInfo("natTraversalHandlePortMapReplyWithAddress: %p Response %s Port %5d External %.4a:%d lease %d error %d",
// No need to set m->NextScheduledNATOp here, since we're only ever extending the m->retryPortMap time
else if (err > NATErr_None && err <= NATErr_Opcode) n->NewResult = mStatus_NATPortMappingUnsupported;
}
else
{
LogInfo("natTraversalHandlePortMapReplyWithAddress: %p %s Response %s Port %5d External %.4a:%d changed to %.4a:%d lease %d",
n,
/* else */ "Unknown " ),
n->InterfaceID = InterfaceID;
n->NewAddress = extaddr;
if (n->Protocol) n->RequestedPort = extport; // Don't report the (PCP) external port to address-only operations
NATSetNextRenewalTime(m, n); // Got our port mapping; now set timer to renew it at halfway point
}
}
mDNSexport void natTraversalHandlePortMapReply(mDNS *const m, NATTraversalInfo *n, const mDNSInterfaceID InterfaceID, mDNSu16 err, mDNSIPPort extport, mDNSu32 lease, NATTProtocol protocol)
{
natTraversalHandlePortMapReplyWithAddress(m, n, InterfaceID, err, m->ExtAddress, extport, lease, protocol);
}
// Must be called with the mDNS_Lock held
{
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",
#if ForceAlerts
*(long*)0 = 0;
#endif
return(mStatus_AlreadyRegistered);
}
if (traversal->Protocol && traversal->Protocol == (*n)->Protocol && mDNSSameIPPort(traversal->IntPort, (*n)->IntPort) &&
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",
}
// Initialize necessary fields
traversal->ExpiryTime = 0;
// set default lease if necessary
#ifdef _LEGACY_NAT_TRAVERSAL_
#endif // _LEGACY_NAT_TRAVERSAL_
if (!m->NATTraversals) // If this is our first NAT request, kick off an address request too
{
m->retryGetAddr = m->timenow;
}
// If this is an address-only operation, initialize to the current global address,
// or (in non-PCP environments) we won't know the address until the next external
{
}
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
{
NATTraversalInfo *p;
else
{
return(mStatus_BadReferenceErr);
}
traversal->Protocol, mDNSVal16(traversal->IntPort), mDNSVal16(traversal->RequestedPort), traversal->NATLease);
if (m->CurrentNATTraversal == traversal)
// If there is a match for the operation being stopped, don't send a deletion request (unmap)
for (p = m->NATTraversals; p; p=p->next)
{
(!p->Protocol && traversal->Protocol == NATOp_MapTCP && mDNSSameIPPort(traversal->IntPort, DiscardPort))) :
{
LogInfo("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->retryInterval = 0;
// In case we most recently sent NAT-PMP, we need to set sentNATPMP to false so
// that we'll send a NAT-PMP request to destroy the mapping. We do this because
// the NATTraversal struct has already been cut from the list, and the client
// layer will destroy the memory upon returning from this function, so we can't
// try PCP first and then fall-back to NAT-PMP. That is, if we most recently
// now, because we won't get a chance later.
// Both NAT-PMP & PCP RFCs state that the suggested port in deletion requests
// should be zero. And for PCP, the suggested external address should also be
// zero, specifically, the all-zeros IPv4-mapped address, since we would only
// would have requested an IPv4 address.
}
// 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_
{
}
#endif // _LEGACY_NAT_TRAVERSAL_
return(mStatus_NoError);
}
{
mDNS_Lock(m);
mDNS_Unlock(m);
return(status);
}
{
mDNS_Lock(m);
mDNS_Unlock(m);
return(status);
}
// ***************************************************************************
#pragma mark -
#endif
// Lock must be held -- otherwise m->timenow is undefined
{
// 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
SetNextQueryTime(m, q);
#endif
}
mDNSlocal mDNSu8 *putLLQ(DNSMessage *const msg, mDNSu8 *ptr, const DNSQuestion *const question, const LLQOptData *const data)
{
//!!!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);
// 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);
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.
{
//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
{
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)
{
StartLLQPolling(m,q);
return;
}
if (!llq) // Retransmission: need to make a new LLQOptData
{
llqBuf.err = LLQErr_NoError; // Don't need to tell server UDP notification port when sending over UDP
}
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;
if (responsePtr)
{
mStatus err = mDNSSendDNSMessage(m, &m->omsg, responsePtr, mDNSInterface_Any, q->LocalSocket, &q->servAddr, q->servPort, mDNSNULL, mDNSNULL, mDNSfalse);
if (err) { LogMsg("sendChallengeResponse: mDNSSendDNSMessage%s failed: %d", q->tcp ? " (TCP)" : "", err); }
}
else StartLLQPolling(m,q);
}
{
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)
{
{ LogMsg("ERROR: recvSetupResponse %##s (%s) - rcode && rcode != kDNSFlag1_RC_NXDomain", q->qname.c, DNSTypeName(q->qtype)); return; }
{ LogMsg("ERROR: recvSetupResponse %##s (%s) - bad op %d", q->qname.c, DNSTypeName(q->qtype), llq->llqOp); return; }
{ 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; }
// cache expiration in case we go to sleep before finishing setup
// update state
q->state = LLQ_SecondaryRequest;
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))
{
}
if (llq->err) { LogMsg("ERROR: recvSetupResponse %##s (%s) code %d from server", q->qname.c, DNSTypeName(q->qtype), llq->err); StartLLQPolling(m,q); return; }
{ 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);
#endif
}
}
mDNSexport uDNS_LLQType uDNS_recvLLQResponse(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *const end,
{
DNSQuestion pktQ, *q;
{
{
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",
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));
{
// 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
// would kick us back to LLQInitialRequest. So, resetting the state here may not be useful.
//
// 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
// Double-NAT state.
{
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
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))
{
//debugf("Sending LLQ ack for %##s (%s)", q->qname.c, DNSTypeName(q->qtype));
if (ackEnd) mDNSSendDNSMessage(m, &m->omsg, ackEnd, mDNSInterface_Any, q->LocalSocket, srcaddr, srcport, mDNSNULL, mDNSNULL, mDNSfalse);
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 (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.
q->ntries = 0;
}
*matchQuestion = q;
return uDNS_LLQ_Ignore;
}
{
// 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 uDNS_LLQ_Not;
}
// Stub definition of TCPSocket_struct so we can access flags field. (Rest of TCPSocket_struct is platform-dependent.)
// 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 **backpointer =
q ? &q->tcp :
LogMsg("tcpCallback: %d backpointer %p incorrect tcpInfo %p question %p rr %p",
{
// 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
LogMsg("tcpCallback: ERROR: tcpInfo->rr->resrec.name %p != &tcpInfo->rr->namestorage %p",
// connection is established - send the message
{
// Lease renewal over TCP, resulting from opening a TCP connection in sendLLQRefresh
}
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
llqData.err = GetLLQEventPort(m, &tcpInfo->Addr); // We're using TCP; tell server what UDP port to send notifications to
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, (DNSSECQuestion(q) ? DNSSecQFlags : uQueryFlags));
end = putQuestion(&tcpInfo->request, tcpInfo->request.data, tcpInfo->request.data + AbsoluteMaxDNSMessageData, &q->qname, q->qtype, q->qclass);
{
if (q->ProxyQuestion)
end = DNSProxySetAttributes(q, &tcpInfo->request.h, &tcpInfo->request, end, tcpInfo->request.data + AbsoluteMaxDNSMessageData);
else
}
}
err = mDNSSendDNSMessage(m, &tcpInfo->request, end, mDNSInterface_Any, mDNSNULL, &tcpInfo->Addr, tcpInfo->Port, sock, AuthInfo, mDNSfalse);
if (err) { debugf("ERROR: tcpCallback: mDNSSendDNSMessage - %d", err); err = mStatus_UnknownErr; goto exit; }
// Record time we sent this question
if (q)
{
mDNS_Lock(m);
if (q->ThisQInterval < (256 * mDNSPlatformOneSecond)) // Now we have a TCP connection open, make sure we wait at least 256 seconds before retrying
SetNextQueryTime(m, q);
mDNS_Unlock(m);
}
}
else
{
long n;
const mDNSBool Read_replylen = (tcpInfo->nread < 2); // Do we need to read the replylen field first?
if (Read_replylen) // First read the two-byte length preceeding the DNS message
{
if (n < 0)
{
LogMsg("ERROR: tcpCallback - attempt to read message length failed (%d)", n);
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)
{
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
return;
}
}
{ LogMsg("ERROR: tcpCallback - length too short (%d bytes)", tcpInfo->replylen); err = mStatus_UnknownErr; goto exit; }
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)
{
// If this is our only read for this invokation, and it fails, then that's bad.
// But if we did successfully read some or all of the replylen field this time through,
// and this is now our second read from the socket, then it's expected that sometimes
// there may be no more data present, and that's perfectly okay.
// Assuming failure of the second read is a problem is what caused this bug:
// <rdar://problem/15043194> mDNSResponder fails to read DNS over TCP packet correctly
if (!Read_replylen) { LogMsg("ERROR: tcpCallback - read returned %d", n); err = mStatus_ConnFailed; }
goto exit;
}
else if (closed)
{
if (tcpInfo->numReplies == 0)
{
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
return;
}
}
{
tcpInfo->numReplies++;
tcpInfo->reply = mDNSNULL; // Detach reply buffer from tcpInfo_t, to make sure client callback can't cause it to be disposed
// 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 (backpointer)
if (!q || !q->LongLived || m->SleepState)
// USE CAUTION HERE: Invoking mDNSCoreReceive may have caused the environment to change, including canceling this operation itself
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
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.
if (q->LongLived)
{
// 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
if (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
{
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.
SetNextQueryTime(m, q);
LogMsg("tcpCallback: stream connection for %##s (%s) failed, retrying in %d ms", q->qname.c, DNSTypeName(q->qtype), q->ThisQInterval);
}
// 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.
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)
{
}
}
mDNS_Unlock(m);
}
}
mDNSlocal tcpInfo_t *MakeTCPConn(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *const end,
{
info->m = m;
info->requestLen = 0;
info->numReplies = 0;
if (msg)
{
}
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.
// 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.
else if (err != mStatus_ConnPending ) { LogInfo("MakeTCPConn: connection failed"); DisposeTCPConn(info); return(mDNSNULL); }
return(info);
}
{
}
// Lock must be held
{
{
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
SetNextQueryTime(m, q);
return;
}
// Either we don't have {PCP, NAT-PMP, UPnP/IGD} support (ExternalPort is zero) or behind a Double NAT that may or
{
LogInfo("startLLQHandshake: Cannot receive inbound packets; will poll for %##s (%s) External Port %d, NAT Result %d",
StartLLQPolling(m, q);
return;
}
if (mDNSIPPortIsZero(q->servPort))
{
q->ThisQInterval = LLQ_POLL_INTERVAL + mDNSRandom(LLQ_POLL_INTERVAL/10); // Retry in approx 15 minutes
SetNextQueryTime(m, q);
// We know q->servPort is zero because of check above
return;
}
if (PrivateQuery(q))
{
if (q->tcp) LogInfo("startLLQHandshake: Disposing existing TCP connection for %##s (%s)", q->qname.c, DNSTypeName(q->qtype));
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.
return;
}
{
// 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->ReqLease = kLLQ_DefLease;
q->ThisQInterval = 0;
}
SetNextQueryTime(m, q);
}
else
{
debugf("startLLQHandshake: m->AdvertisedV4 %#a%s Server %#a:%d%s %##s (%s)",
if (q->ntries++ >= kLLQ_MAX_TRIES)
{
StartLLQPolling(m, q);
}
else
{
// set llq rdata
llqData.err = LLQErr_NoError; // Don't need to tell server UDP notification port when sending over UDP
mDNSSendDNSMessage(m, &m->omsg, end, mDNSInterface_Any, q->LocalSocket, &q->servAddr, q->servPort, mDNSNULL, mDNSNULL, mDNSfalse);
// update question state
q->state = LLQ_InitialRequest;
q->ReqLease = kLLQ_DefLease;
SetNextQueryTime(m, q);
}
}
}
// forward declaration so GetServiceTarget can do reverse lookup if needed
{
if (!rr->AutoTarget) // If not automatically tracking this host's current name, just return the existing target
else
{
{
}
else
#endif // APPLE_OSX_mDNSResponder
{
int best = 0;
{
if (SameDomainName(SkipLeadingLabels(rr->resrec.name, srvcount - x), SkipLeadingLabels(&hi->fqdn, hostcount - x)))
}
}
if (m->StaticHostname.c[0]) return(&m->StaticHostname);
else GetStaticHostname(m); // asynchronously do reverse lookup for primary IPv4 address
return(mDNSNULL);
}
}
mDNSlocal const domainname *PUBLIC_UPDATE_SERVICE_TYPE = (const domainname*)"\x0B_dns-update" "\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
// 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)
{
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
{
mDNS_StopQuery(m, question);
LogMsg("GetZoneData_QuestionCallback: Question %##s (%s) ThisQInterval %d not -1", question->qname.c, DNSTypeName(question->qtype), question->ThisQInterval);
{
}
else if (zd->CurrentSOA->c[0])
{
{
// To keep the load on the server down, we don't chop down on
// SOA lookups for AutoTunnels
}
else
{
}
}
else
{
}
}
{
mDNS_StopQuery(m, question);
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
{
zd->ZonePrivate = mDNSfalse; // Causes ZoneDataSRV() to yield a different SRV name when building the query
}
else
#endif
{
{
}
else
{
}
}
}
{
mDNS_StopQuery(m, question);
LogMsg("GetZoneData_QuestionCallback: Question %##s (%s) ThisQInterval %d not -1", question->qname.c, DNSTypeName(question->qtype), question->ThisQInterval);
else
// 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
#endif
// The caller needs to free the memory when done with zone data
}
}
// GetZoneData_StartQuery is called from normal client context (lock not held, or client callback)
{
if (qtype == kDNSType_SRV)
{
}
// 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.qname.c[0] = 0; // Already set
//LogMsg("GetZoneData_StartQuery %##s (%s) %p", zd->question.qname.c, DNSTypeName(zd->question.qtype), zd->question.Private);
}
// 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)
{
int initialskip = (AuthInfo && AuthInfo->AutoTunnel) ? DomainNameLength(name) - DomainNameLength(&AuthInfo->domain) : 0;
mDNS_DropLockBeforeCallback(); // GetZoneData_StartQuery expects to be called from a normal callback, so we emulate that here
{
// 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.
}
else
{
if (AuthInfo && AuthInfo->AutoTunnel) LogInfo("StartGetZoneData: Not Bypassing SOA, SRV query for %##s", AuthInfo->domain.c);
}
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.
{
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
{
return(GetAuthInfoForName_internal(m, &q->qname));
}
// ***************************************************************************
#endif
// When this function is called, service record is already deregistered. We just
// have to deregister the PTR and TXT records.
{
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; }
for (r = m->ResourceRecords; r; r=r->next)
{
if (!AuthRecord_uDNS(r)) continue;
srvRR = r->Additional1;
srvRR = r->DependentOn;
LogMsg("UpdateAllServiceRecords: ERROR!! Resource record %s wrong, expecting SRV type", ARDisplayString(m, srvRR));
{
if (!reg)
{
r->SRVChanged = mDNStrue;
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) ||
else
{
ActivateUnicastRegistration(m, r);
}
}
}
}
}
// Called in normal client context (lock not held)
// Currently only supports SRV records for nat mapping
{
const domainname *target;
debugf("SRVNatMap complete %.4a IntPort %u ExternalPort %u NATLease %u", &n->ExternalAddress, mDNSVal16(n->IntPort), mDNSVal16(n->ExternalPort), n->NATLease);
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.
{
// We need to clear out the NATinfo state so that it will result in re-acquiring the mapping
// and hence this callback called again.
{
}
return;
}
mDNS_Lock(m);
// Reevaluate the target always as Target could have changed while
// we were getting the port mapping (See UpdateOneSRVRecord)
{
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));
mDNS_Unlock(m);
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.
{
}
// 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
// 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.
// 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
// Delay the record registration by MERGE_DELAY_TIME so that we can merge them
// into one update
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.
}
{
const mDNSu8 *p;
{
LogInfo("StartRecordNatMap: Resource Record %##s type %d, not supported", rr->resrec.name->c, rr->resrec.rrtype);
return;
}
//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];
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));
// Shouldn't be trying to set IntPort here --
// BuildUpdateMessage overwrites srs->RR_SRV.resrec.rdata->u.srv.port with external (mapped) port number
}
// 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.
{
if (*list)
{
// Temporary workaround to cancel any active NAT mapping operation
{
}
return(mStatus_NoError);
}
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.
{
// 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
UnlinkResourceRecord(m, rr);
mDNS_Register_internal(m, rr);
// Register the other records
}
// Called with lock held
{
// 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
mDNSBool TargetChanged = (newtarget->c[0] && rr->state == regState_NoTarget) || !SameDomainName(curtarget, newtarget);
// 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
mDNSBool NowNeedNATMAP = (rr->AutoTarget == Target_AutoHostAndNATMAP && !mDNSIPPortIsZero(port) && mDNSv4AddrIsRFC1918(&m->AdvertisedV4.ip.v4) && rr->nta && !mDNSAddrIsRFC1918(&rr->nta->Addr));
mDNSBool PortWasMapped = (rr->NATinfo.clientContext && !mDNSSameIPPort(rr->NATinfo.RequestedPort, port)); // I think this is always false -- SC Sept 07
(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",
TargetChanged, HaveZoneData, mDNSVal16(port), NowNeedNATMAP, WereBehindNAT, PortWasMapped, NATChanged);
mDNS_CheckLock(m);
if (!TargetChanged && !NATChanged) return;
{
LogInfo("UpdateOneSRVRecord: Deregistering record, Ignoring TargetChanged %d, NATChanged %d for %##s, state %d", TargetChanged, NATChanged,
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);
{
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:
// 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])
{
return;
}
return;
case regState_DeregPending:
// We are in DeregPending either because the service was deregistered from above or we handled
// 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.
//
// called as part of service deregistration. When the response comes back, we call
// kDNSRecordTypeDeregistering state.
//
// 3. If the upper layer deregisters the service, we check for kDNSRecordTypeDeregistering both
// 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
if (newtarget->c[0])
{
LogInfo("UpdateOneSRVRecord: SRV record changed for service %##s, registering with new target %##s",
}
else
{
LogInfo("UpdateOneSRVRecord: SRV record changed for service %##s de-registering", rr->resrec.name->c);
}
return;
case regState_Unregistered:
}
}
{
m->NextSRVUpdate = 0;
if (m->CurrentRecord)
LogMsg("UpdateAllSRVRecords ERROR m->CurrentRecord already set %s", ARDisplayString(m, m->CurrentRecord));
m->CurrentRecord = m->ResourceRecords;
while (m->CurrentRecord)
{
UpdateOneSRVRecord(m, rptr);
}
}
// Forward reference: AdvertiseHostname references HostnameCallback, and HostnameCallback calls AdvertiseHostname
// Called in normal client context (lock not held)
{
if (!h) { LogMsg("RegisterHostnameRecord: registration cancelled"); return; }
if (!n->Result)
{
{
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,
}
else
{
LogInfo("Advertising hostname %##s IPv4 %.4a (NAT gateway's external address)", h->arv4.resrec.name->c, &n->ExternalAddress);
mDNS_Register(m, &h->arv4);
}
}
}
// register record or begin NAT traversal
{
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);
{
// If we already have a NAT query active, stop it and restart it to make sure we get another callback
h->natinfo.clientContext = h;
mDNS_StartNATOperation_internal(m, &h->natinfo);
}
else
{
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);
mDNS_Register_internal(m, &h->arv6);
}
}
{
if (result == mStatus_MemFree)
{
if (hi)
{
// If we're still in the Hostnames list, update to new address
HostnameInfo *i;
// Else, we're not still in the Hostnames list, so free the memory
{
}
}
return;
}
if (result)
{
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 (rr->state != regState_Unregistered) LogMsg("Error: HostnameCallback invoked with error code for record not in regState_Unregistered!");
{
// only deliver status if both v4 and v6 fail
if (hi->StatusCallback)
}
return;
}
// register any pending services that require a target
mDNS_Lock(m);
mDNS_Unlock(m);
// Deliver success to client
else
if (hi->StatusCallback)
}
mDNSlocal void FoundStaticHostname(mDNS *const m, DNSQuestion *question, const ResourceRecord *const answer, QC_result AddRecord)
{
HostnameInfo *h = m->Hostnames;
(void)question;
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");
{
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
return;
}
h = h->next;
}
mDNS_Lock(m);
mDNS_Unlock(m);
}
{
mDNS_Lock(m);
storedname->c[0] = 0;
mDNS_Unlock(m);
}
}
// Called with lock held
{
char buf[MAX_REVERSE_MAPPING_NAME_V4];
DNSQuestion *q = &m->ReverseMap;
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
if (!MakeDomainNameFromDNSNameString(&q->qname, buf)) { LogMsg("Error: GetStaticHostname - bad name %s", buf); return; }
q->InterfaceID = mDNSInterface_Any;
q->flags = 0;
q->qtype = kDNSType_PTR;
q->qclass = kDNSClass_IN;
q->ExpectUnique = mDNSfalse;
q->ForceMCast = mDNSfalse;
q->ReturnIntermed = mDNStrue;
q->SuppressUnusable = mDNSfalse;
q->DenyOnCellInterface = mDNSfalse;
q->DenyOnExpInterface = mDNSfalse;
q->SearchListIndex = 0;
q->AppendSearchDomains = 0;
q->TimeoutQuestion = 0;
q->WakeOnResolve = 0;
q->ValidationRequired = 0;
q->ValidatingResponse = 0;
q->ProxyQuestion = 0;
q->pid = mDNSPlatformGetPID();
q->QuestionContext = mDNSNULL;
err = mDNS_StartQuery_internal(m, q);
}
mDNSexport void mDNS_AddDynDNSHostName(mDNS *m, const domainname *fqdn, mDNSRecordCallback *StatusCallback, const void *StatusContext)
{
// allocate and format new address record
AdvertiseHostname(m, *ptr);
}
{
else
{
// We do it this way because, if we have no active v6 record, the "mDNS_Deregister_internal(m, &hi->arv4);"
// below could free the memory, and we have to make sure we don't touch hi fields after that.
mDNSBool f4 = hi->arv4.resrec.RecordType != kDNSRecordTypeUnregistered && hi->arv4.state != regState_Unregistered;
mDNSBool f6 = hi->arv6.resrec.RecordType != kDNSRecordTypeUnregistered && hi->arv6.state != regState_Unregistered;
// When both deregistrations complete we'll free the memory in the mStatus_MemFree callback
}
mDNS_CheckLock(m);
}
// Currently called without holding the lock
// Maybe we should change that?
mDNSexport void mDNS_SetPrimaryInterfaceInfo(mDNS *m, const mDNSAddr *v4addr, const mDNSAddr *v6addr, const mDNSAddr *router)
{
if (m->mDNS_busy != m->mDNS_reentrancy)
LogMsg("mDNS_SetPrimaryInterfaceInfo: mDNS_busy (%ld) != mDNS_reentrancy (%ld)", m->mDNS_busy, m->mDNS_reentrancy);
if (v4addr && v4addr->type != mDNSAddrType_IPv4) { LogMsg("mDNS_SetPrimaryInterfaceInfo v4 address - incorrect type. Discarding. %#a", v4addr); return; }
if (v6addr && v6addr->type != mDNSAddrType_IPv6) { LogMsg("mDNS_SetPrimaryInterfaceInfo v6 address - incorrect type. Discarding. %#a", v6addr); return; }
if (router && router->type != mDNSAddrType_IPv4) { LogMsg("mDNS_SetPrimaryInterfaceInfo passed non-v4 router. Discarding. %#a", router); return; }
mDNS_Lock(m);
if (v6addr)
else
v6 = zerov6Addr;
// setting router to zero indicates that nat mappings must be reestablished when router is reset
{
HostnameInfo *i;
LogInfo("mDNS_SetPrimaryInterfaceInfo: %s%s%s%#a %#a %#a",
{
{
}
{
}
// AdvertiseHostname will only register new address records.
// For records still in the process of deregistering it will ignore them, and let the mStatus_MemFree callback handle them.
AdvertiseHostname(m, i);
}
if (v4Changed || RouterChanged)
{
// If we have a non-zero IPv4 address, we should try immediately to see if we have a NAT gateway
// If we have no IPv4 address, we don't want to be in quite such a hurry to report failures to our clients
// <rdar://problem/6935929> Sleeping server sometimes briefly disappears over Back to My Mac after it wakes up
NATTraversalInfo *n;
m->ExtAddress = zerov4Addr;
for (n = m->NATTraversals; n; n=n->next)
n->NewAddress = zerov4Addr;
LogInfo("mDNS_SetPrimaryInterfaceInfo:%s%s: recreating NAT mappings in %d seconds",
}
m->StaticHostname.c[0] = 0;
#endif
}
mDNS_Unlock(m);
}
// ***************************************************************************
#endif
mDNSlocal mStatus ParseTSIGError(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *const end, const domainname *const displayname)
{
int i;
for (i = 0; i < msg->h.numAdditionals; i++)
{
if (m->rec.r.resrec.RecordType != kDNSRecordTypePacketNegative && m->rec.r.resrec.rrtype == kDNSType_TSIG)
{
else if (err == TSIG_ErrBadTime) { LogMsg("%##s: bad time", displayname->c); err = mStatus_BadTime; }
else if (err) { LogMsg("%##s: unknown tsig error %d", displayname->c, err); err = mStatus_UnknownErr; }
goto finish;
}
}
return err;
}
mDNSlocal mStatus checkUpdateResult(mDNS *const m, const domainname *const displayname, const mDNSu8 rcode, const DNSMessage *const msg, const mDNSu8 *const end)
{
(void)msg; // currently unused, needed for TSIG errors
if (!rcode) return mStatus_NoError;
else if (rcode == kDNSFlag1_RC_YXDomain)
{
return mStatus_NameConflict;
}
else if (rcode == kDNSFlag1_RC_Refused)
{
return mStatus_Refused;
}
else if (rcode == kDNSFlag1_RC_NXRRSet)
{
return mStatus_NoSuchRecord;
}
else if (rcode == kDNSFlag1_RC_NotAuth)
{
// TSIG errors should come with FormErr as per RFC 2845, but BIND 9 sends them with NotAuth so we look here too
if (!tsigerr)
{
return mStatus_UnknownErr;
}
else return tsigerr;
}
else if (rcode == kDNSFlag1_RC_FormErr)
{
if (!tsigerr)
{
return mStatus_UnknownErr;
}
else return tsigerr;
}
else
{
return mStatus_UnknownErr;
}
}
// We add three Additional Records for unicast resource record registrations
// which is a function of AuthInfo and AutoTunnel properties
{
// OPT RR : Emptyname(.) + base size + rdataOPT
// HINFO: Resource Record Name + base size + RDATA
// HINFO is added only for autotunnels
hinfoSize = 0;
//TSIG: Resource Record Name + base size + RDATA
// RDATA:
// Algorithm name: hmac-md5.sig-alg.reg.int (8+7+3+3 + 5 bytes for length = 26 bytes)
// Time: 6 bytes
// Fudge: 2 bytes
// Mac Size: 2 bytes
// Mac: 16 bytes
// ID: 2 bytes
// Error: 2 bytes
// Len: 2 bytes
// Total: 58 bytes
tsigSize = 0;
}
//Note: Make sure that RREstimatedSize is updated accordingly if anything that is done here
//would modify rdlength/rdestimate
{
//If this record is deregistering, then just send the deletion record
{
return ptr;
}
// This is a common function to both sending an update in a group or individual
// records separately. Hence, we change the state here.
// For Advisory records like e.g., _services._dns-sd, which is shared, don't send goodbyes as multiple
// host might be registering records and deregistering from one does not make sense
{
}
{
// delete old RData
if (!(ptr = putDeletionRecordWithLimit(&m->omsg, ptr, &rr->resrec, limit))) goto exit; // delete old rdata
// add new RData
if (!(ptr = PutResourceRecordTTLWithLimit(&m->omsg, ptr, &m->omsg.h.mDNS_numUpdates, &rr->resrec, rr->resrec.rroriginalttl, limit))) goto exit;
}
else
{
if (rr->resrec.RecordType == kDNSRecordTypeKnownUnique || rr->resrec.RecordType == kDNSRecordTypeVerified)
{
// KnownUnique : Delete any previous value
// For Unicast registrations, we don't verify that it is unique, but set to verified and hence we want to
// delete any previous value
}
{
// For now don't do this, until we have the logic for intelligent grouping of individual records into logical service record sets
//ptr = putPrereqNameNotInUse(rr->resrec.name, &m->omsg, ptr, end);
}
ptr = PutResourceRecordTTLWithLimit(&m->omsg, ptr, &m->omsg.h.mDNS_numUpdates, &rr->resrec, rr->resrec.rroriginalttl, limit);
}
return ptr;
exit:
return mDNSNULL;
}
// Called with lock held
{
// For the ability to register large TXT records, we limit the single record registrations
// to AbsoluteMaxDNSMessageData
mDNS_CheckLock(m);
{
// We never call this function when there is no zone information . Log a message if it ever happens.
LogMsg("SendRecordRegistration: No Zone information, should not happen %s", ARDisplayString(m, rr));
return;
}
// set zone
{
}
{
if (rr->tcp) LogInfo("SendRecordRegistration: Disposing existing TCP connection for %s", ARDisplayString(m, rr));
if (!rr->nta) { LogMsg("SendRecordRegistration:Private:ERROR!! nta is NULL for %s", ARDisplayString(m, rr)); return; }
rr->tcp = MakeTCPConn(m, &m->omsg, ptr, kTCPSocketFlags_UseTLS, &rr->nta->Addr, rr->nta->Port, &rr->nta->Host, mDNSNULL, rr);
}
else
{
if (!rr->nta) { LogMsg("SendRecordRegistration:ERROR!! nta is NULL for %s", ARDisplayString(m, rr)); return; }
err = mDNSSendDNSMessage(m, &m->omsg, ptr, mDNSInterface_Any, mDNSNULL, &rr->nta->Addr, rr->nta->Port, mDNSNULL, GetAuthInfoForName_internal(m, rr->resrec.name), mDNSfalse);
}
SetRecordRetry(m, rr, 0);
return;
exit:
LogMsg("SendRecordRegistration: Error formatting message for %s, disabling further updates", ARDisplayString(m, rr));
// Disable this record from future updates
}
// Is the given record "rr" eligible for merging ?
{
(void) m; //unused
// A record is eligible for merge, if the following properties are met.
//
// 1. uDNS Resource Record
// 2. It is time to send them now
// 3. It is in proper state
// 4. Update zone has been resolved
// 5. if DomainAuthInfo exists for the zone, it should not be soon deleted
// 6. Zone information is present
// 7. Update server is not zero
// 8. It has a non-null zone
// 9. It uses a lease option
// 10. DontMerge is not set
//
// Following code is implemented as separate "if" statements instead of one "if" statement
// is for better debugging purposes e.g., we know exactly what failed if debugging turned on.
{ debugf("IsRecordMergeable: Time %d not reached for %s", rr->LastAPTime + rr->ThisAPInterval - m->timenow, ARDisplayString(m, rr)); return mDNSfalse; }
if (info && info->deltime && m->timenow - info->deltime >= 0) {debugf("IsRecordMergeable: Domain %##s will be deleted soon", info->domain.c); return mDNSfalse;}
if (rr->state != regState_DeregPending && rr->state != regState_Pending && rr->state != regState_Registered && rr->state != regState_Refresh && rr->state != regState_UpdatePending)
{ debugf("IsRecordMergeable: state %d not right %s", rr->state, ARDisplayString(m, rr)); return mDNSfalse; }
if (rr->mState == mergeState_DontMerge) {debugf("IsRecordMergeable Dontmerge true %s", ARDisplayString(m, rr)); return mDNSfalse;}
return mDNStrue;
}
// Is the resource record "rr" eligible to merge to with "currentRR" ?
mDNSlocal mDNSBool AreRecordsMergeable(mDNS *const m, AuthRecord *currentRR, AuthRecord *rr, mDNSs32 time)
{
// A record is eligible to merge with another record as long it is eligible for merge in itself
// and it has the same zone information as the other record
{ debugf("AreRecordMergeable zone mismatch current rr Zone %##s, rr zone %##s", currentRR->zone->c, rr->zone->c); return mDNSfalse; }
return mDNStrue;
}
// If we can't build the message successfully because of problems in pre-computing
// the space, we disable merging for all the current records
{
AuthRecord *rr;
{
// Restarting the registration is much simpler than saving and restoring
// the exact time
}
}
mDNSlocal void SendGroupRRMessage(mDNS *const m, AuthRecord *anchorRR, mDNSu8 *ptr, DomainAuthInfo *info)
{
// This has to go in the additional section and hence need to be done last
if (!ptr)
{
LogMsg("SendGroupRRMessage: ERROR: Could not put lease option, failing the group registration");
// if we can't put the lease, we need to undo the merge
RRMergeFailure(m);
return;
}
{
if (anchorRR->tcp) debugf("SendGroupRRMessage: Disposing existing TCP connection for %s", ARDisplayString(m, anchorRR));
if (!anchorRR->nta) { LogMsg("SendGroupRRMessage:ERROR!! nta is NULL for %s", ARDisplayString(m, anchorRR)); return; }
anchorRR->tcp = MakeTCPConn(m, &m->omsg, ptr, kTCPSocketFlags_UseTLS, &anchorRR->nta->Addr, anchorRR->nta->Port, &anchorRR->nta->Host, mDNSNULL, anchorRR);
if (!anchorRR->tcp) LogInfo("SendGroupRRMessage: Cannot establish TCP connection for %s", ARDisplayString(m, anchorRR));
else LogInfo("SendGroupRRMessage: Sent a group update ID: %d start %p, end %p, limit %p", mDNSVal16(m->omsg.h.id), m->omsg.data, ptr, limit);
}
else
{
mStatus err = mDNSSendDNSMessage(m, &m->omsg, ptr, mDNSInterface_Any, mDNSNULL, &anchorRR->nta->Addr, anchorRR->nta->Port, mDNSNULL, info, mDNSfalse);
if (err) LogInfo("SendGroupRRMessage: Cannot send UDP message for %s", ARDisplayString(m, anchorRR));
else LogInfo("SendGroupRRMessage: Sent a group UDP update ID: %d start %p, end %p, limit %p", mDNSVal16(m->omsg.h.id), m->omsg.data, ptr, limit);
}
return;
}
// As we always include the zone information and the resource records contain zone name
// at the end, it will get compressed. Hence, we subtract zoneSize and add two bytes for
// the compression pointer
{
int rdlength;
// Note: Estimation of the record size has to mirror the logic in BuildUpdateMessage, otherwise estimation
// would be wrong. Currently BuildUpdateMessage calls SetNewRData in UpdatePending case. Hence, we need
// to account for that here. Otherwise, we might under estimate the size.
// old RData that will be deleted
// new RData that will be added
else
{
debugf("RREstimatedSize: ResourceRecord %##s (%s), DomainNameLength %d, zoneSize %d, rdestimate %d",
rr->resrec.name->c, DNSTypeName(rr->resrec.rrtype), DomainNameLength(rr->resrec.name), zoneSize, rdlength);
}
if (rr->resrec.RecordType == kDNSRecordTypeKnownUnique || rr->resrec.RecordType == kDNSRecordTypeVerified)
{
// Deletion Record: Resource Record Name + Base size (10) + 0
// Record: Resource Record Name (Compressed = 2) + Base size (10) + rdestimate
debugf("RREstimatedSize: ResourceRecord %##s (%s), DomainNameLength %d, zoneSize %d, rdestimate %d",
rr->resrec.name->c, DNSTypeName(rr->resrec.rrtype), DomainNameLength(rr->resrec.name), zoneSize, rdlength);
}
else
{
}
}
{
AuthRecord *rr;
// Look for records that needs to be sent in the next two seconds (MERGE_DELAY_TIME is set to 1 second).
// The logic is as follows.
//
// 1. Record 1 finishes getting zone data and its registration gets delayed by 1 second
// 2. Record 2 comes 0.1 second later, finishes getting its zone data and its registration is also delayed by
// 1 second which is now scheduled at 1.1 second
//
// By looking for 1 second into the future (m->timenow + MERGE_DELAY_TIME below does that) we have merged both
// of the above records. Note that we can't look for records too much into the future as this will affect the
// retry logic. The first retry is scheduled at 3 seconds. Hence, we should always look smaller than that.
// Anything more than one second will affect the first retry to happen sooner.
//
// Note: As a side effect of looking one second into the future to facilitate merging, the retries happen
// one second sooner.
{
if (!firstRR)
{
}
if (rr->SendRNow) LogMsg("MarkRRForSending: Resourcerecord %s already marked for sending", ARDisplayString(m, rr));
}
// get registered at different times but we want the refreshes to be all merged and sent
// as one update. Hence, we accelerate some of the records so that they will sync up in
// the future. Look at the records excluding the ones that we have already sent in the
// previous pass. If it half way through its scheduled refresh/retransmit, merge them
// into this packet.
//
// Note that we only look at Registered/Refresh state to keep it simple. As we don't know
// whether the current update will fit into one or more packets, merging a resource record
// (which is in a different state) that has been scheduled for retransmit would trigger
// sending more packets.
if (firstRR)
{
int acc = 0;
{
continue;
acc++;
}
}
return firstRR;
}
{
AuthRecord *rr;
int nrecords = 0;
// We try to fit as many ResourceRecords as possible in AbsoluteNormal/MaxDNSMessageData. Before we start
// putting in resource records, we need to reserve space for a few things. Every group/packet should
// have the following.
//
// 1) Needs space for the Zone information (which needs to be at the beginning)
// 2) Additional section MUST have space for lease option, HINFO and TSIG option (which needs to
// to be at the end)
//
// In future we need to reserve space for the pre-requisites which also goes at the beginning.
// To accomodate pre-requisites in the future, first we walk the whole list marking records
// that can be sent in this packet and computing the space needed for these records.
// For TXT and SRV records, we delete the previous record if any by sending the same
// resource record with ANY RDATA and zero rdlen. Hence, we need to have space for both of them.
while (startRR)
{
nrecords = 0;
zoneSize = 0;
{
if (!anchorRR)
{
// Though we allow single record registrations for UDP to be AbsoluteMaxDNSMessageData (See
// SendRecordRegistration) to handle large TXT records, to avoid fragmentation we limit UDP
// message to NormalMaxDNSMessageData
else spaceleft = NormalMaxDNSMessageData;
if (spaceleft <= 0)
{
LogMsg("SendGroupUpdates: ERROR!!: spaceleft is zero at the beginning");
RRMergeFailure(m);
return mDNSfalse;
}
// Build the initial part of message before putting in the other records
msgid = mDNS_NewMessageID(m);
// We need zone information at the beginning of the packet. Length: ZNAME, ZTYPE(2), ZCLASS(2)
// zone has to be non-NULL for a record to be mergeable, hence it is safe to set/ examine zone
//without checking for NULL.
if (spaceleft <= 0)
{
LogMsg("SendGroupUpdates: ERROR no space for zone information, disabling merge");
RRMergeFailure(m);
return mDNSfalse;
}
if (!next)
{
LogMsg("SendGroupUpdates: ERROR! Cannot put zone, disabling merge");
RRMergeFailure(m);
return mDNSfalse;
}
}
{
// If we can't fit even a single message, skip it, it will be sent separately
// in CheckRecordUpdates
if (!nrecords)
{
LogInfo("SendGroupUpdates: Skipping message %s, spaceleft %d, rrSize %d", ARDisplayString(m, rr), spaceleft, rrSize);
// Mark this as not sent so that the caller knows about it
// We need to remove the merge delay so that we can send it immediately
}
else
{
LogInfo("SendGroupUpdates:1: Parsed %d records and sending using %s, spaceleft %d, rrSize %d", nrecords, ARDisplayString(m, anchorRR), spaceleft, rrSize);
}
break; // breaks out of for loop
}
LogInfo("SendGroupUpdates: Building a message with resource record %s, next %p, state %d, ttl %d", ARDisplayString(m, rr), next, rr->state, rr->resrec.rroriginalttl);
{
// We calculated the space and if we can't fit in, we had some bug in the calculation,
// disable merge completely.
RRMergeFailure(m);
return mDNSfalse;
}
// If our estimate was higher, adjust to the actual size
LogMsg("SendGroupUpdates: ERROR!! Record size estimation is wrong for %s, Estimate %d, Actual %d, state %d", ARDisplayString(m, rr), rrSize, next - oldnext, rr->state);
nrecords++;
// We could have sent an update earlier with this "rr" as anchorRR for which we never got a response.
// To preserve ordering, we blow away the previous connection before sending this.
// By setting the retry time interval here, we will not be looking at these records
// again when we return to CheckGroupRecordUpdates.
SetRecordRetry(m, rr, 0);
}
// Either we have parsed all the records or stopped at "rr" above due to lack of space
}
if (anchorRR)
{
LogInfo("SendGroupUpdates: Parsed %d records and sending using %s", nrecords, ARDisplayString(m, anchorRR));
}
return sentallRecords;
}
// Merge the record registrations and send them as a group only if they
// have same DomainAuthInfo and hence the same key to put the TSIG
{
// Keep sending as long as there is at least one record to be sent
while (MarkRRForSending(m))
{
if (!SendGroupUpdates(m))
{
// if everything that was marked was not sent, send them out individually
{
// SendRecordRegistrtion might delete the rr from list, hence
// dereference nextRR before calling the function
{
// Any records marked for sending should be eligible to be sent out
// immediately. Just being cautious
{ LogMsg("CheckGroupRecordUpdates: ERROR!! Resourcerecord %s not ready", ARDisplayString(m, rr)); continue; }
SendRecordRegistration(m, rr);
}
}
}
}
debugf("CheckGroupRecordUpdates: No work, returning");
return;
}
{
// we were registering/deeregistering
domainname *dt;
{
// we don't have a target, if we just derregistered, then we don't have to do anything
{
LogInfo("hndlSRVChanged: SRVChanged, No Target, SRV Deregistered for %##s, state %d", rr->resrec.name->c,
return;
}
// we don't have a target, if we just registered, we need to deregister
{
LogInfo("hndlSRVChanged: SRVChanged, No Target, Deregistering again %##s, state %d", rr->resrec.name->c, rr->state);
return;
}
LogInfo("hndlSRVChanged: Not in DeregPending or RegPending state %##s, state %d", rr->resrec.name->c, rr->state);
}
else
{
// If we were in registered state and SRV changed to NULL, we deregister and come back here
// if we have a target, we need to register again.
//
// if we just registered check to see if it is same. If it is different just re-register the
// SRV and its assoicated records
//
// UpdateOneSRVRecord takes care of re-registering all service records
{
rr->state = regState_NoTarget; // NoTarget will allow us to pick up new target OR nat traversal state
LogInfo("hndlSRVChanged: SRVChanged, Valid Target %##s, Registering all records for %##s, state %d",
UpdateOneSRVRecord(m, rr);
return;
}
// Target did not change while this record was registering. Hence, we go to
// Registered state - the state we started from.
}
}
// Called with lock held
{
mDNS_CheckLock(m);
LogInfo("hndlRecordUpdateReply: err %d ID %d state %d %s(%p)", err, mDNSVal16(rr->updateid), rr->state, ARDisplayString(m, rr), rr);
if (err == mStatus_BadSig || err == mStatus_BadKey || err == mStatus_BadTime) UpdateAutoTunnelDomainStatuses(m);
#endif
// Later when need to send an update, we will get the zone data again. Thus we avoid
// using stale information.
//
// Note: By clearing out the zone info here, it also helps better merging of records
// in some cases. For example, when we get out regState_NoTarget state e.g., move out
// of Double NAT, we want all the records to be in one update. Some BTMM records like
// _autotunnel6 and host records are registered/deregistered when NAT state changes.
// As they are re-registered the zone information is cleared out. To merge with other
// records that might be possibly going out, clearing out the information here helps
// as all of them try to get the zone data.
{
// We always expect the question to be stopped when we get a valid response from the server.
// If the zone info tries to change during this time, updateid would be different and hence
// this response should not have been accepted.
LogMsg("hndlRecordUpdateReply: ResourceRecord %s, zone info question %##s (%s) interval %d not -1",
ARDisplayString(m, rr), rr->nta->question.qname.c, DNSTypeName(rr->nta->question.qtype), rr->nta->question.ThisQInterval);
}
// that could have happened during that time.
{
debugf("hndlRecordUpdateReply: Received reply for deregister record %##s type %d", rr->resrec.name->c, rr->resrec.rrtype);
CompleteDeregistration(m, rr);
return;
}
// We are returning early without updating the state. When we come back from sleep we will re-register after
// re-initializing all the state as though it is a first registration. If the record can't be registered e.g.,
// no target, it will be deregistered. Hence, the updating to the right state should not matter when going
// to sleep.
if (m->SleepState)
{
// Need to set it to NoTarget state so that RecordReadyForSleep knows that
// we are done
return;
}
{
// deallocate old RData
}
if (rr->SRVChanged)
{
hndlSRVChanged(m, rr);
else
{
LogInfo("hndlRecordUpdateReply: Deregistered %##s (%s), state %d", rr->resrec.name->c, DNSTypeName(rr->resrec.rrtype), rr->state);
if (rr->state != regState_DeregPending) LogMsg("hndlRecordUpdateReply: ResourceRecord %s not in DeregPending state %d", ARDisplayString(m, rr), rr->state);
}
return;
}
{
if (!err)
{
}
else
{
// Retry without lease only for non-Private domains
LogMsg("hndlRecordUpdateReply: Registration of record %##s type %d failed with error %d", rr->resrec.name->c, rr->resrec.rrtype, err);
if (!rr->Private && rr->uselease && err == mStatus_UnknownErr && mDNSSameIPPort(UpdatePort, UnicastDNSPort))
{
LogMsg("hndlRecordUpdateReply: Will retry update of record %##s without lease option", rr->resrec.name->c);
SetNextuDNSEvent(m, rr);
return;
}
// Communicate the error to the application in the callback below
}
}
{
SetNextuDNSEvent(m, rr);
return;
}
// Don't invoke the callback on error as this may not be useful to the client.
// The client may potentially delete the resource record on error which we normally
// delete during deregistration
{
}
// CAUTION: MUST NOT do anything more with rr after calling rr->Callback(), because the client's callback function
// is allowed to do anything, including starting/stopping queries, registering/deregistering records, etc.
}
mDNSlocal void uDNS_ReceiveNATPMPPacket(mDNS *m, const mDNSInterfaceID InterfaceID, mDNSu8 *pkt, mDNSu16 len)
{
// Minimum NAT-PMP packet is vers (1) opcode (1) + err (2) = 4 bytes
// Read multi-byte error value (field is identical in a NATPortMapReply)
{
NATTraversalInfo *n;
LogInfo("NAT-PMP version unsupported message received");
for (n = m->NATTraversals; n; n=n->next)
{
// Send a NAT-PMP request for this operation as needed
// and update the state variables
uDNS_SendNATMsg(m, n, mDNSfalse);
}
m->NextScheduledNATOp = m->timenow;
return;
}
// The minimum reasonable NAT-PMP packet length is vers (1) + opcode (1) + err (2) + upseconds (4) = 8 bytes
// If it's not at least this long, bail before we byte-swap the upseconds field & overrun our buffer.
// The retry timer will ensure we converge to correctness.
if (len < 8)
{
return;
}
// Read multi-byte upseconds value (field is identical in a NATPortMapReply)
AddrReply->upseconds = (mDNSs32) ((mDNSs32)pkt[4] << 24 | (mDNSs32)pkt[5] << 16 | (mDNSs32)pkt[6] << 8 | pkt[7]);
debugf("uDNS_ReceiveNATPMPPacket %X upseconds %u nat_elapsed %d our_elapsed %d", AddrReply->opcode, AddrReply->upseconds, nat_elapsed, our_elapsed);
// We compute a conservative estimate of how much the NAT gateways's clock should have advanced
// 1. We subtract 12.5% from our own measured elapsed time, to allow for NAT gateways that have an inacurate clock that runs slowly
// 2. We add a two-second safety margin to allow for rounding errors: e.g.
// -- if NAT gateway sends a packet at t=2.000 seconds, then one at t=7.999, that's approximately 6 real seconds,
// but based on the values in the packet (2,7) the apparent difference according to the packet is only 5 seconds
// we could receive the t=2 packet at our t=1.999 seconds, which we round down to 1
// and the t=7.999 packet at our t=8.000 seconds, which we record as 8,
// giving an apparent local time difference of 7 seconds
// The two-second safety margin coves this possible calculation discrepancy
{ LogMsg("NAT-PMP epoch time check failed: assuming NAT gateway %#a rebooted", &m->Router); RecreateNATMappings(m, 0); }
m->LastNATReplyLocalTime = m->timenow;
#ifdef _LEGACY_NAT_TRAVERSAL_
LNT_ClearState(m);
#endif // _LEGACY_NAT_TRAVERSAL_
{
static char msgbuf[16];
mDNSASLLog((uuid_t *)&m->asl_uuid, "natt.natpmp.AddressRequest", AddrReply->err ? "failure" : "success", msgbuf, "");
#endif
if (!AddrReply->err && len < sizeof(NATAddrReply)) { LogMsg("NAT-PMP AddrResponse message too short (%d bytes)", len); return; }
}
{
static char msgbuf[16];
mDNS_snprintf(msgbuf, sizeof(msgbuf), "%s - %d", AddrReply->opcode == NATOp_MapUDPResponse ? "UDP" : "TCP", PortMapReply->err);
mDNSASLLog((uuid_t *)&m->asl_uuid, "natt.natpmp.PortMapRequest", PortMapReply->err ? "failure" : "success", msgbuf, "");
#endif
if (!PortMapReply->err)
{
if (len < sizeof(NATPortMapReply)) { LogMsg("NAT-PMP PortMapReply message too short (%d bytes)", len); return; }
PortMapReply->NATRep_lease = (mDNSu32) ((mDNSu32)pkt[12] << 24 | (mDNSu32)pkt[13] << 16 | (mDNSu32)pkt[14] << 8 | pkt[15]);
}
// Since some NAT-PMP server implementations don't return the requested internal port in
// the reply, we can't associate this reply with a particular NATTraversalInfo structure.
// We globally keep track of the most recent error code for mappings.
natTraversalHandlePortMapReply(m, ptr, InterfaceID, PortMapReply->err, PortMapReply->extport, PortMapReply->NATRep_lease, NATTProtocolNATPMP);
}
// Don't need an SSDP socket if we get a NAT-PMP packet
if (m->SSDPSocket) { debugf("uDNS_ReceiveNATPMPPacket destroying SSDPSocket %p", &m->SSDPSocket); mDNSPlatformUDPClose(m->SSDPSocket); m->SSDPSocket = mDNSNULL; }
}
mDNSlocal void uDNS_ReceivePCPPacket(mDNS *m, const mDNSInterfaceID InterfaceID, mDNSu8 *pkt, mDNSu16 len)
{
// Minimum PCP packet is 24 bytes
if (len < 24)
{
return;
}
if ((reply->opCode & 0x80) == 0x00 || (strippedOpCode != PCPOp_Announce && strippedOpCode != PCPOp_Map))
{
return;
}
// Read multi-byte values
reply->lifetime = (mDNSs32)((mDNSs32)pkt[4] << 24 | (mDNSs32)pkt[5] << 16 | (mDNSs32)pkt[ 6] << 8 | pkt[ 7]);
reply->epoch = (mDNSs32)((mDNSs32)pkt[8] << 24 | (mDNSs32)pkt[9] << 16 | (mDNSs32)pkt[10] << 8 | pkt[11]);
debugf("uDNS_ReceivePCPPacket: %X %X upseconds %u client_delta %d server_delta %d", reply->opCode, reply->result, reply->epoch, client_delta, server_delta);
// If seconds since the epoch is 0, use 1 so we'll check epoch validity next time
m->LastNATReplyLocalTime = m->timenow;
#ifdef _LEGACY_NAT_TRAVERSAL_
LNT_ClearState(m);
#endif // _LEGACY_NAT_TRAVERSAL_
// Don't need an SSDP socket if we get a PCP packet
if (m->SSDPSocket) { debugf("uDNS_ReceivePCPPacket: destroying SSDPSocket %p", &m->SSDPSocket); mDNSPlatformUDPClose(m->SSDPSocket); m->SSDPSocket = mDNSNULL; }
if (checkEpochValidity && (client_delta + 2 < server_delta - server_delta / 16 || server_delta + 2 < client_delta - client_delta / 16))
{
// If this is an ANNOUNCE packet, wait a random interval up to 5 seconds
// otherwise, refresh immediately
mDNSu32 waitTicks = strippedOpCode ? 0 : mDNSRandom(PCP_WAITSECS_AFTER_EPOCH_INVALID * mDNSPlatformOneSecond);
LogMsg("uDNS_ReceivePCPPacket: Epoch invalid, %#a likely rebooted, waiting %u ticks", &m->Router, waitTicks);
// we can ignore the rest of this packet, as new requests are about to go out
return;
}
if (strippedOpCode == PCPOp_Announce)
return;
// We globally keep track of the most recent error code for mappings.
// This seems bad to do with PCP, but best not change it now.
{
if (len < sizeof(PCPMapReply))
{
return;
}
// Check the nonce
if (reply->nonce[0] != m->PCPNonce[0] || reply->nonce[1] != m->PCPNonce[1] || reply->nonce[2] != m->PCPNonce[2])
{
LogMsg("uDNS_ReceivePCPPacket: invalid nonce, ignoring. received { %x %x %x } expected { %x %x %x }",
return;
}
// Get the values
// Get the external address, which should be mapped, since we only support IPv4
{
// fall through to report the error
}
else if (mDNSIPv4AddressIsZero(mappedAddress))
{
// If this is the deletion case, we will have sent the zero IPv4-mapped address
// in our request, and the server should reflect it in the response, so we
// should not log about receiving a zero address. And in this case, we no
// longer have a NATTraversal to report errors back to, so it's ok to set the
// result here.
// In other cases, a zero address is an error, and we will have a NATTraversal
// to report back to, so set an error and fall through to report it.
// CheckNATMappings will log the error.
}
}
else
{
LogInfo("uDNS_ReceivePCPPacket: error received from server. opcode %X result %X lifetime %X epoch %X",
// If the packet is long enough, get the protocol & intport for matching to report
// the error
if (len >= sizeof(PCPMapReply))
{
}
}
{
mDNSu8 ptrProtocol = ((ptr->Protocol & NATOp_MapTCP) == NATOp_MapTCP ? PCPProto_TCP : PCPProto_UDP);
{
natTraversalHandlePortMapReplyWithAddress(m, ptr, InterfaceID, reply->result ? NATErr_NetFail : NATErr_None, mappedAddress, extport, reply->lifetime, NATTProtocolPCP);
}
}
}
mDNSexport void uDNS_ReceiveNATPacket(mDNS *m, const mDNSInterfaceID InterfaceID, mDNSu8 *pkt, mDNSu16 len)
{
if (len == 0)
LogMsg("uDNS_ReceiveNATPacket: zero length packet");
else if (pkt[0] == NATMAP_VERS)
else
LogMsg("uDNS_ReceiveNATPacket: packet with version %u (expected %u or %u)", pkt[0], PCP_VERS, NATMAP_VERS);
}
// <rdar://problem/3925163> Shorten DNS-SD queries to avoid NAT bugs
// <rdar://problem/4288449> Add check to avoid crashing NAT gateways that have buggy DNS relay code
//
// We know of bugs in home NAT gateways that cause them to crash if they receive certain DNS queries.
// The DNS queries that make them crash are perfectly legal DNS queries, but even if they weren't,
// the gateway shouldn't crash -- in today's world of viruses and network attacks, software has to
// be written assuming that a malicious attacker could send them any packet, properly-formed or not.
// Still, we don't want to be crashing people's home gateways, so we go out of our way to avoid
// the queries that crash them.
//
// Some examples:
//
// 1. Any query where the name ends in ".in-addr.arpa." and the text before this is 32 or more bytes.
// The query type does not need to be PTR -- the gateway will crash for any query type.
// e.g. "ping long-name-crashes-the-buggy-router.in-addr.arpa" will crash one of these.
//
// 2. Any query that results in a large response with the TC bit set.
//
// 3. Any PTR query that doesn't begin with four decimal numbers.
// These gateways appear to assume that the only possible PTR query is a reverse-mapping query
// (e.g. "1.0.168.192.in-addr.arpa") and if they ever get a PTR query where the first four
// labels are not all decimal numbers in the range 0-255, they handle that by crashing.
// These gateways also ignore the remainder of the name following the four decimal numbers
// -- whether or not it actually says in-addr.arpa, they just make up an answer anyway.
//
// The challenge therefore is to craft a query that will discern whether the DNS server
// is one of these buggy ones, without crashing it. Furthermore we don't want our test
// queries making it all the way to the root name servers, putting extra load on those
// name servers and giving Apple a bad reputation. To this end we send this query:
// dig -t ptr 1.0.0.127.dnsbugtest.1.0.0.127.in-addr.arpa.
//
// The text preceding the ".in-addr.arpa." is under 32 bytes, so it won't cause crash (1).
// It will not yield a large response with the TC bit set, so it won't cause crash (2).
// It starts with four decimal numbers, so it won't cause crash (3).
// The name falls within the "1.0.0.127.in-addr.arpa." domain, the reverse-mapping name for the local
// loopback address, and therefore the query will black-hole at the first properly-configured DNS server
// it reaches, making it highly unlikely that this query will make it all the way to the root.
//
// Finally, the correct response to this query is NXDOMAIN or a similar error, but the
// gateways that ignore the remainder of the name following the four decimal numbers
// give themselves away by actually returning a result for this nonsense query.
"\x1" "1" "\x1" "0" "\x1" "0" "\x3" "127" "\xa" "dnsbugtest"
"\x1" "1" "\x1" "0" "\x1" "0" "\x3" "127" "\x7" "in-addr" "\x4" "arpa";
// See comments above for DNSRelayTestQuestion
// If this is the kind of query that has the risk of crashing buggy DNS servers, we do a test question first
{
int i;
if (q->AuthInfo) return(mDNStrue); // Don't need a test query for private queries sent directly to authoritative server over TLS/TCP
for (i=0; i<4; i++) // If qname does not begin with num.num.num.num, can't skip the test query
{
p += 1 + p[0];
}
// If remainder of qname is ".in-addr.arpa.", this is a vanilla reverse-mapping query and
// we can safely do it without needing a test query first, otherwise we need the test query.
}
// Returns mDNStrue if response was handled
mDNSlocal mDNSBool uDNS_ReceiveTestQuestionResponse(mDNS *const m, DNSMessage *const msg, const mDNSu8 *const end,
{
DNSServer *s;
// 1. Find out if this is an answer to one of our test questions
// 2. If the DNS relay gave us a positive response, then it's got buggy firmware
// else, if the DNS relay gave us an error or no-answer response, it passed our test
else
// 3. Find occurrences of this server in our list, and mark them appropriately
for (s = m->DNSServers; s; s = s->next)
{
mDNSBool matchaddr = (s->teststate != result && mDNSSameAddress(srcaddr, &s->addr) && mDNSSameIPPort(srcport, s->port));
{
DNSQuestion *q;
if (result == DNSServer_Passed)
{
LogInfo("DNS Server %#a:%d (%#a:%d) %d passed%s",
}
else
{
LogMsg("NOTE: Wide-Area Service Discovery disabled to avoid crashing defective DNS relay %#a:%d (%#a:%d) %d%s",
}
// If this server has just changed state from DNSServer_Untested to DNSServer_Passed, then retrigger any waiting questions.
// We use the NoTestQuery() test so that we only retrigger questions that were actually blocked waiting for this test to complete.
if (q->qDNSServer == s && !NoTestQuery(q))
{
q->unansweredQueries = 0;
m->NextScheduledQuery = m->timenow;
}
}
}
return(mDNStrue); // Return mDNStrue to tell uDNS_ReceiveMsg it doesn't need to process this packet further
}
// Called from mDNSCoreReceive with the lock held
mDNSexport void uDNS_ReceiveMsg(mDNS *const m, DNSMessage *const msg, const mDNSu8 *const end, const mDNSAddr *const srcaddr, const mDNSIPPort srcport)
{
(void)srcport; // Unused
debugf("uDNS_ReceiveMsg from %#-15a with "
"%2d Question%s %2d Answer%s %2d Authorit%s %2d Additional%s %d bytes",
{
//if (srcaddr && recvLLQResponse(m, msg, end, srcaddr, srcport)) return;
if (msg->h.flags.b[0] & kDNSFlag0_TC && mDNSSameOpaque16(qptr->TargetQID, msg->h.id) && m->timenow - qptr->LastQTime < RESPONSE_WINDOW)
{
else
{
// Don't reuse TCP connections. We might have failed over to a different DNS server
// while the first TCP connection is in progress. We need a new TCP connection to the
// new DNS server. So, always try to establish a new connection.
qptr->tcp = MakeTCPConn(m, mDNSNULL, mDNSNULL, kTCPSocketFlags_Zero, srcaddr, srcport, mDNSNULL, qptr, mDNSNULL);
}
}
}
{
//rcode = kDNSFlag1_RC_ServFail; // Simulate server failure (rcode 2)
// Walk through all the records that matches the messageID. There could be multiple
// records if we had sent them in a group
if (m->CurrentRecord)
LogMsg("uDNS_ReceiveMsg ERROR m->CurrentRecord already set %s", ARDisplayString(m, m->CurrentRecord));
m->CurrentRecord = m->ResourceRecords;
while (m->CurrentRecord)
{
{
{
rptr->refreshCount = 0;
}
// We pass the random value to make sure that if we update multiple
// records, they all get the same random value
}
}
}
}
// ***************************************************************************
#endif
{
if (q->ReqLease)
{
LogMsg("Unable to refresh LLQ %##s (%s) - will retry in %d seconds", q->qname.c, DNSTypeName(q->qtype), LLQ_POLL_INTERVAL / mDNSPlatformOneSecond);
StartLLQPolling(m,q);
return;
}
llq.err = q->tcp ? GetLLQEventPort(m, &q->servAddr) : LLQErr_NoError; // If using TCP tell server what UDP port to send notifications to
if (!end) { LogMsg("sendLLQRefresh: putLLQ failed %##s (%s)", q->qname.c, DNSTypeName(q->qtype)); return; }
// Note that we (conditionally) add HINFO and TSIG here, since the question might be going away,
// so we may not be able to reference it (most importantly it's AuthInfo) when we actually send the message
if (!end) { LogMsg("sendLLQRefresh: putHINFO failed %##s (%s)", q->qname.c, DNSTypeName(q->qtype)); return; }
if (PrivateQuery(q))
{
if (!end) { LogMsg("sendLLQRefresh: DNSDigest_SignMessage failed %##s (%s)", q->qname.c, DNSTypeName(q->qtype)); return; }
}
if (PrivateQuery(q) && !q->tcp)
{
if (!q->nta)
{
// Note: If a question is in LLQ_Established state, we never free the zone data for the
// question (PrivateQuery). If we free, we reset the state to something other than LLQ_Established.
// This function is called only if the query is in LLQ_Established state and hence nta should
// never be NULL. In spite of that, we have seen q->nta being NULL in the field. Just refetch the
// zone data in that case.
return;
// ThisQInterval is not adjusted when we return from here which means that we will get called back
// again immediately. As q->servAddr and q->servPort are still valid and the nta->Host is initialized
// without any additional discovery for PrivateQuery, things work.
}
q->tcp = MakeTCPConn(m, &m->omsg, end, kTCPSocketFlags_UseTLS, &q->servAddr, q->servPort, &q->nta->Host, q, mDNSNULL);
}
else
{
// if AuthInfo and AuthInfo->AutoTunnel is set, we use the TCP socket but don't need to pass the AuthInfo as
// we already protected the message above.
err = mDNSSendDNSMessage(m, &m->omsg, end, mDNSInterface_Any, q->LocalSocket, &q->servAddr, q->servPort, q->tcp ? q->tcp->sock : mDNSNULL, mDNSNULL, mDNSfalse);
if (err)
{
}
}
q->ntries++;
SetNextQueryTime(m, q);
}
{
mDNS_Lock(m);
// If we get here it means that the GetZoneData operation has completed.
// We hold on to the zone data if it is AutoTunnel as we use the hostname
// in zoneInfo during the TLS connection setup.
q->servPort = zeroIPPort;
if (!err && zoneInfo && !mDNSIPPortIsZero(zoneInfo->Port) && !mDNSAddressIsZero(&zoneInfo->Addr) && zoneInfo->Host.c[0])
{
if (!PrivateQuery(q))
{
// We don't need the zone data as we use it only for the Host information which we
// don't need if we are not going to use TLS connections.
if (q->nta)
{
if (q->nta != zoneInfo) LogMsg("LLQGotZoneData: nta (%p) != zoneInfo (%p) %##s (%s)", q->nta, zoneInfo, q->qname.c, DNSTypeName(q->qtype));
CancelGetZoneData(m, q->nta);
}
}
q->ntries = 0;
startLLQHandshake(m, q);
}
else
{
if (q->nta)
{
if (q->nta != zoneInfo) LogMsg("LLQGotZoneData: nta (%p) != zoneInfo (%p) %##s (%s)", q->nta, zoneInfo, q->qname.c, DNSTypeName(q->qtype));
CancelGetZoneData(m, q->nta);
}
StartLLQPolling(m,q);
if (err == mStatus_NoSuchNameErr)
{
// this actually failed, so mark it by setting address to all ones
}
}
mDNS_Unlock(m);
}
// Called in normal callback context (i.e. mDNS_busy and mDNS_reentrancy are both 1)
{
LogInfo("PrivateQueryGotZoneData %##s (%s) err %d Zone %##s Private %d", q->qname.c, DNSTypeName(q->qtype), err, zoneInfo->ZoneName.c, zoneInfo->ZonePrivate);
if (q->nta != zoneInfo) LogMsg("PrivateQueryGotZoneData:ERROR!!: nta (%p) != zoneInfo (%p) %##s (%s)", q->nta, zoneInfo, q->qname.c, DNSTypeName(q->qtype));
if (err || !zoneInfo || mDNSAddressIsZero(&zoneInfo->Addr) || mDNSIPPortIsZero(zoneInfo->Port) || !zoneInfo->Host.c[0])
{
LogInfo("PrivateQueryGotZoneData: ERROR!! %##s (%s) invoked with error code %d %p %#a:%d",
CancelGetZoneData(m, q->nta);
return;
}
if (!zoneInfo->ZonePrivate)
{
debugf("Private port lookup failed -- retrying without TLS -- %##s (%s)", q->qname.c, DNSTypeName(q->qtype));
CancelGetZoneData(m, q->nta);
mDNS_Lock(m);
SetNextQueryTime(m, q);
mDNS_Unlock(m);
return;
// Next call to uDNS_CheckCurrentQuestion() will do this as a non-private query
}
if (!PrivateQuery(q))
{
LogMsg("PrivateQueryGotZoneData: ERROR!! Not a private query %##s (%s) AuthInfo %p", q->qname.c, DNSTypeName(q->qtype), q->AuthInfo);
CancelGetZoneData(m, q->nta);
return;
}
q->TargetQID = mDNS_NewMessageID(m);
if (!q->nta) { LogMsg("PrivateQueryGotZoneData:ERROR!! nta is NULL for %##s (%s)", q->qname.c, DNSTypeName(q->qtype)); return; }
q->tcp = MakeTCPConn(m, mDNSNULL, mDNSNULL, kTCPSocketFlags_UseTLS, &zoneInfo->Addr, zoneInfo->Port, &q->nta->Host, q, mDNSNULL);
}
// ***************************************************************************
#endif
// Called in normal callback context (i.e. mDNS_busy and mDNS_reentrancy are both 1)
{
LogMsg("RecordRegistrationGotZoneData: nta (%p) != zoneData (%p) %##s (%s)", newRR->nta, zoneData, newRR->resrec.name->c, DNSTypeName(newRR->resrec.rrtype));
if (m->mDNS_busy != m->mDNS_reentrancy)
LogMsg("RecordRegistrationGotZoneData: mDNS_busy (%ld) != mDNS_reentrancy (%ld)", m->mDNS_busy, m->mDNS_reentrancy);
// make sure record is still in list (!!!)
if (!ptr)
{
LogMsg("RecordRegistrationGotZoneData - RR no longer in list. Discarding.");
return;
}
if (err)
{
return;
}
if (!zoneData) { LogMsg("ERROR: RecordRegistrationGotZoneData invoked with NULL result and no error"); return; }
{
LogMsg("ERROR: New resource record's class (%d) does not match zone class (%d)", newRR->resrec.rrclass, zoneData->ZoneClass);
return;
}
// Don't try to do updates to the root name server.
// We might be tempted also to block updates to any single-label name server (e.g. com, edu, net, etc.) but some
// organizations use their own private pseudo-TLD, like ".home", etc, and we don't want to block that.
{
LogInfo("RecordRegistrationGotZoneData: No name server found claiming responsibility for \"%##s\"!", newRR->resrec.name->c);
return;
}
// Store discovered zone data
{
LogMsg("RecordRegistrationGotZoneData: Zone \"%##s\" is longer than \"%##s\"", zoneData->ZoneName.c, newRR->resrec.name->c);
return;
}
{
LogMsg("RecordRegistrationGotZoneData: Zone \"%##s\" does not match \"%##s\" for \"%##s\"", newRR->zone->c, zoneData->ZoneName.c, newRR->resrec.name->c);
return;
}
{
LogInfo("RecordRegistrationGotZoneData: No _dns-update._udp service found for \"%##s\"!", newRR->resrec.name->c);
return;
}
debugf("RecordRegistrationGotZoneData: Set zone information for %##s %##s to %#a:%d",
// If we are deregistering, uDNS_DeregisterRecord will do that as it has the zone data now.
{
mDNS_Lock(m);
mDNS_Unlock(m);
return;
}
{
const domainname *target;
// we were fetching zone data.
mDNS_Lock(m);
mDNS_Unlock(m);
{
if (t) t->c[0] = 0;
return;
}
}
// If we have non-zero service port (always?)
// and a private address, and update server is non-private
// and this service is AutoTarget
// then initiate a NAT mapping request. On completion it will do SendRecordRegistration() for us
mDNSv4AddrIsRFC1918(&m->AdvertisedV4.ip.v4) && newRR->nta && !mDNSAddrIsRFC1918(&newRR->nta->Addr) &&
{
{
LogMsg("RecordRegistrationGotZoneData: ERROR!! AutoTunnel has Target_AutoHostAndNATMAP for %s", ARDisplayString(m, newRR));
if (t) t->c[0] = 0;
return;
}
// During network transitions, we are called multiple times in different states. Setup NAT
// state just once for this record.
{
StartRecordNatMap(m, newRR);
return;
}
else LogInfo("RecordRegistrationGotZoneData: StartRecordNatMap for %s, state %d, context %p", ARDisplayString(m, newRR), newRR->state, newRR->NATinfo.clientContext);
}
mDNS_Lock(m);
// 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. If a previous update resulted in error, then don't reset the
// interval. Preserve the back-off so that we don't keep retrying aggressively.
{
}
{
// Delay the record registration by MERGE_DELAY_TIME so that we can merge them
// into one update
}
mDNS_Unlock(m);
}
{
mDNS_CheckLock(m);
{
LogMsg("SendRecordDeRegistration: No zone info for Resource record %s RecordType %d", ARDisplayString(m, rr), rr->resrec.RecordType);
return;
}
// set zone
{
if (rr->tcp) LogInfo("SendRecordDeregistration: Disposing existing TCP connection for %s", ARDisplayString(m, rr));
if (!rr->nta) { LogMsg("SendRecordDeregistration:Private:ERROR!! nta is NULL for %s", ARDisplayString(m, rr)); return; }
rr->tcp = MakeTCPConn(m, &m->omsg, ptr, kTCPSocketFlags_UseTLS, &rr->nta->Addr, rr->nta->Port, &rr->nta->Host, mDNSNULL, rr);
}
else
{
if (!rr->nta) { LogMsg("SendRecordDeregistration:ERROR!! nta is NULL for %s", ARDisplayString(m, rr)); return; }
err = mDNSSendDNSMessage(m, &m->omsg, ptr, mDNSInterface_Any, mDNSNULL, &rr->nta->Addr, rr->nta->Port, mDNSNULL, GetAuthInfoForName_internal(m, rr->resrec.name), mDNSfalse);
//if (rr->state == regState_DeregPending) CompleteDeregistration(m, rr); // Don't touch rr after this
}
SetRecordRetry(m, rr, 0);
return;
exit:
}
{
{
case regState_Refresh:
case regState_Pending:
case regState_UpdatePending:
case regState_Registered: break;
case regState_DeregPending: break;
case regState_NATError:
case regState_NATMap:
// A record could be in NoTarget to start with if the corresponding SRV record could not find a target.
// It is also possible to reenter the NoTarget state when we move to a network with a NAT that has
// no {PCP, NAT-PMP, UPnP/IGD} support. In that case before we entered NoTarget, we already deregistered with
// the server.
case regState_NoTarget:
case regState_Unregistered:
case regState_Zero:
default:
LogInfo("uDNS_DeregisterRecord: State %d for %##s type %s", rr->state, rr->resrec.name->c, DNSTypeName(rr->resrec.rrtype));
// This function may be called during sleep when there are no sleep proxy servers
return mStatus_NoError;
}
// if unsent rdata is queued, free it.
//
// The data may be queued in QueuedRData or InFlightRData.
//
// 1) If the record is in Registered state, we store it in InFlightRData and copy the same in "rdata"
// *just* before sending the update to the server. Till we get the response, InFlightRData and "rdata"
// in the resource record are same. We don't want to free in that case. It will be freed when "rdata"
// is freed. If they are not same, the update has not been sent and we should free it here.
//
// 2) If the record is in UpdatePending state, we queue the update in QueuedRData. When the previous update
// comes back from the server, we copy it from QueuedRData to InFlightRData and repeat (1). This implies
// that QueuedRData can never be same as "rdata" in the resource record. As long as we have something
// left in QueuedRData, we should free it here.
{
{
}
else
}
{
else
{
}
}
// If a current group registration is pending, we can't send this deregisration till that registration
// has reached the server i.e., the ordering is important. Previously, if we did not send this
// registration in a group, then the previous connection will be torn down as part of sending the
// deregistration. If we send this in a group, we need to locate the resource record that was used
// to send this registration and terminate that connection. This means all the updates on that might
// be lost (assuming the response is not waiting for us at the socket) and the retry will send the
// update again sometime in the near future.
//
// NOTE: SSL handshake failures normally free the TCP connection immediately. Hence, you may not
// find the TCP below there. This case can happen only when tcp is trying to actively retransmit
// the request or SSL negotiation taking time i.e resource record is actively trying to get the
// message to the server. During that time a deregister has to happen.
{
{
{
if (found)
}
}
if (!found) LogInfo("uDNSDeregisterRecord: Cannot find the anchor Resource Record for %s, not an error", ARDisplayString(m, rr));
}
// Retry logic for deregistration should be no different from sending registration the first time.
// Currently ThisAPInterval most likely is set to the refresh interval
{
// Delay the record deregistration by MERGE_DELAY_TIME so that we can merge them
// into one update. If the domain is being deleted, delay by 2 * MERGE_DELAY_TIME
// so that we can merge all the AutoTunnel records and the service records in
// one update (they get deregistered a little apart)
}
// IsRecordMergeable could have returned false for several reasons e.g., DontMerge is set or
// no zone information. Most likely it is the latter, CheckRecordUpdates will fetch the zone
// data when it encounters this record.
return mStatus_NoError;
}
{
{
case regState_DeregPending:
case regState_Unregistered:
// not actively registered
goto unreg_error;
case regState_NATMap:
case regState_NoTarget:
// change rdata directly since it hasn't been sent yet
return mStatus_NoError;
case regState_Pending:
case regState_Refresh:
case regState_UpdatePending:
// registration in-flight. queue rdata and return
// if unsent rdata is already queued, free it before we replace it
return mStatus_NoError;
case regState_Registered:
SetNextuDNSEvent(m, rr);
return mStatus_NoError;
case regState_NATError:
LogMsg("ERROR: uDNS_UpdateRecord called for record %##s with bad state regState_NATError", rr->resrec.name->c);
return mStatus_UnknownErr; // states for service records only
}
LogMsg("uDNS_UpdateRecord: Requested update of record %##s type %d, in erroneous state %d",
return mStatus_Invalid;
}
// ***************************************************************************
#endif
{
DNSQuestion *q = m->CurrentQuestion;
{
// If we are not receiving any responses for DNSSEC question, it could be due to
// silently drops the packets. Also as per RFC 5625 there are certain buggy DNS Proxies
// option if we have not received any responses indicating that the server or
// the middlebox is DNSSEC aware. If we receive at least one response to a DNSSEC
// question, we don't turn off validation. Also, we wait for MAX_DNSSEC_RETRANSMISSIONS
// before turning off validation to accomodate packet loss.
//
// Note: req_DO affects only DNSSEC_VALIDATION_SECURE_OPTIONAL questions;
// DNSSEC_VALIDATION_SECURE questions ignores req_DO.
{
q->qDNSServer->retransDO++;
{
}
}
if (!q->qDNSServer->req_DO)
{
if (q->ProxyQuestion)
q->ProxyDNSSECOK = mDNSfalse;
LogInfo("handle_unanswered_query: unanswered query for %##s (%s), so turned off validation for %#a",
}
}
}
// The question to be checked is not passed in as an explicit parameter;
// instead it is implicit that the question to be checked is m->CurrentQuestion.
{
DNSQuestion *q = m->CurrentQuestion;
if (m->timenow - NextQSendTime(q) < 0) return;
if (q->LongLived)
{
switch (q->state)
{
case LLQ_InitialRequest: startLLQHandshake(m, q); break;
case LLQ_SecondaryRequest:
// For PrivateQueries, we need to start the handshake again as we don't do the Challenge/Response step
if (PrivateQuery(q))
startLLQHandshake(m, q);
else
sendChallengeResponse(m, q, mDNSNULL);
break;
case LLQ_Established: sendLLQRefresh(m, q); break;
case LLQ_Poll: break; // Do nothing (handled below)
}
}
// We repeat the check above (rather than just making this the "else" case) because startLLQHandshake can change q->state to LLQ_Poll
{
if (q->unansweredQueries >= MAX_UCAST_UNANSWERED_QUERIES)
{
if (orig)
LogInfo("uDNS_CheckCurrentQuestion: Sent %d unanswered queries for %##s (%s) to %#a:%d (%##s)",
q->unansweredQueries, q->qname.c, DNSTypeName(q->qtype), &orig->addr, mDNSVal16(orig->port), orig->domain.c);
PenalizeDNSServer(m, q, zeroID);
q->noServerResponse = 1;
}
// There are two cases here.
//
// 1. We have only one DNS server for this question. It is not responding even after we sent MAX_UCAST_UNANSWERED_QUERIES.
// In that case, we need to keep retrying till we get a response. But we need to backoff as we retry. We set
// noServerResponse in the block above and below we do not touch the question interval. When we come here, we
// already waited for the response. We need to send another query right at this moment. We do that below by
// reinitializing dns servers and reissuing the query.
//
// 2. We have more than one DNS server. If at least one server did not respond, we would have set noServerResponse
// either now (the last server in the list) or before (non-last server in the list). In either case, if we have
// reached the end of DNS server list, we need to try again from the beginning. Ideally we should try just the
// servers that did not respond, but for simplicity we try all the servers. Once we reached the end of list, we
// set triedAllServersOnce so that we don't try all the servers aggressively. See PenalizeDNSServer.
if (!q->qDNSServer && q->noServerResponse)
{
q->triedAllServersOnce = 1;
// Re-initialize all DNS servers for this question. If we have a DNSServer, DNSServerChangeForQuestion will
// handle all the work including setting the new DNS server.
SetValidDNSServers(m, q);
new = GetServerForQuestion(m, q);
if (new)
{
LogInfo("uDNS_checkCurrentQuestion: Retrying question %p %##s (%s) DNS Server %#a:%d ThisQInterval %d",
q, q->qname.c, DNSTypeName(q->qtype), new ? &new->addr : mDNSNULL, mDNSVal16(new ? new->port : zp), q->ThisQInterval);
DNSServerChangeForQuestion(m, q, new);
}
if (qptr->DuplicateOf == q) { qptr->validDNSServers = q->validDNSServers; qptr->qDNSServer = q->qDNSServer; }
}
{
{
end = putQuestion(&m->omsg, m->omsg.data, m->omsg.data + AbsoluteMaxDNSMessageData, &q->qname, q->qtype, q->qclass);
{
if (q->ProxyQuestion)
end = DNSProxySetAttributes(q, &m->omsg.h, &m->omsg, end, m->omsg.data + AbsoluteMaxDNSMessageData);
else
}
private = PrivateQuery(q);
}
else if (m->timenow - q->qDNSServer->lasttest >= INIT_UCAST_POLL_INTERVAL) // Make sure at least three seconds has elapsed since last test query
{
end = putQuestion(&m->omsg, m->omsg.data, m->omsg.data + AbsoluteMaxDNSMessageData, DNSRelayTestQuestion, kDNSType_PTR, kDNSClass_IN);
}
{
//LogMsg("uDNS_CheckCurrentQuestion %p %d %p %##s (%s)", q, NextQSendTime(q) - m->timenow, private, q->qname.c, DNSTypeName(q->qtype));
if (private)
{
q->nta = StartGetZoneData(m, &q->qname, q->LongLived ? ZoneServiceLLQ : ZoneServiceQuery, PrivateQueryGotZoneData, q);
if (q->state == LLQ_Poll) q->ThisQInterval = (LLQ_POLL_INTERVAL + mDNSRandom(LLQ_POLL_INTERVAL/10)) / QuestionIntervalStep;
}
else
{
debugf("uDNS_CheckCurrentQuestion sending %p %##s (%s) %#a:%d UnansweredQueries %d",
q->qDNSServer ? &q->qDNSServer->addr : mDNSNULL, mDNSVal16(q->qDNSServer ? q->qDNSServer->port : zeroIPPort), q->unansweredQueries);
if (!q->LocalSocket)
{
if (q->LocalSocket)
}
if (!q->LocalSocket) err = mStatus_NoMemoryErr; // If failed to make socket (should be very rare), we'll try again next time
else err = mDNSSendDNSMessage(m, &m->omsg, end, q->qDNSServer->interface, q->LocalSocket, &q->qDNSServer->addr, q->qDNSServer->port, mDNSNULL, mDNSNULL, q->UseBackgroundTrafficClass);
}
}
if (err != mStatus_TransientErr) // if it is not a transient error backoff and DO NOT flood queries unnecessarily
{
q->ThisQInterval = q->ThisQInterval * QuestionIntervalStep; // Only increase interval if send succeeded
q->unansweredQueries++;
if (q->ThisQInterval > MAX_UCAST_POLL_INTERVAL)
{
// We don't want to retransmit too soon. Hence, we always schedule our first
// retransmisson at 3 seconds rather than one second
if (q->ThisQInterval > LLQ_POLL_INTERVAL)
LogInfo("uDNS_CheckCurrentQuestion: private non polling question for %##s (%s) will be retried in %d ms", q->qname.c, DNSTypeName(q->qtype), q->ThisQInterval);
}
if (q->qDNSServer->cellIntf)
{
// We don't want to retransmit too soon. Schedule our first retransmisson at
// MIN_UCAST_RETRANS_TIMEOUT seconds.
if (q->ThisQInterval < MIN_UCAST_RETRANS_TIMEOUT)
}
debugf("uDNS_CheckCurrentQuestion: Increased ThisQInterval to %d for %##s (%s), cell %d", q->ThisQInterval, q->qname.c, DNSTypeName(q->qtype), q->qDNSServer->cellIntf);
}
SetNextQueryTime(m, q);
}
else
{
// If we have no server for this query, or the only server is a disabled one, then we deliver
// a transient failure indication to the client. This is important for things like iPhone
// where we want to return timely feedback to the user when no network is available.
// After calling MakeNegativeCacheRecord() we store the resulting record in the
// cache so that it will be visible to other clients asking the same question.
// (When we have a group of identical questions, only the active representative of the group gets
// passed to uDNS_CheckCurrentQuestion -- we only want one set of query packets hitting the wire --
// but we want *all* of the questions to get answer callbacks.)
if (!q->qDNSServer)
{
if (!mDNSOpaque64IsZero(&q->validDNSServers))
LogMsg("uDNS_CheckCurrentQuestion: ERROR!!: valid DNSServer bits not zero 0x%x, 0x%x for question %##s (%s)",
// If we reached the end of list while picking DNS servers, then we don't want to deactivate the
// question. Try after 60 seconds. We find this by looking for valid DNSServers for this question,
// if we find any, then we must have tried them before we came here. This avoids maintaining
// another state variable to see if we had valid DNS servers for this question.
SetValidDNSServers(m, q);
if (mDNSOpaque64IsZero(&q->validDNSServers))
{
LogInfo("uDNS_CheckCurrentQuestion: no DNS server for %##s (%s)", q->qname.c, DNSTypeName(q->qtype));
q->ThisQInterval = 0;
}
else
{
// Pretend that we sent this question. As this is an ActiveQuestion, the NextScheduledQuery should
// be set properly. Also, we need to properly backoff in cases where we don't set the question to
// MaxQuestionInterval when we answer the question e.g., LongLived, we need to keep backing off
SetNextQueryTime(m, q);
// Pick a new DNS server now. Otherwise, when the cache is 80% of its expiry, we will try
// to send a query and come back to the same place here and log the above message.
q->qDNSServer = GetServerForQuestion(m, q);
if (qptr->DuplicateOf == q) { qptr->validDNSServers = q->validDNSServers; qptr->qDNSServer = q->qDNSServer; }
{
LogInfo("uDNS_checkCurrentQuestion: Tried all DNS servers, retry question %p SuppressUnusable %d %##s (%s) with DNS Server %#a:%d after 60 seconds, ThisQInterval %d",
q->qDNSServer ? &q->qDNSServer->addr : mDNSNULL, mDNSVal16(q->qDNSServer ? q->qDNSServer->port : zp), q->ThisQInterval);
}
}
}
else
{
q->ThisQInterval = 0;
LogMsg("uDNS_CheckCurrentQuestion DNS server %#a:%d for %##s is disabled", &q->qDNSServer->addr, mDNSVal16(q->qDNSServer->port), q->qname.c);
}
if (cg)
{
{
{
}
}
}
// For some of the WAB queries that we generate form within the mDNSResponder, most of the home routers
// don't understand and return ServFail/NXDomain. In those cases, we don't want to try too often. We try
// every fifteen minutes in that case
MakeNegativeCacheRecord(m, &m->rec.r, &q->qname, q->qnamehash, q->qtype, q->qclass, (DomainEnumQuery(&q->qname) ? 60 * 15 : 60), mDNSInterface_Any, q->qDNSServer);
q->unansweredQueries = 0;
if (!mDNSOpaque16IsZero(q->responseFlags))
// We're already using the m->CurrentQuestion pointer, so CacheRecordAdd can't use it to walk the question list.
// To solve this problem we set rr->DelayDelivery to a nonzero value (which happens to be 'now') so that we
// momentarily defer generating answer callbacks until mDNS_Execute time.
// MUST NOT touch m->CurrentQuestion (or q) after this -- client callback could have deleted it
}
}
}
{
if (m->NATTraversals && rfc1918) // Do we need to open a socket to receive multicast announcements from router?
{
if (m->NATMcastRecvskt == mDNSNULL) // If we are behind a NAT and the socket hasn't been opened yet, open it
{
// we need to log a message if we can't get our socket, but only the first time (after success)
if (!m->NATMcastRecvskt)
{
if (needLog)
{
LogMsg("CheckNATMappings: Failed to allocate port 5350 UDP multicast socket for PCP & NAT-PMP announcements");
}
}
else
}
}
else // else, we don't want to listen for announcements, so close them if they're open
{
if (m->NATMcastRecvskt) { mDNSPlatformUDPClose(m->NATMcastRecvskt); m->NATMcastRecvskt = mDNSNULL; }
if (m->SSDPSocket) { debugf("CheckNATMappings destroying SSDPSocket %p", &m->SSDPSocket); mDNSPlatformUDPClose(m->SSDPSocket); m->SSDPSocket = mDNSNULL; }
}
m->CurrentNATTraversal = m->NATTraversals;
while (m->CurrentNATTraversal)
{
if (HaveRoutable) // If not RFC 1918 address, our own address and port are effectively our external address and port
{
cur->ExpiryTime = 0;
}
else // Check if it's time to send port mapping packet(s)
{
{
{
cur->ExpiryTime = 0;
}
NATSetNextRenewalTime(m, cur);
else // else no mapping; use exponential backoff sequence
{
}
}
{
}
}
// Notify the client if necessary. We invoke the callback if:
// (1) We have an effective address,
// or we've tried and failed a couple of times to discover it
// AND
// (2) the client requested the address only,
// or the client won't need a mapping because we have a routable address,
// or the client has an expiry time and therefore a successful mapping,
// or we've tried and failed a couple of times (see "Time line" below)
// AND
// (3) we have new data to give the client that's changed since the last callback
//
// Time line is: Send, Wait 500ms, Send, Wait 1sec, Send, Wait 2sec, Send
// At this point we've sent three requests without an answer, we've just sent our fourth request,
// retryInterval is now 4 seconds, which is greater than NATMAP_INIT_RETRY * 8 (2 seconds),
// so we return an error result to the caller.
{
const mStatus EffectiveResult = cur->NewResult ? cur->NewResult : mDNSv4AddrIsRFC1918(&EffectiveAddress) ? mStatus_DoubleNAT : mStatus_NoError;
if (HaveRoutable)
else
if (!cur->Protocol || HaveRoutable || cur->ExpiryTime || cur->retryInterval > NATMAP_INIT_RETRY * 8)
{
{
//LogMsg("NAT callback %d %d %d", cur->Protocol, cur->ExpiryTime, cur->retryInterval);
{
if (!EffectiveResult)
LogInfo("CheckNATMapping: Failed to obtain NAT port mapping %p from router %#a external address %.4a internal port %5d interval %d error %d",
else
LogMsg("CheckNATMapping: Failed to obtain NAT port mapping %p from router %#a external address %.4a internal port %5d interval %d error %d",
}
mDNS_DropLockBeforeCallback(); // Allow client to legally make mDNS API calls from the callback
if (cur->clientCallback)
mDNS_ReclaimLockAfterCallback(); // Decrement mDNS_reentrancy to block mDNS API calls again
// MUST NOT touch cur after invoking the callback
}
}
}
}
}
{
AuthRecord *rr;
{
if (!AuthRecord_uDNS(rr)) continue;
if (rr->state == regState_NoTarget) {debugf("CheckRecordUpdates: Record %##s in NoTarget", rr->resrec.name->c); continue;}
// While we are waiting for the port mapping, we have nothing to do. The port mapping callback
// will take care of this
if (rr->state == regState_NATMap) {debugf("CheckRecordUpdates: Record %##s in NATMap", rr->resrec.name->c); continue;}
if (rr->state == regState_Pending || rr->state == regState_DeregPending || rr->state == regState_UpdatePending ||
{
{
{
// Zero out the updateid so that if we have a pending response from the server, it won't
// be accepted as a valid response. If we accept the response, we might free the new "nta"
rr->nta = StartGetZoneData(m, rr->resrec.name, ZoneServiceUpdate, RecordRegistrationGotZoneData, rr);
// We have just started the GetZoneData. We need to wait for it to finish. SetRecordRetry here
// schedules the update timer to fire in the future.
//
// There are three cases.
//
// 1) When the updates are sent the first time, the first retry is intended to be at three seconds
// in the future. But by calling SetRecordRetry here we set it to nine seconds. But it does not
// matter because when the answer comes back, RecordRegistrationGotZoneData resets the interval
// back to INIT_RECORD_REG_INTERVAL. This also gives enough time for the query.
//
// 2) In the case of update errors (updateError), this causes further backoff as
// RecordRegistrationGotZoneData does not reset the timer. This is intentional as in the case of
// errors, we don't want to update aggressively.
//
// 3) We might be refreshing the update. This is very similar to case (1). RecordRegistrationGotZoneData
// resets it back to INIT_RECORD_REG_INTERVAL.
//
SetRecordRetry(m, rr, 0);
}
else SendRecordRegistration(m, rr);
}
}
}
return nextevent;
}
{
DNSServer *d;
nexte = CheckRecordUpdates(m);
if (m->NextuDNSEvent - nexte > 0)
m->NextuDNSEvent = nexte;
for (d = m->DNSServers; d; d=d->next)
if (d->penaltyTime)
{
if (m->timenow - d->penaltyTime >= 0)
{
d->penaltyTime = 0;
}
else
if (m->NextuDNSEvent - d->penaltyTime > 0)
m->NextuDNSEvent = d->penaltyTime;
}
if (m->CurrentQuestion)
LogMsg("uDNS_Tasks ERROR m->CurrentQuestion already set: %##s (%s)", m->CurrentQuestion->qname.c, DNSTypeName(m->CurrentQuestion->qtype));
m->CurrentQuestion = m->Questions;
{
DNSQuestion *const q = m->CurrentQuestion;
{
if (q == m->CurrentQuestion)
if (m->NextuDNSEvent - NextQSendTime(q) > 0)
m->NextuDNSEvent = NextQSendTime(q);
}
// If m->CurrentQuestion wasn't modified out from under us, advance it now
// We can't do this at the start of the loop because uDNS_CheckCurrentQuestion()
// depends on having m->CurrentQuestion point to the right question
if (m->CurrentQuestion == q)
m->CurrentQuestion = q->next;
}
m->CurrentQuestion = mDNSNULL;
}
// ***************************************************************************
#endif
{
AuthRecord *rr;
{
if (AuthRecord_uDNS(rr))
{
// Zero out the updateid so that if we have a pending response from the server, it won't
// be accepted as a valid response.
{
}
// We are waiting to update the resource record. The original data of the record is
// in OrigRData and the updated value is in InFlightRData. Free the old and the new
// one will be registered when we come back.
{
// act as if the update succeeded, since we're about to delete the name anyway
// deallocate old RData
}
// If we have not begun the registration process i.e., never sent a registration packet,
// then uDNS_DeregisterRecord will not send a deregistration
uDNS_DeregisterRecord(m, rr);
// When we wake, we call ActivateUnicastRegistration which starts at StartGetZoneData
}
}
}
{
SearchListElem **p;
// Check to see if we already have this domain in our list
for (p = &SearchList; *p; p = &(*p)->next)
{
// If domain is already in list, and marked for deletion, unmark the delete
// Be careful not to touch the other flags that may be present
tmp = *p;
break;
}
// move to end of list so that we maintain the same order
while (*p) p = &(*p)->next;
else
{
// if domain not in list, add to list, mark as add (1)
*p = mDNSPlatformMemAllocate(sizeof(SearchListElem));
if (!*p) { LogMsg("ERROR: mDNS_AddSearchDomain - malloc"); return; }
mDNSPlatformMemZero(*p, sizeof(SearchListElem));
(*p)->InterfaceID = InterfaceID;
}
}
{
(void)m; // unused
}
mDNSlocal void FoundDomain(mDNS *const m, DNSQuestion *question, const ResourceRecord *const answer, QC_result AddRecord)
{
const char *name;
else if (question == &slElem->DefBrowseQ) name = mDNS_DomainTypeNames[mDNS_DomainTypeBrowseDefault];
else if (question == &slElem->AutomaticBrowseQ) name = mDNS_DomainTypeNames[mDNS_DomainTypeBrowseAutomatic];
else if (question == &slElem->DefRegisterQ) name = mDNS_DomainTypeNames[mDNS_DomainTypeRegistrationDefault];
else { LogMsg("FoundDomain - unknown question"); return; }
LogInfo("FoundDomain: %p %s %s Q %##s A %s", answer->InterfaceID, AddRecord ? "Add" : "Rmv", name, question->qname.c, RRDisplayString(m, answer));
if (AddRecord)
{
mDNS_SetupResourceRecord(&arElem->ar, mDNSNULL, mDNSInterface_LocalOnly, kDNSType_PTR, 7200, kDNSRecordTypeShared, AuthRecordLocalOnly, FreeARElemCallback, arElem);
if (err) { LogMsg("ERROR: FoundDomain - mDNS_Register returned %d", err); mDNSPlatformMemFree(arElem); return; }
}
else
{
while (*ptr)
{
{
// Memory will be freed in the FreeARElemCallback
}
else
}
}
}
mDNSexport void udns_validatelists(void *const v)
{
mDNS *const m = v;
NATTraversalInfo *n;
for (n = m->NATTraversals; n; n=n->next)
LogMemCorruption("m->NATTraversals: %p is garbage", n);
DNSServer *d;
for (d = m->DNSServers; d; d=d->next)
LogMemCorruption("m->Hostnames: %p is garbage", n);
}
#endif
// This should probably move to the UDS daemon -- the concept of legacy clients and automatic registration / automatic browsing
// is really a UDS API issue, not something intrinsic to uDNS
{
// "delete" parameter indicates the type of query.
switch (delete)
{
case UDNS_WAB_BROWSE_QUERY:
break;
case UDNS_WAB_LBROWSE_QUERY:
break;
case UDNS_WAB_REG_QUERY:
break;
default:
LogMsg("uDNS_DeleteWABQueries: ERROR!! returning from default");
return;
}
// When we get the results to the domain enumeration queries, we add a LocalOnly
// entry. For example, if we issue a domain enumeration query for b._dns-sd._udp.xxxx.com,
// and when we get a response, we add a LocalOnly entry b._dns-sd._udp.local whose RDATA
// points to what we got in the response. Locate the appropriate LocalOnly entries and delete
// them.
if (name1)
{
}
if (name2)
{
}
while (*arList)
{
{
LogInfo("uDNS_DeleteWABQueries: Deregistering PTR %##s -> %##s", dereg->ar.resrec.name->c, dereg->ar.resrec.rdata->u.name.c);
// Memory will be freed in the FreeARElemCallback
}
else
{
LogInfo("uDNS_DeleteWABQueries: Skipping PTR %##s -> %##s", dereg->ar.resrec.name->c, dereg->ar.resrec.rdata->u.name.c);
}
}
}
{
int action = 0;
// step 1: mark each element for removal
// Make sure we have the search domains from the platform layer so that if we start the WAB
// queries below, we have the latest information.
mDNS_Lock(m);
{
// If the configuration did not change, clear the flag so that we don't free the searchlist.
// We still have to start the domain enumeration queries as we may not have started them
// before.
LogInfo("uDNS_SetupWABQueries: No config change");
}
mDNS_Unlock(m);
if (m->WABBrowseQueriesCount)
if (m->WABLBrowseQueriesCount)
if (m->WABRegQueriesCount)
// delete elems marked for removal, do queries for elems marked add
while (*p)
{
ptr = *p;
LogInfo("uDNS_SetupWABQueries:action 0x%x: Flags 0x%x, AuthRecs %p, InterfaceID %p %##s", action, ptr->flag, ptr->AuthRecs, ptr->InterfaceID, ptr->domain.c);
// If SLE_DELETE is set, stop all the queries, deregister all the records and free the memory.
// Otherwise, check to see what the "action" requires. If a particular action bit is not set and
// we have started the corresponding queries as indicated by the "flags", stop those queries and
// deregister the records corresponding to them.
{
{
// If the user has "local" in their DNS searchlist, we ignore that for the purposes of domain enumeration queries
// We suppressed the domain enumeration for scoped search domains below. When we enable that
// enable this.
{
}
{
}
{
}
// deregister records generated from answers to the query
while (arList)
{
LogInfo("uDNS_SetupWABQueries: DELETE Deregistering PTR %##s -> %##s", dereg->ar.resrec.name->c, dereg->ar.resrec.rdata->u.name.c);
// Memory will be freed in the FreeARElemCallback
}
continue;
}
// If the user has "local" in their DNS searchlist, we ignore that for the purposes of domain enumeration queries
// We suppressed the domain enumeration for scoped search domains below. When we enable that
// enable this.
{
}
{
}
{
}
// Fall through to handle the ADDs
}
{
// If the user has "local" in their DNS searchlist, we ignore that for the purposes of domain enumeration queries.
// Also, suppress the domain enumeration for scoped search domains for now until there is a need.
{
err1 = mDNS_GetDomains(m, &ptr->BrowseQ, mDNS_DomainTypeBrowse, &ptr->domain, ptr->InterfaceID, FoundDomain, ptr);
if (err1)
{
LogMsg("uDNS_SetupWABQueries: GetDomains for domain %##s returned error(s):\n"
}
else
{
}
err2 = mDNS_GetDomains(m, &ptr->DefBrowseQ, mDNS_DomainTypeBrowseDefault, &ptr->domain, ptr->InterfaceID, FoundDomain, ptr);
if (err2)
{
LogMsg("uDNS_SetupWABQueries: GetDomains for domain %##s returned error(s):\n"
}
else
{
}
// For simplicity, we mark a single bit for denoting that both the browse queries have started.
// It is not clear as to why one would fail to start and the other would succeed in starting up.
// If that happens, we will try to stop both the queries and one of them won't be in the list and
// it is not a hard error.
{
}
}
}
{
// If the user has "local" in their DNS searchlist, we ignore that for the purposes of domain enumeration queries.
// Also, suppress the domain enumeration for scoped search domains for now until there is a need.
{
err1 = mDNS_GetDomains(m, &ptr->AutomaticBrowseQ, mDNS_DomainTypeBrowseAutomatic, &ptr->domain, ptr->InterfaceID, FoundDomain, ptr);
if (err1)
{
LogMsg("uDNS_SetupWABQueries: GetDomains for domain %##s returned error(s):\n"
"%d (mDNS_DomainTypeBrowseAutomatic)\n",
}
else
{
}
}
}
{
// If the user has "local" in their DNS searchlist, we ignore that for the purposes of domain enumeration queries.
// Also, suppress the domain enumeration for scoped search domains for now until there is a need.
{
err1 = mDNS_GetDomains(m, &ptr->RegisterQ, mDNS_DomainTypeRegistration, &ptr->domain, ptr->InterfaceID, FoundDomain, ptr);
if (err1)
{
LogMsg("uDNS_SetupWABQueries: GetDomains for domain %##s returned error(s):\n"
}
else
{
}
err2 = mDNS_GetDomains(m, &ptr->DefRegisterQ, mDNS_DomainTypeRegistrationDefault, &ptr->domain, ptr->InterfaceID, FoundDomain, ptr);
if (err2)
{
LogMsg("uDNS_SetupWABQueries: GetDomains for domain %##s returned error(s):\n"
}
else
{
}
{
}
}
}
}
}
// mDNS_StartWABQueries is called once per API invocation where normally
// one of the bits is set.
{
if (queryType & UDNS_WAB_BROWSE_QUERY)
{
m->WABBrowseQueriesCount++;
}
if (queryType & UDNS_WAB_LBROWSE_QUERY)
{
m->WABLBrowseQueriesCount++;
}
if (queryType & UDNS_WAB_REG_QUERY)
{
m->WABRegQueriesCount++;
}
}
// mDNS_StopWABQueries is called once per API invocation where normally
// one of the bits is set.
{
if (queryType & UDNS_WAB_BROWSE_QUERY)
{
m->WABBrowseQueriesCount--;
}
if (queryType & UDNS_WAB_LBROWSE_QUERY)
{
m->WABLBrowseQueriesCount--;
}
if (queryType & UDNS_WAB_REG_QUERY)
{
m->WABRegQueriesCount--;
}
}
mDNSexport domainname *uDNS_GetNextSearchDomain(mDNS *const m, mDNSInterfaceID InterfaceID, mDNSs8 *searchIndex, mDNSBool ignoreDotLocal)
{
SearchListElem *p = SearchList;
int count = *searchIndex;
(void) m; // unused
if (count < 0) { LogMsg("uDNS_GetNextSearchDomain: count %d less than zero", count); return mDNSNULL; }
// Skip the domains that we already looked at before. Guard against "p"
// being NULL. When search domains change we may not set the SearchListIndex
// of the question to zero immediately e.g., domain enumeration query calls
// uDNS_SetupWABQueries which reads in the new search domain but does not
// restart the questions immediately. Questions are restarted as part of
// network change and hence temporarily SearchListIndex may be out of range.
p = p->next;
while (p)
{
if (labels > 0)
{
{
LogInfo("uDNS_GetNextSearchDomain: skipping search domain %##s, InterfaceID %p", p->domain.c, p->InterfaceID);
(*searchIndex)++;
p = p->next;
continue;
}
{
LogInfo("uDNS_GetNextSearchDomain: skipping local domain %##s, InterfaceID %p", p->domain.c, p->InterfaceID);
(*searchIndex)++;
p = p->next;
continue;
}
}
// Point to the next one in the list which we will look at next time.
(*searchIndex)++;
// When we are appending search domains in a ActiveDirectory domain, the question's InterfaceID
// set to mDNSInterface_Unicast. Match the unscoped entries in that case.
p->InterfaceID == InterfaceID)
{
LogInfo("uDNS_GetNextSearchDomain returning domain %##s, InterfaceID %p", p->domain.c, p->InterfaceID);
return &p->domain;
}
LogInfo("uDNS_GetNextSearchDomain skipping domain %##s, InterfaceID %p", p->domain.c, p->InterfaceID);
p = p->next;
}
return mDNSNULL;
}
{
CacheGroup *cg;
{
// If a resource record can answer A or AAAA, they need to be flushed so that we will
// deliver an ADD or RMV
{
}
}
}
// Retry questions which has seach domains appended
{
DNSQuestion *q;
// Check to see if there are any questions which needs search domains to be applied.
// If there is none, search domains can't possibly affect them.
{
if (q->AppendSearchDomains)
{
break;
}
}
if (!found)
{
LogInfo("RetrySearchDomainQuestions: Questions with AppendSearchDomain not found");
return;
}
LogInfo("RetrySearchDomainQuestions: Question with AppendSearchDomain found %##s (%s)", q->qname.c, DNSTypeName(q->qtype));
// does this. When we restart the question, we first want to try the new search domains rather
// than use the entries that is already in the cache. When we appended search domains, we might
// have created cache entries which is no longer valid as there are new search domains now
}
// Construction of Default Browse domain list (i.e. when clients pass NULL) is as follows:
// 1) query for b._dns-sd._udp.local on LocalOnly interface
// (.local manually generated via explicit callback)
// 2) for each search domain (from prefs pane), query for b._dns-sd._udp.<searchdomain>.
// 3) for each result from (2), register LocalOnly PTR record b._dns-sd._udp.local. -> <result>
// 4) result above should generate a callback from question in (1). result added to global list
// 5) global list delivered to client via GetSearchDomainList()
// 6) client calls to enumerate domains now go over LocalOnly interface
// (!!!KRS may add outgoing interface in addition)
{
// Check our structures are reasonable sizes. Including overly-large buffers, or embedding
// other overly-large structures instead of having a pointer to them, can inadvertently
// cause structure sizes (and therefore memory usage) to balloon unreasonably.
};
#else // !UNICAST_DISABLED
{
(void) m;
(void) rr;
return mDNSNULL;
}
{
(void) m;
(void) name;
return mDNSNULL;
}
{
(void) m;
(void) q;
return mDNSNULL;
}
{
(void) m;
(void) q;
}
{
(void) tcp;
}
{
(void) m;
(void) traversal;
return mStatus_UnsupportedErr;
}
{
(void) m;
(void) traversal;
return mStatus_UnsupportedErr;
}
{
(void) m;
(void) q;
}
mDNSexport ZoneData *StartGetZoneData(mDNS *const m, const domainname *const name, const ZoneService target, ZoneDataCallback callback, void *ZoneDataContext)
{
(void) m;
(void) name;
(void) target;
(void) callback;
(void) ZoneDataContext;
return mDNSNULL;
}
{
(void) m;
(void) err;
(void) zoneData;
}
mDNSexport uDNS_LLQType uDNS_recvLLQResponse(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *const end,
{
(void) m;
(void) msg;
(void) end;
(void) srcaddr;
(void) srcport;
(void) matchQuestion;
return uDNS_LLQ_Not;
}
{
(void) m;
(void) q;
(void) responseFlags;
}
{
(void) domain;
(void) InterfaceID;
}
{
(void) m;
}
mDNSexport mStatus mDNS_SetSecretForDomain(mDNS *m, DomainAuthInfo *info, const domainname *domain, const domainname *keyname, const char *b64keydata, const domainname *hostname, mDNSIPPort *port, mDNSBool autoTunnel)
{
(void) m;
(void) info;
(void) domain;
(void) keyname;
(void) b64keydata;
(void) hostname;
(void) port;
(void) autoTunnel;
return mStatus_UnsupportedErr;
}
mDNSexport domainname *uDNS_GetNextSearchDomain(mDNS *const m, mDNSInterfaceID InterfaceID, mDNSs8 *searchIndex, mDNSBool ignoreDotLocal)
{
(void) m;
(void) InterfaceID;
(void) searchIndex;
(void) ignoreDotLocal;
return mDNSNULL;
}
{
(void) m;
(void) name;
return mDNSNULL;
}
{
(void) m;
(void) traversal;
return mStatus_UnsupportedErr;
}
{
(void) m;
(void) traversal;
return mStatus_UnsupportedErr;
}
mDNSexport DNSServer *mDNS_AddDNSServer(mDNS *const m, const domainname *d, const mDNSInterfaceID interface, const mDNSs32 serviceID, const mDNSAddr *addr,
const mDNSIPPort port, mDNSu32 scoped, mDNSu32 timeout, mDNSBool cellIntf, mDNSu16 resGroupID, mDNSBool reqA,
{
(void) m;
(void) d;
(void) interface;
(void) serviceID;
(void) addr;
(void) port;
(void) scoped;
(void) timeout;
(void) cellIntf;
(void) resGroupID;
(void) reqA;
(void) reqAAAA;
(void) reqDO;
return mDNSNULL;
}
{
(void) m;
}
{
(void) m;
(void) queryType;
}
{
(void) m;
(void) queryType;
}
mDNSexport void mDNS_AddDynDNSHostName(mDNS *m, const domainname *fqdn, mDNSRecordCallback *StatusCallback, const void *StatusContext)
{
(void) m;
(void) fqdn;
(void) StatusCallback;
(void) StatusContext;
}
mDNSexport void mDNS_SetPrimaryInterfaceInfo(mDNS *m, const mDNSAddr *v4addr, const mDNSAddr *v6addr, const mDNSAddr *router)
{
(void) m;
(void) v4addr;
(void) v6addr;
(void) router;
}
{
(void) m;
(void) fqdn;
}
{
(void) m;
(void) waitTicks;
}
{
(void)q;
return mDNSfalse;
}
#endif // !UNICAST_DISABLED