1/* -*- Mode: C; tab-width: 4 -*-
2 *
3 * Copyright (c) 2002-2013 Apple Computer, Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef __UDNS_H_
19#define __UDNS_H_
20
21#include "mDNSEmbeddedAPI.h"
22#include "DNSCommon.h"
23
24#ifdef  __cplusplus
25extern "C" {
26#endif
27
28#define RESTART_GOODBYE_DELAY    (6 * mDNSPlatformOneSecond) // delay after restarting LLQ before nuking previous known answers (avoids flutter if we restart before we have networking up)
29#define INIT_UCAST_POLL_INTERVAL (3 * mDNSPlatformOneSecond) // this interval is used after send failures on network transitions
30                                                             // which typically heal quickly, so we start agressively and exponentially back off
31#define MAX_UCAST_POLL_INTERVAL (60 * 60 * mDNSPlatformOneSecond)
32//#define MAX_UCAST_POLL_INTERVAL (1 * 60 * mDNSPlatformOneSecond)
33#define LLQ_POLL_INTERVAL       (15 * 60 * mDNSPlatformOneSecond) // Polling interval for zones w/ an advertised LLQ port (ie not static zones) if LLQ fails due to NAT, etc.
34#define RESPONSE_WINDOW (60 * mDNSPlatformOneSecond)         // require server responses within one minute of request
35#define MAX_DNSSEC_UNANSWERED_QUERIES 1                      // number of unanswered queries from any one uDNS server before turning off DNSSEC Validation
36#define MAX_UCAST_UNANSWERED_QUERIES 2                       // number of unanswered queries from any one uDNS server before trying another server
37#define DNSSERVER_PENALTY_TIME (60 * mDNSPlatformOneSecond)  // number of seconds for which new questions don't pick this server
38
39// On some interfaces, we want to delay the first retransmission to a minimum of 2 seconds
40// rather than the default (1 second).
41#define MIN_UCAST_RETRANS_TIMEOUT (2 * mDNSPlatformOneSecond)
42
43#define DEFAULT_UPDATE_LEASE 7200
44
45#define QuestionIntervalStep 3
46#define QuestionIntervalStep2 (QuestionIntervalStep*QuestionIntervalStep)
47#define QuestionIntervalStep3 (QuestionIntervalStep*QuestionIntervalStep*QuestionIntervalStep)
48#define InitialQuestionInterval ((mDNSPlatformOneSecond + QuestionIntervalStep-1) / QuestionIntervalStep)
49#define MaxQuestionInterval         (3600 * mDNSPlatformOneSecond)
50#define UDNSBackOffMultiplier 2
51#define MinQuestionInterval (1 * mDNSPlatformOneSecond)
52
53// For Unicast record registrations, we initialize the interval to 1 second. When we send any query for
54// the record registration e.g., GetZoneData, we always back off by QuestionIntervalStep
55// so that the first retry does not happen until 3 seconds which should be enough for TCP/TLS to be done.
56#define INIT_RECORD_REG_INTERVAL (1 * mDNSPlatformOneSecond)
57#define MAX_RECORD_REG_INTERVAL (15 * 60 * mDNSPlatformOneSecond)
58#define MERGE_DELAY_TIME    (1 * mDNSPlatformOneSecond)
59
60// If we are refreshing, we do it at least 5 times with a min update frequency of
61// 5 minutes
62#define MAX_UPDATE_REFRESH_COUNT    5
63#define MIN_UPDATE_REFRESH_TIME     (5 * 60 * mDNSPlatformOneSecond)
64
65// For questions that use kDNSServiceFlagsTimeout and we don't have a matching resolver e.g., no dns servers,
66// then use the default value of 30 seconds
67#define DEFAULT_UDNS_TIMEOUT    30 // in seconds
68
69// For questions that are validating responses (q->ValidatingResponse == 1), use 10 seconds
70// which accomodates two DNS servers and two queries per DNS server.
71#define DEFAULT_UDNSSEC_TIMEOUT    10 // in seconds
72
73// If we are sending queries with EDNS0/DO option and we have no indications that the server
74// is DNSSEC aware and we have already reached MAX_DNSSEC_RETRANSMISSIONS, we disable
75// validation (for optional case only) for any questions that uses this server
76#define MAX_DNSSEC_RETRANSMISSIONS 3
77
78// Entry points into unicast-specific routines
79
80extern void LLQGotZoneData(mDNS *const m, mStatus err, const ZoneData *zoneInfo);
81extern void startLLQHandshake(mDNS *m, DNSQuestion *q);
82extern void sendLLQRefresh(mDNS *m, DNSQuestion *q);
83
84extern void DNSPushNotificationGotZoneData(mDNS *const m, mStatus err, const ZoneData *zoneInfo);
85extern void DiscoverDNSPushNotificationServer(mDNS *m, DNSQuestion *q);
86extern void SubscribeToDNSPushNotificationServer(mDNS *m, DNSQuestion *q);
87extern void UnSubscribeToDNSPushNotificationServer(mDNS *m, DNSQuestion *q);
88
89extern void SleepRecordRegistrations(mDNS *m);
90
91// uDNS_UpdateRecord
92// following fields must be set, and the update validated, upon entry.
93// rr->NewRData
94// rr->newrdlength
95// rr->UpdateCallback
96
97extern mStatus uDNS_UpdateRecord(mDNS *m, AuthRecord *rr);
98
99extern void SetNextQueryTime(mDNS *const m, const DNSQuestion *const q);
100extern mStatus mDNS_Register_internal(mDNS *const m, AuthRecord *const rr);
101extern mStatus mDNS_Deregister_internal(mDNS *const m, AuthRecord *const rr, mDNS_Dereg_type drt);
102extern mStatus mDNS_StartQuery_internal(mDNS *const m, DNSQuestion *const question);
103extern mStatus mDNS_StopQuery_internal(mDNS *const m, DNSQuestion *const question);
104extern mStatus mDNS_StartNATOperation_internal(mDNS *const m, NATTraversalInfo *traversal);
105
106extern void RecordRegistrationGotZoneData(mDNS *const m, mStatus err, const ZoneData *zoneData);
107extern mStatus uDNS_DeregisterRecord(mDNS *const m, AuthRecord *const rr);
108extern const domainname *GetServiceTarget(mDNS *m, AuthRecord *const rr);
109extern void uDNS_CheckCurrentQuestion(mDNS *const m);
110
111// integer fields of msg header must be in HOST byte order before calling this routine
112extern void uDNS_ReceiveMsg(mDNS *const m, DNSMessage *const msg, const mDNSu8 *const end,
113                            const mDNSAddr *const srcaddr, const mDNSIPPort srcport);
114
115extern void uDNS_Tasks(mDNS *const m);
116extern void UpdateAllSRVRecords(mDNS *m);
117extern void CheckNATMappings(mDNS *m);
118
119extern mStatus         uDNS_SetupDNSConfig(mDNS *const m);
120
121// uDNS_SetupWABQueries reads search domains from the platform layer and starts the Wide Area Bonjour
122// (WAB) domain enumeration queries if necessary.
123
124#define UDNS_WAB_BROWSE_QUERY    0x00000001 // Browse queries (b, db)
125#define UDNS_WAB_LBROWSE_QUERY   0x00000002 // Browse queries (lb)
126#define UDNS_WAB_REG_QUERY       0x00000004 // Registration queries (r and dr)
127
128extern void uDNS_SetupWABQueries(mDNS *const m);
129extern void uDNS_StartWABQueries(mDNS *const m, int queryType);
130extern void uDNS_StopWABQueries(mDNS *const m, int queryType);
131extern domainname      *uDNS_GetNextSearchDomain(mDNSInterfaceID InterfaceID, mDNSs8 *searchIndex, mDNSBool ignoreDotLocal);
132
133extern void uDNS_RestartQuestionAsTCP(mDNS *m, DNSQuestion *const q, const mDNSAddr *const srcaddr, const mDNSIPPort srcport);
134
135typedef enum
136{
137    uDNS_LLQ_Not = 0,   // Normal uDNS answer: Flush any stale records from cache, and respect record TTL
138    uDNS_LLQ_Ignore,    // LLQ initial challenge packet: ignore -- has no useful records for us
139    uDNS_LLQ_Entire,    // LLQ initial set of answers: Flush any stale records from cache, but assume TTL is 2 x LLQ refresh interval
140    uDNS_LLQ_Events     // LLQ event packet: don't flush cache; assume TTL is 2 x LLQ refresh interval
141} uDNS_LLQType;
142
143extern uDNS_LLQType    uDNS_recvLLQResponse(mDNS *const m, const DNSMessage *const msg, const mDNSu8 *const end, const mDNSAddr *const srcaddr, const mDNSIPPort srcport, DNSQuestion **matchQuestion);
144extern DomainAuthInfo *GetAuthInfoForName_internal(mDNS *m, const domainname *const name);
145extern DomainAuthInfo *GetAuthInfoForQuestion(mDNS *m, const DNSQuestion *const q);
146extern void DisposeTCPConn(struct tcpInfo_t *tcp);
147
148// NAT traversal
149extern void uDNS_ReceiveNATPacket(mDNS *m, const mDNSInterfaceID InterfaceID, mDNSu8 *pkt, mDNSu16 len); // Called for each received PCP or NAT-PMP packet
150extern void natTraversalHandleAddressReply(mDNS *const m, mDNSu16 err, mDNSv4Addr ExtAddr);
151extern void natTraversalHandlePortMapReply(mDNS *const m, NATTraversalInfo *n, const mDNSInterfaceID InterfaceID, mDNSu16 err, mDNSIPPort extport, mDNSu32 lease, NATTProtocol protocol);
152
153// DNS Push Notification
154extern void SubscribeToDNSPushNotification(mDNS *m, DNSQuestion *q);
155
156
157#ifdef  __cplusplus
158}
159#endif
160
161#endif // __UDNS_H
162