Lines Matching defs:m
67 mDNSlocal void BeginSleepProcessing(mDNS *const m);
68 mDNSlocal void RetrySPSRegistrations(mDNS *const m);
69 mDNSlocal void SendWakeup(mDNS *const m, mDNSInterfaceID InterfaceID, mDNSEthAddr *EthAddr, mDNSOpaque48 *password);
70 mDNSlocal mDNSBool CacheRecordRmvEventsForQuestion(mDNS *const m, DNSQuestion *q);
71 mDNSlocal mDNSBool LocalRecordRmvEventsForQuestion(mDNS *const m, DNSQuestion *q);
72 mDNSlocal void mDNS_PurgeForQuestion(mDNS *const m, DNSQuestion *q);
73 mDNSlocal void CheckForDNSSECRecords(mDNS *const m, DNSQuestion *q);
74 mDNSlocal void mDNS_SendKeepalives(mDNS *const m);
78 mDNSlocal void AdvertiseAllInterfaceRecords(mDNS *const m);
79 mDNSlocal void DeadvertiseAllInterfaceRecords(mDNS *const m);
80 mDNSlocal void FreeNSECRecords(mDNS *const m, CacheRecord *NSECRecords);
81 mDNSlocal void mDNSParseNSEC3Records(mDNS *const m, const DNSMessage *const response, const mDNSu8 *end,
145 mDNSlocal void SetNextQueryStopTime(mDNS *const m, const DNSQuestion *const q)
147 mDNS_CheckLock(m);
150 if (m->mDNS_busy != m->mDNS_reentrancy+1) *(long*)0 = 0;
153 if (m->NextScheduledStopTime - q->StopTime > 0)
154 m->NextScheduledStopTime = q->StopTime;
157 mDNSexport void SetNextQueryTime(mDNS *const m, const DNSQuestion *const q)
159 mDNS_CheckLock(m);
162 if (m->mDNS_busy != m->mDNS_reentrancy+1) *(long*)0 = 0;
168 // m->NextScheduledQuery = NextQSendTime(q) or
169 // m->NextuDNSEvent = NextQSendTime(q)
170 mDNSs32 *const timer = mDNSOpaque16IsZero(q->TargetQID) ? &m->NextScheduledQuery : &m->NextuDNSEvent;
299 mDNSexport AuthGroup *InsertAuthRecord(mDNS *const m, AuthHash *r, AuthRecord *rr)
307 LogInfo("InsertAuthRecord: inserting auth record %s from table", ARDisplayString(m, rr));
314 mDNSexport AuthGroup *RemoveAuthRecord(mDNS *const m, AuthHash *r, AuthRecord *rr)
322 if (!a) { LogMsg("RemoveAuthRecord: ERROR!! AuthGroup not found for %s", ARDisplayString(m, rr)); return mDNSNULL; }
332 LogInfo("RemoveAuthRecord: removing auth record %s from table", ARDisplayString(m, rr));
341 mDNSexport CacheGroup *CacheGroupForName(const mDNS *const m, const mDNSu32 slot, const mDNSu32 namehash, const domainname *const name)
344 for (cg = m->rrcache_hash[slot]; cg; cg=cg->next)
350 mDNSlocal CacheGroup *CacheGroupForRecord(const mDNS *const m, const mDNSu32 slot, const ResourceRecord *const rr)
352 return(CacheGroupForName(m, slot, rr->namehash, rr->name));
355 mDNSexport mDNSBool mDNS_AddressIsLocalSubnet(mDNS *const m, const mDNSInterfaceID InterfaceID, const mDNSAddr *addr, mDNSBool *myself)
363 for (intf = m->HostInterfaces; intf; intf = intf->next)
384 for (intf = m->HostInterfaces; intf; intf = intf->next)
409 mDNSlocal NetworkInterfaceInfo *FirstInterfaceForID(mDNS *const m, const mDNSInterfaceID InterfaceID)
411 NetworkInterfaceInfo *intf = m->HostInterfaces;
416 mDNSlocal NetworkInterfaceInfo *FirstIPv4LLInterfaceForID(mDNS *const m, const mDNSInterfaceID InterfaceID)
425 for (intf = m->HostInterfaces; intf; intf = intf->next)
437 mDNSexport char *InterfaceNameForID(mDNS *const m, const mDNSInterfaceID InterfaceID)
439 NetworkInterfaceInfo *intf = FirstInterfaceForID(m, InterfaceID);
444 mDNSlocal void GenerateNegativeResponse(mDNS *const m, QC_result qc)
447 if (!m->CurrentQuestion) { LogMsg("GenerateNegativeResponse: ERROR!! CurrentQuestion not set"); return; }
448 q = m->CurrentQuestion;
451 MakeNegativeCacheRecord(m, &m->rec.r, &q->qname, q->qnamehash, q->qtype, q->qclass, 60, mDNSInterface_Any, mDNSNULL);
459 AnswerCurrentQuestionWithResourceRecord(m, &m->rec.r, qc);
460 if (m->CurrentQuestion == q) { q->ThisQInterval = 0; } // Deactivate this question
462 m->rec.r.resrec.RecordType = 0; // Clear RecordType to show we're not still using it
465 mDNSexport void AnswerQuestionByFollowingCNAME(mDNS *const m, DNSQuestion *q, ResourceRecord *rr)
471 q, q->qname.c, DNSTypeName(q->qtype), q->CNAMEReferrals, selfref ? " (Self-Referential)" : "", RRDisplayString(m, rr));
483 if (mDNSPlatformPeekUDP(m, q->LocalSocket))
509 q, q->qname.c, DNSTypeName(q->qtype), q->CNAMEReferrals, RRDisplayString(m, rr));
511 mDNS_StopQuery_internal(m, q); // Stop old query
520 q, q->qname.c, DNSTypeName(q->qtype), RRDisplayString(m, rr));
523 mDNS_StartQuery_internal(m, q); // start new query
539 // Note: All the callers should use the m->CurrentQuestion to see if the question is still valid or not
540 mDNSlocal void AnswerLocalQuestionWithLocalAuthRecord(mDNS *const m, AuthRecord *rr, QC_result AddRecord)
542 DNSQuestion *q = m->CurrentQuestion;
547 LogMsg("AnswerLocalQuestionWithLocalAuthRecord: ERROR!! CurrentQuestion NULL while answering with %s", ARDisplayString(m, rr));
557 AddRecord ? "Add" : "Rmv", rr->resrec.RecordType, ARDisplayString(m, rr));
574 q->QuestionCallback(m, q, &rr->resrec, AddRecord);
578 // using m->CurrentQuestion
579 if (followcname && m->CurrentQuestion == q)
580 AnswerQuestionByFollowingCNAME(m, q, &rr->resrec);
585 q->QuestionCallback(m, q, &rr->resrec, AddRecord);
591 mDNSlocal void AnswerInterfaceAnyQuestionsWithLocalAuthRecord(mDNS *const m, AuthRecord *rr, QC_result AddRecord)
593 if (m->CurrentQuestion)
594 LogMsg("AnswerInterfaceAnyQuestionsWithLocalAuthRecord: ERROR m->CurrentQuestion already set: %##s (%s)",
595 m->CurrentQuestion->qname.c, DNSTypeName(m->CurrentQuestion->qtype));
596 m->CurrentQuestion = m->Questions;
597 while (m->CurrentQuestion && m->CurrentQuestion != m->NewQuestions)
600 DNSQuestion *q = m->CurrentQuestion;
606 AnswerLocalQuestionWithLocalAuthRecord(m, rr, AddRecord); // MUST NOT dereference q again
607 if (m->CurrentQuestion == q) // If m->CurrentQuestion was not auto-advanced, do it ourselves now
608 m->CurrentQuestion = q->next;
610 m->CurrentQuestion = mDNSNULL;
621 // AnswerAllLocalQuestionsWithLocalAuthRecord is used by the m->NewLocalRecords loop in mDNS_Execute(),
624 mDNSlocal void AnswerAllLocalQuestionsWithLocalAuthRecord(mDNS *const m, AuthRecord *rr, QC_result AddRecord)
626 if (m->CurrentQuestion)
627 LogMsg("AnswerAllLocalQuestionsWithLocalAuthRecord ERROR m->CurrentQuestion already set: %##s (%s)",
628 m->CurrentQuestion->qname.c, DNSTypeName(m->CurrentQuestion->qtype));
630 m->CurrentQuestion = m->LocalOnlyQuestions;
631 while (m->CurrentQuestion && m->CurrentQuestion != m->NewLocalOnlyQuestions)
634 DNSQuestion *q = m->CurrentQuestion;
641 AnswerLocalQuestionWithLocalAuthRecord(m, rr, AddRecord); // MUST NOT dereference q again
642 if (m->CurrentQuestion == q) // If m->CurrentQuestion was not auto-advanced, do it ourselves now
643 m->CurrentQuestion = q->next;
646 m->CurrentQuestion = mDNSNULL;
650 AnswerInterfaceAnyQuestionsWithLocalAuthRecord(m, rr, AddRecord);
791 mDNSlocal void SetNextAnnounceProbeTime(mDNS *const m, const AuthRecord *const rr)
795 if ((rr->LastAPTime + rr->ThisAPInterval) - m->timenow > mDNSPlatformOneSecond * 10)
797 LogMsg("SetNextAnnounceProbeTime: ProbeCount %d Next in %d %s", rr->ProbeCount, (rr->LastAPTime + rr->ThisAPInterval) - m->timenow, ARDisplayString(m, rr));
798 LogMsg("SetNextAnnounceProbeTime: m->SuppressProbes %d m->timenow %d diff %d", m->SuppressProbes, m->timenow, m->SuppressProbes - m->timenow);
800 if (m->NextScheduledProbe - (rr->LastAPTime + rr->ThisAPInterval) >= 0)
801 m->NextScheduledProbe = (rr->LastAPTime + rr->ThisAPInterval);
806 if (m->NextScheduledProbe - m->timenow < 0)
807 m->NextScheduledProbe = m->timenow;
811 if (m->NextScheduledResponse - (rr->LastAPTime + rr->ThisAPInterval) >= 0)
812 m->NextScheduledResponse = (rr->LastAPTime + rr->ThisAPInterval);
816 mDNSlocal void InitializeLastAPTime(mDNS *const m, AuthRecord *const rr)
821 // * If this is a record type that's going to probe, then we use the m->SuppressProbes time.
822 // * Otherwise, if it's not going to probe, but m->SuppressProbes is set because we have other
830 // * If it's not going to probe and m->SuppressProbes is not already set then we should announce immediately.
835 if (m->SuppressProbes == 0 || m->SuppressProbes - m->timenow < 0)
843 m->SuppressProbes = NonZeroTime(m->timenow + DefaultProbeIntervalForTypeUnique/2 + mDNSRandom(DefaultProbeIntervalForTypeUnique/2));
846 if (m->SuppressProbes - m->NextScheduledProbe >= 0)
847 m->SuppressProbes = NonZeroTime(m->NextScheduledProbe);
848 if (m->SuppressProbes - m->timenow < 0) // Make sure we don't set m->SuppressProbes excessively in the past
849 m->SuppressProbes = m->timenow;
852 if (m->SuppressProbes - m->NextScheduledQuery >= 0)
853 m->SuppressProbes = NonZeroTime(m->NextScheduledQuery);
854 if (m->SuppressProbes - m->timenow < 0) // Make sure we don't set m->SuppressProbes excessively in the past
855 m->SuppressProbes = m->timenow;
857 // except... don't expect to be able to send before the m->SuppressSending timer fires
858 if (m->SuppressSending && m->SuppressProbes - m->SuppressSending < 0)
859 m->SuppressProbes = NonZeroTime(m->SuppressSending);
861 if (m->SuppressProbes - m->timenow > mDNSPlatformOneSecond * 8)
863 LogMsg("InitializeLastAPTime ERROR m->SuppressProbes %d m->NextScheduledProbe %d m->NextScheduledQuery %d m->SuppressSending %d %d",
864 m->SuppressProbes - m->timenow,
865 m->NextScheduledProbe - m->timenow,
866 m->NextScheduledQuery - m->timenow,
867 m->SuppressSending,
868 m->SuppressSending - m->timenow);
869 m->SuppressProbes = NonZeroTime(m->timenow + DefaultProbeIntervalForTypeUnique/2 + mDNSRandom(DefaultProbeIntervalForTypeUnique/2));
872 rr->LastAPTime = m->SuppressProbes - rr->ThisAPInterval;
874 else if (m->SuppressProbes && m->SuppressProbes - m->timenow >= 0)
875 rr->LastAPTime = m->SuppressProbes - rr->ThisAPInterval + DefaultProbeIntervalForTypeUnique * DefaultProbeCountForTypeUnique + rr->ThisAPInterval / 2;
877 rr->LastAPTime = m->timenow - rr->ThisAPInterval;
886 rr->LastAPTime = m->timenow;
890 rr->LastMCTime = m->timenow;
893 SetNextAnnounceProbeTime(m, rr);
896 mDNSlocal const domainname *SetUnicastTargetToHostName(mDNS *const m, AuthRecord *rr)
904 DomainAuthInfo *AuthInfo = GetAuthInfoForName_internal(m, rr->resrec.name);
908 target = GetServiceTarget(m, rr);
912 LogInfo("SetUnicastTargetToHostName No target for %s", ARDisplayString(m, rr));
918 LogInfo("SetUnicastTargetToHostName target %##s for resource record %s", target->c, ARDisplayString(m,rr));
923 // Right now this only applies to mDNS (.local) services where the target host is always m->MulticastHostname
925 mDNSlocal void SetTargetToHostName(mDNS *const m, AuthRecord *const rr)
928 const domainname *newname = &m->MulticastHostname;
934 const domainname *const n = SetUnicastTargetToHostName(m, rr);
962 InitializeLastAPTime(m, rr);
966 mDNSlocal void AcknowledgeRecord(mDNS *const m, AuthRecord *const rr)
974 rr->RecordCallback(m, rr, mStatus_NoError);
979 mDNSexport void ActivateUnicastRegistration(mDNS *const m, AuthRecord *const rr)
996 LogMsg("ActivateUnicastRegistration: ERROR!! Resource record %s wrong, expecting SRV type", ARDisplayString(m, srvRR));
1001 ARDisplayString(m, srvRR), srvRR->state, rr->resrec.name->c, DNSTypeName(rr->resrec.rrtype));
1009 LogInfo("ActivateUnicastRegistration record %s in regState_NoTarget, not activating", ARDisplayString(m, rr));
1019 LogInfo("ActivateUnicastRegistration: Resource record %s, current state %d, moving to DeregPending", ARDisplayString(m, rr), rr->state);
1024 LogInfo("ActivateUnicastRegistration: Resource record %s, current state %d, moving to Pending", ARDisplayString(m, rr), rr->state);
1031 rr->LastAPTime = m->timenow - rr->ThisAPInterval;
1041 mDNS_StopNATOperation_internal(m, &rr->NATinfo);
1044 if (rr->nta) { CancelGetZoneData(m, rr->nta); rr->nta = mDNSNULL; }
1046 if (m->NextuDNSEvent - (rr->LastAPTime + rr->ThisAPInterval) >= 0)
1047 m->NextuDNSEvent = (rr->LastAPTime + rr->ThisAPInterval);
1134 mDNSlocal void DecrementAutoTargetServices(mDNS *const m, AuthRecord *const rr)
1138 m->AutoTargetServices--;
1139 LogInfo("DecrementAutoTargetServices: AutoService Record %s, AutoTargetServices %d", ARDisplayString(m, rr), m->AutoTargetServices);
1140 if (!m->AutoTargetServices)
1141 DeadvertiseAllInterfaceRecords(m);
1145 mDNSlocal void IncrementAutoTargetServices(mDNS *const m, AuthRecord *const rr)
1149 int count = m->AutoTargetServices;
1153 m->AutoTargetServices++;
1154 LogInfo("IncrementAutoTargetServices: AutoService Record %s, AutoTargetServices %d", ARDisplayString(m, rr), m->AutoTargetServices);
1156 AdvertiseAllInterfaceRecords(m);
1160 mDNSlocal void getKeepaliveRaddr(mDNS *const m, AuthRecord *rr, mDNSAddr *raddr)
1173 LogMsg("getKeepaliveRaddr: not a valid record %s for keepalive %#a:%d %#a:%d", ARDisplayString(m, rr), &laddr, lport.NotAnInteger, raddr, rport.NotAnInteger);
1180 mDNSexport mStatus mDNS_Register_internal(mDNS *const m, AuthRecord *const rr)
1184 AuthRecord **p = &m->ResourceRecords;
1185 AuthRecord **d = &m->DuplicateRecords;
1188 { LogMsg("mDNS_Register_internal: TTL %X should be 1 - 0x7FFFFFFF %s", rr->resrec.rroriginalttl, ARDisplayString(m, rr)); return(mStatus_BadParamErr); }
1191 { LogMsg("mDNS_Register_internal: RecordType must be non-zero %s", ARDisplayString(m, rr)); return(mStatus_BadParamErr); }
1193 if (m->ShutdownTime)
1194 { LogMsg("mDNS_Register_internal: Shutting down, can't register %s", ARDisplayString(m, rr)); return(mStatus_ServiceNotRunning); }
1196 if (m->DivertMulticastAdvertisements && !AuthRecord_uDNS(rr))
1206 NetworkInterfaceInfo *intf = FirstInterfaceForID(m, rr->resrec.InterfaceID);
1210 LogInfo("mDNS_Register_internal: Diverting record to local-only %s", ARDisplayString(m, rr));
1215 if (CheckAuthSameRecord(&m->rrauth, rr))
1295 if (!rr->AutoTarget) InitializeLastAPTime(m, rr);
1345 SetTargetToHostName(m, rr); // Also sets rdlength and rdestimate for us, and calls InitializeLastAPTime();
1354 LogInfo("mDNS_Register_internal: record %s in NoTarget state", ARDisplayString(m, rr));
1365 { LogMsg("Attempt to register record with invalid name: %s", ARDisplayString(m, rr)); return(mStatus_Invalid); }
1369 { LogMsg("Attempt to register record with invalid rdata: %s", ARDisplayString(m, rr)); return(mStatus_Invalid); }
1383 if (CheckAuthRecordConflict(&m->rrauth, rr))
1385 LogInfo("mDNS_Register_internal: Name conflict %s (%p), InterfaceID %p", ARDisplayString(m, rr), rr, rr->resrec.InterfaceID);
1395 if (!m->NewLocalRecords) m->NewLocalRecords = rr;
1405 if (rr->state != regState_NoTarget) ActivateUnicastRegistration(m, rr);
1416 r = CheckAuthIdenticalRecord(&m->rrauth, rr);
1420 for (r = m->ResourceRecords; r; r=r->next)
1430 debugf("mDNS_Register_internal:Adding to duplicate list %s", ARDisplayString(m,rr));
1441 debugf("mDNS_Register_internal: Adding to active record list %s", ARDisplayString(m,rr));
1445 ag = InsertAuthRecord(m, &m->rrauth, rr);
1447 m->NewLocalOnlyRecords = mDNStrue;
1452 AcknowledgeRecord(m, rr);
1457 if (!m->NewLocalRecords) m->NewLocalRecords = rr;
1471 getKeepaliveRaddr(m, rr, &raddr);
1474 mDNSPlatformGetRemoteMacAddr(m, &raddr);
1480 IncrementAutoTargetServices(m, rr);
1483 AcknowledgeRecord(m, rr);
1486 mDNS_UpdateAllowSleep(m);
1492 mDNSlocal void RecordProbeFailure(mDNS *const m, const AuthRecord *const rr)
1494 m->ProbeFailTime = m->timenow;
1495 m->NumFailedProbes++;
1503 if (m->NumFailedProbes >= 15)
1505 m->SuppressProbes = NonZeroTime(m->timenow + mDNSPlatformOneSecond * 5);
1507 m->NumFailedProbes, rr->resrec.name->c, DNSTypeName(rr->resrec.rrtype));
1511 mDNSlocal void CompleteRDataUpdate(mDNS *const m, AuthRecord *const rr)
1518 rr->UpdateCallback(m, rr, OldRData, OldRDLen); // ... and let the client know
1524 mDNSexport mStatus mDNS_Deregister_internal(mDNS *const m, AuthRecord *const rr, mDNS_Dereg_type drt)
1528 AuthRecord **p = &m->ResourceRecords; // Find this record in our list of active records
1538 a = AuthGroupForRecord(&m->rrauth, slot, &rr->resrec);
1556 for (r2 = m->DuplicateRecords; r2; r2=r2->next) if (RecordIsLocalDuplicate(r2, rr)) r2->ProbeCount = 0xFF;
1562 AuthRecord **d = &m->DuplicateRecords;
1573 if (!InsertAuthRecord(m, &m->rrauth, dup)) LogMsg("mDNS_Deregister_internal: ERROR!! cannot insert %s", ARDisplayString(m, dup));
1605 p = &m->DuplicateRecords;
1629 LogMsg("mDNS_Deregister_internal: Record %p not found in list %s", rr, ARDisplayString(m,rr));
1637 // it's tempting to just do "AnswerAllLocalQuestionsWithLocalAuthRecord(m, rr, mDNSfalse)" here, but that would not not be safe.
1638 // The AnswerAllLocalQuestionsWithLocalAuthRecord routine walks the question list invoking client callbacks, using the "m->CurrentQuestion"
1641 // which means that the "m->CurrentQuestion" mechanism is already in use to protect that list, so we can't use it twice.
1654 m->LocalRemoveEvents = mDNStrue;
1655 uDNS_DeregisterRecord(m, rr);
1676 mDNS_StopNATOperation_internal(m, &rr->NATinfo);
1679 if (rr->nta) { CancelGetZoneData(m, rr->nta); rr->nta = mDNSNULL; }
1685 LogMsg("mDNS_Deregister_internal: %s already marked kDNSRecordTypeUnregistered", ARDisplayString(m, rr));
1688 LogMsg("mDNS_Deregister_internal: %s already marked kDNSRecordTypeDeregistering", ARDisplayString(m, rr));
1701 verbosedebugf("mDNS_Deregister_internal: Starting deregistration for %s", ARDisplayString(m, rr));
1706 rr->LastAPTime = m->timenow - rr->ThisAPInterval;
1707 m->LocalRemoveEvents = mDNStrue;
1708 if (m->NextScheduledResponse - (m->timenow + mDNSPlatformOneSecond/10) >= 0)
1709 m->NextScheduledResponse = (m->timenow + mDNSPlatformOneSecond/10);
1715 AuthGroup *ag = RemoveAuthRecord(m, &m->rrauth, rr);
1721 if (m->NewLocalRecords == rr) m->NewLocalRecords = rr->next;
1722 DecrementAutoTargetServices(m, rr);
1725 if (m->CurrentRecord == rr) m->CurrentRecord = rr->next;
1730 // if (rr->AnsweredLocalQ) { AnswerAllLocalQuestionsWithLocalAuthRecord(m, rr, mDNSfalse); rr->AnsweredLocalQ = mDNSfalse; }
1732 verbosedebugf("mDNS_Deregister_internal: Deleting record for %s", ARDisplayString(m, rr));
1740 if (rr->NewRData) CompleteRDataUpdate(m, rr); // Update our rdata, clear the NewRData pointer, and return memory to the client
1750 LogInfo("mDNS_Deregister_internal: mStatus_MemFree for %s", ARDisplayString(m, rr));
1752 rr->RecordCallback(m, rr, mStatus_MemFree); // MUST NOT touch rr after this
1757 RecordProbeFailure(m, rr);
1760 rr->RecordCallback(m, rr, mStatus_NameConflict); // MUST NOT touch rr after this
1765 r2 = m->DuplicateRecords;
1774 mDNS_Deregister_internal(m, r2, mDNS_Dereg_conflict);
1777 r2 = m->DuplicateRecords;
1782 mDNS_UpdateAllowSleep(m);
1807 mDNSlocal void AddAdditionalsToResponseList(mDNS *const m, AuthRecord *ResponseRecords, AuthRecord ***nrpp, const mDNSInterfaceID InterfaceID)
1823 for (rr2=m->ResourceRecords; rr2; rr2=rr2->next) // Scan list of resource records
1832 for (rr2=m->ResourceRecords; rr2; rr2=rr2->next) // Scan list of resource records
1841 if (ResourceRecordIsValidInterfaceAnswer(&m->DeviceInfo, InterfaceID) &&
1842 SameDomainLabel(rr->resrec.rdata->u.name.c, m->DeviceInfo.resrec.name->c))
1843 AddRecordToResponseList(nrpp, &m->DeviceInfo, rr);
1856 mDNSlocal void SendDelayedUnicastResponse(mDNS *const m, const mDNSAddr *const dest, const mDNSInterfaceID InterfaceID)
1861 NetworkInterfaceInfo *intf = FirstInterfaceForID(m, InterfaceID);
1865 for (rr = m->ResourceRecords; rr; rr=rr->next)
1886 LogInfo("SendDelayedUnicastResponse: Not sending %s, on %s", ARDisplayString(m, rr), InterfaceNameForID(m, InterfaceID));
1900 AddAdditionalsToResponseList(m, ResponseRecords, &nrp, InterfaceID);
1904 mDNSu8 *responseptr = m->omsg.data;
1906 InitializeDNSMessage(&m->omsg.h, zeroID, ResponseFlags);
1921 newptr = PutResourceRecordTTLWithLimit(&m->omsg, responseptr, &m->omsg.h.numAnswers, &rr->resrec, rr->resrec.rroriginalttl,
1922 m->omsg.data + (AllowedRRSpace(&m->omsg) - AnoninfoSpace));
1925 if (!newptr && m->omsg.h.numAnswers)
1939 for (rr = m->ResourceRecords; rr; rr=rr->next)
1945 newptr = PutResourceRecord(&m->omsg, responseptr, &m->omsg.h.numAuthorities, nsec3RR);
1949 debugf("SendDelayedUnicastResponse: Added NSEC3 Record %s on %p", RRDisplayString(m, nsec3RR), intf->InterfaceID);
1954 LogMsg("SendDelayedUnicastResponse: ERROR!! Cannot Add NSEC3 Record %s on %p", RRDisplayString(m, nsec3RR), intf->InterfaceID);
1966 newptr = PutResourceRecord(&m->omsg, responseptr, &m->omsg.h.numAdditionals, &rr->resrec);
1970 if (newptr && m->omsg.h.numAnswers) rr->RequireGoodbye = mDNStrue;
1978 if (m->omsg.h.numAnswers)
1979 mDNSSendDNSMessage(m, &m->omsg, responseptr, InterfaceID, mDNSNULL, dest, MulticastDNSPort, mDNSNULL, mDNSNULL, mDNSfalse);
1983 // CompleteDeregistration guarantees that on exit the record will have been cut from the m->ResourceRecords list
1985 mDNSexport void CompleteDeregistration(mDNS *const m, AuthRecord *rr)
1987 LogInfo("CompleteDeregistration: called for Resource record %s", ARDisplayString(m, rr));
1993 if (rr->AnsweredLocalQ) { AnswerAllLocalQuestionsWithLocalAuthRecord(m, rr, QC_rmv); rr->AnsweredLocalQ = mDNSfalse; }
1994 mDNS_Deregister_internal(m, rr, mDNS_Dereg_normal); // Don't touch rr after this
1998 // any deregistering records that remain in the m->ResourceRecords list.
2002 mDNSlocal void DiscardDeregistrations(mDNS *const m)
2004 if (m->CurrentRecord)
2005 LogMsg("DiscardDeregistrations ERROR m->CurrentRecord already set %s", ARDisplayString(m, m->CurrentRecord));
2006 m->CurrentRecord = m->ResourceRecords;
2008 while (m->CurrentRecord)
2010 AuthRecord *rr = m->CurrentRecord;
2012 CompleteDeregistration(m, rr); // Don't touch rr after this
2014 m->CurrentRecord = rr->next;
2087 mDNSlocal void SendARP(mDNS *const m, const mDNSu8 op, const AuthRecord *const rr,
2091 mDNSu8 *ptr = m->omsg.data;
2092 NetworkInterfaceInfo *intf = FirstInterfaceForID(m, rr->resrec.InterfaceID);
2093 if (!intf) { LogMsg("SendARP: No interface with InterfaceID %p found %s", rr->resrec.InterfaceID, ARDisplayString(m,rr)); return; }
2124 mDNSPlatformSendRawPacket(m->omsg.data, ptr, rr->resrec.InterfaceID);
2152 mDNSlocal void SendNDP(mDNS *const m, const mDNSu8 op, const mDNSu8 flags, const AuthRecord *const rr,
2157 mDNSu8 *ptr = m->omsg.data;
2163 NetworkInterfaceInfo *intf = FirstInterfaceForID(m, rr->resrec.InterfaceID);
2164 if (!intf) { LogMsg("SendNDP: No interface with InterfaceID %p found %s", rr->resrec.InterfaceID, ARDisplayString(m,rr)); return; }
2244 m->omsg.data[0x13] = ptr - &m->omsg.data[0x36]; // Compute actual length
2245 checksum.NotAnInteger = ~IPv6CheckSum(spa, v6dst, 0x3A, &m->omsg.data[0x36], m->omsg.data[0x13]);
2246 m->omsg.data[0x38] = checksum.b[0];
2247 m->omsg.data[0x39] = checksum.b[1];
2249 mDNSPlatformSendRawPacket(m->omsg.data, ptr, rr->resrec.InterfaceID);
2252 mDNSlocal void SetupTracerOpt(const mDNS *const m, rdataOPT *const Trace)
2255 Trace->u.tracer.platf = m->mDNS_plat;
2262 mDNSlocal void SetupOwnerOpt(const mDNS *const m, const NetworkInterfaceInfo *const intf, rdataOPT *const owner)
2265 owner->u.owner.seq = m->SleepSeqNum;
2266 owner->u.owner.HMAC = m->PrimaryMAC;
2273 owner->optlen = DNSOpt_Owner_Space(&m->PrimaryMAC, &intf->MAC) - 4;
2282 mDNSlocal mDNSBool ShouldSendGoodbyesBeforeSleep(mDNS *const m, const NetworkInterfaceInfo *intf, AuthRecord *rr)
2289 if ((rr->AuthFlags & AuthFlagsWakeOnly) && (m->SleepState != SleepState_Awake))
2291 debugf("ShouldSendGoodbyesBeforeSleep: marking for goodbye", ARDisplayString(m, rr));
2295 if (m->SleepState != SleepState_Sleeping)
2302 debugf("ShouldSendGoodbyesBeforeSleep: not sending goodbye %s, int %p", ARDisplayString(m, rr), intf->InterfaceID);
2307 debugf("ShouldSendGoodbyesBeforeSleep: sending goodbye %s, int %p", ARDisplayString(m, rr), intf->InterfaceID);
2328 mDNSlocal void SendResponses(mDNS *const m)
2333 const NetworkInterfaceInfo *intf = GetFirstActiveInterface(m->HostInterfaces);
2335 m->NextScheduledResponse = m->timenow + 0x78000000;
2337 if (m->SleepState == SleepState_Transferring) RetrySPSRegistrations(m);
2339 for (rr = m->ResourceRecords; rr; rr=rr->next)
2346 if (!mDNSIPv4AddressIsZero(rr->v4Requester)) SendDelayedUnicastResponse(m, &v4, rr->ImmedAnswer);
2347 if (!mDNSIPv6AddressIsZero(rr->v6Requester)) SendDelayedUnicastResponse(m, &v6, rr->ImmedAnswer);
2350 LogMsg("SendResponses: ERROR: rr->ImmedUnicast still set: %s", ARDisplayString(m, rr));
2360 for (rr = m->ResourceRecords; rr; rr=rr->next)
2362 while (rr->NextUpdateCredit && m->timenow - rr->NextUpdateCredit >= 0) GrantUpdateCredit(rr);
2363 if (TimeToAnnounceThisRecord(rr, m->timenow))
2373 LogSPS("SendResponses: Sending wakeup %2d for %.6a %s", rr->AnnounceCount-3, &rr->WakeUp.IMAC, ARDisplayString(m, rr));
2374 SendWakeup(m, rr->resrec.InterfaceID, &rr->WakeUp.IMAC, &rr->WakeUp.password);
2384 r2->AnnounceCount-3, &r2->WakeUp.HMAC, &r2->WakeUp.IMAC, ARDisplayString(m,r2));
2385 SendNDP(m, NDP_Adv, NDP_Override, r2, &r2->AddressProxy.ip.v6, &r2->WakeUp.IMAC, &AllHosts_v6, &AllHosts_v6_Eth);
2387 r2->LastAPTime = m->timenow;
2401 rr->LastAPTime = m->timenow;
2405 rr->AnnounceCount, &rr->WakeUp.HMAC, &rr->WakeUp.IMAC, ARDisplayString(m,rr));
2406 SendARP(m, 1, rr, &rr->AddressProxy.ip.v4, &zeroEthAddr, &rr->AddressProxy.ip.v4, &onesEthAddr);
2411 rr->AnnounceCount, &rr->WakeUp.HMAC, &rr->WakeUp.IMAC, ARDisplayString(m,rr));
2412 SendNDP(m, NDP_Adv, NDP_Override, rr, &rr->AddressProxy.ip.v6, mDNSNULL, &AllHosts_v6, &AllHosts_v6_Eth);
2429 for (rr = m->ResourceRecords; rr; rr=rr->next)
2432 TimeToAnnounceThisRecord(rr, m->timenow + rr->ThisAPInterval/2) &&
2441 for (rr = m->ResourceRecords; rr; rr=rr->next)
2444 for (r2=m->ResourceRecords; r2; r2=r2->next) // Scan list of resource records
2456 if (ResourceRecordIsValidAnswer(&m->DeviceInfo) && SameDomainLabel(rr->resrec.rdata->u.name.c, m->DeviceInfo.resrec.name->c))
2458 if (!m->DeviceInfo.ImmedAnswer) m->DeviceInfo.ImmedAnswer = rr->ImmedAnswer;
2459 else m->DeviceInfo.ImmedAnswer = mDNSInterfaceMark;
2469 for (rr = m->ResourceRecords; rr; rr=rr->next)
2474 for (r2 = m->ResourceRecords; r2; r2=r2->next)
2482 for (r2 = m->ResourceRecords; r2; r2=r2->next)
2490 for (rr = m->ResourceRecords; rr; rr=rr->next)
2496 rr->LastMCTime = m->timenow;
2499 if (TimeToAnnounceThisRecord(rr, m->timenow + rr->ThisAPInterval/2))
2504 rr->LastAPTime = m->timenow;
2512 rr->LastMCTime = m->timenow;
2515 SetNextAnnounceProbeTime(m, rr);
2516 //if (rr->SendRNow) LogMsg("%-15.4a %s", &rr->v4Requester, ARDisplayString(m, rr));
2525 int OwnerRecordSpace = (m->AnnounceOwner && intf->MAC.l[0]) ? DNSOpt_Header_Space + DNSOpt_Owner_Space(&m->PrimaryMAC, &intf->MAC) : 0;
2531 mDNSu8 *responseptr = m->omsg.data;
2533 InitializeDNSMessage(&m->omsg.h, zeroID, ResponseFlags);
2539 for (rr = m->ResourceRecords; rr; rr=rr->next)
2547 LogInfo("SendResponses: Not sending %s, on %s", ARDisplayString(m, rr), InterfaceNameForID(m, rr->SendRNow));
2555 (rr->resrec.RecordType != kDNSRecordTypeDeregistering && !ShouldSendGoodbyesBeforeSleep(m, intf, rr));
2562 newptr = PutRR_OS_TTL(responseptr, &m->omsg.h.numAnswers, &rr->resrec, 0);
2582 newptr = PutRR_OS_TTL(responseptr, &m->omsg.h.numAnswers, &rr->resrec, active ? rr->resrec.rroriginalttl : 0);
2589 else if (rr->LastAPTime == m->timenow) numAnnounce++;else numAnswer++;
2604 debugf("SendResponses: Marking %s, OwnerRecordSpace %d, TraceRecordSpace %d, limit %p", ARDisplayString(m, rr), OwnerRecordSpace,
2605 TraceRecordSpace, m->omsg.data + AllowedRRSpace(&m->omsg) - OwnerRecordSpace - TraceRecordSpace);
2622 for (rr = m->ResourceRecords; rr; rr=rr->next)
2628 newptr = PutRR_OS_TTL(newptr, &m->omsg.h.numAuthorities, nsec3RR, nsec3RR->rroriginalttl);
2632 debugf("SendResponses: Added NSEC3 %s, OwnerRecordSpace %d, TraceRecordSpace %d, limit %p", ARDisplayString(m, rr), OwnerRecordSpace,
2633 TraceRecordSpace, m->omsg.data + AllowedRRSpace(&m->omsg) - OwnerRecordSpace - TraceRecordSpace);
2637 LogMsg("SendResponses: Cannot add NSEC3 %s, OwnerRecordSpace %d, TraceRecordSpace %d, limit %p", ARDisplayString(m, rr), OwnerRecordSpace,
2638 TraceRecordSpace, m->omsg.data + AllowedRRSpace(&m->omsg) - OwnerRecordSpace - TraceRecordSpace);
2645 for (rr = m->ResourceRecords; rr; rr=rr->next)
2650 mDNSBool SendAdditional = (m->omsg.h.numAnswers > 0);
2658 for (a = m->ResourceRecords; a; a=a->next)
2659 if (a->LastMCTime == m->timenow &&
2674 newptr = PutRR_OS(newptr, &m->omsg.h.numAdditionals, &rr->resrec);
2685 rr->LastMCTime = m->timenow;
2695 for (rr = m->ResourceRecords; rr; rr=rr->next)
2715 for (r2 = m->ResourceRecords; r2; r2=r2->next)
2718 if (r2->resrec.rrtype >= kDNSQType_ANY) { LogMsg("SendResponses: Can't create NSEC for record %s", ARDisplayString(m, r2)); break; }
2724 newptr = PutRR_OS(responseptr, &m->omsg.h.numAdditionals, &nsec.resrec);
2743 if (m->omsg.h.numAnswers || m->omsg.h.numAdditionals)
2757 SetupOwnerOpt(m, intf, &opt.resrec.rdata->u.opt[0]);
2758 SetupTracerOpt(m, &opt.resrec.rdata->u.opt[1]);
2762 SetupOwnerOpt(m, intf, &opt.resrec.rdata->u.opt[0]);
2766 SetupTracerOpt(m, &opt.resrec.rdata->u.opt[0]);
2768 newptr = PutResourceRecord(&m->omsg, responseptr, &m->omsg.h.numAdditionals, &opt.resrec);
2772 LogInfo("SendResponses put %s %s: %s %s", OwnerRecordSpace ? "OWNER" : "", TraceRecordSpace ? "TRACER" : "", intf->ifname, ARDisplayString(m, &opt));
2774 else if (m->omsg.h.numAnswers + m->omsg.h.numAuthorities + m->omsg.h.numAdditionals == 1)
2777 m->omsg.h.numQuestions, m->omsg.h.numAnswers, m->omsg.h.numAuthorities, m->omsg.h.numAdditionals, ARDisplayString(m, &opt));
2782 m->omsg.h.numQuestions, m->omsg.h.numAnswers, m->omsg.h.numAuthorities, m->omsg.h.numAdditionals, ARDisplayString(m, &opt));
2790 m->omsg.h.numAdditionals, m->omsg.h.numAdditionals == 1 ? "" : "s", intf->InterfaceID);
2792 if (intf->IPv4Available) mDNSSendDNSMessage(m, &m->omsg, responseptr, intf->InterfaceID, mDNSNULL, &AllDNSLinkGroup_v4, MulticastDNSPort, mDNSNULL, mDNSNULL, mDNSfalse);
2793 if (intf->IPv6Available) mDNSSendDNSMessage(m, &m->omsg, responseptr, intf->InterfaceID, mDNSNULL, &AllDNSLinkGroup_v6, MulticastDNSPort, mDNSNULL, mDNSNULL, mDNSfalse);
2794 if (!m->SuppressSending) m->SuppressSending = NonZeroTime(m->timenow + (mDNSPlatformOneSecond+9)/10);
2814 if (m->CurrentRecord)
2815 LogMsg("SendResponses ERROR m->CurrentRecord already set %s", ARDisplayString(m, m->CurrentRecord));
2816 m->CurrentRecord = m->ResourceRecords;
2817 while (m->CurrentRecord)
2819 rr = m->CurrentRecord;
2820 m->CurrentRecord = rr->next;
2826 (uint32_t)rr->SendRNow, (uint32_t)rr->resrec.InterfaceID, rr->resrec.RecordType, ARDisplayString(m, rr));
2832 if (rr->NewRData) CompleteRDataUpdate(m, rr); // Update our rdata, clear the NewRData pointer, and return memory to the client
2837 if (!AuthRecord_uDNS(rr)) CompleteDeregistration(m, rr); // Don't touch rr after this
2848 verbosedebugf("SendResponses: Next in %ld ticks", m->NextScheduledResponse - m->timenow);
2871 mDNSexport void ScheduleNextCacheCheckTime(mDNS *const m, const mDNSu32 slot, const mDNSs32 event)
2873 if (m->rrcache_nextcheck[slot] - event > 0)
2874 m->rrcache_nextcheck[slot] = event;
2875 if (m->NextCacheCheck - event > 0)
2876 m->NextCacheCheck = event;
2884 mDNSexport void SetNextCacheCheckTimeForRecord(mDNS *const m, CacheRecord *const rr)
2895 (rr->NextRequiredQuery - m->timenow) / mDNSPlatformOneSecond, CacheCheckGracePeriod(rr), CRDisplayString(m,rr));
2897 ScheduleNextCacheCheckTime(m, HashSlot(rr->resrec.name), NextCacheCheckEvent(rr));
2905 mDNSexport mStatus mDNS_Reconfirm_internal(mDNS *const m, CacheRecord *const rr, mDNSu32 interval)
2913 if (RRExpireTime(rr) - m->timenow > (mDNSs32)((interval * 4) / 3))
2918 if (!m->RandomReconfirmDelay) m->RandomReconfirmDelay = 1 + mDNSRandom(0x3FFFFFFF);
2919 interval += m->RandomReconfirmDelay % ((interval/3) + 1);
2920 rr->TimeRcvd = m->timenow - (mDNSs32)interval * 3;
2922 SetNextCacheCheckTimeForRecord(m, rr);
2925 RRExpireTime(rr) - m->timenow, CRDisplayString(m, rr), rr->CRActiveQuestion);
2932 mDNSlocal mDNSBool BuildQuestion(mDNS *const m, DNSMessage *query, mDNSu8 **queryptr, DNSQuestion *q,
2935 mDNSBool ucast = (q->LargeAnswers || q->RequestUnicast) && m->CanReceiveUnicastOn5353;
2949 const CacheGroup *const cg = CacheGroupForName(m, slot, q->qnamehash, &q->qname);
2959 rr->TimeRcvd + TicksTTL(rr)/2 - m->timenow > // and its half-way-to-expiry time is at least 1 second away
2989 if (ucast) q->ExpectUnicastResp = NonZeroTime(m->timenow);
2997 rr->LastUnansweredTime = m->timenow;
2998 SetNextCacheCheckTimeForRecord(m, rr);
3017 mDNSlocal void ReconfirmAntecedents(mDNS *const m, const domainname *const name, const mDNSu32 namehash, const int depth)
3028 LogInfo("ReconfirmAntecedents: Reconfirming (depth=%d) %s", depth, CRDisplayString(m, cr));
3029 mDNS_Reconfirm_internal(m, cr, kDefaultReconfirmTimeForNoAnswer);
3031 ReconfirmAntecedents(m, cr->resrec.name, cr->resrec.namehash, depth+1);
3040 mDNSlocal const CacheRecord *CacheHasAddressTypeForName(mDNS *const m, const domainname *const name, const mDNSu32 namehash)
3042 CacheGroup *const cg = CacheGroupForName(m, HashSlot(name), namehash, name);
3049 mDNSlocal const CacheRecord *FindSPSInCache1(mDNS *const m, const DNSQuestion *const q, const CacheRecord *const c0, const CacheRecord *const c1)
3052 CacheGroup *const cg = CacheGroupForName(m, HashSlot(&q->qname), q->qnamehash, &q->qname);
3059 if (!IdenticalSameNameRecord(&cr->resrec, &m->SPSRecords.RR_PTR.resrec)) // and is not our own advertised service...
3066 (void) m;
3100 mDNSexport void FindSPSInCache(mDNS *const m, const DNSQuestion *const q, const CacheRecord *sps[3])
3102 sps[0] = FindSPSInCache1(m, q, mDNSNULL, mDNSNULL);
3103 sps[1] = !sps[0] ? mDNSNULL : FindSPSInCache1(m, q, sps[0], mDNSNULL);
3104 sps[2] = !sps[1] ? mDNSNULL : FindSPSInCache1(m, q, sps[0], sps[1]);
3163 mDNSlocal void mDNSSendWakeOnResolve(mDNS *const m, DNSQuestion *q)
3205 m->mDNSStats.WakeOnResolves++;
3206 mDNSPlatformSendWakeupPacket(m, InterfaceID, EthAddr, IPAddr, InitialWakeOnResolveCount - q->WakeOnResolveCount);
3216 mDNSlocal mDNSBool AccelerateThisQuery(mDNS *const m, DNSQuestion *q)
3219 if (TimeToSendThisQuestion(q, m->timenow + q->ThisQInterval/10))
3223 if (TimeToSendThisQuestion(q, m->timenow + q->ThisQInterval/2))
3228 const CacheGroup *const cg = CacheGroupForName(m, slot, q->qnamehash, &q->qname);
3233 rr->TimeRcvd + TicksTTL(rr)/2 - m->timenow >= 0 && // and it is less than half-way to expiry
3234 rr->NextRequiredQuery - (m->timenow + q->ThisQInterval) > 0) // and we'll ask at least once again before NextRequiredQuery
3258 mDNSlocal void SendQueries(mDNS *const m)
3268 const NetworkInterfaceInfo *intf = GetFirstActiveInterface(m->HostInterfaces);
3279 if (m->timenow + TicksTTL(cr)/50 - cr->NextRequiredQuery >= 0)
3281 debugf("Sending %d%% cache expiration query for %s", 80 + 5 * cr->UnansweredQueries, CRDisplayString(m, cr));
3283 ExpireDupSuppressInfoOnInterface(q->DupSuppress, m->timenow - TicksTTL(cr)/20, cr->resrec.InterfaceID);
3292 q->LastQTime = m->timenow - q->ThisQInterval;
3294 m->mDNSStats.CacheRefreshQueries++;
3319 if (m->CurrentQuestion)
3320 LogMsg("SendQueries ERROR m->CurrentQuestion already set: %##s (%s)", m->CurrentQuestion->qname.c, DNSTypeName(m->CurrentQuestion->qtype));
3321 m->CurrentQuestion = m->Questions;
3322 while (m->CurrentQuestion && m->CurrentQuestion != m->NewQuestions)
3324 q = m->CurrentQuestion;
3325 if (q->Target.type && (q->SendQNow || TimeToSendThisQuestion(q, m->timenow)))
3327 mDNSu8 *qptr = m->omsg.data;
3328 const mDNSu8 *const limit = m->omsg.data + sizeof(m->omsg.data);
3331 if (!q->LocalSocket) q->LocalSocket = mDNSPlatformUDPSocket(m, zeroIPPort);
3334 InitializeDNSMessage(&m->omsg.h, q->TargetQID, QueryFlags);
3335 qptr = putQuestion(&m->omsg, qptr, limit, &q->qname, q->qtype, q->qclass);
3336 mDNSSendDNSMessage(m, &m->omsg, qptr, mDNSInterface_Any, q->LocalSocket, &q->Target, q->TargetPort, mDNSNULL, mDNSNULL, q->UseBackgroundTrafficClass);
3341 q->LastQTime = m->timenow;
3342 q->LastQTxTime = m->timenow;
3345 q->ExpectUnicastResp = NonZeroTime(m->timenow);
3347 else if (mDNSOpaque16IsZero(q->TargetQID) && !q->Target.type && TimeToSendThisQuestion(q, m->timenow))
3349 //LogInfo("Time to send %##s (%s) %d", q->qname.c, DNSTypeName(q->qtype), m->timenow - NextQSendTime(q));
3354 // If m->CurrentQuestion wasn't modified out from under us, advance it now
3356 // m->CurrentQuestion point to the right question
3357 if (q == m->CurrentQuestion) m->CurrentQuestion = m->CurrentQuestion->next;
3359 while (m->CurrentQuestion)
3361 LogInfo("SendQueries question loop 1: Skipping NewQuestion %##s (%s)", m->CurrentQuestion->qname.c, DNSTypeName(m->CurrentQuestion->qtype));
3362 m->CurrentQuestion = m->CurrentQuestion->next;
3364 m->CurrentQuestion = mDNSNULL;
3370 // which causes NextScheduledQuery to get (incorrectly) set to m->timenow. Setting it here is the right place, because the very
3372 m->NextScheduledQuery = m->timenow + 0x78000000;
3373 for (q = m->Questions; q && q != m->NewQuestions; q=q->next)
3376 && (q->SendQNow || (!q->Target.type && ActiveQuestion(q) && q->ThisQInterval <= maxExistingQuestionInterval && AccelerateThisQuery(m,q))))
3381 if (m->timenow - (q->LastQTime + (q->ThisQInterval/2)) >= 0)
3394 q->LastQTime = m->timenow;
3414 !(RRTypeIsAddressType(q->qtype) && CacheHasAddressTypeForName(m, &q->qname, q->qnamehash)))
3424 ReconfirmAntecedents(m, &q->qname, q->qnamehash, 0);
3433 q->LastQTime = m->timenow;
3438 ExpireDupSuppressInfo(q->DupSuppress, m->timenow - q->ThisQInterval/2);
3440 q->LastQTxTime = m->timenow;
3445 // We don't need to consider NewQuestions here because for those we'll set m->NextScheduledQuery in AnswerNewQuestion
3446 SetNextQueryTime(m,q);
3451 m->NextScheduledProbe = m->timenow + 0x78000000;
3453 if (m->CurrentRecord)
3454 LogMsg("SendQueries ERROR m->CurrentRecord already set %s", ARDisplayString(m, m->CurrentRecord));
3455 m->CurrentRecord = m->ResourceRecords;
3456 while (m->CurrentRecord)
3458 ar = m->CurrentRecord;
3459 m->CurrentRecord = ar->next;
3462 // 1. If it's not reached its probe time, just make sure we update m->NextScheduledProbe correctly
3463 if (m->timenow - (ar->LastAPTime + ar->ThisAPInterval) < 0)
3465 SetNextAnnounceProbeTime(m, ar);
3467 // 2. else, if it has reached its probe time, mark it for sending and then update m->NextScheduledProbe correctly
3477 LogSPS("SendQueries ARP Probe %d %s %s", ar->ProbeCount, InterfaceNameForID(m, ar->resrec.InterfaceID), ARDisplayString(m,ar));
3478 SendARP(m, 1, ar, &zerov4Addr, &zeroEthAddr, &ar->AddressProxy.ip.v4, &ar->WakeUp.IMAC);
3482 LogSPS("SendQueries NDP Probe %d %s %s", ar->ProbeCount, InterfaceNameForID(m, ar->resrec.InterfaceID), ARDisplayString(m,ar));
3487 SendNDP(m, NDP_Sol, 0, ar, &zerov6Addr, mDNSNULL, &ar->AddressProxy.ip.v6, &ar->WakeUp.IMAC);
3491 ar->LastAPTime = m->timenow;
3497 SetNextAnnounceProbeTime(m, ar);
3503 for (r2 = m->DuplicateRecords; r2; r2=r2->next)
3514 if (!ar->Acknowledged) AcknowledgeRecord(m, ar);
3518 // and update m->NextScheduledResponse so it will be announced
3521 if (!ar->Acknowledged) AcknowledgeRecord(m, ar); // Defensive, just in case it got missed somehow
3524 ar->LastAPTime = m->timenow - DefaultAnnounceIntervalForTypeUnique;
3525 SetNextAnnounceProbeTime(m, ar);
3529 m->CurrentRecord = m->DuplicateRecords;
3530 while (m->CurrentRecord)
3532 ar = m->CurrentRecord;
3533 m->CurrentRecord = ar->next;
3535 AcknowledgeRecord(m, ar);
3542 int OwnerRecordSpace = (m->AnnounceOwner && intf->MAC.l[0]) ? DNSOpt_Header_Space + DNSOpt_Owner_Space(&m->PrimaryMAC, &intf->MAC) : 0;
3544 mDNSu8 *queryptr = m->omsg.data;
3547 InitializeDNSMessage(&m->omsg.h, zeroID, QueryFlags);
3556 for (q = m->Questions; q && q != m->NewQuestions; q=q->next)
3563 q->qname.c, DNSTypeName(q->qtype), queryptr - m->omsg.data, queryptr + answerforecast - m->omsg.data);
3568 LogInfo("SendQueries: Not sending (%s) %##s on %s", DNSTypeName(q->qtype), q->qname.c, InterfaceNameForID(m, intf->InterfaceID));
3573 BuildQuestion(m, &m->omsg, &queryptr, q, &kalistptr, &answerforecast))
3583 m->mDNSStats.DupQuerySuppressions++;
3593 mDNSSendWakeOnResolve(m, q);
3607 for (ar = m->ResourceRecords; ar; ar=ar->next)
3610 mDNSBool ucast = (ar->ProbeCount >= DefaultProbeCountForTypeUnique-1) && m->CanReceiveUnicastOn5353;
3612 const mDNSu8 *const limit = m->omsg.data + (m->omsg.h.numQuestions ? NormalMaxDNSMessageData : AbsoluteMaxDNSMessageData);
3615 mDNSu8 *newptr = putQuestion(&m->omsg, queryptr, limit - forecast, ar->resrec.name, kDNSQType_ANY, (mDNSu16)(ar->resrec.rrclass | ucbit));
3632 mDNSu32 SecsSinceRcvd = ((mDNSu32)(m->timenow - ka->TimeRcvd)) / mDNSPlatformOneSecond;
3633 mDNSu8 *newptr = PutResourceRecordTTLWithLimit(&m->omsg, queryptr, &m->omsg.h.numAnswers, &ka->resrec, ka->resrec.rroriginalttl - SecsSinceRcvd,
3634 m->omsg.data + NormalMaxDNSMessageData - OwnerRecordSpace - TraceRecordSpace);
3638 ka->resrec.name->c, DNSTypeName(ka->resrec.rrtype), queryptr - m->omsg.data, newptr - m->omsg.data);
3647 if (m->omsg.h.numQuestions > 1)
3648 LogMsg("SendQueries: Put %d answers; No more space for known answers", m->omsg.h.numAnswers);
3649 m->omsg.h.flags.b[0] |= kDNSFlag0_TC;
3654 for (ar = m->ResourceRecords; ar; ar=ar->next)
3658 mDNSu8 *newptr = PutResourceRecord(&m->omsg, queryptr, &m->omsg.h.numAuthorities, &ar->resrec);
3661 else LogMsg("SendQueries: How did we fail to have space for the Update record %s", ARDisplayString(m,ar));
3665 for (q = m->Questions; q; q = q->next)
3669 mDNSu8 *newptr = PutResourceRecord(&m->omsg, queryptr, &m->omsg.h.numAuthorities, q->AnonInfo->nsec3RR);
3672 debugf("SendQueries: Added NSEC3 record %s on InterfaceID %p", RRDisplayString(m, q->AnonInfo->nsec3RR), intf->InterfaceID);
3677 LogMsg("SendQueries: ERROR!! Cannot add NSEC3 record %s on InterfaceID %p", RRDisplayString(m, q->AnonInfo->nsec3RR), intf->InterfaceID);
3683 if (queryptr > m->omsg.data)
3697 SetupOwnerOpt(m, intf, &opt.resrec.rdata->u.opt[0]);
3698 SetupTracerOpt(m, &opt.resrec.rdata->u.opt[1]);
3702 SetupOwnerOpt(m, intf, &opt.resrec.rdata->u.opt[0]);
3706 SetupTracerOpt(m, &opt.resrec.rdata->u.opt[0]);
3708 LogInfo("SendQueries putting %s %s: %s %s", OwnerRecordSpace ? "OWNER" : "", TraceRecordSpace ? "TRACER" : "", intf->ifname, ARDisplayString(m, &opt));
3709 queryptr = PutResourceRecordTTLWithLimit(&m->omsg, queryptr, &m->omsg.h.numAdditionals,
3710 &opt.resrec, opt.resrec.rroriginalttl, m->omsg.data + AbsoluteMaxDNSMessageData);
3714 m->omsg.h.numQuestions, m->omsg.h.numAnswers, m->omsg.h.numAuthorities, m->omsg.h.numAdditionals, ARDisplayString(m, &opt));
3716 if (queryptr > m->omsg.data + NormalMaxDNSMessageData)
3718 if (m->omsg.h.numQuestions != 1 || m->omsg.h.numAnswers != 0 || m->omsg.h.numAuthorities != 1 || m->omsg.h.numAdditionals != 1)
3720 TraceRecordSpace ? "TRACER" : "", m->omsg.data, m->omsg.data + NormalMaxDNSMessageData, queryptr, m->omsg.h.numQuestions, m->omsg.h.numAnswers,
3721 m->omsg.h.numAuthorities, m->omsg.h.numAdditionals, ARDisplayString(m, &opt));
3725 if ((m->omsg.h.flags.b[0] & kDNSFlag0_TC) && m->omsg.h.numQuestions > 1)
3726 LogMsg("SendQueries: Should not have more than one question (%d) in a truncated packet", m->omsg.h.numQuestions);
3728 m->omsg.h.numQuestions, m->omsg.h.numQuestions == 1 ? "" : "s",
3729 m->omsg.h.numAnswers, m->omsg.h.numAnswers == 1 ? "" : "s",
3730 m->omsg.h.numAuthorities, m->omsg.h.numAuthorities == 1 ? "" : "s", intf->InterfaceID);
3731 if (intf->IPv4Available) mDNSSendDNSMessage(m, &m->omsg, queryptr, intf->InterfaceID, mDNSNULL, &AllDNSLinkGroup_v4, MulticastDNSPort, mDNSNULL, mDNSNULL, useBackgroundTrafficClass);
3732 if (intf->IPv6Available) mDNSSendDNSMessage(m, &m->omsg, queryptr, intf->InterfaceID, mDNSNULL, &AllDNSLinkGroup_v6, MulticastDNSPort, mDNSNULL, mDNSNULL, useBackgroundTrafficClass);
3733 if (!m->SuppressSending) m->SuppressSending = NonZeroTime(m->timenow + (mDNSPlatformOneSecond+9)/10);
3753 for (ar = m->ResourceRecords; ar; ar=ar->next)
3758 (uint32_t)ar->SendRNow, (uint32_t)ar->resrec.InterfaceID, ARDisplayString(m, ar));
3771 if (m->timenow + TicksTTL(cr)/50 - cr->NextRequiredQuery >= 0)
3775 SetNextCacheCheckTimeForRecord(m, cr);
3783 for (q = m->Questions; q; q=q->next)
3788 for (x = m->NewQuestions; x; x=x->next) if (x == q) break; // Check if this question is a NewQuestion
3797 mDNSlocal void SendWakeup(mDNS *const m, mDNSInterfaceID InterfaceID, mDNSEthAddr *EthAddr, mDNSOpaque48 *password)
3800 mDNSu8 *ptr = m->omsg.data;
3801 NetworkInterfaceInfo *intf = FirstInterfaceForID(m, InterfaceID);
3823 mDNSPlatformSendRawPacket(m->omsg.data, ptr, InterfaceID);
3830 for (i=0; i<6; i++) m->omsg.data[i] = 0xFF;
3831 mDNSPlatformSendRawPacket(m->omsg.data, ptr, InterfaceID);
3843 mDNSlocal void ResetQuestionState(mDNS *const m, DNSQuestion *q)
3845 q->LastQTime = m->timenow;
3846 q->LastQTxTime = m->timenow;
3857 // Any code walking either list must use the m->CurrentQuestion (and possibly m->CurrentRecord) mechanism to protect against this.
3858 // In fact, to enforce this, the routine will *only* answer the question currently pointed to by m->CurrentQuestion,
3860 mDNSexport void AnswerCurrentQuestionWithResourceRecord(mDNS *const m, CacheRecord *const rr, const QC_result AddRecord)
3862 DNSQuestion *const q = m->CurrentQuestion;
3866 q->CurrentAnswers, AddRecord ? "Add" : "Rmv", rr->resrec.rroriginalttl, CRDisplayString(m, rr));
3894 "LOAddressAnswers %d", q, q->qname.c, DNSTypeName(q->qtype), ARDisplayString(m, rr),
3904 if (!q->TimeoutQuestion || rr->resrec.RecordType != kDNSRecordTypePacketNegative || (m->timenow - q->StopTime < 0))
3912 rr->LastUsed = m->timenow;
3915 if (!rr->CRActiveQuestion) m->rrcache_active++; // If not previously active, increment rrcache_active count
3917 rr->CRActiveQuestion, q, CRDisplayString(m,rr), q->CurrentAnswers);
3919 SetNextCacheCheckTimeForRecord(m, rr);
3933 ResetQuestionState(m, q);
3950 MakeNegativeCacheRecord(m, &neg, &q->qname, q->qnamehash, q->qtype, q->qclass, 1, rr->resrec.InterfaceID, q->qDNSServer);
3951 q->QuestionCallback(m, q, &neg.resrec, AddRecord);
3954 q->QuestionCallback(m, q, &rr->resrec, AddRecord);
3971 if (m->CurrentQuestion == q && (AddRecord == QC_add) && !q->ValidatingResponse && q->ValidationRequired &&
3977 VerifySignature(m, mDNSNULL, q);
3988 if (!ValidatingQuestion(q) && followcname && m->CurrentQuestion == q)
3989 AnswerQuestionByFollowingCNAME(m, q, &rr->resrec);
3992 mDNSlocal void CacheRecordDeferredAdd(mDNS *const m, CacheRecord *rr)
3995 if (m->CurrentQuestion)
3996 LogMsg("CacheRecordDeferredAdd ERROR m->CurrentQuestion already set: %##s (%s)",
3997 m->CurrentQuestion->qname.c, DNSTypeName(m->CurrentQuestion->qtype));
3998 m->CurrentQuestion = m->Questions;
3999 while (m->CurrentQuestion && m->CurrentQuestion != m->NewQuestions)
4001 DNSQuestion *q = m->CurrentQuestion;
4003 AnswerCurrentQuestionWithResourceRecord(m, rr, QC_add);
4004 if (m->CurrentQuestion == q) // If m->CurrentQuestion was not auto-advanced, do it ourselves now
4005 m->CurrentQuestion = q->next;
4007 m->CurrentQuestion = mDNSNULL;
4010 mDNSlocal mDNSs32 CheckForSoonToExpireRecords(mDNS *const m, const domainname *const name, const mDNSu32 namehash, const mDNSu32 slot, mDNSBool *purge)
4012 const mDNSs32 threshhold = m->timenow + mDNSPlatformOneSecond; // See if there are any records expiring within one second
4013 const mDNSs32 start = m->timenow - 0x10000000;
4015 CacheGroup *cg = CacheGroupForName(m, slot, namehash, name);
4051 if (m->timenow - (rr->TimeRcvd + ((mDNSs32)uTTL * mDNSPlatformOneSecond)) >= 0)
4054 CRDisplayString(m, rr), rr->resrec.rroriginalttl, uTTL, (m->timenow - rr->TimeRcvd)/mDNSPlatformOneSecond);
4073 // the end of the question list, and m->NewQuestions will be set to indicate the first new question.
4079 mDNSlocal void CacheRecordAdd(mDNS *const m, CacheRecord *rr)
4085 for (q = m->Questions; q && q != m->NewQuestions; q=q->next)
4095 if (q->LastAnswerPktNum != m->PktNum)
4097 q->LastAnswerPktNum = m->PktNum;
4099 q->ThisQInterval > InitialQuestionInterval * QuestionIntervalStep3 && m->timenow - q->LastQTxTime < mDNSPlatformOneSecond)
4103 q->LastQTime = m->timenow - InitialQuestionInterval + (mDNSs32)mDNSRandom((mDNSu32)mDNSPlatformOneSecond*4);
4105 SetNextQueryTime(m,q);
4131 if (m->CurrentQuestion)
4132 LogMsg("CacheRecordAdd ERROR m->CurrentQuestion already set: %##s (%s)", m->CurrentQuestion->qname.c, DNSTypeName(m->CurrentQuestion->qtype));
4133 m->CurrentQuestion = m->Questions;
4134 while (m->CurrentQuestion && m->CurrentQuestion != m->NewQuestions)
4136 q = m->CurrentQuestion;
4138 AnswerCurrentQuestionWithResourceRecord(m, rr, QC_add);
4139 if (m->CurrentQuestion == q) // If m->CurrentQuestion was not auto-advanced, do it ourselves now
4140 m->CurrentQuestion = q->next;
4142 m->CurrentQuestion = mDNSNULL;
4145 SetNextCacheCheckTimeForRecord(m, rr);
4150 // the end of the question list, and m->NewQuestions will be set to indicate the first new question.
4158 mDNSlocal void NoCacheAnswer(mDNS *const m, CacheRecord *rr)
4160 LogMsg("No cache space: Delivering non-cached result for %##s", m->rec.r.resrec.name->c);
4161 if (m->CurrentQuestion)
4162 LogMsg("NoCacheAnswer ERROR m->CurrentQuestion already set: %##s (%s)", m->CurrentQuestion->qname.c, DNSTypeName(m->CurrentQuestion->qtype));
4163 m->CurrentQuestion = m->Questions;
4164 // We do this for *all* questions, not stopping when we get to m->NewQuestions,
4166 while (m->CurrentQuestion)
4168 DNSQuestion *q = m->CurrentQuestion;
4170 AnswerCurrentQuestionWithResourceRecord(m, rr, QC_addnocache); // QC_addnocache means "don't expect remove events for this"
4171 if (m->CurrentQuestion == q) // If m->CurrentQuestion was not auto-advanced, do it ourselves now
4172 m->CurrentQuestion = q->next;
4174 m->CurrentQuestion = mDNSNULL;
4180 // the end of the question list, and m->NewQuestions will be set to indicate the first new question.
4186 mDNSlocal void CacheRecordRmv(mDNS *const m, CacheRecord *rr)
4188 if (m->CurrentQuestion)
4189 LogMsg("CacheRecordRmv ERROR m->CurrentQuestion already set: %##s (%s)",
4190 m->CurrentQuestion->qname.c, DNSTypeName(m->CurrentQuestion->qtype));
4191 m->CurrentQuestion = m->Questions;
4195 while (m->CurrentQuestion && m->CurrentQuestion != m->NewQuestions)
4197 DNSQuestion *q = m->CurrentQuestion;
4204 verbosedebugf("CacheRecordRmv %p %s", rr, CRDisplayString(m, rr));
4226 q->LastQTime = m->timenow - q->ThisQInterval;
4227 SetNextQueryTime(m,q);
4237 ReconfirmAntecedents(m, &q->qname, q->qnamehash, 0);
4239 AnswerCurrentQuestionWithResourceRecord(m, rr, QC_rmv);
4242 if (m->CurrentQuestion == q) // If m->CurrentQuestion was not auto-advanced, do it ourselves now
4243 m->CurrentQuestion = q->next;
4245 m->CurrentQuestion = mDNSNULL;
4248 mDNSlocal void ReleaseCacheEntity(mDNS *const m, CacheEntity *e)
4254 e->next = m->rrcache_free;
4255 m->rrcache_free = e;
4256 m->rrcache_totalused--;
4259 mDNSlocal void ReleaseCacheGroup(mDNS *const m, CacheGroup **cp)
4270 ReleaseCacheEntity(m, e);
4273 mDNSlocal void ReleaseAdditionalCacheRecords(mDNS *const m, CacheRecord **rp)
4295 m->rrcache_totalused_unicast -= rr->resrec.rdlength;
4297 BumpDNSSECStats(m, kStatsActionDecrement, kStatsTypeMemoryUsage, rr->resrec.rdlength);
4299 ReleaseCacheEntity(m, (CacheEntity *)rr);
4303 mDNSexport void ReleaseCacheRecord(mDNS *const m, CacheRecord *r)
4308 //LogMsg("ReleaseCacheRecord: Releasing %s", CRDisplayString(m, r));
4312 cg = CacheGroupForRecord(m, slot, &r->resrec);
4338 m->rrcache_totalused_unicast -= r->resrec.rdlength;
4340 BumpDNSSECStats(m, kStatsActionDecrement, kStatsTypeMemoryUsage, r->resrec.rdlength);
4343 ReleaseAdditionalCacheRecords(m, &r->nsec);
4344 ReleaseAdditionalCacheRecords(m, &r->soa);
4346 ReleaseCacheEntity(m, (CacheEntity *)r);
4352 mDNSlocal void CheckCacheExpiration(mDNS *const m, const mDNSu32 slot, CacheGroup *const cg)
4356 if (m->lock_rrcache) { LogMsg("CheckCacheExpiration ERROR! Cache already locked!"); return; }
4357 m->lock_rrcache = 1;
4363 if (m->timenow - event >= 0) // If expired, delete it
4368 m->timenow - rr->TimeRcvd, rr->resrec.rroriginalttl, rr->CRActiveQuestion, CRDisplayString(m, rr));
4382 q->LastQTime = m->timenow - q->ThisQInterval;
4383 SetNextQueryTime(m, q);
4385 CacheRecordRmv(m, rr);
4386 m->rrcache_active--;
4388 ReleaseCacheRecord(m, rr);
4393 if (rr->DelayDelivery && rr->DelayDelivery - m->timenow > 0)
4397 if (rr->DelayDelivery) CacheRecordDeferredAdd(m, rr);
4400 if (m->timenow - rr->NextRequiredQuery < 0) // If not yet time for next query
4406 m->NextScheduledQuery = m->timenow;
4408 // which will correctly update m->NextCacheCheck for us.
4409 event = m->timenow + 0x3FFFFFFF;
4414 (event - m->timenow) / mDNSPlatformOneSecond, CacheCheckGracePeriod(rr), CRDisplayString(m, rr));
4415 if (m->rrcache_nextcheck[slot] - event > 0)
4416 m->rrcache_nextcheck[slot] = event;
4422 m->lock_rrcache = 0;
4425 // "LORecord" includes both LocalOnly and P2P record. This function assumes m->CurrentQuestion is pointing to "q".
4432 mDNSlocal mDNSBool AnswerQuestionWithLORecord(mDNS *const m, DNSQuestion *q, mDNSBool checkOnly)
4438 if (m->CurrentRecord)
4439 LogMsg("AnswerQuestionWithLORecord ERROR m->CurrentRecord already set %s", ARDisplayString(m, m->CurrentRecord));
4442 ag = AuthGroupForName(&m->rrauth, slot, q->qnamehash, &q->qname);
4445 m->CurrentRecord = ag->members;
4446 while (m->CurrentRecord && m->CurrentRecord != ag->NewLocalOnlyRecords)
4448 AuthRecord *rr = m->CurrentRecord;
4449 m->CurrentRecord = rr->next;
4464 ARDisplayString(m, rr));
4465 m->CurrentRecord = mDNSNULL;
4468 AnswerLocalQuestionWithLocalAuthRecord(m, rr, QC_add);
4469 if (m->CurrentQuestion != q)
4474 m->CurrentRecord = mDNSNULL;
4476 if (m->CurrentQuestion != q)
4517 mDNSlocal void AnswerSuppressedQuestion(mDNS *const m, DNSQuestion *q)
4526 GenerateNegativeResponse(m, QC_suppressed);
4532 mDNSlocal void AnswerNewQuestion(mDNS *const m)
4535 DNSQuestion *const q = m->NewQuestions; // Grab the question we're going to answer
4537 CacheGroup *const cg = CacheGroupForName(m, slot, q->qnamehash, &q->qname);
4542 if (cg) CheckCacheExpiration(m, slot, cg);
4543 if (m->NewQuestions != q) { LogInfo("AnswerNewQuestion: Question deleted while doing CheckCacheExpiration"); goto exit; }
4544 m->NewQuestions = q->next;
4550 // whether our current m->NewQuestions question got deleted by one of these callbacks is to store the
4551 // value of m->NewQuestions in 'q' before calling CheckCacheExpiration(), and then verify afterwards
4552 // that they're still the same. If m->NewQuestions has changed (because mDNS_StopQuery_internal
4557 // We can't use the usual m->CurrentQuestion mechanism for this because CacheRecordDeferredAdd() and
4559 // Fortunately mDNS_StopQuery_internal auto-advances both m->CurrentQuestion *AND* m->NewQuestions when
4562 if (m->lock_rrcache) LogMsg("AnswerNewQuestion ERROR! Cache already locked!");
4565 // If the client's question callback deletes the question, then m->CurrentQuestion will
4567 m->lock_rrcache = 1;
4568 if (m->CurrentQuestion)
4569 LogMsg("AnswerNewQuestion ERROR m->CurrentQuestion already set: %##s (%s)",
4570 m->CurrentQuestion->qname.c, DNSTypeName(m->CurrentQuestion->qtype));
4571 m->CurrentQuestion = q; // Indicate which question we're answering, so we'll know if it gets deleted
4576 MakeNegativeCacheRecord(m, &m->rec.r, &q->qname, q->qnamehash, q->qtype, q->qclass, 60, mDNSInterface_Any, q->qDNSServer);
4578 AnswerCurrentQuestionWithResourceRecord(m, &m->rec.r, QC_addnocache);
4580 if (m->CurrentQuestion == q) q->NoAnswer = NoAnswer_Fail; // Restore NoAnswer state
4581 m->rec.r.resrec.RecordType = 0; // Clear RecordType to show we're not still using it
4584 if (m->CurrentQuestion != q)
4592 if (AnswerQuestionWithLORecord(m, q, mDNSfalse))
4603 AnswerSuppressedQuestion(m, q);
4612 mDNSu32 SecsSinceRcvd = ((mDNSu32)(m->timenow - rr->TimeRcvd)) / mDNSPlatformOneSecond;
4616 rr->resrec.rroriginalttl, SecsSinceRcvd, CRDisplayString(m, rr), m->timenow, rr->TimeRcvd);
4628 AnswerCurrentQuestionWithResourceRecord(m, rr, QC_add);
4629 if (m->CurrentQuestion != q) break; // If callback deleted q, then we're finished here
4636 if (m->CurrentQuestion != q) { debugf("AnswerNewQuestion: Question deleted while giving cache answers"); goto exit; }
4645 GenerateNegativeResponse(m, QC_forceresponse);
4648 if (m->CurrentQuestion != q) { debugf("AnswerNewQuestion: Question deleted while giving negative answer"); goto exit; }
4656 q->LastQTime = m->timenow - q->ThisQInterval;
4660 if (!m->RandomQueryDelay)
4661 m->RandomQueryDelay = (mDNSPlatformOneSecond + mDNSRandom(mDNSPlatformOneSecond*5) - 1) / 50 + 1;
4662 q->LastQTime += m->RandomQueryDelay;
4666 // IN ALL CASES make sure that m->NextScheduledQuery is set appropriately.
4667 // In cases where m->NewQuestions->DelayAnswering is set, we may have delayed generating our
4669 // m->NextScheduledQuery may now be set to 'never', and in that case -- even though we're *not* doing
4670 // ShouldQueryImmediately -- we still need to make sure we set m->NextScheduledQuery correctly.
4671 SetNextQueryTime(m,q);
4674 m->CurrentQuestion = mDNSNULL;
4675 m->lock_rrcache = 0;
4680 mDNSlocal void AnswerNewLocalOnlyQuestion(mDNS *const m)
4684 DNSQuestion *q = m->NewLocalOnlyQuestions; // Grab the question we're going to answer
4685 m->NewLocalOnlyQuestions = q->next; // Advance NewLocalOnlyQuestions to the next (if any)
4689 if (m->CurrentQuestion)
4690 LogMsg("AnswerNewLocalOnlyQuestion ERROR m->CurrentQuestion already set: %##s (%s)",
4691 m->CurrentQuestion->qname.c, DNSTypeName(m->CurrentQuestion->qtype));
4692 m->CurrentQuestion = q; // Indicate which question we're answering, so we'll know if it gets deleted
4694 if (m->CurrentRecord)
4695 LogMsg("AnswerNewLocalOnlyQuestion ERROR m->CurrentRecord already set %s", ARDisplayString(m, m->CurrentRecord));
4701 ag = AuthGroupForName(&m->rrauth, slot, q->qnamehash, &q->qname);
4704 m->CurrentRecord = ag->members;
4705 while (m->CurrentRecord && m->CurrentRecord != ag->NewLocalOnlyRecords)
4707 AuthRecord *rr = m->CurrentRecord;
4708 m->CurrentRecord = rr->next;
4711 AnswerLocalQuestionWithLocalAuthRecord(m, rr, QC_add);
4712 if (m->CurrentQuestion != q) break; // If callback deleted q, then we're finished here
4717 if (m->CurrentQuestion == q)
4719 m->CurrentRecord = m->ResourceRecords;
4721 while (m->CurrentRecord && m->CurrentRecord != m->NewLocalRecords)
4723 AuthRecord *rr = m->CurrentRecord;
4724 m->CurrentRecord = rr->next;
4727 AnswerLocalQuestionWithLocalAuthRecord(m, rr, QC_add);
4728 if (m->CurrentQuestion != q) break; // If callback deleted q, then we're finished here
4733 m->CurrentQuestion = mDNSNULL;
4734 m->CurrentRecord = mDNSNULL;
4737 mDNSlocal CacheEntity *GetCacheEntity(mDNS *const m, const CacheGroup *const PreserveCG)
4741 if (m->lock_rrcache) { LogMsg("GetFreeCacheRR ERROR! Cache already locked!"); return(mDNSNULL); }
4742 m->lock_rrcache = 1;
4745 if (!m->rrcache_free && m->MainCallback)
4747 if (m->rrcache_totalused != m->rrcache_size)
4748 LogMsg("GetFreeCacheRR: count mismatch: m->rrcache_totalused %lu != m->rrcache_size %lu",
4749 m->rrcache_totalused, m->rrcache_size);
4756 if (m->rrcache_size > 5000 && m->rrcache_size / 32 > m->rrcache_active)
4757 LogInfo("Possible denial-of-service attack in progress: m->rrcache_size %lu; m->rrcache_active %lu",
4758 m->rrcache_size, m->rrcache_active);
4762 m->MainCallback(m, mStatus_GrowCache);
4769 if (!m->rrcache_free)
4771 mDNSu32 oldtotalused = m->rrcache_totalused;
4775 CacheGroup **cp = &m->rrcache_hash[slot];
4789 ReleaseCacheRecord(m, rr);
4796 else ReleaseCacheGroup(m, cp);
4800 oldtotalused - m->rrcache_totalused, oldtotalused, m->rrcache_totalused);
4803 if (m->rrcache_free) // If there are records in the free list, take one
4805 e = m->rrcache_free;
4806 m->rrcache_free = e->next;
4807 if (++m->rrcache_totalused >= m->rrcache_report)
4809 LogInfo("RR Cache now using %ld objects", m->rrcache_totalused);
4810 if (m->rrcache_report < 100) m->rrcache_report += 10;
4811 else if (m->rrcache_report < 1000) m->rrcache_report += 100;
4812 else m->rrcache_report += 1000;
4817 m->lock_rrcache = 0;
4822 mDNSlocal CacheRecord *GetCacheRecord(mDNS *const m, CacheGroup *cg, mDNSu16 RDLength)
4824 CacheRecord *r = (CacheRecord *)GetCacheEntity(m, cg);
4832 else { ReleaseCacheEntity(m, (CacheEntity*)r); r = mDNSNULL; }
4838 mDNSlocal CacheGroup *GetCacheGroup(mDNS *const m, const mDNSu32 slot, const ResourceRecord *const rr)
4841 CacheGroup *cg = (CacheGroup*)GetCacheEntity(m, mDNSNULL);
4843 cg->next = m->rrcache_hash[slot];
4854 ReleaseCacheEntity(m, (CacheEntity*)cg);
4859 if (CacheGroupForRecord(m, slot, rr)) LogMsg("GetCacheGroup: Already have CacheGroup for %##s", rr->name->c);
4860 m->rrcache_hash[slot] = cg;
4861 if (CacheGroupForRecord(m, slot, rr) != cg) LogMsg("GetCacheGroup: Not finding CacheGroup for %##s", rr->name->c);
4866 mDNSexport void mDNS_PurgeCacheResourceRecord(mDNS *const m, CacheRecord *rr)
4868 mDNS_CheckLock(m);
4875 rr->TimeRcvd = m->timenow - mDNSPlatformOneSecond * 60;
4878 SetNextCacheCheckTimeForRecord(m, rr);
4881 mDNSexport mDNSs32 mDNS_TimeNow(const mDNS *const m)
4884 mDNSPlatformLock(m);
4885 if (m->mDNS_busy)
4887 LogMsg("mDNS_TimeNow called while holding mDNS lock. This is incorrect. Code protected by lock should just use m->timenow.");
4888 if (!m->timenow) LogMsg("mDNS_TimeNow: m->mDNS_busy is %ld but m->timenow not set", m->mDNS_busy);
4891 if (m->timenow) time = m->timenow;
4892 else time = mDNS_TimeNow_NoLock(m);
4893 mDNSPlatformUnlock(m);
4899 // (GetNextScheduledEvent() returns "now" when m->SPSProxyListChanged is set)
4901 if (m->SPSProxyListChanged && m->SPSProxyListChanged != (X)) mDNSPlatformUpdateProxyList(m, m->SPSProxyListChanged); \
4902 m->SPSProxyListChanged = (X); } while(0)
4905 mDNSlocal void CheckProxyRecords(mDNS *const m, AuthRecord *list)
4907 m->CurrentRecord = list;
4908 while (m->CurrentRecord)
4910 AuthRecord *rr = m->CurrentRecord;
4913 // If m->SPSSocket is NULL that means we're not acting as a sleep proxy any more,
4915 if (m->SPSSocket && m->timenow - rr->TimeExpire < 0) // If proxy record not expired yet, update m->NextScheduledSPS
4917 if (m->NextScheduledSPS - rr->TimeExpire > 0)
4918 m->NextScheduledSPS = rr->TimeExpire;
4923 m->ProxyRecords, &rr->WakeUp.HMAC, &rr->WakeUp.IMAC, rr->WakeUp.seq, ARDisplayString(m, rr));
4925 mDNS_Deregister_internal(m, rr, mDNS_Dereg_normal);
4929 // Mustn't advance m->CurrentRecord until *after* mDNS_Deregister_internal, because
4931 if (m->CurrentRecord == rr) // If m->CurrentRecord was not advanced for us, do it now
4932 m->CurrentRecord = rr->next;
4936 mDNSlocal void CheckRmvEventsForLocalRecords(mDNS *const m)
4938 while (m->CurrentRecord)
4940 AuthRecord *rr = m->CurrentRecord;
4943 debugf("CheckRmvEventsForLocalRecords: Generating local RMV events for %s", ARDisplayString(m, rr));
4945 AnswerAllLocalQuestionsWithLocalAuthRecord(m, rr, QC_rmv);
4946 if (m->CurrentRecord == rr) // If rr still exists in list, restore its state now
4952 if (RRLocalOnly(rr)) CompleteDeregistration(m, rr);
4955 if (m->CurrentRecord == rr) // If m->CurrentRecord was not auto-advanced, do it ourselves now
4956 m->CurrentRecord = rr->next;
4960 mDNSlocal void TimeoutQuestions(mDNS *const m)
4962 m->NextScheduledStopTime = m->timenow + 0x3FFFFFFF;
4963 if (m->CurrentQuestion)
4964 LogMsg("TimeoutQuestions ERROR m->CurrentQuestion already set: %##s (%s)", m->CurrentQuestion->qname.c,
4965 DNSTypeName(m->CurrentQuestion->qtype));
4966 m->CurrentQuestion = m->Questions;
4967 while (m->CurrentQuestion)
4969 DNSQuestion *const q = m->CurrentQuestion;
4975 if (m->timenow - q->StopTime >= 0)
4977 LogInfo("TimeoutQuestions: question %p %##s timed out, time %d", q, q->qname.c, m->timenow - q->StopTime);
4978 GenerateNegativeResponse(m, QC_forceresponse);
4979 if (m->CurrentQuestion == q) q->StopTime = 0;
4983 if (m->NextScheduledStopTime - q->StopTime > 0)
4984 m->NextScheduledStopTime = q->StopTime;
4987 // If m->CurrentQuestion wasn't modified out from under us, advance it now
4989 // depends on having m->CurrentQuestion point to the right question
4990 if (m->CurrentQuestion == q)
4991 m->CurrentQuestion = q->next;
4993 m->CurrentQuestion = mDNSNULL;
4996 mDNSlocal void mDNSCoreFreeProxyRR(mDNS *const m)
4998 AuthRecord *rrPtr = m->SPSRRSet, *rrNext = mDNSNULL;
5006 m->SPSRRSet = mDNSNULL;
5009 mDNSexport mDNSs32 mDNS_Execute(mDNS *const m)
5011 mDNS_Lock(m); // Must grab lock before trying to read m->timenow
5014 mDNSLogStatistics(m);
5017 if (m->timenow - m->NextScheduledEvent >= 0)
5026 if (m->CurrentQuestion)
5027 LogMsg("mDNS_Execute: ERROR m->CurrentQuestion already set: %##s (%s)",
5028 m->CurrentQuestion->qname.c, DNSTypeName(m->CurrentQuestion->qtype));
5030 if (m->CurrentRecord)
5031 LogMsg("mDNS_Execute: ERROR m->CurrentRecord already set: %s", ARDisplayString(m, m->CurrentRecord));
5034 if (m->SuppressProbes && m->timenow - m->SuppressProbes >= 0) m->SuppressProbes = 0;
5037 if (m->NumFailedProbes && m->timenow - m->ProbeFailTime >= mDNSPlatformOneSecond * 10) m->NumFailedProbes = 0;
5040 if (m->rrcache_size && m->timenow - m->NextCacheCheck >= 0)
5043 m->NextCacheCheck = m->timenow + 0x3FFFFFFF;
5046 if (m->timenow - m->rrcache_nextcheck[slot] >= 0)
5048 CacheGroup **cp = &m->rrcache_hash[slot];
5049 m->rrcache_nextcheck[slot] = m->timenow + 0x3FFFFFFF;
5052 debugf("m->NextCacheCheck %4d Slot %3d %##s", numchecked, slot, *cp ? (*cp)->name : (domainname*)"\x04NULL");
5054 CheckCacheExpiration(m, slot, *cp);
5056 else ReleaseCacheGroup(m, cp);
5061 if (m->NextCacheCheck - m->rrcache_nextcheck[slot] > 0)
5062 m->NextCacheCheck = m->rrcache_nextcheck[slot];
5064 debugf("m->NextCacheCheck %4d checked, next in %d", numchecked, m->NextCacheCheck - m->timenow);
5067 if (m->timenow - m->NextScheduledSPS >= 0)
5069 m->NextScheduledSPS = m->timenow + 0x3FFFFFFF;
5070 CheckProxyRecords(m, m->DuplicateRecords); // Clear m->DuplicateRecords first, then m->ResourceRecords
5071 CheckProxyRecords(m, m->ResourceRecords);
5078 if (m->timenow - m->NextScheduledKA >= 0)
5080 m->NextScheduledKA = m->timenow + 0x3FFFFFFF;
5081 mDNS_SendKeepalives(m);
5085 if (m->AnnounceOwner && m->timenow - m->AnnounceOwner >= 0)
5087 m->AnnounceOwner = 0;
5090 if (m->DelaySleep && m->timenow - m->DelaySleep >= 0)
5092 m->DelaySleep = 0;
5093 if (m->SleepState == SleepState_Transferring)
5096 BeginSleepProcessing(m);
5101 for (i=0; m->NewQuestions && i<1000; i++)
5103 if (m->NewQuestions->DelayAnswering && m->timenow - m->NewQuestions->DelayAnswering < 0) break;
5104 AnswerNewQuestion(m);
5109 // we begin generating *any* new ADD events in the m->NewLocalOnlyQuestions and m->NewLocalRecords loops below.
5110 for (i=0; i<1000 && m->LocalRemoveEvents; i++)
5112 m->LocalRemoveEvents = mDNSfalse;
5113 m->CurrentRecord = m->ResourceRecords;
5114 CheckRmvEventsForLocalRecords(m);
5117 for (ag = m->rrauth.rrauth_hash[slot]; ag; ag = ag->next)
5119 m->CurrentRecord = ag->members;
5120 if (m->CurrentRecord) CheckRmvEventsForLocalRecords(m);
5124 if (i >= 1000) LogMsg("mDNS_Execute: m->LocalRemoveEvents exceeded loop limit");
5126 for (i=0; m->NewLocalOnlyQuestions && i<1000; i++) AnswerNewLocalOnlyQuestion(m);
5130 for (i=0; i<1000 && m->NewLocalRecords && m->NewLocalRecords != head; i++)
5132 AuthRecord *rr = m->NewLocalRecords;
5133 m->NewLocalRecords = m->NewLocalRecords->next;
5136 debugf("mDNS_Execute: Delivering Add event with LocalAuthRecord %s", ARDisplayString(m, rr));
5137 AnswerAllLocalQuestionsWithLocalAuthRecord(m, rr, QC_add);
5144 debugf("mDNS_Execute: Just one LocalAuthRecord %s, breaking out of the loop early", ARDisplayString(m, rr));
5145 if (head != mDNSNULL || m->NewLocalRecords != mDNSNULL)
5146 LogMsg("mDNS_Execute: ERROR!!: head %p, NewLocalRecords %p", head, m->NewLocalRecords);
5152 AuthRecord **p = &m->ResourceRecords; // Find this record in our list of active records
5153 debugf("mDNS_Execute: Skipping LocalAuthRecord %s", ARDisplayString(m, rr));
5158 else { LogMsg("mDNS_Execute: ERROR!! Cannot find record %s in ResourceRecords list", ARDisplayString(m, rr)); break; }
5173 m->NewLocalRecords = head;
5174 debugf("mDNS_Execute: Setting NewLocalRecords to %s", (head ? ARDisplayString(m, head) : "NULL"));
5176 if (i >= 1000) LogMsg("mDNS_Execute: m->NewLocalRecords exceeded loop limit");
5180 if (m->NewLocalOnlyRecords)
5182 m->NewLocalOnlyRecords = mDNSfalse;
5184 for (ag = m->rrauth.rrauth_hash[slot]; ag; ag = ag->next)
5193 debugf("mDNS_Execute: Delivering Add event with LocalAuthRecord %s", ARDisplayString(m, rr));
5194 AnswerAllLocalQuestionsWithLocalAuthRecord(m, rr, QC_add);
5196 else LogMsg("mDNS_Execute: LocalOnlyRecord %s not ready", ARDisplayString(m, rr));
5204 if (m->mDNSPlatformStatus != mStatus_NoError || (m->SleepState == SleepState_Sleeping))
5205 DiscardDeregistrations(m);
5206 if (m->mDNSPlatformStatus == mStatus_NoError && (m->SuppressSending == 0 || m->timenow - m->SuppressSending >= 0))
5214 m->SuppressSending = 0;
5217 if (m->timenow - m->NextScheduledQuery >= 0 || m->timenow - m->NextScheduledProbe >= 0) SendQueries(m);
5218 if (m->timenow - m->NextScheduledQuery >= 0)
5222 m->timenow, m->NextScheduledQuery, m->timenow - m->NextScheduledQuery);
5223 m->NextScheduledQuery = m->timenow + mDNSPlatformOneSecond;
5224 for (q = m->Questions; q && q != m->NewQuestions; q=q->next)
5225 if (ActiveQuestion(q) && m->timenow - NextQSendTime(q) >= 0)
5228 if (m->timenow - m->NextScheduledProbe >= 0)
5231 m->timenow, m->NextScheduledProbe, m->timenow - m->NextScheduledProbe);
5232 m->NextScheduledProbe = m->timenow + mDNSPlatformOneSecond;
5236 if (m->timenow - m->NextScheduledResponse >= 0) SendResponses(m);
5237 if (m->timenow - m->NextScheduledResponse >= 0)
5240 m->NextScheduledResponse = m->timenow + mDNSPlatformOneSecond;
5245 m->RandomQueryDelay = 0;
5246 m->RandomReconfirmDelay = 0;
5248 if (m->NextScheduledStopTime && m->timenow - m->NextScheduledStopTime >= 0) TimeoutQuestions(m);
5250 if (m->NextSRVUpdate && m->timenow - m->NextSRVUpdate >= 0) UpdateAllSRVRecords(m);
5251 if (m->timenow - m->NextScheduledNATOp >= 0) CheckNATMappings(m);
5252 if (m->timenow - m->NextuDNSEvent >= 0) uDNS_Tasks(m);
5272 // set the timer according to the m->NextScheduledEvent value, and then when the timer fires, the timer
5276 mDNS_Unlock(m); // Calling mDNS_Unlock is what gives m->NextScheduledEvent its new value
5277 return(m->NextScheduledEvent);
5281 mDNSlocal void SuspendLLQs(mDNS *m)
5284 for (q = m->Questions; q; q = q->next)
5286 { q->ReqLease = 0; sendLLQRefresh(m, q); }
5290 mDNSlocal mDNSBool QuestionHasLocalAnswers(mDNS *const m, DNSQuestion *q)
5297 ag = AuthGroupForName(&m->rrauth, slot, q->qnamehash, &q->qname);
5304 LogInfo("QuestionHasLocalAnswers: Question %p %##s (%s) has local answer %s", q, q->qname.c, DNSTypeName(q->qtype), ARDisplayString(m, rr));
5317 mDNSlocal void ActivateUnicastQuery(mDNS *const m, DNSQuestion *const question, mDNSBool ScheduleImmediately)
5333 AddNewClientTunnel(m, question);
5343 if (question->nta) { CancelGetZoneData(m, question->nta); question->nta = mDNSNULL; }
5352 if (ScheduleImmediately && !QuestionHasLocalAnswers(m, question))
5355 question->LastQTime = m->timenow - question->ThisQInterval;
5356 SetNextQueryTime(m, question);
5362 mDNSexport void mDNSCoreRestartAddressQueries(mDNS *const m, mDNSBool SearchDomainsChanged, FlushCache flushCacheRecords,
5368 mDNS_CheckLock(m);
5371 if (flushCacheRecords) flushCacheRecords(m);
5383 if (m->RestartQuestion)
5384 LogMsg("mDNSCoreRestartAddressQueries: ERROR!! m->RestartQuestion already set: %##s (%s)",
5385 m->RestartQuestion->qname.c, DNSTypeName(m->RestartQuestion->qtype));
5387 m->RestartQuestion = m->Questions;
5388 while (m->RestartQuestion)
5390 q = m->RestartQuestion;
5391 m->RestartQuestion = q->next;
5432 if (!CacheRecordRmvEventsForQuestion(m, q)) { LogInfo("mDNSCoreRestartAddressQueries: Question deleted while delivering Cache Record RMV events"); continue; }
5435 if (!LocalRecordRmvEventsForQuestion(m, q)) { LogInfo("mDNSCoreRestartAddressQueries: Question deleted while delivering Local Record RMV events"); continue; }
5439 mDNS_StopQuery_internal(m, q);
5457 if (BeforeStartCallback) BeforeStartCallback(m, context);
5466 mDNS_StartQuery_internal(m, q);
5470 mDNSexport void mDNSCoreRestartQueries(mDNS *const m)
5476 if (m->CurrentQuestion)
5477 LogMsg("mDNSCoreRestartQueries: ERROR m->CurrentQuestion already set: %##s (%s)",
5478 m->CurrentQuestion->qname.c, DNSTypeName(m->CurrentQuestion->qtype));
5479 m->CurrentQuestion = m->Questions;
5480 while (m->CurrentQuestion)
5482 q = m->CurrentQuestion;
5483 m->CurrentQuestion = m->CurrentQuestion->next;
5484 if (!mDNSOpaque16IsZero(q->TargetQID) && ActiveQuestion(q)) ActivateUnicastQuery(m, q, mDNStrue);
5489 for (q = m->Questions; q; q=q->next) // Scan our list of questions
5490 mDNSCoreRestartQuestion(m, q);
5494 mDNSexport void mDNSCoreRestartQuestion(mDNS *const m, DNSQuestion *q)
5504 q->LastQTime = m->timenow - q->ThisQInterval;
5506 ExpireDupSuppressInfo(q->DupSuppress, m->timenow);
5507 m->NextScheduledQuery = m->timenow;
5512 mDNSexport void mDNSCoreRestartRegistration(mDNS *const m, AuthRecord *rr, int announceCount)
5530 InitializeLastAPTime(m, rr);
5540 mDNSexport void mDNS_UpdateAllowSleep(mDNS *const m)
5548 if (m->SystemSleepOnlyIfWakeOnLAN)
5551 if (m->ProxyRecords)
5554 mDNS_snprintf(reason, sizeof(reason), "sleep proxy for %d records", m->ProxyRecords);
5555 LogInfo("mDNS_UpdateAllowSleep: Sleep disabled because we are proxying %d records", m->ProxyRecords);
5558 if (allowSleep && mDNSCoreHaveAdvertisedMulticastServices(m))
5562 for (intf = GetFirstActiveInterface(m->HostInterfaces); intf; intf = GetFirstActiveInterface(intf->next))
5576 const CacheRecord *cr = FindSPSInCache1(m, &intf->NetWakeBrowse, mDNSNULL, mDNSNULL);
5584 else if (m->SPSType != 0)
5586 mDNSu32 mymetric = LocalSPSMetric(m);
5602 mDNSPlatformSetAllowSleep(m, allowSleep, reason);
5604 (void) m;
5608 mDNSlocal mDNSBool mDNSUpdateOkToSend(mDNS *const m, AuthRecord *rr, NetworkInterfaceInfo *const intf, mDNSu32 scopeid)
5614 m->timenow - (rr->LastAPTime + rr->ThisAPInterval) < 0)
5634 mDNSexport void UpdateRMACCallback(mDNS *const m, void *context)
5637 m->CurrentRecord = m->ResourceRecords;
5645 while (m->CurrentRecord)
5647 AuthRecord *rr = m->CurrentRecord;
5652 getKeepaliveRaddr(m, rr, &raddr);
5655 UpdateKeepaliveRData(m, rr, mDNSNULL, mDNStrue, (char *)(addrmap->ethaddr));
5658 m->CurrentRecord = rr->next;
5667 mDNSexport mStatus UpdateKeepaliveRData(mDNS *const m, AuthRecord *rr, NetworkInterfaceInfo *const intf, mDNSBool updateMac, char *ethAddr)
5687 LogMsg("UpdateKeepaliveRData: not a valid record %s for keepalive %#a:%d %#a:%d", ARDisplayString(m, rr), &laddr, lport.NotAnInteger, &raddr, rport.NotAnInteger);
5694 newrdlength = mDNS_snprintf((char *)&txt.c[1], sizeof(txt.c) - 1, "t=%d i=%d c=%d h=%#a d=%#a l=%u r=%u m=%s", timeout, kKeepaliveRetryInterval, kKeepaliveRetryCount, &laddr, &raddr, mDNSVal16(lport), mDNSVal16(rport), ethAddr);
5696 newrdlength = mDNS_snprintf((char *)&txt.c[1], sizeof(txt.c) - 1, "t=%d i=%d c=%d H=%#a D=%#a l=%u r=%u m=%s", timeout, kKeepaliveRetryInterval, kKeepaliveRetryCount, &laddr, &raddr, mDNSVal16(lport), mDNSVal16(rport), ethAddr);
5705 ret = mDNSPlatformRetrieveTCPInfo(m, &laddr, &lport, &raddr, &rport, &mti);
5718 newrdlength = mDNS_snprintf((char *)&txt.c[1], sizeof(txt.c) - 1, "t=%d i=%d c=%d h=%#a d=%#a l=%u r=%u m=%.6a s=%u a=%u w=%u", timeout, kKeepaliveRetryInterval, kKeepaliveRetryCount, &laddr, &raddr, mDNSVal16(lport), mDNSVal16(rport), ð, mti.seq, mti.ack, mti.window);
5720 newrdlength = mDNS_snprintf((char *)&txt.c[1], sizeof(txt.c) - 1, "t=%d i=%d c=%d H=%#a D=%#a l=%u r=%u m=%.6a s=%u a=%u w=%u", timeout, kKeepaliveRetryInterval, kKeepaliveRetryCount, &laddr, &raddr, mDNSVal16(lport), mDNSVal16(rport), ð, mti.seq, mti.ack, mti.window);
5726 LogMsg("UpdateKeepaliveRData: could not allocate memory %s", ARDisplayString(m, rr));
5749 LogSPS("UpdateKeepaliveRData: Freed allocated memory for keep alive packet: %s ", ARDisplayString(m, rr));
5753 LogSPS("UpdateKeepaliveRData: successfully updated the record %s", ARDisplayString(m, rr));
5757 mDNSlocal void SendSPSRegistrationForOwner(mDNS *const m, NetworkInterfaceInfo *const intf, const mDNSOpaque16 id, const OwnerOptData *const owner)
5759 const int optspace = DNSOpt_Header_Space + DNSOpt_LeaseData_Space + DNSOpt_Owner_Space(&m->PrimaryMAC, &intf->MAC);
5765 scopeid = mDNSPlatformInterfaceIndexfromInterfaceID(m, intf->InterfaceID, mDNStrue);
5768 intf->NextSPSAttemptTime = m->timenow + mDNSPlatformOneSecond;
5769 if (m->NextScheduledSPRetry - intf->NextSPSAttemptTime > 0)
5770 m->NextScheduledSPRetry = intf->NextSPSAttemptTime;
5786 msgid = mDNS_NewMessageID(m);
5787 for (rr = m->ResourceRecords; rr; rr=rr->next)
5813 mDNSu8 *p = m->omsg.data;
5818 InitializeDNSMessage(&m->omsg.h, msgid, UpdateReqFlags);
5820 for (rr = m->ResourceRecords; rr; rr=rr->next)
5821 if (rr->SendRNow || mDNSUpdateOkToSend(m, rr, intf, scopeid))
5826 const mDNSu8 *const limit = m->omsg.data + (m->omsg.h.mDNS_numUpdates ? NormalMaxDNSMessageData : AbsoluteMaxDNSMessageData) - optspace;
5829 if (mDNS_KeepaliveRecord(&rr->resrec) && (UpdateKeepaliveRData(m, rr, intf, mDNSfalse, mDNSNULL) != mStatus_NoError))
5841 newptr = PutResourceRecordTTLWithLimit(&m->omsg, p, &m->omsg.h.mDNS_numUpdates, &rr->resrec, rr->resrec.rroriginalttl, limit);
5844 LogSPS("SendSPSRegistration put %s FAILED %d/%d %s", intf->ifname, p - m->omsg.data, limit - m->omsg.data, ARDisplayString(m, rr));
5847 LogSPS("SendSPSRegistration put %s 0x%x 0x%x (updateid %d) %s", intf->ifname, rr->updateIntID.l[1], rr->updateIntID.l[0], mDNSVal16(m->omsg.h.id), ARDisplayString(m, rr));
5850 rr->LastAPTime = m->timenow;
5852 if (mDNSOpaque16IsZero(rr->updateid)) LogMsg("SendSPSRegistration: ERROR!! rr %s updateid is zero", ARDisplayString(m, rr));
5853 if (m->NextScheduledResponse - (rr->LastAPTime + rr->ThisAPInterval) >= 0)
5854 m->NextScheduledResponse = (rr->LastAPTime + rr->ThisAPInterval);
5860 if (!m->omsg.h.mDNS_numUpdates) break;
5872 SetupOwnerOpt(m, intf, &opt.resrec.rdata->u.opt[1]); // use our own interface information
5879 LogSPS("SendSPSRegistration put %s %s", intf->ifname, ARDisplayString(m, &opt));
5880 p = PutResourceRecordTTLWithLimit(&m->omsg, p, &m->omsg.h.numAdditionals, &opt.resrec, opt.resrec.rroriginalttl, m->omsg.data + AbsoluteMaxDNSMessageData);
5882 LogMsg("SendSPSRegistration: Failed to put OPT record (%d updates) %s", m->omsg.h.mDNS_numUpdates, ARDisplayString(m, &opt));
5888 mDNSVal16(m->omsg.h.id), m->omsg.h.mDNS_numUpdates, p - m->omsg.data, &intf->SPSAddr[sps], mDNSVal16(intf->SPSPort[sps]));
5889 // if (intf->NextSPSAttempt < 5) m->omsg.h.flags = zeroID; // For simulating packet loss
5890 err = mDNSSendDNSMessage(m, &m->omsg, p, intf->InterfaceID, mDNSNULL, &intf->SPSAddr[sps], intf->SPSPort[sps], mDNSNULL, mDNSNULL, mDNSfalse);
5896 mDNS_StartQuery_internal(m, &intf->NetWakeResolve[sps]);
5903 intf->NextSPSAttemptTime = m->timenow + mDNSPlatformOneSecond * 10; // If successful, update NextSPSAttemptTime
5909 mDNSlocal mDNSBool RecordIsFirstOccurrenceOfOwner(mDNS *const m, const AuthRecord *const rr)
5912 for (ar = m->ResourceRecords; ar && ar != rr; ar=ar->next)
5917 mDNSlocal void mDNSCoreStoreProxyRR(mDNS *const m, const mDNSInterfaceID InterfaceID, AuthRecord *const rr)
5948 newRR->next = m->SPSRRSet;
5949 m->SPSRRSet = newRR;
5950 LogSPS("%s : Storing proxy record : %s ", __func__, ARDisplayString(m, rr));
5958 mDNSlocal void SPSInitRecordsBeforeUpdate(mDNS *const m, mDNSOpaque64 updateIntID, mDNSBool *WakeOnlyService)
5967 mDNSCoreFreeProxyRR(m);
5971 for (ar = m->ResourceRecords; ar; ar=ar->next)
5990 LogSPS("Setting scopeid (ALL) 0x%x 0x%x for %s", updateIntID.l[1], updateIntID.l[0], ARDisplayString(m, ar));
5997 mDNSu32 scopeid = mDNSPlatformInterfaceIndexfromInterfaceID(m, ar->resrec.InterfaceID, mDNStrue);
6002 ar->updateIntID.l[0], ARDisplayString(m, ar));
6013 mDNSCoreStoreProxyRR(m, ar->resrec.InterfaceID, ar);
6018 mDNSlocal void SendSPSRegistration(mDNS *const m, NetworkInterfaceInfo *const intf, const mDNSOpaque16 id)
6023 SendSPSRegistrationForOwner(m, intf, id, &owner);
6025 for (ar = m->ResourceRecords; ar; ar=ar->next)
6027 if (!mDNSPlatformMemSame(&owner, &ar->WakeUp, sizeof(owner)) && RecordIsFirstOccurrenceOfOwner(m, ar))
6030 SendSPSRegistrationForOwner(m, intf, id, &owner);
6036 mDNSlocal void RetrySPSRegistrations(mDNS *const m)
6041 // First make sure none of our interfaces' NextSPSAttemptTimes are inadvertently set to m->timenow + mDNSPlatformOneSecond * 10
6042 for (intf = GetFirstActiveInterface(m->HostInterfaces); intf; intf = GetFirstActiveInterface(intf->next))
6043 if (intf->NextSPSAttempt && intf->NextSPSAttemptTime == m->timenow + mDNSPlatformOneSecond * 10)
6047 for (rr = m->ResourceRecords; rr; rr=rr->next)
6048 if (!AuthRecord_uDNS(rr) && !mDNSOpaque16IsZero(rr->updateid) && m->timenow - (rr->LastAPTime + rr->ThisAPInterval) >= 0)
6050 for (intf = GetFirstActiveInterface(m->HostInterfaces); intf; intf = GetFirstActiveInterface(intf->next))
6053 mDNSu32 scopeid = mDNSPlatformInterfaceIndexfromInterfaceID(m, intf->InterfaceID, mDNStrue);
6057 LogSPS("RetrySPSRegistrations: 0x%x 0x%x (updateid %d) %s", rr->updateIntID.l[1], rr->updateIntID.l[0], mDNSVal16(rr->updateid), ARDisplayString(m, rr));
6058 SendSPSRegistration(m, intf, rr->updateid);
6064 for (intf = GetFirstActiveInterface(m->HostInterfaces); intf; intf = GetFirstActiveInterface(intf->next))
6065 if (intf->NextSPSAttempt && intf->NextSPSAttemptTime == m->timenow + mDNSPlatformOneSecond * 10 && intf->NextSPSAttempt < 8)
6069 mDNSlocal void NetWakeResolve(mDNS *const m, DNSQuestion *question, const ResourceRecord *const answer, QC_result AddRecord)
6073 (void)m; // Unused
6074 LogSPS("NetWakeResolve: SPS: %d Add: %d %s", sps, AddRecord, RRDisplayString(m, answer));
6084 mDNS_StopQuery(m, question);
6088 mDNS_StartQuery(m, question);
6093 mDNS_StopQuery(m, question);
6097 mDNS_Lock(m);
6098 if (sps == intf->NextSPSAttempt/3) SendSPSRegistration(m, intf, zeroID); // If we're ready for this result, use it now
6099 mDNS_Unlock(m);
6104 mDNS_StopQuery(m, question);
6107 mDNS_StartQuery(m, question);
6112 mDNS_StopQuery(m, question);
6116 mDNS_Lock(m);
6117 if (sps == intf->NextSPSAttempt/3) SendSPSRegistration(m, intf, zeroID); // If we're ready for this result, use it now
6118 mDNS_Unlock(m);
6122 mDNSexport mDNSBool mDNSCoreHaveAdvertisedMulticastServices(mDNS *const m)
6125 for (rr = m->ResourceRecords; rr; rr=rr->next)
6135 mDNSlocal void SendGoodbyesForWakeOnlyService(mDNS *const m, mDNSBool *WakeOnlyService)
6142 for (rr = m->ResourceRecords; rr; rr=rr->next)
6154 mDNSlocal void SendSleepGoodbyes(mDNS *const m, mDNSBool AllInterfaces, mDNSBool unicast)
6157 m->SleepState = SleepState_Sleeping;
6164 for (intf = GetFirstActiveInterface(m->HostInterfaces); intf; intf = GetFirstActiveInterface(intf->next))
6172 SleepRecordRegistrations(m); // If we have no SPS, need to deregister our uDNS records
6177 for (rr = m->ResourceRecords; rr; rr=rr->next)
6180 SendResponses(m);
6193 mDNSlocal mDNSBool skipSameSubnetRegistration(mDNS *const m, mDNSInterfaceID *regID, mDNSu32 count, mDNSInterfaceID intfid)
6199 for (newIntf = FirstInterfaceForID(m, intfid); newIntf; newIntf = newIntf->next)
6208 for (intf = FirstInterfaceForID(m, regID[i]); intf; intf = intf->next)
6226 mDNSlocal void DoKeepaliveCallbacks(mDNS *m)
6229 m->CurrentRecord = m->ResourceRecords;
6230 while (m->CurrentRecord)
6232 AuthRecord *const rr = m->CurrentRecord;
6235 LogSPS("DoKeepaliveCallbacks: Invoking the callback for %s", ARDisplayString(m, rr));
6237 rr->RecordCallback(m, rr, mStatus_BadStateErr);
6239 if (m->CurrentRecord == rr) // If m->CurrentRecord was not advanced for us, do it now
6240 m->CurrentRecord = rr->next;
6245 mDNSlocal void BeginSleepProcessing(mDNS *const m)
6256 m->NextScheduledSPRetry = m->timenow;
6258 if (!m->SystemWakeOnLANEnabled) LogSPS("BeginSleepProcessing: m->SystemWakeOnLANEnabled is false");
6259 else if (!mDNSCoreHaveAdvertisedMulticastServices(m)) LogSPS("BeginSleepProcessing: No advertised services");
6267 for (intf = GetFirstActiveInterface(m->HostInterfaces); intf; intf = GetFirstActiveInterface(intf->next))
6296 if (skipSameSubnetRegistration(m, registeredIntfIDS, registeredCount, intf->InterfaceID))
6305 if (ActivateLocalProxy(m, intf) == mStatus_NoError)
6307 SendGoodbyesForWakeOnlyService(m, &WakeOnlyService);
6311 // This will leave m->SleepState set to SleepState_Transferring,
6324 if (m->SystemWakeOnLANEnabled == mDNS_WakeOnBattery)
6330 FindSPSInCache(m, &intf->NetWakeBrowse, sps);
6332 intf->ifname, &intf->ip, NextQSendTime(&intf->NetWakeBrowse) - m->timenow, intf->NetWakeBrowse.ThisQInterval);
6339 intf->NextSPSAttemptTime = m->timenow + mDNSPlatformOneSecond;
6341 scopeid = mDNSPlatformInterfaceIndexfromInterfaceID(m, intf->InterfaceID, mDNStrue);
6346 // Don't need to set m->NextScheduledSPRetry here because we already set "m->NextScheduledSPRetry = m->timenow" above
6356 if (intf->NetWakeResolve[i].ThisQInterval >= 0) mDNS_StopQuery(m, &intf->NetWakeResolve[i]);
6360 LogSPS("BeginSleepProcessing: %-6s Found Sleep Proxy Server %d TTL %d %s", intf->ifname, i, sps[i]->resrec.rroriginalttl, CRDisplayString(m, sps[i]));
6363 mDNS_StartQuery_internal(m, &intf->NetWakeResolve[i]);
6378 SPSInitRecordsBeforeUpdate(m, updateIntID, &WakeOnlyService);
6385 DoKeepaliveCallbacks(m);
6406 SendSleepGoodbyes(m, mDNStrue, mDNStrue);
6411 SendSleepGoodbyes(m, mDNSfalse, mDNSfalse);
6417 SendResponses(m);
6421 // Call mDNSCoreMachineSleep(m, mDNStrue) when the machine is about to go to sleep.
6422 // Call mDNSCoreMachineSleep(m, mDNSfalse) when the machine is has just woken up.
6424 mDNSexport void mDNSCoreMachineSleep(mDNS *const m, mDNSBool sleep)
6428 LogSPS("%s (old state %d) at %ld", sleep ? "Sleeping" : "Waking", m->SleepState, m->timenow);
6430 if (sleep && !m->SleepState) // Going to sleep
6432 mDNS_Lock(m);
6434 if (m->SPSSocket)
6436 mDNSu8 oldstate = m->SPSState;
6438 m->SPSState = 2;
6440 if (oldstate == 1) mDNS_DeregisterService(m, &m->SPSRecords);
6447 m->SleepState = SleepState_Transferring;
6448 if (m->SystemWakeOnLANEnabled && m->DelaySleep)
6451 LogSPS("mDNSCoreMachineSleep: Re-sleeping immediately after waking; will delay for %d ticks", m->DelaySleep - m->timenow);
6452 m->SleepLimit = NonZeroTime(m->DelaySleep + mDNSPlatformOneSecond * 10);
6456 m->DelaySleep = 0;
6457 m->SleepLimit = NonZeroTime(m->timenow + mDNSPlatformOneSecond * 10);
6458 m->mDNSStats.Sleeps++;
6459 BeginSleepProcessing(m);
6463 SuspendLLQs(m);
6466 RemoveAutoTunnel6Record(m);
6468 LogSPS("mDNSCoreMachineSleep: m->SleepState %d (%s) seq %d", m->SleepState,
6469 m->SleepState == SleepState_Transferring ? "Transferring" :
6470 m->SleepState == SleepState_Sleeping ? "Sleeping" : "?", m->SleepSeqNum);
6471 mDNS_Unlock(m);
6481 mDNS_Lock(m);
6483 m->SleepLimit = 0;
6485 // If we were previously sleeping, but now we're not, increment m->SleepSeqNum to indicate that we're entering a new period of wakefulness
6486 if (m->SleepState != SleepState_Awake)
6488 m->SleepState = SleepState_Awake;
6489 m->SleepSeqNum++;
6494 m->DelaySleep = NonZeroTime(m->timenow + mDNSPlatformOneSecond * 16);
6497 if (m->SPSState == 3)
6499 m->SPSState = 0;
6500 mDNSCoreBeSleepProxyServer_internal(m, m->SPSType, m->SPSPortability, m->SPSMarginalPower, m->SPSTotalPower, m->SPSFeatureFlags);
6502 m->mDNSStats.Wakes++;
6505 for (intf = GetFirstActiveInterface(m->HostInterfaces); intf; intf = GetFirstActiveInterface(intf->next)) intf->NextSPSAttempt = -1;
6508 mDNSCoreRestartQueries(m);
6511 m->NextSRVUpdate = NonZeroTime(m->timenow + mDNSPlatformOneSecond);
6512 LogInfo("mDNSCoreMachineSleep waking: NextSRVUpdate in %d %d", m->NextSRVUpdate - m->timenow, m->timenow);
6519 m->StatStartTime = currtime;
6522 diff = currtime - m->TimeSlept;
6542 const mDNSs32 remain = uTTL - (m->timenow - cr->TimeRcvd) / mDNSPlatformOneSecond;
6554 CRDisplayString(m, cr), diff, remain);
6555 mDNS_PurgeCacheResourceRecord(m, cr);
6559 if (m->timenow - (cr->TimeRcvd + ((mDNSs32)uTTL * mDNSPlatformOneSecond)) >= 0)
6562 CRDisplayString(m, cr), remain, diff);
6563 mDNS_PurgeCacheResourceRecord(m, cr);
6567 LogInfo("mDNSCoreMachineSleep: %s: Adjusted the remain ttl %u by %d seconds", CRDisplayString(m, cr), remain, diff);
6573 mDNS_Reconfirm_internal(m, cr, kDefaultReconfirmTimeForWake);
6578 for (rr = m->ResourceRecords; rr; rr=rr->next)
6582 ActivateUnicastRegistration(m, rr);
6586 mDNSCoreRestartRegistration(m, rr, -1);
6598 RecreateNATMappings(m, mDNSPlatformOneSecond * 5);
6599 mDNS_Unlock(m);
6603 mDNSexport mDNSBool mDNSCoreReadyForSleep(mDNS *m, mDNSs32 now)
6609 mDNS_Lock(m);
6611 if (m->DelaySleep) goto notready;
6614 if (m->SleepLimit - now > 0 && m->NextScheduledSPRetry - now > 0) goto notready;
6616 m->NextScheduledSPRetry = now + 0x40000000UL;
6619 for (intf = GetFirstActiveInterface(m->HostInterfaces); intf; intf = GetFirstActiveInterface(intf->next))
6626 SendSPSRegistration(m, intf, zeroID);
6631 if (m->NextScheduledSPRetry - intf->NextSPSAttemptTime > 0)
6632 m->NextScheduledSPRetry = intf->NextSPSAttemptTime;
6636 for (intf = GetFirstActiveInterface(m->HostInterfaces); intf; intf = GetFirstActiveInterface(intf->next))
6648 for (rr = m->ResourceRecords; rr; rr = rr->next)
6651 { LogSPS("mDNSCoreReadyForSleep: waiting for SPS updateIntID 0x%x 0x%x (updateid %d) %s", rr->updateIntID.l[1], rr->updateIntID.l[0], mDNSVal16(rr->updateid), ARDisplayString(m,rr)); goto spsnotready; }
6654 for (q = m->Questions; q; q = q->next)
6662 for (rr = m->ResourceRecords; rr; rr = rr->next)
6666 { LogSPS("mDNSCoreReadyForSleep: waiting for Record updateIntID 0x%x 0x%x (updateid %d) %s", rr->updateIntID.l[1], rr->updateIntID.l[0], mDNSVal16(rr->updateid), ARDisplayString(m,rr)); goto notready; }
6668 if (!RecordReadyForSleep(m, rr)) { LogSPS("mDNSCoreReadyForSleep: waiting for %s", ARDisplayString(m, rr)); goto notready; }
6672 mDNS_Unlock(m);
6679 if (now - m->SleepLimit >= 0)
6683 for (intf = GetFirstActiveInterface(m->HostInterfaces); intf; intf = GetFirstActiveInterface(intf->next))
6688 mDNS_DeactivateNetWake_internal(m, intf);
6691 for (rr = m->ResourceRecords; rr; rr = rr->next)
6695 LogSPS("ReadyForSleep clearing updateIntID 0x%x 0x%x (updateid %d) for %s", rr->updateIntID.l[1], rr->updateIntID.l[0], mDNSVal16(rr->updateid), ARDisplayString(m, rr));
6705 m->SleepLimit = now + mDNSPlatformOneSecond * 1;
6707 SendSleepGoodbyes(m, mDNStrue, mDNStrue);
6711 mDNS_Unlock(m);
6715 mDNSexport mDNSs32 mDNSCoreIntervalToNextWake(mDNS *const m, mDNSs32 now)
6726 for (nat = m->NATTraversals; nat; nat=nat->next)
6742 for (ar = m->ResourceRecords; ar; ar = ar->next)
6751 (t - now) / mDNSPlatformOneSecond, ARDisplayString(m, ar));
6866 mDNSlocal mDNSBool MatchDependentOn(const mDNS *const m, const CacheRecord *const pktrr, const AuthRecord *const master)
6869 for (r1 = m->ResourceRecords; r1; r1=r1->next)
6878 for (r1 = m->DuplicateRecords; r1; r1=r1->next)
6894 mDNSlocal const AuthRecord *FindRRSet(const mDNS *const m, const CacheRecord *const pktrr)
6897 for (rr = m->ResourceRecords; rr; rr=rr->next)
6917 mDNSlocal mDNSBool PacketRRConflict(const mDNS *const m, const AuthRecord *const our, const CacheRecord *const pktrr)
6923 if (our->DependentOn || MatchDependentOn(m, pktrr, our)) return(mDNSfalse);
6928 const AuthRecord *pktset = FindRRSet(m, pktrr);
6944 mDNSlocal void ResolveSimultaneousProbe(mDNS *const m, const DNSMessage *const query, const mDNSu8 *const end,
6953 ptr = GetLargeResourceRecord(m, query, ptr, end, q->InterfaceID, kDNSRecordTypePacketAuth, &m->rec);
6955 if (m->rec.r.resrec.RecordType != kDNSRecordTypePacketNegative && ResourceRecordAnswersQuestion(&m->rec.r.resrec, q))
6958 if (PacketRRConflict(m, our, &m->rec.r))
6960 int result = (int)our->resrec.rrclass - (int)m->rec.r.resrec.rrclass;
6961 if (!result) result = (int)our->resrec.rrtype - (int)m->rec.r.resrec.rrtype;
6962 if (!result) result = CompareRData(our, &m->rec.r);
6966 LogMsg("ResolveSimultaneousProbe: %p Pkt Record: %08lX %s", q->InterfaceID, m->rec.r.resrec.rdatahash, CRDisplayString(m, &m->rec.r));
6967 LogMsg("ResolveSimultaneousProbe: %p Our Record %d %s %08lX %s", our->resrec.InterfaceID, our->ProbeCount, msg, our->resrec.rdatahash, ARDisplayString(m, our));
6974 m->SuppressProbes = NonZeroTime(m->timenow + mDNSPlatformOneSecond);
6977 InitializeLastAPTime(m, our);
6984 LogMsg("ResolveSimultaneousProbe: %p Pkt Record: %08lX %s", q->InterfaceID, m->rec.r.resrec.rdatahash, CRDisplayString(m, &m->rec.r));
6985 LogMsg("ResolveSimultaneousProbe: %p Our Record %d ign: %08lX %s", our->resrec.InterfaceID, our->ProbeCount, our->resrec.rdatahash, ARDisplayString(m, our));
6989 m->rec.r.resrec.RecordType = 0; // Clear RecordType to show we're not still using it
6994 m->rec.r.resrec.RecordType = 0; // Clear RecordType to show we're not still using it
6997 mDNSlocal CacheRecord *FindIdenticalRecordInCache(const mDNS *const m, const ResourceRecord *const pktrr)
7000 CacheGroup *cg = CacheGroupForRecord(m, slot, pktrr);
7017 mDNSlocal void DeregisterProxyRecord(mDNS *const m, AuthRecord *const rr)
7021 mDNS_Deregister_internal(m, rr, mDNS_Dereg_normal);
7022 SetSPSProxyListChanged(m->rec.r.resrec.InterfaceID);
7025 mDNSlocal void ClearKeepaliveProxyRecords(mDNS *const m, const OwnerOptData *const owner, AuthRecord *const thelist, const mDNSInterfaceID InterfaceID)
7027 if (m->CurrentRecord)
7028 LogMsg("ClearIdenticalProxyRecords ERROR m->CurrentRecord already set %s", ARDisplayString(m, m->CurrentRecord));
7029 m->CurrentRecord = thelist;
7033 while (m->CurrentRecord)
7035 AuthRecord *const rr = m->CurrentRecord;
7038 if (mDNS_KeepaliveRecord(&m->rec.r.resrec))
7041 m->ProxyRecords, &rr->WakeUp.HMAC, &rr->WakeUp.IMAC, rr->WakeUp.seq, owner->seq, ARDisplayString(m, rr));
7042 DeregisterProxyRecord(m, rr);
7045 // Mustn't advance m->CurrentRecord until *after* mDNS_Deregister_internal, because
7047 if (m->CurrentRecord == rr) // If m->CurrentRecord was not advanced for us, do it now
7048 m->CurrentRecord = rr->next;
7054 mDNSlocal void ClearIdenticalProxyRecords(mDNS *const m, const OwnerOptData *const owner, AuthRecord *const thelist)
7056 if (m->CurrentRecord)
7057 LogMsg("ClearIdenticalProxyRecords ERROR m->CurrentRecord already set %s", ARDisplayString(m, m->CurrentRecord));
7058 m->CurrentRecord = thelist;
7059 while (m->CurrentRecord)
7061 AuthRecord *const rr = m->CurrentRecord;
7062 if (m->rec.r.resrec.InterfaceID == rr->resrec.InterfaceID && mDNSSameEthAddress(&owner->HMAC, &rr->WakeUp.HMAC))
7063 if (IdenticalResourceRecord(&rr->resrec, &m->rec.r.resrec))
7066 m->ProxyRecords, &rr->WakeUp.HMAC, &rr->WakeUp.IMAC, rr->WakeUp.seq, owner->seq, ARDisplayString(m, rr));
7067 DeregisterProxyRecord(m, rr);
7069 // Mustn't advance m->CurrentRecord until *after* mDNS_Deregister_internal, because
7071 if (m->CurrentRecord == rr) // If m->CurrentRecord was not advanced for us, do it now
7072 m->CurrentRecord = rr->next;
7077 mDNSlocal void ClearProxyRecords(mDNS *const m, const OwnerOptData *const owner, AuthRecord *const thelist)
7079 if (m->CurrentRecord)
7080 LogMsg("ClearProxyRecords ERROR m->CurrentRecord already set %s", ARDisplayString(m, m->CurrentRecord));
7081 m->CurrentRecord = thelist;
7082 while (m->CurrentRecord)
7084 AuthRecord *const rr = m->CurrentRecord;
7085 if (m->rec.r.resrec.InterfaceID == rr->resrec.InterfaceID && mDNSSameEthAddress(&owner->HMAC, &rr->WakeUp.HMAC))
7086 if (owner->seq != rr->WakeUp.seq || m->timenow - rr->TimeRcvd > mDNSPlatformOneSecond * 60)
7095 &rr->WakeUp.HMAC, &rr->WakeUp.IMAC, ARDisplayString(m,rr));
7096 SendNDP(m, NDP_Adv, NDP_Override, rr, &rr->AddressProxy.ip.v6, &rr->WakeUp.IMAC, &AllHosts_v6, &AllHosts_v6_Eth);
7100 m->ProxyRecords, rr->AnnounceCount, rr->resrec.RecordType,
7101 &rr->WakeUp.HMAC, &rr->WakeUp.IMAC, rr->WakeUp.seq, owner->seq, ARDisplayString(m, rr));
7105 mDNS_Deregister_internal(m, rr, mDNS_Dereg_normal);
7106 SetSPSProxyListChanged(m->rec.r.resrec.InterfaceID);
7108 // Mustn't advance m->CurrentRecord until *after* mDNS_Deregister_internal, because
7110 if (m->CurrentRecord == rr) // If m->CurrentRecord was not advanced for us, do it now
7111 m->CurrentRecord = rr->next;
7116 mDNSlocal mDNSu8 *ProcessQuery(mDNS *const m, const DNSMessage *const query, const mDNSu8 *const end,
7120 mDNSBool FromLocalSubnet = srcaddr && mDNS_AddressIsLocalSubnet(m, InterfaceID, srcaddr, mDNSNULL);
7145 ptr = GetLargeResourceRecord(m, query, ptr, end, InterfaceID, kDNSRecordTypePacketAdd, &m->rec);
7146 if (ptr && m->rec.r.resrec.RecordType != kDNSRecordTypePacketNegative && m->rec.r.resrec.rrtype == kDNSType_OPT)
7149 const rdataOPT *const e = (const rdataOPT *)&m->rec.r.resrec.rdata->u.data[m->rec.r.resrec.rdlength];
7152 for (opt = &m->rec.r.resrec.rdata->u.opt[0]; opt < e; opt++)
7155 ClearProxyRecords(m, &opt->u.owner, m->DuplicateRecords);
7156 ClearProxyRecords(m, &opt->u.owner, m->ResourceRecords);
7159 m->rec.r.resrec.RecordType = 0; // Clear RecordType to show we're not still using it
7166 mDNSParseNSEC3Records(m, query, end, InterfaceID, &McastNSEC3Records);
7183 InitializeAnonInfoForQuestion(m, &McastNSEC3Records, &pktq);
7196 m->mDNSStats.UnicastBitInQueries++;
7198 m->mDNSStats.NormalQueries++;
7203 // Note: We use the m->CurrentRecord mechanism here because calling ResolveSimultaneousProbe
7208 if (m->CurrentRecord)
7209 LogMsg("ProcessQuery ERROR m->CurrentRecord already set %s", ARDisplayString(m, m->CurrentRecord));
7210 m->CurrentRecord = m->ResourceRecords;
7211 while (m->CurrentRecord)
7213 rr = m->CurrentRecord;
7214 m->CurrentRecord = rr->next;
7217 m->mDNSStats.MatchingAnswersForQueries++;
7221 ResolveSimultaneousProbe(m, query, end, &pktq, rr);
7247 if (m->timenow - (rr->LastMCTime + mDNSPlatformOneSecond) >= 0 ||
7267 m->NextScheduledResponse = m->timenow;
7275 m->mDNSStats.KnownAnswerMultiplePkts++;
7289 CacheGroup *cg = CacheGroupForName(m, slot, pktq.qnamehash, &pktq.qname);
7304 if (cr->MPUnansweredQ == 0 || m->timenow - cr->MPLastUnansweredQT >= mDNSPlatformOneSecond)
7310 cr->MPLastUnansweredQT = m->timenow;
7329 for (q = m->Questions; q; q=q->next)
7330 if (!q->Target.type && ActiveQuestion(q) && m->timenow - q->LastQTxTime > mDNSPlatformOneSecond / 4)
7348 for (rr = m->ResourceRecords; rr; rr=rr->next) // Now build our list of potential answers
7355 AddAdditionalsToResponseList(m, ResponseRecords, &nrp, InterfaceID);
7364 ptr = GetLargeResourceRecord(m, query, ptr, end, InterfaceID, kDNSRecordTypePacketAns, &m->rec);
7366 if (m->rec.r.resrec.RecordType != kDNSRecordTypePacketNegative)
7371 if (MustSendRecord(rr) && ShouldSuppressKnownAnswer(&m->rec.r, rr))
7373 m->mDNSStats.KnownAnswerSuppressions++;
7380 for (rr=m->ResourceRecords; rr; rr=rr->next)
7383 if (rr->ImmedAnswer == InterfaceID && ShouldSuppressKnownAnswer(&m->rec.r, rr))
7395 m->mDNSStats.KnownAnswerSuppressions++;
7399 LogMsg("Suppressed after%4d: %s", m->timenow - rr->ImmedAnswerMarkTime, ARDisplayString(m, rr));
7405 ourcacherr = FindIdenticalRecordInCache(m, &m->rec.r.resrec);
7410 if (ourcacherr && ourcacherr->MPExpectingKA && m->timenow - ourcacherr->MPLastUnansweredQT < mDNSPlatformOneSecond)
7424 if (cr->resrec.InterfaceID == InterfaceID && IdenticalResourceRecord(&m->rec.r.resrec, &cr->resrec))
7437 if (ResourceRecordAnswersQuestion(&m->rec.r.resrec, q))
7443 m->rec.r.resrec.RecordType = 0; // Clear RecordType to show we're not still using it
7469 if (m->timenow - (rr->LastMCTime + TicksTTL(rr)/4) >= 0)
7477 m->mDNSStats.UnicastDemotedToMulticast++;
7486 m->mDNSStats.MulticastResponses++;
7491 m->mDNSStats.UnicastResponses++;
7503 rr->ImmedAnswerMarkTime = m->timenow;
7505 m->NextScheduledResponse = m->timenow;
7538 // No need to set m->NextScheduledResponse here
7546 if (delayresponse && (!m->SuppressSending || (m->SuppressSending - m->timenow) < (delayresponse + 49) / 50))
7549 mDNSs32 oldss = m->SuppressSending;
7551 LogMsg("Current SuppressSending delay%5ld; require%5ld", m->SuppressSending - m->timenow, (delayresponse + 49) / 50);
7566 m->SuppressSending = m->timenow + (delayresponse + (mDNSs32)mDNSRandom((mDNSu32)mDNSPlatformOneSecond*5) + 49) / 50;
7567 if (m->SuppressSending == 0) m->SuppressSending = 1;
7570 LogMsg("Set SuppressSending to %5ld", m->SuppressSending - m->timenow);
7581 m->rec.r.resrec.RecordType = 0; // Clear RecordType to show we're not still using it
7605 if (cr->UnansweredQueries == 0 || m->timenow - cr->LastUnansweredTime >= mDNSPlatformOneSecond)
7608 cr->LastUnansweredTime = m->timenow;
7612 cr->UnansweredQueries, cr->MPUnansweredQ, cr->MPUnansweredKA, CRDisplayString(m, cr));
7614 SetNextCacheCheckTimeForRecord(m, cr);
7623 if (RRExpireTime(cr) - m->timenow > 4 * mDNSPlatformOneSecond)
7625 cr->UnansweredQueries, cr->MPUnansweredQ, cr->MPUnansweredKA, CRDisplayString(m, cr));
7627 m->mDNSStats.PoofCacheDeletions++;
7628 mDNS_Reconfirm_internal(m, cr, kDefaultReconfirmTimeForNoAnswer);
7642 mDNSu32 remain = (mDNSu32)(RRExpireTime(cr) - m->timenow) / 4;
7647 if (RRExpireTime(cr) - m->timenow > 4 * mDNSPlatformOneSecond)
7649 cr->UnansweredQueries, cr->MPUnansweredQ, cr->MPUnansweredKA, CRDisplayString(m, cr));
7659 mDNS_Reconfirm_internal(m, cr, remain);
7670 i = RecordDupSuppressInfo(q->DupSuppress, m->timenow, InterfaceID, srcaddr->type);
7678 FreeNSECRecords(m, McastNSEC3Records);
7684 mDNSlocal void mDNSCoreReceiveQuery(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *const end,
7690 !mDNSAddrIsDNSMulticast(dstaddr) && mDNS_AddressIsLocalSubnet(m, InterfaceID, srcaddr, mDNSNULL);
7712 responseend = ProcessQuery(m, msg, end, srcaddr, InterfaceID,
7713 !mDNSSameIPPort(srcport, MulticastDNSPort), mDNSAddrIsDNSMulticast(dstaddr), QueryWasLocalUnicast, &m->omsg);
7718 m->omsg.h.numQuestions, m->omsg.h.numQuestions == 1 ? "" : "s",
7719 m->omsg.h.numAnswers, m->omsg.h.numAnswers == 1 ? "" : "s",
7720 m->omsg.h.numAdditionals, m->omsg.h.numAdditionals == 1 ? "" : "s",
7722 mDNSSendDNSMessage(m, &m->omsg, responseend, InterfaceID, mDNSNULL, srcaddr, srcport, mDNSNULL, mDNSNULL, mDNSfalse);
7727 mDNSlocal mDNSBool TrustedSource(const mDNS *const m, const mDNSAddr *const srcaddr)
7730 (void)m; // Unused
7732 for (s = m->DNSServers; s; s = s->next)
7743 mDNSlocal DNSQuestion *ExpectingUnicastResponseForQuestion(const mDNS *const m, const mDNSIPPort port, const mDNSOpaque16 id, const DNSQuestion *const question, mDNSBool tcp)
7746 for (q = m->Questions; q; q=q->next)
7762 mDNSlocal DNSQuestion *ExpectingUnicastResponseForRecord(mDNS *const m,
7769 for (q = m->Questions; q; q=q->next)
7775 debugf("ExpectingUnicastResponseForRecord msg->h.id %d q->TargetQID %d for %s", mDNSVal16(id), mDNSVal16(q->TargetQID), CRDisplayString(m, rr));
7795 // if (TrustedSource(m, srcaddr)) return(mDNStrue);
7797 q->qname.c, DNSTypeName(q->qtype), &q->Target, mDNSVal16(srcp), srcaddr, mDNSVal16(port), CRDisplayString(m, rr));
7803 if (SrcLocal && q->ExpectUnicastResp && (mDNSu32)(m->timenow - q->ExpectUnicastResp) < (mDNSu32)(mDNSPlatformOneSecond*2))
7828 mDNSlocal mDNSBool ExpectingMulticastResponseForRecord(mDNS *const m, CacheRecord *rr, const mDNSAddr *srcaddr, mDNSBool recordAccepted,
7837 LogInfo("ExpectingMulticastResponseForRecord:A:AAAA: accepting %s, from %#a due to same packet %d", CRDisplayString(m, rr), srcaddr, m->PktNum);
7840 for (q = m->Questions; q; q=q->next)
7851 InitializeAnonInfoForCR(m, McastNSEC3Records, rr);
7862 LogInfo("ExpectingMulticastResponseForRecord: Name and Type match, accepting %s, from %#a", CRDisplayString(m, rr), srcaddr);
7864 LogInfo("ExpectingMulticastResponseForRecord: record %s, question %##s (%s)", CRDisplayString(m, rr), q->qname.c, DNSTypeName(q->qtype));
7881 CRDisplayString(m, rr), q->qname.c, srcaddr, m->PktNum);
7889 CacheGroup *cg = CacheGroupForName(m, slot, namehash, name);
7903 CRDisplayString(m, rr), srcaddr, m->PktNum);
7912 debugf("ExpectingMulticastResponseForRecord: discarding %s, from %#a, pktnum %d", CRDisplayString(m, rr), srcaddr, m->PktNum);
7930 mDNSexport CacheRecord *CreateNewCacheEntry(mDNS *const m, const mDNSu32 slot, CacheGroup *cg, mDNSs32 delay, mDNSBool Add, const mDNSAddr *sourceAddress)
7933 mDNSu16 RDLength = GetRDLengthMem(&m->rec.r.resrec);
7935 if (!m->rec.r.resrec.InterfaceID) debugf("CreateNewCacheEntry %s", CRDisplayString(m, &m->rec.r));
7938 // LogInfo("Rdata len %4d > InlineCacheRDSize %d %s", RDLength, InlineCacheRDSize, CRDisplayString(m, &m->rec.r));
7940 if (!cg) cg = GetCacheGroup(m, slot, &m->rec.r.resrec); // If we don't have a CacheGroup for this name, make one now
7941 if (cg) rr = GetCacheRecord(m, cg, RDLength); // Make a cache record, being careful not to recycle cg
7942 if (!rr) NoCacheAnswer(m, &m->rec.r);
7946 *rr = m->rec.r; // Block copy the CacheRecord object
7954 if (m->rec.r.resrec.AnonInfo)
7956 rr->resrec.AnonInfo = m->rec.r.resrec.AnonInfo;
7957 m->rec.r.resrec.AnonInfo = mDNSNULL;
7963 LogMsg("rr->resrec.rdata == &rr->rdatastorage but length > InlineCacheRDSize %##s", m->rec.r.resrec.name->c);
7965 LogMsg("rr->resrec.rdata != &rr->rdatastorage but length <= InlineCacheRDSize %##s", m->rec.r.resrec.name->c);
7967 mDNSPlatformMemCopy(rr->resrec.rdata, m->rec.r.resrec.rdata, sizeofRDataHeader + RDLength);
7978 m->rrcache_totalused_unicast += rr->resrec.rdlength;
7980 BumpDNSSECStats(m, kStatsActionIncrement, kStatsTypeMemoryUsage, rr->resrec.rdlength);
7987 CacheRecordAdd(m, rr); // CacheRecordAdd calls SetNextCacheCheckTimeForRecord(m, rr); for us
8001 ReleaseCacheRecord(m, rr);
8002 NoCacheAnswer(m, &m->rec.r);
8010 mDNSlocal void RefreshCacheRecord(mDNS *const m, CacheRecord *rr, mDNSu32 ttl)
8012 rr->TimeRcvd = m->timenow;
8020 SetNextCacheCheckTimeForRecord(m, rr);
8023 mDNSexport void GrantCacheExtensions(mDNS *const m, DNSQuestion *q, mDNSu32 lease)
8027 CacheGroup *cg = CacheGroupForName(m, slot, q->qnamehash, &q->qname);
8031 //LogInfo("GrantCacheExtensions: new lease %d / %s", lease, CRDisplayString(m, rr));
8032 RefreshCacheRecord(m, rr, lease);
8072 // in m->rec.
8073 mDNSlocal mDNSBool IsResponseAcceptable(mDNS *const m, const CacheRecord *crlist, DNSQuestion *q, mDNSBool *nseclist)
8075 CacheRecord *const newcr = &m->rec.r;
8091 LogInfo("IsResponseAcceptable: Found a matching entry for %##s in the CacheFlushRecords %s", rr->name->c, CRDisplayString(m, cr));
8106 CRDisplayString(m, newcr));
8124 LogInfo("IsResponseAcceptable: Accepting RRSIG %s matches question type %s", CRDisplayString(m, newcr),
8130 LogInfo("IsResponseAcceptable: Accepting RRSIG %s matches %s type (nseclist = 1)", CRDisplayString(m, newcr), DNSTypeName(typeCovered));
8136 LogInfo("IsResponseAcceptable: Accepting RRSIG %s matches SOA type (nseclist = 1)", CRDisplayString(m, newcr));
8146 LogMsg("IsResponseAcceptable: ERROR!! Not a unicast NSEC %s", CRDisplayString(m, newcr));
8149 LogInfo("IsResponseAcceptable: Accepting NSEC %s (nseclist = 1)", CRDisplayString(m, newcr));
8155 LogInfo("IsResponseAcceptable: Accepting SOA %s (nseclist = 1)", CRDisplayString(m, newcr));
8161 LogInfo("IsResponseAcceptable: Accepting NSEC3 %s (nseclist = 1)", CRDisplayString(m, newcr));
8169 mDNSlocal void FreeNSECRecords(mDNS *const m, CacheRecord *NSECRecords)
8176 ReleaseCacheRecord(m, rp);
8182 mDNSlocal void mDNSCoreReceiveNoDNSSECAnswers(mDNS *const m, const DNSMessage *const response, const mDNSu8 *end, const mDNSAddr *dstaddr,
8193 if (ptr && (qptr = ExpectingUnicastResponseForQuestion(m, dstport, response->h.id, &pktq, !dstaddr)) &&
8205 for (q = qptr->next ; q && q != m->NewQuestions; q = next)
8215 q->QuestionCallback(m, q, mDNSNULL, QC_nodnssec);
8219 qptr->QuestionCallback(m, qptr, mDNSNULL, QC_nodnssec);
8225 mDNSlocal void mDNSCoreReceiveNoUnicastAnswers(mDNS *const m, const DNSMessage *const response, const mDNSu8 *end, const mDNSAddr *dstaddr,
8237 if (ptr && (qptr = ExpectingUnicastResponseForQuestion(m, dstport, response->h.id, &q, !dstaddr)))
8241 CacheGroup *cg = CacheGroupForName(m, slot, q.qnamehash, &q.qname);
8246 if (RRExpireTime(rr) - m->timenow > 0) break;
8271 m->CurrentQuestion = qptr;
8277 GenerateNegativeResponse(m, QC_forceresponse);
8278 m->CurrentQuestion = mDNSNULL;
8307 ptr = GetLargeResourceRecord(m, response, ptr, end, InterfaceID, kDNSRecordTypePacketAuth, &m->rec);
8308 if (ptr && m->rec.r.resrec.RecordType != kDNSRecordTypePacketNegative && m->rec.r.resrec.rrtype == kDNSType_SOA)
8310 const mDNSu32 s = HashSlot(m->rec.r.resrec.name);
8311 CacheGroup *cgSOA = CacheGroupForRecord(m, s, &m->rec.r.resrec);
8312 const rdataSOA *const soa = (const rdataSOA *)m->rec.r.resrec.rdata->u.data;
8317 if (ttl_s > m->rec.r.resrec.rroriginalttl && m->rec.r.resrec.name->c[0])
8318 ttl_s = m->rec.r.resrec.rroriginalttl;
8323 SOARecord = CreateNewCacheEntry(m, s, cgSOA, 1, mDNSfalse, mDNSNULL);
8335 int scount = CountLabels(m->rec.r.resrec.name);
8337 if (SameDomainName(SkipLeadingLabels(&q.qname, qcount - scount), m->rec.r.resrec.name))
8341 m->rec.r.resrec.RecordType = 0; // Clear RecordType to show we're not still using it
8363 LogInfo("mDNSCoreReceiveNoUnicastAnswers: Renewing negative TTL from %d to %d %s", neg->resrec.rroriginalttl, negttl, CRDisplayString(m, neg));
8364 RefreshCacheRecord(m, neg, negttl);
8370 ResetQuestionState(m, qptr);
8377 if (!AddNSECSForCacheRecord(m, NSECRecords, neg, rcode))
8381 LogInfo("mDNSCoreReceiveNoUnicastAnswers: AddNSECSForCacheRecord failed to add NSEC for negcr %s during refresh", CRDisplayString(m, neg));
8382 FreeNSECRecords(m, NSECRecords);
8390 ReleaseCacheRecord(m, neg->soa);
8399 MakeNegativeCacheRecord(m, &m->rec.r, name, hash, q.qtype, q.qclass, negttl, mDNSInterface_Any, qptr->qDNSServer);
8400 m->rec.r.responseFlags = response->h.flags;
8406 cg = CacheGroupForName(m, slot, hash, name);
8411 negcr = CreateNewCacheEntry(m, slot, cg, 1, mDNStrue, mDNSNULL);
8415 if (!AddNSECSForCacheRecord(m, NSECRecords, negcr, rcode))
8418 CRDisplayString(m, negcr));
8419 FreeNSECRecords(m, NSECRecords);
8424 LogInfo("mDNSCoreReceiveNoUnicastAnswers: AddNSECSForCacheRecord added neg NSEC for %s", CRDisplayString(m, negcr));
8428 CacheRecordDeferredAdd(m, negcr);
8430 m->rec.r.resrec.RecordType = 0; // Clear RecordType to show we're not still using it
8436 negcr = CreateNewCacheEntry(m, slot, cg, 1, mDNStrue, mDNSNULL);
8447 ReleaseCacheRecord(m, negcr->soa);
8451 CacheRecordDeferredAdd(m, negcr);
8454 m->rec.r.responseFlags = zeroID;
8455 m->rec.r.resrec.RecordType = 0; // Clear RecordType to show we're not still using it
8461 cg = CacheGroupForName(m, slot, hash, name);
8467 if (NSECRecords) { LogInfo("mDNSCoreReceiveNoUnicastAnswers: NSECRecords not used"); FreeNSECRecords(m, NSECRecords); }
8468 if (SOARecord) { LogInfo("mDNSCoreReceiveNoUnicastAnswers: SOARecord not used"); ReleaseCacheRecord(m, SOARecord); }
8471 mDNSlocal void mDNSCorePrintStoredProxyRecords(mDNS *const m)
8475 for (rrPtr = m->SPSRRSet; rrPtr; rrPtr = rrPtr->next)
8477 LogSPS("%s", ARDisplayString(m, rrPtr));
8481 mDNSlocal mDNSBool mDNSCoreRegisteredProxyRecord(mDNS *const m, AuthRecord *rr)
8485 for (rrPtr = m->SPSRRSet; rrPtr; rrPtr = rrPtr->next)
8489 LogSPS("mDNSCoreRegisteredProxyRecord: Ignoring packet registered with sleep proxy : %s ", ARDisplayString(m, rr));
8493 mDNSCorePrintStoredProxyRecords(m);
8497 mDNSlocal CacheRecord* mDNSCoreReceiveCacheCheck(mDNS *const m, const DNSMessage *const response, uDNS_LLQType LLQType,
8511 mDNSu16 id2 = (m->rec.r.resrec.rDNSServer ? m->rec.r.resrec.rDNSServer->resGroupID : 0);
8517 if (match && IdenticalSameNameRecord(&m->rec.r.resrec, &rr->resrec))
8519 if (m->rec.r.resrec.rdlength > InlineCacheRDSize)
8521 m->rec.r.resrec.rdlength, InterfaceID, CRDisplayString(m, &m->rec.r));
8523 if (m->rec.r.resrec.RecordType & kDNSRecordTypePacketUniqueMask)
8538 for (q = m->Questions; q; q=q->next)
8543 rr->resrec.RecordType = m->rec.r.resrec.RecordType;
8547 if (!SameRDataBody(&m->rec.r.resrec, &rr->resrec.rdata->u, SameDomainNameCS))
8554 rr->TimeRcvd = m->timenow;
8556 SetNextCacheCheckTimeForRecord(m, rr);
8557 LogInfo("mDNSCoreReceiveCacheCheck: Discarding due to domainname case change old: %s", CRDisplayString(m, rr));
8558 LogInfo("mDNSCoreReceiveCacheCheck: Discarding due to domainname case change new: %s", CRDisplayString(m, &m->rec.r));
8560 NextCacheCheckEvent(rr) - m->timenow, slot, m->rrcache_nextcheck[slot] - m->timenow, m->NextCacheCheck - m->timenow);
8563 else if (!IdenticalAnonInfo(m->rec.r.resrec.AnonInfo, rr->resrec.AnonInfo))
8581 rr->TimeRcvd = m->timenow;
8583 SetNextCacheCheckTimeForRecord(m, rr);
8584 LogInfo("mDNSCoreReceiveCacheCheck: AnonInfo changed for %s", CRDisplayString(m, rr));
8588 else if (m->rec.r.resrec.rroriginalttl > 0)
8592 m->mDNSStats.CacheRefreshed++;
8594 if (rr->resrec.rroriginalttl == 0) debugf("uDNS rescuing %s", CRDisplayString(m, rr));
8595 RefreshCacheRecord(m, rr, m->rec.r.resrec.rroriginalttl);
8608 LogInfo("mDNSCoreReceiveCacheCheck: rescuing RR %s", CRDisplayString(m, rr));
8621 for (q = m->Questions; q; q=q->next)
8626 ResetQuestionState(m, q);
8645 debugf("DE for %s", CRDisplayString(m, rr));
8646 if (RRExpireTime(rr) - m->timenow > mDNSPlatformOneSecond)
8649 rr->TimeRcvd = m->timenow;
8651 SetNextCacheCheckTimeForRecord(m, rr);
8660 mDNSlocal void mDNSParseNSEC3Records(mDNS *const m, const DNSMessage *const response, const mDNSu8 *end,
8680 ptr = GetLargeResourceRecord(m, response, ptr, end, InterfaceID, kDNSRecordTypePacketAuth, &m->rec);
8681 if (!ptr || m->rec.r.resrec.RecordType == kDNSRecordTypePacketNegative || m->rec.r.resrec.rrtype != kDNSType_NSEC3)
8683 debugf("mDNSParseNSEC3Records: ptr %p, Record %s, ignoring", ptr, CRDisplayString(m, &m->rec.r));
8684 m->rec.r.resrec.RecordType = 0;
8687 slot = HashSlot(m->rec.r.resrec.name);
8688 cg = CacheGroupForRecord(m, slot, &m->rec.r.resrec);
8691 rr = CreateNewCacheEntry(m, slot, cg, 0, mDNSfalse, mDNSNULL);
8694 debugf("mDNSParseNSEC3Records: %s", CRDisplayString(m, rr));
8698 m->rec.r.resrec.RecordType = 0; // Clear RecordType to show we're not still using it
8702 mDNSlocal void mDNSCoreResetRecord(mDNS *const m)
8704 m->rec.r.resrec.RecordType = 0; // Clear RecordType to show we're not still using it
8705 if (m->rec.r.resrec.AnonInfo)
8707 FreeAnonInfo(m->rec.r.resrec.AnonInfo);
8708 m->rec.r.resrec.AnonInfo = mDNSNULL;
8736 mDNSlocal void mDNSCoreReceiveResponse(mDNS *const m,
8744 mDNSBool ResponseSrcLocal = !srcaddr || mDNS_AddressIsLocalSubnet(m, InterfaceID, srcaddr, &myself);
8747 uDNS_LLQType LLQType = uDNS_recvLLQResponse(m, response, end, srcaddr, srcport, &llqMatch);
8763 NetworkInterfaceInfo *llintf = FirstIPv4LLInterfaceForID(m, InterfaceID);
8817 // we could instead use the "m->PktNum" mechanism to tag each cache record with the packet number in
8825 if (ptr && (qptr = ExpectingUnicastResponseForQuestion(m, dstport, response->h.id, &q, !dstaddr)))
8833 CacheGroup *cg = CacheGroupForName(m, slot, q.qnamehash, &q.qname);
8851 rr->resrec.InterfaceID, CRDisplayString(m, rr));
8853 rr->TimeRcvd = m->timenow - TicksTTL(rr) - 1;
8858 LogInfo("mDNSCoreReceiveResponse: CRDNSSECQuestion set for record %s, question %##s (%s)", CRDisplayString(m, rr),
8883 PenalizeDNSServer(m, qptr, response->h.flags);
8902 BumpDNSSECStats(m, kStatsActionSet, kStatsTypeMsgSize, (end - response->data));
8910 mDNSParseNSEC3Records(m, response, end, InterfaceID, &McastNSEC3Records);
8923 ptr = GetLargeResourceRecord(m, response, ptr, end, InterfaceID, RecordType, &m->rec);
8926 if (m->rec.r.resrec.RecordType == kDNSRecordTypePacketNegative)
8928 mDNSCoreResetRecord(m);
8934 if (mDNSOpaque16IsZero(response->h.id) && m->rec.r.resrec.rrtype == kDNSType_NSEC3)
8936 mDNSCoreResetRecord(m);
8940 if (m->rec.r.resrec.rrtype == kDNSType_TSIG)
8942 mDNSCoreResetRecord(m);
8945 if (m->rec.r.resrec.rrtype == kDNSType_OPT)
8948 const rdataOPT *const e = (const rdataOPT *)&m->rec.r.resrec.rdata->u.data[m->rec.r.resrec.rdlength];
8951 for (opt = &m->rec.r.resrec.rdata->u.opt[0]; opt < e; opt++)
8954 ClearProxyRecords(m, &opt->u.owner, m->DuplicateRecords);
8955 ClearProxyRecords(m, &opt->u.owner, m->ResourceRecords);
8957 mDNSCoreResetRecord(m);
8961 if ((m->rec.r.resrec.rrtype == kDNSType_CNAME) && SameDomainName(m->rec.r.resrec.name, &m->rec.r.resrec.rdata->u.name))
8963 LogInfo("mDNSCoreReceiveResponse: CNAME loop domain name %##s", m->rec.r.resrec.name->c);
8964 mDNSCoreResetRecord(m);
8972 m->rec.r.resrec.rroriginalttl = GetEffectiveTTL(LLQType, m->rec.r.resrec.rroriginalttl);
8986 if (llqMatch != mDNSNULL) m->rec.r.resrec.rDNSServer = uDNSServer = llqMatch->qDNSServer;
9005 DNSQuestion *q = ExpectingUnicastResponseForRecord(m, srcaddr, ResponseSrcLocal, dstport, response->h.id, &m->rec.r, !dstaddr);
9022 m->rec.r.resrec.rDNSServer = uDNSServer = q->qDNSServer;
9033 debugf("mDNSCoreReceiveResponse: Can't find question for record name %##s", m->rec.r.resrec.name->c);
9038 else if (ExpectingMulticastResponseForRecord(m, &m->rec.r, srcaddr, recordAccepted, &McastNSEC3Records))
9042 LogInfo("mDNSCoreReceiveResponse: Accepting record in response to QU question %s, InterfaceID %p", CRDisplayString(m, &m->rec.r),
9045 else if (IsDeviceInfoRecord(m->rec.r.resrec.name))
9050 CRDisplayString(m, &m->rec.r), InterfaceID);
9054 else if (llintf && llintf->IgnoreIPv4LL && m->rec.r.resrec.rrtype == kDNSType_A)
9056 CacheRecord *const rr = &m->rec.r;
9063 LogInfo("mDNSResponder: Dropping LinkLocal packet %s", CRDisplayString(m, &m->rec.r));
9064 mDNSCoreResetRecord(m);
9070 if (mDNSOpaque16IsZero(response->h.id) && m->rec.r.resrec.rrtype != kDNSType_NSEC)
9072 if (m->CurrentRecord)
9073 LogMsg("mDNSCoreReceiveResponse ERROR m->CurrentRecord already set %s", ARDisplayString(m, m->CurrentRecord));
9074 m->CurrentRecord = m->ResourceRecords;
9075 while (m->CurrentRecord)
9077 AuthRecord *rr = m->CurrentRecord;
9078 m->CurrentRecord = rr->next;
9084 if (PacketRRMatchesSignature(&m->rec.r, rr)) // If interface, name, type (if shared record) and class match...
9087 if (IdenticalSameNameRecord(&m->rec.r.resrec, &rr->resrec))
9090 if (m->rec.r.resrec.rroriginalttl >= rr->resrec.rroriginalttl/2 || m->SleepState)
9097 if (rr->ImmedAnswer == mDNSNULL) { rr->ImmedAnswer = InterfaceID; m->NextScheduledResponse = m->timenow; }
9098 else if (rr->ImmedAnswer != InterfaceID) { rr->ImmedAnswer = mDNSInterfaceMark; m->NextScheduledResponse = m->timenow; }
9102 else if (m->rec.r.resrec.rroriginalttl > 0 && PacketRRConflict(m, rr, &m->rec.r))
9104 LogInfo("mDNSCoreReceiveResponse: Pkt Record: %08lX %s", m->rec.r.resrec.rdatahash, CRDisplayString(m, &m->rec.r));
9105 LogInfo("mDNSCoreReceiveResponse: Our Record: %08lX %s", rr->resrec.rdatahash, ARDisplayString(m, rr));
9112 LogInfo("mDNSCoreReceiveResponse: Dep Record: %08lX %s", rr->resrec.rdatahash, ARDisplayString(m, rr));
9117 LogInfo("mDNSCoreReceiveResponse: Already reset to Probing: %s", ARDisplayString(m, rr));
9119 LogMsg("mDNSCoreReceiveResponse: Ignoring response received before we even began probing: %s", ARDisplayString(m, rr));
9122 LogMsg("mDNSCoreReceiveResponse: Received from %#a:%d %s", srcaddr, mDNSVal16(srcport), CRDisplayString(m, &m->rec.r));
9126 LogMsg("mDNSCoreReceiveResponse: Resetting to Probing: %s", ARDisplayString(m, rr));
9134 InitializeLastAPTime(m, rr);
9135 RecordProbeFailure(m, rr); // Repeated late conflicts also cause us to back off to the slower probing rate
9141 if (!mDNSCoreRegisteredProxyRecord(m, rr))
9143 LogMsg("mDNSCoreReceiveResponse: ProbeCount %d; will deregister %s", rr->ProbeCount, ARDisplayString(m, rr));
9145 m->mDNSStats.NameConflicts++;
9146 mDNS_Deregister_internal(m, rr, mDNS_Dereg_conflict);
9156 LogMsg("mDNSCoreReceiveResponse: Unexpected conflict discarding %s", ARDisplayString(m, rr));
9157 m->mDNSStats.KnownUniqueNameConflicts++;
9158 mDNS_Deregister_internal(m, rr, mDNS_Dereg_conflict);
9161 LogMsg("mDNSCoreReceiveResponse: Unexpected record type %X %s", rr->resrec.RecordType, ARDisplayString(m, rr));
9168 else if (m->rec.r.resrec.rrtype == rr->resrec.rrtype)
9169 if ((m->rec.r.resrec.RecordType & kDNSRecordTypePacketUniqueMask) && m->timenow - rr->LastMCTime > mDNSPlatformOneSecond/2)
9170 { rr->ImmedAnswer = mDNSInterfaceMark; m->NextScheduledResponse = m->timenow; }
9178 AcceptableResponse = IsResponseAcceptable(m, CacheFlushRecords, unicastQuestion, &nseclist);
9179 if (AcceptableResponse) m->rec.r.resrec.rDNSServer = uDNSServer;
9185 if (!AcceptableResponse) LogInfo("mDNSCoreReceiveResponse ignoring %s", CRDisplayString(m, &m->rec.r));
9186 if (m->rrcache_size && AcceptableResponse)
9188 const mDNSu32 slot = HashSlot(m->rec.r.resrec.name);
9189 CacheGroup *cg = CacheGroupForRecord(m, slot, &m->rec.r.resrec);
9193 InitializeAnonInfoForCR(m, &McastNSEC3Records, &m->rec.r);
9202 rr = mDNSCoreReceiveCacheCheck(m, response, LLQType, slot, cg, unicastQuestion, &cfp, &NSECCachePtr, InterfaceID);
9211 if (!m->mDNSOppCaching && !rr && !myself && mDNSOpaque16IsZero(response->h.id))
9213 if (!ExpectingMulticastResponseForRecord(m, &m->rec.r, srcaddr, recordAccepted, &McastNSEC3Records))
9215 //LogMsg("mDNSCoreReceiveResponse: discarding %s", CRDisplayString(m, &m->rec.r));
9216 mDNSCoreResetRecord(m);
9228 if (!rr && m->rec.r.resrec.rroriginalttl > 0)
9230 const mDNSBool AddToCFList = (m->rec.r.resrec.RecordType & kDNSRecordTypePacketUniqueMask) && (LLQType != uDNS_LLQ_Events);
9234 delay = NonZeroTime(m->timenow + mDNSPlatformOneSecond);
9236 delay = CheckForSoonToExpireRecords(m, m->rec.r.resrec.name, m->rec.r.resrec.namehash, slot, mDNSNULL);
9242 rr = CreateNewCacheEntry(m, slot, cg, delay, !nseclist, srcaddr);
9258 LogInfo("mDNSCoreReceiveResponse: CRDNSSECQuestion set for new record %s, question %##s (%s)", CRDisplayString(m, rr),
9267 rr->TimeRcvd = m->timenow;
9279 ScheduleNextCacheCheckTime(m, slot, rr->DelayDelivery);
9285 if (rr && rr->resrec.AnonInfo && m->rec.r.resrec.AnonInfo)
9287 CopyAnonInfoForCR(m, rr, &m->rec.r);
9291 mDNSCoreResetRecord(m);
9295 mDNSCoreResetRecord(m);
9303 const CacheGroup *cg = CacheGroupForRecord(m, slot, &r1->resrec);
9356 if (m->timenow - r2->TimeRcvd < mDNSPlatformOneSecond && RRExpireTime(r2) - m->timenow > mDNSPlatformOneSecond)
9374 r2->resrec.rroriginalttl, r1->resrec.rroriginalttl, CRDisplayString(m, r2));
9377 r2->TimeRcvd = m->timenow;
9381 verbosedebugf("Cache flush new %p age %d expire in %d %s", r1, m->timenow - r1->TimeRcvd, RRExpireTime(r1) - m->timenow, CRDisplayString(m, r1));
9382 verbosedebugf("Cache flush old %p age %d expire in %d %s", r2, m->timenow - r2->TimeRcvd, RRExpireTime(r2) - m->timenow, CRDisplayString(m, r2));
9402 if (r2->TimeRcvd == m->timenow && r2->resrec.rroriginalttl == 1 && r2->UnansweredQueries == MaxUnansweredQueries)
9404 LogInfo("Cache flush for DE record %s", CRDisplayString(m, r2));
9407 else if (RRExpireTime(r2) - m->timenow > mDNSPlatformOneSecond)
9413 r2->TimeRcvd = m->timenow - 1;
9414 // We use (m->timenow - 1) instead of m->timenow, because we use that to identify records
9418 SetNextCacheCheckTimeForRecord(m, r2);
9433 LogInfo("mDNSCoreReceiveResponse: Updating NSECCachePtr to %s", CRDisplayString(m, r1));
9438 if (!AddNSECSForCacheRecord(m, NSECRecords, NSECCachePtr, rcode))
9440 LogInfo("mDNSCoreReceiveResponse: AddNSECSForCacheRecord failed to add NSEC for %s", CRDisplayString(m, NSECCachePtr));
9441 FreeNSECRecords(m, NSECRecords);
9446 r1->DelayDelivery = CheckForSoonToExpireRecords(m, r1->resrec.name, r1->resrec.namehash, slot, mDNSNULL);
9448 if (!r1->DelayDelivery) CacheRecordDeferredAdd(m, r1);
9449 else ScheduleNextCacheCheckTime(m, slot, r1->DelayDelivery);
9457 LogInfo("mDNSCoreReceieveResponse: Updating NSEC records in %s", CRDisplayString(m, NSECCachePtr));
9458 if (!AddNSECSForCacheRecord(m, NSECRecords, NSECCachePtr, rcode))
9460 LogInfo("mDNSCoreReceiveResponse: AddNSECSForCacheRecord failed to add NSEC for %s", CRDisplayString(m, NSECCachePtr));
9461 FreeNSECRecords(m, NSECRecords);
9478 mDNSCoreReceiveNoDNSSECAnswers(m, response, end, dstaddr, dstport, InterfaceID);
9481 mDNSCoreReceiveNoUnicastAnswers(m, response, end, dstaddr, dstport, InterfaceID, LLQType, rcode, NSECRecords);
9486 FreeNSECRecords(m, McastNSEC3Records);
9496 mDNSlocal void ScheduleWakeupForList(mDNS *const m, mDNSInterfaceID InterfaceID, mDNSEthAddr *e, AuthRecord *const thelist)
9498 // We need to use the m->CurrentRecord mechanism here when dealing with DuplicateRecords list as
9507 m->CurrentRecord = thelist;
9508 while (m->CurrentRecord)
9510 AuthRecord *const rr = m->CurrentRecord;
9513 LogInfo("ScheduleWakeupForList: Scheduling wakeup packets for %s", ARDisplayString(m, rr));
9514 mDNS_Deregister_internal(m, rr, mDNS_Dereg_normal);
9516 if (m->CurrentRecord == rr) // If m->CurrentRecord was not advanced for us, do it now
9517 m->CurrentRecord = rr->next;
9521 mDNSlocal void ScheduleWakeup(mDNS *const m, mDNSInterfaceID InterfaceID, mDNSEthAddr *e)
9524 ScheduleWakeupForList(m, InterfaceID, e, m->DuplicateRecords);
9525 ScheduleWakeupForList(m, InterfaceID, e, m->ResourceRecords);
9528 mDNSlocal void SPSRecordCallback(mDNS *const m, AuthRecord *const ar, mStatus result)
9531 LogInfo("SPS Callback %d %s", result, ARDisplayString(m, ar));
9535 mDNS_Lock(m);
9536 LogMsg("%-7s Conflicting mDNS -- waking %.6a %s", InterfaceNameForID(m, ar->resrec.InterfaceID), &ar->WakeUp.HMAC, ARDisplayString(m, ar));
9539 SendWakeup(m, ar->resrec.InterfaceID, &ar->WakeUp.IMAC, &ar->WakeUp.password); // Send one wakeup magic packet
9540 ScheduleWakeup(m, ar->resrec.InterfaceID, &ar->WakeUp.HMAC); // Schedule all other records with the same owner to be woken
9542 mDNS_Unlock(m);
9547 m->ProxyRecords--;
9549 mDNS_UpdateAllowSleep(m);
9780 else if (param == 'm')
9802 case 'm':
9833 mDNSlocal AuthRecord* mDNS_MatchKeepaliveInfo(mDNS *const m, const mDNSAddr* pladdr, const mDNSAddr* praddr, const mDNSIPPort plport,
9843 for (ar = m->ResourceRecords; ar; ar=ar->next)
9857 debugf("mDNS_MatchKeepaliveInfo: not a valid record %s for keepalive", ARDisplayString(m, ar));
9876 mDNSlocal void mDNS_SendKeepalives(mDNS *const m)
9880 for (ar = m->ResourceRecords; ar; ar=ar->next)
9900 debugf("mDNS_SendKeepalives: not a valid record %s for keepalive", ARDisplayString(m, ar));
9907 if (!ar->KATimeExpire || (m->timenow - ar->KATimeExpire >= 0))
9910 ar->KATimeExpire = NonZeroTime(m->timenow + timeout * mDNSPlatformOneSecond);
9912 if (m->NextScheduledKA - ar->KATimeExpire > 0)
9913 m->NextScheduledKA = ar->KATimeExpire;
9917 mDNSlocal void mDNS_SendKeepaliveACK(mDNS *const m, AuthRecord *ar)
9940 LogInfo("mDNS_SendKeepaliveACK: not a valid record %s for keepalive", ARDisplayString(m, ar));
9947 mDNSlocal void mDNSCoreReceiveUpdate(mDNS *const m,
9954 mDNSu8 *p = m->omsg.data;
9967 if (!InterfaceID || !m->SPSSocket || !mDNSSameIPPort(dstport, m->SPSSocket->port)) return;
9970 DumpPacket(m, mStatus_NoError, mDNSfalse, "UDP", srcaddr, srcport, dstaddr, dstport, msg, end);
9975 ptr = GetLargeResourceRecord(m, msg, ptr, end, 0, kDNSRecordTypePacketAdd, &m->rec);
9976 if (ptr && m->rec.r.resrec.RecordType != kDNSRecordTypePacketNegative && m->rec.r.resrec.rrtype == kDNSType_OPT)
9979 const rdataOPT *const e = (const rdataOPT *)&m->rec.r.resrec.rdata->u.data[m->rec.r.resrec.rdlength];
9980 for (o = &m->rec.r.resrec.rdata->u.opt[0]; o < e; o++)
9986 m->rec.r.resrec.RecordType = 0; // Clear RecordType to show we're not still using it
9989 InitializeDNSMessage(&m->omsg.h, msg->h.id, UpdateRespFlags);
10000 m->omsg.h.flags.b[1] |= kDNSFlag1_RC_FormErr;
10002 else if (m->ProxyRecords + msg->h.mDNS_numUpdates > MAX_PROXY_RECORDS)
10009 m->ProxyRecords, msg->h.mDNS_numUpdates, m->ProxyRecords + msg->h.mDNS_numUpdates, MAX_PROXY_RECORDS);
10011 m->omsg.h.flags.b[1] |= kDNSFlag1_RC_Refused;
10026 ClearKeepaliveProxyRecords(m, &owner, m->DuplicateRecords, InterfaceID);
10027 ClearKeepaliveProxyRecords(m, &owner, m->ResourceRecords, InterfaceID);
10031 ptr = GetLargeResourceRecord(m, msg, ptr, end, InterfaceID, kDNSRecordTypePacketAuth, &m->rec);
10032 if (ptr && m->rec.r.resrec.RecordType != kDNSRecordTypePacketNegative)
10034 mDNSu16 RDLengthMem = GetRDLengthMem(&m->rec.r.resrec);
10038 m->omsg.h.flags.b[1] |= kDNSFlag1_RC_Refused;
10043 mDNSu8 RecordType = m->rec.r.resrec.RecordType & kDNSRecordTypePacketUniqueMask ? kDNSRecordTypeUnique : kDNSRecordTypeShared;
10044 m->rec.r.resrec.rrclass &= ~kDNSClass_UniqueRRSet;
10046 if (!mDNS_KeepaliveRecord(&m->rec.r.resrec))
10048 ClearIdenticalProxyRecords(m, &owner, m->DuplicateRecords); // Make sure we don't have any old stale duplicates of this record
10049 ClearIdenticalProxyRecords(m, &owner, m->ResourceRecords);
10051 mDNS_SetupResourceRecord(ar, mDNSNULL, InterfaceID, m->rec.r.resrec.rrtype, m->rec.r.resrec.rroriginalttl, RecordType, AuthRecordAny, SPSRecordCallback, ar);
10052 AssignDomainName(&ar->namestorage, m->rec.r.resrec.name);
10053 ar->resrec.rdlength = GetRDLength(&m->rec.r.resrec, mDNSfalse);
10055 mDNSPlatformMemCopy(ar->resrec.rdata->u.data, m->rec.r.resrec.rdata->u.data, RDLengthMem);
10058 if (m->rec.r.resrec.rrtype == kDNSType_PTR)
10060 mDNSs32 t = ReverseMapDomainType(m->rec.r.resrec.name);
10061 if (t == mDNSAddrType_IPv4) GetIPv4FromName(&ar->AddressProxy, m->rec.r.resrec.name);
10062 else if (t == mDNSAddrType_IPv6) GetIPv6FromName(&ar->AddressProxy, m->rec.r.resrec.name);
10063 debugf("mDNSCoreReceiveUpdate: PTR %d %d %#a %s", t, ar->AddressProxy.type, &ar->AddressProxy, ARDisplayString(m, ar));
10066 ar->TimeRcvd = m->timenow;
10067 ar->TimeExpire = m->timenow + updatelease * mDNSPlatformOneSecond;
10068 if (m->NextScheduledSPS - ar->TimeExpire > 0)
10069 m->NextScheduledSPS = ar->TimeExpire;
10071 mDNS_Register_internal(m, ar);
10073 m->ProxyRecords++;
10074 mDNS_UpdateAllowSleep(m);
10075 LogSPS("SPS Registered %4d %X %s", m->ProxyRecords, RecordType, ARDisplayString(m,ar));
10078 m->rec.r.resrec.RecordType = 0; // Clear RecordType to show we're not still using it
10081 if (m->omsg.h.flags.b[1] & kDNSFlag1_RC_Mask)
10084 ClearProxyRecords(m, &owner, m->DuplicateRecords);
10085 ClearProxyRecords(m, &owner, m->ResourceRecords);
10095 p = PutResourceRecordTTLWithLimit(&m->omsg, p, &m->omsg.h.numAdditionals, &opt.resrec, opt.resrec.rroriginalttl, m->omsg.data + AbsoluteMaxDNSMessageData);
10099 if (p) mDNSSendDNSMessage(m, &m->omsg, p, InterfaceID, m->SPSSocket, srcaddr, srcport, mDNSNULL, mDNSNULL, mDNSfalse);
10100 mDNS_SendKeepalives(m);
10103 mDNSlocal void mDNSCoreReceiveUpdateR(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *end, const mDNSAddr *srcaddr, const mDNSInterfaceID InterfaceID)
10111 ptr = GetLargeResourceRecord(m, msg, ptr, end, 0, kDNSRecordTypePacketAdd, &m->rec);
10112 if (ptr && m->rec.r.resrec.RecordType != kDNSRecordTypePacketNegative && m->rec.r.resrec.rrtype == kDNSType_OPT)
10115 const rdataOPT *const e = (const rdataOPT *)&m->rec.r.resrec.rdata->u.data[m->rec.r.resrec.rdlength];
10116 for (o = &m->rec.r.resrec.rdata->u.opt[0]; o < e; o++)
10123 m->rec.r.resrec.RecordType = 0; // Clear RecordType to show we're not still using it
10126 if (m->CurrentRecord)
10127 LogMsg("mDNSCoreReceiveUpdateR ERROR m->CurrentRecord already set %s", ARDisplayString(m, m->CurrentRecord));
10128 m->CurrentRecord = m->ResourceRecords;
10129 while (m->CurrentRecord)
10131 AuthRecord *const rr = m->CurrentRecord;
10137 mDNSu32 scopeid = mDNSPlatformInterfaceIndexfromInterfaceID(m, InterfaceID, mDNStrue);
10142 rr->expire = NonZeroTime(m->timenow + updatelease * mDNSPlatformOneSecond);
10143 LogSPS("Sleep Proxy %s record %5d 0x%x 0x%x (%d) %s", rr->WakeUp.HMAC.l[0] ? "transferred" : "registered", updatelease, rr->updateIntID.l[1], rr->updateIntID.l[0], mDNSVal16(rr->updateid), ARDisplayString(m,rr));
10148 mDNS_Deregister_internal(m, rr, mDNS_Dereg_normal);
10151 // Mustn't advance m->CurrentRecord until *after* mDNS_Deregister_internal, because
10153 if (m->CurrentRecord == rr) // If m->CurrentRecord was not advanced for us, do it now
10154 m->CurrentRecord = rr->next;
10158 char *ifname = InterfaceNameForID(m, InterfaceID);
10165 if (m->SleepLimit) m->NextScheduledSPRetry = m->timenow;
10168 mDNSexport void MakeNegativeCacheRecord(mDNS *const m, CacheRecord *const cr,
10171 if (cr == &m->rec.r && m->rec.r.resrec.RecordType)
10173 LogMsg("MakeNegativeCacheRecord: m->rec appears to be already in use for %s", CRDisplayString(m, &m->rec.r));
10195 cr->TimeRcvd = m->timenow;
10197 cr->NextRequiredQuery = m->timenow;
10198 cr->LastUsed = m->timenow;
10217 mDNSexport void mDNSCoreReceive(mDNS *const m, void *const pkt, const mDNSu8 *const end,
10233 if (mDNSSameAddress(srcaddr, &m->Router))
10236 if (mDNSSameIPPort(srcport, SSDPPort) || (m->SSDPSocket && mDNSSameIPPort(dstport, m->SSDPSocket->port)))
10238 mDNS_Lock(m);
10239 LNT_ConfigureRouterInfo(m, InterfaceID, pkt, (mDNSu16)(end - (mDNSu8 *)pkt));
10240 mDNS_Unlock(m);
10246 mDNS_Lock(m);
10247 uDNS_ReceiveNATPacket(m, InterfaceID, pkt, (mDNSu16)(end - (mDNSu8 *)pkt));
10248 mDNS_Unlock(m);
10253 else if (m->SSDPSocket && mDNSSameIPPort(dstport, m->SSDPSocket->port)) { debugf("Ignoring SSDP response from %#a:%d", srcaddr, mDNSVal16(srcport)); return; }
10270 if (!m) { LogMsg("mDNSCoreReceive ERROR m is NULL"); return; }
10276 mDNS_Lock(m);
10277 m->PktNum++;
10280 m->MPktNum++;
10285 if (!mDNS_AddressIsLocalSubnet(m, InterfaceID, srcaddr, mDNSNULL) &&
10288 m->RemoteSubnet++;
10299 DumpPacket(m, mStatus_NoError, mDNSfalse, TLS ? "TLS" : !dstaddr ? "TCP" : "UDP", srcaddr, srcport, dstaddr, dstport, msg, end);
10300 uDNS_ReceiveMsg(m, msg, end, srcaddr, srcport);
10304 if (QR_OP == StdQ) mDNSCoreReceiveQuery (m, msg, end, srcaddr, srcport, dstaddr, dstport, ifid);
10305 else if (QR_OP == StdR) mDNSCoreReceiveResponse(m, msg, end, srcaddr, srcport, dstaddr, dstport, ifid);
10306 else if (QR_OP == UpdQ) mDNSCoreReceiveUpdate (m, msg, end, srcaddr, srcport, dstaddr, dstport, InterfaceID);
10307 else if (QR_OP == UpdR) mDNSCoreReceiveUpdateR (m, msg, end, srcaddr, InterfaceID);
10329 mDNS_Unlock(m);
10366 mDNSlocal DNSQuestion *FindDuplicateQuestion(const mDNS *const m, const DNSQuestion *const question)
10373 for (q = m->Questions; q && q != question; q=q->next) // Scan our list for another question
10394 mDNSlocal void UpdateQuestionDuplicates(mDNS *const m, DNSQuestion *const question)
10409 for (q = m->Questions; q; q=q->next) // Scan our list of questions
10472 SetNextQueryTime(m,q);
10477 mDNSexport McastResolver *mDNS_AddMcastResolver(mDNS *const m, const domainname *d, const mDNSInterfaceID interface, mDNSu32 timeout)
10479 McastResolver **p = &m->McastResolvers;
10486 mDNS_CheckLock(m);
10520 mDNSinline mDNSs32 PenaltyTimeForServer(mDNS *m, DNSServer *server)
10525 ptime = server->penaltyTime - m->timenow;
10532 ptime, server->penaltyTime, m->timenow);
10575 mDNSlocal mDNSu32 GetTimeoutForMcastQuestion(mDNS *m, DNSQuestion *question)
10581 for (curr = m->McastResolvers; curr; curr = curr->next)
10688 mDNSexport mDNSu32 SetValidDNSServers(mDNS *m, DNSQuestion *question)
10699 for (curr = m->DNSServers; curr; curr = curr->next)
10763 mDNSlocal DNSServer *GetBestServer(mDNS *m, const domainname *name, mDNSInterfaceID InterfaceID, mDNSs32 ServiceID, mDNSOpaque64 validBits,
10776 for (curr = m->DNSServers; curr; curr = curr->next)
10794 currPenaltyTime = PenaltyTimeForServer(m, curr);
10838 mDNSlocal DNSServer *GetServerForName(mDNS *m, const domainname *name, mDNSInterfaceID InterfaceID, mDNSs32 ServiceID)
10847 if (InterfaceID) ifname = InterfaceNameForID(m, InterfaceID);
10852 curmatch = GetBestServer(m, name, InterfaceID, ServiceID, allValid, mDNSNULL, mDNStrue);
10856 mDNSVal16(curmatch->port), (curmatch->penaltyTime ? (curmatch->penaltyTime - m->timenow) : 0), ifname ? ifname : "None",
10865 mDNSexport DNSServer *GetServerForQuestion(mDNS *m, DNSQuestion *question)
10877 ifname = InterfaceNameForID(m, InterfaceID);
10881 curmatch = GetBestServer(m, name, InterfaceID, question->ServiceID, question->validDNSServers, &currindex, mDNSfalse);
10890 (curmatch->penaltyTime ? (curmatch->penaltyTime - m->timenow) : 0), ifname ? ifname : "None",
10907 mDNSlocal void LLQNATCallback(mDNS *m, NATTraversalInfo *n)
10910 mDNS_Lock(m);
10913 for (q = m->Questions; q; q=q->next)
10915 startLLQHandshake(m, q); // If ExternalPort is zero, will do StartLLQPolling instead
10917 UpdateAutoTunnelDomainStatuses(m);
10919 mDNS_Unlock(m);
10922 mDNSlocal mDNSBool IsPrivateDomain(mDNS *const m, DNSQuestion *q)
10926 AuthInfo = GetAuthInfoForName_internal(m, &q->qname);
10942 mDNSlocal mDNSBool ShouldSuppressUnicastQuery(mDNS *const m, DNSQuestion *q, DNSServer *d)
10952 if (IsPrivateDomain(m, q))
10984 mDNSlocal mDNSBool ShouldSuppressDotLocalQuery(mDNS *const m, DNSQuestion *q)
11000 for (intf = m->HostInterfaces; intf; intf = intf->next)
11010 // Set m->CurrentQuestion as it is required by AnswerQuestionWithLORecord.
11011 m->CurrentQuestion = q;
11012 ret = AnswerQuestionWithLORecord(m, q, mDNStrue);
11013 m->CurrentQuestion = mDNSNULL;
11023 for (rr = m->ResourceRecords; rr; rr = rr->next)
11027 LogInfo("ShouldSuppressDotLocalQuery: Found resource record %s for %##s (%s) not suppressing", ARDisplayString(m, rr),
11035 mDNSlocal mDNSBool ShouldSuppressQuery(mDNS *const m, DNSQuestion *q)
11048 if (!ShouldSuppressDotLocalQuery(m, q))
11060 return (ShouldSuppressUnicastQuery(m, q, q->qDNSServer));
11063 mDNSlocal void CacheRecordRmvEventsForCurrentQuestion(mDNS *const m, DNSQuestion *q)
11070 cg = CacheGroupForName(m, slot, q->qnamehash, &q->qname);
11077 CRDisplayString(m, rr), q, q->qname.c, DNSTypeName(q->qtype), rr->CRActiveQuestion, q->CurrentAnswers);
11084 q->qname.c, CRDisplayString(m, rr), q->LOAddressAnswers);
11098 for (qptr = m->Questions; qptr; qptr=qptr->next)
11105 qptr, CRDisplayString(m,rr), q->CurrentAnswers, qptr->CurrentAnswers, qptr->SuppressUnusable, qptr->SuppressQuery);
11108 if (!qptr) m->rrcache_active--; // If no longer active, decrement rrcache_active count
11110 AnswerCurrentQuestionWithResourceRecord(m, rr, QC_rmv);
11111 if (m->CurrentQuestion != q) break; // If callback deleted q, then we're finished here
11116 mDNSlocal mDNSBool IsQuestionNew(mDNS *const m, DNSQuestion *question)
11119 for (q = m->NewQuestions; q; q = q->next)
11124 mDNSlocal mDNSBool LocalRecordRmvEventsForQuestion(mDNS *const m, DNSQuestion *q)
11130 if (m->CurrentQuestion)
11131 LogMsg("LocalRecordRmvEventsForQuestion: ERROR m->CurrentQuestion already set: %##s (%s)",
11132 m->CurrentQuestion->qname.c, DNSTypeName(m->CurrentQuestion->qtype));
11134 if (IsQuestionNew(m, q))
11139 m->CurrentQuestion = q;
11141 ag = AuthGroupForName(&m->rrauth, slot, q->qnamehash, &q->qname);
11149 ARDisplayString(m, rr));
11157 AnswerLocalQuestionWithLocalAuthRecord(m, rr, QC_rmv); // MUST NOT dereference q again
11158 if (m->CurrentQuestion != q) { m->CurrentQuestion = mDNSNULL; return mDNSfalse; }
11161 m->CurrentQuestion = mDNSNULL;
11167 mDNSlocal mDNSBool CacheRecordRmvEventsForQuestion(mDNS *const m, DNSQuestion *q)
11169 if (m->CurrentQuestion)
11170 LogMsg("CacheRecordRmvEventsForQuestion: ERROR m->CurrentQuestion already set: %##s (%s)",
11171 m->CurrentQuestion->qname.c, DNSTypeName(m->CurrentQuestion->qtype));
11175 if (!IsQuestionNew(m, q) && !q->LOAddressAnswers)
11177 m->CurrentQuestion = q;
11178 CacheRecordRmvEventsForCurrentQuestion(m, q);
11179 if (m->CurrentQuestion != q) { m->CurrentQuestion = mDNSNULL; return mDNSfalse; }
11180 m->CurrentQuestion = mDNSNULL;
11186 mDNSlocal void SuppressStatusChanged(mDNS *const m, DNSQuestion *q, DNSQuestion **restart)
11194 if (!CacheRecordRmvEventsForQuestion(m, q))
11206 if (!LocalRecordRmvEventsForQuestion(m, q))
11240 mDNS_StopQuery_internal(m, q);
11246 mDNSexport void CheckSuppressUnusableQuestions(mDNS *const m)
11262 if (m->RestartQuestion)
11263 LogMsg("CheckSuppressUnusableQuestions: ERROR!! m->RestartQuestion already set: %##s (%s)",
11264 m->RestartQuestion->qname.c, DNSTypeName(m->RestartQuestion->qtype));
11265 m->RestartQuestion = m->Questions;
11266 while (m->RestartQuestion)
11268 q = m->RestartQuestion;
11269 m->RestartQuestion = q->next;
11273 q->SuppressQuery = ShouldSuppressQuery(m, q);
11279 SuppressStatusChanged(m, q, &restart);
11289 mDNS_StartQuery_internal(m, q);
11293 mDNSlocal void RestartUnicastQuestions(mDNS *const m)
11298 if (m->RestartQuestion)
11299 LogMsg("RestartUnicastQuestions: ERROR!! m->RestartQuestion already set: %##s (%s)",
11300 m->RestartQuestion->qname.c, DNSTypeName(m->RestartQuestion->qtype));
11301 m->RestartQuestion = m->Questions;
11302 while (m->RestartQuestion)
11304 q = m->RestartQuestion;
11305 m->RestartQuestion = q->next;
11312 SuppressStatusChanged(m, q, &restart);
11321 mDNS_StartQuery_internal(m, q);
11328 mDNSlocal mStatus ValidateParameters(mDNS *const m, DNSQuestion *const question)
11351 NetworkInterfaceInfo *intf = FirstInterfaceForID(m, question->InterfaceID);
11362 mDNSlocal void InitDNSConfig(mDNS *const m, DNSQuestion *const question)
11377 mDNSu32 timeout = SetValidDNSServers(m, question);
11389 question->StopTime = NonZeroTime(m->timenow + timeout * mDNSPlatformOneSecond);
11393 question->qDNSServer = GetServerForQuestion(m, question);
11402 question->StopTime = NonZeroTime(m->timenow + GetTimeoutForMcastQuestion(m, question) * mDNSPlatformOneSecond);
11406 SetNextQueryStopTime(m, question);
11409 SetNextQueryTime(m,question);
11414 mDNSlocal mDNSBool InitCommonState(mDNS *const m, DNSQuestion *const question)
11434 question->DelayAnswering = CheckForSoonToExpireRecords(m, &question->qname, question->qnamehash, HashSlot(&question->qname), &purge);
11435 question->LastQTime = m->timenow;
11437 question->LastAnswerPktNum = m->PktNum;
11469 question->ServiceID = mDNSPlatformGetServiceID(m, question);
11474 InitDNSConfig(m, question);
11476 question->AuthInfo = GetAuthInfoForQuestion(m, question);
11479 question->SuppressQuery = ShouldSuppressQuery(m, question);
11483 question->DisallowPID = (question->ServiceID == 0 || (mDNSPlatformAllowPID(m, question) == 0));
11523 question->LastQTxTime = m->timenow;
11539 question->qname.c, DNSTypeName(question->qtype), question->InterfaceID, m->timenow,
11540 NextQSendTime(question) - m->timenow,
11541 question->DelayAnswering ? question->DelayAnswering - m->timenow : 0,
11546 question->DelayAnswering - m->timenow, question->qname.c, DNSTypeName(question->qtype));
11567 mDNSlocal void InitLLQNATState(mDNS *const m)
11570 if (!m->LLQNAT.clientCallback)
11572 m->LLQNAT.Protocol = NATOp_MapUDP;
11573 m->LLQNAT.IntPort = m->UnicastPort4;
11574 m->LLQNAT.RequestedPort = m->UnicastPort4;
11575 m->LLQNAT.clientCallback = LLQNATCallback;
11576 m->LLQNAT.clientContext = (void*)1; // Means LLQ NAT Traversal just started
11577 mDNS_StartNATOperation_internal(m, &m->LLQNAT);
11592 mDNSlocal void InitDNSSECProxyState(mDNS *const m, DNSQuestion *const question)
11594 (void) m;
11623 mDNSlocal void FinalizeUnicastQuestion(mDNS *const m, DNSQuestion *question, mDNSBool purge)
11637 ActivateUnicastQuery(m, question, mDNSfalse);
11644 mDNS_PurgeForQuestion(m, question);
11649 CheckForDNSSECRecords(m, question);
11658 InitLLQNATState(m);
11660 UpdateAutoTunnelDomainStatuses(m);
11665 mDNSexport mStatus mDNS_StartQuery_internal(mDNS *const m, DNSQuestion *const question)
11673 if (m->rrcache_size == 0)
11676 vStatus = ValidateParameters(m, question);
11682 (question->Target.type || Question_uDNS(question)) ? mDNS_NewMessageID(m) :
11688 q = &m->Questions;
11690 q = &m->LocalOnlyQuestions;
11708 purge = InitCommonState(m, question);
11711 InitDNSSECProxyState(m, question);
11716 question->DuplicateOf = FindDuplicateQuestion(m, question);
11722 if (!m->NewLocalOnlyQuestions)
11723 m->NewLocalOnlyQuestions = question;
11727 if (!m->NewQuestions)
11728 m->NewQuestions = question;
11734 // m->NewQuestions if necessary then we could end up recursively re-entering
11738 FinalizeUnicastQuestion(m, question, purge);
11745 mDNS_PurgeForQuestion(m, question);
11754 mDNSexport void CancelGetZoneData(mDNS *const m, ZoneData *nta)
11762 mDNS_StopQuery_internal(m, &nta->question);
11769 mDNSexport mStatus mDNS_StopQuery_internal(mDNS *const m, DNSQuestion *const question)
11772 CacheGroup *cg = CacheGroupForName(m, slot, question->qnamehash, &question->qname);
11774 DNSQuestion **qp = &m->Questions;
11778 if (question->InterfaceID == mDNSInterface_LocalOnly || question->InterfaceID == mDNSInterface_P2P) qp = &m->LocalOnlyQuestions;
11795 UpdateQuestionDuplicates(m, question);
11808 for (q = m->Questions; q; q=q->next) // Scan our list of questions
11813 "CurrentAnswers %d, SuppressQuery %d", q, CRDisplayString(m,rr), question->CurrentAnswers, q->CurrentAnswers, q->SuppressQuery);
11815 if (!q) m->rrcache_active--; // If no longer active, decrement rrcache_active count
11821 if (m->CurrentQuestion == question)
11825 m->CurrentQuestion = question->next;
11828 if (m->NewQuestions == question)
11832 m->NewQuestions = question->next;
11835 if (m->NewLocalOnlyQuestions == question) m->NewLocalOnlyQuestions = question->next;
11837 if (m->RestartQuestion == question)
11841 m->RestartQuestion = question->next;
11844 if (m->ValidationQuestion == question)
11848 m->ValidationQuestion = question->next;
11851 // Take care not to trash question->next until *after* we've updated m->CurrentQuestion and m->NewQuestions
11859 // invalid before we even use it. By making sure that we update m->CurrentQuestion and m->NewQuestions if necessary
11867 for (q = m->Questions; q; q=q->next)
11871 if (!m->LLQNAT.clientCallback) // Should never happen, but just in case...
11878 mDNS_StopNATOperation_internal(m, &m->LLQNAT);
11879 m->LLQNAT.clientCallback = mDNSNULL; // Means LLQ NAT Traversal not running
11887 sendLLQRefresh(m, question);
11899 UpdateAutoTunnelDomainStatuses(m);
11903 if (question->nta) { CancelGetZoneData(m, question->nta); question->nta = mDNSNULL; }
11908 question->DAIFreeCallback(m, question->DNSSECAuthInfo);
11920 mDNSexport mStatus mDNS_StartQuery(mDNS *const m, DNSQuestion *const question)
11923 mDNS_Lock(m);
11924 status = mDNS_StartQuery_internal(m, question);
11925 mDNS_Unlock(m);
11929 mDNSexport mStatus mDNS_StopQuery(mDNS *const m, DNSQuestion *const question)
11932 mDNS_Lock(m);
11933 status = mDNS_StopQuery_internal(m, question);
11934 mDNS_Unlock(m);
11942 mDNSexport mStatus mDNS_StopQueryWithRemoves(mDNS *const m, DNSQuestion *const question)
11946 mDNS_Lock(m);
11949 for (qq = m->NewQuestions; qq; qq=qq->next) if (qq == question) break;
11951 status = mDNS_StopQuery_internal(m, question);
11956 CacheGroup *const cg = CacheGroupForName(m, slot, question->qnamehash, &question->qname);
11963 question->QuestionCallback(m, question, &rr->resrec, QC_rmv);
11966 mDNS_Unlock(m);
11970 mDNSexport mStatus mDNS_Reconfirm(mDNS *const m, CacheRecord *const cr)
11973 mDNS_Lock(m);
11974 status = mDNS_Reconfirm_internal(m, cr, kDefaultReconfirmTimeForNoAnswer);
11975 if (status == mStatus_NoError) ReconfirmAntecedents(m, cr->resrec.name, cr->resrec.namehash, 0);
11976 mDNS_Unlock(m);
11980 mDNSexport mStatus mDNS_ReconfirmByValue(mDNS *const m, ResourceRecord *const rr)
11984 mDNS_Lock(m);
11985 cr = FindIdenticalRecordInCache(m, rr);
11986 debugf("mDNS_ReconfirmByValue: %p %s", cr, RRDisplayString(m, rr));
11987 if (cr) status = mDNS_Reconfirm_internal(m, cr, kDefaultReconfirmTimeForNoAnswer);
11988 if (status == mStatus_NoError) ReconfirmAntecedents(m, cr->resrec.name, cr->resrec.namehash, 0);
11989 mDNS_Unlock(m);
11993 mDNSlocal mStatus mDNS_StartBrowse_internal(mDNS *const m, DNSQuestion *const question,
12035 return(mDNS_StartQuery_internal(m, question));
12038 mDNSexport mStatus mDNS_StartBrowse(mDNS *const m, DNSQuestion *const question,
12045 mDNS_Lock(m);
12046 status = mDNS_StartBrowse_internal(m, question, srv, domain, anondata, InterfaceID, flags, ForceMCast, useBackgroundTrafficClass, Callback, Context);
12047 mDNS_Unlock(m);
12051 mDNSlocal mDNSBool MachineHasActiveIPv6(mDNS *const m)
12054 for (intf = m->HostInterfaces; intf; intf = intf->next)
12059 mDNSlocal void FoundServiceInfoSRV(mDNS *const m, DNSQuestion *question, const ResourceRecord *const answer, QC_result AddRecord)
12076 mDNS_StartQuery(m, &query->qAv4);
12078 if (MachineHasActiveIPv6(m)) mDNS_StartQuery(m, &query->qAv6);
12084 mDNS_StopQuery(m, &query->qAv4);
12085 if (query->qAv6.ThisQInterval >= 0) mDNS_StopQuery(m, &query->qAv6);
12104 mDNS_StartQuery(m, &query->qAv4);
12106 if (MachineHasActiveIPv6(m)) mDNS_StartQuery(m, &query->qAv6);
12114 query->ServiceInfoQueryCallback(m, query);
12120 mDNSlocal void FoundServiceInfoTXT(mDNS *const m, DNSQuestion *question, const ResourceRecord *const answer, QC_result AddRecord)
12141 query->ServiceInfoQueryCallback(m, query);
12145 mDNSlocal void FoundServiceInfo(mDNS *const m, DNSQuestion *question, const ResourceRecord *const answer, QC_result AddRecord)
12148 //LogInfo("FoundServiceInfo %d %s", AddRecord, RRDisplayString(m, answer));
12181 query->ServiceInfoQueryCallback(m, query);
12189 mDNSexport mStatus mDNS_StartResolveService(mDNS *const m,
12193 mDNS_Lock(m);
12322 status = mDNS_StartQuery_internal(m, &query->qSRV);
12323 if (status == mStatus_NoError) status = mDNS_StartQuery_internal(m, &query->qTXT);
12324 if (status != mStatus_NoError) mDNS_StopResolveService(m, query);
12326 mDNS_Unlock(m);
12330 mDNSexport void mDNS_StopResolveService (mDNS *const m, ServiceInfoQuery *q)
12332 mDNS_Lock(m);
12334 if (q->qSRV.ThisQInterval >= 0) mDNS_StopQuery_internal(m, &q->qSRV);
12335 if (q->qTXT.ThisQInterval >= 0) mDNS_StopQuery_internal(m, &q->qTXT);
12336 if (q->qAv4.ThisQInterval >= 0) mDNS_StopQuery_internal(m, &q->qAv4);
12337 if (q->qAv6.ThisQInterval >= 0) mDNS_StopQuery_internal(m, &q->qAv6);
12338 mDNS_Unlock(m);
12341 mDNSexport mStatus mDNS_GetDomains(mDNS *const m, DNSQuestion *const question, mDNS_DomainType DomainType, const domainname *dom,
12374 return(mDNS_StartQuery(m, question));
12383 mDNSexport mStatus mDNS_Register(mDNS *const m, AuthRecord *const rr)
12386 mDNS_Lock(m);
12387 status = mDNS_Register_internal(m, rr);
12388 mDNS_Unlock(m);
12392 mDNSexport mStatus mDNS_Update(mDNS *const m, AuthRecord *const rr, mDNSu32 newttl,
12397 LogMsg("Attempt to update record with invalid rdata: %s", GetRRDisplayString_rdb(&rr->resrec, &newrdata->u, m->MsgBuffer));
12401 mDNS_Lock(m);
12412 rr->UpdateCallback(m, rr, n, rr->newrdlength); // ...and let the client free this memory, if necessary
12422 mStatus status = uDNS_UpdateRecord(m, rr);
12425 mDNS_Unlock(m);
12432 CompleteRDataUpdate(m, rr);
12436 InitializeLastAPTime(m, rr);
12437 while (rr->NextUpdateCredit && m->timenow - rr->NextUpdateCredit >= 0) GrantUpdateCredit(rr);
12439 if (!rr->NextUpdateCredit) rr->NextUpdateCredit = NonZeroTime(m->timenow + kUpdateCreditRefreshInterval);
12444 if (!rr->UpdateBlocked) rr->UpdateBlocked = NonZeroTime(m->timenow + (mDNSs32)delay * mDNSPlatformOneSecond);
12453 mDNS_Unlock(m);
12460 mDNSexport mStatus mDNS_Deregister(mDNS *const m, AuthRecord *const rr)
12463 mDNS_Lock(m);
12464 status = mDNS_Deregister_internal(m, rr, mDNS_Dereg_normal);
12465 mDNS_Unlock(m);
12470 mDNSlocal void mDNS_HostNameCallback(mDNS *const m, AuthRecord *const rr, mStatus result);
12472 mDNSlocal NetworkInterfaceInfo *FindFirstAdvertisedInterface(mDNS *const m)
12475 for (intf = m->HostInterfaces; intf; intf = intf->next)
12480 mDNSlocal void AdvertiseInterface(mDNS *const m, NetworkInterfaceInfo *set)
12491 if (!m->AutoTargetServices)
12498 primary = FindFirstAdvertisedInterface(m);
12520 AssignDomainName(&set->RR_A.namestorage, &m->MulticastHostname);
12551 mDNS_Register_internal(m, &set->RR_A);
12552 mDNS_Register_internal(m, &set->RR_PTR);
12559 if (!NO_HINFO && m->HIHardware.c[0] > 0 && m->HISoftware.c[0] > 0 && m->HIHardware.c[0] + m->HISoftware.c[0] <= 254)
12562 AssignDomainName(&set->RR_HINFO.namestorage, &m->MulticastHostname);
12564 mDNSPlatformMemCopy(p, &m->HIHardware, 1 + (mDNSu32)m->HIHardware.c[0]);
12566 mDNSPlatformMemCopy(p, &m->HISoftware, 1 + (mDNSu32)m->HISoftware.c[0]);
12567 mDNS_Register_internal(m, &set->RR_HINFO);
12576 mDNSlocal void DeadvertiseInterface(mDNS *const m, NetworkInterfaceInfo *set)
12581 NetworkInterfaceInfo *primary = FindFirstAdvertisedInterface(m);
12583 for (intf = m->HostInterfaces; intf; intf = intf->next)
12592 if (set->RR_A.resrec.RecordType) mDNS_Deregister_internal(m, &set->RR_A, mDNS_Dereg_normal);
12593 if (set->RR_PTR.resrec.RecordType) mDNS_Deregister_internal(m, &set->RR_PTR, mDNS_Dereg_normal);
12594 if (set->RR_HINFO.resrec.RecordType) mDNS_Deregister_internal(m, &set->RR_HINFO, mDNS_Dereg_normal);
12602 mDNSlocal void AdvertiseAllInterfaceRecords(mDNS *const m)
12605 for (intf = m->HostInterfaces; intf; intf = intf->next)
12610 AdvertiseInterface(m, intf);
12615 mDNSlocal void DeadvertiseAllInterfaceRecords(mDNS *const m)
12619 for (intf = m->HostInterfaces; intf; intf = intf->next)
12624 DeadvertiseInterface(m, intf);
12628 (void) m; //unused
12632 mDNSexport void mDNS_SetFQDN(mDNS *const m)
12639 if (!AppendDomainLabel(&newmname, &m->hostlabel)) { LogMsg("ERROR: mDNS_SetFQDN: Cannot create MulticastHostname"); return; }
12642 mDNS_Lock(m);
12644 if (SameDomainNameCS(&m->MulticastHostname, &newmname)) debugf("mDNS_SetFQDN - hostname unchanged");
12647 AssignDomainName(&m->MulticastHostname, &newmname);
12650 for (intf = m->HostInterfaces; intf; intf = intf->next)
12651 if (intf->Advertise) DeadvertiseInterface(m, intf);
12654 for (intf = m->HostInterfaces; intf; intf = intf->next)
12655 if (intf->Advertise) AdvertiseInterface(m, intf);
12659 for (rr = m->ResourceRecords; rr; rr=rr->next) if (rr->AutoTarget) SetTargetToHostName(m, rr);
12660 for (rr = m->DuplicateRecords; rr; rr=rr->next) if (rr->AutoTarget) SetTargetToHostName(m, rr);
12662 mDNS_Unlock(m);
12665 mDNSlocal void mDNS_HostNameCallback(mDNS *const m, AuthRecord *const rr, mStatus result)
12681 if (m->MainCallback)
12682 m->MainCallback(m, mStatus_NoError);
12686 domainlabel oldlabel = m->hostlabel;
12689 if (m->MainCallback)
12690 m->MainCallback(m, mStatus_NameConflict);
12695 if (SameDomainLabel(m->hostlabel.c, oldlabel.c))
12696 IncrementLabelSuffix(&m->hostlabel, mDNSfalse);
12700 mDNS_SetFQDN(m);
12701 LogMsg("Local Hostname %#s.local already in use; will try %#s.local instead", oldlabel.c, m->hostlabel.c);
12713 mDNSlocal void UpdateInterfaceProtocols(mDNS *const m, NetworkInterfaceInfo *active)
12718 for (intf = m->HostInterfaces; intf; intf = intf->next)
12726 mDNSlocal void RestartRecordGetZoneData(mDNS * const m)
12730 for (rr = m->ResourceRecords; rr; rr=rr->next)
12736 if (rr->nta) { rr->updateid = zeroID; CancelGetZoneData(m, rr->nta); }
12737 rr->nta = StartGetZoneData(m, rr->resrec.name, ZoneServiceUpdate, RecordRegistrationGotZoneData, rr);
12741 mDNSlocal void InitializeNetWakeState(mDNS *const m, NetworkInterfaceInfo *set)
12757 set->NextSPSAttemptTime = m->timenow;
12760 mDNSexport void mDNS_ActivateNetWake_internal(mDNS *const m, NetworkInterfaceInfo *set)
12762 NetworkInterfaceInfo *p = m->HostInterfaces;
12769 mDNS_StartBrowse_internal(m, &set->NetWakeBrowse, &SleepProxyServiceType, &localdomain, mDNSNULL, set->InterfaceID, 0, mDNSfalse, mDNSfalse, m->SPSBrowseCallback, set);
12773 mDNSexport void mDNS_DeactivateNetWake_internal(mDNS *const m, NetworkInterfaceInfo *set)
12775 NetworkInterfaceInfo *p = m->HostInterfaces;
12789 mDNS_StopQuery_internal(m, &set->NetWakeBrowse);
12790 for (i=0; i<3; i++) if (set->NetWakeResolve[i].ThisQInterval >= 0) mDNS_StopQuery_internal(m, &set->NetWakeResolve[i]);
12793 if (m->SPSBrowseCallback)
12796 m->SPSBrowseCallback(m, &set->NetWakeBrowse, mDNSNULL, QC_rmv);
12802 InitializeNetWakeState(m, set);
12806 mDNSexport mStatus mDNS_RegisterInterface(mDNS *const m, NetworkInterfaceInfo *set, mDNSBool flapping)
12810 NetworkInterfaceInfo **p = &m->HostInterfaces;
12818 mDNS_Lock(m);
12825 InitializeNetWakeState(m, set);
12833 mDNS_Unlock(m);
12853 AdvertiseInterface(m, set);
12861 if (set->NetWake) mDNS_ActivateNetWake_internal(m, set);
12885 // We must *never* set m->SuppressSending to more than that (or set it repeatedly in a way
12889 // See <rdar://problem/4073853> mDNS: m->SuppressSending set too enthusiastically
12890 if (!m->SuppressSending) m->SuppressSending = m->timenow + (mDNSs32)mDNSRandom((mDNSu32)InitialQuestionInterval);
12895 m->mDNSStats.InterfaceUpFlap++;
12899 if (m->SuppressProbes == 0 ||
12900 m->SuppressProbes - NonZeroTime(m->timenow + probedelay) < 0)
12901 m->SuppressProbes = NonZeroTime(m->timenow + probedelay);
12907 m->AnnounceOwner = NonZeroTime(m->timenow + 60 * mDNSPlatformOneSecond);
12910 m->mDNSStats.InterfaceUp++;
12911 for (q = m->Questions; q; q=q->next) // Scan our list of questions
12934 q->LastQTime = m->timenow - q->ThisQInterval + qdelay;
12938 SetNextQueryTime(m,q);
12945 for (rr = m->ResourceRecords; rr; rr=rr->next)
12951 mDNSCoreRestartRegistration(m, rr, numannounce);
12955 DNSSECProbe(m);
12959 RestartRecordGetZoneData(m);
12961 mDNS_UpdateAllowSleep(m);
12963 mDNS_Unlock(m);
12970 mDNSexport void mDNS_DeregisterInterface(mDNS *const m, NetworkInterfaceInfo *set, mDNSBool flapping)
12972 NetworkInterfaceInfo **p = &m->HostInterfaces;
12975 mDNS_Lock(m);
12979 if (!*p) { debugf("mDNS_DeregisterInterface: NetworkInterfaceInfo not found in list"); mDNS_Unlock(m); return; }
12981 mDNS_DeactivateNetWake_internal(m, set);
12991 for (intf = m->HostInterfaces; intf; intf = intf->next)
12993 UpdateInterfaceProtocols(m, intf);
12997 NetworkInterfaceInfo *intf = FirstInterfaceForID(m, set->InterfaceID);
13005 UpdateInterfaceProtocols(m, intf);
13007 if (intf->NetWake) mDNS_ActivateNetWake_internal(m, intf);
13011 for (intf = m->HostInterfaces; intf; intf = intf->next)
13026 m->mDNSStats.InterfaceDown++;
13031 m->mDNSStats.InterfaceDownFlap++;
13036 for (q = m->Questions; q; q=q->next)
13057 mDNS_Reconfirm_internal(m, rr, kDefaultReconfirmTimeForFlappingInterface);
13064 mDNS_PurgeCacheResourceRecord(m, rr);
13072 if (set->Advertise) DeadvertiseInterface(m, set);
13078 if (revalidate && !m->ShutdownTime)
13085 mDNS_Reconfirm_internal(m, rr, kDefaultReconfirmTimeForFlappingInterface);
13088 mDNS_UpdateAllowSleep(m);
13090 mDNS_Unlock(m);
13139 mDNSlocal void ServiceCallback(mDNS *const m, AuthRecord *const rr, mStatus result)
13142 (void)m; // Unused parameter
13161 mDNS_DeregisterService(m, sr); // Unlink the records from our list
13196 sr->ServiceCallback(m, sr, result);
13199 mDNSlocal void NSSCallback(mDNS *const m, AuthRecord *const rr, mStatus result)
13203 sr->ServiceCallback(m, sr, result);
13235 // then the default host name (m->MulticastHostname) is automatically used
13237 mDNSexport mStatus mDNS_RegisterService(mDNS *const m, ServiceRecordSet *sr,
13280 return(mDNS_RegisterNoSuchService(m, &sr->RR_SRV, name, type, domain, mDNSNULL, InterfaceID, NSSCallback, sr, flags));
13345 mDNS_Lock(m);
13349 err = mDNS_Register_internal(m, &sr->RR_SRV);
13355 mDNS_Unlock(m);
13358 if (!err) err = mDNS_Register_internal(m, &sr->RR_TXT);
13364 if (!err) err = mDNS_Register_internal(m, &sr->RR_ADV);
13365 for (i=0; i<NumSubTypes; i++) if (!err) err = mDNS_Register_internal(m, &sr->SubTypes[i]);
13366 if (!err) err = mDNS_Register_internal(m, &sr->RR_PTR);
13368 mDNS_Unlock(m);
13370 if (err) mDNS_DeregisterService(m, sr);
13374 mDNSexport mStatus mDNS_AddRecordToService(mDNS *const m, ServiceRecordSet *sr,
13389 mDNS_Lock(m);
13400 status = mDNS_Register_internal(m, &extra->r);
13403 mDNS_Unlock(m);
13407 mDNSexport mStatus mDNS_RemoveRecordFromService(mDNS *const m, ServiceRecordSet *sr, ExtraResourceRecord *extra,
13413 mDNS_Lock(m);
13427 status = mDNS_Deregister_internal(m, &extra->r, mDNS_Dereg_normal);
13429 mDNS_Unlock(m);
13433 mDNSexport mStatus mDNS_RenameAndReregisterService(mDNS *const m, ServiceRecordSet *const sr, const domainlabel *newname)
13435 // Note: Don't need to use mDNS_Lock(m) here, because this code is just using public routines
13455 err = mDNS_RegisterService(m, sr, newname, &type, &domain,
13467 err = mDNS_AddRecordToService(m, sr, e, e->r.resrec.rdata, e->r.resrec.rroriginalttl, 0);
13476 mDNSexport mStatus mDNS_DeregisterService_drt(mDNS *const m, ServiceRecordSet *sr, mDNS_Dereg_type drt)
13479 if (mDNSIPPortIsZero(sr->RR_SRV.resrec.rdata->u.srv.port)) return(mDNS_DeregisterNoSuchService(m, &sr->RR_SRV));
13498 // we end up incrementing m->nicelabel multiple times instead of just once.
13508 mDNS_Lock(m);
13513 mDNS_Deregister_internal(m, &sr->RR_SRV, mDNS_Dereg_repeat);
13514 mDNS_Deregister_internal(m, &sr->RR_TXT, mDNS_Dereg_repeat);
13516 mDNS_Deregister_internal(m, &sr->RR_ADV, drt);
13522 mDNS_Deregister_internal(m, &e->r, mDNS_Dereg_repeat);
13527 mDNS_Deregister_internal(m, &sr->SubTypes[i], drt);
13529 status = mDNS_Deregister_internal(m, &sr->RR_PTR, drt);
13530 mDNS_Unlock(m);
13541 mDNSexport mStatus mDNS_RegisterNoSuchService(mDNS *const m, AuthRecord *const rr,
13557 return(mDNS_Register(m, rr));
13560 mDNSexport mStatus mDNS_AdvertiseDomains(mDNS *const m, AuthRecord *rr,
13574 return(mDNS_Register(m, rr));
13577 mDNSlocal mDNSBool mDNS_IdUsedInResourceRecordsList(mDNS * const m, mDNSOpaque16 id)
13580 for (r = m->ResourceRecords; r; r=r->next) if (mDNSSameOpaque16(id, r->updateid)) return mDNStrue;
13584 mDNSlocal mDNSBool mDNS_IdUsedInQuestionsList(mDNS * const m, mDNSOpaque16 id)
13587 for (q = m->Questions; q; q=q->next) if (mDNSSameOpaque16(id, q->TargetQID)) return mDNStrue;
13591 mDNSexport mDNSOpaque16 mDNS_NewMessageID(mDNS * const m)
13599 if (!mDNS_IdUsedInResourceRecordsList(m, id) && !mDNS_IdUsedInQuestionsList(m, id)) break;
13613 mDNSlocal void RestartARPProbing(mDNS *const m, AuthRecord *const rr)
13635 if (rr->AnnounceCount == InitialAnnounceCount && m->timenow - rr->LastAPTime >= 0)
13636 InitializeLastAPTime(m, rr);
13641 rr->LastAPTime = m->timenow + mDNSPlatformOneSecond * 9; // Send first packet at rr->LastAPTime + rr->ThisAPInterval, i.e. 10 seconds from now
13642 SetNextAnnounceProbeTime(m, rr);
13646 mDNSlocal void mDNSCoreReceiveRawARP(mDNS *const m, const ARP_EthIP *const arp, const mDNSInterfaceID InterfaceID)
13650 NetworkInterfaceInfo *intf = FirstInterfaceForID(m, InterfaceID);
13653 mDNS_Lock(m);
13665 for (rr = m->ResourceRecords; rr; rr=rr->next)
13677 intf->ifname, msg, &arp->sha, &arp->spa, &arp->tpa, &rr->WakeUp.HMAC, &rr->WakeUp.IMAC, ARDisplayString(m, rr));
13681 RestartARPProbing(m, rr);
13683 LogSPS("Reached maximum number of restarts for probing - %s", ARDisplayString(m,rr));
13687 mDNSPlatformSetLocalAddressCacheEntry(m, &rr->AddressProxy, &rr->WakeUp.IMAC, InterfaceID);
13691 SendARP(m, 2, rr, &arp->tpa, &arp->sha, &arp->spa, &arp->sha);
13709 for (rr = m->ResourceRecords; rr; rr=rr->next)
13716 &arp->sha, &arp->spa, &arp->tpa, &rr->WakeUp.HMAC, &rr->WakeUp.IMAC, ARDisplayString(m, rr));
13720 RestartARPProbing(m, rr);
13725 &arp->sha, &arp->spa, &arp->tpa, ARDisplayString(m, rr));
13730 &arp->sha, &arp->spa, &arp->tpa, &rr->WakeUp.HMAC, &rr->WakeUp.IMAC, ARDisplayString(m, rr));
13731 ScheduleWakeup(m, rr->resrec.InterfaceID, &rr->WakeUp.HMAC);
13737 mDNS_Unlock(m);
13756 mDNSlocal void mDNSCoreReceiveRawND(mDNS *const m, const mDNSEthAddr *const sha, const mDNSv6Addr *spa,
13760 NetworkInterfaceInfo *intf = FirstInterfaceForID(m, InterfaceID);
13763 mDNS_Lock(m);
13770 for (rr = m->ResourceRecords; rr; rr=rr->next)
13784 intf->ifname, msg, sha, spa, &ndp->target, &rr->WakeUp.HMAC, &rr->WakeUp.IMAC, ARDisplayString(m, rr));
13788 RestartARPProbing(m, rr);
13790 LogSPS("Reached maximum number of restarts for probing - %s", ARDisplayString(m,rr));
13793 mDNSPlatformSetLocalAddressCacheEntry(m, &rr->AddressProxy, &rr->WakeUp.IMAC, InterfaceID);
13795 SendNDP(m, NDP_Adv, NDP_Solicited, rr, &ndp->target, mDNSNULL, spa, sha);
13797 SendNDP(m, NDP_Adv, 0, rr, &ndp->target, mDNSNULL, &AllHosts_v6, &AllHosts_v6_Eth);
13812 for (rr = m->ResourceRecords; rr; rr=rr->next)
13819 sha, spa, &ndp->target, &rr->WakeUp.HMAC, &rr->WakeUp.IMAC, ARDisplayString(m, rr));
13823 RestartARPProbing(m, rr);
13827 ndp->type == NDP_Sol ? "Solicitation " : "Advertisement", sha, spa, &ndp->target, ARDisplayString(m, rr));
13832 sha, spa, &ndp->target, &rr->WakeUp.HMAC, &rr->WakeUp.IMAC, ARDisplayString(m, rr));
13833 ScheduleWakeup(m, rr->resrec.InterfaceID, &rr->WakeUp.HMAC);
13839 mDNS_Unlock(m);
13842 mDNSlocal void mDNSCoreReceiveRawTransportPacket(mDNS *const m, const mDNSEthAddr *const sha, const mDNSAddr *const src, const mDNSAddr *const dst, const mDNSu8 protocol,
13863 kr = mDNS_MatchKeepaliveInfo(m, dst, src, port, t->tcp.src, &seq, &ack);
13908 mDNS_SendKeepaliveACK(m, kr);
13982 if (!checksum) mDNSCoreReceiveRawND(m, sha, &src->ip.v6, &t->ndp, &t->bytes[len], InterfaceID);
13995 mDNS_Lock(m);
13996 for (rr = m->ResourceRecords; rr; rr=rr->next)
14002 for (r2 = m->ResourceRecords; r2; r2=r2->next)
14013 InterfaceNameForID(m, rr->resrec.InterfaceID), dst, &rr->WakeUp.HMAC, &rr->WakeUp.IMAC, ARDisplayString(m, r2));
14014 ScheduleWakeup(m, rr->resrec.InterfaceID, &rr->WakeUp.HMAC);
14018 InterfaceNameForID(m, rr->resrec.InterfaceID), dst, &rr->WakeUp.HMAC, tp, mDNSVal16(port));
14020 mDNS_Unlock(m);
14024 mDNSexport void mDNSCoreReceiveRawPacket(mDNS *const m, const mDNSu8 *const p, const mDNSu8 *const end, const mDNSInterfaceID InterfaceID)
14043 mDNSCoreReceiveRawARP(m, &pkt->arp, InterfaceID);
14052 mDNSCoreReceiveRawTransportPacket(m, ð->src, &src, &dst, pkt->v4.protocol, p, (TransportLayerPacket*)trans, end, InterfaceID, 0);
14062 mDNSCoreReceiveRawTransportPacket(m, ð->src, &src, &dst, pkt->v6.pro, p, (TransportLayerPacket*)trans, end, InterfaceID,
14067 mDNSlocal void ConstructSleepProxyServerName(mDNS *const m, domainlabel *name)
14070 m->SPSType, m->SPSPortability, m->SPSMarginalPower, m->SPSTotalPower, m->SPSFeatureFlags, &m->nicelabel);
14074 mDNSlocal void SleepProxyServerCallback(mDNS *const m, ServiceRecordSet *const srs, mStatus result)
14077 mDNS_RenameAndReregisterService(m, srs, mDNSNULL);
14080 if (m->SleepState)
14081 m->SPSState = 3;
14084 m->SPSState = (mDNSu8)(m->SPSSocket != mDNSNULL);
14085 if (m->SPSState)
14088 ConstructSleepProxyServerName(m, &name);
14089 mDNS_RegisterService(m, srs,
14091 mDNSNULL, m->SPSSocket->port, // Host, port
14097 LogSPS("Sleep Proxy Server %#s %s", srs->RR_SRV.resrec.name->c, m->SPSState ? "started" : "stopped");
14104 mDNSexport void mDNSCoreBeSleepProxyServer_internal(mDNS *const m, mDNSu8 sps, mDNSu8 port, mDNSu8 marginalpower, mDNSu8 totpower, mDNSu8 features)
14111 if (!sps && m->SPSSocket) { mDNSPlatformUDPClose(m->SPSSocket); m->SPSSocket = mDNSNULL; }
14115 if (m->SPSState == 1 && sps != m->SPSType)
14116 { m->SPSState = 2; mDNS_DeregisterService_drt(m, &m->SPSRecords, sps ? mDNS_Dereg_rapid : mDNS_Dereg_normal); }
14120 m->SPSType = sps;
14121 m->SPSPortability = port;
14122 m->SPSMarginalPower = marginalpower;
14123 m->SPSTotalPower = totpower;
14124 m->SPSFeatureFlags = features;
14128 if (!m->SPSSocket)
14130 m->SPSSocket = mDNSPlatformUDPSocket(m, zeroIPPort);
14131 if (!m->SPSSocket) { LogMsg("mDNSCoreBeSleepProxyServer: Failed to allocate SPSSocket"); goto fail; }
14134 if (m->SPSState == 0) SleepProxyServerCallback(m, &m->SPSRecords, mStatus_MemFree);
14137 else if (m->SPSState)
14139 LogSPS("mDNSCoreBeSleepProxyServer turning off from state %d; will wake clients", m->SPSState);
14140 m->NextScheduledSPS = m->timenow;
14152 mDNSlocal void mDNS_GrowCache_internal(mDNS *const m, CacheEntity *storage, mDNSu32 numrecords)
14159 storage[numrecords-1].next = m->rrcache_free;
14160 m->rrcache_free = storage;
14161 m->rrcache_size += numrecords;
14165 mDNSexport void mDNS_GrowCache(mDNS *const m, CacheEntity *storage, mDNSu32 numrecords)
14167 mDNS_Lock(m);
14168 mDNS_GrowCache_internal(m, storage, numrecords);
14169 mDNS_Unlock(m);
14172 mDNSexport mStatus mDNS_Init(mDNS *const m, mDNS_PlatformSupport *const p,
14182 m->p = p;
14183 m->CanReceiveUnicastOn5353 = mDNSfalse; // Assume we can't receive unicasts on 5353, unless platform layer tells us otherwise
14184 m->AdvertiseLocalAddresses = AdvertiseLocalAddresses;
14185 m->DivertMulticastAdvertisements = mDNSfalse;
14186 m->mDNSPlatformStatus = mStatus_Waiting;
14187 m->UnicastPort4 = zeroIPPort;
14188 m->UnicastPort6 = zeroIPPort;
14189 m->PrimaryMAC = zeroEthAddr;
14190 m->MainCallback = Callback;
14191 m->MainContext = Context;
14192 m->rec.r.resrec.RecordType = 0;
14193 m->rec.r.resrec.AnonInfo = mDNSNULL;
14196 m->mDNS_busy = 0;
14197 m->mDNS_reentrancy = 0;
14198 m->ShutdownTime = 0;
14199 m->lock_rrcache = 0;
14200 m->lock_Questions = 0;
14201 m->lock_Records = 0;
14206 m->timenow_adjust = (mDNSs32)mDNSRandom(0xFFFFFFFF);
14207 timenow = mDNS_TimeNow_NoLock(m);
14209 m->timenow = 0; // MUST only be set within mDNS_Lock/mDNS_Unlock section
14210 m->timenow_last = timenow;
14211 m->NextScheduledEvent = timenow;
14212 m->SuppressSending = timenow;
14213 m->NextCacheCheck = timenow + 0x78000000;
14214 m->NextScheduledQuery = timenow + 0x78000000;
14215 m->NextScheduledProbe = timenow + 0x78000000;
14216 m->NextScheduledResponse = timenow + 0x78000000;
14217 m->NextScheduledNATOp = timenow + 0x78000000;
14218 m->NextScheduledSPS = timenow + 0x78000000;
14219 m->NextScheduledKA = timenow + 0x78000000;
14220 m->NextScheduledStopTime = timenow + 0x78000000;
14221 m->RandomQueryDelay = 0;
14222 m->RandomReconfirmDelay = 0;
14223 m->PktNum = 0;
14224 m->MPktNum = 0;
14225 m->LocalRemoveEvents = mDNSfalse;
14226 m->SleepState = SleepState_Awake;
14227 m->SleepSeqNum = 0;
14228 m->SystemWakeOnLANEnabled = mDNSfalse;
14229 m->AnnounceOwner = NonZeroTime(timenow + 60 * mDNSPlatformOneSecond);
14230 m->DelaySleep = 0;
14231 m->SleepLimit = 0;
14234 m->StatStartTime = mDNSPlatformUTC();
14235 m->NextStatLogTime = m->StatStartTime + kDefaultNextStatsticsLogTime;
14236 m->ActiveStatTime = 0;
14237 m->UnicastPacketsSent = 0;
14238 m->MulticastPacketsSent = 0;
14239 m->RemoteSubnet = 0;
14243 m->Questions = mDNSNULL;
14244 m->NewQuestions = mDNSNULL;
14245 m->CurrentQuestion = mDNSNULL;
14246 m->LocalOnlyQuestions = mDNSNULL;
14247 m->NewLocalOnlyQuestions = mDNSNULL;
14248 m->RestartQuestion = mDNSNULL;
14249 m->ValidationQuestion = mDNSNULL;
14250 m->rrcache_size = 0;
14251 m->rrcache_totalused = 0;
14252 m->rrcache_active = 0;
14253 m->rrcache_report = 10;
14254 m->rrcache_free = mDNSNULL;
14258 m->rrcache_hash[slot] = mDNSNULL;
14259 m->rrcache_nextcheck[slot] = timenow + 0x78000000;;
14262 mDNS_GrowCache_internal(m, rrcachestorage, rrcachesize);
14263 m->rrauth.rrauth_free = mDNSNULL;
14266 m->rrauth.rrauth_hash[slot] = mDNSNULL;
14269 m->hostlabel.c[0] = 0;
14270 m->nicelabel.c[0] = 0;
14271 m->MulticastHostname.c[0] = 0;
14272 m->HIHardware.c[0] = 0;
14273 m->HISoftware.c[0] = 0;
14274 m->ResourceRecords = mDNSNULL;
14275 m->DuplicateRecords = mDNSNULL;
14276 m->NewLocalRecords = mDNSNULL;
14277 m->NewLocalOnlyRecords = mDNSfalse;
14278 m->CurrentRecord = mDNSNULL;
14279 m->HostInterfaces = mDNSNULL;
14280 m->ProbeFailTime = 0;
14281 m->NumFailedProbes = 0;
14282 m->SuppressProbes = 0;
14285 m->NextuDNSEvent = timenow + 0x78000000;
14286 m->NextSRVUpdate = timenow + 0x78000000;
14288 m->DNSServers = mDNSNULL;
14290 m->Router = zeroAddr;
14291 m->AdvertisedV4 = zeroAddr;
14292 m->AdvertisedV6 = zeroAddr;
14294 m->AuthInfoList = mDNSNULL;
14296 m->ReverseMap.ThisQInterval = -1;
14297 m->StaticHostname.c[0] = 0;
14298 m->FQDN.c[0] = 0;
14299 m->Hostnames = mDNSNULL;
14300 m->AutoTunnelNAT.clientContext = mDNSNULL;
14302 m->WABBrowseQueriesCount = 0;
14303 m->WABLBrowseQueriesCount = 0;
14304 m->WABRegQueriesCount = 0;
14306 m->mDNSOppCaching = mDNStrue;
14308 m->mDNSOppCaching = mDNSfalse;
14310 m->AutoTargetServices = 0;
14313 m->LLQNAT.clientCallback = mDNSNULL;
14314 m->LLQNAT.clientContext = mDNSNULL;
14315 m->NATTraversals = mDNSNULL;
14316 m->CurrentNATTraversal = mDNSNULL;
14317 m->retryIntervalGetAddr = 0; // delta between time sent and retry
14318 m->retryGetAddr = timenow + 0x78000000; // absolute time when we retry
14319 m->ExtAddress = zerov4Addr;
14320 m->PCPNonce[0] = mDNSRandom(-1);
14321 m->PCPNonce[1] = mDNSRandom(-1);
14322 m->PCPNonce[2] = mDNSRandom(-1);
14324 m->NATMcastRecvskt = mDNSNULL;
14325 m->LastNATupseconds = 0;
14326 m->LastNATReplyLocalTime = timenow;
14327 m->LastNATMapResultCode = NATErr_None;
14329 m->UPnPInterfaceID = 0;
14330 m->SSDPSocket = mDNSNULL;
14331 m->SSDPWANPPPConnection = mDNSfalse;
14332 m->UPnPRouterPort = zeroIPPort;
14333 m->UPnPSOAPPort = zeroIPPort;
14334 m->UPnPRouterURL = mDNSNULL;
14335 m->UPnPWANPPPConnection = mDNSfalse;
14336 m->UPnPSOAPURL = mDNSNULL;
14337 m->UPnPRouterAddressString = mDNSNULL;
14338 m->UPnPSOAPAddressString = mDNSNULL;
14339 m->SPSType = 0;
14340 m->SPSPortability = 0;
14341 m->SPSMarginalPower = 0;
14342 m->SPSTotalPower = 0;
14343 m->SPSFeatureFlags = 0;
14344 m->SPSState = 0;
14345 m->SPSProxyListChanged = mDNSNULL;
14346 m->SPSSocket = mDNSNULL;
14347 m->SPSBrowseCallback = mDNSNULL;
14348 m->ProxyRecords = 0;
14353 m->TunnelClients = mDNSNULL;
14358 m->WCF = WCFConnectionNew();
14359 if (!m->WCF) { LogMsg("WCFConnectionNew failed"); return -1; }
14365 result = mDNSPlatformInit(m);
14370 uDNS_SetupDNSConfig(m); // Get initial DNS configuration
14376 mDNSexport void mDNS_ConfigChanged(mDNS *const m)
14378 if (m->SPSState == 1)
14383 DeconstructServiceName(m->SPSRecords.RR_SRV.resrec.name, &name, &type, &domain);
14385 ConstructSleepProxyServerName(m, &newname);
14391 m->SPSState = 2;
14393 mDNS_DeregisterService_drt(m, &m->SPSRecords, mDNS_Dereg_rapid);
14398 if (m->MainCallback)
14399 m->MainCallback(m, mStatus_ConfigChanged);
14402 mDNSlocal void DynDNSHostNameCallback(mDNS *const m, AuthRecord *const rr, mStatus result)
14404 (void)m; // unused
14409 mDNSlocal void PurgeOrReconfirmCacheRecord(mDNS *const m, CacheRecord *cr, const DNSServer * const ptr, mDNSBool lameduck)
14421 ptr, &ptr->addr, mDNSVal16(ptr->port), ptr->domain.c, CRDisplayString(m, cr));
14425 LogInfo("PurgeorReconfirmCacheRecord: Purging Resourcerecord %s, RecordType %x", CRDisplayString(m, cr), cr->resrec.RecordType);
14426 mDNS_PurgeCacheResourceRecord(m, cr);
14430 LogInfo("PurgeorReconfirmCacheRecord: Reconfirming Resourcerecord %s, RecordType %x", CRDisplayString(m, cr), cr->resrec.RecordType);
14431 mDNS_Reconfirm_internal(m, cr, kDefaultReconfirmTimeForNoAnswer);
14435 mDNSlocal void mDNS_PurgeForQuestion(mDNS *const m, DNSQuestion *q)
14438 CacheGroup *const cg = CacheGroupForName(m, slot, q->qnamehash, &q->qname);
14452 LogInfo("mDNS_PurgeForQuestion: Flushing %s", CRDisplayString(m, rp));
14453 mDNS_PurgeCacheResourceRecord(m, rp);
14471 mDNSlocal void CheckForDNSSECRecords(mDNS *const m, DNSQuestion *q)
14474 CacheGroup *const cg = CacheGroupForName(m, slot, q->qnamehash, &q->qname);
14485 LogInfo("CheckForDNSSECRecords: Flushing %s", CRDisplayString(m, rp));
14486 mDNS_PurgeCacheResourceRecord(m, rp);
14494 mDNSexport mDNSBool mDNS_CheckForCacheRecord(mDNS *const m, DNSQuestion *q, mDNSu16 qtype)
14498 CacheGroup *const cg = CacheGroupForName(m, slot, q->qnamehash, &q->qname);
14510 LogInfo("mDNS_CheckForCacheRecord: Found %s", CRDisplayString(m, rp));
14517 mDNSexport void DNSServerChangeForQuestion(mDNS *const m, DNSQuestion *q, DNSServer *new)
14521 (void) m;
14536 mDNSlocal void SetConfigState(mDNS *const m, mDNSBool delete)
14543 for (ptr = m->DNSServers; ptr; ptr = ptr->next)
14552 for (mr = m->McastResolvers; mr; mr = mr->next)
14557 for (ptr = m->DNSServers; ptr; ptr = ptr->next)
14563 for (mr = m->McastResolvers; mr; mr = mr->next)
14568 mDNSexport mStatus uDNS_SetupDNSConfig(mDNS *const m)
14576 DNSServer *ptr, **p = &m->DNSServers;
14577 const DNSServer *oldServers = m->DNSServers;
14579 McastResolver *mr, **mres = &m->McastResolvers;
14584 uDNS_SetupWABQueries(m);
14586 mDNS_Lock(m);
14595 SetConfigState(m, mDNStrue);
14596 if (!mDNSPlatformSetDNSConfig(m, mDNStrue, mDNSfalse, &fqdn, mDNSNULL, mDNSNULL, mDNStrue))
14598 SetConfigState(m, mDNSfalse);
14599 mDNS_Unlock(m);
14651 for (q = m->Questions; q; q=q->next)
14658 SetValidDNSServers(m, q);
14660 s = GetServerForQuestion(m, q);
14682 new = ShouldSuppressUnicastQuery(m, q, s);
14700 DNSServerChangeForQuestion(m, q, s);
14710 q->TargetQID = mDNS_NewMessageID(m);
14715 ActivateUnicastQuery(m, q, mDNStrue);
14720 if (qptr->DuplicateOf == q) ActivateUnicastQuery(m, qptr, mDNStrue);
14735 RestartUnicastQuestions(m);
14759 ptr = GetServerForName(m, cr->resrec.name,
14773 LogInfo("uDNS_SetupDNSConfig: Purging Resourcerecord %s, New DNS server %#a , Old DNS server %#a", CRDisplayString(m, cr),
14775 mDNS_PurgeCacheResourceRecord(m, cr);
14779 LogInfo("uDNS_SetupDNSConfig: Purging/Reconfirming Resourcerecord %s, New DNS server %#a, Old DNS server %#a", CRDisplayString(m, cr),
14781 PurgeOrReconfirmCacheRecord(m, cr, ptr, mDNSfalse);
14815 " to be freed", CRDisplayString(m, cr), qptr->qname.c, DNSTypeName(qptr->qtype), qptr->InterfaceID, &ptr->addr);
14823 " resetting to question's DNSServer Address %#a", CRDisplayString(m, cr), qptr->qname.c, DNSTypeName(qptr->qtype),
14835 PurgeOrReconfirmCacheRecord(m, cr, ptr, mDNStrue);
14853 if ((m->DNSServers != mDNSNULL) != (oldServers != mDNSNULL))
14860 mDNS_PurgeCacheResourceRecord(m, cr);
14865 m->DNSServers ? "DNS server became" : "No DNS servers", count);
14868 RestartRecordGetZoneData(m);
14872 if (!SameDomainName(&fqdn, &m->FQDN))
14874 if (m->FQDN.c[0]) mDNS_RemoveDynDNSHostName(m, &m->FQDN);
14876 AssignDomainName(&m->FQDN, &fqdn);
14878 if (m->FQDN.c[0])
14880 mDNSPlatformDynDNSHostNameStatusChanged(&m->FQDN, 1);
14881 mDNS_AddDynDNSHostName(m, &m->FQDN, DynDNSHostNameCallback, mDNSNULL);
14885 mDNS_Unlock(m);
14891 if (mDNSPlatformGetPrimaryInterface(m, &v4, &v6, &r) == mStatus_NoError && !mDNSv4AddressIsLinkLocal(&v4.ip.v4))
14893 mDNS_SetPrimaryInterfaceInfo(m,
14900 mDNS_SetPrimaryInterfaceInfo(m, mDNSNULL, mDNSNULL, mDNSNULL);
14901 if (m->FQDN.c[0]) mDNSPlatformDynDNSHostNameStatusChanged(&m->FQDN, 1); // Set status to 1 to indicate temporary failure
14908 mDNSexport void mDNSCoreInitComplete(mDNS *const m, mStatus result)
14910 m->mDNSPlatformStatus = result;
14911 if (m->MainCallback)
14913 mDNS_Lock(m);
14915 m->MainCallback(m, mStatus_NoError);
14917 mDNS_Unlock(m);
14921 mDNSlocal void DeregLoop(mDNS *const m, AuthRecord *const start)
14923 m->CurrentRecord = start;
14924 while (m->CurrentRecord)
14926 AuthRecord *rr = m->CurrentRecord;
14929 rr, rr->resrec.RecordType, ARDisplayString(m, rr));
14931 mDNS_Deregister_internal(m, rr, mDNS_Dereg_rapid);
14935 rr->LastAPTime = m->timenow - rr->ThisAPInterval;
14937 // Mustn't advance m->CurrentRecord until *after* mDNS_Deregister_internal, because
14939 if (m->CurrentRecord == rr) // If m->CurrentRecord was not advanced for us, do it now
14940 m->CurrentRecord = rr->next;
14944 mDNSexport void mDNS_StartExit(mDNS *const m)
14949 mDNS_Lock(m);
14952 m->ShutdownTime = NonZeroTime(m->timenow + mDNSPlatformOneSecond * 5);
14954 mDNSCoreBeSleepProxyServer_internal(m, 0, 0, 0, 0, 0);
14960 if (m->WCF) WCFConnectionDealloc((WCFConnection *)m->WCF);
14968 SuspendLLQs(m);
14971 while (m->Hostnames) mDNS_RemoveDynDNSHostName(m, &m->Hostnames->fqdn);
14981 mDNS_Deregister_internal(m, &dereg->ar, mDNS_Dereg_normal); // Memory will be freed in the FreeARElemCallback
14986 for (intf = m->HostInterfaces; intf; intf = intf->next)
14988 DeadvertiseInterface(m, intf);
14991 while (m->NATTraversals)
14993 NATTraversalInfo *t = m->NATTraversals;
14994 mDNS_StopNATOperation_internal(m, t); // This will cut 't' from the list, thereby advancing m->NATTraversals in the process
15011 if (m->CurrentRecord)
15012 LogMsg("mDNS_StartExit: ERROR m->CurrentRecord already set %s", ARDisplayString(m, m->CurrentRecord));
15019 DeregLoop(m, m->DuplicateRecords);
15021 DeregLoop(m, m->ResourceRecords);
15025 if (m->NextScheduledResponse - m->timenow < mDNSPlatformOneSecond)
15027 m->NextScheduledResponse = m->timenow;
15028 m->SuppressSending = 0;
15031 if (m->ResourceRecords) LogInfo("mDNS_StartExit: Sending final record deregistrations");
15034 for (rr = m->DuplicateRecords; rr; rr = rr->next)
15035 LogMsg("mDNS_StartExit: Should not still have Duplicate Records remaining: %02X %s", rr->resrec.RecordType, ARDisplayString(m, rr));
15038 if (m->mDNSPlatformStatus != mStatus_NoError) DiscardDeregistrations(m);
15040 mDNS_Unlock(m);
15045 mDNSexport void mDNS_FinalExit(mDNS *const m)
15052 mDNSPlatformClose(m);
15056 while (m->rrcache_hash[slot])
15058 CacheGroup *cg = m->rrcache_hash[slot];
15064 ReleaseCacheRecord(m, cr);
15067 ReleaseCacheGroup(m, &m->rrcache_hash[slot]);
15070 debugf("mDNS_FinalExit: RR Cache was using %ld records, %lu active", m->rrcache_totalused, rrcache_active);
15071 if (rrcache_active != m->rrcache_active)
15072 LogMsg("*** ERROR *** rrcache_active %lu != m->rrcache_active %lu", rrcache_active, m->rrcache_active);
15074 for (rr = m->ResourceRecords; rr; rr = rr->next)
15075 LogMsg("mDNS_FinalExit failed to send goodbye for: %p %02X %s", rr, rr->resrec.RecordType, ARDisplayString(m, rr));