react-native-zeroconf 0.13.8 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NOTICE +49 -0
- package/README.md +489 -65
- package/android/build.gradle +23 -24
- package/android/src/main/AndroidManifest.xml +5 -2
- package/android/src/main/java/com/balthazargronon/RCTZeroconf/ZeroConfImplFactory.java +10 -3
- package/android/src/main/java/com/balthazargronon/RCTZeroconf/ZeroconfModule.java +3 -2
- package/android/src/main/java/com/balthazargronon/RCTZeroconf/nsd/NsdServiceImpl.java +83 -22
- package/android/src/main/java/com/balthazargronon/RCTZeroconf/rx2dnssd/DnssdImpl.java +61 -16
- package/android/src/main/java/com/github/druk/dnssd/BaseListener.java +33 -0
- package/android/src/main/java/com/github/druk/dnssd/BrowseListener.java +67 -0
- package/android/src/main/java/com/github/druk/dnssd/DNSRecord.java +47 -0
- package/android/src/main/java/com/github/druk/dnssd/DNSSD.java +650 -0
- package/android/src/main/java/com/github/druk/dnssd/DNSSDBindable.java +67 -0
- package/android/src/main/java/com/github/druk/dnssd/DNSSDEmbedded.java +135 -0
- package/android/src/main/java/com/github/druk/dnssd/DNSSDException.java +66 -0
- package/android/src/main/java/com/github/druk/dnssd/DNSSDRecordRegistrar.java +55 -0
- package/android/src/main/java/com/github/druk/dnssd/DNSSDRegistration.java +54 -0
- package/android/src/main/java/com/github/druk/dnssd/DNSSDService.java +35 -0
- package/android/src/main/java/com/github/druk/dnssd/DomainListener.java +57 -0
- package/android/src/main/java/com/github/druk/dnssd/InternalBrowseListener.java +76 -0
- package/android/src/main/java/com/github/druk/dnssd/InternalDNSSD.java +870 -0
- package/android/src/main/java/com/github/druk/dnssd/InternalDNSSDRecordRegistrar.java +30 -0
- package/android/src/main/java/com/github/druk/dnssd/InternalDNSSDRegistration.java +34 -0
- package/android/src/main/java/com/github/druk/dnssd/InternalDNSSDService.java +29 -0
- package/android/src/main/java/com/github/druk/dnssd/InternalDomainListener.java +62 -0
- package/android/src/main/java/com/github/druk/dnssd/InternalQueryListener.java +61 -0
- package/android/src/main/java/com/github/druk/dnssd/InternalRegisterListener.java +51 -0
- package/android/src/main/java/com/github/druk/dnssd/InternalResolveListener.java +58 -0
- package/android/src/main/java/com/github/druk/dnssd/NSClass.java +16 -0
- package/android/src/main/java/com/github/druk/dnssd/NSType.java +58 -0
- package/android/src/main/java/com/github/druk/dnssd/QueryListener.java +54 -0
- package/android/src/main/java/com/github/druk/dnssd/RegisterListener.java +46 -0
- package/android/src/main/java/com/github/druk/dnssd/RegisterRecordListener.java +35 -0
- package/android/src/main/java/com/github/druk/dnssd/ResolveListener.java +55 -0
- package/android/src/main/java/com/github/druk/dnssd/TXTRecord.java +293 -0
- package/android/src/main/java/com/github/druk/rx2dnssd/BonjourService.java +400 -0
- package/android/src/main/java/com/github/druk/rx2dnssd/Rx2BrowseListener.java +55 -0
- package/android/src/main/java/com/github/druk/rx2dnssd/Rx2Dnssd.java +127 -0
- package/android/src/main/java/com/github/druk/rx2dnssd/Rx2DnssdBindable.java +35 -0
- package/android/src/main/java/com/github/druk/rx2dnssd/Rx2DnssdCommon.java +280 -0
- package/android/src/main/java/com/github/druk/rx2dnssd/Rx2DnssdEmbedded.java +31 -0
- package/android/src/main/java/com/github/druk/rx2dnssd/Rx2QueryListener.java +70 -0
- package/android/src/main/java/com/github/druk/rx2dnssd/Rx2RegisterListener.java +47 -0
- package/android/src/main/java/com/github/druk/rx2dnssd/Rx2ResolveListener.java +51 -0
- package/android/src/main/jni/Android.mk +95 -0
- package/android/src/main/jni/Application.mk +3 -0
- package/android/src/main/jni/DNSSD.java.h +290 -0
- package/android/src/main/jni/JNISupport.c +1140 -0
- package/android/src/main/jni/mdnsresponder/Android.bp +215 -0
- package/android/src/main/jni/mdnsresponder/Clients/ClientCommon.c +75 -0
- package/android/src/main/jni/mdnsresponder/Clients/ClientCommon.h +41 -0
- package/android/src/main/jni/mdnsresponder/Clients/Makefile +54 -0
- package/android/src/main/jni/mdnsresponder/Clients/ReadMe.txt +25 -0
- package/android/src/main/jni/mdnsresponder/Clients/dns-sd.c +1351 -0
- package/android/src/main/jni/mdnsresponder/LICENSE +13 -0
- package/android/src/main/jni/mdnsresponder/MODULE_LICENSE_APACHE2 +0 -0
- package/android/src/main/jni/mdnsresponder/Makefile +41 -0
- package/android/src/main/jni/mdnsresponder/NOTICE +13 -0
- package/android/src/main/jni/mdnsresponder/PrivateDNS.txt +275 -0
- package/android/src/main/jni/mdnsresponder/README.txt +166 -0
- package/android/src/main/jni/mdnsresponder/README.version +3 -0
- package/android/src/main/jni/mdnsresponder/android/caseMapping/Iphlpapi.h +1 -0
- package/android/src/main/jni/mdnsresponder/android/caseMapping/README +3 -0
- package/android/src/main/jni/mdnsresponder/android/caseMapping/Resource.h +1 -0
- package/android/src/main/jni/mdnsresponder/android/caseMapping/Ws2tcpip.h +1 -0
- package/android/src/main/jni/mdnsresponder/android/caseMapping/ipExport.h +1 -0
- package/android/src/main/jni/mdnsresponder/android/windows_firewall_dummy.c +10 -0
- package/android/src/main/jni/mdnsresponder/mDNSCore/DNSCommon.c +3157 -0
- package/android/src/main/jni/mdnsresponder/mDNSCore/DNSCommon.h +292 -0
- package/android/src/main/jni/mdnsresponder/mDNSCore/DNSDigest.c +1579 -0
- package/android/src/main/jni/mdnsresponder/mDNSCore/Implementer Notes.txt +66 -0
- package/android/src/main/jni/mdnsresponder/mDNSCore/mDNS.c +11469 -0
- package/android/src/main/jni/mdnsresponder/mDNSCore/mDNSDebug.h +164 -0
- package/android/src/main/jni/mdnsresponder/mDNSCore/mDNSEmbeddedAPI.h +2974 -0
- package/android/src/main/jni/mdnsresponder/mDNSCore/uDNS.c +4969 -0
- package/android/src/main/jni/mdnsresponder/mDNSCore/uDNS.h +132 -0
- package/android/src/main/jni/mdnsresponder/mDNSPosix/Client.c +223 -0
- package/android/src/main/jni/mdnsresponder/mDNSPosix/ExampleClientApp.c +91 -0
- package/android/src/main/jni/mdnsresponder/mDNSPosix/ExampleClientApp.h +18 -0
- package/android/src/main/jni/mdnsresponder/mDNSPosix/Identify.c +378 -0
- package/android/src/main/jni/mdnsresponder/mDNSPosix/Makefile +520 -0
- package/android/src/main/jni/mdnsresponder/mDNSPosix/NetMonitor.c +1010 -0
- package/android/src/main/jni/mdnsresponder/mDNSPosix/PosixDaemon.c +329 -0
- package/android/src/main/jni/mdnsresponder/mDNSPosix/ProxyResponder.c +300 -0
- package/android/src/main/jni/mdnsresponder/mDNSPosix/ReadMe.txt +314 -0
- package/android/src/main/jni/mdnsresponder/mDNSPosix/Responder.c +792 -0
- package/android/src/main/jni/mdnsresponder/mDNSPosix/Services.txt +36 -0
- package/android/src/main/jni/mdnsresponder/mDNSPosix/libnss_mdns.8 +148 -0
- package/android/src/main/jni/mdnsresponder/mDNSPosix/mDNSPosix.c +1685 -0
- package/android/src/main/jni/mdnsresponder/mDNSPosix/mDNSPosix.h +88 -0
- package/android/src/main/jni/mdnsresponder/mDNSPosix/mDNSUNP.c +726 -0
- package/android/src/main/jni/mdnsresponder/mDNSPosix/mDNSUNP.h +130 -0
- package/android/src/main/jni/mdnsresponder/mDNSPosix/mdnsd.sh +73 -0
- package/android/src/main/jni/mdnsresponder/mDNSPosix/nss_ReadMe.txt +125 -0
- package/android/src/main/jni/mdnsresponder/mDNSPosix/nss_mdns.c +2723 -0
- package/android/src/main/jni/mdnsresponder/mDNSPosix/nss_mdns.conf +13 -0
- package/android/src/main/jni/mdnsresponder/mDNSPosix/nss_mdns.conf.5 +135 -0
- package/android/src/main/jni/mdnsresponder/mDNSPosix/parselog.py +247 -0
- package/android/src/main/jni/mdnsresponder/mDNSResponder.sln +400 -0
- package/android/src/main/jni/mdnsresponder/mDNSShared/CommonServices.h +1537 -0
- package/android/src/main/jni/mdnsresponder/mDNSShared/DebugServices.c +3075 -0
- package/android/src/main/jni/mdnsresponder/mDNSShared/DebugServices.h +1607 -0
- package/android/src/main/jni/mdnsresponder/mDNSShared/GenLinkedList.c +319 -0
- package/android/src/main/jni/mdnsresponder/mDNSShared/GenLinkedList.h +90 -0
- package/android/src/main/jni/mdnsresponder/mDNSShared/PlatformCommon.c +220 -0
- package/android/src/main/jni/mdnsresponder/mDNSShared/PlatformCommon.h +18 -0
- package/android/src/main/jni/mdnsresponder/mDNSShared/dns-sd.1 +194 -0
- package/android/src/main/jni/mdnsresponder/mDNSShared/dns_sd.h +2452 -0
- package/android/src/main/jni/mdnsresponder/mDNSShared/dnsextd.8 +69 -0
- package/android/src/main/jni/mdnsresponder/mDNSShared/dnsextd.c +3146 -0
- package/android/src/main/jni/mdnsresponder/mDNSShared/dnsextd.conf +60 -0
- package/android/src/main/jni/mdnsresponder/mDNSShared/dnsextd.h +163 -0
- package/android/src/main/jni/mdnsresponder/mDNSShared/dnsextd_lexer.l +84 -0
- package/android/src/main/jni/mdnsresponder/mDNSShared/dnsextd_parser.y +585 -0
- package/android/src/main/jni/mdnsresponder/mDNSShared/dnssd_clientlib.c +368 -0
- package/android/src/main/jni/mdnsresponder/mDNSShared/dnssd_clientshim.c +802 -0
- package/android/src/main/jni/mdnsresponder/mDNSShared/dnssd_clientstub.c +1988 -0
- package/android/src/main/jni/mdnsresponder/mDNSShared/dnssd_ipc.c +161 -0
- package/android/src/main/jni/mdnsresponder/mDNSShared/dnssd_ipc.h +219 -0
- package/android/src/main/jni/mdnsresponder/mDNSShared/mDNS.1 +198 -0
- package/android/src/main/jni/mdnsresponder/mDNSShared/mDNSDebug.c +93 -0
- package/android/src/main/jni/mdnsresponder/mDNSShared/mDNSResponder.8 +116 -0
- package/android/src/main/jni/mdnsresponder/mDNSShared/uds_daemon.c +4758 -0
- package/android/src/main/jni/mdnsresponder/mDNSShared/uds_daemon.h +80 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/BrowsingPage.cpp +441 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/BrowsingPage.h +156 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/ConfigDialog.cpp +59 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/ConfigDialog.h +48 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/ConfigPropertySheet.cpp +301 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/ConfigPropertySheet.h +105 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/ControlPanel.cpp +380 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/ControlPanel.def +20 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/ControlPanel.h +84 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/ControlPanel.rc +141 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/ControlPanel.vcproj +727 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/ControlPanelDll.rc +123 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/ControlPanelExe.cpp +373 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/ControlPanelExe.h +48 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/ControlPanelExe.rc +123 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/ControlPanelExe.vcproj +764 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/ControlPanelLocRes.rc +270 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/ControlPanelLocRes.vcproj +487 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/ControlPanelRes.rc +134 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/ControlPanelRes.vcproj +518 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/FourthPage.cpp +197 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/FourthPage.h +87 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/RegistrationPage.cpp +387 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/RegistrationPage.h +75 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/SecondPage.cpp +544 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/SecondPage.h +107 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/ServicesPage.cpp +273 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/ServicesPage.h +123 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/SharedSecret.cpp +115 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/SharedSecret.h +54 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/res/ControlPanel.dll.manifest +10 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/res/ControlPanel.exe.manifest +17 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/res/ControlPanel.manifest +17 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/res/ControlPanel.rc2 +13 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/res/ControlPanel64.manifest +17 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/res/EnergySaver.ico +0 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/res/controlpanel.ico +0 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/res/failure.ico +0 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/res/success.ico +0 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/resource.h +56 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/stdafx.cpp +20 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/ControlPanel/stdafx.h +64 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLL/dll.aps +0 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLL/dll.rc +102 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLL/dllmain.c +112 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLL/dnssd.def +48 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLL/dnssd.vcproj +472 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLL/resource.h +27 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLL.NET/AssemblyInfo.cpp +84 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLL.NET/PString.h +70 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLL.NET/Stdafx.cpp +22 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLL.NET/Stdafx.h +33 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLL.NET/dnssd_NET.cpp +1234 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLL.NET/dnssd_NET.h +1392 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLL.NET/dnssd_NET.ico +0 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLL.NET/dnssd_NET.rc +113 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLL.NET/dnssd_NET.vcproj +446 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLL.NET/resource.h +3 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLStub/DLLStub.cpp +693 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLStub/DLLStub.h +52 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLStub/DLLStub.vcproj +324 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/DLLX.cpp +208 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/DLLX.def +35 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/DLLX.idl +491 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/DLLX.rc +126 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/DLLX.rgs +11 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/DLLX.vcproj +625 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/DNSSD.cpp +892 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/DNSSDEventManager.cpp +31 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/DNSSDEventManager.h +133 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/DNSSDEventManager.rgs +27 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/DNSSDRecord.cpp +101 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/DNSSDRecord.h +185 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/DNSSDRecord.rgs +27 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/DNSSDService.cpp +2095 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/DNSSDService.h +429 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/DNSSDService.rgs +27 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/StringServices.cpp +344 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/StringServices.h +102 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/TXTRecord.cpp +381 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/TXTRecord.h +203 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/TXTRecord.rgs +27 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/_IDNSSDEvents_CP.h +358 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/dlldatax.c +51 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/dlldatax.h +49 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/resource.h +30 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DLLX/stdafx.h +88 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/Windows/ApplicationVS2002.sln +21 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/Windows/ApplicationVS2002.vcproj +253 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/Windows/ApplicationVS2003.sln +21 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/Windows/ApplicationVS2003.vcproj +267 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/Windows/Resources/Application.ico +0 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/Windows/Resources/Application.rc +323 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/Windows/Resources/Application.rc2 +20 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/Windows/Resources/Resource.h +53 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/Windows/Sources/AboutDialog.cpp +71 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/Windows/Sources/AboutDialog.h +62 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/Windows/Sources/Application.cpp +111 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/Windows/Sources/Application.h +68 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/Windows/Sources/ChooserDialog.cpp +1426 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/Windows/Sources/ChooserDialog.h +131 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/Windows/Sources/LoginDialog.cpp +109 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/Windows/Sources/LoginDialog.h +53 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/Windows/Sources/StdAfx.cpp +18 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/Windows/Sources/StdAfx.h +48 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/WindowsCE/Application.vcc +37 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/WindowsCE/Application.vcp +868 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/WindowsCE/Application.vcw +29 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/WindowsCE/Resources/Application.ico +0 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/WindowsCE/Resources/Application.rc +194 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/WindowsCE/Resources/Application.rc2 +13 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/WindowsCE/Resources/newres.h +28 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/WindowsCE/Resources/resource.h +22 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/WindowsCE/Sources/Application.cpp +92 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/WindowsCE/Sources/Application.h +57 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/WindowsCE/Sources/BrowserDialog.cpp +394 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/WindowsCE/Sources/BrowserDialog.h +84 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/WindowsCE/Sources/StdAfx.cpp +18 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/DNSServiceBrowser/WindowsCE/Sources/StdAfx.h +46 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/Java/Java.vcproj +111 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/Java/jdns_sd.rc +62 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/Java/makefile +143 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/Java/makefile64 +143 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/NSPTool/NSPTool.aps +0 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/NSPTool/NSPTool.c +581 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/NSPTool/NSPTool.rc +103 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/NSPTool/NSPTool.vcproj +409 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/NSPTool/Prefix.h +28 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/NSPTool/resource.h +27 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/PosixCompat.c +128 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/PosixCompat.h +70 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/README.txt +85 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/RegNames.h +57 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/Secret.c +338 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/Secret.h +42 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/SystemService/EventLog.mc +11 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/SystemService/EventLogMessages.bin +0 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/SystemService/Firewall.cpp +484 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/SystemService/Firewall.h +79 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/SystemService/Prefix.h +30 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/SystemService/Service.aps +0 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/SystemService/Service.c +2669 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/SystemService/Service.h +30 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/SystemService/Service.mcp +0 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/SystemService/Service.rc +114 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/SystemService/Service.vcproj +562 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/SystemService/main.c +32 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/SystemService/res/mDNSResponder.manifest +12 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/SystemService/res/mDNSResponder64.manifest +12 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/SystemService/resource.h +17 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/SystemService/resrc1.h +15 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/VPCDetect.cpp +165 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/VPCDetect.h +36 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/WinServices.cpp +93 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/WinServices.h +34 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/WinVersRes.h +35 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/isocode.h +135 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/loclibrary.c +268 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/loclibrary.h +54 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/mDNSWin32.c +5200 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/mDNSWin32.h +208 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/mdnsNSP/ReadMe.txt +15 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/mdnsNSP/mdnsNSP.aps +0 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/mdnsNSP/mdnsNSP.c +2430 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/mdnsNSP/mdnsNSP.def +24 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/mdnsNSP/mdnsNSP.rc +104 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/mdnsNSP/mdnsNSP.vcproj +457 -0
- package/android/src/main/jni/mdnsresponder/mDNSWindows/mdnsNSP/resource.h +27 -0
- package/android/src/main/jni/mdnsresponder/mdnsd.rc +7 -0
- package/android/src/main/jni/nameser.h +588 -0
- package/dist/index.js +160 -1
- package/index.d.ts +91 -0
- package/ios/RNZeroconf/RNNetServiceSerializer.m +7 -3
- package/ios/RNZeroconf/RNZeroconf.m +8 -0
- package/package.json +25 -17
- package/src/index.js +183 -0
|
@@ -0,0 +1,3157 @@
|
|
|
1
|
+
/* -*- Mode: C; tab-width: 4 -*-
|
|
2
|
+
*
|
|
3
|
+
* Copyright (c) 2002-2003 Apple Computer, Inc. All rights reserved.
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
// Set mDNS_InstantiateInlines to tell mDNSEmbeddedAPI.h to instantiate inline functions, if necessary
|
|
19
|
+
#define mDNS_InstantiateInlines 1
|
|
20
|
+
#include "DNSCommon.h"
|
|
21
|
+
|
|
22
|
+
// Disable certain benign warnings with Microsoft compilers
|
|
23
|
+
#if (defined(_MSC_VER))
|
|
24
|
+
// Disable "conditional expression is constant" warning for debug macros.
|
|
25
|
+
// Otherwise, this generates warnings for the perfectly natural construct "while(1)"
|
|
26
|
+
// If someone knows a variant way of writing "while(1)" that doesn't generate warning messages, please let us know
|
|
27
|
+
#pragma warning(disable:4127)
|
|
28
|
+
// Disable "array is too small to include a terminating null character" warning
|
|
29
|
+
// -- domain labels have an initial length byte, not a terminating null character
|
|
30
|
+
#pragma warning(disable:4295)
|
|
31
|
+
#endif
|
|
32
|
+
|
|
33
|
+
// ***************************************************************************
|
|
34
|
+
#if COMPILER_LIKES_PRAGMA_MARK
|
|
35
|
+
#pragma mark - Program Constants
|
|
36
|
+
#endif
|
|
37
|
+
|
|
38
|
+
mDNSexport const mDNSInterfaceID mDNSInterface_Any = 0;
|
|
39
|
+
mDNSexport const mDNSInterfaceID mDNSInterfaceMark = (mDNSInterfaceID)-1;
|
|
40
|
+
mDNSexport const mDNSInterfaceID mDNSInterface_LocalOnly = (mDNSInterfaceID)-2;
|
|
41
|
+
mDNSexport const mDNSInterfaceID mDNSInterface_Unicast = (mDNSInterfaceID)-3;
|
|
42
|
+
mDNSexport const mDNSInterfaceID mDNSInterface_P2P = (mDNSInterfaceID)-4;
|
|
43
|
+
|
|
44
|
+
// Note: Microsoft's proposed "Link Local Multicast Name Resolution Protocol" (LLMNR) is essentially a limited version of
|
|
45
|
+
// Multicast DNS, using the same packet formats, naming syntax, and record types as Multicast DNS, but on a different UDP
|
|
46
|
+
// port and multicast address, which means it won't interoperate with the existing installed base of Multicast DNS responders.
|
|
47
|
+
// LLMNR uses IPv4 multicast address 224.0.0.252, IPv6 multicast address FF02::0001:0003, and UDP port 5355.
|
|
48
|
+
// Uncomment the appropriate lines below to build a special Multicast DNS responder for testing interoperability
|
|
49
|
+
// with Microsoft's LLMNR client code.
|
|
50
|
+
|
|
51
|
+
#define DiscardPortAsNumber 9
|
|
52
|
+
#define SSHPortAsNumber 22
|
|
53
|
+
#define UnicastDNSPortAsNumber 53
|
|
54
|
+
#define SSDPPortAsNumber 1900
|
|
55
|
+
#define IPSECPortAsNumber 4500
|
|
56
|
+
#define NSIPCPortAsNumber 5030 // Port used for dnsextd to talk to local nameserver bound to loopback
|
|
57
|
+
#define NATPMPAnnouncementPortAsNumber 5350
|
|
58
|
+
#define NATPMPPortAsNumber 5351
|
|
59
|
+
#define DNSEXTPortAsNumber 5352 // Port used for end-to-end DNS operations like LLQ, Updates with Leases, etc.
|
|
60
|
+
#define MulticastDNSPortAsNumber 5353
|
|
61
|
+
#define LoopbackIPCPortAsNumber 5354
|
|
62
|
+
//#define MulticastDNSPortAsNumber 5355 // LLMNR
|
|
63
|
+
#define PrivateDNSPortAsNumber 5533
|
|
64
|
+
|
|
65
|
+
mDNSexport const mDNSIPPort DiscardPort = { { DiscardPortAsNumber >> 8, DiscardPortAsNumber & 0xFF } };
|
|
66
|
+
mDNSexport const mDNSIPPort SSHPort = { { SSHPortAsNumber >> 8, SSHPortAsNumber & 0xFF } };
|
|
67
|
+
mDNSexport const mDNSIPPort UnicastDNSPort = { { UnicastDNSPortAsNumber >> 8, UnicastDNSPortAsNumber & 0xFF } };
|
|
68
|
+
mDNSexport const mDNSIPPort SSDPPort = { { SSDPPortAsNumber >> 8, SSDPPortAsNumber & 0xFF } };
|
|
69
|
+
mDNSexport const mDNSIPPort IPSECPort = { { IPSECPortAsNumber >> 8, IPSECPortAsNumber & 0xFF } };
|
|
70
|
+
mDNSexport const mDNSIPPort NSIPCPort = { { NSIPCPortAsNumber >> 8, NSIPCPortAsNumber & 0xFF } };
|
|
71
|
+
mDNSexport const mDNSIPPort NATPMPAnnouncementPort = { { NATPMPAnnouncementPortAsNumber >> 8, NATPMPAnnouncementPortAsNumber & 0xFF } };
|
|
72
|
+
mDNSexport const mDNSIPPort NATPMPPort = { { NATPMPPortAsNumber >> 8, NATPMPPortAsNumber & 0xFF } };
|
|
73
|
+
mDNSexport const mDNSIPPort DNSEXTPort = { { DNSEXTPortAsNumber >> 8, DNSEXTPortAsNumber & 0xFF } };
|
|
74
|
+
mDNSexport const mDNSIPPort MulticastDNSPort = { { MulticastDNSPortAsNumber >> 8, MulticastDNSPortAsNumber & 0xFF } };
|
|
75
|
+
mDNSexport const mDNSIPPort LoopbackIPCPort = { { LoopbackIPCPortAsNumber >> 8, LoopbackIPCPortAsNumber & 0xFF } };
|
|
76
|
+
mDNSexport const mDNSIPPort PrivateDNSPort = { { PrivateDNSPortAsNumber >> 8, PrivateDNSPortAsNumber & 0xFF } };
|
|
77
|
+
|
|
78
|
+
mDNSexport const OwnerOptData zeroOwner = { 0, 0, { { 0 } }, { { 0 } }, { { 0 } } };
|
|
79
|
+
|
|
80
|
+
mDNSexport const mDNSIPPort zeroIPPort = { { 0 } };
|
|
81
|
+
mDNSexport const mDNSv4Addr zerov4Addr = { { 0 } };
|
|
82
|
+
mDNSexport const mDNSv6Addr zerov6Addr = { { 0 } };
|
|
83
|
+
mDNSexport const mDNSEthAddr zeroEthAddr = { { 0 } };
|
|
84
|
+
mDNSexport const mDNSv4Addr onesIPv4Addr = { { 255, 255, 255, 255 } };
|
|
85
|
+
mDNSexport const mDNSv6Addr onesIPv6Addr = { { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 } };
|
|
86
|
+
mDNSexport const mDNSEthAddr onesEthAddr = { { 255, 255, 255, 255, 255, 255 } };
|
|
87
|
+
mDNSexport const mDNSAddr zeroAddr = { mDNSAddrType_None, {{{ 0 }}} };
|
|
88
|
+
|
|
89
|
+
mDNSexport const mDNSv4Addr AllDNSAdminGroup = { { 239, 255, 255, 251 } };
|
|
90
|
+
mDNSexport const mDNSv4Addr AllHosts_v4 = { { 224, 0, 0, 1 } }; // For NAT-PMP Annoucements
|
|
91
|
+
mDNSexport const mDNSv6Addr AllHosts_v6 = { { 0xFF,0x02,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x01 } };
|
|
92
|
+
mDNSexport const mDNSv6Addr NDP_prefix = { { 0xFF,0x02,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x01, 0xFF,0x00,0x00,0xFB } }; // FF02:0:0:0:0:1:FF00::/104
|
|
93
|
+
mDNSexport const mDNSEthAddr AllHosts_v6_Eth = { { 0x33, 0x33, 0x00, 0x00, 0x00, 0x01 } };
|
|
94
|
+
mDNSexport const mDNSAddr AllDNSLinkGroup_v4 = { mDNSAddrType_IPv4, { { { 224, 0, 0, 251 } } } };
|
|
95
|
+
//mDNSexport const mDNSAddr AllDNSLinkGroup_v4 = { mDNSAddrType_IPv4, { { { 224, 0, 0, 252 } } } }; // LLMNR
|
|
96
|
+
mDNSexport const mDNSAddr AllDNSLinkGroup_v6 = { mDNSAddrType_IPv6, { { { 0xFF,0x02,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0xFB } } } };
|
|
97
|
+
//mDNSexport const mDNSAddr AllDNSLinkGroup_v6 = { mDNSAddrType_IPv6, { { { 0xFF,0x02,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x01,0x00,0x03 } } } }; // LLMNR
|
|
98
|
+
|
|
99
|
+
mDNSexport const mDNSOpaque16 zeroID = { { 0, 0 } };
|
|
100
|
+
mDNSexport const mDNSOpaque16 onesID = { { 255, 255 } };
|
|
101
|
+
mDNSexport const mDNSOpaque16 QueryFlags = { { kDNSFlag0_QR_Query | kDNSFlag0_OP_StdQuery, 0 } };
|
|
102
|
+
mDNSexport const mDNSOpaque16 uQueryFlags = { { kDNSFlag0_QR_Query | kDNSFlag0_OP_StdQuery | kDNSFlag0_RD, 0 } };
|
|
103
|
+
mDNSexport const mDNSOpaque16 ResponseFlags = { { kDNSFlag0_QR_Response | kDNSFlag0_OP_StdQuery | kDNSFlag0_AA, 0 } };
|
|
104
|
+
mDNSexport const mDNSOpaque16 UpdateReqFlags = { { kDNSFlag0_QR_Query | kDNSFlag0_OP_Update, 0 } };
|
|
105
|
+
mDNSexport const mDNSOpaque16 UpdateRespFlags = { { kDNSFlag0_QR_Response | kDNSFlag0_OP_Update, 0 } };
|
|
106
|
+
|
|
107
|
+
mDNSexport const mDNSOpaque64 zeroOpaque64 = { { 0 } };
|
|
108
|
+
|
|
109
|
+
// ***************************************************************************
|
|
110
|
+
#if COMPILER_LIKES_PRAGMA_MARK
|
|
111
|
+
#pragma mark -
|
|
112
|
+
#pragma mark - General Utility Functions
|
|
113
|
+
#endif
|
|
114
|
+
|
|
115
|
+
// return true for RFC1918 private addresses
|
|
116
|
+
mDNSexport mDNSBool mDNSv4AddrIsRFC1918(mDNSv4Addr *addr)
|
|
117
|
+
{
|
|
118
|
+
return ((addr->b[0] == 10) || // 10/8 prefix
|
|
119
|
+
(addr->b[0] == 172 && (addr->b[1] & 0xF0) == 16) || // 172.16/12
|
|
120
|
+
(addr->b[0] == 192 && addr->b[1] == 168)); // 192.168/16
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
mDNSexport NetworkInterfaceInfo *GetFirstActiveInterface(NetworkInterfaceInfo *intf)
|
|
124
|
+
{
|
|
125
|
+
while (intf && !intf->InterfaceActive) intf = intf->next;
|
|
126
|
+
return(intf);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
mDNSexport mDNSInterfaceID GetNextActiveInterfaceID(const NetworkInterfaceInfo *intf)
|
|
130
|
+
{
|
|
131
|
+
const NetworkInterfaceInfo *next = GetFirstActiveInterface(intf->next);
|
|
132
|
+
if (next) return(next->InterfaceID); else return(mDNSNULL);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
mDNSexport mDNSu32 NumCacheRecordsForInterfaceID(const mDNS *const m, mDNSInterfaceID id)
|
|
136
|
+
{
|
|
137
|
+
mDNSu32 slot, used = 0;
|
|
138
|
+
CacheGroup *cg;
|
|
139
|
+
const CacheRecord *rr;
|
|
140
|
+
FORALL_CACHERECORDS(slot, cg, rr)
|
|
141
|
+
if (rr->resrec.InterfaceID == id) used++;
|
|
142
|
+
return(used);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
mDNSexport char *DNSTypeName(mDNSu16 rrtype)
|
|
146
|
+
{
|
|
147
|
+
switch (rrtype)
|
|
148
|
+
{
|
|
149
|
+
case kDNSType_A: return("Addr");
|
|
150
|
+
case kDNSType_NS: return("NS");
|
|
151
|
+
case kDNSType_CNAME:return("CNAME");
|
|
152
|
+
case kDNSType_SOA: return("SOA");
|
|
153
|
+
case kDNSType_NULL: return("NULL");
|
|
154
|
+
case kDNSType_PTR: return("PTR");
|
|
155
|
+
case kDNSType_HINFO:return("HINFO");
|
|
156
|
+
case kDNSType_TXT: return("TXT");
|
|
157
|
+
case kDNSType_AAAA: return("AAAA");
|
|
158
|
+
case kDNSType_SRV: return("SRV");
|
|
159
|
+
case kDNSType_OPT: return("OPT");
|
|
160
|
+
case kDNSType_NSEC: return("NSEC");
|
|
161
|
+
case kDNSType_TSIG: return("TSIG");
|
|
162
|
+
case kDNSQType_ANY: return("ANY");
|
|
163
|
+
default: {
|
|
164
|
+
static char buffer[16];
|
|
165
|
+
mDNS_snprintf(buffer, sizeof(buffer), "(%d)", rrtype);
|
|
166
|
+
return(buffer);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Note slight bug: this code uses the rdlength from the ResourceRecord object, to display
|
|
172
|
+
// the rdata from the RDataBody object. Sometimes this could be the wrong length -- but as
|
|
173
|
+
// long as this routine is only used for debugging messages, it probably isn't a big problem.
|
|
174
|
+
mDNSexport char *GetRRDisplayString_rdb(const ResourceRecord *const rr, const RDataBody *const rd1, char *const buffer)
|
|
175
|
+
{
|
|
176
|
+
const RDataBody2 *const rd = (RDataBody2 *)rd1;
|
|
177
|
+
#define RemSpc (MaxMsg-1-length)
|
|
178
|
+
char *ptr = buffer;
|
|
179
|
+
mDNSu32 length = mDNS_snprintf(buffer, MaxMsg-1, "%4d %##s %s ", rr->rdlength, rr->name->c, DNSTypeName(rr->rrtype));
|
|
180
|
+
if (rr->RecordType == kDNSRecordTypePacketNegative) return(buffer);
|
|
181
|
+
if (!rr->rdlength) { mDNS_snprintf(buffer+length, RemSpc, "<< ZERO RDATA LENGTH >>"); return(buffer); }
|
|
182
|
+
|
|
183
|
+
switch (rr->rrtype)
|
|
184
|
+
{
|
|
185
|
+
case kDNSType_A: mDNS_snprintf(buffer+length, RemSpc, "%.4a", &rd->ipv4); break;
|
|
186
|
+
|
|
187
|
+
case kDNSType_NS: // Same as PTR
|
|
188
|
+
case kDNSType_CNAME:// Same as PTR
|
|
189
|
+
case kDNSType_PTR: mDNS_snprintf(buffer+length, RemSpc, "%##s", rd->name.c); break;
|
|
190
|
+
|
|
191
|
+
case kDNSType_SOA: mDNS_snprintf(buffer+length, RemSpc, "%##s %##s %d %d %d %d %d",
|
|
192
|
+
rd->soa.mname.c, rd->soa.rname.c,
|
|
193
|
+
rd->soa.serial, rd->soa.refresh, rd->soa.retry, rd->soa.expire, rd->soa.min);
|
|
194
|
+
break;
|
|
195
|
+
|
|
196
|
+
case kDNSType_HINFO:// Display this the same as TXT (show all constituent strings)
|
|
197
|
+
case kDNSType_TXT: {
|
|
198
|
+
const mDNSu8 *t = rd->txt.c;
|
|
199
|
+
while (t < rd->txt.c + rr->rdlength)
|
|
200
|
+
{
|
|
201
|
+
length += mDNS_snprintf(buffer+length, RemSpc, "%s%#s", t > rd->txt.c ? "¦" : "", t);
|
|
202
|
+
t += 1 + t[0];
|
|
203
|
+
}
|
|
204
|
+
} break;
|
|
205
|
+
|
|
206
|
+
case kDNSType_AAAA: mDNS_snprintf(buffer+length, RemSpc, "%.16a", &rd->ipv6); break;
|
|
207
|
+
case kDNSType_SRV: mDNS_snprintf(buffer+length, RemSpc, "%u %u %u %##s",
|
|
208
|
+
rd->srv.priority, rd->srv.weight, mDNSVal16(rd->srv.port), rd->srv.target.c); break;
|
|
209
|
+
|
|
210
|
+
case kDNSType_OPT: {
|
|
211
|
+
const rdataOPT *opt;
|
|
212
|
+
const rdataOPT *const end = (const rdataOPT *)&rd->data[rr->rdlength];
|
|
213
|
+
length += mDNS_snprintf(buffer+length, RemSpc, "Max %d", rr->rrclass);
|
|
214
|
+
for (opt = &rd->opt[0]; opt < end; opt++)
|
|
215
|
+
{
|
|
216
|
+
switch(opt->opt)
|
|
217
|
+
{
|
|
218
|
+
case kDNSOpt_LLQ:
|
|
219
|
+
length += mDNS_snprintf(buffer+length, RemSpc, " Vers %d", opt->u.llq.vers);
|
|
220
|
+
length += mDNS_snprintf(buffer+length, RemSpc, " Op %d", opt->u.llq.llqOp);
|
|
221
|
+
length += mDNS_snprintf(buffer+length, RemSpc, " Err/Port %d", opt->u.llq.err);
|
|
222
|
+
length += mDNS_snprintf(buffer+length, RemSpc, " ID %08X%08X", opt->u.llq.id.l[0], opt->u.llq.id.l[1]);
|
|
223
|
+
length += mDNS_snprintf(buffer+length, RemSpc, " Lease %d", opt->u.llq.llqlease);
|
|
224
|
+
break;
|
|
225
|
+
case kDNSOpt_Lease:
|
|
226
|
+
length += mDNS_snprintf(buffer+length, RemSpc, " Lease %d", opt->u.updatelease);
|
|
227
|
+
break;
|
|
228
|
+
case kDNSOpt_Owner:
|
|
229
|
+
length += mDNS_snprintf(buffer+length, RemSpc, " Vers %d", opt->u.owner.vers);
|
|
230
|
+
length += mDNS_snprintf(buffer+length, RemSpc, " Seq %3d", (mDNSu8)opt->u.owner.seq); // Display as unsigned
|
|
231
|
+
length += mDNS_snprintf(buffer+length, RemSpc, " MAC %.6a", opt->u.owner.HMAC.b);
|
|
232
|
+
if (opt->optlen >= DNSOpt_OwnerData_ID_Wake_Space-4)
|
|
233
|
+
{
|
|
234
|
+
length += mDNS_snprintf(buffer+length, RemSpc, " I-MAC %.6a", opt->u.owner.IMAC.b);
|
|
235
|
+
if (opt->optlen > DNSOpt_OwnerData_ID_Wake_Space-4)
|
|
236
|
+
length += mDNS_snprintf(buffer+length, RemSpc, " Password %.6a", opt->u.owner.password.b);
|
|
237
|
+
}
|
|
238
|
+
break;
|
|
239
|
+
default:
|
|
240
|
+
length += mDNS_snprintf(buffer+length, RemSpc, " Unknown %d", opt->opt);
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
break;
|
|
246
|
+
|
|
247
|
+
case kDNSType_NSEC: {
|
|
248
|
+
mDNSu16 i;
|
|
249
|
+
for (i=0; i<255; i++)
|
|
250
|
+
if (rd->nsec.bitmap[i>>3] & (128 >> (i&7)))
|
|
251
|
+
length += mDNS_snprintf(buffer+length, RemSpc, "%s ", DNSTypeName(i));
|
|
252
|
+
}
|
|
253
|
+
break;
|
|
254
|
+
|
|
255
|
+
default: mDNS_snprintf(buffer+length, RemSpc, "RDLen %d: %s", rr->rdlength, rd->data);
|
|
256
|
+
// Really should scan buffer to check if text is valid UTF-8 and only replace with dots if not
|
|
257
|
+
for (ptr = buffer; *ptr; ptr++) if (*ptr < ' ') *ptr = '.';
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
return(buffer);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// See comments in mDNSEmbeddedAPI.h
|
|
264
|
+
#if _PLATFORM_HAS_STRONG_PRNG_
|
|
265
|
+
#define mDNSRandomNumber mDNSPlatformRandomNumber
|
|
266
|
+
#else
|
|
267
|
+
mDNSlocal mDNSu32 mDNSRandomFromSeed(mDNSu32 seed)
|
|
268
|
+
{
|
|
269
|
+
return seed * 21 + 1;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
mDNSlocal mDNSu32 mDNSMixRandomSeed(mDNSu32 seed, mDNSu8 iteration)
|
|
273
|
+
{
|
|
274
|
+
return iteration ? mDNSMixRandomSeed(mDNSRandomFromSeed(seed), --iteration) : seed;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
mDNSlocal mDNSu32 mDNSRandomNumber()
|
|
278
|
+
{
|
|
279
|
+
static mDNSBool seeded = mDNSfalse;
|
|
280
|
+
static mDNSu32 seed = 0;
|
|
281
|
+
if (!seeded)
|
|
282
|
+
{
|
|
283
|
+
seed = mDNSMixRandomSeed(mDNSPlatformRandomSeed(), 100);
|
|
284
|
+
seeded = mDNStrue;
|
|
285
|
+
}
|
|
286
|
+
return (seed = mDNSRandomFromSeed(seed));
|
|
287
|
+
}
|
|
288
|
+
#endif // ! _PLATFORM_HAS_STRONG_PRNG_
|
|
289
|
+
|
|
290
|
+
mDNSexport mDNSu32 mDNSRandom(mDNSu32 max) // Returns pseudo-random result from zero to max inclusive
|
|
291
|
+
{
|
|
292
|
+
mDNSu32 ret = 0;
|
|
293
|
+
mDNSu32 mask = 1;
|
|
294
|
+
|
|
295
|
+
while (mask < max) mask = (mask << 1) | 1;
|
|
296
|
+
|
|
297
|
+
do ret = mDNSRandomNumber() & mask;
|
|
298
|
+
while (ret > max);
|
|
299
|
+
|
|
300
|
+
return ret;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
mDNSexport mDNSBool mDNSSameAddress(const mDNSAddr *ip1, const mDNSAddr *ip2)
|
|
304
|
+
{
|
|
305
|
+
if (ip1->type == ip2->type)
|
|
306
|
+
{
|
|
307
|
+
switch (ip1->type)
|
|
308
|
+
{
|
|
309
|
+
case mDNSAddrType_None : return(mDNStrue); // Empty addresses have no data and are therefore always equal
|
|
310
|
+
case mDNSAddrType_IPv4 : return(mDNSBool)(mDNSSameIPv4Address(ip1->ip.v4, ip2->ip.v4));
|
|
311
|
+
case mDNSAddrType_IPv6 : return(mDNSBool)(mDNSSameIPv6Address(ip1->ip.v6, ip2->ip.v6));
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return(mDNSfalse);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
mDNSexport mDNSBool mDNSAddrIsDNSMulticast(const mDNSAddr *ip)
|
|
318
|
+
{
|
|
319
|
+
switch(ip->type)
|
|
320
|
+
{
|
|
321
|
+
case mDNSAddrType_IPv4: return(mDNSBool)(mDNSSameIPv4Address(ip->ip.v4, AllDNSLinkGroup_v4.ip.v4));
|
|
322
|
+
case mDNSAddrType_IPv6: return(mDNSBool)(mDNSSameIPv6Address(ip->ip.v6, AllDNSLinkGroup_v6.ip.v6));
|
|
323
|
+
default: return(mDNSfalse);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// ***************************************************************************
|
|
328
|
+
#if COMPILER_LIKES_PRAGMA_MARK
|
|
329
|
+
#pragma mark -
|
|
330
|
+
#pragma mark - Domain Name Utility Functions
|
|
331
|
+
#endif
|
|
332
|
+
|
|
333
|
+
mDNSexport mDNSBool SameDomainLabel(const mDNSu8 *a, const mDNSu8 *b)
|
|
334
|
+
{
|
|
335
|
+
int i;
|
|
336
|
+
const int len = *a++;
|
|
337
|
+
|
|
338
|
+
if (len > MAX_DOMAIN_LABEL)
|
|
339
|
+
{ debugf("Malformed label (too long)"); return(mDNSfalse); }
|
|
340
|
+
|
|
341
|
+
if (len != *b++) return(mDNSfalse);
|
|
342
|
+
for (i=0; i<len; i++)
|
|
343
|
+
{
|
|
344
|
+
mDNSu8 ac = *a++;
|
|
345
|
+
mDNSu8 bc = *b++;
|
|
346
|
+
if (mDNSIsUpperCase(ac)) ac += 'a' - 'A';
|
|
347
|
+
if (mDNSIsUpperCase(bc)) bc += 'a' - 'A';
|
|
348
|
+
if (ac != bc) return(mDNSfalse);
|
|
349
|
+
}
|
|
350
|
+
return(mDNStrue);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
mDNSexport mDNSBool SameDomainName(const domainname *const d1, const domainname *const d2)
|
|
354
|
+
{
|
|
355
|
+
const mDNSu8 * a = d1->c;
|
|
356
|
+
const mDNSu8 * b = d2->c;
|
|
357
|
+
const mDNSu8 *const max = d1->c + MAX_DOMAIN_NAME; // Maximum that's valid
|
|
358
|
+
|
|
359
|
+
while (*a || *b)
|
|
360
|
+
{
|
|
361
|
+
if (a + 1 + *a >= max)
|
|
362
|
+
{ debugf("Malformed domain name (more than 256 characters)"); return(mDNSfalse); }
|
|
363
|
+
if (!SameDomainLabel(a, b)) return(mDNSfalse);
|
|
364
|
+
a += 1 + *a;
|
|
365
|
+
b += 1 + *b;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
return(mDNStrue);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
mDNSexport mDNSBool SameDomainNameCS(const domainname *const d1, const domainname *const d2)
|
|
372
|
+
{
|
|
373
|
+
mDNSu16 l1 = DomainNameLength(d1);
|
|
374
|
+
mDNSu16 l2 = DomainNameLength(d2);
|
|
375
|
+
return(l1 <= MAX_DOMAIN_NAME && l1 == l2 && mDNSPlatformMemSame(d1, d2, l1));
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
mDNSexport mDNSBool IsLocalDomain(const domainname *d)
|
|
379
|
+
{
|
|
380
|
+
// Domains that are defined to be resolved via link-local multicast are:
|
|
381
|
+
// local., 254.169.in-addr.arpa., and {8,9,A,B}.E.F.ip6.arpa.
|
|
382
|
+
static const domainname *nL = (const domainname*)"\x5" "local";
|
|
383
|
+
static const domainname *nR = (const domainname*)"\x3" "254" "\x3" "169" "\x7" "in-addr" "\x4" "arpa";
|
|
384
|
+
static const domainname *n8 = (const domainname*)"\x1" "8" "\x1" "e" "\x1" "f" "\x3" "ip6" "\x4" "arpa";
|
|
385
|
+
static const domainname *n9 = (const domainname*)"\x1" "9" "\x1" "e" "\x1" "f" "\x3" "ip6" "\x4" "arpa";
|
|
386
|
+
static const domainname *nA = (const domainname*)"\x1" "a" "\x1" "e" "\x1" "f" "\x3" "ip6" "\x4" "arpa";
|
|
387
|
+
static const domainname *nB = (const domainname*)"\x1" "b" "\x1" "e" "\x1" "f" "\x3" "ip6" "\x4" "arpa";
|
|
388
|
+
|
|
389
|
+
const domainname *d1, *d2, *d3, *d4, *d5; // Top-level domain, second-level domain, etc.
|
|
390
|
+
d1 = d2 = d3 = d4 = d5 = mDNSNULL;
|
|
391
|
+
while (d->c[0])
|
|
392
|
+
{
|
|
393
|
+
d5 = d4; d4 = d3; d3 = d2; d2 = d1; d1 = d;
|
|
394
|
+
d = (const domainname*)(d->c + 1 + d->c[0]);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
if (d1 && SameDomainName(d1, nL)) return(mDNStrue);
|
|
398
|
+
if (d4 && SameDomainName(d4, nR)) return(mDNStrue);
|
|
399
|
+
if (d5 && SameDomainName(d5, n8)) return(mDNStrue);
|
|
400
|
+
if (d5 && SameDomainName(d5, n9)) return(mDNStrue);
|
|
401
|
+
if (d5 && SameDomainName(d5, nA)) return(mDNStrue);
|
|
402
|
+
if (d5 && SameDomainName(d5, nB)) return(mDNStrue);
|
|
403
|
+
return(mDNSfalse);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
mDNSexport const mDNSu8 *LastLabel(const domainname *d)
|
|
407
|
+
{
|
|
408
|
+
const mDNSu8 *p = d->c;
|
|
409
|
+
while (d->c[0])
|
|
410
|
+
{
|
|
411
|
+
p = d->c;
|
|
412
|
+
d = (const domainname*)(d->c + 1 + d->c[0]);
|
|
413
|
+
}
|
|
414
|
+
return(p);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// Returns length of a domain name INCLUDING the byte for the final null label
|
|
418
|
+
// e.g. for the root label "." it returns one
|
|
419
|
+
// For the FQDN "com." it returns 5 (length byte, three data bytes, final zero)
|
|
420
|
+
// Legal results are 1 (just root label) to 256 (MAX_DOMAIN_NAME)
|
|
421
|
+
// If the given domainname is invalid, result is 257 (MAX_DOMAIN_NAME+1)
|
|
422
|
+
mDNSexport mDNSu16 DomainNameLengthLimit(const domainname *const name, const mDNSu8 *limit)
|
|
423
|
+
{
|
|
424
|
+
const mDNSu8 *src = name->c;
|
|
425
|
+
while (src < limit && *src <= MAX_DOMAIN_LABEL)
|
|
426
|
+
{
|
|
427
|
+
if (*src == 0) return((mDNSu16)(src - name->c + 1));
|
|
428
|
+
src += 1 + *src;
|
|
429
|
+
}
|
|
430
|
+
return(MAX_DOMAIN_NAME+1);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// CompressedDomainNameLength returns the length of a domain name INCLUDING the byte
|
|
434
|
+
// for the final null label, e.g. for the root label "." it returns one.
|
|
435
|
+
// E.g. for the FQDN "foo.com." it returns 9
|
|
436
|
+
// (length, three data bytes, length, three more data bytes, final zero).
|
|
437
|
+
// In the case where a parent domain name is provided, and the given name is a child
|
|
438
|
+
// of that parent, CompressedDomainNameLength returns the length of the prefix portion
|
|
439
|
+
// of the child name, plus TWO bytes for the compression pointer.
|
|
440
|
+
// E.g. for the name "foo.com." with parent "com.", it returns 6
|
|
441
|
+
// (length, three data bytes, two-byte compression pointer).
|
|
442
|
+
mDNSexport mDNSu16 CompressedDomainNameLength(const domainname *const name, const domainname *parent)
|
|
443
|
+
{
|
|
444
|
+
const mDNSu8 *src = name->c;
|
|
445
|
+
if (parent && parent->c[0] == 0) parent = mDNSNULL;
|
|
446
|
+
while (*src)
|
|
447
|
+
{
|
|
448
|
+
if (*src > MAX_DOMAIN_LABEL) return(MAX_DOMAIN_NAME+1);
|
|
449
|
+
if (parent && SameDomainName((const domainname *)src, parent)) return((mDNSu16)(src - name->c + 2));
|
|
450
|
+
src += 1 + *src;
|
|
451
|
+
if (src - name->c >= MAX_DOMAIN_NAME) return(MAX_DOMAIN_NAME+1);
|
|
452
|
+
}
|
|
453
|
+
return((mDNSu16)(src - name->c + 1));
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// CountLabels() returns number of labels in name, excluding final root label
|
|
457
|
+
// (e.g. for "apple.com." CountLabels returns 2.)
|
|
458
|
+
mDNSexport int CountLabels(const domainname *d)
|
|
459
|
+
{
|
|
460
|
+
int count = 0;
|
|
461
|
+
const mDNSu8 *ptr;
|
|
462
|
+
for (ptr = d->c; *ptr; ptr = ptr + ptr[0] + 1) count++;
|
|
463
|
+
return count;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
// SkipLeadingLabels skips over the first 'skip' labels in the domainname,
|
|
467
|
+
// returning a pointer to the suffix with 'skip' labels removed.
|
|
468
|
+
mDNSexport const domainname *SkipLeadingLabels(const domainname *d, int skip)
|
|
469
|
+
{
|
|
470
|
+
while (skip > 0 && d->c[0]) { d = (const domainname *)(d->c + 1 + d->c[0]); skip--; }
|
|
471
|
+
return(d);
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
// AppendLiteralLabelString appends a single label to an existing (possibly empty) domainname.
|
|
475
|
+
// The C string contains the label as-is, with no escaping, etc.
|
|
476
|
+
// Any dots in the name are literal dots, not label separators
|
|
477
|
+
// If successful, AppendLiteralLabelString returns a pointer to the next unused byte
|
|
478
|
+
// in the domainname bufer (i.e. the next byte after the terminating zero).
|
|
479
|
+
// If unable to construct a legal domain name (i.e. label more than 63 bytes, or total more than 256 bytes)
|
|
480
|
+
// AppendLiteralLabelString returns mDNSNULL.
|
|
481
|
+
mDNSexport mDNSu8 *AppendLiteralLabelString(domainname *const name, const char *cstr)
|
|
482
|
+
{
|
|
483
|
+
mDNSu8 * ptr = name->c + DomainNameLength(name) - 1; // Find end of current name
|
|
484
|
+
const mDNSu8 *const lim1 = name->c + MAX_DOMAIN_NAME - 1; // Limit of how much we can add (not counting final zero)
|
|
485
|
+
const mDNSu8 *const lim2 = ptr + 1 + MAX_DOMAIN_LABEL;
|
|
486
|
+
const mDNSu8 *const lim = (lim1 < lim2) ? lim1 : lim2;
|
|
487
|
+
mDNSu8 *lengthbyte = ptr++; // Record where the length is going to go
|
|
488
|
+
|
|
489
|
+
while (*cstr && ptr < lim) *ptr++ = (mDNSu8)*cstr++; // Copy the data
|
|
490
|
+
*lengthbyte = (mDNSu8)(ptr - lengthbyte - 1); // Fill in the length byte
|
|
491
|
+
*ptr++ = 0; // Put the null root label on the end
|
|
492
|
+
if (*cstr) return(mDNSNULL); // Failure: We didn't successfully consume all input
|
|
493
|
+
else return(ptr); // Success: return new value of ptr
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// AppendDNSNameString appends zero or more labels to an existing (possibly empty) domainname.
|
|
497
|
+
// The C string is in conventional DNS syntax:
|
|
498
|
+
// Textual labels, escaped as necessary using the usual DNS '\' notation, separated by dots.
|
|
499
|
+
// If successful, AppendDNSNameString returns a pointer to the next unused byte
|
|
500
|
+
// in the domainname bufer (i.e. the next byte after the terminating zero).
|
|
501
|
+
// If unable to construct a legal domain name (i.e. label more than 63 bytes, or total more than 256 bytes)
|
|
502
|
+
// AppendDNSNameString returns mDNSNULL.
|
|
503
|
+
mDNSexport mDNSu8 *AppendDNSNameString(domainname *const name, const char *cstring)
|
|
504
|
+
{
|
|
505
|
+
const char *cstr = cstring;
|
|
506
|
+
mDNSu8 * ptr = name->c + DomainNameLength(name) - 1; // Find end of current name
|
|
507
|
+
const mDNSu8 *const lim = name->c + MAX_DOMAIN_NAME - 1; // Limit of how much we can add (not counting final zero)
|
|
508
|
+
while (*cstr && ptr < lim) // While more characters, and space to put them...
|
|
509
|
+
{
|
|
510
|
+
mDNSu8 *lengthbyte = ptr++; // Record where the length is going to go
|
|
511
|
+
if (*cstr == '.') { LogMsg("AppendDNSNameString: Illegal empty label in name \"%s\"", cstring); return(mDNSNULL); }
|
|
512
|
+
while (*cstr && *cstr != '.' && ptr < lim) // While we have characters in the label...
|
|
513
|
+
{
|
|
514
|
+
mDNSu8 c = (mDNSu8)*cstr++; // Read the character
|
|
515
|
+
if (c == '\\') // If escape character, check next character
|
|
516
|
+
{
|
|
517
|
+
c = (mDNSu8)*cstr++; // Assume we'll just take the next character
|
|
518
|
+
if (mDNSIsDigit(cstr[-1]) && mDNSIsDigit(cstr[0]) && mDNSIsDigit(cstr[1]))
|
|
519
|
+
{ // If three decimal digits,
|
|
520
|
+
int v0 = cstr[-1] - '0'; // then interpret as three-digit decimal
|
|
521
|
+
int v1 = cstr[ 0] - '0';
|
|
522
|
+
int v2 = cstr[ 1] - '0';
|
|
523
|
+
int val = v0 * 100 + v1 * 10 + v2;
|
|
524
|
+
if (val <= 255) { c = (mDNSu8)val; cstr += 2; } // If valid three-digit decimal value, use it
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
*ptr++ = c; // Write the character
|
|
528
|
+
}
|
|
529
|
+
if (*cstr) cstr++; // Skip over the trailing dot (if present)
|
|
530
|
+
if (ptr - lengthbyte - 1 > MAX_DOMAIN_LABEL) // If illegal label, abort
|
|
531
|
+
return(mDNSNULL);
|
|
532
|
+
*lengthbyte = (mDNSu8)(ptr - lengthbyte - 1); // Fill in the length byte
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
*ptr++ = 0; // Put the null root label on the end
|
|
536
|
+
if (*cstr) return(mDNSNULL); // Failure: We didn't successfully consume all input
|
|
537
|
+
else return(ptr); // Success: return new value of ptr
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// AppendDomainLabel appends a single label to a name.
|
|
541
|
+
// If successful, AppendDomainLabel returns a pointer to the next unused byte
|
|
542
|
+
// in the domainname bufer (i.e. the next byte after the terminating zero).
|
|
543
|
+
// If unable to construct a legal domain name (i.e. label more than 63 bytes, or total more than 256 bytes)
|
|
544
|
+
// AppendDomainLabel returns mDNSNULL.
|
|
545
|
+
mDNSexport mDNSu8 *AppendDomainLabel(domainname *const name, const domainlabel *const label)
|
|
546
|
+
{
|
|
547
|
+
int i;
|
|
548
|
+
mDNSu8 *ptr = name->c + DomainNameLength(name) - 1;
|
|
549
|
+
|
|
550
|
+
// Check label is legal
|
|
551
|
+
if (label->c[0] > MAX_DOMAIN_LABEL) return(mDNSNULL);
|
|
552
|
+
|
|
553
|
+
// Check that ptr + length byte + data bytes + final zero does not exceed our limit
|
|
554
|
+
if (ptr + 1 + label->c[0] + 1 > name->c + MAX_DOMAIN_NAME) return(mDNSNULL);
|
|
555
|
+
|
|
556
|
+
for (i=0; i<=label->c[0]; i++) *ptr++ = label->c[i]; // Copy the label data
|
|
557
|
+
*ptr++ = 0; // Put the null root label on the end
|
|
558
|
+
return(ptr);
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
mDNSexport mDNSu8 *AppendDomainName(domainname *const name, const domainname *const append)
|
|
562
|
+
{
|
|
563
|
+
mDNSu8 * ptr = name->c + DomainNameLength(name) - 1; // Find end of current name
|
|
564
|
+
const mDNSu8 *const lim = name->c + MAX_DOMAIN_NAME - 1; // Limit of how much we can add (not counting final zero)
|
|
565
|
+
const mDNSu8 * src = append->c;
|
|
566
|
+
while (src[0])
|
|
567
|
+
{
|
|
568
|
+
int i;
|
|
569
|
+
if (ptr + 1 + src[0] > lim) return(mDNSNULL);
|
|
570
|
+
for (i=0; i<=src[0]; i++) *ptr++ = src[i];
|
|
571
|
+
*ptr = 0; // Put the null root label on the end
|
|
572
|
+
src += i;
|
|
573
|
+
}
|
|
574
|
+
return(ptr);
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
// MakeDomainLabelFromLiteralString makes a single domain label from a single literal C string (with no escaping).
|
|
578
|
+
// If successful, MakeDomainLabelFromLiteralString returns mDNStrue.
|
|
579
|
+
// If unable to convert the whole string to a legal domain label (i.e. because length is more than 63 bytes) then
|
|
580
|
+
// MakeDomainLabelFromLiteralString makes a legal domain label from the first 63 bytes of the string and returns mDNSfalse.
|
|
581
|
+
// In some cases silently truncated oversized names to 63 bytes is acceptable, so the return result may be ignored.
|
|
582
|
+
// In other cases silent truncation may not be acceptable, so in those cases the calling function needs to check the return result.
|
|
583
|
+
mDNSexport mDNSBool MakeDomainLabelFromLiteralString(domainlabel *const label, const char *cstr)
|
|
584
|
+
{
|
|
585
|
+
mDNSu8 * ptr = label->c + 1; // Where we're putting it
|
|
586
|
+
const mDNSu8 *const limit = label->c + 1 + MAX_DOMAIN_LABEL; // The maximum we can put
|
|
587
|
+
while (*cstr && ptr < limit) *ptr++ = (mDNSu8)*cstr++; // Copy the label
|
|
588
|
+
label->c[0] = (mDNSu8)(ptr - label->c - 1); // Set the length byte
|
|
589
|
+
return(*cstr == 0); // Return mDNStrue if we successfully consumed all input
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
// MakeDomainNameFromDNSNameString makes a native DNS-format domainname from a C string.
|
|
593
|
+
// The C string is in conventional DNS syntax:
|
|
594
|
+
// Textual labels, escaped as necessary using the usual DNS '\' notation, separated by dots.
|
|
595
|
+
// If successful, MakeDomainNameFromDNSNameString returns a pointer to the next unused byte
|
|
596
|
+
// in the domainname bufer (i.e. the next byte after the terminating zero).
|
|
597
|
+
// If unable to construct a legal domain name (i.e. label more than 63 bytes, or total more than 256 bytes)
|
|
598
|
+
// MakeDomainNameFromDNSNameString returns mDNSNULL.
|
|
599
|
+
mDNSexport mDNSu8 *MakeDomainNameFromDNSNameString(domainname *const name, const char *cstr)
|
|
600
|
+
{
|
|
601
|
+
name->c[0] = 0; // Make an empty domain name
|
|
602
|
+
return(AppendDNSNameString(name, cstr)); // And then add this string to it
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
mDNSexport char *ConvertDomainLabelToCString_withescape(const domainlabel *const label, char *ptr, char esc)
|
|
606
|
+
{
|
|
607
|
+
const mDNSu8 * src = label->c; // Domain label we're reading
|
|
608
|
+
const mDNSu8 len = *src++; // Read length of this (non-null) label
|
|
609
|
+
const mDNSu8 *const end = src + len; // Work out where the label ends
|
|
610
|
+
if (len > MAX_DOMAIN_LABEL) return(mDNSNULL); // If illegal label, abort
|
|
611
|
+
while (src < end) // While we have characters in the label
|
|
612
|
+
{
|
|
613
|
+
mDNSu8 c = *src++;
|
|
614
|
+
if (esc)
|
|
615
|
+
{
|
|
616
|
+
if (c == '.' || c == esc) // If character is a dot or the escape character
|
|
617
|
+
*ptr++ = esc; // Output escape character
|
|
618
|
+
else if (c <= ' ') // If non-printing ascii,
|
|
619
|
+
{ // Output decimal escape sequence
|
|
620
|
+
*ptr++ = esc;
|
|
621
|
+
*ptr++ = (char) ('0' + (c / 100) );
|
|
622
|
+
*ptr++ = (char) ('0' + (c / 10) % 10);
|
|
623
|
+
c = (mDNSu8)('0' + (c ) % 10);
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
*ptr++ = (char)c; // Copy the character
|
|
627
|
+
}
|
|
628
|
+
*ptr = 0; // Null-terminate the string
|
|
629
|
+
return(ptr); // and return
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
// Note: To guarantee that there will be no possible overrun, cstr must be at least MAX_ESCAPED_DOMAIN_NAME (1009 bytes)
|
|
633
|
+
mDNSexport char *ConvertDomainNameToCString_withescape(const domainname *const name, char *ptr, char esc)
|
|
634
|
+
{
|
|
635
|
+
const mDNSu8 *src = name->c; // Domain name we're reading
|
|
636
|
+
const mDNSu8 *const max = name->c + MAX_DOMAIN_NAME; // Maximum that's valid
|
|
637
|
+
|
|
638
|
+
if (*src == 0) *ptr++ = '.'; // Special case: For root, just write a dot
|
|
639
|
+
|
|
640
|
+
while (*src) // While more characters in the domain name
|
|
641
|
+
{
|
|
642
|
+
if (src + 1 + *src >= max) return(mDNSNULL);
|
|
643
|
+
ptr = ConvertDomainLabelToCString_withescape((const domainlabel *)src, ptr, esc);
|
|
644
|
+
if (!ptr) return(mDNSNULL);
|
|
645
|
+
src += 1 + *src;
|
|
646
|
+
*ptr++ = '.'; // Write the dot after the label
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
*ptr++ = 0; // Null-terminate the string
|
|
650
|
+
return(ptr); // and return
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
// RFC 1034 rules:
|
|
654
|
+
// Host names must start with a letter, end with a letter or digit,
|
|
655
|
+
// and have as interior characters only letters, digits, and hyphen.
|
|
656
|
+
// This was subsequently modified in RFC 1123 to allow the first character to be either a letter or a digit
|
|
657
|
+
|
|
658
|
+
mDNSexport void ConvertUTF8PstringToRFC1034HostLabel(const mDNSu8 UTF8Name[], domainlabel *const hostlabel)
|
|
659
|
+
{
|
|
660
|
+
const mDNSu8 * src = &UTF8Name[1];
|
|
661
|
+
const mDNSu8 *const end = &UTF8Name[1] + UTF8Name[0];
|
|
662
|
+
mDNSu8 * ptr = &hostlabel->c[1];
|
|
663
|
+
const mDNSu8 *const lim = &hostlabel->c[1] + MAX_DOMAIN_LABEL;
|
|
664
|
+
while (src < end)
|
|
665
|
+
{
|
|
666
|
+
// Delete apostrophes from source name
|
|
667
|
+
if (src[0] == '\'') { src++; continue; } // Standard straight single quote
|
|
668
|
+
if (src + 2 < end && src[0] == 0xE2 && src[1] == 0x80 && src[2] == 0x99)
|
|
669
|
+
{ src += 3; continue; } // Unicode curly apostrophe
|
|
670
|
+
if (ptr < lim)
|
|
671
|
+
{
|
|
672
|
+
if (mDNSValidHostChar(*src, (ptr > &hostlabel->c[1]), (src < end-1))) *ptr++ = *src;
|
|
673
|
+
else if (ptr > &hostlabel->c[1] && ptr[-1] != '-') *ptr++ = '-';
|
|
674
|
+
}
|
|
675
|
+
src++;
|
|
676
|
+
}
|
|
677
|
+
while (ptr > &hostlabel->c[1] && ptr[-1] == '-') ptr--; // Truncate trailing '-' marks
|
|
678
|
+
hostlabel->c[0] = (mDNSu8)(ptr - &hostlabel->c[1]);
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
#define ValidTransportProtocol(X) ( (X)[0] == 4 && (X)[1] == '_' && \
|
|
682
|
+
((((X)[2] | 0x20) == 'u' && ((X)[3] | 0x20) == 'd') || (((X)[2] | 0x20) == 't' && ((X)[3] | 0x20) == 'c')) && \
|
|
683
|
+
((X)[4] | 0x20) == 'p')
|
|
684
|
+
|
|
685
|
+
mDNSexport mDNSu8 *ConstructServiceName(domainname *const fqdn,
|
|
686
|
+
const domainlabel *name, const domainname *type, const domainname *const domain)
|
|
687
|
+
{
|
|
688
|
+
int i, len;
|
|
689
|
+
mDNSu8 *dst = fqdn->c;
|
|
690
|
+
const mDNSu8 *src;
|
|
691
|
+
const char *errormsg;
|
|
692
|
+
#if APPLE_OSX_mDNSResponder
|
|
693
|
+
mDNSBool loggedUnderscore = mDNSfalse;
|
|
694
|
+
static char typeBuf[MAX_ESCAPED_DOMAIN_NAME];
|
|
695
|
+
#endif
|
|
696
|
+
|
|
697
|
+
// In the case where there is no name (and ONLY in that case),
|
|
698
|
+
// a single-label subtype is allowed as the first label of a three-part "type"
|
|
699
|
+
if (!name && type)
|
|
700
|
+
{
|
|
701
|
+
const mDNSu8 *s0 = type->c;
|
|
702
|
+
if (s0[0] && s0[0] < 0x40) // If legal first label (at least one character, and no more than 63)
|
|
703
|
+
{
|
|
704
|
+
const mDNSu8 * s1 = s0 + 1 + s0[0];
|
|
705
|
+
if (s1[0] && s1[0] < 0x40) // and legal second label (at least one character, and no more than 63)
|
|
706
|
+
{
|
|
707
|
+
const mDNSu8 *s2 = s1 + 1 + s1[0];
|
|
708
|
+
if (s2[0] && s2[0] < 0x40 && s2[1+s2[0]] == 0) // and we have three and only three labels
|
|
709
|
+
{
|
|
710
|
+
static const mDNSu8 SubTypeLabel[5] = "\x04_sub";
|
|
711
|
+
src = s0; // Copy the first label
|
|
712
|
+
len = *src;
|
|
713
|
+
for (i=0; i <= len; i++) *dst++ = *src++;
|
|
714
|
+
for (i=0; i < (int)sizeof(SubTypeLabel); i++) *dst++ = SubTypeLabel[i];
|
|
715
|
+
type = (const domainname *)s1;
|
|
716
|
+
|
|
717
|
+
// Special support to enable the DNSServiceBrowse call made by Bonjour Browser
|
|
718
|
+
// For these queries, we retract the "._sub" we just added between the subtype and the main type
|
|
719
|
+
// Remove after Bonjour Browser is updated to use DNSServiceQueryRecord instead of DNSServiceBrowse
|
|
720
|
+
if (SameDomainName((domainname*)s0, (const domainname*)"\x09_services\x07_dns-sd\x04_udp"))
|
|
721
|
+
dst -= sizeof(SubTypeLabel);
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
if (name && name->c[0])
|
|
728
|
+
{
|
|
729
|
+
src = name->c; // Put the service name into the domain name
|
|
730
|
+
len = *src;
|
|
731
|
+
if (len >= 0x40) { errormsg = "Service instance name too long"; goto fail; }
|
|
732
|
+
for (i=0; i<=len; i++) *dst++ = *src++;
|
|
733
|
+
}
|
|
734
|
+
else
|
|
735
|
+
name = (domainlabel*)""; // Set this up to be non-null, to avoid errors if we have to call LogMsg() below
|
|
736
|
+
|
|
737
|
+
src = type->c; // Put the service type into the domain name
|
|
738
|
+
len = *src;
|
|
739
|
+
if (len < 2 || len > 16)
|
|
740
|
+
{
|
|
741
|
+
LogMsg("Bad service type in %#s.%##s%##s Application protocol name must be underscore plus 1-15 characters. "
|
|
742
|
+
"See <http://www.dns-sd.org/ServiceTypes.html>", name->c, type->c, domain->c);
|
|
743
|
+
#if APPLE_OSX_mDNSResponder
|
|
744
|
+
ConvertDomainNameToCString(type, typeBuf);
|
|
745
|
+
mDNSASLLog(mDNSNULL, "serviceType.nameTooLong", "noop", typeBuf, "");
|
|
746
|
+
#endif
|
|
747
|
+
}
|
|
748
|
+
if (len < 2 || len >= 0x40 || (len > 16 && !SameDomainName(domain, &localdomain))) return(mDNSNULL);
|
|
749
|
+
if (src[1] != '_') { errormsg = "Application protocol name must begin with underscore"; goto fail; }
|
|
750
|
+
for (i=2; i<=len; i++)
|
|
751
|
+
{
|
|
752
|
+
// Letters and digits are allowed anywhere
|
|
753
|
+
if (mDNSIsLetter(src[i]) || mDNSIsDigit(src[i])) continue;
|
|
754
|
+
// Hyphens are only allowed as interior characters
|
|
755
|
+
// Underscores are not supposed to be allowed at all, but for backwards compatibility with some old products we do allow them,
|
|
756
|
+
// with the same rule as hyphens
|
|
757
|
+
if ((src[i] == '-' || src[i] == '_') && i > 2 && i < len)
|
|
758
|
+
{
|
|
759
|
+
#if APPLE_OSX_mDNSResponder
|
|
760
|
+
if (src[i] == '_' && loggedUnderscore == mDNSfalse)
|
|
761
|
+
{
|
|
762
|
+
ConvertDomainNameToCString(type, typeBuf);
|
|
763
|
+
mDNSASLLog(mDNSNULL, "serviceType.nameWithUnderscore", "noop", typeBuf, "");
|
|
764
|
+
loggedUnderscore = mDNStrue;
|
|
765
|
+
}
|
|
766
|
+
#endif
|
|
767
|
+
continue;
|
|
768
|
+
}
|
|
769
|
+
errormsg = "Application protocol name must contain only letters, digits, and hyphens";
|
|
770
|
+
#if APPLE_OSX_mDNSResponder
|
|
771
|
+
{
|
|
772
|
+
ConvertDomainNameToCString(type, typeBuf);
|
|
773
|
+
mDNSASLLog(mDNSNULL, "serviceType.nameWithIllegalCharacters", "noop", typeBuf, "");
|
|
774
|
+
}
|
|
775
|
+
#endif
|
|
776
|
+
goto fail;
|
|
777
|
+
}
|
|
778
|
+
for (i=0; i<=len; i++) *dst++ = *src++;
|
|
779
|
+
|
|
780
|
+
len = *src;
|
|
781
|
+
if (!ValidTransportProtocol(src)) { errormsg = "Transport protocol name must be _udp or _tcp"; goto fail; }
|
|
782
|
+
for (i=0; i<=len; i++) *dst++ = *src++;
|
|
783
|
+
|
|
784
|
+
if (*src) { errormsg = "Service type must have only two labels"; goto fail; }
|
|
785
|
+
|
|
786
|
+
*dst = 0;
|
|
787
|
+
if (!domain->c[0]) { errormsg = "Service domain must be non-empty"; goto fail; }
|
|
788
|
+
if (SameDomainName(domain, (const domainname*)"\x05" "local" "\x04" "arpa"))
|
|
789
|
+
{ errormsg = "Illegal domain \"local.arpa.\" Use \"local.\" (or empty string)"; goto fail; }
|
|
790
|
+
dst = AppendDomainName(fqdn, domain);
|
|
791
|
+
if (!dst) { errormsg = "Service domain too long"; goto fail; }
|
|
792
|
+
return(dst);
|
|
793
|
+
|
|
794
|
+
fail:
|
|
795
|
+
LogMsg("ConstructServiceName: %s: %#s.%##s%##s", errormsg, name->c, type->c, domain->c);
|
|
796
|
+
return(mDNSNULL);
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
// A service name has the form: instance.application-protocol.transport-protocol.domain
|
|
800
|
+
// DeconstructServiceName is currently fairly forgiving: It doesn't try to enforce character
|
|
801
|
+
// set or length limits for the protocol names, and the final domain is allowed to be empty.
|
|
802
|
+
// However, if the given FQDN doesn't contain at least three labels,
|
|
803
|
+
// DeconstructServiceName will reject it and return mDNSfalse.
|
|
804
|
+
mDNSexport mDNSBool DeconstructServiceName(const domainname *const fqdn,
|
|
805
|
+
domainlabel *const name, domainname *const type, domainname *const domain)
|
|
806
|
+
{
|
|
807
|
+
int i, len;
|
|
808
|
+
const mDNSu8 *src = fqdn->c;
|
|
809
|
+
const mDNSu8 *max = fqdn->c + MAX_DOMAIN_NAME;
|
|
810
|
+
mDNSu8 *dst;
|
|
811
|
+
|
|
812
|
+
dst = name->c; // Extract the service name
|
|
813
|
+
len = *src;
|
|
814
|
+
if (!len) { debugf("DeconstructServiceName: FQDN empty!"); return(mDNSfalse); }
|
|
815
|
+
if (len >= 0x40) { debugf("DeconstructServiceName: Instance name too long"); return(mDNSfalse); }
|
|
816
|
+
for (i=0; i<=len; i++) *dst++ = *src++;
|
|
817
|
+
|
|
818
|
+
dst = type->c; // Extract the service type
|
|
819
|
+
len = *src;
|
|
820
|
+
if (!len) { debugf("DeconstructServiceName: FQDN contains only one label!"); return(mDNSfalse); }
|
|
821
|
+
if (len >= 0x40) { debugf("DeconstructServiceName: Application protocol name too long"); return(mDNSfalse); }
|
|
822
|
+
if (src[1] != '_'){ debugf("DeconstructServiceName: No _ at start of application protocol"); return(mDNSfalse); }
|
|
823
|
+
for (i=0; i<=len; i++) *dst++ = *src++;
|
|
824
|
+
|
|
825
|
+
len = *src;
|
|
826
|
+
if (!len) { debugf("DeconstructServiceName: FQDN contains only two labels!"); return(mDNSfalse); }
|
|
827
|
+
if (!ValidTransportProtocol(src))
|
|
828
|
+
{ debugf("DeconstructServiceName: Transport protocol must be _udp or _tcp");
|
|
829
|
+
#ifndef EMBEDDED
|
|
830
|
+
return(mDNSfalse);
|
|
831
|
+
#endif
|
|
832
|
+
}
|
|
833
|
+
for (i=0; i<=len; i++) *dst++ = *src++;
|
|
834
|
+
*dst++ = 0; // Put terminator on the end of service type
|
|
835
|
+
|
|
836
|
+
dst = domain->c; // Extract the service domain
|
|
837
|
+
while (*src)
|
|
838
|
+
{
|
|
839
|
+
len = *src;
|
|
840
|
+
if (len >= 0x40)
|
|
841
|
+
{ debugf("DeconstructServiceName: Label in service domain too long"); return(mDNSfalse); }
|
|
842
|
+
if (src + 1 + len + 1 >= max)
|
|
843
|
+
{ debugf("DeconstructServiceName: Total service domain too long"); return(mDNSfalse); }
|
|
844
|
+
for (i=0; i<=len; i++) *dst++ = *src++;
|
|
845
|
+
}
|
|
846
|
+
*dst++ = 0; // Put the null root label on the end
|
|
847
|
+
|
|
848
|
+
return(mDNStrue);
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
// Notes on UTF-8:
|
|
852
|
+
// 0xxxxxxx represents a 7-bit ASCII value from 0x00 to 0x7F
|
|
853
|
+
// 10xxxxxx is a continuation byte of a multi-byte character
|
|
854
|
+
// 110xxxxx is the first byte of a 2-byte character (11 effective bits; values 0x 80 - 0x 800-1)
|
|
855
|
+
// 1110xxxx is the first byte of a 3-byte character (16 effective bits; values 0x 800 - 0x 10000-1)
|
|
856
|
+
// 11110xxx is the first byte of a 4-byte character (21 effective bits; values 0x 10000 - 0x 200000-1)
|
|
857
|
+
// 111110xx is the first byte of a 5-byte character (26 effective bits; values 0x 200000 - 0x 4000000-1)
|
|
858
|
+
// 1111110x is the first byte of a 6-byte character (31 effective bits; values 0x4000000 - 0x80000000-1)
|
|
859
|
+
//
|
|
860
|
+
// UTF-16 surrogate pairs are used in UTF-16 to encode values larger than 0xFFFF.
|
|
861
|
+
// Although UTF-16 surrogate pairs are not supposed to appear in legal UTF-8, we want to be defensive
|
|
862
|
+
// about that too. (See <http://www.unicode.org/faq/utf_bom.html#34>, "What are surrogates?")
|
|
863
|
+
// The first of pair is a UTF-16 value in the range 0xD800-0xDBFF (11101101 1010xxxx 10xxxxxx in UTF-8),
|
|
864
|
+
// and the second is a UTF-16 value in the range 0xDC00-0xDFFF (11101101 1011xxxx 10xxxxxx in UTF-8).
|
|
865
|
+
|
|
866
|
+
mDNSexport mDNSu32 TruncateUTF8ToLength(mDNSu8 *string, mDNSu32 length, mDNSu32 max)
|
|
867
|
+
{
|
|
868
|
+
if (length > max)
|
|
869
|
+
{
|
|
870
|
+
mDNSu8 c1 = string[max]; // First byte after cut point
|
|
871
|
+
mDNSu8 c2 = (max+1 < length) ? string[max+1] : (mDNSu8)0xB0; // Second byte after cut point
|
|
872
|
+
length = max; // Trim length down
|
|
873
|
+
while (length > 0)
|
|
874
|
+
{
|
|
875
|
+
// Check if the byte right after the chop point is a UTF-8 continuation byte,
|
|
876
|
+
// or if the character right after the chop point is the second of a UTF-16 surrogate pair.
|
|
877
|
+
// If so, then we continue to chop more bytes until we get to a legal chop point.
|
|
878
|
+
mDNSBool continuation = ((c1 & 0xC0) == 0x80);
|
|
879
|
+
mDNSBool secondsurrogate = (c1 == 0xED && (c2 & 0xF0) == 0xB0);
|
|
880
|
+
if (!continuation && !secondsurrogate) break;
|
|
881
|
+
c2 = c1;
|
|
882
|
+
c1 = string[--length];
|
|
883
|
+
}
|
|
884
|
+
// Having truncated characters off the end of our string, also cut off any residual white space
|
|
885
|
+
while (length > 0 && string[length-1] <= ' ') length--;
|
|
886
|
+
}
|
|
887
|
+
return(length);
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
// Returns true if a rich text label ends in " (nnn)", or if an RFC 1034
|
|
891
|
+
// name ends in "-nnn", where n is some decimal number.
|
|
892
|
+
mDNSexport mDNSBool LabelContainsSuffix(const domainlabel *const name, const mDNSBool RichText)
|
|
893
|
+
{
|
|
894
|
+
mDNSu16 l = name->c[0];
|
|
895
|
+
|
|
896
|
+
if (RichText)
|
|
897
|
+
{
|
|
898
|
+
if (l < 4) return mDNSfalse; // Need at least " (2)"
|
|
899
|
+
if (name->c[l--] != ')') return mDNSfalse; // Last char must be ')'
|
|
900
|
+
if (!mDNSIsDigit(name->c[l])) return mDNSfalse; // Preceeded by a digit
|
|
901
|
+
l--;
|
|
902
|
+
while (l > 2 && mDNSIsDigit(name->c[l])) l--; // Strip off digits
|
|
903
|
+
return (name->c[l] == '(' && name->c[l - 1] == ' ');
|
|
904
|
+
}
|
|
905
|
+
else
|
|
906
|
+
{
|
|
907
|
+
if (l < 2) return mDNSfalse; // Need at least "-2"
|
|
908
|
+
if (!mDNSIsDigit(name->c[l])) return mDNSfalse; // Last char must be a digit
|
|
909
|
+
l--;
|
|
910
|
+
while (l > 2 && mDNSIsDigit(name->c[l])) l--; // Strip off digits
|
|
911
|
+
return (name->c[l] == '-');
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
// removes an auto-generated suffix (appended on a name collision) from a label. caller is
|
|
916
|
+
// responsible for ensuring that the label does indeed contain a suffix. returns the number
|
|
917
|
+
// from the suffix that was removed.
|
|
918
|
+
mDNSexport mDNSu32 RemoveLabelSuffix(domainlabel *name, mDNSBool RichText)
|
|
919
|
+
{
|
|
920
|
+
mDNSu32 val = 0, multiplier = 1;
|
|
921
|
+
|
|
922
|
+
// Chop closing parentheses from RichText suffix
|
|
923
|
+
if (RichText && name->c[0] >= 1 && name->c[name->c[0]] == ')') name->c[0]--;
|
|
924
|
+
|
|
925
|
+
// Get any existing numerical suffix off the name
|
|
926
|
+
while (mDNSIsDigit(name->c[name->c[0]]))
|
|
927
|
+
{ val += (name->c[name->c[0]] - '0') * multiplier; multiplier *= 10; name->c[0]--; }
|
|
928
|
+
|
|
929
|
+
// Chop opening parentheses or dash from suffix
|
|
930
|
+
if (RichText)
|
|
931
|
+
{
|
|
932
|
+
if (name->c[0] >= 2 && name->c[name->c[0]] == '(' && name->c[name->c[0]-1] == ' ') name->c[0] -= 2;
|
|
933
|
+
}
|
|
934
|
+
else
|
|
935
|
+
{
|
|
936
|
+
if (name->c[0] >= 1 && name->c[name->c[0]] == '-') name->c[0] -= 1;
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
return(val);
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
// appends a numerical suffix to a label, with the number following a whitespace and enclosed
|
|
943
|
+
// in parentheses (rich text) or following two consecutive hyphens (RFC 1034 domain label).
|
|
944
|
+
mDNSexport void AppendLabelSuffix(domainlabel *const name, mDNSu32 val, const mDNSBool RichText)
|
|
945
|
+
{
|
|
946
|
+
mDNSu32 divisor = 1, chars = 2; // Shortest possible RFC1034 name suffix is 2 characters ("-2")
|
|
947
|
+
if (RichText) chars = 4; // Shortest possible RichText suffix is 4 characters (" (2)")
|
|
948
|
+
|
|
949
|
+
// Truncate trailing spaces from RichText names
|
|
950
|
+
if (RichText) while (name->c[name->c[0]] == ' ') name->c[0]--;
|
|
951
|
+
|
|
952
|
+
while (divisor < 0xFFFFFFFFUL/10 && val >= divisor * 10) { divisor *= 10; chars++; }
|
|
953
|
+
|
|
954
|
+
name->c[0] = (mDNSu8) TruncateUTF8ToLength(name->c+1, name->c[0], MAX_DOMAIN_LABEL - chars);
|
|
955
|
+
|
|
956
|
+
if (RichText) { name->c[++name->c[0]] = ' '; name->c[++name->c[0]] = '('; }
|
|
957
|
+
else { name->c[++name->c[0]] = '-'; }
|
|
958
|
+
|
|
959
|
+
while (divisor)
|
|
960
|
+
{
|
|
961
|
+
name->c[++name->c[0]] = (mDNSu8)('0' + val / divisor);
|
|
962
|
+
val %= divisor;
|
|
963
|
+
divisor /= 10;
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
if (RichText) name->c[++name->c[0]] = ')';
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
mDNSexport void IncrementLabelSuffix(domainlabel *name, mDNSBool RichText)
|
|
970
|
+
{
|
|
971
|
+
mDNSu32 val = 0;
|
|
972
|
+
|
|
973
|
+
if (LabelContainsSuffix(name, RichText))
|
|
974
|
+
val = RemoveLabelSuffix(name, RichText);
|
|
975
|
+
|
|
976
|
+
// If no existing suffix, start by renaming "Foo" as "Foo (2)" or "Foo-2" as appropriate.
|
|
977
|
+
// If existing suffix in the range 2-9, increment it.
|
|
978
|
+
// If we've had ten conflicts already, there are probably too many hosts trying to use the same name,
|
|
979
|
+
// so add a random increment to improve the chances of finding an available name next time.
|
|
980
|
+
if (val == 0) val = 2;
|
|
981
|
+
else if (val < 10) val++;
|
|
982
|
+
else val += 1 + mDNSRandom(99);
|
|
983
|
+
|
|
984
|
+
AppendLabelSuffix(name, val, RichText);
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
// ***************************************************************************
|
|
988
|
+
#if COMPILER_LIKES_PRAGMA_MARK
|
|
989
|
+
#pragma mark -
|
|
990
|
+
#pragma mark - Resource Record Utility Functions
|
|
991
|
+
#endif
|
|
992
|
+
|
|
993
|
+
// Set up a AuthRecord with sensible default values.
|
|
994
|
+
// These defaults may be overwritten with new values before mDNS_Register is called
|
|
995
|
+
mDNSexport void mDNS_SetupResourceRecord(AuthRecord *rr, RData *RDataStorage, mDNSInterfaceID InterfaceID,
|
|
996
|
+
mDNSu16 rrtype, mDNSu32 ttl, mDNSu8 RecordType, AuthRecType artype, mDNSRecordCallback Callback, void *Context)
|
|
997
|
+
{
|
|
998
|
+
//
|
|
999
|
+
// LocalOnly auth record can be created with LocalOnly InterfaceID or a valid InterfaceID.
|
|
1000
|
+
// Most of the applications normally create with LocalOnly InterfaceID and we store them as
|
|
1001
|
+
// such, so that we can deliver the response to questions that specify LocalOnly InterfaceID.
|
|
1002
|
+
// LocalOnly resource records can also be created with valid InterfaceID which happens today
|
|
1003
|
+
// when we create LocalOnly records for /etc/hosts.
|
|
1004
|
+
|
|
1005
|
+
if (InterfaceID == mDNSInterface_LocalOnly && artype != AuthRecordLocalOnly)
|
|
1006
|
+
{
|
|
1007
|
+
LogMsg("mDNS_SetupResourceRecord: ERROR!! Mismatch LocalOnly record InterfaceID %p called with artype %d", InterfaceID, artype);
|
|
1008
|
+
return;
|
|
1009
|
+
}
|
|
1010
|
+
else if (InterfaceID == mDNSInterface_P2P && artype != AuthRecordP2P)
|
|
1011
|
+
{
|
|
1012
|
+
LogMsg("mDNS_SetupResourceRecord: ERROR!! Mismatch P2P record InterfaceID %p called with artype %d", InterfaceID, artype);
|
|
1013
|
+
return;
|
|
1014
|
+
}
|
|
1015
|
+
else if (!InterfaceID && (artype == AuthRecordP2P || artype == AuthRecordLocalOnly))
|
|
1016
|
+
{
|
|
1017
|
+
LogMsg("mDNS_SetupResourceRecord: ERROR!! Mismatch InterfaceAny record InterfaceID %p called with artype %d", InterfaceID, artype);
|
|
1018
|
+
return;
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
// Don't try to store a TTL bigger than we can represent in platform time units
|
|
1022
|
+
if (ttl > 0x7FFFFFFFUL / mDNSPlatformOneSecond)
|
|
1023
|
+
ttl = 0x7FFFFFFFUL / mDNSPlatformOneSecond;
|
|
1024
|
+
else if (ttl == 0) // And Zero TTL is illegal
|
|
1025
|
+
ttl = DefaultTTLforRRType(rrtype);
|
|
1026
|
+
|
|
1027
|
+
// Field Group 1: The actual information pertaining to this resource record
|
|
1028
|
+
rr->resrec.RecordType = RecordType;
|
|
1029
|
+
rr->resrec.InterfaceID = InterfaceID;
|
|
1030
|
+
rr->resrec.name = &rr->namestorage;
|
|
1031
|
+
rr->resrec.rrtype = rrtype;
|
|
1032
|
+
rr->resrec.rrclass = kDNSClass_IN;
|
|
1033
|
+
rr->resrec.rroriginalttl = ttl;
|
|
1034
|
+
rr->resrec.rDNSServer = mDNSNULL;
|
|
1035
|
+
// rr->resrec.rdlength = MUST set by client and/or in mDNS_Register_internal
|
|
1036
|
+
// rr->resrec.rdestimate = set in mDNS_Register_internal
|
|
1037
|
+
// rr->resrec.rdata = MUST be set by client
|
|
1038
|
+
|
|
1039
|
+
if (RDataStorage)
|
|
1040
|
+
rr->resrec.rdata = RDataStorage;
|
|
1041
|
+
else
|
|
1042
|
+
{
|
|
1043
|
+
rr->resrec.rdata = &rr->rdatastorage;
|
|
1044
|
+
rr->resrec.rdata->MaxRDLength = sizeof(RDataBody);
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
// Field Group 2: Persistent metadata for Authoritative Records
|
|
1048
|
+
rr->Additional1 = mDNSNULL;
|
|
1049
|
+
rr->Additional2 = mDNSNULL;
|
|
1050
|
+
rr->DependentOn = mDNSNULL;
|
|
1051
|
+
rr->RRSet = mDNSNULL;
|
|
1052
|
+
rr->RecordCallback = Callback;
|
|
1053
|
+
rr->RecordContext = Context;
|
|
1054
|
+
|
|
1055
|
+
rr->AutoTarget = Target_Manual;
|
|
1056
|
+
rr->AllowRemoteQuery = mDNSfalse;
|
|
1057
|
+
rr->ForceMCast = mDNSfalse;
|
|
1058
|
+
|
|
1059
|
+
rr->WakeUp = zeroOwner;
|
|
1060
|
+
rr->AddressProxy = zeroAddr;
|
|
1061
|
+
rr->TimeRcvd = 0;
|
|
1062
|
+
rr->TimeExpire = 0;
|
|
1063
|
+
rr->ARType = artype;
|
|
1064
|
+
|
|
1065
|
+
// Field Group 3: Transient state for Authoritative Records (set in mDNS_Register_internal)
|
|
1066
|
+
// Field Group 4: Transient uDNS state for Authoritative Records (set in mDNS_Register_internal)
|
|
1067
|
+
|
|
1068
|
+
// For now, until the uDNS code is fully integrated, it's helpful to zero the uDNS state fields here too, just in case
|
|
1069
|
+
// (e.g. uDNS_RegisterService short-circuits the usual mDNS_Register_internal record registration calls, so a bunch
|
|
1070
|
+
// of fields don't get set up properly. In particular, if we don't zero rr->QueuedRData then the uDNS code crashes.)
|
|
1071
|
+
rr->state = regState_Zero;
|
|
1072
|
+
rr->uselease = 0;
|
|
1073
|
+
rr->expire = 0;
|
|
1074
|
+
rr->Private = 0;
|
|
1075
|
+
rr->updateid = zeroID;
|
|
1076
|
+
rr->zone = rr->resrec.name;
|
|
1077
|
+
rr->nta = mDNSNULL;
|
|
1078
|
+
rr->tcp = mDNSNULL;
|
|
1079
|
+
rr->OrigRData = 0;
|
|
1080
|
+
rr->OrigRDLen = 0;
|
|
1081
|
+
rr->InFlightRData = 0;
|
|
1082
|
+
rr->InFlightRDLen = 0;
|
|
1083
|
+
rr->QueuedRData = 0;
|
|
1084
|
+
rr->QueuedRDLen = 0;
|
|
1085
|
+
mDNSPlatformMemZero(&rr->NATinfo, sizeof(rr->NATinfo));
|
|
1086
|
+
rr->SRVChanged = mDNSfalse;
|
|
1087
|
+
rr->mState = mergeState_Zero;
|
|
1088
|
+
|
|
1089
|
+
rr->namestorage.c[0] = 0; // MUST be set by client before calling mDNS_Register()
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
mDNSexport void mDNS_SetupQuestion(DNSQuestion *const q, const mDNSInterfaceID InterfaceID, const domainname *const name,
|
|
1093
|
+
const mDNSu16 qtype, mDNSQuestionCallback *const callback, void *const context)
|
|
1094
|
+
{
|
|
1095
|
+
q->InterfaceID = InterfaceID;
|
|
1096
|
+
q->Target = zeroAddr;
|
|
1097
|
+
AssignDomainName(&q->qname, name);
|
|
1098
|
+
q->qtype = qtype;
|
|
1099
|
+
q->qclass = kDNSClass_IN;
|
|
1100
|
+
q->LongLived = (qtype == kDNSType_PTR);
|
|
1101
|
+
q->ExpectUnique = (qtype != kDNSType_PTR);
|
|
1102
|
+
q->ForceMCast = mDNSfalse;
|
|
1103
|
+
q->ReturnIntermed = mDNSfalse;
|
|
1104
|
+
q->SuppressUnusable = mDNSfalse;
|
|
1105
|
+
q->SearchListIndex = 0;
|
|
1106
|
+
q->AppendSearchDomains = 0;
|
|
1107
|
+
q->RetryWithSearchDomains = mDNSfalse;
|
|
1108
|
+
q->TimeoutQuestion = 0;
|
|
1109
|
+
q->WakeOnResolve = 0;
|
|
1110
|
+
q->qnameOrig = mDNSNULL;
|
|
1111
|
+
q->QuestionCallback = callback;
|
|
1112
|
+
q->QuestionContext = context;
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
mDNSexport mDNSu32 RDataHashValue(const ResourceRecord *const rr)
|
|
1116
|
+
{
|
|
1117
|
+
int len = rr->rdlength;
|
|
1118
|
+
const RDataBody2 *const rdb = (RDataBody2 *)rr->rdata->u.data;
|
|
1119
|
+
switch(rr->rrtype)
|
|
1120
|
+
{
|
|
1121
|
+
case kDNSType_NS:
|
|
1122
|
+
case kDNSType_CNAME:
|
|
1123
|
+
case kDNSType_PTR:
|
|
1124
|
+
case kDNSType_DNAME: return DomainNameHashValue(&rdb->name);
|
|
1125
|
+
|
|
1126
|
+
case kDNSType_SOA: return rdb->soa.serial +
|
|
1127
|
+
rdb->soa.refresh +
|
|
1128
|
+
rdb->soa.retry +
|
|
1129
|
+
rdb->soa.expire +
|
|
1130
|
+
rdb->soa.min +
|
|
1131
|
+
DomainNameHashValue(&rdb->soa.mname) +
|
|
1132
|
+
DomainNameHashValue(&rdb->soa.rname);
|
|
1133
|
+
|
|
1134
|
+
case kDNSType_MX:
|
|
1135
|
+
case kDNSType_AFSDB:
|
|
1136
|
+
case kDNSType_RT:
|
|
1137
|
+
case kDNSType_KX: return DomainNameHashValue(&rdb->mx.exchange);
|
|
1138
|
+
|
|
1139
|
+
case kDNSType_RP: return DomainNameHashValue(&rdb->rp.mbox) + DomainNameHashValue(&rdb->rp.txt);
|
|
1140
|
+
|
|
1141
|
+
case kDNSType_PX: return DomainNameHashValue(&rdb->px.map822) + DomainNameHashValue(&rdb->px.mapx400);
|
|
1142
|
+
|
|
1143
|
+
case kDNSType_SRV: return DomainNameHashValue(&rdb->srv.target);
|
|
1144
|
+
|
|
1145
|
+
case kDNSType_OPT: return 0; // OPT is a pseudo-RR container structure; makes no sense to compare
|
|
1146
|
+
|
|
1147
|
+
case kDNSType_NSEC: len = sizeof(rdataNSEC); // Use in-memory length of 32, and fall through default checksum computation below
|
|
1148
|
+
|
|
1149
|
+
default:
|
|
1150
|
+
{
|
|
1151
|
+
mDNSu32 sum = 0;
|
|
1152
|
+
int i;
|
|
1153
|
+
for (i=0; i+1 < len; i+=2)
|
|
1154
|
+
{
|
|
1155
|
+
sum += (((mDNSu32)(rdb->data[i])) << 8) | rdb->data[i+1];
|
|
1156
|
+
sum = (sum<<3) | (sum>>29);
|
|
1157
|
+
}
|
|
1158
|
+
if (i < len)
|
|
1159
|
+
{
|
|
1160
|
+
sum += ((mDNSu32)(rdb->data[i])) << 8;
|
|
1161
|
+
}
|
|
1162
|
+
return(sum);
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
// r1 has to be a full ResourceRecord including rrtype and rdlength
|
|
1168
|
+
// r2 is just a bare RDataBody, which MUST be the same rrtype and rdlength as r1
|
|
1169
|
+
mDNSexport mDNSBool SameRDataBody(const ResourceRecord *const r1, const RDataBody *const r2, DomainNameComparisonFn *samename)
|
|
1170
|
+
{
|
|
1171
|
+
const RDataBody2 *const b1 = (RDataBody2 *)r1->rdata->u.data;
|
|
1172
|
+
const RDataBody2 *const b2 = (RDataBody2 *)r2;
|
|
1173
|
+
switch(r1->rrtype)
|
|
1174
|
+
{
|
|
1175
|
+
case kDNSType_NS:
|
|
1176
|
+
case kDNSType_CNAME:
|
|
1177
|
+
case kDNSType_PTR:
|
|
1178
|
+
case kDNSType_DNAME:return(SameDomainName(&b1->name, &b2->name));
|
|
1179
|
+
|
|
1180
|
+
case kDNSType_SOA: return(mDNSBool)( b1->soa.serial == b2->soa.serial &&
|
|
1181
|
+
b1->soa.refresh == b2->soa.refresh &&
|
|
1182
|
+
b1->soa.retry == b2->soa.retry &&
|
|
1183
|
+
b1->soa.expire == b2->soa.expire &&
|
|
1184
|
+
b1->soa.min == b2->soa.min &&
|
|
1185
|
+
samename(&b1->soa.mname, &b2->soa.mname) &&
|
|
1186
|
+
samename(&b1->soa.rname, &b2->soa.rname));
|
|
1187
|
+
|
|
1188
|
+
case kDNSType_MX:
|
|
1189
|
+
case kDNSType_AFSDB:
|
|
1190
|
+
case kDNSType_RT:
|
|
1191
|
+
case kDNSType_KX: return(mDNSBool)( b1->mx.preference == b2->mx.preference &&
|
|
1192
|
+
samename(&b1->mx.exchange, &b2->mx.exchange));
|
|
1193
|
+
|
|
1194
|
+
case kDNSType_RP: return(mDNSBool)( samename(&b1->rp.mbox, &b2->rp.mbox) &&
|
|
1195
|
+
samename(&b1->rp.txt, &b2->rp.txt));
|
|
1196
|
+
|
|
1197
|
+
case kDNSType_PX: return(mDNSBool)( b1->px.preference == b2->px.preference &&
|
|
1198
|
+
samename(&b1->px.map822, &b2->px.map822) &&
|
|
1199
|
+
samename(&b1->px.mapx400, &b2->px.mapx400));
|
|
1200
|
+
|
|
1201
|
+
case kDNSType_SRV: return(mDNSBool)( b1->srv.priority == b2->srv.priority &&
|
|
1202
|
+
b1->srv.weight == b2->srv.weight &&
|
|
1203
|
+
mDNSSameIPPort(b1->srv.port, b2->srv.port) &&
|
|
1204
|
+
samename(&b1->srv.target, &b2->srv.target));
|
|
1205
|
+
|
|
1206
|
+
case kDNSType_OPT: return mDNSfalse; // OPT is a pseudo-RR container structure; makes no sense to compare
|
|
1207
|
+
|
|
1208
|
+
case kDNSType_NSEC: return(mDNSPlatformMemSame(b1->data, b2->data, sizeof(rdataNSEC)));
|
|
1209
|
+
|
|
1210
|
+
default: return(mDNSPlatformMemSame(b1->data, b2->data, r1->rdlength));
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
// ResourceRecordAnswersQuestion returns mDNStrue if the given resource record is a valid answer to the given question.
|
|
1215
|
+
// SameNameRecordAnswersQuestion is the same, except it skips the expensive SameDomainName() call.
|
|
1216
|
+
// SameDomainName() is generally cheap when the names don't match, but expensive when they do match,
|
|
1217
|
+
// because it has to check all the way to the end of the names to be sure.
|
|
1218
|
+
// In cases where we know in advance that the names match it's especially advantageous to skip the
|
|
1219
|
+
// SameDomainName() call because that's precisely the time when it's most expensive and least useful.
|
|
1220
|
+
|
|
1221
|
+
mDNSexport mDNSBool SameNameRecordAnswersQuestion(const ResourceRecord *const rr, const DNSQuestion *const q)
|
|
1222
|
+
{
|
|
1223
|
+
// LocalOnly/P2P questions can be answered with AuthRecordAny in this function. LocalOnly/P2P records
|
|
1224
|
+
// are handled in LocalOnlyRecordAnswersQuestion
|
|
1225
|
+
if ((rr->InterfaceID == mDNSInterface_LocalOnly) || (rr->InterfaceID == mDNSInterface_P2P))
|
|
1226
|
+
{
|
|
1227
|
+
LogMsg("SameNameRecordAnswersQuestion: ERROR!! called with LocalOnly ResourceRecord %p, Question %p", rr->InterfaceID, q->InterfaceID);
|
|
1228
|
+
return mDNSfalse;
|
|
1229
|
+
}
|
|
1230
|
+
if (rr->InterfaceID &&
|
|
1231
|
+
q ->InterfaceID && q->InterfaceID != mDNSInterface_LocalOnly &&
|
|
1232
|
+
rr->InterfaceID != q->InterfaceID) return(mDNSfalse);
|
|
1233
|
+
|
|
1234
|
+
// Resource record received via unicast, the DNSServer entries should match ?
|
|
1235
|
+
if (!rr->InterfaceID && rr->rDNSServer != q->qDNSServer) return(mDNSfalse);
|
|
1236
|
+
|
|
1237
|
+
// If ResourceRecord received via multicast, but question was unicast, then shouldn't use record to answer this question
|
|
1238
|
+
if (rr->InterfaceID && !mDNSOpaque16IsZero(q->TargetQID)) return(mDNSfalse);
|
|
1239
|
+
|
|
1240
|
+
// RR type CNAME matches any query type. QTYPE ANY matches any RR type. QCLASS ANY matches any RR class.
|
|
1241
|
+
if (!RRTypeAnswersQuestionType(rr,q->qtype)) return(mDNSfalse);
|
|
1242
|
+
if (rr->rrclass != q->qclass && q->qclass != kDNSQClass_ANY) return(mDNSfalse);
|
|
1243
|
+
|
|
1244
|
+
return(mDNStrue);
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
mDNSexport mDNSBool ResourceRecordAnswersQuestion(const ResourceRecord *const rr, const DNSQuestion *const q)
|
|
1248
|
+
{
|
|
1249
|
+
// LocalOnly/P2P questions can be answered with AuthRecordAny in this function. LocalOnly/P2P records
|
|
1250
|
+
// are handled in LocalOnlyRecordAnswersQuestion
|
|
1251
|
+
if ((rr->InterfaceID == mDNSInterface_LocalOnly) || (rr->InterfaceID == mDNSInterface_P2P))
|
|
1252
|
+
{
|
|
1253
|
+
LogMsg("ResourceRecordAnswersQuestion: ERROR!! called with LocalOnly/P2P ResourceRecord %p, Question %p", rr->InterfaceID, q->InterfaceID);
|
|
1254
|
+
return mDNSfalse;
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
if (rr->InterfaceID &&
|
|
1258
|
+
q ->InterfaceID && q->InterfaceID != mDNSInterface_LocalOnly &&
|
|
1259
|
+
rr->InterfaceID != q->InterfaceID) return(mDNSfalse);
|
|
1260
|
+
|
|
1261
|
+
// Resource record received via unicast, the DNSServer entries should match ?
|
|
1262
|
+
if (!rr->InterfaceID && rr->rDNSServer != q->qDNSServer) return(mDNSfalse);
|
|
1263
|
+
|
|
1264
|
+
// If ResourceRecord received via multicast, but question was unicast, then shouldn't use record to answer this question.
|
|
1265
|
+
if (rr->InterfaceID && !mDNSOpaque16IsZero(q->TargetQID)) return(mDNSfalse);
|
|
1266
|
+
|
|
1267
|
+
// RR type CNAME matches any query type. QTYPE ANY matches any RR type. QCLASS ANY matches any RR class.
|
|
1268
|
+
if (!RRTypeAnswersQuestionType(rr,q->qtype)) return(mDNSfalse);
|
|
1269
|
+
if (rr->rrclass != q->qclass && q->qclass != kDNSQClass_ANY) return(mDNSfalse);
|
|
1270
|
+
|
|
1271
|
+
return(rr->namehash == q->qnamehash && SameDomainName(rr->name, &q->qname));
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
// We have a separate function to handle LocalOnly AuthRecords because they can be created with
|
|
1275
|
+
// a valid InterfaceID (e.g., scoped /etc/hosts) and can be used to answer unicast questions unlike
|
|
1276
|
+
// multicast resource records (which has a valid InterfaceID) which can't be used to answer
|
|
1277
|
+
// unicast questions. ResourceRecordAnswersQuestion/SameNameRecordAnswersQuestion can't tell whether
|
|
1278
|
+
// a resource record is multicast or LocalOnly by just looking at the ResourceRecord because
|
|
1279
|
+
// LocalOnly records are truly identified by ARType in the AuthRecord. As P2P and LocalOnly record
|
|
1280
|
+
// are kept in the same hash table, we use the same function to make it easy for the callers when
|
|
1281
|
+
// they walk the hash table to answer LocalOnly/P2P questions
|
|
1282
|
+
//
|
|
1283
|
+
mDNSexport mDNSBool LocalOnlyRecordAnswersQuestion(AuthRecord *const ar, const DNSQuestion *const q)
|
|
1284
|
+
{
|
|
1285
|
+
ResourceRecord *rr = &ar->resrec;
|
|
1286
|
+
|
|
1287
|
+
// mDNSInterface_Any questions can be answered with LocalOnly/P2P records in this function. AuthRecord_Any
|
|
1288
|
+
// records are handled in ResourceRecordAnswersQuestion/SameNameRecordAnswersQuestion
|
|
1289
|
+
if (RRAny(ar))
|
|
1290
|
+
{
|
|
1291
|
+
LogMsg("LocalOnlyRecordAnswersQuestion: ERROR!! called with regular AuthRecordAny %##s", rr->name->c);
|
|
1292
|
+
return mDNSfalse;
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
// Questions with mDNSInterface_LocalOnly InterfaceID should be answered with all resource records that are
|
|
1296
|
+
// *local* to the machine. These include resource records that have InterfaceID set to mDNSInterface_LocalOnly,
|
|
1297
|
+
// mDNSInterface_Any and any other real InterfaceID. Hence, LocalOnly questions should not be checked against
|
|
1298
|
+
// the InterfaceID in the resource record.
|
|
1299
|
+
//
|
|
1300
|
+
// mDNSInterface_Unicast does not indicate any scope and hence treat them like mDNSInterface_Any.
|
|
1301
|
+
|
|
1302
|
+
if (rr->InterfaceID &&
|
|
1303
|
+
q->InterfaceID && q->InterfaceID != mDNSInterface_LocalOnly && q->InterfaceID != mDNSInterface_Unicast &&
|
|
1304
|
+
rr->InterfaceID != q->InterfaceID) return(mDNSfalse);
|
|
1305
|
+
|
|
1306
|
+
// Entries in /etc/hosts are added as LocalOnly resource records. The LocalOnly resource records
|
|
1307
|
+
// may have a scope e.g., fe80::1%en0. The question may be scoped or not: the InterfaceID may be set
|
|
1308
|
+
// to mDNSInterface_Any, mDNSInterface_LocalOnly or a real InterfaceID (scoped).
|
|
1309
|
+
//
|
|
1310
|
+
// 1) Question: Any, LocalOnly Record: no scope. This question should be answered with this record.
|
|
1311
|
+
//
|
|
1312
|
+
// 2) Question: Any, LocalOnly Record: scoped. This question should be answered with the record because
|
|
1313
|
+
// traditionally applications never specify scope e.g., getaddrinfo, but need to be able
|
|
1314
|
+
// to get to /etc/hosts entries.
|
|
1315
|
+
//
|
|
1316
|
+
// 3) Question: Scoped (LocalOnly or InterfaceID), LocalOnly Record: no scope. This is the inverse of (2).
|
|
1317
|
+
// If we register a LocalOnly record, we need to answer a LocalOnly question. If the /etc/hosts has a
|
|
1318
|
+
// non scoped entry, it may not make sense to answer a scoped question. But we can't tell these two
|
|
1319
|
+
// cases apart. As we currently answer LocalOnly question with LocalOnly record, we continue to do so.
|
|
1320
|
+
//
|
|
1321
|
+
// 4) Question: Scoped (LocalOnly or InterfaceID), LocalOnly Record: scoped. LocalOnly questions should be
|
|
1322
|
+
// answered with any resource record where as if it has a valid InterfaceID, the scope should match.
|
|
1323
|
+
//
|
|
1324
|
+
// (1) and (2) is bypassed because we check for a non-NULL InterfaceID above. For (3), the InterfaceID is NULL
|
|
1325
|
+
// and hence bypassed above. For (4) we bypassed LocalOnly questions and checked the scope of the record
|
|
1326
|
+
// against the question.
|
|
1327
|
+
//
|
|
1328
|
+
// For P2P, InterfaceIDs of the question and the record should match.
|
|
1329
|
+
|
|
1330
|
+
// If ResourceRecord received via multicast, but question was unicast, then shouldn't use record to answer this question.
|
|
1331
|
+
// LocalOnly authoritative answers are exempt. LocalOnly authoritative answers are used for /etc/host entries.
|
|
1332
|
+
// We don't want a local process to be able to create a fake LocalOnly address record for "www.bigbank.com" which would then
|
|
1333
|
+
// cause other applications (e.g. Safari) to connect to the wrong address. The rpc to register records filters out records
|
|
1334
|
+
// with names that don't end in local and have mDNSInterface_LocalOnly set.
|
|
1335
|
+
//
|
|
1336
|
+
// Note: The check is bypassed for LocalOnly and for P2P it is not needed as only .local records are registered and for
|
|
1337
|
+
// a question to match its names, it also has to end in .local and that question can't be a unicast question (See
|
|
1338
|
+
// Question_uDNS macro and its usage). As P2P does not enforce .local only registrations we still make this check
|
|
1339
|
+
// and also makes it future proof.
|
|
1340
|
+
|
|
1341
|
+
if (ar->ARType != AuthRecordLocalOnly && rr->InterfaceID && !mDNSOpaque16IsZero(q->TargetQID)) return(mDNSfalse);
|
|
1342
|
+
|
|
1343
|
+
// RR type CNAME matches any query type. QTYPE ANY matches any RR type. QCLASS ANY matches any RR class.
|
|
1344
|
+
if (!RRTypeAnswersQuestionType(rr,q->qtype)) return(mDNSfalse);
|
|
1345
|
+
if (rr->rrclass != q->qclass && q->qclass != kDNSQClass_ANY) return(mDNSfalse);
|
|
1346
|
+
|
|
1347
|
+
return(rr->namehash == q->qnamehash && SameDomainName(rr->name, &q->qname));
|
|
1348
|
+
}
|
|
1349
|
+
|
|
1350
|
+
mDNSexport mDNSBool AnyTypeRecordAnswersQuestion(const ResourceRecord *const rr, const DNSQuestion *const q)
|
|
1351
|
+
{
|
|
1352
|
+
// LocalOnly/P2P questions can be answered with AuthRecordAny in this function. LocalOnly/P2P records
|
|
1353
|
+
// are handled in LocalOnlyRecordAnswersQuestion
|
|
1354
|
+
if ((rr->InterfaceID == mDNSInterface_LocalOnly) || (rr->InterfaceID == mDNSInterface_P2P))
|
|
1355
|
+
{
|
|
1356
|
+
LogMsg("AnyTypeRecordAnswersQuestion: ERROR!! called with LocalOnly ResourceRecord %p, Question %p", rr->InterfaceID, q->InterfaceID);
|
|
1357
|
+
return mDNSfalse;
|
|
1358
|
+
}
|
|
1359
|
+
if (rr->InterfaceID &&
|
|
1360
|
+
q ->InterfaceID && q->InterfaceID != mDNSInterface_LocalOnly &&
|
|
1361
|
+
rr->InterfaceID != q->InterfaceID) return(mDNSfalse);
|
|
1362
|
+
|
|
1363
|
+
// Resource record received via unicast, the DNSServer entries should match ?
|
|
1364
|
+
// Note that Auth Records are normally setup with NULL InterfaceID and
|
|
1365
|
+
// both the DNSServers are assumed to be NULL in that case
|
|
1366
|
+
if (!rr->InterfaceID && rr->rDNSServer != q->qDNSServer) return(mDNSfalse);
|
|
1367
|
+
|
|
1368
|
+
// If ResourceRecord received via multicast, but question was unicast, then shouldn't use record to answer this question
|
|
1369
|
+
if (rr->InterfaceID && !mDNSOpaque16IsZero(q->TargetQID)) return(mDNSfalse);
|
|
1370
|
+
|
|
1371
|
+
if (rr->rrclass != q->qclass && q->qclass != kDNSQClass_ANY) return(mDNSfalse);
|
|
1372
|
+
|
|
1373
|
+
return(rr->namehash == q->qnamehash && SameDomainName(rr->name, &q->qname));
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
// This is called with both unicast resource record and multicast resource record. The question that
|
|
1377
|
+
// received the unicast response could be the regular unicast response from a DNS server or a response
|
|
1378
|
+
// to a mDNS QU query. The main reason we need this function is that we can't compare DNSServers between the
|
|
1379
|
+
// question and the resource record because the resource record is not completely initialized in
|
|
1380
|
+
// mDNSCoreReceiveResponse when this function is called.
|
|
1381
|
+
mDNSexport mDNSBool ResourceRecordAnswersUnicastResponse(const ResourceRecord *const rr, const DNSQuestion *const q)
|
|
1382
|
+
{
|
|
1383
|
+
// For resource records created using multicast, the InterfaceIDs have to match
|
|
1384
|
+
if (rr->InterfaceID &&
|
|
1385
|
+
q->InterfaceID && rr->InterfaceID != q->InterfaceID) return(mDNSfalse);
|
|
1386
|
+
|
|
1387
|
+
// If ResourceRecord received via multicast, but question was unicast, then shouldn't use record to answer this question.
|
|
1388
|
+
if (rr->InterfaceID && !mDNSOpaque16IsZero(q->TargetQID)) return(mDNSfalse);
|
|
1389
|
+
|
|
1390
|
+
// RR type CNAME matches any query type. QTYPE ANY matches any RR type. QCLASS ANY matches any RR class.
|
|
1391
|
+
if (!RRTypeAnswersQuestionType(rr,q->qtype)) return(mDNSfalse);
|
|
1392
|
+
|
|
1393
|
+
if (rr->rrclass != q->qclass && q->qclass != kDNSQClass_ANY) return(mDNSfalse);
|
|
1394
|
+
|
|
1395
|
+
return(rr->namehash == q->qnamehash && SameDomainName(rr->name, &q->qname));
|
|
1396
|
+
}
|
|
1397
|
+
|
|
1398
|
+
mDNSexport mDNSu16 GetRDLength(const ResourceRecord *const rr, mDNSBool estimate)
|
|
1399
|
+
{
|
|
1400
|
+
const RDataBody2 *const rd = (RDataBody2 *)rr->rdata->u.data;
|
|
1401
|
+
const domainname *const name = estimate ? rr->name : mDNSNULL;
|
|
1402
|
+
if (rr->rrclass == kDNSQClass_ANY) return(rr->rdlength); // Used in update packets to mean "Delete An RRset" (RFC 2136)
|
|
1403
|
+
else switch (rr->rrtype)
|
|
1404
|
+
{
|
|
1405
|
+
case kDNSType_A: return(sizeof(rd->ipv4));
|
|
1406
|
+
|
|
1407
|
+
case kDNSType_NS:
|
|
1408
|
+
case kDNSType_CNAME:
|
|
1409
|
+
case kDNSType_PTR:
|
|
1410
|
+
case kDNSType_DNAME:return(CompressedDomainNameLength(&rd->name, name));
|
|
1411
|
+
|
|
1412
|
+
case kDNSType_SOA: return(mDNSu16)(CompressedDomainNameLength(&rd->soa.mname, name) +
|
|
1413
|
+
CompressedDomainNameLength(&rd->soa.rname, name) +
|
|
1414
|
+
5 * sizeof(mDNSOpaque32));
|
|
1415
|
+
|
|
1416
|
+
case kDNSType_NULL:
|
|
1417
|
+
case kDNSType_TSIG:
|
|
1418
|
+
case kDNSType_TXT:
|
|
1419
|
+
case kDNSType_X25:
|
|
1420
|
+
case kDNSType_ISDN:
|
|
1421
|
+
case kDNSType_LOC:
|
|
1422
|
+
case kDNSType_DHCID:return(rr->rdlength); // Not self-describing, so have to just trust rdlength
|
|
1423
|
+
|
|
1424
|
+
case kDNSType_HINFO:return(mDNSu16)(2 + (int)rd->data[0] + (int)rd->data[1 + (int)rd->data[0]]);
|
|
1425
|
+
|
|
1426
|
+
case kDNSType_MX:
|
|
1427
|
+
case kDNSType_AFSDB:
|
|
1428
|
+
case kDNSType_RT:
|
|
1429
|
+
case kDNSType_KX: return(mDNSu16)(2 + CompressedDomainNameLength(&rd->mx.exchange, name));
|
|
1430
|
+
|
|
1431
|
+
case kDNSType_RP: return(mDNSu16)(CompressedDomainNameLength(&rd->rp.mbox, name) +
|
|
1432
|
+
CompressedDomainNameLength(&rd->rp.txt, name));
|
|
1433
|
+
|
|
1434
|
+
case kDNSType_PX: return(mDNSu16)(2 + CompressedDomainNameLength(&rd->px.map822, name) +
|
|
1435
|
+
CompressedDomainNameLength(&rd->px.mapx400, name));
|
|
1436
|
+
|
|
1437
|
+
case kDNSType_AAAA: return(sizeof(rd->ipv6));
|
|
1438
|
+
|
|
1439
|
+
case kDNSType_SRV: return(mDNSu16)(6 + CompressedDomainNameLength(&rd->srv.target, name));
|
|
1440
|
+
|
|
1441
|
+
case kDNSType_OPT: return(rr->rdlength);
|
|
1442
|
+
|
|
1443
|
+
case kDNSType_NSEC: {
|
|
1444
|
+
int i;
|
|
1445
|
+
for (i=sizeof(rdataNSEC); i>0; i--) if (rd->nsec.bitmap[i-1]) break;
|
|
1446
|
+
// For our simplified use of NSEC synthetic records:
|
|
1447
|
+
// nextname is always the record's own name,
|
|
1448
|
+
// and if we have at least one record type that exists,
|
|
1449
|
+
// - the block number is always 0,
|
|
1450
|
+
// - the count byte is a value in the range 1-32,
|
|
1451
|
+
// - followed by the 1-32 data bytes
|
|
1452
|
+
return(mDNSu16)((estimate ? 2 : DomainNameLength(rr->name)) + (i ? (2 + i) : 0));
|
|
1453
|
+
}
|
|
1454
|
+
|
|
1455
|
+
default: debugf("Warning! Don't know how to get length of resource type %d", rr->rrtype);
|
|
1456
|
+
return(rr->rdlength);
|
|
1457
|
+
}
|
|
1458
|
+
}
|
|
1459
|
+
|
|
1460
|
+
// When a local client registers (or updates) a record, we use this routine to do some simple validation checks
|
|
1461
|
+
// to help reduce the risk of bogus malformed data on the network
|
|
1462
|
+
mDNSexport mDNSBool ValidateRData(const mDNSu16 rrtype, const mDNSu16 rdlength, const RData *const rd)
|
|
1463
|
+
{
|
|
1464
|
+
mDNSu16 len;
|
|
1465
|
+
|
|
1466
|
+
switch(rrtype)
|
|
1467
|
+
{
|
|
1468
|
+
case kDNSType_A: return(rdlength == sizeof(mDNSv4Addr));
|
|
1469
|
+
|
|
1470
|
+
case kDNSType_NS: // Same as PTR
|
|
1471
|
+
case kDNSType_MD: // Same as PTR
|
|
1472
|
+
case kDNSType_MF: // Same as PTR
|
|
1473
|
+
case kDNSType_CNAME:// Same as PTR
|
|
1474
|
+
//case kDNSType_SOA not checked
|
|
1475
|
+
case kDNSType_MB: // Same as PTR
|
|
1476
|
+
case kDNSType_MG: // Same as PTR
|
|
1477
|
+
case kDNSType_MR: // Same as PTR
|
|
1478
|
+
//case kDNSType_NULL not checked (no specified format, so always valid)
|
|
1479
|
+
//case kDNSType_WKS not checked
|
|
1480
|
+
case kDNSType_PTR: len = DomainNameLengthLimit(&rd->u.name, rd->u.data + rdlength);
|
|
1481
|
+
return(len <= MAX_DOMAIN_NAME && rdlength == len);
|
|
1482
|
+
|
|
1483
|
+
case kDNSType_HINFO:// Same as TXT (roughly)
|
|
1484
|
+
case kDNSType_MINFO:// Same as TXT (roughly)
|
|
1485
|
+
case kDNSType_TXT: if (!rdlength) return(mDNSfalse); // TXT record has to be at least one byte (RFC 1035)
|
|
1486
|
+
{
|
|
1487
|
+
const mDNSu8 *ptr = rd->u.txt.c;
|
|
1488
|
+
const mDNSu8 *end = rd->u.txt.c + rdlength;
|
|
1489
|
+
while (ptr < end) ptr += 1 + ptr[0];
|
|
1490
|
+
return (ptr == end);
|
|
1491
|
+
}
|
|
1492
|
+
|
|
1493
|
+
case kDNSType_AAAA: return(rdlength == sizeof(mDNSv6Addr));
|
|
1494
|
+
|
|
1495
|
+
case kDNSType_MX: // Must be at least two-byte preference, plus domainname
|
|
1496
|
+
// Call to DomainNameLengthLimit() implicitly enforces both requirements for us
|
|
1497
|
+
len = DomainNameLengthLimit(&rd->u.mx.exchange, rd->u.data + rdlength);
|
|
1498
|
+
return(len <= MAX_DOMAIN_NAME && rdlength == 2+len);
|
|
1499
|
+
|
|
1500
|
+
case kDNSType_SRV: // Must be at least priority+weight+port, plus domainname
|
|
1501
|
+
// Call to DomainNameLengthLimit() implicitly enforces both requirements for us
|
|
1502
|
+
len = DomainNameLengthLimit(&rd->u.srv.target, rd->u.data + rdlength);
|
|
1503
|
+
return(len <= MAX_DOMAIN_NAME && rdlength == 6+len);
|
|
1504
|
+
|
|
1505
|
+
//case kDNSType_NSEC not checked
|
|
1506
|
+
|
|
1507
|
+
default: return(mDNStrue); // Allow all other types without checking
|
|
1508
|
+
}
|
|
1509
|
+
}
|
|
1510
|
+
|
|
1511
|
+
// ***************************************************************************
|
|
1512
|
+
#if COMPILER_LIKES_PRAGMA_MARK
|
|
1513
|
+
#pragma mark -
|
|
1514
|
+
#pragma mark - DNS Message Creation Functions
|
|
1515
|
+
#endif
|
|
1516
|
+
|
|
1517
|
+
mDNSexport void InitializeDNSMessage(DNSMessageHeader *h, mDNSOpaque16 id, mDNSOpaque16 flags)
|
|
1518
|
+
{
|
|
1519
|
+
h->id = id;
|
|
1520
|
+
h->flags = flags;
|
|
1521
|
+
h->numQuestions = 0;
|
|
1522
|
+
h->numAnswers = 0;
|
|
1523
|
+
h->numAuthorities = 0;
|
|
1524
|
+
h->numAdditionals = 0;
|
|
1525
|
+
}
|
|
1526
|
+
|
|
1527
|
+
mDNSexport const mDNSu8 *FindCompressionPointer(const mDNSu8 *const base, const mDNSu8 *const end, const mDNSu8 *const domname)
|
|
1528
|
+
{
|
|
1529
|
+
const mDNSu8 *result = end - *domname - 1;
|
|
1530
|
+
|
|
1531
|
+
if (*domname == 0) return(mDNSNULL); // There's no point trying to match just the root label
|
|
1532
|
+
|
|
1533
|
+
// This loop examines each possible starting position in packet, starting end of the packet and working backwards
|
|
1534
|
+
while (result >= base)
|
|
1535
|
+
{
|
|
1536
|
+
// If the length byte and first character of the label match, then check further to see
|
|
1537
|
+
// if this location in the packet will yield a useful name compression pointer.
|
|
1538
|
+
if (result[0] == domname[0] && result[1] == domname[1])
|
|
1539
|
+
{
|
|
1540
|
+
const mDNSu8 *name = domname;
|
|
1541
|
+
const mDNSu8 *targ = result;
|
|
1542
|
+
while (targ + *name < end)
|
|
1543
|
+
{
|
|
1544
|
+
// First see if this label matches
|
|
1545
|
+
int i;
|
|
1546
|
+
const mDNSu8 *pointertarget;
|
|
1547
|
+
for (i=0; i <= *name; i++) if (targ[i] != name[i]) break;
|
|
1548
|
+
if (i <= *name) break; // If label did not match, bail out
|
|
1549
|
+
targ += 1 + *name; // Else, did match, so advance target pointer
|
|
1550
|
+
name += 1 + *name; // and proceed to check next label
|
|
1551
|
+
if (*name == 0 && *targ == 0) return(result); // If no more labels, we found a match!
|
|
1552
|
+
if (*name == 0) break; // If no more labels to match, we failed, so bail out
|
|
1553
|
+
|
|
1554
|
+
// The label matched, so now follow the pointer (if appropriate) and then see if the next label matches
|
|
1555
|
+
if (targ[0] < 0x40) continue; // If length value, continue to check next label
|
|
1556
|
+
if (targ[0] < 0xC0) break; // If 40-BF, not valid
|
|
1557
|
+
if (targ+1 >= end) break; // Second byte not present!
|
|
1558
|
+
pointertarget = base + (((mDNSu16)(targ[0] & 0x3F)) << 8) + targ[1];
|
|
1559
|
+
if (targ < pointertarget) break; // Pointertarget must point *backwards* in the packet
|
|
1560
|
+
if (pointertarget[0] >= 0x40) break; // Pointertarget must point to a valid length byte
|
|
1561
|
+
targ = pointertarget;
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1564
|
+
result--; // We failed to match at this search position, so back up the tentative result pointer and try again
|
|
1565
|
+
}
|
|
1566
|
+
return(mDNSNULL);
|
|
1567
|
+
}
|
|
1568
|
+
|
|
1569
|
+
// Put a string of dot-separated labels as length-prefixed labels
|
|
1570
|
+
// domainname is a fully-qualified name (i.e. assumed to be ending in a dot, even if it doesn't)
|
|
1571
|
+
// msg points to the message we're building (pass mDNSNULL if we don't want to use compression pointers)
|
|
1572
|
+
// end points to the end of the message so far
|
|
1573
|
+
// ptr points to where we want to put the name
|
|
1574
|
+
// limit points to one byte past the end of the buffer that we must not overrun
|
|
1575
|
+
// domainname is the name to put
|
|
1576
|
+
mDNSexport mDNSu8 *putDomainNameAsLabels(const DNSMessage *const msg,
|
|
1577
|
+
mDNSu8 *ptr, const mDNSu8 *const limit, const domainname *const name)
|
|
1578
|
+
{
|
|
1579
|
+
const mDNSu8 *const base = (const mDNSu8 *)msg;
|
|
1580
|
+
const mDNSu8 * np = name->c;
|
|
1581
|
+
const mDNSu8 *const max = name->c + MAX_DOMAIN_NAME; // Maximum that's valid
|
|
1582
|
+
const mDNSu8 * pointer = mDNSNULL;
|
|
1583
|
+
const mDNSu8 *const searchlimit = ptr;
|
|
1584
|
+
|
|
1585
|
+
if (!ptr) { LogMsg("putDomainNameAsLabels %##s ptr is null", name->c); return(mDNSNULL); }
|
|
1586
|
+
|
|
1587
|
+
if (!*np) // If just writing one-byte root label, make sure we have space for that
|
|
1588
|
+
{
|
|
1589
|
+
if (ptr >= limit) return(mDNSNULL);
|
|
1590
|
+
}
|
|
1591
|
+
else // else, loop through writing labels and/or a compression offset
|
|
1592
|
+
{
|
|
1593
|
+
do {
|
|
1594
|
+
if (*np > MAX_DOMAIN_LABEL)
|
|
1595
|
+
{ LogMsg("Malformed domain name %##s (label more than 63 bytes)", name->c); return(mDNSNULL); }
|
|
1596
|
+
|
|
1597
|
+
// This check correctly allows for the final trailing root label:
|
|
1598
|
+
// e.g.
|
|
1599
|
+
// Suppose our domain name is exactly 256 bytes long, including the final trailing root label.
|
|
1600
|
+
// Suppose np is now at name->c[249], and we're about to write our last non-null label ("local").
|
|
1601
|
+
// We know that max will be at name->c[256]
|
|
1602
|
+
// That means that np + 1 + 5 == max - 1, so we (just) pass the "if" test below, write our
|
|
1603
|
+
// six bytes, then exit the loop, write the final terminating root label, and the domain
|
|
1604
|
+
// name we've written is exactly 256 bytes long, exactly at the correct legal limit.
|
|
1605
|
+
// If the name is one byte longer, then we fail the "if" test below, and correctly bail out.
|
|
1606
|
+
if (np + 1 + *np >= max)
|
|
1607
|
+
{ LogMsg("Malformed domain name %##s (more than 256 bytes)", name->c); return(mDNSNULL); }
|
|
1608
|
+
|
|
1609
|
+
if (base) pointer = FindCompressionPointer(base, searchlimit, np);
|
|
1610
|
+
if (pointer) // Use a compression pointer if we can
|
|
1611
|
+
{
|
|
1612
|
+
const mDNSu16 offset = (mDNSu16)(pointer - base);
|
|
1613
|
+
if (ptr+2 > limit) return(mDNSNULL); // If we don't have two bytes of space left, give up
|
|
1614
|
+
*ptr++ = (mDNSu8)(0xC0 | (offset >> 8));
|
|
1615
|
+
*ptr++ = (mDNSu8)( offset & 0xFF);
|
|
1616
|
+
return(ptr);
|
|
1617
|
+
}
|
|
1618
|
+
else // Else copy one label and try again
|
|
1619
|
+
{
|
|
1620
|
+
int i;
|
|
1621
|
+
mDNSu8 len = *np++;
|
|
1622
|
+
// If we don't at least have enough space for this label *plus* a terminating zero on the end, give up
|
|
1623
|
+
if (ptr + 1 + len >= limit) return(mDNSNULL);
|
|
1624
|
+
*ptr++ = len;
|
|
1625
|
+
for (i=0; i<len; i++) *ptr++ = *np++;
|
|
1626
|
+
}
|
|
1627
|
+
} while (*np); // While we've got characters remaining in the name, continue
|
|
1628
|
+
}
|
|
1629
|
+
|
|
1630
|
+
*ptr++ = 0; // Put the final root label
|
|
1631
|
+
return(ptr);
|
|
1632
|
+
}
|
|
1633
|
+
|
|
1634
|
+
mDNSlocal mDNSu8 *putVal16(mDNSu8 *ptr, mDNSu16 val)
|
|
1635
|
+
{
|
|
1636
|
+
ptr[0] = (mDNSu8)((val >> 8 ) & 0xFF);
|
|
1637
|
+
ptr[1] = (mDNSu8)((val ) & 0xFF);
|
|
1638
|
+
return ptr + sizeof(mDNSOpaque16);
|
|
1639
|
+
}
|
|
1640
|
+
|
|
1641
|
+
mDNSlocal mDNSu8 *putVal32(mDNSu8 *ptr, mDNSu32 val)
|
|
1642
|
+
{
|
|
1643
|
+
ptr[0] = (mDNSu8)((val >> 24) & 0xFF);
|
|
1644
|
+
ptr[1] = (mDNSu8)((val >> 16) & 0xFF);
|
|
1645
|
+
ptr[2] = (mDNSu8)((val >> 8) & 0xFF);
|
|
1646
|
+
ptr[3] = (mDNSu8)((val ) & 0xFF);
|
|
1647
|
+
return ptr + sizeof(mDNSu32);
|
|
1648
|
+
}
|
|
1649
|
+
|
|
1650
|
+
// msg points to the message we're building (pass mDNSNULL if we don't want to use compression pointers)
|
|
1651
|
+
mDNSexport mDNSu8 *putRData(const DNSMessage *const msg, mDNSu8 *ptr, const mDNSu8 *const limit, const ResourceRecord *const rr)
|
|
1652
|
+
{
|
|
1653
|
+
const RDataBody2 *const rdb = (RDataBody2 *)rr->rdata->u.data;
|
|
1654
|
+
switch (rr->rrtype)
|
|
1655
|
+
{
|
|
1656
|
+
case kDNSType_A: if (rr->rdlength != 4)
|
|
1657
|
+
{ debugf("putRData: Illegal length %d for kDNSType_A", rr->rdlength); return(mDNSNULL); }
|
|
1658
|
+
if (ptr + 4 > limit) return(mDNSNULL);
|
|
1659
|
+
*ptr++ = rdb->ipv4.b[0];
|
|
1660
|
+
*ptr++ = rdb->ipv4.b[1];
|
|
1661
|
+
*ptr++ = rdb->ipv4.b[2];
|
|
1662
|
+
*ptr++ = rdb->ipv4.b[3];
|
|
1663
|
+
return(ptr);
|
|
1664
|
+
|
|
1665
|
+
case kDNSType_NS:
|
|
1666
|
+
case kDNSType_CNAME:
|
|
1667
|
+
case kDNSType_PTR:
|
|
1668
|
+
case kDNSType_DNAME:return(putDomainNameAsLabels(msg, ptr, limit, &rdb->name));
|
|
1669
|
+
|
|
1670
|
+
case kDNSType_SOA: ptr = putDomainNameAsLabels(msg, ptr, limit, &rdb->soa.mname);
|
|
1671
|
+
if (!ptr) return(mDNSNULL);
|
|
1672
|
+
ptr = putDomainNameAsLabels(msg, ptr, limit, &rdb->soa.rname);
|
|
1673
|
+
if (!ptr || ptr + 20 > limit) return(mDNSNULL);
|
|
1674
|
+
ptr = putVal32(ptr, rdb->soa.serial);
|
|
1675
|
+
ptr = putVal32(ptr, rdb->soa.refresh);
|
|
1676
|
+
ptr = putVal32(ptr, rdb->soa.retry);
|
|
1677
|
+
ptr = putVal32(ptr, rdb->soa.expire);
|
|
1678
|
+
ptr = putVal32(ptr, rdb->soa.min);
|
|
1679
|
+
return(ptr);
|
|
1680
|
+
|
|
1681
|
+
case kDNSType_NULL:
|
|
1682
|
+
case kDNSType_HINFO:
|
|
1683
|
+
case kDNSType_TSIG:
|
|
1684
|
+
case kDNSType_TXT:
|
|
1685
|
+
case kDNSType_X25:
|
|
1686
|
+
case kDNSType_ISDN:
|
|
1687
|
+
case kDNSType_LOC:
|
|
1688
|
+
case kDNSType_DHCID:if (ptr + rr->rdlength > limit) return(mDNSNULL);
|
|
1689
|
+
mDNSPlatformMemCopy(ptr, rdb->data, rr->rdlength);
|
|
1690
|
+
return(ptr + rr->rdlength);
|
|
1691
|
+
|
|
1692
|
+
case kDNSType_MX:
|
|
1693
|
+
case kDNSType_AFSDB:
|
|
1694
|
+
case kDNSType_RT:
|
|
1695
|
+
case kDNSType_KX: if (ptr + 3 > limit) return(mDNSNULL);
|
|
1696
|
+
ptr = putVal16(ptr, rdb->mx.preference);
|
|
1697
|
+
return(putDomainNameAsLabels(msg, ptr, limit, &rdb->mx.exchange));
|
|
1698
|
+
|
|
1699
|
+
case kDNSType_RP: ptr = putDomainNameAsLabels(msg, ptr, limit, &rdb->rp.mbox);
|
|
1700
|
+
if (!ptr) return(mDNSNULL);
|
|
1701
|
+
ptr = putDomainNameAsLabels(msg, ptr, limit, &rdb->rp.txt);
|
|
1702
|
+
return(ptr);
|
|
1703
|
+
|
|
1704
|
+
case kDNSType_PX: if (ptr + 5 > limit) return(mDNSNULL);
|
|
1705
|
+
ptr = putVal16(ptr, rdb->px.preference);
|
|
1706
|
+
ptr = putDomainNameAsLabels(msg, ptr, limit, &rdb->px.map822);
|
|
1707
|
+
if (!ptr) return(mDNSNULL);
|
|
1708
|
+
ptr = putDomainNameAsLabels(msg, ptr, limit, &rdb->px.mapx400);
|
|
1709
|
+
return(ptr);
|
|
1710
|
+
|
|
1711
|
+
case kDNSType_AAAA: if (rr->rdlength != sizeof(rdb->ipv6))
|
|
1712
|
+
{ debugf("putRData: Illegal length %d for kDNSType_AAAA", rr->rdlength); return(mDNSNULL); }
|
|
1713
|
+
if (ptr + sizeof(rdb->ipv6) > limit) return(mDNSNULL);
|
|
1714
|
+
mDNSPlatformMemCopy(ptr, &rdb->ipv6, sizeof(rdb->ipv6));
|
|
1715
|
+
return(ptr + sizeof(rdb->ipv6));
|
|
1716
|
+
|
|
1717
|
+
case kDNSType_SRV: if (ptr + 7 > limit) return(mDNSNULL);
|
|
1718
|
+
*ptr++ = (mDNSu8)(rdb->srv.priority >> 8);
|
|
1719
|
+
*ptr++ = (mDNSu8)(rdb->srv.priority & 0xFF);
|
|
1720
|
+
*ptr++ = (mDNSu8)(rdb->srv.weight >> 8);
|
|
1721
|
+
*ptr++ = (mDNSu8)(rdb->srv.weight & 0xFF);
|
|
1722
|
+
*ptr++ = rdb->srv.port.b[0];
|
|
1723
|
+
*ptr++ = rdb->srv.port.b[1];
|
|
1724
|
+
return(putDomainNameAsLabels(msg, ptr, limit, &rdb->srv.target));
|
|
1725
|
+
|
|
1726
|
+
case kDNSType_OPT: {
|
|
1727
|
+
int len = 0;
|
|
1728
|
+
const rdataOPT *opt;
|
|
1729
|
+
const rdataOPT *const end = (const rdataOPT *)&rr->rdata->u.data[rr->rdlength];
|
|
1730
|
+
for (opt = &rr->rdata->u.opt[0]; opt < end; opt++) len += DNSOpt_Data_Space(opt);
|
|
1731
|
+
if (ptr + len > limit) { LogMsg("ERROR: putOptRData - out of space"); return mDNSNULL; }
|
|
1732
|
+
|
|
1733
|
+
for (opt = &rr->rdata->u.opt[0]; opt < end; opt++)
|
|
1734
|
+
{
|
|
1735
|
+
const int space = DNSOpt_Data_Space(opt);
|
|
1736
|
+
ptr = putVal16(ptr, opt->opt);
|
|
1737
|
+
ptr = putVal16(ptr, (mDNSu16)space - 4);
|
|
1738
|
+
switch (opt->opt)
|
|
1739
|
+
{
|
|
1740
|
+
case kDNSOpt_LLQ:
|
|
1741
|
+
ptr = putVal16(ptr, opt->u.llq.vers);
|
|
1742
|
+
ptr = putVal16(ptr, opt->u.llq.llqOp);
|
|
1743
|
+
ptr = putVal16(ptr, opt->u.llq.err);
|
|
1744
|
+
mDNSPlatformMemCopy(ptr, opt->u.llq.id.b, 8); // 8-byte id
|
|
1745
|
+
ptr += 8;
|
|
1746
|
+
ptr = putVal32(ptr, opt->u.llq.llqlease);
|
|
1747
|
+
break;
|
|
1748
|
+
case kDNSOpt_Lease:
|
|
1749
|
+
ptr = putVal32(ptr, opt->u.updatelease);
|
|
1750
|
+
break;
|
|
1751
|
+
case kDNSOpt_Owner:
|
|
1752
|
+
*ptr++ = opt->u.owner.vers;
|
|
1753
|
+
*ptr++ = opt->u.owner.seq;
|
|
1754
|
+
mDNSPlatformMemCopy(ptr, opt->u.owner.HMAC.b, 6); // 6-byte Host identifier
|
|
1755
|
+
ptr += 6;
|
|
1756
|
+
if (space >= DNSOpt_OwnerData_ID_Wake_Space)
|
|
1757
|
+
{
|
|
1758
|
+
mDNSPlatformMemCopy(ptr, opt->u.owner.IMAC.b, 6); // 6-byte interface MAC
|
|
1759
|
+
ptr += 6;
|
|
1760
|
+
if (space > DNSOpt_OwnerData_ID_Wake_Space)
|
|
1761
|
+
{
|
|
1762
|
+
mDNSPlatformMemCopy(ptr, opt->u.owner.password.b, space - DNSOpt_OwnerData_ID_Wake_Space);
|
|
1763
|
+
ptr += space - DNSOpt_OwnerData_ID_Wake_Space;
|
|
1764
|
+
}
|
|
1765
|
+
}
|
|
1766
|
+
break;
|
|
1767
|
+
}
|
|
1768
|
+
}
|
|
1769
|
+
return ptr;
|
|
1770
|
+
}
|
|
1771
|
+
|
|
1772
|
+
case kDNSType_NSEC: {
|
|
1773
|
+
// For our simplified use of NSEC synthetic records:
|
|
1774
|
+
// nextname is always the record's own name,
|
|
1775
|
+
// the block number is always 0,
|
|
1776
|
+
// the count byte is a value in the range 1-32,
|
|
1777
|
+
// followed by the 1-32 data bytes
|
|
1778
|
+
int i, j;
|
|
1779
|
+
for (i=sizeof(rdataNSEC); i>0; i--) if (rdb->nsec.bitmap[i-1]) break;
|
|
1780
|
+
ptr = putDomainNameAsLabels(msg, ptr, limit, rr->name);
|
|
1781
|
+
if (!ptr) return(mDNSNULL);
|
|
1782
|
+
if (i) // Only put a block if at least one type exists for this name
|
|
1783
|
+
{
|
|
1784
|
+
if (ptr + 2 + i > limit) return(mDNSNULL);
|
|
1785
|
+
*ptr++ = 0;
|
|
1786
|
+
*ptr++ = (mDNSu8)i;
|
|
1787
|
+
for (j=0; j<i; j++) *ptr++ = rdb->nsec.bitmap[j];
|
|
1788
|
+
}
|
|
1789
|
+
return ptr;
|
|
1790
|
+
}
|
|
1791
|
+
|
|
1792
|
+
default: debugf("putRData: Warning! Writing unknown resource type %d as raw data", rr->rrtype);
|
|
1793
|
+
if (ptr + rr->rdlength > limit) return(mDNSNULL);
|
|
1794
|
+
mDNSPlatformMemCopy(ptr, rdb->data, rr->rdlength);
|
|
1795
|
+
return(ptr + rr->rdlength);
|
|
1796
|
+
}
|
|
1797
|
+
}
|
|
1798
|
+
|
|
1799
|
+
#define IsUnicastUpdate(X) (!mDNSOpaque16IsZero((X)->h.id) && ((X)->h.flags.b[0] & kDNSFlag0_OP_Mask) == kDNSFlag0_OP_Update)
|
|
1800
|
+
|
|
1801
|
+
mDNSexport mDNSu8 *PutResourceRecordTTLWithLimit(DNSMessage *const msg, mDNSu8 *ptr, mDNSu16 *count, ResourceRecord *rr, mDNSu32 ttl, const mDNSu8 *limit)
|
|
1802
|
+
{
|
|
1803
|
+
mDNSu8 *endofrdata;
|
|
1804
|
+
mDNSu16 actualLength;
|
|
1805
|
+
// When sending SRV to conventional DNS server (i.e. in DNS update requests) we should not do name compression on the rdata (RFC 2782)
|
|
1806
|
+
const DNSMessage *const rdatacompressionbase = (IsUnicastUpdate(msg) && rr->rrtype == kDNSType_SRV) ? mDNSNULL : msg;
|
|
1807
|
+
|
|
1808
|
+
if (rr->RecordType == kDNSRecordTypeUnregistered)
|
|
1809
|
+
{
|
|
1810
|
+
LogMsg("PutResourceRecord ERROR! Attempt to put kDNSRecordTypeUnregistered %##s (%s)", rr->name->c, DNSTypeName(rr->rrtype));
|
|
1811
|
+
return(ptr);
|
|
1812
|
+
}
|
|
1813
|
+
|
|
1814
|
+
if (!ptr) { LogMsg("PutResourceRecordTTLWithLimit ptr is null"); return(mDNSNULL); }
|
|
1815
|
+
|
|
1816
|
+
ptr = putDomainNameAsLabels(msg, ptr, limit, rr->name);
|
|
1817
|
+
if (!ptr || ptr + 10 >= limit) return(mDNSNULL); // If we're out-of-space, return mDNSNULL
|
|
1818
|
+
ptr[0] = (mDNSu8)(rr->rrtype >> 8);
|
|
1819
|
+
ptr[1] = (mDNSu8)(rr->rrtype & 0xFF);
|
|
1820
|
+
ptr[2] = (mDNSu8)(rr->rrclass >> 8);
|
|
1821
|
+
ptr[3] = (mDNSu8)(rr->rrclass & 0xFF);
|
|
1822
|
+
ptr[4] = (mDNSu8)((ttl >> 24) & 0xFF);
|
|
1823
|
+
ptr[5] = (mDNSu8)((ttl >> 16) & 0xFF);
|
|
1824
|
+
ptr[6] = (mDNSu8)((ttl >> 8) & 0xFF);
|
|
1825
|
+
ptr[7] = (mDNSu8)( ttl & 0xFF);
|
|
1826
|
+
// ptr[8] and ptr[9] filled in *after* we find out how much space the rdata takes
|
|
1827
|
+
|
|
1828
|
+
endofrdata = putRData(rdatacompressionbase, ptr+10, limit, rr);
|
|
1829
|
+
if (!endofrdata) { verbosedebugf("Ran out of space in PutResourceRecord for %##s (%s)", rr->name->c, DNSTypeName(rr->rrtype)); return(mDNSNULL); }
|
|
1830
|
+
|
|
1831
|
+
// Go back and fill in the actual number of data bytes we wrote
|
|
1832
|
+
// (actualLength can be less than rdlength when domain name compression is used)
|
|
1833
|
+
actualLength = (mDNSu16)(endofrdata - ptr - 10);
|
|
1834
|
+
ptr[8] = (mDNSu8)(actualLength >> 8);
|
|
1835
|
+
ptr[9] = (mDNSu8)(actualLength & 0xFF);
|
|
1836
|
+
|
|
1837
|
+
if (count) (*count)++;
|
|
1838
|
+
else LogMsg("PutResourceRecordTTL: ERROR: No target count to update for %##s (%s)", rr->name->c, DNSTypeName(rr->rrtype));
|
|
1839
|
+
return(endofrdata);
|
|
1840
|
+
}
|
|
1841
|
+
|
|
1842
|
+
mDNSlocal mDNSu8 *putEmptyResourceRecord(DNSMessage *const msg, mDNSu8 *ptr, const mDNSu8 *const limit, mDNSu16 *count, const AuthRecord *rr)
|
|
1843
|
+
{
|
|
1844
|
+
ptr = putDomainNameAsLabels(msg, ptr, limit, rr->resrec.name);
|
|
1845
|
+
if (!ptr || ptr + 10 > limit) return(mDNSNULL); // If we're out-of-space, return mDNSNULL
|
|
1846
|
+
ptr[0] = (mDNSu8)(rr->resrec.rrtype >> 8); // Put type
|
|
1847
|
+
ptr[1] = (mDNSu8)(rr->resrec.rrtype & 0xFF);
|
|
1848
|
+
ptr[2] = (mDNSu8)(rr->resrec.rrclass >> 8); // Put class
|
|
1849
|
+
ptr[3] = (mDNSu8)(rr->resrec.rrclass & 0xFF);
|
|
1850
|
+
ptr[4] = ptr[5] = ptr[6] = ptr[7] = 0; // TTL is zero
|
|
1851
|
+
ptr[8] = ptr[9] = 0; // RDATA length is zero
|
|
1852
|
+
(*count)++;
|
|
1853
|
+
return(ptr + 10);
|
|
1854
|
+
}
|
|
1855
|
+
|
|
1856
|
+
mDNSexport mDNSu8 *putQuestion(DNSMessage *const msg, mDNSu8 *ptr, const mDNSu8 *const limit, const domainname *const name, mDNSu16 rrtype, mDNSu16 rrclass)
|
|
1857
|
+
{
|
|
1858
|
+
ptr = putDomainNameAsLabels(msg, ptr, limit, name);
|
|
1859
|
+
if (!ptr || ptr+4 >= limit) return(mDNSNULL); // If we're out-of-space, return mDNSNULL
|
|
1860
|
+
ptr[0] = (mDNSu8)(rrtype >> 8);
|
|
1861
|
+
ptr[1] = (mDNSu8)(rrtype & 0xFF);
|
|
1862
|
+
ptr[2] = (mDNSu8)(rrclass >> 8);
|
|
1863
|
+
ptr[3] = (mDNSu8)(rrclass & 0xFF);
|
|
1864
|
+
msg->h.numQuestions++;
|
|
1865
|
+
return(ptr+4);
|
|
1866
|
+
}
|
|
1867
|
+
|
|
1868
|
+
// for dynamic updates
|
|
1869
|
+
mDNSexport mDNSu8 *putZone(DNSMessage *const msg, mDNSu8 *ptr, mDNSu8 *limit, const domainname *zone, mDNSOpaque16 zoneClass)
|
|
1870
|
+
{
|
|
1871
|
+
ptr = putDomainNameAsLabels(msg, ptr, limit, zone);
|
|
1872
|
+
if (!ptr || ptr + 4 > limit) return mDNSNULL; // If we're out-of-space, return NULL
|
|
1873
|
+
*ptr++ = (mDNSu8)(kDNSType_SOA >> 8);
|
|
1874
|
+
*ptr++ = (mDNSu8)(kDNSType_SOA & 0xFF);
|
|
1875
|
+
*ptr++ = zoneClass.b[0];
|
|
1876
|
+
*ptr++ = zoneClass.b[1];
|
|
1877
|
+
msg->h.mDNS_numZones++;
|
|
1878
|
+
return ptr;
|
|
1879
|
+
}
|
|
1880
|
+
|
|
1881
|
+
// for dynamic updates
|
|
1882
|
+
mDNSexport mDNSu8 *putPrereqNameNotInUse(const domainname *const name, DNSMessage *const msg, mDNSu8 *const ptr, mDNSu8 *const end)
|
|
1883
|
+
{
|
|
1884
|
+
AuthRecord prereq;
|
|
1885
|
+
mDNS_SetupResourceRecord(&prereq, mDNSNULL, mDNSInterface_Any, kDNSQType_ANY, kStandardTTL, 0, AuthRecordAny, mDNSNULL, mDNSNULL);
|
|
1886
|
+
AssignDomainName(&prereq.namestorage, name);
|
|
1887
|
+
prereq.resrec.rrtype = kDNSQType_ANY;
|
|
1888
|
+
prereq.resrec.rrclass = kDNSClass_NONE;
|
|
1889
|
+
return putEmptyResourceRecord(msg, ptr, end, &msg->h.mDNS_numPrereqs, &prereq);
|
|
1890
|
+
}
|
|
1891
|
+
|
|
1892
|
+
// for dynamic updates
|
|
1893
|
+
mDNSexport mDNSu8 *putDeletionRecord(DNSMessage *msg, mDNSu8 *ptr, ResourceRecord *rr)
|
|
1894
|
+
{
|
|
1895
|
+
// deletion: specify record w/ TTL 0, class NONE
|
|
1896
|
+
const mDNSu16 origclass = rr->rrclass;
|
|
1897
|
+
rr->rrclass = kDNSClass_NONE;
|
|
1898
|
+
ptr = PutResourceRecordTTLJumbo(msg, ptr, &msg->h.mDNS_numUpdates, rr, 0);
|
|
1899
|
+
rr->rrclass = origclass;
|
|
1900
|
+
return ptr;
|
|
1901
|
+
}
|
|
1902
|
+
|
|
1903
|
+
// for dynamic updates
|
|
1904
|
+
mDNSexport mDNSu8 *putDeletionRecordWithLimit(DNSMessage *msg, mDNSu8 *ptr, ResourceRecord *rr, mDNSu8 *limit)
|
|
1905
|
+
{
|
|
1906
|
+
// deletion: specify record w/ TTL 0, class NONE
|
|
1907
|
+
const mDNSu16 origclass = rr->rrclass;
|
|
1908
|
+
rr->rrclass = kDNSClass_NONE;
|
|
1909
|
+
ptr = PutResourceRecordTTLWithLimit(msg, ptr, &msg->h.mDNS_numUpdates, rr, 0, limit);
|
|
1910
|
+
rr->rrclass = origclass;
|
|
1911
|
+
return ptr;
|
|
1912
|
+
}
|
|
1913
|
+
|
|
1914
|
+
mDNSexport mDNSu8 *putDeleteRRSetWithLimit(DNSMessage *msg, mDNSu8 *ptr, const domainname *name, mDNSu16 rrtype, mDNSu8 *limit)
|
|
1915
|
+
{
|
|
1916
|
+
mDNSu16 class = kDNSQClass_ANY;
|
|
1917
|
+
|
|
1918
|
+
ptr = putDomainNameAsLabels(msg, ptr, limit, name);
|
|
1919
|
+
if (!ptr || ptr + 10 >= limit) return mDNSNULL; // If we're out-of-space, return mDNSNULL
|
|
1920
|
+
ptr[0] = (mDNSu8)(rrtype >> 8);
|
|
1921
|
+
ptr[1] = (mDNSu8)(rrtype & 0xFF);
|
|
1922
|
+
ptr[2] = (mDNSu8)(class >> 8);
|
|
1923
|
+
ptr[3] = (mDNSu8)(class & 0xFF);
|
|
1924
|
+
ptr[4] = ptr[5] = ptr[6] = ptr[7] = 0; // zero ttl
|
|
1925
|
+
ptr[8] = ptr[9] = 0; // zero rdlength/rdata
|
|
1926
|
+
|
|
1927
|
+
msg->h.mDNS_numUpdates++;
|
|
1928
|
+
return ptr + 10;
|
|
1929
|
+
}
|
|
1930
|
+
|
|
1931
|
+
// for dynamic updates
|
|
1932
|
+
mDNSexport mDNSu8 *putDeleteAllRRSets(DNSMessage *msg, mDNSu8 *ptr, const domainname *name)
|
|
1933
|
+
{
|
|
1934
|
+
const mDNSu8 *limit = msg->data + AbsoluteMaxDNSMessageData;
|
|
1935
|
+
mDNSu16 class = kDNSQClass_ANY;
|
|
1936
|
+
mDNSu16 rrtype = kDNSQType_ANY;
|
|
1937
|
+
|
|
1938
|
+
ptr = putDomainNameAsLabels(msg, ptr, limit, name);
|
|
1939
|
+
if (!ptr || ptr + 10 >= limit) return mDNSNULL; // If we're out-of-space, return mDNSNULL
|
|
1940
|
+
ptr[0] = (mDNSu8)(rrtype >> 8);
|
|
1941
|
+
ptr[1] = (mDNSu8)(rrtype & 0xFF);
|
|
1942
|
+
ptr[2] = (mDNSu8)(class >> 8);
|
|
1943
|
+
ptr[3] = (mDNSu8)(class & 0xFF);
|
|
1944
|
+
ptr[4] = ptr[5] = ptr[6] = ptr[7] = 0; // zero ttl
|
|
1945
|
+
ptr[8] = ptr[9] = 0; // zero rdlength/rdata
|
|
1946
|
+
|
|
1947
|
+
msg->h.mDNS_numUpdates++;
|
|
1948
|
+
return ptr + 10;
|
|
1949
|
+
}
|
|
1950
|
+
|
|
1951
|
+
// for dynamic updates
|
|
1952
|
+
mDNSexport mDNSu8 *putUpdateLease(DNSMessage *msg, mDNSu8 *end, mDNSu32 lease)
|
|
1953
|
+
{
|
|
1954
|
+
AuthRecord rr;
|
|
1955
|
+
mDNS_SetupResourceRecord(&rr, mDNSNULL, mDNSInterface_Any, kDNSType_OPT, kStandardTTL, kDNSRecordTypeKnownUnique, AuthRecordAny, mDNSNULL, mDNSNULL);
|
|
1956
|
+
rr.resrec.rrclass = NormalMaxDNSMessageData;
|
|
1957
|
+
rr.resrec.rdlength = sizeof(rdataOPT); // One option in this OPT record
|
|
1958
|
+
rr.resrec.rdestimate = sizeof(rdataOPT);
|
|
1959
|
+
rr.resrec.rdata->u.opt[0].opt = kDNSOpt_Lease;
|
|
1960
|
+
rr.resrec.rdata->u.opt[0].u.updatelease = lease;
|
|
1961
|
+
end = PutResourceRecordTTLJumbo(msg, end, &msg->h.numAdditionals, &rr.resrec, 0);
|
|
1962
|
+
if (!end) { LogMsg("ERROR: putUpdateLease - PutResourceRecordTTL"); return mDNSNULL; }
|
|
1963
|
+
return end;
|
|
1964
|
+
}
|
|
1965
|
+
|
|
1966
|
+
// for dynamic updates
|
|
1967
|
+
mDNSexport mDNSu8 *putUpdateLeaseWithLimit(DNSMessage *msg, mDNSu8 *end, mDNSu32 lease, mDNSu8 *limit)
|
|
1968
|
+
{
|
|
1969
|
+
AuthRecord rr;
|
|
1970
|
+
mDNS_SetupResourceRecord(&rr, mDNSNULL, mDNSInterface_Any, kDNSType_OPT, kStandardTTL, kDNSRecordTypeKnownUnique, AuthRecordAny, mDNSNULL, mDNSNULL);
|
|
1971
|
+
rr.resrec.rrclass = NormalMaxDNSMessageData;
|
|
1972
|
+
rr.resrec.rdlength = sizeof(rdataOPT); // One option in this OPT record
|
|
1973
|
+
rr.resrec.rdestimate = sizeof(rdataOPT);
|
|
1974
|
+
rr.resrec.rdata->u.opt[0].opt = kDNSOpt_Lease;
|
|
1975
|
+
rr.resrec.rdata->u.opt[0].u.updatelease = lease;
|
|
1976
|
+
end = PutResourceRecordTTLWithLimit(msg, end, &msg->h.numAdditionals, &rr.resrec, 0, limit);
|
|
1977
|
+
if (!end) { LogMsg("ERROR: putUpdateLease - PutResourceRecordTTLWithLimit"); return mDNSNULL; }
|
|
1978
|
+
return end;
|
|
1979
|
+
}
|
|
1980
|
+
|
|
1981
|
+
mDNSexport mDNSu8 *putHINFO(const mDNS *const m, DNSMessage *const msg, mDNSu8 *end, DomainAuthInfo *authInfo, mDNSu8 *limit)
|
|
1982
|
+
{
|
|
1983
|
+
if (authInfo && authInfo->AutoTunnel)
|
|
1984
|
+
{
|
|
1985
|
+
AuthRecord hinfo;
|
|
1986
|
+
mDNSu8 *h = hinfo.rdatastorage.u.data;
|
|
1987
|
+
mDNSu16 len = 2 + m->HIHardware.c[0] + m->HISoftware.c[0];
|
|
1988
|
+
mDNSu8 *newptr;
|
|
1989
|
+
mDNS_SetupResourceRecord(&hinfo, mDNSNULL, mDNSInterface_Any, kDNSType_HINFO, 0, kDNSRecordTypeUnique, AuthRecordAny, mDNSNULL, mDNSNULL);
|
|
1990
|
+
AppendDomainLabel(&hinfo.namestorage, &m->hostlabel);
|
|
1991
|
+
AppendDomainName (&hinfo.namestorage, &authInfo->domain);
|
|
1992
|
+
hinfo.resrec.rroriginalttl = 0;
|
|
1993
|
+
mDNSPlatformMemCopy(h, &m->HIHardware, 1 + (mDNSu32)m->HIHardware.c[0]);
|
|
1994
|
+
h += 1 + (int)h[0];
|
|
1995
|
+
mDNSPlatformMemCopy(h, &m->HISoftware, 1 + (mDNSu32)m->HISoftware.c[0]);
|
|
1996
|
+
hinfo.resrec.rdlength = len;
|
|
1997
|
+
hinfo.resrec.rdestimate = len;
|
|
1998
|
+
newptr = PutResourceRecordTTLWithLimit(msg, end, &msg->h.numAdditionals, &hinfo.resrec, 0, limit);
|
|
1999
|
+
return newptr;
|
|
2000
|
+
}
|
|
2001
|
+
else
|
|
2002
|
+
return end;
|
|
2003
|
+
}
|
|
2004
|
+
|
|
2005
|
+
// ***************************************************************************
|
|
2006
|
+
#if COMPILER_LIKES_PRAGMA_MARK
|
|
2007
|
+
#pragma mark -
|
|
2008
|
+
#pragma mark - DNS Message Parsing Functions
|
|
2009
|
+
#endif
|
|
2010
|
+
|
|
2011
|
+
mDNSexport mDNSu32 DomainNameHashValue(const domainname *const name)
|
|
2012
|
+
{
|
|
2013
|
+
mDNSu32 sum = 0;
|
|
2014
|
+
const mDNSu8 *c;
|
|
2015
|
+
|
|
2016
|
+
for (c = name->c; c[0] != 0 && c[1] != 0; c += 2)
|
|
2017
|
+
{
|
|
2018
|
+
sum += ((mDNSIsUpperCase(c[0]) ? c[0] + 'a' - 'A' : c[0]) << 8) |
|
|
2019
|
+
(mDNSIsUpperCase(c[1]) ? c[1] + 'a' - 'A' : c[1]);
|
|
2020
|
+
sum = (sum<<3) | (sum>>29);
|
|
2021
|
+
}
|
|
2022
|
+
if (c[0]) sum += ((mDNSIsUpperCase(c[0]) ? c[0] + 'a' - 'A' : c[0]) << 8);
|
|
2023
|
+
return(sum);
|
|
2024
|
+
}
|
|
2025
|
+
|
|
2026
|
+
mDNSexport void SetNewRData(ResourceRecord *const rr, RData *NewRData, mDNSu16 rdlength)
|
|
2027
|
+
{
|
|
2028
|
+
domainname *target;
|
|
2029
|
+
if (NewRData)
|
|
2030
|
+
{
|
|
2031
|
+
rr->rdata = NewRData;
|
|
2032
|
+
rr->rdlength = rdlength;
|
|
2033
|
+
}
|
|
2034
|
+
// Must not try to get target pointer until after updating rr->rdata
|
|
2035
|
+
target = GetRRDomainNameTarget(rr);
|
|
2036
|
+
rr->rdlength = GetRDLength(rr, mDNSfalse);
|
|
2037
|
+
rr->rdestimate = GetRDLength(rr, mDNStrue);
|
|
2038
|
+
rr->rdatahash = target ? DomainNameHashValue(target) : RDataHashValue(rr);
|
|
2039
|
+
}
|
|
2040
|
+
|
|
2041
|
+
mDNSexport const mDNSu8 *skipDomainName(const DNSMessage *const msg, const mDNSu8 *ptr, const mDNSu8 *const end)
|
|
2042
|
+
{
|
|
2043
|
+
mDNSu16 total = 0;
|
|
2044
|
+
|
|
2045
|
+
if (ptr < (mDNSu8*)msg || ptr >= end)
|
|
2046
|
+
{ debugf("skipDomainName: Illegal ptr not within packet boundaries"); return(mDNSNULL); }
|
|
2047
|
+
|
|
2048
|
+
while (1) // Read sequence of labels
|
|
2049
|
+
{
|
|
2050
|
+
const mDNSu8 len = *ptr++; // Read length of this label
|
|
2051
|
+
if (len == 0) return(ptr); // If length is zero, that means this name is complete
|
|
2052
|
+
switch (len & 0xC0)
|
|
2053
|
+
{
|
|
2054
|
+
case 0x00: if (ptr + len >= end) // Remember: expect at least one more byte for the root label
|
|
2055
|
+
{ debugf("skipDomainName: Malformed domain name (overruns packet end)"); return(mDNSNULL); }
|
|
2056
|
+
if (total + 1 + len >= MAX_DOMAIN_NAME) // Remember: expect at least one more byte for the root label
|
|
2057
|
+
{ debugf("skipDomainName: Malformed domain name (more than 256 characters)"); return(mDNSNULL); }
|
|
2058
|
+
ptr += len;
|
|
2059
|
+
total += 1 + len;
|
|
2060
|
+
break;
|
|
2061
|
+
|
|
2062
|
+
case 0x40: debugf("skipDomainName: Extended EDNS0 label types 0x%X not supported", len); return(mDNSNULL);
|
|
2063
|
+
case 0x80: debugf("skipDomainName: Illegal label length 0x%X", len); return(mDNSNULL);
|
|
2064
|
+
case 0xC0: return(ptr+1);
|
|
2065
|
+
}
|
|
2066
|
+
}
|
|
2067
|
+
}
|
|
2068
|
+
|
|
2069
|
+
// Routine to fetch an FQDN from the DNS message, following compression pointers if necessary.
|
|
2070
|
+
mDNSexport const mDNSu8 *getDomainName(const DNSMessage *const msg, const mDNSu8 *ptr, const mDNSu8 *const end,
|
|
2071
|
+
domainname *const name)
|
|
2072
|
+
{
|
|
2073
|
+
const mDNSu8 *nextbyte = mDNSNULL; // Record where we got to before we started following pointers
|
|
2074
|
+
mDNSu8 *np = name->c; // Name pointer
|
|
2075
|
+
const mDNSu8 *const limit = np + MAX_DOMAIN_NAME; // Limit so we don't overrun buffer
|
|
2076
|
+
|
|
2077
|
+
if (ptr < (mDNSu8*)msg || ptr >= end)
|
|
2078
|
+
{ debugf("getDomainName: Illegal ptr not within packet boundaries"); return(mDNSNULL); }
|
|
2079
|
+
|
|
2080
|
+
*np = 0; // Tentatively place the root label here (may be overwritten if we have more labels)
|
|
2081
|
+
|
|
2082
|
+
while (1) // Read sequence of labels
|
|
2083
|
+
{
|
|
2084
|
+
const mDNSu8 len = *ptr++; // Read length of this label
|
|
2085
|
+
if (len == 0) break; // If length is zero, that means this name is complete
|
|
2086
|
+
switch (len & 0xC0)
|
|
2087
|
+
{
|
|
2088
|
+
int i;
|
|
2089
|
+
mDNSu16 offset;
|
|
2090
|
+
|
|
2091
|
+
case 0x00: if (ptr + len >= end) // Remember: expect at least one more byte for the root label
|
|
2092
|
+
{ debugf("getDomainName: Malformed domain name (overruns packet end)"); return(mDNSNULL); }
|
|
2093
|
+
if (np + 1 + len >= limit) // Remember: expect at least one more byte for the root label
|
|
2094
|
+
{ debugf("getDomainName: Malformed domain name (more than 256 characters)"); return(mDNSNULL); }
|
|
2095
|
+
*np++ = len;
|
|
2096
|
+
for (i=0; i<len; i++) *np++ = *ptr++;
|
|
2097
|
+
*np = 0; // Tentatively place the root label here (may be overwritten if we have more labels)
|
|
2098
|
+
break;
|
|
2099
|
+
|
|
2100
|
+
case 0x40: debugf("getDomainName: Extended EDNS0 label types 0x%X not supported in name %##s", len, name->c);
|
|
2101
|
+
return(mDNSNULL);
|
|
2102
|
+
|
|
2103
|
+
case 0x80: debugf("getDomainName: Illegal label length 0x%X in domain name %##s", len, name->c); return(mDNSNULL);
|
|
2104
|
+
|
|
2105
|
+
case 0xC0: offset = (mDNSu16)((((mDNSu16)(len & 0x3F)) << 8) | *ptr++);
|
|
2106
|
+
if (!nextbyte) nextbyte = ptr; // Record where we got to before we started following pointers
|
|
2107
|
+
ptr = (mDNSu8 *)msg + offset;
|
|
2108
|
+
if (ptr < (mDNSu8*)msg || ptr >= end)
|
|
2109
|
+
{ debugf("getDomainName: Illegal compression pointer not within packet boundaries"); return(mDNSNULL); }
|
|
2110
|
+
if (*ptr & 0xC0)
|
|
2111
|
+
{ debugf("getDomainName: Compression pointer must point to real label"); return(mDNSNULL); }
|
|
2112
|
+
break;
|
|
2113
|
+
}
|
|
2114
|
+
}
|
|
2115
|
+
|
|
2116
|
+
if (nextbyte) return(nextbyte);
|
|
2117
|
+
else return(ptr);
|
|
2118
|
+
}
|
|
2119
|
+
|
|
2120
|
+
mDNSexport const mDNSu8 *skipResourceRecord(const DNSMessage *msg, const mDNSu8 *ptr, const mDNSu8 *end)
|
|
2121
|
+
{
|
|
2122
|
+
mDNSu16 pktrdlength;
|
|
2123
|
+
|
|
2124
|
+
ptr = skipDomainName(msg, ptr, end);
|
|
2125
|
+
if (!ptr) { debugf("skipResourceRecord: Malformed RR name"); return(mDNSNULL); }
|
|
2126
|
+
|
|
2127
|
+
if (ptr + 10 > end) { debugf("skipResourceRecord: Malformed RR -- no type/class/ttl/len!"); return(mDNSNULL); }
|
|
2128
|
+
pktrdlength = (mDNSu16)((mDNSu16)ptr[8] << 8 | ptr[9]);
|
|
2129
|
+
ptr += 10;
|
|
2130
|
+
if (ptr + pktrdlength > end) { debugf("skipResourceRecord: RDATA exceeds end of packet"); return(mDNSNULL); }
|
|
2131
|
+
|
|
2132
|
+
return(ptr + pktrdlength);
|
|
2133
|
+
}
|
|
2134
|
+
|
|
2135
|
+
mDNSexport const mDNSu8 *GetLargeResourceRecord(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *ptr,
|
|
2136
|
+
const mDNSu8 *end, const mDNSInterfaceID InterfaceID, mDNSu8 RecordType, LargeCacheRecord *const largecr)
|
|
2137
|
+
{
|
|
2138
|
+
CacheRecord *const rr = &largecr->r;
|
|
2139
|
+
RDataBody2 *const rdb = (RDataBody2 *)rr->smallrdatastorage.data;
|
|
2140
|
+
mDNSu16 pktrdlength;
|
|
2141
|
+
|
|
2142
|
+
if (largecr == &m->rec && m->rec.r.resrec.RecordType)
|
|
2143
|
+
{
|
|
2144
|
+
LogMsg("GetLargeResourceRecord: m->rec appears to be already in use for %s", CRDisplayString(m, &m->rec.r));
|
|
2145
|
+
#if ForceAlerts
|
|
2146
|
+
*(long*)0 = 0;
|
|
2147
|
+
#endif
|
|
2148
|
+
}
|
|
2149
|
+
|
|
2150
|
+
rr->next = mDNSNULL;
|
|
2151
|
+
rr->resrec.name = &largecr->namestorage;
|
|
2152
|
+
|
|
2153
|
+
rr->NextInKAList = mDNSNULL;
|
|
2154
|
+
rr->TimeRcvd = m ? m->timenow : 0;
|
|
2155
|
+
rr->DelayDelivery = 0;
|
|
2156
|
+
rr->NextRequiredQuery = m ? m->timenow : 0; // Will be updated to the real value when we call SetNextCacheCheckTimeForRecord()
|
|
2157
|
+
rr->LastUsed = m ? m->timenow : 0;
|
|
2158
|
+
rr->CRActiveQuestion = mDNSNULL;
|
|
2159
|
+
rr->UnansweredQueries = 0;
|
|
2160
|
+
rr->LastUnansweredTime= 0;
|
|
2161
|
+
#if ENABLE_MULTI_PACKET_QUERY_SNOOPING
|
|
2162
|
+
rr->MPUnansweredQ = 0;
|
|
2163
|
+
rr->MPLastUnansweredQT= 0;
|
|
2164
|
+
rr->MPUnansweredKA = 0;
|
|
2165
|
+
rr->MPExpectingKA = mDNSfalse;
|
|
2166
|
+
#endif
|
|
2167
|
+
rr->NextInCFList = mDNSNULL;
|
|
2168
|
+
|
|
2169
|
+
rr->resrec.InterfaceID = InterfaceID;
|
|
2170
|
+
rr->resrec.rDNSServer = mDNSNULL;
|
|
2171
|
+
|
|
2172
|
+
ptr = getDomainName(msg, ptr, end, &largecr->namestorage); // Will bail out correctly if ptr is NULL
|
|
2173
|
+
if (!ptr) { debugf("GetLargeResourceRecord: Malformed RR name"); return(mDNSNULL); }
|
|
2174
|
+
rr->resrec.namehash = DomainNameHashValue(rr->resrec.name);
|
|
2175
|
+
|
|
2176
|
+
if (ptr + 10 > end) { debugf("GetLargeResourceRecord: Malformed RR -- no type/class/ttl/len!"); return(mDNSNULL); }
|
|
2177
|
+
|
|
2178
|
+
rr->resrec.rrtype = (mDNSu16) ((mDNSu16)ptr[0] << 8 | ptr[1]);
|
|
2179
|
+
rr->resrec.rrclass = (mDNSu16)(((mDNSu16)ptr[2] << 8 | ptr[3]) & kDNSClass_Mask);
|
|
2180
|
+
rr->resrec.rroriginalttl = (mDNSu32) ((mDNSu32)ptr[4] << 24 | (mDNSu32)ptr[5] << 16 | (mDNSu32)ptr[6] << 8 | ptr[7]);
|
|
2181
|
+
if (rr->resrec.rroriginalttl > 0x70000000UL / mDNSPlatformOneSecond && (mDNSs32)rr->resrec.rroriginalttl != -1)
|
|
2182
|
+
rr->resrec.rroriginalttl = 0x70000000UL / mDNSPlatformOneSecond;
|
|
2183
|
+
// Note: We don't have to adjust m->NextCacheCheck here -- this is just getting a record into memory for
|
|
2184
|
+
// us to look at. If we decide to copy it into the cache, then we'll update m->NextCacheCheck accordingly.
|
|
2185
|
+
pktrdlength = (mDNSu16)((mDNSu16)ptr[8] << 8 | ptr[9]);
|
|
2186
|
+
|
|
2187
|
+
// If mDNS record has cache-flush bit set, we mark it unique
|
|
2188
|
+
// For uDNS records, all are implicitly deemed unique (a single DNS server is always
|
|
2189
|
+
// authoritative for the entire RRSet), unless this is a truncated response
|
|
2190
|
+
if (ptr[2] & (kDNSClass_UniqueRRSet >> 8) || (!InterfaceID && !(msg->h.flags.b[0] & kDNSFlag0_TC)))
|
|
2191
|
+
RecordType |= kDNSRecordTypePacketUniqueMask;
|
|
2192
|
+
ptr += 10;
|
|
2193
|
+
if (ptr + pktrdlength > end) { debugf("GetLargeResourceRecord: RDATA exceeds end of packet"); return(mDNSNULL); }
|
|
2194
|
+
end = ptr + pktrdlength; // Adjust end to indicate the end of the rdata for this resource record
|
|
2195
|
+
|
|
2196
|
+
rr->resrec.rdata = (RData*)&rr->smallrdatastorage;
|
|
2197
|
+
rr->resrec.rdata->MaxRDLength = MaximumRDSize;
|
|
2198
|
+
|
|
2199
|
+
if (!RecordType) LogMsg("GetLargeResourceRecord: No RecordType for %##s", rr->resrec.name->c);
|
|
2200
|
+
|
|
2201
|
+
// IMPORTANT: Any record type we understand and unpack into a structure containing domainnames needs to have corresponding
|
|
2202
|
+
// cases in SameRDataBody() and RDataHashValue() to do a semantic comparison (or checksum) of the structure instead of a blind
|
|
2203
|
+
// bitwise memory compare (or sum). This is because a domainname is a fixed size structure holding variable-length data.
|
|
2204
|
+
// Any bytes past the logical end of the name are undefined, and a blind bitwise memory compare may indicate that
|
|
2205
|
+
// two domainnames are different when semantically they are the same name and it's only the unused bytes that differ.
|
|
2206
|
+
if (rr->resrec.rrclass == kDNSQClass_ANY && pktrdlength == 0) // Used in update packets to mean "Delete An RRset" (RFC 2136)
|
|
2207
|
+
rr->resrec.rdlength = 0;
|
|
2208
|
+
else switch (rr->resrec.rrtype)
|
|
2209
|
+
{
|
|
2210
|
+
case kDNSType_A: if (pktrdlength != sizeof(mDNSv4Addr)) goto fail;
|
|
2211
|
+
rdb->ipv4.b[0] = ptr[0];
|
|
2212
|
+
rdb->ipv4.b[1] = ptr[1];
|
|
2213
|
+
rdb->ipv4.b[2] = ptr[2];
|
|
2214
|
+
rdb->ipv4.b[3] = ptr[3];
|
|
2215
|
+
break;
|
|
2216
|
+
|
|
2217
|
+
case kDNSType_NS:
|
|
2218
|
+
case kDNSType_CNAME:
|
|
2219
|
+
case kDNSType_PTR:
|
|
2220
|
+
case kDNSType_DNAME:ptr = getDomainName(msg, ptr, end, &rdb->name);
|
|
2221
|
+
if (ptr != end) { debugf("GetLargeResourceRecord: Malformed CNAME/PTR RDATA name"); goto fail; }
|
|
2222
|
+
//debugf("%##s PTR %##s rdlen %d", rr->resrec.name.c, rdb->name.c, pktrdlength);
|
|
2223
|
+
break;
|
|
2224
|
+
|
|
2225
|
+
case kDNSType_SOA: ptr = getDomainName(msg, ptr, end, &rdb->soa.mname);
|
|
2226
|
+
if (!ptr) { debugf("GetLargeResourceRecord: Malformed SOA RDATA mname"); goto fail; }
|
|
2227
|
+
ptr = getDomainName(msg, ptr, end, &rdb->soa.rname);
|
|
2228
|
+
if (!ptr) { debugf("GetLargeResourceRecord: Malformed SOA RDATA rname"); goto fail; }
|
|
2229
|
+
if (ptr + 0x14 != end) { debugf("GetLargeResourceRecord: Malformed SOA RDATA"); goto fail; }
|
|
2230
|
+
rdb->soa.serial = (mDNSs32) ((mDNSs32)ptr[0x00] << 24 | (mDNSs32)ptr[0x01] << 16 | (mDNSs32)ptr[0x02] << 8 | ptr[0x03]);
|
|
2231
|
+
rdb->soa.refresh = (mDNSu32) ((mDNSu32)ptr[0x04] << 24 | (mDNSu32)ptr[0x05] << 16 | (mDNSu32)ptr[0x06] << 8 | ptr[0x07]);
|
|
2232
|
+
rdb->soa.retry = (mDNSu32) ((mDNSu32)ptr[0x08] << 24 | (mDNSu32)ptr[0x09] << 16 | (mDNSu32)ptr[0x0A] << 8 | ptr[0x0B]);
|
|
2233
|
+
rdb->soa.expire = (mDNSu32) ((mDNSu32)ptr[0x0C] << 24 | (mDNSu32)ptr[0x0D] << 16 | (mDNSu32)ptr[0x0E] << 8 | ptr[0x0F]);
|
|
2234
|
+
rdb->soa.min = (mDNSu32) ((mDNSu32)ptr[0x10] << 24 | (mDNSu32)ptr[0x11] << 16 | (mDNSu32)ptr[0x12] << 8 | ptr[0x13]);
|
|
2235
|
+
break;
|
|
2236
|
+
|
|
2237
|
+
case kDNSType_NULL:
|
|
2238
|
+
case kDNSType_HINFO:
|
|
2239
|
+
case kDNSType_TSIG:
|
|
2240
|
+
case kDNSType_TXT:
|
|
2241
|
+
case kDNSType_X25:
|
|
2242
|
+
case kDNSType_ISDN:
|
|
2243
|
+
case kDNSType_LOC:
|
|
2244
|
+
case kDNSType_DHCID:if (pktrdlength > rr->resrec.rdata->MaxRDLength)
|
|
2245
|
+
{
|
|
2246
|
+
debugf("GetLargeResourceRecord: %s rdata size (%d) exceeds storage (%d)",
|
|
2247
|
+
DNSTypeName(rr->resrec.rrtype), pktrdlength, rr->resrec.rdata->MaxRDLength);
|
|
2248
|
+
goto fail;
|
|
2249
|
+
}
|
|
2250
|
+
rr->resrec.rdlength = pktrdlength;
|
|
2251
|
+
mDNSPlatformMemCopy(rdb->data, ptr, pktrdlength);
|
|
2252
|
+
break;
|
|
2253
|
+
|
|
2254
|
+
case kDNSType_MX:
|
|
2255
|
+
case kDNSType_AFSDB:
|
|
2256
|
+
case kDNSType_RT:
|
|
2257
|
+
case kDNSType_KX: if (pktrdlength < 3) goto fail; // Preference + domainname
|
|
2258
|
+
rdb->mx.preference = (mDNSu16)((mDNSu16)ptr[0] << 8 | ptr[1]);
|
|
2259
|
+
ptr = getDomainName(msg, ptr+2, end, &rdb->mx.exchange);
|
|
2260
|
+
if (ptr != end) { debugf("GetLargeResourceRecord: Malformed MX name"); goto fail; }
|
|
2261
|
+
//debugf("%##s SRV %##s rdlen %d", rr->resrec.name.c, rdb->srv.target.c, pktrdlength);
|
|
2262
|
+
break;
|
|
2263
|
+
|
|
2264
|
+
case kDNSType_RP: ptr = getDomainName(msg, ptr, end, &rdb->rp.mbox); // Domainname + domainname
|
|
2265
|
+
if (!ptr) { debugf("GetLargeResourceRecord: Malformed RP mbox"); goto fail; }
|
|
2266
|
+
ptr = getDomainName(msg, ptr, end, &rdb->rp.txt);
|
|
2267
|
+
if (ptr != end) { debugf("GetLargeResourceRecord: Malformed RP txt"); goto fail; }
|
|
2268
|
+
break;
|
|
2269
|
+
|
|
2270
|
+
case kDNSType_PX: if (pktrdlength < 4) goto fail; // Preference + domainname + domainname
|
|
2271
|
+
rdb->px.preference = (mDNSu16)((mDNSu16)ptr[0] << 8 | ptr[1]);
|
|
2272
|
+
ptr = getDomainName(msg, ptr, end, &rdb->px.map822);
|
|
2273
|
+
if (!ptr) { debugf("GetLargeResourceRecord: Malformed PX map822"); goto fail; }
|
|
2274
|
+
ptr = getDomainName(msg, ptr, end, &rdb->px.mapx400);
|
|
2275
|
+
if (ptr != end) { debugf("GetLargeResourceRecord: Malformed PX mapx400"); goto fail; }
|
|
2276
|
+
break;
|
|
2277
|
+
|
|
2278
|
+
case kDNSType_AAAA: if (pktrdlength != sizeof(mDNSv6Addr)) goto fail;
|
|
2279
|
+
mDNSPlatformMemCopy(&rdb->ipv6, ptr, sizeof(rdb->ipv6));
|
|
2280
|
+
break;
|
|
2281
|
+
|
|
2282
|
+
case kDNSType_SRV: if (pktrdlength < 7) goto fail; // Priority + weight + port + domainname
|
|
2283
|
+
rdb->srv.priority = (mDNSu16)((mDNSu16)ptr[0] << 8 | ptr[1]);
|
|
2284
|
+
rdb->srv.weight = (mDNSu16)((mDNSu16)ptr[2] << 8 | ptr[3]);
|
|
2285
|
+
rdb->srv.port.b[0] = ptr[4];
|
|
2286
|
+
rdb->srv.port.b[1] = ptr[5];
|
|
2287
|
+
ptr = getDomainName(msg, ptr+6, end, &rdb->srv.target);
|
|
2288
|
+
if (ptr != end) { debugf("GetLargeResourceRecord: Malformed SRV RDATA name"); goto fail; }
|
|
2289
|
+
//debugf("%##s SRV %##s rdlen %d", rr->resrec.name.c, rdb->srv.target.c, pktrdlength);
|
|
2290
|
+
break;
|
|
2291
|
+
|
|
2292
|
+
case kDNSType_OPT: {
|
|
2293
|
+
rdataOPT *opt = rr->resrec.rdata->u.opt;
|
|
2294
|
+
rr->resrec.rdlength = 0;
|
|
2295
|
+
while (ptr < end && (mDNSu8 *)(opt+1) < &rr->resrec.rdata->u.data[MaximumRDSize])
|
|
2296
|
+
{
|
|
2297
|
+
const rdataOPT *const currentopt = opt;
|
|
2298
|
+
if (ptr + 4 > end) { LogInfo("GetLargeResourceRecord: OPT RDATA ptr + 4 > end"); goto fail; }
|
|
2299
|
+
opt->opt = (mDNSu16)((mDNSu16)ptr[0] << 8 | ptr[1]);
|
|
2300
|
+
opt->optlen = (mDNSu16)((mDNSu16)ptr[2] << 8 | ptr[3]);
|
|
2301
|
+
ptr += 4;
|
|
2302
|
+
if (ptr + opt->optlen > end) { LogInfo("GetLargeResourceRecord: ptr + opt->optlen > end"); goto fail; }
|
|
2303
|
+
switch (opt->opt)
|
|
2304
|
+
{
|
|
2305
|
+
case kDNSOpt_LLQ:
|
|
2306
|
+
if (opt->optlen == DNSOpt_LLQData_Space - 4)
|
|
2307
|
+
{
|
|
2308
|
+
opt->u.llq.vers = (mDNSu16)((mDNSu16)ptr[0] << 8 | ptr[1]);
|
|
2309
|
+
opt->u.llq.llqOp = (mDNSu16)((mDNSu16)ptr[2] << 8 | ptr[3]);
|
|
2310
|
+
opt->u.llq.err = (mDNSu16)((mDNSu16)ptr[4] << 8 | ptr[5]);
|
|
2311
|
+
mDNSPlatformMemCopy(opt->u.llq.id.b, ptr+6, 8);
|
|
2312
|
+
opt->u.llq.llqlease = (mDNSu32) ((mDNSu32)ptr[14] << 24 | (mDNSu32)ptr[15] << 16 | (mDNSu32)ptr[16] << 8 | ptr[17]);
|
|
2313
|
+
if (opt->u.llq.llqlease > 0x70000000UL / mDNSPlatformOneSecond)
|
|
2314
|
+
opt->u.llq.llqlease = 0x70000000UL / mDNSPlatformOneSecond;
|
|
2315
|
+
opt++;
|
|
2316
|
+
}
|
|
2317
|
+
break;
|
|
2318
|
+
case kDNSOpt_Lease:
|
|
2319
|
+
if (opt->optlen == DNSOpt_LeaseData_Space - 4)
|
|
2320
|
+
{
|
|
2321
|
+
opt->u.updatelease = (mDNSu32) ((mDNSu32)ptr[0] << 24 | (mDNSu32)ptr[1] << 16 | (mDNSu32)ptr[2] << 8 | ptr[3]);
|
|
2322
|
+
if (opt->u.updatelease > 0x70000000UL / mDNSPlatformOneSecond)
|
|
2323
|
+
opt->u.updatelease = 0x70000000UL / mDNSPlatformOneSecond;
|
|
2324
|
+
opt++;
|
|
2325
|
+
}
|
|
2326
|
+
break;
|
|
2327
|
+
case kDNSOpt_Owner:
|
|
2328
|
+
if (ValidOwnerLength(opt->optlen))
|
|
2329
|
+
{
|
|
2330
|
+
opt->u.owner.vers = ptr[0];
|
|
2331
|
+
opt->u.owner.seq = ptr[1];
|
|
2332
|
+
mDNSPlatformMemCopy(opt->u.owner.HMAC.b, ptr+2, 6); // 6-byte MAC address
|
|
2333
|
+
mDNSPlatformMemCopy(opt->u.owner.IMAC.b, ptr+2, 6); // 6-byte MAC address
|
|
2334
|
+
opt->u.owner.password = zeroEthAddr;
|
|
2335
|
+
if (opt->optlen >= DNSOpt_OwnerData_ID_Wake_Space-4)
|
|
2336
|
+
{
|
|
2337
|
+
mDNSPlatformMemCopy(opt->u.owner.IMAC.b, ptr+8, 6); // 6-byte MAC address
|
|
2338
|
+
// This mDNSPlatformMemCopy is safe because the ValidOwnerLength(opt->optlen) check above
|
|
2339
|
+
// ensures that opt->optlen is no more than DNSOpt_OwnerData_ID_Wake_PW6_Space - 4
|
|
2340
|
+
if (opt->optlen > DNSOpt_OwnerData_ID_Wake_Space-4)
|
|
2341
|
+
mDNSPlatformMemCopy(opt->u.owner.password.b, ptr+14, opt->optlen - (DNSOpt_OwnerData_ID_Wake_Space-4));
|
|
2342
|
+
}
|
|
2343
|
+
opt++;
|
|
2344
|
+
}
|
|
2345
|
+
break;
|
|
2346
|
+
}
|
|
2347
|
+
ptr += currentopt->optlen;
|
|
2348
|
+
}
|
|
2349
|
+
rr->resrec.rdlength = (mDNSu16)((mDNSu8*)opt - rr->resrec.rdata->u.data);
|
|
2350
|
+
if (ptr != end) { LogInfo("GetLargeResourceRecord: Malformed OptRdata"); goto fail; }
|
|
2351
|
+
break;
|
|
2352
|
+
}
|
|
2353
|
+
|
|
2354
|
+
case kDNSType_NSEC: {
|
|
2355
|
+
unsigned int i, j;
|
|
2356
|
+
domainname d;
|
|
2357
|
+
ptr = getDomainName(msg, ptr, end, &d); // Ignored for our simplified use of NSEC synthetic records
|
|
2358
|
+
if (!ptr) { LogInfo("GetLargeResourceRecord: Malformed NSEC nextname"); goto fail; }
|
|
2359
|
+
mDNSPlatformMemZero(rdb->nsec.bitmap, sizeof(rdb->nsec.bitmap));
|
|
2360
|
+
if (ptr < end)
|
|
2361
|
+
{
|
|
2362
|
+
if (*ptr++ != 0) { debugf("GetLargeResourceRecord: We only handle block zero NSECs"); goto fail; }
|
|
2363
|
+
i = *ptr++;
|
|
2364
|
+
if (i > sizeof(rdataNSEC)) { debugf("GetLargeResourceRecord: invalid block length %d", i); goto fail; }
|
|
2365
|
+
for (j=0; j<i; j++) rdb->nsec.bitmap[j] = *ptr++;
|
|
2366
|
+
}
|
|
2367
|
+
if (ptr != end) { debugf("GetLargeResourceRecord: Malformed NSEC"); goto fail; }
|
|
2368
|
+
break;
|
|
2369
|
+
}
|
|
2370
|
+
|
|
2371
|
+
default: if (pktrdlength > rr->resrec.rdata->MaxRDLength)
|
|
2372
|
+
{
|
|
2373
|
+
debugf("GetLargeResourceRecord: rdata %d (%s) size (%d) exceeds storage (%d)",
|
|
2374
|
+
rr->resrec.rrtype, DNSTypeName(rr->resrec.rrtype), pktrdlength, rr->resrec.rdata->MaxRDLength);
|
|
2375
|
+
goto fail;
|
|
2376
|
+
}
|
|
2377
|
+
debugf("GetLargeResourceRecord: Warning! Reading resource type %d (%s) as opaque data",
|
|
2378
|
+
rr->resrec.rrtype, DNSTypeName(rr->resrec.rrtype));
|
|
2379
|
+
// Note: Just because we don't understand the record type, that doesn't
|
|
2380
|
+
// mean we fail. The DNS protocol specifies rdlength, so we can
|
|
2381
|
+
// safely skip over unknown records and ignore them.
|
|
2382
|
+
// We also grab a binary copy of the rdata anyway, since the caller
|
|
2383
|
+
// might know how to interpret it even if we don't.
|
|
2384
|
+
rr->resrec.rdlength = pktrdlength;
|
|
2385
|
+
mDNSPlatformMemCopy(rdb->data, ptr, pktrdlength);
|
|
2386
|
+
break;
|
|
2387
|
+
}
|
|
2388
|
+
|
|
2389
|
+
SetNewRData(&rr->resrec, mDNSNULL, 0); // Sets rdlength, rdestimate, rdatahash for us
|
|
2390
|
+
|
|
2391
|
+
// Success! Now fill in RecordType to show this record contains valid data
|
|
2392
|
+
rr->resrec.RecordType = RecordType;
|
|
2393
|
+
return(end);
|
|
2394
|
+
|
|
2395
|
+
fail:
|
|
2396
|
+
// If we were unable to parse the rdata in this record, we indicate that by
|
|
2397
|
+
// returing a 'kDNSRecordTypePacketNegative' record with rdlength set to zero
|
|
2398
|
+
rr->resrec.RecordType = kDNSRecordTypePacketNegative;
|
|
2399
|
+
rr->resrec.rdlength = 0;
|
|
2400
|
+
rr->resrec.rdestimate = 0;
|
|
2401
|
+
rr->resrec.rdatahash = 0;
|
|
2402
|
+
return(end);
|
|
2403
|
+
}
|
|
2404
|
+
|
|
2405
|
+
mDNSexport const mDNSu8 *skipQuestion(const DNSMessage *msg, const mDNSu8 *ptr, const mDNSu8 *end)
|
|
2406
|
+
{
|
|
2407
|
+
ptr = skipDomainName(msg, ptr, end);
|
|
2408
|
+
if (!ptr) { debugf("skipQuestion: Malformed domain name in DNS question section"); return(mDNSNULL); }
|
|
2409
|
+
if (ptr+4 > end) { debugf("skipQuestion: Malformed DNS question section -- no query type and class!"); return(mDNSNULL); }
|
|
2410
|
+
return(ptr+4);
|
|
2411
|
+
}
|
|
2412
|
+
|
|
2413
|
+
mDNSexport const mDNSu8 *getQuestion(const DNSMessage *msg, const mDNSu8 *ptr, const mDNSu8 *end, const mDNSInterfaceID InterfaceID,
|
|
2414
|
+
DNSQuestion *question)
|
|
2415
|
+
{
|
|
2416
|
+
mDNSPlatformMemZero(question, sizeof(*question));
|
|
2417
|
+
question->InterfaceID = InterfaceID;
|
|
2418
|
+
if (!InterfaceID) question->TargetQID = onesID; // In DNSQuestions we use TargetQID as the indicator of whether it's unicast or multicast
|
|
2419
|
+
ptr = getDomainName(msg, ptr, end, &question->qname);
|
|
2420
|
+
if (!ptr) { debugf("Malformed domain name in DNS question section"); return(mDNSNULL); }
|
|
2421
|
+
if (ptr+4 > end) { debugf("Malformed DNS question section -- no query type and class!"); return(mDNSNULL); }
|
|
2422
|
+
|
|
2423
|
+
question->qnamehash = DomainNameHashValue(&question->qname);
|
|
2424
|
+
question->qtype = (mDNSu16)((mDNSu16)ptr[0] << 8 | ptr[1]); // Get type
|
|
2425
|
+
question->qclass = (mDNSu16)((mDNSu16)ptr[2] << 8 | ptr[3]); // and class
|
|
2426
|
+
return(ptr+4);
|
|
2427
|
+
}
|
|
2428
|
+
|
|
2429
|
+
mDNSexport const mDNSu8 *LocateAnswers(const DNSMessage *const msg, const mDNSu8 *const end)
|
|
2430
|
+
{
|
|
2431
|
+
int i;
|
|
2432
|
+
const mDNSu8 *ptr = msg->data;
|
|
2433
|
+
for (i = 0; i < msg->h.numQuestions && ptr; i++) ptr = skipQuestion(msg, ptr, end);
|
|
2434
|
+
return(ptr);
|
|
2435
|
+
}
|
|
2436
|
+
|
|
2437
|
+
mDNSexport const mDNSu8 *LocateAuthorities(const DNSMessage *const msg, const mDNSu8 *const end)
|
|
2438
|
+
{
|
|
2439
|
+
int i;
|
|
2440
|
+
const mDNSu8 *ptr = LocateAnswers(msg, end);
|
|
2441
|
+
for (i = 0; i < msg->h.numAnswers && ptr; i++) ptr = skipResourceRecord(msg, ptr, end);
|
|
2442
|
+
return(ptr);
|
|
2443
|
+
}
|
|
2444
|
+
|
|
2445
|
+
mDNSexport const mDNSu8 *LocateAdditionals(const DNSMessage *const msg, const mDNSu8 *const end)
|
|
2446
|
+
{
|
|
2447
|
+
int i;
|
|
2448
|
+
const mDNSu8 *ptr = LocateAuthorities(msg, end);
|
|
2449
|
+
for (i = 0; i < msg->h.numAuthorities; i++) ptr = skipResourceRecord(msg, ptr, end);
|
|
2450
|
+
return (ptr);
|
|
2451
|
+
}
|
|
2452
|
+
|
|
2453
|
+
mDNSexport const mDNSu8 *LocateOptRR(const DNSMessage *const msg, const mDNSu8 *const end, int minsize)
|
|
2454
|
+
{
|
|
2455
|
+
int i;
|
|
2456
|
+
const mDNSu8 *ptr = LocateAdditionals(msg, end);
|
|
2457
|
+
|
|
2458
|
+
// Locate the OPT record.
|
|
2459
|
+
// According to RFC 2671, "One OPT pseudo-RR can be added to the additional data section of either a request or a response."
|
|
2460
|
+
// This implies that there may be *at most* one OPT record per DNS message, in the Additional Section,
|
|
2461
|
+
// but not necessarily the *last* entry in the Additional Section.
|
|
2462
|
+
for (i = 0; ptr && i < msg->h.numAdditionals; i++)
|
|
2463
|
+
{
|
|
2464
|
+
if (ptr + DNSOpt_Header_Space + minsize <= end && // Make sure we have 11+minsize bytes of data
|
|
2465
|
+
ptr[0] == 0 && // Name must be root label
|
|
2466
|
+
ptr[1] == (kDNSType_OPT >> 8 ) && // rrtype OPT
|
|
2467
|
+
ptr[2] == (kDNSType_OPT & 0xFF) &&
|
|
2468
|
+
((mDNSu16)ptr[9] << 8 | (mDNSu16)ptr[10]) >= (mDNSu16)minsize)
|
|
2469
|
+
return(ptr);
|
|
2470
|
+
else
|
|
2471
|
+
ptr = skipResourceRecord(msg, ptr, end);
|
|
2472
|
+
}
|
|
2473
|
+
return(mDNSNULL);
|
|
2474
|
+
}
|
|
2475
|
+
|
|
2476
|
+
// On success, GetLLQOptData returns pointer to storage within shared "m->rec";
|
|
2477
|
+
// it is caller's responsibilty to clear m->rec.r.resrec.RecordType after use
|
|
2478
|
+
// Note: An OPT RDataBody actually contains one or more variable-length rdataOPT objects packed together
|
|
2479
|
+
// The code that currently calls this assumes there's only one, instead of iterating through the set
|
|
2480
|
+
mDNSexport const rdataOPT *GetLLQOptData(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *const end)
|
|
2481
|
+
{
|
|
2482
|
+
const mDNSu8 *ptr = LocateOptRR(msg, end, DNSOpt_LLQData_Space);
|
|
2483
|
+
if (ptr)
|
|
2484
|
+
{
|
|
2485
|
+
ptr = GetLargeResourceRecord(m, msg, ptr, end, 0, kDNSRecordTypePacketAdd, &m->rec);
|
|
2486
|
+
if (ptr && m->rec.r.resrec.RecordType != kDNSRecordTypePacketNegative) return(&m->rec.r.resrec.rdata->u.opt[0]);
|
|
2487
|
+
}
|
|
2488
|
+
return(mDNSNULL);
|
|
2489
|
+
}
|
|
2490
|
+
|
|
2491
|
+
// Get the lease life of records in a dynamic update
|
|
2492
|
+
// returns 0 on error or if no lease present
|
|
2493
|
+
mDNSexport mDNSu32 GetPktLease(mDNS *m, DNSMessage *msg, const mDNSu8 *end)
|
|
2494
|
+
{
|
|
2495
|
+
mDNSu32 result = 0;
|
|
2496
|
+
const mDNSu8 *ptr = LocateOptRR(msg, end, DNSOpt_LeaseData_Space);
|
|
2497
|
+
if (ptr) ptr = GetLargeResourceRecord(m, msg, ptr, end, 0, kDNSRecordTypePacketAdd, &m->rec);
|
|
2498
|
+
if (ptr && m->rec.r.resrec.rdlength >= DNSOpt_LeaseData_Space && m->rec.r.resrec.rdata->u.opt[0].opt == kDNSOpt_Lease)
|
|
2499
|
+
result = m->rec.r.resrec.rdata->u.opt[0].u.updatelease;
|
|
2500
|
+
m->rec.r.resrec.RecordType = 0; // Clear RecordType to show we're not still using it
|
|
2501
|
+
return(result);
|
|
2502
|
+
}
|
|
2503
|
+
|
|
2504
|
+
mDNSlocal const mDNSu8 *DumpRecords(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *ptr, const mDNSu8 *const end, int count, char *label)
|
|
2505
|
+
{
|
|
2506
|
+
int i;
|
|
2507
|
+
LogMsg("%2d %s", count, label);
|
|
2508
|
+
for (i = 0; i < count && ptr; i++)
|
|
2509
|
+
{
|
|
2510
|
+
// This puts a LargeCacheRecord on the stack instead of using the shared m->rec storage,
|
|
2511
|
+
// but since it's only used for debugging (and probably only on OS X, not on
|
|
2512
|
+
// embedded systems) putting a 9kB object on the stack isn't a big problem.
|
|
2513
|
+
LargeCacheRecord largecr;
|
|
2514
|
+
ptr = GetLargeResourceRecord(m, msg, ptr, end, mDNSInterface_Any, kDNSRecordTypePacketAns, &largecr);
|
|
2515
|
+
if (ptr) LogMsg("%2d TTL%8d %s", i, largecr.r.resrec.rroriginalttl, CRDisplayString(m, &largecr.r));
|
|
2516
|
+
}
|
|
2517
|
+
if (!ptr) LogMsg("ERROR: Premature end of packet data");
|
|
2518
|
+
return(ptr);
|
|
2519
|
+
}
|
|
2520
|
+
|
|
2521
|
+
#define DNS_OP_Name(X) ( \
|
|
2522
|
+
(X) == kDNSFlag0_OP_StdQuery ? "" : \
|
|
2523
|
+
(X) == kDNSFlag0_OP_Iquery ? "Iquery " : \
|
|
2524
|
+
(X) == kDNSFlag0_OP_Status ? "Status " : \
|
|
2525
|
+
(X) == kDNSFlag0_OP_Unused3 ? "Unused3 " : \
|
|
2526
|
+
(X) == kDNSFlag0_OP_Notify ? "Notify " : \
|
|
2527
|
+
(X) == kDNSFlag0_OP_Update ? "Update " : "?? " )
|
|
2528
|
+
|
|
2529
|
+
#define DNS_RC_Name(X) ( \
|
|
2530
|
+
(X) == kDNSFlag1_RC_NoErr ? "NoErr" : \
|
|
2531
|
+
(X) == kDNSFlag1_RC_FormErr ? "FormErr" : \
|
|
2532
|
+
(X) == kDNSFlag1_RC_ServFail ? "ServFail" : \
|
|
2533
|
+
(X) == kDNSFlag1_RC_NXDomain ? "NXDomain" : \
|
|
2534
|
+
(X) == kDNSFlag1_RC_NotImpl ? "NotImpl" : \
|
|
2535
|
+
(X) == kDNSFlag1_RC_Refused ? "Refused" : \
|
|
2536
|
+
(X) == kDNSFlag1_RC_YXDomain ? "YXDomain" : \
|
|
2537
|
+
(X) == kDNSFlag1_RC_YXRRSet ? "YXRRSet" : \
|
|
2538
|
+
(X) == kDNSFlag1_RC_NXRRSet ? "NXRRSet" : \
|
|
2539
|
+
(X) == kDNSFlag1_RC_NotAuth ? "NotAuth" : \
|
|
2540
|
+
(X) == kDNSFlag1_RC_NotZone ? "NotZone" : "??" )
|
|
2541
|
+
|
|
2542
|
+
// Note: DumpPacket expects the packet header fields in host byte order, not network byte order
|
|
2543
|
+
mDNSexport void DumpPacket(mDNS *const m, mStatus status, mDNSBool sent, char *transport,
|
|
2544
|
+
const mDNSAddr *srcaddr, mDNSIPPort srcport,
|
|
2545
|
+
const mDNSAddr *dstaddr, mDNSIPPort dstport, const DNSMessage *const msg, const mDNSu8 *const end)
|
|
2546
|
+
{
|
|
2547
|
+
mDNSBool IsUpdate = ((msg->h.flags.b[0] & kDNSFlag0_OP_Mask) == kDNSFlag0_OP_Update);
|
|
2548
|
+
const mDNSu8 *ptr = msg->data;
|
|
2549
|
+
int i;
|
|
2550
|
+
DNSQuestion q;
|
|
2551
|
+
char tbuffer[64], sbuffer[64], dbuffer[64] = "";
|
|
2552
|
+
if (!status) tbuffer[mDNS_snprintf(tbuffer, sizeof(tbuffer), sent ? "Sent" : "Received" )] = 0;
|
|
2553
|
+
else tbuffer[mDNS_snprintf(tbuffer, sizeof(tbuffer), "ERROR %d %sing", status, sent ? "Send" : "Receiv")] = 0;
|
|
2554
|
+
if (sent) sbuffer[mDNS_snprintf(sbuffer, sizeof(sbuffer), "port " )] = 0;
|
|
2555
|
+
else sbuffer[mDNS_snprintf(sbuffer, sizeof(sbuffer), "%#a:", srcaddr)] = 0;
|
|
2556
|
+
if (dstaddr || !mDNSIPPortIsZero(dstport))
|
|
2557
|
+
dbuffer[mDNS_snprintf(dbuffer, sizeof(dbuffer), " to %#a:%d", dstaddr, mDNSVal16(dstport))] = 0;
|
|
2558
|
+
|
|
2559
|
+
LogMsg("-- %s %s DNS %s%s (flags %02X%02X) RCODE: %s (%d) %s%s%s%s%s%sID: %d %d bytes from %s%d%s%s --",
|
|
2560
|
+
tbuffer, transport,
|
|
2561
|
+
DNS_OP_Name(msg->h.flags.b[0] & kDNSFlag0_OP_Mask),
|
|
2562
|
+
msg->h.flags.b[0] & kDNSFlag0_QR_Response ? "Response" : "Query",
|
|
2563
|
+
msg->h.flags.b[0], msg->h.flags.b[1],
|
|
2564
|
+
DNS_RC_Name(msg->h.flags.b[1] & kDNSFlag1_RC_Mask),
|
|
2565
|
+
msg->h.flags.b[1] & kDNSFlag1_RC_Mask,
|
|
2566
|
+
msg->h.flags.b[0] & kDNSFlag0_AA ? "AA " : "",
|
|
2567
|
+
msg->h.flags.b[0] & kDNSFlag0_TC ? "TC " : "",
|
|
2568
|
+
msg->h.flags.b[0] & kDNSFlag0_RD ? "RD " : "",
|
|
2569
|
+
msg->h.flags.b[1] & kDNSFlag1_RA ? "RA " : "",
|
|
2570
|
+
msg->h.flags.b[1] & kDNSFlag1_AD ? "AD " : "",
|
|
2571
|
+
msg->h.flags.b[1] & kDNSFlag1_CD ? "CD " : "",
|
|
2572
|
+
mDNSVal16(msg->h.id),
|
|
2573
|
+
end - msg->data,
|
|
2574
|
+
sbuffer, mDNSVal16(srcport), dbuffer,
|
|
2575
|
+
(msg->h.flags.b[0] & kDNSFlag0_TC) ? " (truncated)" : ""
|
|
2576
|
+
);
|
|
2577
|
+
|
|
2578
|
+
LogMsg("%2d %s", msg->h.numQuestions, IsUpdate ? "Zone" : "Questions");
|
|
2579
|
+
for (i = 0; i < msg->h.numQuestions && ptr; i++)
|
|
2580
|
+
{
|
|
2581
|
+
ptr = getQuestion(msg, ptr, end, mDNSInterface_Any, &q);
|
|
2582
|
+
if (ptr) LogMsg("%2d %##s %s", i, q.qname.c, DNSTypeName(q.qtype));
|
|
2583
|
+
}
|
|
2584
|
+
ptr = DumpRecords(m, msg, ptr, end, msg->h.numAnswers, IsUpdate ? "Prerequisites" : "Answers");
|
|
2585
|
+
ptr = DumpRecords(m, msg, ptr, end, msg->h.numAuthorities, IsUpdate ? "Updates" : "Authorities");
|
|
2586
|
+
ptr = DumpRecords(m, msg, ptr, end, msg->h.numAdditionals, "Additionals");
|
|
2587
|
+
LogMsg("--------------");
|
|
2588
|
+
}
|
|
2589
|
+
|
|
2590
|
+
// ***************************************************************************
|
|
2591
|
+
#if COMPILER_LIKES_PRAGMA_MARK
|
|
2592
|
+
#pragma mark -
|
|
2593
|
+
#pragma mark - Packet Sending Functions
|
|
2594
|
+
#endif
|
|
2595
|
+
|
|
2596
|
+
// Stub definition of TCPSocket_struct so we can access flags field. (Rest of TCPSocket_struct is platform-dependent.)
|
|
2597
|
+
struct TCPSocket_struct { TCPSocketFlags flags; /* ... */ };
|
|
2598
|
+
|
|
2599
|
+
struct UDPSocket_struct
|
|
2600
|
+
{
|
|
2601
|
+
mDNSIPPort port; // MUST BE FIRST FIELD -- mDNSCoreReceive expects every UDPSocket_struct to begin with mDNSIPPort port
|
|
2602
|
+
};
|
|
2603
|
+
|
|
2604
|
+
// Note: When we sign a DNS message using DNSDigest_SignMessage(), the current real-time clock value is used, which
|
|
2605
|
+
// is why we generally defer signing until we send the message, to ensure the signature is as fresh as possible.
|
|
2606
|
+
mDNSexport mStatus mDNSSendDNSMessage(mDNS *const m, DNSMessage *const msg, mDNSu8 *end,
|
|
2607
|
+
mDNSInterfaceID InterfaceID, UDPSocket *src, const mDNSAddr *dst, mDNSIPPort dstport, TCPSocket *sock, DomainAuthInfo *authInfo)
|
|
2608
|
+
{
|
|
2609
|
+
mStatus status = mStatus_NoError;
|
|
2610
|
+
const mDNSu16 numAdditionals = msg->h.numAdditionals;
|
|
2611
|
+
mDNSu8 *newend;
|
|
2612
|
+
mDNSu8 *limit = msg->data + AbsoluteMaxDNSMessageData;
|
|
2613
|
+
|
|
2614
|
+
// Zero-length message data is okay (e.g. for a DNS Update ack, where all we need is an ID and an error code
|
|
2615
|
+
if (end < msg->data || end - msg->data > AbsoluteMaxDNSMessageData)
|
|
2616
|
+
{
|
|
2617
|
+
LogMsg("mDNSSendDNSMessage: invalid message %p %p %d", msg->data, end, end - msg->data);
|
|
2618
|
+
return mStatus_BadParamErr;
|
|
2619
|
+
}
|
|
2620
|
+
|
|
2621
|
+
newend = putHINFO(m, msg, end, authInfo, limit);
|
|
2622
|
+
if (!newend) LogMsg("mDNSSendDNSMessage: putHINFO failed msg %p end %p, limit %p", msg->data, end, limit); // Not fatal
|
|
2623
|
+
else end = newend;
|
|
2624
|
+
|
|
2625
|
+
// Put all the integer values in IETF byte-order (MSB first, LSB second)
|
|
2626
|
+
SwapDNSHeaderBytes(msg);
|
|
2627
|
+
|
|
2628
|
+
if (authInfo) DNSDigest_SignMessage(msg, &end, authInfo, 0); // DNSDigest_SignMessage operates on message in network byte order
|
|
2629
|
+
if (!end) { LogMsg("mDNSSendDNSMessage: DNSDigest_SignMessage failed"); status = mStatus_NoMemoryErr; }
|
|
2630
|
+
else
|
|
2631
|
+
{
|
|
2632
|
+
// Send the packet on the wire
|
|
2633
|
+
if (!sock)
|
|
2634
|
+
status = mDNSPlatformSendUDP(m, msg, end, InterfaceID, src, dst, dstport);
|
|
2635
|
+
else
|
|
2636
|
+
{
|
|
2637
|
+
mDNSu16 msglen = (mDNSu16)(end - (mDNSu8 *)msg);
|
|
2638
|
+
mDNSu8 lenbuf[2] = { (mDNSu8)(msglen >> 8), (mDNSu8)(msglen & 0xFF) };
|
|
2639
|
+
long nsent = mDNSPlatformWriteTCP(sock, (char*)lenbuf, 2); // Should do scatter/gather here -- this is probably going out as two packets
|
|
2640
|
+
if (nsent != 2) { LogMsg("mDNSSendDNSMessage: write msg length failed %d/%d", nsent, 2); status = mStatus_ConnFailed; }
|
|
2641
|
+
else
|
|
2642
|
+
{
|
|
2643
|
+
nsent = mDNSPlatformWriteTCP(sock, (char *)msg, msglen);
|
|
2644
|
+
if (nsent != msglen) { LogMsg("mDNSSendDNSMessage: write msg body failed %d/%d", nsent, msglen); status = mStatus_ConnFailed; }
|
|
2645
|
+
}
|
|
2646
|
+
}
|
|
2647
|
+
}
|
|
2648
|
+
|
|
2649
|
+
// Swap the integer values back the way they were (remember that numAdditionals may have been changed by putHINFO and/or SignMessage)
|
|
2650
|
+
SwapDNSHeaderBytes(msg);
|
|
2651
|
+
|
|
2652
|
+
// Dump the packet with the HINFO and TSIG
|
|
2653
|
+
if (mDNS_PacketLoggingEnabled && !mDNSOpaque16IsZero(msg->h.id))
|
|
2654
|
+
DumpPacket(m, status, mDNStrue, sock && (sock->flags & kTCPSocketFlags_UseTLS) ? "TLS" : sock ? "TCP" : "UDP", mDNSNULL, src ? src->port : MulticastDNSPort, dst, dstport, msg, end);
|
|
2655
|
+
|
|
2656
|
+
// put the number of additionals back the way it was
|
|
2657
|
+
msg->h.numAdditionals = numAdditionals;
|
|
2658
|
+
|
|
2659
|
+
return(status);
|
|
2660
|
+
}
|
|
2661
|
+
|
|
2662
|
+
// ***************************************************************************
|
|
2663
|
+
#if COMPILER_LIKES_PRAGMA_MARK
|
|
2664
|
+
#pragma mark -
|
|
2665
|
+
#pragma mark - RR List Management & Task Management
|
|
2666
|
+
#endif
|
|
2667
|
+
|
|
2668
|
+
mDNSexport void mDNS_Lock_(mDNS *const m, const char * const functionname)
|
|
2669
|
+
{
|
|
2670
|
+
// MUST grab the platform lock FIRST!
|
|
2671
|
+
mDNSPlatformLock(m);
|
|
2672
|
+
|
|
2673
|
+
// Normally, mDNS_reentrancy is zero and so is mDNS_busy
|
|
2674
|
+
// However, when we call a client callback mDNS_busy is one, and we increment mDNS_reentrancy too
|
|
2675
|
+
// If that client callback does mDNS API calls, mDNS_reentrancy and mDNS_busy will both be one
|
|
2676
|
+
// If mDNS_busy != mDNS_reentrancy that's a bad sign
|
|
2677
|
+
if (m->mDNS_busy != m->mDNS_reentrancy)
|
|
2678
|
+
{
|
|
2679
|
+
LogMsg("%s: mDNS_Lock: Locking failure! mDNS_busy (%ld) != mDNS_reentrancy (%ld)", functionname, m->mDNS_busy, m->mDNS_reentrancy);
|
|
2680
|
+
#if ForceAlerts
|
|
2681
|
+
*(long*)0 = 0;
|
|
2682
|
+
#endif
|
|
2683
|
+
}
|
|
2684
|
+
|
|
2685
|
+
// If this is an initial entry into the mDNSCore code, set m->timenow
|
|
2686
|
+
// else, if this is a re-entrant entry into the mDNSCore code, m->timenow should already be set
|
|
2687
|
+
if (m->mDNS_busy == 0)
|
|
2688
|
+
{
|
|
2689
|
+
if (m->timenow)
|
|
2690
|
+
LogMsg("%s: mDNS_Lock: m->timenow already set (%ld/%ld)", functionname, m->timenow, mDNS_TimeNow_NoLock(m));
|
|
2691
|
+
m->timenow = mDNS_TimeNow_NoLock(m);
|
|
2692
|
+
if (m->timenow == 0) m->timenow = 1;
|
|
2693
|
+
}
|
|
2694
|
+
else if (m->timenow == 0)
|
|
2695
|
+
{
|
|
2696
|
+
LogMsg("%s: mDNS_Lock: m->mDNS_busy is %ld but m->timenow not set", functionname, m->mDNS_busy);
|
|
2697
|
+
m->timenow = mDNS_TimeNow_NoLock(m);
|
|
2698
|
+
if (m->timenow == 0) m->timenow = 1;
|
|
2699
|
+
}
|
|
2700
|
+
|
|
2701
|
+
if (m->timenow_last - m->timenow > 0)
|
|
2702
|
+
{
|
|
2703
|
+
m->timenow_adjust += m->timenow_last - m->timenow;
|
|
2704
|
+
LogMsg("%s: mDNSPlatformRawTime went backwards by %ld ticks; setting correction factor to %ld", functionname, m->timenow_last - m->timenow, m->timenow_adjust);
|
|
2705
|
+
m->timenow = m->timenow_last;
|
|
2706
|
+
}
|
|
2707
|
+
m->timenow_last = m->timenow;
|
|
2708
|
+
|
|
2709
|
+
// Increment mDNS_busy so we'll recognise re-entrant calls
|
|
2710
|
+
m->mDNS_busy++;
|
|
2711
|
+
}
|
|
2712
|
+
|
|
2713
|
+
mDNSlocal AuthRecord *AnyLocalRecordReady(const mDNS *const m)
|
|
2714
|
+
{
|
|
2715
|
+
AuthRecord *rr;
|
|
2716
|
+
for (rr = m->NewLocalRecords; rr; rr = rr->next)
|
|
2717
|
+
if (LocalRecordReady(rr)) return rr;
|
|
2718
|
+
return mDNSNULL;
|
|
2719
|
+
}
|
|
2720
|
+
|
|
2721
|
+
mDNSlocal mDNSs32 GetNextScheduledEvent(const mDNS *const m)
|
|
2722
|
+
{
|
|
2723
|
+
mDNSs32 e = m->timenow + 0x78000000;
|
|
2724
|
+
if (m->mDNSPlatformStatus != mStatus_NoError) return(e);
|
|
2725
|
+
if (m->NewQuestions)
|
|
2726
|
+
{
|
|
2727
|
+
if (m->NewQuestions->DelayAnswering) e = m->NewQuestions->DelayAnswering;
|
|
2728
|
+
else return(m->timenow);
|
|
2729
|
+
}
|
|
2730
|
+
if (m->NewLocalOnlyQuestions) return(m->timenow);
|
|
2731
|
+
if (m->NewLocalRecords && AnyLocalRecordReady(m)) return(m->timenow);
|
|
2732
|
+
if (m->NewLocalOnlyRecords) return(m->timenow);
|
|
2733
|
+
if (m->SPSProxyListChanged) return(m->timenow);
|
|
2734
|
+
if (m->LocalRemoveEvents) return(m->timenow);
|
|
2735
|
+
|
|
2736
|
+
#ifndef UNICAST_DISABLED
|
|
2737
|
+
if (e - m->NextuDNSEvent > 0) e = m->NextuDNSEvent;
|
|
2738
|
+
if (e - m->NextScheduledNATOp > 0) e = m->NextScheduledNATOp;
|
|
2739
|
+
if (m->NextSRVUpdate && e - m->NextSRVUpdate > 0) e = m->NextSRVUpdate;
|
|
2740
|
+
#endif
|
|
2741
|
+
|
|
2742
|
+
if (e - m->NextCacheCheck > 0) e = m->NextCacheCheck;
|
|
2743
|
+
if (e - m->NextScheduledSPS > 0) e = m->NextScheduledSPS;
|
|
2744
|
+
// NextScheduledSPRetry only valid when DelaySleep not set
|
|
2745
|
+
if (!m->DelaySleep && m->SleepLimit && e - m->NextScheduledSPRetry > 0) e = m->NextScheduledSPRetry;
|
|
2746
|
+
if (m->DelaySleep && e - m->DelaySleep > 0) e = m->DelaySleep;
|
|
2747
|
+
|
|
2748
|
+
if (m->SuppressSending)
|
|
2749
|
+
{
|
|
2750
|
+
if (e - m->SuppressSending > 0) e = m->SuppressSending;
|
|
2751
|
+
}
|
|
2752
|
+
else
|
|
2753
|
+
{
|
|
2754
|
+
if (e - m->NextScheduledQuery > 0) e = m->NextScheduledQuery;
|
|
2755
|
+
if (e - m->NextScheduledProbe > 0) e = m->NextScheduledProbe;
|
|
2756
|
+
if (e - m->NextScheduledResponse > 0) e = m->NextScheduledResponse;
|
|
2757
|
+
}
|
|
2758
|
+
if (e - m->NextScheduledStopTime > 0) e = m->NextScheduledStopTime;
|
|
2759
|
+
return(e);
|
|
2760
|
+
}
|
|
2761
|
+
|
|
2762
|
+
mDNSexport void ShowTaskSchedulingError(mDNS *const m)
|
|
2763
|
+
{
|
|
2764
|
+
AuthRecord *rr;
|
|
2765
|
+
mDNS_Lock(m);
|
|
2766
|
+
|
|
2767
|
+
LogMsg("Task Scheduling Error: Continuously busy for more than a second");
|
|
2768
|
+
|
|
2769
|
+
// Note: To accurately diagnose *why* we're busy, the debugging code here needs to mirror the logic in GetNextScheduledEvent above
|
|
2770
|
+
|
|
2771
|
+
if (m->NewQuestions && (!m->NewQuestions->DelayAnswering || m->timenow - m->NewQuestions->DelayAnswering >= 0))
|
|
2772
|
+
LogMsg("Task Scheduling Error: NewQuestion %##s (%s)",
|
|
2773
|
+
m->NewQuestions->qname.c, DNSTypeName(m->NewQuestions->qtype));
|
|
2774
|
+
|
|
2775
|
+
if (m->NewLocalOnlyQuestions)
|
|
2776
|
+
LogMsg("Task Scheduling Error: NewLocalOnlyQuestions %##s (%s)",
|
|
2777
|
+
m->NewLocalOnlyQuestions->qname.c, DNSTypeName(m->NewLocalOnlyQuestions->qtype));
|
|
2778
|
+
|
|
2779
|
+
if (m->NewLocalRecords)
|
|
2780
|
+
{
|
|
2781
|
+
rr = AnyLocalRecordReady(m);
|
|
2782
|
+
if (rr) LogMsg("Task Scheduling Error: NewLocalRecords %s", ARDisplayString(m, rr));
|
|
2783
|
+
}
|
|
2784
|
+
|
|
2785
|
+
if (m->NewLocalOnlyRecords) LogMsg("Task Scheduling Error: NewLocalOnlyRecords");
|
|
2786
|
+
|
|
2787
|
+
if (m->SPSProxyListChanged) LogMsg("Task Scheduling Error: SPSProxyListChanged");
|
|
2788
|
+
if (m->LocalRemoveEvents) LogMsg("Task Scheduling Error: LocalRemoveEvents");
|
|
2789
|
+
|
|
2790
|
+
if (m->timenow - m->NextScheduledEvent >= 0)
|
|
2791
|
+
LogMsg("Task Scheduling Error: m->NextScheduledEvent %d", m->timenow - m->NextScheduledEvent);
|
|
2792
|
+
|
|
2793
|
+
#ifndef UNICAST_DISABLED
|
|
2794
|
+
if (m->timenow - m->NextuDNSEvent >= 0)
|
|
2795
|
+
LogMsg("Task Scheduling Error: m->NextuDNSEvent %d", m->timenow - m->NextuDNSEvent);
|
|
2796
|
+
if (m->timenow - m->NextScheduledNATOp >= 0)
|
|
2797
|
+
LogMsg("Task Scheduling Error: m->NextScheduledNATOp %d", m->timenow - m->NextScheduledNATOp);
|
|
2798
|
+
if (m->NextSRVUpdate && m->timenow - m->NextSRVUpdate >= 0)
|
|
2799
|
+
LogMsg("Task Scheduling Error: m->NextSRVUpdate %d", m->timenow - m->NextSRVUpdate);
|
|
2800
|
+
#endif
|
|
2801
|
+
|
|
2802
|
+
if (m->timenow - m->NextCacheCheck >= 0)
|
|
2803
|
+
LogMsg("Task Scheduling Error: m->NextCacheCheck %d", m->timenow - m->NextCacheCheck);
|
|
2804
|
+
if (m->timenow - m->NextScheduledSPS >= 0)
|
|
2805
|
+
LogMsg("Task Scheduling Error: m->NextScheduledSPS %d", m->timenow - m->NextScheduledSPS);
|
|
2806
|
+
if (!m->DelaySleep && m->SleepLimit && m->timenow - m->NextScheduledSPRetry >= 0)
|
|
2807
|
+
LogMsg("Task Scheduling Error: m->NextScheduledSPRetry %d", m->timenow - m->NextScheduledSPRetry);
|
|
2808
|
+
if (m->DelaySleep && m->timenow - m->DelaySleep >= 0)
|
|
2809
|
+
LogMsg("Task Scheduling Error: m->DelaySleep %d", m->timenow - m->DelaySleep);
|
|
2810
|
+
|
|
2811
|
+
if (m->SuppressSending && m->timenow - m->SuppressSending >= 0)
|
|
2812
|
+
LogMsg("Task Scheduling Error: m->SuppressSending %d", m->timenow - m->SuppressSending);
|
|
2813
|
+
if (m->timenow - m->NextScheduledQuery >= 0)
|
|
2814
|
+
LogMsg("Task Scheduling Error: m->NextScheduledQuery %d", m->timenow - m->NextScheduledQuery);
|
|
2815
|
+
if (m->timenow - m->NextScheduledProbe >= 0)
|
|
2816
|
+
LogMsg("Task Scheduling Error: m->NextScheduledProbe %d", m->timenow - m->NextScheduledProbe);
|
|
2817
|
+
if (m->timenow - m->NextScheduledResponse >= 0)
|
|
2818
|
+
LogMsg("Task Scheduling Error: m->NextScheduledResponse %d", m->timenow - m->NextScheduledResponse);
|
|
2819
|
+
|
|
2820
|
+
mDNS_Unlock(m);
|
|
2821
|
+
}
|
|
2822
|
+
|
|
2823
|
+
mDNSexport void mDNS_Unlock_(mDNS *const m, const char * const functionname)
|
|
2824
|
+
{
|
|
2825
|
+
// Decrement mDNS_busy
|
|
2826
|
+
m->mDNS_busy--;
|
|
2827
|
+
|
|
2828
|
+
// Check for locking failures
|
|
2829
|
+
if (m->mDNS_busy != m->mDNS_reentrancy)
|
|
2830
|
+
{
|
|
2831
|
+
LogMsg("%s: mDNS_Unlock: Locking failure! mDNS_busy (%ld) != mDNS_reentrancy (%ld)", functionname, m->mDNS_busy, m->mDNS_reentrancy);
|
|
2832
|
+
#if ForceAlerts
|
|
2833
|
+
*(long*)0 = 0;
|
|
2834
|
+
#endif
|
|
2835
|
+
}
|
|
2836
|
+
|
|
2837
|
+
// If this is a final exit from the mDNSCore code, set m->NextScheduledEvent and clear m->timenow
|
|
2838
|
+
if (m->mDNS_busy == 0)
|
|
2839
|
+
{
|
|
2840
|
+
m->NextScheduledEvent = GetNextScheduledEvent(m);
|
|
2841
|
+
if (m->timenow == 0) LogMsg("%s: mDNS_Unlock: ERROR! m->timenow aready zero", functionname);
|
|
2842
|
+
m->timenow = 0;
|
|
2843
|
+
}
|
|
2844
|
+
|
|
2845
|
+
// MUST release the platform lock LAST!
|
|
2846
|
+
mDNSPlatformUnlock(m);
|
|
2847
|
+
}
|
|
2848
|
+
|
|
2849
|
+
// ***************************************************************************
|
|
2850
|
+
#if COMPILER_LIKES_PRAGMA_MARK
|
|
2851
|
+
#pragma mark -
|
|
2852
|
+
#pragma mark - Specialized mDNS version of vsnprintf
|
|
2853
|
+
#endif
|
|
2854
|
+
|
|
2855
|
+
static const struct mDNSprintf_format
|
|
2856
|
+
{
|
|
2857
|
+
unsigned leftJustify : 1;
|
|
2858
|
+
unsigned forceSign : 1;
|
|
2859
|
+
unsigned zeroPad : 1;
|
|
2860
|
+
unsigned havePrecision : 1;
|
|
2861
|
+
unsigned hSize : 1;
|
|
2862
|
+
unsigned lSize : 1;
|
|
2863
|
+
char altForm;
|
|
2864
|
+
char sign; // +, - or space
|
|
2865
|
+
unsigned int fieldWidth;
|
|
2866
|
+
unsigned int precision;
|
|
2867
|
+
} mDNSprintf_format_default = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
2868
|
+
|
|
2869
|
+
mDNSexport mDNSu32 mDNS_vsnprintf(char *sbuffer, mDNSu32 buflen, const char *fmt, va_list arg)
|
|
2870
|
+
{
|
|
2871
|
+
mDNSu32 nwritten = 0;
|
|
2872
|
+
int c;
|
|
2873
|
+
if (buflen == 0) return(0);
|
|
2874
|
+
buflen--; // Pre-reserve one space in the buffer for the terminating null
|
|
2875
|
+
if (buflen == 0) goto exit;
|
|
2876
|
+
|
|
2877
|
+
for (c = *fmt; c != 0; c = *++fmt)
|
|
2878
|
+
{
|
|
2879
|
+
if (c != '%')
|
|
2880
|
+
{
|
|
2881
|
+
*sbuffer++ = (char)c;
|
|
2882
|
+
if (++nwritten >= buflen) goto exit;
|
|
2883
|
+
}
|
|
2884
|
+
else
|
|
2885
|
+
{
|
|
2886
|
+
unsigned int i=0, j;
|
|
2887
|
+
// The mDNS Vsprintf Argument Conversion Buffer is used as a temporary holding area for
|
|
2888
|
+
// generating decimal numbers, hexdecimal numbers, IP addresses, domain name strings, etc.
|
|
2889
|
+
// The size needs to be enough for a 256-byte domain name plus some error text.
|
|
2890
|
+
#define mDNS_VACB_Size 300
|
|
2891
|
+
char mDNS_VACB[mDNS_VACB_Size];
|
|
2892
|
+
#define mDNS_VACB_Lim (&mDNS_VACB[mDNS_VACB_Size])
|
|
2893
|
+
#define mDNS_VACB_Remain(s) ((mDNSu32)(mDNS_VACB_Lim - s))
|
|
2894
|
+
char *s = mDNS_VACB_Lim, *digits;
|
|
2895
|
+
struct mDNSprintf_format F = mDNSprintf_format_default;
|
|
2896
|
+
|
|
2897
|
+
while (1) // decode flags
|
|
2898
|
+
{
|
|
2899
|
+
c = *++fmt;
|
|
2900
|
+
if (c == '-') F.leftJustify = 1;
|
|
2901
|
+
else if (c == '+') F.forceSign = 1;
|
|
2902
|
+
else if (c == ' ') F.sign = ' ';
|
|
2903
|
+
else if (c == '#') F.altForm++;
|
|
2904
|
+
else if (c == '0') F.zeroPad = 1;
|
|
2905
|
+
else break;
|
|
2906
|
+
}
|
|
2907
|
+
|
|
2908
|
+
if (c == '*') // decode field width
|
|
2909
|
+
{
|
|
2910
|
+
int f = va_arg(arg, int);
|
|
2911
|
+
if (f < 0) { f = -f; F.leftJustify = 1; }
|
|
2912
|
+
F.fieldWidth = (unsigned int)f;
|
|
2913
|
+
c = *++fmt;
|
|
2914
|
+
}
|
|
2915
|
+
else
|
|
2916
|
+
{
|
|
2917
|
+
for (; c >= '0' && c <= '9'; c = *++fmt)
|
|
2918
|
+
F.fieldWidth = (10 * F.fieldWidth) + (c - '0');
|
|
2919
|
+
}
|
|
2920
|
+
|
|
2921
|
+
if (c == '.') // decode precision
|
|
2922
|
+
{
|
|
2923
|
+
if ((c = *++fmt) == '*')
|
|
2924
|
+
{ F.precision = va_arg(arg, unsigned int); c = *++fmt; }
|
|
2925
|
+
else for (; c >= '0' && c <= '9'; c = *++fmt)
|
|
2926
|
+
F.precision = (10 * F.precision) + (c - '0');
|
|
2927
|
+
F.havePrecision = 1;
|
|
2928
|
+
}
|
|
2929
|
+
|
|
2930
|
+
if (F.leftJustify) F.zeroPad = 0;
|
|
2931
|
+
|
|
2932
|
+
conv:
|
|
2933
|
+
switch (c) // perform appropriate conversion
|
|
2934
|
+
{
|
|
2935
|
+
unsigned long n;
|
|
2936
|
+
case 'h' : F.hSize = 1; c = *++fmt; goto conv;
|
|
2937
|
+
case 'l' : // fall through
|
|
2938
|
+
case 'L' : F.lSize = 1; c = *++fmt; goto conv;
|
|
2939
|
+
case 'd' :
|
|
2940
|
+
case 'i' : if (F.lSize) n = (unsigned long)va_arg(arg, long);
|
|
2941
|
+
else n = (unsigned long)va_arg(arg, int);
|
|
2942
|
+
if (F.hSize) n = (short) n;
|
|
2943
|
+
if ((long) n < 0) { n = (unsigned long)-(long)n; F.sign = '-'; }
|
|
2944
|
+
else if (F.forceSign) F.sign = '+';
|
|
2945
|
+
goto decimal;
|
|
2946
|
+
case 'u' : if (F.lSize) n = va_arg(arg, unsigned long);
|
|
2947
|
+
else n = va_arg(arg, unsigned int);
|
|
2948
|
+
if (F.hSize) n = (unsigned short) n;
|
|
2949
|
+
F.sign = 0;
|
|
2950
|
+
goto decimal;
|
|
2951
|
+
decimal: if (!F.havePrecision)
|
|
2952
|
+
{
|
|
2953
|
+
if (F.zeroPad)
|
|
2954
|
+
{
|
|
2955
|
+
F.precision = F.fieldWidth;
|
|
2956
|
+
if (F.sign) --F.precision;
|
|
2957
|
+
}
|
|
2958
|
+
if (F.precision < 1) F.precision = 1;
|
|
2959
|
+
}
|
|
2960
|
+
if (F.precision > mDNS_VACB_Size - 1)
|
|
2961
|
+
F.precision = mDNS_VACB_Size - 1;
|
|
2962
|
+
for (i = 0; n; n /= 10, i++) *--s = (char)(n % 10 + '0');
|
|
2963
|
+
for (; i < F.precision; i++) *--s = '0';
|
|
2964
|
+
if (F.sign) { *--s = F.sign; i++; }
|
|
2965
|
+
break;
|
|
2966
|
+
|
|
2967
|
+
case 'o' : if (F.lSize) n = va_arg(arg, unsigned long);
|
|
2968
|
+
else n = va_arg(arg, unsigned int);
|
|
2969
|
+
if (F.hSize) n = (unsigned short) n;
|
|
2970
|
+
if (!F.havePrecision)
|
|
2971
|
+
{
|
|
2972
|
+
if (F.zeroPad) F.precision = F.fieldWidth;
|
|
2973
|
+
if (F.precision < 1) F.precision = 1;
|
|
2974
|
+
}
|
|
2975
|
+
if (F.precision > mDNS_VACB_Size - 1)
|
|
2976
|
+
F.precision = mDNS_VACB_Size - 1;
|
|
2977
|
+
for (i = 0; n; n /= 8, i++) *--s = (char)(n % 8 + '0');
|
|
2978
|
+
if (F.altForm && i && *s != '0') { *--s = '0'; i++; }
|
|
2979
|
+
for (; i < F.precision; i++) *--s = '0';
|
|
2980
|
+
break;
|
|
2981
|
+
|
|
2982
|
+
case 'a' : {
|
|
2983
|
+
unsigned char *a = va_arg(arg, unsigned char *);
|
|
2984
|
+
if (!a) { static char emsg[] = "<<NULL>>"; s = emsg; i = sizeof(emsg)-1; }
|
|
2985
|
+
else
|
|
2986
|
+
{
|
|
2987
|
+
s = mDNS_VACB; // Adjust s to point to the start of the buffer, not the end
|
|
2988
|
+
if (F.altForm)
|
|
2989
|
+
{
|
|
2990
|
+
mDNSAddr *ip = (mDNSAddr*)a;
|
|
2991
|
+
switch (ip->type)
|
|
2992
|
+
{
|
|
2993
|
+
case mDNSAddrType_IPv4: F.precision = 4; a = (unsigned char *)&ip->ip.v4; break;
|
|
2994
|
+
case mDNSAddrType_IPv6: F.precision = 16; a = (unsigned char *)&ip->ip.v6; break;
|
|
2995
|
+
default: F.precision = 0; break;
|
|
2996
|
+
}
|
|
2997
|
+
}
|
|
2998
|
+
if (F.altForm && !F.precision)
|
|
2999
|
+
i = mDNS_snprintf(mDNS_VACB, sizeof(mDNS_VACB), "«ZERO ADDRESS»");
|
|
3000
|
+
else switch (F.precision)
|
|
3001
|
+
{
|
|
3002
|
+
case 4: i = mDNS_snprintf(mDNS_VACB, sizeof(mDNS_VACB), "%d.%d.%d.%d",
|
|
3003
|
+
a[0], a[1], a[2], a[3]); break;
|
|
3004
|
+
case 6: i = mDNS_snprintf(mDNS_VACB, sizeof(mDNS_VACB), "%02X:%02X:%02X:%02X:%02X:%02X",
|
|
3005
|
+
a[0], a[1], a[2], a[3], a[4], a[5]); break;
|
|
3006
|
+
case 16: i = mDNS_snprintf(mDNS_VACB, sizeof(mDNS_VACB),
|
|
3007
|
+
"%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X:%02X%02X",
|
|
3008
|
+
a[0x0], a[0x1], a[0x2], a[0x3], a[0x4], a[0x5], a[0x6], a[0x7],
|
|
3009
|
+
a[0x8], a[0x9], a[0xA], a[0xB], a[0xC], a[0xD], a[0xE], a[0xF]); break;
|
|
3010
|
+
default: i = mDNS_snprintf(mDNS_VACB, sizeof(mDNS_VACB), "%s", "<< ERROR: Must specify"
|
|
3011
|
+
" address size (i.e. %.4a=IPv4, %.6a=Ethernet, %.16a=IPv6) >>"); break;
|
|
3012
|
+
}
|
|
3013
|
+
}
|
|
3014
|
+
}
|
|
3015
|
+
break;
|
|
3016
|
+
|
|
3017
|
+
case 'p' : F.havePrecision = F.lSize = 1;
|
|
3018
|
+
F.precision = sizeof(void*) * 2; // 8 characters on 32-bit; 16 characters on 64-bit
|
|
3019
|
+
case 'X' : digits = "0123456789ABCDEF";
|
|
3020
|
+
goto hexadecimal;
|
|
3021
|
+
case 'x' : digits = "0123456789abcdef";
|
|
3022
|
+
hexadecimal:if (F.lSize) n = va_arg(arg, unsigned long);
|
|
3023
|
+
else n = va_arg(arg, unsigned int);
|
|
3024
|
+
if (F.hSize) n = (unsigned short) n;
|
|
3025
|
+
if (!F.havePrecision)
|
|
3026
|
+
{
|
|
3027
|
+
if (F.zeroPad)
|
|
3028
|
+
{
|
|
3029
|
+
F.precision = F.fieldWidth;
|
|
3030
|
+
if (F.altForm) F.precision -= 2;
|
|
3031
|
+
}
|
|
3032
|
+
if (F.precision < 1) F.precision = 1;
|
|
3033
|
+
}
|
|
3034
|
+
if (F.precision > mDNS_VACB_Size - 1)
|
|
3035
|
+
F.precision = mDNS_VACB_Size - 1;
|
|
3036
|
+
for (i = 0; n; n /= 16, i++) *--s = digits[n % 16];
|
|
3037
|
+
for (; i < F.precision; i++) *--s = '0';
|
|
3038
|
+
if (F.altForm) { *--s = (char)c; *--s = '0'; i += 2; }
|
|
3039
|
+
break;
|
|
3040
|
+
|
|
3041
|
+
case 'c' : *--s = (char)va_arg(arg, int); i = 1; break;
|
|
3042
|
+
|
|
3043
|
+
case 's' : s = va_arg(arg, char *);
|
|
3044
|
+
if (!s) { static char emsg[] = "<<NULL>>"; s = emsg; i = sizeof(emsg)-1; }
|
|
3045
|
+
else switch (F.altForm)
|
|
3046
|
+
{
|
|
3047
|
+
case 0: i=0;
|
|
3048
|
+
if (!F.havePrecision) // C string
|
|
3049
|
+
while (s[i]) i++;
|
|
3050
|
+
else
|
|
3051
|
+
{
|
|
3052
|
+
while ((i < F.precision) && s[i]) i++;
|
|
3053
|
+
// Make sure we don't truncate in the middle of a UTF-8 character
|
|
3054
|
+
// If last character we got was any kind of UTF-8 multi-byte character,
|
|
3055
|
+
// then see if we have to back up.
|
|
3056
|
+
// This is not as easy as the similar checks below, because
|
|
3057
|
+
// here we can't assume it's safe to examine the *next* byte, so we
|
|
3058
|
+
// have to confine ourselves to working only backwards in the string.
|
|
3059
|
+
j = i; // Record where we got to
|
|
3060
|
+
// Now, back up until we find first non-continuation-char
|
|
3061
|
+
while (i>0 && (s[i-1] & 0xC0) == 0x80) i--;
|
|
3062
|
+
// Now s[i-1] is the first non-continuation-char
|
|
3063
|
+
// and (j-i) is the number of continuation-chars we found
|
|
3064
|
+
if (i>0 && (s[i-1] & 0xC0) == 0xC0) // If we found a start-char
|
|
3065
|
+
{
|
|
3066
|
+
i--; // Tentatively eliminate this start-char as well
|
|
3067
|
+
// Now (j-i) is the number of characters we're considering eliminating.
|
|
3068
|
+
// To be legal UTF-8, the start-char must contain (j-i) one-bits,
|
|
3069
|
+
// followed by a zero bit. If we shift it right by (7-(j-i)) bits
|
|
3070
|
+
// (with sign extension) then the result has to be 0xFE.
|
|
3071
|
+
// If this is right, then we reinstate the tentatively eliminated bytes.
|
|
3072
|
+
if (((j-i) < 7) && (((s[i] >> (7-(j-i))) & 0xFF) == 0xFE)) i = j;
|
|
3073
|
+
}
|
|
3074
|
+
}
|
|
3075
|
+
break;
|
|
3076
|
+
case 1: i = (unsigned char) *s++; break; // Pascal string
|
|
3077
|
+
case 2: { // DNS label-sequence name
|
|
3078
|
+
unsigned char *a = (unsigned char *)s;
|
|
3079
|
+
s = mDNS_VACB; // Adjust s to point to the start of the buffer, not the end
|
|
3080
|
+
if (*a == 0) *s++ = '.'; // Special case for root DNS name
|
|
3081
|
+
while (*a)
|
|
3082
|
+
{
|
|
3083
|
+
char buf[63*4+1];
|
|
3084
|
+
if (*a > 63)
|
|
3085
|
+
{ s += mDNS_snprintf(s, mDNS_VACB_Remain(s), "<<INVALID LABEL LENGTH %u>>", *a); break; }
|
|
3086
|
+
if (s + *a >= &mDNS_VACB[254])
|
|
3087
|
+
{ s += mDNS_snprintf(s, mDNS_VACB_Remain(s), "<<NAME TOO LONG>>"); break; }
|
|
3088
|
+
// Need to use ConvertDomainLabelToCString to do proper escaping here,
|
|
3089
|
+
// so it's clear what's a literal dot and what's a label separator
|
|
3090
|
+
ConvertDomainLabelToCString((domainlabel*)a, buf);
|
|
3091
|
+
s += mDNS_snprintf(s, mDNS_VACB_Remain(s), "%s.", buf);
|
|
3092
|
+
a += 1 + *a;
|
|
3093
|
+
}
|
|
3094
|
+
i = (mDNSu32)(s - mDNS_VACB);
|
|
3095
|
+
s = mDNS_VACB; // Reset s back to the start of the buffer
|
|
3096
|
+
break;
|
|
3097
|
+
}
|
|
3098
|
+
}
|
|
3099
|
+
// Make sure we don't truncate in the middle of a UTF-8 character (see similar comment below)
|
|
3100
|
+
if (F.havePrecision && i > F.precision)
|
|
3101
|
+
{ i = F.precision; while (i>0 && (s[i] & 0xC0) == 0x80) i--; }
|
|
3102
|
+
break;
|
|
3103
|
+
|
|
3104
|
+
case 'n' : s = va_arg(arg, char *);
|
|
3105
|
+
if (F.hSize) * (short *) s = (short)nwritten;
|
|
3106
|
+
else if (F.lSize) * (long *) s = (long)nwritten;
|
|
3107
|
+
else * (int *) s = (int)nwritten;
|
|
3108
|
+
continue;
|
|
3109
|
+
|
|
3110
|
+
default: s = mDNS_VACB;
|
|
3111
|
+
i = mDNS_snprintf(mDNS_VACB, sizeof(mDNS_VACB), "<<UNKNOWN FORMAT CONVERSION CODE %%%c>>", c);
|
|
3112
|
+
|
|
3113
|
+
case '%' : *sbuffer++ = (char)c;
|
|
3114
|
+
if (++nwritten >= buflen) goto exit;
|
|
3115
|
+
break;
|
|
3116
|
+
}
|
|
3117
|
+
|
|
3118
|
+
if (i < F.fieldWidth && !F.leftJustify) // Pad on the left
|
|
3119
|
+
do {
|
|
3120
|
+
*sbuffer++ = ' ';
|
|
3121
|
+
if (++nwritten >= buflen) goto exit;
|
|
3122
|
+
} while (i < --F.fieldWidth);
|
|
3123
|
+
|
|
3124
|
+
// Make sure we don't truncate in the middle of a UTF-8 character.
|
|
3125
|
+
// Note: s[i] is the first eliminated character; i.e. the next character *after* the last character of the
|
|
3126
|
+
// allowed output. If s[i] is a UTF-8 continuation character, then we've cut a unicode character in half,
|
|
3127
|
+
// so back up 'i' until s[i] is no longer a UTF-8 continuation character. (if the input was proprly
|
|
3128
|
+
// formed, s[i] will now be the UTF-8 start character of the multi-byte character we just eliminated).
|
|
3129
|
+
if (i > buflen - nwritten)
|
|
3130
|
+
{ i = buflen - nwritten; while (i>0 && (s[i] & 0xC0) == 0x80) i--; }
|
|
3131
|
+
for (j=0; j<i; j++) *sbuffer++ = *s++; // Write the converted result
|
|
3132
|
+
nwritten += i;
|
|
3133
|
+
if (nwritten >= buflen) goto exit;
|
|
3134
|
+
|
|
3135
|
+
for (; i < F.fieldWidth; i++) // Pad on the right
|
|
3136
|
+
{
|
|
3137
|
+
*sbuffer++ = ' ';
|
|
3138
|
+
if (++nwritten >= buflen) goto exit;
|
|
3139
|
+
}
|
|
3140
|
+
}
|
|
3141
|
+
}
|
|
3142
|
+
exit:
|
|
3143
|
+
*sbuffer++ = 0;
|
|
3144
|
+
return(nwritten);
|
|
3145
|
+
}
|
|
3146
|
+
|
|
3147
|
+
mDNSexport mDNSu32 mDNS_snprintf(char *sbuffer, mDNSu32 buflen, const char *fmt, ...)
|
|
3148
|
+
{
|
|
3149
|
+
mDNSu32 length;
|
|
3150
|
+
|
|
3151
|
+
va_list ptr;
|
|
3152
|
+
va_start(ptr,fmt);
|
|
3153
|
+
length = mDNS_vsnprintf(sbuffer, buflen, fmt, ptr);
|
|
3154
|
+
va_end(ptr);
|
|
3155
|
+
|
|
3156
|
+
return(length);
|
|
3157
|
+
}
|