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