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