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