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