mDNSEmbeddedAPI.h revision 47e4cebad7397422144bb03a21f3f7682c062c4a
147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt/* -*- Mode: C; tab-width: 4 -*-
247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt *
347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * Copyright (c) 2002-2003 Apple Computer, Inc. All rights reserved.
447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt *
547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * Licensed under the Apache License, Version 2.0 (the "License");
647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * you may not use this file except in compliance with the License.
747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * You may obtain a copy of the License at
847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt *
947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt *     http://www.apache.org/licenses/LICENSE-2.0
1047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt *
1147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * Unless required by applicable law or agreed to in writing, software
1247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * distributed under the License is distributed on an "AS IS" BASIS,
1347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * See the License for the specific language governing permissions and
1547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt * limitations under the License.
1647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
1747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   NOTE:
1847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   If you're building an application that uses DNS Service Discovery
1947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   this is probably NOT the header file you're looking for.
2047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   In most cases you will want to use /usr/include/dns_sd.h instead.
2147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
2247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   This header file defines the lowest level raw interface to mDNSCore,
2347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   which is appropriate *only* on tiny embedded systems where everything
2447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   runs in a single address space and memory is extremely constrained.
2547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   All the APIs here are malloc-free, which means that the caller is
2647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   responsible for passing in a pointer to the relevant storage that
2747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   will be used in the execution of that call, and (when called with
2847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   correct parameters) all the calls are guaranteed to succeed. There
2947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   is never a case where a call can suffer intermittent failures because
3047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   the implementation calls malloc() and sometimes malloc() returns NULL
3147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   because memory is so limited that no more is available.
3247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   This is primarily for devices that need to have precisely known fixed
3347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   memory requirements, with absolutely no uncertainty or run-time variation,
3447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   but that certainty comes at a cost of more difficult programming.
3547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
3647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   For applications running on general-purpose desktop operating systems
3747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   (Mac OS, Linux, Solaris, Windows, etc.) the API you should use is
3847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   /usr/include/dns_sd.h, which defines the API by which multiple
3947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   independent client processes communicate their DNS Service Discovery
4047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   requests to a single "mdnsd" daemon running in the background.
4147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
4247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   Even on platforms that don't run multiple independent processes in
4347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   multiple independent address spaces, you can still use the preferred
4447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   dns_sd.h APIs by linking in "dnssd_clientshim.c", which implements
4547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   the standard "dns_sd.h" API calls, allocates any required storage
4647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   using malloc(), and then calls through to the low-level malloc-free
4747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   mDNSCore routines defined here. This has the benefit that even though
4847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   you're running on a small embedded system with a single address space,
4947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   you can still use the exact same client C code as you'd use on a
5047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt   general-purpose desktop system.
5147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
5247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt */
5347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
5447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#ifndef __mDNSClientAPI_h
5547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define __mDNSClientAPI_h
5647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
5747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if defined(EFI32) || defined(EFI64) || defined(EFIX64)
5847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// EFI doesn't have stdarg.h unless it's building with GCC.
5947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include "Tiano.h"
6047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if !defined(__GNUC__)
6147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define va_list         VA_LIST
6247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define va_start(a, b)  VA_START(a, b)
6347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define va_end(a)       VA_END(a)
6447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define va_arg(a, b)    VA_ARG(a, b)
6547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
6647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#else
6747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include <stdarg.h>		// stdarg.h is required for for va_list support for the mDNS_vsnprintf declaration
6847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
6947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
7047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include "mDNSDebug.h"
7147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if APPLE_OSX_mDNSResponder
7247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include <uuid/uuid.h>
7347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
7447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
7547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#ifdef __cplusplus
7647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	extern "C" {
7747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
7847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
7947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ***************************************************************************
8047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Function scope indicators
8147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
8247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// If you see "mDNSlocal" before a function name in a C file, it means the function is not callable outside this file
8347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#ifndef mDNSlocal
8447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSlocal static
8547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
8647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// If you see "mDNSexport" before a symbol in a C file, it means the symbol is exported for use by clients
8747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// For every "mDNSexport" in a C file, there needs to be a corresponding "extern" declaration in some header file
8847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// (When a C file #includes a header file, the "extern" declarations tell the compiler:
8947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// "This symbol exists -- but not necessarily in this C file.")
9047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#ifndef mDNSexport
9147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSexport
9247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
9347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
9447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Explanation: These local/export markers are a little habit of mine for signaling the programmers' intentions.
9547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// When "mDNSlocal" is just a synonym for "static", and "mDNSexport" is a complete no-op, you could be
9647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// forgiven for asking what purpose they serve. The idea is that if you see "mDNSexport" in front of a
9747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// function definition it means the programmer intended it to be exported and callable from other files
9847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// in the project. If you see "mDNSlocal" in front of a function definition it means the programmer
9947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// intended it to be private to that file. If you see neither in front of a function definition it
10047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// means the programmer forgot (so you should work out which it is supposed to be, and fix it).
10147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Using "mDNSlocal" instead of "static" makes it easier to do a textual searches for one or the other.
10247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// For example you can do a search for "static" to find if any functions declare any local variables as "static"
10347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// (generally a bad idea unless it's also "const", because static storage usually risks being non-thread-safe)
10447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// without the results being cluttered with hundreds of matches for functions declared static.
10547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// - Stuart Cheshire
10647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
10747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ***************************************************************************
10847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Structure packing macro
10947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
11047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// If we're not using GNUC, it's not fatal.
11147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Most compilers naturally pack the on-the-wire structures correctly anyway, so a plain "struct" is usually fine.
11247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// In the event that structures are not packed correctly, mDNS_Init() will detect this and report an error, so the
11347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// developer will know what's wrong, and can investigate what needs to be done on that compiler to provide proper packing.
11447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#ifndef packedstruct
11547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt #if ((__GNUC__ > 2) || ((__GNUC__ == 2) && (__GNUC_MINOR__ >= 9)))
11647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  #define packedstruct struct __attribute__((__packed__))
11747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  #define packedunion  union  __attribute__((__packed__))
11847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt #else
11947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  #define packedstruct struct
12047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  #define packedunion  union
12147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt #endif
12247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
12347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
12447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ***************************************************************************
12547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if 0
12647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark - DNS Resource Record class and type constants
12747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
12847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
12947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef enum							// From RFC 1035
13047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
13147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSClass_IN               = 1,		// Internet
13247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSClass_CS               = 2,		// CSNET
13347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSClass_CH               = 3,		// CHAOS
13447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSClass_HS               = 4,		// Hesiod
13547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSClass_NONE             = 254,	// Used in DNS UPDATE [RFC 2136]
13647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
13747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSClass_Mask             = 0x7FFF,// Multicast DNS uses the bottom 15 bits to identify the record class...
13847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSClass_UniqueRRSet      = 0x8000,// ... and the top bit indicates that all other cached records are now invalid
13947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
14047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSQClass_ANY             = 255,	// Not a DNS class, but a DNS query class, meaning "all classes"
14147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSQClass_UnicastResponse = 0x8000	// Top bit set in a question means "unicast response acceptable"
14247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} DNS_ClassValues;
14347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
14447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef enum				// From RFC 1035
14547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
14647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_A = 1,			//  1 Address
14747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_NS,			//  2 Name Server
14847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_MD,			//  3 Mail Destination
14947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_MF,			//  4 Mail Forwarder
15047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_CNAME,			//  5 Canonical Name
15147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_SOA,			//  6 Start of Authority
15247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_MB,			//  7 Mailbox
15347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_MG,			//  8 Mail Group
15447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_MR,			//  9 Mail Rename
15547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_NULL,			// 10 NULL RR
15647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_WKS,			// 11 Well-known-service
15747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_PTR,			// 12 Domain name pointer
15847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_HINFO,			// 13 Host information
15947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_MINFO,			// 14 Mailbox information
16047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_MX,			// 15 Mail Exchanger
16147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_TXT,			// 16 Arbitrary text string
16247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_RP,			// 17 Responsible person
16347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_AFSDB,			// 18 AFS cell database
16447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_X25,			// 19 X_25 calling address
16547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_ISDN,			// 20 ISDN calling address
16647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_RT,			// 21 Router
16747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_NSAP,			// 22 NSAP address
16847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_NSAP_PTR,		// 23 Reverse NSAP lookup (deprecated)
16947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_SIG,			// 24 Security signature
17047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_KEY,			// 25 Security key
17147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_PX,			// 26 X.400 mail mapping
17247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_GPOS,			// 27 Geographical position (withdrawn)
17347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_AAAA,			// 28 IPv6 Address
17447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_LOC,			// 29 Location Information
17547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_NXT,			// 30 Next domain (security)
17647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_EID,			// 31 Endpoint identifier
17747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_NIMLOC,		// 32 Nimrod Locator
17847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_SRV,			// 33 Service record
17947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_ATMA,			// 34 ATM Address
18047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_NAPTR,			// 35 Naming Authority PoinTeR
18147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_KX,			// 36 Key Exchange
18247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_CERT,			// 37 Certification record
18347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_A6,			// 38 IPv6 Address (deprecated)
18447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_DNAME,			// 39 Non-terminal DNAME (for IPv6)
18547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_SINK,			// 40 Kitchen sink (experimental)
18647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_OPT,			// 41 EDNS0 option (meta-RR)
18747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_APL,			// 42 Address Prefix List
18847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_DS,			// 43 Delegation Signer
18947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_SSHFP,			// 44 SSH Key Fingerprint
19047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_IPSECKEY,		// 45 IPSECKEY
19147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_RRSIG,			// 46 RRSIG
19247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_NSEC,			// 47 Denial of Existence
19347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_DNSKEY,		// 48 DNSKEY
19447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_DHCID,			// 49 DHCP Client Identifier
19547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_NSEC3,			// 50 Hashed Authenticated Denial of Existence
19647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_NSEC3PARAM,	// 51 Hashed Authenticated Denial of Existence
19747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
19847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_HIP = 55,		// 55 Host Identity Protocol
19947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
20047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_SPF = 99,		// 99 Sender Policy Framework for E-Mail
20147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_UINFO,			// 100 IANA-Reserved
20247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_UID,			// 101 IANA-Reserved
20347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_GID,			// 102 IANA-Reserved
20447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_UNSPEC,		// 103 IANA-Reserved
20547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
20647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_TKEY = 249,	// 249 Transaction key
20747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_TSIG,			// 250 Transaction signature
20847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_IXFR,			// 251 Incremental zone transfer
20947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_AXFR,			// 252 Transfer zone of authority
21047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_MAILB,			// 253 Transfer mailbox records
21147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSType_MAILA,			// 254 Transfer mail agent records
21247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSQType_ANY			// Not a DNS type, but a DNS query type, meaning "all types"
21347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} DNS_TypeValues;
21447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
21547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ***************************************************************************
21647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if 0
21747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark -
21847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark - Simple types
21947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
22047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
22147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNS defines its own names for these common types to simplify portability across
22247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// multiple platforms that may each have their own (different) names for these types.
22347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef          int   mDNSBool;
22447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef   signed char  mDNSs8;
22547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef unsigned char  mDNSu8;
22647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef   signed short mDNSs16;
22747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef unsigned short mDNSu16;
22847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
22947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// <http://gcc.gnu.org/onlinedocs/gcc-3.3.3/cpp/Common-Predefined-Macros.html> says
23047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//   __LP64__ _LP64
23147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//   These macros are defined, with value 1, if (and only if) the compilation is
23247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//   for a target where long int and pointer both use 64-bits and int uses 32-bit.
23347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// <http://www.intel.com/software/products/compilers/clin/docs/ug/lin1077.htm> says
23447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//   Macro Name __LP64__ Value 1
23547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// A quick Google search for "defined(__LP64__)" OR "#ifdef __LP64__" gives 2590 hits and
23647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// a search for "#if __LP64__" gives only 12, so I think we'll go with the majority and use defined()
23747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if defined(_ILP64) || defined(__ILP64__)
23847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef   signed int32 mDNSs32;
23947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef unsigned int32 mDNSu32;
24047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#elif defined(_LP64) || defined(__LP64__)
24147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef   signed int   mDNSs32;
24247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef unsigned int   mDNSu32;
24347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#else
24447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef   signed long  mDNSs32;
24547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef unsigned long  mDNSu32;
24647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//typedef   signed int mDNSs32;
24747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//typedef unsigned int mDNSu32;
24847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
24947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
25047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// To enforce useful type checking, we make mDNSInterfaceID be a pointer to a dummy struct
25147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// This way, mDNSInterfaceIDs can be assigned, and compared with each other, but not with other types
25247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Declaring the type to be the typical generic "void *" would lack this type checking
25347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct mDNSInterfaceID_dummystruct { void *dummy; } *mDNSInterfaceID;
25447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
25547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// These types are for opaque two- and four-byte identifiers.
25647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// The "NotAnInteger" fields of the unions allow the value to be conveniently passed around in a
25747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// register for the sake of efficiency, and compared for equality or inequality, but don't forget --
25847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// just because it is in a register doesn't mean it is an integer. Operations like greater than,
25947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// less than, add, multiply, increment, decrement, etc., are undefined for opaque identifiers,
26047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// and if you make the mistake of trying to do those using the NotAnInteger field, then you'll
26147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// find you get code that doesn't work consistently on big-endian and little-endian machines.
26247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if defined(_WIN32)
26347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt #pragma pack(push,2)
26447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
26547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef       union { mDNSu8 b[ 2]; mDNSu16 NotAnInteger; } mDNSOpaque16;
26647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef       union { mDNSu8 b[ 4]; mDNSu32 NotAnInteger; } mDNSOpaque32;
26747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef packedunion { mDNSu8 b[ 6]; mDNSu16 w[3]; mDNSu32 l[1]; } mDNSOpaque48;
26847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef       union { mDNSu8 b[ 8]; mDNSu16 w[4]; mDNSu32 l[2]; } mDNSOpaque64;
26947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef       union { mDNSu8 b[16]; mDNSu16 w[8]; mDNSu32 l[4]; } mDNSOpaque128;
27047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if defined(_WIN32)
27147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt #pragma pack(pop)
27247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
27347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
27447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef mDNSOpaque16  mDNSIPPort;		// An IP port is a two-byte opaque identifier (not an integer)
27547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef mDNSOpaque32  mDNSv4Addr;		// An IP address is a four-byte opaque identifier (not an integer)
27647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef mDNSOpaque128 mDNSv6Addr;		// An IPv6 address is a 16-byte opaque identifier (not an integer)
27747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef mDNSOpaque48  mDNSEthAddr;		// An Ethernet address is a six-byte opaque identifier (not an integer)
27847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
27947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Bit operations for opaque 64 bit quantity. Uses the 32 bit quantity(l[2]) to set and clear bits
28047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSNBBY 8
28147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define bit_set_opaque64(op64, index) (op64.l[((index))/(sizeof(mDNSu32) * mDNSNBBY)] |= (1 << ((index) % (sizeof(mDNSu32) * mDNSNBBY))))
28247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define bit_clr_opaque64(op64, index) (op64.l[((index))/(sizeof(mDNSu32) * mDNSNBBY)] &= ~(1 << ((index) % (sizeof(mDNSu32) * mDNSNBBY))))
28347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define bit_get_opaque64(op64, index) (op64.l[((index))/(sizeof(mDNSu32) * mDNSNBBY)] & (1 << ((index) % (sizeof(mDNSu32) * mDNSNBBY))))
28447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
28547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltenum
28647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
28747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSAddrType_None    = 0,
28847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSAddrType_IPv4    = 4,
28947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSAddrType_IPv6    = 6,
29047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSAddrType_Unknown = ~0	// Special marker value used in known answer list recording
29147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
29247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
29347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltenum
29447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
29547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSTransport_None = 0,
29647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSTransport_UDP  = 1,
29747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSTransport_TCP  = 2
29847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
29947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
30047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct
30147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
30247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32 type;
30347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	union { mDNSv6Addr v6; mDNSv4Addr v4; } ip;
30447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} mDNSAddr;
30547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
30647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltenum { mDNSfalse = 0, mDNStrue = 1 };
30747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
30847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSNULL 0L
30947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
31047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltenum
31147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
31247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_Waiting           = 1,
31347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_NoError           = 0,
31447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
31547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// mDNS return values are in the range FFFE FF00 (-65792) to FFFE FFFF (-65537)
31647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// The top end of the range (FFFE FFFF) is used for error codes;
31747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// the bottom end of the range (FFFE FF00) is used for non-error values;
31847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
31947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Error codes:
32047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_UnknownErr                = -65537,		// First value: 0xFFFE FFFF
32147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_NoSuchNameErr             = -65538,
32247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_NoMemoryErr               = -65539,
32347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_BadParamErr               = -65540,
32447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_BadReferenceErr           = -65541,
32547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_BadStateErr               = -65542,
32647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_BadFlagsErr               = -65543,
32747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_UnsupportedErr            = -65544,
32847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_NotInitializedErr         = -65545,
32947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_NoCache                   = -65546,
33047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_AlreadyRegistered         = -65547,
33147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_NameConflict              = -65548,
33247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_Invalid                   = -65549,
33347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_Firewall                  = -65550,
33447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_Incompatible              = -65551,
33547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_BadInterfaceErr           = -65552,
33647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_Refused                   = -65553,
33747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_NoSuchRecord              = -65554,
33847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_NoAuth                    = -65555,
33947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_NoSuchKey                 = -65556,
34047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_NATTraversal              = -65557,
34147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_DoubleNAT                 = -65558,
34247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_BadTime                   = -65559,
34347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_BadSig                    = -65560,     // while we define this per RFC 2845, BIND 9 returns Refused for bad/missing signatures
34447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_BadKey                    = -65561,
34547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_TransientErr              = -65562,     // transient failures, e.g. sending packets shortly after a network transition or wake from sleep
34647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_ServiceNotRunning         = -65563,     // Background daemon not running
34747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_NATPortMappingUnsupported = -65564,     // NAT doesn't support NAT-PMP or UPnP
34847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_NATPortMappingDisabled    = -65565,     // NAT supports NAT-PMP or UPnP but it's disabled by the administrator
34947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_NoRouter                  = -65566,
35047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_PollingMode               = -65567,
35147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_Timeout                   = -65568,
35247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// -65568 to -65786 currently unused; available for allocation
35347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
35447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// tcp connection status
35547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_ConnPending       = -65787,
35647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_ConnFailed        = -65788,
35747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_ConnEstablished   = -65789,
35847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
35947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Non-error values:
36047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_GrowCache         = -65790,
36147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_ConfigChanged     = -65791,
36247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus_MemFree           = -65792		// Last value: 0xFFFE FF00
36347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// mStatus_MemFree is the last legal mDNS error code, at the end of the range allocated for mDNS
36447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
36547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
36647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef mDNSs32 mStatus;
36747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
36847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// RFC 1034/1035 specify that a domain label consists of a length byte plus up to 63 characters
36947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define MAX_DOMAIN_LABEL 63
37047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct { mDNSu8 c[ 64]; } domainlabel;		// One label: length byte and up to 63 characters
37147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
37247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// RFC 1034/1035/2181 specify that a domain name (length bytes and data bytes) may be up to 255 bytes long,
37347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// plus the terminating zero at the end makes 256 bytes total in the on-the-wire format.
37447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define MAX_DOMAIN_NAME 256
37547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct { mDNSu8 c[256]; } domainname;		// Up to 256 bytes of length-prefixed domainlabels
37647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
37747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct { mDNSu8 c[256]; } UTF8str255;		// Null-terminated C string
37847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
37947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// The longest legal textual form of a DNS name is 1009 bytes, including the C-string terminating NULL at the end.
38047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Explanation:
38147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// When a native domainname object is converted to printable textual form using ConvertDomainNameToCString(),
38247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// non-printing characters are represented in the conventional DNS way, as '\ddd', where ddd is a three-digit decimal number.
38347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// The longest legal domain name is 256 bytes, in the form of four labels as shown below:
38447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Length byte, 63 data bytes, length byte, 63 data bytes, length byte, 63 data bytes, length byte, 62 data bytes, zero byte.
38547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Each label is encoded textually as characters followed by a trailing dot.
38647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// If every character has to be represented as a four-byte escape sequence, then this makes the maximum textual form four labels
38747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// plus the C-string terminating NULL as shown below:
38847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 63*4+1 + 63*4+1 + 63*4+1 + 62*4+1 + 1 = 1009.
38947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Note that MAX_ESCAPED_DOMAIN_LABEL is not normally used: If you're only decoding a single label, escaping is usually not required.
39047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// It is for domain names, where dots are used as label separators, that proper escaping is vital.
39147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define MAX_ESCAPED_DOMAIN_LABEL 254
39247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define MAX_ESCAPED_DOMAIN_NAME 1009
39347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
39447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// MAX_REVERSE_MAPPING_NAME
39547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// For IPv4: "123.123.123.123.in-addr.arpa."  30 bytes including terminating NUL
39647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// For IPv6: "x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.ip6.arpa."  74 bytes including terminating NUL
39747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
39847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define MAX_REVERSE_MAPPING_NAME_V4 30
39947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define MAX_REVERSE_MAPPING_NAME_V6 74
40047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define MAX_REVERSE_MAPPING_NAME    74
40147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
40247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Most records have a TTL of 75 minutes, so that their 80% cache-renewal query occurs once per hour.
40347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// For records containing a hostname (in the name on the left, or in the rdata on the right),
40447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// like A, AAAA, reverse-mapping PTR, and SRV, we use a two-minute TTL by default, because we don't want
40547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// them to hang around for too long in the cache if the host in question crashes or otherwise goes away.
40647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
40747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define kStandardTTL (3600UL * 100 / 80)
40847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define kHostNameTTL 120UL
40947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
41047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Some applications want to register their SRV records with a lower ttl so that in case the server
41147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// using a dynamic port number restarts, the clients will not have stale information for more than
41247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 10 seconds
41347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
41447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define kHostNameSmallTTL 10UL
41547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
41647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
41747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Multicast DNS uses announcements (gratuitous responses) to update peer caches.
41847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// This means it is feasible to use relatively larger TTL values than we might otherwise
41947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// use, because we have a cache coherency protocol to keep the peer caches up to date.
42047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// With Unicast DNS, once an authoritative server gives a record with a certain TTL value to a client
42147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// or caching server, that client or caching server is entitled to hold onto the record until its TTL
42247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// expires, and has no obligation to contact the authoritative server again until that time arrives.
42347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// This means that whereas Multicast DNS can use announcements to pre-emptively update stale data
42447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// before it would otherwise have expired, standard Unicast DNS (not using LLQs) has no equivalent
42547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mechanism, and TTL expiry is the *only* mechanism by which stale data gets deleted. Because of this,
42647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// we currently limit the TTL to ten seconds in such cases where no dynamic cache updating is possible.
42747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define kStaticCacheTTL 10
42847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
42947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define DefaultTTLforRRType(X) (((X) == kDNSType_A || (X) == kDNSType_AAAA || (X) == kDNSType_SRV) ? kHostNameTTL : kStandardTTL)
43047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
43147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct AuthRecord_struct AuthRecord;
43247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct ServiceRecordSet_struct ServiceRecordSet;
43347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct CacheRecord_struct CacheRecord;
43447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct CacheGroup_struct CacheGroup;
43547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct AuthGroup_struct AuthGroup;
43647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct DNSQuestion_struct DNSQuestion;
43747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct ZoneData_struct ZoneData;
43847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct mDNS_struct mDNS;
43947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct mDNS_PlatformSupport_struct mDNS_PlatformSupport;
44047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct NATTraversalInfo_struct NATTraversalInfo;
44147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
44247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Structure to abstract away the differences between TCP/SSL sockets, and one for UDP sockets
44347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// The actual definition of these structures appear in the appropriate platform support code
44447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct TCPSocket_struct TCPSocket;
44547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct UDPSocket_struct UDPSocket;
44647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
44747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ***************************************************************************
44847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if 0
44947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark -
45047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark - DNS Message structures
45147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
45247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
45347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNS_numZones   numQuestions
45447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNS_numPrereqs numAnswers
45547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNS_numUpdates numAuthorities
45647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
45747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef packedstruct
45847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
45947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSOpaque16 id;
46047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSOpaque16 flags;
46147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16 numQuestions;
46247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16 numAnswers;
46347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16 numAuthorities;
46447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16 numAdditionals;
46547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} DNSMessageHeader;
46647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
46747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// We can send and receive packets up to 9000 bytes (Ethernet Jumbo Frame size, if that ever becomes widely used)
46847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// However, in the normal case we try to limit packets to 1500 bytes so that we don't get IP fragmentation on standard Ethernet
46947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 40 (IPv6 header) + 8 (UDP header) + 12 (DNS message header) + 1440 (DNS message body) = 1500 total
47047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define AbsoluteMaxDNSMessageData 8940
47147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define NormalMaxDNSMessageData 1440
47247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef packedstruct
47347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
47447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSMessageHeader h;						// Note: Size 12 bytes
47547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8 data[AbsoluteMaxDNSMessageData];	// 40 (IPv6) + 8 (UDP) + 12 (DNS header) + 8940 (data) = 9000
47647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} DNSMessage;
47747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
47847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct tcpInfo_t
47947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
48047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNS             *m;
48147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	TCPSocket        *sock;
48247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSMessage        request;
48347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	int               requestLen;
48447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion      *question;   // For queries
48547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord       *rr;         // For record updates
48647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSAddr          Addr;
48747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort        Port;
48847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort        SrcPort;
48947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSMessage       *reply;
49047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16           replylen;
49147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	unsigned long     nread;
49247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	int               numReplies;
49347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} tcpInfo_t;
49447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
49547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ***************************************************************************
49647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if 0
49747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark -
49847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark - Other Packet Format Structures
49947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
50047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
50147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef packedstruct
50247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
50347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSEthAddr  dst;
50447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSEthAddr  src;
50547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSOpaque16 ethertype;
50647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} EthernetHeader;		// 14 bytes
50747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
50847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef packedstruct
50947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
51047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSOpaque16 hrd;
51147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSOpaque16 pro;
51247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8       hln;
51347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8       pln;
51447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSOpaque16 op;
51547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSEthAddr  sha;
51647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv4Addr   spa;
51747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSEthAddr  tha;
51847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv4Addr   tpa;
51947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} ARP_EthIP;			// 28 bytes
52047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
52147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef packedstruct
52247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
52347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8       vlen;
52447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8       tos;
52547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16      totlen;
52647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSOpaque16 id;
52747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSOpaque16 flagsfrags;
52847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8       ttl;
52947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8       protocol;	// Payload type: 0x06 = TCP, 0x11 = UDP
53047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16      checksum;
53147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv4Addr   src;
53247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv4Addr   dst;
53347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} IPv4Header;			// 20 bytes
53447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
53547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef packedstruct
53647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
53747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32      vcf;		// Version, Traffic Class, Flow Label
53847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16      len;		// Payload Length
53947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8       pro;		// Type of next header: 0x06 = TCP, 0x11 = UDP, 0x3A = ICMPv6
54047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8       ttl;		// Hop Limit
54147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv6Addr   src;
54247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv6Addr   dst;
54347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} IPv6Header;			// 40 bytes
54447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
54547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef packedstruct
54647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
54747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv6Addr   src;
54847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv6Addr   dst;
54947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSOpaque32 len;
55047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSOpaque32 pro;
55147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} IPv6PseudoHeader;		// 40 bytes
55247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
55347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef union
55447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
55547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8       bytes[20];
55647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	ARP_EthIP    arp;
55747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	IPv4Header   v4;
55847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	IPv6Header   v6;
55947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} NetworkLayerPacket;
56047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
56147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef packedstruct
56247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
56347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort   src;
56447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort   dst;
56547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32      seq;
56647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32      ack;
56747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8       offset;
56847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8       flags;
56947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16      window;
57047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16      checksum;
57147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16      urgent;
57247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} TCPHeader;			// 20 bytes; IP protocol type 0x06
57347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
57447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef packedstruct
57547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
57647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort   src;
57747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort   dst;
57847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16      len;		// Length including UDP header (i.e. minimum value is 8 bytes)
57947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16      checksum;
58047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} UDPHeader;			// 8 bytes; IP protocol type 0x11
58147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
58247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef packedstruct
58347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
58447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8       type;		// 0x87 == Neighbor Solicitation, 0x88 == Neighbor Advertisement
58547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8       code;
58647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16      checksum;
58747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32      flags_res;	// R/S/O flags and reserved bits
58847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv6Addr   target;
58947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Typically 8 bytes of options are also present
59047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} IPv6NDP;				// 24 bytes or more; IP protocol type 0x3A
59147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
59247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define NDP_Sol 0x87
59347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define NDP_Adv 0x88
59447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
59547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define NDP_Router    0x80
59647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define NDP_Solicited 0x40
59747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define NDP_Override  0x20
59847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
59947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define NDP_SrcLL 1
60047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define NDP_TgtLL 2
60147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
60247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef union
60347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
60447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8       bytes[20];
60547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	TCPHeader    tcp;
60647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	UDPHeader    udp;
60747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	IPv6NDP      ndp;
60847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} TransportLayerPacket;
60947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
61047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef packedstruct
61147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
61247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSOpaque64 InitiatorCookie;
61347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSOpaque64 ResponderCookie;
61447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8       NextPayload;
61547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8       Version;
61647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8       ExchangeType;
61747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8       Flags;
61847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSOpaque32 MessageID;
61947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32      Length;
62047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} IKEHeader;			// 28 bytes
62147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
62247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ***************************************************************************
62347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if 0
62447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark -
62547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark - Resource Record structures
62647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
62747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
62847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Authoritative Resource Records:
62947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// There are four basic types: Shared, Advisory, Unique, Known Unique
63047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
63147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// * Shared Resource Records do not have to be unique
63247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// -- Shared Resource Records are used for DNS-SD service PTRs
63347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// -- It is okay for several hosts to have RRs with the same name but different RDATA
63447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// -- We use a random delay on responses to reduce collisions when all the hosts respond to the same query
63547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// -- These RRs typically have moderately high TTLs (e.g. one hour)
63647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// -- These records are announced on startup and topology changes for the benefit of passive listeners
63747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// -- These records send a goodbye packet when deregistering
63847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
63947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// * Advisory Resource Records are like Shared Resource Records, except they don't send a goodbye packet
64047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
64147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// * Unique Resource Records should be unique among hosts within any given mDNS scope
64247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// -- The majority of Resource Records are of this type
64347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// -- If two entities on the network have RRs with the same name but different RDATA, this is a conflict
64447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// -- Responses may be sent immediately, because only one host should be responding to any particular query
64547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// -- These RRs typically have low TTLs (e.g. a few minutes)
64647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// -- On startup and after topology changes, a host issues queries to verify uniqueness
64747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
64847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// * Known Unique Resource Records are treated like Unique Resource Records, except that mDNS does
64947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// not have to verify their uniqueness because this is already known by other means (e.g. the RR name
65047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// is derived from the host's IP or Ethernet address, which is already known to be a unique identifier).
65147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
65247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Summary of properties of different record types:
65347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Probe?    Does this record type send probes before announcing?
65447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Conflict? Does this record type react if we observe an apparent conflict?
65547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Goodbye?  Does this record type send a goodbye packet on departure?
65647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
65747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//               Probe? Conflict? Goodbye? Notes
65847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Unregistered                            Should not appear in any list (sanity check value)
65947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Shared         No      No       Yes     e.g. Service PTR record
66047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Deregistering  No      No       Yes     Shared record about to announce its departure and leave the list
66147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Advisory       No      No       No
66247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Unique         Yes     Yes      No      Record intended to be unique -- will probe to verify
66347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Verified       Yes     Yes      No      Record has completed probing, and is verified unique
66447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// KnownUnique    No      Yes      No      Record is assumed by other means to be unique
66547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
66647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Valid lifecycle of a record:
66747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Unregistered ->                   Shared      -> Deregistering -(goodbye)-> Unregistered
66847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Unregistered ->                   Advisory                               -> Unregistered
66947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Unregistered -> Unique -(probe)-> Verified                               -> Unregistered
67047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Unregistered ->                   KnownUnique                            -> Unregistered
67147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
67247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Each Authoritative kDNSRecordType has only one bit set. This makes it easy to quickly see if a record
67347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// is one of a particular set of types simply by performing the appropriate bitwise masking operation.
67447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
67547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Cache Resource Records (received from the network):
67647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// There are four basic types: Answer, Unique Answer, Additional, Unique Additional
67747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Bit 7 (the top bit) of kDNSRecordType is always set for Cache Resource Records; always clear for Authoritative Resource Records
67847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Bit 6 (value 0x40) is set for answer records; clear for authority/additional records
67947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Bit 5 (value 0x20) is set for records received with the kDNSClass_UniqueRRSet
68047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
68147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltenum
68247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
68347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSRecordTypeUnregistered     = 0x00,	// Not currently in any list
68447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSRecordTypeDeregistering    = 0x01,	// Shared record about to announce its departure and leave the list
68547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
68647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSRecordTypeUnique           = 0x02,	// Will become a kDNSRecordTypeVerified when probing is complete
68747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
68847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSRecordTypeAdvisory         = 0x04,	// Like Shared, but no goodbye packet
68947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSRecordTypeShared           = 0x08,	// Shared means record name does not have to be unique -- use random delay on responses
69047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
69147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSRecordTypeVerified         = 0x10,	// Unique means mDNS should check that name is unique (and then send immediate responses)
69247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSRecordTypeKnownUnique      = 0x20,	// Known Unique means mDNS can assume name is unique without checking
69347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	                                        // For Dynamic Update records, Known Unique means the record must already exist on the server.
69447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSRecordTypeUniqueMask       = (kDNSRecordTypeUnique | kDNSRecordTypeVerified | kDNSRecordTypeKnownUnique),
69547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSRecordTypeActiveSharedMask = (kDNSRecordTypeAdvisory         | kDNSRecordTypeShared),
69647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSRecordTypeActiveUniqueMask = (kDNSRecordTypeVerified         | kDNSRecordTypeKnownUnique),
69747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSRecordTypeActiveMask       = (kDNSRecordTypeActiveSharedMask | kDNSRecordTypeActiveUniqueMask),
69847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
69947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSRecordTypePacketAdd        = 0x80,	// Received in the Additional  Section of a DNS Response
70047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSRecordTypePacketAddUnique  = 0x90,	// Received in the Additional  Section of a DNS Response with kDNSClass_UniqueRRSet set
70147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSRecordTypePacketAuth       = 0xA0,	// Received in the Authorities Section of a DNS Response
70247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSRecordTypePacketAuthUnique = 0xB0,	// Received in the Authorities Section of a DNS Response with kDNSClass_UniqueRRSet set
70347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSRecordTypePacketAns        = 0xC0,	// Received in the Answer      Section of a DNS Response
70447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSRecordTypePacketAnsUnique  = 0xD0,	// Received in the Answer      Section of a DNS Response with kDNSClass_UniqueRRSet set
70547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
70647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSRecordTypePacketNegative   = 0xF0,	// Pseudo-RR generated to cache non-existence results like NXDomain
70747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
70847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kDNSRecordTypePacketUniqueMask = 0x10	// True for PacketAddUnique, PacketAnsUnique, PacketAuthUnique, kDNSRecordTypePacketNegative
70947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
71047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
71147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef packedstruct { mDNSu16 priority; mDNSu16 weight; mDNSIPPort port; domainname target;   } rdataSRV;
71247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef packedstruct { mDNSu16 preference;                                domainname exchange; } rdataMX;
71347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef packedstruct { domainname mbox; domainname txt;                                        } rdataRP;
71447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef packedstruct { mDNSu16 preference; domainname map822; domainname mapx400;              } rdataPX;
71547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
71647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef packedstruct
71747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
71847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname mname;
71947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname rname;
72047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32 serial;		// Modular counter; increases when zone changes
72147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32 refresh;	// Time in seconds that a slave waits after successful replication of the database before it attempts replication again
72247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32 retry;		// Time in seconds that a slave waits after an unsuccessful replication attempt before it attempts replication again
72347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32 expire;		// Time in seconds that a slave holds on to old data while replication attempts remain unsuccessful
72447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32 min;		// Nominally the minimum record TTL for this zone, in seconds; also used for negative caching.
72547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} rdataSOA;
72647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
72747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// EDNS Option Code registrations are recorded in the "DNS EDNS0 Options" section of
72847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// <http://www.iana.org/assignments/dns-parameters>
72947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
73047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define kDNSOpt_LLQ   1
73147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define kDNSOpt_Lease 2
73247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define kDNSOpt_NSID  3
73347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define kDNSOpt_Owner 4
73447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
73547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct
73647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
73747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16      vers;
73847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16      llqOp;
73947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16      err;	// Or UDP reply port, in setup request
74047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Note: In the in-memory form, there's typically a two-byte space here, so that the following 64-bit id is word-aligned
74147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSOpaque64 id;
74247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32      llqlease;
74347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} LLQOptData;
74447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
74547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct
74647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
74747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8       vers;		// Version number of this Owner OPT record
74847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs8       seq;		// Sleep/wake epoch
74947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSEthAddr  HMAC;		// Host's primary identifier (e.g. MAC of on-board Ethernet)
75047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSEthAddr  IMAC;		// Interface's MAC address (if different to primary MAC)
75147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSOpaque48 password;	// Optional password
75247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} OwnerOptData;
75347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
75447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Note: rdataOPT format may be repeated an arbitrary number of times in a single resource record
75547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef packedstruct
75647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
75747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16 opt;
75847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16 optlen;
75947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	union { LLQOptData llq; mDNSu32 updatelease; OwnerOptData owner; } u;
76047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} rdataOPT;
76147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
76247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Space needed to put OPT records into a packet:
76347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Header      11 bytes (name 1, type 2, class 2, TTL 4, length 2)
76447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// LLQ rdata   18 bytes (opt 2, len 2, vers 2, op 2, err 2, id 8, lease 4)
76547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Lease rdata  8 bytes (opt 2, len 2, lease 4)
76647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Owner rdata 12-24    (opt 2, len 2, owner 8-20)
76747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
76847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define DNSOpt_Header_Space                 11
76947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define DNSOpt_LLQData_Space               (4 + 2 + 2 + 2 + 8 + 4)
77047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define DNSOpt_LeaseData_Space             (4 + 4)
77147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define DNSOpt_OwnerData_ID_Space          (4 + 2 + 6)
77247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define DNSOpt_OwnerData_ID_Wake_Space     (4 + 2 + 6 + 6)
77347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define DNSOpt_OwnerData_ID_Wake_PW4_Space (4 + 2 + 6 + 6 + 4)
77447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define DNSOpt_OwnerData_ID_Wake_PW6_Space (4 + 2 + 6 + 6 + 6)
77547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
77647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define ValidOwnerLength(X) (	(X) == DNSOpt_OwnerData_ID_Space          - 4 || \
77747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt								(X) == DNSOpt_OwnerData_ID_Wake_Space     - 4 || \
77847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt								(X) == DNSOpt_OwnerData_ID_Wake_PW4_Space - 4 || \
77947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt								(X) == DNSOpt_OwnerData_ID_Wake_PW6_Space - 4    )
78047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
78147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define DNSOpt_Owner_Space(A,B) (mDNSSameEthAddress((A),(B)) ? DNSOpt_OwnerData_ID_Space : DNSOpt_OwnerData_ID_Wake_Space)
78247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
78347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define DNSOpt_Data_Space(O) (                                  \
78447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	(O)->opt == kDNSOpt_LLQ   ? DNSOpt_LLQData_Space   :        \
78547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	(O)->opt == kDNSOpt_Lease ? DNSOpt_LeaseData_Space :        \
78647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	(O)->opt == kDNSOpt_Owner ? DNSOpt_Owner_Space(&(O)->u.owner.HMAC, &(O)->u.owner.IMAC) : 0x10000)
78747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
78847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// A maximal NSEC record is:
78947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//   256 bytes domainname 'nextname'
79047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// + 256 * 34 = 8704 bytes of bitmap data
79147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// = 8960 bytes total
79247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// For now we only support NSEC records encoding DNS types 0-255 and ignore the nextname (we always set it to be the same as the rrname),
79347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// which gives us a fixed in-memory size of 32 bytes (256 bits)
79447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct
79547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
79647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8 bitmap[32];
79747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} rdataNSEC;
79847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
79947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// StandardAuthRDSize is 264 (256+8), which is large enough to hold a maximum-sized SRV record (6 + 256 bytes)
80047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// MaximumRDSize is 8K the absolute maximum we support (at least for now)
80147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define StandardAuthRDSize 264
80247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define MaximumRDSize 8192
80347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
80447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// InlineCacheRDSize is 68
80547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Records received from the network with rdata this size or less have their rdata stored right in the CacheRecord object
80647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Records received from the network with rdata larger than this have additional storage allocated for the rdata
80747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// A quick unscientific sample from a busy network at Apple with lots of machines revealed this:
80847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 1461 records in cache
80947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 292 were one-byte TXT records
81047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 136 were four-byte A records
81147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 184 were sixteen-byte AAAA records
81247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 780 were various PTR, TXT and SRV records from 12-64 bytes
81347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Only 69 records had rdata bigger than 64 bytes
81447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Note that since CacheRecord object and a CacheGroup object are allocated out of the same pool, it's sensible to
81547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// have them both be the same size. Making one smaller without making the other smaller won't actually save any memory.
81647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define InlineCacheRDSize 68
81747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
81847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// On 64-bit, the pointers in a CacheRecord are bigger, and that creates 8 bytes more space for the name in a CacheGroup
81947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if ENABLE_MULTI_PACKET_QUERY_SNOOPING
82047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	#if defined(_ILP64) || defined(__ILP64__) || defined(_LP64) || defined(__LP64__) || defined(_WIN64)
82147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	#define InlineCacheGroupNameSize 160
82247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	#else
82347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	#define InlineCacheGroupNameSize 148
82447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	#endif
82547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#else
82647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	#if defined(_ILP64) || defined(__ILP64__) || defined(_LP64) || defined(__LP64__) || defined(_WIN64)
82747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	#define InlineCacheGroupNameSize 144
82847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	#else
82947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	#define InlineCacheGroupNameSize 132
83047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	#endif
83147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
83247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
83347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// The RDataBody union defines the common rdata types that fit into our 264-byte limit
83447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef union
83547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
83647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8      data[StandardAuthRDSize];
83747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv4Addr  ipv4;		// For 'A' record
83847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname  name;		// For PTR, NS, CNAME, DNAME
83947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	UTF8str255  txt;
84047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	rdataMX     mx;
84147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv6Addr  ipv6;		// For 'AAAA' record
84247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	rdataSRV    srv;
84347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	rdataOPT    opt[2];		// For EDNS0 OPT record; RDataBody may contain multiple variable-length rdataOPT objects packed together
84447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	rdataNSEC   nsec;
84547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} RDataBody;
84647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
84747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// The RDataBody2 union is the same as above, except it includes fields for the larger types like soa, rp, px
84847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef union
84947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
85047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8      data[StandardAuthRDSize];
85147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv4Addr  ipv4;		// For 'A' record
85247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname  name;		// For PTR, NS, CNAME, DNAME
85347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	rdataSOA    soa;		// This is large; not included in the normal RDataBody definition
85447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	UTF8str255  txt;
85547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	rdataMX     mx;
85647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	rdataRP     rp;			// This is large; not included in the normal RDataBody definition
85747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	rdataPX     px;			// This is large; not included in the normal RDataBody definition
85847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv6Addr  ipv6;		// For 'AAAA' record
85947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	rdataSRV    srv;
86047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	rdataOPT    opt[2];		// For EDNS0 OPT record; RDataBody may contain multiple variable-length rdataOPT objects packed together
86147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	rdataNSEC   nsec;
86247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} RDataBody2;
86347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
86447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct
86547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
86647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16    MaxRDLength;	// Amount of storage allocated for rdata (usually sizeof(RDataBody))
86747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16    padding;		// So that RDataBody is aligned on 32-bit boundary
86847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	RDataBody  u;
86947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} RData;
87047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
87147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// sizeofRDataHeader should be 4 bytes
87247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define sizeofRDataHeader (sizeof(RData) - sizeof(RDataBody))
87347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
87447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// RData_small is a smaller version of the RData object, used for inline data storage embedded in a CacheRecord_struct
87547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct
87647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
87747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16    MaxRDLength;	// Storage allocated for data (may be greater than InlineCacheRDSize if additional storage follows this object)
87847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16    padding;		// So that data is aligned on 32-bit boundary
87947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8     data[InlineCacheRDSize];
88047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} RData_small;
88147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
88247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Note: Within an mDNSRecordCallback mDNS all API calls are legal except mDNS_Init(), mDNS_Exit(), mDNS_Execute()
88347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef void mDNSRecordCallback(mDNS *const m, AuthRecord *const rr, mStatus result);
88447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
88547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Note:
88647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Restrictions: An mDNSRecordUpdateCallback may not make any mDNS API calls.
88747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// The intent of this callback is to allow the client to free memory, if necessary.
88847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// The internal data structures of the mDNS code may not be in a state where mDNS API calls may be made safely.
88947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef void mDNSRecordUpdateCallback(mDNS *const m, AuthRecord *const rr, RData *OldRData, mDNSu16 OldRDLen);
89047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
89147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ***************************************************************************
89247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if 0
89347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark -
89447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark - NAT Traversal structures and constants
89547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
89647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
89747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define NATMAP_MAX_RETRY_INTERVAL    ((mDNSPlatformOneSecond * 60) * 15)    // Max retry interval is 15 minutes
89847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define NATMAP_MIN_RETRY_INTERVAL     (mDNSPlatformOneSecond * 2)           // Min retry interval is 2 seconds
89947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define NATMAP_INIT_RETRY             (mDNSPlatformOneSecond / 4)           // start at 250ms w/ exponential decay
90047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define NATMAP_DEFAULT_LEASE          (60 * 60 * 2)                         // 2 hour lease life in seconds
90147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define NATMAP_VERS 0
90247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
90347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef enum
90447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
90547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	NATOp_AddrRequest    = 0,
90647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	NATOp_MapUDP         = 1,
90747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	NATOp_MapTCP         = 2,
90847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
90947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	NATOp_AddrResponse   = 0x80 | 0,
91047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	NATOp_MapUDPResponse = 0x80 | 1,
91147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	NATOp_MapTCPResponse = 0x80 | 2,
91247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} NATOp_t;
91347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
91447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltenum
91547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
91647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	NATErr_None    = 0,
91747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	NATErr_Vers    = 1,
91847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	NATErr_Refused = 2,
91947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	NATErr_NetFail = 3,
92047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	NATErr_Res     = 4,
92147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	NATErr_Opcode  = 5
92247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
92347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
92447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef mDNSu16 NATErr_t;
92547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
92647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef packedstruct
92747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
92847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8 vers;
92947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8 opcode;
93047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} NATAddrRequest;
93147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
93247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef packedstruct
93347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
93447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8     vers;
93547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8     opcode;
93647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16    err;
93747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32    upseconds;		// Time since last NAT engine reboot, in seconds
93847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv4Addr ExtAddr;
93947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} NATAddrReply;
94047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
94147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef packedstruct
94247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
94347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8 vers;
94447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8 opcode;
94547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSOpaque16 unused;
94647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort intport;
94747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort extport;
94847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32    NATReq_lease;
94947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} NATPortMapRequest;
95047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
95147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef packedstruct
95247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
95347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8     vers;
95447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8     opcode;
95547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16    err;
95647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32    upseconds;		// Time since last NAT engine reboot, in seconds
95747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort intport;
95847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort extport;
95947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32    NATRep_lease;
96047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} NATPortMapReply;
96147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
96247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef enum
96347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
96447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	LNTDiscoveryOp      = 1,
96547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	LNTExternalAddrOp   = 2,
96647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	LNTPortMapOp        = 3,
96747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	LNTPortMapDeleteOp  = 4
96847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} LNTOp_t;
96947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
97047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define LNT_MAXBUFSIZE 8192
97147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct tcpLNTInfo_struct tcpLNTInfo;
97247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltstruct tcpLNTInfo_struct
97347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
97447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	tcpLNTInfo       *next;
97547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNS             *m;
97647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	NATTraversalInfo *parentNATInfo;	// pointer back to the parent NATTraversalInfo
97747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	TCPSocket        *sock;
97847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	LNTOp_t           op;				// operation performed using this connection
97947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSAddr          Address;			// router address
98047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort        Port;				// router port
98147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8           *Request;			// xml request to router
98247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	int               requestLen;
98347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8           *Reply;			// xml reply from router
98447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	int               replyLen;
98547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	unsigned long     nread;			// number of bytes read so far
98647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	int               retries;			// number of times we've tried to do this port mapping
98747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
98847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
98947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef void (*NATTraversalClientCallback)(mDNS *m, NATTraversalInfo *n);
99047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
99147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// if m->timenow <  ExpiryTime then we have an active mapping, and we'll renew halfway to expiry
99247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// if m->timenow >= ExpiryTime then our mapping has expired, and we're trying to create one
99347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
99447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltstruct NATTraversalInfo_struct
99547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
99647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Internal state fields. These are used internally by mDNSCore; the client layer needn't be concerned with them.
99747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	NATTraversalInfo           *next;
99847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
99947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32                     ExpiryTime;			// Time this mapping expires, or zero if no mapping
100047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32                     retryInterval;		// Current interval, between last packet we sent and the next one
100147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32                     retryPortMap;		// If Protocol is nonzero, time to send our next mapping packet
100247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus                     NewResult;			// New error code; will be copied to Result just prior to invoking callback
100347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
100447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#ifdef _LEGACY_NAT_TRAVERSAL_
100547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	tcpLNTInfo                  tcpInfo;			// Legacy NAT traversal (UPnP) TCP connection
100647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
100747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
100847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Result fields: When the callback is invoked these fields contain the answers the client is looking for
100947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// When the callback is invoked ExternalPort is *usually* set to be the same the same as RequestedPort, except:
101047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// (a) When we're behind a NAT gateway with port mapping disabled, ExternalPort is reported as zero to
101147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	//     indicate that we don't currently have a working mapping (but RequestedPort retains the external port
101247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	//     we'd like to get, the next time we meet an accomodating NAT gateway willing to give us one).
101347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// (b) When we have a routable non-RFC1918 address, we don't *need* a port mapping, so ExternalPort
101447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	//     is reported as the same as our InternalPort, since that is effectively our externally-visible port too.
101547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	//     Again, RequestedPort retains the external port we'd like to get the next time we find ourself behind a NAT gateway.
101647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// To improve stability of port mappings, RequestedPort is updated any time we get a successful
101747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// mapping response from the NAT-PMP or UPnP gateway. For example, if we ask for port 80, and
101847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// get assigned port 81, then thereafter we'll contine asking for port 81.
101947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSInterfaceID             InterfaceID;
102047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv4Addr                  ExternalAddress;	// Initially set to onesIPv4Addr, until first callback
102147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort                  ExternalPort;
102247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32                     Lifetime;
102347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus                     Result;
102447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
102547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Client API fields: The client must set up these fields *before* making any NAT traversal API calls
102647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8                      Protocol;			// NATOp_MapUDP or NATOp_MapTCP, or zero if just requesting the external IP address
102747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort                  IntPort;			// Client's internal port number (doesn't change)
102847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort                  RequestedPort;		// Requested external port; may be updated with actual value assigned by gateway
102947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32                     NATLease;			// Requested lifetime in seconds (doesn't change)
103047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	NATTraversalClientCallback  clientCallback;
103147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	void                       *clientContext;
103247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
103347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
103447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltenum
103547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
103647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSServer_Untested = 0,
103747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSServer_Passed   = 1,
103847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSServer_Failed   = 2,
103947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSServer_Disabled = 3
104047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
104147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
104247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltenum
104347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
104447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSServer_FlagDelete = 1,
104547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSServer_FlagNew    = 2
104647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
104747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
104847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltenum
104947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
105047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	McastResolver_FlagDelete = 1,
105147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	McastResolver_FlagNew    = 2
105247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
105347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
105447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct McastResolver
105547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
105647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	struct McastResolver *next;
105747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSInterfaceID interface;
105847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32         flags;		// Set when we're planning to delete this from the list
105947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname      domain;
106047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32         timeout;	// timeout value for questions
106147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} McastResolver;
106247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
106347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct DNSServer
106447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
106547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	struct DNSServer *next;
106647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSInterfaceID interface;	// For specialized uses; we can have DNS servers reachable over specific interfaces
106747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSAddr        addr;
106847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort      port;
106947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSOpaque16    testid;
107047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32         flags;		// Set when we're planning to delete this from the list
107147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32         teststate;	// Have we sent bug-detection query to this server?
107247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32         lasttest;	// Time we sent last bug-detection query to this server
107347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname      domain;		// name->server matching for "split dns"
107447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32			penaltyTime; // amount of time this server is penalized
107547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool		scoped;		// interface should be matched against question only
107647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt								// if scoped is set
107747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32			timeout;	// timeout value for questions
107847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} DNSServer;
107947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
108047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct							// Size is 36 bytes when compiling for 32-bit; 48 when compiling for 64-bit
108147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
108247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8           RecordType;		// See enum above
108347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16          rrtype;
108447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16          rrclass;
108547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32          rroriginalttl;		// In seconds
108647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16          rdlength;			// Size of the raw rdata, in bytes, in the on-the-wire format
108747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt										// (In-memory storage may be larger, for structures containing 'holes', like SOA,
108847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt										// or smaller, for NSEC where we don't bother storing the nextname field)
108947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16          rdestimate;		// Upper bound on on-the-wire size of rdata after name compression
109047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32          namehash;			// Name-based (i.e. case-insensitive) hash of name
109147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32          rdatahash;			// For rdata containing domain name (e.g. PTR, SRV, CNAME etc.), case-insensitive name hash
109247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt										// else, for all other rdata, 32-bit hash of the raw rdata
109347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt										// Note: This requirement is important. Various routines like AddAdditionalsToResponseList(),
109447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt										// ReconfirmAntecedents(), etc., use rdatahash as a pre-flight check to see
109547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt										// whether it's worth doing a full SameDomainName() call. If the rdatahash
109647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt										// is not a correct case-insensitive name hash, they'll get false negatives.
109747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
109847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Grouping pointers together at the end of the structure improves the memory layout efficiency
109947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSInterfaceID  InterfaceID;		// Set if this RR is specific to one interface
110047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt										// For records received off the wire, InterfaceID is *always* set to the receiving interface
110147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt										// For our authoritative records, InterfaceID is usually zero, except for those few records
110247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt										// that are interface-specific (e.g. address records, especially linklocal addresses)
110347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	const domainname *name;
110447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	RData           *rdata;				// Pointer to storage for this rdata
110547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSServer       *rDNSServer;		// Unicast DNS server authoritative for this entry;null for multicast
110647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} ResourceRecord;
110747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
110847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Unless otherwise noted, states may apply to either independent record registrations or service registrations
110947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef enum
111047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
111147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	regState_Zero              = 0,
111247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	regState_Pending           = 1,     // update sent, reply not received
111347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	regState_Registered        = 2,     // update sent, reply received
111447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	regState_DeregPending      = 3,     // dereg sent, reply not received
111547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	regState_Unregistered      = 4,     // not in any list
111647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	regState_Refresh           = 5,     // outstanding refresh (or target change) message
111747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	regState_NATMap            = 6,     // establishing NAT port mapping
111847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	regState_UpdatePending     = 7,     // update in flight as result of mDNS_Update call
111947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	regState_NoTarget          = 8,     // SRV Record registration pending registration of hostname
112047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	regState_NATError          = 9     // unable to complete NAT traversal
112147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} regState_t;
112247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
112347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltenum
112447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
112547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	Target_Manual = 0,
112647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	Target_AutoHost = 1,
112747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	Target_AutoHostAndNATMAP = 2
112847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
112947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
113047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef enum
113147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
113247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mergeState_Zero = 0,
113347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mergeState_DontMerge = 1  // Set on fatal error conditions to disable merging
113447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} mergeState_t;
113547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
113647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltstruct AuthGroup_struct				// Header object for a list of AuthRecords with the same name
113747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
113847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthGroup      *next;				// Next AuthGroup object in this hash table bucket
113947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32         namehash;			// Name-based (i.e. case insensitive) hash of name
114047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord     *members;			// List of CacheRecords with this same name
114147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord    **rrauth_tail;		// Tail end of that list
114247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname     *name;				// Common name for all AuthRecords in this list
114347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord     *NewLocalOnlyRecords;
114447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Size to here is 20 bytes when compiling 32-bit; 40 bytes when compiling 64-bit
114547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8          namestorage[InlineCacheGroupNameSize];
114647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
114747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
114847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define AUTH_HASH_SLOTS 499
114947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define FORALL_AUTHRECORDS(SLOT,AG,AR)                           	\
115047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	for ((SLOT) = 0; (SLOT) < AUTH_HASH_SLOTS; (SLOT)++)         	\
115147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		for ((AG)=m->rrauth.rrauth_hash[(SLOT)]; (AG); (AG)=(AG)->next) \
115247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt			for ((AR) = (AG)->members; (AR); (AR)=(AR)->next)
115347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
115447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef union AuthEntity_union AuthEntity;
115547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltunion AuthEntity_union { AuthEntity *next; AuthGroup ag; };
115647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct {
115747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32 rrauth_size;				// Total number of available auth entries
115847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32 rrauth_totalused;			// Number of auth entries currently occupied
115947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32 rrauth_report;
116047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8  rrauth_lock;				// For debugging: Set at times when these lists may not be modified
116147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthEntity *rrauth_free;
116247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthGroup *rrauth_hash[AUTH_HASH_SLOTS];
116347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt}AuthHash;
116447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
116547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// AuthRecordAny includes mDNSInterface_Any and interface specific auth records (anything
116647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// other than P2P or LocalOnly)
116747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef enum
116847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
116947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecordAny, 				// registered for *Any, NOT including P2P interfaces
117047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecordAnyIncludeP2P, 	// registered for *Any, including P2P interfaces
117147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecordLocalOnly,
117247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecordP2P				// discovered over D2D/P2P framework
117347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} AuthRecType;
117447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
117547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltstruct AuthRecord_struct
117647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
117747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// For examples of how to set up this structure for use in mDNS_Register(),
117847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// see mDNS_AdvertiseInterface() or mDNS_RegisterService().
117947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Basically, resrec and persistent metadata need to be set up before calling mDNS_Register().
118047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// mDNS_SetupResourceRecord() is avaliable as a helper routine to set up most fields to sensible default values for you
118147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
118247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord     *next;				// Next in list; first element of structure for efficiency reasons
118347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Field Group 1: Common ResourceRecord fields
118447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	ResourceRecord  resrec;				// 36 bytes when compiling for 32-bit; 48 when compiling for 64-bit
118547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
118647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Field Group 2: Persistent metadata for Authoritative Records
118747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord     *Additional1;		// Recommended additional record to include in response (e.g. SRV for PTR record)
118847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord     *Additional2;		// Another additional (e.g. TXT for PTR record)
118947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord     *DependentOn;		// This record depends on another for its uniqueness checking
119047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord     *RRSet;				// This unique record is part of an RRSet
119147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSRecordCallback *RecordCallback;	// Callback function to call for state changes, and to free memory asynchronously on deregistration
119247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	void           *RecordContext;		// Context parameter for the callback function
119347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8          AutoTarget;			// Set if the target of this record (PTR, CNAME, SRV, etc.) is our host name
119447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8          AllowRemoteQuery;	// Set if we allow hosts not on the local link to query this record
119547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8          ForceMCast;			// Set by client to advertise solely via multicast, even for apparently unicast names
119647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
119747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	OwnerOptData    WakeUp;				// WakeUp.HMAC.l[0] nonzero indicates that this is a Sleep Proxy record
119847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSAddr        AddressProxy;		// For reverse-mapping Sleep Proxy PTR records, address in question
119947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32         TimeRcvd;			// In platform time units
120047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32         TimeExpire;			// In platform time units
120147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecType     ARType;             // LocalOnly, P2P or Normal ?
120247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
120347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Field Group 3: Transient state for Authoritative Records
120447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8          Acknowledged;		// Set if we've given the success callback to the client
120547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8          ProbeCount;			// Number of probes remaining before this record is valid (kDNSRecordTypeUnique)
120647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8          AnnounceCount;		// Number of announcements remaining (kDNSRecordTypeShared)
120747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8          RequireGoodbye;		// Set if this RR has been announced on the wire and will require a goodbye packet
120847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8          AnsweredLocalQ;		// Set if this AuthRecord has been delivered to any local question (LocalOnly or mDNSInterface_Any)
120947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8          IncludeInProbe;		// Set if this RR is being put into a probe right now
121047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8          ImmedUnicast;		// Set if we may send our response directly via unicast to the requester
121147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSInterfaceID SendNSECNow;		// Set if we need to generate associated NSEC data for this rrname
121247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSInterfaceID ImmedAnswer;		// Someone on this interface issued a query we need to answer (all-ones for all interfaces)
121347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if MDNS_LOG_ANSWER_SUPPRESSION_TIMES
121447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32         ImmedAnswerMarkTime;
121547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
121647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSInterfaceID ImmedAdditional;	// Hint that we might want to also send this record, just to be helpful
121747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSInterfaceID SendRNow;			// The interface this query is being sent on right now
121847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv4Addr      v4Requester;		// Recent v4 query for this record, or all-ones if more than one recent query
121947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv6Addr      v6Requester;		// Recent v6 query for this record, or all-ones if more than one recent query
122047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord     *NextResponse;		// Link to the next element in the chain of responses to generate
122147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	const mDNSu8   *NR_AnswerTo;		// Set if this record was selected by virtue of being a direct answer to a question
122247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord     *NR_AdditionalTo;	// Set if this record was selected by virtue of being additional to another
122347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32         ThisAPInterval;		// In platform time units: Current interval for announce/probe
122447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32         LastAPTime;			// In platform time units: Last time we sent announcement/probe
122547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32         LastMCTime;			// Last time we multicast this record (used to guard against packet-storm attacks)
122647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSInterfaceID LastMCInterface;	// Interface this record was multicast on at the time LastMCTime was recorded
122747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	RData          *NewRData;			// Set if we are updating this record with new rdata
122847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16         newrdlength;		// ... and the length of the new RData
122947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSRecordUpdateCallback *UpdateCallback;
123047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32         UpdateCredits;		// Token-bucket rate limiting of excessive updates
123147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32         NextUpdateCredit;	// Time next token is added to bucket
123247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32         UpdateBlocked;		// Set if update delaying is in effect
123347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
123447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Field Group 4: Transient uDNS state for Authoritative Records
123547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	regState_t   state;			// Maybe combine this with resrec.RecordType state? Right now it's ambiguous and confusing.
123647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt								// e.g. rr->resrec.RecordType can be kDNSRecordTypeUnregistered,
123747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt								// and rr->state can be regState_Unregistered
123847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt								// What if we find one of those statements is true and the other false? What does that mean?
123947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool     uselease;		// dynamic update contains (should contain) lease option
124047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32      expire;		// In platform time units: expiration of lease (-1 for static)
124147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool     Private;		// If zone is private, DNS updates may have to be encrypted to prevent eavesdropping
124247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSOpaque16 updateid;		// Identifier to match update request and response -- also used when transferring records to Sleep Proxy
124347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	const domainname *zone;		// the zone that is updated
124447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	ZoneData  *nta;
124547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	struct tcpInfo_t *tcp;
124647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	NATTraversalInfo  NATinfo;
124747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool SRVChanged;       // temporarily deregistered service because its SRV target or port changed
124847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mergeState_t  mState;      // Unicast Record Registrations merge state
124947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8		  refreshCount; // Number of refreshes to the server
125047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus		  updateError;  // Record update resulted in Error ?
125147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
125247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// uDNS_UpdateRecord support fields
125347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Do we really need all these in *addition* to NewRData and newrdlength above?
125447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	void *UpdateContext;	// Context parameter for the update callback function
125547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16 OrigRDLen;		// previously registered, being deleted
125647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16 InFlightRDLen;	// currently being registered
125747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16 QueuedRDLen;	// pending operation (re-transmitting if necessary) THEN register the queued update
125847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	RData *OrigRData;
125947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	RData *InFlightRData;
126047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	RData *QueuedRData;
126147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
126247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Field Group 5: Large data objects go at the end
126347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname      namestorage;
126447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	RData           rdatastorage;		// Normally the storage is right here, except for oversized records
126547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// rdatastorage MUST be the last thing in the structure -- when using oversized AuthRecords, extra bytes
126647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// are appended after the end of the AuthRecord, logically augmenting the size of the rdatastorage
126747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// DO NOT ADD ANY MORE FIELDS HERE
126847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
126947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
127047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// IsLocalDomain alone is not sufficient to determine that a record is mDNS or uDNS. By default domain names within
127147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// the "local" pseudo-TLD (and within the IPv4 and IPv6 link-local reverse mapping domains) are automatically treated
127247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// as mDNS records, but it is also possible to force any record (even those not within one of the inherently local
127347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// domains) to be handled as an mDNS record by setting the ForceMCast flag, or by setting a non-zero InterfaceID.
127447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// For example, the reverse-mapping PTR record created in AdvertiseInterface sets the ForceMCast flag, since it points to
127547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// a dot-local hostname, and therefore it would make no sense to register this record with a wide-area Unicast DNS server.
127647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// The same applies to Sleep Proxy records, which we will answer for when queried via mDNS, but we never want to try
127747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// to register them with a wide-area Unicast DNS server -- and we probably don't have the required credentials anyway.
127847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Currently we have no concept of a wide-area uDNS record scoped to a particular interface, so if the InterfaceID is
127947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// nonzero we treat this the same as ForceMCast.
128047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Note: Question_uDNS(Q) is used in *only* one place -- on entry to mDNS_StartQuery_internal, to decide whether to set TargetQID.
128147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Everywhere else in the code, the determination of whether a question is unicast is made by checking to see if TargetQID is nonzero.
128247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define AuthRecord_uDNS(R) ((R)->resrec.InterfaceID == mDNSInterface_Any && !(R)->ForceMCast && !IsLocalDomain((R)->resrec.name))
128347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define Question_uDNS(Q)   ((Q)->InterfaceID == mDNSInterface_Unicast || \
128447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	((Q)->InterfaceID != mDNSInterface_LocalOnly && (Q)->InterfaceID != mDNSInterface_P2P && !(Q)->ForceMCast && !IsLocalDomain(&(Q)->qname)))
128547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
128647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define RRLocalOnly(rr) ((rr)->ARType == AuthRecordLocalOnly || (rr)->ARType == AuthRecordP2P)
128747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
128847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define RRAny(rr) ((rr)->ARType == AuthRecordAny || (rr)->ARType == AuthRecordAnyIncludeP2P)
128947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
129047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Question (A or AAAA) that is suppressed currently because IPv4 or IPv6 address
129147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// is not available locally for A or AAAA question respectively
129247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define QuerySuppressed(Q) ((Q)->SuppressUnusable && (Q)->SuppressQuery)
129347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
129447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define PrivateQuery(Q) ((Q)->AuthInfo && (Q)->AuthInfo->AutoTunnel)
129547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
129647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Normally we always lookup the cache and /etc/hosts before sending the query on the wire. For single label
129747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// queries (A and AAAA) that are unqualified (indicated by AppendSearchDomains), we want to append search
129847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// domains before we try them as such
129947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define ApplySearchDomainsFirst(q) ((q)->AppendSearchDomains && (CountLabels(&((q)->qname))) == 1)
130047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
130147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Wrapper struct for Auth Records for higher-level code that cannot use the AuthRecord's ->next pointer field
130247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct ARListElem
130347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
130447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	struct ARListElem *next;
130547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord ar;          // Note: Must be last element of structure, to accomodate oversized AuthRecords
130647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} ARListElem;
130747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
130847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltstruct CacheGroup_struct				// Header object for a list of CacheRecords with the same name
130947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
131047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	CacheGroup     *next;				// Next CacheGroup object in this hash table bucket
131147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32         namehash;			// Name-based (i.e. case insensitive) hash of name
131247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	CacheRecord    *members;			// List of CacheRecords with this same name
131347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	CacheRecord   **rrcache_tail;		// Tail end of that list
131447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname     *name;				// Common name for all CacheRecords in this list
131547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Size to here is 20 bytes when compiling 32-bit; 40 bytes when compiling 64-bit
131647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8          namestorage[InlineCacheGroupNameSize];
131747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
131847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
131947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
132047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltstruct CacheRecord_struct
132147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
132247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	CacheRecord    *next;				// Next in list; first element of structure for efficiency reasons
132347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	ResourceRecord  resrec;				// 36 bytes when compiling for 32-bit; 48 when compiling for 64-bit
132447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
132547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Transient state for Cache Records
132647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	CacheRecord    *NextInKAList;		// Link to the next element in the chain of known answers to send
132747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32         TimeRcvd;			// In platform time units
132847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32         DelayDelivery;		// Set if we want to defer delivery of this answer to local clients
132947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32         NextRequiredQuery;	// In platform time units
133047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32         LastUsed;			// In platform time units
133147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion    *CRActiveQuestion;	// Points to an active question referencing this answer. Can never point to a NewQuestion.
133247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32         UnansweredQueries;	// Number of times we've issued a query for this record without getting an answer
133347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32         LastUnansweredTime;	// In platform time units; last time we incremented UnansweredQueries
133447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if ENABLE_MULTI_PACKET_QUERY_SNOOPING
133547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32         MPUnansweredQ;		// Multi-packet query handling: Number of times we've seen a query for this record
133647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32         MPLastUnansweredQT;	// Multi-packet query handling: Last time we incremented MPUnansweredQ
133747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32         MPUnansweredKA;		// Multi-packet query handling: Number of times we've seen this record in a KA list
133847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool        MPExpectingKA;		// Multi-packet query handling: Set when we increment MPUnansweredQ; allows one KA
133947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
134047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	CacheRecord    *NextInCFList;		// Set if this is in the list of records we just received with the cache flush bit set
134147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Size to here is 76 bytes when compiling 32-bit; 104 bytes when compiling 64-bit
134247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	RData_small     smallrdatastorage;	// Storage for small records is right here (4 bytes header + 68 bytes data = 72 bytes)
134347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
134447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
134547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Storage sufficient to hold either a CacheGroup header or a CacheRecord
134647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// -- for best efficiency (to avoid wasted unused storage) they should be the same size
134747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef union CacheEntity_union CacheEntity;
134847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltunion CacheEntity_union { CacheEntity *next; CacheGroup cg; CacheRecord cr; };
134947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
135047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct
135147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
135247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	CacheRecord r;
135347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8 _extradata[MaximumRDSize-InlineCacheRDSize];		// Glue on the necessary number of extra bytes
135447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname namestorage;									// Needs to go *after* the extra rdata bytes
135547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} LargeCacheRecord;
135647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
135747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct HostnameInfo
135847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
135947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	struct HostnameInfo *next;
136047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	NATTraversalInfo natinfo;
136147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname fqdn;
136247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord arv4;                          // registered IPv4 address record
136347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord arv6;                          // registered IPv6 address record
136447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSRecordCallback *StatusCallback;       // callback to deliver success or error code to client layer
136547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	const void *StatusContext;                // Client Context
136647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} HostnameInfo;
136747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
136847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct ExtraResourceRecord_struct ExtraResourceRecord;
136947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltstruct ExtraResourceRecord_struct
137047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
137147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	ExtraResourceRecord *next;
137247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32 ClientID;  // Opaque ID field to be used by client to map an AddRecord call to a set of Extra records
137347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord r;
137447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Note: Add any additional fields *before* the AuthRecord in this structure, not at the end.
137547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// In some cases clients can allocate larger chunks of memory and set r->rdata->MaxRDLength to indicate
137647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// that this extra memory is available, which would result in any fields after the AuthRecord getting smashed
137747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
137847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
137947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Note: Within an mDNSServiceCallback mDNS all API calls are legal except mDNS_Init(), mDNS_Exit(), mDNS_Execute()
138047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef void mDNSServiceCallback(mDNS *const m, ServiceRecordSet *const sr, mStatus result);
138147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
138247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// A ServiceRecordSet has no special meaning to the core code of the Multicast DNS protocol engine;
138347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// it is just a convenience structure to group together the records that make up a standard service
138447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// registration so that they can be allocted and deallocted together as a single memory object.
138547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// It contains its own ServiceCallback+ServiceContext to report aggregate results up to the next layer of software above.
138647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// It also contains:
138747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//  * the basic PTR/SRV/TXT triplet used to represent any DNS-SD service
138847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//  * the "_services" PTR record for service enumeration
138947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//  * the optional list of SubType PTR records
139047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//  * the optional list of additional records attached to the service set (e.g. iChat pictures)
139147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
139247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltstruct ServiceRecordSet_struct
139347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
139447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// These internal state fields are used internally by mDNSCore; the client layer needn't be concerned with them.
139547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// No fields need to be set up by the client prior to calling mDNS_RegisterService();
139647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// all required data is passed as parameters to that function.
139747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSServiceCallback *ServiceCallback;
139847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	void                *ServiceContext;
139947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool             Conflict;	// Set if this record set was forcibly deregistered because of a conflict
140047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
140147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	ExtraResourceRecord *Extras;	// Optional list of extra AuthRecords attached to this service registration
140247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32              NumSubTypes;
140347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord          *SubTypes;
140447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord           RR_ADV;	// e.g. _services._dns-sd._udp.local. PTR _printer._tcp.local.
140547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord           RR_PTR;	// e.g. _printer._tcp.local.        PTR Name._printer._tcp.local.
140647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord           RR_SRV;	// e.g. Name._printer._tcp.local.   SRV 0 0 port target
140747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord           RR_TXT;	// e.g. Name._printer._tcp.local.   TXT PrintQueueName
140847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Don't add any fields after AuthRecord RR_TXT.
140947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// This is where the implicit extra space goes if we allocate a ServiceRecordSet containing an oversized RR_TXT record
141047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
141147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
141247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ***************************************************************************
141347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if 0
141447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark -
141547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark - Question structures
141647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
141747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
141847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// We record the last eight instances of each duplicate query
141947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// This gives us v4/v6 on each of Ethernet, AirPort and Firewire, and two free slots "for future expansion"
142047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// If the host has more active interfaces that this it is not fatal -- duplicate question suppression will degrade gracefully.
142147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Since we will still remember the last eight, the busiest interfaces will still get the effective duplicate question suppression.
142247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define DupSuppressInfoSize 8
142347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
142447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct
142547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
142647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32               Time;
142747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSInterfaceID       InterfaceID;
142847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32               Type;				// v4 or v6?
142947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} DupSuppressInfo;
143047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
143147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef enum
143247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
143347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	LLQ_InitialRequest    = 1,
143447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	LLQ_SecondaryRequest  = 2,
143547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	LLQ_Established       = 3,
143647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	LLQ_Poll              = 4
143747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} LLQ_State;
143847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
143947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// LLQ constants
144047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define kLLQ_Vers      1
144147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define kLLQ_DefLease  7200 // 2 hours
144247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define kLLQ_MAX_TRIES 3    // retry an operation 3 times max
144347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define kLLQ_INIT_RESEND 2 // resend an un-ack'd packet after 2 seconds, then double for each additional
144447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// LLQ Operation Codes
144547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define kLLQOp_Setup     1
144647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define kLLQOp_Refresh   2
144747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define kLLQOp_Event     3
144847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
144947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// LLQ Errror Codes
145047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltenum
145147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
145247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	LLQErr_NoError    = 0,
145347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	LLQErr_ServFull   = 1,
145447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	LLQErr_Static     = 2,
145547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	LLQErr_FormErr    = 3,
145647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	LLQErr_NoSuchLLQ  = 4,
145747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	LLQErr_BadVers    = 5,
145847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	LLQErr_UnknownErr = 6
145947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
146047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
146147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltenum { NoAnswer_Normal = 0, NoAnswer_Suspended = 1, NoAnswer_Fail = 2 };
146247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
146347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define HMAC_LEN    64
146447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define HMAC_IPAD   0x36
146547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define HMAC_OPAD   0x5c
146647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define MD5_LEN     16
146747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
146847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define AutoTunnelUnregistered(X) (                                              \
146947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	(X)->AutoTunnelHostRecord.resrec.RecordType == kDNSRecordTypeUnregistered && \
147047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	(X)->AutoTunnelDeviceInfo.resrec.RecordType == kDNSRecordTypeUnregistered && \
147147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	(X)->AutoTunnelService.   resrec.RecordType == kDNSRecordTypeUnregistered && \
147247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	(X)->AutoTunnel6Record.   resrec.RecordType == kDNSRecordTypeUnregistered    )
147347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
147447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Internal data structure to maintain authentication information
147547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct DomainAuthInfo
147647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
147747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	struct DomainAuthInfo *next;
147847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32          deltime;				// If we're planning to delete this DomainAuthInfo, the time we want it deleted
147947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	const char*      AutoTunnel;            // If NULL, this is not an AutoTunnel DAI. Otherwise, this is prepended to the IPSec identifier
148047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord       AutoTunnelHostRecord;	// User-visible hostname; used as SRV target for AutoTunnel services
148147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord       AutoTunnelTarget;		// Opaque hostname of tunnel endpoint; used as SRV target for AutoTunnelService record
148247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord       AutoTunnelDeviceInfo;	// Device info of tunnel endpoint
148347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord       AutoTunnelService;		// Service record (possibly NAT-Mapped) of IKE daemon implementing tunnel endpoint
148447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord       AutoTunnel6Record;     // AutoTunnel AAAA Record obtained from Connectivityd
148547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	NATTraversalInfo AutoTunnelNAT;
148647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname       domain;
148747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname       keyname;
148847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname       hostname;
148947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort       port;
149047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char             b64keydata[32];
149147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8           keydata_ipad[HMAC_LEN];	// padded key for inner hash rounds
149247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8           keydata_opad[HMAC_LEN];	// padded key for outer hash rounds
149347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} DomainAuthInfo;
149447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
149547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Note: Within an mDNSQuestionCallback mDNS all API calls are legal except mDNS_Init(), mDNS_Exit(), mDNS_Execute()
149647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef enum { QC_rmv = 0, QC_add = 1, QC_addnocache = 2 } QC_result;
149747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef void mDNSQuestionCallback(mDNS *const m, DNSQuestion *question, const ResourceRecord *const answer, QC_result AddRecord);
149847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
149947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define NextQSendTime(Q)  ((Q)->LastQTime + (Q)->ThisQInterval)
150047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define ActiveQuestion(Q) ((Q)->ThisQInterval > 0 && !(Q)->DuplicateOf)
150147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define TimeToSendThisQuestion(Q,time) (ActiveQuestion(Q) && (time) - NextQSendTime(Q) >= 0)
150247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
150347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltstruct DNSQuestion_struct
150447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
150547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Internal state fields. These are used internally by mDNSCore; the client layer needn't be concerned with them.
150647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion          *next;
150747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32               qnamehash;
150847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32               DelayAnswering;	// Set if we want to defer answering this question until the cache settles
150947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32               LastQTime;		// Last scheduled transmission of this Q on *all* applicable interfaces
151047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32               ThisQInterval;	// LastQTime + ThisQInterval is the next scheduled transmission of this Q
151147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt											// ThisQInterval > 0 for an active question;
151247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt											// ThisQInterval = 0 for a suspended question that's still in the list
151347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt											// ThisQInterval = -1 for a cancelled question (should not still be in list)
151447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32               ExpectUnicastResp;// Set when we send a query with the kDNSQClass_UnicastResponse bit set
151547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32               LastAnswerPktNum;	// The sequence number of the last response packet containing an answer to this Q
151647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32               RecentAnswerPkts;	// Number of answers since the last time we sent this query
151747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32               CurrentAnswers;	// Number of records currently in the cache that answer this question
151847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32               LargeAnswers;		// Number of answers with rdata > 1024 bytes
151947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32               UniqueAnswers;	// Number of answers received with kDNSClass_UniqueRRSet bit set
152047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSInterfaceID       FlappingInterface1;// Set when an interface goes away, to flag if remove events are delivered for this Q
152147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSInterfaceID       FlappingInterface2;// Set when an interface goes away, to flag if remove events are delivered for this Q
152247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DomainAuthInfo       *AuthInfo;			// Non-NULL if query is currently being done using Private DNS
152347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion          *DuplicateOf;
152447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion          *NextInDQList;
152547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DupSuppressInfo       DupSuppress[DupSuppressInfoSize];
152647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSInterfaceID       SendQNow;			// The interface this query is being sent on right now
152747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool              SendOnAll;		// Set if we're sending this question on all active interfaces
152847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32               RequestUnicast;	// Non-zero if we want to send query with kDNSQClass_UnicastResponse bit set
152947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32               LastQTxTime;		// Last time this Q was sent on one (but not necessarily all) interfaces
153047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32               CNAMEReferrals;	// Count of how many CNAME redirections we've done
153147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool              SuppressQuery;    // This query should be suppressed and not sent on the wire
153247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8                LOAddressAnswers; // Number of answers from the local only auth records that are
153347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		                                    // answering A, AAAA and CNAME (/etc/hosts)
153447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8                WakeOnResolveCount; // Number of wakes that should be sent on resolve
153547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32               StopTime;			// Time this question should be stopped by giving them a negative answer
153647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
153747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Wide Area fields. These are used internally by the uDNS core
153847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	UDPSocket            *LocalSocket;
153947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool             deliverAddEvents;  // Change in DNSSserver requiring to deliver ADD events
154047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSServer            *qDNSServer;		// Caching server for this query (in the absence of an SRV saying otherwise)
154147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSOpaque64          validDNSServers;  // Valid DNSServers for this question
154247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16              noServerResponse;  // At least one server did not respond.
154347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16              triedAllServersOnce; // Tried all DNS servers once
154447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8               unansweredQueries;// The number of unanswered queries to this server
154547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
154647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	ZoneData             *nta;				// Used for getting zone data for private or LLQ query
154747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSAddr              servAddr;			// Address and port learned from _dns-llq, _dns-llq-tls or _dns-query-tls SRV query
154847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort            servPort;
154947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	struct tcpInfo_t *tcp;
155047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort            tcpSrcPort;		// Local Port TCP packet received on;need this as tcp struct is disposed
155147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt											// by tcpCallback before calling into mDNSCoreReceive
155247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8                NoAnswer;			// Set if we want to suppress answers until tunnel setup has completed
155347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
155447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// LLQ-specific fields. These fields are only meaningful when LongLived flag is set
155547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	LLQ_State             state;
155647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32               ReqLease;			// seconds (relative)
155747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32               expire;			// ticks (absolute)
155847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs16               ntries;           // for UDP: the number of packets sent for this LLQ state
155947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	                                       // for TCP: there is some ambiguity in the use of this variable, but in general, it is
156047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	                                       //          the number of TCP/TLS connection attempts for this LLQ state, or
156147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	                                       //          the number of packets sent for this TCP/TLS connection
156247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSOpaque64          id;
156347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
156447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Client API fields: The client must set up these fields *before* calling mDNS_StartQuery()
156547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSInterfaceID       InterfaceID;		// Non-zero if you want to issue queries only on a single specific IP interface
156647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSAddr              Target;			// Non-zero if you want to direct queries to a specific unicast target address
156747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort            TargetPort;		// Must be set if Target is set
156847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSOpaque16          TargetQID;		// Must be set if Target is set
156947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname            qname;
157047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16               qtype;
157147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16               qclass;
157247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool              LongLived;        // Set by client for calls to mDNS_StartQuery to indicate LLQs to unicast layer.
157347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool              ExpectUnique;		// Set by client if it's expecting unique RR(s) for this question, not shared RRs
157447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool              ForceMCast;		// Set by client to force mDNS query, even for apparently uDNS names
157547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool              ReturnIntermed;	// Set by client to request callbacks for intermediate CNAME/NXDOMAIN results
157647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool              SuppressUnusable; // Set by client to suppress unusable queries to be sent on the wire
157747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool              RetryWithSearchDomains;	// Retry with search domains if there is no entry in the cache or AuthRecords
157847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8                TimeoutQuestion; // Timeout this question if there is no reply in configured time
157947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8                WakeOnResolve; // Send wakeup on resolve
158047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs8                SearchListIndex;  // Index into SearchList; Used by the client layer but not touched by core
158147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs8                AppendSearchDomains; // Search domains can be appended for this query
158247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs8                AppendLocalSearchDomains; // Search domains ending in .local can be appended for this query
158347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname           *qnameOrig;       // Copy of the original question name if it is not fully qualified
158447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSQuestionCallback *QuestionCallback;
158547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	void                 *QuestionContext;
158647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
158747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
158847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct
158947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
159047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Client API fields: The client must set up name and InterfaceID *before* calling mDNS_StartResolveService()
159147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// When the callback is invoked, ip, port, TXTlen and TXTinfo will have been filled in with the results learned from the network.
159247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname      name;
159347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSInterfaceID InterfaceID;		// ID of the interface the response was received on
159447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSAddr        ip;					// Remote (destination) IP address where this service can be accessed
159547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort      port;				// Port where this service can be accessed
159647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16         TXTlen;
159747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8          TXTinfo[2048];		// Additional demultiplexing information (e.g. LPR queue name)
159847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} ServiceInfo;
159947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
160047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Note: Within an mDNSServiceInfoQueryCallback mDNS all API calls are legal except mDNS_Init(), mDNS_Exit(), mDNS_Execute()
160147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct ServiceInfoQuery_struct ServiceInfoQuery;
160247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef void mDNSServiceInfoQueryCallback(mDNS *const m, ServiceInfoQuery *query);
160347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltstruct ServiceInfoQuery_struct
160447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
160547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Internal state fields. These are used internally by mDNSCore; the client layer needn't be concerned with them.
160647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// No fields need to be set up by the client prior to calling mDNS_StartResolveService();
160747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// all required data is passed as parameters to that function.
160847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// The ServiceInfoQuery structure memory is working storage for mDNSCore to discover the requested information
160947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// and place it in the ServiceInfo structure. After the client has called mDNS_StopResolveService(), it may
161047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// dispose of the ServiceInfoQuery structure while retaining the results in the ServiceInfo structure.
161147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion                   qSRV;
161247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion                   qTXT;
161347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion                   qAv4;
161447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion                   qAv6;
161547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8                        GotSRV;
161647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8                        GotTXT;
161747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8                        GotADD;
161847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32                       Answers;
161947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	ServiceInfo                  *info;
162047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSServiceInfoQueryCallback *ServiceInfoQueryCallback;
162147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	void                         *ServiceInfoQueryContext;
162247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
162347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
162447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef enum { ZoneServiceUpdate, ZoneServiceQuery, ZoneServiceLLQ } ZoneService;
162547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
162647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef void ZoneDataCallback(mDNS *const m, mStatus err, const ZoneData *result);
162747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
162847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltstruct ZoneData_struct
162947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
163047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname       ChildName;			// Name for which we're trying to find the responsible server
163147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	ZoneService      ZoneService;		// Which service we're seeking for this zone (update, query, or LLQ)
163247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname       *CurrentSOA;		// Points to somewhere within ChildName
163347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname       ZoneName;			// Discovered result: Left-hand-side of SOA record
163447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16          ZoneClass;			// Discovered result: DNS Class from SOA record
163547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname       Host;				// Discovered result: Target host from SRV record
163647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort       Port;				// Discovered result: Update port, query port, or LLQ port from SRV record
163747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSAddr         Addr;				// Discovered result: Address of Target host from SRV record
163847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool         ZonePrivate;		// Discovered result: Does zone require encrypted queries?
163947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	ZoneDataCallback *ZoneDataCallback;	// Caller-specified function to be called upon completion
164047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	void             *ZoneDataContext;
164147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion      question;			// Storage for any active question
164247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
164347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
164447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern ZoneData *StartGetZoneData(mDNS *const m, const domainname *const name, const ZoneService target, ZoneDataCallback callback, void *callbackInfo);
164547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void CancelGetZoneData(mDNS *const m, ZoneData *nta);
164647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSBool IsGetZoneDataQuestion(DNSQuestion *q);
164747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
164847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct DNameListElem
164947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
165047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	struct DNameListElem *next;
165147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32 uid;
165247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname name;
165347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} DNameListElem;
165447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
165547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if APPLE_OSX_mDNSResponder
165647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Different states that we go through locating the peer
165747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define TC_STATE_AAAA_PEER			0x000000001		/* Peer's BTMM IPv6 address */
165847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define TC_STATE_AAAA_PEER_RELAY	0x000000002		/* Peer's IPv6 Relay address */
165947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define TC_STATE_SRV_PEER			0x000000003		/* Peer's SRV Record corresponding to IPv4 address */
166047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define TC_STATE_ADDR_PEER			0x000000004		/* Peer's IPv4 address */
166147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
166247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct ClientTunnel
166347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
166447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	struct ClientTunnel *next;
166547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	const char *prefix;
166647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname dstname;
166747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool   MarkedForDeletion;
166847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv6Addr loc_inner;
166947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv4Addr loc_outer;
167047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv6Addr loc_outer6;
167147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv6Addr rmt_inner;
167247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv4Addr rmt_outer;
167347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv6Addr rmt_outer6;
167447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort rmt_outer_port;
167547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16	tc_state;
167647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion q;
167747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} ClientTunnel;
167847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
167947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
168047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ***************************************************************************
168147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if 0
168247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark -
168347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark - NetworkInterfaceInfo_struct
168447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
168547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
168647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct NetworkInterfaceInfo_struct NetworkInterfaceInfo;
168747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
168847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// A NetworkInterfaceInfo_struct serves two purposes:
168947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 1. It holds the address, PTR and HINFO records to advertise a given IP address on a given physical interface
169047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 2. It tells mDNSCore which physical interfaces are available; each physical interface has its own unique InterfaceID.
169147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//    Since there may be multiple IP addresses on a single physical interface,
169247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//    there may be multiple NetworkInterfaceInfo_structs with the same InterfaceID.
169347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//    In this case, to avoid sending the same packet n times, when there's more than one
169447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//    struct with the same InterfaceID, mDNSCore picks one member of the set to be the
169547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//    active representative of the set; all others have the 'InterfaceActive' flag unset.
169647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
169747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltstruct NetworkInterfaceInfo_struct
169847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
169947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Internal state fields. These are used internally by mDNSCore; the client layer needn't be concerned with them.
170047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	NetworkInterfaceInfo *next;
170147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
170247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8          InterfaceActive;	// Set if interface is sending & receiving packets (see comment above)
170347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8          IPv4Available;		// If InterfaceActive, set if v4 available on this InterfaceID
170447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8          IPv6Available;		// If InterfaceActive, set if v6 available on this InterfaceID
170547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
170647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion     NetWakeBrowse;
170747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion     NetWakeResolve[3];	// For fault-tolerance, we try up to three Sleep Proxies
170847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSAddr        SPSAddr[3];
170947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort      SPSPort[3];
171047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32         NextSPSAttempt;		// -1 if we're not currently attempting to register with any Sleep Proxy
171147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32         NextSPSAttemptTime;
171247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
171347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Standard AuthRecords that every Responder host should have (one per active IP address)
171447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord RR_A;					// 'A' or 'AAAA' (address) record for our ".local" name
171547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord RR_PTR;					// PTR (reverse lookup) record
171647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord RR_HINFO;
171747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
171847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Client API fields: The client must set up these fields *before* calling mDNS_RegisterInterface()
171947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSInterfaceID InterfaceID;		// Identifies physical interface; MUST NOT be 0, -1, or -2
172047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSAddr        ip;					// The IPv4 or IPv6 address to advertise
172147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSAddr        mask;
172247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSEthAddr     MAC;
172347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char            ifname[64];			// Windows uses a GUID string for the interface name, which doesn't fit in 16 bytes
172447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8          Advertise;			// False if you are only searching on this interface
172547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8          McastTxRx;			// Send/Receive multicast on this { InterfaceID, address family } ?
172647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8          NetWake;			// Set if Wake-On-Magic-Packet is enabled on this interface
172747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8          Loopback;			// Set if this is the loopback interface
172847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
172947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
173047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define SLE_DELETE              0x00000001
173147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define SLE_WAB_QUERY_STARTED   0x00000002
173247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
173347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef struct SearchListElem
173447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
173547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	struct SearchListElem *next;
173647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname domain;
173747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	int flag;
173847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSInterfaceID InterfaceID;
173947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion BrowseQ;
174047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion DefBrowseQ;
174147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion AutomaticBrowseQ;
174247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion RegisterQ;
174347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion DefRegisterQ;
174447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	int	numCfAnswers;
174547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	ARListElem *AuthRecs;
174647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} SearchListElem;
174747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
174847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// For domain enumeration and automatic browsing
174947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// This is the user's DNS search list.
175047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// In each of these domains we search for our special pointer records (lb._dns-sd._udp.<domain>, etc.)
175147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// to discover recommended domains for domain enumeration (browse, default browse, registration,
175247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// default registration) and possibly one or more recommended automatic browsing domains.
175347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern SearchListElem *SearchList;		// This really ought to be part of mDNS_struct -- SC
175447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
175547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ***************************************************************************
175647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if 0
175747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark -
175847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark - Main mDNS object, used to hold all the mDNS state
175947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
176047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
176147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef void mDNSCallback(mDNS *const m, mStatus result);
176247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
176347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define CACHE_HASH_SLOTS 499
176447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
176547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltenum		// Bit flags -- i.e. values should be 1, 2, 4, 8, etc.
176647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
176747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNS_KnownBug_LimitedIPv6       = 1,
176847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNS_KnownBug_LossySyslog       = 2		// <rdar://problem/6561888>
176947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
177047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
177147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltenum
177247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
177347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	SleepState_Awake = 0,
177447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	SleepState_Transferring = 1,
177547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	SleepState_Sleeping = 2
177647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
177747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
177847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltstruct mDNS_struct
177947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
178047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Internal state fields. These hold the main internal state of mDNSCore;
178147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// the client layer needn't be concerned with them.
178247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// No fields need to be set up by the client prior to calling mDNS_Init();
178347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// all required data is passed as parameters to that function.
178447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
178547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNS_PlatformSupport *p;			// Pointer to platform-specific data of indeterminite size
178647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32  KnownBugs;
178747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool CanReceiveUnicastOn5353;
178847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool AdvertiseLocalAddresses;
178947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool DivertMulticastAdvertisements; // from interfaces that do not advertise local addresses to local-only
179047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mStatus mDNSPlatformStatus;
179147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort UnicastPort4;
179247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort UnicastPort6;
179347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSEthAddr PrimaryMAC;				// Used as unique host ID
179447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSCallback *MainCallback;
179547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	void         *MainContext;
179647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
179747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// For debugging: To catch and report locking failures
179847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32 mDNS_busy;					// Incremented between mDNS_Lock/mDNS_Unlock section
179947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32 mDNS_reentrancy;			// Incremented when calling a client callback
180047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8  lock_rrcache;				// For debugging: Set at times when these lists may not be modified
180147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8  lock_Questions;
180247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8  lock_Records;
180347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#ifndef MaxMsg
180447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	#define MaxMsg 160
180547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
180647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char MsgBuffer[MaxMsg];				// Temp storage used while building error log messages
180747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
180847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Task Scheduling variables
180947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32  timenow_adjust;			// Correction applied if we ever discover time went backwards
181047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32  timenow;					// The time that this particular activation of the mDNS code started
181147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32  timenow_last;				// The time the last time we ran
181247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32  NextScheduledEvent;		// Derived from values below
181347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32  ShutdownTime;				// Set when we're shutting down; allows us to skip some unnecessary steps
181447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32  SuppressSending;			// Don't send local-link mDNS packets during this time
181547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32  NextCacheCheck;			// Next time to refresh cache record before it expires
181647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32  NextScheduledQuery;		// Next time to send query in its exponential backoff sequence
181747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32  NextScheduledProbe;		// Next time to probe for new authoritative record
181847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32  NextScheduledResponse;		// Next time to send authoritative record(s) in responses
181947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32  NextScheduledNATOp;		// Next time to send NAT-traversal packets
182047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32  NextScheduledSPS;			// Next time to purge expiring Sleep Proxy records
182147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32  RandomQueryDelay;			// For de-synchronization of query packets on the wire
182247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32  RandomReconfirmDelay;		// For de-synchronization of reconfirmation queries on the wire
182347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32  PktNum;					// Unique sequence number assigned to each received packet
182447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8   LocalRemoveEvents;			// Set if we may need to deliver remove events for local-only questions and/or local-only records
182547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8   SleepState;				// Set if we're sleeping
182647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8   SleepSeqNum;				// "Epoch number" of our current period of wakefulness
182747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8   SystemWakeOnLANEnabled;	// Set if we want to register with a Sleep Proxy before going to sleep
182847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8   SentSleepProxyRegistration;// Set if we registered (or tried to register) with a Sleep Proxy
182947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8   SystemSleepOnlyIfWakeOnLAN;// Set if we may only sleep if we managed to register with a Sleep Proxy
183047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32  AnnounceOwner;				// After waking from sleep, include OWNER option in packets until this time
183147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32  DelaySleep;				// To inhibit re-sleeping too quickly right after wake
183247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32  SleepLimit;				// Time window to allow deregistrations, etc.,
183347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt										// during which underying platform layer should inhibit system sleep
183447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32  NextScheduledSPRetry;		// Time next sleep proxy registration action is required.
183547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt										// Only valid if SleepLimit is nonzero and DelaySleep is zero.
183647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
183747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32 NextScheduledStopTime;      // Next time to stop a question
183847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
183947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// These fields only required for mDNS Searcher...
184047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion *Questions;				// List of all registered questions, active and inactive
184147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion *NewQuestions;			// Fresh questions not yet answered from cache
184247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion *CurrentQuestion;		// Next question about to be examined in AnswerLocalQuestions()
184347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion *LocalOnlyQuestions;	// Questions with InterfaceID set to mDNSInterface_LocalOnly or mDNSInterface_P2P
184447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion *NewLocalOnlyQuestions;	// Fresh local-only or P2P questions not yet answered
184547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion *RestartQuestion;		// Questions that are being restarted (stop followed by start)
184647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32 rrcache_size;				// Total number of available cache entries
184747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32 rrcache_totalused;			// Number of cache entries currently occupied
184847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32 rrcache_active;				// Number of cache entries currently occupied by records that answer active questions
184947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32 rrcache_report;
185047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	CacheEntity *rrcache_free;
185147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	CacheGroup *rrcache_hash[CACHE_HASH_SLOTS];
185247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32  rrcache_nextcheck[CACHE_HASH_SLOTS];
185347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
185447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthHash rrauth;
185547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
185647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Fields below only required for mDNS Responder...
185747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainlabel nicelabel;				// Rich text label encoded using canonically precomposed UTF-8
185847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainlabel hostlabel;				// Conforms to RFC 1034 "letter-digit-hyphen" ARPANET host name rules
185947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname  MulticastHostname;		// Fully Qualified "dot-local" Host Name, e.g. "Foo.local."
186047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	UTF8str255  HIHardware;
186147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	UTF8str255  HISoftware;
186247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord  DeviceInfo;
186347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord *ResourceRecords;
186447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord *DuplicateRecords;		// Records currently 'on hold' because they are duplicates of existing records
186547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord *NewLocalRecords;		// Fresh AuthRecords (public) not yet delivered to our local-only questions
186647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	AuthRecord *CurrentRecord;			// Next AuthRecord about to be examined
186747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool    NewLocalOnlyRecords;	// Fresh AuthRecords (local only) not yet delivered to our local questions
186847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	NetworkInterfaceInfo *HostInterfaces;
186947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32 ProbeFailTime;
187047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32 NumFailedProbes;
187147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32 SuppressProbes;
187247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
187347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Unicast-specific data
187447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32           NextuDNSEvent;		// uDNS next event
187547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32           NextSRVUpdate;        // Time to perform delayed update
187647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
187747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSServer        *DNSServers;           // list of DNS servers
187847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	McastResolver    *McastResolvers;       // list of Mcast Resolvers
187947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
188047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSAddr          Router;
188147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSAddr          AdvertisedV4;         // IPv4 address pointed to by hostname
188247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSAddr          AdvertisedV6;         // IPv6 address pointed to by hostname
188347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
188447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DomainAuthInfo   *AuthInfoList;         // list of domains requiring authentication for updates
188547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
188647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion       ReverseMap;           // Reverse-map query to find static hostname for service target
188747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSQuestion       AutomaticBrowseDomainQ;
188847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname        StaticHostname;       // Current answer to reverse-map query
188947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainname        FQDN;
189047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	HostnameInfo     *Hostnames;            // List of registered hostnames + hostname metadata
189147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv6Addr        AutoTunnelHostAddr;	// IPv6 address advertised for AutoTunnel services on this machine
189247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool          AutoTunnelHostAddrActive;
189347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// AutoTunnel Relay address has two distinct uses
189447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// AutoTunnelRelayAddrIn: If non-zero, it means that this host can be reached (inbound connection) through the relay
189547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// AutoTunnelRelayAddrOut: If non-zero, it means that this host can use the relay to reach (outbound connection) the
189647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// other hosts through the relay
189747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv6Addr        AutoTunnelRelayAddrIn;
189847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv6Addr        AutoTunnelRelayAddrOut;
189947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	domainlabel       AutoTunnelLabel;		// Used to construct hostname for *IPv4* address of tunnel endpoints
190047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
190147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool          StartWABQueries;		// Start WAB queries for the purpose of domain enumeration
190247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool          RegisterAutoTunnel6;
190347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
190447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// NAT-Traversal fields
190547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	NATTraversalInfo  LLQNAT;					// Single shared NAT Traversal to receive inbound LLQ notifications
190647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	NATTraversalInfo *NATTraversals;
190747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	NATTraversalInfo *CurrentNATTraversal;
190847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32           retryIntervalGetAddr;		// delta between time sent and retry
190947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32           retryGetAddr;				// absolute time when we retry
191047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSv4Addr        ExternalAddress;
191147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
191247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	UDPSocket        *NATMcastRecvskt;			// For receiving NAT-PMP AddrReply multicasts from router on port 5350
191347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu32           LastNATupseconds;			// NAT engine uptime in seconds, from most recent NAT packet
191447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSs32           LastNATReplyLocalTime;	// Local time in ticks when most recent NAT packet was received
191547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu16           LastNATMapResultCode;		// Most recent error code for mappings
191647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
191747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	tcpLNTInfo        tcpAddrInfo;				// legacy NAT traversal TCP connection info for external address
191847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	tcpLNTInfo        tcpDeviceInfo;			// legacy NAT traversal TCP connection info for device info
191947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	tcpLNTInfo       *tcpInfoUnmapList;			// list of pending unmap requests
192047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSInterfaceID   UPnPInterfaceID;
192147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	UDPSocket        *SSDPSocket;               // For SSDP request/response
192247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool          SSDPWANPPPConnection;     // whether we should send the SSDP query for WANIPConnection or WANPPPConnection
192347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort        UPnPRouterPort;			// port we send discovery messages to
192447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSIPPort        UPnPSOAPPort;				// port we send SOAP messages to
192547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8           *UPnPRouterURL;			// router's URL string
192647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSBool          UPnPWANPPPConnection;     // whether we're using WANIPConnection or WANPPPConnection
192747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8           *UPnPSOAPURL;				// router's SOAP control URL string
192847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8           *UPnPRouterAddressString;	// holds both the router's address and port
192947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8           *UPnPSOAPAddressString;	// holds both address and port for SOAP messages
193047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
193147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Sleep Proxy Server fields
193247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8            SPSType;					// 0 = off, 10-99 encodes desirability metric
193347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8            SPSPortability;			// 10-99
193447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8            SPSMarginalPower;			// 10-99
193547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8            SPSTotalPower;			// 10-99
193647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSu8            SPSState;					// 0 = off, 1 = running, 2 = shutting down, 3 = suspended during sleep
193747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSInterfaceID   SPSProxyListChanged;
193847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	UDPSocket        *SPSSocket;
193947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	ServiceRecordSet  SPSRecords;
194047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSQuestionCallback *SPSBrowseCallback;    // So the platform layer can do something useful with SPS browse results
194147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	int               ProxyRecords;				// Total number of records we're holding as proxy
194247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	#define           MAX_PROXY_RECORDS 10000	/* DOS protection: 400 machines at 25 records each */
194347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
194447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if APPLE_OSX_mDNSResponder
194547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	ClientTunnel     *TunnelClients;
194647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	uuid_t           asl_uuid;					// uuid for ASL logging
194747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	void		    *WCF;
194847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
194947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
195047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Fixed storage, to avoid creating large objects on the stack
195147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// The imsg is declared as a union with a pointer type to enforce CPU-appropriate alignment
195247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	union { DNSMessage m; void *p; } imsg;  // Incoming message received from wire
195347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	DNSMessage        omsg;                 // Outgoing message we're building
195447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	LargeCacheRecord  rec;                  // Resource Record extracted from received message
195547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
195647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
195747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define FORALL_CACHERECORDS(SLOT,CG,CR)                           \
195847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	for ((SLOT) = 0; (SLOT) < CACHE_HASH_SLOTS; (SLOT)++)         \
195947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		for ((CG)=m->rrcache_hash[(SLOT)]; (CG); (CG)=(CG)->next) \
196047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt			for ((CR) = (CG)->members; (CR); (CR)=(CR)->next)
196147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
196247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ***************************************************************************
196347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if 0
196447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark -
196547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark - Useful Static Constants
196647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
196747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
196847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSInterfaceID mDNSInterface_Any;				// Zero
196947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSInterfaceID mDNSInterface_LocalOnly;		// Special value
197047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSInterfaceID mDNSInterface_Unicast;			// Special value
197147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSInterfaceID mDNSInterfaceMark;				// Special value
197247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSInterfaceID mDNSInterface_P2P;				// Special value
197347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
197447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSIPPort   DiscardPort;
197547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSIPPort   SSHPort;
197647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSIPPort   UnicastDNSPort;
197747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSIPPort   SSDPPort;
197847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSIPPort   IPSECPort;
197947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSIPPort   NSIPCPort;
198047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSIPPort   NATPMPAnnouncementPort;
198147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSIPPort   NATPMPPort;
198247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSIPPort   DNSEXTPort;
198347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSIPPort   MulticastDNSPort;
198447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSIPPort   LoopbackIPCPort;
198547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSIPPort   PrivateDNSPort;
198647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
198747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const OwnerOptData    zeroOwner;
198847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
198947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSIPPort      zeroIPPort;
199047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSv4Addr      zerov4Addr;
199147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSv6Addr      zerov6Addr;
199247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSEthAddr     zeroEthAddr;
199347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSv4Addr      onesIPv4Addr;
199447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSv6Addr      onesIPv6Addr;
199547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSEthAddr     onesEthAddr;
199647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSAddr        zeroAddr;
199747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
199847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSv4Addr   AllDNSAdminGroup;
199947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSv4Addr   AllHosts_v4;
200047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSv6Addr   AllHosts_v6;
200147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSv6Addr   NDP_prefix;
200247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSEthAddr  AllHosts_v6_Eth;
200347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSAddr     AllDNSLinkGroup_v4;
200447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSAddr     AllDNSLinkGroup_v6;
200547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
200647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSOpaque16 zeroID;
200747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSOpaque16 onesID;
200847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSOpaque16 QueryFlags;
200947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSOpaque16 uQueryFlags;
201047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSOpaque16 ResponseFlags;
201147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSOpaque16 UpdateReqFlags;
201247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSOpaque16 UpdateRespFlags;
201347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
201447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSOpaque64 zeroOpaque64;
201547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
201647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSBool StrictUnicastOrdering;
201747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSu8 NumUnicastDNSServers;
201847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
201947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define localdomain           (*(const domainname *)"\x5" "local")
202047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define DeviceInfoName        (*(const domainname *)"\xC" "_device-info" "\x4" "_tcp")
202147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define SleepProxyServiceType (*(const domainname *)"\xC" "_sleep-proxy" "\x4" "_udp")
202247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
202347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ***************************************************************************
202447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if 0
202547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark -
202647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark - Inline functions
202747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
202847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
202947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if (defined(_MSC_VER))
203047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	#define mDNSinline static __inline
203147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#elif ((__GNUC__ > 2) || ((__GNUC__ == 2) && (__GNUC_MINOR__ >= 9)))
203247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	#define mDNSinline static inline
203347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
203447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
203547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// If we're not doing inline functions, then this header needs to have the extern declarations
203647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if !defined(mDNSinline)
203747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSs32      NonZeroTime(mDNSs32 t);
203847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSu16      mDNSVal16(mDNSOpaque16 x);
203947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSOpaque16 mDNSOpaque16fromIntVal(mDNSu16 v);
204047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
204147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
204247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// If we're compiling the particular C file that instantiates our inlines, then we
204347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// define "mDNSinline" (to empty string) so that we generate code in the following section
204447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if (!defined(mDNSinline) && mDNS_InstantiateInlines)
204547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSinline
204647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
204747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
204847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#ifdef mDNSinline
204947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
205047e4cebad7397422144bb03a21f3f7682c062c4aRobert GreenwaltmDNSinline mDNSs32 NonZeroTime(mDNSs32 t) { if (t) return(t); else return(1); }
205147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
205247e4cebad7397422144bb03a21f3f7682c062c4aRobert GreenwaltmDNSinline mDNSu16 mDNSVal16(mDNSOpaque16 x) { return((mDNSu16)((mDNSu16)x.b[0] <<  8 | (mDNSu16)x.b[1])); }
205347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
205447e4cebad7397422144bb03a21f3f7682c062c4aRobert GreenwaltmDNSinline mDNSOpaque16 mDNSOpaque16fromIntVal(mDNSu16 v)
205547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
205647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSOpaque16 x;
205747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	x.b[0] = (mDNSu8)(v >> 8);
205847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	x.b[1] = (mDNSu8)(v & 0xFF);
205947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	return(x);
206047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	}
206147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
206247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
206347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
206447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ***************************************************************************
206547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if 0
206647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark -
206747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark - Main Client Functions
206847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
206947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
207047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Every client should call mDNS_Init, passing in storage for the mDNS object and the mDNS_PlatformSupport object.
207147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
207247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Clients that are only advertising services should use mDNS_Init_NoCache and mDNS_Init_ZeroCacheSize.
207347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Clients that plan to perform queries (mDNS_StartQuery, mDNS_StartBrowse, mDNS_StartResolveService, etc.)
207447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// need to provide storage for the resource record cache, or the query calls will return 'mStatus_NoCache'.
207547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// The rrcachestorage parameter is the address of memory for the resource record cache, and
207647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// the rrcachesize parameter is the number of entries in the CacheRecord array passed in.
207747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// (i.e. the size of the cache memory needs to be sizeof(CacheRecord) * rrcachesize).
207847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// OS X 10.3 Panther uses an initial cache size of 64 entries, and then mDNSCore sends an
207947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mStatus_GrowCache message if it needs more.
208047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
208147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Most clients should use mDNS_Init_AdvertiseLocalAddresses. This causes mDNSCore to automatically
208247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// create the correct address records for all the hosts interfaces. If you plan to advertise
208347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// services being offered by the local machine, this is almost always what you want.
208447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// There are two cases where you might use mDNS_Init_DontAdvertiseLocalAddresses:
208547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 1. A client-only device, that browses for services but doesn't advertise any of its own.
208647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 2. A proxy-registration service, that advertises services being offered by other machines, and takes
208747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//    the appropriate steps to manually create the correct address records for those other machines.
208847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// In principle, a proxy-like registration service could manually create address records for its own machine too,
208947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// but this would be pointless extra effort when using mDNS_Init_AdvertiseLocalAddresses does that for you.
209047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
209147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Note that a client-only device that wishes to prohibit multicast advertisements (e.g. from
209247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// higher-layer API calls) must also set DivertMulticastAdvertisements in the mDNS structure and
209347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// advertise local address(es) on a loopback interface.
209447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
209547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// When mDNS has finished setting up the client's callback is called
209647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// A client can also spin and poll the mDNSPlatformStatus field to see when it changes from mStatus_Waiting to mStatus_NoError
209747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
209847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Call mDNS_StartExit to tidy up before exiting
209947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Because exiting may be an asynchronous process (e.g. if unicast records need to be deregistered)
210047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// client layer may choose to wait until mDNS_ExitNow() returns true before calling mDNS_FinalExit().
210147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
210247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Call mDNS_Register with a completed AuthRecord object to register a resource record
210347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// If the resource record type is kDNSRecordTypeUnique (or kDNSknownunique) then if a conflicting resource record is discovered,
210447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// the resource record's mDNSRecordCallback will be called with error code mStatus_NameConflict. The callback should deregister
210547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// the record, and may then try registering the record again after picking a new name (e.g. by automatically appending a number).
210647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Following deregistration, the RecordCallback will be called with result mStatus_MemFree to signal that it is safe to deallocate
210747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// the record's storage (memory must be freed asynchronously to allow for goodbye packets and dynamic update deregistration).
210847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
210947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Call mDNS_StartQuery to initiate a query. mDNS will proceed to issue Multicast DNS query packets, and any time a response
211047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// is received containing a record which matches the question, the DNSQuestion's mDNSAnswerCallback function will be called
211147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Call mDNS_StopQuery when no more answers are required
211247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
211347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Care should be taken on multi-threaded or interrupt-driven environments.
211447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// The main mDNS routines call mDNSPlatformLock() on entry and mDNSPlatformUnlock() on exit;
211547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// each platform layer needs to implement these appropriately for its respective platform.
211647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// For example, if the support code on a particular platform implements timer callbacks at interrupt time, then
211747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNSPlatformLock/Unlock need to disable interrupts or do similar concurrency control to ensure that the mDNS
211847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// code is not entered by an interrupt-time timer callback while in the middle of processing a client call.
211947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
212047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus mDNS_Init      (mDNS *const m, mDNS_PlatformSupport *const p,
212147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt								CacheEntity *rrcachestorage, mDNSu32 rrcachesize,
212247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt								mDNSBool AdvertiseLocalAddresses,
212347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt								mDNSCallback *Callback, void *Context);
212447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// See notes above on use of NoCache/ZeroCacheSize
212547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNS_Init_NoCache                     mDNSNULL
212647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNS_Init_ZeroCacheSize               0
212747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// See notes above on use of Advertise/DontAdvertiseLocalAddresses
212847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNS_Init_AdvertiseLocalAddresses     mDNStrue
212947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNS_Init_DontAdvertiseLocalAddresses mDNSfalse
213047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNS_Init_NoInitCallback              mDNSNULL
213147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNS_Init_NoInitCallbackContext       mDNSNULL
213247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
213347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void    mDNS_ConfigChanged(mDNS *const m);
213447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void    mDNS_GrowCache (mDNS *const m, CacheEntity *storage, mDNSu32 numrecords);
213547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void    mDNS_GrowAuth (mDNS *const m, AuthEntity *storage, mDNSu32 numrecords);
213647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void    mDNS_StartExit (mDNS *const m);
213747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void    mDNS_FinalExit (mDNS *const m);
213847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNS_Close(m) do { mDNS_StartExit(m); mDNS_FinalExit(m); } while(0)
213947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNS_ExitNow(m, now) ((now) - (m)->ShutdownTime >= 0 || (!(m)->ResourceRecords))
214047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
214147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSs32 mDNS_Execute   (mDNS *const m);
214247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
214347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus mDNS_Register  (mDNS *const m, AuthRecord *const rr);
214447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus mDNS_Update    (mDNS *const m, AuthRecord *const rr, mDNSu32 newttl,
214547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt								const mDNSu16 newrdlength, RData *const newrdata, mDNSRecordUpdateCallback *Callback);
214647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus mDNS_Deregister(mDNS *const m, AuthRecord *const rr);
214747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
214847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus mDNS_StartQuery(mDNS *const m, DNSQuestion *const question);
214947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus mDNS_StopQuery (mDNS *const m, DNSQuestion *const question);
215047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus mDNS_StopQueryWithRemoves(mDNS *const m, DNSQuestion *const question);
215147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus mDNS_Reconfirm (mDNS *const m, CacheRecord *const cacherr);
215247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus mDNS_ReconfirmByValue(mDNS *const m, ResourceRecord *const rr);
215347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void    mDNS_PurgeCacheResourceRecord(mDNS *const m, CacheRecord *rr);
215447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSs32 mDNS_TimeNow(const mDNS *const m);
215547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
215647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus mDNS_StartNATOperation(mDNS *const m, NATTraversalInfo *traversal);
215747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus mDNS_StopNATOperation(mDNS *const m, NATTraversalInfo *traversal);
215847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus mDNS_StopNATOperation_internal(mDNS *m, NATTraversalInfo *traversal);
215947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
216047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern DomainAuthInfo *GetAuthInfoForName(mDNS *m, const domainname *const name);
216147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
216247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void    mDNS_UpdateAllowSleep(mDNS *const m);
216347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
216447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ***************************************************************************
216547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if 0
216647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark -
216747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark - Platform support functions that are accessible to the client layer too
216847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
216947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
217047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSs32  mDNSPlatformOneSecond;
217147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
217247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ***************************************************************************
217347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if 0
217447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark -
217547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark - General utility and helper functions
217647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
217747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
217847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNS_Dereg_normal is used for most calls to mDNS_Deregister_internal
217947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNS_Dereg_rapid is used to send one goodbye instead of three, when we want the memory available for reuse sooner
218047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNS_Dereg_conflict is used to indicate that this record is being forcibly deregistered because of a conflict
218147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNS_Dereg_repeat is used when cleaning up, for records that may have already been forcibly deregistered
218247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef enum { mDNS_Dereg_normal, mDNS_Dereg_rapid, mDNS_Dereg_conflict, mDNS_Dereg_repeat } mDNS_Dereg_type;
218347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
218447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNS_RegisterService is a single call to register the set of resource records associated with a given named service.
218547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
218647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNS_StartResolveService is single call which is equivalent to multiple calls to mDNS_StartQuery,
218747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// to find the IP address, port number, and demultiplexing information for a given named service.
218847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// As with mDNS_StartQuery, it executes asynchronously, and calls the ServiceInfoQueryCallback when the answer is
218947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// found. After the service is resolved, the client should call mDNS_StopResolveService to complete the transaction.
219047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// The client can also call mDNS_StopResolveService at any time to abort the transaction.
219147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
219247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNS_AddRecordToService adds an additional record to a Service Record Set.  This record may be deregistered
219347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// via mDNS_RemoveRecordFromService, or by deregistering the service.  mDNS_RemoveRecordFromService is passed a
219447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// callback to free the memory associated with the extra RR when it is safe to do so.  The ExtraResourceRecord
219547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// object can be found in the record's context pointer.
219647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
219747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNS_GetBrowseDomains is a special case of the mDNS_StartQuery call, where the resulting answers
219847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// are a list of PTR records indicating (in the rdata) domains that are recommended for browsing.
219947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// After getting the list of domains to browse, call mDNS_StopQuery to end the search.
220047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNS_GetDefaultBrowseDomain returns the name of the domain that should be highlighted by default.
220147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
220247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNS_GetRegistrationDomains and mDNS_GetDefaultRegistrationDomain are the equivalent calls to get the list
220347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// of one or more domains that should be offered to the user as choices for where they may register their service,
220447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// and the default domain in which to register in the case where the user has made no selection.
220547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
220647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void    mDNS_SetupResourceRecord(AuthRecord *rr, RData *RDataStorage, mDNSInterfaceID InterfaceID,
220747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt               mDNSu16 rrtype, mDNSu32 ttl, mDNSu8 RecordType, AuthRecType artype, mDNSRecordCallback Callback, void *Context);
220847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
220947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNS_RegisterService() flags parameter bit definitions
221047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltenum
221147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
221247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		regFlagIncludeP2P	= 0x1,	// include P2P interfaces when using mDNSInterface_Any
221347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt		regFlagKnownUnique	= 0x2	// client guarantees that SRV and TXT record names are unique
221447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
221547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
221647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus mDNS_RegisterService  (mDNS *const m, ServiceRecordSet *sr,
221747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt               const domainlabel *const name, const domainname *const type, const domainname *const domain,
221847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt               const domainname *const host, mDNSIPPort port, const mDNSu8 txtinfo[], mDNSu16 txtlen,
221947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt               AuthRecord *SubTypes, mDNSu32 NumSubTypes,
222047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt               mDNSInterfaceID InterfaceID, mDNSServiceCallback Callback, void *Context, mDNSu32 flags);
222147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus mDNS_AddRecordToService(mDNS *const m, ServiceRecordSet *sr, ExtraResourceRecord *extra, RData *rdata, mDNSu32 ttl,  mDNSu32 includeP2P);
222247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus mDNS_RemoveRecordFromService(mDNS *const m, ServiceRecordSet *sr, ExtraResourceRecord *extra, mDNSRecordCallback MemFreeCallback, void *Context);
222347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus mDNS_RenameAndReregisterService(mDNS *const m, ServiceRecordSet *const sr, const domainlabel *newname);
222447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus mDNS_DeregisterService_drt(mDNS *const m, ServiceRecordSet *sr, mDNS_Dereg_type drt);
222547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNS_DeregisterService(M,S) mDNS_DeregisterService_drt((M), (S), mDNS_Dereg_normal)
222647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
222747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus mDNS_RegisterNoSuchService(mDNS *const m, AuthRecord *const rr,
222847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt               const domainlabel *const name, const domainname *const type, const domainname *const domain,
222947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt               const domainname *const host,
223047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt               const mDNSInterfaceID InterfaceID, mDNSRecordCallback Callback, void *Context, mDNSBool includeP2P);
223147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define        mDNS_DeregisterNoSuchService mDNS_Deregister
223247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
223347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void mDNS_SetupQuestion(DNSQuestion *const q, const mDNSInterfaceID InterfaceID, const domainname *const name,
223447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt               const mDNSu16 qtype, mDNSQuestionCallback *const callback, void *const context);
223547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
223647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus mDNS_StartBrowse(mDNS *const m, DNSQuestion *const question,
223747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt               const domainname *const srv, const domainname *const domain,
223847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt               const mDNSInterfaceID InterfaceID, mDNSBool ForceMCast, mDNSQuestionCallback *Callback, void *Context);
223947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define        mDNS_StopBrowse mDNS_StopQuery
224047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
224147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus mDNS_StartResolveService(mDNS *const m, ServiceInfoQuery *query, ServiceInfo *info, mDNSServiceInfoQueryCallback *Callback, void *Context);
224247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void    mDNS_StopResolveService (mDNS *const m, ServiceInfoQuery *query);
224347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
224447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef enum
224547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
224647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNS_DomainTypeBrowse              = 0,
224747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNS_DomainTypeBrowseDefault       = 1,
224847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNS_DomainTypeBrowseAutomatic     = 2,
224947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNS_DomainTypeRegistration        = 3,
225047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNS_DomainTypeRegistrationDefault = 4,
225147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
225247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNS_DomainTypeMax = 4
225347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} mDNS_DomainType;
225447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
225547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const char *const mDNS_DomainTypeNames[];
225647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
225747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus mDNS_GetDomains(mDNS *const m, DNSQuestion *const question, mDNS_DomainType DomainType, const domainname *dom,
225847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt								const mDNSInterfaceID InterfaceID, mDNSQuestionCallback *Callback, void *Context);
225947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define        mDNS_StopGetDomains mDNS_StopQuery
226047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus mDNS_AdvertiseDomains(mDNS *const m, AuthRecord *rr, mDNS_DomainType DomainType, const mDNSInterfaceID InterfaceID, char *domname);
226147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define        mDNS_StopAdvertiseDomains mDNS_Deregister
226247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
226347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSOpaque16 mDNS_NewMessageID(mDNS *const m);
226447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSBool mDNS_AddressIsLocalSubnet(mDNS *const m, const mDNSInterfaceID InterfaceID, const mDNSAddr *addr);
226547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
226647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern DNSServer *GetServerForName(mDNS *m, const domainname *name, mDNSInterfaceID InterfaceID);
226747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern DNSServer *GetServerForQuestion(mDNS *m, DNSQuestion *question);
226847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSu32 SetValidDNSServers(mDNS *m, DNSQuestion *question);
226947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
227047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ***************************************************************************
227147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if 0
227247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark -
227347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark - DNS name utility functions
227447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
227547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
227647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// In order to expose the full capabilities of the DNS protocol (which allows any arbitrary eight-bit values
227747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// in domain name labels, including unlikely characters like ascii nulls and even dots) all the mDNS APIs
227847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// work with DNS's native length-prefixed strings. For convenience in C, the following utility functions
227947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// are provided for converting between C's null-terminated strings and DNS's length-prefixed strings.
228047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
228147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Assignment
228247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// A simple C structure assignment of a domainname can cause a protection fault by accessing unmapped memory,
228347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// because that object is defined to be 256 bytes long, but not all domainname objects are truly the full size.
228447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// This macro uses mDNSPlatformMemCopy() to make sure it only touches the actual bytes that are valid.
228547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define AssignDomainName(DST, SRC) do { mDNSu16 len__ = DomainNameLength((SRC)); \
228647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	if (len__ <= MAX_DOMAIN_NAME) mDNSPlatformMemCopy((DST)->c, (SRC)->c, len__); else (DST)->c[0] = 0; } while(0)
228747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
228847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Comparison functions
228947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define SameDomainLabelCS(A,B) ((A)[0] == (B)[0] && mDNSPlatformMemSame((A)+1, (B)+1, (A)[0]))
229047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSBool SameDomainLabel(const mDNSu8 *a, const mDNSu8 *b);
229147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSBool SameDomainName(const domainname *const d1, const domainname *const d2);
229247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSBool SameDomainNameCS(const domainname *const d1, const domainname *const d2);
229347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef mDNSBool DomainNameComparisonFn(const domainname *const d1, const domainname *const d2);
229447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSBool IsLocalDomain(const domainname *d);     // returns true for domains that by default should be looked up using link-local multicast
229547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
229647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define StripFirstLabel(X) ((const domainname *)&(X)->c[(X)->c[0] ? 1 + (X)->c[0] : 0])
229747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
229847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define FirstLabel(X)  ((const domainlabel *)(X))
229947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define SecondLabel(X) ((const domainlabel *)StripFirstLabel(X))
230047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define ThirdLabel(X)  ((const domainlabel *)StripFirstLabel(StripFirstLabel(X)))
230147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
230247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern const mDNSu8 *LastLabel(const domainname *d);
230347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
230447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Get total length of domain name, in native DNS format, including terminal root label
230547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//   (e.g. length of "com." is 5 (length byte, three data bytes, final zero)
230647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSu16  DomainNameLengthLimit(const domainname *const name, const mDNSu8 *limit);
230747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define DomainNameLength(name) DomainNameLengthLimit((name), (name)->c + MAX_DOMAIN_NAME)
230847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
230947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Append functions to append one or more labels to an existing native format domain name:
231047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//   AppendLiteralLabelString adds a single label from a literal C string, with no escape character interpretation.
231147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//   AppendDNSNameString      adds zero or more labels from a C string using conventional DNS dots-and-escaping interpretation
231247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//   AppendDomainLabel        adds a single label from a native format domainlabel
231347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//   AppendDomainName         adds zero or more labels from a native format domainname
231447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSu8  *AppendLiteralLabelString(domainname *const name, const char *cstr);
231547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSu8  *AppendDNSNameString     (domainname *const name, const char *cstr);
231647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSu8  *AppendDomainLabel       (domainname *const name, const domainlabel *const label);
231747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSu8  *AppendDomainName        (domainname *const name, const domainname *const append);
231847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
231947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Convert from null-terminated string to native DNS format:
232047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//   The DomainLabel form makes a single label from a literal C string, with no escape character interpretation.
232147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//   The DomainName form makes native format domain name from a C string using conventional DNS interpretation:
232247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//     dots separate labels, and within each label, '\.' represents a literal dot, '\\' represents a literal
232347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//     backslash and backslash with three decimal digits (e.g. \000) represents an arbitrary byte value.
232447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSBool MakeDomainLabelFromLiteralString(domainlabel *const label, const char *cstr);
232547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSu8  *MakeDomainNameFromDNSNameString (domainname  *const name,  const char *cstr);
232647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
232747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Convert native format domainlabel or domainname back to C string format
232847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// IMPORTANT:
232947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// When using ConvertDomainLabelToCString, the target buffer must be MAX_ESCAPED_DOMAIN_LABEL (254) bytes long
233047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// to guarantee there will be no buffer overrun. It is only safe to use a buffer shorter than this in rare cases
233147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// where the label is known to be constrained somehow (for example, if the label is known to be either "_tcp" or "_udp").
233247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Similarly, when using ConvertDomainNameToCString, the target buffer must be MAX_ESCAPED_DOMAIN_NAME (1009) bytes long.
233347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// See definitions of MAX_ESCAPED_DOMAIN_LABEL and MAX_ESCAPED_DOMAIN_NAME for more detailed explanation.
233447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern char    *ConvertDomainLabelToCString_withescape(const domainlabel *const name, char *cstr, char esc);
233547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define         ConvertDomainLabelToCString_unescaped(D,C) ConvertDomainLabelToCString_withescape((D), (C), 0)
233647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define         ConvertDomainLabelToCString(D,C)           ConvertDomainLabelToCString_withescape((D), (C), '\\')
233747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern char    *ConvertDomainNameToCString_withescape(const domainname *const name, char *cstr, char esc);
233847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define         ConvertDomainNameToCString_unescaped(D,C) ConvertDomainNameToCString_withescape((D), (C), 0)
233947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define         ConvertDomainNameToCString(D,C)           ConvertDomainNameToCString_withescape((D), (C), '\\')
234047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
234147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void     ConvertUTF8PstringToRFC1034HostLabel(const mDNSu8 UTF8Name[], domainlabel *const hostlabel);
234247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
234347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSu8  *ConstructServiceName(domainname *const fqdn, const domainlabel *name, const domainname *type, const domainname *const domain);
234447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSBool DeconstructServiceName(const domainname *const fqdn, domainlabel *const name, domainname *const type, domainname *const domain);
234547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
234647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Note: Some old functions have been replaced by more sensibly-named versions.
234747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// You can uncomment the hash-defines below if you don't want to have to change your source code right away.
234847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// When updating your code, note that (unlike the old versions) *all* the new routines take the target object
234947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// as their first parameter.
235047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//#define ConvertCStringToDomainName(SRC,DST)  MakeDomainNameFromDNSNameString((DST),(SRC))
235147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//#define ConvertCStringToDomainLabel(SRC,DST) MakeDomainLabelFromLiteralString((DST),(SRC))
235247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//#define AppendStringLabelToName(DST,SRC)     AppendLiteralLabelString((DST),(SRC))
235347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//#define AppendStringNameToName(DST,SRC)      AppendDNSNameString((DST),(SRC))
235447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//#define AppendDomainLabelToName(DST,SRC)     AppendDomainLabel((DST),(SRC))
235547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//#define AppendDomainNameToName(DST,SRC)      AppendDomainName((DST),(SRC))
235647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
235747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ***************************************************************************
235847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if 0
235947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark -
236047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark - Other utility functions and macros
236147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
236247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
236347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNS_vsnprintf/snprintf return the number of characters written, excluding the final terminating null.
236447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// The output is always null-terminated: for example, if the output turns out to be exactly buflen long,
236547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// then the output will be truncated by one character to allow space for the terminating null.
236647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Unlike standard C vsnprintf/snprintf, they return the number of characters *actually* written,
236747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// not the number of characters that *would* have been printed were buflen unlimited.
236847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSu32 mDNS_vsnprintf(char *sbuffer, mDNSu32 buflen, const char *fmt, va_list arg);
236947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSu32 mDNS_snprintf(char *sbuffer, mDNSu32 buflen, const char *fmt, ...) IS_A_PRINTF_STYLE_FUNCTION(3,4);
237047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSu32 NumCacheRecordsForInterfaceID(const mDNS *const m, mDNSInterfaceID id);
237147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern char *DNSTypeName(mDNSu16 rrtype);
237247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern char *GetRRDisplayString_rdb(const ResourceRecord *const rr, const RDataBody *const rd1, char *const buffer);
237347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define RRDisplayString(m, rr) GetRRDisplayString_rdb(rr, &(rr)->rdata->u, (m)->MsgBuffer)
237447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define ARDisplayString(m, rr) GetRRDisplayString_rdb(&(rr)->resrec, &(rr)->resrec.rdata->u, (m)->MsgBuffer)
237547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define CRDisplayString(m, rr) GetRRDisplayString_rdb(&(rr)->resrec, &(rr)->resrec.rdata->u, (m)->MsgBuffer)
237647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSBool mDNSSameAddress(const mDNSAddr *ip1, const mDNSAddr *ip2);
237747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void IncrementLabelSuffix(domainlabel *name, mDNSBool RichText);
237847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSBool mDNSv4AddrIsRFC1918(mDNSv4Addr *addr);  // returns true for RFC1918 private addresses
237947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSAddrIsRFC1918(X) ((X)->type == mDNSAddrType_IPv4 && mDNSv4AddrIsRFC1918(&(X)->ip.v4))
238047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
238147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSSameIPPort(A,B)      ((A).NotAnInteger == (B).NotAnInteger)
238247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSSameOpaque16(A,B)    ((A).NotAnInteger == (B).NotAnInteger)
238347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSSameOpaque32(A,B)    ((A).NotAnInteger == (B).NotAnInteger)
238447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSSameOpaque64(A,B)    ((A)->l[0] == (B)->l[0] && (A)->l[1] == (B)->l[1])
238547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
238647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSSameIPv4Address(A,B) ((A).NotAnInteger == (B).NotAnInteger)
238747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSSameIPv6Address(A,B) ((A).l[0] == (B).l[0] && (A).l[1] == (B).l[1] && (A).l[2] == (B).l[2] && (A).l[3] == (B).l[3])
238847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSSameEthAddress(A,B)  ((A)->w[0] == (B)->w[0] && (A)->w[1] == (B)->w[1] && (A)->w[2] == (B)->w[2])
238947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
239047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSIPPortIsZero(A)      ((A).NotAnInteger                            == 0)
239147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSOpaque16IsZero(A)    ((A).NotAnInteger                            == 0)
239247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSOpaque64IsZero(A)    (((A)->l[0] | (A)->l[1]                    ) == 0)
239347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSIPv4AddressIsZero(A) ((A).NotAnInteger                            == 0)
239447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSIPv6AddressIsZero(A) (((A).l[0] | (A).l[1] | (A).l[2] | (A).l[3]) == 0)
239547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSEthAddressIsZero(A)  (((A).w[0] | (A).w[1] | (A).w[2]           ) == 0)
239647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
239747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSIPv4AddressIsOnes(A) ((A).NotAnInteger == 0xFFFFFFFF)
239847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSIPv6AddressIsOnes(A) (((A).l[0] & (A).l[1] & (A).l[2] & (A).l[3]) == 0xFFFFFFFF)
239947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
240047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSAddressIsAllDNSLinkGroup(X) (                                                            \
240147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	((X)->type == mDNSAddrType_IPv4 && mDNSSameIPv4Address((X)->ip.v4, AllDNSLinkGroup_v4.ip.v4)) || \
240247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	((X)->type == mDNSAddrType_IPv6 && mDNSSameIPv6Address((X)->ip.v6, AllDNSLinkGroup_v6.ip.v6))    )
240347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
240447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSAddressIsZero(X) (                                                \
240547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	((X)->type == mDNSAddrType_IPv4 && mDNSIPv4AddressIsZero((X)->ip.v4))  || \
240647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	((X)->type == mDNSAddrType_IPv6 && mDNSIPv6AddressIsZero((X)->ip.v6))     )
240747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
240847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSAddressIsValidNonZero(X) (                                        \
240947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	((X)->type == mDNSAddrType_IPv4 && !mDNSIPv4AddressIsZero((X)->ip.v4)) || \
241047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	((X)->type == mDNSAddrType_IPv6 && !mDNSIPv6AddressIsZero((X)->ip.v6))    )
241147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
241247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSAddressIsOnes(X) (                                                \
241347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	((X)->type == mDNSAddrType_IPv4 && mDNSIPv4AddressIsOnes((X)->ip.v4))  || \
241447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	((X)->type == mDNSAddrType_IPv6 && mDNSIPv6AddressIsOnes((X)->ip.v6))     )
241547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
241647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSAddressIsValid(X) (                                                                                             \
241747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	((X)->type == mDNSAddrType_IPv4) ? !(mDNSIPv4AddressIsZero((X)->ip.v4) || mDNSIPv4AddressIsOnes((X)->ip.v4)) :          \
241847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	((X)->type == mDNSAddrType_IPv6) ? !(mDNSIPv6AddressIsZero((X)->ip.v6) || mDNSIPv6AddressIsOnes((X)->ip.v6)) : mDNSfalse)
241947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
242047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSv4AddressIsLinkLocal(X) ((X)->b[0] ==  169 &&  (X)->b[1]         ==  254)
242147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSv6AddressIsLinkLocal(X) ((X)->b[0] == 0xFE && ((X)->b[1] & 0xC0) == 0x80)
242247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
242347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSAddressIsLinkLocal(X)  (                                                    \
242447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	((X)->type == mDNSAddrType_IPv4) ? mDNSv4AddressIsLinkLocal(&(X)->ip.v4) :          \
242547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	((X)->type == mDNSAddrType_IPv6) ? mDNSv6AddressIsLinkLocal(&(X)->ip.v6) : mDNSfalse)
242647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
242747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSv4AddressIsLoopback(X) ((X)->b[0] == 127 && (X)->b[1] == 0 && (X)->b[2] == 0 && (X)->b[3] == 1)
242847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSv6AddressIsLoopback(X) ((((X)->l[0] | (X)->l[1] | (X)->l[2]) == 0) && ((X)->b[12] == 0 && (X)->b[13] == 0 && (X)->b[14] == 0 && (X)->b[15] == 1))
242947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
243047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ***************************************************************************
243147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if 0
243247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark -
243347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark - Authentication Support
243447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
243547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
243647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Unicast DNS and Dynamic Update specific Client Calls
243747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
243847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNS_SetSecretForDomain tells the core to authenticate (via TSIG with an HMAC_MD5 hash of the shared secret)
243947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// when dynamically updating a given zone (and its subdomains).  The key used in authentication must be in
244047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// domain name format.  The shared secret must be a null-terminated base64 encoded string.  A minimum size of
244147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 16 bytes (128 bits) is recommended for an MD5 hash as per RFC 2485.
244247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Calling this routine multiple times for a zone replaces previously entered values.  Call with a NULL key
244347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// to disable authentication for the zone.  A non-NULL autoTunnelPrefix means this is an AutoTunnel domain,
244447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// and the value is prepended to the IPSec identifier (used for key lookup)
244547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
244647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus mDNS_SetSecretForDomain(mDNS *m, DomainAuthInfo *info,
244747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	const domainname *domain, const domainname *keyname, const char *b64keydata, const domainname *hostname, mDNSIPPort *port, const char *autoTunnelPrefix);
244847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
244947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void RecreateNATMappings(mDNS *const m);
245047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
245147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Hostname/Unicast Interface Configuration
245247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
245347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// All hostnames advertised point to one IPv4 address and/or one IPv6 address, set via SetPrimaryInterfaceInfo.  Invoking this routine
245447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// updates all existing hostnames to point to the new address.
245547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
245647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// A hostname is added via AddDynDNSHostName, which points to the primary interface's v4 and/or v6 addresss
245747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
245847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// The status callback is invoked to convey success or failure codes - the callback should not modify the AuthRecord or free memory.
245947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Added hostnames may be removed (deregistered) via mDNS_RemoveDynDNSHostName.
246047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
246147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Host domains added prior to specification of the primary interface address and computer name will be deferred until
246247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// these values are initialized.
246347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
246447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// DNS servers used to resolve unicast queries are specified by mDNS_AddDNSServer.
246547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// For "split" DNS configurations, in which queries for different domains are sent to different servers (e.g. VPN and external),
246647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// a domain may be associated with a DNS server.  For standard configurations, specify the root label (".") or NULL.
246747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
246847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void mDNS_AddDynDNSHostName(mDNS *m, const domainname *fqdn, mDNSRecordCallback *StatusCallback, const void *StatusContext);
246947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void mDNS_RemoveDynDNSHostName(mDNS *m, const domainname *fqdn);
247047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void mDNS_SetPrimaryInterfaceInfo(mDNS *m, const mDNSAddr *v4addr,  const mDNSAddr *v6addr, const mDNSAddr *router);
247147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern DNSServer *mDNS_AddDNSServer(mDNS *const m, const domainname *d, const mDNSInterfaceID interface, const mDNSAddr *addr, const mDNSIPPort port, mDNSBool scoped, mDNSu32 timeout);
247247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void PenalizeDNSServer(mDNS *const m, DNSQuestion *q);
247347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void mDNS_AddSearchDomain(const domainname *const domain, mDNSInterfaceID InterfaceID);
247447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
247547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern McastResolver *mDNS_AddMcastResolver(mDNS *const m, const domainname *d, const mDNSInterfaceID interface, mDNSu32 timeout);
247647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
247747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// We use ((void *)0) here instead of mDNSNULL to avoid compile warnings on gcc 4.2
247847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNS_AddSearchDomain_CString(X, I) \
247947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	do { domainname d__; if (((X) != (void*)0) && MakeDomainNameFromDNSNameString(&d__, (X)) && d__.c[0]) mDNS_AddSearchDomain(&d__, I); } while(0)
248047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
248147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Routines called by the core, exported by DNSDigest.c
248247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
248347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Convert an arbitrary base64 encoded key key into an HMAC key (stored in AuthInfo struct)
248447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSs32 DNSDigest_ConstructHMACKeyfromBase64(DomainAuthInfo *info, const char *b64key);
248547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
248647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// sign a DNS message.  The message must be complete, with all values in network byte order.  end points to the end
248747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// of the message, and is modified by this routine.  numAdditionals is a pointer to the number of additional
248847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// records in HOST byte order, which is incremented upon successful completion of this routine.  The function returns
248947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// the new end pointer on success, and NULL on failure.
249047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void DNSDigest_SignMessage(DNSMessage *msg, mDNSu8 **end, DomainAuthInfo *info, mDNSu16 tcode);
249147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
249247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define SwapDNSHeaderBytes(M) do { \
249347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt    (M)->h.numQuestions   = (mDNSu16)((mDNSu8 *)&(M)->h.numQuestions  )[0] << 8 | ((mDNSu8 *)&(M)->h.numQuestions  )[1]; \
249447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt    (M)->h.numAnswers     = (mDNSu16)((mDNSu8 *)&(M)->h.numAnswers    )[0] << 8 | ((mDNSu8 *)&(M)->h.numAnswers    )[1]; \
249547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt    (M)->h.numAuthorities = (mDNSu16)((mDNSu8 *)&(M)->h.numAuthorities)[0] << 8 | ((mDNSu8 *)&(M)->h.numAuthorities)[1]; \
249647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt    (M)->h.numAdditionals = (mDNSu16)((mDNSu8 *)&(M)->h.numAdditionals)[0] << 8 | ((mDNSu8 *)&(M)->h.numAdditionals)[1]; \
249747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt    } while (0)
249847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
249947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define DNSDigest_SignMessageHostByteOrder(M,E,INFO) \
250047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	do { SwapDNSHeaderBytes(M); DNSDigest_SignMessage((M), (E), (INFO), 0); SwapDNSHeaderBytes(M); } while (0)
250147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
250247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// verify a DNS message.  The message must be complete, with all values in network byte order.  end points to the
250347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// end of the record.  tsig is a pointer to the resource record that contains the TSIG OPT record.  info is
250447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// the matching key to use for verifying the message.  This function expects that the additionals member
250547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// of the DNS message header has already had one subtracted from it.
250647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSBool DNSDigest_VerifyMessage(DNSMessage *msg, mDNSu8 *end, LargeCacheRecord *tsig, DomainAuthInfo *info, mDNSu16 *rcode, mDNSu16 *tcode);
250747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
250847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ***************************************************************************
250947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if 0
251047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark -
251147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark - PlatformSupport interface
251247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
251347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
251447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// This section defines the interface to the Platform Support layer.
251547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Normal client code should not use any of types defined here, or directly call any of the functions defined here.
251647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// The definitions are placed here because sometimes clients do use these calls indirectly, via other supported client operations.
251747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// For example, AssignDomainName is a macro defined using mDNSPlatformMemCopy()
251847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
251947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Every platform support module must provide the following functions.
252047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNSPlatformInit() typically opens a communication endpoint, and starts listening for mDNS packets.
252147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// When Setup is complete, the platform support layer calls mDNSCoreInitComplete().
252247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNSPlatformSendUDP() sends one UDP packet
252347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// When a packet is received, the PlatformSupport code calls mDNSCoreReceive()
252447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNSPlatformClose() tidies up on exit
252547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
252647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Note: mDNSPlatformMemAllocate/mDNSPlatformMemFree are only required for handling oversized resource records and unicast DNS.
252747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// If your target platform has a well-defined specialized application, and you know that all the records it uses
252847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// are InlineCacheRDSize or less, then you can just make a simple mDNSPlatformMemAllocate() stub that always returns
252947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// NULL. InlineCacheRDSize is a compile-time constant, which is set by default to 68. If you need to handle records
253047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// a little larger than this and you don't want to have to implement run-time allocation and freeing, then you
253147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// can raise the value of this constant to a suitable value (at the expense of increased memory usage).
253247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
253347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// USE CAUTION WHEN CALLING mDNSPlatformRawTime: The m->timenow_adjust correction factor needs to be added
253447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Generally speaking:
253547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Code that's protected by the main mDNS lock should just use the m->timenow value
253647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Code outside the main mDNS lock should use mDNS_TimeNow(m) to get properly adjusted time
253747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// In certain cases there may be reasons why it's necessary to get the time without taking the lock first
253847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// (e.g. inside the routines that are doing the locking and unlocking, where a call to get the lock would result in a
253947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// recursive loop); in these cases use mDNS_TimeNow_NoLock(m) to get mDNSPlatformRawTime with the proper correction factor added.
254047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
254147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNSPlatformUTC returns the time, in seconds, since Jan 1st 1970 UTC and is required for generating TSIG records
254247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
254347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus  mDNSPlatformInit        (mDNS *const m);
254447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void     mDNSPlatformClose       (mDNS *const m);
254547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus  mDNSPlatformSendUDP(const mDNS *const m, const void *const msg, const mDNSu8 *const end,
254647e4cebad7397422144bb03a21f3f7682c062c4aRobert GreenwaltmDNSInterfaceID InterfaceID, UDPSocket *src, const mDNSAddr *dst, mDNSIPPort dstport);
254747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
254847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void     mDNSPlatformLock        (const mDNS *const m);
254947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void     mDNSPlatformUnlock      (const mDNS *const m);
255047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
255147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void     mDNSPlatformStrCopy     (      void *dst, const void *src);
255247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSu32  mDNSPlatformStrLen      (                 const void *src);
255347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void     mDNSPlatformMemCopy     (      void *dst, const void *src, mDNSu32 len);
255447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSBool mDNSPlatformMemSame     (const void *dst, const void *src, mDNSu32 len);
255547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void     mDNSPlatformMemZero     (      void *dst,                  mDNSu32 len);
255647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if APPLE_OSX_mDNSResponder && MACOSX_MDNS_MALLOC_DEBUGGING
255747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define         mDNSPlatformMemAllocate(X) mallocL(#X, X)
255847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#else
255947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void *   mDNSPlatformMemAllocate (mDNSu32 len);
256047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
256147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void     mDNSPlatformMemFree     (void *mem);
256247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
256347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// If the platform doesn't have a strong PRNG, we define a naive multiply-and-add based on a seed
256447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// from the platform layer.  Long-term, we should embed an arc4 implementation, but the strength
256547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// will still depend on the randomness of the seed.
256647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if !defined(_PLATFORM_HAS_STRONG_PRNG_) && (_BUILDING_XCODE_PROJECT_ || defined(_WIN32))
256747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define _PLATFORM_HAS_STRONG_PRNG_ 1
256847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
256947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if _PLATFORM_HAS_STRONG_PRNG_
257047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSu32  mDNSPlatformRandomNumber(void);
257147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#else
257247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSu32  mDNSPlatformRandomSeed  (void);
257347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif // _PLATFORM_HAS_STRONG_PRNG_
257447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
257547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus  mDNSPlatformTimeInit    (void);
257647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSs32  mDNSPlatformRawTime     (void);
257747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSs32  mDNSPlatformUTC         (void);
257847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNS_TimeNow_NoLock(m) (mDNSPlatformRawTime() + (m)->timenow_adjust)
257947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
258047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if MDNS_DEBUGMSGS
258147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void	mDNSPlatformWriteDebugMsg(const char *msg);
258247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
258347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void	mDNSPlatformWriteLogMsg(const char *ident, const char *msg, mDNSLogLevel_t loglevel);
258447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
258547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if APPLE_OSX_mDNSResponder
258647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Utility function for ASL logging
258747e4cebad7397422144bb03a21f3f7682c062c4aRobert GreenwaltmDNSexport void mDNSASLLog(uuid_t *uuid, const char *subdomain, const char *result, const char *signature, const char *fmt, ...);
258847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
258947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
259047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Platform support modules should provide the following functions to map between opaque interface IDs
259147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// and interface indexes in order to support the DNS-SD API. If your target platform does not support
259247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// multiple interfaces and/or does not support the DNS-SD API, these functions can be empty.
259347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSInterfaceID mDNSPlatformInterfaceIDfromInterfaceIndex(mDNS *const m, mDNSu32 ifindex);
259447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSu32 mDNSPlatformInterfaceIndexfromInterfaceID(mDNS *const m, mDNSInterfaceID id, mDNSBool suppressNetworkChange);
259547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
259647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Every platform support module must provide the following functions if it is to support unicast DNS
259747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// and Dynamic Update.
259847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// All TCP socket operations implemented by the platform layer MUST NOT BLOCK.
259947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNSPlatformTCPConnect initiates a TCP connection with a peer, adding the socket descriptor to the
260047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// main event loop.  The return value indicates whether the connection succeeded, failed, or is pending
260147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// (i.e. the call would block.)  On return, the descriptor parameter is set to point to the connected socket.
260247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// The TCPConnectionCallback is subsequently invoked when the connection
260347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// completes (in which case the ConnectionEstablished parameter is true), or data is available for
260447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// reading on the socket (indicated by the ConnectionEstablished parameter being false.)  If the connection
260547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// asynchronously fails, the TCPConnectionCallback should be invoked as usual, with the error being
260647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// returned in subsequent calls to PlatformReadTCP or PlatformWriteTCP.  (This allows for platforms
260747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// with limited asynchronous error detection capabilities.)  PlatformReadTCP and PlatformWriteTCP must
260847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// return the number of bytes read/written, 0 if the call would block, and -1 if an error.  PlatformReadTCP
260947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// should set the closed argument if the socket has been closed.
261047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// PlatformTCPCloseConnection must close the connection to the peer and remove the descriptor from the
261147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// event loop.  CloseConnectin may be called at any time, including in a ConnectionCallback.
261247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
261347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef enum
261447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
261547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kTCPSocketFlags_Zero   = 0,
261647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	kTCPSocketFlags_UseTLS = (1 << 0)
261747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} TCPSocketFlags;
261847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
261947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef void (*TCPConnectionCallback)(TCPSocket *sock, void *context, mDNSBool ConnectionEstablished, mStatus err);
262047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern TCPSocket *mDNSPlatformTCPSocket(mDNS *const m, TCPSocketFlags flags, mDNSIPPort *port);	// creates a TCP socket
262147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern TCPSocket *mDNSPlatformTCPAccept(TCPSocketFlags flags, int sd);
262247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern int        mDNSPlatformTCPGetFD(TCPSocket *sock);
262347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus    mDNSPlatformTCPConnect(TCPSocket *sock, const mDNSAddr *dst, mDNSOpaque16 dstport, domainname *hostname,
262447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt										mDNSInterfaceID InterfaceID, TCPConnectionCallback callback, void *context);
262547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void       mDNSPlatformTCPCloseConnection(TCPSocket *sock);
262647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern long       mDNSPlatformReadTCP(TCPSocket *sock, void *buf, unsigned long buflen, mDNSBool *closed);
262747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern long       mDNSPlatformWriteTCP(TCPSocket *sock, const char *msg, unsigned long len);
262847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern UDPSocket *mDNSPlatformUDPSocket(mDNS *const m, const mDNSIPPort requestedport);
262947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void       mDNSPlatformUDPClose(UDPSocket *sock);
263047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void       mDNSPlatformReceiveBPF_fd(mDNS *const m, int fd);
263147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void       mDNSPlatformUpdateProxyList(mDNS *const m, const mDNSInterfaceID InterfaceID);
263247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void       mDNSPlatformSendRawPacket(const void *const msg, const mDNSu8 *const end, mDNSInterfaceID InterfaceID);
263347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void       mDNSPlatformSetLocalAddressCacheEntry(mDNS *const m, const mDNSAddr *const tpa, const mDNSEthAddr *const tha, mDNSInterfaceID InterfaceID);
263447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void       mDNSPlatformSourceAddrForDest(mDNSAddr *const src, const mDNSAddr *const dst);
263547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
263647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNSPlatformTLSSetupCerts/mDNSPlatformTLSTearDownCerts used by dnsextd
263747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus    mDNSPlatformTLSSetupCerts(void);
263847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void       mDNSPlatformTLSTearDownCerts(void);
263947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
264047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Platforms that support unicast browsing and dynamic update registration for clients who do not specify a domain
264147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// in browse/registration calls must implement these routines to get the "default" browse/registration list.
264247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
264347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void       mDNSPlatformSetDNSConfig(mDNS *const m, mDNSBool setservers, mDNSBool setsearch, domainname *const fqdn, DNameListElem **RegDomains, DNameListElem **BrowseDomains);
264447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus    mDNSPlatformGetPrimaryInterface(mDNS *const m, mDNSAddr *v4, mDNSAddr *v6, mDNSAddr *router);
264547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void       mDNSPlatformDynDNSHostNameStatusChanged(const domainname *const dname, const mStatus status);
264647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
264747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void       mDNSPlatformSetAllowSleep(mDNS *const m, mDNSBool allowSleep, const char *reason);
264847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void       mDNSPlatformSendWakeupPacket(mDNS *const m, mDNSInterfaceID InterfaceID, char *EthAddr, char *IPAddr, int iteration);
264947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSBool   mDNSPlatformValidRecordForInterface(AuthRecord *rr, const NetworkInterfaceInfo *intf);
265047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
265147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#ifdef _LEGACY_NAT_TRAVERSAL_
265247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Support for legacy NAT traversal protocols, implemented by the platform layer and callable by the core.
265347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void     LNT_SendDiscoveryMsg(mDNS *m);
265447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void     LNT_ConfigureRouterInfo(mDNS *m, const mDNSInterfaceID InterfaceID, const mDNSu8 *const data, const mDNSu16 len);
265547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus  LNT_GetExternalAddress(mDNS *m);
265647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus  LNT_MapPort(mDNS *m, NATTraversalInfo *n);
265747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus  LNT_UnmapPort(mDNS *m, NATTraversalInfo *n);
265847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void     LNT_ClearState(mDNS *const m);
265947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif // _LEGACY_NAT_TRAVERSAL_
266047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
266147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// The core mDNS code provides these functions, for the platform support code to call at appropriate times
266247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
266347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNS_SetFQDN() is called once on startup (typically from mDNSPlatformInit())
266447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// and then again on each subsequent change of the host name.
266547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
266647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNS_RegisterInterface() is used by the platform support layer to inform mDNSCore of what
266747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// physical and/or logical interfaces are available for sending and receiving packets.
266847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Typically it is called on startup for each available interface, but register/deregister may be
266947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// called again later, on multiple occasions, to inform the core of interface configuration changes.
267047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// If set->Advertise is set non-zero, then mDNS_RegisterInterface() also registers the standard
267147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// resource records that should be associated with every publicised IP address/interface:
267247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// -- Name-to-address records (A/AAAA)
267347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// -- Address-to-name records (PTR)
267447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// -- Host information (HINFO)
267547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// IMPORTANT: The specified mDNSInterfaceID MUST NOT be 0, -1, or -2; these values have special meaning
267647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNS_RegisterInterface does not result in the registration of global hostnames via dynamic update -
267747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// see mDNS_SetPrimaryInterfaceInfo, mDNS_AddDynDNSHostName, etc. for this purpose.
267847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Note that the set may be deallocated immediately after it is deregistered via mDNS_DeegisterInterface.
267947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
268047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNS_RegisterDNS() is used by the platform support layer to provide the core with the addresses of
268147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// available domain name servers for unicast queries/updates.  RegisterDNS() should be called once for
268247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// each name server, typically at startup, or when a new name server becomes available.  DeregiterDNS()
268347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// must be called whenever a registered name server becomes unavailable.  DeregisterDNSList deregisters
268447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// all registered servers.  mDNS_DNSRegistered() returns true if one or more servers are registered in the core.
268547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
268647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNSCoreInitComplete() is called when the platform support layer is finished.
268747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Typically this is at the end of mDNSPlatformInit(), but may be later
268847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// (on platforms like OT that allow asynchronous initialization of the networking stack).
268947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
269047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNSCoreReceive() is called when a UDP packet is received
269147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
269247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mDNSCoreMachineSleep() is called when the machine sleeps or wakes
269347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// (This refers to heavyweight laptop-style sleep/wake that disables network access,
269447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// not lightweight second-by-second CPU power management modes.)
269547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
269647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void     mDNS_SetFQDN(mDNS *const m);
269747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void     mDNS_ActivateNetWake_internal  (mDNS *const m, NetworkInterfaceInfo *set);
269847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void     mDNS_DeactivateNetWake_internal(mDNS *const m, NetworkInterfaceInfo *set);
269947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus  mDNS_RegisterInterface  (mDNS *const m, NetworkInterfaceInfo *set, mDNSBool flapping);
270047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void     mDNS_DeregisterInterface(mDNS *const m, NetworkInterfaceInfo *set, mDNSBool flapping);
270147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void     mDNSCoreInitComplete(mDNS *const m, mStatus result);
270247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void     mDNSCoreReceive(mDNS *const m, void *const msg, const mDNSu8 *const end,
270347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt								const mDNSAddr *const srcaddr, const mDNSIPPort srcport,
270447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt								const mDNSAddr *dstaddr, const mDNSIPPort dstport, const mDNSInterfaceID InterfaceID);
270547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void		mDNSCoreRestartQueries(mDNS *const m);
270647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef void    (*FlushCache)(mDNS *const m);
270747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef void    (*CallbackBeforeStartQuery)(mDNS *const m, void *context);
270847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void		mDNSCoreRestartAddressQueries(mDNS *const m, mDNSBool SearchDomainsChanged, FlushCache flushCacheRecords,
270947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt											  CallbackBeforeStartQuery beforeQueryStart, void *context);
271047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSBool mDNSCoreHaveAdvertisedMulticastServices(mDNS *const m);
271147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void     mDNSCoreMachineSleep(mDNS *const m, mDNSBool wake);
271247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSBool mDNSCoreReadyForSleep(mDNS *m, mDNSs32 now);
271347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSs32  mDNSCoreIntervalToNextWake(mDNS *const m, mDNSs32 now);
271447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
271547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void     mDNSCoreReceiveRawPacket  (mDNS *const m, const mDNSu8 *const p, const mDNSu8 *const end, const mDNSInterfaceID InterfaceID);
271647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
271747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSBool mDNSAddrIsDNSMulticast(const mDNSAddr *ip);
271847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
271947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern CacheRecord *CreateNewCacheEntry(mDNS *const m, const mDNSu32 slot, CacheGroup *cg, mDNSs32 delay);
272047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void ScheduleNextCacheCheckTime(mDNS *const m, const mDNSu32 slot, const mDNSs32 event);
272147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void GrantCacheExtensions(mDNS *const m, DNSQuestion *q, mDNSu32 lease);
272247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void MakeNegativeCacheRecord(mDNS *const m, CacheRecord *const cr,
272347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	const domainname *const name, const mDNSu32 namehash, const mDNSu16 rrtype, const mDNSu16 rrclass, mDNSu32 ttl_seconds,
272447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSInterfaceID InterfaceID, DNSServer *dnsserver);
272547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void CompleteDeregistration(mDNS *const m, AuthRecord *rr);
272647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void AnswerCurrentQuestionWithResourceRecord(mDNS *const m, CacheRecord *const rr, const QC_result AddRecord);
272747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern char *InterfaceNameForID(mDNS *const m, const mDNSInterfaceID InterfaceID);
272847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void DNSServerChangeForQuestion(mDNS *const m, DNSQuestion *q, DNSServer *newServer);
272947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void ActivateUnicastRegistration(mDNS *const m, AuthRecord *const rr);
273047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void CheckSuppressUnusableQuestions(mDNS *const m);
273147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void RetrySearchDomainQuestions(mDNS *const m);
273247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
273347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Used only in logging to restrict the number of /etc/hosts entries printed
273447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void FreeEtcHosts(mDNS *const m, AuthRecord *const rr, mStatus result);
273547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// exported for using the hash for /etc/hosts AuthRecords
273647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern AuthGroup *AuthGroupForName(AuthHash *r, const mDNSu32 slot, const mDNSu32 namehash, const domainname *const name);
273747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern AuthGroup *AuthGroupForRecord(AuthHash *r, const mDNSu32 slot, const ResourceRecord *const rr);
273847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern AuthGroup *InsertAuthRecord(mDNS *const m, AuthHash *r, AuthRecord *rr);
273947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern AuthGroup *RemoveAuthRecord(mDNS *const m, AuthHash *r, AuthRecord *rr);
274047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
274147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// For now this AutoTunnel stuff is specific to Mac OS X.
274247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// In the future, if there's demand, we may see if we can abstract it out cleanly into the platform layer
274347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if APPLE_OSX_mDNSResponder
274447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void AutoTunnelCallback(mDNS *const m, DNSQuestion *question, const ResourceRecord *const answer, QC_result AddRecord);
274547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void AddNewClientTunnel(mDNS *const m, DNSQuestion *const q);
274647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void SetupLocalAutoTunnelInterface_internal(mDNS *const m, mDNSBool servicesStarting);
274747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void UpdateAutoTunnelDomainStatuses(const mDNS *const m);
274847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mStatus ActivateLocalProxy(mDNS *const m, char *ifname);
274947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void RemoveAutoTunnel6Record(mDNS *const m);
275047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern mDNSBool RecordReadyForSleep(mDNS *const m, AuthRecord *rr);
275147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
275247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
275347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ***************************************************************************
275447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if 0
275547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark -
275647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark - Sleep Proxy
275747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
275847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
275947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Sleep Proxy Server Property Encoding
276047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
276147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Sleep Proxy Servers are advertised using a structured service name, consisting of four
276247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// metrics followed by a human-readable name. The metrics assist clients in deciding which
276347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Sleep Proxy Server(s) to use when multiple are available on the network. Each metric
276447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// is a two-digit decimal number in the range 10-99. Lower metrics are generally better.
276547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
276647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//   AA-BB-CC-DD Name
276747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
276847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Metrics:
276947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
277047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// AA = Intent
277147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// BB = Portability
277247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// CC = Marginal Power
277347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// DD = Total Power
277447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
277547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
277647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ** Intent Metric **
277747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
277847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 20 = Dedicated Sleep Proxy Server -- a device, permanently powered on,
277947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      installed for the express purpose of providing Sleep Proxy Service.
278047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
278147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 30 = Primary Network Infrastructure Hardware -- a router, DHCP server, NAT gateway,
278247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      or similar permanently installed device which is permanently powered on.
278347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      This is hardware designed for the express purpose of being network
278447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      infrastructure, and for most home users is typically a single point
278547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      of failure for the local network -- e.g. most home users only have
278647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      a single NAT gateway / DHCP server. Even though in principle the
278747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      hardware might technically be capable of running different software,
278847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      a typical user is unlikely to do that. e.g. AirPort base station.
278947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
279047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 40 = Primary Network Infrastructure Software -- a general-purpose computer
279147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      (e.g. Mac, Windows, Linux, etc.) which is currently running DHCP server
279247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      or NAT gateway software, but the user could choose to turn that off
279347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      fairly easily. e.g. iMac running Internet Sharing
279447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
279547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 50 = Secondary Network Infrastructure Hardware -- like primary infrastructure
279647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      hardware, except not a single point of failure for the entire local network.
279747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      For example, an AirPort base station in bridge mode. This may have clients
279847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      associated with it, and if it goes away those clients will be inconvenienced,
279947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      but unlike the NAT gateway / DHCP server, the entire local network is not
280047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      dependent on it.
280147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
280247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 60 = Secondary Network Infrastructure Software -- like 50, but in a general-
280347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      purpose CPU.
280447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
280547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 70 = Incidentally Available Hardware -- a device which has no power switch
280647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      and is generally left powered on all the time. Even though it is not a
280747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      part of what we conventionally consider network infrastructure (router,
280847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      DHCP, NAT, DNS, etc.), and the rest of the network can operate fine
280947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      without it, since it's available and unlikely to be turned off, it is a
281047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      reasonable candidate for providing Sleep Proxy Service e.g. Apple TV,
281147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      or an AirPort base station in client mode, associated with an existing
281247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      wireless network (e.g. AirPort Express connected to a music system, or
281347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      being used to share a USB printer).
281447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
281547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 80 = Incidentally Available Software -- a general-purpose computer which
281647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      happens at this time to be set to "never sleep", and as such could be
281747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      useful as a Sleep Proxy Server, but has not been intentionally provided
281847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      for this purpose. Of all the Intent Metric categories this is the
281947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      one most likely to be shut down or put to sleep without warning.
282047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      However, if nothing else is availalable, it may be better than nothing.
282147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//      e.g. Office computer in the workplace which has been set to "never sleep"
282247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
282347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
282447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ** Portability Metric **
282547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
282647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Inversely related to mass of device, on the basis that, all other things
282747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// being equal, heavier devices are less likely to be moved than lighter devices.
282847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// E.g. A MacBook running Internet Sharing is probably more likely to be
282947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// put to sleep and taken away than a Mac Pro running Internet Sharing.
283047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// The Portability Metric is a logarithmic decibel scale, computed by taking the
283147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// (approximate) mass of the device in milligrammes, taking the base 10 logarithm
283247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// of that, multiplying by 10, and subtracting the result from 100:
283347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
283447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//   Portability Metric = 100 - (log10(mg) * 10)
283547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
283647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// The Portability Metric is not necessarily computed literally from the actual
283747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// mass of the device; the intent is just that lower numbers indicate more
283847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// permanent devices, and higher numbers indicate devices more likely to be
283947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// removed from the network, e.g., in order of increasing portability:
284047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
284147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Mac Pro < iMac < Laptop < iPhone
284247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
284347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Example values:
284447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
284547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 10 = 1 metric tonne
284647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 40 = 1kg
284747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 70 = 1g
284847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 90 = 10mg
284947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
285047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
285147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ** Marginal Power and Total Power Metrics **
285247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
285347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// The Marginal Power Metric is the power difference between sleeping and staying awake
285447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// to be a Sleep Proxy Server.
285547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
285647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// The Total Power Metric is the total power consumption when being Sleep Proxy Server.
285747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
285847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// The Power Metrics use a logarithmic decibel scale, computed as ten times the
285947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// base 10 logarithm of the (approximate) power in microwatts:
286047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
286147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//   Power Metric = log10(uW) * 10
286247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
286347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Higher values indicate higher power consumption. Example values:
286447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt//
286547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 10 =  10 uW
286647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 20 = 100 uW
286747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 30 =   1 mW
286847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 60 =   1 W
286947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// 90 =   1 kW
287047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
287147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalttypedef enum
287247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
287347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSSleepProxyMetric_Dedicated          = 20,
287447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSSleepProxyMetric_PrimaryHardware    = 30,
287547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSSleepProxyMetric_PrimarySoftware    = 40,
287647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSSleepProxyMetric_SecondaryHardware  = 50,
287747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSSleepProxyMetric_SecondarySoftware  = 60,
287847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSSleepProxyMetric_IncidentalHardware = 70,
287947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	mDNSSleepProxyMetric_IncidentalSoftware = 80
288047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	} mDNSSleepProxyMetric;
288147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
288247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void mDNSCoreBeSleepProxyServer_internal(mDNS *const m, mDNSu8 sps, mDNSu8 port, mDNSu8 marginalpower, mDNSu8 totpower);
288347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define mDNSCoreBeSleepProxyServer(M,S,P,MP,TP) \
288447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	do { mDNS_Lock(m); mDNSCoreBeSleepProxyServer_internal((M),(S),(P),(MP),(TP)); mDNS_Unlock(m); } while(0)
288547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
288647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltextern void FindSPSInCache(mDNS *const m, const DNSQuestion *const q, const CacheRecord *sps[3]);
288747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define PrototypeSPSName(X) ((X)[0] >= 11 && (X)[3] == '-' && (X)[ 4] == '9' && (X)[ 5] == '9' && \
288847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt                                             (X)[6] == '-' && (X)[ 7] == '9' && (X)[ 8] == '9' && \
288947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt                                             (X)[9] == '-' && (X)[10] == '9' && (X)[11] == '9'    )
289047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define ValidSPSName(X) ((X)[0] >= 5 && mDNSIsDigit((X)[1]) && mDNSIsDigit((X)[2]) && mDNSIsDigit((X)[4]) && mDNSIsDigit((X)[5]))
289147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define SPSMetric(X) (!ValidSPSName(X) || PrototypeSPSName(X) ? 1000000 : \
289247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	((X)[1]-'0') * 100000 + ((X)[2]-'0') * 10000 + ((X)[4]-'0') * 1000 + ((X)[5]-'0') * 100 + ((X)[7]-'0') * 10 + ((X)[8]-'0'))
289347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
289447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ***************************************************************************
289547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if 0
289647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark -
289747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#pragma mark - Compile-Time assertion checks
289847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
289947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
290047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Some C compiler cleverness. We can make the compiler check certain things for
290147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// us, and report compile-time errors if anything is wrong. The usual way to do
290247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// this would be to use a run-time "if" statement, but then you don't find out
290347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// what's wrong until you run the software. This way, if the assertion condition
290447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// is false, the array size is negative, and the complier complains immediately.
290547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
290647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltstruct CompileTimeAssertionChecks_mDNS
290747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	{
290847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Check that the compiler generated our on-the-wire packet format structure definitions
290947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// properly packed, without adding padding bytes to align fields on 32-bit or 64-bit boundaries.
291047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char assert0[(sizeof(rdataSRV)         == 262                          ) ? 1 : -1];
291147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char assert1[(sizeof(DNSMessageHeader) ==  12                          ) ? 1 : -1];
291247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char assert2[(sizeof(DNSMessage)       ==  12+AbsoluteMaxDNSMessageData) ? 1 : -1];
291347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char assert3[(sizeof(mDNSs8)           ==   1                          ) ? 1 : -1];
291447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char assert4[(sizeof(mDNSu8)           ==   1                          ) ? 1 : -1];
291547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char assert5[(sizeof(mDNSs16)          ==   2                          ) ? 1 : -1];
291647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char assert6[(sizeof(mDNSu16)          ==   2                          ) ? 1 : -1];
291747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char assert7[(sizeof(mDNSs32)          ==   4                          ) ? 1 : -1];
291847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char assert8[(sizeof(mDNSu32)          ==   4                          ) ? 1 : -1];
291947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char assert9[(sizeof(mDNSOpaque16)     ==   2                          ) ? 1 : -1];
292047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char assertA[(sizeof(mDNSOpaque32)     ==   4                          ) ? 1 : -1];
292147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char assertB[(sizeof(mDNSOpaque128)    ==  16                          ) ? 1 : -1];
292247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char assertC[(sizeof(CacheRecord  )    ==  sizeof(CacheGroup)          ) ? 1 : -1];
292347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char assertD[(sizeof(int)              >=  4                           ) ? 1 : -1];
292447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char assertE[(StandardAuthRDSize       >=  256                         ) ? 1 : -1];
292547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char assertF[(sizeof(EthernetHeader)   ==   14                         ) ? 1 : -1];
292647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char assertG[(sizeof(ARP_EthIP     )   ==   28                         ) ? 1 : -1];
292747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char assertH[(sizeof(IPv4Header    )   ==   20                         ) ? 1 : -1];
292847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char assertI[(sizeof(IPv6Header    )   ==   40                         ) ? 1 : -1];
292947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char assertJ[(sizeof(IPv6NDP       )   ==   24                         ) ? 1 : -1];
293047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char assertK[(sizeof(UDPHeader     )   ==    8                         ) ? 1 : -1];
293147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char assertL[(sizeof(IKEHeader     )   ==   28                         ) ? 1 : -1];
293247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char assertM[(sizeof(TCPHeader     )   ==   20                         ) ? 1 : -1];
293347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
293447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// Check our structures are reasonable sizes. Including overly-large buffers, or embedding
293547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// other overly-large structures instead of having a pointer to them, can inadvertently
293647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	// cause structure sizes (and therefore memory usage) to balloon unreasonably.
293747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char sizecheck_RDataBody           [(sizeof(RDataBody)            ==   264) ? 1 : -1];
293847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char sizecheck_ResourceRecord      [(sizeof(ResourceRecord)       <=    64) ? 1 : -1];
293947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char sizecheck_AuthRecord          [(sizeof(AuthRecord)           <=  1208) ? 1 : -1];
294047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char sizecheck_CacheRecord         [(sizeof(CacheRecord)          <=   184) ? 1 : -1];
294147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char sizecheck_CacheGroup          [(sizeof(CacheGroup)           <=   184) ? 1 : -1];
294247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char sizecheck_DNSQuestion         [(sizeof(DNSQuestion)          <=   786) ? 1 : -1];
294347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char sizecheck_ZoneData            [(sizeof(ZoneData)             <=  1624) ? 1 : -1];
294447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char sizecheck_NATTraversalInfo    [(sizeof(NATTraversalInfo)     <=   192) ? 1 : -1];
294547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char sizecheck_HostnameInfo        [(sizeof(HostnameInfo)         <=  3050) ? 1 : -1];
294647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char sizecheck_DNSServer           [(sizeof(DNSServer)            <=   320) ? 1 : -1];
294747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char sizecheck_NetworkInterfaceInfo[(sizeof(NetworkInterfaceInfo) <=  6850) ? 1 : -1];
294847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char sizecheck_ServiceRecordSet    [(sizeof(ServiceRecordSet)     <=  5500) ? 1 : -1];
294947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char sizecheck_DomainAuthInfo      [(sizeof(DomainAuthInfo)       <=  7808) ? 1 : -1];
295047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char sizecheck_ServiceInfoQuery    [(sizeof(ServiceInfoQuery)     <=  3200) ? 1 : -1];
295147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#if APPLE_OSX_mDNSResponder
295247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	char sizecheck_ClientTunnel        [(sizeof(ClientTunnel)         <=  1148) ? 1 : -1];
295347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
295447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	};
295547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
295647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// ***************************************************************************
295747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
295847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#ifdef __cplusplus
295947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt	}
296047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
296147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
296247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#endif
2963