1*472cd20dSToomas Soome /*
2*472cd20dSToomas Soome * Copyright (c) 2003-2020 Apple Inc. All rights reserved.
3c65ebfc7SToomas Soome *
4c65ebfc7SToomas Soome * Licensed under the Apache License, Version 2.0 (the "License");
5c65ebfc7SToomas Soome * you may not use this file except in compliance with the License.
6c65ebfc7SToomas Soome * You may obtain a copy of the License at
7c65ebfc7SToomas Soome *
8c65ebfc7SToomas Soome * http://www.apache.org/licenses/LICENSE-2.0
9c65ebfc7SToomas Soome *
10c65ebfc7SToomas Soome * Unless required by applicable law or agreed to in writing, software
11c65ebfc7SToomas Soome * distributed under the License is distributed on an "AS IS" BASIS,
12c65ebfc7SToomas Soome * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c65ebfc7SToomas Soome * See the License for the specific language governing permissions and
14c65ebfc7SToomas Soome * limitations under the License.
15c65ebfc7SToomas Soome */
16c65ebfc7SToomas Soome
17c65ebfc7SToomas Soome #if defined(_WIN32)
18c65ebfc7SToomas Soome #include <process.h>
19c65ebfc7SToomas Soome #define usleep(X) Sleep(((X)+999)/1000)
20c65ebfc7SToomas Soome #else
21c65ebfc7SToomas Soome #include <fcntl.h>
22c65ebfc7SToomas Soome #include <errno.h>
23c65ebfc7SToomas Soome #include <sys/ioctl.h>
24c65ebfc7SToomas Soome #include <sys/types.h>
25c65ebfc7SToomas Soome #include <sys/time.h>
26c65ebfc7SToomas Soome #include <sys/resource.h>
27c65ebfc7SToomas Soome #endif
28c65ebfc7SToomas Soome
29c65ebfc7SToomas Soome #include <stdlib.h>
30c65ebfc7SToomas Soome #include <stdio.h>
31c65ebfc7SToomas Soome
32c65ebfc7SToomas Soome #include "mDNSEmbeddedAPI.h"
33c65ebfc7SToomas Soome #include "DNSCommon.h"
34c65ebfc7SToomas Soome #include "uDNS.h"
35c65ebfc7SToomas Soome #include "uds_daemon.h"
36c65ebfc7SToomas Soome #include "dns_sd_internal.h"
37c65ebfc7SToomas Soome
38c65ebfc7SToomas Soome // Apple-specific functionality, not required for other platforms
39c65ebfc7SToomas Soome #if APPLE_OSX_mDNSResponder
40*472cd20dSToomas Soome #include <os/log.h>
41c65ebfc7SToomas Soome #include <sys/ucred.h>
42c65ebfc7SToomas Soome #ifndef PID_FILE
43*472cd20dSToomas Soome #define NO_PID_FILE // We need to signal that this platform has no PID file, and not just that we are taking the default
44c65ebfc7SToomas Soome #endif
45c65ebfc7SToomas Soome #endif
46c65ebfc7SToomas Soome
47c65ebfc7SToomas Soome #ifdef LOCAL_PEEREPID
48c65ebfc7SToomas Soome #include <sys/un.h> // for LOCAL_PEEREPID
49c65ebfc7SToomas Soome #include <sys/socket.h> // for getsockopt
50c65ebfc7SToomas Soome #include <sys/proc_info.h> // for struct proc_bsdshortinfo
51c65ebfc7SToomas Soome #include <libproc.h> // for proc_pidinfo()
52c65ebfc7SToomas Soome #endif //LOCAL_PEEREPID
53c65ebfc7SToomas Soome
54*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, D2D)
55*472cd20dSToomas Soome #include "D2D.h"
56c65ebfc7SToomas Soome #endif
57c65ebfc7SToomas Soome
58c65ebfc7SToomas Soome #if APPLE_OSX_mDNSResponder
59c65ebfc7SToomas Soome #include "BLE.h"
60*472cd20dSToomas Soome #endif
61c65ebfc7SToomas Soome
62*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, TRUST_ENFORCEMENT)
63*472cd20dSToomas Soome #include "mDNSMacOSX.h"
64*472cd20dSToomas Soome #include <os/feature_private.h>
65*472cd20dSToomas Soome #endif
66c65ebfc7SToomas Soome
67*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, AUDIT_TOKEN)
68*472cd20dSToomas Soome #include <bsm/libbsm.h>
69*472cd20dSToomas Soome #endif
70c65ebfc7SToomas Soome
71*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, QUERIER)
72*472cd20dSToomas Soome #include "QuerierSupport.h"
73*472cd20dSToomas Soome #endif
74c65ebfc7SToomas Soome
75*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, QUERIER) && MDNSRESPONDER_SUPPORTS(APPLE, IPC_TLV)
76*472cd20dSToomas Soome #include "mdns_tlv.h"
77*472cd20dSToomas Soome #endif
78*472cd20dSToomas Soome
79*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, DNSSECv2)
80*472cd20dSToomas Soome #include "dnssec_v2.h"
81*472cd20dSToomas Soome #endif
82*472cd20dSToomas Soome
83*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, DNSSD_XPC_SERVICE)
84*472cd20dSToomas Soome #include "dnssd_server.h"
85*472cd20dSToomas Soome #endif
86c65ebfc7SToomas Soome
87c65ebfc7SToomas Soome // User IDs 0-500 are system-wide processes, not actual users in the usual sense
88c65ebfc7SToomas Soome // User IDs for real user accounts start at 501 and count up from there
89c65ebfc7SToomas Soome #define SystemUID(X) ((X) <= 500)
90c65ebfc7SToomas Soome
91c65ebfc7SToomas Soome // ***************************************************************************
92c65ebfc7SToomas Soome #if COMPILER_LIKES_PRAGMA_MARK
93c65ebfc7SToomas Soome #pragma mark -
94c65ebfc7SToomas Soome #pragma mark - Globals
95c65ebfc7SToomas Soome #endif
96c65ebfc7SToomas Soome
97c65ebfc7SToomas Soome // globals
98c65ebfc7SToomas Soome mDNSexport mDNS mDNSStorage;
99c65ebfc7SToomas Soome mDNSexport const char ProgramName[] = "mDNSResponder";
100c65ebfc7SToomas Soome
101c65ebfc7SToomas Soome #if defined(USE_TCP_LOOPBACK)
102c65ebfc7SToomas Soome static char* boundPath = NULL;
103c65ebfc7SToomas Soome #else
104c65ebfc7SToomas Soome static char* boundPath = MDNS_UDS_SERVERPATH;
105c65ebfc7SToomas Soome #endif
106c65ebfc7SToomas Soome #if DEBUG
107c65ebfc7SToomas Soome #define MDNS_UDS_SERVERPATH_DEBUG "/var/tmp/mDNSResponder"
108c65ebfc7SToomas Soome #endif
109c65ebfc7SToomas Soome static dnssd_sock_t listenfd = dnssd_InvalidSocket;
110c65ebfc7SToomas Soome static request_state *all_requests = NULL;
111c65ebfc7SToomas Soome #ifdef LOCAL_PEEREPID
112c65ebfc7SToomas Soome struct proc_bsdshortinfo proc;
113c65ebfc7SToomas Soome #endif //LOCAL_PEEREPID
114c65ebfc7SToomas Soome mDNSlocal void set_peer_pid(request_state *request);
115c65ebfc7SToomas Soome mDNSlocal void LogMcastClientInfo(request_state *req);
116c65ebfc7SToomas Soome mDNSlocal void GetMcastClients(request_state *req);
117c65ebfc7SToomas Soome static mDNSu32 mcount; // tracks the current active mcast operations for McastLogging
118c65ebfc7SToomas Soome static mDNSu32 i_mcount; // sets mcount when McastLogging is enabled(PROF signal is sent)
119c65ebfc7SToomas Soome static mDNSu32 n_mrecords; // tracks the current active mcast records for McastLogging
120c65ebfc7SToomas Soome static mDNSu32 n_mquests; // tracks the current active mcast questions for McastLogging
121c65ebfc7SToomas Soome
122c65ebfc7SToomas Soome
123*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, METRICS)
124c65ebfc7SToomas Soome mDNSu32 curr_num_regservices = 0;
125c65ebfc7SToomas Soome mDNSu32 max_num_regservices = 0;
126c65ebfc7SToomas Soome #endif
127c65ebfc7SToomas Soome
128c65ebfc7SToomas Soome // Note asymmetry here between registration and browsing.
129c65ebfc7SToomas Soome // For service registrations we only automatically register in domains that explicitly appear in local configuration data
130c65ebfc7SToomas Soome // (so AutoRegistrationDomains could equally well be called SCPrefRegDomains)
131c65ebfc7SToomas Soome // For service browsing we also learn automatic browsing domains from the network, so for that case we have:
132c65ebfc7SToomas Soome // 1. SCPrefBrowseDomains (local configuration data)
133c65ebfc7SToomas Soome // 2. LocalDomainEnumRecords (locally-generated local-only PTR records -- equivalent to slElem->AuthRecs in uDNS.c)
134c65ebfc7SToomas Soome // 3. AutoBrowseDomains, which is populated by tracking add/rmv events in AutomaticBrowseDomainChange, the callback function for our mDNS_GetDomains call.
135c65ebfc7SToomas Soome // By creating and removing our own LocalDomainEnumRecords, we trigger AutomaticBrowseDomainChange callbacks just like domains learned from the network would.
136c65ebfc7SToomas Soome
137c65ebfc7SToomas Soome mDNSexport DNameListElem *AutoRegistrationDomains; // Domains where we automatically register for empty-string registrations
138c65ebfc7SToomas Soome
139c65ebfc7SToomas Soome static DNameListElem *SCPrefBrowseDomains; // List of automatic browsing domains read from SCPreferences for "empty string" browsing
140c65ebfc7SToomas Soome static ARListElem *LocalDomainEnumRecords; // List of locally-generated PTR records to augment those we learn from the network
141c65ebfc7SToomas Soome mDNSexport DNameListElem *AutoBrowseDomains; // List created from those local-only PTR records plus records we get from the network
142c65ebfc7SToomas Soome
143c65ebfc7SToomas Soome #define MSG_PAD_BYTES 5 // pad message buffer (read from client) with n zero'd bytes to guarantee
144c65ebfc7SToomas Soome // n get_string() calls w/o buffer overrun
145c65ebfc7SToomas Soome // initialization, setup/teardown functions
146c65ebfc7SToomas Soome
147c65ebfc7SToomas Soome // If a platform specifies its own PID file name, we use that
148c65ebfc7SToomas Soome #ifndef PID_FILE
149c65ebfc7SToomas Soome #define PID_FILE "/var/run/mDNSResponder.pid"
150c65ebfc7SToomas Soome #endif
151c65ebfc7SToomas Soome
152c65ebfc7SToomas Soome // ***************************************************************************
153c65ebfc7SToomas Soome #if COMPILER_LIKES_PRAGMA_MARK
154c65ebfc7SToomas Soome #pragma mark -
155c65ebfc7SToomas Soome #pragma mark - General Utility Functions
156c65ebfc7SToomas Soome #endif
157c65ebfc7SToomas Soome
GetNewRequestID(void)158*472cd20dSToomas Soome mDNSlocal mDNSu32 GetNewRequestID(void)
159*472cd20dSToomas Soome {
160*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, DNSSD_XPC_SERVICE)
161*472cd20dSToomas Soome return dnssd_server_get_new_request_id();
162*472cd20dSToomas Soome #else
163*472cd20dSToomas Soome static mDNSu32 s_last_id = 0;
164*472cd20dSToomas Soome return ++s_last_id;
165*472cd20dSToomas Soome #endif
166*472cd20dSToomas Soome }
167*472cd20dSToomas Soome
FatalError(char * errmsg)168c65ebfc7SToomas Soome mDNSlocal void FatalError(char *errmsg)
169c65ebfc7SToomas Soome {
170c65ebfc7SToomas Soome LogMsg("%s: %s", errmsg, dnssd_strerror(dnssd_errno));
171c65ebfc7SToomas Soome abort();
172c65ebfc7SToomas Soome }
173c65ebfc7SToomas Soome
dnssd_htonl(mDNSu32 l)174c65ebfc7SToomas Soome mDNSlocal mDNSu32 dnssd_htonl(mDNSu32 l)
175c65ebfc7SToomas Soome {
176c65ebfc7SToomas Soome mDNSu32 ret;
177c65ebfc7SToomas Soome char *data = (char*) &ret;
178c65ebfc7SToomas Soome put_uint32(l, &data);
179c65ebfc7SToomas Soome return ret;
180c65ebfc7SToomas Soome }
181c65ebfc7SToomas Soome
182c65ebfc7SToomas Soome // hack to search-replace perror's to LogMsg's
my_perror(char * errmsg)183c65ebfc7SToomas Soome mDNSlocal void my_perror(char *errmsg)
184c65ebfc7SToomas Soome {
185c65ebfc7SToomas Soome LogMsg("%s: %d (%s)", errmsg, dnssd_errno, dnssd_strerror(dnssd_errno));
186c65ebfc7SToomas Soome }
187c65ebfc7SToomas Soome
188c65ebfc7SToomas Soome // Throttled version of my_perror: Logs once every 250 msgs
my_throttled_perror(char * err_msg)189c65ebfc7SToomas Soome mDNSlocal void my_throttled_perror(char *err_msg)
190c65ebfc7SToomas Soome {
191c65ebfc7SToomas Soome static int uds_throttle_count = 0;
192c65ebfc7SToomas Soome if ((uds_throttle_count++ % 250) == 0)
193c65ebfc7SToomas Soome my_perror(err_msg);
194c65ebfc7SToomas Soome }
195c65ebfc7SToomas Soome
196c65ebfc7SToomas Soome // LogMcastQuestion/LogMcastQ should be called after the DNSQuestion struct is initialized(especially for q->TargetQID)
197c65ebfc7SToomas Soome // Hence all calls are made after mDNS_StartQuery()/mDNS_StopQuery()/mDNS_StopBrowse() is called.
LogMcastQuestion(const DNSQuestion * const q,request_state * req,q_state status)198c65ebfc7SToomas Soome mDNSlocal void LogMcastQuestion(const DNSQuestion *const q, request_state *req, q_state status)
199c65ebfc7SToomas Soome {
200c65ebfc7SToomas Soome if (mDNSOpaque16IsZero(q->TargetQID)) // Check for Mcast Query
201c65ebfc7SToomas Soome {
202c65ebfc7SToomas Soome mDNSBool mflag = mDNSfalse;
203c65ebfc7SToomas Soome if (status == q_start)
204c65ebfc7SToomas Soome {
205c65ebfc7SToomas Soome if (++mcount == 1)
206c65ebfc7SToomas Soome mflag = mDNStrue;
207c65ebfc7SToomas Soome }
208c65ebfc7SToomas Soome else
209c65ebfc7SToomas Soome {
210c65ebfc7SToomas Soome mcount--;
211c65ebfc7SToomas Soome }
212c65ebfc7SToomas Soome LogMcast("%s: %##s (%s) (%s) Client(%d)[%s]", status ? "+Question" : "-Question", q->qname.c, DNSTypeName(q->qtype),
213c65ebfc7SToomas Soome q->InterfaceID == mDNSInterface_LocalOnly ? "lo" :
214c65ebfc7SToomas Soome q->InterfaceID == mDNSInterface_P2P ? "p2p" :
215c65ebfc7SToomas Soome q->InterfaceID == mDNSInterface_BLE ? "BLE" :
216c65ebfc7SToomas Soome q->InterfaceID == mDNSInterface_Any ? "any" : InterfaceNameForID(&mDNSStorage, q->InterfaceID),
217c65ebfc7SToomas Soome req->process_id, req->pid_name);
218c65ebfc7SToomas Soome LogMcastStateInfo(mflag, mDNSfalse, mDNSfalse);
219c65ebfc7SToomas Soome }
220c65ebfc7SToomas Soome return;
221c65ebfc7SToomas Soome }
222c65ebfc7SToomas Soome
223c65ebfc7SToomas Soome // LogMcastService/LogMcastS should be called after the AuthRecord struct is initialized
224c65ebfc7SToomas Soome // Hence all calls are made after mDNS_Register()/ just before mDNS_Deregister()
LogMcastService(const AuthRecord * const ar,request_state * req,reg_state status)225c65ebfc7SToomas Soome mDNSlocal void LogMcastService(const AuthRecord *const ar, request_state *req, reg_state status)
226c65ebfc7SToomas Soome {
227c65ebfc7SToomas Soome if (!AuthRecord_uDNS(ar)) // Check for Mcast Service
228c65ebfc7SToomas Soome {
229c65ebfc7SToomas Soome mDNSBool mflag = mDNSfalse;
230c65ebfc7SToomas Soome if (status == reg_start)
231c65ebfc7SToomas Soome {
232c65ebfc7SToomas Soome if (++mcount == 1)
233c65ebfc7SToomas Soome mflag = mDNStrue;
234c65ebfc7SToomas Soome }
235c65ebfc7SToomas Soome else
236c65ebfc7SToomas Soome {
237c65ebfc7SToomas Soome mcount--;
238c65ebfc7SToomas Soome }
239c65ebfc7SToomas Soome LogMcast("%s: %##s (%s) (%s) Client(%d)[%s]", status ? "+Service" : "-Service", ar->resrec.name->c, DNSTypeName(ar->resrec.rrtype),
240c65ebfc7SToomas Soome ar->resrec.InterfaceID == mDNSInterface_LocalOnly ? "lo" :
241c65ebfc7SToomas Soome ar->resrec.InterfaceID == mDNSInterface_P2P ? "p2p" :
242c65ebfc7SToomas Soome ar->resrec.InterfaceID == mDNSInterface_BLE ? "BLE" :
243c65ebfc7SToomas Soome ar->resrec.InterfaceID == mDNSInterface_Any ? "all" : InterfaceNameForID(&mDNSStorage, ar->resrec.InterfaceID),
244c65ebfc7SToomas Soome req->process_id, req->pid_name);
245c65ebfc7SToomas Soome LogMcastStateInfo(mflag, mDNSfalse, mDNSfalse);
246c65ebfc7SToomas Soome }
247c65ebfc7SToomas Soome return;
248c65ebfc7SToomas Soome }
249c65ebfc7SToomas Soome
250c65ebfc7SToomas Soome // For complete Mcast State Log, pass mDNStrue to mstatelog in LogMcastStateInfo()
LogMcastStateInfo(mDNSBool mflag,mDNSBool start,mDNSBool mstatelog)251c65ebfc7SToomas Soome mDNSexport void LogMcastStateInfo(mDNSBool mflag, mDNSBool start, mDNSBool mstatelog)
252c65ebfc7SToomas Soome {
253c65ebfc7SToomas Soome mDNS *const m = &mDNSStorage;
254c65ebfc7SToomas Soome if (!mstatelog)
255c65ebfc7SToomas Soome {
256c65ebfc7SToomas Soome if (!all_requests)
257c65ebfc7SToomas Soome {
258c65ebfc7SToomas Soome LogMcastNoIdent("<None>");
259c65ebfc7SToomas Soome }
260c65ebfc7SToomas Soome else
261c65ebfc7SToomas Soome {
262c65ebfc7SToomas Soome request_state *req, *r;
263c65ebfc7SToomas Soome for (req = all_requests; req; req=req->next)
264c65ebfc7SToomas Soome {
265c65ebfc7SToomas Soome if (req->primary) // If this is a subbordinate operation, check that the parent is in the list
266c65ebfc7SToomas Soome {
267c65ebfc7SToomas Soome for (r = all_requests; r && r != req; r=r->next)
268c65ebfc7SToomas Soome if (r == req->primary)
269c65ebfc7SToomas Soome goto foundpar;
270c65ebfc7SToomas Soome }
271c65ebfc7SToomas Soome // For non-subbordinate operations, and subbordinate operations that have lost their parent, write out their info
272c65ebfc7SToomas Soome GetMcastClients(req);
273c65ebfc7SToomas Soome foundpar:;
274c65ebfc7SToomas Soome }
275c65ebfc7SToomas Soome LogMcastNoIdent("--- MCAST RECORDS COUNT[%d] MCAST QUESTIONS COUNT[%d] ---", n_mrecords, n_mquests);
276c65ebfc7SToomas Soome n_mrecords = n_mquests = 0; // Reset the values
277c65ebfc7SToomas Soome }
278c65ebfc7SToomas Soome }
279c65ebfc7SToomas Soome else
280c65ebfc7SToomas Soome {
281c65ebfc7SToomas Soome static mDNSu32 i_mpktnum;
282c65ebfc7SToomas Soome i_mcount = 0;
283c65ebfc7SToomas Soome if (start)
284c65ebfc7SToomas Soome mcount = 0;
285c65ebfc7SToomas Soome // mcount is initialized to 0 when the PROF signal is sent since mcount could have
286c65ebfc7SToomas Soome // wrong value if MulticastLogging is disabled and then re-enabled
287c65ebfc7SToomas Soome LogMcastNoIdent("--- START MCAST STATE LOG ---");
288c65ebfc7SToomas Soome if (!all_requests)
289c65ebfc7SToomas Soome {
290c65ebfc7SToomas Soome mcount = 0;
291c65ebfc7SToomas Soome LogMcastNoIdent("<None>");
292c65ebfc7SToomas Soome }
293c65ebfc7SToomas Soome else
294c65ebfc7SToomas Soome {
295c65ebfc7SToomas Soome request_state *req, *r;
296c65ebfc7SToomas Soome for (req = all_requests; req; req=req->next)
297c65ebfc7SToomas Soome {
298c65ebfc7SToomas Soome if (req->primary) // If this is a subbordinate operation, check that the parent is in the list
299c65ebfc7SToomas Soome {
300c65ebfc7SToomas Soome for (r = all_requests; r && r != req; r=r->next)
301c65ebfc7SToomas Soome if (r == req->primary)
302c65ebfc7SToomas Soome goto foundparent;
303c65ebfc7SToomas Soome LogMcastNoIdent("%3d: Orphan operation; parent not found in request list", req->sd);
304c65ebfc7SToomas Soome }
305c65ebfc7SToomas Soome // For non-subbordinate operations, and subbordinate operations that have lost their parent, write out their info
306c65ebfc7SToomas Soome LogMcastClientInfo(req);
307c65ebfc7SToomas Soome foundparent:;
308c65ebfc7SToomas Soome }
309c65ebfc7SToomas Soome if(!mcount) // To initially set mcount
310c65ebfc7SToomas Soome mcount = i_mcount;
311c65ebfc7SToomas Soome }
312c65ebfc7SToomas Soome if (mcount == 0)
313c65ebfc7SToomas Soome {
314c65ebfc7SToomas Soome i_mpktnum = m->MPktNum;
315c65ebfc7SToomas Soome LogMcastNoIdent("--- MCOUNT[%d]: IMPKTNUM[%d] ---", mcount, i_mpktnum);
316c65ebfc7SToomas Soome }
317c65ebfc7SToomas Soome if (mflag)
318c65ebfc7SToomas Soome LogMcastNoIdent("--- MCOUNT[%d]: CMPKTNUM[%d] - IMPKTNUM[%d] = [%d]PKTS ---", mcount, m->MPktNum, i_mpktnum, (m->MPktNum - i_mpktnum));
319c65ebfc7SToomas Soome LogMcastNoIdent("--- END MCAST STATE LOG ---");
320c65ebfc7SToomas Soome }
321c65ebfc7SToomas Soome }
322c65ebfc7SToomas Soome
abort_request(request_state * req)323c65ebfc7SToomas Soome mDNSlocal void abort_request(request_state *req)
324c65ebfc7SToomas Soome {
325c65ebfc7SToomas Soome if (req->terminate == (req_termination_fn) ~0)
326*472cd20dSToomas Soome {
327*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT,
328*472cd20dSToomas Soome "[R%d] abort_request: ERROR: Attempt to abort operation %p with req->terminate %p", req->request_id, req, req->terminate);
329*472cd20dSToomas Soome return;
330*472cd20dSToomas Soome }
331c65ebfc7SToomas Soome
332c65ebfc7SToomas Soome // First stop whatever mDNSCore operation we were doing
333c65ebfc7SToomas Soome // If this is actually a shared connection operation, then its req->terminate function will scan
334c65ebfc7SToomas Soome // the all_requests list and terminate any subbordinate operations sharing this file descriptor
335c65ebfc7SToomas Soome if (req->terminate) req->terminate(req);
336*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, QUERIER)
337*472cd20dSToomas Soome if (req->custom_service_id != 0)
338*472cd20dSToomas Soome {
339*472cd20dSToomas Soome Querier_DeregisterCustomDNSService(req->custom_service_id);
340*472cd20dSToomas Soome req->custom_service_id = 0;
341*472cd20dSToomas Soome }
342*472cd20dSToomas Soome #endif
343c65ebfc7SToomas Soome
344c65ebfc7SToomas Soome if (!dnssd_SocketValid(req->sd))
345*472cd20dSToomas Soome {
346*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT,
347*472cd20dSToomas Soome "[R%d] abort_request: ERROR: Attempt to abort operation %p with invalid fd %d", req->request_id, req, req->sd);
348*472cd20dSToomas Soome return;
349*472cd20dSToomas Soome }
350c65ebfc7SToomas Soome
351c65ebfc7SToomas Soome // Now, if this request_state is not subordinate to some other primary, close file descriptor and discard replies
352c65ebfc7SToomas Soome if (!req->primary)
353c65ebfc7SToomas Soome {
354*472cd20dSToomas Soome if (req->errsd != req->sd)
355*472cd20dSToomas Soome {
356*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEBUG,
357*472cd20dSToomas Soome "[R%d] Removing FD %d and closing errsd %d", req->request_id, req->sd, req->errsd);
358*472cd20dSToomas Soome }
359*472cd20dSToomas Soome else
360*472cd20dSToomas Soome {
361*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEBUG,
362*472cd20dSToomas Soome "[R%d] Removing FD %d", req->request_id, req->sd);
363*472cd20dSToomas Soome }
364c65ebfc7SToomas Soome udsSupportRemoveFDFromEventLoop(req->sd, req->platform_data); // Note: This also closes file descriptor req->sd for us
365c65ebfc7SToomas Soome if (req->errsd != req->sd) { dnssd_close(req->errsd); req->errsd = req->sd; }
366c65ebfc7SToomas Soome
367c65ebfc7SToomas Soome while (req->replies) // free pending replies
368c65ebfc7SToomas Soome {
369c65ebfc7SToomas Soome reply_state *ptr = req->replies;
370c65ebfc7SToomas Soome req->replies = req->replies->next;
371c65ebfc7SToomas Soome freeL("reply_state (abort)", ptr);
372c65ebfc7SToomas Soome }
373c65ebfc7SToomas Soome }
374c65ebfc7SToomas Soome
375c65ebfc7SToomas Soome // Set req->sd to something invalid, so that udsserver_idle knows to unlink and free this structure
376*472cd20dSToomas Soome #if MDNS_MALLOC_DEBUGGING
377*472cd20dSToomas Soome // Don't use dnssd_InvalidSocket (-1) because that's the sentinel value MDNS_MALLOC_DEBUGGING uses
378c65ebfc7SToomas Soome // for detecting when the memory for an object is inadvertently freed while the object is still on some list
379*472cd20dSToomas Soome #ifdef WIN32
380*472cd20dSToomas Soome #error This will not work on Windows, look at IsValidSocket in mDNSShared/CommonServices.h to see why
381*472cd20dSToomas Soome #endif
382c65ebfc7SToomas Soome req->sd = req->errsd = -2;
383c65ebfc7SToomas Soome #else
384c65ebfc7SToomas Soome req->sd = req->errsd = dnssd_InvalidSocket;
385c65ebfc7SToomas Soome #endif
386c65ebfc7SToomas Soome // We also set req->terminate to a bogus value so we know if abort_request() gets called again for this request
387c65ebfc7SToomas Soome req->terminate = (req_termination_fn) ~0;
388c65ebfc7SToomas Soome }
389c65ebfc7SToomas Soome
390c65ebfc7SToomas Soome #if DEBUG
SetDebugBoundPath(void)391c65ebfc7SToomas Soome mDNSexport void SetDebugBoundPath(void)
392c65ebfc7SToomas Soome {
393c65ebfc7SToomas Soome #if !defined(USE_TCP_LOOPBACK)
394c65ebfc7SToomas Soome boundPath = MDNS_UDS_SERVERPATH_DEBUG;
395c65ebfc7SToomas Soome #endif
396c65ebfc7SToomas Soome }
397c65ebfc7SToomas Soome
IsDebugSocketInUse(void)398c65ebfc7SToomas Soome mDNSexport int IsDebugSocketInUse(void)
399c65ebfc7SToomas Soome {
400c65ebfc7SToomas Soome #if !defined(USE_TCP_LOOPBACK)
401c65ebfc7SToomas Soome return !strcmp(boundPath, MDNS_UDS_SERVERPATH_DEBUG);
402c65ebfc7SToomas Soome #else
403c65ebfc7SToomas Soome return mDNSfalse;
404c65ebfc7SToomas Soome #endif
405c65ebfc7SToomas Soome }
406c65ebfc7SToomas Soome #endif
407c65ebfc7SToomas Soome
AbortUnlinkAndFree(request_state * req)408c65ebfc7SToomas Soome mDNSlocal void AbortUnlinkAndFree(request_state *req)
409c65ebfc7SToomas Soome {
410c65ebfc7SToomas Soome request_state **p = &all_requests;
411c65ebfc7SToomas Soome abort_request(req);
412c65ebfc7SToomas Soome while (*p && *p != req) p=&(*p)->next;
413*472cd20dSToomas Soome if (*p)
414*472cd20dSToomas Soome {
415*472cd20dSToomas Soome *p = req->next;
416*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, TRUST_ENFORCEMENT)
417*472cd20dSToomas Soome if (req->trust)
418*472cd20dSToomas Soome {
419*472cd20dSToomas Soome void * context = mdns_trust_get_context(req->trust);
420*472cd20dSToomas Soome mdns_trust_set_context(req->trust, NULL);
421*472cd20dSToomas Soome if (context) freeL("context/AbortUnlinkAndFree", context);
422*472cd20dSToomas Soome mdns_trust_forget(&req->trust);
423*472cd20dSToomas Soome }
424*472cd20dSToomas Soome #endif
425*472cd20dSToomas Soome freeL("request_state/AbortUnlinkAndFree", req);
426*472cd20dSToomas Soome }
427c65ebfc7SToomas Soome else LogMsg("AbortUnlinkAndFree: ERROR: Attempt to abort operation %p not in list", req);
428c65ebfc7SToomas Soome }
429c65ebfc7SToomas Soome
create_reply(const reply_op_t op,const size_t datalen,request_state * const request)430c65ebfc7SToomas Soome mDNSlocal reply_state *create_reply(const reply_op_t op, const size_t datalen, request_state *const request)
431c65ebfc7SToomas Soome {
432c65ebfc7SToomas Soome reply_state *reply;
433c65ebfc7SToomas Soome
434c65ebfc7SToomas Soome if ((unsigned)datalen < sizeof(reply_hdr))
435c65ebfc7SToomas Soome {
436c65ebfc7SToomas Soome LogMsg("ERROR: create_reply - data length less than length of required fields");
437c65ebfc7SToomas Soome return NULL;
438c65ebfc7SToomas Soome }
439c65ebfc7SToomas Soome
440*472cd20dSToomas Soome reply = (reply_state *) callocL("reply_state", sizeof(reply_state) + datalen - sizeof(reply_hdr));
441*472cd20dSToomas Soome if (!reply) FatalError("ERROR: calloc");
442c65ebfc7SToomas Soome
443c65ebfc7SToomas Soome reply->next = mDNSNULL;
444c65ebfc7SToomas Soome reply->totallen = (mDNSu32)datalen + sizeof(ipc_msg_hdr);
445c65ebfc7SToomas Soome reply->nwriten = 0;
446c65ebfc7SToomas Soome
447c65ebfc7SToomas Soome reply->mhdr->version = VERSION;
448c65ebfc7SToomas Soome reply->mhdr->datalen = (mDNSu32)datalen;
449c65ebfc7SToomas Soome reply->mhdr->ipc_flags = 0;
450c65ebfc7SToomas Soome reply->mhdr->op = op;
451c65ebfc7SToomas Soome reply->mhdr->client_context = request->hdr.client_context;
452c65ebfc7SToomas Soome reply->mhdr->reg_index = 0;
453c65ebfc7SToomas Soome
454c65ebfc7SToomas Soome return reply;
455c65ebfc7SToomas Soome }
456c65ebfc7SToomas Soome
457c65ebfc7SToomas Soome // Append a reply to the list in a request object
458c65ebfc7SToomas Soome // If our request is sharing a connection, then we append our reply_state onto the primary's list
459c65ebfc7SToomas Soome // If the request does not want asynchronous replies, then the reply is freed instead of being appended to any list.
append_reply(request_state * req,reply_state * rep)460c65ebfc7SToomas Soome mDNSlocal void append_reply(request_state *req, reply_state *rep)
461c65ebfc7SToomas Soome {
462c65ebfc7SToomas Soome request_state *r;
463c65ebfc7SToomas Soome reply_state **ptr;
464c65ebfc7SToomas Soome
465c65ebfc7SToomas Soome if (req->no_reply)
466c65ebfc7SToomas Soome {
467c65ebfc7SToomas Soome freeL("reply_state/append_reply", rep);
468c65ebfc7SToomas Soome return;
469c65ebfc7SToomas Soome }
470c65ebfc7SToomas Soome
471c65ebfc7SToomas Soome r = req->primary ? req->primary : req;
472c65ebfc7SToomas Soome ptr = &r->replies;
473c65ebfc7SToomas Soome while (*ptr) ptr = &(*ptr)->next;
474c65ebfc7SToomas Soome *ptr = rep;
475c65ebfc7SToomas Soome rep->next = NULL;
476c65ebfc7SToomas Soome }
477c65ebfc7SToomas Soome
478c65ebfc7SToomas Soome // Generates a response message giving name, type, domain, plus interface index,
479c65ebfc7SToomas Soome // suitable for a browse result or service registration result.
480c65ebfc7SToomas Soome // On successful completion rep is set to point to a malloc'd reply_state struct
GenerateNTDResponse(const domainname * const servicename,const mDNSInterfaceID id,request_state * const request,reply_state ** const rep,reply_op_t op,DNSServiceFlags flags,mStatus err)481c65ebfc7SToomas Soome mDNSlocal mStatus GenerateNTDResponse(const domainname *const servicename, const mDNSInterfaceID id,
482c65ebfc7SToomas Soome request_state *const request, reply_state **const rep, reply_op_t op, DNSServiceFlags flags, mStatus err)
483c65ebfc7SToomas Soome {
484c65ebfc7SToomas Soome domainlabel name;
485c65ebfc7SToomas Soome domainname type, dom;
486c65ebfc7SToomas Soome *rep = NULL;
487*472cd20dSToomas Soome if (servicename && !DeconstructServiceName(servicename, &name, &type, &dom))
488c65ebfc7SToomas Soome return kDNSServiceErr_Invalid;
489c65ebfc7SToomas Soome else
490c65ebfc7SToomas Soome {
491c65ebfc7SToomas Soome char namestr[MAX_DOMAIN_LABEL+1];
492c65ebfc7SToomas Soome char typestr[MAX_ESCAPED_DOMAIN_NAME];
493c65ebfc7SToomas Soome char domstr [MAX_ESCAPED_DOMAIN_NAME];
494c65ebfc7SToomas Soome int len;
495c65ebfc7SToomas Soome char *data;
496c65ebfc7SToomas Soome
497*472cd20dSToomas Soome if (servicename)
498*472cd20dSToomas Soome {
499c65ebfc7SToomas Soome ConvertDomainLabelToCString_unescaped(&name, namestr);
500c65ebfc7SToomas Soome ConvertDomainNameToCString(&type, typestr);
501c65ebfc7SToomas Soome ConvertDomainNameToCString(&dom, domstr);
502*472cd20dSToomas Soome }
503*472cd20dSToomas Soome else
504*472cd20dSToomas Soome {
505*472cd20dSToomas Soome namestr[0] = 0;
506*472cd20dSToomas Soome typestr[0] = 0;
507*472cd20dSToomas Soome domstr[0] = 0;
508*472cd20dSToomas Soome }
509c65ebfc7SToomas Soome
510c65ebfc7SToomas Soome // Calculate reply data length
511c65ebfc7SToomas Soome len = sizeof(DNSServiceFlags);
512c65ebfc7SToomas Soome len += sizeof(mDNSu32); // if index
513c65ebfc7SToomas Soome len += sizeof(DNSServiceErrorType);
514c65ebfc7SToomas Soome len += (int) (strlen(namestr) + 1);
515c65ebfc7SToomas Soome len += (int) (strlen(typestr) + 1);
516c65ebfc7SToomas Soome len += (int) (strlen(domstr) + 1);
517c65ebfc7SToomas Soome
518c65ebfc7SToomas Soome // Build reply header
519c65ebfc7SToomas Soome *rep = create_reply(op, len, request);
520c65ebfc7SToomas Soome (*rep)->rhdr->flags = dnssd_htonl(flags);
521c65ebfc7SToomas Soome (*rep)->rhdr->ifi = dnssd_htonl(mDNSPlatformInterfaceIndexfromInterfaceID(&mDNSStorage, id, mDNSfalse));
522c65ebfc7SToomas Soome (*rep)->rhdr->error = dnssd_htonl(err);
523c65ebfc7SToomas Soome
524c65ebfc7SToomas Soome // Build reply body
525c65ebfc7SToomas Soome data = (char *)&(*rep)->rhdr[1];
526c65ebfc7SToomas Soome put_string(namestr, &data);
527c65ebfc7SToomas Soome put_string(typestr, &data);
528c65ebfc7SToomas Soome put_string(domstr, &data);
529c65ebfc7SToomas Soome
530c65ebfc7SToomas Soome return mStatus_NoError;
531c65ebfc7SToomas Soome }
532c65ebfc7SToomas Soome }
533c65ebfc7SToomas Soome
GenerateBrowseReply(const domainname * const servicename,const mDNSInterfaceID id,request_state * const request,reply_state ** const rep,reply_op_t op,DNSServiceFlags flags,mStatus err)534c65ebfc7SToomas Soome mDNSlocal void GenerateBrowseReply(const domainname *const servicename, const mDNSInterfaceID id,
535c65ebfc7SToomas Soome request_state *const request, reply_state **const rep, reply_op_t op, DNSServiceFlags flags, mStatus err)
536c65ebfc7SToomas Soome {
537c65ebfc7SToomas Soome char namestr[MAX_DOMAIN_LABEL+1];
538c65ebfc7SToomas Soome char typestr[MAX_ESCAPED_DOMAIN_NAME];
539c65ebfc7SToomas Soome static const char domstr[] = ".";
540c65ebfc7SToomas Soome int len;
541c65ebfc7SToomas Soome char *data;
542c65ebfc7SToomas Soome
543c65ebfc7SToomas Soome *rep = NULL;
544c65ebfc7SToomas Soome
545*472cd20dSToomas Soome if (servicename)
546*472cd20dSToomas Soome {
547c65ebfc7SToomas Soome // 1. Put first label in namestr
548c65ebfc7SToomas Soome ConvertDomainLabelToCString_unescaped((const domainlabel *)servicename, namestr);
549c65ebfc7SToomas Soome
550c65ebfc7SToomas Soome // 2. Put second label and "local" into typestr
551c65ebfc7SToomas Soome mDNS_snprintf(typestr, sizeof(typestr), "%#s.local.", SecondLabel(servicename));
552*472cd20dSToomas Soome }
553*472cd20dSToomas Soome else
554*472cd20dSToomas Soome {
555*472cd20dSToomas Soome namestr[0] = 0;
556*472cd20dSToomas Soome typestr[0] = 0;
557*472cd20dSToomas Soome }
558c65ebfc7SToomas Soome
559c65ebfc7SToomas Soome // Calculate reply data length
560c65ebfc7SToomas Soome len = sizeof(DNSServiceFlags);
561c65ebfc7SToomas Soome len += sizeof(mDNSu32); // if index
562c65ebfc7SToomas Soome len += sizeof(DNSServiceErrorType);
563c65ebfc7SToomas Soome len += (int) (strlen(namestr) + 1);
564c65ebfc7SToomas Soome len += (int) (strlen(typestr) + 1);
565c65ebfc7SToomas Soome len += (int) (strlen(domstr) + 1);
566c65ebfc7SToomas Soome
567c65ebfc7SToomas Soome // Build reply header
568c65ebfc7SToomas Soome *rep = create_reply(op, len, request);
569c65ebfc7SToomas Soome (*rep)->rhdr->flags = dnssd_htonl(flags);
570c65ebfc7SToomas Soome (*rep)->rhdr->ifi = dnssd_htonl(mDNSPlatformInterfaceIndexfromInterfaceID(&mDNSStorage, id, mDNSfalse));
571c65ebfc7SToomas Soome (*rep)->rhdr->error = dnssd_htonl(err);
572c65ebfc7SToomas Soome
573c65ebfc7SToomas Soome // Build reply body
574c65ebfc7SToomas Soome data = (char *)&(*rep)->rhdr[1];
575c65ebfc7SToomas Soome put_string(namestr, &data);
576c65ebfc7SToomas Soome put_string(typestr, &data);
577c65ebfc7SToomas Soome put_string(domstr, &data);
578c65ebfc7SToomas Soome }
579c65ebfc7SToomas Soome
580c65ebfc7SToomas Soome // Returns a resource record (allocated w/ malloc) containing the data found in an IPC message
581c65ebfc7SToomas Soome // Data must be in the following format: flags, interfaceIndex, name, rrtype, rrclass, rdlen, rdata, (optional) ttl
582c65ebfc7SToomas Soome // (ttl only extracted/set if ttl argument is non-zero). Returns NULL for a bad-parameter error
read_rr_from_ipc_msg(request_state * request,int GetTTL,int validate_flags)583c65ebfc7SToomas Soome mDNSlocal AuthRecord *read_rr_from_ipc_msg(request_state *request, int GetTTL, int validate_flags)
584c65ebfc7SToomas Soome {
585c65ebfc7SToomas Soome DNSServiceFlags flags = get_flags(&request->msgptr, request->msgend);
586c65ebfc7SToomas Soome mDNSu32 interfaceIndex = get_uint32(&request->msgptr, request->msgend);
587*472cd20dSToomas Soome char name[MAX_ESCAPED_DOMAIN_NAME];
588c65ebfc7SToomas Soome int str_err = get_string(&request->msgptr, request->msgend, name, sizeof(name));
589c65ebfc7SToomas Soome mDNSu16 type = get_uint16(&request->msgptr, request->msgend);
590c65ebfc7SToomas Soome mDNSu16 class = get_uint16(&request->msgptr, request->msgend);
591c65ebfc7SToomas Soome mDNSu16 rdlen = get_uint16(&request->msgptr, request->msgend);
592*472cd20dSToomas Soome const mDNSu8 *const rdata = (const mDNSu8 *)get_rdata (&request->msgptr, request->msgend, rdlen);
593c65ebfc7SToomas Soome mDNSu32 ttl = GetTTL ? get_uint32(&request->msgptr, request->msgend) : 0;
594*472cd20dSToomas Soome size_t rdcapacity;
595c65ebfc7SToomas Soome AuthRecord *rr;
596c65ebfc7SToomas Soome mDNSInterfaceID InterfaceID;
597c65ebfc7SToomas Soome AuthRecType artype;
598*472cd20dSToomas Soome mDNSu8 recordType;
599c65ebfc7SToomas Soome
600c65ebfc7SToomas Soome request->flags = flags;
601c65ebfc7SToomas Soome request->interfaceIndex = interfaceIndex;
602c65ebfc7SToomas Soome
603c65ebfc7SToomas Soome if (str_err) { LogMsg("ERROR: read_rr_from_ipc_msg - get_string"); return NULL; }
604c65ebfc7SToomas Soome
605c65ebfc7SToomas Soome if (!request->msgptr) { LogMsg("Error reading Resource Record from client"); return NULL; }
606c65ebfc7SToomas Soome
607c65ebfc7SToomas Soome if (validate_flags &&
608c65ebfc7SToomas Soome !((flags & kDNSServiceFlagsShared) == kDNSServiceFlagsShared) &&
609*472cd20dSToomas Soome !((flags & kDNSServiceFlagsUnique) == kDNSServiceFlagsUnique) &&
610*472cd20dSToomas Soome !((flags & kDNSServiceFlagsKnownUnique) == kDNSServiceFlagsKnownUnique))
611c65ebfc7SToomas Soome {
612*472cd20dSToomas Soome LogMsg("ERROR: Bad resource record flags (must be one of either kDNSServiceFlagsShared, kDNSServiceFlagsUnique or kDNSServiceFlagsKnownUnique)");
613c65ebfc7SToomas Soome return NULL;
614c65ebfc7SToomas Soome }
615c65ebfc7SToomas Soome InterfaceID = mDNSPlatformInterfaceIDfromInterfaceIndex(&mDNSStorage, interfaceIndex);
616*472cd20dSToomas Soome
617*472cd20dSToomas Soome // The registration is scoped to a specific interface index, but the interface is not currently on our list.
618*472cd20dSToomas Soome if ((InterfaceID == mDNSInterface_Any) && (interfaceIndex != kDNSServiceInterfaceIndexAny))
619*472cd20dSToomas Soome {
620*472cd20dSToomas Soome // On Apple platforms, an interface's mDNSInterfaceID is equal to its index. Using an interface index that isn't
621*472cd20dSToomas Soome // currently valid will cause the registration to take place as soon as it becomes valid. On other platforms,
622*472cd20dSToomas Soome // mDNSInterfaceID is actually a pointer to a platform-specific interface object, but we don't know what the pointer
623*472cd20dSToomas Soome // for the interface index will be ahead of time. For now, just return NULL to indicate an error condition since the
624*472cd20dSToomas Soome // interface index is invalid. Otherwise, the registration would be performed on all interfaces.
625*472cd20dSToomas Soome #if APPLE_OSX_mDNSResponder
626*472cd20dSToomas Soome InterfaceID = (mDNSInterfaceID)(uintptr_t)interfaceIndex;
627*472cd20dSToomas Soome #else
628*472cd20dSToomas Soome return NULL;
629*472cd20dSToomas Soome #endif
630*472cd20dSToomas Soome }
631*472cd20dSToomas Soome rdcapacity = (rdlen > sizeof(RDataBody2)) ? rdlen : sizeof(RDataBody2);
632*472cd20dSToomas Soome rr = (AuthRecord *) callocL("AuthRecord/read_rr_from_ipc_msg", sizeof(*rr) - sizeof(RDataBody) + rdcapacity);
633*472cd20dSToomas Soome if (!rr) FatalError("ERROR: calloc");
634*472cd20dSToomas Soome
635c65ebfc7SToomas Soome if (InterfaceID == mDNSInterface_LocalOnly)
636c65ebfc7SToomas Soome artype = AuthRecordLocalOnly;
637c65ebfc7SToomas Soome else if (InterfaceID == mDNSInterface_P2P || InterfaceID == mDNSInterface_BLE)
638c65ebfc7SToomas Soome artype = AuthRecordP2P;
639c65ebfc7SToomas Soome else if ((InterfaceID == mDNSInterface_Any) && (flags & kDNSServiceFlagsIncludeP2P)
640c65ebfc7SToomas Soome && (flags & kDNSServiceFlagsIncludeAWDL))
641c65ebfc7SToomas Soome artype = AuthRecordAnyIncludeAWDLandP2P;
642c65ebfc7SToomas Soome else if ((InterfaceID == mDNSInterface_Any) && (flags & kDNSServiceFlagsIncludeP2P))
643c65ebfc7SToomas Soome artype = AuthRecordAnyIncludeP2P;
644c65ebfc7SToomas Soome else if ((InterfaceID == mDNSInterface_Any) && (flags & kDNSServiceFlagsIncludeAWDL))
645c65ebfc7SToomas Soome artype = AuthRecordAnyIncludeAWDL;
646c65ebfc7SToomas Soome else
647c65ebfc7SToomas Soome artype = AuthRecordAny;
648c65ebfc7SToomas Soome
649*472cd20dSToomas Soome if (flags & kDNSServiceFlagsShared)
650*472cd20dSToomas Soome recordType = (mDNSu8) kDNSRecordTypeShared;
651*472cd20dSToomas Soome else if (flags & kDNSServiceFlagsKnownUnique)
652*472cd20dSToomas Soome recordType = (mDNSu8) kDNSRecordTypeKnownUnique;
653*472cd20dSToomas Soome else
654*472cd20dSToomas Soome recordType = (mDNSu8) kDNSRecordTypeUnique;
655*472cd20dSToomas Soome
656*472cd20dSToomas Soome mDNS_SetupResourceRecord(rr, mDNSNULL, InterfaceID, type, 0, recordType, artype, mDNSNULL, mDNSNULL);
657c65ebfc7SToomas Soome
658c65ebfc7SToomas Soome if (!MakeDomainNameFromDNSNameString(&rr->namestorage, name))
659c65ebfc7SToomas Soome {
660c65ebfc7SToomas Soome LogMsg("ERROR: bad name: %s", name);
661c65ebfc7SToomas Soome freeL("AuthRecord/read_rr_from_ipc_msg", rr);
662c65ebfc7SToomas Soome return NULL;
663c65ebfc7SToomas Soome }
664c65ebfc7SToomas Soome
665c65ebfc7SToomas Soome if (flags & kDNSServiceFlagsAllowRemoteQuery) rr->AllowRemoteQuery = mDNStrue;
666c65ebfc7SToomas Soome rr->resrec.rrclass = class;
667c65ebfc7SToomas Soome rr->resrec.rdlength = rdlen;
668*472cd20dSToomas Soome rr->resrec.rdata->MaxRDLength = (mDNSu16)rdcapacity;
669*472cd20dSToomas Soome if (!SetRData(mDNSNULL, rdata, rdata + rdlen, &rr->resrec, rdlen))
670*472cd20dSToomas Soome {
671*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_ERROR,
672*472cd20dSToomas Soome "[R%u] read_rr_from_ipc_msg: SetRData failed for " PRI_DM_NAME " (" PUB_S ")",
673*472cd20dSToomas Soome request->request_id, DM_NAME_PARAM(rr->resrec.name), DNSTypeName(type));
674*472cd20dSToomas Soome freeL("AuthRecord/read_rr_from_ipc_msg", rr);
675*472cd20dSToomas Soome return NULL;
676*472cd20dSToomas Soome }
677c65ebfc7SToomas Soome if (GetTTL) rr->resrec.rroriginalttl = ttl;
678c65ebfc7SToomas Soome rr->resrec.namehash = DomainNameHashValue(rr->resrec.name);
679c65ebfc7SToomas Soome SetNewRData(&rr->resrec, mDNSNULL, 0); // Sets rr->rdatahash for us
680c65ebfc7SToomas Soome return rr;
681c65ebfc7SToomas Soome }
682c65ebfc7SToomas Soome
build_domainname_from_strings(domainname * srv,char * name,char * regtype,char * domain)683c65ebfc7SToomas Soome mDNSlocal int build_domainname_from_strings(domainname *srv, char *name, char *regtype, char *domain)
684c65ebfc7SToomas Soome {
685c65ebfc7SToomas Soome domainlabel n;
686c65ebfc7SToomas Soome domainname d, t;
687c65ebfc7SToomas Soome
688c65ebfc7SToomas Soome if (!MakeDomainLabelFromLiteralString(&n, name)) return -1;
689c65ebfc7SToomas Soome if (!MakeDomainNameFromDNSNameString(&t, regtype)) return -1;
690c65ebfc7SToomas Soome if (!MakeDomainNameFromDNSNameString(&d, domain)) return -1;
691c65ebfc7SToomas Soome if (!ConstructServiceName(srv, &n, &t, &d)) return -1;
692c65ebfc7SToomas Soome return 0;
693c65ebfc7SToomas Soome }
694c65ebfc7SToomas Soome
send_all(dnssd_sock_t s,const char * ptr,int len)695c65ebfc7SToomas Soome mDNSlocal void send_all(dnssd_sock_t s, const char *ptr, int len)
696c65ebfc7SToomas Soome {
697*472cd20dSToomas Soome const ssize_t n = send(s, ptr, len, 0);
698c65ebfc7SToomas Soome // On a freshly-created Unix Domain Socket, the kernel should *never* fail to buffer a small write for us
699c65ebfc7SToomas Soome // (four bytes for a typical error code return, 12 bytes for DNSServiceGetProperty(DaemonVersion)).
700c65ebfc7SToomas Soome // If it does fail, we don't attempt to handle this failure, but we do log it so we know something is wrong.
701c65ebfc7SToomas Soome if (n < len)
702*472cd20dSToomas Soome {
703*472cd20dSToomas Soome LogMsg("ERROR: send_all(%d) wrote %ld of %d errno %d (%s)",
704*472cd20dSToomas Soome s, (long)n, len, dnssd_errno, dnssd_strerror(dnssd_errno));
705*472cd20dSToomas Soome }
706c65ebfc7SToomas Soome }
707c65ebfc7SToomas Soome
708c65ebfc7SToomas Soome #if 0
709c65ebfc7SToomas Soome mDNSlocal mDNSBool AuthorizedDomain(const request_state * const request, const domainname * const d, const DNameListElem * const doms)
710c65ebfc7SToomas Soome {
711c65ebfc7SToomas Soome const DNameListElem *delem = mDNSNULL;
712c65ebfc7SToomas Soome int bestDelta = -1; // the delta of the best match, lower is better
713c65ebfc7SToomas Soome int dLabels = 0;
714c65ebfc7SToomas Soome mDNSBool allow = mDNSfalse;
715c65ebfc7SToomas Soome
716c65ebfc7SToomas Soome if (SystemUID(request->uid)) return mDNStrue;
717c65ebfc7SToomas Soome
718c65ebfc7SToomas Soome dLabels = CountLabels(d);
719c65ebfc7SToomas Soome for (delem = doms; delem; delem = delem->next)
720c65ebfc7SToomas Soome {
721c65ebfc7SToomas Soome if (delem->uid)
722c65ebfc7SToomas Soome {
723c65ebfc7SToomas Soome int delemLabels = CountLabels(&delem->name);
724c65ebfc7SToomas Soome int delta = dLabels - delemLabels;
725c65ebfc7SToomas Soome if ((bestDelta == -1 || delta <= bestDelta) && SameDomainName(&delem->name, SkipLeadingLabels(d, delta)))
726c65ebfc7SToomas Soome {
727c65ebfc7SToomas Soome bestDelta = delta;
728c65ebfc7SToomas Soome allow = (allow || (delem->uid == request->uid));
729c65ebfc7SToomas Soome }
730c65ebfc7SToomas Soome }
731c65ebfc7SToomas Soome }
732c65ebfc7SToomas Soome
733c65ebfc7SToomas Soome return bestDelta == -1 ? mDNStrue : allow;
734c65ebfc7SToomas Soome }
735c65ebfc7SToomas Soome #endif
736c65ebfc7SToomas Soome
737*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, AUDIT_TOKEN)
SetupAuditTokenForRequest(request_state * request)738*472cd20dSToomas Soome mDNSlocal void SetupAuditTokenForRequest(request_state *request)
739*472cd20dSToomas Soome {
740*472cd20dSToomas Soome
741*472cd20dSToomas Soome pid_t audit_pid = audit_token_to_pid(request->audit_token);
742*472cd20dSToomas Soome if (audit_pid == 0)
743*472cd20dSToomas Soome {
744*472cd20dSToomas Soome #if !defined(LOCAL_PEERTOKEN)
745*472cd20dSToomas Soome #define LOCAL_PEERTOKEN 0x006 /* retrieve peer audit token */
746*472cd20dSToomas Soome #endif
747*472cd20dSToomas Soome socklen_t len = sizeof(audit_token_t);
748*472cd20dSToomas Soome int ret = getsockopt(request->sd, SOL_LOCAL, LOCAL_PEERTOKEN, &request->audit_token, &len);
749*472cd20dSToomas Soome if (ret != 0)
750*472cd20dSToomas Soome {
751*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_ERROR,
752*472cd20dSToomas Soome "SetupAuditTokenForRequest: No audit_token using LOCAL_PEERTOKEN (%s PID %d) for op %d ret(%d)",
753*472cd20dSToomas Soome request->pid_name, request->process_id, request->hdr.op, ret);
754*472cd20dSToomas Soome }
755*472cd20dSToomas Soome }
756*472cd20dSToomas Soome }
757*472cd20dSToomas Soome #endif
758*472cd20dSToomas Soome
759c65ebfc7SToomas Soome // ***************************************************************************
760c65ebfc7SToomas Soome #if COMPILER_LIKES_PRAGMA_MARK
761c65ebfc7SToomas Soome #pragma mark -
762c65ebfc7SToomas Soome #pragma mark - external helpers
763c65ebfc7SToomas Soome #endif
764c65ebfc7SToomas Soome
765*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, D2D)
external_start_advertising_helper(service_instance * const instance)766c65ebfc7SToomas Soome mDNSlocal void external_start_advertising_helper(service_instance *const instance)
767c65ebfc7SToomas Soome {
768c65ebfc7SToomas Soome AuthRecord *st = instance->subtypes;
769c65ebfc7SToomas Soome ExtraResourceRecord *e;
770c65ebfc7SToomas Soome int i;
771*472cd20dSToomas Soome const pid_t requestPID = instance->request->process_id;
772c65ebfc7SToomas Soome
773c65ebfc7SToomas Soome if (mDNSIPPortIsZero(instance->request->u.servicereg.port))
774c65ebfc7SToomas Soome {
775c65ebfc7SToomas Soome LogInfo("external_start_advertising_helper: Not registering service with port number zero");
776c65ebfc7SToomas Soome return;
777c65ebfc7SToomas Soome }
778c65ebfc7SToomas Soome
779c65ebfc7SToomas Soome if (instance->external_advertise) LogMsg("external_start_advertising_helper: external_advertise already set!");
780c65ebfc7SToomas Soome
781c65ebfc7SToomas Soome for ( i = 0; i < instance->request->u.servicereg.num_subtypes; i++)
782*472cd20dSToomas Soome external_start_advertising_service(&st[i].resrec, instance->request->flags, requestPID);
783c65ebfc7SToomas Soome
784*472cd20dSToomas Soome external_start_advertising_service(&instance->srs.RR_PTR.resrec, instance->request->flags, requestPID);
785*472cd20dSToomas Soome external_start_advertising_service(&instance->srs.RR_SRV.resrec, instance->request->flags, requestPID);
786*472cd20dSToomas Soome external_start_advertising_service(&instance->srs.RR_TXT.resrec, instance->request->flags, requestPID);
787c65ebfc7SToomas Soome
788c65ebfc7SToomas Soome for (e = instance->srs.Extras; e; e = e->next)
789*472cd20dSToomas Soome external_start_advertising_service(&e->r.resrec, instance->request->flags, requestPID);
790c65ebfc7SToomas Soome
791c65ebfc7SToomas Soome instance->external_advertise = mDNStrue;
792c65ebfc7SToomas Soome }
793c65ebfc7SToomas Soome
external_stop_advertising_helper(service_instance * const instance)794c65ebfc7SToomas Soome mDNSlocal void external_stop_advertising_helper(service_instance *const instance)
795c65ebfc7SToomas Soome {
796c65ebfc7SToomas Soome AuthRecord *st = instance->subtypes;
797c65ebfc7SToomas Soome ExtraResourceRecord *e;
798c65ebfc7SToomas Soome int i;
799c65ebfc7SToomas Soome
800c65ebfc7SToomas Soome if (!instance->external_advertise) return;
801c65ebfc7SToomas Soome
802c65ebfc7SToomas Soome LogInfo("external_stop_advertising_helper: calling external_stop_advertising_service");
803c65ebfc7SToomas Soome
804*472cd20dSToomas Soome if (instance->request)
805*472cd20dSToomas Soome {
806*472cd20dSToomas Soome const pid_t requestPID = instance->request->process_id;
807c65ebfc7SToomas Soome for (i = 0; i < instance->request->u.servicereg.num_subtypes; i++)
808*472cd20dSToomas Soome {
809*472cd20dSToomas Soome external_stop_advertising_service(&st[i].resrec, instance->request->flags, requestPID);
810*472cd20dSToomas Soome }
811c65ebfc7SToomas Soome
812*472cd20dSToomas Soome external_stop_advertising_service(&instance->srs.RR_PTR.resrec, instance->request->flags, requestPID);
813*472cd20dSToomas Soome external_stop_advertising_service(&instance->srs.RR_SRV.resrec, instance->request->flags, requestPID);
814*472cd20dSToomas Soome external_stop_advertising_service(&instance->srs.RR_TXT.resrec, instance->request->flags, requestPID);
815c65ebfc7SToomas Soome
816c65ebfc7SToomas Soome for (e = instance->srs.Extras; e; e = e->next)
817*472cd20dSToomas Soome {
818*472cd20dSToomas Soome external_stop_advertising_service(&e->r.resrec, instance->request->flags, requestPID);
819*472cd20dSToomas Soome }
820*472cd20dSToomas Soome }
821c65ebfc7SToomas Soome
822c65ebfc7SToomas Soome instance->external_advertise = mDNSfalse;
823c65ebfc7SToomas Soome }
824*472cd20dSToomas Soome #endif // MDNSRESPONDER_SUPPORTS(APPLE, D2D)
825*472cd20dSToomas Soome
826*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, TRUST_ENFORCEMENT)
_get_trust_results_dispatch_queue(void)827*472cd20dSToomas Soome mDNSlocal dispatch_queue_t _get_trust_results_dispatch_queue(void)
828*472cd20dSToomas Soome {
829*472cd20dSToomas Soome static dispatch_once_t once = 0;
830*472cd20dSToomas Soome static dispatch_queue_t queue = NULL;
831*472cd20dSToomas Soome
832*472cd20dSToomas Soome dispatch_once(&once, ^{
833*472cd20dSToomas Soome dispatch_queue_attr_t const attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_UTILITY, 0);
834*472cd20dSToomas Soome queue = dispatch_queue_create("com.apple.mDNSResponder.trust_results-queue", attr);
835*472cd20dSToomas Soome });
836*472cd20dSToomas Soome return queue;
837*472cd20dSToomas Soome }
838*472cd20dSToomas Soome #endif
839c65ebfc7SToomas Soome
840c65ebfc7SToomas Soome // ***************************************************************************
841c65ebfc7SToomas Soome #if COMPILER_LIKES_PRAGMA_MARK
842c65ebfc7SToomas Soome #pragma mark -
843c65ebfc7SToomas Soome #pragma mark - DNSServiceRegister
844c65ebfc7SToomas Soome #endif
845c65ebfc7SToomas Soome
FreeExtraRR(mDNS * const m,AuthRecord * const rr,mStatus result)846c65ebfc7SToomas Soome mDNSexport void FreeExtraRR(mDNS *const m, AuthRecord *const rr, mStatus result)
847c65ebfc7SToomas Soome {
848c65ebfc7SToomas Soome ExtraResourceRecord *extra = (ExtraResourceRecord *)rr->RecordContext;
849c65ebfc7SToomas Soome (void)m; // Unused
850c65ebfc7SToomas Soome
851c65ebfc7SToomas Soome if (result != mStatus_MemFree) { LogMsg("Error: FreeExtraRR invoked with unexpected error %d", result); return; }
852c65ebfc7SToomas Soome
853c65ebfc7SToomas Soome LogInfo(" FreeExtraRR %s", RRDisplayString(m, &rr->resrec));
854c65ebfc7SToomas Soome
855c65ebfc7SToomas Soome if (rr->resrec.rdata != &rr->rdatastorage)
856c65ebfc7SToomas Soome freeL("Extra RData", rr->resrec.rdata);
857c65ebfc7SToomas Soome freeL("ExtraResourceRecord/FreeExtraRR", extra);
858c65ebfc7SToomas Soome }
859c65ebfc7SToomas Soome
unlink_and_free_service_instance(service_instance * srv)860c65ebfc7SToomas Soome mDNSlocal void unlink_and_free_service_instance(service_instance *srv)
861c65ebfc7SToomas Soome {
862c65ebfc7SToomas Soome ExtraResourceRecord *e = srv->srs.Extras, *tmp;
863c65ebfc7SToomas Soome
864*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, D2D)
865c65ebfc7SToomas Soome external_stop_advertising_helper(srv);
866*472cd20dSToomas Soome #endif
867c65ebfc7SToomas Soome
868c65ebfc7SToomas Soome // clear pointers from parent struct
869c65ebfc7SToomas Soome if (srv->request)
870c65ebfc7SToomas Soome {
871c65ebfc7SToomas Soome service_instance **p = &srv->request->u.servicereg.instances;
872c65ebfc7SToomas Soome while (*p)
873c65ebfc7SToomas Soome {
874c65ebfc7SToomas Soome if (*p == srv) { *p = (*p)->next; break; }
875c65ebfc7SToomas Soome p = &(*p)->next;
876c65ebfc7SToomas Soome }
877c65ebfc7SToomas Soome }
878c65ebfc7SToomas Soome
879c65ebfc7SToomas Soome while (e)
880c65ebfc7SToomas Soome {
881c65ebfc7SToomas Soome e->r.RecordContext = e;
882c65ebfc7SToomas Soome tmp = e;
883c65ebfc7SToomas Soome e = e->next;
884c65ebfc7SToomas Soome FreeExtraRR(&mDNSStorage, &tmp->r, mStatus_MemFree);
885c65ebfc7SToomas Soome }
886c65ebfc7SToomas Soome
887c65ebfc7SToomas Soome if (srv->srs.RR_TXT.resrec.rdata != &srv->srs.RR_TXT.rdatastorage)
888c65ebfc7SToomas Soome freeL("TXT RData", srv->srs.RR_TXT.resrec.rdata);
889c65ebfc7SToomas Soome
890c65ebfc7SToomas Soome if (srv->subtypes)
891c65ebfc7SToomas Soome {
892c65ebfc7SToomas Soome freeL("ServiceSubTypes", srv->subtypes);
893c65ebfc7SToomas Soome srv->subtypes = NULL;
894c65ebfc7SToomas Soome }
895c65ebfc7SToomas Soome freeL("service_instance", srv);
896c65ebfc7SToomas Soome }
897c65ebfc7SToomas Soome
898c65ebfc7SToomas Soome // Count how many other service records we have locally with the same name, but different rdata.
899c65ebfc7SToomas Soome // For auto-named services, we can have at most one per machine -- if we allowed two auto-named services of
900c65ebfc7SToomas Soome // the same type on the same machine, we'd get into an infinite autoimmune-response loop of continuous renaming.
CountPeerRegistrations(ServiceRecordSet * const srs)901c65ebfc7SToomas Soome mDNSexport int CountPeerRegistrations(ServiceRecordSet *const srs)
902c65ebfc7SToomas Soome {
903c65ebfc7SToomas Soome int count = 0;
904c65ebfc7SToomas Soome ResourceRecord *r = &srs->RR_SRV.resrec;
905c65ebfc7SToomas Soome AuthRecord *rr;
906c65ebfc7SToomas Soome
907c65ebfc7SToomas Soome for (rr = mDNSStorage.ResourceRecords; rr; rr=rr->next)
908c65ebfc7SToomas Soome if (rr->resrec.rrtype == kDNSType_SRV && SameDomainName(rr->resrec.name, r->name) && !IdenticalSameNameRecord(&rr->resrec, r))
909c65ebfc7SToomas Soome count++;
910c65ebfc7SToomas Soome
911c65ebfc7SToomas Soome verbosedebugf("%d peer registrations for %##s", count, r->name->c);
912c65ebfc7SToomas Soome return(count);
913c65ebfc7SToomas Soome }
914c65ebfc7SToomas Soome
CountExistingRegistrations(domainname * srv,mDNSIPPort port)915c65ebfc7SToomas Soome mDNSexport int CountExistingRegistrations(domainname *srv, mDNSIPPort port)
916c65ebfc7SToomas Soome {
917c65ebfc7SToomas Soome int count = 0;
918c65ebfc7SToomas Soome AuthRecord *rr;
919c65ebfc7SToomas Soome for (rr = mDNSStorage.ResourceRecords; rr; rr=rr->next)
920c65ebfc7SToomas Soome if (rr->resrec.rrtype == kDNSType_SRV &&
921c65ebfc7SToomas Soome mDNSSameIPPort(rr->resrec.rdata->u.srv.port, port) &&
922c65ebfc7SToomas Soome SameDomainName(rr->resrec.name, srv))
923c65ebfc7SToomas Soome count++;
924c65ebfc7SToomas Soome return(count);
925c65ebfc7SToomas Soome }
926c65ebfc7SToomas Soome
SendServiceRemovalNotification(ServiceRecordSet * const srs)927c65ebfc7SToomas Soome mDNSlocal void SendServiceRemovalNotification(ServiceRecordSet *const srs)
928c65ebfc7SToomas Soome {
929c65ebfc7SToomas Soome reply_state *rep;
930c65ebfc7SToomas Soome service_instance *instance = srs->ServiceContext;
931c65ebfc7SToomas Soome if (GenerateNTDResponse(srs->RR_SRV.resrec.name, srs->RR_SRV.resrec.InterfaceID, instance->request, &rep, reg_service_reply_op, 0, mStatus_NoError) != mStatus_NoError)
932c65ebfc7SToomas Soome LogMsg("%3d: SendServiceRemovalNotification: %##s is not valid DNS-SD SRV name", instance->request->sd, srs->RR_SRV.resrec.name->c);
933c65ebfc7SToomas Soome else { append_reply(instance->request, rep); instance->clientnotified = mDNSfalse; }
934c65ebfc7SToomas Soome }
935c65ebfc7SToomas Soome
936c65ebfc7SToomas Soome // service registration callback performs three duties - frees memory for deregistered services,
937c65ebfc7SToomas Soome // handles name conflicts, and delivers completed registration information to the client
regservice_callback(mDNS * const m,ServiceRecordSet * const srs,mStatus result)938c65ebfc7SToomas Soome mDNSlocal void regservice_callback(mDNS *const m, ServiceRecordSet *const srs, mStatus result)
939c65ebfc7SToomas Soome {
940c65ebfc7SToomas Soome mStatus err;
941c65ebfc7SToomas Soome mDNSBool SuppressError = mDNSfalse;
942c65ebfc7SToomas Soome service_instance *instance;
943c65ebfc7SToomas Soome reply_state *rep;
944c65ebfc7SToomas Soome (void)m; // Unused
945c65ebfc7SToomas Soome
946*472cd20dSToomas Soome if (!srs)
947*472cd20dSToomas Soome {
948*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT, "regservice_callback: srs is NULL %d", result);
949*472cd20dSToomas Soome return;
950*472cd20dSToomas Soome }
951c65ebfc7SToomas Soome
952c65ebfc7SToomas Soome instance = srs->ServiceContext;
953*472cd20dSToomas Soome if (!instance)
954*472cd20dSToomas Soome {
955*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT, "regservice_callback: srs->ServiceContext is NULL %d", result);
956*472cd20dSToomas Soome return;
957*472cd20dSToomas Soome }
958c65ebfc7SToomas Soome
959c65ebfc7SToomas Soome // don't send errors up to client for wide-area, empty-string registrations
960c65ebfc7SToomas Soome if (instance->request &&
961c65ebfc7SToomas Soome instance->request->u.servicereg.default_domain &&
962c65ebfc7SToomas Soome !instance->default_local)
963c65ebfc7SToomas Soome SuppressError = mDNStrue;
964c65ebfc7SToomas Soome
965c65ebfc7SToomas Soome if (mDNS_LoggingEnabled)
966c65ebfc7SToomas Soome {
967*472cd20dSToomas Soome const char *result_description;
968*472cd20dSToomas Soome char description[32]; // 32-byte is enough for holding "suppressed error -2147483648\0"
969*472cd20dSToomas Soome mDNSu32 request_id = instance->request ? instance->request->request_id : 0;
970*472cd20dSToomas Soome switch (result) {
971*472cd20dSToomas Soome case mStatus_NoError:
972*472cd20dSToomas Soome result_description = "REGISTERED";
973*472cd20dSToomas Soome break;
974*472cd20dSToomas Soome case mStatus_MemFree:
975*472cd20dSToomas Soome result_description = "DEREGISTERED";
976*472cd20dSToomas Soome break;
977*472cd20dSToomas Soome case mStatus_NameConflict:
978*472cd20dSToomas Soome result_description = "NAME CONFLICT";
979*472cd20dSToomas Soome break;
980*472cd20dSToomas Soome default:
981*472cd20dSToomas Soome mDNS_snprintf(description, sizeof(description), "%s %d", SuppressError ? "suppressed error" : "CALLBACK", result);
982*472cd20dSToomas Soome result_description = description;
983*472cd20dSToomas Soome break;
984*472cd20dSToomas Soome }
985*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO, "[R%u] DNSServiceRegister(" PRI_DM_NAME ", %u) %s",
986*472cd20dSToomas Soome request_id, DM_NAME_PARAM(srs->RR_SRV.resrec.name), mDNSVal16(srs->RR_SRV.resrec.rdata->u.srv.port), result_description);
987c65ebfc7SToomas Soome }
988c65ebfc7SToomas Soome
989*472cd20dSToomas Soome if (!instance->request && result != mStatus_MemFree)
990*472cd20dSToomas Soome {
991*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT, "regservice_callback: instance->request is NULL %d", result);
992*472cd20dSToomas Soome return;
993*472cd20dSToomas Soome }
994c65ebfc7SToomas Soome
995c65ebfc7SToomas Soome if (result == mStatus_NoError)
996c65ebfc7SToomas Soome {
997c65ebfc7SToomas Soome if (instance->request->u.servicereg.allowremotequery)
998c65ebfc7SToomas Soome {
999c65ebfc7SToomas Soome ExtraResourceRecord *e;
1000c65ebfc7SToomas Soome srs->RR_ADV.AllowRemoteQuery = mDNStrue;
1001c65ebfc7SToomas Soome srs->RR_PTR.AllowRemoteQuery = mDNStrue;
1002c65ebfc7SToomas Soome srs->RR_SRV.AllowRemoteQuery = mDNStrue;
1003c65ebfc7SToomas Soome srs->RR_TXT.AllowRemoteQuery = mDNStrue;
1004c65ebfc7SToomas Soome for (e = instance->srs.Extras; e; e = e->next) e->r.AllowRemoteQuery = mDNStrue;
1005c65ebfc7SToomas Soome }
1006c65ebfc7SToomas Soome
1007c65ebfc7SToomas Soome if (GenerateNTDResponse(srs->RR_SRV.resrec.name, srs->RR_SRV.resrec.InterfaceID, instance->request, &rep, reg_service_reply_op, kDNSServiceFlagsAdd, result) != mStatus_NoError)
1008*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT, "[R%u] regservice_callback: " PRI_DM_NAME " is not valid DNS-SD SRV name", instance->request->request_id, DM_NAME_PARAM(srs->RR_SRV.resrec.name));
1009c65ebfc7SToomas Soome else { append_reply(instance->request, rep); instance->clientnotified = mDNStrue; }
1010c65ebfc7SToomas Soome
1011*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, D2D)
1012c65ebfc7SToomas Soome if (callExternalHelpers(instance->request->u.servicereg.InterfaceID, &instance->domain, instance->request->flags))
1013c65ebfc7SToomas Soome {
1014*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO, "[R%u] regservice_callback: calling external_start_advertising_helper()", instance->request->request_id);
1015c65ebfc7SToomas Soome external_start_advertising_helper(instance);
1016c65ebfc7SToomas Soome }
1017*472cd20dSToomas Soome #endif
1018c65ebfc7SToomas Soome if (instance->request->u.servicereg.autoname && CountPeerRegistrations(srs) == 0)
1019c65ebfc7SToomas Soome RecordUpdatedNiceLabel(0); // Successfully got new name, tell user immediately
1020c65ebfc7SToomas Soome }
1021c65ebfc7SToomas Soome else if (result == mStatus_MemFree)
1022c65ebfc7SToomas Soome {
1023*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, METRICS)
1024c65ebfc7SToomas Soome curr_num_regservices--;
1025c65ebfc7SToomas Soome #endif
1026c65ebfc7SToomas Soome if (instance->request && instance->renameonmemfree)
1027c65ebfc7SToomas Soome {
1028*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, D2D)
1029c65ebfc7SToomas Soome external_stop_advertising_helper(instance);
1030*472cd20dSToomas Soome #endif
1031c65ebfc7SToomas Soome instance->renameonmemfree = 0;
1032c65ebfc7SToomas Soome err = mDNS_RenameAndReregisterService(m, srs, &instance->request->u.servicereg.name);
1033*472cd20dSToomas Soome if (err)
1034*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT, "[R%u] ERROR: regservice_callback - RenameAndReregisterService returned %d", instance->request->request_id, err);
1035c65ebfc7SToomas Soome // error should never happen - safest to log and continue
1036c65ebfc7SToomas Soome }
1037c65ebfc7SToomas Soome else
1038c65ebfc7SToomas Soome unlink_and_free_service_instance(instance);
1039c65ebfc7SToomas Soome }
1040c65ebfc7SToomas Soome else if (result == mStatus_NameConflict)
1041c65ebfc7SToomas Soome {
1042c65ebfc7SToomas Soome if (instance->request->u.servicereg.autorename)
1043c65ebfc7SToomas Soome {
1044*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, D2D)
1045c65ebfc7SToomas Soome external_stop_advertising_helper(instance);
1046*472cd20dSToomas Soome #endif
1047c65ebfc7SToomas Soome if (instance->request->u.servicereg.autoname && CountPeerRegistrations(srs) == 0)
1048c65ebfc7SToomas Soome {
1049c65ebfc7SToomas Soome // On conflict for an autoname service, rename and reregister *all* autoname services
1050c65ebfc7SToomas Soome IncrementLabelSuffix(&m->nicelabel, mDNStrue);
1051c65ebfc7SToomas Soome mDNS_ConfigChanged(m); // Will call back into udsserver_handle_configchange()
1052c65ebfc7SToomas Soome }
1053c65ebfc7SToomas Soome else // On conflict for a non-autoname service, rename and reregister just that one service
1054c65ebfc7SToomas Soome {
1055c65ebfc7SToomas Soome if (instance->clientnotified) SendServiceRemovalNotification(srs);
1056c65ebfc7SToomas Soome mDNS_RenameAndReregisterService(m, srs, mDNSNULL);
1057c65ebfc7SToomas Soome }
1058c65ebfc7SToomas Soome }
1059c65ebfc7SToomas Soome else
1060c65ebfc7SToomas Soome {
1061c65ebfc7SToomas Soome if (!SuppressError)
1062c65ebfc7SToomas Soome {
1063c65ebfc7SToomas Soome if (GenerateNTDResponse(srs->RR_SRV.resrec.name, srs->RR_SRV.resrec.InterfaceID, instance->request, &rep, reg_service_reply_op, kDNSServiceFlagsAdd, result) != mStatus_NoError)
1064*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT, "[R%u] regservice_callback: " PRI_DM_NAME " is not valid DNS-SD SRV name", instance->request->request_id, DM_NAME_PARAM(srs->RR_SRV.resrec.name));
1065c65ebfc7SToomas Soome else { append_reply(instance->request, rep); instance->clientnotified = mDNStrue; }
1066c65ebfc7SToomas Soome }
1067c65ebfc7SToomas Soome unlink_and_free_service_instance(instance);
1068c65ebfc7SToomas Soome }
1069c65ebfc7SToomas Soome }
1070c65ebfc7SToomas Soome else // Not mStatus_NoError, mStatus_MemFree, or mStatus_NameConflict
1071c65ebfc7SToomas Soome {
1072c65ebfc7SToomas Soome if (!SuppressError)
1073c65ebfc7SToomas Soome {
1074c65ebfc7SToomas Soome if (GenerateNTDResponse(srs->RR_SRV.resrec.name, srs->RR_SRV.resrec.InterfaceID, instance->request, &rep, reg_service_reply_op, kDNSServiceFlagsAdd, result) != mStatus_NoError)
1075*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT, "[R%u] regservice_callback: " PRI_DM_NAME " is not valid DNS-SD SRV name", instance->request->request_id, DM_NAME_PARAM(srs->RR_SRV.resrec.name));
1076c65ebfc7SToomas Soome else { append_reply(instance->request, rep); instance->clientnotified = mDNStrue; }
1077c65ebfc7SToomas Soome }
1078c65ebfc7SToomas Soome }
1079c65ebfc7SToomas Soome }
1080c65ebfc7SToomas Soome
regrecord_callback(mDNS * const m,AuthRecord * rr,mStatus result)1081c65ebfc7SToomas Soome mDNSlocal void regrecord_callback(mDNS *const m, AuthRecord *rr, mStatus result)
1082c65ebfc7SToomas Soome {
1083c65ebfc7SToomas Soome (void)m; // Unused
1084c65ebfc7SToomas Soome if (!rr->RecordContext) // parent struct already freed by termination callback
1085c65ebfc7SToomas Soome {
1086c65ebfc7SToomas Soome if (result == mStatus_NoError)
1087*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT, "Error: regrecord_callback: successful registration of orphaned record " PRI_S, ARDisplayString(m, rr));
1088c65ebfc7SToomas Soome else
1089c65ebfc7SToomas Soome {
1090*472cd20dSToomas Soome if (result != mStatus_MemFree)
1091*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT, "regrecord_callback: error %d received after parent termination", result);
1092c65ebfc7SToomas Soome
1093c65ebfc7SToomas Soome // We come here when the record is being deregistered either from DNSServiceRemoveRecord or connection_termination.
1094c65ebfc7SToomas Soome // If the record has been updated, we need to free the rdata. Every time we call mDNS_Update, it calls update_callback
1095c65ebfc7SToomas Soome // with the old rdata (so that we can free it) and stores the new rdata in "rr->resrec.rdata". This means, we need
1096c65ebfc7SToomas Soome // to free the latest rdata for which the update_callback was never called with.
1097c65ebfc7SToomas Soome if (rr->resrec.rdata != &rr->rdatastorage) freeL("RData/regrecord_callback", rr->resrec.rdata);
1098c65ebfc7SToomas Soome freeL("AuthRecord/regrecord_callback", rr);
1099c65ebfc7SToomas Soome }
1100c65ebfc7SToomas Soome }
1101c65ebfc7SToomas Soome else
1102c65ebfc7SToomas Soome {
1103c65ebfc7SToomas Soome registered_record_entry *re = rr->RecordContext;
1104c65ebfc7SToomas Soome request_state *request = re->request;
1105c65ebfc7SToomas Soome
1106c65ebfc7SToomas Soome if (mDNS_LoggingEnabled)
1107c65ebfc7SToomas Soome {
1108*472cd20dSToomas Soome const char *result_description;
1109*472cd20dSToomas Soome char description[16]; // 16-byte is enough for holding -2147483648\0
1110*472cd20dSToomas Soome switch (result) {
1111*472cd20dSToomas Soome case mStatus_NoError:
1112*472cd20dSToomas Soome result_description = "REGISTERED";
1113*472cd20dSToomas Soome break;
1114*472cd20dSToomas Soome case mStatus_MemFree:
1115*472cd20dSToomas Soome result_description = "DEREGISTERED";
1116*472cd20dSToomas Soome break;
1117*472cd20dSToomas Soome case mStatus_NameConflict:
1118*472cd20dSToomas Soome result_description = "NAME CONFLICT";
1119*472cd20dSToomas Soome break;
1120*472cd20dSToomas Soome default:
1121*472cd20dSToomas Soome mDNS_snprintf(description, sizeof(description), "%d", result);
1122*472cd20dSToomas Soome result_description = description;
1123*472cd20dSToomas Soome break;
1124*472cd20dSToomas Soome }
1125*472cd20dSToomas Soome
1126*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO, "[R%u] DNSServiceRegisterRecord(%u " PRI_S ")" PUB_S,
1127*472cd20dSToomas Soome request->request_id, re->key, RRDisplayString(m, &rr->resrec), result_description);
1128c65ebfc7SToomas Soome }
1129c65ebfc7SToomas Soome
1130c65ebfc7SToomas Soome if (result != mStatus_MemFree)
1131c65ebfc7SToomas Soome {
1132c65ebfc7SToomas Soome int len = sizeof(DNSServiceFlags) + sizeof(mDNSu32) + sizeof(DNSServiceErrorType);
1133c65ebfc7SToomas Soome reply_state *reply = create_reply(reg_record_reply_op, len, request);
1134c65ebfc7SToomas Soome reply->mhdr->client_context = re->regrec_client_context;
1135c65ebfc7SToomas Soome reply->rhdr->flags = dnssd_htonl(0);
1136c65ebfc7SToomas Soome reply->rhdr->ifi = dnssd_htonl(mDNSPlatformInterfaceIndexfromInterfaceID(m, rr->resrec.InterfaceID, mDNSfalse));
1137c65ebfc7SToomas Soome reply->rhdr->error = dnssd_htonl(result);
1138c65ebfc7SToomas Soome append_reply(request, reply);
1139c65ebfc7SToomas Soome }
1140c65ebfc7SToomas Soome
1141c65ebfc7SToomas Soome if (result)
1142c65ebfc7SToomas Soome {
1143c65ebfc7SToomas Soome // If this is a callback to a keepalive record, do not free it.
1144c65ebfc7SToomas Soome if (result == mStatus_BadStateErr)
1145c65ebfc7SToomas Soome {
1146*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
1147*472cd20dSToomas Soome "[R%u] regrecord_callback: Callback with error code mStatus_BadStateErr - not freeing the record.", request->request_id);
1148c65ebfc7SToomas Soome }
1149c65ebfc7SToomas Soome else
1150c65ebfc7SToomas Soome {
1151c65ebfc7SToomas Soome // unlink from list, free memory
1152c65ebfc7SToomas Soome registered_record_entry **ptr = &request->u.reg_recs;
1153c65ebfc7SToomas Soome while (*ptr && (*ptr) != re) ptr = &(*ptr)->next;
1154*472cd20dSToomas Soome if (!*ptr)
1155*472cd20dSToomas Soome {
1156*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT,
1157*472cd20dSToomas Soome "[R%u] regrecord_callback - record not in list!", request->request_id);
1158*472cd20dSToomas Soome return;
1159*472cd20dSToomas Soome }
1160c65ebfc7SToomas Soome *ptr = (*ptr)->next;
1161c65ebfc7SToomas Soome freeL("registered_record_entry AuthRecord regrecord_callback", re->rr);
1162c65ebfc7SToomas Soome freeL("registered_record_entry regrecord_callback", re);
1163c65ebfc7SToomas Soome }
1164c65ebfc7SToomas Soome }
1165c65ebfc7SToomas Soome else
1166c65ebfc7SToomas Soome {
1167*472cd20dSToomas Soome if (re->external_advertise)
1168*472cd20dSToomas Soome {
1169*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT,
1170*472cd20dSToomas Soome "[R%u] regrecord_callback: external_advertise already set!", request->request_id);
1171*472cd20dSToomas Soome }
1172c65ebfc7SToomas Soome
1173*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, D2D)
1174c65ebfc7SToomas Soome if (callExternalHelpers(re->origInterfaceID, &rr->namestorage, request->flags))
1175c65ebfc7SToomas Soome {
1176*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
1177*472cd20dSToomas Soome "[R%u] regrecord_callback: calling external_start_advertising_service", request->request_id);
1178*472cd20dSToomas Soome external_start_advertising_service(&rr->resrec, request->flags, request->process_id);
1179c65ebfc7SToomas Soome re->external_advertise = mDNStrue;
1180c65ebfc7SToomas Soome }
1181*472cd20dSToomas Soome #endif
1182c65ebfc7SToomas Soome }
1183c65ebfc7SToomas Soome }
1184c65ebfc7SToomas Soome }
1185c65ebfc7SToomas Soome
1186c65ebfc7SToomas Soome // set_peer_pid() is called after mem is allocated for each new request in NewRequest()
1187c65ebfc7SToomas Soome // This accounts for 2 places (connect_callback, request_callback)
set_peer_pid(request_state * request)1188c65ebfc7SToomas Soome mDNSlocal void set_peer_pid(request_state *request)
1189c65ebfc7SToomas Soome {
1190c65ebfc7SToomas Soome request->pid_name[0] = '\0';
1191c65ebfc7SToomas Soome request->process_id = -1;
1192c65ebfc7SToomas Soome #ifdef LOCAL_PEEREPID
1193*472cd20dSToomas Soome pid_t p = (pid_t) -1;
1194*472cd20dSToomas Soome socklen_t len = sizeof(p);
1195c65ebfc7SToomas Soome if (request->sd < 0)
1196c65ebfc7SToomas Soome return;
1197c65ebfc7SToomas Soome // to extract the effective pid value
1198c65ebfc7SToomas Soome if (getsockopt(request->sd, SOL_LOCAL, LOCAL_PEEREPID, &p, &len) != 0)
1199c65ebfc7SToomas Soome return;
1200c65ebfc7SToomas Soome // to extract the process name from the pid value
1201c65ebfc7SToomas Soome if (proc_pidinfo(p, PROC_PIDT_SHORTBSDINFO, 1, &proc, PROC_PIDT_SHORTBSDINFO_SIZE) == 0)
1202c65ebfc7SToomas Soome return;
1203c65ebfc7SToomas Soome mDNSPlatformStrLCopy(request->pid_name, proc.pbsi_comm, sizeof(request->pid_name));
1204c65ebfc7SToomas Soome request->process_id = p;
1205c65ebfc7SToomas Soome debugf("set_peer_pid: Client PEEREPID is %d %s", p, request->pid_name);
1206c65ebfc7SToomas Soome #else // !LOCAL_PEEREPID
1207c65ebfc7SToomas Soome LogInfo("set_peer_pid: Not Supported on this version of OS");
1208c65ebfc7SToomas Soome if (request->sd < 0)
1209c65ebfc7SToomas Soome return;
1210c65ebfc7SToomas Soome #endif // LOCAL_PEEREPID
1211c65ebfc7SToomas Soome }
1212c65ebfc7SToomas Soome
connection_termination(request_state * request)1213c65ebfc7SToomas Soome mDNSlocal void connection_termination(request_state *request)
1214c65ebfc7SToomas Soome {
1215c65ebfc7SToomas Soome // When terminating a shared connection, we need to scan the all_requests list
1216c65ebfc7SToomas Soome // and terminate any subbordinate operations sharing this file descriptor
1217c65ebfc7SToomas Soome request_state **req = &all_requests;
1218c65ebfc7SToomas Soome
1219*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
1220*472cd20dSToomas Soome "[R%d] DNSServiceCreateConnection STOP PID[%d](" PUB_S ")",
1221*472cd20dSToomas Soome request->request_id, request->process_id, request->pid_name);
1222c65ebfc7SToomas Soome
1223c65ebfc7SToomas Soome while (*req)
1224c65ebfc7SToomas Soome {
1225c65ebfc7SToomas Soome if ((*req)->primary == request)
1226c65ebfc7SToomas Soome {
1227c65ebfc7SToomas Soome // Since we're already doing a list traversal, we unlink the request directly instead of using AbortUnlinkAndFree()
1228c65ebfc7SToomas Soome request_state *tmp = *req;
1229c65ebfc7SToomas Soome if (tmp->primary == tmp) LogMsg("connection_termination ERROR (*req)->primary == *req for %p %d", tmp, tmp->sd);
1230c65ebfc7SToomas Soome if (tmp->replies) LogMsg("connection_termination ERROR How can subordinate req %p %d have replies queued?", tmp, tmp->sd);
1231c65ebfc7SToomas Soome abort_request(tmp);
1232c65ebfc7SToomas Soome *req = tmp->next;
1233*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, TRUST_ENFORCEMENT)
1234*472cd20dSToomas Soome if (tmp->trust)
1235*472cd20dSToomas Soome {
1236*472cd20dSToomas Soome void * context = mdns_trust_get_context(tmp->trust);
1237*472cd20dSToomas Soome mdns_trust_set_context(tmp->trust, NULL);
1238*472cd20dSToomas Soome if (context) freeL("context/connection_termination", context);
1239*472cd20dSToomas Soome mdns_trust_forget(&tmp->trust);
1240*472cd20dSToomas Soome }
1241*472cd20dSToomas Soome #endif
1242c65ebfc7SToomas Soome freeL("request_state/connection_termination", tmp);
1243c65ebfc7SToomas Soome }
1244c65ebfc7SToomas Soome else
1245c65ebfc7SToomas Soome req = &(*req)->next;
1246c65ebfc7SToomas Soome }
1247c65ebfc7SToomas Soome
1248c65ebfc7SToomas Soome while (request->u.reg_recs)
1249c65ebfc7SToomas Soome {
1250c65ebfc7SToomas Soome registered_record_entry *ptr = request->u.reg_recs;
1251*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
1252*472cd20dSToomas Soome "[R%d] DNSServiceRegisterRecord(0x%X, %d, " PRI_S ") STOP PID[%d](" PUB_S ")",
1253*472cd20dSToomas Soome request->request_id, request->flags, request->interfaceIndex, RRDisplayString(&mDNSStorage, &ptr->rr->resrec), request->process_id,
1254*472cd20dSToomas Soome request->pid_name);
1255c65ebfc7SToomas Soome request->u.reg_recs = request->u.reg_recs->next;
1256c65ebfc7SToomas Soome ptr->rr->RecordContext = NULL;
1257c65ebfc7SToomas Soome if (ptr->external_advertise)
1258c65ebfc7SToomas Soome {
1259c65ebfc7SToomas Soome ptr->external_advertise = mDNSfalse;
1260*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, D2D)
1261*472cd20dSToomas Soome external_stop_advertising_service(&ptr->rr->resrec, request->flags, request->process_id);
1262*472cd20dSToomas Soome #endif
1263c65ebfc7SToomas Soome }
1264c65ebfc7SToomas Soome LogMcastS(ptr->rr, request, reg_stop);
1265c65ebfc7SToomas Soome mDNS_Deregister(&mDNSStorage, ptr->rr); // Will free ptr->rr for us
1266c65ebfc7SToomas Soome freeL("registered_record_entry/connection_termination", ptr);
1267c65ebfc7SToomas Soome }
1268c65ebfc7SToomas Soome }
1269c65ebfc7SToomas Soome
handle_cancel_request(request_state * request)1270c65ebfc7SToomas Soome mDNSlocal void handle_cancel_request(request_state *request)
1271c65ebfc7SToomas Soome {
1272c65ebfc7SToomas Soome request_state **req = &all_requests;
1273*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEBUG, "[R%d] Cancel %08X %08X",
1274*472cd20dSToomas Soome request->request_id, request->hdr.client_context.u32[1], request->hdr.client_context.u32[0]);
1275c65ebfc7SToomas Soome while (*req)
1276c65ebfc7SToomas Soome {
1277c65ebfc7SToomas Soome if ((*req)->primary == request &&
1278c65ebfc7SToomas Soome (*req)->hdr.client_context.u32[0] == request->hdr.client_context.u32[0] &&
1279c65ebfc7SToomas Soome (*req)->hdr.client_context.u32[1] == request->hdr.client_context.u32[1])
1280c65ebfc7SToomas Soome {
1281c65ebfc7SToomas Soome // Since we're already doing a list traversal, we unlink the request directly instead of using AbortUnlinkAndFree()
1282c65ebfc7SToomas Soome request_state *tmp = *req;
1283c65ebfc7SToomas Soome abort_request(tmp);
1284c65ebfc7SToomas Soome *req = tmp->next;
1285*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, TRUST_ENFORCEMENT)
1286*472cd20dSToomas Soome if (tmp->trust)
1287*472cd20dSToomas Soome {
1288*472cd20dSToomas Soome void * context = mdns_trust_get_context(tmp->trust);
1289*472cd20dSToomas Soome mdns_trust_set_context(tmp->trust, NULL);
1290*472cd20dSToomas Soome if (context) freeL("context/handle_cancel_request", context);
1291*472cd20dSToomas Soome mdns_trust_forget(&tmp->trust);
1292*472cd20dSToomas Soome }
1293*472cd20dSToomas Soome #endif
1294c65ebfc7SToomas Soome freeL("request_state/handle_cancel_request", tmp);
1295c65ebfc7SToomas Soome }
1296c65ebfc7SToomas Soome else
1297c65ebfc7SToomas Soome req = &(*req)->next;
1298c65ebfc7SToomas Soome }
1299c65ebfc7SToomas Soome }
1300c65ebfc7SToomas Soome
_handle_regrecord_request_start(request_state * request,AuthRecord * rr)1301*472cd20dSToomas Soome mDNSlocal mStatus _handle_regrecord_request_start(request_state *request, AuthRecord * rr)
1302c65ebfc7SToomas Soome {
1303*472cd20dSToomas Soome mStatus err;
1304c65ebfc7SToomas Soome registered_record_entry *re;
1305c65ebfc7SToomas Soome // Don't allow non-local domains to be regsitered as LocalOnly. Allowing this would permit
1306c65ebfc7SToomas Soome // clients to register records such as www.bigbank.com A w.x.y.z to redirect Safari.
1307c65ebfc7SToomas Soome if (rr->resrec.InterfaceID == mDNSInterface_LocalOnly && !IsLocalDomain(rr->resrec.name) &&
1308c65ebfc7SToomas Soome rr->resrec.rrclass == kDNSClass_IN && (rr->resrec.rrtype == kDNSType_A || rr->resrec.rrtype == kDNSType_AAAA ||
1309c65ebfc7SToomas Soome rr->resrec.rrtype == kDNSType_CNAME))
1310c65ebfc7SToomas Soome {
1311c65ebfc7SToomas Soome freeL("AuthRecord/handle_regrecord_request", rr);
1312c65ebfc7SToomas Soome return (mStatus_BadParamErr);
1313c65ebfc7SToomas Soome }
1314c65ebfc7SToomas Soome // allocate registration entry, link into list
1315*472cd20dSToomas Soome re = (registered_record_entry *) callocL("registered_record_entry", sizeof(*re));
1316*472cd20dSToomas Soome if (!re) FatalError("ERROR: calloc");
1317c65ebfc7SToomas Soome re->key = request->hdr.reg_index;
1318c65ebfc7SToomas Soome re->rr = rr;
1319c65ebfc7SToomas Soome re->regrec_client_context = request->hdr.client_context;
1320c65ebfc7SToomas Soome re->request = request;
1321c65ebfc7SToomas Soome re->external_advertise = mDNSfalse;
1322c65ebfc7SToomas Soome rr->RecordContext = re;
1323c65ebfc7SToomas Soome rr->RecordCallback = regrecord_callback;
1324c65ebfc7SToomas Soome
1325c65ebfc7SToomas Soome re->origInterfaceID = rr->resrec.InterfaceID;
1326c65ebfc7SToomas Soome if (rr->resrec.InterfaceID == mDNSInterface_P2P)
1327c65ebfc7SToomas Soome rr->resrec.InterfaceID = mDNSInterface_Any;
1328c65ebfc7SToomas Soome #if 0
1329c65ebfc7SToomas Soome if (!AuthorizedDomain(request, rr->resrec.name, AutoRegistrationDomains)) return (mStatus_NoError);
1330c65ebfc7SToomas Soome #endif
1331c65ebfc7SToomas Soome if (rr->resrec.rroriginalttl == 0)
1332c65ebfc7SToomas Soome rr->resrec.rroriginalttl = DefaultTTLforRRType(rr->resrec.rrtype);
1333c65ebfc7SToomas Soome
1334*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
1335*472cd20dSToomas Soome "[R%d] DNSServiceRegisterRecord(0x%X, %d, " PRI_S ") START PID[%d](" PUB_S ")",
1336*472cd20dSToomas Soome request->request_id, request->flags, request->interfaceIndex, RRDisplayString(&mDNSStorage, &rr->resrec), request->process_id,
1337*472cd20dSToomas Soome request->pid_name);
1338c65ebfc7SToomas Soome
1339c65ebfc7SToomas Soome err = mDNS_Register(&mDNSStorage, rr);
1340c65ebfc7SToomas Soome if (err)
1341c65ebfc7SToomas Soome {
1342*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
1343*472cd20dSToomas Soome "[R%d] DNSServiceRegisterRecord(0x%X, %d," PRI_S ") ERROR (%d)",
1344*472cd20dSToomas Soome request->request_id, request->flags, request->interfaceIndex, RRDisplayString(&mDNSStorage, &rr->resrec), err);
1345c65ebfc7SToomas Soome freeL("registered_record_entry", re);
1346c65ebfc7SToomas Soome freeL("registered_record_entry/AuthRecord", rr);
1347c65ebfc7SToomas Soome }
1348c65ebfc7SToomas Soome else
1349c65ebfc7SToomas Soome {
1350c65ebfc7SToomas Soome LogMcastS(rr, request, reg_start);
1351c65ebfc7SToomas Soome re->next = request->u.reg_recs;
1352c65ebfc7SToomas Soome request->u.reg_recs = re;
1353c65ebfc7SToomas Soome }
1354*472cd20dSToomas Soome return err;
1355*472cd20dSToomas Soome }
1356*472cd20dSToomas Soome
1357*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, TRUST_ENFORCEMENT)
1358*472cd20dSToomas Soome
_return_regrecord_request_error(request_state * request,mStatus error)1359*472cd20dSToomas Soome mDNSlocal void _return_regrecord_request_error(request_state *request, mStatus error)
1360*472cd20dSToomas Soome {
1361*472cd20dSToomas Soome reply_state *rep;
1362*472cd20dSToomas Soome if (GenerateNTDResponse(NULL, 0, request, &rep, reg_record_reply_op, 0, error) != mStatus_NoError)
1363*472cd20dSToomas Soome {
1364*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT, "[R%u] DNSServiceRegisterRecord _return_regrecord_request_error: error(%d)", request->request_id, error);
1365*472cd20dSToomas Soome }
1366*472cd20dSToomas Soome else
1367*472cd20dSToomas Soome {
1368*472cd20dSToomas Soome append_reply(request, rep);
1369*472cd20dSToomas Soome }
1370*472cd20dSToomas Soome }
1371*472cd20dSToomas Soome
_handle_regrecord_request_with_trust(request_state * request,AuthRecord * rr)1372*472cd20dSToomas Soome mDNSlocal mStatus _handle_regrecord_request_with_trust(request_state *request, AuthRecord * rr)
1373*472cd20dSToomas Soome {
1374*472cd20dSToomas Soome mStatus err;
1375*472cd20dSToomas Soome if (audit_token_to_pid(request->audit_token) == 0)
1376*472cd20dSToomas Soome {
1377*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_WARNING, "[R%u] _handle_regrecord_request_with_trust: no audit token for pid(%s %d)", request->request_id, request->pid_name, request->process_id);
1378*472cd20dSToomas Soome err = _handle_regrecord_request_start(request, rr);
1379*472cd20dSToomas Soome }
1380*472cd20dSToomas Soome else
1381*472cd20dSToomas Soome {
1382*472cd20dSToomas Soome const char *service_ptr = NULL;
1383*472cd20dSToomas Soome char type_str[MAX_ESCAPED_DOMAIN_NAME] = "";
1384*472cd20dSToomas Soome domainlabel name;
1385*472cd20dSToomas Soome domainname type, domain;
1386*472cd20dSToomas Soome bool good = DeconstructServiceName(rr->resrec.name, &name, &type, &domain);
1387*472cd20dSToomas Soome if (good)
1388*472cd20dSToomas Soome {
1389*472cd20dSToomas Soome ConvertDomainNameToCString(&type, type_str);
1390*472cd20dSToomas Soome service_ptr = type_str;
1391*472cd20dSToomas Soome }
1392*472cd20dSToomas Soome
1393*472cd20dSToomas Soome mdns_trust_flags_t flags = mdns_trust_flags_none;
1394*472cd20dSToomas Soome mdns_trust_status_t status = mdns_trust_check_bonjour(request->audit_token, service_ptr, &flags);
1395*472cd20dSToomas Soome switch (status)
1396*472cd20dSToomas Soome {
1397*472cd20dSToomas Soome case mdns_trust_status_denied:
1398*472cd20dSToomas Soome case mdns_trust_status_pending:
1399*472cd20dSToomas Soome {
1400*472cd20dSToomas Soome mdns_trust_t trust = mdns_trust_create(request->audit_token, service_ptr, flags);
1401*472cd20dSToomas Soome if (!trust)
1402*472cd20dSToomas Soome {
1403*472cd20dSToomas Soome freeL("AuthRecord/_handle_regrecord_request_with_trust", rr);
1404*472cd20dSToomas Soome err = mStatus_NoMemoryErr;
1405*472cd20dSToomas Soome goto exit;
1406*472cd20dSToomas Soome }
1407*472cd20dSToomas Soome mdns_trust_set_context(trust, rr);
1408*472cd20dSToomas Soome mdns_trust_set_queue(trust, _get_trust_results_dispatch_queue());
1409*472cd20dSToomas Soome mdns_trust_set_event_handler(trust, ^(mdns_trust_event_t event, mdns_trust_status_t update)
1410*472cd20dSToomas Soome {
1411*472cd20dSToomas Soome if (event == mdns_trust_event_result)
1412*472cd20dSToomas Soome {
1413*472cd20dSToomas Soome mStatus error = (update != mdns_trust_status_granted) ? mStatus_PolicyDenied : mStatus_NoError;
1414*472cd20dSToomas Soome KQueueLock();
1415*472cd20dSToomas Soome AuthRecord * _rr = mdns_trust_get_context(trust);
1416*472cd20dSToomas Soome if (_rr)
1417*472cd20dSToomas Soome {
1418*472cd20dSToomas Soome if (!error)
1419*472cd20dSToomas Soome {
1420*472cd20dSToomas Soome mdns_trust_set_context(trust, NULL); // _handle_regrecord_request_start handles free
1421*472cd20dSToomas Soome error = _handle_regrecord_request_start(request, _rr);
1422*472cd20dSToomas Soome // No context means the request was canceled before we got here
1423*472cd20dSToomas Soome }
1424*472cd20dSToomas Soome if (error) // (not else if) Always check for error result
1425*472cd20dSToomas Soome {
1426*472cd20dSToomas Soome _return_regrecord_request_error(request, error);
1427*472cd20dSToomas Soome }
1428*472cd20dSToomas Soome }
1429*472cd20dSToomas Soome KQueueUnlock("_handle_regrecord_request_with_trust");
1430*472cd20dSToomas Soome }
1431*472cd20dSToomas Soome });
1432*472cd20dSToomas Soome request->trust = trust;
1433*472cd20dSToomas Soome mdns_trust_activate(trust);
1434*472cd20dSToomas Soome err = mStatus_NoError;
1435*472cd20dSToomas Soome break;
1436*472cd20dSToomas Soome }
1437*472cd20dSToomas Soome
1438*472cd20dSToomas Soome case mdns_trust_status_no_entitlement:
1439*472cd20dSToomas Soome err = mStatus_NoAuth;
1440*472cd20dSToomas Soome break;
1441*472cd20dSToomas Soome
1442*472cd20dSToomas Soome case mdns_trust_status_granted:
1443*472cd20dSToomas Soome err = _handle_regrecord_request_start(request, rr);
1444*472cd20dSToomas Soome break;
1445*472cd20dSToomas Soome
1446*472cd20dSToomas Soome default:
1447*472cd20dSToomas Soome err = mStatus_UnknownErr;
1448*472cd20dSToomas Soome break;
1449*472cd20dSToomas Soome }
1450*472cd20dSToomas Soome }
1451*472cd20dSToomas Soome exit:
1452*472cd20dSToomas Soome return err;
1453*472cd20dSToomas Soome }
1454*472cd20dSToomas Soome #endif // TRUST_ENFORCEMENT
1455*472cd20dSToomas Soome
handle_regrecord_request(request_state * request)1456*472cd20dSToomas Soome mDNSlocal mStatus handle_regrecord_request(request_state *request)
1457*472cd20dSToomas Soome {
1458*472cd20dSToomas Soome mStatus err = mStatus_BadParamErr;
1459*472cd20dSToomas Soome AuthRecord *rr;
1460*472cd20dSToomas Soome
1461*472cd20dSToomas Soome if (request->terminate != connection_termination)
1462*472cd20dSToomas Soome { LogMsg("%3d: DNSServiceRegisterRecord(not a shared connection ref)", request->sd); return(err); }
1463*472cd20dSToomas Soome
1464*472cd20dSToomas Soome rr = read_rr_from_ipc_msg(request, 1, 1);
1465*472cd20dSToomas Soome if (rr)
1466*472cd20dSToomas Soome {
1467*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, TRUST_ENFORCEMENT)
1468*472cd20dSToomas Soome if (os_feature_enabled(mDNSResponder, bonjour_privacy) &&
1469*472cd20dSToomas Soome IsLocalDomain(rr->resrec.name))
1470*472cd20dSToomas Soome {
1471*472cd20dSToomas Soome err = _handle_regrecord_request_with_trust(request, rr);
1472*472cd20dSToomas Soome }
1473*472cd20dSToomas Soome else
1474*472cd20dSToomas Soome {
1475*472cd20dSToomas Soome err = _handle_regrecord_request_start(request, rr);
1476*472cd20dSToomas Soome }
1477*472cd20dSToomas Soome #else
1478*472cd20dSToomas Soome err = _handle_regrecord_request_start(request, rr);
1479*472cd20dSToomas Soome #endif
1480c65ebfc7SToomas Soome }
1481c65ebfc7SToomas Soome return(err);
1482c65ebfc7SToomas Soome }
1483c65ebfc7SToomas Soome
1484c65ebfc7SToomas Soome mDNSlocal void UpdateDeviceInfoRecord(mDNS *const m);
1485c65ebfc7SToomas Soome
regservice_termination_callback(request_state * request)1486c65ebfc7SToomas Soome mDNSlocal void regservice_termination_callback(request_state *request)
1487c65ebfc7SToomas Soome {
1488c65ebfc7SToomas Soome if (!request)
1489c65ebfc7SToomas Soome {
1490c65ebfc7SToomas Soome LogMsg("regservice_termination_callback context is NULL");
1491c65ebfc7SToomas Soome return;
1492c65ebfc7SToomas Soome }
1493c65ebfc7SToomas Soome while (request->u.servicereg.instances)
1494c65ebfc7SToomas Soome {
1495c65ebfc7SToomas Soome service_instance *p = request->u.servicereg.instances;
1496c65ebfc7SToomas Soome request->u.servicereg.instances = request->u.servicereg.instances->next;
1497c65ebfc7SToomas Soome // only safe to free memory if registration is not valid, i.e. deregister fails (which invalidates p)
1498*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO, "[R%d] DNSServiceRegister(" PRI_DM_NAME ", %u) STOP PID[%d](" PUB_S ")",
1499*472cd20dSToomas Soome request->request_id, DM_NAME_PARAM(p->srs.RR_SRV.resrec.name),
1500c65ebfc7SToomas Soome mDNSVal16(p->srs.RR_SRV.resrec.rdata->u.srv.port), request->process_id, request->pid_name);
1501c65ebfc7SToomas Soome
1502*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, D2D)
1503c65ebfc7SToomas Soome external_stop_advertising_helper(p);
1504*472cd20dSToomas Soome #endif
1505c65ebfc7SToomas Soome
1506c65ebfc7SToomas Soome // Clear backpointer *before* calling mDNS_DeregisterService/unlink_and_free_service_instance
1507c65ebfc7SToomas Soome // We don't need unlink_and_free_service_instance to cut its element from the list, because we're already advancing
1508c65ebfc7SToomas Soome // request->u.servicereg.instances as we work our way through the list, implicitly cutting one element at a time
1509c65ebfc7SToomas Soome // We can't clear p->request *after* the calling mDNS_DeregisterService/unlink_and_free_service_instance
1510c65ebfc7SToomas Soome // because by then we might have already freed p
1511c65ebfc7SToomas Soome p->request = NULL;
1512c65ebfc7SToomas Soome LogMcastS(&p->srs.RR_SRV, request, reg_stop);
1513c65ebfc7SToomas Soome if (mDNS_DeregisterService(&mDNSStorage, &p->srs))
1514c65ebfc7SToomas Soome {
1515c65ebfc7SToomas Soome unlink_and_free_service_instance(p);
1516c65ebfc7SToomas Soome // Don't touch service_instance *p after this -- it's likely to have been freed already
1517c65ebfc7SToomas Soome }
1518c65ebfc7SToomas Soome }
1519c65ebfc7SToomas Soome if (request->u.servicereg.txtdata)
1520c65ebfc7SToomas Soome {
1521c65ebfc7SToomas Soome freeL("service_info txtdata", request->u.servicereg.txtdata);
1522c65ebfc7SToomas Soome request->u.servicereg.txtdata = NULL;
1523c65ebfc7SToomas Soome }
1524c65ebfc7SToomas Soome if (request->u.servicereg.autoname)
1525c65ebfc7SToomas Soome {
1526c65ebfc7SToomas Soome // Clear autoname before calling UpdateDeviceInfoRecord() so it doesn't mistakenly include this in its count of active autoname registrations
1527c65ebfc7SToomas Soome request->u.servicereg.autoname = mDNSfalse;
1528c65ebfc7SToomas Soome UpdateDeviceInfoRecord(&mDNSStorage);
1529c65ebfc7SToomas Soome }
1530c65ebfc7SToomas Soome }
1531c65ebfc7SToomas Soome
LocateSubordinateRequest(request_state * request)1532c65ebfc7SToomas Soome mDNSlocal request_state *LocateSubordinateRequest(request_state *request)
1533c65ebfc7SToomas Soome {
1534c65ebfc7SToomas Soome request_state *req;
1535c65ebfc7SToomas Soome for (req = all_requests; req; req = req->next)
1536c65ebfc7SToomas Soome if (req->primary == request &&
1537c65ebfc7SToomas Soome req->hdr.client_context.u32[0] == request->hdr.client_context.u32[0] &&
1538c65ebfc7SToomas Soome req->hdr.client_context.u32[1] == request->hdr.client_context.u32[1]) return(req);
1539c65ebfc7SToomas Soome return(request);
1540c65ebfc7SToomas Soome }
1541c65ebfc7SToomas Soome
add_record_to_service(request_state * request,service_instance * instance,mDNSu16 rrtype,mDNSu16 rdlen,const mDNSu8 * const rdata,mDNSu32 ttl)1542*472cd20dSToomas Soome mDNSlocal mStatus add_record_to_service(request_state *request, service_instance *instance, mDNSu16 rrtype, mDNSu16 rdlen,
1543*472cd20dSToomas Soome const mDNSu8 *const rdata, mDNSu32 ttl)
1544c65ebfc7SToomas Soome {
1545c65ebfc7SToomas Soome ServiceRecordSet *srs = &instance->srs;
1546c65ebfc7SToomas Soome mStatus result;
1547*472cd20dSToomas Soome const size_t rdcapacity = (rdlen > sizeof(RDataBody2)) ? rdlen : sizeof(RDataBody2);
1548*472cd20dSToomas Soome ExtraResourceRecord *extra = (ExtraResourceRecord *)callocL("ExtraResourceRecord", sizeof(*extra) - sizeof(RDataBody) + rdcapacity);
1549*472cd20dSToomas Soome if (!extra) { my_perror("ERROR: calloc"); return mStatus_NoMemoryErr; }
1550c65ebfc7SToomas Soome
1551c65ebfc7SToomas Soome extra->r.resrec.rrtype = rrtype;
1552*472cd20dSToomas Soome extra->r.resrec.rdata = &extra->r.rdatastorage;
1553*472cd20dSToomas Soome extra->r.resrec.rdata->MaxRDLength = (mDNSu16)rdcapacity;
1554c65ebfc7SToomas Soome extra->r.resrec.rdlength = rdlen;
1555*472cd20dSToomas Soome if (!SetRData(mDNSNULL, rdata, rdata + rdlen, &extra->r.resrec, rdlen))
1556*472cd20dSToomas Soome {
1557*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_ERROR,
1558*472cd20dSToomas Soome "[R%u] read_rr_from_ipc_msg: SetRData failed for " PRI_DM_NAME " (" PUB_S ")",
1559*472cd20dSToomas Soome request->request_id, DM_NAME_PARAM(request->u.servicereg.instances ?
1560*472cd20dSToomas Soome request->u.servicereg.instances->srs.RR_SRV.resrec.name : mDNSNULL), DNSTypeName(rrtype));
1561*472cd20dSToomas Soome freeL("ExtraResourceRecord/add_record_to_service", extra);
1562*472cd20dSToomas Soome return mStatus_BadParamErr;
1563*472cd20dSToomas Soome }
1564*472cd20dSToomas Soome SetNewRData(&extra->r.resrec, mDNSNULL, 0); // Sets rr->rdatahash for us
1565c65ebfc7SToomas Soome // use InterfaceID value from DNSServiceRegister() call that created the original service
1566c65ebfc7SToomas Soome extra->r.resrec.InterfaceID = request->u.servicereg.InterfaceID;
1567c65ebfc7SToomas Soome
1568c65ebfc7SToomas Soome result = mDNS_AddRecordToService(&mDNSStorage, srs, extra, &extra->r.rdatastorage, ttl, request->flags);
1569c65ebfc7SToomas Soome if (result)
1570c65ebfc7SToomas Soome {
1571c65ebfc7SToomas Soome freeL("ExtraResourceRecord/add_record_to_service", extra);
1572c65ebfc7SToomas Soome return result;
1573c65ebfc7SToomas Soome }
1574c65ebfc7SToomas Soome LogMcastS(&srs->RR_PTR, request, reg_start);
1575c65ebfc7SToomas Soome
1576c65ebfc7SToomas Soome extra->ClientID = request->hdr.reg_index;
1577*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, D2D)
1578c65ebfc7SToomas Soome if ( instance->external_advertise
1579c65ebfc7SToomas Soome && callExternalHelpers(request->u.servicereg.InterfaceID, &instance->domain, request->flags))
1580c65ebfc7SToomas Soome {
1581c65ebfc7SToomas Soome LogInfo("add_record_to_service: calling external_start_advertising_service");
1582*472cd20dSToomas Soome external_start_advertising_service(&extra->r.resrec, request->flags, request->process_id);
1583c65ebfc7SToomas Soome }
1584*472cd20dSToomas Soome #endif
1585c65ebfc7SToomas Soome return result;
1586c65ebfc7SToomas Soome }
1587c65ebfc7SToomas Soome
handle_add_request(request_state * request)1588c65ebfc7SToomas Soome mDNSlocal mStatus handle_add_request(request_state *request)
1589c65ebfc7SToomas Soome {
1590c65ebfc7SToomas Soome service_instance *i;
1591c65ebfc7SToomas Soome mStatus result = mStatus_UnknownErr;
1592c65ebfc7SToomas Soome DNSServiceFlags flags = get_flags (&request->msgptr, request->msgend);
1593c65ebfc7SToomas Soome mDNSu16 rrtype = get_uint16(&request->msgptr, request->msgend);
1594c65ebfc7SToomas Soome mDNSu16 rdlen = get_uint16(&request->msgptr, request->msgend);
1595*472cd20dSToomas Soome const mDNSu8 *const rdata = (const mDNSu8 *)get_rdata(&request->msgptr, request->msgend, rdlen);
1596c65ebfc7SToomas Soome mDNSu32 ttl = get_uint32(&request->msgptr, request->msgend);
1597c65ebfc7SToomas Soome if (!ttl) ttl = DefaultTTLforRRType(rrtype);
1598c65ebfc7SToomas Soome (void)flags; // Unused
1599c65ebfc7SToomas Soome
1600*472cd20dSToomas Soome if (!request->msgptr)
1601*472cd20dSToomas Soome {
1602*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT,
1603*472cd20dSToomas Soome "[R%d] DNSServiceAddRecord(unreadable parameters)", request->request_id);
1604*472cd20dSToomas Soome return(mStatus_BadParamErr);
1605*472cd20dSToomas Soome }
1606c65ebfc7SToomas Soome
1607c65ebfc7SToomas Soome // If this is a shared connection, check if the operation actually applies to a subordinate request_state object
1608c65ebfc7SToomas Soome if (request->terminate == connection_termination) request = LocateSubordinateRequest(request);
1609c65ebfc7SToomas Soome
1610c65ebfc7SToomas Soome if (request->terminate != regservice_termination_callback)
1611*472cd20dSToomas Soome {
1612*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT,
1613*472cd20dSToomas Soome "[R%d] DNSServiceAddRecord(not a registered service ref)", request->request_id);
1614*472cd20dSToomas Soome return(mStatus_BadParamErr);
1615*472cd20dSToomas Soome }
1616c65ebfc7SToomas Soome
1617c65ebfc7SToomas Soome // For a service registered with zero port, don't allow adding records. This mostly happens due to a bug
1618c65ebfc7SToomas Soome // in the application. See radar://9165807.
1619c65ebfc7SToomas Soome if (mDNSIPPortIsZero(request->u.servicereg.port))
1620*472cd20dSToomas Soome {
1621*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT,
1622*472cd20dSToomas Soome "[R%d] DNSServiceAddRecord: adding record to a service registered with zero port", request->request_id);
1623*472cd20dSToomas Soome return(mStatus_BadParamErr);
1624*472cd20dSToomas Soome }
1625*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
1626*472cd20dSToomas Soome "[R%d] DNSServiceAddRecord(%X, " PRI_DM_NAME ", " PUB_S ", %d) PID[%d](" PUB_S ")",
1627*472cd20dSToomas Soome request->request_id, flags,
1628*472cd20dSToomas Soome DM_NAME_PARAM((request->u.servicereg.instances) ? (request->u.servicereg.instances->srs.RR_SRV.resrec.name) : mDNSNULL),
1629*472cd20dSToomas Soome DNSTypeName(rrtype), rdlen, request->process_id, request->pid_name);
1630c65ebfc7SToomas Soome
1631c65ebfc7SToomas Soome for (i = request->u.servicereg.instances; i; i = i->next)
1632c65ebfc7SToomas Soome {
1633c65ebfc7SToomas Soome result = add_record_to_service(request, i, rrtype, rdlen, rdata, ttl);
1634c65ebfc7SToomas Soome if (result && i->default_local) break;
1635c65ebfc7SToomas Soome else result = mStatus_NoError; // suppress non-local default errors
1636c65ebfc7SToomas Soome }
1637c65ebfc7SToomas Soome
1638c65ebfc7SToomas Soome return(result);
1639c65ebfc7SToomas Soome }
1640c65ebfc7SToomas Soome
update_callback(mDNS * const m,AuthRecord * const rr,RData * oldrd,mDNSu16 oldrdlen)1641c65ebfc7SToomas Soome mDNSlocal void update_callback(mDNS *const m, AuthRecord *const rr, RData *oldrd, mDNSu16 oldrdlen)
1642c65ebfc7SToomas Soome {
1643c65ebfc7SToomas Soome mDNSBool external_advertise = (rr->UpdateContext) ? *((mDNSBool *)rr->UpdateContext) : mDNSfalse;
1644c65ebfc7SToomas Soome (void)m; // Unused
1645c65ebfc7SToomas Soome
1646c65ebfc7SToomas Soome // There are three cases.
1647c65ebfc7SToomas Soome //
1648c65ebfc7SToomas Soome // 1. We have updated the primary TXT record of the service
1649c65ebfc7SToomas Soome // 2. We have updated the TXT record that was added to the service using DNSServiceAddRecord
1650c65ebfc7SToomas Soome // 3. We have updated the TXT record that was registered using DNSServiceRegisterRecord
1651c65ebfc7SToomas Soome //
1652c65ebfc7SToomas Soome // external_advertise is set if we have advertised at least once during the initial addition
1653c65ebfc7SToomas Soome // of the record in all of the three cases above. We should have checked for InterfaceID/LocalDomain
1654c65ebfc7SToomas Soome // checks during the first time and hence we don't do any checks here
1655c65ebfc7SToomas Soome if (external_advertise)
1656c65ebfc7SToomas Soome {
1657c65ebfc7SToomas Soome ResourceRecord ext = rr->resrec;
1658*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, D2D)
1659c65ebfc7SToomas Soome DNSServiceFlags flags = deriveD2DFlagsFromAuthRecType(rr->ARType);
1660*472cd20dSToomas Soome #endif
1661c65ebfc7SToomas Soome
1662c65ebfc7SToomas Soome if (ext.rdlength == oldrdlen && mDNSPlatformMemSame(&ext.rdata->u, &oldrd->u, oldrdlen)) goto exit;
1663c65ebfc7SToomas Soome SetNewRData(&ext, oldrd, oldrdlen);
1664*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, D2D)
1665*472cd20dSToomas Soome external_stop_advertising_service(&ext, flags, 0);
1666c65ebfc7SToomas Soome LogInfo("update_callback: calling external_start_advertising_service");
1667*472cd20dSToomas Soome external_start_advertising_service(&rr->resrec, flags, 0);
1668*472cd20dSToomas Soome #endif
1669c65ebfc7SToomas Soome }
1670c65ebfc7SToomas Soome exit:
1671c65ebfc7SToomas Soome if (oldrd != &rr->rdatastorage) freeL("RData/update_callback", oldrd);
1672c65ebfc7SToomas Soome }
1673c65ebfc7SToomas Soome
update_record(AuthRecord * ar,mDNSu16 rdlen,const mDNSu8 * const rdata,mDNSu32 ttl,const mDNSBool * const external_advertise,const mDNSu32 request_id)1674*472cd20dSToomas Soome mDNSlocal mStatus update_record(AuthRecord *ar, mDNSu16 rdlen, const mDNSu8 *const rdata, mDNSu32 ttl,
1675*472cd20dSToomas Soome const mDNSBool *const external_advertise, const mDNSu32 request_id)
1676c65ebfc7SToomas Soome {
1677*472cd20dSToomas Soome ResourceRecord rr;
1678c65ebfc7SToomas Soome mStatus result;
1679*472cd20dSToomas Soome const size_t rdcapacity = (rdlen > sizeof(RDataBody2)) ? rdlen : sizeof(RDataBody2);
1680*472cd20dSToomas Soome RData *newrd = (RData *) callocL("RData/update_record", sizeof(*newrd) - sizeof(RDataBody) + rdcapacity);
1681*472cd20dSToomas Soome if (!newrd) FatalError("ERROR: calloc");
1682*472cd20dSToomas Soome mDNSPlatformMemZero(&rr, (mDNSu32)sizeof(rr));
1683*472cd20dSToomas Soome rr.name = ar->resrec.name;
1684*472cd20dSToomas Soome rr.rrtype = ar->resrec.rrtype;
1685*472cd20dSToomas Soome rr.rrclass = ar->resrec.rrclass;
1686*472cd20dSToomas Soome rr.rdata = newrd;
1687*472cd20dSToomas Soome rr.rdata->MaxRDLength = (mDNSu16)rdcapacity;
1688*472cd20dSToomas Soome rr.rdlength = rdlen;
1689*472cd20dSToomas Soome if (!SetRData(mDNSNULL, rdata, rdata + rdlen, &rr, rdlen))
1690*472cd20dSToomas Soome {
1691*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_ERROR,
1692*472cd20dSToomas Soome "[R%u] update_record: SetRData failed for " PRI_DM_NAME " (" PUB_S ")",
1693*472cd20dSToomas Soome request_id, DM_NAME_PARAM(rr.name), DNSTypeName(rr.rrtype));
1694*472cd20dSToomas Soome freeL("RData/update_record", newrd);
1695*472cd20dSToomas Soome return mStatus_BadParamErr;
1696*472cd20dSToomas Soome }
1697*472cd20dSToomas Soome rdlen = GetRDLength(&rr, mDNSfalse);
1698c65ebfc7SToomas Soome // BIND named (name daemon) doesn't allow TXT records with zero-length rdata. This is strictly speaking correct,
1699c65ebfc7SToomas Soome // since RFC 1035 specifies a TXT record as "One or more <character-string>s", not "Zero or more <character-string>s".
1700c65ebfc7SToomas Soome // Since some legacy apps try to create zero-length TXT records, we'll silently correct it here.
1701*472cd20dSToomas Soome if (ar->resrec.rrtype == kDNSType_TXT && rdlen == 0) { rdlen = 1; newrd->u.txt.c[0] = 0; }
1702c65ebfc7SToomas Soome
1703*472cd20dSToomas Soome if (external_advertise) ar->UpdateContext = (void *)external_advertise;
1704c65ebfc7SToomas Soome
1705*472cd20dSToomas Soome result = mDNS_Update(&mDNSStorage, ar, ttl, rdlen, newrd, update_callback);
1706*472cd20dSToomas Soome if (result) { LogMsg("update_record: Error %d for %s", (int)result, ARDisplayString(&mDNSStorage, ar)); freeL("RData/update_record", newrd); }
1707c65ebfc7SToomas Soome return result;
1708c65ebfc7SToomas Soome }
1709c65ebfc7SToomas Soome
handle_update_request(request_state * request)1710c65ebfc7SToomas Soome mDNSlocal mStatus handle_update_request(request_state *request)
1711c65ebfc7SToomas Soome {
1712c65ebfc7SToomas Soome const ipc_msg_hdr *const hdr = &request->hdr;
1713c65ebfc7SToomas Soome mStatus result = mStatus_BadReferenceErr;
1714c65ebfc7SToomas Soome service_instance *i;
1715c65ebfc7SToomas Soome AuthRecord *rr = NULL;
1716c65ebfc7SToomas Soome
1717c65ebfc7SToomas Soome // get the message data
1718c65ebfc7SToomas Soome DNSServiceFlags flags = get_flags (&request->msgptr, request->msgend); // flags unused
1719c65ebfc7SToomas Soome mDNSu16 rdlen = get_uint16(&request->msgptr, request->msgend);
1720*472cd20dSToomas Soome const mDNSu8 *const rdata = (const mDNSu8 *)get_rdata(&request->msgptr, request->msgend, rdlen);
1721c65ebfc7SToomas Soome mDNSu32 ttl = get_uint32(&request->msgptr, request->msgend);
1722c65ebfc7SToomas Soome (void)flags; // Unused
1723c65ebfc7SToomas Soome
1724*472cd20dSToomas Soome if (!request->msgptr)
1725*472cd20dSToomas Soome {
1726*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT,
1727*472cd20dSToomas Soome "[R%d] DNSServiceUpdateRecord(unreadable parameters)", request->request_id);
1728*472cd20dSToomas Soome return(mStatus_BadParamErr);
1729*472cd20dSToomas Soome }
1730c65ebfc7SToomas Soome
1731c65ebfc7SToomas Soome // If this is a shared connection, check if the operation actually applies to a subordinate request_state object
1732c65ebfc7SToomas Soome if (request->terminate == connection_termination) request = LocateSubordinateRequest(request);
1733c65ebfc7SToomas Soome
1734c65ebfc7SToomas Soome if (request->terminate == connection_termination)
1735c65ebfc7SToomas Soome {
1736c65ebfc7SToomas Soome // update an individually registered record
1737c65ebfc7SToomas Soome registered_record_entry *reptr;
1738c65ebfc7SToomas Soome for (reptr = request->u.reg_recs; reptr; reptr = reptr->next)
1739c65ebfc7SToomas Soome {
1740c65ebfc7SToomas Soome if (reptr->key == hdr->reg_index)
1741c65ebfc7SToomas Soome {
1742*472cd20dSToomas Soome result = update_record(reptr->rr, rdlen, rdata, ttl, &reptr->external_advertise, request->request_id);
1743*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT,
1744*472cd20dSToomas Soome "[R%d] DNSServiceUpdateRecord(" PRI_DM_NAME ", " PUB_S ") PID[%d](" PUB_S ")",
1745*472cd20dSToomas Soome request->request_id, DM_NAME_PARAM(reptr->rr->resrec.name),
1746*472cd20dSToomas Soome reptr->rr ? DNSTypeName(reptr->rr->resrec.rrtype) : "<NONE>",
1747c65ebfc7SToomas Soome request->process_id, request->pid_name);
1748c65ebfc7SToomas Soome goto end;
1749c65ebfc7SToomas Soome }
1750c65ebfc7SToomas Soome }
1751c65ebfc7SToomas Soome result = mStatus_BadReferenceErr;
1752c65ebfc7SToomas Soome goto end;
1753c65ebfc7SToomas Soome }
1754c65ebfc7SToomas Soome
1755c65ebfc7SToomas Soome if (request->terminate != regservice_termination_callback)
1756*472cd20dSToomas Soome {
1757*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT,
1758*472cd20dSToomas Soome "[R%d] DNSServiceUpdateRecord(not a registered service ref)", request->request_id);
1759*472cd20dSToomas Soome return(mStatus_BadParamErr);
1760*472cd20dSToomas Soome }
1761c65ebfc7SToomas Soome
1762c65ebfc7SToomas Soome // For a service registered with zero port, only SRV record is initialized. Don't allow any updates.
1763c65ebfc7SToomas Soome if (mDNSIPPortIsZero(request->u.servicereg.port))
1764*472cd20dSToomas Soome {
1765*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT,
1766*472cd20dSToomas Soome "[R%d] DNSServiceUpdateRecord: updating the record of a service registered with zero port", request->request_id);
1767*472cd20dSToomas Soome return(mStatus_BadParamErr);
1768*472cd20dSToomas Soome }
1769c65ebfc7SToomas Soome
1770c65ebfc7SToomas Soome // update the saved off TXT data for the service
1771c65ebfc7SToomas Soome if (hdr->reg_index == TXT_RECORD_INDEX)
1772c65ebfc7SToomas Soome {
1773c65ebfc7SToomas Soome if (request->u.servicereg.txtdata)
1774c65ebfc7SToomas Soome { freeL("service_info txtdata", request->u.servicereg.txtdata); request->u.servicereg.txtdata = NULL; }
1775c65ebfc7SToomas Soome if (rdlen > 0)
1776c65ebfc7SToomas Soome {
1777c65ebfc7SToomas Soome request->u.servicereg.txtdata = mallocL("service_info txtdata", rdlen);
1778c65ebfc7SToomas Soome if (!request->u.servicereg.txtdata) FatalError("ERROR: handle_update_request - malloc");
1779c65ebfc7SToomas Soome mDNSPlatformMemCopy(request->u.servicereg.txtdata, rdata, rdlen);
1780c65ebfc7SToomas Soome }
1781c65ebfc7SToomas Soome request->u.servicereg.txtlen = rdlen;
1782c65ebfc7SToomas Soome }
1783c65ebfc7SToomas Soome
1784c65ebfc7SToomas Soome // update a record from a service record set
1785c65ebfc7SToomas Soome for (i = request->u.servicereg.instances; i; i = i->next)
1786c65ebfc7SToomas Soome {
1787c65ebfc7SToomas Soome if (hdr->reg_index == TXT_RECORD_INDEX) rr = &i->srs.RR_TXT;
1788c65ebfc7SToomas Soome else
1789c65ebfc7SToomas Soome {
1790c65ebfc7SToomas Soome ExtraResourceRecord *e;
1791c65ebfc7SToomas Soome for (e = i->srs.Extras; e; e = e->next)
1792c65ebfc7SToomas Soome if (e->ClientID == hdr->reg_index) { rr = &e->r; break; }
1793c65ebfc7SToomas Soome }
1794c65ebfc7SToomas Soome
1795c65ebfc7SToomas Soome if (!rr) { result = mStatus_BadReferenceErr; goto end; }
1796*472cd20dSToomas Soome result = update_record(rr, rdlen, rdata, ttl, &i->external_advertise, request->request_id);
1797c65ebfc7SToomas Soome if (result && i->default_local) goto end;
1798c65ebfc7SToomas Soome else result = mStatus_NoError; // suppress non-local default errors
1799c65ebfc7SToomas Soome }
1800c65ebfc7SToomas Soome
1801c65ebfc7SToomas Soome end:
1802c65ebfc7SToomas Soome if (request->terminate == regservice_termination_callback)
1803c65ebfc7SToomas Soome LogOperation("%3d: DNSServiceUpdateRecord(%##s, %s) PID[%d](%s)", request->sd,
1804c65ebfc7SToomas Soome (request->u.servicereg.instances) ? request->u.servicereg.instances->srs.RR_SRV.resrec.name->c : NULL,
1805c65ebfc7SToomas Soome rr ? DNSTypeName(rr->resrec.rrtype) : "<NONE>",
1806c65ebfc7SToomas Soome request->process_id, request->pid_name);
1807c65ebfc7SToomas Soome
1808c65ebfc7SToomas Soome return(result);
1809c65ebfc7SToomas Soome }
1810c65ebfc7SToomas Soome
1811c65ebfc7SToomas Soome // remove a resource record registered via DNSServiceRegisterRecord()
remove_record(request_state * request)1812c65ebfc7SToomas Soome mDNSlocal mStatus remove_record(request_state *request)
1813c65ebfc7SToomas Soome {
1814c65ebfc7SToomas Soome mStatus err = mStatus_UnknownErr;
1815c65ebfc7SToomas Soome registered_record_entry *e, **ptr = &request->u.reg_recs;
1816c65ebfc7SToomas Soome
1817c65ebfc7SToomas Soome while (*ptr && (*ptr)->key != request->hdr.reg_index) ptr = &(*ptr)->next;
1818c65ebfc7SToomas Soome if (!*ptr) { LogMsg("%3d: DNSServiceRemoveRecord(%u) not found", request->sd, request->hdr.reg_index); return mStatus_BadReferenceErr; }
1819c65ebfc7SToomas Soome e = *ptr;
1820c65ebfc7SToomas Soome *ptr = e->next; // unlink
1821c65ebfc7SToomas Soome
1822c65ebfc7SToomas Soome LogOperation("%3d: DNSServiceRemoveRecord(%u %s) PID[%d](%s)",
1823c65ebfc7SToomas Soome request->sd, e->key, RRDisplayString(&mDNSStorage, &e->rr->resrec), request->process_id, request->pid_name);
1824c65ebfc7SToomas Soome e->rr->RecordContext = NULL;
1825c65ebfc7SToomas Soome if (e->external_advertise)
1826c65ebfc7SToomas Soome {
1827*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, D2D)
1828*472cd20dSToomas Soome external_stop_advertising_service(&e->rr->resrec, request->flags, request->process_id);
1829*472cd20dSToomas Soome #endif
1830c65ebfc7SToomas Soome e->external_advertise = mDNSfalse;
1831c65ebfc7SToomas Soome }
1832c65ebfc7SToomas Soome LogMcastS(e->rr, request, reg_stop);
1833c65ebfc7SToomas Soome err = mDNS_Deregister(&mDNSStorage, e->rr); // Will free e->rr for us; we're responsible for freeing e
1834c65ebfc7SToomas Soome if (err)
1835c65ebfc7SToomas Soome {
1836c65ebfc7SToomas Soome LogMsg("ERROR: remove_record, mDNS_Deregister: %d", err);
1837c65ebfc7SToomas Soome freeL("registered_record_entry AuthRecord remove_record", e->rr);
1838c65ebfc7SToomas Soome }
1839c65ebfc7SToomas Soome freeL("registered_record_entry remove_record", e);
1840c65ebfc7SToomas Soome return err;
1841c65ebfc7SToomas Soome }
1842c65ebfc7SToomas Soome
remove_extra(const request_state * const request,service_instance * const serv,mDNSu16 * const rrtype)1843c65ebfc7SToomas Soome mDNSlocal mStatus remove_extra(const request_state *const request, service_instance *const serv, mDNSu16 *const rrtype)
1844c65ebfc7SToomas Soome {
1845c65ebfc7SToomas Soome mStatus err = mStatus_BadReferenceErr;
1846c65ebfc7SToomas Soome ExtraResourceRecord *ptr;
1847c65ebfc7SToomas Soome
1848c65ebfc7SToomas Soome for (ptr = serv->srs.Extras; ptr; ptr = ptr->next)
1849c65ebfc7SToomas Soome {
1850c65ebfc7SToomas Soome if (ptr->ClientID == request->hdr.reg_index) // found match
1851c65ebfc7SToomas Soome {
1852c65ebfc7SToomas Soome *rrtype = ptr->r.resrec.rrtype;
1853*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, D2D)
1854*472cd20dSToomas Soome if (serv->external_advertise)
1855*472cd20dSToomas Soome {
1856*472cd20dSToomas Soome external_stop_advertising_service(&ptr->r.resrec, request->flags, request->process_id);
1857*472cd20dSToomas Soome }
1858*472cd20dSToomas Soome #endif
1859c65ebfc7SToomas Soome err = mDNS_RemoveRecordFromService(&mDNSStorage, &serv->srs, ptr, FreeExtraRR, ptr);
1860c65ebfc7SToomas Soome break;
1861c65ebfc7SToomas Soome }
1862c65ebfc7SToomas Soome }
1863c65ebfc7SToomas Soome return err;
1864c65ebfc7SToomas Soome }
1865c65ebfc7SToomas Soome
handle_removerecord_request(request_state * request)1866c65ebfc7SToomas Soome mDNSlocal mStatus handle_removerecord_request(request_state *request)
1867c65ebfc7SToomas Soome {
1868c65ebfc7SToomas Soome mStatus err = mStatus_BadReferenceErr;
1869c65ebfc7SToomas Soome get_flags(&request->msgptr, request->msgend); // flags unused
1870c65ebfc7SToomas Soome
1871*472cd20dSToomas Soome if (!request->msgptr)
1872*472cd20dSToomas Soome {
1873*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT,
1874*472cd20dSToomas Soome "[R%d] DNSServiceRemoveRecord(unreadable parameters)", request->request_id);
1875*472cd20dSToomas Soome return(mStatus_BadParamErr);
1876*472cd20dSToomas Soome }
1877c65ebfc7SToomas Soome
1878c65ebfc7SToomas Soome // If this is a shared connection, check if the operation actually applies to a subordinate request_state object
1879c65ebfc7SToomas Soome if (request->terminate == connection_termination) request = LocateSubordinateRequest(request);
1880c65ebfc7SToomas Soome
1881c65ebfc7SToomas Soome if (request->terminate == connection_termination)
1882c65ebfc7SToomas Soome err = remove_record(request); // remove individually registered record
1883c65ebfc7SToomas Soome else if (request->terminate != regservice_termination_callback)
1884*472cd20dSToomas Soome {
1885*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT,
1886*472cd20dSToomas Soome "[R%d] DNSServiceRemoveRecord(not a registered service ref)", request->request_id);
1887*472cd20dSToomas Soome return(mStatus_BadParamErr);
1888*472cd20dSToomas Soome }
1889c65ebfc7SToomas Soome else
1890c65ebfc7SToomas Soome {
1891c65ebfc7SToomas Soome service_instance *i;
1892c65ebfc7SToomas Soome mDNSu16 rrtype = 0;
1893*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO, "[R%d] DNSServiceRemoveRecord(" PRI_DM_NAME ", " PUB_S ") PID[%d](" PUB_S ")",
1894*472cd20dSToomas Soome request->request_id,
1895*472cd20dSToomas Soome DM_NAME_PARAM((request->u.servicereg.instances) ? (request->u.servicereg.instances->srs.RR_SRV.resrec.name) : mDNSNULL),
1896c65ebfc7SToomas Soome rrtype ? DNSTypeName(rrtype) : "<NONE>", request->process_id, request->pid_name);
1897c65ebfc7SToomas Soome for (i = request->u.servicereg.instances; i; i = i->next)
1898c65ebfc7SToomas Soome {
1899c65ebfc7SToomas Soome err = remove_extra(request, i, &rrtype);
1900c65ebfc7SToomas Soome if (err && i->default_local) break;
1901c65ebfc7SToomas Soome else err = mStatus_NoError; // suppress non-local default errors
1902c65ebfc7SToomas Soome }
1903c65ebfc7SToomas Soome }
1904c65ebfc7SToomas Soome
1905c65ebfc7SToomas Soome return(err);
1906c65ebfc7SToomas Soome }
1907c65ebfc7SToomas Soome
1908c65ebfc7SToomas Soome // If there's a comma followed by another character,
1909c65ebfc7SToomas Soome // FindFirstSubType overwrites the comma with a nul and returns the pointer to the next character.
1910c65ebfc7SToomas Soome // Otherwise, it returns a pointer to the final nul at the end of the string
FindFirstSubType(char * p)1911*472cd20dSToomas Soome mDNSlocal char *FindFirstSubType(char *p)
1912c65ebfc7SToomas Soome {
1913c65ebfc7SToomas Soome while (*p)
1914c65ebfc7SToomas Soome {
1915c65ebfc7SToomas Soome if (p[0] == '\\' && p[1])
1916c65ebfc7SToomas Soome {
1917c65ebfc7SToomas Soome p += 2;
1918c65ebfc7SToomas Soome }
1919c65ebfc7SToomas Soome else if (p[0] == ',' && p[1])
1920c65ebfc7SToomas Soome {
1921c65ebfc7SToomas Soome *p++ = 0;
1922c65ebfc7SToomas Soome return(p);
1923c65ebfc7SToomas Soome }
1924c65ebfc7SToomas Soome else
1925c65ebfc7SToomas Soome {
1926c65ebfc7SToomas Soome p++;
1927c65ebfc7SToomas Soome }
1928c65ebfc7SToomas Soome }
1929c65ebfc7SToomas Soome return(p);
1930c65ebfc7SToomas Soome }
1931c65ebfc7SToomas Soome
1932c65ebfc7SToomas Soome // If there's a comma followed by another character,
1933c65ebfc7SToomas Soome // FindNextSubType overwrites the comma with a nul and returns the pointer to the next character.
1934c65ebfc7SToomas Soome // If it finds an illegal unescaped dot in the subtype name, it returns mDNSNULL
1935c65ebfc7SToomas Soome // Otherwise, it returns a pointer to the final nul at the end of the string
FindNextSubType(char * p)1936c65ebfc7SToomas Soome mDNSlocal char *FindNextSubType(char *p)
1937c65ebfc7SToomas Soome {
1938c65ebfc7SToomas Soome while (*p)
1939c65ebfc7SToomas Soome {
1940c65ebfc7SToomas Soome if (p[0] == '\\' && p[1]) // If escape character
1941c65ebfc7SToomas Soome p += 2; // ignore following character
1942c65ebfc7SToomas Soome else if (p[0] == ',') // If we found a comma
1943c65ebfc7SToomas Soome {
1944c65ebfc7SToomas Soome if (p[1]) *p++ = 0;
1945c65ebfc7SToomas Soome return(p);
1946c65ebfc7SToomas Soome }
1947c65ebfc7SToomas Soome else if (p[0] == '.')
1948c65ebfc7SToomas Soome return(mDNSNULL);
1949c65ebfc7SToomas Soome else p++;
1950c65ebfc7SToomas Soome }
1951c65ebfc7SToomas Soome return(p);
1952c65ebfc7SToomas Soome }
1953c65ebfc7SToomas Soome
1954c65ebfc7SToomas Soome // Returns -1 if illegal subtype found
ChopSubTypes(char * regtype)1955*472cd20dSToomas Soome mDNSlocal mDNSs32 ChopSubTypes(char *regtype)
1956c65ebfc7SToomas Soome {
1957c65ebfc7SToomas Soome mDNSs32 NumSubTypes = 0;
1958*472cd20dSToomas Soome char *stp = FindFirstSubType(regtype);
1959c65ebfc7SToomas Soome while (stp && *stp) // If we found a comma...
1960c65ebfc7SToomas Soome {
1961c65ebfc7SToomas Soome if (*stp == ',') return(-1);
1962c65ebfc7SToomas Soome NumSubTypes++;
1963c65ebfc7SToomas Soome stp = FindNextSubType(stp);
1964c65ebfc7SToomas Soome }
1965c65ebfc7SToomas Soome if (!stp) return(-1);
1966c65ebfc7SToomas Soome return(NumSubTypes);
1967c65ebfc7SToomas Soome }
1968c65ebfc7SToomas Soome
AllocateSubTypes(mDNSs32 NumSubTypes,char * p)1969*472cd20dSToomas Soome mDNSlocal AuthRecord *AllocateSubTypes(mDNSs32 NumSubTypes, char *p)
1970c65ebfc7SToomas Soome {
1971c65ebfc7SToomas Soome AuthRecord *st = mDNSNULL;
1972c65ebfc7SToomas Soome if (NumSubTypes)
1973c65ebfc7SToomas Soome {
1974c65ebfc7SToomas Soome mDNSs32 i;
1975*472cd20dSToomas Soome st = (AuthRecord *) callocL("ServiceSubTypes", NumSubTypes * sizeof(AuthRecord));
1976c65ebfc7SToomas Soome if (!st) return(mDNSNULL);
1977c65ebfc7SToomas Soome for (i = 0; i < NumSubTypes; i++)
1978c65ebfc7SToomas Soome {
1979c65ebfc7SToomas Soome mDNS_SetupResourceRecord(&st[i], mDNSNULL, mDNSInterface_Any, kDNSQType_ANY, kStandardTTL, 0, AuthRecordAny, mDNSNULL, mDNSNULL);
1980c65ebfc7SToomas Soome while (*p) p++;
1981c65ebfc7SToomas Soome p++;
1982c65ebfc7SToomas Soome if (!MakeDomainNameFromDNSNameString(&st[i].namestorage, p))
1983c65ebfc7SToomas Soome {
1984c65ebfc7SToomas Soome freeL("ServiceSubTypes", st);
1985c65ebfc7SToomas Soome return(mDNSNULL);
1986c65ebfc7SToomas Soome }
1987c65ebfc7SToomas Soome }
1988c65ebfc7SToomas Soome }
1989c65ebfc7SToomas Soome return(st);
1990c65ebfc7SToomas Soome }
1991c65ebfc7SToomas Soome
register_service_instance(request_state * request,const domainname * domain)1992c65ebfc7SToomas Soome mDNSlocal mStatus register_service_instance(request_state *request, const domainname *domain)
1993c65ebfc7SToomas Soome {
1994c65ebfc7SToomas Soome service_instance **ptr, *instance;
1995c65ebfc7SToomas Soome size_t extra_size = (request->u.servicereg.txtlen > sizeof(RDataBody)) ? (request->u.servicereg.txtlen - sizeof(RDataBody)) : 0;
1996c65ebfc7SToomas Soome const mDNSBool DomainIsLocal = SameDomainName(domain, &localdomain);
1997c65ebfc7SToomas Soome mStatus result;
1998c65ebfc7SToomas Soome mDNSInterfaceID interfaceID = request->u.servicereg.InterfaceID;
1999c65ebfc7SToomas Soome
2000c65ebfc7SToomas Soome // If the client specified an interface, but no domain, then we honor the specified interface for the "local" (mDNS)
2001c65ebfc7SToomas Soome // registration but for the wide-area registrations we don't (currently) have any concept of a wide-area unicast
2002c65ebfc7SToomas Soome // registrations scoped to a specific interface, so for the automatic domains we add we must *not* specify an interface.
2003c65ebfc7SToomas Soome // (Specifying an interface with an apparently wide-area domain (i.e. something other than "local")
2004c65ebfc7SToomas Soome // currently forces the registration to use mDNS multicast despite the apparently wide-area domain.)
2005c65ebfc7SToomas Soome if (request->u.servicereg.default_domain && !DomainIsLocal) interfaceID = mDNSInterface_Any;
2006c65ebfc7SToomas Soome
2007c65ebfc7SToomas Soome for (ptr = &request->u.servicereg.instances; *ptr; ptr = &(*ptr)->next)
2008c65ebfc7SToomas Soome {
2009c65ebfc7SToomas Soome if (SameDomainName(&(*ptr)->domain, domain))
2010c65ebfc7SToomas Soome {
2011c65ebfc7SToomas Soome LogMsg("register_service_instance: domain %##s already registered for %#s.%##s",
2012c65ebfc7SToomas Soome domain->c, &request->u.servicereg.name, &request->u.servicereg.type);
2013c65ebfc7SToomas Soome return mStatus_AlreadyRegistered;
2014c65ebfc7SToomas Soome }
2015c65ebfc7SToomas Soome }
2016c65ebfc7SToomas Soome
2017*472cd20dSToomas Soome instance = (service_instance *) callocL("service_instance", sizeof(*instance) + extra_size);
2018*472cd20dSToomas Soome if (!instance) { my_perror("ERROR: calloc"); return mStatus_NoMemoryErr; }
2019c65ebfc7SToomas Soome
2020c65ebfc7SToomas Soome instance->next = mDNSNULL;
2021c65ebfc7SToomas Soome instance->request = request;
2022c65ebfc7SToomas Soome instance->renameonmemfree = 0;
2023c65ebfc7SToomas Soome instance->clientnotified = mDNSfalse;
2024c65ebfc7SToomas Soome instance->default_local = (request->u.servicereg.default_domain && DomainIsLocal);
2025c65ebfc7SToomas Soome instance->external_advertise = mDNSfalse;
2026c65ebfc7SToomas Soome AssignDomainName(&instance->domain, domain);
2027c65ebfc7SToomas Soome
2028*472cd20dSToomas Soome instance->subtypes = AllocateSubTypes(request->u.servicereg.num_subtypes, request->u.servicereg.type_as_string);
2029c65ebfc7SToomas Soome
2030c65ebfc7SToomas Soome if (request->u.servicereg.num_subtypes && !instance->subtypes)
2031c65ebfc7SToomas Soome {
2032c65ebfc7SToomas Soome unlink_and_free_service_instance(instance);
2033c65ebfc7SToomas Soome instance = NULL;
2034c65ebfc7SToomas Soome FatalError("ERROR: malloc");
2035c65ebfc7SToomas Soome }
2036c65ebfc7SToomas Soome
2037c65ebfc7SToomas Soome result = mDNS_RegisterService(&mDNSStorage, &instance->srs,
2038c65ebfc7SToomas Soome &request->u.servicereg.name, &request->u.servicereg.type, domain,
2039c65ebfc7SToomas Soome request->u.servicereg.host.c[0] ? &request->u.servicereg.host : NULL,
2040c65ebfc7SToomas Soome request->u.servicereg.port,
20413b436d06SToomas Soome mDNSNULL, request->u.servicereg.txtdata, request->u.servicereg.txtlen,
2042c65ebfc7SToomas Soome instance->subtypes, request->u.servicereg.num_subtypes,
2043c65ebfc7SToomas Soome interfaceID, regservice_callback, instance, request->flags);
2044c65ebfc7SToomas Soome
2045c65ebfc7SToomas Soome if (!result)
2046c65ebfc7SToomas Soome {
2047c65ebfc7SToomas Soome *ptr = instance; // Append this to the end of our request->u.servicereg.instances list
2048c65ebfc7SToomas Soome LogOperation("%3d: DNSServiceRegister(%##s, %u) ADDED", instance->request->sd,
2049c65ebfc7SToomas Soome instance->srs.RR_SRV.resrec.name->c, mDNSVal16(request->u.servicereg.port));
2050c65ebfc7SToomas Soome LogMcastS(&instance->srs.RR_SRV, request, reg_start);
2051c65ebfc7SToomas Soome }
2052c65ebfc7SToomas Soome else
2053c65ebfc7SToomas Soome {
2054c65ebfc7SToomas Soome LogMsg("register_service_instance %#s.%##s%##s error %d",
2055c65ebfc7SToomas Soome &request->u.servicereg.name, &request->u.servicereg.type, domain->c, result);
2056c65ebfc7SToomas Soome unlink_and_free_service_instance(instance);
2057c65ebfc7SToomas Soome }
2058c65ebfc7SToomas Soome
2059c65ebfc7SToomas Soome return result;
2060c65ebfc7SToomas Soome }
2061c65ebfc7SToomas Soome
udsserver_default_reg_domain_changed(const DNameListElem * const d,const mDNSBool add)2062c65ebfc7SToomas Soome mDNSlocal void udsserver_default_reg_domain_changed(const DNameListElem *const d, const mDNSBool add)
2063c65ebfc7SToomas Soome {
2064c65ebfc7SToomas Soome request_state *request;
2065c65ebfc7SToomas Soome
2066c65ebfc7SToomas Soome LogMsg("%s registration domain %##s", add ? "Adding" : "Removing", d->name.c);
2067c65ebfc7SToomas Soome for (request = all_requests; request; request = request->next)
2068c65ebfc7SToomas Soome {
2069c65ebfc7SToomas Soome if (request->terminate != regservice_termination_callback) continue;
2070c65ebfc7SToomas Soome if (!request->u.servicereg.default_domain) continue;
2071c65ebfc7SToomas Soome if (!d->uid || SystemUID(request->uid) || request->uid == d->uid)
2072c65ebfc7SToomas Soome {
2073c65ebfc7SToomas Soome service_instance **ptr = &request->u.servicereg.instances;
2074c65ebfc7SToomas Soome while (*ptr && !SameDomainName(&(*ptr)->domain, &d->name)) ptr = &(*ptr)->next;
2075c65ebfc7SToomas Soome if (add)
2076c65ebfc7SToomas Soome {
2077c65ebfc7SToomas Soome // If we don't already have this domain in our list for this registration, add it now
2078c65ebfc7SToomas Soome if (!*ptr) register_service_instance(request, &d->name);
2079c65ebfc7SToomas Soome else debugf("udsserver_default_reg_domain_changed %##s already in list, not re-adding", &d->name);
2080c65ebfc7SToomas Soome }
2081c65ebfc7SToomas Soome else
2082c65ebfc7SToomas Soome {
2083c65ebfc7SToomas Soome // Normally we should not fail to find the specified instance
2084c65ebfc7SToomas Soome // One case where this can happen is if a uDNS update fails for some reason,
2085c65ebfc7SToomas Soome // and regservice_callback then calls unlink_and_free_service_instance and disposes of that instance.
2086c65ebfc7SToomas Soome if (!*ptr)
2087c65ebfc7SToomas Soome LogMsg("udsserver_default_reg_domain_changed domain %##s not found for service %#s type %s",
2088c65ebfc7SToomas Soome &d->name, request->u.servicereg.name.c, request->u.servicereg.type_as_string);
2089c65ebfc7SToomas Soome else
2090c65ebfc7SToomas Soome {
2091c65ebfc7SToomas Soome DNameListElem *p;
2092c65ebfc7SToomas Soome for (p = AutoRegistrationDomains; p; p=p->next)
2093c65ebfc7SToomas Soome if (!p->uid || SystemUID(request->uid) || request->uid == p->uid)
2094c65ebfc7SToomas Soome if (SameDomainName(&d->name, &p->name)) break;
2095c65ebfc7SToomas Soome if (p) debugf("udsserver_default_reg_domain_changed %##s still in list, not removing", &d->name);
2096c65ebfc7SToomas Soome else
2097c65ebfc7SToomas Soome {
2098c65ebfc7SToomas Soome mStatus err;
2099c65ebfc7SToomas Soome service_instance *si = *ptr;
2100c65ebfc7SToomas Soome *ptr = si->next;
2101c65ebfc7SToomas Soome if (si->clientnotified) SendServiceRemovalNotification(&si->srs); // Do this *before* clearing si->request backpointer
2102c65ebfc7SToomas Soome // Now that we've cut this service_instance from the list, we MUST clear the si->request backpointer.
2103c65ebfc7SToomas Soome // Otherwise what can happen is this: While our mDNS_DeregisterService is in the
2104c65ebfc7SToomas Soome // process of completing asynchronously, the client cancels the entire operation, so
2105c65ebfc7SToomas Soome // regservice_termination_callback then runs through the whole list deregistering each
2106c65ebfc7SToomas Soome // instance, clearing the backpointers, and then disposing the parent request_state object.
2107c65ebfc7SToomas Soome // However, because this service_instance isn't in the list any more, regservice_termination_callback
2108c65ebfc7SToomas Soome // has no way to find it and clear its backpointer, and then when our mDNS_DeregisterService finally
2109c65ebfc7SToomas Soome // completes later with a mStatus_MemFree message, it calls unlink_and_free_service_instance() with
2110c65ebfc7SToomas Soome // a service_instance with a stale si->request backpointer pointing to memory that's already been freed.
2111c65ebfc7SToomas Soome si->request = NULL;
2112c65ebfc7SToomas Soome err = mDNS_DeregisterService(&mDNSStorage, &si->srs);
2113c65ebfc7SToomas Soome if (err) { LogMsg("udsserver_default_reg_domain_changed err %d", err); unlink_and_free_service_instance(si); }
2114c65ebfc7SToomas Soome }
2115c65ebfc7SToomas Soome }
2116c65ebfc7SToomas Soome }
2117c65ebfc7SToomas Soome }
2118c65ebfc7SToomas Soome }
2119c65ebfc7SToomas Soome }
2120c65ebfc7SToomas Soome
2121c65ebfc7SToomas Soome // Returns true if the interfaceIndex value matches one of the pre-defined
2122c65ebfc7SToomas Soome // special values listed in the switch statement below.
PreDefinedInterfaceIndex(mDNSu32 interfaceIndex)2123c65ebfc7SToomas Soome mDNSlocal mDNSBool PreDefinedInterfaceIndex(mDNSu32 interfaceIndex)
2124c65ebfc7SToomas Soome {
2125c65ebfc7SToomas Soome switch(interfaceIndex)
2126c65ebfc7SToomas Soome {
2127c65ebfc7SToomas Soome case kDNSServiceInterfaceIndexAny:
2128c65ebfc7SToomas Soome case kDNSServiceInterfaceIndexLocalOnly:
2129c65ebfc7SToomas Soome case kDNSServiceInterfaceIndexUnicast:
2130c65ebfc7SToomas Soome case kDNSServiceInterfaceIndexP2P:
2131c65ebfc7SToomas Soome case kDNSServiceInterfaceIndexBLE:
2132c65ebfc7SToomas Soome return mDNStrue;
2133c65ebfc7SToomas Soome default:
2134c65ebfc7SToomas Soome return mDNSfalse;
2135c65ebfc7SToomas Soome }
2136c65ebfc7SToomas Soome }
2137c65ebfc7SToomas Soome
_handle_regservice_request_start(request_state * request,const domainname * const d)2138*472cd20dSToomas Soome mDNSlocal mStatus _handle_regservice_request_start(request_state *request, const domainname * const d)
2139*472cd20dSToomas Soome {
2140*472cd20dSToomas Soome mStatus err;
2141*472cd20dSToomas Soome
2142*472cd20dSToomas Soome request->terminate = regservice_termination_callback;
2143*472cd20dSToomas Soome err = register_service_instance(request, d);
2144*472cd20dSToomas Soome
2145*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, METRICS)
2146*472cd20dSToomas Soome ++curr_num_regservices;
2147*472cd20dSToomas Soome if (curr_num_regservices > max_num_regservices)
2148*472cd20dSToomas Soome max_num_regservices = curr_num_regservices;
2149*472cd20dSToomas Soome #endif
2150*472cd20dSToomas Soome
2151*472cd20dSToomas Soome #if 0
2152*472cd20dSToomas Soome err = AuthorizedDomain(request, d, AutoRegistrationDomains) ? register_service_instance(request, d) : mStatus_NoError;
2153*472cd20dSToomas Soome #endif
2154*472cd20dSToomas Soome if (!err)
2155*472cd20dSToomas Soome {
2156*472cd20dSToomas Soome if (request->u.servicereg.autoname) UpdateDeviceInfoRecord(&mDNSStorage);
2157*472cd20dSToomas Soome
2158*472cd20dSToomas Soome if (request->u.servicereg.default_domain)
2159*472cd20dSToomas Soome {
2160*472cd20dSToomas Soome DNameListElem *ptr;
2161*472cd20dSToomas Soome // Note that we don't report errors for non-local, non-explicit domains
2162*472cd20dSToomas Soome for (ptr = AutoRegistrationDomains; ptr; ptr = ptr->next)
2163*472cd20dSToomas Soome if (!ptr->uid || SystemUID(request->uid) || request->uid == ptr->uid)
2164*472cd20dSToomas Soome register_service_instance(request, &ptr->name);
2165*472cd20dSToomas Soome }
2166*472cd20dSToomas Soome }
2167*472cd20dSToomas Soome return err;
2168*472cd20dSToomas Soome }
2169*472cd20dSToomas Soome
2170*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, TRUST_ENFORCEMENT)
2171*472cd20dSToomas Soome
_return_regservice_request_error(request_state * request,mStatus error)2172*472cd20dSToomas Soome mDNSlocal void _return_regservice_request_error(request_state *request, mStatus error)
2173*472cd20dSToomas Soome {
2174*472cd20dSToomas Soome if (request->u.servicereg.txtdata)
2175*472cd20dSToomas Soome {
2176*472cd20dSToomas Soome freeL("service_info txtdata", request->u.servicereg.txtdata);
2177*472cd20dSToomas Soome request->u.servicereg.txtdata = NULL;
2178*472cd20dSToomas Soome }
2179*472cd20dSToomas Soome
2180*472cd20dSToomas Soome reply_state *rep;
2181*472cd20dSToomas Soome if (GenerateNTDResponse(NULL, 0, request, &rep, reg_service_reply_op, 0, error) != mStatus_NoError)
2182*472cd20dSToomas Soome {
2183*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT, "[R%u] DNSServiceRegister _return_regservice_request_error: error(%d)", request->request_id, error);
2184*472cd20dSToomas Soome }
2185*472cd20dSToomas Soome else
2186*472cd20dSToomas Soome {
2187*472cd20dSToomas Soome append_reply(request, rep);
2188*472cd20dSToomas Soome }
2189*472cd20dSToomas Soome }
2190*472cd20dSToomas Soome
_handle_regservice_request_with_trust(request_state * request,const domainname * const d)2191*472cd20dSToomas Soome mDNSlocal mStatus _handle_regservice_request_with_trust(request_state *request, const domainname * const d)
2192*472cd20dSToomas Soome {
2193*472cd20dSToomas Soome mStatus err;
2194*472cd20dSToomas Soome if (audit_token_to_pid(request->audit_token) == 0)
2195*472cd20dSToomas Soome {
2196*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_WARNING, "[R%u] _handle_regservice_request_with_trust: no audit token for pid(%s %d)", request->request_id, request->pid_name, request->process_id);
2197*472cd20dSToomas Soome err = _handle_regservice_request_start(request, d);
2198*472cd20dSToomas Soome }
2199*472cd20dSToomas Soome else
2200*472cd20dSToomas Soome {
2201*472cd20dSToomas Soome mdns_trust_flags_t flags = mdns_trust_flags_none;
2202*472cd20dSToomas Soome mdns_trust_status_t status = mdns_trust_check_register_service(request->audit_token, request->u.servicereg.type_as_string, &flags);
2203*472cd20dSToomas Soome switch (status) {
2204*472cd20dSToomas Soome case mdns_trust_status_denied:
2205*472cd20dSToomas Soome case mdns_trust_status_pending:
2206*472cd20dSToomas Soome {
2207*472cd20dSToomas Soome mdns_trust_t trust = mdns_trust_create(request->audit_token, request->u.servicereg.type_as_string, flags);
2208*472cd20dSToomas Soome if (!trust)
2209*472cd20dSToomas Soome {
2210*472cd20dSToomas Soome err = mStatus_NoMemoryErr;
2211*472cd20dSToomas Soome goto exit;
2212*472cd20dSToomas Soome }
2213*472cd20dSToomas Soome void * context = mallocL("context/_handle_regservice_request_with_trust", sizeof(domainname));
2214*472cd20dSToomas Soome if (!context)
2215*472cd20dSToomas Soome {
2216*472cd20dSToomas Soome my_perror("ERROR: mallocL context/_handle_regservice_request_with_trust");
2217*472cd20dSToomas Soome mdns_release(trust);
2218*472cd20dSToomas Soome err = mStatus_NoMemoryErr;
2219*472cd20dSToomas Soome goto exit;
2220*472cd20dSToomas Soome }
2221*472cd20dSToomas Soome memcpy(context, d, sizeof(domainname));
2222*472cd20dSToomas Soome mdns_trust_set_context(trust, context);
2223*472cd20dSToomas Soome
2224*472cd20dSToomas Soome mdns_trust_set_queue(trust, _get_trust_results_dispatch_queue());
2225*472cd20dSToomas Soome mdns_trust_set_event_handler(trust, ^(mdns_trust_event_t event, mdns_trust_status_t update)
2226*472cd20dSToomas Soome {
2227*472cd20dSToomas Soome if (event == mdns_trust_event_result)
2228*472cd20dSToomas Soome {
2229*472cd20dSToomas Soome mStatus error = (update != mdns_trust_status_granted) ? mStatus_PolicyDenied : mStatus_NoError;
2230*472cd20dSToomas Soome KQueueLock();
2231*472cd20dSToomas Soome const domainname * _d = mdns_trust_get_context(trust);
2232*472cd20dSToomas Soome if (_d)
2233*472cd20dSToomas Soome {
2234*472cd20dSToomas Soome if (!error)
2235*472cd20dSToomas Soome {
2236*472cd20dSToomas Soome error = _handle_regservice_request_start(request, _d);
2237*472cd20dSToomas Soome // No context means the request was canceled before we got here
2238*472cd20dSToomas Soome }
2239*472cd20dSToomas Soome if (error) // (not else if) Always check for error result
2240*472cd20dSToomas Soome {
2241*472cd20dSToomas Soome _return_regservice_request_error(request, error);
2242*472cd20dSToomas Soome }
2243*472cd20dSToomas Soome }
2244*472cd20dSToomas Soome KQueueUnlock("_register_service_instance_with_trust");
2245*472cd20dSToomas Soome }
2246*472cd20dSToomas Soome });
2247*472cd20dSToomas Soome request->trust = trust;
2248*472cd20dSToomas Soome mdns_trust_activate(trust);
2249*472cd20dSToomas Soome err = mStatus_NoError;
2250*472cd20dSToomas Soome break;
2251*472cd20dSToomas Soome }
2252*472cd20dSToomas Soome
2253*472cd20dSToomas Soome case mdns_trust_status_no_entitlement:
2254*472cd20dSToomas Soome err = mStatus_NoAuth;
2255*472cd20dSToomas Soome break;
2256*472cd20dSToomas Soome
2257*472cd20dSToomas Soome case mdns_trust_status_granted:
2258*472cd20dSToomas Soome err = _handle_regservice_request_start(request, d);
2259*472cd20dSToomas Soome break;
2260*472cd20dSToomas Soome
2261*472cd20dSToomas Soome default:
2262*472cd20dSToomas Soome err = mStatus_UnknownErr;
2263*472cd20dSToomas Soome break;
2264*472cd20dSToomas Soome }
2265*472cd20dSToomas Soome }
2266*472cd20dSToomas Soome exit:
2267*472cd20dSToomas Soome return err;
2268*472cd20dSToomas Soome }
2269*472cd20dSToomas Soome #endif // TRUST_ENFORCEMENT
2270*472cd20dSToomas Soome
handle_regservice_request(request_state * request)2271c65ebfc7SToomas Soome mDNSlocal mStatus handle_regservice_request(request_state *request)
2272c65ebfc7SToomas Soome {
2273c65ebfc7SToomas Soome char name[256]; // Lots of spare space for extra-long names that we'll auto-truncate down to 63 bytes
2274c65ebfc7SToomas Soome char domain[MAX_ESCAPED_DOMAIN_NAME], host[MAX_ESCAPED_DOMAIN_NAME];
2275*472cd20dSToomas Soome char type_as_string[MAX_ESCAPED_DOMAIN_NAME]; // Note that this service type may include a trailing list of subtypes
2276c65ebfc7SToomas Soome domainname d, srv;
2277c65ebfc7SToomas Soome mStatus err;
2278c65ebfc7SToomas Soome const char *msgTXTData;
2279c65ebfc7SToomas Soome
2280c65ebfc7SToomas Soome DNSServiceFlags flags = get_flags(&request->msgptr, request->msgend);
2281c65ebfc7SToomas Soome mDNSu32 interfaceIndex = get_uint32(&request->msgptr, request->msgend);
2282c65ebfc7SToomas Soome mDNSInterfaceID InterfaceID;
2283c65ebfc7SToomas Soome
2284c65ebfc7SToomas Soome // Map kDNSServiceInterfaceIndexP2P to kDNSServiceInterfaceIndexAny with the
2285c65ebfc7SToomas Soome // kDNSServiceFlagsIncludeP2P flag set.
2286c65ebfc7SToomas Soome if (interfaceIndex == kDNSServiceInterfaceIndexP2P)
2287c65ebfc7SToomas Soome {
2288c65ebfc7SToomas Soome LogOperation("handle_regservice_request: mapping kDNSServiceInterfaceIndexP2P to kDNSServiceInterfaceIndexAny + kDNSServiceFlagsIncludeP2P");
2289c65ebfc7SToomas Soome flags |= kDNSServiceFlagsIncludeP2P;
2290c65ebfc7SToomas Soome interfaceIndex = kDNSServiceInterfaceIndexAny;
2291c65ebfc7SToomas Soome }
2292c65ebfc7SToomas Soome
2293c65ebfc7SToomas Soome InterfaceID = mDNSPlatformInterfaceIDfromInterfaceIndex(&mDNSStorage, interfaceIndex);
2294c65ebfc7SToomas Soome
2295c65ebfc7SToomas Soome // The registration is scoped to a specific interface index, but the
2296c65ebfc7SToomas Soome // interface is not currently in our list.
2297c65ebfc7SToomas Soome if (interfaceIndex && !InterfaceID)
2298c65ebfc7SToomas Soome {
2299c65ebfc7SToomas Soome // If it's one of the specially defined inteface index values, just return an error.
2300c65ebfc7SToomas Soome if (PreDefinedInterfaceIndex(interfaceIndex))
2301c65ebfc7SToomas Soome {
2302c65ebfc7SToomas Soome LogInfo("handle_regservice_request: bad interfaceIndex %d", interfaceIndex);
2303c65ebfc7SToomas Soome return(mStatus_BadParamErr);
2304c65ebfc7SToomas Soome }
2305c65ebfc7SToomas Soome
2306c65ebfc7SToomas Soome // Otherwise, use the specified interface index value and the registration will
2307c65ebfc7SToomas Soome // be applied to that interface when it comes up.
2308c65ebfc7SToomas Soome InterfaceID = (mDNSInterfaceID)(uintptr_t)interfaceIndex;
2309c65ebfc7SToomas Soome LogInfo("handle_regservice_request: registration pending for interface index %d", interfaceIndex);
2310c65ebfc7SToomas Soome }
2311c65ebfc7SToomas Soome
2312c65ebfc7SToomas Soome if (get_string(&request->msgptr, request->msgend, name, sizeof(name )) < 0 ||
2313*472cd20dSToomas Soome get_string(&request->msgptr, request->msgend, type_as_string, sizeof(type_as_string)) < 0 ||
2314*472cd20dSToomas Soome get_string(&request->msgptr, request->msgend, domain, sizeof(domain )) < 0 ||
2315*472cd20dSToomas Soome get_string(&request->msgptr, request->msgend, host, sizeof(host )) < 0)
2316c65ebfc7SToomas Soome { LogMsg("ERROR: handle_regservice_request - Couldn't read name/regtype/domain"); return(mStatus_BadParamErr); }
2317c65ebfc7SToomas Soome
2318c65ebfc7SToomas Soome request->flags = flags;
2319c65ebfc7SToomas Soome request->interfaceIndex = interfaceIndex;
2320c65ebfc7SToomas Soome request->u.servicereg.InterfaceID = InterfaceID;
2321c65ebfc7SToomas Soome request->u.servicereg.instances = NULL;
2322c65ebfc7SToomas Soome request->u.servicereg.txtlen = 0;
2323c65ebfc7SToomas Soome request->u.servicereg.txtdata = NULL;
2324c65ebfc7SToomas Soome mDNSPlatformStrLCopy(request->u.servicereg.type_as_string, type_as_string, sizeof(request->u.servicereg.type_as_string));
2325c65ebfc7SToomas Soome
2326c65ebfc7SToomas Soome if (request->msgptr + 2 > request->msgend) request->msgptr = NULL;
2327c65ebfc7SToomas Soome else
2328c65ebfc7SToomas Soome {
2329c65ebfc7SToomas Soome request->u.servicereg.port.b[0] = *request->msgptr++;
2330c65ebfc7SToomas Soome request->u.servicereg.port.b[1] = *request->msgptr++;
2331c65ebfc7SToomas Soome }
2332c65ebfc7SToomas Soome
2333c65ebfc7SToomas Soome request->u.servicereg.txtlen = get_uint16(&request->msgptr, request->msgend);
2334c65ebfc7SToomas Soome msgTXTData = get_rdata(&request->msgptr, request->msgend, request->u.servicereg.txtlen);
2335c65ebfc7SToomas Soome
2336c65ebfc7SToomas Soome if (!request->msgptr) { LogMsg("%3d: DNSServiceRegister(unreadable parameters)", request->sd); return(mStatus_BadParamErr); }
2337c65ebfc7SToomas Soome
2338c65ebfc7SToomas Soome if (request->u.servicereg.txtlen)
2339c65ebfc7SToomas Soome {
2340c65ebfc7SToomas Soome request->u.servicereg.txtdata = mallocL("service_info txtdata", request->u.servicereg.txtlen);
2341c65ebfc7SToomas Soome if (!request->u.servicereg.txtdata) FatalError("ERROR: handle_regservice_request - malloc");
2342c65ebfc7SToomas Soome mDNSPlatformMemCopy(request->u.servicereg.txtdata, msgTXTData, request->u.servicereg.txtlen);
2343c65ebfc7SToomas Soome }
2344c65ebfc7SToomas Soome
2345c65ebfc7SToomas Soome // Check for sub-types after the service type
2346*472cd20dSToomas Soome request->u.servicereg.num_subtypes = ChopSubTypes(request->u.servicereg.type_as_string); // Note: Modifies regtype string to remove trailing subtypes
2347c65ebfc7SToomas Soome if (request->u.servicereg.num_subtypes < 0)
2348c65ebfc7SToomas Soome {
2349c65ebfc7SToomas Soome LogMsg("ERROR: handle_regservice_request - ChopSubTypes failed %s", request->u.servicereg.type_as_string);
2350c65ebfc7SToomas Soome goto bad_param;
2351c65ebfc7SToomas Soome }
2352c65ebfc7SToomas Soome
2353c65ebfc7SToomas Soome // Don't try to construct "domainname t" until *after* ChopSubTypes has worked its magic
2354c65ebfc7SToomas Soome if (!*request->u.servicereg.type_as_string || !MakeDomainNameFromDNSNameString(&request->u.servicereg.type, request->u.servicereg.type_as_string))
2355c65ebfc7SToomas Soome { LogMsg("ERROR: handle_regservice_request - type_as_string bad %s", request->u.servicereg.type_as_string); goto bad_param; }
2356c65ebfc7SToomas Soome
2357c65ebfc7SToomas Soome if (!name[0])
2358c65ebfc7SToomas Soome {
2359c65ebfc7SToomas Soome request->u.servicereg.name = mDNSStorage.nicelabel;
2360c65ebfc7SToomas Soome request->u.servicereg.autoname = mDNStrue;
2361c65ebfc7SToomas Soome }
2362c65ebfc7SToomas Soome else
2363c65ebfc7SToomas Soome {
2364c65ebfc7SToomas Soome // If the client is allowing AutoRename, then truncate name to legal length before converting it to a DomainLabel
2365c65ebfc7SToomas Soome if ((flags & kDNSServiceFlagsNoAutoRename) == 0)
2366c65ebfc7SToomas Soome {
2367c65ebfc7SToomas Soome int newlen = TruncateUTF8ToLength((mDNSu8*)name, mDNSPlatformStrLen(name), MAX_DOMAIN_LABEL);
2368c65ebfc7SToomas Soome name[newlen] = 0;
2369c65ebfc7SToomas Soome }
2370c65ebfc7SToomas Soome if (!MakeDomainLabelFromLiteralString(&request->u.servicereg.name, name))
2371c65ebfc7SToomas Soome { LogMsg("ERROR: handle_regservice_request - name bad %s", name); goto bad_param; }
2372c65ebfc7SToomas Soome request->u.servicereg.autoname = mDNSfalse;
2373c65ebfc7SToomas Soome }
2374c65ebfc7SToomas Soome
2375c65ebfc7SToomas Soome if (*domain)
2376c65ebfc7SToomas Soome {
2377c65ebfc7SToomas Soome request->u.servicereg.default_domain = mDNSfalse;
2378c65ebfc7SToomas Soome if (!MakeDomainNameFromDNSNameString(&d, domain))
2379c65ebfc7SToomas Soome { LogMsg("ERROR: handle_regservice_request - domain bad %s", domain); goto bad_param; }
2380c65ebfc7SToomas Soome }
2381c65ebfc7SToomas Soome else
2382c65ebfc7SToomas Soome {
2383c65ebfc7SToomas Soome request->u.servicereg.default_domain = mDNStrue;
2384c65ebfc7SToomas Soome MakeDomainNameFromDNSNameString(&d, "local.");
2385c65ebfc7SToomas Soome }
2386c65ebfc7SToomas Soome
2387c65ebfc7SToomas Soome if (!ConstructServiceName(&srv, &request->u.servicereg.name, &request->u.servicereg.type, &d))
2388c65ebfc7SToomas Soome {
2389c65ebfc7SToomas Soome LogMsg("ERROR: handle_regservice_request - Couldn't ConstructServiceName from, “%#s” “%##s” “%##s”",
2390c65ebfc7SToomas Soome request->u.servicereg.name.c, request->u.servicereg.type.c, d.c); goto bad_param;
2391c65ebfc7SToomas Soome }
2392c65ebfc7SToomas Soome
2393c65ebfc7SToomas Soome if (!MakeDomainNameFromDNSNameString(&request->u.servicereg.host, host))
2394c65ebfc7SToomas Soome { LogMsg("ERROR: handle_regservice_request - host bad %s", host); goto bad_param; }
2395c65ebfc7SToomas Soome request->u.servicereg.autorename = (flags & kDNSServiceFlagsNoAutoRename ) == 0;
2396c65ebfc7SToomas Soome request->u.servicereg.allowremotequery = (flags & kDNSServiceFlagsAllowRemoteQuery) != 0;
2397c65ebfc7SToomas Soome
2398c65ebfc7SToomas Soome // Some clients use mDNS for lightweight copy protection, registering a pseudo-service with
2399c65ebfc7SToomas Soome // a port number of zero. When two instances of the protected client are allowed to run on one
2400c65ebfc7SToomas Soome // machine, we don't want to see misleading "Bogus client" messages in syslog and the console.
2401c65ebfc7SToomas Soome if (!mDNSIPPortIsZero(request->u.servicereg.port))
2402c65ebfc7SToomas Soome {
2403c65ebfc7SToomas Soome int count = CountExistingRegistrations(&srv, request->u.servicereg.port);
2404c65ebfc7SToomas Soome if (count)
2405c65ebfc7SToomas Soome LogMsg("Client application[%d](%s) registered %d identical instances of service %##s port %u.", request->process_id,
2406c65ebfc7SToomas Soome request->pid_name, count+1, srv.c, mDNSVal16(request->u.servicereg.port));
2407c65ebfc7SToomas Soome }
2408c65ebfc7SToomas Soome
2409c65ebfc7SToomas Soome #if APPLE_OSX_mDNSResponder && ENABLE_BLE_TRIGGERED_BONJOUR
2410c65ebfc7SToomas Soome // Determine if this request should be promoted to use BLE triggered feature.
2411c65ebfc7SToomas Soome if (shouldUseBLE(InterfaceID, 0, &request->u.servicereg.type, &d))
2412c65ebfc7SToomas Soome {
2413c65ebfc7SToomas Soome request->flags |= (kDNSServiceFlagsAutoTrigger | kDNSServiceFlagsIncludeAWDL);
2414c65ebfc7SToomas Soome LogInfo("handle_regservice_request: registration promoted to use kDNSServiceFlagsAutoTrigger");
2415c65ebfc7SToomas Soome }
2416c65ebfc7SToomas Soome #endif // APPLE_OSX_mDNSResponder && ENABLE_BLE_TRIGGERED_BONJOUR
2417c65ebfc7SToomas Soome
2418*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
2419*472cd20dSToomas Soome "[R%d] DNSServiceRegister(%X, %d, \"" PRI_S "\", \"" PRI_S "\", \"" PRI_S "\", \"" PRI_S "\", %u) START PID[%d](" PUB_S ")",
2420*472cd20dSToomas Soome request->request_id, request->flags, interfaceIndex, name, request->u.servicereg.type_as_string, domain, host,
2421c65ebfc7SToomas Soome mDNSVal16(request->u.servicereg.port), request->process_id, request->pid_name);
2422c65ebfc7SToomas Soome
2423c65ebfc7SToomas Soome // We need to unconditionally set request->terminate, because even if we didn't successfully
2424c65ebfc7SToomas Soome // start any registrations right now, subsequent configuration changes may cause successful
2425c65ebfc7SToomas Soome // registrations to be added, and we'll need to cancel them before freeing this memory.
2426c65ebfc7SToomas Soome // We also need to set request->terminate first, before adding additional service instances,
2427*472cd20dSToomas Soome // because the udsserver_validatelists uses the request->terminate function pointer to determine
2428c65ebfc7SToomas Soome // what kind of request this is, and therefore what kind of list validation is required.
2429*472cd20dSToomas Soome request->terminate = NULL;
2430c65ebfc7SToomas Soome
2431*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, TRUST_ENFORCEMENT)
2432*472cd20dSToomas Soome if (os_feature_enabled(mDNSResponder, bonjour_privacy) &&
2433*472cd20dSToomas Soome (request->u.servicereg.default_domain || IsLocalDomain(&d)))
2434c65ebfc7SToomas Soome {
2435*472cd20dSToomas Soome err = _handle_regservice_request_with_trust(request, &d);
2436*472cd20dSToomas Soome if (err == mStatus_NoAuth && request->u.servicereg.txtdata)
2437c65ebfc7SToomas Soome {
2438*472cd20dSToomas Soome freeL("service_info txtdata", request->u.servicereg.txtdata);
2439*472cd20dSToomas Soome request->u.servicereg.txtdata = NULL;
2440c65ebfc7SToomas Soome }
2441c65ebfc7SToomas Soome }
2442*472cd20dSToomas Soome else
2443*472cd20dSToomas Soome {
2444*472cd20dSToomas Soome err = _handle_regservice_request_start(request, &d);
2445*472cd20dSToomas Soome }
2446*472cd20dSToomas Soome #else
2447*472cd20dSToomas Soome err = _handle_regservice_request_start(request, &d);
2448*472cd20dSToomas Soome #endif
2449c65ebfc7SToomas Soome
2450c65ebfc7SToomas Soome return(err);
2451c65ebfc7SToomas Soome
2452c65ebfc7SToomas Soome bad_param:
2453c65ebfc7SToomas Soome freeL("handle_regservice_request (txtdata)", request->u.servicereg.txtdata);
2454c65ebfc7SToomas Soome request->u.servicereg.txtdata = NULL;
2455c65ebfc7SToomas Soome return mStatus_BadParamErr;
2456c65ebfc7SToomas Soome }
2457c65ebfc7SToomas Soome
2458c65ebfc7SToomas Soome // ***************************************************************************
2459c65ebfc7SToomas Soome #if COMPILER_LIKES_PRAGMA_MARK
2460c65ebfc7SToomas Soome #pragma mark -
2461c65ebfc7SToomas Soome #pragma mark - DNSServiceBrowse
2462c65ebfc7SToomas Soome #endif
2463c65ebfc7SToomas Soome
FoundInstance(mDNS * const m,DNSQuestion * question,const ResourceRecord * const answer,QC_result AddRecord)2464c65ebfc7SToomas Soome mDNSlocal void FoundInstance(mDNS *const m, DNSQuestion *question, const ResourceRecord *const answer, QC_result AddRecord)
2465c65ebfc7SToomas Soome {
2466c65ebfc7SToomas Soome DNSServiceFlags flags = AddRecord ? kDNSServiceFlagsAdd : 0;
2467c65ebfc7SToomas Soome request_state *req = question->QuestionContext;
2468c65ebfc7SToomas Soome reply_state *rep;
2469c65ebfc7SToomas Soome (void)m; // Unused
2470c65ebfc7SToomas Soome
2471c65ebfc7SToomas Soome if (answer->rrtype != kDNSType_PTR)
2472c65ebfc7SToomas Soome { LogMsg("%3d: FoundInstance: Should not be called with rrtype %d (not a PTR record)", req->sd, answer->rrtype); return; }
2473c65ebfc7SToomas Soome
2474c65ebfc7SToomas Soome if (mDNSOpaque16IsZero(question->TargetQID) && (question->BrowseThreshold > 0) && (question->CurrentAnswers >= question->BrowseThreshold))
2475c65ebfc7SToomas Soome {
2476c65ebfc7SToomas Soome flags |= kDNSServiceFlagsThresholdReached;
2477c65ebfc7SToomas Soome }
2478c65ebfc7SToomas Soome
2479c65ebfc7SToomas Soome // if returning a negative answer, then use question's name in reply
2480c65ebfc7SToomas Soome if (answer->RecordType == kDNSRecordTypePacketNegative)
2481c65ebfc7SToomas Soome {
2482c65ebfc7SToomas Soome GenerateBrowseReply(&question->qname, answer->InterfaceID, req, &rep, browse_reply_op, flags, kDNSServiceErr_NoSuchRecord);
2483c65ebfc7SToomas Soome goto validReply;
2484c65ebfc7SToomas Soome }
2485c65ebfc7SToomas Soome
2486c65ebfc7SToomas Soome if (GenerateNTDResponse(&answer->rdata->u.name, answer->InterfaceID, req, &rep, browse_reply_op, flags, mStatus_NoError) != mStatus_NoError)
2487c65ebfc7SToomas Soome {
2488c65ebfc7SToomas Soome if (SameDomainName(&req->u.browser.regtype, (const domainname*)"\x09_services\x07_dns-sd\x04_udp"))
2489c65ebfc7SToomas Soome {
2490c65ebfc7SToomas Soome // Special support to enable the DNSServiceBrowse call made by Bonjour Browser
2491c65ebfc7SToomas Soome // Remove after Bonjour Browser is updated to use DNSServiceQueryRecord instead of DNSServiceBrowse
2492c65ebfc7SToomas Soome GenerateBrowseReply(&answer->rdata->u.name, answer->InterfaceID, req, &rep, browse_reply_op, flags, mStatus_NoError);
2493c65ebfc7SToomas Soome goto validReply;
2494c65ebfc7SToomas Soome }
2495c65ebfc7SToomas Soome
2496c65ebfc7SToomas Soome LogMsg("%3d: FoundInstance: %##s PTR %##s received from network is not valid DNS-SD service pointer",
2497c65ebfc7SToomas Soome req->sd, answer->name->c, answer->rdata->u.name.c);
2498c65ebfc7SToomas Soome return;
2499c65ebfc7SToomas Soome }
2500c65ebfc7SToomas Soome
2501c65ebfc7SToomas Soome validReply:
2502c65ebfc7SToomas Soome
2503*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
2504*472cd20dSToomas Soome "[R%d->Q%d] DNSServiceBrowse(" PRI_DM_NAME ", " PUB_S ") RESULT " PUB_S " interface %d: " PRI_S,
2505*472cd20dSToomas Soome req->request_id, mDNSVal16(question->TargetQID), DM_NAME_PARAM(&question->qname), DNSTypeName(question->qtype),
2506*472cd20dSToomas Soome AddRecord ? "ADD" : "RMV", mDNSPlatformInterfaceIndexfromInterfaceID(m, answer->InterfaceID, mDNSfalse),
2507*472cd20dSToomas Soome RRDisplayString(m, answer));
2508c65ebfc7SToomas Soome
2509c65ebfc7SToomas Soome append_reply(req, rep);
2510c65ebfc7SToomas Soome }
2511c65ebfc7SToomas Soome
SetQuestionPolicy(DNSQuestion * q,request_state * req)2512c65ebfc7SToomas Soome mDNSlocal void SetQuestionPolicy(DNSQuestion *q, request_state *req)
2513c65ebfc7SToomas Soome {
2514c65ebfc7SToomas Soome q->euid = req->uid;
2515c65ebfc7SToomas Soome // The policy is either based on pid or UUID. Pass a zero pid
2516c65ebfc7SToomas Soome // to the "core" if the UUID is valid. If we always pass the pid,
2517c65ebfc7SToomas Soome // then the "core" needs to determine whether the uuid is valid
2518c65ebfc7SToomas Soome // by examining all the 16 bytes at the time of the policy
2519c65ebfc7SToomas Soome // check and also when setting the delegate socket option. Also, it
2520c65ebfc7SToomas Soome // requires that we zero out the uuid wherever the question is
2521c65ebfc7SToomas Soome // initialized to make sure that it is not interpreted as valid.
2522c65ebfc7SToomas Soome // To prevent these intrusive changes, just pass a zero pid to indicate
2523c65ebfc7SToomas Soome // that pid is not valid when uuid is valid. In future if we need the
2524c65ebfc7SToomas Soome // pid in the question, we will reevaluate this strategy.
2525c65ebfc7SToomas Soome if (req->validUUID)
2526c65ebfc7SToomas Soome {
2527c65ebfc7SToomas Soome mDNSPlatformMemCopy(q->uuid, req->uuid, UUID_SIZE);
2528c65ebfc7SToomas Soome q->pid = 0;
2529c65ebfc7SToomas Soome }
2530c65ebfc7SToomas Soome else
2531c65ebfc7SToomas Soome {
2532c65ebfc7SToomas Soome q->pid = req->process_id;
2533c65ebfc7SToomas Soome }
2534c65ebfc7SToomas Soome
2535c65ebfc7SToomas Soome //debugf("SetQuestionPolicy: q->euid[%d] q->pid[%d] uuid is valid : %s", q->euid, q->pid, req->validUUID ? "true" : "false");
2536c65ebfc7SToomas Soome }
2537c65ebfc7SToomas Soome
add_domain_to_browser(request_state * info,const domainname * d)2538c65ebfc7SToomas Soome mDNSlocal mStatus add_domain_to_browser(request_state *info, const domainname *d)
2539c65ebfc7SToomas Soome {
2540c65ebfc7SToomas Soome browser_t *b, *p;
2541c65ebfc7SToomas Soome mStatus err;
2542c65ebfc7SToomas Soome
2543c65ebfc7SToomas Soome for (p = info->u.browser.browsers; p; p = p->next)
2544c65ebfc7SToomas Soome {
2545c65ebfc7SToomas Soome if (SameDomainName(&p->domain, d))
2546c65ebfc7SToomas Soome { debugf("add_domain_to_browser %##s already in list", d->c); return mStatus_AlreadyRegistered; }
2547c65ebfc7SToomas Soome }
2548c65ebfc7SToomas Soome
2549*472cd20dSToomas Soome b = (browser_t *) callocL("browser_t", sizeof(*b));
2550c65ebfc7SToomas Soome if (!b) return mStatus_NoMemoryErr;
2551c65ebfc7SToomas Soome AssignDomainName(&b->domain, d);
2552c65ebfc7SToomas Soome SetQuestionPolicy(&b->q, info);
2553*472cd20dSToomas Soome err = mDNS_StartBrowse(&mDNSStorage, &b->q, &info->u.browser.regtype, d, info->u.browser.interface_id, info->flags,
2554c65ebfc7SToomas Soome info->u.browser.ForceMCast, (info->flags & kDNSServiceFlagsBackgroundTrafficClass) != 0, FoundInstance, info);
2555c65ebfc7SToomas Soome if (err)
2556c65ebfc7SToomas Soome {
2557c65ebfc7SToomas Soome LogMsg("mDNS_StartBrowse returned %d for type %##s domain %##s", err, info->u.browser.regtype.c, d->c);
2558c65ebfc7SToomas Soome freeL("browser_t/add_domain_to_browser", b);
2559c65ebfc7SToomas Soome }
2560c65ebfc7SToomas Soome else
2561c65ebfc7SToomas Soome {
2562c65ebfc7SToomas Soome b->next = info->u.browser.browsers;
2563c65ebfc7SToomas Soome info->u.browser.browsers = b;
2564c65ebfc7SToomas Soome
2565c65ebfc7SToomas Soome #if APPLE_OSX_mDNSResponder && ENABLE_BLE_TRIGGERED_BONJOUR
2566c65ebfc7SToomas Soome // Determine if this request should be promoted to use BLE triggered discovery.
2567c65ebfc7SToomas Soome if (shouldUseBLE(info->u.browser.interface_id, 0, &info->u.browser.regtype, (domainname *) d))
2568c65ebfc7SToomas Soome {
2569c65ebfc7SToomas Soome info->flags |= (kDNSServiceFlagsAutoTrigger | kDNSServiceFlagsIncludeAWDL);
2570c65ebfc7SToomas Soome b->q.flags |= (kDNSServiceFlagsAutoTrigger | kDNSServiceFlagsIncludeAWDL);
2571c65ebfc7SToomas Soome LogInfo("add_domain_to_browser: request promoted to use kDNSServiceFlagsAutoTrigger");
2572c65ebfc7SToomas Soome }
2573c65ebfc7SToomas Soome #endif // APPLE_OSX_mDNSResponder && ENABLE_BLE_TRIGGERED_BONJOUR
2574c65ebfc7SToomas Soome
2575c65ebfc7SToomas Soome LogMcastQ(&b->q, info, q_start);
2576*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, D2D)
2577c65ebfc7SToomas Soome if (callExternalHelpers(info->u.browser.interface_id, &b->domain, info->flags))
2578c65ebfc7SToomas Soome {
2579c65ebfc7SToomas Soome domainname tmp;
2580c65ebfc7SToomas Soome ConstructServiceName(&tmp, NULL, &info->u.browser.regtype, &b->domain);
25813b436d06SToomas Soome LogDebug("add_domain_to_browser: calling external_start_browsing_for_service()");
2582*472cd20dSToomas Soome external_start_browsing_for_service(info->u.browser.interface_id, &tmp, kDNSType_PTR, info->flags, info->process_id);
2583c65ebfc7SToomas Soome }
2584*472cd20dSToomas Soome #endif
2585c65ebfc7SToomas Soome }
2586c65ebfc7SToomas Soome return err;
2587c65ebfc7SToomas Soome }
2588c65ebfc7SToomas Soome
browse_termination_callback(request_state * info)2589c65ebfc7SToomas Soome mDNSlocal void browse_termination_callback(request_state *info)
2590c65ebfc7SToomas Soome {
2591c65ebfc7SToomas Soome if (info->u.browser.default_domain)
2592c65ebfc7SToomas Soome {
2593c65ebfc7SToomas Soome // Stop the domain enumeration queries to discover the WAB legacy browse domains
2594c65ebfc7SToomas Soome LogInfo("%3d: DNSServiceBrowse Cancel WAB PID[%d](%s)", info->sd, info->process_id, info->pid_name);
2595c65ebfc7SToomas Soome uDNS_StopWABQueries(&mDNSStorage, UDNS_WAB_LBROWSE_QUERY);
2596c65ebfc7SToomas Soome }
2597c65ebfc7SToomas Soome while (info->u.browser.browsers)
2598c65ebfc7SToomas Soome {
2599c65ebfc7SToomas Soome browser_t *ptr = info->u.browser.browsers;
2600c65ebfc7SToomas Soome
2601*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, D2D)
2602c65ebfc7SToomas Soome if (callExternalHelpers(ptr->q.InterfaceID, &ptr->domain, ptr->q.flags))
2603c65ebfc7SToomas Soome {
2604c65ebfc7SToomas Soome domainname tmp;
2605c65ebfc7SToomas Soome ConstructServiceName(&tmp, NULL, &info->u.browser.regtype, &ptr->domain);
2606c65ebfc7SToomas Soome LogInfo("browse_termination_callback: calling external_stop_browsing_for_service()");
2607*472cd20dSToomas Soome external_stop_browsing_for_service(ptr->q.InterfaceID, &tmp, kDNSType_PTR, ptr->q.flags, info->process_id);
2608c65ebfc7SToomas Soome }
2609*472cd20dSToomas Soome #endif
2610*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
2611*472cd20dSToomas Soome "[R%d] DNSServiceBrowse(%X, %d, \"" PRI_DM_NAME "\") STOP PID[%d](" PUB_S ")",
2612*472cd20dSToomas Soome info->request_id, info->flags, info->interfaceIndex, DM_NAME_PARAM(&ptr->q.qname),
2613*472cd20dSToomas Soome info->process_id, info->pid_name);
2614c65ebfc7SToomas Soome
2615c65ebfc7SToomas Soome info->u.browser.browsers = ptr->next;
2616c65ebfc7SToomas Soome mDNS_StopBrowse(&mDNSStorage, &ptr->q); // no need to error-check result
2617c65ebfc7SToomas Soome LogMcastQ(&ptr->q, info, q_stop);
2618c65ebfc7SToomas Soome freeL("browser_t/browse_termination_callback", ptr);
2619c65ebfc7SToomas Soome }
2620c65ebfc7SToomas Soome }
2621c65ebfc7SToomas Soome
udsserver_automatic_browse_domain_changed(const DNameListElem * const d,const mDNSBool add)2622c65ebfc7SToomas Soome mDNSlocal void udsserver_automatic_browse_domain_changed(const DNameListElem *const d, const mDNSBool add)
2623c65ebfc7SToomas Soome {
2624c65ebfc7SToomas Soome request_state *request;
2625c65ebfc7SToomas Soome debugf("udsserver_automatic_browse_domain_changed: %s default browse domain %##s", add ? "Adding" : "Removing", d->name.c);
2626c65ebfc7SToomas Soome
2627c65ebfc7SToomas Soome for (request = all_requests; request; request = request->next)
2628c65ebfc7SToomas Soome {
2629c65ebfc7SToomas Soome if (request->terminate != browse_termination_callback) continue; // Not a browse operation
2630c65ebfc7SToomas Soome if (!request->u.browser.default_domain) continue; // Not an auto-browse operation
2631c65ebfc7SToomas Soome if (!d->uid || SystemUID(request->uid) || request->uid == d->uid)
2632c65ebfc7SToomas Soome {
2633c65ebfc7SToomas Soome browser_t **ptr = &request->u.browser.browsers;
2634c65ebfc7SToomas Soome while (*ptr && !SameDomainName(&(*ptr)->domain, &d->name)) ptr = &(*ptr)->next;
2635c65ebfc7SToomas Soome if (add)
2636c65ebfc7SToomas Soome {
2637c65ebfc7SToomas Soome // If we don't already have this domain in our list for this browse operation, add it now
2638c65ebfc7SToomas Soome if (!*ptr) add_domain_to_browser(request, &d->name);
2639c65ebfc7SToomas Soome else debugf("udsserver_automatic_browse_domain_changed %##s already in list, not re-adding", &d->name);
2640c65ebfc7SToomas Soome }
2641c65ebfc7SToomas Soome else
2642c65ebfc7SToomas Soome {
2643c65ebfc7SToomas Soome if (!*ptr) LogMsg("udsserver_automatic_browse_domain_changed ERROR %##s not found", &d->name);
2644c65ebfc7SToomas Soome else
2645c65ebfc7SToomas Soome {
2646c65ebfc7SToomas Soome DNameListElem *p;
2647c65ebfc7SToomas Soome for (p = AutoBrowseDomains; p; p=p->next)
2648c65ebfc7SToomas Soome if (!p->uid || SystemUID(request->uid) || request->uid == p->uid)
2649c65ebfc7SToomas Soome if (SameDomainName(&d->name, &p->name)) break;
2650c65ebfc7SToomas Soome if (p) debugf("udsserver_automatic_browse_domain_changed %##s still in list, not removing", &d->name);
2651c65ebfc7SToomas Soome else
2652c65ebfc7SToomas Soome {
2653c65ebfc7SToomas Soome browser_t *rem = *ptr;
2654c65ebfc7SToomas Soome *ptr = (*ptr)->next;
2655c65ebfc7SToomas Soome mDNS_StopQueryWithRemoves(&mDNSStorage, &rem->q);
2656c65ebfc7SToomas Soome freeL("browser_t/udsserver_automatic_browse_domain_changed", rem);
2657c65ebfc7SToomas Soome }
2658c65ebfc7SToomas Soome }
2659c65ebfc7SToomas Soome }
2660c65ebfc7SToomas Soome }
2661c65ebfc7SToomas Soome }
2662c65ebfc7SToomas Soome }
2663c65ebfc7SToomas Soome
FreeARElemCallback(mDNS * const m,AuthRecord * const rr,mStatus result)2664c65ebfc7SToomas Soome mDNSlocal void FreeARElemCallback(mDNS *const m, AuthRecord *const rr, mStatus result)
2665c65ebfc7SToomas Soome {
2666c65ebfc7SToomas Soome (void)m; // unused
2667c65ebfc7SToomas Soome if (result == mStatus_MemFree)
2668c65ebfc7SToomas Soome {
2669c65ebfc7SToomas Soome // On shutdown, mDNS_Close automatically deregisters all records
2670c65ebfc7SToomas Soome // Since in this case no one has called DeregisterLocalOnlyDomainEnumPTR to cut the record
2671c65ebfc7SToomas Soome // from the LocalDomainEnumRecords list, we do this here before we free the memory.
2672c65ebfc7SToomas Soome // (This should actually no longer be necessary, now that we do the proper cleanup in
2673c65ebfc7SToomas Soome // udsserver_exit. To confirm this, we'll log an error message if we do find a record that
2674c65ebfc7SToomas Soome // hasn't been cut from the list yet. If these messages don't appear, we can delete this code.)
2675c65ebfc7SToomas Soome ARListElem **ptr = &LocalDomainEnumRecords;
2676c65ebfc7SToomas Soome while (*ptr && &(*ptr)->ar != rr) ptr = &(*ptr)->next;
2677c65ebfc7SToomas Soome if (*ptr) { *ptr = (*ptr)->next; LogMsg("FreeARElemCallback: Have to cut %s", ARDisplayString(m, rr)); }
2678c65ebfc7SToomas Soome mDNSPlatformMemFree(rr->RecordContext);
2679c65ebfc7SToomas Soome }
2680c65ebfc7SToomas Soome }
2681c65ebfc7SToomas Soome
2682c65ebfc7SToomas Soome // RegisterLocalOnlyDomainEnumPTR and DeregisterLocalOnlyDomainEnumPTR largely duplicate code in
2683c65ebfc7SToomas Soome // "FoundDomain" in uDNS.c for creating and destroying these special mDNSInterface_LocalOnly records.
2684c65ebfc7SToomas Soome // We may want to turn the common code into a subroutine.
2685c65ebfc7SToomas Soome
RegisterLocalOnlyDomainEnumPTR(mDNS * m,const domainname * d,int type)2686c65ebfc7SToomas Soome mDNSlocal void RegisterLocalOnlyDomainEnumPTR(mDNS *m, const domainname *d, int type)
2687c65ebfc7SToomas Soome {
2688c65ebfc7SToomas Soome // allocate/register legacy and non-legacy _browse PTR record
2689c65ebfc7SToomas Soome mStatus err;
2690*472cd20dSToomas Soome ARListElem *ptr = (ARListElem *) mDNSPlatformMemAllocateClear(sizeof(*ptr));
2691c65ebfc7SToomas Soome
2692c65ebfc7SToomas Soome debugf("Incrementing %s refcount for %##s",
2693c65ebfc7SToomas Soome (type == mDNS_DomainTypeBrowse ) ? "browse domain " :
2694c65ebfc7SToomas Soome (type == mDNS_DomainTypeRegistration ) ? "registration dom" :
2695c65ebfc7SToomas Soome (type == mDNS_DomainTypeBrowseAutomatic) ? "automatic browse" : "?", d->c);
2696c65ebfc7SToomas Soome
2697c65ebfc7SToomas Soome mDNS_SetupResourceRecord(&ptr->ar, mDNSNULL, mDNSInterface_LocalOnly, kDNSType_PTR, 7200, kDNSRecordTypeShared, AuthRecordLocalOnly, FreeARElemCallback, ptr);
2698c65ebfc7SToomas Soome MakeDomainNameFromDNSNameString(&ptr->ar.namestorage, mDNS_DomainTypeNames[type]);
2699c65ebfc7SToomas Soome AppendDNSNameString (&ptr->ar.namestorage, "local");
2700c65ebfc7SToomas Soome AssignDomainName(&ptr->ar.resrec.rdata->u.name, d);
2701c65ebfc7SToomas Soome err = mDNS_Register(m, &ptr->ar);
2702c65ebfc7SToomas Soome if (err)
2703c65ebfc7SToomas Soome {
2704c65ebfc7SToomas Soome LogMsg("SetSCPrefsBrowseDomain: mDNS_Register returned error %d", err);
2705c65ebfc7SToomas Soome mDNSPlatformMemFree(ptr);
2706c65ebfc7SToomas Soome }
2707c65ebfc7SToomas Soome else
2708c65ebfc7SToomas Soome {
2709c65ebfc7SToomas Soome ptr->next = LocalDomainEnumRecords;
2710c65ebfc7SToomas Soome LocalDomainEnumRecords = ptr;
2711c65ebfc7SToomas Soome }
2712c65ebfc7SToomas Soome }
2713c65ebfc7SToomas Soome
DeregisterLocalOnlyDomainEnumPTR(mDNS * m,const domainname * d,int type)2714c65ebfc7SToomas Soome mDNSlocal void DeregisterLocalOnlyDomainEnumPTR(mDNS *m, const domainname *d, int type)
2715c65ebfc7SToomas Soome {
2716c65ebfc7SToomas Soome ARListElem **ptr = &LocalDomainEnumRecords;
2717c65ebfc7SToomas Soome domainname lhs; // left-hand side of PTR, for comparison
2718c65ebfc7SToomas Soome
2719c65ebfc7SToomas Soome debugf("Decrementing %s refcount for %##s",
2720c65ebfc7SToomas Soome (type == mDNS_DomainTypeBrowse ) ? "browse domain " :
2721c65ebfc7SToomas Soome (type == mDNS_DomainTypeRegistration ) ? "registration dom" :
2722c65ebfc7SToomas Soome (type == mDNS_DomainTypeBrowseAutomatic) ? "automatic browse" : "?", d->c);
2723c65ebfc7SToomas Soome
2724c65ebfc7SToomas Soome MakeDomainNameFromDNSNameString(&lhs, mDNS_DomainTypeNames[type]);
2725c65ebfc7SToomas Soome AppendDNSNameString (&lhs, "local");
2726c65ebfc7SToomas Soome
2727c65ebfc7SToomas Soome while (*ptr)
2728c65ebfc7SToomas Soome {
2729c65ebfc7SToomas Soome if (SameDomainName(&(*ptr)->ar.resrec.rdata->u.name, d) && SameDomainName((*ptr)->ar.resrec.name, &lhs))
2730c65ebfc7SToomas Soome {
2731c65ebfc7SToomas Soome ARListElem *rem = *ptr;
2732c65ebfc7SToomas Soome *ptr = (*ptr)->next;
2733c65ebfc7SToomas Soome mDNS_Deregister(m, &rem->ar);
2734c65ebfc7SToomas Soome return;
2735c65ebfc7SToomas Soome }
2736c65ebfc7SToomas Soome else ptr = &(*ptr)->next;
2737c65ebfc7SToomas Soome }
2738c65ebfc7SToomas Soome }
2739c65ebfc7SToomas Soome
AddAutoBrowseDomain(const mDNSu32 uid,const domainname * const name)2740c65ebfc7SToomas Soome mDNSlocal void AddAutoBrowseDomain(const mDNSu32 uid, const domainname *const name)
2741c65ebfc7SToomas Soome {
2742*472cd20dSToomas Soome DNameListElem *new = (DNameListElem *) mDNSPlatformMemAllocateClear(sizeof(*new));
2743c65ebfc7SToomas Soome if (!new) { LogMsg("ERROR: malloc"); return; }
2744c65ebfc7SToomas Soome AssignDomainName(&new->name, name);
2745c65ebfc7SToomas Soome new->uid = uid;
2746c65ebfc7SToomas Soome new->next = AutoBrowseDomains;
2747c65ebfc7SToomas Soome AutoBrowseDomains = new;
2748c65ebfc7SToomas Soome udsserver_automatic_browse_domain_changed(new, mDNStrue);
2749c65ebfc7SToomas Soome }
2750c65ebfc7SToomas Soome
RmvAutoBrowseDomain(const mDNSu32 uid,const domainname * const name)2751c65ebfc7SToomas Soome mDNSlocal void RmvAutoBrowseDomain(const mDNSu32 uid, const domainname *const name)
2752c65ebfc7SToomas Soome {
2753c65ebfc7SToomas Soome DNameListElem **p = &AutoBrowseDomains;
2754c65ebfc7SToomas Soome while (*p && (!SameDomainName(&(*p)->name, name) || (*p)->uid != uid)) p = &(*p)->next;
2755c65ebfc7SToomas Soome if (!*p) LogMsg("RmvAutoBrowseDomain: Got remove event for domain %##s not in list", name->c);
2756c65ebfc7SToomas Soome else
2757c65ebfc7SToomas Soome {
2758c65ebfc7SToomas Soome DNameListElem *ptr = *p;
2759c65ebfc7SToomas Soome *p = ptr->next;
2760c65ebfc7SToomas Soome udsserver_automatic_browse_domain_changed(ptr, mDNSfalse);
2761c65ebfc7SToomas Soome mDNSPlatformMemFree(ptr);
2762c65ebfc7SToomas Soome }
2763c65ebfc7SToomas Soome }
2764c65ebfc7SToomas Soome
SetPrefsBrowseDomains(mDNS * m,DNameListElem * browseDomains,mDNSBool add)2765c65ebfc7SToomas Soome mDNSlocal void SetPrefsBrowseDomains(mDNS *m, DNameListElem *browseDomains, mDNSBool add)
2766c65ebfc7SToomas Soome {
2767c65ebfc7SToomas Soome DNameListElem *d;
2768c65ebfc7SToomas Soome for (d = browseDomains; d; d = d->next)
2769c65ebfc7SToomas Soome {
2770c65ebfc7SToomas Soome if (add)
2771c65ebfc7SToomas Soome {
2772c65ebfc7SToomas Soome RegisterLocalOnlyDomainEnumPTR(m, &d->name, mDNS_DomainTypeBrowse);
2773c65ebfc7SToomas Soome AddAutoBrowseDomain(d->uid, &d->name);
2774c65ebfc7SToomas Soome }
2775c65ebfc7SToomas Soome else
2776c65ebfc7SToomas Soome {
2777c65ebfc7SToomas Soome DeregisterLocalOnlyDomainEnumPTR(m, &d->name, mDNS_DomainTypeBrowse);
2778c65ebfc7SToomas Soome RmvAutoBrowseDomain(d->uid, &d->name);
2779c65ebfc7SToomas Soome }
2780c65ebfc7SToomas Soome }
2781c65ebfc7SToomas Soome }
2782c65ebfc7SToomas Soome
2783c65ebfc7SToomas Soome #if APPLE_OSX_mDNSResponder
2784c65ebfc7SToomas Soome
UpdateDeviceInfoRecord(mDNS * const m)2785c65ebfc7SToomas Soome mDNSlocal void UpdateDeviceInfoRecord(mDNS *const m)
2786c65ebfc7SToomas Soome {
2787c65ebfc7SToomas Soome int num_autoname = 0;
2788c65ebfc7SToomas Soome request_state *req;
2789c65ebfc7SToomas Soome
2790c65ebfc7SToomas Soome // Don't need to register the device info record for kDNSServiceInterfaceIndexLocalOnly registrations.
2791c65ebfc7SToomas Soome for (req = all_requests; req; req = req->next)
2792c65ebfc7SToomas Soome {
2793c65ebfc7SToomas Soome if (req->terminate == regservice_termination_callback && req->u.servicereg.autoname && req->interfaceIndex != kDNSServiceInterfaceIndexLocalOnly)
2794c65ebfc7SToomas Soome num_autoname++;
2795c65ebfc7SToomas Soome }
2796c65ebfc7SToomas Soome
2797c65ebfc7SToomas Soome // If DeviceInfo record is currently registered, see if we need to deregister it
2798c65ebfc7SToomas Soome if (m->DeviceInfo.resrec.RecordType != kDNSRecordTypeUnregistered)
2799c65ebfc7SToomas Soome if (num_autoname == 0 || !SameDomainLabelCS(m->DeviceInfo.resrec.name->c, m->nicelabel.c))
2800c65ebfc7SToomas Soome {
2801c65ebfc7SToomas Soome LogOperation("UpdateDeviceInfoRecord Deregister %##s", m->DeviceInfo.resrec.name);
2802c65ebfc7SToomas Soome mDNS_Deregister(m, &m->DeviceInfo);
2803c65ebfc7SToomas Soome }
2804c65ebfc7SToomas Soome
2805c65ebfc7SToomas Soome // If DeviceInfo record is not currently registered, see if we need to register it
2806c65ebfc7SToomas Soome if (m->DeviceInfo.resrec.RecordType == kDNSRecordTypeUnregistered)
2807c65ebfc7SToomas Soome if (num_autoname > 0)
2808c65ebfc7SToomas Soome {
2809c65ebfc7SToomas Soome mDNS_SetupResourceRecord(&m->DeviceInfo, mDNSNULL, mDNSNULL, kDNSType_TXT, kStandardTTL, kDNSRecordTypeAdvisory, AuthRecordAny, mDNSNULL, mDNSNULL);
2810c65ebfc7SToomas Soome ConstructServiceName(&m->DeviceInfo.namestorage, &m->nicelabel, &DeviceInfoName, &localdomain);
2811c65ebfc7SToomas Soome m->DeviceInfo.resrec.rdlength = initializeDeviceInfoTXT(m, m->DeviceInfo.resrec.rdata->u.data);
2812c65ebfc7SToomas Soome LogOperation("UpdateDeviceInfoRecord Register %##s", m->DeviceInfo.resrec.name);
2813c65ebfc7SToomas Soome mDNS_Register(m, &m->DeviceInfo);
2814c65ebfc7SToomas Soome }
2815c65ebfc7SToomas Soome }
2816c65ebfc7SToomas Soome #else // APPLE_OSX_mDNSResponder
UpdateDeviceInfoRecord(mDNS * const m)2817c65ebfc7SToomas Soome mDNSlocal void UpdateDeviceInfoRecord(mDNS *const m)
2818c65ebfc7SToomas Soome {
2819c65ebfc7SToomas Soome (void)m; // unused
2820c65ebfc7SToomas Soome }
2821c65ebfc7SToomas Soome #endif // APPLE_OSX_mDNSResponder
2822c65ebfc7SToomas Soome
udsserver_handle_configchange(mDNS * const m)2823c65ebfc7SToomas Soome mDNSexport void udsserver_handle_configchange(mDNS *const m)
2824c65ebfc7SToomas Soome {
2825c65ebfc7SToomas Soome request_state *req;
2826c65ebfc7SToomas Soome service_instance *ptr;
2827c65ebfc7SToomas Soome DNameListElem *RegDomains = NULL;
2828c65ebfc7SToomas Soome DNameListElem *BrowseDomains = NULL;
2829c65ebfc7SToomas Soome DNameListElem *p;
2830c65ebfc7SToomas Soome
2831c65ebfc7SToomas Soome UpdateDeviceInfoRecord(m);
2832c65ebfc7SToomas Soome
2833c65ebfc7SToomas Soome // For autoname services, see if the default service name has changed, necessitating an automatic update
2834c65ebfc7SToomas Soome for (req = all_requests; req; req = req->next)
2835c65ebfc7SToomas Soome if (req->terminate == regservice_termination_callback)
2836c65ebfc7SToomas Soome if (req->u.servicereg.autoname && !SameDomainLabelCS(req->u.servicereg.name.c, m->nicelabel.c))
2837c65ebfc7SToomas Soome {
2838c65ebfc7SToomas Soome req->u.servicereg.name = m->nicelabel;
2839c65ebfc7SToomas Soome for (ptr = req->u.servicereg.instances; ptr; ptr = ptr->next)
2840c65ebfc7SToomas Soome {
2841c65ebfc7SToomas Soome ptr->renameonmemfree = 1;
2842c65ebfc7SToomas Soome if (ptr->clientnotified) SendServiceRemovalNotification(&ptr->srs);
2843c65ebfc7SToomas Soome LogInfo("udsserver_handle_configchange: Calling deregister for Service %##s", ptr->srs.RR_PTR.resrec.name->c);
2844c65ebfc7SToomas Soome if (mDNS_DeregisterService_drt(m, &ptr->srs, mDNS_Dereg_rapid))
2845c65ebfc7SToomas Soome regservice_callback(m, &ptr->srs, mStatus_MemFree); // If service deregistered already, we can re-register immediately
2846c65ebfc7SToomas Soome }
2847c65ebfc7SToomas Soome }
2848c65ebfc7SToomas Soome
2849c65ebfc7SToomas Soome // Let the platform layer get the current DNS information
2850c65ebfc7SToomas Soome mDNS_Lock(m);
2851c65ebfc7SToomas Soome mDNSPlatformSetDNSConfig(mDNSfalse, mDNSfalse, mDNSNULL, &RegDomains, &BrowseDomains, mDNSfalse);
2852c65ebfc7SToomas Soome mDNS_Unlock(m);
2853c65ebfc7SToomas Soome
2854c65ebfc7SToomas Soome // Any automatic registration domains are also implicitly automatic browsing domains
2855c65ebfc7SToomas Soome if (RegDomains) SetPrefsBrowseDomains(m, RegDomains, mDNStrue); // Add the new list first
2856c65ebfc7SToomas Soome if (AutoRegistrationDomains) SetPrefsBrowseDomains(m, AutoRegistrationDomains, mDNSfalse); // Then clear the old list
2857c65ebfc7SToomas Soome
2858c65ebfc7SToomas Soome // Add any new domains not already in our AutoRegistrationDomains list
2859c65ebfc7SToomas Soome for (p=RegDomains; p; p=p->next)
2860c65ebfc7SToomas Soome {
2861c65ebfc7SToomas Soome DNameListElem **pp = &AutoRegistrationDomains;
2862c65ebfc7SToomas Soome while (*pp && ((*pp)->uid != p->uid || !SameDomainName(&(*pp)->name, &p->name))) pp = &(*pp)->next;
2863c65ebfc7SToomas Soome if (!*pp) // If not found in our existing list, this is a new default registration domain
2864c65ebfc7SToomas Soome {
2865c65ebfc7SToomas Soome RegisterLocalOnlyDomainEnumPTR(m, &p->name, mDNS_DomainTypeRegistration);
2866c65ebfc7SToomas Soome udsserver_default_reg_domain_changed(p, mDNStrue);
2867c65ebfc7SToomas Soome }
2868c65ebfc7SToomas Soome else // else found same domainname in both old and new lists, so no change, just delete old copy
2869c65ebfc7SToomas Soome {
2870c65ebfc7SToomas Soome DNameListElem *del = *pp;
2871c65ebfc7SToomas Soome *pp = (*pp)->next;
2872c65ebfc7SToomas Soome mDNSPlatformMemFree(del);
2873c65ebfc7SToomas Soome }
2874c65ebfc7SToomas Soome }
2875c65ebfc7SToomas Soome
2876c65ebfc7SToomas Soome // Delete any domains in our old AutoRegistrationDomains list that are now gone
2877c65ebfc7SToomas Soome while (AutoRegistrationDomains)
2878c65ebfc7SToomas Soome {
2879c65ebfc7SToomas Soome DNameListElem *del = AutoRegistrationDomains;
2880c65ebfc7SToomas Soome AutoRegistrationDomains = AutoRegistrationDomains->next; // Cut record from list FIRST,
2881c65ebfc7SToomas Soome DeregisterLocalOnlyDomainEnumPTR(m, &del->name, mDNS_DomainTypeRegistration);
2882c65ebfc7SToomas Soome udsserver_default_reg_domain_changed(del, mDNSfalse); // before calling udsserver_default_reg_domain_changed()
2883c65ebfc7SToomas Soome mDNSPlatformMemFree(del);
2884c65ebfc7SToomas Soome }
2885c65ebfc7SToomas Soome
2886c65ebfc7SToomas Soome // Now we have our new updated automatic registration domain list
2887c65ebfc7SToomas Soome AutoRegistrationDomains = RegDomains;
2888c65ebfc7SToomas Soome
2889c65ebfc7SToomas Soome // Add new browse domains to internal list
2890c65ebfc7SToomas Soome if (BrowseDomains) SetPrefsBrowseDomains(m, BrowseDomains, mDNStrue);
2891c65ebfc7SToomas Soome
2892c65ebfc7SToomas Soome // Remove old browse domains from internal list
2893c65ebfc7SToomas Soome if (SCPrefBrowseDomains)
2894c65ebfc7SToomas Soome {
2895c65ebfc7SToomas Soome SetPrefsBrowseDomains(m, SCPrefBrowseDomains, mDNSfalse);
2896c65ebfc7SToomas Soome while (SCPrefBrowseDomains)
2897c65ebfc7SToomas Soome {
2898c65ebfc7SToomas Soome DNameListElem *fptr = SCPrefBrowseDomains;
2899c65ebfc7SToomas Soome SCPrefBrowseDomains = SCPrefBrowseDomains->next;
2900c65ebfc7SToomas Soome mDNSPlatformMemFree(fptr);
2901c65ebfc7SToomas Soome }
2902c65ebfc7SToomas Soome }
2903c65ebfc7SToomas Soome
2904c65ebfc7SToomas Soome // Replace the old browse domains array with the new array
2905c65ebfc7SToomas Soome SCPrefBrowseDomains = BrowseDomains;
2906c65ebfc7SToomas Soome }
2907c65ebfc7SToomas Soome
AutomaticBrowseDomainChange(mDNS * const m,DNSQuestion * q,const ResourceRecord * const answer,QC_result AddRecord)2908c65ebfc7SToomas Soome mDNSlocal void AutomaticBrowseDomainChange(mDNS *const m, DNSQuestion *q, const ResourceRecord *const answer, QC_result AddRecord)
2909c65ebfc7SToomas Soome {
2910c65ebfc7SToomas Soome (void)m; // unused;
2911c65ebfc7SToomas Soome (void)q; // unused
2912c65ebfc7SToomas Soome
2913c65ebfc7SToomas Soome LogOperation("AutomaticBrowseDomainChange: %s automatic browse domain %##s",
2914c65ebfc7SToomas Soome AddRecord ? "Adding" : "Removing", answer->rdata->u.name.c);
2915c65ebfc7SToomas Soome
2916c65ebfc7SToomas Soome if (AddRecord) AddAutoBrowseDomain(0, &answer->rdata->u.name);
2917c65ebfc7SToomas Soome else RmvAutoBrowseDomain(0, &answer->rdata->u.name);
2918c65ebfc7SToomas Soome }
2919c65ebfc7SToomas Soome
_handle_browse_request_start(request_state * request,const char * domain)2920*472cd20dSToomas Soome mDNSlocal mStatus _handle_browse_request_start(request_state *request, const char * domain)
2921c65ebfc7SToomas Soome {
2922*472cd20dSToomas Soome domainname d;
2923c65ebfc7SToomas Soome mStatus err = mStatus_NoError;
2924c65ebfc7SToomas Soome
2925c65ebfc7SToomas Soome request->terminate = browse_termination_callback;
2926c65ebfc7SToomas Soome
2927c65ebfc7SToomas Soome if (domain[0])
2928c65ebfc7SToomas Soome {
2929c65ebfc7SToomas Soome if (!MakeDomainNameFromDNSNameString(&d, domain)) return(mStatus_BadParamErr);
2930c65ebfc7SToomas Soome err = add_domain_to_browser(request, &d);
2931c65ebfc7SToomas Soome }
2932c65ebfc7SToomas Soome else
2933c65ebfc7SToomas Soome {
2934c65ebfc7SToomas Soome DNameListElem *sdom;
2935c65ebfc7SToomas Soome for (sdom = AutoBrowseDomains; sdom; sdom = sdom->next)
2936c65ebfc7SToomas Soome if (!sdom->uid || SystemUID(request->uid) || request->uid == sdom->uid)
2937c65ebfc7SToomas Soome {
2938c65ebfc7SToomas Soome err = add_domain_to_browser(request, &sdom->name);
2939c65ebfc7SToomas Soome if (err)
2940c65ebfc7SToomas Soome {
2941c65ebfc7SToomas Soome if (SameDomainName(&sdom->name, &localdomain)) break;
2942c65ebfc7SToomas Soome else err = mStatus_NoError; // suppress errors for non-local "default" domains
2943c65ebfc7SToomas Soome }
2944c65ebfc7SToomas Soome }
2945c65ebfc7SToomas Soome }
2946c65ebfc7SToomas Soome
2947c65ebfc7SToomas Soome return(err);
2948c65ebfc7SToomas Soome }
2949c65ebfc7SToomas Soome
2950*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, TRUST_ENFORCEMENT)
2951*472cd20dSToomas Soome
_return_browse_request_error(request_state * request,mStatus error)2952*472cd20dSToomas Soome mDNSlocal void _return_browse_request_error(request_state *request, mStatus error)
2953*472cd20dSToomas Soome {
2954*472cd20dSToomas Soome reply_state *rep;
2955*472cd20dSToomas Soome
2956*472cd20dSToomas Soome GenerateBrowseReply(NULL, 0, request, &rep, browse_reply_op, 0, error);
2957*472cd20dSToomas Soome
2958*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
2959*472cd20dSToomas Soome "[R%d] DNSServiceBrowse _return_browse_request_error: error (%d)", request->request_id, error);
2960*472cd20dSToomas Soome
2961*472cd20dSToomas Soome append_reply(request, rep);
2962*472cd20dSToomas Soome }
2963*472cd20dSToomas Soome
_handle_browse_request_with_trust(request_state * request,const char * domain)2964*472cd20dSToomas Soome mDNSlocal mStatus _handle_browse_request_with_trust(request_state *request, const char * domain)
2965*472cd20dSToomas Soome {
2966*472cd20dSToomas Soome mStatus err;
2967*472cd20dSToomas Soome if (audit_token_to_pid(request->audit_token) == 0)
2968*472cd20dSToomas Soome {
2969*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_WARNING, "[R%u] _handle_browse_request_with_trust: no audit token for pid(%s %d)", request->request_id, request->pid_name, request->process_id);
2970*472cd20dSToomas Soome err = _handle_browse_request_start(request, domain);
2971*472cd20dSToomas Soome }
2972*472cd20dSToomas Soome else
2973*472cd20dSToomas Soome {
2974*472cd20dSToomas Soome char typestr[MAX_ESCAPED_DOMAIN_NAME];
2975*472cd20dSToomas Soome typestr[0] = 0;
2976*472cd20dSToomas Soome (void)ConvertDomainNameToCString(&request->u.browser.regtype, typestr);
2977*472cd20dSToomas Soome mdns_trust_flags_t flags = mdns_trust_flags_none;
2978*472cd20dSToomas Soome mdns_trust_status_t status = mdns_trust_check_bonjour(request->audit_token, typestr, &flags);
2979*472cd20dSToomas Soome switch (status)
2980*472cd20dSToomas Soome {
2981*472cd20dSToomas Soome case mdns_trust_status_denied:
2982*472cd20dSToomas Soome case mdns_trust_status_pending:
2983*472cd20dSToomas Soome {
2984*472cd20dSToomas Soome mdns_trust_t trust = mdns_trust_create(request->audit_token, typestr, flags);
2985*472cd20dSToomas Soome if (!trust )
2986*472cd20dSToomas Soome {
2987*472cd20dSToomas Soome err = mStatus_NoMemoryErr;
2988*472cd20dSToomas Soome goto exit;
2989*472cd20dSToomas Soome }
2990*472cd20dSToomas Soome
2991*472cd20dSToomas Soome size_t len = strlen(domain) + 1;
2992*472cd20dSToomas Soome void * context = mallocL("context/_handle_browse_request_with_trust", len);
2993*472cd20dSToomas Soome if (!context)
2994*472cd20dSToomas Soome {
2995*472cd20dSToomas Soome my_perror("ERROR: mallocL context/_handle_browse_request_with_trust");
2996*472cd20dSToomas Soome mdns_release(trust);
2997*472cd20dSToomas Soome err = mStatus_NoMemoryErr;
2998*472cd20dSToomas Soome goto exit;
2999*472cd20dSToomas Soome }
3000*472cd20dSToomas Soome memcpy(context, domain, len);
3001*472cd20dSToomas Soome mdns_trust_set_context(trust, context);
3002*472cd20dSToomas Soome
3003*472cd20dSToomas Soome mdns_trust_set_queue(trust, _get_trust_results_dispatch_queue());
3004*472cd20dSToomas Soome mdns_trust_set_event_handler(trust, ^(mdns_trust_event_t event, mdns_trust_status_t update)
3005*472cd20dSToomas Soome {
3006*472cd20dSToomas Soome if (event == mdns_trust_event_result)
3007*472cd20dSToomas Soome {
3008*472cd20dSToomas Soome mStatus error = (update != mdns_trust_status_granted) ? mStatus_PolicyDenied : mStatus_NoError;
3009*472cd20dSToomas Soome KQueueLock();
3010*472cd20dSToomas Soome const char * _domain = mdns_trust_get_context(trust);
3011*472cd20dSToomas Soome if (_domain)
3012*472cd20dSToomas Soome {
3013*472cd20dSToomas Soome if (!error)
3014*472cd20dSToomas Soome {
3015*472cd20dSToomas Soome error = _handle_browse_request_start(request, _domain);
3016*472cd20dSToomas Soome // No context means the request was canceled before we got here
3017*472cd20dSToomas Soome }
3018*472cd20dSToomas Soome if (error) // (not else if) Always check for error result
3019*472cd20dSToomas Soome {
3020*472cd20dSToomas Soome _return_browse_request_error(request, error);
3021*472cd20dSToomas Soome }
3022*472cd20dSToomas Soome }
3023*472cd20dSToomas Soome KQueueUnlock("_handle_browse_request_with_trust");
3024*472cd20dSToomas Soome }
3025*472cd20dSToomas Soome });
3026*472cd20dSToomas Soome request->trust = trust;
3027*472cd20dSToomas Soome mdns_trust_activate(trust);
3028*472cd20dSToomas Soome err = mStatus_NoError;
3029*472cd20dSToomas Soome break;
3030*472cd20dSToomas Soome }
3031*472cd20dSToomas Soome
3032*472cd20dSToomas Soome case mdns_trust_status_no_entitlement:
3033*472cd20dSToomas Soome err = mStatus_NoAuth;
3034*472cd20dSToomas Soome break;
3035*472cd20dSToomas Soome
3036*472cd20dSToomas Soome case mdns_trust_status_granted:
3037*472cd20dSToomas Soome err = _handle_browse_request_start(request, domain);
3038*472cd20dSToomas Soome break;
3039*472cd20dSToomas Soome
3040*472cd20dSToomas Soome default:
3041*472cd20dSToomas Soome err = mStatus_UnknownErr;
3042*472cd20dSToomas Soome break;
3043*472cd20dSToomas Soome }
3044*472cd20dSToomas Soome }
3045*472cd20dSToomas Soome exit:
3046*472cd20dSToomas Soome return err;
3047*472cd20dSToomas Soome }
3048*472cd20dSToomas Soome #endif // TRUST_ENFORCEMENT
3049*472cd20dSToomas Soome
handle_browse_request(request_state * request)3050*472cd20dSToomas Soome mDNSlocal mStatus handle_browse_request(request_state *request)
3051*472cd20dSToomas Soome {
3052*472cd20dSToomas Soome // Note that regtype may include a trailing subtype
3053*472cd20dSToomas Soome char regtype[MAX_ESCAPED_DOMAIN_NAME], domain[MAX_ESCAPED_DOMAIN_NAME];
3054*472cd20dSToomas Soome domainname typedn, temp;
3055*472cd20dSToomas Soome mDNSs32 NumSubTypes;
3056*472cd20dSToomas Soome mStatus err = mStatus_NoError;
3057*472cd20dSToomas Soome
3058*472cd20dSToomas Soome DNSServiceFlags flags = get_flags(&request->msgptr, request->msgend);
3059*472cd20dSToomas Soome mDNSu32 interfaceIndex = get_uint32(&request->msgptr, request->msgend);
3060*472cd20dSToomas Soome mDNSInterfaceID InterfaceID = mDNSPlatformInterfaceIDfromInterfaceIndex(&mDNSStorage, interfaceIndex);
3061*472cd20dSToomas Soome
3062*472cd20dSToomas Soome // The browse is scoped to a specific interface index, but the
3063*472cd20dSToomas Soome // interface is not currently in our list.
3064*472cd20dSToomas Soome if (interfaceIndex && !InterfaceID)
3065*472cd20dSToomas Soome {
3066*472cd20dSToomas Soome // If it's one of the specially defined inteface index values, just return an error.
3067*472cd20dSToomas Soome if (PreDefinedInterfaceIndex(interfaceIndex))
3068*472cd20dSToomas Soome {
3069*472cd20dSToomas Soome LogInfo("handle_browse_request: bad interfaceIndex %d", interfaceIndex);
3070*472cd20dSToomas Soome return(mStatus_BadParamErr);
3071*472cd20dSToomas Soome }
3072*472cd20dSToomas Soome
3073*472cd20dSToomas Soome // Otherwise, use the specified interface index value and the browse will
3074*472cd20dSToomas Soome // be applied to that interface when it comes up.
3075*472cd20dSToomas Soome InterfaceID = (mDNSInterfaceID)(uintptr_t)interfaceIndex;
3076*472cd20dSToomas Soome LogInfo("handle_browse_request: browse pending for interface index %d", interfaceIndex);
3077*472cd20dSToomas Soome }
3078*472cd20dSToomas Soome
3079*472cd20dSToomas Soome if (get_string(&request->msgptr, request->msgend, regtype, sizeof(regtype)) < 0 ||
3080*472cd20dSToomas Soome get_string(&request->msgptr, request->msgend, domain, sizeof(domain )) < 0) return(mStatus_BadParamErr);
3081*472cd20dSToomas Soome
3082*472cd20dSToomas Soome if (!request->msgptr) { LogMsg("%3d: DNSServiceBrowse(unreadable parameters)", request->sd); return(mStatus_BadParamErr); }
3083*472cd20dSToomas Soome
3084*472cd20dSToomas Soome request->flags = flags;
3085*472cd20dSToomas Soome request->interfaceIndex = interfaceIndex;
3086*472cd20dSToomas Soome typedn.c[0] = 0;
3087*472cd20dSToomas Soome NumSubTypes = ChopSubTypes(regtype); // Note: Modifies regtype string to remove trailing subtypes
3088*472cd20dSToomas Soome if (NumSubTypes < 0 || NumSubTypes > 1)
3089*472cd20dSToomas Soome return(mStatus_BadParamErr);
3090*472cd20dSToomas Soome if (NumSubTypes == 1)
3091*472cd20dSToomas Soome {
3092*472cd20dSToomas Soome if (!AppendDNSNameString(&typedn, regtype + strlen(regtype) + 1))
3093*472cd20dSToomas Soome return(mStatus_BadParamErr);
3094*472cd20dSToomas Soome }
3095*472cd20dSToomas Soome
3096*472cd20dSToomas Soome if (!regtype[0] || !AppendDNSNameString(&typedn, regtype)) return(mStatus_BadParamErr);
3097*472cd20dSToomas Soome
3098*472cd20dSToomas Soome if (!MakeDomainNameFromDNSNameString(&temp, regtype)) return(mStatus_BadParamErr);
3099*472cd20dSToomas Soome // For over-long service types, we only allow domain "local"
3100*472cd20dSToomas Soome if (temp.c[0] > 15 && domain[0] == 0) mDNSPlatformStrLCopy(domain, "local.", sizeof(domain));
3101*472cd20dSToomas Soome
3102*472cd20dSToomas Soome // Set up browser info
3103*472cd20dSToomas Soome request->u.browser.ForceMCast = (flags & kDNSServiceFlagsForceMulticast) != 0;
3104*472cd20dSToomas Soome request->u.browser.interface_id = InterfaceID;
3105*472cd20dSToomas Soome AssignDomainName(&request->u.browser.regtype, &typedn);
3106*472cd20dSToomas Soome request->u.browser.default_domain = !domain[0];
3107*472cd20dSToomas Soome request->u.browser.browsers = NULL;
3108*472cd20dSToomas Soome
3109*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO, "[R%d] DNSServiceBrowse(%X, %d, \"" PRI_DM_NAME "\", \"" PRI_S "\") START PID[%d](" PUB_S ")",
3110*472cd20dSToomas Soome request->request_id, request->flags, interfaceIndex, DM_NAME_PARAM(&request->u.browser.regtype), domain,
3111*472cd20dSToomas Soome request->process_id, request->pid_name);
3112*472cd20dSToomas Soome
3113*472cd20dSToomas Soome if (request->u.browser.default_domain)
3114*472cd20dSToomas Soome {
3115*472cd20dSToomas Soome // Start the domain enumeration queries to discover the WAB browse domains
3116*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
3117*472cd20dSToomas Soome "[R%d] DNSServiceBrowse Start WAB PID[%d](" PUB_S ")",
3118*472cd20dSToomas Soome request->request_id, request->process_id, request->pid_name);
3119*472cd20dSToomas Soome uDNS_StartWABQueries(&mDNSStorage, UDNS_WAB_LBROWSE_QUERY);
3120*472cd20dSToomas Soome }
3121*472cd20dSToomas Soome // We need to unconditionally set request->terminate, because even if we didn't successfully
3122*472cd20dSToomas Soome // start any browses right now, subsequent configuration changes may cause successful
3123*472cd20dSToomas Soome // browses to be added, and we'll need to cancel them before freeing this memory.
3124*472cd20dSToomas Soome request->terminate = NULL;
3125*472cd20dSToomas Soome
3126*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, TRUST_ENFORCEMENT)
3127*472cd20dSToomas Soome domainname d;
3128*472cd20dSToomas Soome if (!MakeDomainNameFromDNSNameString(&d, domain)) return(mStatus_BadParamErr);
3129*472cd20dSToomas Soome
3130*472cd20dSToomas Soome if (os_feature_enabled(mDNSResponder, bonjour_privacy) &&
3131*472cd20dSToomas Soome (request->u.browser.default_domain || IsLocalDomain(&d) || request->u.browser.ForceMCast))
3132*472cd20dSToomas Soome {
3133*472cd20dSToomas Soome err = _handle_browse_request_with_trust(request, domain);
3134*472cd20dSToomas Soome }
3135*472cd20dSToomas Soome else
3136*472cd20dSToomas Soome {
3137*472cd20dSToomas Soome err = _handle_browse_request_start(request, domain);
3138*472cd20dSToomas Soome }
3139*472cd20dSToomas Soome #else
3140*472cd20dSToomas Soome err = _handle_browse_request_start(request, domain);
3141*472cd20dSToomas Soome #endif
3142*472cd20dSToomas Soome
3143*472cd20dSToomas Soome return(err);
3144*472cd20dSToomas Soome }
3145*472cd20dSToomas Soome
3146c65ebfc7SToomas Soome // ***************************************************************************
3147c65ebfc7SToomas Soome #if COMPILER_LIKES_PRAGMA_MARK
3148c65ebfc7SToomas Soome #pragma mark -
3149c65ebfc7SToomas Soome #pragma mark - DNSServiceResolve
3150c65ebfc7SToomas Soome #endif
3151c65ebfc7SToomas Soome
resolve_termination_callback(request_state * request)3152*472cd20dSToomas Soome mDNSlocal void resolve_termination_callback(request_state *request)
3153*472cd20dSToomas Soome {
3154*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
3155*472cd20dSToomas Soome "[R%d] DNSServiceResolve(%X, %d, \"" PRI_DM_NAME "\") STOP PID[%d](" PUB_S ")",
3156*472cd20dSToomas Soome request->request_id, request->flags, request->interfaceIndex, DM_NAME_PARAM(&request->u.resolve.qtxt.qname),
3157*472cd20dSToomas Soome request->process_id, request->pid_name);
3158*472cd20dSToomas Soome mDNS_StopQuery(&mDNSStorage, &request->u.resolve.qtxt);
3159*472cd20dSToomas Soome mDNS_StopQuery(&mDNSStorage, &request->u.resolve.qsrv);
3160*472cd20dSToomas Soome LogMcastQ(&request->u.resolve.qsrv, request, q_stop);
3161*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, D2D)
3162*472cd20dSToomas Soome if (request->u.resolve.external_advertise)
3163*472cd20dSToomas Soome {
3164*472cd20dSToomas Soome external_stop_resolving_service(request->u.resolve.qsrv.InterfaceID, &request->u.resolve.qsrv.qname, request->flags, request->process_id);
3165*472cd20dSToomas Soome }
3166*472cd20dSToomas Soome #endif
3167*472cd20dSToomas Soome }
3168*472cd20dSToomas Soome
3169*472cd20dSToomas Soome typedef struct {
3170*472cd20dSToomas Soome char regtype[MAX_ESCAPED_DOMAIN_NAME];
3171*472cd20dSToomas Soome domainname fqdn;
3172*472cd20dSToomas Soome mDNSInterfaceID InterfaceID;
3173*472cd20dSToomas Soome } _resolve_start_params_t;
3174*472cd20dSToomas Soome
_handle_resolve_request_start(request_state * request,const _resolve_start_params_t * const params)3175*472cd20dSToomas Soome mDNSlocal mStatus _handle_resolve_request_start(request_state *request, const _resolve_start_params_t * const params)
3176*472cd20dSToomas Soome {
3177*472cd20dSToomas Soome mStatus err;
3178*472cd20dSToomas Soome
3179*472cd20dSToomas Soome err = mDNS_StartQuery(&mDNSStorage, &request->u.resolve.qsrv);
3180*472cd20dSToomas Soome
3181*472cd20dSToomas Soome if (!err)
3182*472cd20dSToomas Soome {
3183*472cd20dSToomas Soome err = mDNS_StartQuery(&mDNSStorage, &request->u.resolve.qtxt);
3184*472cd20dSToomas Soome if (err)
3185*472cd20dSToomas Soome {
3186*472cd20dSToomas Soome mDNS_StopQuery(&mDNSStorage, &request->u.resolve.qsrv);
3187*472cd20dSToomas Soome }
3188*472cd20dSToomas Soome else
3189*472cd20dSToomas Soome {
3190*472cd20dSToomas Soome request->terminate = resolve_termination_callback;
3191*472cd20dSToomas Soome LogMcastQ(&request->u.resolve.qsrv, request, q_start);
3192*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, D2D)
3193*472cd20dSToomas Soome if (callExternalHelpers(params->InterfaceID, ¶ms->fqdn, request->flags))
3194*472cd20dSToomas Soome {
3195*472cd20dSToomas Soome request->u.resolve.external_advertise = mDNStrue;
3196*472cd20dSToomas Soome LogInfo("handle_resolve_request: calling external_start_resolving_service()");
3197*472cd20dSToomas Soome external_start_resolving_service(params->InterfaceID, ¶ms->fqdn, request->flags, request->process_id);
3198*472cd20dSToomas Soome }
3199*472cd20dSToomas Soome #else
3200*472cd20dSToomas Soome (void)params;
3201*472cd20dSToomas Soome #endif
3202*472cd20dSToomas Soome }
3203*472cd20dSToomas Soome }
3204*472cd20dSToomas Soome return err;
3205*472cd20dSToomas Soome }
3206*472cd20dSToomas Soome
resolve_result_callback(mDNS * const m,DNSQuestion * question,const ResourceRecord * const answer,QC_result AddRecord)3207c65ebfc7SToomas Soome mDNSlocal void resolve_result_callback(mDNS *const m, DNSQuestion *question, const ResourceRecord *const answer, QC_result AddRecord)
3208c65ebfc7SToomas Soome {
3209c65ebfc7SToomas Soome size_t len = 0;
3210c65ebfc7SToomas Soome char fullname[MAX_ESCAPED_DOMAIN_NAME], target[MAX_ESCAPED_DOMAIN_NAME] = "0";
3211c65ebfc7SToomas Soome char *data;
3212c65ebfc7SToomas Soome reply_state *rep;
3213c65ebfc7SToomas Soome request_state *req = question->QuestionContext;
3214c65ebfc7SToomas Soome const DNSServiceErrorType error =
3215c65ebfc7SToomas Soome (answer->RecordType == kDNSRecordTypePacketNegative) ? kDNSServiceErr_NoSuchRecord : kDNSServiceErr_NoError;
3216c65ebfc7SToomas Soome (void)m; // Unused
3217c65ebfc7SToomas Soome
3218c65ebfc7SToomas Soome LogOperation("%3d: DNSServiceResolve(%##s) %s interface %d: %s",
3219c65ebfc7SToomas Soome req->sd, question->qname.c, AddRecord ? "ADD" : "RMV",
3220c65ebfc7SToomas Soome mDNSPlatformInterfaceIndexfromInterfaceID(m, answer->InterfaceID, mDNSfalse), RRDisplayString(m, answer));
3221c65ebfc7SToomas Soome
3222c65ebfc7SToomas Soome if (!AddRecord)
3223c65ebfc7SToomas Soome {
3224c65ebfc7SToomas Soome if (req->u.resolve.srv == answer) req->u.resolve.srv = mDNSNULL;
3225c65ebfc7SToomas Soome if (req->u.resolve.txt == answer) req->u.resolve.txt = mDNSNULL;
3226c65ebfc7SToomas Soome return;
3227c65ebfc7SToomas Soome }
3228c65ebfc7SToomas Soome
3229c65ebfc7SToomas Soome if (answer->rrtype == kDNSType_SRV) req->u.resolve.srv = answer;
3230c65ebfc7SToomas Soome if (answer->rrtype == kDNSType_TXT) req->u.resolve.txt = answer;
3231c65ebfc7SToomas Soome
3232c65ebfc7SToomas Soome if (!req->u.resolve.txt || !req->u.resolve.srv) return; // only deliver result to client if we have both answers
3233c65ebfc7SToomas Soome
3234c65ebfc7SToomas Soome ConvertDomainNameToCString(answer->name, fullname);
3235c65ebfc7SToomas Soome
3236c65ebfc7SToomas Soome if (answer->RecordType != kDNSRecordTypePacketNegative)
3237c65ebfc7SToomas Soome ConvertDomainNameToCString(&req->u.resolve.srv->rdata->u.srv.target, target);
3238c65ebfc7SToomas Soome
3239c65ebfc7SToomas Soome // calculate reply length
3240c65ebfc7SToomas Soome len += sizeof(DNSServiceFlags);
3241c65ebfc7SToomas Soome len += sizeof(mDNSu32); // interface index
3242c65ebfc7SToomas Soome len += sizeof(DNSServiceErrorType);
3243c65ebfc7SToomas Soome len += strlen(fullname) + 1;
3244c65ebfc7SToomas Soome len += strlen(target) + 1;
3245c65ebfc7SToomas Soome len += 2 * sizeof(mDNSu16); // port, txtLen
3246c65ebfc7SToomas Soome len += req->u.resolve.txt->rdlength;
3247c65ebfc7SToomas Soome
3248c65ebfc7SToomas Soome // allocate/init reply header
3249c65ebfc7SToomas Soome rep = create_reply(resolve_reply_op, len, req);
3250c65ebfc7SToomas Soome rep->rhdr->flags = dnssd_htonl(0);
3251c65ebfc7SToomas Soome rep->rhdr->ifi = dnssd_htonl(mDNSPlatformInterfaceIndexfromInterfaceID(m, answer->InterfaceID, mDNSfalse));
3252c65ebfc7SToomas Soome rep->rhdr->error = dnssd_htonl(error);
3253c65ebfc7SToomas Soome
3254c65ebfc7SToomas Soome data = (char *)&rep->rhdr[1];
3255c65ebfc7SToomas Soome
3256c65ebfc7SToomas Soome // write reply data to message
3257c65ebfc7SToomas Soome put_string(fullname, &data);
3258c65ebfc7SToomas Soome put_string(target, &data);
3259c65ebfc7SToomas Soome *data++ = req->u.resolve.srv->rdata->u.srv.port.b[0];
3260c65ebfc7SToomas Soome *data++ = req->u.resolve.srv->rdata->u.srv.port.b[1];
3261c65ebfc7SToomas Soome put_uint16(req->u.resolve.txt->rdlength, &data);
3262c65ebfc7SToomas Soome put_rdata (req->u.resolve.txt->rdlength, req->u.resolve.txt->rdata->u.data, &data);
3263c65ebfc7SToomas Soome
3264*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO, "[R%d->Q%d] DNSServiceResolve(" PRI_S ") RESULT " PRI_S ":%d",
3265*472cd20dSToomas Soome req->request_id, mDNSVal16(question->TargetQID), fullname, target,
3266*472cd20dSToomas Soome mDNSVal16(req->u.resolve.srv->rdata->u.srv.port));
3267c65ebfc7SToomas Soome append_reply(req, rep);
3268c65ebfc7SToomas Soome }
3269c65ebfc7SToomas Soome
3270*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, TRUST_ENFORCEMENT)
3271*472cd20dSToomas Soome
_return_resolve_request_error(request_state * request,mStatus error)3272*472cd20dSToomas Soome mDNSlocal void _return_resolve_request_error(request_state * request, mStatus error)
3273c65ebfc7SToomas Soome {
3274*472cd20dSToomas Soome size_t len;
3275*472cd20dSToomas Soome char * emptystr = "\0";
3276*472cd20dSToomas Soome char * data;
3277*472cd20dSToomas Soome reply_state *rep;
3278*472cd20dSToomas Soome
3279*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
3280*472cd20dSToomas Soome "[R%u] DNSServiceResolve _return_resolve_request_error: error(%d)", request->request_id, error);
3281*472cd20dSToomas Soome
3282*472cd20dSToomas Soome // calculate reply length
3283*472cd20dSToomas Soome len = sizeof(DNSServiceFlags);
3284*472cd20dSToomas Soome len += sizeof(mDNSu32); // interface index
3285*472cd20dSToomas Soome len += sizeof(DNSServiceErrorType);
3286*472cd20dSToomas Soome len += 2; // name, target
3287*472cd20dSToomas Soome len += 2 * sizeof(mDNSu16); // port, txtLen
3288*472cd20dSToomas Soome len += 0; //req->u.resolve.txt->rdlength;
3289*472cd20dSToomas Soome
3290*472cd20dSToomas Soome rep = create_reply(resolve_reply_op, len, request);
3291*472cd20dSToomas Soome
3292*472cd20dSToomas Soome rep->rhdr->flags = 0;
3293*472cd20dSToomas Soome rep->rhdr->ifi = 0;
3294*472cd20dSToomas Soome rep->rhdr->error = dnssd_htonl(error);
3295*472cd20dSToomas Soome
3296*472cd20dSToomas Soome data = (char *)&rep->rhdr[1];
3297*472cd20dSToomas Soome
3298*472cd20dSToomas Soome // write reply data to message
3299*472cd20dSToomas Soome put_string(emptystr, &data); // name
3300*472cd20dSToomas Soome put_string(emptystr, &data); // target
3301*472cd20dSToomas Soome put_uint16(0, &data); // port
3302*472cd20dSToomas Soome put_uint16(0, &data); // txtLen
3303*472cd20dSToomas Soome
3304*472cd20dSToomas Soome append_reply(request, rep);
3305c65ebfc7SToomas Soome }
3306c65ebfc7SToomas Soome
_handle_resolve_request_with_trust(request_state * request,const _resolve_start_params_t * const params)3307*472cd20dSToomas Soome mDNSlocal mStatus _handle_resolve_request_with_trust(request_state *request, const _resolve_start_params_t * const params)
3308*472cd20dSToomas Soome {
3309*472cd20dSToomas Soome mStatus err;
3310*472cd20dSToomas Soome if (audit_token_to_pid(request->audit_token) == 0)
3311*472cd20dSToomas Soome {
3312*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_WARNING, "[R%u] _handle_resolve_request_with_trust: no audit token for pid(%s %d)", request->request_id, request->pid_name, request->process_id);
3313*472cd20dSToomas Soome err = _handle_resolve_request_start(request, params);
3314*472cd20dSToomas Soome }
3315*472cd20dSToomas Soome else
3316*472cd20dSToomas Soome {
3317*472cd20dSToomas Soome mdns_trust_flags_t flags = mdns_trust_flags_none;
3318*472cd20dSToomas Soome mdns_trust_status_t status = mdns_trust_check_bonjour(request->audit_token, params->regtype, &flags);
3319*472cd20dSToomas Soome switch (status)
3320*472cd20dSToomas Soome {
3321*472cd20dSToomas Soome case mdns_trust_status_denied:
3322*472cd20dSToomas Soome case mdns_trust_status_pending:
3323*472cd20dSToomas Soome {
3324*472cd20dSToomas Soome mdns_trust_t trust = mdns_trust_create(request->audit_token, params->regtype, flags);
3325*472cd20dSToomas Soome if (!trust )
3326*472cd20dSToomas Soome {
3327*472cd20dSToomas Soome err = mStatus_NoMemoryErr;
3328*472cd20dSToomas Soome goto exit;
3329*472cd20dSToomas Soome }
3330*472cd20dSToomas Soome
3331*472cd20dSToomas Soome void * context = mallocL("context/_handle_resolve_request_with_trust", sizeof(_resolve_start_params_t));
3332*472cd20dSToomas Soome if (!context)
3333*472cd20dSToomas Soome {
3334*472cd20dSToomas Soome my_perror("ERROR: mallocL context/_handle_resolve_request_with_trust");
3335*472cd20dSToomas Soome mdns_release(trust);
3336*472cd20dSToomas Soome err = mStatus_NoMemoryErr;
3337*472cd20dSToomas Soome goto exit;
3338*472cd20dSToomas Soome }
3339*472cd20dSToomas Soome memcpy(context, params, sizeof(_resolve_start_params_t));
3340*472cd20dSToomas Soome mdns_trust_set_context(trust, context);
3341*472cd20dSToomas Soome mdns_trust_set_queue(trust, _get_trust_results_dispatch_queue());
3342*472cd20dSToomas Soome mdns_trust_set_event_handler(trust, ^(mdns_trust_event_t event, mdns_trust_status_t update)
3343*472cd20dSToomas Soome {
3344*472cd20dSToomas Soome if (event == mdns_trust_event_result)
3345*472cd20dSToomas Soome {
3346*472cd20dSToomas Soome mStatus error = (update != mdns_trust_status_granted) ? mStatus_PolicyDenied : mStatus_NoError;
3347*472cd20dSToomas Soome KQueueLock();
3348*472cd20dSToomas Soome _resolve_start_params_t * _params = mdns_trust_get_context(trust);
3349*472cd20dSToomas Soome if (_params)
3350*472cd20dSToomas Soome {
3351*472cd20dSToomas Soome if (!error)
3352*472cd20dSToomas Soome {
3353*472cd20dSToomas Soome error = _handle_resolve_request_start(request, _params);
3354*472cd20dSToomas Soome // No context means the request was canceled before we got here
3355*472cd20dSToomas Soome }
3356*472cd20dSToomas Soome if (error) // (not else if) Always check for error result
3357*472cd20dSToomas Soome {
3358*472cd20dSToomas Soome _return_resolve_request_error(request, error);
3359*472cd20dSToomas Soome }
3360*472cd20dSToomas Soome }
3361*472cd20dSToomas Soome KQueueUnlock("_handle_resolve_request_with_trust");
3362*472cd20dSToomas Soome }
3363*472cd20dSToomas Soome });
3364*472cd20dSToomas Soome request->trust = trust;
3365*472cd20dSToomas Soome mdns_trust_activate(trust);
3366*472cd20dSToomas Soome err = mStatus_NoError;
3367*472cd20dSToomas Soome break;
3368*472cd20dSToomas Soome }
3369*472cd20dSToomas Soome
3370*472cd20dSToomas Soome case mdns_trust_status_no_entitlement:
3371*472cd20dSToomas Soome err = mStatus_NoAuth;
3372*472cd20dSToomas Soome break;
3373*472cd20dSToomas Soome
3374*472cd20dSToomas Soome case mdns_trust_status_granted:
3375*472cd20dSToomas Soome err = _handle_resolve_request_start(request, params);
3376*472cd20dSToomas Soome break;
3377*472cd20dSToomas Soome
3378*472cd20dSToomas Soome default:
3379*472cd20dSToomas Soome err = mStatus_UnknownErr;
3380*472cd20dSToomas Soome break;
3381*472cd20dSToomas Soome }
3382*472cd20dSToomas Soome }
3383*472cd20dSToomas Soome exit:
3384*472cd20dSToomas Soome return err;
3385*472cd20dSToomas Soome }
3386*472cd20dSToomas Soome #endif // TRUST_ENFORCEMENT
3387*472cd20dSToomas Soome
handle_resolve_request(request_state * request)3388c65ebfc7SToomas Soome mDNSlocal mStatus handle_resolve_request(request_state *request)
3389c65ebfc7SToomas Soome {
3390*472cd20dSToomas Soome char name[256], domain[MAX_ESCAPED_DOMAIN_NAME];
3391*472cd20dSToomas Soome _resolve_start_params_t params;
3392c65ebfc7SToomas Soome mStatus err;
3393c65ebfc7SToomas Soome
3394c65ebfc7SToomas Soome // extract the data from the message
3395c65ebfc7SToomas Soome DNSServiceFlags flags = get_flags(&request->msgptr, request->msgend);
3396c65ebfc7SToomas Soome mDNSu32 interfaceIndex = get_uint32(&request->msgptr, request->msgend);
3397c65ebfc7SToomas Soome
3398c65ebfc7SToomas Soome // Map kDNSServiceInterfaceIndexP2P to kDNSServiceInterfaceIndexAny with the kDNSServiceFlagsIncludeP2P
3399c65ebfc7SToomas Soome // flag set so that the resolve will run over P2P interfaces that are not yet created.
3400c65ebfc7SToomas Soome if (interfaceIndex == kDNSServiceInterfaceIndexP2P)
3401c65ebfc7SToomas Soome {
3402c65ebfc7SToomas Soome LogOperation("handle_resolve_request: mapping kDNSServiceInterfaceIndexP2P to kDNSServiceInterfaceIndexAny + kDNSServiceFlagsIncludeP2P");
3403c65ebfc7SToomas Soome flags |= kDNSServiceFlagsIncludeP2P;
3404c65ebfc7SToomas Soome interfaceIndex = kDNSServiceInterfaceIndexAny;
3405c65ebfc7SToomas Soome }
3406c65ebfc7SToomas Soome
3407*472cd20dSToomas Soome params.InterfaceID = mDNSPlatformInterfaceIDfromInterfaceIndex(&mDNSStorage, interfaceIndex);
3408c65ebfc7SToomas Soome
3409c65ebfc7SToomas Soome // The operation is scoped to a specific interface index, but the
3410c65ebfc7SToomas Soome // interface is not currently in our list.
3411*472cd20dSToomas Soome if (interfaceIndex && !params.InterfaceID)
3412c65ebfc7SToomas Soome {
3413c65ebfc7SToomas Soome // If it's one of the specially defined inteface index values, just return an error.
3414c65ebfc7SToomas Soome if (PreDefinedInterfaceIndex(interfaceIndex))
3415c65ebfc7SToomas Soome {
3416c65ebfc7SToomas Soome LogInfo("handle_resolve_request: bad interfaceIndex %d", interfaceIndex);
3417c65ebfc7SToomas Soome return(mStatus_BadParamErr);
3418c65ebfc7SToomas Soome }
3419c65ebfc7SToomas Soome
3420c65ebfc7SToomas Soome // Otherwise, use the specified interface index value and the operation will
3421c65ebfc7SToomas Soome // be applied to that interface when it comes up.
3422*472cd20dSToomas Soome params.InterfaceID = (mDNSInterfaceID)(uintptr_t)interfaceIndex;
3423c65ebfc7SToomas Soome LogInfo("handle_resolve_request: resolve pending for interface index %d", interfaceIndex);
3424c65ebfc7SToomas Soome }
3425c65ebfc7SToomas Soome
3426*472cd20dSToomas Soome if (get_string(&request->msgptr, request->msgend, name, sizeof(name )) < 0 ||
3427*472cd20dSToomas Soome get_string(&request->msgptr, request->msgend, params.regtype, sizeof(params.regtype)) < 0 ||
3428*472cd20dSToomas Soome get_string(&request->msgptr, request->msgend, domain, sizeof(domain )) < 0)
3429c65ebfc7SToomas Soome { LogMsg("ERROR: handle_resolve_request - Couldn't read name/regtype/domain"); return(mStatus_BadParamErr); }
3430c65ebfc7SToomas Soome
3431c65ebfc7SToomas Soome if (!request->msgptr) { LogMsg("%3d: DNSServiceResolve(unreadable parameters)", request->sd); return(mStatus_BadParamErr); }
3432c65ebfc7SToomas Soome
3433*472cd20dSToomas Soome if (build_domainname_from_strings(¶ms.fqdn, name, params.regtype, domain) < 0)
3434*472cd20dSToomas Soome { LogMsg("ERROR: handle_resolve_request bad “%s” “%s” “%s”", name, params.regtype, domain); return(mStatus_BadParamErr); }
3435c65ebfc7SToomas Soome
3436c65ebfc7SToomas Soome mDNSPlatformMemZero(&request->u.resolve, sizeof(request->u.resolve));
3437c65ebfc7SToomas Soome
3438c65ebfc7SToomas Soome #if APPLE_OSX_mDNSResponder && ENABLE_BLE_TRIGGERED_BONJOUR
3439c65ebfc7SToomas Soome // Determine if this request should be promoted to use BLE triggered discovery.
3440c65ebfc7SToomas Soome if (shouldUseBLE(InterfaceID, 0, (domainname *)SkipLeadingLabels(&fqdn, 1), &fqdn))
3441c65ebfc7SToomas Soome {
3442c65ebfc7SToomas Soome flags |= (kDNSServiceFlagsAutoTrigger | kDNSServiceFlagsIncludeAWDL);
3443c65ebfc7SToomas Soome LogInfo("handle_resolve_request: request promoted to use kDNSServiceFlagsAutoTrigger");
3444c65ebfc7SToomas Soome }
3445c65ebfc7SToomas Soome #endif // APPLE_OSX_mDNSResponder && ENABLE_BLE_TRIGGERED_BONJOUR
3446c65ebfc7SToomas Soome
3447c65ebfc7SToomas Soome request->flags = flags;
3448c65ebfc7SToomas Soome request->interfaceIndex = interfaceIndex;
3449c65ebfc7SToomas Soome
3450c65ebfc7SToomas Soome // format questions
3451*472cd20dSToomas Soome request->u.resolve.qsrv.InterfaceID = params.InterfaceID;
3452c65ebfc7SToomas Soome request->u.resolve.qsrv.flags = flags;
3453*472cd20dSToomas Soome AssignDomainName(&request->u.resolve.qsrv.qname, ¶ms.fqdn);
3454c65ebfc7SToomas Soome request->u.resolve.qsrv.qtype = kDNSType_SRV;
3455c65ebfc7SToomas Soome request->u.resolve.qsrv.qclass = kDNSClass_IN;
3456c65ebfc7SToomas Soome request->u.resolve.qsrv.LongLived = (flags & kDNSServiceFlagsLongLivedQuery ) != 0;
3457c65ebfc7SToomas Soome request->u.resolve.qsrv.ExpectUnique = mDNStrue;
3458c65ebfc7SToomas Soome request->u.resolve.qsrv.ForceMCast = (flags & kDNSServiceFlagsForceMulticast ) != 0;
3459c65ebfc7SToomas Soome request->u.resolve.qsrv.ReturnIntermed = (flags & kDNSServiceFlagsReturnIntermediates) != 0;
3460c65ebfc7SToomas Soome request->u.resolve.qsrv.SuppressUnusable = mDNSfalse;
3461c65ebfc7SToomas Soome request->u.resolve.qsrv.AppendSearchDomains = 0;
3462c65ebfc7SToomas Soome request->u.resolve.qsrv.TimeoutQuestion = 0;
3463c65ebfc7SToomas Soome request->u.resolve.qsrv.WakeOnResolve = (flags & kDNSServiceFlagsWakeOnResolve) != 0;
3464*472cd20dSToomas Soome request->u.resolve.qsrv.UseBackgroundTraffic = (flags & kDNSServiceFlagsBackgroundTrafficClass) != 0;
3465c65ebfc7SToomas Soome request->u.resolve.qsrv.ProxyQuestion = 0;
3466c65ebfc7SToomas Soome request->u.resolve.qsrv.pid = request->process_id;
3467c65ebfc7SToomas Soome request->u.resolve.qsrv.euid = request->uid;
3468c65ebfc7SToomas Soome request->u.resolve.qsrv.QuestionCallback = resolve_result_callback;
3469c65ebfc7SToomas Soome request->u.resolve.qsrv.QuestionContext = request;
3470c65ebfc7SToomas Soome
3471*472cd20dSToomas Soome request->u.resolve.qtxt.InterfaceID = params.InterfaceID;
3472c65ebfc7SToomas Soome request->u.resolve.qtxt.flags = flags;
3473*472cd20dSToomas Soome AssignDomainName(&request->u.resolve.qtxt.qname, ¶ms.fqdn);
3474c65ebfc7SToomas Soome request->u.resolve.qtxt.qtype = kDNSType_TXT;
3475c65ebfc7SToomas Soome request->u.resolve.qtxt.qclass = kDNSClass_IN;
3476c65ebfc7SToomas Soome request->u.resolve.qtxt.LongLived = (flags & kDNSServiceFlagsLongLivedQuery ) != 0;
3477c65ebfc7SToomas Soome request->u.resolve.qtxt.ExpectUnique = mDNStrue;
3478c65ebfc7SToomas Soome request->u.resolve.qtxt.ForceMCast = (flags & kDNSServiceFlagsForceMulticast ) != 0;
3479c65ebfc7SToomas Soome request->u.resolve.qtxt.ReturnIntermed = (flags & kDNSServiceFlagsReturnIntermediates) != 0;
3480c65ebfc7SToomas Soome request->u.resolve.qtxt.SuppressUnusable = mDNSfalse;
3481c65ebfc7SToomas Soome request->u.resolve.qtxt.AppendSearchDomains = 0;
3482c65ebfc7SToomas Soome request->u.resolve.qtxt.TimeoutQuestion = 0;
3483c65ebfc7SToomas Soome request->u.resolve.qtxt.WakeOnResolve = 0;
3484*472cd20dSToomas Soome request->u.resolve.qtxt.UseBackgroundTraffic = (flags & kDNSServiceFlagsBackgroundTrafficClass) != 0;
3485c65ebfc7SToomas Soome request->u.resolve.qtxt.ProxyQuestion = 0;
3486c65ebfc7SToomas Soome request->u.resolve.qtxt.pid = request->process_id;
3487c65ebfc7SToomas Soome request->u.resolve.qtxt.euid = request->uid;
3488c65ebfc7SToomas Soome request->u.resolve.qtxt.QuestionCallback = resolve_result_callback;
3489c65ebfc7SToomas Soome request->u.resolve.qtxt.QuestionContext = request;
3490c65ebfc7SToomas Soome
3491c65ebfc7SToomas Soome request->u.resolve.ReportTime = NonZeroTime(mDNS_TimeNow(&mDNSStorage) + 130 * mDNSPlatformOneSecond);
3492c65ebfc7SToomas Soome
3493c65ebfc7SToomas Soome request->u.resolve.external_advertise = mDNSfalse;
3494c65ebfc7SToomas Soome
3495c65ebfc7SToomas Soome #if 0
3496c65ebfc7SToomas Soome if (!AuthorizedDomain(request, &fqdn, AutoBrowseDomains)) return(mStatus_NoError);
3497c65ebfc7SToomas Soome #endif
3498c65ebfc7SToomas Soome
3499c65ebfc7SToomas Soome // ask the questions
3500*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
3501*472cd20dSToomas Soome "[R%d] DNSServiceResolve(%X, %d, \"" PRI_DM_NAME "\") START PID[%d](" PUB_S ")",
3502*472cd20dSToomas Soome request->request_id, flags, interfaceIndex, DM_NAME_PARAM(&request->u.resolve.qsrv.qname),
3503*472cd20dSToomas Soome request->process_id, request->pid_name);
3504c65ebfc7SToomas Soome
3505*472cd20dSToomas Soome request->terminate = NULL;
3506*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, TRUST_ENFORCEMENT)
3507*472cd20dSToomas Soome domainname d;
3508*472cd20dSToomas Soome if (!MakeDomainNameFromDNSNameString(&d, domain)) return(mStatus_BadParamErr);
3509c65ebfc7SToomas Soome
3510*472cd20dSToomas Soome if (os_feature_enabled(mDNSResponder, bonjour_privacy) &&
3511*472cd20dSToomas Soome (IsLocalDomain(&d) || request->u.resolve.qsrv.ForceMCast))
3512c65ebfc7SToomas Soome {
3513*472cd20dSToomas Soome err = _handle_resolve_request_with_trust(request, ¶ms);
3514c65ebfc7SToomas Soome }
3515c65ebfc7SToomas Soome else
3516c65ebfc7SToomas Soome {
3517*472cd20dSToomas Soome err = _handle_resolve_request_start(request, ¶ms);
3518c65ebfc7SToomas Soome }
3519*472cd20dSToomas Soome #else
3520*472cd20dSToomas Soome err = _handle_resolve_request_start(request, ¶ms);
3521*472cd20dSToomas Soome #endif
3522c65ebfc7SToomas Soome
3523c65ebfc7SToomas Soome return(err);
3524c65ebfc7SToomas Soome }
3525c65ebfc7SToomas Soome
3526c65ebfc7SToomas Soome // ***************************************************************************
3527c65ebfc7SToomas Soome #if COMPILER_LIKES_PRAGMA_MARK
3528c65ebfc7SToomas Soome #pragma mark -
3529c65ebfc7SToomas Soome #pragma mark - DNSServiceQueryRecord
3530c65ebfc7SToomas Soome #endif
3531c65ebfc7SToomas Soome
queryrecord_result_reply(mDNS * const m,DNSQuestion * question,const ResourceRecord * const answer,QC_result AddRecord,DNSServiceErrorType error,void * context)3532*472cd20dSToomas Soome mDNSlocal void queryrecord_result_reply(mDNS *const m, DNSQuestion *question, const ResourceRecord *const answer, QC_result AddRecord, DNSServiceErrorType error, void *context)
3533c65ebfc7SToomas Soome {
3534c65ebfc7SToomas Soome char name[MAX_ESCAPED_DOMAIN_NAME];
3535c65ebfc7SToomas Soome size_t len;
3536c65ebfc7SToomas Soome DNSServiceFlags flags = 0;
3537c65ebfc7SToomas Soome reply_state *rep;
3538c65ebfc7SToomas Soome char *data;
3539*472cd20dSToomas Soome request_state *req = (request_state *)context;
3540*472cd20dSToomas Soome const char *dnssec_result_description = "";
3541c65ebfc7SToomas Soome
3542c65ebfc7SToomas Soome ConvertDomainNameToCString(answer->name, name);
3543c65ebfc7SToomas Soome
3544*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, DNSSECv2)
3545*472cd20dSToomas Soome if (question->DNSSECStatus.enable_dnssec) {
3546*472cd20dSToomas Soome if (answer->dnssec_result == dnssec_secure)
3547*472cd20dSToomas Soome {
3548*472cd20dSToomas Soome flags |= kDNSServiceFlagsSecure;
3549*472cd20dSToomas Soome dnssec_result_description = ", DNSSEC_Secure";
3550*472cd20dSToomas Soome }
3551*472cd20dSToomas Soome else if (answer->dnssec_result == dnssec_insecure)
3552*472cd20dSToomas Soome {
3553*472cd20dSToomas Soome flags |= kDNSServiceFlagsInsecure;
3554*472cd20dSToomas Soome dnssec_result_description = ", DNSSEC_Insecure";
3555*472cd20dSToomas Soome }
3556*472cd20dSToomas Soome else if (answer->dnssec_result == dnssec_bogus)
3557*472cd20dSToomas Soome {
3558*472cd20dSToomas Soome flags |= kDNSServiceFlagsBogus;
3559*472cd20dSToomas Soome dnssec_result_description = ", DNSSEC_Bogus";
3560*472cd20dSToomas Soome }
3561*472cd20dSToomas Soome else if (answer->dnssec_result == dnssec_indeterminate)
3562*472cd20dSToomas Soome {
3563*472cd20dSToomas Soome flags |= kDNSServiceFlagsIndeterminate;
3564*472cd20dSToomas Soome dnssec_result_description = ", DNSSEC_Indeterminate";
3565*472cd20dSToomas Soome }
3566*472cd20dSToomas Soome } else if (question->DNSSECStatus.tried_dnssec_but_unsigned) {
3567*472cd20dSToomas Soome // handle the case where we restart the question without the DNSSEC while the user requires DNSSEC result, for
3568*472cd20dSToomas Soome // some reason we failed to get DNSSEC records. In which case, even if we go back to normal query, we should pass
3569*472cd20dSToomas Soome // the DNSSEC result
3570*472cd20dSToomas Soome flags |= kDNSServiceFlagsInsecure;
3571*472cd20dSToomas Soome dnssec_result_description = ", DNSSEC_Insecure";
3572*472cd20dSToomas Soome }
3573*472cd20dSToomas Soome #endif // MDNSRESPONDER_SUPPORTS(APPLE, DNSSECv2)
3574*472cd20dSToomas Soome
3575*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
3576*472cd20dSToomas Soome "[R%u->Q%u] DNSService" PUB_S "(" PRI_DM_NAME ", " PUB_S ") RESULT " PUB_S " interface %d: (" PUB_S PUB_S ")" PRI_S,
3577*472cd20dSToomas Soome req->request_id, mDNSVal16(question->TargetQID), req->hdr.op == query_request ? "QueryRecord" : "GetAddrInfo",
3578*472cd20dSToomas Soome DM_NAME_PARAM(&question->qname), DNSTypeName(question->qtype), AddRecord ? "ADD" : "RMV",
35793b436d06SToomas Soome mDNSPlatformInterfaceIndexfromInterfaceID(m, answer->InterfaceID, mDNSfalse),
3580*472cd20dSToomas Soome MortalityDisplayString(answer->mortality), dnssec_result_description, RRDisplayString(m, answer));
3581c65ebfc7SToomas Soome
3582c65ebfc7SToomas Soome len = sizeof(DNSServiceFlags); // calculate reply data length
3583c65ebfc7SToomas Soome len += sizeof(mDNSu32); // interface index
3584c65ebfc7SToomas Soome len += sizeof(DNSServiceErrorType);
3585c65ebfc7SToomas Soome len += strlen(name) + 1;
3586c65ebfc7SToomas Soome len += 3 * sizeof(mDNSu16); // type, class, rdlen
3587c65ebfc7SToomas Soome len += answer->rdlength;
3588c65ebfc7SToomas Soome len += sizeof(mDNSu32); // TTL
3589c65ebfc7SToomas Soome
3590c65ebfc7SToomas Soome rep = create_reply(req->hdr.op == query_request ? query_reply_op : addrinfo_reply_op, len, req);
3591c65ebfc7SToomas Soome
3592c65ebfc7SToomas Soome if (AddRecord)
3593c65ebfc7SToomas Soome flags |= kDNSServiceFlagsAdd;
35943b436d06SToomas Soome if (answer->mortality == Mortality_Ghost)
35953b436d06SToomas Soome flags |= kDNSServiceFlagsExpiredAnswer;
3596*472cd20dSToomas Soome if (!question->InitialCacheMiss)
3597*472cd20dSToomas Soome flags |= kDNSServiceFlagAnsweredFromCache;
3598c65ebfc7SToomas Soome
3599c65ebfc7SToomas Soome rep->rhdr->flags = dnssd_htonl(flags);
3600c65ebfc7SToomas Soome // Call mDNSPlatformInterfaceIndexfromInterfaceID, but suppressNetworkChange (last argument). Otherwise, if the
3601c65ebfc7SToomas Soome // InterfaceID is not valid, then it simulates a "NetworkChanged" which in turn makes questions
3602c65ebfc7SToomas Soome // to be stopped and started including *this* one. Normally the InterfaceID is valid. But when we
3603c65ebfc7SToomas Soome // are using the /etc/hosts entries to answer a question, the InterfaceID may not be known to the
3604c65ebfc7SToomas Soome // mDNS core . Eventually, we should remove the calls to "NetworkChanged" in
3605c65ebfc7SToomas Soome // mDNSPlatformInterfaceIndexfromInterfaceID when it can't find InterfaceID as ResourceRecords
3606c65ebfc7SToomas Soome // should not have existed to answer this question if the corresponding interface is not valid.
3607c65ebfc7SToomas Soome rep->rhdr->ifi = dnssd_htonl(mDNSPlatformInterfaceIndexfromInterfaceID(m, answer->InterfaceID, mDNStrue));
3608c65ebfc7SToomas Soome rep->rhdr->error = dnssd_htonl(error);
3609c65ebfc7SToomas Soome
3610c65ebfc7SToomas Soome data = (char *)&rep->rhdr[1];
3611c65ebfc7SToomas Soome
3612c65ebfc7SToomas Soome put_string(name, &data);
3613c65ebfc7SToomas Soome put_uint16(answer->rrtype, &data);
3614c65ebfc7SToomas Soome put_uint16(answer->rrclass, &data);
3615c65ebfc7SToomas Soome put_uint16(answer->rdlength, &data);
3616c65ebfc7SToomas Soome // We need to use putRData here instead of the crude put_rdata function, because the crude put_rdata
3617c65ebfc7SToomas Soome // function just does a blind memory copy without regard to structures that may have holes in them.
3618c65ebfc7SToomas Soome if (answer->rdlength)
3619c65ebfc7SToomas Soome if (!putRData(mDNSNULL, (mDNSu8 *)data, (mDNSu8 *)rep->rhdr + len, answer))
3620c65ebfc7SToomas Soome LogMsg("queryrecord_result_reply putRData failed %d", (mDNSu8 *)rep->rhdr + len - (mDNSu8 *)data);
3621c65ebfc7SToomas Soome data += answer->rdlength;
3622c65ebfc7SToomas Soome put_uint32(AddRecord ? answer->rroriginalttl : 0, &data);
3623c65ebfc7SToomas Soome
3624c65ebfc7SToomas Soome append_reply(req, rep);
3625c65ebfc7SToomas Soome }
3626c65ebfc7SToomas Soome
queryrecord_termination_callback(request_state * request)3627c65ebfc7SToomas Soome mDNSlocal void queryrecord_termination_callback(request_state *request)
3628c65ebfc7SToomas Soome {
3629*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
3630*472cd20dSToomas Soome "[R%u] DNSServiceQueryRecord(%X, %d, " PRI_DM_NAME ", " PUB_S ") STOP PID[%d](" PUB_S ")",
3631*472cd20dSToomas Soome request->request_id, request->flags, request->interfaceIndex,
3632*472cd20dSToomas Soome DM_NAME_PARAM(QueryRecordClientRequestGetQName(&request->u.queryrecord)),
3633*472cd20dSToomas Soome DNSTypeName(QueryRecordClientRequestGetType(&request->u.queryrecord)), request->process_id, request->pid_name);
3634*472cd20dSToomas Soome
3635*472cd20dSToomas Soome QueryRecordClientRequestStop(&request->u.queryrecord);
3636*472cd20dSToomas Soome }
3637*472cd20dSToomas Soome
3638*472cd20dSToomas Soome typedef struct {
3639*472cd20dSToomas Soome char qname[MAX_ESCAPED_DOMAIN_NAME];
3640*472cd20dSToomas Soome mDNSu32 interfaceIndex;
3641*472cd20dSToomas Soome DNSServiceFlags flags;
3642*472cd20dSToomas Soome mDNSu16 qtype;
3643*472cd20dSToomas Soome mDNSu16 qclass;
3644*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, QUERIER)
3645*472cd20dSToomas Soome mDNSBool require_privacy;
3646*472cd20dSToomas Soome #endif
3647*472cd20dSToomas Soome } _queryrecord_start_params_t;
3648*472cd20dSToomas Soome
3649*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, QUERIER) && MDNSRESPONDER_SUPPORTS(APPLE, IPC_TLV)
ipc_tlv_get_resolver_config_plist_data(const mDNSu8 * const start,const mDNSu8 * const end,size_t * outLen)3650*472cd20dSToomas Soome mDNSlocal const mDNSu8 * ipc_tlv_get_resolver_config_plist_data(const mDNSu8 *const start, const mDNSu8 *const end,
3651*472cd20dSToomas Soome size_t *outLen)
3652c65ebfc7SToomas Soome {
3653*472cd20dSToomas Soome size_t len = 0;
3654*472cd20dSToomas Soome const mDNSu8 *value = NULL;
3655*472cd20dSToomas Soome mdns_tlv16_get_value(start, end, IPC_TLV_TYPE_RESOLVER_CONFIG_PLIST_DATA, &len, &value, NULL);
3656*472cd20dSToomas Soome if (outLen)
3657*472cd20dSToomas Soome {
3658*472cd20dSToomas Soome *outLen = len;
3659*472cd20dSToomas Soome }
3660*472cd20dSToomas Soome return value;
3661*472cd20dSToomas Soome }
3662*472cd20dSToomas Soome
ipc_tlv_get_require_privacy(const mDNSu8 * const start,const mDNSu8 * const end)3663*472cd20dSToomas Soome mDNSlocal mDNSBool ipc_tlv_get_require_privacy(const mDNSu8 *const start, const mDNSu8 *const end)
3664*472cd20dSToomas Soome {
3665*472cd20dSToomas Soome size_t len = 0;
3666*472cd20dSToomas Soome const mDNSu8 *value = NULL;
3667*472cd20dSToomas Soome mdns_tlv16_get_value(start, end, IPC_TLV_TYPE_REQUIRE_PRIVACY, &len, &value, NULL);
3668*472cd20dSToomas Soome return ((len == 1) && (*value != 0)) ? mDNStrue : mDNSfalse;
3669*472cd20dSToomas Soome }
3670*472cd20dSToomas Soome #endif
3671*472cd20dSToomas Soome
_handle_queryrecord_request_start(request_state * request,const _queryrecord_start_params_t * const params)3672*472cd20dSToomas Soome mDNSlocal mStatus _handle_queryrecord_request_start(request_state *request, const _queryrecord_start_params_t * const params)
3673*472cd20dSToomas Soome {
3674*472cd20dSToomas Soome mStatus err;
3675*472cd20dSToomas Soome
3676*472cd20dSToomas Soome request->terminate = queryrecord_termination_callback;
3677*472cd20dSToomas Soome
3678*472cd20dSToomas Soome QueryRecordClientRequestParams queryParams;
3679*472cd20dSToomas Soome QueryRecordClientRequestParamsInit(&queryParams);
3680*472cd20dSToomas Soome queryParams.requestID = request->request_id;
3681*472cd20dSToomas Soome queryParams.qnameStr = params->qname;
3682*472cd20dSToomas Soome queryParams.interfaceIndex = params->interfaceIndex;
3683*472cd20dSToomas Soome queryParams.flags = params->flags;
3684*472cd20dSToomas Soome queryParams.qtype = params->qtype;
3685*472cd20dSToomas Soome queryParams.qclass = params->qclass;
3686*472cd20dSToomas Soome queryParams.effectivePID = request->validUUID ? 0 : request->process_id;
3687*472cd20dSToomas Soome queryParams.effectiveUUID = request->validUUID ? request->uuid : mDNSNULL;
3688*472cd20dSToomas Soome queryParams.peerUID = request->uid;
3689*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, QUERIER)
3690*472cd20dSToomas Soome queryParams.needEncryption = params->require_privacy ? mDNStrue : mDNSfalse;
3691*472cd20dSToomas Soome queryParams.customID = request->custom_service_id;
3692*472cd20dSToomas Soome #endif
3693*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, AUDIT_TOKEN)
3694*472cd20dSToomas Soome queryParams.peerAuditToken = &request->audit_token;
3695*472cd20dSToomas Soome #endif
3696*472cd20dSToomas Soome err = QueryRecordClientRequestStart(&request->u.queryrecord, &queryParams, queryrecord_result_reply, request);
3697*472cd20dSToomas Soome return err;
3698*472cd20dSToomas Soome }
3699*472cd20dSToomas Soome
3700*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, TRUST_ENFORCEMENT)
3701*472cd20dSToomas Soome
_return_queryrecord_request_error(request_state * request,mStatus error)3702*472cd20dSToomas Soome mDNSlocal void _return_queryrecord_request_error(request_state * request, mStatus error)
3703*472cd20dSToomas Soome {
3704*472cd20dSToomas Soome size_t len;
3705*472cd20dSToomas Soome char * emptystr = "\0";
3706*472cd20dSToomas Soome char * data;
3707*472cd20dSToomas Soome reply_state *rep;
3708*472cd20dSToomas Soome
3709*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
3710*472cd20dSToomas Soome "[R%u] DNSService" PUB_S " _return_queryrecord_request_error: error(%d)",
3711*472cd20dSToomas Soome request->request_id, request->hdr.op == query_request ? "QueryRecord" : "GetAddrInfo", error);
3712*472cd20dSToomas Soome
3713*472cd20dSToomas Soome len = sizeof(DNSServiceFlags); // calculate reply data length
3714*472cd20dSToomas Soome len += sizeof(mDNSu32); // interface index
3715*472cd20dSToomas Soome len += sizeof(DNSServiceErrorType);
3716*472cd20dSToomas Soome len += strlen(emptystr) + 1;
3717*472cd20dSToomas Soome len += 3 * sizeof(mDNSu16); // type, class, rdlen
3718*472cd20dSToomas Soome len += 0;//answer->rdlength;
3719*472cd20dSToomas Soome len += sizeof(mDNSu32); // TTL
3720*472cd20dSToomas Soome
3721*472cd20dSToomas Soome rep = create_reply(request->hdr.op == query_request ? query_reply_op : addrinfo_reply_op, len, request);
3722*472cd20dSToomas Soome
3723*472cd20dSToomas Soome rep->rhdr->flags = 0;
3724*472cd20dSToomas Soome rep->rhdr->ifi = 0;
3725*472cd20dSToomas Soome rep->rhdr->error = dnssd_htonl(error);
3726*472cd20dSToomas Soome
3727*472cd20dSToomas Soome data = (char *)&rep->rhdr[1];
3728*472cd20dSToomas Soome
3729*472cd20dSToomas Soome put_string(emptystr, &data);
3730*472cd20dSToomas Soome put_uint16(0, &data);
3731*472cd20dSToomas Soome put_uint16(0, &data);
3732*472cd20dSToomas Soome put_uint16(0, &data);
3733*472cd20dSToomas Soome data += 0;
3734*472cd20dSToomas Soome put_uint32(0, &data);
3735*472cd20dSToomas Soome
3736*472cd20dSToomas Soome append_reply(request, rep);
3737*472cd20dSToomas Soome }
3738*472cd20dSToomas Soome
_handle_queryrecord_request_with_trust(request_state * request,const _queryrecord_start_params_t * const params)3739*472cd20dSToomas Soome mDNSlocal mStatus _handle_queryrecord_request_with_trust(request_state *request, const _queryrecord_start_params_t * const params)
3740*472cd20dSToomas Soome {
3741*472cd20dSToomas Soome mStatus err;
3742*472cd20dSToomas Soome if (audit_token_to_pid(request->audit_token) == 0)
3743*472cd20dSToomas Soome {
3744*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_WARNING, "[R%u] _handle_queryrecord_request_with_trust: no audit token for pid(%s %d)", request->request_id, request->pid_name, request->process_id);
3745*472cd20dSToomas Soome err = _handle_queryrecord_request_start(request, params);
3746c65ebfc7SToomas Soome }
3747c65ebfc7SToomas Soome else
3748c65ebfc7SToomas Soome {
3749*472cd20dSToomas Soome const char *service_ptr = NULL;
3750*472cd20dSToomas Soome char type_str[MAX_ESCAPED_DOMAIN_NAME] = "";
3751*472cd20dSToomas Soome domainname query_name;
3752*472cd20dSToomas Soome if (MakeDomainNameFromDNSNameString(&query_name, params->qname))
3753*472cd20dSToomas Soome {
3754*472cd20dSToomas Soome domainlabel name;
3755*472cd20dSToomas Soome domainname type, domain;
3756*472cd20dSToomas Soome bool good = DeconstructServiceName(&query_name, &name, &type, &domain);
3757*472cd20dSToomas Soome if (good)
3758*472cd20dSToomas Soome {
3759*472cd20dSToomas Soome ConvertDomainNameToCString(&type, type_str);
3760*472cd20dSToomas Soome service_ptr = type_str;
3761*472cd20dSToomas Soome }
3762c65ebfc7SToomas Soome }
3763c65ebfc7SToomas Soome
3764*472cd20dSToomas Soome mdns_trust_flags_t flags = mdns_trust_flags_none;
3765*472cd20dSToomas Soome mdns_trust_status_t status = mdns_trust_check_query(request->audit_token, params->qname, service_ptr, params->qtype, (params->flags & kDNSServiceFlagsForceMulticast) != 0, &flags);
3766*472cd20dSToomas Soome switch (status)
3767c65ebfc7SToomas Soome {
3768*472cd20dSToomas Soome case mdns_trust_status_denied:
3769*472cd20dSToomas Soome case mdns_trust_status_pending:
3770*472cd20dSToomas Soome {
3771*472cd20dSToomas Soome mdns_trust_t trust = mdns_trust_create(request->audit_token, service_ptr, flags);
3772*472cd20dSToomas Soome if (!trust )
3773*472cd20dSToomas Soome {
3774*472cd20dSToomas Soome err = mStatus_NoMemoryErr;
3775*472cd20dSToomas Soome goto exit;
3776c65ebfc7SToomas Soome }
3777c65ebfc7SToomas Soome
3778*472cd20dSToomas Soome void * context = mallocL("context/_handle_queryrecord_request_with_trust", sizeof(_queryrecord_start_params_t));
3779*472cd20dSToomas Soome if (!context)
3780c65ebfc7SToomas Soome {
3781*472cd20dSToomas Soome my_perror("ERROR: mallocL context/_handle_queryrecord_request_with_trust");
3782*472cd20dSToomas Soome mdns_release(trust);
3783*472cd20dSToomas Soome err = mStatus_NoMemoryErr;
3784*472cd20dSToomas Soome goto exit;
3785c65ebfc7SToomas Soome }
3786*472cd20dSToomas Soome memcpy(context, params, sizeof(_queryrecord_start_params_t));
3787*472cd20dSToomas Soome mdns_trust_set_context(trust, context);
3788*472cd20dSToomas Soome mdns_trust_set_queue(trust, _get_trust_results_dispatch_queue());
3789*472cd20dSToomas Soome mdns_trust_set_event_handler(trust, ^(mdns_trust_event_t event, mdns_trust_status_t update)
3790c65ebfc7SToomas Soome {
3791*472cd20dSToomas Soome if (event == mdns_trust_event_result)
3792c65ebfc7SToomas Soome {
3793*472cd20dSToomas Soome mStatus error = (update != mdns_trust_status_granted) ? mStatus_PolicyDenied : mStatus_NoError;
3794*472cd20dSToomas Soome KQueueLock();
3795*472cd20dSToomas Soome _queryrecord_start_params_t * _params = mdns_trust_get_context(trust);
3796*472cd20dSToomas Soome if (_params)
3797c65ebfc7SToomas Soome {
3798*472cd20dSToomas Soome if (!error)
3799c65ebfc7SToomas Soome {
3800*472cd20dSToomas Soome error = _handle_queryrecord_request_start(request, _params);
3801*472cd20dSToomas Soome // No context means the request was canceled before we got here
3802c65ebfc7SToomas Soome }
3803*472cd20dSToomas Soome if (error) // (not else if) Always check for error result
3804c65ebfc7SToomas Soome {
3805*472cd20dSToomas Soome _return_queryrecord_request_error(request, error);
3806c65ebfc7SToomas Soome }
3807c65ebfc7SToomas Soome }
3808*472cd20dSToomas Soome KQueueUnlock("_handle_queryrecord_request_with_trust");
3809c65ebfc7SToomas Soome }
3810*472cd20dSToomas Soome });
3811*472cd20dSToomas Soome request->trust = trust;
3812*472cd20dSToomas Soome mdns_trust_activate(trust);
3813*472cd20dSToomas Soome err = mStatus_NoError;
3814*472cd20dSToomas Soome break;
3815*472cd20dSToomas Soome }
3816*472cd20dSToomas Soome
3817*472cd20dSToomas Soome case mdns_trust_status_no_entitlement:
3818*472cd20dSToomas Soome err = mStatus_NoAuth;
3819*472cd20dSToomas Soome break;
3820*472cd20dSToomas Soome
3821*472cd20dSToomas Soome case mdns_trust_status_granted:
3822*472cd20dSToomas Soome err = _handle_queryrecord_request_start(request, params);
3823*472cd20dSToomas Soome break;
3824*472cd20dSToomas Soome
3825*472cd20dSToomas Soome default:
3826*472cd20dSToomas Soome err = mStatus_UnknownErr;
3827*472cd20dSToomas Soome break;
3828*472cd20dSToomas Soome }
3829*472cd20dSToomas Soome }
3830*472cd20dSToomas Soome exit:
3831*472cd20dSToomas Soome return err;
3832*472cd20dSToomas Soome }
3833*472cd20dSToomas Soome #endif // TRUST_ENFORCEMENT
3834c65ebfc7SToomas Soome
handle_queryrecord_request(request_state * request)3835c65ebfc7SToomas Soome mDNSlocal mStatus handle_queryrecord_request(request_state *request)
3836c65ebfc7SToomas Soome {
3837c65ebfc7SToomas Soome mStatus err;
3838*472cd20dSToomas Soome _queryrecord_start_params_t params;
3839c65ebfc7SToomas Soome
3840*472cd20dSToomas Soome params.flags = get_flags(&request->msgptr, request->msgend);
3841*472cd20dSToomas Soome params.interfaceIndex = get_uint32(&request->msgptr, request->msgend);
3842*472cd20dSToomas Soome if (get_string(&request->msgptr, request->msgend, params.qname, sizeof(params.qname)) < 0)
3843c65ebfc7SToomas Soome {
3844*472cd20dSToomas Soome err = mStatus_BadParamErr;
3845*472cd20dSToomas Soome goto exit;
3846c65ebfc7SToomas Soome }
3847*472cd20dSToomas Soome params.qtype = get_uint16(&request->msgptr, request->msgend);
3848*472cd20dSToomas Soome params.qclass = get_uint16(&request->msgptr, request->msgend);
3849c65ebfc7SToomas Soome
3850c65ebfc7SToomas Soome if (!request->msgptr)
3851*472cd20dSToomas Soome {
3852*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT,
3853*472cd20dSToomas Soome "[R%d] DNSServiceQueryRecord(unreadable parameters)", request->request_id);
3854*472cd20dSToomas Soome err = mStatus_BadParamErr;
3855*472cd20dSToomas Soome goto exit;
3856*472cd20dSToomas Soome }
3857*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, QUERIER)
3858*472cd20dSToomas Soome params.require_privacy = mDNSfalse;
3859c65ebfc7SToomas Soome #endif
3860*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, QUERIER) && MDNSRESPONDER_SUPPORTS(APPLE, IPC_TLV)
3861*472cd20dSToomas Soome if (request->msgptr && (request->hdr.ipc_flags & IPC_FLAGS_TRAILING_TLVS))
3862c65ebfc7SToomas Soome {
3863*472cd20dSToomas Soome size_t len;
3864*472cd20dSToomas Soome const mDNSu8 *const start = (const mDNSu8 *)request->msgptr;
3865*472cd20dSToomas Soome const mDNSu8 *const end = (const mDNSu8 *)request->msgend;
3866*472cd20dSToomas Soome const mDNSu8 *const data = ipc_tlv_get_resolver_config_plist_data(start, end, &len);
3867*472cd20dSToomas Soome if (data)
3868*472cd20dSToomas Soome {
3869*472cd20dSToomas Soome request->custom_service_id = Querier_RegisterCustomDNSServiceWithPListData(data, len);
3870*472cd20dSToomas Soome }
3871*472cd20dSToomas Soome params.require_privacy = ipc_tlv_get_require_privacy(start, end);
3872*472cd20dSToomas Soome }
3873*472cd20dSToomas Soome #endif
3874*472cd20dSToomas Soome request->flags = params.flags;
3875*472cd20dSToomas Soome request->interfaceIndex = params.interfaceIndex;
3876*472cd20dSToomas Soome
3877*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
3878*472cd20dSToomas Soome "[R%d] DNSServiceQueryRecord(%X, %d, " PRI_S ", " PUB_S ") START PID[%d](" PUB_S ")",
3879*472cd20dSToomas Soome request->request_id, request->flags, request->interfaceIndex, params.qname, DNSTypeName(params.qtype), request->process_id,
3880*472cd20dSToomas Soome request->pid_name);
3881*472cd20dSToomas Soome
3882*472cd20dSToomas Soome mDNSPlatformMemZero(&request->u.queryrecord, (mDNSu32)sizeof(request->u.queryrecord));
3883*472cd20dSToomas Soome request->terminate = NULL;
3884*472cd20dSToomas Soome
3885*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, TRUST_ENFORCEMENT)
3886*472cd20dSToomas Soome if (os_feature_enabled(mDNSResponder, bonjour_privacy))
3887*472cd20dSToomas Soome {
3888*472cd20dSToomas Soome err = _handle_queryrecord_request_with_trust(request, ¶ms);
3889c65ebfc7SToomas Soome }
3890c65ebfc7SToomas Soome else
3891c65ebfc7SToomas Soome {
3892*472cd20dSToomas Soome err = _handle_queryrecord_request_start(request, ¶ms);
3893c65ebfc7SToomas Soome }
3894*472cd20dSToomas Soome #else
3895*472cd20dSToomas Soome err = _handle_queryrecord_request_start(request, ¶ms);
3896*472cd20dSToomas Soome #endif
3897c65ebfc7SToomas Soome
3898*472cd20dSToomas Soome exit:
3899c65ebfc7SToomas Soome return(err);
3900c65ebfc7SToomas Soome }
3901c65ebfc7SToomas Soome
3902c65ebfc7SToomas Soome // ***************************************************************************
3903c65ebfc7SToomas Soome #if COMPILER_LIKES_PRAGMA_MARK
3904c65ebfc7SToomas Soome #pragma mark -
3905c65ebfc7SToomas Soome #pragma mark - DNSServiceEnumerateDomains
3906c65ebfc7SToomas Soome #endif
3907c65ebfc7SToomas Soome
format_enumeration_reply(request_state * request,const char * domain,DNSServiceFlags flags,mDNSu32 ifi,DNSServiceErrorType err)3908c65ebfc7SToomas Soome mDNSlocal reply_state *format_enumeration_reply(request_state *request,
3909c65ebfc7SToomas Soome const char *domain, DNSServiceFlags flags, mDNSu32 ifi, DNSServiceErrorType err)
3910c65ebfc7SToomas Soome {
3911c65ebfc7SToomas Soome size_t len;
3912c65ebfc7SToomas Soome reply_state *reply;
3913c65ebfc7SToomas Soome char *data;
3914c65ebfc7SToomas Soome
3915c65ebfc7SToomas Soome len = sizeof(DNSServiceFlags);
3916c65ebfc7SToomas Soome len += sizeof(mDNSu32);
3917c65ebfc7SToomas Soome len += sizeof(DNSServiceErrorType);
3918c65ebfc7SToomas Soome len += strlen(domain) + 1;
3919c65ebfc7SToomas Soome
3920c65ebfc7SToomas Soome reply = create_reply(enumeration_reply_op, len, request);
3921c65ebfc7SToomas Soome reply->rhdr->flags = dnssd_htonl(flags);
3922c65ebfc7SToomas Soome reply->rhdr->ifi = dnssd_htonl(ifi);
3923c65ebfc7SToomas Soome reply->rhdr->error = dnssd_htonl(err);
3924c65ebfc7SToomas Soome data = (char *)&reply->rhdr[1];
3925c65ebfc7SToomas Soome put_string(domain, &data);
3926c65ebfc7SToomas Soome return reply;
3927c65ebfc7SToomas Soome }
3928c65ebfc7SToomas Soome
enum_termination_callback(request_state * request)3929c65ebfc7SToomas Soome mDNSlocal void enum_termination_callback(request_state *request)
3930c65ebfc7SToomas Soome {
3931c65ebfc7SToomas Soome // Stop the domain enumeration queries to discover the WAB Browse/Registration domains
3932c65ebfc7SToomas Soome if (request->u.enumeration.flags & kDNSServiceFlagsRegistrationDomains)
3933c65ebfc7SToomas Soome {
3934c65ebfc7SToomas Soome LogInfo("%3d: DNSServiceEnumeration Cancel WAB Registration PID[%d](%s)", request->sd, request->process_id, request->pid_name);
3935c65ebfc7SToomas Soome uDNS_StopWABQueries(&mDNSStorage, UDNS_WAB_REG_QUERY);
3936c65ebfc7SToomas Soome }
3937c65ebfc7SToomas Soome else
3938c65ebfc7SToomas Soome {
3939c65ebfc7SToomas Soome LogInfo("%3d: DNSServiceEnumeration Cancel WAB Browse PID[%d](%s)", request->sd, request->process_id, request->pid_name);
3940c65ebfc7SToomas Soome uDNS_StopWABQueries(&mDNSStorage, UDNS_WAB_BROWSE_QUERY | UDNS_WAB_LBROWSE_QUERY);
3941c65ebfc7SToomas Soome mDNS_StopGetDomains(&mDNSStorage, &request->u.enumeration.q_autoall);
3942c65ebfc7SToomas Soome }
3943c65ebfc7SToomas Soome mDNS_StopGetDomains(&mDNSStorage, &request->u.enumeration.q_all);
3944c65ebfc7SToomas Soome mDNS_StopGetDomains(&mDNSStorage, &request->u.enumeration.q_default);
3945c65ebfc7SToomas Soome }
3946c65ebfc7SToomas Soome
enum_result_callback(mDNS * const m,DNSQuestion * const question,const ResourceRecord * const answer,QC_result AddRecord)3947c65ebfc7SToomas Soome mDNSlocal void enum_result_callback(mDNS *const m,
3948c65ebfc7SToomas Soome DNSQuestion *const question, const ResourceRecord *const answer, QC_result AddRecord)
3949c65ebfc7SToomas Soome {
3950c65ebfc7SToomas Soome char domain[MAX_ESCAPED_DOMAIN_NAME];
3951c65ebfc7SToomas Soome request_state *request = question->QuestionContext;
3952c65ebfc7SToomas Soome DNSServiceFlags flags = 0;
3953c65ebfc7SToomas Soome reply_state *reply;
3954c65ebfc7SToomas Soome (void)m; // Unused
3955c65ebfc7SToomas Soome
3956c65ebfc7SToomas Soome if (answer->rrtype != kDNSType_PTR) return;
3957c65ebfc7SToomas Soome
3958c65ebfc7SToomas Soome #if 0
3959c65ebfc7SToomas Soome if (!AuthorizedDomain(request, &answer->rdata->u.name, request->u.enumeration.flags ? AutoRegistrationDomains : AutoBrowseDomains)) return;
3960c65ebfc7SToomas Soome #endif
3961c65ebfc7SToomas Soome
3962c65ebfc7SToomas Soome // We only return add/remove events for the browse and registration lists
3963c65ebfc7SToomas Soome // For the default browse and registration answers, we only give an "ADD" event
3964c65ebfc7SToomas Soome if (question == &request->u.enumeration.q_default && !AddRecord) return;
3965c65ebfc7SToomas Soome
3966c65ebfc7SToomas Soome if (AddRecord)
3967c65ebfc7SToomas Soome {
3968c65ebfc7SToomas Soome flags |= kDNSServiceFlagsAdd;
3969c65ebfc7SToomas Soome if (question == &request->u.enumeration.q_default) flags |= kDNSServiceFlagsDefault;
3970c65ebfc7SToomas Soome }
3971c65ebfc7SToomas Soome
3972c65ebfc7SToomas Soome ConvertDomainNameToCString(&answer->rdata->u.name, domain);
3973c65ebfc7SToomas Soome // Note that we do NOT propagate specific interface indexes to the client - for example, a domain we learn from
3974c65ebfc7SToomas Soome // a machine's system preferences may be discovered on the LocalOnly interface, but should be browsed on the
3975c65ebfc7SToomas Soome // network, so we just pass kDNSServiceInterfaceIndexAny
3976c65ebfc7SToomas Soome reply = format_enumeration_reply(request, domain, flags, kDNSServiceInterfaceIndexAny, kDNSServiceErr_NoError);
3977c65ebfc7SToomas Soome if (!reply) { LogMsg("ERROR: enum_result_callback, format_enumeration_reply"); return; }
3978c65ebfc7SToomas Soome
3979*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
3980*472cd20dSToomas Soome "[R%d->Q%d] DNSServiceEnumerateDomains(%2.*s) RESULT " PUB_S ": " PRI_S,
3981*472cd20dSToomas Soome request->request_id, mDNSVal16(question->TargetQID), question->qname.c[0], &question->qname.c[1],
3982*472cd20dSToomas Soome AddRecord ? "ADD" : "RMV", domain);
3983c65ebfc7SToomas Soome
3984c65ebfc7SToomas Soome append_reply(request, reply);
3985c65ebfc7SToomas Soome }
3986c65ebfc7SToomas Soome
handle_enum_request(request_state * request)3987c65ebfc7SToomas Soome mDNSlocal mStatus handle_enum_request(request_state *request)
3988c65ebfc7SToomas Soome {
3989c65ebfc7SToomas Soome mStatus err;
3990c65ebfc7SToomas Soome DNSServiceFlags flags = get_flags(&request->msgptr, request->msgend);
3991c65ebfc7SToomas Soome DNSServiceFlags reg = flags & kDNSServiceFlagsRegistrationDomains;
3992c65ebfc7SToomas Soome mDNS_DomainType t_all = reg ? mDNS_DomainTypeRegistration : mDNS_DomainTypeBrowse;
3993c65ebfc7SToomas Soome mDNS_DomainType t_default = reg ? mDNS_DomainTypeRegistrationDefault : mDNS_DomainTypeBrowseDefault;
3994c65ebfc7SToomas Soome mDNSu32 interfaceIndex = get_uint32(&request->msgptr, request->msgend);
3995c65ebfc7SToomas Soome mDNSInterfaceID InterfaceID = mDNSPlatformInterfaceIDfromInterfaceIndex(&mDNSStorage, interfaceIndex);
3996c65ebfc7SToomas Soome if (interfaceIndex && !InterfaceID) return(mStatus_BadParamErr);
3997c65ebfc7SToomas Soome
3998c65ebfc7SToomas Soome if (!request->msgptr)
3999c65ebfc7SToomas Soome { LogMsg("%3d: DNSServiceEnumerateDomains(unreadable parameters)", request->sd); return(mStatus_BadParamErr); }
4000c65ebfc7SToomas Soome
4001c65ebfc7SToomas Soome request->flags = flags;
4002c65ebfc7SToomas Soome request->interfaceIndex = interfaceIndex;
4003c65ebfc7SToomas Soome
4004c65ebfc7SToomas Soome // mark which kind of enumeration we're doing so that we know what domain enumeration queries to stop
4005c65ebfc7SToomas Soome request->u.enumeration.flags = reg;
4006c65ebfc7SToomas Soome
4007c65ebfc7SToomas Soome // enumeration requires multiple questions, so we must link all the context pointers so that
4008c65ebfc7SToomas Soome // necessary context can be reached from the callbacks
4009c65ebfc7SToomas Soome request->u.enumeration.q_all.QuestionContext = request;
4010c65ebfc7SToomas Soome request->u.enumeration.q_default.QuestionContext = request;
4011c65ebfc7SToomas Soome if (!reg) request->u.enumeration.q_autoall.QuestionContext = request;
4012c65ebfc7SToomas Soome
4013c65ebfc7SToomas Soome // if the caller hasn't specified an explicit interface, we use local-only to get the system-wide list.
4014c65ebfc7SToomas Soome if (!InterfaceID) InterfaceID = mDNSInterface_LocalOnly;
4015c65ebfc7SToomas Soome
4016c65ebfc7SToomas Soome // make the calls
4017c65ebfc7SToomas Soome LogOperation("%3d: DNSServiceEnumerateDomains(%X=%s)", request->sd, flags,
4018c65ebfc7SToomas Soome (flags & kDNSServiceFlagsBrowseDomains ) ? "kDNSServiceFlagsBrowseDomains" :
4019c65ebfc7SToomas Soome (flags & kDNSServiceFlagsRegistrationDomains) ? "kDNSServiceFlagsRegistrationDomains" : "<<Unknown>>");
4020c65ebfc7SToomas Soome err = mDNS_GetDomains(&mDNSStorage, &request->u.enumeration.q_all, t_all, NULL, InterfaceID, enum_result_callback, request);
4021c65ebfc7SToomas Soome if (!err)
4022c65ebfc7SToomas Soome {
4023c65ebfc7SToomas Soome err = mDNS_GetDomains(&mDNSStorage, &request->u.enumeration.q_default, t_default, NULL, InterfaceID, enum_result_callback, request);
4024c65ebfc7SToomas Soome if (err) mDNS_StopGetDomains(&mDNSStorage, &request->u.enumeration.q_all);
4025c65ebfc7SToomas Soome else if (!reg)
4026c65ebfc7SToomas Soome {
4027c65ebfc7SToomas Soome err = mDNS_GetDomains(&mDNSStorage, &request->u.enumeration.q_autoall, mDNS_DomainTypeBrowseAutomatic, NULL, InterfaceID, enum_result_callback, request);
4028c65ebfc7SToomas Soome if (err)
4029c65ebfc7SToomas Soome {
4030c65ebfc7SToomas Soome mDNS_StopGetDomains(&mDNSStorage, &request->u.enumeration.q_all);
4031c65ebfc7SToomas Soome mDNS_StopGetDomains(&mDNSStorage, &request->u.enumeration.q_default);
4032c65ebfc7SToomas Soome }
4033c65ebfc7SToomas Soome }
4034c65ebfc7SToomas Soome if (!err) request->terminate = enum_termination_callback;
4035c65ebfc7SToomas Soome }
4036c65ebfc7SToomas Soome if (!err)
4037c65ebfc7SToomas Soome {
4038c65ebfc7SToomas Soome // Start the domain enumeration queries to discover the WAB Browse/Registration domains
4039c65ebfc7SToomas Soome if (reg)
4040c65ebfc7SToomas Soome {
4041c65ebfc7SToomas Soome LogInfo("%3d: DNSServiceEnumerateDomains Start WAB Registration PID[%d](%s)", request->sd, request->process_id, request->pid_name);
4042c65ebfc7SToomas Soome uDNS_StartWABQueries(&mDNSStorage, UDNS_WAB_REG_QUERY);
4043c65ebfc7SToomas Soome }
4044c65ebfc7SToomas Soome else
4045c65ebfc7SToomas Soome {
4046c65ebfc7SToomas Soome LogInfo("%3d: DNSServiceEnumerateDomains Start WAB Browse PID[%d](%s)", request->sd, request->process_id, request->pid_name);
4047c65ebfc7SToomas Soome uDNS_StartWABQueries(&mDNSStorage, UDNS_WAB_BROWSE_QUERY | UDNS_WAB_LBROWSE_QUERY);
4048c65ebfc7SToomas Soome }
4049c65ebfc7SToomas Soome }
4050c65ebfc7SToomas Soome
4051c65ebfc7SToomas Soome return(err);
4052c65ebfc7SToomas Soome }
4053c65ebfc7SToomas Soome
4054c65ebfc7SToomas Soome // ***************************************************************************
4055c65ebfc7SToomas Soome #if COMPILER_LIKES_PRAGMA_MARK
4056c65ebfc7SToomas Soome #pragma mark -
4057c65ebfc7SToomas Soome #pragma mark - DNSServiceReconfirmRecord & Misc
4058c65ebfc7SToomas Soome #endif
4059c65ebfc7SToomas Soome
handle_reconfirm_request(request_state * request)4060c65ebfc7SToomas Soome mDNSlocal mStatus handle_reconfirm_request(request_state *request)
4061c65ebfc7SToomas Soome {
4062c65ebfc7SToomas Soome mStatus status = mStatus_BadParamErr;
4063c65ebfc7SToomas Soome AuthRecord *rr = read_rr_from_ipc_msg(request, 0, 0);
4064c65ebfc7SToomas Soome if (rr)
4065c65ebfc7SToomas Soome {
4066c65ebfc7SToomas Soome status = mDNS_ReconfirmByValue(&mDNSStorage, &rr->resrec);
4067c65ebfc7SToomas Soome LogOperation(
4068c65ebfc7SToomas Soome (status == mStatus_NoError) ?
4069c65ebfc7SToomas Soome "%3d: DNSServiceReconfirmRecord(%s) interface %d initiated PID[%d](%s)" :
4070c65ebfc7SToomas Soome "%3d: DNSServiceReconfirmRecord(%s) interface %d failed PID[%d](%s) status %d",
4071c65ebfc7SToomas Soome request->sd, RRDisplayString(&mDNSStorage, &rr->resrec),
4072c65ebfc7SToomas Soome mDNSPlatformInterfaceIndexfromInterfaceID(&mDNSStorage, rr->resrec.InterfaceID, mDNSfalse),
4073c65ebfc7SToomas Soome request->process_id, request->pid_name, status);
4074c65ebfc7SToomas Soome freeL("AuthRecord/handle_reconfirm_request", rr);
4075c65ebfc7SToomas Soome }
4076c65ebfc7SToomas Soome return(status);
4077c65ebfc7SToomas Soome }
4078c65ebfc7SToomas Soome
4079c65ebfc7SToomas Soome #if APPLE_OSX_mDNSResponder
4080c65ebfc7SToomas Soome
handle_release_request(request_state * request)4081c65ebfc7SToomas Soome mDNSlocal mStatus handle_release_request(request_state *request)
4082c65ebfc7SToomas Soome {
4083c65ebfc7SToomas Soome mStatus err = 0;
4084c65ebfc7SToomas Soome char name[256], regtype[MAX_ESCAPED_DOMAIN_NAME], domain[MAX_ESCAPED_DOMAIN_NAME];
4085c65ebfc7SToomas Soome domainname instance;
4086c65ebfc7SToomas Soome
4087c65ebfc7SToomas Soome // extract the data from the message
4088c65ebfc7SToomas Soome DNSServiceFlags flags = get_flags(&request->msgptr, request->msgend);
4089c65ebfc7SToomas Soome
4090*472cd20dSToomas Soome if (get_string(&request->msgptr, request->msgend, name, sizeof(name )) < 0 ||
4091*472cd20dSToomas Soome get_string(&request->msgptr, request->msgend, regtype, sizeof(regtype)) < 0 ||
4092*472cd20dSToomas Soome get_string(&request->msgptr, request->msgend, domain, sizeof(domain )) < 0)
4093c65ebfc7SToomas Soome {
4094c65ebfc7SToomas Soome LogMsg("ERROR: handle_release_request - Couldn't read name/regtype/domain");
4095c65ebfc7SToomas Soome return(mStatus_BadParamErr);
4096c65ebfc7SToomas Soome }
4097c65ebfc7SToomas Soome
4098c65ebfc7SToomas Soome if (!request->msgptr)
4099c65ebfc7SToomas Soome {
4100c65ebfc7SToomas Soome LogMsg("%3d: PeerConnectionRelease(unreadable parameters)", request->sd);
4101c65ebfc7SToomas Soome return(mStatus_BadParamErr);
4102c65ebfc7SToomas Soome }
4103c65ebfc7SToomas Soome
4104c65ebfc7SToomas Soome if (build_domainname_from_strings(&instance, name, regtype, domain) < 0)
4105c65ebfc7SToomas Soome {
4106c65ebfc7SToomas Soome LogMsg("ERROR: handle_release_request bad “%s” “%s” “%s”", name, regtype, domain);
4107c65ebfc7SToomas Soome return(mStatus_BadParamErr);
4108c65ebfc7SToomas Soome }
4109c65ebfc7SToomas Soome
4110*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
4111*472cd20dSToomas Soome "[R%d] PeerConnectionRelease(%X " PRI_DM_NAME ") START PID[%d](" PUB_S ")",
4112*472cd20dSToomas Soome request->request_id, flags, DM_NAME_PARAM(&instance), request->process_id, request->pid_name);
4113c65ebfc7SToomas Soome
4114*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, D2D)
4115c65ebfc7SToomas Soome external_connection_release(&instance);
4116*472cd20dSToomas Soome #endif
4117c65ebfc7SToomas Soome return(err);
4118c65ebfc7SToomas Soome }
4119c65ebfc7SToomas Soome
4120c65ebfc7SToomas Soome #else // APPLE_OSX_mDNSResponder
4121c65ebfc7SToomas Soome
handle_release_request(request_state * request)4122c65ebfc7SToomas Soome mDNSlocal mStatus handle_release_request(request_state *request)
4123c65ebfc7SToomas Soome {
4124c65ebfc7SToomas Soome (void) request;
4125c65ebfc7SToomas Soome return mStatus_UnsupportedErr;
4126c65ebfc7SToomas Soome }
4127c65ebfc7SToomas Soome
4128c65ebfc7SToomas Soome #endif // APPLE_OSX_mDNSResponder
4129c65ebfc7SToomas Soome
handle_setdomain_request(request_state * request)4130c65ebfc7SToomas Soome mDNSlocal mStatus handle_setdomain_request(request_state *request)
4131c65ebfc7SToomas Soome {
4132c65ebfc7SToomas Soome char domainstr[MAX_ESCAPED_DOMAIN_NAME];
4133c65ebfc7SToomas Soome domainname domain;
4134c65ebfc7SToomas Soome DNSServiceFlags flags = get_flags(&request->msgptr, request->msgend);
4135c65ebfc7SToomas Soome (void)flags; // Unused
4136*472cd20dSToomas Soome if (get_string(&request->msgptr, request->msgend, domainstr, sizeof(domainstr)) < 0 ||
4137c65ebfc7SToomas Soome !MakeDomainNameFromDNSNameString(&domain, domainstr))
4138c65ebfc7SToomas Soome { LogMsg("%3d: DNSServiceSetDefaultDomainForUser(unreadable parameters)", request->sd); return(mStatus_BadParamErr); }
4139c65ebfc7SToomas Soome
4140c65ebfc7SToomas Soome LogOperation("%3d: DNSServiceSetDefaultDomainForUser(%##s)", request->sd, domain.c);
4141c65ebfc7SToomas Soome return(mStatus_NoError);
4142c65ebfc7SToomas Soome }
4143c65ebfc7SToomas Soome
4144c65ebfc7SToomas Soome typedef packedstruct
4145c65ebfc7SToomas Soome {
4146c65ebfc7SToomas Soome mStatus err;
4147c65ebfc7SToomas Soome mDNSu32 len;
4148c65ebfc7SToomas Soome mDNSu32 vers;
4149c65ebfc7SToomas Soome } DaemonVersionReply;
4150c65ebfc7SToomas Soome
handle_getproperty_request(request_state * request)4151c65ebfc7SToomas Soome mDNSlocal void handle_getproperty_request(request_state *request)
4152c65ebfc7SToomas Soome {
4153c65ebfc7SToomas Soome const mStatus BadParamErr = dnssd_htonl((mDNSu32)mStatus_BadParamErr);
4154c65ebfc7SToomas Soome char prop[256];
4155c65ebfc7SToomas Soome if (get_string(&request->msgptr, request->msgend, prop, sizeof(prop)) >= 0)
4156c65ebfc7SToomas Soome {
4157*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
4158*472cd20dSToomas Soome "[R%d] DNSServiceGetProperty(" PUB_S ")", request->request_id, prop);
4159c65ebfc7SToomas Soome if (!strcmp(prop, kDNSServiceProperty_DaemonVersion))
4160c65ebfc7SToomas Soome {
4161c65ebfc7SToomas Soome DaemonVersionReply x = { 0, dnssd_htonl(4), dnssd_htonl(_DNS_SD_H) };
4162c65ebfc7SToomas Soome send_all(request->sd, (const char *)&x, sizeof(x));
4163c65ebfc7SToomas Soome return;
4164c65ebfc7SToomas Soome }
4165c65ebfc7SToomas Soome }
4166c65ebfc7SToomas Soome
4167c65ebfc7SToomas Soome // If we didn't recogize the requested property name, return BadParamErr
4168c65ebfc7SToomas Soome send_all(request->sd, (const char *)&BadParamErr, sizeof(BadParamErr));
4169c65ebfc7SToomas Soome }
4170c65ebfc7SToomas Soome
4171c65ebfc7SToomas Soome #ifdef APPLE_OSX_mDNSResponder
4172c65ebfc7SToomas Soome // The caller can specify either the pid or the uuid. If the pid is not specified,
4173c65ebfc7SToomas Soome // update the effective uuid. Don't overwrite the pid which is used for debugging
4174c65ebfc7SToomas Soome // purposes and initialized when the socket is opened.
handle_connection_delegate_request(request_state * request)4175c65ebfc7SToomas Soome mDNSlocal void handle_connection_delegate_request(request_state *request)
4176c65ebfc7SToomas Soome {
4177c65ebfc7SToomas Soome mDNSs32 pid;
4178c65ebfc7SToomas Soome socklen_t len;
4179c65ebfc7SToomas Soome
4180*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
4181*472cd20dSToomas Soome "[R%d] DNSServiceCreateDelegateConnection START PID[%d](" PUB_S ")",
4182*472cd20dSToomas Soome request->request_id, request->process_id, request->pid_name);
4183c65ebfc7SToomas Soome request->terminate = connection_termination;
4184c65ebfc7SToomas Soome
4185c65ebfc7SToomas Soome len = 0;
4186c65ebfc7SToomas Soome pid = get_uint32(&request->msgptr, request->msgend);
4187c65ebfc7SToomas Soome #ifdef LOCAL_PEEREPID
4188c65ebfc7SToomas Soome if (pid)
4189c65ebfc7SToomas Soome {
4190c65ebfc7SToomas Soome len = sizeof(pid);
4191c65ebfc7SToomas Soome if (getsockopt(request->sd, SOL_LOCAL, LOCAL_PEEREPID, &request->process_id, &len) != 0)
4192c65ebfc7SToomas Soome {
4193c65ebfc7SToomas Soome LogMsg("handle_connection_delegate_request: getsockopt for LOCAL_PEEREPID failed errno:%d / %s", errno, strerror(errno));
4194c65ebfc7SToomas Soome return;
4195c65ebfc7SToomas Soome }
4196c65ebfc7SToomas Soome // to extract the process name from the pid value
4197c65ebfc7SToomas Soome if (proc_pidinfo(request->process_id, PROC_PIDT_SHORTBSDINFO, 1, &proc, PROC_PIDT_SHORTBSDINFO_SIZE) == 0)
4198c65ebfc7SToomas Soome return;
4199c65ebfc7SToomas Soome mDNSPlatformStrLCopy(request->pid_name, proc.pbsi_comm, sizeof(request->pid_name));
4200c65ebfc7SToomas Soome debugf("handle_connection_delegate_request: process id %d, name %s", request->process_id, request->pid_name);
4201c65ebfc7SToomas Soome }
4202c65ebfc7SToomas Soome #endif
4203c65ebfc7SToomas Soome #ifdef LOCAL_PEEREUUID
4204c65ebfc7SToomas Soome if (!pid)
4205c65ebfc7SToomas Soome {
4206c65ebfc7SToomas Soome len = UUID_SIZE;
4207c65ebfc7SToomas Soome if (getsockopt(request->sd, SOL_LOCAL, LOCAL_PEEREUUID, request->uuid, &len) != 0)
4208c65ebfc7SToomas Soome {
4209c65ebfc7SToomas Soome LogMsg("handle_connection_delegate_request: getsockopt for LOCAL_PEEREUUID failed errno:%d / %s", errno, strerror(errno));
4210c65ebfc7SToomas Soome return;
4211c65ebfc7SToomas Soome }
4212c65ebfc7SToomas Soome request->validUUID = mDNStrue;
4213c65ebfc7SToomas Soome }
4214c65ebfc7SToomas Soome #endif
4215c65ebfc7SToomas Soome }
4216c65ebfc7SToomas Soome #else
handle_connection_delegate_request(request_state * request)4217c65ebfc7SToomas Soome mDNSlocal void handle_connection_delegate_request(request_state *request)
4218c65ebfc7SToomas Soome {
4219c65ebfc7SToomas Soome (void) request;
4220c65ebfc7SToomas Soome }
4221c65ebfc7SToomas Soome #endif
4222c65ebfc7SToomas Soome
4223c65ebfc7SToomas Soome typedef packedstruct
4224c65ebfc7SToomas Soome {
4225c65ebfc7SToomas Soome mStatus err;
4226c65ebfc7SToomas Soome mDNSs32 pid;
4227c65ebfc7SToomas Soome } PIDInfo;
4228c65ebfc7SToomas Soome
4229c65ebfc7SToomas Soome // ***************************************************************************
4230c65ebfc7SToomas Soome #if COMPILER_LIKES_PRAGMA_MARK
4231c65ebfc7SToomas Soome #pragma mark -
4232c65ebfc7SToomas Soome #pragma mark - DNSServiceNATPortMappingCreate
4233c65ebfc7SToomas Soome #endif
4234c65ebfc7SToomas Soome
4235c65ebfc7SToomas Soome #define DNSServiceProtocol(X) ((X) == NATOp_AddrRequest ? 0 : (X) == NATOp_MapUDP ? kDNSServiceProtocol_UDP : kDNSServiceProtocol_TCP)
4236c65ebfc7SToomas Soome
port_mapping_termination_callback(request_state * request)4237c65ebfc7SToomas Soome mDNSlocal void port_mapping_termination_callback(request_state *request)
4238c65ebfc7SToomas Soome {
4239*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO, "[R%d] DNSServiceNATPortMappingCreate(%X, %u, %u, %d) STOP PID[%d](" PUB_S ")",
4240*472cd20dSToomas Soome request->request_id, DNSServiceProtocol(request->u.pm.NATinfo.Protocol),
4241c65ebfc7SToomas Soome mDNSVal16(request->u.pm.NATinfo.IntPort), mDNSVal16(request->u.pm.ReqExt), request->u.pm.NATinfo.NATLease,
4242c65ebfc7SToomas Soome request->process_id, request->pid_name);
4243*472cd20dSToomas Soome
4244c65ebfc7SToomas Soome mDNS_StopNATOperation(&mDNSStorage, &request->u.pm.NATinfo);
4245c65ebfc7SToomas Soome }
4246c65ebfc7SToomas Soome
4247c65ebfc7SToomas Soome // Called via function pointer when we get a NAT Traversal (address request or port mapping) response
port_mapping_create_request_callback(mDNS * m,NATTraversalInfo * n)4248c65ebfc7SToomas Soome mDNSlocal void port_mapping_create_request_callback(mDNS *m, NATTraversalInfo *n)
4249c65ebfc7SToomas Soome {
4250c65ebfc7SToomas Soome request_state *request = (request_state *)n->clientContext;
4251c65ebfc7SToomas Soome reply_state *rep;
4252c65ebfc7SToomas Soome int replyLen;
4253c65ebfc7SToomas Soome char *data;
4254c65ebfc7SToomas Soome
4255c65ebfc7SToomas Soome if (!request) { LogMsg("port_mapping_create_request_callback called with unknown request_state object"); return; }
4256c65ebfc7SToomas Soome
4257c65ebfc7SToomas Soome // calculate reply data length
4258c65ebfc7SToomas Soome replyLen = sizeof(DNSServiceFlags);
4259c65ebfc7SToomas Soome replyLen += 3 * sizeof(mDNSu32); // if index + addr + ttl
4260c65ebfc7SToomas Soome replyLen += sizeof(DNSServiceErrorType);
4261c65ebfc7SToomas Soome replyLen += 2 * sizeof(mDNSu16); // Internal Port + External Port
4262c65ebfc7SToomas Soome replyLen += sizeof(mDNSu8); // protocol
4263c65ebfc7SToomas Soome
4264c65ebfc7SToomas Soome rep = create_reply(port_mapping_reply_op, replyLen, request);
4265c65ebfc7SToomas Soome
4266c65ebfc7SToomas Soome rep->rhdr->flags = dnssd_htonl(0);
4267c65ebfc7SToomas Soome rep->rhdr->ifi = dnssd_htonl(mDNSPlatformInterfaceIndexfromInterfaceID(m, n->InterfaceID, mDNSfalse));
4268c65ebfc7SToomas Soome rep->rhdr->error = dnssd_htonl(n->Result);
4269c65ebfc7SToomas Soome
4270c65ebfc7SToomas Soome data = (char *)&rep->rhdr[1];
4271c65ebfc7SToomas Soome
4272c65ebfc7SToomas Soome *data++ = request->u.pm.NATinfo.ExternalAddress.b[0];
4273c65ebfc7SToomas Soome *data++ = request->u.pm.NATinfo.ExternalAddress.b[1];
4274c65ebfc7SToomas Soome *data++ = request->u.pm.NATinfo.ExternalAddress.b[2];
4275c65ebfc7SToomas Soome *data++ = request->u.pm.NATinfo.ExternalAddress.b[3];
4276c65ebfc7SToomas Soome *data++ = DNSServiceProtocol(request->u.pm.NATinfo.Protocol);
4277c65ebfc7SToomas Soome *data++ = request->u.pm.NATinfo.IntPort.b[0];
4278c65ebfc7SToomas Soome *data++ = request->u.pm.NATinfo.IntPort.b[1];
4279c65ebfc7SToomas Soome *data++ = request->u.pm.NATinfo.ExternalPort.b[0];
4280c65ebfc7SToomas Soome *data++ = request->u.pm.NATinfo.ExternalPort.b[1];
4281c65ebfc7SToomas Soome put_uint32(request->u.pm.NATinfo.Lifetime, &data);
4282c65ebfc7SToomas Soome
4283*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
4284*472cd20dSToomas Soome "[R%d] DNSServiceNATPortMappingCreate(%X, %u, %u, %d) RESULT " PRI_IPv4_ADDR ":%u TTL %u",
4285*472cd20dSToomas Soome request->request_id, DNSServiceProtocol(request->u.pm.NATinfo.Protocol),
4286c65ebfc7SToomas Soome mDNSVal16(request->u.pm.NATinfo.IntPort), mDNSVal16(request->u.pm.ReqExt), request->u.pm.NATinfo.NATLease,
4287*472cd20dSToomas Soome &request->u.pm.NATinfo.ExternalAddress, mDNSVal16(request->u.pm.NATinfo.ExternalPort),
4288*472cd20dSToomas Soome request->u.pm.NATinfo.Lifetime);
4289c65ebfc7SToomas Soome
4290c65ebfc7SToomas Soome append_reply(request, rep);
4291c65ebfc7SToomas Soome }
4292c65ebfc7SToomas Soome
handle_port_mapping_request(request_state * request)4293c65ebfc7SToomas Soome mDNSlocal mStatus handle_port_mapping_request(request_state *request)
4294c65ebfc7SToomas Soome {
4295c65ebfc7SToomas Soome mDNSu32 ttl = 0;
4296c65ebfc7SToomas Soome mStatus err = mStatus_NoError;
4297c65ebfc7SToomas Soome
4298c65ebfc7SToomas Soome DNSServiceFlags flags = get_flags(&request->msgptr, request->msgend);
4299c65ebfc7SToomas Soome mDNSu32 interfaceIndex = get_uint32(&request->msgptr, request->msgend);
4300c65ebfc7SToomas Soome mDNSInterfaceID InterfaceID = mDNSPlatformInterfaceIDfromInterfaceIndex(&mDNSStorage, interfaceIndex);
4301c65ebfc7SToomas Soome mDNSu8 protocol = (mDNSu8)get_uint32(&request->msgptr, request->msgend);
4302c65ebfc7SToomas Soome (void)flags; // Unused
4303c65ebfc7SToomas Soome if (interfaceIndex && !InterfaceID) return(mStatus_BadParamErr);
4304c65ebfc7SToomas Soome if (request->msgptr + 8 > request->msgend) request->msgptr = NULL;
4305c65ebfc7SToomas Soome else
4306c65ebfc7SToomas Soome {
4307c65ebfc7SToomas Soome request->u.pm.NATinfo.IntPort.b[0] = *request->msgptr++;
4308c65ebfc7SToomas Soome request->u.pm.NATinfo.IntPort.b[1] = *request->msgptr++;
4309c65ebfc7SToomas Soome request->u.pm.ReqExt.b[0] = *request->msgptr++;
4310c65ebfc7SToomas Soome request->u.pm.ReqExt.b[1] = *request->msgptr++;
4311c65ebfc7SToomas Soome ttl = get_uint32(&request->msgptr, request->msgend);
4312c65ebfc7SToomas Soome }
4313c65ebfc7SToomas Soome
4314c65ebfc7SToomas Soome if (!request->msgptr)
4315*472cd20dSToomas Soome {
4316*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT,
4317*472cd20dSToomas Soome "[R%d] DNSServiceNATPortMappingCreate(unreadable parameters)", request->request_id);
4318*472cd20dSToomas Soome return(mStatus_BadParamErr);
4319*472cd20dSToomas Soome }
4320c65ebfc7SToomas Soome
4321c65ebfc7SToomas Soome if (protocol == 0) // If protocol == 0 (i.e. just request public address) then IntPort, ExtPort, ttl must be zero too
4322c65ebfc7SToomas Soome {
4323c65ebfc7SToomas Soome if (!mDNSIPPortIsZero(request->u.pm.NATinfo.IntPort) || !mDNSIPPortIsZero(request->u.pm.ReqExt) || ttl) return(mStatus_BadParamErr);
4324c65ebfc7SToomas Soome }
4325c65ebfc7SToomas Soome else
4326c65ebfc7SToomas Soome {
4327c65ebfc7SToomas Soome if (mDNSIPPortIsZero(request->u.pm.NATinfo.IntPort)) return(mStatus_BadParamErr);
4328c65ebfc7SToomas Soome if (!(protocol & (kDNSServiceProtocol_UDP | kDNSServiceProtocol_TCP))) return(mStatus_BadParamErr);
4329c65ebfc7SToomas Soome }
4330c65ebfc7SToomas Soome
4331c65ebfc7SToomas Soome request->flags = flags;
4332c65ebfc7SToomas Soome request->interfaceIndex = interfaceIndex;
4333c65ebfc7SToomas Soome request->u.pm.NATinfo.Protocol = !protocol ? NATOp_AddrRequest : (protocol == kDNSServiceProtocol_UDP) ? NATOp_MapUDP : NATOp_MapTCP;
4334c65ebfc7SToomas Soome // u.pm.NATinfo.IntPort = already set above
4335c65ebfc7SToomas Soome request->u.pm.NATinfo.RequestedPort = request->u.pm.ReqExt;
4336c65ebfc7SToomas Soome request->u.pm.NATinfo.NATLease = ttl;
4337c65ebfc7SToomas Soome request->u.pm.NATinfo.clientCallback = port_mapping_create_request_callback;
4338c65ebfc7SToomas Soome request->u.pm.NATinfo.clientContext = request;
4339c65ebfc7SToomas Soome
4340*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
4341*472cd20dSToomas Soome "[R%d] DNSServiceNATPortMappingCreate(%X, %u, %u, %d) START PID[%d](" PUB_S ")",
4342*472cd20dSToomas Soome request->request_id, protocol, mDNSVal16(request->u.pm.NATinfo.IntPort), mDNSVal16(request->u.pm.ReqExt),
4343*472cd20dSToomas Soome request->u.pm.NATinfo.NATLease, request->process_id, request->pid_name);
4344c65ebfc7SToomas Soome err = mDNS_StartNATOperation(&mDNSStorage, &request->u.pm.NATinfo);
4345c65ebfc7SToomas Soome if (err) LogMsg("ERROR: mDNS_StartNATOperation: %d", (int)err);
4346c65ebfc7SToomas Soome else request->terminate = port_mapping_termination_callback;
4347c65ebfc7SToomas Soome
4348c65ebfc7SToomas Soome return(err);
4349c65ebfc7SToomas Soome }
4350c65ebfc7SToomas Soome
4351c65ebfc7SToomas Soome // ***************************************************************************
4352c65ebfc7SToomas Soome #if COMPILER_LIKES_PRAGMA_MARK
4353c65ebfc7SToomas Soome #pragma mark -
4354c65ebfc7SToomas Soome #pragma mark - DNSServiceGetAddrInfo
4355c65ebfc7SToomas Soome #endif
4356c65ebfc7SToomas Soome
addrinfo_termination_callback(request_state * request)4357c65ebfc7SToomas Soome mDNSlocal void addrinfo_termination_callback(request_state *request)
4358c65ebfc7SToomas Soome {
4359*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
4360*472cd20dSToomas Soome "[R%u] DNSServiceGetAddrInfo(" PRI_DM_NAME ") STOP PID[%d](" PUB_S ")",
4361*472cd20dSToomas Soome request->request_id, DM_NAME_PARAM(GetAddrInfoClientRequestGetQName(&request->u.addrinfo)),
4362c65ebfc7SToomas Soome request->process_id, request->pid_name);
4363c65ebfc7SToomas Soome
4364*472cd20dSToomas Soome GetAddrInfoClientRequestStop(&request->u.addrinfo);
4365c65ebfc7SToomas Soome }
4366c65ebfc7SToomas Soome
4367*472cd20dSToomas Soome typedef struct {
4368*472cd20dSToomas Soome mDNSu32 protocols;
4369*472cd20dSToomas Soome char hostname[MAX_ESCAPED_DOMAIN_NAME];
4370*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, QUERIER)
4371*472cd20dSToomas Soome mDNSBool require_privacy;
4372*472cd20dSToomas Soome #endif
4373*472cd20dSToomas Soome } _addrinfo_start_params_t;
4374c65ebfc7SToomas Soome
_handle_addrinfo_request_start(request_state * request,const _addrinfo_start_params_t * const params)4375*472cd20dSToomas Soome mDNSlocal mStatus _handle_addrinfo_request_start(request_state *request, const _addrinfo_start_params_t * const params)
4376c65ebfc7SToomas Soome {
4377*472cd20dSToomas Soome mStatus err;
4378*472cd20dSToomas Soome
4379*472cd20dSToomas Soome request->terminate = addrinfo_termination_callback;
4380*472cd20dSToomas Soome
4381*472cd20dSToomas Soome GetAddrInfoClientRequestParams gaiParams;
4382*472cd20dSToomas Soome GetAddrInfoClientRequestParamsInit(&gaiParams);
4383*472cd20dSToomas Soome gaiParams.requestID = request->request_id;
4384*472cd20dSToomas Soome gaiParams.hostnameStr = params->hostname;
4385*472cd20dSToomas Soome gaiParams.interfaceIndex = request->interfaceIndex;
4386*472cd20dSToomas Soome gaiParams.flags = request->flags;
4387*472cd20dSToomas Soome gaiParams.protocols = params->protocols;
4388*472cd20dSToomas Soome gaiParams.effectivePID = request->validUUID ? 0 : request->process_id;
4389*472cd20dSToomas Soome gaiParams.effectiveUUID = request->validUUID ? request->uuid : mDNSNULL;
4390*472cd20dSToomas Soome gaiParams.peerUID = request->uid;
4391*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, QUERIER)
4392*472cd20dSToomas Soome gaiParams.needEncryption = params->require_privacy ? mDNStrue : mDNSfalse;
4393*472cd20dSToomas Soome gaiParams.customID = request->custom_service_id;
4394*472cd20dSToomas Soome #endif
4395*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, AUDIT_TOKEN)
4396*472cd20dSToomas Soome gaiParams.peerAuditToken = &request->audit_token;
4397*472cd20dSToomas Soome #endif
4398*472cd20dSToomas Soome err = GetAddrInfoClientRequestStart(&request->u.addrinfo, &gaiParams, queryrecord_result_reply, request);
4399*472cd20dSToomas Soome
4400*472cd20dSToomas Soome return err;
4401c65ebfc7SToomas Soome }
4402*472cd20dSToomas Soome
4403*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, TRUST_ENFORCEMENT)
4404*472cd20dSToomas Soome
_return_addrinfo_request_error(request_state * request,mStatus error)4405*472cd20dSToomas Soome mDNSlocal void _return_addrinfo_request_error(request_state * request, mStatus error)
4406c65ebfc7SToomas Soome {
4407*472cd20dSToomas Soome _return_queryrecord_request_error(request, error);
4408c65ebfc7SToomas Soome }
4409*472cd20dSToomas Soome
_handle_addrinfo_request_with_trust(request_state * request,const _addrinfo_start_params_t * const params)4410*472cd20dSToomas Soome mDNSlocal mStatus _handle_addrinfo_request_with_trust(request_state *request, const _addrinfo_start_params_t * const params)
4411c65ebfc7SToomas Soome {
4412*472cd20dSToomas Soome mStatus err;
4413*472cd20dSToomas Soome if (audit_token_to_pid(request->audit_token) == 0)
4414c65ebfc7SToomas Soome {
4415*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_WARNING, "[R%u] _handle_addrinfo_request_with_trust: no audit token for pid(%s %d)", request->request_id, request->pid_name, request->process_id);
4416*472cd20dSToomas Soome err = _handle_addrinfo_request_start(request, params);
4417c65ebfc7SToomas Soome }
4418*472cd20dSToomas Soome else
4419c65ebfc7SToomas Soome {
4420*472cd20dSToomas Soome mdns_trust_flags_t flags = mdns_trust_flags_none;
4421*472cd20dSToomas Soome mdns_trust_status_t status = mdns_trust_check_getaddrinfo(request->audit_token, params->hostname, &flags);
4422*472cd20dSToomas Soome switch (status)
4423c65ebfc7SToomas Soome {
4424*472cd20dSToomas Soome case mdns_trust_status_denied:
4425*472cd20dSToomas Soome case mdns_trust_status_pending:
4426c65ebfc7SToomas Soome {
4427*472cd20dSToomas Soome mdns_trust_t trust = mdns_trust_create(request->audit_token, NULL, flags);
4428*472cd20dSToomas Soome if (!trust )
4429c65ebfc7SToomas Soome {
4430*472cd20dSToomas Soome err = mStatus_NoMemoryErr;
4431*472cd20dSToomas Soome goto exit;
4432c65ebfc7SToomas Soome }
4433*472cd20dSToomas Soome
4434*472cd20dSToomas Soome void * context = mallocL("context/_handle_addrinfo_request_with_trust", sizeof(_addrinfo_start_params_t));
4435*472cd20dSToomas Soome if (!context)
4436c65ebfc7SToomas Soome {
4437*472cd20dSToomas Soome my_perror("ERROR: mallocL context/_handle_addrinfo_request_with_trust");
4438*472cd20dSToomas Soome mdns_release(trust);
4439*472cd20dSToomas Soome err = mStatus_NoMemoryErr;
4440*472cd20dSToomas Soome goto exit;
4441*472cd20dSToomas Soome }
4442*472cd20dSToomas Soome memcpy(context, params, sizeof(_addrinfo_start_params_t));
4443*472cd20dSToomas Soome mdns_trust_set_context(trust, context);
4444*472cd20dSToomas Soome mdns_trust_set_queue(trust, _get_trust_results_dispatch_queue());
4445*472cd20dSToomas Soome mdns_trust_set_event_handler(trust, ^(mdns_trust_event_t event, mdns_trust_status_t update)
4446c65ebfc7SToomas Soome {
4447*472cd20dSToomas Soome if (event == mdns_trust_event_result)
4448*472cd20dSToomas Soome {
4449*472cd20dSToomas Soome mStatus error = (update != mdns_trust_status_granted) ? mStatus_PolicyDenied : mStatus_NoError;
4450*472cd20dSToomas Soome KQueueLock();
4451*472cd20dSToomas Soome _addrinfo_start_params_t * _params = mdns_trust_get_context(trust);
4452*472cd20dSToomas Soome if (_params)
4453*472cd20dSToomas Soome {
4454*472cd20dSToomas Soome if (!error)
4455*472cd20dSToomas Soome {
4456*472cd20dSToomas Soome error = _handle_addrinfo_request_start(request, _params);
4457*472cd20dSToomas Soome // No context means the request was canceled before we got here
4458c65ebfc7SToomas Soome }
4459*472cd20dSToomas Soome if (error) // (not else if) Always check for error result
4460*472cd20dSToomas Soome {
4461*472cd20dSToomas Soome _return_addrinfo_request_error(request, error);
4462c65ebfc7SToomas Soome }
4463c65ebfc7SToomas Soome }
4464*472cd20dSToomas Soome KQueueUnlock("_handle_addrinfo_request_with_trust");
4465c65ebfc7SToomas Soome }
4466*472cd20dSToomas Soome });
4467*472cd20dSToomas Soome request->trust = trust;
4468*472cd20dSToomas Soome mdns_trust_activate(trust);
4469*472cd20dSToomas Soome err = mStatus_NoError;
4470*472cd20dSToomas Soome break;
4471*472cd20dSToomas Soome }
4472*472cd20dSToomas Soome
4473*472cd20dSToomas Soome case mdns_trust_status_no_entitlement:
4474*472cd20dSToomas Soome err = mStatus_NoAuth;
4475*472cd20dSToomas Soome break;
4476*472cd20dSToomas Soome
4477*472cd20dSToomas Soome case mdns_trust_status_granted:
4478*472cd20dSToomas Soome err = _handle_addrinfo_request_start(request, params);
4479*472cd20dSToomas Soome break;
4480*472cd20dSToomas Soome
4481*472cd20dSToomas Soome default:
4482*472cd20dSToomas Soome err = mStatus_UnknownErr;
4483*472cd20dSToomas Soome break;
4484*472cd20dSToomas Soome }
4485*472cd20dSToomas Soome }
4486*472cd20dSToomas Soome exit:
4487*472cd20dSToomas Soome return err;
4488*472cd20dSToomas Soome }
4489*472cd20dSToomas Soome #endif // TRUST_ENFORCEMENT
4490c65ebfc7SToomas Soome
handle_addrinfo_request(request_state * request)4491c65ebfc7SToomas Soome mDNSlocal mStatus handle_addrinfo_request(request_state *request)
4492c65ebfc7SToomas Soome {
4493*472cd20dSToomas Soome mStatus err;
4494*472cd20dSToomas Soome DNSServiceFlags flags;
4495*472cd20dSToomas Soome mDNSu32 interfaceIndex;
4496*472cd20dSToomas Soome _addrinfo_start_params_t params;
4497c65ebfc7SToomas Soome
4498*472cd20dSToomas Soome flags = get_flags(&request->msgptr, request->msgend);
4499*472cd20dSToomas Soome interfaceIndex = get_uint32(&request->msgptr, request->msgend);
4500*472cd20dSToomas Soome params.protocols = get_uint32(&request->msgptr, request->msgend);
4501*472cd20dSToomas Soome if (get_string(&request->msgptr, request->msgend, params.hostname, sizeof(params.hostname)) < 0)
4502c65ebfc7SToomas Soome {
4503*472cd20dSToomas Soome err = mStatus_BadParamErr;
4504*472cd20dSToomas Soome goto exit;
4505c65ebfc7SToomas Soome }
4506*472cd20dSToomas Soome if (!request->msgptr)
4507c65ebfc7SToomas Soome {
4508*472cd20dSToomas Soome LogMsg("%3d: DNSServiceGetAddrInfo(unreadable parameters)", request->sd);
4509*472cd20dSToomas Soome err = mStatus_BadParamErr;
4510*472cd20dSToomas Soome goto exit;
4511*472cd20dSToomas Soome }
4512*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, QUERIER)
4513*472cd20dSToomas Soome params.require_privacy = mDNSfalse;
4514*472cd20dSToomas Soome #endif
4515*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, QUERIER) && MDNSRESPONDER_SUPPORTS(APPLE, IPC_TLV)
4516*472cd20dSToomas Soome if (request->msgptr && (request->hdr.ipc_flags & IPC_FLAGS_TRAILING_TLVS))
4517c65ebfc7SToomas Soome {
4518*472cd20dSToomas Soome size_t len;
4519*472cd20dSToomas Soome const mDNSu8 *const start = (const mDNSu8 *)request->msgptr;
4520*472cd20dSToomas Soome const mDNSu8 *const end = (const mDNSu8 *)request->msgend;
4521*472cd20dSToomas Soome const mDNSu8 *const data = ipc_tlv_get_resolver_config_plist_data(start, end, &len);
4522*472cd20dSToomas Soome if (data)
4523*472cd20dSToomas Soome {
4524*472cd20dSToomas Soome request->custom_service_id = Querier_RegisterCustomDNSServiceWithPListData(data, len);
4525c65ebfc7SToomas Soome }
4526*472cd20dSToomas Soome params.require_privacy = ipc_tlv_get_require_privacy(start, end);
4527c65ebfc7SToomas Soome }
4528*472cd20dSToomas Soome #endif
4529c65ebfc7SToomas Soome request->flags = flags;
4530c65ebfc7SToomas Soome request->interfaceIndex = interfaceIndex;
4531c65ebfc7SToomas Soome
4532*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
4533*472cd20dSToomas Soome "[R%u] DNSServiceGetAddrInfo(%X, %d, %u, " PRI_S ") START PID[%d](" PUB_S ")",
4534*472cd20dSToomas Soome request->request_id, request->flags, request->interfaceIndex, params.protocols, params.hostname, request->process_id,
4535*472cd20dSToomas Soome request->pid_name);
4536c65ebfc7SToomas Soome
4537*472cd20dSToomas Soome mDNSPlatformMemZero(&request->u.addrinfo, (mDNSu32)sizeof(request->u.addrinfo));
4538*472cd20dSToomas Soome request->terminate = NULL;
4539c65ebfc7SToomas Soome
4540*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, TRUST_ENFORCEMENT)
4541*472cd20dSToomas Soome if (os_feature_enabled(mDNSResponder, bonjour_privacy))
4542*472cd20dSToomas Soome {
4543*472cd20dSToomas Soome err = _handle_addrinfo_request_with_trust(request, ¶ms);
4544*472cd20dSToomas Soome }
4545*472cd20dSToomas Soome else
4546*472cd20dSToomas Soome {
4547*472cd20dSToomas Soome err = _handle_addrinfo_request_start(request, ¶ms);
4548*472cd20dSToomas Soome }
4549*472cd20dSToomas Soome #else
4550*472cd20dSToomas Soome err = _handle_addrinfo_request_start(request, ¶ms);
4551c65ebfc7SToomas Soome #endif
4552c65ebfc7SToomas Soome
4553*472cd20dSToomas Soome exit:
4554c65ebfc7SToomas Soome return(err);
4555c65ebfc7SToomas Soome }
4556c65ebfc7SToomas Soome
4557c65ebfc7SToomas Soome // ***************************************************************************
4558c65ebfc7SToomas Soome #if COMPILER_LIKES_PRAGMA_MARK
4559c65ebfc7SToomas Soome #pragma mark -
4560c65ebfc7SToomas Soome #pragma mark - Main Request Handler etc.
4561c65ebfc7SToomas Soome #endif
4562c65ebfc7SToomas Soome
NewRequest(void)4563c65ebfc7SToomas Soome mDNSlocal request_state *NewRequest(void)
4564c65ebfc7SToomas Soome {
4565*472cd20dSToomas Soome request_state *request;
4566c65ebfc7SToomas Soome request_state **p = &all_requests;
4567*472cd20dSToomas Soome request = (request_state *) callocL("request_state", sizeof(*request));
4568*472cd20dSToomas Soome if (!request) FatalError("ERROR: calloc");
4569*472cd20dSToomas Soome while (*p) p = &(*p)->next;
4570*472cd20dSToomas Soome *p = request;
4571*472cd20dSToomas Soome return(request);
4572c65ebfc7SToomas Soome }
4573c65ebfc7SToomas Soome
4574c65ebfc7SToomas Soome // read_msg may be called any time when the transfer state (req->ts) is t_morecoming.
4575c65ebfc7SToomas Soome // if there is no data on the socket, the socket will be closed and t_terminated will be returned
read_msg(request_state * req)4576c65ebfc7SToomas Soome mDNSlocal void read_msg(request_state *req)
4577c65ebfc7SToomas Soome {
4578c65ebfc7SToomas Soome if (req->ts == t_terminated || req->ts == t_error)
4579*472cd20dSToomas Soome {
4580*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_ERROR,
4581*472cd20dSToomas Soome "[R%u] ERROR: read_msg called with transfer state terminated or error", req->request_id);
4582*472cd20dSToomas Soome req->ts = t_error;
4583*472cd20dSToomas Soome return;
4584*472cd20dSToomas Soome }
4585c65ebfc7SToomas Soome
4586c65ebfc7SToomas Soome if (req->ts == t_complete) // this must be death or something is wrong
4587c65ebfc7SToomas Soome {
4588c65ebfc7SToomas Soome char buf[4]; // dummy for death notification
4589c65ebfc7SToomas Soome int nread = udsSupportReadFD(req->sd, buf, 4, 0, req->platform_data);
4590c65ebfc7SToomas Soome if (!nread) { req->ts = t_terminated; return; }
4591c65ebfc7SToomas Soome if (nread < 0) goto rerror;
4592*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_ERROR,
4593*472cd20dSToomas Soome "[R%u] ERROR: read data from a completed request", req->request_id);
4594c65ebfc7SToomas Soome req->ts = t_error;
4595c65ebfc7SToomas Soome return;
4596c65ebfc7SToomas Soome }
4597c65ebfc7SToomas Soome
4598c65ebfc7SToomas Soome if (req->ts != t_morecoming)
4599*472cd20dSToomas Soome {
4600*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_ERROR,
4601*472cd20dSToomas Soome "[R%u] ERROR: read_msg called with invalid transfer state (%d)", req->request_id, req->ts);
4602*472cd20dSToomas Soome req->ts = t_error;
4603*472cd20dSToomas Soome return;
4604*472cd20dSToomas Soome }
4605c65ebfc7SToomas Soome
4606c65ebfc7SToomas Soome if (req->hdr_bytes < sizeof(ipc_msg_hdr))
4607c65ebfc7SToomas Soome {
4608c65ebfc7SToomas Soome mDNSu32 nleft = sizeof(ipc_msg_hdr) - req->hdr_bytes;
4609c65ebfc7SToomas Soome int nread = udsSupportReadFD(req->sd, (char *)&req->hdr + req->hdr_bytes, nleft, 0, req->platform_data);
4610c65ebfc7SToomas Soome if (nread == 0) { req->ts = t_terminated; return; }
4611c65ebfc7SToomas Soome if (nread < 0) goto rerror;
4612c65ebfc7SToomas Soome req->hdr_bytes += nread;
4613c65ebfc7SToomas Soome if (req->hdr_bytes > sizeof(ipc_msg_hdr))
4614*472cd20dSToomas Soome {
4615*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_ERROR,
4616*472cd20dSToomas Soome "[R%u] ERROR: read_msg - read too many header bytes", req->request_id);
4617*472cd20dSToomas Soome req->ts = t_error;
4618*472cd20dSToomas Soome return;
4619*472cd20dSToomas Soome }
4620c65ebfc7SToomas Soome
4621c65ebfc7SToomas Soome // only read data if header is complete
4622c65ebfc7SToomas Soome if (req->hdr_bytes == sizeof(ipc_msg_hdr))
4623c65ebfc7SToomas Soome {
4624c65ebfc7SToomas Soome ConvertHeaderBytes(&req->hdr);
4625c65ebfc7SToomas Soome if (req->hdr.version != VERSION)
4626*472cd20dSToomas Soome {
4627*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_ERROR,
4628*472cd20dSToomas Soome "[R%u] ERROR: client version 0x%08X daemon version 0x%08X", req->request_id, req->hdr.version, VERSION);
4629*472cd20dSToomas Soome req->ts = t_error;
4630*472cd20dSToomas Soome return;
4631*472cd20dSToomas Soome }
4632c65ebfc7SToomas Soome
4633c65ebfc7SToomas Soome // Largest conceivable single request is a DNSServiceRegisterRecord() or DNSServiceAddRecord()
4634c65ebfc7SToomas Soome // with 64kB of rdata. Adding 1009 byte for a maximal domain name, plus a safety margin
4635c65ebfc7SToomas Soome // for other overhead, this means any message above 70kB is definitely bogus.
4636c65ebfc7SToomas Soome if (req->hdr.datalen > 70000)
4637*472cd20dSToomas Soome {
4638*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_ERROR,
4639*472cd20dSToomas Soome "[R%u] ERROR: read_msg: hdr.datalen %u (0x%X) > 70000", req->request_id, req->hdr.datalen, req->hdr.datalen);
4640*472cd20dSToomas Soome req->ts = t_error;
4641*472cd20dSToomas Soome return;
4642*472cd20dSToomas Soome }
4643*472cd20dSToomas Soome req->msgbuf = (char *) callocL("request_state msgbuf", req->hdr.datalen + MSG_PAD_BYTES);
4644*472cd20dSToomas Soome if (!req->msgbuf) { my_perror("ERROR: calloc"); req->ts = t_error; return; }
4645c65ebfc7SToomas Soome req->msgptr = req->msgbuf;
4646c65ebfc7SToomas Soome req->msgend = req->msgbuf + req->hdr.datalen;
4647c65ebfc7SToomas Soome }
4648c65ebfc7SToomas Soome }
4649c65ebfc7SToomas Soome
4650c65ebfc7SToomas Soome // If our header is complete, but we're still needing more body data, then try to read it now
4651c65ebfc7SToomas Soome // Note: For cancel_request req->hdr.datalen == 0, but there's no error return socket for cancel_request
4652c65ebfc7SToomas Soome // Any time we need to get the error return socket we know we'll have at least one data byte
4653c65ebfc7SToomas Soome // (even if only the one-byte empty C string placeholder for the old ctrl_path parameter)
4654c65ebfc7SToomas Soome if (req->hdr_bytes == sizeof(ipc_msg_hdr) && req->data_bytes < req->hdr.datalen)
4655c65ebfc7SToomas Soome {
4656c65ebfc7SToomas Soome mDNSu32 nleft = req->hdr.datalen - req->data_bytes;
4657*472cd20dSToomas Soome ssize_t nread;
4658c65ebfc7SToomas Soome #if !defined(_WIN32)
4659c65ebfc7SToomas Soome struct iovec vec = { req->msgbuf + req->data_bytes, nleft }; // Tell recvmsg where we want the bytes put
4660c65ebfc7SToomas Soome struct msghdr msg;
4661c65ebfc7SToomas Soome struct cmsghdr *cmsg;
4662c65ebfc7SToomas Soome char cbuf[CMSG_SPACE(4 * sizeof(dnssd_sock_t))];
4663c65ebfc7SToomas Soome msg.msg_name = 0;
4664c65ebfc7SToomas Soome msg.msg_namelen = 0;
4665c65ebfc7SToomas Soome msg.msg_iov = &vec;
4666c65ebfc7SToomas Soome msg.msg_iovlen = 1;
4667c65ebfc7SToomas Soome msg.msg_control = cbuf;
4668c65ebfc7SToomas Soome msg.msg_controllen = sizeof(cbuf);
4669c65ebfc7SToomas Soome msg.msg_flags = 0;
4670c65ebfc7SToomas Soome nread = recvmsg(req->sd, &msg, 0);
4671c65ebfc7SToomas Soome #else
4672c65ebfc7SToomas Soome nread = udsSupportReadFD(req->sd, (char *)req->msgbuf + req->data_bytes, nleft, 0, req->platform_data);
4673c65ebfc7SToomas Soome #endif
4674c65ebfc7SToomas Soome if (nread == 0) { req->ts = t_terminated; return; }
4675c65ebfc7SToomas Soome if (nread < 0) goto rerror;
4676c65ebfc7SToomas Soome req->data_bytes += nread;
4677c65ebfc7SToomas Soome if (req->data_bytes > req->hdr.datalen)
4678*472cd20dSToomas Soome {
4679*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_ERROR,
4680*472cd20dSToomas Soome "[R%u] ERROR: read_msg - read too many data bytes", req->request_id);
4681*472cd20dSToomas Soome req->ts = t_error;
4682*472cd20dSToomas Soome return;
4683*472cd20dSToomas Soome }
4684c65ebfc7SToomas Soome #if !defined(_WIN32)
4685c65ebfc7SToomas Soome cmsg = CMSG_FIRSTHDR(&msg);
4686c65ebfc7SToomas Soome #if DEBUG_64BIT_SCM_RIGHTS
4687*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT,
4688*472cd20dSToomas Soome "[R%u] Expecting %d %d %d %d", req->request_id, sizeof(cbuf), sizeof(cbuf), SOL_SOCKET, SCM_RIGHTS);
4689*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT,
4690*472cd20dSToomas Soome "[R%u] Got %d %d %d %d", req->request_id, msg.msg_controllen, cmsg ? cmsg->cmsg_len : -1, cmsg ? cmsg->cmsg_level : -1, cmsg ? cmsg->cmsg_type : -1);
4691c65ebfc7SToomas Soome #endif // DEBUG_64BIT_SCM_RIGHTS
4692c65ebfc7SToomas Soome if (cmsg && cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS)
4693c65ebfc7SToomas Soome {
4694c65ebfc7SToomas Soome #if APPLE_OSX_mDNSResponder
4695c65ebfc7SToomas Soome // Strictly speaking BPF_fd belongs solely in the platform support layer, but because
4696c65ebfc7SToomas Soome // of privilege separation on Mac OS X we need to get BPF_fd from mDNSResponderHelper,
4697c65ebfc7SToomas Soome // and it's convenient to repurpose the existing fd-passing code here for that task
4698c65ebfc7SToomas Soome if (req->hdr.op == send_bpf)
4699c65ebfc7SToomas Soome {
4700c65ebfc7SToomas Soome dnssd_sock_t x = *(dnssd_sock_t *)CMSG_DATA(cmsg);
4701*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT,
4702*472cd20dSToomas Soome "[R%u] Got len %d, BPF %d", req->request_id, cmsg->cmsg_len, x);
4703c65ebfc7SToomas Soome mDNSPlatformReceiveBPF_fd(x);
4704c65ebfc7SToomas Soome }
4705c65ebfc7SToomas Soome else
4706c65ebfc7SToomas Soome #endif // APPLE_OSX_mDNSResponder
4707c65ebfc7SToomas Soome req->errsd = *(dnssd_sock_t *)CMSG_DATA(cmsg);
4708c65ebfc7SToomas Soome #if DEBUG_64BIT_SCM_RIGHTS
4709*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEFAULT,
4710*472cd20dSToomas Soome "[R%u] read req->errsd %d", req->request_id, req->errsd);
4711c65ebfc7SToomas Soome #endif // DEBUG_64BIT_SCM_RIGHTS
4712c65ebfc7SToomas Soome if (req->data_bytes < req->hdr.datalen)
4713c65ebfc7SToomas Soome {
4714*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_DEBUG,
4715*472cd20dSToomas Soome "[R%u] Client(PID [%d](" PUB_S ")) sent result code socket %d via SCM_RIGHTS with req->data_bytes %d < req->hdr.datalen %d",
4716*472cd20dSToomas Soome req->request_id, req->process_id, req->pid_name, req->errsd, req->data_bytes, req->hdr.datalen);
4717c65ebfc7SToomas Soome req->ts = t_error;
4718c65ebfc7SToomas Soome return;
4719c65ebfc7SToomas Soome }
4720c65ebfc7SToomas Soome }
4721c65ebfc7SToomas Soome #endif
4722c65ebfc7SToomas Soome }
4723c65ebfc7SToomas Soome
4724c65ebfc7SToomas Soome // If our header and data are both complete, see if we need to make our separate error return socket
4725c65ebfc7SToomas Soome if (req->hdr_bytes == sizeof(ipc_msg_hdr) && req->data_bytes == req->hdr.datalen)
4726c65ebfc7SToomas Soome {
4727c65ebfc7SToomas Soome if (req->terminate && req->hdr.op != cancel_request)
4728c65ebfc7SToomas Soome {
4729c65ebfc7SToomas Soome dnssd_sockaddr_t cliaddr;
4730c65ebfc7SToomas Soome #if defined(USE_TCP_LOOPBACK)
4731c65ebfc7SToomas Soome mDNSOpaque16 port;
4732c65ebfc7SToomas Soome u_long opt = 1;
4733c65ebfc7SToomas Soome port.b[0] = req->msgptr[0];
4734c65ebfc7SToomas Soome port.b[1] = req->msgptr[1];
4735c65ebfc7SToomas Soome req->msgptr += 2;
4736c65ebfc7SToomas Soome cliaddr.sin_family = AF_INET;
4737c65ebfc7SToomas Soome cliaddr.sin_port = port.NotAnInteger;
4738c65ebfc7SToomas Soome cliaddr.sin_addr.s_addr = inet_addr(MDNS_TCP_SERVERADDR);
4739c65ebfc7SToomas Soome #else
4740c65ebfc7SToomas Soome char ctrl_path[MAX_CTLPATH];
4741c65ebfc7SToomas Soome get_string(&req->msgptr, req->msgend, ctrl_path, MAX_CTLPATH); // path is first element in message buffer
4742c65ebfc7SToomas Soome mDNSPlatformMemZero(&cliaddr, sizeof(cliaddr));
4743c65ebfc7SToomas Soome cliaddr.sun_family = AF_LOCAL;
4744c65ebfc7SToomas Soome mDNSPlatformStrLCopy(cliaddr.sun_path, ctrl_path, sizeof(cliaddr.sun_path));
4745c65ebfc7SToomas Soome // If the error return path UDS name is empty string, that tells us
4746c65ebfc7SToomas Soome // that this is a new version of the library that's going to pass us
4747c65ebfc7SToomas Soome // the error return path socket via sendmsg/recvmsg
4748c65ebfc7SToomas Soome if (ctrl_path[0] == 0)
4749c65ebfc7SToomas Soome {
4750c65ebfc7SToomas Soome if (req->errsd == req->sd)
4751*472cd20dSToomas Soome {
4752*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_ERROR,
4753*472cd20dSToomas Soome "[R%u] read_msg: ERROR failed to get errsd via SCM_RIGHTS", req->request_id);
4754*472cd20dSToomas Soome req->ts = t_error;
4755*472cd20dSToomas Soome return;
4756*472cd20dSToomas Soome }
4757c65ebfc7SToomas Soome goto got_errfd;
4758c65ebfc7SToomas Soome }
4759c65ebfc7SToomas Soome #endif
4760c65ebfc7SToomas Soome
4761c65ebfc7SToomas Soome req->errsd = socket(AF_DNSSD, SOCK_STREAM, 0);
4762c65ebfc7SToomas Soome if (!dnssd_SocketValid(req->errsd))
4763c65ebfc7SToomas Soome {
4764c65ebfc7SToomas Soome my_throttled_perror("ERROR: socket");
4765c65ebfc7SToomas Soome req->ts = t_error;
4766c65ebfc7SToomas Soome return;
4767c65ebfc7SToomas Soome }
4768c65ebfc7SToomas Soome
4769c65ebfc7SToomas Soome if (connect(req->errsd, (struct sockaddr *)&cliaddr, sizeof(cliaddr)) < 0)
4770c65ebfc7SToomas Soome {
4771c65ebfc7SToomas Soome #if !defined(USE_TCP_LOOPBACK)
4772c65ebfc7SToomas Soome struct stat sb;
4773*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_ERROR,
4774*472cd20dSToomas Soome "[R%u] read_msg: Couldn't connect to error return path socket " PUB_S " errno %d (" PUB_S ")",
4775*472cd20dSToomas Soome req->request_id, cliaddr.sun_path, dnssd_errno, dnssd_strerror(dnssd_errno));
4776c65ebfc7SToomas Soome if (stat(cliaddr.sun_path, &sb) < 0)
4777*472cd20dSToomas Soome {
4778*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_ERROR,
4779*472cd20dSToomas Soome "[R%u] read_msg: stat failed " PUB_S " errno %d (" PUB_S ")",
4780*472cd20dSToomas Soome req->request_id, cliaddr.sun_path, dnssd_errno, dnssd_strerror(dnssd_errno));
4781*472cd20dSToomas Soome }
4782c65ebfc7SToomas Soome else
4783*472cd20dSToomas Soome {
4784*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_ERROR,
4785*472cd20dSToomas Soome "[R%u] read_msg: file " PUB_S " mode %o (octal) uid %d gid %d",
4786*472cd20dSToomas Soome req->request_id, cliaddr.sun_path, sb.st_mode, sb.st_uid, sb.st_gid);
4787*472cd20dSToomas Soome }
4788c65ebfc7SToomas Soome #endif
4789c65ebfc7SToomas Soome req->ts = t_error;
4790c65ebfc7SToomas Soome return;
4791c65ebfc7SToomas Soome }
4792c65ebfc7SToomas Soome
4793c65ebfc7SToomas Soome #if !defined(USE_TCP_LOOPBACK)
4794c65ebfc7SToomas Soome got_errfd:
4795c65ebfc7SToomas Soome #endif
4796*472cd20dSToomas Soome
4797c65ebfc7SToomas Soome #if defined(_WIN32)
4798c65ebfc7SToomas Soome if (ioctlsocket(req->errsd, FIONBIO, &opt) != 0)
4799c65ebfc7SToomas Soome #else
4800c65ebfc7SToomas Soome if (fcntl(req->errsd, F_SETFL, fcntl(req->errsd, F_GETFL, 0) | O_NONBLOCK) != 0)
4801c65ebfc7SToomas Soome #endif
4802c65ebfc7SToomas Soome {
4803*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_ERROR,
4804*472cd20dSToomas Soome "[R%u] ERROR: could not set control socket to non-blocking mode errno %d (" PUB_S ")",
4805*472cd20dSToomas Soome req->request_id, dnssd_errno, dnssd_strerror(dnssd_errno));
4806c65ebfc7SToomas Soome req->ts = t_error;
4807c65ebfc7SToomas Soome return;
4808c65ebfc7SToomas Soome }
4809c65ebfc7SToomas Soome }
4810c65ebfc7SToomas Soome
4811c65ebfc7SToomas Soome req->ts = t_complete;
4812c65ebfc7SToomas Soome }
4813c65ebfc7SToomas Soome
4814c65ebfc7SToomas Soome return;
4815c65ebfc7SToomas Soome
4816c65ebfc7SToomas Soome rerror:
4817c65ebfc7SToomas Soome if (dnssd_errno == dnssd_EWOULDBLOCK || dnssd_errno == dnssd_EINTR) return;
4818*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_ERROR,
4819*472cd20dSToomas Soome "[R%u] ERROR: read_msg errno %d (" PUB_S ")", req->request_id, dnssd_errno, dnssd_strerror(dnssd_errno));
4820c65ebfc7SToomas Soome req->ts = t_error;
4821c65ebfc7SToomas Soome }
4822c65ebfc7SToomas Soome
handle_client_request(request_state * req)4823c65ebfc7SToomas Soome mDNSlocal mStatus handle_client_request(request_state *req)
4824c65ebfc7SToomas Soome {
4825c65ebfc7SToomas Soome mStatus err = mStatus_NoError;
4826*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, AUDIT_TOKEN)
4827*472cd20dSToomas Soome SetupAuditTokenForRequest(req);
4828*472cd20dSToomas Soome #endif
4829c65ebfc7SToomas Soome switch(req->hdr.op)
4830c65ebfc7SToomas Soome {
4831c65ebfc7SToomas Soome // These are all operations that have their own first-class request_state object
4832c65ebfc7SToomas Soome case connection_request:
4833*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
4834*472cd20dSToomas Soome "[R%d] DNSServiceCreateConnection START PID[%d](" PUB_S ")",
4835*472cd20dSToomas Soome req->request_id, req->process_id, req->pid_name);
4836c65ebfc7SToomas Soome req->terminate = connection_termination;
4837c65ebfc7SToomas Soome break;
4838c65ebfc7SToomas Soome case connection_delegate_request:
4839*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
4840*472cd20dSToomas Soome "[R%d] DNSServiceCreateDelegateConnection START PID[%d](" PRI_S ")",
4841*472cd20dSToomas Soome req->request_id, req->process_id, req->pid_name);
4842c65ebfc7SToomas Soome req->terminate = connection_termination;
4843c65ebfc7SToomas Soome handle_connection_delegate_request(req);
4844c65ebfc7SToomas Soome break;
4845c65ebfc7SToomas Soome case resolve_request: err = handle_resolve_request (req); break;
4846c65ebfc7SToomas Soome case query_request: err = handle_queryrecord_request (req); break;
4847c65ebfc7SToomas Soome case browse_request: err = handle_browse_request (req); break;
4848c65ebfc7SToomas Soome case reg_service_request: err = handle_regservice_request (req); break;
4849c65ebfc7SToomas Soome case enumeration_request: err = handle_enum_request (req); break;
4850c65ebfc7SToomas Soome case reconfirm_record_request: err = handle_reconfirm_request (req); break;
4851c65ebfc7SToomas Soome case setdomain_request: err = handle_setdomain_request (req); break;
4852c65ebfc7SToomas Soome case getproperty_request: handle_getproperty_request (req); break;
4853c65ebfc7SToomas Soome case port_mapping_request: err = handle_port_mapping_request(req); break;
4854c65ebfc7SToomas Soome case addrinfo_request: err = handle_addrinfo_request (req); break;
4855c65ebfc7SToomas Soome case send_bpf: /* Do nothing for send_bpf */ break;
4856c65ebfc7SToomas Soome
4857c65ebfc7SToomas Soome // These are all operations that work with an existing request_state object
4858c65ebfc7SToomas Soome case reg_record_request: err = handle_regrecord_request (req); break;
4859c65ebfc7SToomas Soome case add_record_request: err = handle_add_request (req); break;
4860c65ebfc7SToomas Soome case update_record_request: err = handle_update_request (req); break;
4861c65ebfc7SToomas Soome case remove_record_request: err = handle_removerecord_request(req); break;
4862c65ebfc7SToomas Soome case cancel_request: handle_cancel_request (req); break;
4863c65ebfc7SToomas Soome case release_request: err = handle_release_request (req); break;
4864c65ebfc7SToomas Soome default: LogMsg("request_callback: %3d:ERROR: Unsupported UDS req:%d PID[%d][%s]",
4865c65ebfc7SToomas Soome req->sd, req->hdr.op, req->process_id, req->pid_name);
4866c65ebfc7SToomas Soome err = mStatus_BadParamErr;
4867c65ebfc7SToomas Soome break;
4868c65ebfc7SToomas Soome }
4869c65ebfc7SToomas Soome
4870c65ebfc7SToomas Soome return err;
4871c65ebfc7SToomas Soome }
4872c65ebfc7SToomas Soome
4873c65ebfc7SToomas Soome #define RecordOrientedOp(X) \
4874c65ebfc7SToomas Soome ((X) == reg_record_request || (X) == add_record_request || (X) == update_record_request || (X) == remove_record_request)
4875c65ebfc7SToomas Soome
4876c65ebfc7SToomas Soome // The lightweight operations are the ones that don't need a dedicated request_state structure allocated for them
4877c65ebfc7SToomas Soome #define LightweightOp(X) (RecordOrientedOp(X) || (X) == cancel_request)
4878c65ebfc7SToomas Soome
request_callback(int fd,void * info)4879*472cd20dSToomas Soome mDNSlocal void request_callback(int fd, void *info)
4880c65ebfc7SToomas Soome {
4881c65ebfc7SToomas Soome mStatus err = 0;
4882c65ebfc7SToomas Soome request_state *req = info;
4883c65ebfc7SToomas Soome mDNSs32 min_size = sizeof(DNSServiceFlags);
4884c65ebfc7SToomas Soome (void)fd; // Unused
4885c65ebfc7SToomas Soome
4886c65ebfc7SToomas Soome for (;;)
4887c65ebfc7SToomas Soome {
4888c65ebfc7SToomas Soome read_msg(req);
4889c65ebfc7SToomas Soome if (req->ts == t_morecoming)
4890c65ebfc7SToomas Soome return;
4891c65ebfc7SToomas Soome if (req->ts == t_terminated || req->ts == t_error)
4892c65ebfc7SToomas Soome {
4893c65ebfc7SToomas Soome AbortUnlinkAndFree(req);
4894c65ebfc7SToomas Soome return;
4895c65ebfc7SToomas Soome }
4896c65ebfc7SToomas Soome if (req->ts != t_complete)
4897c65ebfc7SToomas Soome {
4898c65ebfc7SToomas Soome LogMsg("request_callback: req->ts %d != t_complete PID[%d][%s]", req->ts, req->process_id, req->pid_name);
4899c65ebfc7SToomas Soome AbortUnlinkAndFree(req);
4900c65ebfc7SToomas Soome return;
4901c65ebfc7SToomas Soome }
4902c65ebfc7SToomas Soome
4903c65ebfc7SToomas Soome switch(req->hdr.op) // Interface + other data
4904c65ebfc7SToomas Soome {
4905c65ebfc7SToomas Soome case connection_request: min_size = 0; break;
4906c65ebfc7SToomas Soome case connection_delegate_request: min_size = 4; /* pid */ break;
4907c65ebfc7SToomas Soome case reg_service_request: min_size += sizeof(mDNSu32) + 4 /* name, type, domain, host */ + 4 /* port, textlen */; break;
4908c65ebfc7SToomas Soome case add_record_request: min_size += 4 /* type, rdlen */ + 4 /* ttl */; break;
4909c65ebfc7SToomas Soome case update_record_request: min_size += 2 /* rdlen */ + 4 /* ttl */; break;
4910c65ebfc7SToomas Soome case remove_record_request: break;
4911c65ebfc7SToomas Soome case browse_request: min_size += sizeof(mDNSu32) + 2 /* type, domain */; break;
4912c65ebfc7SToomas Soome case resolve_request: min_size += sizeof(mDNSu32) + 3 /* type, type, domain */; break;
4913c65ebfc7SToomas Soome case query_request: min_size += sizeof(mDNSu32) + 1 /* name */ + 4 /* type, class*/; break;
4914c65ebfc7SToomas Soome case enumeration_request: min_size += sizeof(mDNSu32); break;
4915c65ebfc7SToomas Soome case reg_record_request: min_size += sizeof(mDNSu32) + 1 /* name */ + 6 /* type, class, rdlen */ + 4 /* ttl */; break;
4916c65ebfc7SToomas Soome case reconfirm_record_request: min_size += sizeof(mDNSu32) + 1 /* name */ + 6 /* type, class, rdlen */; break;
4917c65ebfc7SToomas Soome case setdomain_request: min_size += 1 /* domain */; break;
4918c65ebfc7SToomas Soome case getproperty_request: min_size = 2; break;
4919c65ebfc7SToomas Soome case port_mapping_request: min_size += sizeof(mDNSu32) + 4 /* udp/tcp */ + 4 /* int/ext port */ + 4 /* ttl */; break;
4920c65ebfc7SToomas Soome case addrinfo_request: min_size += sizeof(mDNSu32) + 4 /* v4/v6 */ + 1 /* hostname */; break;
4921c65ebfc7SToomas Soome case send_bpf: // Same as cancel_request below
4922c65ebfc7SToomas Soome case cancel_request: min_size = 0; break;
4923c65ebfc7SToomas Soome case release_request: min_size += sizeof(mDNSu32) + 3 /* type, type, domain */; break;
4924c65ebfc7SToomas Soome default: LogMsg("request_callback: ERROR: validate_message - unsupported req type: %d PID[%d][%s]",
4925c65ebfc7SToomas Soome req->hdr.op, req->process_id, req->pid_name);
4926c65ebfc7SToomas Soome min_size = -1; break;
4927c65ebfc7SToomas Soome }
4928c65ebfc7SToomas Soome
4929c65ebfc7SToomas Soome if ((mDNSs32)req->data_bytes < min_size)
4930c65ebfc7SToomas Soome {
4931c65ebfc7SToomas Soome LogMsg("request_callback: Invalid message %d bytes; min for %d is %d PID[%d][%s]",
4932c65ebfc7SToomas Soome req->data_bytes, req->hdr.op, min_size, req->process_id, req->pid_name);
4933c65ebfc7SToomas Soome AbortUnlinkAndFree(req);
4934c65ebfc7SToomas Soome return;
4935c65ebfc7SToomas Soome }
4936c65ebfc7SToomas Soome if (LightweightOp(req->hdr.op) && !req->terminate)
4937c65ebfc7SToomas Soome {
4938c65ebfc7SToomas Soome LogMsg("request_callback: Reg/Add/Update/Remove %d require existing connection PID[%d][%s]",
4939c65ebfc7SToomas Soome req->hdr.op, req->process_id, req->pid_name);
4940c65ebfc7SToomas Soome AbortUnlinkAndFree(req);
4941c65ebfc7SToomas Soome return;
4942c65ebfc7SToomas Soome }
4943c65ebfc7SToomas Soome
4944c65ebfc7SToomas Soome // If req->terminate is already set, this means this operation is sharing an existing connection
4945c65ebfc7SToomas Soome if (req->terminate && !LightweightOp(req->hdr.op))
4946c65ebfc7SToomas Soome {
4947c65ebfc7SToomas Soome request_state *newreq = NewRequest();
4948c65ebfc7SToomas Soome newreq->primary = req;
4949c65ebfc7SToomas Soome newreq->sd = req->sd;
4950c65ebfc7SToomas Soome newreq->errsd = req->errsd;
4951c65ebfc7SToomas Soome newreq->uid = req->uid;
4952c65ebfc7SToomas Soome newreq->hdr = req->hdr;
4953c65ebfc7SToomas Soome newreq->msgbuf = req->msgbuf;
4954c65ebfc7SToomas Soome newreq->msgptr = req->msgptr;
4955c65ebfc7SToomas Soome newreq->msgend = req->msgend;
4956*472cd20dSToomas Soome newreq->request_id = GetNewRequestID();
4957*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, AUDIT_TOKEN)
4958*472cd20dSToomas Soome newreq->audit_token = req->audit_token;
4959*472cd20dSToomas Soome #endif
4960c65ebfc7SToomas Soome // if the parent request is a delegate connection, copy the
4961c65ebfc7SToomas Soome // relevant bits
4962c65ebfc7SToomas Soome if (req->validUUID)
4963c65ebfc7SToomas Soome {
4964c65ebfc7SToomas Soome newreq->validUUID = mDNStrue;
4965c65ebfc7SToomas Soome mDNSPlatformMemCopy(newreq->uuid, req->uuid, UUID_SIZE);
4966c65ebfc7SToomas Soome }
4967c65ebfc7SToomas Soome else
4968c65ebfc7SToomas Soome {
4969c65ebfc7SToomas Soome if (req->process_id)
4970c65ebfc7SToomas Soome {
4971c65ebfc7SToomas Soome newreq->process_id = req->process_id;
4972c65ebfc7SToomas Soome mDNSPlatformStrLCopy(newreq->pid_name, req->pid_name, (mDNSu32)sizeof(newreq->pid_name));
4973c65ebfc7SToomas Soome }
4974c65ebfc7SToomas Soome else
4975c65ebfc7SToomas Soome {
4976c65ebfc7SToomas Soome set_peer_pid(newreq);
4977c65ebfc7SToomas Soome }
4978c65ebfc7SToomas Soome }
4979c65ebfc7SToomas Soome req = newreq;
4980c65ebfc7SToomas Soome }
4981c65ebfc7SToomas Soome
4982c65ebfc7SToomas Soome // Check if the request wants no asynchronous replies.
4983c65ebfc7SToomas Soome if (req->hdr.ipc_flags & IPC_FLAGS_NOREPLY) req->no_reply = 1;
4984c65ebfc7SToomas Soome
4985c65ebfc7SToomas Soome // If we're shutting down, don't allow new client requests
4986c65ebfc7SToomas Soome // We do allow "cancel" and "getproperty" during shutdown
4987c65ebfc7SToomas Soome if (mDNSStorage.ShutdownTime && req->hdr.op != cancel_request && req->hdr.op != getproperty_request)
4988c65ebfc7SToomas Soome err = mStatus_ServiceNotRunning;
4989c65ebfc7SToomas Soome else
4990c65ebfc7SToomas Soome err = handle_client_request(req);
4991c65ebfc7SToomas Soome
4992c65ebfc7SToomas Soome // req->msgbuf may be NULL, e.g. for connection_request or remove_record_request
4993c65ebfc7SToomas Soome if (req->msgbuf) freeL("request_state msgbuf", req->msgbuf);
4994c65ebfc7SToomas Soome
4995c65ebfc7SToomas Soome // There's no return data for a cancel request (DNSServiceRefDeallocate returns no result)
4996c65ebfc7SToomas Soome // For a DNSServiceGetProperty call, the handler already generated the response, so no need to do it again here
4997c65ebfc7SToomas Soome if (req->hdr.op != cancel_request && req->hdr.op != getproperty_request && req->hdr.op != send_bpf && req->hdr.op != getpid_request)
4998c65ebfc7SToomas Soome {
4999c65ebfc7SToomas Soome const mStatus err_netorder = dnssd_htonl(err);
5000c65ebfc7SToomas Soome send_all(req->errsd, (const char *)&err_netorder, sizeof(err_netorder));
5001c65ebfc7SToomas Soome if (req->errsd != req->sd)
5002c65ebfc7SToomas Soome {
5003c65ebfc7SToomas Soome dnssd_close(req->errsd);
5004c65ebfc7SToomas Soome req->errsd = req->sd;
5005c65ebfc7SToomas Soome // Also need to reset the parent's errsd, if this is a subordinate operation
5006c65ebfc7SToomas Soome if (req->primary) req->primary->errsd = req->primary->sd;
5007c65ebfc7SToomas Soome }
5008c65ebfc7SToomas Soome }
5009c65ebfc7SToomas Soome
5010c65ebfc7SToomas Soome // Reset ready to accept the next req on this pipe
5011c65ebfc7SToomas Soome if (req->primary) req = req->primary;
5012c65ebfc7SToomas Soome req->ts = t_morecoming;
5013c65ebfc7SToomas Soome req->hdr_bytes = 0;
5014c65ebfc7SToomas Soome req->data_bytes = 0;
5015c65ebfc7SToomas Soome req->msgbuf = mDNSNULL;
5016c65ebfc7SToomas Soome req->msgptr = mDNSNULL;
5017c65ebfc7SToomas Soome req->msgend = 0;
5018c65ebfc7SToomas Soome }
5019c65ebfc7SToomas Soome }
5020c65ebfc7SToomas Soome
connect_callback(int fd,void * info)5021*472cd20dSToomas Soome mDNSlocal void connect_callback(int fd, void *info)
5022c65ebfc7SToomas Soome {
5023c65ebfc7SToomas Soome dnssd_sockaddr_t cliaddr;
5024c65ebfc7SToomas Soome dnssd_socklen_t len = (dnssd_socklen_t) sizeof(cliaddr);
5025c65ebfc7SToomas Soome dnssd_sock_t sd = accept(fd, (struct sockaddr*) &cliaddr, &len);
5026c65ebfc7SToomas Soome #if defined(SO_NOSIGPIPE) || defined(_WIN32)
5027c65ebfc7SToomas Soome unsigned long optval = 1;
5028c65ebfc7SToomas Soome #endif
5029c65ebfc7SToomas Soome
5030c65ebfc7SToomas Soome (void)info; // Unused
5031c65ebfc7SToomas Soome
5032c65ebfc7SToomas Soome if (!dnssd_SocketValid(sd))
5033c65ebfc7SToomas Soome {
5034c65ebfc7SToomas Soome if (dnssd_errno != dnssd_EWOULDBLOCK)
5035c65ebfc7SToomas Soome my_throttled_perror("ERROR: accept");
5036c65ebfc7SToomas Soome return;
5037c65ebfc7SToomas Soome }
5038c65ebfc7SToomas Soome
5039c65ebfc7SToomas Soome #ifdef SO_NOSIGPIPE
5040c65ebfc7SToomas Soome // Some environments (e.g. OS X) support turning off SIGPIPE for a socket
5041c65ebfc7SToomas Soome if (setsockopt(sd, SOL_SOCKET, SO_NOSIGPIPE, &optval, sizeof(optval)) < 0)
5042c65ebfc7SToomas Soome LogMsg("%3d: WARNING: setsockopt - SO_NOSIGPIPE %d (%s)", sd, dnssd_errno, dnssd_strerror(dnssd_errno));
5043c65ebfc7SToomas Soome #endif
5044c65ebfc7SToomas Soome
5045c65ebfc7SToomas Soome #if defined(_WIN32)
5046c65ebfc7SToomas Soome if (ioctlsocket(sd, FIONBIO, &optval) != 0)
5047c65ebfc7SToomas Soome #else
5048c65ebfc7SToomas Soome if (fcntl(sd, F_SETFL, fcntl(sd, F_GETFL, 0) | O_NONBLOCK) != 0)
5049c65ebfc7SToomas Soome #endif
5050c65ebfc7SToomas Soome {
5051c65ebfc7SToomas Soome my_perror("ERROR: fcntl(sd, F_SETFL, O_NONBLOCK) - aborting client");
5052c65ebfc7SToomas Soome dnssd_close(sd);
5053c65ebfc7SToomas Soome return;
5054c65ebfc7SToomas Soome }
5055c65ebfc7SToomas Soome else
5056c65ebfc7SToomas Soome {
5057c65ebfc7SToomas Soome request_state *request = NewRequest();
5058c65ebfc7SToomas Soome request->ts = t_morecoming;
5059c65ebfc7SToomas Soome request->sd = sd;
5060c65ebfc7SToomas Soome request->errsd = sd;
5061*472cd20dSToomas Soome request->request_id = GetNewRequestID();
5062c65ebfc7SToomas Soome set_peer_pid(request);
5063c65ebfc7SToomas Soome #if APPLE_OSX_mDNSResponder
5064c65ebfc7SToomas Soome struct xucred x;
5065c65ebfc7SToomas Soome socklen_t xucredlen = sizeof(x);
5066c65ebfc7SToomas Soome if (getsockopt(sd, 0, LOCAL_PEERCRED, &x, &xucredlen) >= 0 && x.cr_version == XUCRED_VERSION)
5067c65ebfc7SToomas Soome request->uid = x.cr_uid; // save the effective userid of the client
5068c65ebfc7SToomas Soome else
5069c65ebfc7SToomas Soome my_perror("ERROR: getsockopt, LOCAL_PEERCRED");
5070c65ebfc7SToomas Soome debugf("LOCAL_PEERCRED %d %u %u %d", xucredlen, x.cr_version, x.cr_uid, x.cr_ngroups);
5071c65ebfc7SToomas Soome #endif // APPLE_OSX_mDNSResponder
50723b436d06SToomas Soome LogDebug("%3d: connect_callback: Adding FD for uid %u", request->sd, request->uid);
5073c65ebfc7SToomas Soome udsSupportAddFDToEventLoop(sd, request_callback, request, &request->platform_data);
5074c65ebfc7SToomas Soome }
5075c65ebfc7SToomas Soome }
5076c65ebfc7SToomas Soome
uds_socket_setup(dnssd_sock_t skt)5077c65ebfc7SToomas Soome mDNSlocal mDNSBool uds_socket_setup(dnssd_sock_t skt)
5078c65ebfc7SToomas Soome {
5079c65ebfc7SToomas Soome #if defined(SO_NP_EXTENSIONS)
5080c65ebfc7SToomas Soome struct so_np_extensions sonpx;
5081c65ebfc7SToomas Soome socklen_t optlen = sizeof(struct so_np_extensions);
5082c65ebfc7SToomas Soome sonpx.npx_flags = SONPX_SETOPTSHUT;
5083c65ebfc7SToomas Soome sonpx.npx_mask = SONPX_SETOPTSHUT;
5084c65ebfc7SToomas Soome if (setsockopt(skt, SOL_SOCKET, SO_NP_EXTENSIONS, &sonpx, optlen) < 0)
5085c65ebfc7SToomas Soome my_perror("WARNING: could not set sockopt - SO_NP_EXTENSIONS");
5086c65ebfc7SToomas Soome #endif
5087c65ebfc7SToomas Soome #if defined(_WIN32)
5088c65ebfc7SToomas Soome // SEH: do we even need to do this on windows?
5089c65ebfc7SToomas Soome // This socket will be given to WSAEventSelect which will automatically set it to non-blocking
5090c65ebfc7SToomas Soome u_long opt = 1;
5091c65ebfc7SToomas Soome if (ioctlsocket(skt, FIONBIO, &opt) != 0)
5092c65ebfc7SToomas Soome #else
5093c65ebfc7SToomas Soome if (fcntl(skt, F_SETFL, fcntl(skt, F_GETFL, 0) | O_NONBLOCK) != 0)
5094c65ebfc7SToomas Soome #endif
5095c65ebfc7SToomas Soome {
5096c65ebfc7SToomas Soome my_perror("ERROR: could not set listen socket to non-blocking mode");
5097c65ebfc7SToomas Soome return mDNSfalse;
5098c65ebfc7SToomas Soome }
5099c65ebfc7SToomas Soome
5100c65ebfc7SToomas Soome if (listen(skt, LISTENQ) != 0)
5101c65ebfc7SToomas Soome {
5102c65ebfc7SToomas Soome my_perror("ERROR: could not listen on listen socket");
5103c65ebfc7SToomas Soome return mDNSfalse;
5104c65ebfc7SToomas Soome }
5105c65ebfc7SToomas Soome
5106c65ebfc7SToomas Soome if (mStatus_NoError != udsSupportAddFDToEventLoop(skt, connect_callback, (void *) NULL, (void **) NULL))
5107c65ebfc7SToomas Soome {
5108c65ebfc7SToomas Soome my_perror("ERROR: could not add listen socket to event loop");
5109c65ebfc7SToomas Soome return mDNSfalse;
5110c65ebfc7SToomas Soome }
5111c65ebfc7SToomas Soome else
5112c65ebfc7SToomas Soome {
5113c65ebfc7SToomas Soome LogOperation("%3d: Listening for incoming Unix Domain Socket client requests", skt);
5114c65ebfc7SToomas Soome mDNSStorage.uds_listener_skt = skt;
5115c65ebfc7SToomas Soome }
5116c65ebfc7SToomas Soome return mDNStrue;
5117c65ebfc7SToomas Soome }
5118c65ebfc7SToomas Soome
5119*472cd20dSToomas Soome #if MDNS_MALLOC_DEBUGGING
5120*472cd20dSToomas Soome mDNSlocal void udsserver_validatelists(void *context);
5121*472cd20dSToomas Soome #endif
5122*472cd20dSToomas Soome
udsserver_init(dnssd_sock_t skts[],const size_t count)5123*472cd20dSToomas Soome mDNSexport int udsserver_init(dnssd_sock_t skts[], const size_t count)
5124c65ebfc7SToomas Soome {
5125c65ebfc7SToomas Soome dnssd_sockaddr_t laddr;
5126c65ebfc7SToomas Soome int ret;
5127c65ebfc7SToomas Soome
5128*472cd20dSToomas Soome #ifndef NO_PID_FILE
5129c65ebfc7SToomas Soome FILE *fp = fopen(PID_FILE, "w");
5130c65ebfc7SToomas Soome if (fp != NULL)
5131c65ebfc7SToomas Soome {
5132*472cd20dSToomas Soome fprintf(fp, "%d\n", getpid());
5133c65ebfc7SToomas Soome fclose(fp);
5134c65ebfc7SToomas Soome }
5135*472cd20dSToomas Soome #endif
5136*472cd20dSToomas Soome
5137*472cd20dSToomas Soome #if MDNS_MALLOC_DEBUGGING
5138*472cd20dSToomas Soome static mDNSListValidator validator;
5139*472cd20dSToomas Soome mDNSPlatformAddListValidator(&validator, udsserver_validatelists, "udsserver_validatelists", NULL);
5140*472cd20dSToomas Soome #endif
5141c65ebfc7SToomas Soome
5142c65ebfc7SToomas Soome if (skts)
5143c65ebfc7SToomas Soome {
5144*472cd20dSToomas Soome size_t i;
5145c65ebfc7SToomas Soome for (i = 0; i < count; i++)
5146c65ebfc7SToomas Soome if (dnssd_SocketValid(skts[i]) && !uds_socket_setup(skts[i]))
5147c65ebfc7SToomas Soome goto error;
5148c65ebfc7SToomas Soome }
5149c65ebfc7SToomas Soome else
5150c65ebfc7SToomas Soome {
5151c65ebfc7SToomas Soome listenfd = socket(AF_DNSSD, SOCK_STREAM, 0);
5152c65ebfc7SToomas Soome if (!dnssd_SocketValid(listenfd))
5153c65ebfc7SToomas Soome {
5154c65ebfc7SToomas Soome my_perror("ERROR: socket(AF_DNSSD, SOCK_STREAM, 0); failed");
5155c65ebfc7SToomas Soome goto error;
5156c65ebfc7SToomas Soome }
5157c65ebfc7SToomas Soome
5158c65ebfc7SToomas Soome mDNSPlatformMemZero(&laddr, sizeof(laddr));
5159c65ebfc7SToomas Soome
5160c65ebfc7SToomas Soome #if defined(USE_TCP_LOOPBACK)
5161c65ebfc7SToomas Soome {
5162c65ebfc7SToomas Soome laddr.sin_family = AF_INET;
5163c65ebfc7SToomas Soome laddr.sin_port = htons(MDNS_TCP_SERVERPORT);
5164c65ebfc7SToomas Soome laddr.sin_addr.s_addr = inet_addr(MDNS_TCP_SERVERADDR);
5165c65ebfc7SToomas Soome ret = bind(listenfd, (struct sockaddr *) &laddr, sizeof(laddr));
5166c65ebfc7SToomas Soome if (ret < 0)
5167c65ebfc7SToomas Soome {
5168c65ebfc7SToomas Soome my_perror("ERROR: bind(listenfd, (struct sockaddr *) &laddr, sizeof(laddr)); failed");
5169c65ebfc7SToomas Soome goto error;
5170c65ebfc7SToomas Soome }
5171c65ebfc7SToomas Soome }
5172c65ebfc7SToomas Soome #else
5173c65ebfc7SToomas Soome {
5174c65ebfc7SToomas Soome mode_t mask = umask(0);
5175c65ebfc7SToomas Soome unlink(boundPath); // OK if this fails
5176c65ebfc7SToomas Soome laddr.sun_family = AF_LOCAL;
5177c65ebfc7SToomas Soome #ifndef NOT_HAVE_SA_LEN
5178c65ebfc7SToomas Soome // According to Stevens (section 3.2), there is no portable way to
5179c65ebfc7SToomas Soome // determine whether sa_len is defined on a particular platform.
5180c65ebfc7SToomas Soome laddr.sun_len = sizeof(struct sockaddr_un);
5181c65ebfc7SToomas Soome #endif
5182c65ebfc7SToomas Soome if (strlen(boundPath) >= sizeof(laddr.sun_path))
5183c65ebfc7SToomas Soome {
5184c65ebfc7SToomas Soome LogMsg("ERROR: MDNS_UDS_SERVERPATH must be < %d characters", (int)sizeof(laddr.sun_path));
5185c65ebfc7SToomas Soome goto error;
5186c65ebfc7SToomas Soome }
5187c65ebfc7SToomas Soome mDNSPlatformStrLCopy(laddr.sun_path, boundPath, sizeof(laddr.sun_path));
5188c65ebfc7SToomas Soome ret = bind(listenfd, (struct sockaddr *) &laddr, sizeof(laddr));
5189c65ebfc7SToomas Soome umask(mask);
5190c65ebfc7SToomas Soome if (ret < 0)
5191c65ebfc7SToomas Soome {
5192c65ebfc7SToomas Soome my_perror("ERROR: bind(listenfd, (struct sockaddr *) &laddr, sizeof(laddr)); failed");
5193c65ebfc7SToomas Soome goto error;
5194c65ebfc7SToomas Soome }
5195c65ebfc7SToomas Soome }
5196c65ebfc7SToomas Soome #endif
5197c65ebfc7SToomas Soome
5198c65ebfc7SToomas Soome if (!uds_socket_setup(listenfd)) goto error;
5199c65ebfc7SToomas Soome }
5200c65ebfc7SToomas Soome
5201c65ebfc7SToomas Soome #if !defined(PLATFORM_NO_RLIMIT)
5202c65ebfc7SToomas Soome {
5203c65ebfc7SToomas Soome // Set maximum number of open file descriptors
5204c65ebfc7SToomas Soome #define MIN_OPENFILES 10240
5205c65ebfc7SToomas Soome struct rlimit maxfds, newfds;
5206c65ebfc7SToomas Soome
5207c65ebfc7SToomas Soome // Due to bugs in OS X (<rdar://problem/2941095>, <rdar://problem/3342704>, <rdar://problem/3839173>)
5208c65ebfc7SToomas Soome // you have to get and set rlimits once before getrlimit will return sensible values
5209c65ebfc7SToomas Soome if (getrlimit(RLIMIT_NOFILE, &maxfds) < 0) { my_perror("ERROR: Unable to get file descriptor limit"); return 0; }
5210c65ebfc7SToomas Soome if (setrlimit(RLIMIT_NOFILE, &maxfds) < 0) my_perror("ERROR: Unable to set maximum file descriptor limit");
5211c65ebfc7SToomas Soome
5212c65ebfc7SToomas Soome if (getrlimit(RLIMIT_NOFILE, &maxfds) < 0) { my_perror("ERROR: Unable to get file descriptor limit"); return 0; }
5213c65ebfc7SToomas Soome newfds.rlim_max = (maxfds.rlim_max > MIN_OPENFILES) ? maxfds.rlim_max : MIN_OPENFILES;
5214c65ebfc7SToomas Soome newfds.rlim_cur = (maxfds.rlim_cur > MIN_OPENFILES) ? maxfds.rlim_cur : MIN_OPENFILES;
5215c65ebfc7SToomas Soome if (newfds.rlim_max != maxfds.rlim_max || newfds.rlim_cur != maxfds.rlim_cur)
5216c65ebfc7SToomas Soome if (setrlimit(RLIMIT_NOFILE, &newfds) < 0) my_perror("ERROR: Unable to set maximum file descriptor limit");
5217c65ebfc7SToomas Soome
5218c65ebfc7SToomas Soome if (getrlimit(RLIMIT_NOFILE, &maxfds) < 0) { my_perror("ERROR: Unable to get file descriptor limit"); return 0; }
5219c65ebfc7SToomas Soome debugf("maxfds.rlim_max %d", (long)maxfds.rlim_max);
5220c65ebfc7SToomas Soome debugf("maxfds.rlim_cur %d", (long)maxfds.rlim_cur);
5221c65ebfc7SToomas Soome }
5222c65ebfc7SToomas Soome #endif
5223c65ebfc7SToomas Soome
5224c65ebfc7SToomas Soome // We start a "LocalOnly" query looking for Automatic Browse Domain records.
5225c65ebfc7SToomas Soome // When Domain Enumeration in uDNS.c finds an "lb" record from the network, its "FoundDomain" routine
5226c65ebfc7SToomas Soome // creates a "LocalOnly" record, which results in our AutomaticBrowseDomainChange callback being invoked
5227c65ebfc7SToomas Soome mDNS_GetDomains(&mDNSStorage, &mDNSStorage.AutomaticBrowseDomainQ, mDNS_DomainTypeBrowseAutomatic,
5228c65ebfc7SToomas Soome mDNSNULL, mDNSInterface_LocalOnly, AutomaticBrowseDomainChange, mDNSNULL);
5229c65ebfc7SToomas Soome
5230c65ebfc7SToomas Soome // Add "local" as recommended registration domain ("dns-sd -E"), recommended browsing domain ("dns-sd -F"), and automatic browsing domain
5231c65ebfc7SToomas Soome RegisterLocalOnlyDomainEnumPTR(&mDNSStorage, &localdomain, mDNS_DomainTypeRegistration);
5232c65ebfc7SToomas Soome RegisterLocalOnlyDomainEnumPTR(&mDNSStorage, &localdomain, mDNS_DomainTypeBrowse);
5233c65ebfc7SToomas Soome AddAutoBrowseDomain(0, &localdomain);
5234c65ebfc7SToomas Soome
5235c65ebfc7SToomas Soome udsserver_handle_configchange(&mDNSStorage);
5236c65ebfc7SToomas Soome return 0;
5237c65ebfc7SToomas Soome
5238c65ebfc7SToomas Soome error:
5239c65ebfc7SToomas Soome
5240c65ebfc7SToomas Soome my_perror("ERROR: udsserver_init");
5241c65ebfc7SToomas Soome return -1;
5242c65ebfc7SToomas Soome }
5243c65ebfc7SToomas Soome
udsserver_exit(void)5244c65ebfc7SToomas Soome mDNSexport int udsserver_exit(void)
5245c65ebfc7SToomas Soome {
5246c65ebfc7SToomas Soome // Cancel all outstanding client requests
5247c65ebfc7SToomas Soome while (all_requests) AbortUnlinkAndFree(all_requests);
5248c65ebfc7SToomas Soome
5249c65ebfc7SToomas Soome // Clean up any special mDNSInterface_LocalOnly records we created, both the entries for "local" we
5250c65ebfc7SToomas Soome // created in udsserver_init, and others we created as a result of reading local configuration data
5251c65ebfc7SToomas Soome while (LocalDomainEnumRecords)
5252c65ebfc7SToomas Soome {
5253c65ebfc7SToomas Soome ARListElem *rem = LocalDomainEnumRecords;
5254c65ebfc7SToomas Soome LocalDomainEnumRecords = LocalDomainEnumRecords->next;
5255c65ebfc7SToomas Soome mDNS_Deregister(&mDNSStorage, &rem->ar);
5256c65ebfc7SToomas Soome }
5257c65ebfc7SToomas Soome
5258c65ebfc7SToomas Soome // If the launching environment created no listening socket,
5259c65ebfc7SToomas Soome // that means we created it ourselves, so we should clean it up on exit
5260c65ebfc7SToomas Soome if (dnssd_SocketValid(listenfd))
5261c65ebfc7SToomas Soome {
5262c65ebfc7SToomas Soome dnssd_close(listenfd);
5263c65ebfc7SToomas Soome #if !defined(USE_TCP_LOOPBACK)
5264c65ebfc7SToomas Soome // Currently, we're unable to remove /var/run/mdnsd because we've changed to userid "nobody"
5265c65ebfc7SToomas Soome // to give up unnecessary privilege, but we need to be root to remove this Unix Domain Socket.
5266c65ebfc7SToomas Soome // It would be nice if we could find a solution to this problem
5267c65ebfc7SToomas Soome if (unlink(boundPath))
5268c65ebfc7SToomas Soome debugf("Unable to remove %s", MDNS_UDS_SERVERPATH);
5269c65ebfc7SToomas Soome #endif
5270c65ebfc7SToomas Soome }
5271c65ebfc7SToomas Soome
5272*472cd20dSToomas Soome #ifndef NO_PID_FILE
5273*472cd20dSToomas Soome unlink(PID_FILE);
5274*472cd20dSToomas Soome #endif
5275c65ebfc7SToomas Soome
5276c65ebfc7SToomas Soome return 0;
5277c65ebfc7SToomas Soome }
5278c65ebfc7SToomas Soome
LogClientInfoToFD(int fd,request_state * req)5279*472cd20dSToomas Soome mDNSlocal void LogClientInfoToFD(int fd, request_state *req)
5280*472cd20dSToomas Soome {
5281*472cd20dSToomas Soome char reqIDStr[14];
5282*472cd20dSToomas Soome char prefix[18];
5283*472cd20dSToomas Soome
5284*472cd20dSToomas Soome mDNS_snprintf(reqIDStr, sizeof(reqIDStr), "[R%u]", req->request_id);
5285*472cd20dSToomas Soome
5286*472cd20dSToomas Soome mDNS_snprintf(prefix, sizeof(prefix), "%-6s %2s", reqIDStr, req->primary ? "->" : "");
5287*472cd20dSToomas Soome
5288*472cd20dSToomas Soome if (!req->terminate)
5289*472cd20dSToomas Soome LogToFD(fd, "%s No operation yet on this socket", prefix);
5290*472cd20dSToomas Soome else if (req->terminate == connection_termination)
5291*472cd20dSToomas Soome {
5292*472cd20dSToomas Soome int num_records = 0, num_ops = 0;
5293*472cd20dSToomas Soome const registered_record_entry *p;
5294*472cd20dSToomas Soome request_state *r;
5295*472cd20dSToomas Soome for (p = req->u.reg_recs; p; p=p->next) num_records++;
5296*472cd20dSToomas Soome for (r = req->next; r; r=r->next) if (r->primary == req) num_ops++;
5297*472cd20dSToomas Soome LogToFD(fd, "%s DNSServiceCreateConnection: %d registered record%s, %d kDNSServiceFlagsShareConnection operation%s PID[%d](%s)",
5298*472cd20dSToomas Soome prefix, num_records, num_records != 1 ? "s" : "", num_ops, num_ops != 1 ? "s" : "",
5299*472cd20dSToomas Soome req->process_id, req->pid_name);
5300*472cd20dSToomas Soome for (p = req->u.reg_recs; p; p=p->next)
5301*472cd20dSToomas Soome LogToFD(fd, " -> DNSServiceRegisterRecord 0x%08X %2d %3d %s PID[%d](%s)",
5302*472cd20dSToomas Soome req->flags, req->interfaceIndex, p->key, ARDisplayString(&mDNSStorage, p->rr), req->process_id, req->pid_name);
5303*472cd20dSToomas Soome for (r = req->next; r; r=r->next) if (r->primary == req) LogClientInfoToFD(fd, r);
5304*472cd20dSToomas Soome }
5305*472cd20dSToomas Soome else if (req->terminate == regservice_termination_callback)
5306*472cd20dSToomas Soome {
5307*472cd20dSToomas Soome service_instance *ptr;
5308*472cd20dSToomas Soome for (ptr = req->u.servicereg.instances; ptr; ptr = ptr->next)
5309*472cd20dSToomas Soome LogToFD(fd, "%-9s DNSServiceRegister 0x%08X %2d %##s %u/%u PID[%d](%s)",
5310*472cd20dSToomas Soome (ptr == req->u.servicereg.instances) ? prefix : "", req->flags, req->interfaceIndex, ptr->srs.RR_SRV.resrec.name->c,
5311*472cd20dSToomas Soome mDNSVal16(req->u.servicereg.port),
5312*472cd20dSToomas Soome SRS_PORT(&ptr->srs), req->process_id, req->pid_name);
5313*472cd20dSToomas Soome }
5314*472cd20dSToomas Soome else if (req->terminate == browse_termination_callback)
5315*472cd20dSToomas Soome {
5316*472cd20dSToomas Soome browser_t *blist;
5317*472cd20dSToomas Soome for (blist = req->u.browser.browsers; blist; blist = blist->next)
5318*472cd20dSToomas Soome LogToFD(fd, "%-9s DNSServiceBrowse 0x%08X %2d %##s PID[%d](%s)",
5319*472cd20dSToomas Soome (blist == req->u.browser.browsers) ? prefix : "", req->flags, req->interfaceIndex, blist->q.qname.c,
5320*472cd20dSToomas Soome req->process_id, req->pid_name);
5321*472cd20dSToomas Soome }
5322*472cd20dSToomas Soome else if (req->terminate == resolve_termination_callback)
5323*472cd20dSToomas Soome LogToFD(fd, "%s DNSServiceResolve 0x%08X %2d %##s PID[%d](%s)",
5324*472cd20dSToomas Soome prefix, req->flags, req->interfaceIndex, req->u.resolve.qsrv.qname.c, req->process_id, req->pid_name);
5325*472cd20dSToomas Soome else if (req->terminate == queryrecord_termination_callback)
5326*472cd20dSToomas Soome LogToFD(fd, "%s DNSServiceQueryRecord 0x%08X %2d %##s (%s) PID[%d](%s)",
5327*472cd20dSToomas Soome prefix, req->flags, req->interfaceIndex, QueryRecordClientRequestGetQName(&req->u.queryrecord), DNSTypeName(QueryRecordClientRequestGetType(&req->u.queryrecord)), req->process_id, req->pid_name);
5328*472cd20dSToomas Soome else if (req->terminate == enum_termination_callback)
5329*472cd20dSToomas Soome LogToFD(fd, "%s DNSServiceEnumerateDomains 0x%08X %2d %##s PID[%d](%s)",
5330*472cd20dSToomas Soome prefix, req->flags, req->interfaceIndex, req->u.enumeration.q_all.qname.c, req->process_id, req->pid_name);
5331*472cd20dSToomas Soome else if (req->terminate == port_mapping_termination_callback)
5332*472cd20dSToomas Soome LogToFD(fd, "%s DNSServiceNATPortMapping 0x%08X %2d %s%s Int %5d Req %5d Ext %.4a:%5d Req TTL %5d Granted TTL %5d PID[%d](%s)",
5333*472cd20dSToomas Soome prefix,
5334*472cd20dSToomas Soome req->flags,
5335*472cd20dSToomas Soome req->interfaceIndex,
5336*472cd20dSToomas Soome req->u.pm.NATinfo.Protocol & NATOp_MapTCP ? "TCP" : " ",
5337*472cd20dSToomas Soome req->u.pm.NATinfo.Protocol & NATOp_MapUDP ? "UDP" : " ",
5338*472cd20dSToomas Soome mDNSVal16(req->u.pm.NATinfo.IntPort),
5339*472cd20dSToomas Soome mDNSVal16(req->u.pm.ReqExt),
5340*472cd20dSToomas Soome &req->u.pm.NATinfo.ExternalAddress,
5341*472cd20dSToomas Soome mDNSVal16(req->u.pm.NATinfo.ExternalPort),
5342*472cd20dSToomas Soome req->u.pm.NATinfo.NATLease,
5343*472cd20dSToomas Soome req->u.pm.NATinfo.Lifetime,
5344*472cd20dSToomas Soome req->process_id, req->pid_name);
5345*472cd20dSToomas Soome else if (req->terminate == addrinfo_termination_callback)
5346*472cd20dSToomas Soome LogToFD(fd, "%s DNSServiceGetAddrInfo 0x%08X %2d %s%s %##s PID[%d](%s)",
5347*472cd20dSToomas Soome prefix, req->flags, req->interfaceIndex,
5348*472cd20dSToomas Soome req->u.addrinfo.protocols & kDNSServiceProtocol_IPv4 ? "v4" : " ",
5349*472cd20dSToomas Soome req->u.addrinfo.protocols & kDNSServiceProtocol_IPv6 ? "v6" : " ",
5350*472cd20dSToomas Soome GetAddrInfoClientRequestGetQName(&req->u.addrinfo), req->process_id, req->pid_name);
5351*472cd20dSToomas Soome else
5352*472cd20dSToomas Soome LogToFD(fd, "%s Unrecognized operation %p", prefix, req->terminate);
5353*472cd20dSToomas Soome }
5354*472cd20dSToomas Soome
LogClientInfo(request_state * req)5355c65ebfc7SToomas Soome mDNSlocal void LogClientInfo(request_state *req)
5356c65ebfc7SToomas Soome {
5357*472cd20dSToomas Soome char reqIDStr[14];
5358*472cd20dSToomas Soome char prefix[18];
5359*472cd20dSToomas Soome
5360*472cd20dSToomas Soome mDNS_snprintf(reqIDStr, sizeof(reqIDStr), "[R%u]", req->request_id);
5361*472cd20dSToomas Soome
5362*472cd20dSToomas Soome mDNS_snprintf(prefix, sizeof(prefix), "%-6s %2s", reqIDStr, req->primary ? "->" : "");
5363c65ebfc7SToomas Soome
5364c65ebfc7SToomas Soome if (!req->terminate)
5365c65ebfc7SToomas Soome LogMsgNoIdent("%s No operation yet on this socket", prefix);
5366c65ebfc7SToomas Soome else if (req->terminate == connection_termination)
5367c65ebfc7SToomas Soome {
5368c65ebfc7SToomas Soome int num_records = 0, num_ops = 0;
5369c65ebfc7SToomas Soome const registered_record_entry *p;
5370c65ebfc7SToomas Soome request_state *r;
5371c65ebfc7SToomas Soome for (p = req->u.reg_recs; p; p=p->next) num_records++;
5372c65ebfc7SToomas Soome for (r = req->next; r; r=r->next) if (r->primary == req) num_ops++;
5373c65ebfc7SToomas Soome LogMsgNoIdent("%s DNSServiceCreateConnection: %d registered record%s, %d kDNSServiceFlagsShareConnection operation%s PID[%d](%s)",
5374c65ebfc7SToomas Soome prefix, num_records, num_records != 1 ? "s" : "", num_ops, num_ops != 1 ? "s" : "",
5375c65ebfc7SToomas Soome req->process_id, req->pid_name);
5376c65ebfc7SToomas Soome for (p = req->u.reg_recs; p; p=p->next)
5377c65ebfc7SToomas Soome LogMsgNoIdent(" -> DNSServiceRegisterRecord 0x%08X %2d %3d %s PID[%d](%s)",
5378c65ebfc7SToomas Soome req->flags, req->interfaceIndex, p->key, ARDisplayString(&mDNSStorage, p->rr), req->process_id, req->pid_name);
5379c65ebfc7SToomas Soome for (r = req->next; r; r=r->next) if (r->primary == req) LogClientInfo(r);
5380c65ebfc7SToomas Soome }
5381c65ebfc7SToomas Soome else if (req->terminate == regservice_termination_callback)
5382c65ebfc7SToomas Soome {
5383c65ebfc7SToomas Soome service_instance *ptr;
5384c65ebfc7SToomas Soome for (ptr = req->u.servicereg.instances; ptr; ptr = ptr->next)
5385*472cd20dSToomas Soome LogMsgNoIdent("%-9s DNSServiceRegister 0x%08X %2d %##s %u/%u PID[%d](%s)",
5386c65ebfc7SToomas Soome (ptr == req->u.servicereg.instances) ? prefix : "", req->flags, req->interfaceIndex, ptr->srs.RR_SRV.resrec.name->c,
5387*472cd20dSToomas Soome mDNSVal16(req->u.servicereg.port),
5388c65ebfc7SToomas Soome SRS_PORT(&ptr->srs), req->process_id, req->pid_name);
5389c65ebfc7SToomas Soome }
5390c65ebfc7SToomas Soome else if (req->terminate == browse_termination_callback)
5391c65ebfc7SToomas Soome {
5392c65ebfc7SToomas Soome browser_t *blist;
5393c65ebfc7SToomas Soome for (blist = req->u.browser.browsers; blist; blist = blist->next)
5394*472cd20dSToomas Soome LogMsgNoIdent("%-9s DNSServiceBrowse 0x%08X %2d %##s PID[%d](%s)",
5395c65ebfc7SToomas Soome (blist == req->u.browser.browsers) ? prefix : "", req->flags, req->interfaceIndex, blist->q.qname.c,
5396*472cd20dSToomas Soome req->process_id, req->pid_name);
5397c65ebfc7SToomas Soome }
5398c65ebfc7SToomas Soome else if (req->terminate == resolve_termination_callback)
5399c65ebfc7SToomas Soome LogMsgNoIdent("%s DNSServiceResolve 0x%08X %2d %##s PID[%d](%s)",
5400c65ebfc7SToomas Soome prefix, req->flags, req->interfaceIndex, req->u.resolve.qsrv.qname.c, req->process_id, req->pid_name);
5401c65ebfc7SToomas Soome else if (req->terminate == queryrecord_termination_callback)
5402c65ebfc7SToomas Soome LogMsgNoIdent("%s DNSServiceQueryRecord 0x%08X %2d %##s (%s) PID[%d](%s)",
5403*472cd20dSToomas Soome prefix, req->flags, req->interfaceIndex, QueryRecordClientRequestGetQName(&req->u.queryrecord), DNSTypeName(QueryRecordClientRequestGetType(&req->u.queryrecord)), req->process_id, req->pid_name);
5404c65ebfc7SToomas Soome else if (req->terminate == enum_termination_callback)
5405c65ebfc7SToomas Soome LogMsgNoIdent("%s DNSServiceEnumerateDomains 0x%08X %2d %##s PID[%d](%s)",
5406c65ebfc7SToomas Soome prefix, req->flags, req->interfaceIndex, req->u.enumeration.q_all.qname.c, req->process_id, req->pid_name);
5407c65ebfc7SToomas Soome else if (req->terminate == port_mapping_termination_callback)
5408c65ebfc7SToomas Soome LogMsgNoIdent("%s DNSServiceNATPortMapping 0x%08X %2d %s%s Int %5d Req %5d Ext %.4a:%5d Req TTL %5d Granted TTL %5d PID[%d](%s)",
5409c65ebfc7SToomas Soome prefix,
5410c65ebfc7SToomas Soome req->flags,
5411c65ebfc7SToomas Soome req->interfaceIndex,
5412c65ebfc7SToomas Soome req->u.pm.NATinfo.Protocol & NATOp_MapTCP ? "TCP" : " ",
5413c65ebfc7SToomas Soome req->u.pm.NATinfo.Protocol & NATOp_MapUDP ? "UDP" : " ",
5414c65ebfc7SToomas Soome mDNSVal16(req->u.pm.NATinfo.IntPort),
5415c65ebfc7SToomas Soome mDNSVal16(req->u.pm.ReqExt),
5416c65ebfc7SToomas Soome &req->u.pm.NATinfo.ExternalAddress,
5417c65ebfc7SToomas Soome mDNSVal16(req->u.pm.NATinfo.ExternalPort),
5418c65ebfc7SToomas Soome req->u.pm.NATinfo.NATLease,
5419c65ebfc7SToomas Soome req->u.pm.NATinfo.Lifetime,
5420c65ebfc7SToomas Soome req->process_id, req->pid_name);
5421c65ebfc7SToomas Soome else if (req->terminate == addrinfo_termination_callback)
5422c65ebfc7SToomas Soome LogMsgNoIdent("%s DNSServiceGetAddrInfo 0x%08X %2d %s%s %##s PID[%d](%s)",
5423c65ebfc7SToomas Soome prefix, req->flags, req->interfaceIndex,
5424*472cd20dSToomas Soome req->u.addrinfo.protocols & kDNSServiceProtocol_IPv4 ? "v4" : " ",
5425*472cd20dSToomas Soome req->u.addrinfo.protocols & kDNSServiceProtocol_IPv6 ? "v6" : " ",
5426*472cd20dSToomas Soome GetAddrInfoClientRequestGetQName(&req->u.addrinfo), req->process_id, req->pid_name);
5427c65ebfc7SToomas Soome else
5428c65ebfc7SToomas Soome LogMsgNoIdent("%s Unrecognized operation %p", prefix, req->terminate);
5429c65ebfc7SToomas Soome }
5430c65ebfc7SToomas Soome
GetMcastClients(request_state * req)5431c65ebfc7SToomas Soome mDNSlocal void GetMcastClients(request_state *req)
5432c65ebfc7SToomas Soome {
5433c65ebfc7SToomas Soome if (req->terminate == connection_termination)
5434c65ebfc7SToomas Soome {
5435c65ebfc7SToomas Soome int num_records = 0, num_ops = 0;
5436c65ebfc7SToomas Soome const registered_record_entry *p;
5437c65ebfc7SToomas Soome request_state *r;
5438c65ebfc7SToomas Soome for (p = req->u.reg_recs; p; p=p->next)
5439c65ebfc7SToomas Soome num_records++;
5440c65ebfc7SToomas Soome for (r = req->next; r; r=r->next)
5441c65ebfc7SToomas Soome if (r->primary == req)
5442c65ebfc7SToomas Soome num_ops++;
5443c65ebfc7SToomas Soome for (p = req->u.reg_recs; p; p=p->next)
5444c65ebfc7SToomas Soome {
5445c65ebfc7SToomas Soome if (!AuthRecord_uDNS(p->rr))
5446c65ebfc7SToomas Soome n_mrecords++;
5447c65ebfc7SToomas Soome }
5448c65ebfc7SToomas Soome for (r = req->next; r; r=r->next)
5449c65ebfc7SToomas Soome if (r->primary == req)
5450c65ebfc7SToomas Soome GetMcastClients(r);
5451c65ebfc7SToomas Soome }
5452c65ebfc7SToomas Soome else if (req->terminate == regservice_termination_callback)
5453c65ebfc7SToomas Soome {
5454c65ebfc7SToomas Soome service_instance *ptr;
5455c65ebfc7SToomas Soome for (ptr = req->u.servicereg.instances; ptr; ptr = ptr->next)
5456c65ebfc7SToomas Soome {
5457c65ebfc7SToomas Soome if (!AuthRecord_uDNS(&ptr->srs.RR_SRV))
5458c65ebfc7SToomas Soome n_mrecords++;
5459c65ebfc7SToomas Soome }
5460c65ebfc7SToomas Soome }
5461c65ebfc7SToomas Soome else if (req->terminate == browse_termination_callback)
5462c65ebfc7SToomas Soome {
5463c65ebfc7SToomas Soome browser_t *blist;
5464c65ebfc7SToomas Soome for (blist = req->u.browser.browsers; blist; blist = blist->next)
5465c65ebfc7SToomas Soome {
5466c65ebfc7SToomas Soome if (mDNSOpaque16IsZero(blist->q.TargetQID))
5467c65ebfc7SToomas Soome n_mquests++;
5468c65ebfc7SToomas Soome }
5469c65ebfc7SToomas Soome }
5470c65ebfc7SToomas Soome else if (req->terminate == resolve_termination_callback)
5471c65ebfc7SToomas Soome {
5472c65ebfc7SToomas Soome if ((mDNSOpaque16IsZero(req->u.resolve.qsrv.TargetQID)) && (req->u.resolve.qsrv.ThisQInterval > 0))
5473c65ebfc7SToomas Soome n_mquests++;
5474c65ebfc7SToomas Soome }
5475c65ebfc7SToomas Soome else if (req->terminate == queryrecord_termination_callback)
5476c65ebfc7SToomas Soome {
5477*472cd20dSToomas Soome if (QueryRecordClientRequestIsMulticast(&req->u.queryrecord))
5478c65ebfc7SToomas Soome n_mquests++;
5479c65ebfc7SToomas Soome }
5480c65ebfc7SToomas Soome else if (req->terminate == addrinfo_termination_callback)
5481c65ebfc7SToomas Soome {
5482*472cd20dSToomas Soome if (GetAddrInfoClientRequestIsMulticast(&req->u.addrinfo))
5483c65ebfc7SToomas Soome n_mquests++;
5484c65ebfc7SToomas Soome }
5485c65ebfc7SToomas Soome else
5486c65ebfc7SToomas Soome {
5487c65ebfc7SToomas Soome return;
5488c65ebfc7SToomas Soome }
5489c65ebfc7SToomas Soome }
5490c65ebfc7SToomas Soome
5491c65ebfc7SToomas Soome
LogMcastClientInfo(request_state * req)5492c65ebfc7SToomas Soome mDNSlocal void LogMcastClientInfo(request_state *req)
5493c65ebfc7SToomas Soome {
5494c65ebfc7SToomas Soome if (!req->terminate)
5495c65ebfc7SToomas Soome LogMcastNoIdent("No operation yet on this socket");
5496c65ebfc7SToomas Soome else if (req->terminate == connection_termination)
5497c65ebfc7SToomas Soome {
5498c65ebfc7SToomas Soome int num_records = 0, num_ops = 0;
5499c65ebfc7SToomas Soome const registered_record_entry *p;
5500c65ebfc7SToomas Soome request_state *r;
5501c65ebfc7SToomas Soome for (p = req->u.reg_recs; p; p=p->next)
5502c65ebfc7SToomas Soome num_records++;
5503c65ebfc7SToomas Soome for (r = req->next; r; r=r->next)
5504c65ebfc7SToomas Soome if (r->primary == req)
5505c65ebfc7SToomas Soome num_ops++;
5506c65ebfc7SToomas Soome for (p = req->u.reg_recs; p; p=p->next)
5507c65ebfc7SToomas Soome {
5508c65ebfc7SToomas Soome if (!AuthRecord_uDNS(p->rr))
5509c65ebfc7SToomas Soome LogMcastNoIdent("R: -> DNSServiceRegisterRecord: %##s %s PID[%d](%s)", p->rr->resrec.name->c,
5510c65ebfc7SToomas Soome DNSTypeName(p->rr->resrec.rrtype), req->process_id, req->pid_name, i_mcount++);
5511c65ebfc7SToomas Soome }
5512c65ebfc7SToomas Soome for (r = req->next; r; r=r->next)
5513c65ebfc7SToomas Soome if (r->primary == req)
5514c65ebfc7SToomas Soome LogMcastClientInfo(r);
5515c65ebfc7SToomas Soome }
5516c65ebfc7SToomas Soome else if (req->terminate == regservice_termination_callback)
5517c65ebfc7SToomas Soome {
5518c65ebfc7SToomas Soome service_instance *ptr;
5519c65ebfc7SToomas Soome for (ptr = req->u.servicereg.instances; ptr; ptr = ptr->next)
5520c65ebfc7SToomas Soome {
5521c65ebfc7SToomas Soome if (!AuthRecord_uDNS(&ptr->srs.RR_SRV))
5522c65ebfc7SToomas Soome LogMcastNoIdent("R: DNSServiceRegister: %##s %u/%u PID[%d](%s)", ptr->srs.RR_SRV.resrec.name->c, mDNSVal16(req->u.servicereg.port),
5523c65ebfc7SToomas Soome SRS_PORT(&ptr->srs), req->process_id, req->pid_name, i_mcount++);
5524c65ebfc7SToomas Soome }
5525c65ebfc7SToomas Soome }
5526c65ebfc7SToomas Soome else if (req->terminate == browse_termination_callback)
5527c65ebfc7SToomas Soome {
5528c65ebfc7SToomas Soome browser_t *blist;
5529c65ebfc7SToomas Soome for (blist = req->u.browser.browsers; blist; blist = blist->next)
5530c65ebfc7SToomas Soome {
5531c65ebfc7SToomas Soome if (mDNSOpaque16IsZero(blist->q.TargetQID))
5532c65ebfc7SToomas Soome LogMcastNoIdent("Q: DNSServiceBrowse %##s %s PID[%d](%s)", blist->q.qname.c, DNSTypeName(blist->q.qtype),
5533c65ebfc7SToomas Soome req->process_id, req->pid_name, i_mcount++);
5534c65ebfc7SToomas Soome }
5535c65ebfc7SToomas Soome }
5536c65ebfc7SToomas Soome else if (req->terminate == resolve_termination_callback)
5537c65ebfc7SToomas Soome {
5538c65ebfc7SToomas Soome if ((mDNSOpaque16IsZero(req->u.resolve.qsrv.TargetQID)) && (req->u.resolve.qsrv.ThisQInterval > 0))
5539c65ebfc7SToomas Soome LogMcastNoIdent("Q: DNSServiceResolve %##s %s PID[%d](%s)", req->u.resolve.qsrv.qname.c, DNSTypeName(req->u.resolve.qsrv.qtype),
5540c65ebfc7SToomas Soome req->process_id, req->pid_name, i_mcount++);
5541c65ebfc7SToomas Soome }
5542c65ebfc7SToomas Soome else if (req->terminate == queryrecord_termination_callback)
5543c65ebfc7SToomas Soome {
5544*472cd20dSToomas Soome if (QueryRecordClientRequestIsMulticast(&req->u.queryrecord))
5545*472cd20dSToomas Soome {
5546*472cd20dSToomas Soome LogMcastNoIdent("Q: DNSServiceQueryRecord %##s %s PID[%d](%s)",
5547*472cd20dSToomas Soome QueryRecordClientRequestGetQName(&req->u.queryrecord),
5548*472cd20dSToomas Soome DNSTypeName(QueryRecordClientRequestGetType(&req->u.queryrecord)),
5549c65ebfc7SToomas Soome req->process_id, req->pid_name, i_mcount++);
5550c65ebfc7SToomas Soome }
5551*472cd20dSToomas Soome }
5552c65ebfc7SToomas Soome else if (req->terminate == addrinfo_termination_callback)
5553c65ebfc7SToomas Soome {
5554*472cd20dSToomas Soome if (GetAddrInfoClientRequestIsMulticast(&req->u.addrinfo))
5555c65ebfc7SToomas Soome {
5556*472cd20dSToomas Soome LogMcastNoIdent("Q: DNSServiceGetAddrInfo %s%s %##s PID[%d](%s)",
5557*472cd20dSToomas Soome req->u.addrinfo.protocols & kDNSServiceProtocol_IPv4 ? "v4" : " ",
5558*472cd20dSToomas Soome req->u.addrinfo.protocols & kDNSServiceProtocol_IPv6 ? "v6" : " ",
5559*472cd20dSToomas Soome GetAddrInfoClientRequestGetQName(&req->u.addrinfo), req->process_id, req->pid_name, i_mcount++);
5560c65ebfc7SToomas Soome }
5561*472cd20dSToomas Soome }
5562c65ebfc7SToomas Soome }
5563c65ebfc7SToomas Soome
RecordTypeName(mDNSu8 rtype)5564c65ebfc7SToomas Soome mDNSlocal char *RecordTypeName(mDNSu8 rtype)
5565c65ebfc7SToomas Soome {
5566c65ebfc7SToomas Soome switch (rtype)
5567c65ebfc7SToomas Soome {
5568c65ebfc7SToomas Soome case kDNSRecordTypeUnregistered: return ("Unregistered ");
5569c65ebfc7SToomas Soome case kDNSRecordTypeDeregistering: return ("Deregistering");
5570c65ebfc7SToomas Soome case kDNSRecordTypeUnique: return ("Unique ");
5571c65ebfc7SToomas Soome case kDNSRecordTypeAdvisory: return ("Advisory ");
5572c65ebfc7SToomas Soome case kDNSRecordTypeShared: return ("Shared ");
5573c65ebfc7SToomas Soome case kDNSRecordTypeVerified: return ("Verified ");
5574c65ebfc7SToomas Soome case kDNSRecordTypeKnownUnique: return ("KnownUnique ");
5575c65ebfc7SToomas Soome default: return("Unknown");
5576c65ebfc7SToomas Soome }
5577c65ebfc7SToomas Soome }
5578c65ebfc7SToomas Soome
LogEtcHostsToFD(int fd,mDNS * const m)5579*472cd20dSToomas Soome mDNSlocal int LogEtcHostsToFD(int fd, mDNS *const m)
5580c65ebfc7SToomas Soome {
5581c65ebfc7SToomas Soome mDNSBool showheader = mDNStrue;
5582c65ebfc7SToomas Soome const AuthRecord *ar;
5583c65ebfc7SToomas Soome mDNSu32 slot;
5584c65ebfc7SToomas Soome AuthGroup *ag;
5585c65ebfc7SToomas Soome int count = 0;
5586c65ebfc7SToomas Soome int authslot = 0;
5587c65ebfc7SToomas Soome mDNSBool truncated = 0;
5588c65ebfc7SToomas Soome
5589c65ebfc7SToomas Soome for (slot = 0; slot < AUTH_HASH_SLOTS; slot++)
5590c65ebfc7SToomas Soome {
5591c65ebfc7SToomas Soome if (m->rrauth.rrauth_hash[slot]) authslot++;
5592c65ebfc7SToomas Soome for (ag = m->rrauth.rrauth_hash[slot]; ag; ag = ag->next)
5593c65ebfc7SToomas Soome for (ar = ag->members; ar; ar = ar->next)
5594c65ebfc7SToomas Soome {
5595c65ebfc7SToomas Soome if (ar->RecordCallback != FreeEtcHosts) continue;
5596*472cd20dSToomas Soome if (showheader) { showheader = mDNSfalse; LogToFD(fd, " State Interface"); }
5597c65ebfc7SToomas Soome
5598c65ebfc7SToomas Soome // Print a maximum of 50 records
5599c65ebfc7SToomas Soome if (count++ >= 50) { truncated = mDNStrue; continue; }
5600c65ebfc7SToomas Soome if (ar->ARType == AuthRecordLocalOnly)
5601c65ebfc7SToomas Soome {
5602c65ebfc7SToomas Soome if (ar->resrec.InterfaceID == mDNSInterface_LocalOnly)
5603*472cd20dSToomas Soome LogToFD(fd, " %s LO %s", RecordTypeName(ar->resrec.RecordType), ARDisplayString(m, ar));
5604c65ebfc7SToomas Soome else
5605c65ebfc7SToomas Soome {
5606c65ebfc7SToomas Soome mDNSu32 scopeid = (mDNSu32)(uintptr_t)ar->resrec.InterfaceID;
5607*472cd20dSToomas Soome LogToFD(fd, " %s %u %s", RecordTypeName(ar->resrec.RecordType), scopeid, ARDisplayString(m, ar));
5608c65ebfc7SToomas Soome }
5609c65ebfc7SToomas Soome }
5610c65ebfc7SToomas Soome }
5611c65ebfc7SToomas Soome }
5612c65ebfc7SToomas Soome
5613*472cd20dSToomas Soome if (showheader) LogToFD(fd, "<None>");
5614*472cd20dSToomas Soome else if (truncated) LogToFD(fd, "<Truncated: to 50 records, Total records %d, Total Auth Groups %d, Auth Slots %d>", count, m->rrauth.rrauth_totalused, authslot);
5615c65ebfc7SToomas Soome return count;
5616c65ebfc7SToomas Soome }
5617c65ebfc7SToomas Soome
LogLocalOnlyAuthRecordsToFD(int fd,mDNS * const m)5618*472cd20dSToomas Soome mDNSlocal void LogLocalOnlyAuthRecordsToFD(int fd, mDNS *const m)
5619c65ebfc7SToomas Soome {
5620c65ebfc7SToomas Soome mDNSBool showheader = mDNStrue;
5621c65ebfc7SToomas Soome const AuthRecord *ar;
5622c65ebfc7SToomas Soome mDNSu32 slot;
5623c65ebfc7SToomas Soome AuthGroup *ag;
5624c65ebfc7SToomas Soome
5625c65ebfc7SToomas Soome for (slot = 0; slot < AUTH_HASH_SLOTS; slot++)
5626c65ebfc7SToomas Soome {
5627c65ebfc7SToomas Soome for (ag = m->rrauth.rrauth_hash[slot]; ag; ag = ag->next)
5628c65ebfc7SToomas Soome for (ar = ag->members; ar; ar = ar->next)
5629c65ebfc7SToomas Soome {
5630c65ebfc7SToomas Soome if (ar->RecordCallback == FreeEtcHosts) continue;
5631*472cd20dSToomas Soome if (showheader) { showheader = mDNSfalse; LogToFD(fd, " State Interface"); }
5632c65ebfc7SToomas Soome
5633c65ebfc7SToomas Soome // Print a maximum of 400 records
5634c65ebfc7SToomas Soome if (ar->ARType == AuthRecordLocalOnly)
5635*472cd20dSToomas Soome LogToFD(fd, " %s LO %s", RecordTypeName(ar->resrec.RecordType), ARDisplayString(m, ar));
5636c65ebfc7SToomas Soome else if (ar->ARType == AuthRecordP2P)
5637c65ebfc7SToomas Soome {
5638c65ebfc7SToomas Soome if (ar->resrec.InterfaceID == mDNSInterface_BLE)
5639*472cd20dSToomas Soome LogToFD(fd, " %s BLE %s", RecordTypeName(ar->resrec.RecordType), ARDisplayString(m, ar));
5640c65ebfc7SToomas Soome else
5641*472cd20dSToomas Soome LogToFD(fd, " %s PP %s", RecordTypeName(ar->resrec.RecordType), ARDisplayString(m, ar));
5642c65ebfc7SToomas Soome }
5643c65ebfc7SToomas Soome }
5644c65ebfc7SToomas Soome }
5645c65ebfc7SToomas Soome
5646*472cd20dSToomas Soome if (showheader) LogToFD(fd, "<None>");
5647c65ebfc7SToomas Soome }
5648c65ebfc7SToomas Soome
LogOneAuthRecordToFD(int fd,const AuthRecord * ar,mDNSs32 now,const char * ifname)5649*472cd20dSToomas Soome mDNSlocal void LogOneAuthRecordToFD(int fd, const AuthRecord *ar, mDNSs32 now, const char *ifname)
5650c65ebfc7SToomas Soome {
5651c65ebfc7SToomas Soome if (AuthRecord_uDNS(ar))
5652c65ebfc7SToomas Soome {
5653*472cd20dSToomas Soome LogToFD(fd, "%7d %7d %7d %-7s %4d %s %s",
5654c65ebfc7SToomas Soome ar->ThisAPInterval / mDNSPlatformOneSecond,
5655c65ebfc7SToomas Soome (ar->LastAPTime + ar->ThisAPInterval - now) / mDNSPlatformOneSecond,
5656c65ebfc7SToomas Soome ar->expire ? (ar->expire - now) / mDNSPlatformOneSecond : 0,
5657c65ebfc7SToomas Soome "-U-",
5658c65ebfc7SToomas Soome ar->state,
5659c65ebfc7SToomas Soome ar->AllowRemoteQuery ? "☠" : " ",
5660c65ebfc7SToomas Soome ARDisplayString(&mDNSStorage, ar));
5661c65ebfc7SToomas Soome }
5662c65ebfc7SToomas Soome else
5663c65ebfc7SToomas Soome {
5664*472cd20dSToomas Soome LogToFD(fd, "%7d %7d %7d %-7s 0x%02X %s %s",
5665c65ebfc7SToomas Soome ar->ThisAPInterval / mDNSPlatformOneSecond,
5666c65ebfc7SToomas Soome ar->AnnounceCount ? (ar->LastAPTime + ar->ThisAPInterval - now) / mDNSPlatformOneSecond : 0,
5667c65ebfc7SToomas Soome ar->TimeExpire ? (ar->TimeExpire - now) / mDNSPlatformOneSecond : 0,
5668c65ebfc7SToomas Soome ifname ? ifname : "ALL",
5669c65ebfc7SToomas Soome ar->resrec.RecordType,
5670c65ebfc7SToomas Soome ar->AllowRemoteQuery ? "☠" : " ",
5671*472cd20dSToomas Soome ARDisplayString(&mDNSStorage, ar));
5672c65ebfc7SToomas Soome }
5673c65ebfc7SToomas Soome }
5674c65ebfc7SToomas Soome
LogAuthRecordsToFD(int fd,const mDNSs32 now,AuthRecord * ResourceRecords,int * proxy)5675*472cd20dSToomas Soome mDNSlocal void LogAuthRecordsToFD(int fd,
5676*472cd20dSToomas Soome const mDNSs32 now, AuthRecord *ResourceRecords, int *proxy)
5677c65ebfc7SToomas Soome {
5678c65ebfc7SToomas Soome mDNSBool showheader = mDNStrue;
5679c65ebfc7SToomas Soome const AuthRecord *ar;
5680c65ebfc7SToomas Soome OwnerOptData owner = zeroOwner;
5681c65ebfc7SToomas Soome for (ar = ResourceRecords; ar; ar=ar->next)
5682c65ebfc7SToomas Soome {
5683c65ebfc7SToomas Soome const char *const ifname = InterfaceNameForID(&mDNSStorage, ar->resrec.InterfaceID);
5684c65ebfc7SToomas Soome if ((ar->WakeUp.HMAC.l[0] != 0) == (proxy != mDNSNULL))
5685c65ebfc7SToomas Soome {
5686*472cd20dSToomas Soome if (showheader) { showheader = mDNSfalse; LogToFD(fd, " Int Next Expire if State"); }
5687c65ebfc7SToomas Soome if (proxy) (*proxy)++;
5688c65ebfc7SToomas Soome if (!mDNSPlatformMemSame(&owner, &ar->WakeUp, sizeof(owner)))
5689c65ebfc7SToomas Soome {
5690c65ebfc7SToomas Soome owner = ar->WakeUp;
5691c65ebfc7SToomas Soome if (owner.password.l[0])
5692*472cd20dSToomas Soome LogToFD(fd, "Proxying for H-MAC %.6a I-MAC %.6a Password %.6a seq %d", &owner.HMAC, &owner.IMAC, &owner.password, owner.seq);
5693c65ebfc7SToomas Soome else if (!mDNSSameEthAddress(&owner.HMAC, &owner.IMAC))
5694*472cd20dSToomas Soome LogToFD(fd, "Proxying for H-MAC %.6a I-MAC %.6a seq %d", &owner.HMAC, &owner.IMAC, owner.seq);
5695c65ebfc7SToomas Soome else
5696*472cd20dSToomas Soome LogToFD(fd, "Proxying for %.6a seq %d", &owner.HMAC, owner.seq);
5697c65ebfc7SToomas Soome }
5698c65ebfc7SToomas Soome if (AuthRecord_uDNS(ar))
5699c65ebfc7SToomas Soome {
5700*472cd20dSToomas Soome LogOneAuthRecordToFD(fd, ar, now, ifname);
5701c65ebfc7SToomas Soome }
5702c65ebfc7SToomas Soome else if (ar->ARType == AuthRecordLocalOnly)
5703c65ebfc7SToomas Soome {
5704*472cd20dSToomas Soome LogToFD(fd, " LO %s", ARDisplayString(&mDNSStorage, ar));
5705c65ebfc7SToomas Soome }
5706c65ebfc7SToomas Soome else if (ar->ARType == AuthRecordP2P)
5707c65ebfc7SToomas Soome {
5708c65ebfc7SToomas Soome if (ar->resrec.InterfaceID == mDNSInterface_BLE)
5709*472cd20dSToomas Soome LogToFD(fd, " BLE %s", ARDisplayString(&mDNSStorage, ar));
5710c65ebfc7SToomas Soome else
5711*472cd20dSToomas Soome LogToFD(fd, " PP %s", ARDisplayString(&mDNSStorage, ar));
5712c65ebfc7SToomas Soome }
5713c65ebfc7SToomas Soome else
5714c65ebfc7SToomas Soome {
5715*472cd20dSToomas Soome LogOneAuthRecordToFD(fd, ar, now, ifname);
5716c65ebfc7SToomas Soome }
5717c65ebfc7SToomas Soome }
5718c65ebfc7SToomas Soome }
5719*472cd20dSToomas Soome if (showheader) LogToFD(fd, "<None>");
5720c65ebfc7SToomas Soome }
5721c65ebfc7SToomas Soome
PrintOneCacheRecordToFD(int fd,const CacheRecord * cr,mDNSu32 slot,const mDNSu32 remain,const char * ifname,mDNSu32 * CacheUsed)5722*472cd20dSToomas Soome mDNSlocal void PrintOneCacheRecordToFD(int fd, const CacheRecord *cr, mDNSu32 slot, const mDNSu32 remain, const char *ifname, mDNSu32 *CacheUsed)
5723c65ebfc7SToomas Soome {
5724*472cd20dSToomas Soome LogToFD(fd, "%3d %s%8d %-7s%s %-6s%s",
5725c65ebfc7SToomas Soome slot,
5726c65ebfc7SToomas Soome cr->CRActiveQuestion ? "*" : " ",
5727c65ebfc7SToomas Soome remain,
5728c65ebfc7SToomas Soome ifname ? ifname : "-U-",
5729c65ebfc7SToomas Soome (cr->resrec.RecordType == kDNSRecordTypePacketNegative) ? "-" :
5730c65ebfc7SToomas Soome (cr->resrec.RecordType & kDNSRecordTypePacketUniqueMask) ? " " : "+",
5731c65ebfc7SToomas Soome DNSTypeName(cr->resrec.rrtype),
5732c65ebfc7SToomas Soome CRDisplayString(&mDNSStorage, cr));
5733c65ebfc7SToomas Soome (*CacheUsed)++;
5734c65ebfc7SToomas Soome }
5735c65ebfc7SToomas Soome
PrintCachedRecordsToFD(int fd,const CacheRecord * cr,mDNSu32 slot,const mDNSu32 remain,const char * ifname,mDNSu32 * CacheUsed)5736*472cd20dSToomas Soome mDNSlocal void PrintCachedRecordsToFD(int fd, const CacheRecord *cr, mDNSu32 slot, const mDNSu32 remain, const char *ifname, mDNSu32 *CacheUsed)
5737c65ebfc7SToomas Soome {
5738c65ebfc7SToomas Soome CacheRecord *soa;
5739c65ebfc7SToomas Soome
5740c65ebfc7SToomas Soome soa = cr->soa;
5741c65ebfc7SToomas Soome if (soa)
5742c65ebfc7SToomas Soome {
5743*472cd20dSToomas Soome PrintOneCacheRecordToFD(fd, soa, slot, remain, ifname, CacheUsed);
5744c65ebfc7SToomas Soome }
5745*472cd20dSToomas Soome }
5746*472cd20dSToomas Soome
LogMDNSStatisticsToFD(int fd,mDNS * const m)5747*472cd20dSToomas Soome mDNSexport void LogMDNSStatisticsToFD(int fd, mDNS *const m)
5748c65ebfc7SToomas Soome {
5749*472cd20dSToomas Soome LogToFD(fd, "--- MDNS Statistics ---");
5750*472cd20dSToomas Soome
5751*472cd20dSToomas Soome LogToFD(fd, "Name Conflicts %u", m->mDNSStats.NameConflicts);
5752*472cd20dSToomas Soome LogToFD(fd, "KnownUnique Name Conflicts %u", m->mDNSStats.KnownUniqueNameConflicts);
5753*472cd20dSToomas Soome LogToFD(fd, "Duplicate Query Suppressions %u", m->mDNSStats.DupQuerySuppressions);
5754*472cd20dSToomas Soome LogToFD(fd, "KA Suppressions %u", m->mDNSStats.KnownAnswerSuppressions);
5755*472cd20dSToomas Soome LogToFD(fd, "KA Multiple Packets %u", m->mDNSStats.KnownAnswerMultiplePkts);
5756*472cd20dSToomas Soome LogToFD(fd, "Poof Cache Deletions %u", m->mDNSStats.PoofCacheDeletions);
5757*472cd20dSToomas Soome LogToFD(fd, "--------------------------------");
5758*472cd20dSToomas Soome
5759*472cd20dSToomas Soome LogToFD(fd, "Multicast packets Sent %u", m->MulticastPacketsSent);
5760*472cd20dSToomas Soome LogToFD(fd, "Multicast packets Received %u", m->MPktNum);
5761*472cd20dSToomas Soome LogToFD(fd, "Remote Subnet packets %u", m->RemoteSubnet);
5762*472cd20dSToomas Soome LogToFD(fd, "QU questions received %u", m->mDNSStats.UnicastBitInQueries);
5763*472cd20dSToomas Soome LogToFD(fd, "Normal multicast questions %u", m->mDNSStats.NormalQueries);
5764*472cd20dSToomas Soome LogToFD(fd, "Answers for questions %u", m->mDNSStats.MatchingAnswersForQueries);
5765*472cd20dSToomas Soome LogToFD(fd, "Unicast responses %u", m->mDNSStats.UnicastResponses);
5766*472cd20dSToomas Soome LogToFD(fd, "Multicast responses %u", m->mDNSStats.MulticastResponses);
5767*472cd20dSToomas Soome LogToFD(fd, "Unicast response Demotions %u", m->mDNSStats.UnicastDemotedToMulticast);
5768*472cd20dSToomas Soome LogToFD(fd, "--------------------------------");
5769*472cd20dSToomas Soome
5770*472cd20dSToomas Soome LogToFD(fd, "Sleeps %u", m->mDNSStats.Sleeps);
5771*472cd20dSToomas Soome LogToFD(fd, "Wakeups %u", m->mDNSStats.Wakes);
5772*472cd20dSToomas Soome LogToFD(fd, "Interface UP events %u", m->mDNSStats.InterfaceUp);
5773*472cd20dSToomas Soome LogToFD(fd, "Interface UP Flap events %u", m->mDNSStats.InterfaceUpFlap);
5774*472cd20dSToomas Soome LogToFD(fd, "Interface Down events %u", m->mDNSStats.InterfaceDown);
5775*472cd20dSToomas Soome LogToFD(fd, "Interface DownFlap events %u", m->mDNSStats.InterfaceDownFlap);
5776*472cd20dSToomas Soome LogToFD(fd, "Cache refresh queries %u", m->mDNSStats.CacheRefreshQueries);
5777*472cd20dSToomas Soome LogToFD(fd, "Cache refreshed %u", m->mDNSStats.CacheRefreshed);
5778*472cd20dSToomas Soome LogToFD(fd, "Wakeup on Resolves %u", m->mDNSStats.WakeOnResolves);
5779c65ebfc7SToomas Soome }
5780c65ebfc7SToomas Soome
udsserver_info_dump_to_fd(int fd)5781*472cd20dSToomas Soome mDNSexport void udsserver_info_dump_to_fd(int fd)
5782c65ebfc7SToomas Soome {
5783c65ebfc7SToomas Soome mDNS *const m = &mDNSStorage;
5784c65ebfc7SToomas Soome const mDNSs32 now = mDNS_TimeNow(m);
5785c65ebfc7SToomas Soome mDNSu32 CacheUsed = 0, CacheActive = 0, slot;
5786c65ebfc7SToomas Soome int ProxyA = 0, ProxyD = 0;
5787c65ebfc7SToomas Soome mDNSu32 groupCount = 0;
5788c65ebfc7SToomas Soome mDNSu32 mcastRecordCount = 0;
5789c65ebfc7SToomas Soome mDNSu32 ucastRecordCount = 0;
5790c65ebfc7SToomas Soome const CacheGroup *cg;
5791c65ebfc7SToomas Soome const CacheRecord *cr;
5792c65ebfc7SToomas Soome const DNSQuestion *q;
5793c65ebfc7SToomas Soome const DNameListElem *d;
5794c65ebfc7SToomas Soome const SearchListElem *s;
5795c65ebfc7SToomas Soome
5796*472cd20dSToomas Soome LogToFD(fd, "------------ Cache -------------");
5797*472cd20dSToomas Soome LogToFD(fd, "Slt Q TTL if U Type rdlen");
5798c65ebfc7SToomas Soome for (slot = 0; slot < CACHE_HASH_SLOTS; slot++)
5799c65ebfc7SToomas Soome {
5800c65ebfc7SToomas Soome for (cg = m->rrcache_hash[slot]; cg; cg=cg->next)
5801c65ebfc7SToomas Soome {
5802c65ebfc7SToomas Soome groupCount++; // Count one cache entity for the CacheGroup object
5803c65ebfc7SToomas Soome for (cr = cg->members; cr; cr=cr->next)
5804c65ebfc7SToomas Soome {
5805c65ebfc7SToomas Soome const mDNSs32 remain = cr->resrec.rroriginalttl - (now - cr->TimeRcvd) / mDNSPlatformOneSecond;
5806c65ebfc7SToomas Soome const char *ifname;
5807c65ebfc7SToomas Soome mDNSInterfaceID InterfaceID = cr->resrec.InterfaceID;
5808c65ebfc7SToomas Soome mDNSu32 *const countPtr = InterfaceID ? &mcastRecordCount : &ucastRecordCount;
5809*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, QUERIER)
5810*472cd20dSToomas Soome if (!InterfaceID && cr->resrec.dnsservice &&
5811*472cd20dSToomas Soome (mdns_dns_service_get_scope(cr->resrec.dnsservice) == mdns_dns_service_scope_interface))
5812*472cd20dSToomas Soome {
5813*472cd20dSToomas Soome InterfaceID = (mDNSInterfaceID)(uintptr_t)mdns_dns_service_get_interface_index(cr->resrec.dnsservice);
5814*472cd20dSToomas Soome }
5815*472cd20dSToomas Soome #else
5816*472cd20dSToomas Soome if (!InterfaceID && cr->resrec.rDNSServer && cr->resrec.rDNSServer->scopeType)
5817c65ebfc7SToomas Soome InterfaceID = cr->resrec.rDNSServer->interface;
5818*472cd20dSToomas Soome #endif
5819c65ebfc7SToomas Soome ifname = InterfaceNameForID(m, InterfaceID);
5820c65ebfc7SToomas Soome if (cr->CRActiveQuestion) CacheActive++;
5821*472cd20dSToomas Soome PrintOneCacheRecordToFD(fd, cr, slot, remain, ifname, countPtr);
5822*472cd20dSToomas Soome PrintCachedRecordsToFD(fd, cr, slot, remain, ifname, countPtr);
5823c65ebfc7SToomas Soome }
5824c65ebfc7SToomas Soome }
5825c65ebfc7SToomas Soome }
5826c65ebfc7SToomas Soome
5827c65ebfc7SToomas Soome CacheUsed = groupCount + mcastRecordCount + ucastRecordCount;
5828c65ebfc7SToomas Soome if (m->rrcache_totalused != CacheUsed)
5829*472cd20dSToomas Soome LogToFD(fd, "Cache use mismatch: rrcache_totalused is %lu, true count %lu", m->rrcache_totalused, CacheUsed);
5830c65ebfc7SToomas Soome if (m->rrcache_active != CacheActive)
5831*472cd20dSToomas Soome LogToFD(fd, "Cache use mismatch: rrcache_active is %lu, true count %lu", m->rrcache_active, CacheActive);
5832*472cd20dSToomas Soome LogToFD(fd, "Cache size %u entities; %u in use (%u group, %u multicast, %u unicast); %u referenced by active questions",
5833c65ebfc7SToomas Soome m->rrcache_size, CacheUsed, groupCount, mcastRecordCount, ucastRecordCount, CacheActive);
5834c65ebfc7SToomas Soome
5835*472cd20dSToomas Soome LogToFD(fd, "--------- Auth Records ---------");
5836*472cd20dSToomas Soome LogAuthRecordsToFD(fd, now, m->ResourceRecords, mDNSNULL);
5837c65ebfc7SToomas Soome
5838*472cd20dSToomas Soome LogToFD(fd, "--------- LocalOnly, P2P Auth Records ---------");
5839*472cd20dSToomas Soome LogLocalOnlyAuthRecordsToFD(fd, m);
5840c65ebfc7SToomas Soome
5841*472cd20dSToomas Soome LogToFD(fd, "--------- /etc/hosts ---------");
5842*472cd20dSToomas Soome LogEtcHostsToFD(fd, m);
5843c65ebfc7SToomas Soome
5844*472cd20dSToomas Soome LogToFD(fd, "------ Duplicate Records -------");
5845*472cd20dSToomas Soome LogAuthRecordsToFD(fd, now, m->DuplicateRecords, mDNSNULL);
5846c65ebfc7SToomas Soome
5847*472cd20dSToomas Soome LogToFD(fd, "----- Auth Records Proxied -----");
5848*472cd20dSToomas Soome LogAuthRecordsToFD(fd, now, m->ResourceRecords, &ProxyA);
5849c65ebfc7SToomas Soome
5850*472cd20dSToomas Soome LogToFD(fd, "-- Duplicate Records Proxied ---");
5851*472cd20dSToomas Soome LogAuthRecordsToFD(fd, now, m->DuplicateRecords, &ProxyD);
5852c65ebfc7SToomas Soome
5853*472cd20dSToomas Soome LogToFD(fd, "---------- Questions -----------");
5854*472cd20dSToomas Soome if (!m->Questions) LogToFD(fd, "<None>");
5855c65ebfc7SToomas Soome else
5856c65ebfc7SToomas Soome {
5857c65ebfc7SToomas Soome CacheUsed = 0;
5858c65ebfc7SToomas Soome CacheActive = 0;
5859*472cd20dSToomas Soome LogToFD(fd, " Int Next if T NumAns VDNS Qptr DupOf SU SQ Type Name");
5860c65ebfc7SToomas Soome for (q = m->Questions; q; q=q->next)
5861c65ebfc7SToomas Soome {
5862c65ebfc7SToomas Soome mDNSs32 i = q->ThisQInterval / mDNSPlatformOneSecond;
5863c65ebfc7SToomas Soome mDNSs32 n = (NextQSendTime(q) - now) / mDNSPlatformOneSecond;
5864c65ebfc7SToomas Soome char *ifname = InterfaceNameForID(m, q->InterfaceID);
5865c65ebfc7SToomas Soome CacheUsed++;
5866c65ebfc7SToomas Soome if (q->ThisQInterval) CacheActive++;
5867*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, QUERIER)
5868*472cd20dSToomas Soome LogToFD(fd, "%6d%6d %-7s%s %5d 0x%p 0x%p %1d %2d %-5s%##s%s",
5869*472cd20dSToomas Soome #else
5870*472cd20dSToomas Soome LogToFD(fd, "%6d%6d %-7s%s %5d 0x%08x%08x%08x%08x 0x%p 0x%p %1d %2d %-5s%##s%s",
5871*472cd20dSToomas Soome #endif
5872c65ebfc7SToomas Soome i, n,
5873c65ebfc7SToomas Soome ifname ? ifname : mDNSOpaque16IsZero(q->TargetQID) ? "" : "-U-",
5874c65ebfc7SToomas Soome mDNSOpaque16IsZero(q->TargetQID) ? (q->LongLived ? "l" : " ") : (q->LongLived ? "L" : "O"),
5875*472cd20dSToomas Soome q->CurrentAnswers,
5876*472cd20dSToomas Soome #if !MDNSRESPONDER_SUPPORTS(APPLE, QUERIER)
5877*472cd20dSToomas Soome q->validDNSServers.l[3], q->validDNSServers.l[2], q->validDNSServers.l[1], q->validDNSServers.l[0],
5878*472cd20dSToomas Soome #endif
5879*472cd20dSToomas Soome q, q->DuplicateOf,
5880*472cd20dSToomas Soome q->SuppressUnusable, q->Suppressed, DNSTypeName(q->qtype), q->qname.c,
5881c65ebfc7SToomas Soome q->DuplicateOf ? " (dup)" : "");
5882c65ebfc7SToomas Soome }
5883*472cd20dSToomas Soome LogToFD(fd, "%lu question%s; %lu active", CacheUsed, CacheUsed > 1 ? "s" : "", CacheActive);
5884c65ebfc7SToomas Soome }
5885c65ebfc7SToomas Soome
5886*472cd20dSToomas Soome LogToFD(fd, "----- LocalOnly, P2P Questions -----");
5887*472cd20dSToomas Soome if (!m->LocalOnlyQuestions) LogToFD(fd, "<None>");
5888c65ebfc7SToomas Soome else for (q = m->LocalOnlyQuestions; q; q=q->next)
5889*472cd20dSToomas Soome LogToFD(fd, " %3s %5d %-6s%##s%s",
5890c65ebfc7SToomas Soome q->InterfaceID == mDNSInterface_LocalOnly ? "LO ": q->InterfaceID == mDNSInterface_BLE ? "BLE": "P2P",
5891c65ebfc7SToomas Soome q->CurrentAnswers, DNSTypeName(q->qtype), q->qname.c, q->DuplicateOf ? " (dup)" : "");
5892c65ebfc7SToomas Soome
5893*472cd20dSToomas Soome LogToFD(fd, "---- Active UDS Client Requests ----");
5894*472cd20dSToomas Soome if (!all_requests) LogToFD(fd, "<None>");
5895c65ebfc7SToomas Soome else
5896c65ebfc7SToomas Soome {
5897c65ebfc7SToomas Soome request_state *req, *r;
5898c65ebfc7SToomas Soome for (req = all_requests; req; req=req->next)
5899c65ebfc7SToomas Soome {
5900c65ebfc7SToomas Soome if (req->primary) // If this is a subbordinate operation, check that the parent is in the list
5901c65ebfc7SToomas Soome {
5902c65ebfc7SToomas Soome for (r = all_requests; r && r != req; r=r->next) if (r == req->primary) goto foundparent;
5903*472cd20dSToomas Soome LogToFD(fd, "%3d: Orhpan operation %p; parent %p not found in request list", req->sd);
5904c65ebfc7SToomas Soome }
5905c65ebfc7SToomas Soome // For non-subbordinate operations, and subbordinate operations that have lost their parent, write out their info
5906*472cd20dSToomas Soome LogClientInfoToFD(fd, req);
5907c65ebfc7SToomas Soome foundparent:;
5908c65ebfc7SToomas Soome }
5909c65ebfc7SToomas Soome }
5910c65ebfc7SToomas Soome
5911*472cd20dSToomas Soome LogToFD(fd, "-------- NAT Traversals --------");
5912*472cd20dSToomas Soome LogToFD(fd, "ExtAddress %.4a Retry %d Interval %d",
5913c65ebfc7SToomas Soome &m->ExtAddress,
5914c65ebfc7SToomas Soome m->retryGetAddr ? (m->retryGetAddr - now) / mDNSPlatformOneSecond : 0,
5915c65ebfc7SToomas Soome m->retryIntervalGetAddr / mDNSPlatformOneSecond);
5916c65ebfc7SToomas Soome if (m->NATTraversals)
5917c65ebfc7SToomas Soome {
5918c65ebfc7SToomas Soome const NATTraversalInfo *nat;
5919c65ebfc7SToomas Soome for (nat = m->NATTraversals; nat; nat=nat->next)
5920c65ebfc7SToomas Soome {
5921*472cd20dSToomas Soome LogToFD(fd, "%p %s Int %5d %s Err %d Retry %5d Interval %5d Expire %5d Req %.4a:%d Ext %.4a:%d",
5922c65ebfc7SToomas Soome nat,
5923c65ebfc7SToomas Soome nat->Protocol ? (nat->Protocol == NATOp_MapTCP ? "TCP" : "UDP") : "ADD",
5924c65ebfc7SToomas Soome mDNSVal16(nat->IntPort),
5925c65ebfc7SToomas Soome (nat->lastSuccessfulProtocol == NATTProtocolNone ? "None " :
5926c65ebfc7SToomas Soome nat->lastSuccessfulProtocol == NATTProtocolNATPMP ? "NAT-PMP " :
5927c65ebfc7SToomas Soome nat->lastSuccessfulProtocol == NATTProtocolUPNPIGD ? "UPnP/IGD" :
5928c65ebfc7SToomas Soome nat->lastSuccessfulProtocol == NATTProtocolPCP ? "PCP " :
5929c65ebfc7SToomas Soome /* else */ "Unknown " ),
5930c65ebfc7SToomas Soome nat->Result,
5931c65ebfc7SToomas Soome nat->retryPortMap ? (nat->retryPortMap - now) / mDNSPlatformOneSecond : 0,
5932c65ebfc7SToomas Soome nat->retryInterval / mDNSPlatformOneSecond,
5933c65ebfc7SToomas Soome nat->ExpiryTime ? (nat->ExpiryTime - now) / mDNSPlatformOneSecond : 0,
5934c65ebfc7SToomas Soome &nat->NewAddress, mDNSVal16(nat->RequestedPort),
5935c65ebfc7SToomas Soome &nat->ExternalAddress, mDNSVal16(nat->ExternalPort));
5936c65ebfc7SToomas Soome }
5937c65ebfc7SToomas Soome }
5938c65ebfc7SToomas Soome
5939*472cd20dSToomas Soome LogToFD(fd, "--------- AuthInfoList ---------");
5940*472cd20dSToomas Soome if (!m->AuthInfoList) LogToFD(fd, "<None>");
5941c65ebfc7SToomas Soome else
5942c65ebfc7SToomas Soome {
5943c65ebfc7SToomas Soome const DomainAuthInfo *a;
5944c65ebfc7SToomas Soome for (a = m->AuthInfoList; a; a = a->next)
5945c65ebfc7SToomas Soome {
5946*472cd20dSToomas Soome LogToFD(fd, "%##s %##s %##s %d %d",
5947c65ebfc7SToomas Soome a->domain.c, a->keyname.c,
5948c65ebfc7SToomas Soome a->hostname.c, (a->port.b[0] << 8 | a->port.b[1]),
5949*472cd20dSToomas Soome (a->deltime ? (a->deltime - now) : 0));
5950c65ebfc7SToomas Soome }
5951c65ebfc7SToomas Soome }
5952c65ebfc7SToomas Soome
5953*472cd20dSToomas Soome LogToFD(fd, "---------- Misc State ----------");
5954c65ebfc7SToomas Soome
5955*472cd20dSToomas Soome LogToFD(fd, "PrimaryMAC: %.6a", &m->PrimaryMAC);
5956c65ebfc7SToomas Soome
5957*472cd20dSToomas Soome LogToFD(fd, "m->SleepState %d (%s) seq %d",
5958c65ebfc7SToomas Soome m->SleepState,
5959c65ebfc7SToomas Soome m->SleepState == SleepState_Awake ? "Awake" :
5960c65ebfc7SToomas Soome m->SleepState == SleepState_Transferring ? "Transferring" :
5961c65ebfc7SToomas Soome m->SleepState == SleepState_Sleeping ? "Sleeping" : "?",
5962c65ebfc7SToomas Soome m->SleepSeqNum);
5963c65ebfc7SToomas Soome
5964*472cd20dSToomas Soome if (!m->SPSSocket) LogToFD(fd, "Not offering Sleep Proxy Service");
5965c65ebfc7SToomas Soome #ifndef SPC_DISABLED
5966*472cd20dSToomas Soome else LogToFD(fd, "Offering Sleep Proxy Service: %#s", m->SPSRecords.RR_SRV.resrec.name->c);
5967c65ebfc7SToomas Soome #endif
5968*472cd20dSToomas Soome if (m->ProxyRecords == ProxyA + ProxyD) LogToFD(fd, "ProxyRecords: %d + %d = %d", ProxyA, ProxyD, ProxyA + ProxyD);
5969*472cd20dSToomas Soome else LogToFD(fd, "ProxyRecords: MISMATCH %d + %d = %d ≠ %d", ProxyA, ProxyD, ProxyA + ProxyD, m->ProxyRecords);
5970c65ebfc7SToomas Soome
5971*472cd20dSToomas Soome LogToFD(fd, "------ Auto Browse Domains -----");
5972*472cd20dSToomas Soome if (!AutoBrowseDomains) LogToFD(fd, "<None>");
5973*472cd20dSToomas Soome else for (d=AutoBrowseDomains; d; d=d->next) LogToFD(fd, "%##s", d->name.c);
5974c65ebfc7SToomas Soome
5975*472cd20dSToomas Soome LogToFD(fd, "--- Auto Registration Domains --");
5976*472cd20dSToomas Soome if (!AutoRegistrationDomains) LogToFD(fd, "<None>");
5977*472cd20dSToomas Soome else for (d=AutoRegistrationDomains; d; d=d->next) LogToFD(fd, "%##s", d->name.c);
5978c65ebfc7SToomas Soome
5979*472cd20dSToomas Soome LogToFD(fd, "--- Search Domains --");
5980*472cd20dSToomas Soome if (!SearchList) LogToFD(fd, "<None>");
5981c65ebfc7SToomas Soome else
5982c65ebfc7SToomas Soome {
5983c65ebfc7SToomas Soome for (s=SearchList; s; s=s->next)
5984c65ebfc7SToomas Soome {
5985c65ebfc7SToomas Soome char *ifname = InterfaceNameForID(m, s->InterfaceID);
5986*472cd20dSToomas Soome LogToFD(fd, "%##s %s", s->domain.c, ifname ? ifname : "");
5987c65ebfc7SToomas Soome }
5988c65ebfc7SToomas Soome }
5989*472cd20dSToomas Soome LogMDNSStatisticsToFD(fd, m);
5990c65ebfc7SToomas Soome
5991*472cd20dSToomas Soome LogToFD(fd, "---- Task Scheduling Timers ----");
5992c65ebfc7SToomas Soome
5993*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, BONJOUR_ON_DEMAND)
5994*472cd20dSToomas Soome LogToFD(fd, "BonjourEnabled %d", m->BonjourEnabled);
5995*472cd20dSToomas Soome #endif
5996c65ebfc7SToomas Soome
5997c65ebfc7SToomas Soome #if APPLE_OSX_mDNSResponder && ENABLE_BLE_TRIGGERED_BONJOUR
5998*472cd20dSToomas Soome LogToFD(fd, "EnableBLEBasedDiscovery %d", EnableBLEBasedDiscovery);
5999*472cd20dSToomas Soome LogToFD(fd, "DefaultToBLETriggered %d", DefaultToBLETriggered);
6000c65ebfc7SToomas Soome #endif // APPLE_OSX_mDNSResponder && ENABLE_BLE_TRIGGERED_BONJOUR
6001c65ebfc7SToomas Soome
6002c65ebfc7SToomas Soome if (!m->NewQuestions)
6003*472cd20dSToomas Soome LogToFD(fd, "NewQuestion <NONE>");
6004c65ebfc7SToomas Soome else
6005*472cd20dSToomas Soome LogToFD(fd, "NewQuestion DelayAnswering %d %d %##s (%s)",
6006c65ebfc7SToomas Soome m->NewQuestions->DelayAnswering, m->NewQuestions->DelayAnswering-now,
6007c65ebfc7SToomas Soome m->NewQuestions->qname.c, DNSTypeName(m->NewQuestions->qtype));
6008c65ebfc7SToomas Soome
6009c65ebfc7SToomas Soome if (!m->NewLocalOnlyQuestions)
6010*472cd20dSToomas Soome LogToFD(fd, "NewLocalOnlyQuestions <NONE>");
6011c65ebfc7SToomas Soome else
6012*472cd20dSToomas Soome LogToFD(fd, "NewLocalOnlyQuestions %##s (%s)",
6013c65ebfc7SToomas Soome m->NewLocalOnlyQuestions->qname.c, DNSTypeName(m->NewLocalOnlyQuestions->qtype));
6014c65ebfc7SToomas Soome
6015c65ebfc7SToomas Soome if (!m->NewLocalRecords)
6016*472cd20dSToomas Soome LogToFD(fd, "NewLocalRecords <NONE>");
6017c65ebfc7SToomas Soome else
6018*472cd20dSToomas Soome LogToFD(fd, "NewLocalRecords %02X %s", m->NewLocalRecords->resrec.RecordType, ARDisplayString(m, m->NewLocalRecords));
6019c65ebfc7SToomas Soome
6020*472cd20dSToomas Soome LogToFD(fd, "SPSProxyListChanged%s", m->SPSProxyListChanged ? "" : " <NONE>");
6021*472cd20dSToomas Soome LogToFD(fd, "LocalRemoveEvents%s", m->LocalRemoveEvents ? "" : " <NONE>");
6022*472cd20dSToomas Soome LogToFD(fd, "m->WABBrowseQueriesCount %d", m->WABBrowseQueriesCount);
6023*472cd20dSToomas Soome LogToFD(fd, "m->WABLBrowseQueriesCount %d", m->WABLBrowseQueriesCount);
6024*472cd20dSToomas Soome LogToFD(fd, "m->WABRegQueriesCount %d", m->WABRegQueriesCount);
6025*472cd20dSToomas Soome LogToFD(fd, "m->AutoTargetServices %u", m->AutoTargetServices);
6026*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, RANDOM_AWDL_HOSTNAME)
6027*472cd20dSToomas Soome LogToFD(fd, "m->AutoTargetAWDLIncludedCount %u", m->AutoTargetAWDLIncludedCount);
6028*472cd20dSToomas Soome LogToFD(fd, "m->AutoTargetAWDLOnlyCount %u", m->AutoTargetAWDLOnlyCount);
6029c65ebfc7SToomas Soome #endif
6030c65ebfc7SToomas Soome
6031*472cd20dSToomas Soome LogToFD(fd, " ABS (hex) ABS (dec) REL (hex) REL (dec)");
6032*472cd20dSToomas Soome LogToFD(fd, "m->timenow %08X %11d", now, now);
6033*472cd20dSToomas Soome LogToFD(fd, "m->timenow_adjust %08X %11d", m->timenow_adjust, m->timenow_adjust);
6034*472cd20dSToomas Soome LogTimerToFD(fd, "m->NextScheduledEvent ", m->NextScheduledEvent);
6035c65ebfc7SToomas Soome
6036*472cd20dSToomas Soome #ifndef UNICAST_DISABLED
6037*472cd20dSToomas Soome LogTimerToFD(fd, "m->NextuDNSEvent ", m->NextuDNSEvent);
6038*472cd20dSToomas Soome LogTimerToFD(fd, "m->NextSRVUpdate ", m->NextSRVUpdate);
6039*472cd20dSToomas Soome LogTimerToFD(fd, "m->NextScheduledNATOp ", m->NextScheduledNATOp);
6040*472cd20dSToomas Soome LogTimerToFD(fd, "m->retryGetAddr ", m->retryGetAddr);
6041*472cd20dSToomas Soome #endif
6042c65ebfc7SToomas Soome
6043*472cd20dSToomas Soome LogTimerToFD(fd, "m->NextCacheCheck ", m->NextCacheCheck);
6044*472cd20dSToomas Soome LogTimerToFD(fd, "m->NextScheduledSPS ", m->NextScheduledSPS);
6045*472cd20dSToomas Soome LogTimerToFD(fd, "m->NextScheduledKA ", m->NextScheduledKA);
6046c65ebfc7SToomas Soome
6047*472cd20dSToomas Soome #if MDNSRESPONDER_SUPPORTS(APPLE, BONJOUR_ON_DEMAND)
6048*472cd20dSToomas Soome LogTimerToFD(fd, "m->NextBonjourDisableTime ", m->NextBonjourDisableTime);
6049*472cd20dSToomas Soome #endif
6050c65ebfc7SToomas Soome
6051*472cd20dSToomas Soome LogTimerToFD(fd, "m->NextScheduledSPRetry ", m->NextScheduledSPRetry);
6052*472cd20dSToomas Soome LogTimerToFD(fd, "m->DelaySleep ", m->DelaySleep);
6053*472cd20dSToomas Soome
6054*472cd20dSToomas Soome LogTimerToFD(fd, "m->NextScheduledQuery ", m->NextScheduledQuery);
6055*472cd20dSToomas Soome LogTimerToFD(fd, "m->NextScheduledProbe ", m->NextScheduledProbe);
6056*472cd20dSToomas Soome LogTimerToFD(fd, "m->NextScheduledResponse", m->NextScheduledResponse);
6057*472cd20dSToomas Soome
6058*472cd20dSToomas Soome LogTimerToFD(fd, "m->SuppressSending ", m->SuppressSending);
6059*472cd20dSToomas Soome LogTimerToFD(fd, "m->SuppressProbes ", m->SuppressProbes);
6060*472cd20dSToomas Soome LogTimerToFD(fd, "m->ProbeFailTime ", m->ProbeFailTime);
6061*472cd20dSToomas Soome LogTimerToFD(fd, "m->DelaySleep ", m->DelaySleep);
6062*472cd20dSToomas Soome LogTimerToFD(fd, "m->SleepLimit ", m->SleepLimit);
6063*472cd20dSToomas Soome LogTimerToFD(fd, "m->NextScheduledStopTime ", m->NextScheduledStopTime);
6064c65ebfc7SToomas Soome }
6065c65ebfc7SToomas Soome
6066*472cd20dSToomas Soome #if MDNS_MALLOC_DEBUGGING
udsserver_validatelists(void * context)6067*472cd20dSToomas Soome mDNSlocal void udsserver_validatelists(void *context)
6068c65ebfc7SToomas Soome {
6069c65ebfc7SToomas Soome const request_state *req, *p;
6070*472cd20dSToomas Soome (void)context; // unused
6071c65ebfc7SToomas Soome for (req = all_requests; req; req=req->next)
6072c65ebfc7SToomas Soome {
6073c65ebfc7SToomas Soome if (req->next == (request_state *)~0 || (req->sd < 0 && req->sd != -2))
6074c65ebfc7SToomas Soome LogMemCorruption("UDS request list: %p is garbage (%d)", req, req->sd);
6075c65ebfc7SToomas Soome
6076c65ebfc7SToomas Soome if (req->primary == req)
6077c65ebfc7SToomas Soome LogMemCorruption("UDS request list: req->primary should not point to self %p/%d", req, req->sd);
6078c65ebfc7SToomas Soome
6079c65ebfc7SToomas Soome if (req->primary && req->replies)
6080c65ebfc7SToomas Soome LogMemCorruption("UDS request list: Subordinate request %p/%d/%p should not have replies (%p)",
6081c65ebfc7SToomas Soome req, req->sd, req->primary && req->replies);
6082c65ebfc7SToomas Soome
6083c65ebfc7SToomas Soome p = req->primary;
6084c65ebfc7SToomas Soome if ((long)p & 3)
6085c65ebfc7SToomas Soome LogMemCorruption("UDS request list: req %p primary %p is misaligned (%d)", req, p, req->sd);
6086c65ebfc7SToomas Soome else if (p && (p->next == (request_state *)~0 || (p->sd < 0 && p->sd != -2)))
6087c65ebfc7SToomas Soome LogMemCorruption("UDS request list: req %p primary %p is garbage (%d)", req, p, p->sd);
6088c65ebfc7SToomas Soome
6089c65ebfc7SToomas Soome reply_state *rep;
6090c65ebfc7SToomas Soome for (rep = req->replies; rep; rep=rep->next)
6091c65ebfc7SToomas Soome if (rep->next == (reply_state *)~0)
6092c65ebfc7SToomas Soome LogMemCorruption("UDS req->replies: %p is garbage", rep);
6093c65ebfc7SToomas Soome
6094c65ebfc7SToomas Soome if (req->terminate == connection_termination)
6095c65ebfc7SToomas Soome {
6096c65ebfc7SToomas Soome registered_record_entry *r;
6097c65ebfc7SToomas Soome for (r = req->u.reg_recs; r; r=r->next)
6098c65ebfc7SToomas Soome if (r->next == (registered_record_entry *)~0)
6099c65ebfc7SToomas Soome LogMemCorruption("UDS req->u.reg_recs: %p is garbage", r);
6100c65ebfc7SToomas Soome }
6101c65ebfc7SToomas Soome else if (req->terminate == regservice_termination_callback)
6102c65ebfc7SToomas Soome {
6103c65ebfc7SToomas Soome service_instance *s;
6104c65ebfc7SToomas Soome for (s = req->u.servicereg.instances; s; s=s->next)
6105c65ebfc7SToomas Soome if (s->next == (service_instance *)~0)
6106c65ebfc7SToomas Soome LogMemCorruption("UDS req->u.servicereg.instances: %p is garbage", s);
6107c65ebfc7SToomas Soome }
6108c65ebfc7SToomas Soome else if (req->terminate == browse_termination_callback)
6109c65ebfc7SToomas Soome {
6110c65ebfc7SToomas Soome browser_t *b;
6111c65ebfc7SToomas Soome for (b = req->u.browser.browsers; b; b=b->next)
6112c65ebfc7SToomas Soome if (b->next == (browser_t *)~0)
6113c65ebfc7SToomas Soome LogMemCorruption("UDS req->u.browser.browsers: %p is garbage", b);
6114c65ebfc7SToomas Soome }
6115c65ebfc7SToomas Soome }
6116c65ebfc7SToomas Soome
6117c65ebfc7SToomas Soome DNameListElem *d;
6118c65ebfc7SToomas Soome for (d = SCPrefBrowseDomains; d; d=d->next)
6119c65ebfc7SToomas Soome if (d->next == (DNameListElem *)~0 || d->name.c[0] > 63)
6120c65ebfc7SToomas Soome LogMemCorruption("SCPrefBrowseDomains: %p is garbage (%d)", d, d->name.c[0]);
6121c65ebfc7SToomas Soome
6122c65ebfc7SToomas Soome ARListElem *b;
6123c65ebfc7SToomas Soome for (b = LocalDomainEnumRecords; b; b=b->next)
6124c65ebfc7SToomas Soome if (b->next == (ARListElem *)~0 || b->ar.resrec.name->c[0] > 63)
6125c65ebfc7SToomas Soome LogMemCorruption("LocalDomainEnumRecords: %p is garbage (%d)", b, b->ar.resrec.name->c[0]);
6126c65ebfc7SToomas Soome
6127c65ebfc7SToomas Soome for (d = AutoBrowseDomains; d; d=d->next)
6128c65ebfc7SToomas Soome if (d->next == (DNameListElem *)~0 || d->name.c[0] > 63)
6129c65ebfc7SToomas Soome LogMemCorruption("AutoBrowseDomains: %p is garbage (%d)", d, d->name.c[0]);
6130c65ebfc7SToomas Soome
6131c65ebfc7SToomas Soome for (d = AutoRegistrationDomains; d; d=d->next)
6132c65ebfc7SToomas Soome if (d->next == (DNameListElem *)~0 || d->name.c[0] > 63)
6133c65ebfc7SToomas Soome LogMemCorruption("AutoRegistrationDomains: %p is garbage (%d)", d, d->name.c[0]);
6134c65ebfc7SToomas Soome }
6135*472cd20dSToomas Soome #endif // MDNS_MALLOC_DEBUGGING
6136c65ebfc7SToomas Soome
send_msg(request_state * const req)6137c65ebfc7SToomas Soome mDNSlocal int send_msg(request_state *const req)
6138c65ebfc7SToomas Soome {
6139c65ebfc7SToomas Soome reply_state *const rep = req->replies; // Send the first waiting reply
6140c65ebfc7SToomas Soome ssize_t nwriten;
6141c65ebfc7SToomas Soome
6142c65ebfc7SToomas Soome ConvertHeaderBytes(rep->mhdr);
6143c65ebfc7SToomas Soome nwriten = send(req->sd, (char *)&rep->mhdr + rep->nwriten, rep->totallen - rep->nwriten, 0);
6144c65ebfc7SToomas Soome ConvertHeaderBytes(rep->mhdr);
6145c65ebfc7SToomas Soome
6146c65ebfc7SToomas Soome if (nwriten < 0)
6147c65ebfc7SToomas Soome {
6148c65ebfc7SToomas Soome if (dnssd_errno == dnssd_EINTR || dnssd_errno == dnssd_EWOULDBLOCK) nwriten = 0;
6149c65ebfc7SToomas Soome else
6150c65ebfc7SToomas Soome {
6151c65ebfc7SToomas Soome #if !defined(PLATFORM_NO_EPIPE)
6152c65ebfc7SToomas Soome if (dnssd_errno == EPIPE)
6153c65ebfc7SToomas Soome return(req->ts = t_terminated);
6154c65ebfc7SToomas Soome else
6155c65ebfc7SToomas Soome #endif
6156c65ebfc7SToomas Soome {
6157c65ebfc7SToomas Soome LogMsg("send_msg ERROR: failed to write %d of %d bytes to fd %d errno %d (%s)",
6158c65ebfc7SToomas Soome rep->totallen - rep->nwriten, rep->totallen, req->sd, dnssd_errno, dnssd_strerror(dnssd_errno));
6159c65ebfc7SToomas Soome return(t_error);
6160c65ebfc7SToomas Soome }
6161c65ebfc7SToomas Soome }
6162c65ebfc7SToomas Soome }
6163c65ebfc7SToomas Soome rep->nwriten += nwriten;
6164c65ebfc7SToomas Soome return (rep->nwriten == rep->totallen) ? t_complete : t_morecoming;
6165c65ebfc7SToomas Soome }
6166c65ebfc7SToomas Soome
udsserver_idle(mDNSs32 nextevent)6167c65ebfc7SToomas Soome mDNSexport mDNSs32 udsserver_idle(mDNSs32 nextevent)
6168c65ebfc7SToomas Soome {
6169c65ebfc7SToomas Soome mDNSs32 now = mDNS_TimeNow(&mDNSStorage);
6170c65ebfc7SToomas Soome request_state **req = &all_requests;
6171c65ebfc7SToomas Soome
6172c65ebfc7SToomas Soome while (*req)
6173c65ebfc7SToomas Soome {
6174c65ebfc7SToomas Soome request_state *const r = *req;
6175c65ebfc7SToomas Soome
6176c65ebfc7SToomas Soome if (r->terminate == resolve_termination_callback)
6177c65ebfc7SToomas Soome if (r->u.resolve.ReportTime && now - r->u.resolve.ReportTime >= 0)
6178c65ebfc7SToomas Soome {
6179c65ebfc7SToomas Soome r->u.resolve.ReportTime = 0;
6180c65ebfc7SToomas Soome // if client received results and resolve still active
6181c65ebfc7SToomas Soome if (r->u.resolve.txt && r->u.resolve.srv)
6182c65ebfc7SToomas Soome LogMsgNoIdent("Client application PID[%d](%s) has received results for DNSServiceResolve(%##s) yet remains active over two minutes.", r->process_id, r->pid_name, r->u.resolve.qsrv.qname.c);
6183c65ebfc7SToomas Soome }
6184c65ebfc7SToomas Soome
6185c65ebfc7SToomas Soome // Note: Only primary req's have reply lists, not subordinate req's.
6186c65ebfc7SToomas Soome while (r->replies) // Send queued replies
6187c65ebfc7SToomas Soome {
6188c65ebfc7SToomas Soome transfer_state result;
6189c65ebfc7SToomas Soome if (r->replies->next)
6190c65ebfc7SToomas Soome r->replies->rhdr->flags |= dnssd_htonl(kDNSServiceFlagsMoreComing);
6191c65ebfc7SToomas Soome result = send_msg(r); // Returns t_morecoming if buffer full because client is not reading
6192c65ebfc7SToomas Soome if (result == t_complete)
6193c65ebfc7SToomas Soome {
6194c65ebfc7SToomas Soome reply_state *fptr = r->replies;
6195c65ebfc7SToomas Soome r->replies = r->replies->next;
6196c65ebfc7SToomas Soome freeL("reply_state/udsserver_idle", fptr);
6197c65ebfc7SToomas Soome r->time_blocked = 0; // reset failure counter after successful send
6198c65ebfc7SToomas Soome r->unresponsiveness_reports = 0;
6199c65ebfc7SToomas Soome continue;
6200c65ebfc7SToomas Soome }
6201c65ebfc7SToomas Soome else if (result == t_terminated)
6202c65ebfc7SToomas Soome {
6203c65ebfc7SToomas Soome LogInfo("%3d: Could not write data to client PID[%d](%s) because connection is terminated by the client", r->sd, r->process_id, r->pid_name);
6204c65ebfc7SToomas Soome abort_request(r);
6205c65ebfc7SToomas Soome }
6206c65ebfc7SToomas Soome else if (result == t_error)
6207c65ebfc7SToomas Soome {
6208c65ebfc7SToomas Soome LogMsg("%3d: Could not write data to client PID[%d](%s) because of error - aborting connection", r->sd, r->process_id, r->pid_name);
6209c65ebfc7SToomas Soome LogClientInfo(r);
6210c65ebfc7SToomas Soome abort_request(r);
6211c65ebfc7SToomas Soome }
6212c65ebfc7SToomas Soome break;
6213c65ebfc7SToomas Soome }
6214c65ebfc7SToomas Soome
6215c65ebfc7SToomas Soome if (r->replies) // If we failed to send everything, check our time_blocked timer
6216c65ebfc7SToomas Soome {
6217c65ebfc7SToomas Soome if (nextevent - now > mDNSPlatformOneSecond)
6218c65ebfc7SToomas Soome nextevent = now + mDNSPlatformOneSecond;
6219c65ebfc7SToomas Soome
6220*472cd20dSToomas Soome LogRedact(MDNS_LOG_CATEGORY_DEFAULT, MDNS_LOG_INFO,
6221*472cd20dSToomas Soome "[R%u] Could not send all replies. Will try again in %d ticks.", r->request_id, nextevent - now);
6222c65ebfc7SToomas Soome if (mDNSStorage.SleepState != SleepState_Awake)
6223c65ebfc7SToomas Soome r->time_blocked = 0;
6224c65ebfc7SToomas Soome else if (!r->time_blocked)
6225c65ebfc7SToomas Soome r->time_blocked = NonZeroTime(now);
6226c65ebfc7SToomas Soome else if (now - r->time_blocked >= 10 * mDNSPlatformOneSecond * (r->unresponsiveness_reports+1))
6227c65ebfc7SToomas Soome {
6228c65ebfc7SToomas Soome int num = 0;
6229c65ebfc7SToomas Soome struct reply_state *x = r->replies;
6230c65ebfc7SToomas Soome while (x)
6231c65ebfc7SToomas Soome {
6232c65ebfc7SToomas Soome num++;
6233c65ebfc7SToomas Soome x=x->next;
6234c65ebfc7SToomas Soome }
6235c65ebfc7SToomas Soome LogMsg("%3d: Could not write data to client PID[%d](%s) after %ld seconds, %d repl%s waiting",
6236c65ebfc7SToomas Soome r->sd, r->process_id, r->pid_name, (now - r->time_blocked) / mDNSPlatformOneSecond, num, num == 1 ? "y" : "ies");
6237c65ebfc7SToomas Soome if (++r->unresponsiveness_reports >= 60)
6238c65ebfc7SToomas Soome {
6239c65ebfc7SToomas Soome LogMsg("%3d: Client PID[%d](%s) unresponsive; aborting connection", r->sd, r->process_id, r->pid_name);
6240c65ebfc7SToomas Soome LogClientInfo(r);
6241c65ebfc7SToomas Soome abort_request(r);
6242c65ebfc7SToomas Soome }
6243c65ebfc7SToomas Soome }
6244c65ebfc7SToomas Soome }
6245c65ebfc7SToomas Soome
6246c65ebfc7SToomas Soome if (!dnssd_SocketValid(r->sd)) // If this request is finished, unlink it from the list and free the memory
6247c65ebfc7SToomas Soome {
6248c65ebfc7SToomas Soome // Since we're already doing a list traversal, we unlink the request directly instead of using AbortUnlinkAndFree()
6249c65ebfc7SToomas Soome *req = r->next;
6250c65ebfc7SToomas Soome freeL("request_state/udsserver_idle", r);
6251c65ebfc7SToomas Soome }
6252c65ebfc7SToomas Soome else
6253c65ebfc7SToomas Soome req = &r->next;
6254c65ebfc7SToomas Soome }
6255c65ebfc7SToomas Soome return nextevent;
6256c65ebfc7SToomas Soome }
6257c65ebfc7SToomas Soome
6258c65ebfc7SToomas Soome struct CompileTimeAssertionChecks_uds_daemon
6259c65ebfc7SToomas Soome {
6260c65ebfc7SToomas Soome // Check our structures are reasonable sizes. Including overly-large buffers, or embedding
6261c65ebfc7SToomas Soome // other overly-large structures instead of having a pointer to them, can inadvertently
6262c65ebfc7SToomas Soome // cause structure sizes (and therefore memory usage) to balloon unreasonably.
6263*472cd20dSToomas Soome char sizecheck_request_state [(sizeof(request_state) <= 3880) ? 1 : -1];
6264c65ebfc7SToomas Soome char sizecheck_registered_record_entry[(sizeof(registered_record_entry) <= 60) ? 1 : -1];
6265c65ebfc7SToomas Soome char sizecheck_service_instance [(sizeof(service_instance) <= 6552) ? 1 : -1];
6266*472cd20dSToomas Soome char sizecheck_browser_t [(sizeof(browser_t) <= 1480) ? 1 : -1];
6267c65ebfc7SToomas Soome char sizecheck_reply_hdr [(sizeof(reply_hdr) <= 12) ? 1 : -1];
6268c65ebfc7SToomas Soome char sizecheck_reply_state [(sizeof(reply_state) <= 64) ? 1 : -1];
6269c65ebfc7SToomas Soome };
6270c65ebfc7SToomas Soome
6271c65ebfc7SToomas Soome #ifdef UNIT_TEST
6272c65ebfc7SToomas Soome #include "../unittests/uds_daemon_ut.c"
6273c65ebfc7SToomas Soome #endif // UNIT_TEST
6274