1*c65ebfc7SToomas Soome /* -*- Mode: C; tab-width: 4 -*-
2*c65ebfc7SToomas Soome  *
3*c65ebfc7SToomas Soome  * Copyright (c) 2002-2013 Apple Inc. All rights reserved.
4*c65ebfc7SToomas Soome  *
5*c65ebfc7SToomas Soome  * Licensed under the Apache License, Version 2.0 (the "License");
6*c65ebfc7SToomas Soome  * you may not use this file except in compliance with the License.
7*c65ebfc7SToomas Soome  * You may obtain a copy of the License at
8*c65ebfc7SToomas Soome  *
9*c65ebfc7SToomas Soome  *     http://www.apache.org/licenses/LICENSE-2.0
10*c65ebfc7SToomas Soome  *
11*c65ebfc7SToomas Soome  * Unless required by applicable law or agreed to in writing, software
12*c65ebfc7SToomas Soome  * distributed under the License is distributed on an "AS IS" BASIS,
13*c65ebfc7SToomas Soome  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*c65ebfc7SToomas Soome  * See the License for the specific language governing permissions and
15*c65ebfc7SToomas Soome  * limitations under the License.
16*c65ebfc7SToomas Soome  */
17*c65ebfc7SToomas Soome 
18*c65ebfc7SToomas Soome #ifndef UDS_DAEMON_H
19*c65ebfc7SToomas Soome #define UDS_DAEMON_H
20*c65ebfc7SToomas Soome 
21*c65ebfc7SToomas Soome #include "mDNSEmbeddedAPI.h"
22*c65ebfc7SToomas Soome #include "dnssd_ipc.h"
23*c65ebfc7SToomas Soome 
24*c65ebfc7SToomas Soome /* Client request: */
25*c65ebfc7SToomas Soome 
26*c65ebfc7SToomas Soome // ***************************************************************************
27*c65ebfc7SToomas Soome #if COMPILER_LIKES_PRAGMA_MARK
28*c65ebfc7SToomas Soome #pragma mark -
29*c65ebfc7SToomas Soome #pragma mark - Types and Data Structures
30*c65ebfc7SToomas Soome #endif
31*c65ebfc7SToomas Soome 
32*c65ebfc7SToomas Soome typedef enum
33*c65ebfc7SToomas Soome {
34*c65ebfc7SToomas Soome 	t_uninitialized,
35*c65ebfc7SToomas Soome 	t_morecoming,
36*c65ebfc7SToomas Soome 	t_complete,
37*c65ebfc7SToomas Soome 	t_error,
38*c65ebfc7SToomas Soome 	t_terminated
39*c65ebfc7SToomas Soome } transfer_state;
40*c65ebfc7SToomas Soome 
41*c65ebfc7SToomas Soome typedef struct request_state request_state;
42*c65ebfc7SToomas Soome 
43*c65ebfc7SToomas Soome typedef void (*req_termination_fn)(request_state *request);
44*c65ebfc7SToomas Soome 
45*c65ebfc7SToomas Soome typedef struct registered_record_entry
46*c65ebfc7SToomas Soome {
47*c65ebfc7SToomas Soome 	struct registered_record_entry *next;
48*c65ebfc7SToomas Soome 	mDNSu32 key;
49*c65ebfc7SToomas Soome 	client_context_t regrec_client_context;
50*c65ebfc7SToomas Soome 	request_state *request;
51*c65ebfc7SToomas Soome 	mDNSBool external_advertise;
52*c65ebfc7SToomas Soome 	mDNSInterfaceID origInterfaceID;
53*c65ebfc7SToomas Soome 	AuthRecord *rr;             // Pointer to variable-sized AuthRecord (Why a pointer? Why not just embed it here?)
54*c65ebfc7SToomas Soome } registered_record_entry;
55*c65ebfc7SToomas Soome 
56*c65ebfc7SToomas Soome // A single registered service: ServiceRecordSet + bookkeeping
57*c65ebfc7SToomas Soome // Note that we duplicate some fields from parent service_info object
58*c65ebfc7SToomas Soome // to facilitate cleanup, when instances and parent may be deallocated at different times.
59*c65ebfc7SToomas Soome typedef struct service_instance
60*c65ebfc7SToomas Soome {
61*c65ebfc7SToomas Soome 	struct service_instance *next;
62*c65ebfc7SToomas Soome 	request_state *request;
63*c65ebfc7SToomas Soome 	AuthRecord *subtypes;
64*c65ebfc7SToomas Soome 	mDNSBool renameonmemfree;       // Set on config change when we deregister original name
65*c65ebfc7SToomas Soome 	mDNSBool clientnotified;        // Has client been notified of successful registration yet?
66*c65ebfc7SToomas Soome 	mDNSBool default_local;         // is this the "local." from an empty-string registration?
67*c65ebfc7SToomas Soome 	mDNSBool external_advertise;    // is this is being advertised externally?
68*c65ebfc7SToomas Soome 	domainname domain;
69*c65ebfc7SToomas Soome 	ServiceRecordSet srs;           // note -- variable-sized object -- must be last field in struct
70*c65ebfc7SToomas Soome } service_instance;
71*c65ebfc7SToomas Soome 
72*c65ebfc7SToomas Soome // for multi-domain default browsing
73*c65ebfc7SToomas Soome typedef struct browser_t
74*c65ebfc7SToomas Soome {
75*c65ebfc7SToomas Soome 	struct browser_t *next;
76*c65ebfc7SToomas Soome 	domainname domain;
77*c65ebfc7SToomas Soome 	DNSQuestion q;
78*c65ebfc7SToomas Soome } browser_t;
79*c65ebfc7SToomas Soome 
80*c65ebfc7SToomas Soome #ifdef _WIN32
81*c65ebfc7SToomas Soome typedef unsigned int pid_t;
82*c65ebfc7SToomas Soome typedef unsigned int socklen_t;
83*c65ebfc7SToomas Soome #endif
84*c65ebfc7SToomas Soome 
85*c65ebfc7SToomas Soome #if (!defined(MAXCOMLEN))
86*c65ebfc7SToomas Soome #define MAXCOMLEN 16
87*c65ebfc7SToomas Soome #endif
88*c65ebfc7SToomas Soome 
89*c65ebfc7SToomas Soome struct request_state
90*c65ebfc7SToomas Soome {
91*c65ebfc7SToomas Soome 	request_state *next;
92*c65ebfc7SToomas Soome 	request_state *primary;         // If this operation is on a shared socket, pointer to primary
93*c65ebfc7SToomas Soome 	// request_state for the original DNSServiceCreateConnection() operation
94*c65ebfc7SToomas Soome 	dnssd_sock_t sd;
95*c65ebfc7SToomas Soome 	pid_t process_id;               // Client's PID value
96*c65ebfc7SToomas Soome 	char  pid_name[MAXCOMLEN];      // Client's process name
97*c65ebfc7SToomas Soome 	mDNSu8 uuid[UUID_SIZE];
98*c65ebfc7SToomas Soome 	mDNSBool validUUID;
99*c65ebfc7SToomas Soome 	dnssd_sock_t errsd;
100*c65ebfc7SToomas Soome 	mDNSu32 uid;
101*c65ebfc7SToomas Soome 	void * platform_data;
102*c65ebfc7SToomas Soome 
103*c65ebfc7SToomas Soome 	// Note: On a shared connection these fields in the primary structure, including hdr, are re-used
104*c65ebfc7SToomas Soome 	// for each new request. This is because, until we've read the ipc_msg_hdr to find out what the
105*c65ebfc7SToomas Soome 	// operation is, we don't know if we're going to need to allocate a new request_state or not.
106*c65ebfc7SToomas Soome 	transfer_state ts;
107*c65ebfc7SToomas Soome 	mDNSu32 hdr_bytes;              // bytes of header already read
108*c65ebfc7SToomas Soome 	ipc_msg_hdr hdr;
109*c65ebfc7SToomas Soome 	mDNSu32 data_bytes;             // bytes of message data already read
110*c65ebfc7SToomas Soome 	char          *msgbuf;          // pointer to data storage to pass to free()
111*c65ebfc7SToomas Soome 	const char    *msgptr;          // pointer to data to be read from (may be modified)
112*c65ebfc7SToomas Soome 	char          *msgend;          // pointer to byte after last byte of message
113*c65ebfc7SToomas Soome 
114*c65ebfc7SToomas Soome 	// reply, termination, error, and client context info
115*c65ebfc7SToomas Soome 	int no_reply;                   // don't send asynchronous replies to client
116*c65ebfc7SToomas Soome 	mDNSs32 time_blocked;           // record time of a blocked client
117*c65ebfc7SToomas Soome 	int unresponsiveness_reports;
118*c65ebfc7SToomas Soome 	struct reply_state *replies;    // corresponding (active) reply list
119*c65ebfc7SToomas Soome 	req_termination_fn terminate;
120*c65ebfc7SToomas Soome 	DNSServiceFlags flags;
121*c65ebfc7SToomas Soome 	mDNSu32 interfaceIndex;
122*c65ebfc7SToomas Soome 
123*c65ebfc7SToomas Soome 	union
124*c65ebfc7SToomas Soome 	{
125*c65ebfc7SToomas Soome 		registered_record_entry *reg_recs;  // list of registrations for a connection-oriented request
126*c65ebfc7SToomas Soome 		struct
127*c65ebfc7SToomas Soome 		{
128*c65ebfc7SToomas Soome 			mDNSInterfaceID interface_id;
129*c65ebfc7SToomas Soome 			mDNSBool default_domain;
130*c65ebfc7SToomas Soome 			mDNSBool ForceMCast;
131*c65ebfc7SToomas Soome 			domainname regtype;
132*c65ebfc7SToomas Soome 			browser_t *browsers;
133*c65ebfc7SToomas Soome 			const mDNSu8 *AnonData;
134*c65ebfc7SToomas Soome 		} browser;
135*c65ebfc7SToomas Soome 		struct
136*c65ebfc7SToomas Soome 		{
137*c65ebfc7SToomas Soome 			mDNSInterfaceID InterfaceID;
138*c65ebfc7SToomas Soome 			mDNSu16 txtlen;
139*c65ebfc7SToomas Soome 			void *txtdata;
140*c65ebfc7SToomas Soome 			mDNSIPPort port;
141*c65ebfc7SToomas Soome 			domainlabel name;
142*c65ebfc7SToomas Soome 			char type_as_string[MAX_ESCAPED_DOMAIN_NAME];
143*c65ebfc7SToomas Soome 			domainname type;
144*c65ebfc7SToomas Soome 			mDNSBool default_domain;
145*c65ebfc7SToomas Soome 			domainname host;
146*c65ebfc7SToomas Soome 			mDNSBool autoname;              // Set if this name is tied to the Computer Name
147*c65ebfc7SToomas Soome 			mDNSBool autorename;            // Set if this client wants us to automatically rename on conflict
148*c65ebfc7SToomas Soome 			mDNSBool allowremotequery;      // Respond to unicast queries from outside the local link?
149*c65ebfc7SToomas Soome 			int num_subtypes;
150*c65ebfc7SToomas Soome 			mDNSBool AnonData;
151*c65ebfc7SToomas Soome 			service_instance *instances;
152*c65ebfc7SToomas Soome 		} servicereg;
153*c65ebfc7SToomas Soome 		struct
154*c65ebfc7SToomas Soome 		{
155*c65ebfc7SToomas Soome 			mDNSInterfaceID interface_id;
156*c65ebfc7SToomas Soome 			mDNSu32 flags;
157*c65ebfc7SToomas Soome 			mDNSu32 protocol;
158*c65ebfc7SToomas Soome 			DNSQuestion q4;
159*c65ebfc7SToomas Soome 			DNSQuestion *q42;
160*c65ebfc7SToomas Soome 			DNSQuestion q6;
161*c65ebfc7SToomas Soome 			DNSQuestion *q62;
162*c65ebfc7SToomas Soome 			mDNSu8 v4ans;
163*c65ebfc7SToomas Soome 			mDNSu8 v6ans;
164*c65ebfc7SToomas Soome 		} addrinfo;
165*c65ebfc7SToomas Soome 		struct
166*c65ebfc7SToomas Soome 		{
167*c65ebfc7SToomas Soome 			mDNSIPPort ReqExt;              // External port we originally requested, for logging purposes
168*c65ebfc7SToomas Soome 			NATTraversalInfo NATinfo;
169*c65ebfc7SToomas Soome 		} pm;
170*c65ebfc7SToomas Soome 		struct
171*c65ebfc7SToomas Soome 		{
172*c65ebfc7SToomas Soome 			DNSServiceFlags flags;
173*c65ebfc7SToomas Soome 			DNSQuestion q_all;
174*c65ebfc7SToomas Soome 			DNSQuestion q_default;
175*c65ebfc7SToomas Soome 			DNSQuestion q_autoall;
176*c65ebfc7SToomas Soome 		} enumeration;
177*c65ebfc7SToomas Soome 		struct
178*c65ebfc7SToomas Soome 		{
179*c65ebfc7SToomas Soome 			DNSQuestion q;
180*c65ebfc7SToomas Soome 			DNSQuestion *q2;
181*c65ebfc7SToomas Soome 			mDNSu8 ans;
182*c65ebfc7SToomas Soome 		} queryrecord;
183*c65ebfc7SToomas Soome 		struct
184*c65ebfc7SToomas Soome 		{
185*c65ebfc7SToomas Soome 			DNSQuestion qtxt;
186*c65ebfc7SToomas Soome 			DNSQuestion qsrv;
187*c65ebfc7SToomas Soome 			const ResourceRecord *txt;
188*c65ebfc7SToomas Soome 			const ResourceRecord *srv;
189*c65ebfc7SToomas Soome 			mDNSs32 ReportTime;
190*c65ebfc7SToomas Soome 			mDNSBool external_advertise;
191*c65ebfc7SToomas Soome 		} resolve;
192*c65ebfc7SToomas Soome 	} u;
193*c65ebfc7SToomas Soome };
194*c65ebfc7SToomas Soome 
195*c65ebfc7SToomas Soome // struct physically sits between ipc message header and call-specific fields in the message buffer
196*c65ebfc7SToomas Soome typedef struct
197*c65ebfc7SToomas Soome {
198*c65ebfc7SToomas Soome 	DNSServiceFlags flags;          // Note: This field is in NETWORK byte order
199*c65ebfc7SToomas Soome 	mDNSu32 ifi;                    // Note: This field is in NETWORK byte order
200*c65ebfc7SToomas Soome 	DNSServiceErrorType error;      // Note: This field is in NETWORK byte order
201*c65ebfc7SToomas Soome } reply_hdr;
202*c65ebfc7SToomas Soome 
203*c65ebfc7SToomas Soome typedef struct reply_state
204*c65ebfc7SToomas Soome {
205*c65ebfc7SToomas Soome 	struct reply_state *next;       // If there are multiple unsent replies
206*c65ebfc7SToomas Soome 	mDNSu32 totallen;
207*c65ebfc7SToomas Soome 	mDNSu32 nwriten;
208*c65ebfc7SToomas Soome 	ipc_msg_hdr mhdr[1];
209*c65ebfc7SToomas Soome 	reply_hdr rhdr[1];
210*c65ebfc7SToomas Soome } reply_state;
211*c65ebfc7SToomas Soome 
212*c65ebfc7SToomas Soome /* Client interface: */
213*c65ebfc7SToomas Soome 
214*c65ebfc7SToomas Soome #define SRS_PORT(S) mDNSVal16((S)->RR_SRV.resrec.rdata->u.srv.port)
215*c65ebfc7SToomas Soome 
216*c65ebfc7SToomas Soome #define LogTimer(MSG,T) LogMsgNoIdent( MSG " %08X %11d  %08X %11d", (T), (T), (T)-now, (T)-now)
217*c65ebfc7SToomas Soome 
218*c65ebfc7SToomas Soome extern int udsserver_init(dnssd_sock_t skts[], mDNSu32 count);
219*c65ebfc7SToomas Soome extern mDNSs32 udsserver_idle(mDNSs32 nextevent);
220*c65ebfc7SToomas Soome extern void udsserver_info(void);  // print out info about current state
221*c65ebfc7SToomas Soome extern void udsserver_handle_configchange(mDNS *const m);
222*c65ebfc7SToomas Soome extern int udsserver_exit(void);    // should be called prior to app exit
223*c65ebfc7SToomas Soome extern void LogMcastStateInfo(mDNSBool mflag, mDNSBool start, mDNSBool mstatelog);
224*c65ebfc7SToomas Soome #define LogMcastQ       (mDNS_McastLoggingEnabled == 0) ? ((void)0) : LogMcastQuestion
225*c65ebfc7SToomas Soome #define LogMcastS       (mDNS_McastLoggingEnabled == 0) ? ((void)0) : LogMcastService
226*c65ebfc7SToomas Soome #define LogMcast        (mDNS_McastLoggingEnabled == 0) ? ((void)0) : LogMsg
227*c65ebfc7SToomas Soome #define LogMcastNoIdent (mDNS_McastLoggingEnabled == 0) ? ((void)0) : LogMsgNoIdent
228*c65ebfc7SToomas Soome 
229*c65ebfc7SToomas Soome /* Routines that uds_daemon expects to link against: */
230*c65ebfc7SToomas Soome 
231*c65ebfc7SToomas Soome typedef void (*udsEventCallback)(int fd, short filter, void *context);
232*c65ebfc7SToomas Soome extern mStatus udsSupportAddFDToEventLoop(dnssd_sock_t fd, udsEventCallback callback, void *context, void **platform_data);
233*c65ebfc7SToomas Soome extern int     udsSupportReadFD(dnssd_sock_t fd, char* buf, int len, int flags, void *platform_data);
234*c65ebfc7SToomas Soome extern mStatus udsSupportRemoveFDFromEventLoop(dnssd_sock_t fd, void *platform_data); // Note: This also CLOSES the file descriptor as well
235*c65ebfc7SToomas Soome 
236*c65ebfc7SToomas Soome extern void RecordUpdatedNiceLabel(mDNSs32 delay);
237*c65ebfc7SToomas Soome 
238*c65ebfc7SToomas Soome // Globals and functions defined in uds_daemon.c and also shared with the old "daemon.c" on OS X
239*c65ebfc7SToomas Soome 
240*c65ebfc7SToomas Soome extern mDNS mDNSStorage;
241*c65ebfc7SToomas Soome extern DNameListElem *AutoRegistrationDomains;
242*c65ebfc7SToomas Soome extern DNameListElem *AutoBrowseDomains;
243*c65ebfc7SToomas Soome 
244*c65ebfc7SToomas Soome extern mDNSs32 ChopSubTypes(char *regtype, char **AnonData);
245*c65ebfc7SToomas Soome extern AuthRecord *AllocateSubTypes(mDNSs32 NumSubTypes, char *p, char **AnonData);
246*c65ebfc7SToomas Soome extern int CountExistingRegistrations(domainname *srv, mDNSIPPort port);
247*c65ebfc7SToomas Soome extern mDNSBool callExternalHelpers(mDNSInterfaceID InterfaceID, const domainname *const domain, DNSServiceFlags flags);
248*c65ebfc7SToomas Soome extern void FreeExtraRR(mDNS *const m, AuthRecord *const rr, mStatus result);
249*c65ebfc7SToomas Soome extern int CountPeerRegistrations(ServiceRecordSet *const srs);
250*c65ebfc7SToomas Soome 
251*c65ebfc7SToomas Soome #if APPLE_OSX_mDNSResponder
252*c65ebfc7SToomas Soome 
253*c65ebfc7SToomas Soome // D2D interface support
254*c65ebfc7SToomas Soome extern void external_start_browsing_for_service(mDNSInterfaceID InterfaceID, const domainname *const type, DNS_TypeValues qtype, DNSServiceFlags flags);
255*c65ebfc7SToomas Soome extern void external_stop_browsing_for_service(mDNSInterfaceID InterfaceID, const domainname *const type, DNS_TypeValues qtype, DNSServiceFlags flags);
256*c65ebfc7SToomas Soome extern void external_start_advertising_service(const ResourceRecord *const resourceRecord, DNSServiceFlags flags);
257*c65ebfc7SToomas Soome extern void external_stop_advertising_service(const ResourceRecord *const resourceRecord, DNSServiceFlags flags);
258*c65ebfc7SToomas Soome extern void external_start_resolving_service(mDNSInterfaceID InterfaceID, const domainname *const fqdn, DNSServiceFlags flags);
259*c65ebfc7SToomas Soome extern void external_stop_resolving_service(mDNSInterfaceID InterfaceID, const domainname *const fqdn, DNSServiceFlags flags);
260*c65ebfc7SToomas Soome extern void external_connection_release(const domainname *instance);
261*c65ebfc7SToomas Soome 
262*c65ebfc7SToomas Soome #else   // APPLE_OSX_mDNSResponder
263*c65ebfc7SToomas Soome 
264*c65ebfc7SToomas Soome #define external_start_browsing_for_service(A,B,C,D) (void)(A)
265*c65ebfc7SToomas Soome #define external_stop_browsing_for_service(A,B,C,D)  (void)(A)
266*c65ebfc7SToomas Soome #define external_start_advertising_service(A,B)      (void)(A)
267*c65ebfc7SToomas Soome #define external_stop_advertising_service(A,B)       do { (void)(A); (void)(B); } while (0)
268*c65ebfc7SToomas Soome #define external_start_resolving_service(A,B,C)      (void)(A)
269*c65ebfc7SToomas Soome #define external_stop_resolving_service(A,B,C)       (void)(A)
270*c65ebfc7SToomas Soome #define external_connection_release(A)               (void)(A)
271*c65ebfc7SToomas Soome 
272*c65ebfc7SToomas Soome #endif // APPLE_OSX_mDNSResponder
273*c65ebfc7SToomas Soome 
274*c65ebfc7SToomas Soome extern const char mDNSResponderVersionString_SCCS[];
275*c65ebfc7SToomas Soome #define mDNSResponderVersionString (mDNSResponderVersionString_SCCS+5)
276*c65ebfc7SToomas Soome 
277*c65ebfc7SToomas Soome #if DEBUG
278*c65ebfc7SToomas Soome extern void SetDebugBoundPath(void);
279*c65ebfc7SToomas Soome extern int IsDebugSocketInUse(void);
280*c65ebfc7SToomas Soome #endif
281*c65ebfc7SToomas Soome 
282*c65ebfc7SToomas Soome #endif /* UDS_DAEMON_H */
283