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