1c65ebfc7SToomas Soome /* -*- Mode: C; tab-width: 4 -*-
2c65ebfc7SToomas Soome *
3*472cd20dSToomas Soome * Copyright (c) 2003-2020 Apple Inc. All rights reserved.
4c65ebfc7SToomas Soome *
5c65ebfc7SToomas Soome * Redistribution and use in source and binary forms, with or without
6c65ebfc7SToomas Soome * modification, are permitted provided that the following conditions are met:
7c65ebfc7SToomas Soome *
8c65ebfc7SToomas Soome * 1. Redistributions of source code must retain the above copyright notice,
9c65ebfc7SToomas Soome * this list of conditions and the following disclaimer.
10c65ebfc7SToomas Soome * 2. Redistributions in binary form must reproduce the above copyright notice,
11c65ebfc7SToomas Soome * this list of conditions and the following disclaimer in the documentation
12c65ebfc7SToomas Soome * and/or other materials provided with the distribution.
13c65ebfc7SToomas Soome * 3. Neither the name of Apple Inc. ("Apple") nor the names of its
14c65ebfc7SToomas Soome * contributors may be used to endorse or promote products derived from this
15c65ebfc7SToomas Soome * software without specific prior written permission.
16c65ebfc7SToomas Soome *
17c65ebfc7SToomas Soome * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18c65ebfc7SToomas Soome * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19c65ebfc7SToomas Soome * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20c65ebfc7SToomas Soome * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21c65ebfc7SToomas Soome * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22c65ebfc7SToomas Soome * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23c65ebfc7SToomas Soome * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24c65ebfc7SToomas Soome * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25c65ebfc7SToomas Soome * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26c65ebfc7SToomas Soome * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27c65ebfc7SToomas Soome */
28c65ebfc7SToomas Soome
29c65ebfc7SToomas Soome #include <errno.h>
30c65ebfc7SToomas Soome #include <stdlib.h>
313b436d06SToomas Soome #include <fcntl.h>
32c65ebfc7SToomas Soome
33c65ebfc7SToomas Soome #include "dnssd_ipc.h"
34c65ebfc7SToomas Soome
35c65ebfc7SToomas Soome #if APPLE_OSX_mDNSResponder
36c65ebfc7SToomas Soome #include <mach-o/dyld.h>
37c65ebfc7SToomas Soome #include <uuid/uuid.h>
38c65ebfc7SToomas Soome #include <TargetConditionals.h>
39*472cd20dSToomas Soome #include "dns_sd_private.h"
40*472cd20dSToomas Soome #include "dnssd_clientstub_apple.h"
41*472cd20dSToomas Soome #include <CoreUtils/CommonServices.h>
42*472cd20dSToomas Soome #if !defined(__i386__)
43*472cd20dSToomas Soome #define CHECK_BUNDLE_VERSION 1
44*472cd20dSToomas Soome #else
45*472cd20dSToomas Soome #define CHECK_BUNDLE_VERSION 0
46*472cd20dSToomas Soome #endif
47c65ebfc7SToomas Soome #endif
48c65ebfc7SToomas Soome
49c65ebfc7SToomas Soome #if defined(_WIN32)
50c65ebfc7SToomas Soome
51c65ebfc7SToomas Soome #define _SSIZE_T
52c65ebfc7SToomas Soome #include <CommonServices.h>
53c65ebfc7SToomas Soome #include <DebugServices.h>
54c65ebfc7SToomas Soome #include <winsock2.h>
55c65ebfc7SToomas Soome #include <ws2tcpip.h>
56c65ebfc7SToomas Soome #include <windows.h>
57c65ebfc7SToomas Soome #include <stdarg.h>
58c65ebfc7SToomas Soome #include <stdio.h>
59c65ebfc7SToomas Soome
60c65ebfc7SToomas Soome #define sockaddr_mdns sockaddr_in
61c65ebfc7SToomas Soome #define AF_MDNS AF_INET
62c65ebfc7SToomas Soome
63c65ebfc7SToomas Soome // Disable warning: "'type cast' : from data pointer 'void *' to function pointer"
64c65ebfc7SToomas Soome #pragma warning(disable:4055)
65c65ebfc7SToomas Soome
66c65ebfc7SToomas Soome // Disable warning: "nonstandard extension, function/data pointer conversion in expression"
67c65ebfc7SToomas Soome #pragma warning(disable:4152)
68c65ebfc7SToomas Soome
69c65ebfc7SToomas Soome extern BOOL IsSystemServiceDisabled();
70c65ebfc7SToomas Soome
71c65ebfc7SToomas Soome #define sleep(X) Sleep((X) * 1000)
72c65ebfc7SToomas Soome
73c65ebfc7SToomas Soome static int g_initWinsock = 0;
74c65ebfc7SToomas Soome #define LOG_WARNING kDebugLevelWarning
75c65ebfc7SToomas Soome #define LOG_INFO kDebugLevelInfo
syslog(int priority,const char * message,...)76c65ebfc7SToomas Soome static void syslog( int priority, const char * message, ...)
77c65ebfc7SToomas Soome {
78c65ebfc7SToomas Soome va_list args;
79c65ebfc7SToomas Soome int len;
80c65ebfc7SToomas Soome char * buffer;
81c65ebfc7SToomas Soome DWORD err = WSAGetLastError();
82c65ebfc7SToomas Soome (void) priority;
83c65ebfc7SToomas Soome va_start( args, message );
84c65ebfc7SToomas Soome len = _vscprintf( message, args ) + 1;
85c65ebfc7SToomas Soome buffer = malloc( len * sizeof(char) );
86c65ebfc7SToomas Soome if ( buffer ) { vsnprintf( buffer, len, message, args ); OutputDebugString( buffer ); free( buffer ); }
87c65ebfc7SToomas Soome WSASetLastError( err );
88c65ebfc7SToomas Soome }
89c65ebfc7SToomas Soome #else
90c65ebfc7SToomas Soome
91*472cd20dSToomas Soome #include <fcntl.h> // For O_RDWR etc.
92c65ebfc7SToomas Soome #include <sys/time.h>
93c65ebfc7SToomas Soome #include <sys/socket.h>
94c65ebfc7SToomas Soome #include <syslog.h>
95c65ebfc7SToomas Soome
96c65ebfc7SToomas Soome #define sockaddr_mdns sockaddr_un
97c65ebfc7SToomas Soome #define AF_MDNS AF_LOCAL
98c65ebfc7SToomas Soome
99c65ebfc7SToomas Soome #endif
100c65ebfc7SToomas Soome
101*472cd20dSToomas Soome #if CHECK_BUNDLE_VERSION
102*472cd20dSToomas Soome #include "bundle_utilities.h"
103*472cd20dSToomas Soome #include <os/feature_private.h>
104*472cd20dSToomas Soome #endif
105*472cd20dSToomas Soome
106*472cd20dSToomas Soome #if defined(_WIN32)
107c65ebfc7SToomas Soome // <rdar://problem/4096913> Specifies how many times we'll try and connect to the server.
108c65ebfc7SToomas Soome
109c65ebfc7SToomas Soome #define DNSSD_CLIENT_MAXTRIES 4
110*472cd20dSToomas Soome #endif // _WIN32
111c65ebfc7SToomas Soome
112c65ebfc7SToomas Soome // Uncomment the line below to use the old error return mechanism of creating a temporary named socket (e.g. in /var/tmp)
113c65ebfc7SToomas Soome //#define USE_NAMED_ERROR_RETURN_SOCKET 1
114c65ebfc7SToomas Soome
115c65ebfc7SToomas Soome // If the UDS client has not received a response from the daemon in 60 secs, it is unlikely to get one
116c65ebfc7SToomas Soome // Note: Timeout of 3 secs should be sufficient in normal scenarios, but 60 secs is chosen as a safeguard since
117c65ebfc7SToomas Soome // some clients may come up before mDNSResponder itself after a BOOT and on rare ocassions IOPM/Keychain/D2D calls
118c65ebfc7SToomas Soome // in mDNSResponder's INIT may take a much longer time to return
119c65ebfc7SToomas Soome #define DNSSD_CLIENT_TIMEOUT 60
120c65ebfc7SToomas Soome
121c65ebfc7SToomas Soome #ifndef CTL_PATH_PREFIX
122c65ebfc7SToomas Soome #define CTL_PATH_PREFIX "/var/tmp/dnssd_result_socket."
123c65ebfc7SToomas Soome #endif
124c65ebfc7SToomas Soome
125c65ebfc7SToomas Soome typedef struct
126c65ebfc7SToomas Soome {
127c65ebfc7SToomas Soome ipc_msg_hdr ipc_hdr;
128c65ebfc7SToomas Soome DNSServiceFlags cb_flags;
129c65ebfc7SToomas Soome uint32_t cb_interface;
130c65ebfc7SToomas Soome DNSServiceErrorType cb_err;
131c65ebfc7SToomas Soome } CallbackHeader;
132c65ebfc7SToomas Soome
133c65ebfc7SToomas Soome typedef struct _DNSServiceRef_t DNSServiceOp;
134c65ebfc7SToomas Soome typedef struct _DNSRecordRef_t DNSRecord;
135c65ebfc7SToomas Soome
136c65ebfc7SToomas Soome #if !defined(_WIN32)
137c65ebfc7SToomas Soome typedef struct
138c65ebfc7SToomas Soome {
139c65ebfc7SToomas Soome void *AppCallback; // Client callback function and context
140c65ebfc7SToomas Soome void *AppContext;
141c65ebfc7SToomas Soome } SleepKAContext;
142c65ebfc7SToomas Soome #endif
143c65ebfc7SToomas Soome
144c65ebfc7SToomas Soome // client stub callback to process message from server and deliver results to client application
145c65ebfc7SToomas Soome typedef void (*ProcessReplyFn)(DNSServiceOp *const sdr, const CallbackHeader *const cbh, const char *msg, const char *const end);
146c65ebfc7SToomas Soome
147c65ebfc7SToomas Soome #define ValidatorBits 0x12345678
148c65ebfc7SToomas Soome #define DNSServiceRefValid(X) (dnssd_SocketValid((X)->sockfd) && (((X)->sockfd ^ (X)->validator) == ValidatorBits))
149c65ebfc7SToomas Soome
150c65ebfc7SToomas Soome // When using kDNSServiceFlagsShareConnection, there is one primary _DNSServiceOp_t, and zero or more subordinates
151c65ebfc7SToomas Soome // For the primary, the 'next' field points to the first subordinate, and its 'next' field points to the next, and so on.
152c65ebfc7SToomas Soome // For the primary, the 'primary' field is NULL; for subordinates the 'primary' field points back to the associated primary
153c65ebfc7SToomas Soome //
154c65ebfc7SToomas Soome // _DNS_SD_LIBDISPATCH is defined where libdispatch/GCD is available. This does not mean that the application will use the
155c65ebfc7SToomas Soome // DNSServiceSetDispatchQueue API. Hence any new code guarded with _DNS_SD_LIBDISPATCH should still be backwards compatible.
156c65ebfc7SToomas Soome struct _DNSServiceRef_t
157c65ebfc7SToomas Soome {
158c65ebfc7SToomas Soome DNSServiceOp *next; // For shared connection
159c65ebfc7SToomas Soome DNSServiceOp *primary; // For shared connection
160c65ebfc7SToomas Soome dnssd_sock_t sockfd; // Connected socket between client and daemon
161c65ebfc7SToomas Soome dnssd_sock_t validator; // Used to detect memory corruption, double disposals, etc.
162c65ebfc7SToomas Soome client_context_t uid; // For shared connection requests, each subordinate DNSServiceRef has its own ID,
163c65ebfc7SToomas Soome // unique within the scope of the same shared parent DNSServiceRef
164c65ebfc7SToomas Soome uint32_t op; // request_op_t or reply_op_t
165c65ebfc7SToomas Soome uint32_t max_index; // Largest assigned record index - 0 if no additional records registered
166c65ebfc7SToomas Soome uint32_t logcounter; // Counter used to control number of syslog messages we write
167c65ebfc7SToomas Soome int *moreptr; // Set while DNSServiceProcessResult working on this particular DNSServiceRef
168c65ebfc7SToomas Soome ProcessReplyFn ProcessReply; // Function pointer to the code to handle received messages
169c65ebfc7SToomas Soome void *AppCallback; // Client callback function and context
170c65ebfc7SToomas Soome void *AppContext;
171c65ebfc7SToomas Soome DNSRecord *rec;
172c65ebfc7SToomas Soome #if _DNS_SD_LIBDISPATCH
173c65ebfc7SToomas Soome dispatch_source_t disp_source;
174c65ebfc7SToomas Soome dispatch_queue_t disp_queue;
175c65ebfc7SToomas Soome #endif
176c65ebfc7SToomas Soome void *kacontext;
177c65ebfc7SToomas Soome };
178c65ebfc7SToomas Soome
179c65ebfc7SToomas Soome struct _DNSRecordRef_t
180c65ebfc7SToomas Soome {
181c65ebfc7SToomas Soome DNSRecord *recnext;
182c65ebfc7SToomas Soome void *AppContext;
183c65ebfc7SToomas Soome DNSServiceRegisterRecordReply AppCallback;
184c65ebfc7SToomas Soome DNSRecordRef recref;
185c65ebfc7SToomas Soome uint32_t record_index; // index is unique to the ServiceDiscoveryRef
186c65ebfc7SToomas Soome client_context_t uid; // For demultiplexing multiple DNSServiceRegisterRecord calls
187c65ebfc7SToomas Soome DNSServiceOp *sdr;
188c65ebfc7SToomas Soome };
189c65ebfc7SToomas Soome
190*472cd20dSToomas Soome #if CHECK_BUNDLE_VERSION
_should_return_noauth_error(void)191*472cd20dSToomas Soome static bool _should_return_noauth_error(void)
192*472cd20dSToomas Soome {
193*472cd20dSToomas Soome static dispatch_once_t s_once = 0;
194*472cd20dSToomas Soome static bool s_should = false;
195*472cd20dSToomas Soome dispatch_once(&s_once,
196*472cd20dSToomas Soome ^{
197*472cd20dSToomas Soome s_should = bundle_sdk_is_ios14_or_later();
198*472cd20dSToomas Soome });
199*472cd20dSToomas Soome return s_should;
200*472cd20dSToomas Soome }
201*472cd20dSToomas Soome #endif
202*472cd20dSToomas Soome
203c65ebfc7SToomas Soome #if !defined(USE_TCP_LOOPBACK)
SetUDSPath(struct sockaddr_un * saddr,const char * path)204c65ebfc7SToomas Soome static void SetUDSPath(struct sockaddr_un *saddr, const char *path)
205c65ebfc7SToomas Soome {
206c65ebfc7SToomas Soome size_t pathLen;
207c65ebfc7SToomas Soome
208c65ebfc7SToomas Soome pathLen = strlen(path);
209c65ebfc7SToomas Soome if (pathLen < sizeof(saddr->sun_path))
210c65ebfc7SToomas Soome memcpy(saddr->sun_path, path, pathLen + 1);
211c65ebfc7SToomas Soome else
212c65ebfc7SToomas Soome saddr->sun_path[0] = '\0';
213c65ebfc7SToomas Soome }
214c65ebfc7SToomas Soome #endif
215c65ebfc7SToomas Soome
216*472cd20dSToomas Soome enum { write_all_success = 0, write_all_fail = -1, write_all_defunct = -2 };
217*472cd20dSToomas Soome
218c65ebfc7SToomas Soome // Write len bytes. Return 0 on success, -1 on error
write_all(dnssd_sock_t sd,char * buf,size_t len)219c65ebfc7SToomas Soome static int write_all(dnssd_sock_t sd, char *buf, size_t len)
220c65ebfc7SToomas Soome {
221c65ebfc7SToomas Soome // Don't use "MSG_WAITALL"; it returns "Invalid argument" on some Linux versions; use an explicit while() loop instead.
222*472cd20dSToomas Soome //if (send(sd, buf, len, MSG_WAITALL) != len) return write_all_fail;
223c65ebfc7SToomas Soome while (len)
224c65ebfc7SToomas Soome {
225c65ebfc7SToomas Soome ssize_t num_written = send(sd, buf, (long)len, 0);
226c65ebfc7SToomas Soome if (num_written < 0 || (size_t)num_written > len)
227c65ebfc7SToomas Soome {
228c65ebfc7SToomas Soome // Check whether socket has gone defunct,
229c65ebfc7SToomas Soome // otherwise, an error here indicates some OS bug
230c65ebfc7SToomas Soome // or that the mDNSResponder daemon crashed (which should never happen).
231c65ebfc7SToomas Soome #if !defined(__ppc__) && defined(SO_ISDEFUNCT)
232c65ebfc7SToomas Soome int defunct = 0;
233c65ebfc7SToomas Soome socklen_t dlen = sizeof (defunct);
234c65ebfc7SToomas Soome if (getsockopt(sd, SOL_SOCKET, SO_ISDEFUNCT, &defunct, &dlen) < 0)
235c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub write_all: SO_ISDEFUNCT failed %d %s", dnssd_errno, dnssd_strerror(dnssd_errno));
236c65ebfc7SToomas Soome if (!defunct)
237c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub write_all(%d) failed %ld/%ld %d %s", sd,
238c65ebfc7SToomas Soome (long)num_written, (long)len,
239c65ebfc7SToomas Soome (num_written < 0) ? dnssd_errno : 0,
240c65ebfc7SToomas Soome (num_written < 0) ? dnssd_strerror(dnssd_errno) : "");
241c65ebfc7SToomas Soome else
242c65ebfc7SToomas Soome syslog(LOG_INFO, "dnssd_clientstub write_all(%d) DEFUNCT", sd);
243*472cd20dSToomas Soome return defunct ? write_all_defunct : write_all_fail;
244c65ebfc7SToomas Soome #else
245c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub write_all(%d) failed %ld/%ld %d %s", sd,
246c65ebfc7SToomas Soome (long)num_written, (long)len,
247c65ebfc7SToomas Soome (num_written < 0) ? dnssd_errno : 0,
248c65ebfc7SToomas Soome (num_written < 0) ? dnssd_strerror(dnssd_errno) : "");
249*472cd20dSToomas Soome return write_all_fail;
250c65ebfc7SToomas Soome #endif
251c65ebfc7SToomas Soome }
252c65ebfc7SToomas Soome buf += num_written;
253c65ebfc7SToomas Soome len -= num_written;
254c65ebfc7SToomas Soome }
255*472cd20dSToomas Soome return write_all_success;
256c65ebfc7SToomas Soome }
257c65ebfc7SToomas Soome
258*472cd20dSToomas Soome enum { read_all_success = 0, read_all_fail = -1, read_all_wouldblock = -2, read_all_defunct = -3 };
259c65ebfc7SToomas Soome
260c65ebfc7SToomas Soome // Read len bytes. Return 0 on success, read_all_fail on error, or read_all_wouldblock for
read_all(dnssd_sock_t sd,char * buf,int len)261c65ebfc7SToomas Soome static int read_all(dnssd_sock_t sd, char *buf, int len)
262c65ebfc7SToomas Soome {
263c65ebfc7SToomas Soome // Don't use "MSG_WAITALL"; it returns "Invalid argument" on some Linux versions; use an explicit while() loop instead.
264c65ebfc7SToomas Soome //if (recv(sd, buf, len, MSG_WAITALL) != len) return -1;
265c65ebfc7SToomas Soome
266c65ebfc7SToomas Soome while (len)
267c65ebfc7SToomas Soome {
268c65ebfc7SToomas Soome ssize_t num_read = recv(sd, buf, len, 0);
269c65ebfc7SToomas Soome // It is valid to get an interrupted system call error e.g., somebody attaching
270c65ebfc7SToomas Soome // in a debugger, retry without failing
271c65ebfc7SToomas Soome if ((num_read < 0) && (errno == EINTR))
272c65ebfc7SToomas Soome {
273c65ebfc7SToomas Soome syslog(LOG_INFO, "dnssd_clientstub read_all: EINTR continue");
274c65ebfc7SToomas Soome continue;
275c65ebfc7SToomas Soome }
276c65ebfc7SToomas Soome if ((num_read == 0) || (num_read < 0) || (num_read > len))
277c65ebfc7SToomas Soome {
278c65ebfc7SToomas Soome int printWarn = 0;
279c65ebfc7SToomas Soome int defunct = 0;
280c65ebfc7SToomas Soome
281c65ebfc7SToomas Soome // Check whether socket has gone defunct,
282c65ebfc7SToomas Soome // otherwise, an error here indicates some OS bug
283c65ebfc7SToomas Soome // or that the mDNSResponder daemon crashed (which should never happen).
284c65ebfc7SToomas Soome #if defined(WIN32)
285c65ebfc7SToomas Soome // <rdar://problem/7481776> Suppress logs for "A non-blocking socket operation
286c65ebfc7SToomas Soome // could not be completed immediately"
287c65ebfc7SToomas Soome if (WSAGetLastError() != WSAEWOULDBLOCK)
288c65ebfc7SToomas Soome printWarn = 1;
289c65ebfc7SToomas Soome #endif
290c65ebfc7SToomas Soome #if !defined(__ppc__) && defined(SO_ISDEFUNCT)
291c65ebfc7SToomas Soome {
292c65ebfc7SToomas Soome socklen_t dlen = sizeof (defunct);
293c65ebfc7SToomas Soome if (getsockopt(sd, SOL_SOCKET, SO_ISDEFUNCT, &defunct, &dlen) < 0)
294c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub read_all: SO_ISDEFUNCT failed %d %s", dnssd_errno, dnssd_strerror(dnssd_errno));
295c65ebfc7SToomas Soome }
296c65ebfc7SToomas Soome if (!defunct)
297c65ebfc7SToomas Soome printWarn = 1;
298c65ebfc7SToomas Soome #endif
299c65ebfc7SToomas Soome if (printWarn)
300c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub read_all(%d) failed %ld/%ld %d %s", sd,
301c65ebfc7SToomas Soome (long)num_read, (long)len,
302c65ebfc7SToomas Soome (num_read < 0) ? dnssd_errno : 0,
303c65ebfc7SToomas Soome (num_read < 0) ? dnssd_strerror(dnssd_errno) : "");
304c65ebfc7SToomas Soome else if (defunct)
305c65ebfc7SToomas Soome syslog(LOG_INFO, "dnssd_clientstub read_all(%d) DEFUNCT", sd);
306*472cd20dSToomas Soome return (num_read < 0 && dnssd_errno == dnssd_EWOULDBLOCK) ? read_all_wouldblock : (defunct ? read_all_defunct : read_all_fail);
307c65ebfc7SToomas Soome }
308c65ebfc7SToomas Soome buf += num_read;
309c65ebfc7SToomas Soome len -= num_read;
310c65ebfc7SToomas Soome }
311c65ebfc7SToomas Soome return read_all_success;
312c65ebfc7SToomas Soome }
313c65ebfc7SToomas Soome
314c65ebfc7SToomas Soome // Returns 1 if more bytes remain to be read on socket descriptor sd, 0 otherwise
more_bytes(dnssd_sock_t sd)315c65ebfc7SToomas Soome static int more_bytes(dnssd_sock_t sd)
316c65ebfc7SToomas Soome {
317c65ebfc7SToomas Soome struct timeval tv = { 0, 0 };
318c65ebfc7SToomas Soome fd_set readfds;
319c65ebfc7SToomas Soome fd_set *fs;
320c65ebfc7SToomas Soome int ret;
321c65ebfc7SToomas Soome
322c65ebfc7SToomas Soome #if defined(_WIN32)
323c65ebfc7SToomas Soome fs = &readfds;
324c65ebfc7SToomas Soome FD_ZERO(fs);
325c65ebfc7SToomas Soome FD_SET(sd, fs);
326c65ebfc7SToomas Soome ret = select((int)sd+1, fs, (fd_set*)NULL, (fd_set*)NULL, &tv);
327c65ebfc7SToomas Soome #else
328*472cd20dSToomas Soome // This whole thing would probably be better done using kevent() instead of select()
329c65ebfc7SToomas Soome if (sd < FD_SETSIZE)
330c65ebfc7SToomas Soome {
331c65ebfc7SToomas Soome fs = &readfds;
332c65ebfc7SToomas Soome FD_ZERO(fs);
333c65ebfc7SToomas Soome }
334c65ebfc7SToomas Soome else
335c65ebfc7SToomas Soome {
336c65ebfc7SToomas Soome // Compute the number of integers needed for storing "sd". Internally fd_set is stored
337c65ebfc7SToomas Soome // as an array of ints with one bit for each fd and hence we need to compute
338c65ebfc7SToomas Soome // the number of ints needed rather than the number of bytes. If "sd" is 32, we need
339c65ebfc7SToomas Soome // two ints and not just one.
340c65ebfc7SToomas Soome int nfdbits = sizeof (int) * 8;
341c65ebfc7SToomas Soome int nints = (sd/nfdbits) + 1;
342c65ebfc7SToomas Soome fs = (fd_set *)calloc(nints, (size_t)sizeof(int));
343c65ebfc7SToomas Soome if (fs == NULL)
344c65ebfc7SToomas Soome {
345c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub more_bytes: malloc failed");
346c65ebfc7SToomas Soome return 0;
347c65ebfc7SToomas Soome }
348c65ebfc7SToomas Soome }
349c65ebfc7SToomas Soome FD_SET(sd, fs);
350c65ebfc7SToomas Soome ret = select((int)sd+1, fs, (fd_set*)NULL, (fd_set*)NULL, &tv);
351c65ebfc7SToomas Soome if (fs != &readfds)
352c65ebfc7SToomas Soome free(fs);
353c65ebfc7SToomas Soome #endif
354c65ebfc7SToomas Soome return (ret > 0);
355c65ebfc7SToomas Soome }
356c65ebfc7SToomas Soome
357c65ebfc7SToomas Soome // set_waitlimit() implements a timeout using select. It is called from deliver_request() before recv() OR accept()
358c65ebfc7SToomas Soome // to ensure the UDS clients are not blocked in these system calls indefinitely.
359c65ebfc7SToomas Soome // Note: Ideally one should never be blocked here, because it indicates either mDNSResponder daemon is not yet up/hung/
360c65ebfc7SToomas Soome // superbusy/crashed or some other OS bug. For eg: On Windows which suffers from 3rd party software
361c65ebfc7SToomas Soome // (primarily 3rd party firewall software) interfering with proper functioning of the TCP protocol stack it is possible
362c65ebfc7SToomas Soome // the next operation on this socket(recv/accept) is blocked since we depend on TCP to communicate with the system service.
set_waitlimit(dnssd_sock_t sock,int timeout)363c65ebfc7SToomas Soome static int set_waitlimit(dnssd_sock_t sock, int timeout)
364c65ebfc7SToomas Soome {
365c65ebfc7SToomas Soome int gDaemonErr = kDNSServiceErr_NoError;
366c65ebfc7SToomas Soome
367*472cd20dSToomas Soome // The comment below is wrong. The select() routine does not cause stack corruption.
368*472cd20dSToomas Soome // The use of FD_SET out of range for the bitmap is what causes stack corruption.
369*472cd20dSToomas Soome // For how to do this correctly, see the example using calloc() in more_bytes() above.
370*472cd20dSToomas Soome // Even better, both should be changed to use kevent() instead of select().
371c65ebfc7SToomas Soome // To prevent stack corruption since select does not work with timeout if fds > FD_SETSIZE(1024)
372c65ebfc7SToomas Soome if (!gDaemonErr && sock < FD_SETSIZE)
373c65ebfc7SToomas Soome {
374c65ebfc7SToomas Soome struct timeval tv;
375c65ebfc7SToomas Soome fd_set set;
376c65ebfc7SToomas Soome
377c65ebfc7SToomas Soome FD_ZERO(&set);
378c65ebfc7SToomas Soome FD_SET(sock, &set);
379c65ebfc7SToomas Soome tv.tv_sec = timeout;
380c65ebfc7SToomas Soome tv.tv_usec = 0;
381c65ebfc7SToomas Soome if (!select((int)(sock + 1), &set, NULL, NULL, &tv))
382c65ebfc7SToomas Soome {
383c65ebfc7SToomas Soome // Ideally one should never hit this case: See comments before set_waitlimit()
384c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub set_waitlimit:_daemon timed out (%d secs) without any response: Socket %d", timeout, sock);
385c65ebfc7SToomas Soome gDaemonErr = kDNSServiceErr_Timeout;
386c65ebfc7SToomas Soome }
387c65ebfc7SToomas Soome }
388c65ebfc7SToomas Soome return gDaemonErr;
389c65ebfc7SToomas Soome }
390c65ebfc7SToomas Soome
391c65ebfc7SToomas Soome /* create_hdr
392c65ebfc7SToomas Soome *
393c65ebfc7SToomas Soome * allocate and initialize an ipc message header. Value of len should initially be the
394c65ebfc7SToomas Soome * length of the data, and is set to the value of the data plus the header. data_start
395c65ebfc7SToomas Soome * is set to point to the beginning of the data section. SeparateReturnSocket should be
396c65ebfc7SToomas Soome * non-zero for calls that can't receive an immediate error return value on their primary
397c65ebfc7SToomas Soome * socket, and therefore require a separate return path for the error code result.
398c65ebfc7SToomas Soome * if zero, the path to a control socket is appended at the beginning of the message buffer.
399c65ebfc7SToomas Soome * data_start is set past this string.
400c65ebfc7SToomas Soome */
create_hdr(uint32_t op,size_t * len,char ** data_start,int SeparateReturnSocket,DNSServiceOp * ref)401c65ebfc7SToomas Soome static ipc_msg_hdr *create_hdr(uint32_t op, size_t *len, char **data_start, int SeparateReturnSocket, DNSServiceOp *ref)
402c65ebfc7SToomas Soome {
403c65ebfc7SToomas Soome char *msg = NULL;
404c65ebfc7SToomas Soome ipc_msg_hdr *hdr;
405c65ebfc7SToomas Soome int datalen;
406c65ebfc7SToomas Soome #if !defined(USE_TCP_LOOPBACK)
407c65ebfc7SToomas Soome char ctrl_path[64] = ""; // "/var/tmp/dnssd_result_socket.xxxxxxxxxx-xxx-xxxxxx"
408c65ebfc7SToomas Soome #endif
409c65ebfc7SToomas Soome
410c65ebfc7SToomas Soome if (SeparateReturnSocket)
411c65ebfc7SToomas Soome {
412c65ebfc7SToomas Soome #if defined(USE_TCP_LOOPBACK)
413c65ebfc7SToomas Soome *len += 2; // Allocate space for two-byte port number
414c65ebfc7SToomas Soome #elif defined(USE_NAMED_ERROR_RETURN_SOCKET)
415c65ebfc7SToomas Soome struct timeval tv;
416c65ebfc7SToomas Soome if (gettimeofday(&tv, NULL) < 0)
417c65ebfc7SToomas Soome { syslog(LOG_WARNING, "dnssd_clientstub create_hdr: gettimeofday failed %d %s", dnssd_errno, dnssd_strerror(dnssd_errno)); return NULL; }
418c65ebfc7SToomas Soome snprintf(ctrl_path, sizeof(ctrl_path), "%s%d-%.3lx-%.6lu", CTL_PATH_PREFIX, (int)getpid(),
419c65ebfc7SToomas Soome (unsigned long)(tv.tv_sec & 0xFFF), (unsigned long)(tv.tv_usec));
420c65ebfc7SToomas Soome *len += strlen(ctrl_path) + 1;
421c65ebfc7SToomas Soome #else
422c65ebfc7SToomas Soome *len += 1; // Allocate space for single zero byte (empty C string)
423c65ebfc7SToomas Soome #endif
424c65ebfc7SToomas Soome }
425c65ebfc7SToomas Soome
426c65ebfc7SToomas Soome datalen = (int) *len;
427c65ebfc7SToomas Soome *len += sizeof(ipc_msg_hdr);
428c65ebfc7SToomas Soome
429c65ebfc7SToomas Soome // Write message to buffer
430c65ebfc7SToomas Soome msg = malloc(*len);
431c65ebfc7SToomas Soome if (!msg) { syslog(LOG_WARNING, "dnssd_clientstub create_hdr: malloc failed"); return NULL; }
432c65ebfc7SToomas Soome
433c65ebfc7SToomas Soome memset(msg, 0, *len);
434c65ebfc7SToomas Soome hdr = (ipc_msg_hdr *)msg;
435c65ebfc7SToomas Soome hdr->version = VERSION;
436c65ebfc7SToomas Soome hdr->datalen = datalen;
437c65ebfc7SToomas Soome hdr->ipc_flags = 0;
438c65ebfc7SToomas Soome hdr->op = op;
439c65ebfc7SToomas Soome hdr->client_context = ref->uid;
440c65ebfc7SToomas Soome hdr->reg_index = 0;
441c65ebfc7SToomas Soome *data_start = msg + sizeof(ipc_msg_hdr);
442c65ebfc7SToomas Soome #if defined(USE_TCP_LOOPBACK)
443c65ebfc7SToomas Soome // Put dummy data in for the port, since we don't know what it is yet.
444c65ebfc7SToomas Soome // The data will get filled in before we send the message. This happens in deliver_request().
445c65ebfc7SToomas Soome if (SeparateReturnSocket) put_uint16(0, data_start);
446c65ebfc7SToomas Soome #else
447c65ebfc7SToomas Soome if (SeparateReturnSocket) put_string(ctrl_path, data_start);
448c65ebfc7SToomas Soome #endif
449c65ebfc7SToomas Soome return hdr;
450c65ebfc7SToomas Soome }
451c65ebfc7SToomas Soome
FreeDNSRecords(DNSServiceOp * sdRef)452c65ebfc7SToomas Soome static void FreeDNSRecords(DNSServiceOp *sdRef)
453c65ebfc7SToomas Soome {
454c65ebfc7SToomas Soome DNSRecord *rec = sdRef->rec;
455c65ebfc7SToomas Soome while (rec)
456c65ebfc7SToomas Soome {
457c65ebfc7SToomas Soome DNSRecord *next = rec->recnext;
458c65ebfc7SToomas Soome free(rec);
459c65ebfc7SToomas Soome rec = next;
460c65ebfc7SToomas Soome }
461c65ebfc7SToomas Soome }
462c65ebfc7SToomas Soome
FreeDNSServiceOp(DNSServiceOp * x)463c65ebfc7SToomas Soome static void FreeDNSServiceOp(DNSServiceOp *x)
464c65ebfc7SToomas Soome {
465c65ebfc7SToomas Soome // We don't use our DNSServiceRefValid macro here because if we're cleaning up after a socket() call failed
466c65ebfc7SToomas Soome // then sockfd could legitimately contain a failing value (e.g. dnssd_InvalidSocket)
467c65ebfc7SToomas Soome if ((x->sockfd ^ x->validator) != ValidatorBits)
468c65ebfc7SToomas Soome {
469c65ebfc7SToomas Soome }
470c65ebfc7SToomas Soome else
471c65ebfc7SToomas Soome {
472c65ebfc7SToomas Soome x->next = NULL;
473c65ebfc7SToomas Soome x->primary = NULL;
474c65ebfc7SToomas Soome x->sockfd = dnssd_InvalidSocket;
475c65ebfc7SToomas Soome x->validator = 0xDDDDDDDD;
476c65ebfc7SToomas Soome x->op = request_op_none;
477c65ebfc7SToomas Soome x->max_index = 0;
478c65ebfc7SToomas Soome x->logcounter = 0;
479c65ebfc7SToomas Soome x->moreptr = NULL;
480c65ebfc7SToomas Soome x->ProcessReply = NULL;
481c65ebfc7SToomas Soome x->AppCallback = NULL;
482c65ebfc7SToomas Soome x->AppContext = NULL;
483c65ebfc7SToomas Soome #if _DNS_SD_LIBDISPATCH
484c65ebfc7SToomas Soome if (x->disp_source) dispatch_release(x->disp_source);
485c65ebfc7SToomas Soome x->disp_source = NULL;
486c65ebfc7SToomas Soome x->disp_queue = NULL;
487c65ebfc7SToomas Soome #endif
488c65ebfc7SToomas Soome // DNSRecords may have been added to subordinate sdRef e.g., DNSServiceRegister/DNSServiceAddRecord
489c65ebfc7SToomas Soome // or on the main sdRef e.g., DNSServiceCreateConnection/DNSServiceRegisterRecord.
490c65ebfc7SToomas Soome // DNSRecords may have been freed if the application called DNSRemoveRecord.
491c65ebfc7SToomas Soome FreeDNSRecords(x);
492c65ebfc7SToomas Soome if (x->kacontext)
493c65ebfc7SToomas Soome {
494c65ebfc7SToomas Soome free(x->kacontext);
495c65ebfc7SToomas Soome x->kacontext = NULL;
496c65ebfc7SToomas Soome }
497c65ebfc7SToomas Soome free(x);
498c65ebfc7SToomas Soome }
499c65ebfc7SToomas Soome }
500c65ebfc7SToomas Soome
501c65ebfc7SToomas Soome // Return a connected service ref (deallocate with DNSServiceRefDeallocate)
ConnectToServer(DNSServiceRef * ref,DNSServiceFlags flags,uint32_t op,ProcessReplyFn ProcessReply,void * AppCallback,void * AppContext)502c65ebfc7SToomas Soome static DNSServiceErrorType ConnectToServer(DNSServiceRef *ref, DNSServiceFlags flags, uint32_t op, ProcessReplyFn ProcessReply, void *AppCallback, void *AppContext)
503c65ebfc7SToomas Soome {
504*472cd20dSToomas Soome #if defined(_WIN32)
505c65ebfc7SToomas Soome int NumTries = 0;
506*472cd20dSToomas Soome #endif // _WIN32
507c65ebfc7SToomas Soome
508c65ebfc7SToomas Soome dnssd_sockaddr_t saddr;
509c65ebfc7SToomas Soome DNSServiceOp *sdr;
510c65ebfc7SToomas Soome
511c65ebfc7SToomas Soome if (!ref)
512c65ebfc7SToomas Soome {
513c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSService operation with NULL DNSServiceRef");
514c65ebfc7SToomas Soome return kDNSServiceErr_BadParam;
515c65ebfc7SToomas Soome }
516c65ebfc7SToomas Soome
517c65ebfc7SToomas Soome if (flags & kDNSServiceFlagsShareConnection)
518c65ebfc7SToomas Soome {
519c65ebfc7SToomas Soome if (!*ref)
520c65ebfc7SToomas Soome {
521c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub kDNSServiceFlagsShareConnection used with NULL DNSServiceRef");
522c65ebfc7SToomas Soome return kDNSServiceErr_BadParam;
523c65ebfc7SToomas Soome }
524c65ebfc7SToomas Soome if (!DNSServiceRefValid(*ref) || ((*ref)->op != connection_request && (*ref)->op != connection_delegate_request) || (*ref)->primary)
525c65ebfc7SToomas Soome {
526c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub kDNSServiceFlagsShareConnection used with invalid DNSServiceRef %p %08X %08X op %d",
527c65ebfc7SToomas Soome (*ref), (*ref)->sockfd, (*ref)->validator, (*ref)->op);
528c65ebfc7SToomas Soome *ref = NULL;
529c65ebfc7SToomas Soome return kDNSServiceErr_BadReference;
530c65ebfc7SToomas Soome }
531c65ebfc7SToomas Soome }
532c65ebfc7SToomas Soome
533c65ebfc7SToomas Soome #if defined(_WIN32)
534c65ebfc7SToomas Soome if (!g_initWinsock)
535c65ebfc7SToomas Soome {
536c65ebfc7SToomas Soome WSADATA wsaData;
537c65ebfc7SToomas Soome g_initWinsock = 1;
538c65ebfc7SToomas Soome if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) { *ref = NULL; return kDNSServiceErr_ServiceNotRunning; }
539c65ebfc7SToomas Soome }
540c65ebfc7SToomas Soome // <rdar://problem/4096913> If the system service is disabled, we only want to try to connect once
541c65ebfc7SToomas Soome if (IsSystemServiceDisabled())
542c65ebfc7SToomas Soome NumTries = DNSSD_CLIENT_MAXTRIES;
543c65ebfc7SToomas Soome #endif
544c65ebfc7SToomas Soome
545c65ebfc7SToomas Soome sdr = malloc(sizeof(DNSServiceOp));
546c65ebfc7SToomas Soome if (!sdr)
547c65ebfc7SToomas Soome {
548c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub ConnectToServer: malloc failed");
549c65ebfc7SToomas Soome *ref = NULL;
550c65ebfc7SToomas Soome return kDNSServiceErr_NoMemory;
551c65ebfc7SToomas Soome }
552c65ebfc7SToomas Soome sdr->next = NULL;
553c65ebfc7SToomas Soome sdr->primary = NULL;
554c65ebfc7SToomas Soome sdr->sockfd = dnssd_InvalidSocket;
555c65ebfc7SToomas Soome sdr->validator = sdr->sockfd ^ ValidatorBits;
556c65ebfc7SToomas Soome sdr->op = op;
557c65ebfc7SToomas Soome sdr->max_index = 0;
558c65ebfc7SToomas Soome sdr->logcounter = 0;
559c65ebfc7SToomas Soome sdr->moreptr = NULL;
560c65ebfc7SToomas Soome sdr->uid.u32[0] = 0;
561c65ebfc7SToomas Soome sdr->uid.u32[1] = 0;
562c65ebfc7SToomas Soome sdr->ProcessReply = ProcessReply;
563c65ebfc7SToomas Soome sdr->AppCallback = AppCallback;
564c65ebfc7SToomas Soome sdr->AppContext = AppContext;
565c65ebfc7SToomas Soome sdr->rec = NULL;
566c65ebfc7SToomas Soome #if _DNS_SD_LIBDISPATCH
567c65ebfc7SToomas Soome sdr->disp_source = NULL;
568c65ebfc7SToomas Soome sdr->disp_queue = NULL;
569c65ebfc7SToomas Soome #endif
570c65ebfc7SToomas Soome sdr->kacontext = NULL;
571c65ebfc7SToomas Soome
572c65ebfc7SToomas Soome if (flags & kDNSServiceFlagsShareConnection)
573c65ebfc7SToomas Soome {
574c65ebfc7SToomas Soome DNSServiceOp **p = &(*ref)->next; // Append ourselves to end of primary's list
575c65ebfc7SToomas Soome while (*p)
576c65ebfc7SToomas Soome p = &(*p)->next;
577c65ebfc7SToomas Soome *p = sdr;
578c65ebfc7SToomas Soome // Preincrement counter before we use it -- it helps with debugging if we know the all-zeroes ID should never appear
579c65ebfc7SToomas Soome if (++(*ref)->uid.u32[0] == 0)
580c65ebfc7SToomas Soome ++(*ref)->uid.u32[1]; // In parent DNSServiceOp increment UID counter
581c65ebfc7SToomas Soome sdr->primary = *ref; // Set our primary pointer
582c65ebfc7SToomas Soome sdr->sockfd = (*ref)->sockfd; // Inherit primary's socket
583c65ebfc7SToomas Soome sdr->validator = (*ref)->validator;
584c65ebfc7SToomas Soome sdr->uid = (*ref)->uid;
585c65ebfc7SToomas Soome //printf("ConnectToServer sharing socket %d\n", sdr->sockfd);
586c65ebfc7SToomas Soome }
587c65ebfc7SToomas Soome else
588c65ebfc7SToomas Soome {
589c65ebfc7SToomas Soome #ifdef SO_NOSIGPIPE
590c65ebfc7SToomas Soome const unsigned long optval = 1;
591c65ebfc7SToomas Soome #endif
592c65ebfc7SToomas Soome #ifndef USE_TCP_LOOPBACK
593c65ebfc7SToomas Soome char* uds_serverpath = getenv(MDNS_UDS_SERVERPATH_ENVVAR);
594c65ebfc7SToomas Soome if (uds_serverpath == NULL)
595c65ebfc7SToomas Soome uds_serverpath = MDNS_UDS_SERVERPATH;
596c65ebfc7SToomas Soome else if (strlen(uds_serverpath) >= MAX_CTLPATH)
597c65ebfc7SToomas Soome {
598c65ebfc7SToomas Soome uds_serverpath = MDNS_UDS_SERVERPATH;
599c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub ConnectToServer: using default path since env len is invalid");
600c65ebfc7SToomas Soome }
601c65ebfc7SToomas Soome #endif
602c65ebfc7SToomas Soome *ref = NULL;
603c65ebfc7SToomas Soome sdr->sockfd = socket(AF_DNSSD, SOCK_STREAM, 0);
604c65ebfc7SToomas Soome sdr->validator = sdr->sockfd ^ ValidatorBits;
605c65ebfc7SToomas Soome if (!dnssd_SocketValid(sdr->sockfd))
606c65ebfc7SToomas Soome {
607c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub ConnectToServer: socket failed %d %s", dnssd_errno, dnssd_strerror(dnssd_errno));
608c65ebfc7SToomas Soome FreeDNSServiceOp(sdr);
609c65ebfc7SToomas Soome return kDNSServiceErr_NoMemory;
610c65ebfc7SToomas Soome }
611*472cd20dSToomas Soome #if !defined(_WIN32)
612*472cd20dSToomas Soome int fcntl_flags = fcntl(sdr->sockfd, F_GETFD);
613*472cd20dSToomas Soome if (fcntl_flags != -1)
614*472cd20dSToomas Soome {
615*472cd20dSToomas Soome fcntl_flags |= FD_CLOEXEC;
616*472cd20dSToomas Soome int ret = fcntl(sdr->sockfd, F_SETFD, fcntl_flags);
617*472cd20dSToomas Soome if (ret == -1)
618*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub ConnectToServer: Failed to set FD_CLOEXEC on socket %d %s",
619*472cd20dSToomas Soome dnssd_errno, dnssd_strerror(dnssd_errno));
620*472cd20dSToomas Soome }
621*472cd20dSToomas Soome else
622*472cd20dSToomas Soome {
623*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub ConnectToServer: Failed to get the file descriptor flags of socket %d %s",
624*472cd20dSToomas Soome dnssd_errno, dnssd_strerror(dnssd_errno));
625*472cd20dSToomas Soome }
626*472cd20dSToomas Soome #endif // !defined(_WIN32)
627c65ebfc7SToomas Soome #ifdef SO_NOSIGPIPE
628c65ebfc7SToomas Soome // Some environments (e.g. OS X) support turning off SIGPIPE for a socket
629c65ebfc7SToomas Soome if (setsockopt(sdr->sockfd, SOL_SOCKET, SO_NOSIGPIPE, &optval, sizeof(optval)) < 0)
630c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub ConnectToServer: SO_NOSIGPIPE failed %d %s", dnssd_errno, dnssd_strerror(dnssd_errno));
631c65ebfc7SToomas Soome #endif
632c65ebfc7SToomas Soome #if defined(USE_TCP_LOOPBACK)
633c65ebfc7SToomas Soome saddr.sin_family = AF_INET;
634c65ebfc7SToomas Soome saddr.sin_addr.s_addr = inet_addr(MDNS_TCP_SERVERADDR);
635c65ebfc7SToomas Soome saddr.sin_port = htons(MDNS_TCP_SERVERPORT);
636c65ebfc7SToomas Soome #else
637c65ebfc7SToomas Soome saddr.sun_family = AF_LOCAL;
638c65ebfc7SToomas Soome SetUDSPath(&saddr, uds_serverpath);
639c65ebfc7SToomas Soome #if !defined(__ppc__) && defined(SO_DEFUNCTOK)
640c65ebfc7SToomas Soome {
641c65ebfc7SToomas Soome int defunct = 1;
642c65ebfc7SToomas Soome if (setsockopt(sdr->sockfd, SOL_SOCKET, SO_DEFUNCTOK, &defunct, sizeof(defunct)) < 0)
643c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub ConnectToServer: SO_DEFUNCTOK failed %d %s", dnssd_errno, dnssd_strerror(dnssd_errno));
644c65ebfc7SToomas Soome }
645c65ebfc7SToomas Soome #endif
646c65ebfc7SToomas Soome #endif
647c65ebfc7SToomas Soome
648*472cd20dSToomas Soome #if defined(_WIN32)
649c65ebfc7SToomas Soome while (1)
650c65ebfc7SToomas Soome {
651c65ebfc7SToomas Soome int err = connect(sdr->sockfd, (struct sockaddr *) &saddr, sizeof(saddr));
652c65ebfc7SToomas Soome if (!err)
653c65ebfc7SToomas Soome break; // If we succeeded, return sdr
654*472cd20dSToomas Soome
655c65ebfc7SToomas Soome // If we failed, then it may be because the daemon is still launching.
656c65ebfc7SToomas Soome // This can happen for processes that launch early in the boot process, while the
657c65ebfc7SToomas Soome // daemon is still coming up. Rather than fail here, we wait 1 sec and try again.
658c65ebfc7SToomas Soome // If, after DNSSD_CLIENT_MAXTRIES, we still can't connect to the daemon,
659c65ebfc7SToomas Soome // then we give up and return a failure code.
660c65ebfc7SToomas Soome if (++NumTries < DNSSD_CLIENT_MAXTRIES)
661c65ebfc7SToomas Soome {
662c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub ConnectToServer: connect()-> No of tries: %d", NumTries);
663c65ebfc7SToomas Soome sleep(1); // Sleep a bit, then try again
664c65ebfc7SToomas Soome }
665c65ebfc7SToomas Soome else
666c65ebfc7SToomas Soome {
667c65ebfc7SToomas Soome #if !defined(USE_TCP_LOOPBACK)
668c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub ConnectToServer: connect() failed path:%s Socket:%d Err:%d Errno:%d %s",
669c65ebfc7SToomas Soome uds_serverpath, sdr->sockfd, err, dnssd_errno, dnssd_strerror(dnssd_errno));
670c65ebfc7SToomas Soome #endif
671c65ebfc7SToomas Soome dnssd_close(sdr->sockfd);
672c65ebfc7SToomas Soome FreeDNSServiceOp(sdr);
673c65ebfc7SToomas Soome return kDNSServiceErr_ServiceNotRunning;
674c65ebfc7SToomas Soome }
675c65ebfc7SToomas Soome }
676*472cd20dSToomas Soome #else
677*472cd20dSToomas Soome int err = connect(sdr->sockfd, (struct sockaddr *) &saddr, sizeof(saddr));
678*472cd20dSToomas Soome if (err)
679*472cd20dSToomas Soome {
680*472cd20dSToomas Soome #if !defined(USE_TCP_LOOPBACK)
681*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub ConnectToServer: connect() failed path:%s Socket:%d Err:%d Errno:%d %s",
682*472cd20dSToomas Soome uds_serverpath, sdr->sockfd, err, dnssd_errno, dnssd_strerror(dnssd_errno));
683*472cd20dSToomas Soome #endif
684*472cd20dSToomas Soome dnssd_close(sdr->sockfd);
685*472cd20dSToomas Soome FreeDNSServiceOp(sdr);
686*472cd20dSToomas Soome return kDNSServiceErr_ServiceNotRunning;
687*472cd20dSToomas Soome }
688*472cd20dSToomas Soome #endif
689c65ebfc7SToomas Soome }
690c65ebfc7SToomas Soome
691c65ebfc7SToomas Soome *ref = sdr;
692c65ebfc7SToomas Soome return kDNSServiceErr_NoError;
693c65ebfc7SToomas Soome }
694c65ebfc7SToomas Soome
695c65ebfc7SToomas Soome #define deliver_request_bailout(MSG) \
6963b436d06SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub deliver_request: %s failed %d (%s)", (MSG), dnssd_errno, dnssd_strerror(dnssd_errno)); goto cleanup
697c65ebfc7SToomas Soome
deliver_request(ipc_msg_hdr * hdr,DNSServiceOp * sdr)698c65ebfc7SToomas Soome static DNSServiceErrorType deliver_request(ipc_msg_hdr *hdr, DNSServiceOp *sdr)
699c65ebfc7SToomas Soome {
700c65ebfc7SToomas Soome uint32_t datalen;
701c65ebfc7SToomas Soome dnssd_sock_t listenfd = dnssd_InvalidSocket, errsd = dnssd_InvalidSocket;
702c65ebfc7SToomas Soome DNSServiceErrorType err = kDNSServiceErr_Unknown; // Default for the "goto cleanup" cases
703c65ebfc7SToomas Soome int MakeSeparateReturnSocket;
704*472cd20dSToomas Soome int ioresult;
705c65ebfc7SToomas Soome #if defined(USE_TCP_LOOPBACK) || defined(USE_NAMED_ERROR_RETURN_SOCKET)
706c65ebfc7SToomas Soome char *data;
707c65ebfc7SToomas Soome #endif
708c65ebfc7SToomas Soome
709c65ebfc7SToomas Soome if (!hdr)
710c65ebfc7SToomas Soome {
711c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub deliver_request: !hdr");
712c65ebfc7SToomas Soome return kDNSServiceErr_Unknown;
713c65ebfc7SToomas Soome }
714c65ebfc7SToomas Soome
715c65ebfc7SToomas Soome datalen = hdr->datalen; // We take a copy here because we're going to convert hdr->datalen to network byte order
716c65ebfc7SToomas Soome #if defined(USE_TCP_LOOPBACK) || defined(USE_NAMED_ERROR_RETURN_SOCKET)
717c65ebfc7SToomas Soome data = (char *)hdr + sizeof(ipc_msg_hdr);
718c65ebfc7SToomas Soome #endif
719c65ebfc7SToomas Soome
720c65ebfc7SToomas Soome // Note: need to check hdr->op, not sdr->op.
721c65ebfc7SToomas Soome // hdr->op contains the code for the specific operation we're currently doing, whereas sdr->op
722c65ebfc7SToomas Soome // contains the original parent DNSServiceOp (e.g. for an add_record_request, hdr->op will be
723c65ebfc7SToomas Soome // add_record_request but the parent sdr->op will be connection_request or reg_service_request)
724c65ebfc7SToomas Soome MakeSeparateReturnSocket = (sdr->primary ||
725c65ebfc7SToomas Soome hdr->op == reg_record_request || hdr->op == add_record_request || hdr->op == update_record_request || hdr->op == remove_record_request);
726c65ebfc7SToomas Soome
727c65ebfc7SToomas Soome if (!DNSServiceRefValid(sdr))
728c65ebfc7SToomas Soome {
729c65ebfc7SToomas Soome if (hdr)
730c65ebfc7SToomas Soome free(hdr);
731c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub deliver_request: invalid DNSServiceRef %p %08X %08X", sdr, sdr->sockfd, sdr->validator);
732c65ebfc7SToomas Soome return kDNSServiceErr_BadReference;
733c65ebfc7SToomas Soome }
734c65ebfc7SToomas Soome
735c65ebfc7SToomas Soome if (MakeSeparateReturnSocket)
736c65ebfc7SToomas Soome {
737c65ebfc7SToomas Soome #if defined(USE_TCP_LOOPBACK)
738c65ebfc7SToomas Soome {
739c65ebfc7SToomas Soome union { uint16_t s; u_char b[2]; } port;
740c65ebfc7SToomas Soome dnssd_sockaddr_t caddr;
741c65ebfc7SToomas Soome dnssd_socklen_t len = (dnssd_socklen_t) sizeof(caddr);
742c65ebfc7SToomas Soome listenfd = socket(AF_DNSSD, SOCK_STREAM, 0);
7433b436d06SToomas Soome if (!dnssd_SocketValid(listenfd)) {
7443b436d06SToomas Soome deliver_request_bailout("TCP socket");
7453b436d06SToomas Soome }
746c65ebfc7SToomas Soome
747c65ebfc7SToomas Soome caddr.sin_family = AF_INET;
748c65ebfc7SToomas Soome caddr.sin_port = 0;
749c65ebfc7SToomas Soome caddr.sin_addr.s_addr = inet_addr(MDNS_TCP_SERVERADDR);
7503b436d06SToomas Soome if (bind(listenfd, (struct sockaddr*) &caddr, sizeof(caddr)) < 0) {
7513b436d06SToomas Soome deliver_request_bailout("TCP bind");
7523b436d06SToomas Soome }
7533b436d06SToomas Soome if (getsockname(listenfd, (struct sockaddr*) &caddr, &len) < 0) {
7543b436d06SToomas Soome deliver_request_bailout("TCP getsockname");
7553b436d06SToomas Soome }
7563b436d06SToomas Soome if (listen(listenfd, 1) < 0) {
7573b436d06SToomas Soome deliver_request_bailout("TCP listen");
7583b436d06SToomas Soome }
759c65ebfc7SToomas Soome port.s = caddr.sin_port;
760c65ebfc7SToomas Soome data[0] = port.b[0]; // don't switch the byte order, as the
761c65ebfc7SToomas Soome data[1] = port.b[1]; // daemon expects it in network byte order
762c65ebfc7SToomas Soome }
763c65ebfc7SToomas Soome #elif defined(USE_NAMED_ERROR_RETURN_SOCKET)
764c65ebfc7SToomas Soome {
765c65ebfc7SToomas Soome mode_t mask;
766c65ebfc7SToomas Soome int bindresult;
767c65ebfc7SToomas Soome dnssd_sockaddr_t caddr;
768c65ebfc7SToomas Soome listenfd = socket(AF_DNSSD, SOCK_STREAM, 0);
7693b436d06SToomas Soome if (!dnssd_SocketValid(listenfd)) {
7703b436d06SToomas Soome deliver_request_bailout("USE_NAMED_ERROR_RETURN_SOCKET socket");
7713b436d06SToomas Soome }
772c65ebfc7SToomas Soome
773c65ebfc7SToomas Soome caddr.sun_family = AF_LOCAL;
774c65ebfc7SToomas Soome // According to Stevens (section 3.2), there is no portable way to
775c65ebfc7SToomas Soome // determine whether sa_len is defined on a particular platform.
776c65ebfc7SToomas Soome #ifndef NOT_HAVE_SA_LEN
777c65ebfc7SToomas Soome caddr.sun_len = sizeof(struct sockaddr_un);
778c65ebfc7SToomas Soome #endif
779c65ebfc7SToomas Soome SetUDSPath(&caddr, data);
780c65ebfc7SToomas Soome mask = umask(0);
781c65ebfc7SToomas Soome bindresult = bind(listenfd, (struct sockaddr *)&caddr, sizeof(caddr));
782c65ebfc7SToomas Soome umask(mask);
7833b436d06SToomas Soome if (bindresult < 0) {
7843b436d06SToomas Soome deliver_request_bailout("USE_NAMED_ERROR_RETURN_SOCKET bind");
7853b436d06SToomas Soome }
7863b436d06SToomas Soome if (listen(listenfd, 1) < 0) {
7873b436d06SToomas Soome deliver_request_bailout("USE_NAMED_ERROR_RETURN_SOCKET listen");
7883b436d06SToomas Soome }
789c65ebfc7SToomas Soome }
790c65ebfc7SToomas Soome #else
791c65ebfc7SToomas Soome {
792c65ebfc7SToomas Soome dnssd_sock_t sp[2];
7933b436d06SToomas Soome if (socketpair(AF_DNSSD, SOCK_STREAM, 0, sp) < 0) {
7943b436d06SToomas Soome deliver_request_bailout("socketpair");
7953b436d06SToomas Soome }
796c65ebfc7SToomas Soome else
797c65ebfc7SToomas Soome {
798c65ebfc7SToomas Soome errsd = sp[0]; // We'll read our four-byte error code from sp[0]
799c65ebfc7SToomas Soome listenfd = sp[1]; // We'll send sp[1] to the daemon
800c65ebfc7SToomas Soome #if !defined(__ppc__) && defined(SO_DEFUNCTOK)
801c65ebfc7SToomas Soome {
802c65ebfc7SToomas Soome int defunct = 1;
803c65ebfc7SToomas Soome if (setsockopt(errsd, SOL_SOCKET, SO_DEFUNCTOK, &defunct, sizeof(defunct)) < 0)
804*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub deliver_request: SO_DEFUNCTOK failed %d %s", dnssd_errno, dnssd_strerror(dnssd_errno));
805c65ebfc7SToomas Soome }
806c65ebfc7SToomas Soome #endif
807c65ebfc7SToomas Soome }
808c65ebfc7SToomas Soome }
809c65ebfc7SToomas Soome #endif
810c65ebfc7SToomas Soome }
811c65ebfc7SToomas Soome
812c65ebfc7SToomas Soome #if !defined(USE_TCP_LOOPBACK) && !defined(USE_NAMED_ERROR_RETURN_SOCKET)
813c65ebfc7SToomas Soome // If we're going to make a separate error return socket, and pass it to the daemon
814c65ebfc7SToomas Soome // using sendmsg, then we'll hold back one data byte to go with it.
815c65ebfc7SToomas Soome // On some versions of Unix (including Leopard) sending a control message without
816c65ebfc7SToomas Soome // any associated data does not work reliably -- e.g. one particular issue we ran
817c65ebfc7SToomas Soome // into is that if the receiving program is in a kqueue loop waiting to be notified
818c65ebfc7SToomas Soome // of the received message, it doesn't get woken up when the control message arrives.
819c65ebfc7SToomas Soome if (MakeSeparateReturnSocket || sdr->op == send_bpf)
820c65ebfc7SToomas Soome datalen--; // Okay to use sdr->op when checking for op == send_bpf
821c65ebfc7SToomas Soome #endif
822c65ebfc7SToomas Soome
823c65ebfc7SToomas Soome // At this point, our listening socket is set up and waiting, if necessary, for the daemon to connect back to
824c65ebfc7SToomas Soome ConvertHeaderBytes(hdr);
825c65ebfc7SToomas Soome //syslog(LOG_WARNING, "dnssd_clientstub deliver_request writing %lu bytes", (unsigned long)(datalen + sizeof(ipc_msg_hdr)));
826c65ebfc7SToomas Soome //if (MakeSeparateReturnSocket) syslog(LOG_WARNING, "dnssd_clientstub deliver_request name is %s", data);
827c65ebfc7SToomas Soome #if TEST_SENDING_ONE_BYTE_AT_A_TIME
828c65ebfc7SToomas Soome unsigned int i;
829c65ebfc7SToomas Soome for (i=0; i<datalen + sizeof(ipc_msg_hdr); i++)
830c65ebfc7SToomas Soome {
831c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub deliver_request writing %d", i);
832*472cd20dSToomas Soome ioresult = write_all(sdr->sockfd, ((char *)hdr)+i, 1);
833*472cd20dSToomas Soome if (ioresult < write_all_success)
834*472cd20dSToomas Soome {
835*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub deliver_request write_all (byte %u) failed", i);
836*472cd20dSToomas Soome err = (ioresult == write_all_defunct) ? kDNSServiceErr_DefunctConnection : kDNSServiceErr_ServiceNotRunning;
837*472cd20dSToomas Soome goto cleanup;
838*472cd20dSToomas Soome }
839c65ebfc7SToomas Soome usleep(10000);
840c65ebfc7SToomas Soome }
841c65ebfc7SToomas Soome #else
842*472cd20dSToomas Soome ioresult = write_all(sdr->sockfd, (char *)hdr, datalen + sizeof(ipc_msg_hdr));
843*472cd20dSToomas Soome if (ioresult < write_all_success)
844c65ebfc7SToomas Soome {
845c65ebfc7SToomas Soome // write_all already prints an error message if there is an error writing to
846c65ebfc7SToomas Soome // the socket except for DEFUNCT. Logging here is unnecessary and also wrong
847c65ebfc7SToomas Soome // in the case of DEFUNCT sockets
848c65ebfc7SToomas Soome syslog(LOG_INFO, "dnssd_clientstub deliver_request ERROR: write_all(%d, %lu bytes) failed",
849c65ebfc7SToomas Soome sdr->sockfd, (unsigned long)(datalen + sizeof(ipc_msg_hdr)));
850*472cd20dSToomas Soome err = (ioresult == write_all_defunct) ? kDNSServiceErr_DefunctConnection : kDNSServiceErr_ServiceNotRunning;
851c65ebfc7SToomas Soome goto cleanup;
852c65ebfc7SToomas Soome }
853c65ebfc7SToomas Soome #endif
854c65ebfc7SToomas Soome
855c65ebfc7SToomas Soome if (!MakeSeparateReturnSocket)
856c65ebfc7SToomas Soome errsd = sdr->sockfd;
857c65ebfc7SToomas Soome if (MakeSeparateReturnSocket || sdr->op == send_bpf) // Okay to use sdr->op when checking for op == send_bpf
858c65ebfc7SToomas Soome {
859c65ebfc7SToomas Soome #if defined(USE_TCP_LOOPBACK) || defined(USE_NAMED_ERROR_RETURN_SOCKET)
860c65ebfc7SToomas Soome // At this point we may wait in accept for a few milliseconds waiting for the daemon to connect back to us,
861c65ebfc7SToomas Soome // but that's okay -- the daemon should not take more than a few milliseconds to respond.
862c65ebfc7SToomas Soome // set_waitlimit() ensures we do not block indefinitely just in case something is wrong
863c65ebfc7SToomas Soome dnssd_sockaddr_t daddr;
864c65ebfc7SToomas Soome dnssd_socklen_t len = sizeof(daddr);
865c65ebfc7SToomas Soome if ((err = set_waitlimit(listenfd, DNSSD_CLIENT_TIMEOUT)) != kDNSServiceErr_NoError)
866c65ebfc7SToomas Soome goto cleanup;
867c65ebfc7SToomas Soome errsd = accept(listenfd, (struct sockaddr *)&daddr, &len);
8683b436d06SToomas Soome if (!dnssd_SocketValid(errsd)) {
869c65ebfc7SToomas Soome deliver_request_bailout("accept");
8703b436d06SToomas Soome }
871c65ebfc7SToomas Soome #else
872c65ebfc7SToomas Soome
873c65ebfc7SToomas Soome struct iovec vec = { ((char *)hdr) + sizeof(ipc_msg_hdr) + datalen, 1 }; // Send the last byte along with the SCM_RIGHTS
874c65ebfc7SToomas Soome struct msghdr msg;
875c65ebfc7SToomas Soome struct cmsghdr *cmsg;
876c65ebfc7SToomas Soome char cbuf[CMSG_SPACE(4 * sizeof(dnssd_sock_t))];
877c65ebfc7SToomas Soome
878c65ebfc7SToomas Soome msg.msg_name = 0;
879c65ebfc7SToomas Soome msg.msg_namelen = 0;
880c65ebfc7SToomas Soome msg.msg_iov = &vec;
881c65ebfc7SToomas Soome msg.msg_iovlen = 1;
882c65ebfc7SToomas Soome msg.msg_flags = 0;
883c65ebfc7SToomas Soome if (MakeSeparateReturnSocket || sdr->op == send_bpf) // Okay to use sdr->op when checking for op == send_bpf
884c65ebfc7SToomas Soome {
885c65ebfc7SToomas Soome if (sdr->op == send_bpf)
886c65ebfc7SToomas Soome {
887c65ebfc7SToomas Soome int i;
888c65ebfc7SToomas Soome char p[12]; // Room for "/dev/bpf999" with terminating null
889c65ebfc7SToomas Soome for (i=0; i<100; i++)
890c65ebfc7SToomas Soome {
891c65ebfc7SToomas Soome snprintf(p, sizeof(p), "/dev/bpf%d", i);
892c65ebfc7SToomas Soome listenfd = open(p, O_RDWR, 0);
893*472cd20dSToomas Soome //if (dnssd_SocketValid(listenfd)) syslog(LOG_WARNING, "dnssd_clientstub deliver_request Sending fd %d for %s", listenfd, p);
894c65ebfc7SToomas Soome if (!dnssd_SocketValid(listenfd) && dnssd_errno != EBUSY)
895*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub deliver_request Error opening %s %d (%s)", p, dnssd_errno, dnssd_strerror(dnssd_errno));
896c65ebfc7SToomas Soome if (dnssd_SocketValid(listenfd) || dnssd_errno != EBUSY) break;
897c65ebfc7SToomas Soome }
898c65ebfc7SToomas Soome }
899c65ebfc7SToomas Soome msg.msg_control = cbuf;
900c65ebfc7SToomas Soome msg.msg_controllen = CMSG_LEN(sizeof(dnssd_sock_t));
901c65ebfc7SToomas Soome
902c65ebfc7SToomas Soome cmsg = CMSG_FIRSTHDR(&msg);
903c65ebfc7SToomas Soome cmsg->cmsg_len = CMSG_LEN(sizeof(dnssd_sock_t));
904c65ebfc7SToomas Soome cmsg->cmsg_level = SOL_SOCKET;
905c65ebfc7SToomas Soome cmsg->cmsg_type = SCM_RIGHTS;
906c65ebfc7SToomas Soome *((dnssd_sock_t *)CMSG_DATA(cmsg)) = listenfd;
907c65ebfc7SToomas Soome }
908c65ebfc7SToomas Soome
909c65ebfc7SToomas Soome #if TEST_KQUEUE_CONTROL_MESSAGE_BUG
910c65ebfc7SToomas Soome sleep(1);
911c65ebfc7SToomas Soome #endif
912c65ebfc7SToomas Soome
913c65ebfc7SToomas Soome #if DEBUG_64BIT_SCM_RIGHTS
914*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub deliver_request sendmsg read sd=%d write sd=%d %ld %ld %ld/%ld/%ld/%ld",
915c65ebfc7SToomas Soome errsd, listenfd, sizeof(dnssd_sock_t), sizeof(void*),
916c65ebfc7SToomas Soome sizeof(struct cmsghdr) + sizeof(dnssd_sock_t),
917c65ebfc7SToomas Soome CMSG_LEN(sizeof(dnssd_sock_t)), (long)CMSG_SPACE(sizeof(dnssd_sock_t)),
918c65ebfc7SToomas Soome (long)((char*)CMSG_DATA(cmsg) + 4 - cbuf));
919c65ebfc7SToomas Soome #endif // DEBUG_64BIT_SCM_RIGHTS
920c65ebfc7SToomas Soome
921c65ebfc7SToomas Soome if (sendmsg(sdr->sockfd, &msg, 0) < 0)
922c65ebfc7SToomas Soome {
923c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub deliver_request ERROR: sendmsg failed read sd=%d write sd=%d errno %d (%s)",
924c65ebfc7SToomas Soome errsd, listenfd, dnssd_errno, dnssd_strerror(dnssd_errno));
925c65ebfc7SToomas Soome err = kDNSServiceErr_Incompatible;
926c65ebfc7SToomas Soome goto cleanup;
927c65ebfc7SToomas Soome }
928c65ebfc7SToomas Soome
929c65ebfc7SToomas Soome #if DEBUG_64BIT_SCM_RIGHTS
930*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub deliver_request sendmsg read sd=%d write sd=%d okay", errsd, listenfd);
931c65ebfc7SToomas Soome #endif // DEBUG_64BIT_SCM_RIGHTS
932c65ebfc7SToomas Soome
933c65ebfc7SToomas Soome #endif
934c65ebfc7SToomas Soome // Close our end of the socketpair *before* calling read_all() to get the four-byte error code.
935c65ebfc7SToomas Soome // Otherwise, if the daemon closes our socket (or crashes), we will have to wait for a timeout
936c65ebfc7SToomas Soome // in read_all() because the socket is not closed (we still have an open reference to it)
937c65ebfc7SToomas Soome // Note: listenfd is overwritten in the case of send_bpf above and that will be closed here
938c65ebfc7SToomas Soome // for send_bpf operation.
939c65ebfc7SToomas Soome dnssd_close(listenfd);
940c65ebfc7SToomas Soome listenfd = dnssd_InvalidSocket; // Make sure we don't close it a second time in the cleanup handling below
941c65ebfc7SToomas Soome }
942c65ebfc7SToomas Soome
943c65ebfc7SToomas Soome // At this point we may wait in read_all for a few milliseconds waiting for the daemon to send us the error code,
944c65ebfc7SToomas Soome // but that's okay -- the daemon should not take more than a few milliseconds to respond.
945c65ebfc7SToomas Soome // set_waitlimit() ensures we do not block indefinitely just in case something is wrong
946c65ebfc7SToomas Soome if (sdr->op == send_bpf) // Okay to use sdr->op when checking for op == send_bpf
947c65ebfc7SToomas Soome err = kDNSServiceErr_NoError;
948c65ebfc7SToomas Soome else if ((err = set_waitlimit(errsd, DNSSD_CLIENT_TIMEOUT)) == kDNSServiceErr_NoError)
949c65ebfc7SToomas Soome {
950*472cd20dSToomas Soome ioresult = read_all(errsd, (char*)&err, (int)sizeof(err));
951*472cd20dSToomas Soome if (ioresult < read_all_success)
952*472cd20dSToomas Soome err = (ioresult == read_all_defunct) ? kDNSServiceErr_DefunctConnection : kDNSServiceErr_ServiceNotRunning; // On failure read_all will have written a message to syslog for us
953c65ebfc7SToomas Soome else
954c65ebfc7SToomas Soome err = ntohl(err);
955c65ebfc7SToomas Soome }
956c65ebfc7SToomas Soome //syslog(LOG_WARNING, "dnssd_clientstub deliver_request: retrieved error code %d", err);
957c65ebfc7SToomas Soome
958c65ebfc7SToomas Soome cleanup:
959c65ebfc7SToomas Soome if (MakeSeparateReturnSocket)
960c65ebfc7SToomas Soome {
961c65ebfc7SToomas Soome if (dnssd_SocketValid(listenfd)) dnssd_close(listenfd);
962c65ebfc7SToomas Soome if (dnssd_SocketValid(errsd)) dnssd_close(errsd);
963c65ebfc7SToomas Soome #if defined(USE_NAMED_ERROR_RETURN_SOCKET)
964c65ebfc7SToomas Soome // syslog(LOG_WARNING, "dnssd_clientstub deliver_request: removing UDS: %s", data);
965c65ebfc7SToomas Soome if (unlink(data) != 0)
966c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub WARNING: unlink(\"%s\") failed errno %d (%s)", data, dnssd_errno, dnssd_strerror(dnssd_errno));
967c65ebfc7SToomas Soome // else syslog(LOG_WARNING, "dnssd_clientstub deliver_request: removed UDS: %s", data);
968c65ebfc7SToomas Soome #endif
969c65ebfc7SToomas Soome }
970c65ebfc7SToomas Soome
971c65ebfc7SToomas Soome free(hdr);
972c65ebfc7SToomas Soome return err;
973c65ebfc7SToomas Soome }
974c65ebfc7SToomas Soome
DNSServiceRefSockFD(DNSServiceRef sdRef)975c65ebfc7SToomas Soome dnssd_sock_t DNSSD_API DNSServiceRefSockFD(DNSServiceRef sdRef)
976c65ebfc7SToomas Soome {
977c65ebfc7SToomas Soome if (!sdRef) { syslog(LOG_WARNING, "dnssd_clientstub DNSServiceRefSockFD called with NULL DNSServiceRef"); return dnssd_InvalidSocket; }
978c65ebfc7SToomas Soome
979c65ebfc7SToomas Soome if (!DNSServiceRefValid(sdRef))
980c65ebfc7SToomas Soome {
981c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceRefSockFD called with invalid DNSServiceRef %p %08X %08X",
982c65ebfc7SToomas Soome sdRef, sdRef->sockfd, sdRef->validator);
983c65ebfc7SToomas Soome return dnssd_InvalidSocket;
984c65ebfc7SToomas Soome }
985c65ebfc7SToomas Soome
986c65ebfc7SToomas Soome if (sdRef->primary)
987c65ebfc7SToomas Soome {
988c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceRefSockFD undefined for kDNSServiceFlagsShareConnection subordinate DNSServiceRef %p", sdRef);
989c65ebfc7SToomas Soome return dnssd_InvalidSocket;
990c65ebfc7SToomas Soome }
991c65ebfc7SToomas Soome
992c65ebfc7SToomas Soome return sdRef->sockfd;
993c65ebfc7SToomas Soome }
994c65ebfc7SToomas Soome
995c65ebfc7SToomas Soome #if _DNS_SD_LIBDISPATCH
CallbackWithError(DNSServiceRef sdRef,DNSServiceErrorType error)996c65ebfc7SToomas Soome static void CallbackWithError(DNSServiceRef sdRef, DNSServiceErrorType error)
997c65ebfc7SToomas Soome {
998c65ebfc7SToomas Soome DNSServiceOp *sdr = sdRef;
999c65ebfc7SToomas Soome DNSServiceOp *sdrNext;
1000c65ebfc7SToomas Soome DNSRecord *rec;
1001c65ebfc7SToomas Soome DNSRecord *recnext;
1002c65ebfc7SToomas Soome int morebytes;
1003c65ebfc7SToomas Soome
1004c65ebfc7SToomas Soome while (sdr)
1005c65ebfc7SToomas Soome {
1006c65ebfc7SToomas Soome // We can't touch the sdr after the callback as it can be deallocated in the callback
1007c65ebfc7SToomas Soome sdrNext = sdr->next;
1008c65ebfc7SToomas Soome morebytes = 1;
1009c65ebfc7SToomas Soome sdr->moreptr = &morebytes;
1010c65ebfc7SToomas Soome switch (sdr->op)
1011c65ebfc7SToomas Soome {
1012c65ebfc7SToomas Soome case resolve_request:
1013c65ebfc7SToomas Soome if (sdr->AppCallback) ((DNSServiceResolveReply) sdr->AppCallback)(sdr, 0, 0, error, NULL, 0, 0, 0, NULL, sdr->AppContext);
1014c65ebfc7SToomas Soome break;
1015c65ebfc7SToomas Soome case query_request:
1016c65ebfc7SToomas Soome if (sdr->AppCallback) ((DNSServiceQueryRecordReply)sdr->AppCallback)(sdr, 0, 0, error, NULL, 0, 0, 0, NULL, 0, sdr->AppContext);
1017c65ebfc7SToomas Soome break;
1018c65ebfc7SToomas Soome case addrinfo_request:
1019c65ebfc7SToomas Soome if (sdr->AppCallback) ((DNSServiceGetAddrInfoReply)sdr->AppCallback)(sdr, 0, 0, error, NULL, NULL, 0, sdr->AppContext);
1020c65ebfc7SToomas Soome break;
1021c65ebfc7SToomas Soome case browse_request:
1022c65ebfc7SToomas Soome if (sdr->AppCallback) ((DNSServiceBrowseReply) sdr->AppCallback)(sdr, 0, 0, error, NULL, 0, NULL, sdr->AppContext);
1023c65ebfc7SToomas Soome break;
1024c65ebfc7SToomas Soome case reg_service_request:
1025c65ebfc7SToomas Soome if (sdr->AppCallback) ((DNSServiceRegisterReply) sdr->AppCallback)(sdr, 0, error, NULL, 0, NULL, sdr->AppContext);
1026c65ebfc7SToomas Soome break;
1027c65ebfc7SToomas Soome case enumeration_request:
1028c65ebfc7SToomas Soome if (sdr->AppCallback) ((DNSServiceDomainEnumReply) sdr->AppCallback)(sdr, 0, 0, error, NULL, sdr->AppContext);
1029c65ebfc7SToomas Soome break;
1030c65ebfc7SToomas Soome case connection_request:
1031c65ebfc7SToomas Soome case connection_delegate_request:
1032c65ebfc7SToomas Soome // This means Register Record, walk the list of DNSRecords to do the callback
1033c65ebfc7SToomas Soome rec = sdr->rec;
1034c65ebfc7SToomas Soome while (rec)
1035c65ebfc7SToomas Soome {
1036c65ebfc7SToomas Soome recnext = rec->recnext;
1037c65ebfc7SToomas Soome if (rec->AppCallback) ((DNSServiceRegisterRecordReply)rec->AppCallback)(sdr, 0, 0, error, rec->AppContext);
1038c65ebfc7SToomas Soome // The Callback can call DNSServiceRefDeallocate which in turn frees sdr and all the records.
1039c65ebfc7SToomas Soome // Detect that and return early
1040*472cd20dSToomas Soome if (!morebytes) { syslog(LOG_WARNING, "dnssd_clientstub:Record: CallbackwithError morebytes zero"); return; }
1041c65ebfc7SToomas Soome rec = recnext;
1042c65ebfc7SToomas Soome }
1043c65ebfc7SToomas Soome break;
1044c65ebfc7SToomas Soome case port_mapping_request:
1045c65ebfc7SToomas Soome if (sdr->AppCallback) ((DNSServiceNATPortMappingReply)sdr->AppCallback)(sdr, 0, 0, error, 0, 0, 0, 0, 0, sdr->AppContext);
1046c65ebfc7SToomas Soome break;
1047c65ebfc7SToomas Soome default:
1048c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub CallbackWithError called with bad op %d", sdr->op);
1049c65ebfc7SToomas Soome }
1050c65ebfc7SToomas Soome // If DNSServiceRefDeallocate was called in the callback, morebytes will be zero. As the sdRef
1051c65ebfc7SToomas Soome // (and its subordinates) have been freed, we should not proceed further. Note that when we
1052c65ebfc7SToomas Soome // call the callback with a subordinate sdRef the application can call DNSServiceRefDeallocate
1053c65ebfc7SToomas Soome // on the main sdRef and DNSServiceRefDeallocate handles this case by walking all the sdRefs and
1054c65ebfc7SToomas Soome // clears the moreptr so that we can terminate here.
1055c65ebfc7SToomas Soome //
1056c65ebfc7SToomas Soome // If DNSServiceRefDeallocate was not called in the callback, then set moreptr to NULL so that
1057c65ebfc7SToomas Soome // we don't access the stack variable after we return from this function.
1058*472cd20dSToomas Soome if (!morebytes) { syslog(LOG_WARNING, "dnssd_clientstub:sdRef: CallbackwithError morebytes zero sdr %p", sdr); return; }
1059c65ebfc7SToomas Soome else {sdr->moreptr = NULL;}
1060c65ebfc7SToomas Soome sdr = sdrNext;
1061c65ebfc7SToomas Soome }
1062c65ebfc7SToomas Soome }
1063c65ebfc7SToomas Soome #endif // _DNS_SD_LIBDISPATCH
1064c65ebfc7SToomas Soome
1065c65ebfc7SToomas Soome // Handle reply from server, calling application client callback. If there is no reply
1066c65ebfc7SToomas Soome // from the daemon on the socket contained in sdRef, the call will block.
DNSServiceProcessResult(DNSServiceRef sdRef)1067c65ebfc7SToomas Soome DNSServiceErrorType DNSSD_API DNSServiceProcessResult(DNSServiceRef sdRef)
1068c65ebfc7SToomas Soome {
1069c65ebfc7SToomas Soome int morebytes = 0;
1070*472cd20dSToomas Soome int ioresult;
1071*472cd20dSToomas Soome DNSServiceErrorType error;
1072c65ebfc7SToomas Soome
1073c65ebfc7SToomas Soome if (!sdRef) { syslog(LOG_WARNING, "dnssd_clientstub DNSServiceProcessResult called with NULL DNSServiceRef"); return kDNSServiceErr_BadParam; }
1074c65ebfc7SToomas Soome
1075c65ebfc7SToomas Soome if (!DNSServiceRefValid(sdRef))
1076c65ebfc7SToomas Soome {
1077c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceProcessResult called with invalid DNSServiceRef %p %08X %08X", sdRef, sdRef->sockfd, sdRef->validator);
1078c65ebfc7SToomas Soome return kDNSServiceErr_BadReference;
1079c65ebfc7SToomas Soome }
1080c65ebfc7SToomas Soome
1081c65ebfc7SToomas Soome if (sdRef->primary)
1082c65ebfc7SToomas Soome {
1083c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceProcessResult undefined for kDNSServiceFlagsShareConnection subordinate DNSServiceRef %p", sdRef);
1084c65ebfc7SToomas Soome return kDNSServiceErr_BadReference;
1085c65ebfc7SToomas Soome }
1086c65ebfc7SToomas Soome
1087c65ebfc7SToomas Soome if (!sdRef->ProcessReply)
1088c65ebfc7SToomas Soome {
1089c65ebfc7SToomas Soome static int num_logs = 0;
1090c65ebfc7SToomas Soome if (num_logs < 10) syslog(LOG_WARNING, "dnssd_clientstub DNSServiceProcessResult called with DNSServiceRef with no ProcessReply function");
1091c65ebfc7SToomas Soome if (num_logs < 1000) num_logs++;else sleep(1);
1092c65ebfc7SToomas Soome return kDNSServiceErr_BadReference;
1093c65ebfc7SToomas Soome }
1094c65ebfc7SToomas Soome
1095c65ebfc7SToomas Soome do
1096c65ebfc7SToomas Soome {
1097c65ebfc7SToomas Soome CallbackHeader cbh;
1098c65ebfc7SToomas Soome char *data;
1099c65ebfc7SToomas Soome
1100c65ebfc7SToomas Soome // return NoError on EWOULDBLOCK. This will handle the case
1101c65ebfc7SToomas Soome // where a non-blocking socket is told there is data, but it was a false positive.
1102c65ebfc7SToomas Soome // On error, read_all will write a message to syslog for us, so don't need to duplicate that here
1103c65ebfc7SToomas Soome // Note: If we want to properly support using non-blocking sockets in the future
1104*472cd20dSToomas Soome ioresult = read_all(sdRef->sockfd, (void *)&cbh.ipc_hdr, sizeof(cbh.ipc_hdr));
1105*472cd20dSToomas Soome if (ioresult == read_all_fail || ioresult == read_all_defunct)
1106c65ebfc7SToomas Soome {
1107*472cd20dSToomas Soome error = (ioresult == read_all_defunct) ? kDNSServiceErr_DefunctConnection : kDNSServiceErr_ServiceNotRunning;
1108*472cd20dSToomas Soome
1109c65ebfc7SToomas Soome // Set the ProcessReply to NULL before callback as the sdRef can get deallocated
1110c65ebfc7SToomas Soome // in the callback.
1111c65ebfc7SToomas Soome sdRef->ProcessReply = NULL;
1112c65ebfc7SToomas Soome #if _DNS_SD_LIBDISPATCH
1113c65ebfc7SToomas Soome // Call the callbacks with an error if using the dispatch API, as DNSServiceProcessResult
1114c65ebfc7SToomas Soome // is not called by the application and hence need to communicate the error. Cancel the
1115c65ebfc7SToomas Soome // source so that we don't get any more events
1116c65ebfc7SToomas Soome // Note: read_all fails if we could not read from the daemon which can happen if the
1117c65ebfc7SToomas Soome // daemon dies or the file descriptor is disconnected (defunct).
1118c65ebfc7SToomas Soome if (sdRef->disp_source)
1119c65ebfc7SToomas Soome {
1120c65ebfc7SToomas Soome dispatch_source_cancel(sdRef->disp_source);
1121c65ebfc7SToomas Soome dispatch_release(sdRef->disp_source);
1122c65ebfc7SToomas Soome sdRef->disp_source = NULL;
1123*472cd20dSToomas Soome CallbackWithError(sdRef, error);
1124c65ebfc7SToomas Soome }
1125c65ebfc7SToomas Soome #endif
1126c65ebfc7SToomas Soome // Don't touch sdRef anymore as it might have been deallocated
1127*472cd20dSToomas Soome return error;
1128c65ebfc7SToomas Soome }
1129*472cd20dSToomas Soome else if (ioresult == read_all_wouldblock)
1130c65ebfc7SToomas Soome {
1131c65ebfc7SToomas Soome if (morebytes && sdRef->logcounter < 100)
1132c65ebfc7SToomas Soome {
1133c65ebfc7SToomas Soome sdRef->logcounter++;
1134c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceProcessResult error: select indicated data was waiting but read_all returned EWOULDBLOCK");
1135c65ebfc7SToomas Soome }
1136c65ebfc7SToomas Soome return kDNSServiceErr_NoError;
1137c65ebfc7SToomas Soome }
1138c65ebfc7SToomas Soome
1139c65ebfc7SToomas Soome ConvertHeaderBytes(&cbh.ipc_hdr);
1140c65ebfc7SToomas Soome if (cbh.ipc_hdr.version != VERSION)
1141c65ebfc7SToomas Soome {
1142c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceProcessResult daemon version %d does not match client version %d", cbh.ipc_hdr.version, VERSION);
1143c65ebfc7SToomas Soome sdRef->ProcessReply = NULL;
1144c65ebfc7SToomas Soome return kDNSServiceErr_Incompatible;
1145c65ebfc7SToomas Soome }
1146c65ebfc7SToomas Soome
1147c65ebfc7SToomas Soome data = malloc(cbh.ipc_hdr.datalen);
1148c65ebfc7SToomas Soome if (!data) return kDNSServiceErr_NoMemory;
1149*472cd20dSToomas Soome ioresult = read_all(sdRef->sockfd, data, cbh.ipc_hdr.datalen);
1150*472cd20dSToomas Soome if (ioresult < read_all_success) // On error, read_all will write a message to syslog for us
1151c65ebfc7SToomas Soome {
1152*472cd20dSToomas Soome error = (ioresult == read_all_defunct) ? kDNSServiceErr_DefunctConnection : kDNSServiceErr_ServiceNotRunning;
1153*472cd20dSToomas Soome
1154c65ebfc7SToomas Soome // Set the ProcessReply to NULL before callback as the sdRef can get deallocated
1155c65ebfc7SToomas Soome // in the callback.
1156c65ebfc7SToomas Soome sdRef->ProcessReply = NULL;
1157c65ebfc7SToomas Soome #if _DNS_SD_LIBDISPATCH
1158c65ebfc7SToomas Soome // Call the callbacks with an error if using the dispatch API, as DNSServiceProcessResult
1159c65ebfc7SToomas Soome // is not called by the application and hence need to communicate the error. Cancel the
1160c65ebfc7SToomas Soome // source so that we don't get any more events
1161c65ebfc7SToomas Soome if (sdRef->disp_source)
1162c65ebfc7SToomas Soome {
1163c65ebfc7SToomas Soome dispatch_source_cancel(sdRef->disp_source);
1164c65ebfc7SToomas Soome dispatch_release(sdRef->disp_source);
1165c65ebfc7SToomas Soome sdRef->disp_source = NULL;
1166*472cd20dSToomas Soome CallbackWithError(sdRef, error);
1167c65ebfc7SToomas Soome }
1168c65ebfc7SToomas Soome #endif
1169c65ebfc7SToomas Soome // Don't touch sdRef anymore as it might have been deallocated
1170c65ebfc7SToomas Soome free(data);
1171*472cd20dSToomas Soome return error;
1172c65ebfc7SToomas Soome }
1173c65ebfc7SToomas Soome else
1174c65ebfc7SToomas Soome {
1175c65ebfc7SToomas Soome const char *ptr = data;
1176c65ebfc7SToomas Soome cbh.cb_flags = get_flags (&ptr, data + cbh.ipc_hdr.datalen);
1177c65ebfc7SToomas Soome cbh.cb_interface = get_uint32 (&ptr, data + cbh.ipc_hdr.datalen);
1178c65ebfc7SToomas Soome cbh.cb_err = get_error_code(&ptr, data + cbh.ipc_hdr.datalen);
1179c65ebfc7SToomas Soome
1180c65ebfc7SToomas Soome // CAUTION: We have to handle the case where the client calls DNSServiceRefDeallocate from within the callback function.
1181c65ebfc7SToomas Soome // To do this we set moreptr to point to morebytes. If the client does call DNSServiceRefDeallocate(),
1182c65ebfc7SToomas Soome // then that routine will clear morebytes for us, and cause us to exit our loop.
1183c65ebfc7SToomas Soome morebytes = more_bytes(sdRef->sockfd);
1184c65ebfc7SToomas Soome if (morebytes)
1185c65ebfc7SToomas Soome {
1186c65ebfc7SToomas Soome cbh.cb_flags |= kDNSServiceFlagsMoreComing;
1187c65ebfc7SToomas Soome sdRef->moreptr = &morebytes;
1188c65ebfc7SToomas Soome }
1189c65ebfc7SToomas Soome if (ptr) sdRef->ProcessReply(sdRef, &cbh, ptr, data + cbh.ipc_hdr.datalen);
1190c65ebfc7SToomas Soome // Careful code here:
1191c65ebfc7SToomas Soome // If morebytes is non-zero, that means we set sdRef->moreptr above, and the operation was not
1192c65ebfc7SToomas Soome // cancelled out from under us, so now we need to clear sdRef->moreptr so we don't leave a stray
1193c65ebfc7SToomas Soome // dangling pointer pointing to a long-gone stack variable.
1194c65ebfc7SToomas Soome // If morebytes is zero, then one of two thing happened:
1195c65ebfc7SToomas Soome // (a) morebytes was 0 above, so we didn't set sdRef->moreptr, so we don't need to clear it
1196c65ebfc7SToomas Soome // (b) morebytes was 1 above, and we set sdRef->moreptr, but the operation was cancelled (with DNSServiceRefDeallocate()),
1197c65ebfc7SToomas Soome // so we MUST NOT try to dereference our stale sdRef pointer.
1198c65ebfc7SToomas Soome if (morebytes) sdRef->moreptr = NULL;
1199c65ebfc7SToomas Soome }
1200c65ebfc7SToomas Soome free(data);
1201c65ebfc7SToomas Soome } while (morebytes);
1202c65ebfc7SToomas Soome
1203c65ebfc7SToomas Soome return kDNSServiceErr_NoError;
1204c65ebfc7SToomas Soome }
1205c65ebfc7SToomas Soome
DNSServiceRefDeallocate(DNSServiceRef sdRef)1206c65ebfc7SToomas Soome void DNSSD_API DNSServiceRefDeallocate(DNSServiceRef sdRef)
1207c65ebfc7SToomas Soome {
1208c65ebfc7SToomas Soome if (!sdRef) { syslog(LOG_WARNING, "dnssd_clientstub DNSServiceRefDeallocate called with NULL DNSServiceRef"); return; }
1209c65ebfc7SToomas Soome
1210c65ebfc7SToomas Soome if (!DNSServiceRefValid(sdRef)) // Also verifies dnssd_SocketValid(sdRef->sockfd) for us too
1211c65ebfc7SToomas Soome {
1212c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceRefDeallocate called with invalid DNSServiceRef %p %08X %08X", sdRef, sdRef->sockfd, sdRef->validator);
1213c65ebfc7SToomas Soome return;
1214c65ebfc7SToomas Soome }
1215c65ebfc7SToomas Soome
1216c65ebfc7SToomas Soome // If we're in the middle of a DNSServiceProcessResult() invocation for this DNSServiceRef, clear its morebytes flag to break it out of its while loop
1217c65ebfc7SToomas Soome if (sdRef->moreptr) *(sdRef->moreptr) = 0;
1218c65ebfc7SToomas Soome
1219c65ebfc7SToomas Soome if (sdRef->primary) // If this is a subordinate DNSServiceOp, just send a 'stop' command
1220c65ebfc7SToomas Soome {
1221c65ebfc7SToomas Soome DNSServiceOp **p = &sdRef->primary->next;
1222c65ebfc7SToomas Soome while (*p && *p != sdRef) p = &(*p)->next;
1223c65ebfc7SToomas Soome if (*p)
1224c65ebfc7SToomas Soome {
1225c65ebfc7SToomas Soome char *ptr;
1226c65ebfc7SToomas Soome size_t len = 0;
1227c65ebfc7SToomas Soome ipc_msg_hdr *hdr = create_hdr(cancel_request, &len, &ptr, 0, sdRef);
1228c65ebfc7SToomas Soome if (hdr)
1229c65ebfc7SToomas Soome {
1230c65ebfc7SToomas Soome ConvertHeaderBytes(hdr);
1231c65ebfc7SToomas Soome write_all(sdRef->sockfd, (char *)hdr, len);
1232c65ebfc7SToomas Soome free(hdr);
1233c65ebfc7SToomas Soome }
1234c65ebfc7SToomas Soome *p = sdRef->next;
1235c65ebfc7SToomas Soome FreeDNSServiceOp(sdRef);
1236c65ebfc7SToomas Soome }
1237c65ebfc7SToomas Soome }
1238c65ebfc7SToomas Soome else // else, make sure to terminate all subordinates as well
1239c65ebfc7SToomas Soome {
1240c65ebfc7SToomas Soome #if _DNS_SD_LIBDISPATCH
1241c65ebfc7SToomas Soome // The cancel handler will close the fd if a dispatch source has been set
1242c65ebfc7SToomas Soome if (sdRef->disp_source)
1243c65ebfc7SToomas Soome {
1244c65ebfc7SToomas Soome // By setting the ProcessReply to NULL, we make sure that we never call
1245c65ebfc7SToomas Soome // the application callbacks ever, after returning from this function. We
1246c65ebfc7SToomas Soome // assume that DNSServiceRefDeallocate is called from the serial queue
1247c65ebfc7SToomas Soome // that was passed to DNSServiceSetDispatchQueue. Hence, dispatch_source_cancel
1248c65ebfc7SToomas Soome // should cancel all the blocks on the queue and hence there should be no more
1249c65ebfc7SToomas Soome // callbacks when we return from this function. Setting ProcessReply to NULL
1250c65ebfc7SToomas Soome // provides extra protection.
1251c65ebfc7SToomas Soome sdRef->ProcessReply = NULL;
1252c65ebfc7SToomas Soome shutdown(sdRef->sockfd, SHUT_WR);
1253c65ebfc7SToomas Soome dispatch_source_cancel(sdRef->disp_source);
1254c65ebfc7SToomas Soome dispatch_release(sdRef->disp_source);
1255c65ebfc7SToomas Soome sdRef->disp_source = NULL;
1256c65ebfc7SToomas Soome }
1257c65ebfc7SToomas Soome // if disp_queue is set, it means it used the DNSServiceSetDispatchQueue API. In that case,
1258c65ebfc7SToomas Soome // when the source was cancelled, the fd was closed in the handler. Currently the source
1259c65ebfc7SToomas Soome // is cancelled only when the mDNSResponder daemon dies
1260c65ebfc7SToomas Soome else if (!sdRef->disp_queue) dnssd_close(sdRef->sockfd);
1261c65ebfc7SToomas Soome #else
1262c65ebfc7SToomas Soome dnssd_close(sdRef->sockfd);
1263c65ebfc7SToomas Soome #endif
1264c65ebfc7SToomas Soome // Free DNSRecords added in DNSRegisterRecord if they have not
1265c65ebfc7SToomas Soome // been freed in DNSRemoveRecord
1266c65ebfc7SToomas Soome while (sdRef)
1267c65ebfc7SToomas Soome {
1268c65ebfc7SToomas Soome DNSServiceOp *p = sdRef;
1269c65ebfc7SToomas Soome sdRef = sdRef->next;
1270c65ebfc7SToomas Soome // When there is an error reading from the daemon e.g., bad fd, CallbackWithError
1271c65ebfc7SToomas Soome // is called which sets moreptr. It might set the moreptr on a subordinate sdRef
1272c65ebfc7SToomas Soome // but the application might call DNSServiceRefDeallocate with the main sdRef from
1273c65ebfc7SToomas Soome // the callback. Hence, when we loop through the subordinate sdRefs, we need
1274c65ebfc7SToomas Soome // to clear the moreptr so that CallbackWithError can terminate itself instead of
1275c65ebfc7SToomas Soome // walking through the freed sdRefs.
1276c65ebfc7SToomas Soome if (p->moreptr) *(p->moreptr) = 0;
1277c65ebfc7SToomas Soome FreeDNSServiceOp(p);
1278c65ebfc7SToomas Soome }
1279c65ebfc7SToomas Soome }
1280c65ebfc7SToomas Soome }
1281c65ebfc7SToomas Soome
DNSServiceGetProperty(const char * property,void * result,uint32_t * size)1282c65ebfc7SToomas Soome DNSServiceErrorType DNSSD_API DNSServiceGetProperty(const char *property, void *result, uint32_t *size)
1283c65ebfc7SToomas Soome {
1284c65ebfc7SToomas Soome DNSServiceErrorType err;
1285c65ebfc7SToomas Soome char *ptr;
1286c65ebfc7SToomas Soome size_t len;
1287c65ebfc7SToomas Soome ipc_msg_hdr *hdr;
1288c65ebfc7SToomas Soome DNSServiceOp *tmp;
1289c65ebfc7SToomas Soome uint32_t actualsize;
1290*472cd20dSToomas Soome int ioresult;
1291c65ebfc7SToomas Soome
1292c65ebfc7SToomas Soome if (!property || !result || !size)
1293c65ebfc7SToomas Soome return kDNSServiceErr_BadParam;
1294c65ebfc7SToomas Soome
1295c65ebfc7SToomas Soome len = strlen(property) + 1;
1296c65ebfc7SToomas Soome err = ConnectToServer(&tmp, 0, getproperty_request, NULL, NULL, NULL);
1297c65ebfc7SToomas Soome if (err) return err;
1298c65ebfc7SToomas Soome
1299c65ebfc7SToomas Soome hdr = create_hdr(getproperty_request, &len, &ptr, 0, tmp);
1300c65ebfc7SToomas Soome if (!hdr) { DNSServiceRefDeallocate(tmp); return kDNSServiceErr_NoMemory; }
1301c65ebfc7SToomas Soome
1302c65ebfc7SToomas Soome put_string(property, &ptr);
1303c65ebfc7SToomas Soome err = deliver_request(hdr, tmp); // Will free hdr for us
1304c65ebfc7SToomas Soome if (err) { DNSServiceRefDeallocate(tmp); return err; }
1305c65ebfc7SToomas Soome
1306*472cd20dSToomas Soome ioresult = read_all(tmp->sockfd, (char*)&actualsize, (int)sizeof(actualsize));
1307*472cd20dSToomas Soome if (ioresult < read_all_success)
1308*472cd20dSToomas Soome { DNSServiceRefDeallocate(tmp); return (ioresult == read_all_defunct) ? kDNSServiceErr_DefunctConnection : kDNSServiceErr_ServiceNotRunning; }
1309c65ebfc7SToomas Soome
1310c65ebfc7SToomas Soome actualsize = ntohl(actualsize);
1311*472cd20dSToomas Soome ioresult = read_all(tmp->sockfd, (char*)result, actualsize < *size ? actualsize : *size);
1312*472cd20dSToomas Soome if (ioresult < read_all_success)
1313*472cd20dSToomas Soome { DNSServiceRefDeallocate(tmp); return (ioresult == read_all_defunct) ? kDNSServiceErr_DefunctConnection : kDNSServiceErr_ServiceNotRunning; }
1314c65ebfc7SToomas Soome DNSServiceRefDeallocate(tmp);
1315c65ebfc7SToomas Soome
1316c65ebfc7SToomas Soome // Swap version result back to local process byte order
1317c65ebfc7SToomas Soome if (!strcmp(property, kDNSServiceProperty_DaemonVersion) && *size >= 4)
1318c65ebfc7SToomas Soome *(uint32_t*)result = ntohl(*(uint32_t*)result);
1319c65ebfc7SToomas Soome
1320c65ebfc7SToomas Soome *size = actualsize;
1321c65ebfc7SToomas Soome return kDNSServiceErr_NoError;
1322c65ebfc7SToomas Soome }
1323c65ebfc7SToomas Soome
DNSServiceGetPID(const uint16_t srcport,int32_t * pid)1324c65ebfc7SToomas Soome DNSServiceErrorType DNSSD_API DNSServiceGetPID(const uint16_t srcport, int32_t *pid)
1325c65ebfc7SToomas Soome {
1326c65ebfc7SToomas Soome char *ptr;
1327c65ebfc7SToomas Soome ipc_msg_hdr *hdr;
1328c65ebfc7SToomas Soome DNSServiceOp *tmp = NULL;
1329c65ebfc7SToomas Soome size_t len = sizeof(int32_t);
1330*472cd20dSToomas Soome int ioresult;
1331c65ebfc7SToomas Soome
1332c65ebfc7SToomas Soome DNSServiceErrorType err = ConnectToServer(&tmp, 0, getpid_request, NULL, NULL, NULL);
1333c65ebfc7SToomas Soome if (err) return err;
1334c65ebfc7SToomas Soome
1335c65ebfc7SToomas Soome hdr = create_hdr(getpid_request, &len, &ptr, 0, tmp);
1336c65ebfc7SToomas Soome if (!hdr) { DNSServiceRefDeallocate(tmp); return kDNSServiceErr_NoMemory; }
1337c65ebfc7SToomas Soome
1338c65ebfc7SToomas Soome put_uint16(srcport, &ptr);
1339c65ebfc7SToomas Soome err = deliver_request(hdr, tmp); // Will free hdr for us
1340c65ebfc7SToomas Soome if (err) { DNSServiceRefDeallocate(tmp); return err; }
1341c65ebfc7SToomas Soome
1342*472cd20dSToomas Soome ioresult = read_all(tmp->sockfd, (char*)pid, sizeof(int32_t));
1343*472cd20dSToomas Soome if (ioresult < read_all_success)
1344*472cd20dSToomas Soome { DNSServiceRefDeallocate(tmp); return (ioresult == read_all_defunct) ? kDNSServiceErr_DefunctConnection : kDNSServiceErr_ServiceNotRunning; }
1345c65ebfc7SToomas Soome
1346c65ebfc7SToomas Soome DNSServiceRefDeallocate(tmp);
1347c65ebfc7SToomas Soome return kDNSServiceErr_NoError;
1348c65ebfc7SToomas Soome }
1349c65ebfc7SToomas Soome
handle_resolve_response(DNSServiceOp * const sdr,const CallbackHeader * const cbh,const char * data,const char * end)1350c65ebfc7SToomas Soome static void handle_resolve_response(DNSServiceOp *const sdr, const CallbackHeader *const cbh, const char *data, const char *end)
1351c65ebfc7SToomas Soome {
1352c65ebfc7SToomas Soome char fullname[kDNSServiceMaxDomainName];
1353c65ebfc7SToomas Soome char target[kDNSServiceMaxDomainName];
1354c65ebfc7SToomas Soome uint16_t txtlen;
1355c65ebfc7SToomas Soome union { uint16_t s; u_char b[2]; } port;
1356c65ebfc7SToomas Soome unsigned char *txtrecord;
1357c65ebfc7SToomas Soome
1358c65ebfc7SToomas Soome get_string(&data, end, fullname, kDNSServiceMaxDomainName);
1359c65ebfc7SToomas Soome get_string(&data, end, target, kDNSServiceMaxDomainName);
1360c65ebfc7SToomas Soome if (!data || data + 2 > end) goto fail;
1361c65ebfc7SToomas Soome
1362c65ebfc7SToomas Soome port.b[0] = *data++;
1363c65ebfc7SToomas Soome port.b[1] = *data++;
1364c65ebfc7SToomas Soome txtlen = get_uint16(&data, end);
1365c65ebfc7SToomas Soome txtrecord = (unsigned char *)get_rdata(&data, end, txtlen);
1366c65ebfc7SToomas Soome
1367c65ebfc7SToomas Soome if (!data) goto fail;
1368c65ebfc7SToomas Soome ((DNSServiceResolveReply)sdr->AppCallback)(sdr, cbh->cb_flags, cbh->cb_interface, cbh->cb_err, fullname, target, port.s, txtlen, txtrecord, sdr->AppContext);
1369c65ebfc7SToomas Soome return;
1370c65ebfc7SToomas Soome // MUST NOT touch sdr after invoking AppCallback -- client is allowed to dispose it from within callback function
1371c65ebfc7SToomas Soome fail:
1372c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub handle_resolve_response: error reading result from daemon");
1373c65ebfc7SToomas Soome }
1374c65ebfc7SToomas Soome
1375*472cd20dSToomas Soome #if TARGET_OS_IPHONE
1376c65ebfc7SToomas Soome
1377c65ebfc7SToomas Soome static int32_t libSystemVersion = 0;
1378c65ebfc7SToomas Soome
1379c65ebfc7SToomas Soome // Return true if the iOS application linked against a version of libsystem where P2P
1380c65ebfc7SToomas Soome // interfaces were included by default when using kDNSServiceInterfaceIndexAny.
1381c65ebfc7SToomas Soome // Using 160.0.0 == 0xa00000 as the version threshold.
includeP2PWithIndexAny()1382c65ebfc7SToomas Soome static int includeP2PWithIndexAny()
1383c65ebfc7SToomas Soome {
1384c65ebfc7SToomas Soome if (libSystemVersion == 0)
1385c65ebfc7SToomas Soome libSystemVersion = NSVersionOfLinkTimeLibrary("System");
1386c65ebfc7SToomas Soome
1387c65ebfc7SToomas Soome if (libSystemVersion < 0xa00000)
1388c65ebfc7SToomas Soome return 1;
1389c65ebfc7SToomas Soome else
1390c65ebfc7SToomas Soome return 0;
1391c65ebfc7SToomas Soome }
1392c65ebfc7SToomas Soome
1393*472cd20dSToomas Soome #else // TARGET_OS_IPHONE
1394c65ebfc7SToomas Soome
1395c65ebfc7SToomas Soome // always return false for non iOS platforms
includeP2PWithIndexAny()1396c65ebfc7SToomas Soome static int includeP2PWithIndexAny()
1397c65ebfc7SToomas Soome {
1398c65ebfc7SToomas Soome return 0;
1399c65ebfc7SToomas Soome }
1400c65ebfc7SToomas Soome
1401*472cd20dSToomas Soome #endif // TARGET_OS_IPHONE
1402c65ebfc7SToomas Soome
DNSServiceResolve(DNSServiceRef * sdRef,DNSServiceFlags flags,uint32_t interfaceIndex,const char * name,const char * regtype,const char * domain,DNSServiceResolveReply callBack,void * context)1403c65ebfc7SToomas Soome DNSServiceErrorType DNSSD_API DNSServiceResolve
1404c65ebfc7SToomas Soome (
1405c65ebfc7SToomas Soome DNSServiceRef *sdRef,
1406c65ebfc7SToomas Soome DNSServiceFlags flags,
1407c65ebfc7SToomas Soome uint32_t interfaceIndex,
1408c65ebfc7SToomas Soome const char *name,
1409c65ebfc7SToomas Soome const char *regtype,
1410c65ebfc7SToomas Soome const char *domain,
1411c65ebfc7SToomas Soome DNSServiceResolveReply callBack,
1412c65ebfc7SToomas Soome void *context
1413c65ebfc7SToomas Soome )
1414c65ebfc7SToomas Soome {
1415c65ebfc7SToomas Soome char *ptr;
1416c65ebfc7SToomas Soome size_t len;
1417c65ebfc7SToomas Soome ipc_msg_hdr *hdr;
1418c65ebfc7SToomas Soome DNSServiceErrorType err;
1419c65ebfc7SToomas Soome
1420c65ebfc7SToomas Soome if (!sdRef || !name || !regtype || !domain || !callBack) return kDNSServiceErr_BadParam;
1421c65ebfc7SToomas Soome
1422c65ebfc7SToomas Soome // Need a real InterfaceID for WakeOnResolve
1423c65ebfc7SToomas Soome if ((flags & kDNSServiceFlagsWakeOnResolve) != 0 &&
1424c65ebfc7SToomas Soome ((interfaceIndex == kDNSServiceInterfaceIndexAny) ||
1425c65ebfc7SToomas Soome (interfaceIndex == kDNSServiceInterfaceIndexLocalOnly) ||
1426c65ebfc7SToomas Soome (interfaceIndex == kDNSServiceInterfaceIndexUnicast) ||
1427c65ebfc7SToomas Soome (interfaceIndex == kDNSServiceInterfaceIndexP2P) ||
1428c65ebfc7SToomas Soome (interfaceIndex == kDNSServiceInterfaceIndexBLE)))
1429c65ebfc7SToomas Soome {
1430c65ebfc7SToomas Soome return kDNSServiceErr_BadParam;
1431c65ebfc7SToomas Soome }
1432c65ebfc7SToomas Soome
1433c65ebfc7SToomas Soome if ((interfaceIndex == kDNSServiceInterfaceIndexAny) && includeP2PWithIndexAny())
1434c65ebfc7SToomas Soome flags |= kDNSServiceFlagsIncludeP2P;
1435c65ebfc7SToomas Soome
14363b436d06SToomas Soome err = ConnectToServer(sdRef, flags, resolve_request, handle_resolve_response, (void *)callBack, context);
1437c65ebfc7SToomas Soome if (err) return err; // On error ConnectToServer leaves *sdRef set to NULL
1438c65ebfc7SToomas Soome
1439c65ebfc7SToomas Soome // Calculate total message length
1440c65ebfc7SToomas Soome len = sizeof(flags);
1441c65ebfc7SToomas Soome len += sizeof(interfaceIndex);
1442c65ebfc7SToomas Soome len += strlen(name) + 1;
1443c65ebfc7SToomas Soome len += strlen(regtype) + 1;
1444c65ebfc7SToomas Soome len += strlen(domain) + 1;
1445c65ebfc7SToomas Soome
1446c65ebfc7SToomas Soome hdr = create_hdr(resolve_request, &len, &ptr, (*sdRef)->primary ? 1 : 0, *sdRef);
1447c65ebfc7SToomas Soome if (!hdr) { DNSServiceRefDeallocate(*sdRef); *sdRef = NULL; return kDNSServiceErr_NoMemory; }
1448c65ebfc7SToomas Soome
1449c65ebfc7SToomas Soome put_flags(flags, &ptr);
1450c65ebfc7SToomas Soome put_uint32(interfaceIndex, &ptr);
1451c65ebfc7SToomas Soome put_string(name, &ptr);
1452c65ebfc7SToomas Soome put_string(regtype, &ptr);
1453c65ebfc7SToomas Soome put_string(domain, &ptr);
1454c65ebfc7SToomas Soome
1455c65ebfc7SToomas Soome err = deliver_request(hdr, *sdRef); // Will free hdr for us
1456*472cd20dSToomas Soome #if CHECK_BUNDLE_VERSION
1457*472cd20dSToomas Soome if (err == kDNSServiceErr_NoAuth && !_should_return_noauth_error())
1458*472cd20dSToomas Soome {
1459*472cd20dSToomas Soome err = kDNSServiceErr_NoError;
1460*472cd20dSToomas Soome }
1461*472cd20dSToomas Soome #endif
1462c65ebfc7SToomas Soome if (err) { DNSServiceRefDeallocate(*sdRef); *sdRef = NULL; }
1463c65ebfc7SToomas Soome return err;
1464c65ebfc7SToomas Soome }
1465c65ebfc7SToomas Soome
handle_query_response(DNSServiceOp * const sdr,const CallbackHeader * const cbh,const char * data,const char * const end)1466c65ebfc7SToomas Soome static void handle_query_response(DNSServiceOp *const sdr, const CallbackHeader *const cbh, const char *data, const char *const end)
1467c65ebfc7SToomas Soome {
1468*472cd20dSToomas Soome #if CHECK_BUNDLE_VERSION
1469*472cd20dSToomas Soome if (cbh->cb_err == kDNSServiceErr_PolicyDenied && !_should_return_noauth_error())
1470*472cd20dSToomas Soome {
1471*472cd20dSToomas Soome return;
1472*472cd20dSToomas Soome }
1473*472cd20dSToomas Soome #endif
1474c65ebfc7SToomas Soome uint32_t ttl;
1475c65ebfc7SToomas Soome char name[kDNSServiceMaxDomainName];
1476c65ebfc7SToomas Soome uint16_t rrtype, rrclass, rdlen;
1477c65ebfc7SToomas Soome const char *rdata;
1478c65ebfc7SToomas Soome
1479c65ebfc7SToomas Soome get_string(&data, end, name, kDNSServiceMaxDomainName);
1480c65ebfc7SToomas Soome rrtype = get_uint16(&data, end);
1481c65ebfc7SToomas Soome rrclass = get_uint16(&data, end);
1482c65ebfc7SToomas Soome rdlen = get_uint16(&data, end);
1483c65ebfc7SToomas Soome rdata = get_rdata(&data, end, rdlen);
1484c65ebfc7SToomas Soome ttl = get_uint32(&data, end);
1485c65ebfc7SToomas Soome
1486c65ebfc7SToomas Soome if (!data) syslog(LOG_WARNING, "dnssd_clientstub handle_query_response: error reading result from daemon");
1487c65ebfc7SToomas Soome else ((DNSServiceQueryRecordReply)sdr->AppCallback)(sdr, cbh->cb_flags, cbh->cb_interface, cbh->cb_err, name, rrtype, rrclass, rdlen, rdata, ttl, sdr->AppContext);
1488c65ebfc7SToomas Soome // MUST NOT touch sdr after invoking AppCallback -- client is allowed to dispose it from within callback function
1489c65ebfc7SToomas Soome }
1490c65ebfc7SToomas Soome
1491*472cd20dSToomas Soome #if APPLE_OSX_mDNSResponder
get_required_length_for_defaults(const xpc_object_t defaults)1492*472cd20dSToomas Soome static size_t get_required_length_for_defaults(const xpc_object_t defaults)
1493*472cd20dSToomas Soome {
1494*472cd20dSToomas Soome size_t required_len = 0;
1495*472cd20dSToomas Soome size_t plist_data_len = 0;
1496*472cd20dSToomas Soome // Add length for IPC_TLV_TYPE_RESOLVER_CONFIG_PLIST_DATA.
1497*472cd20dSToomas Soome if (xpc_dictionary_get_data(defaults, kDNSServiceDefaultsKey_ResolverConfigPListData, &plist_data_len))
1498*472cd20dSToomas Soome {
1499*472cd20dSToomas Soome required_len += get_required_tlv16_length(plist_data_len);
1500*472cd20dSToomas Soome }
1501*472cd20dSToomas Soome // Add length for IPC_TLV_TYPE_REQUIRE_PRIVACY.
1502*472cd20dSToomas Soome required_len += get_required_tlv16_length(sizeof(uint8_t));
1503*472cd20dSToomas Soome return required_len;
1504*472cd20dSToomas Soome }
1505*472cd20dSToomas Soome
put_tlvs_for_defaults(const xpc_object_t defaults,ipc_msg_hdr * const hdr,char ** ptr)1506*472cd20dSToomas Soome static void put_tlvs_for_defaults(const xpc_object_t defaults, ipc_msg_hdr *const hdr, char **ptr)
1507*472cd20dSToomas Soome {
1508*472cd20dSToomas Soome uint8_t require_privacy;
1509*472cd20dSToomas Soome size_t plist_data_len = 0;
1510*472cd20dSToomas Soome const uint8_t *const plist_data_ptr = xpc_dictionary_get_data(defaults,
1511*472cd20dSToomas Soome kDNSServiceDefaultsKey_ResolverConfigPListData, &plist_data_len);
1512*472cd20dSToomas Soome if (plist_data_ptr)
1513*472cd20dSToomas Soome {
1514*472cd20dSToomas Soome put_tlv16(IPC_TLV_TYPE_RESOLVER_CONFIG_PLIST_DATA, (uint16_t)plist_data_len, plist_data_ptr, ptr);
1515*472cd20dSToomas Soome }
1516*472cd20dSToomas Soome require_privacy = xpc_dictionary_get_bool(defaults, kDNSServiceDefaultsKey_RequirePrivacy) ? 1 : 0;
1517*472cd20dSToomas Soome put_tlv16(IPC_TLV_TYPE_REQUIRE_PRIVACY, sizeof(require_privacy), &require_privacy, ptr);
1518*472cd20dSToomas Soome hdr->ipc_flags |= IPC_FLAGS_TRAILING_TLVS;
1519*472cd20dSToomas Soome }
1520*472cd20dSToomas Soome #endif
1521*472cd20dSToomas Soome
DNSServiceQueryRecord(DNSServiceRef * sdRef,DNSServiceFlags flags,uint32_t interfaceIndex,const char * name,uint16_t rrtype,uint16_t rrclass,DNSServiceQueryRecordReply callBack,void * context)1522c65ebfc7SToomas Soome DNSServiceErrorType DNSSD_API DNSServiceQueryRecord
1523c65ebfc7SToomas Soome (
1524c65ebfc7SToomas Soome DNSServiceRef *sdRef,
1525c65ebfc7SToomas Soome DNSServiceFlags flags,
1526c65ebfc7SToomas Soome uint32_t interfaceIndex,
1527c65ebfc7SToomas Soome const char *name,
1528c65ebfc7SToomas Soome uint16_t rrtype,
1529c65ebfc7SToomas Soome uint16_t rrclass,
1530c65ebfc7SToomas Soome DNSServiceQueryRecordReply callBack,
1531c65ebfc7SToomas Soome void *context
1532c65ebfc7SToomas Soome )
1533c65ebfc7SToomas Soome {
1534c65ebfc7SToomas Soome char *ptr;
1535c65ebfc7SToomas Soome size_t len;
1536c65ebfc7SToomas Soome ipc_msg_hdr *hdr;
1537c65ebfc7SToomas Soome DNSServiceErrorType err;
1538*472cd20dSToomas Soome #if APPLE_OSX_mDNSResponder
1539*472cd20dSToomas Soome xpc_object_t defaults;
1540*472cd20dSToomas Soome #endif
1541c65ebfc7SToomas Soome // NULL name handled below.
1542c65ebfc7SToomas Soome if (!sdRef || !callBack) return kDNSServiceErr_BadParam;
1543c65ebfc7SToomas Soome
1544c65ebfc7SToomas Soome if ((interfaceIndex == kDNSServiceInterfaceIndexAny) && includeP2PWithIndexAny())
1545c65ebfc7SToomas Soome flags |= kDNSServiceFlagsIncludeP2P;
1546c65ebfc7SToomas Soome
15473b436d06SToomas Soome err = ConnectToServer(sdRef, flags, query_request, handle_query_response, (void *)callBack, context);
1548c65ebfc7SToomas Soome if (err) return err; // On error ConnectToServer leaves *sdRef set to NULL
1549c65ebfc7SToomas Soome
1550c65ebfc7SToomas Soome if (!name) name = "\0";
1551c65ebfc7SToomas Soome
1552c65ebfc7SToomas Soome // Calculate total message length
1553c65ebfc7SToomas Soome len = sizeof(flags);
1554c65ebfc7SToomas Soome len += sizeof(uint32_t); // interfaceIndex
1555c65ebfc7SToomas Soome len += strlen(name) + 1;
1556c65ebfc7SToomas Soome len += 2 * sizeof(uint16_t); // rrtype, rrclass
1557*472cd20dSToomas Soome #if APPLE_OSX_mDNSResponder
1558*472cd20dSToomas Soome defaults = DNSServiceGetRetainedResolverDefaults();
1559*472cd20dSToomas Soome if (defaults)
1560*472cd20dSToomas Soome {
1561*472cd20dSToomas Soome len += get_required_length_for_defaults(defaults);
1562*472cd20dSToomas Soome }
1563*472cd20dSToomas Soome #endif
1564c65ebfc7SToomas Soome hdr = create_hdr(query_request, &len, &ptr, (*sdRef)->primary ? 1 : 0, *sdRef);
1565*472cd20dSToomas Soome if (!hdr)
1566*472cd20dSToomas Soome {
1567*472cd20dSToomas Soome DNSServiceRefDeallocate(*sdRef);
1568*472cd20dSToomas Soome *sdRef = NULL;
1569*472cd20dSToomas Soome #if APPLE_OSX_mDNSResponder
1570*472cd20dSToomas Soome xpc_forget(&defaults);
1571*472cd20dSToomas Soome #endif
1572*472cd20dSToomas Soome return kDNSServiceErr_NoMemory;
1573*472cd20dSToomas Soome }
1574c65ebfc7SToomas Soome put_flags(flags, &ptr);
1575c65ebfc7SToomas Soome put_uint32(interfaceIndex, &ptr);
1576c65ebfc7SToomas Soome put_string(name, &ptr);
1577c65ebfc7SToomas Soome put_uint16(rrtype, &ptr);
1578c65ebfc7SToomas Soome put_uint16(rrclass, &ptr);
1579*472cd20dSToomas Soome #if APPLE_OSX_mDNSResponder
1580*472cd20dSToomas Soome if (defaults)
1581*472cd20dSToomas Soome {
1582*472cd20dSToomas Soome put_tlvs_for_defaults(defaults, hdr, &ptr);
1583*472cd20dSToomas Soome xpc_forget(&defaults);
1584*472cd20dSToomas Soome }
1585*472cd20dSToomas Soome #endif
1586c65ebfc7SToomas Soome err = deliver_request(hdr, *sdRef); // Will free hdr for us
1587*472cd20dSToomas Soome #if CHECK_BUNDLE_VERSION
1588*472cd20dSToomas Soome if (err == kDNSServiceErr_NoAuth && !_should_return_noauth_error())
1589*472cd20dSToomas Soome {
1590*472cd20dSToomas Soome err = kDNSServiceErr_NoError;
1591*472cd20dSToomas Soome }
1592*472cd20dSToomas Soome #endif
1593c65ebfc7SToomas Soome if (err) { DNSServiceRefDeallocate(*sdRef); *sdRef = NULL; }
1594c65ebfc7SToomas Soome return err;
1595c65ebfc7SToomas Soome }
1596c65ebfc7SToomas Soome
handle_addrinfo_response(DNSServiceOp * const sdr,const CallbackHeader * const cbh,const char * data,const char * const end)1597c65ebfc7SToomas Soome static void handle_addrinfo_response(DNSServiceOp *const sdr, const CallbackHeader *const cbh, const char *data, const char *const end)
1598c65ebfc7SToomas Soome {
1599*472cd20dSToomas Soome #if CHECK_BUNDLE_VERSION
1600*472cd20dSToomas Soome if (cbh->cb_err == kDNSServiceErr_PolicyDenied && !_should_return_noauth_error())
1601*472cd20dSToomas Soome {
1602*472cd20dSToomas Soome return;
1603*472cd20dSToomas Soome }
1604*472cd20dSToomas Soome #endif
1605c65ebfc7SToomas Soome char hostname[kDNSServiceMaxDomainName];
1606c65ebfc7SToomas Soome uint16_t rrtype, rrclass, rdlen;
1607c65ebfc7SToomas Soome const char *rdata;
1608c65ebfc7SToomas Soome uint32_t ttl;
1609c65ebfc7SToomas Soome
1610c65ebfc7SToomas Soome get_string(&data, end, hostname, kDNSServiceMaxDomainName);
1611c65ebfc7SToomas Soome rrtype = get_uint16(&data, end);
1612c65ebfc7SToomas Soome rrclass = get_uint16(&data, end);
1613c65ebfc7SToomas Soome rdlen = get_uint16(&data, end);
1614c65ebfc7SToomas Soome rdata = get_rdata (&data, end, rdlen);
1615c65ebfc7SToomas Soome ttl = get_uint32(&data, end);
1616c65ebfc7SToomas Soome (void)rrclass; // Unused
1617c65ebfc7SToomas Soome
1618c65ebfc7SToomas Soome // We only generate client callbacks for A and AAAA results (including NXDOMAIN results for
1619c65ebfc7SToomas Soome // those types, if the client has requested those with the kDNSServiceFlagsReturnIntermediates).
1620c65ebfc7SToomas Soome // Other result types, specifically CNAME referrals, are not communicated to the client, because
1621c65ebfc7SToomas Soome // the DNSServiceGetAddrInfoReply interface doesn't have any meaningful way to communiate CNAME referrals.
1622c65ebfc7SToomas Soome if (!data) syslog(LOG_WARNING, "dnssd_clientstub handle_addrinfo_response: error reading result from daemon");
1623c65ebfc7SToomas Soome else if (rrtype == kDNSServiceType_A || rrtype == kDNSServiceType_AAAA)
1624c65ebfc7SToomas Soome {
1625c65ebfc7SToomas Soome struct sockaddr_in sa4;
1626c65ebfc7SToomas Soome struct sockaddr_in6 sa6;
1627c65ebfc7SToomas Soome const struct sockaddr *const sa = (rrtype == kDNSServiceType_A) ? (struct sockaddr*)&sa4 : (struct sockaddr*)&sa6;
1628c65ebfc7SToomas Soome if (rrtype == kDNSServiceType_A)
1629c65ebfc7SToomas Soome {
1630c65ebfc7SToomas Soome memset(&sa4, 0, sizeof(sa4));
1631c65ebfc7SToomas Soome #ifndef NOT_HAVE_SA_LEN
1632c65ebfc7SToomas Soome sa4.sin_len = sizeof(struct sockaddr_in);
1633c65ebfc7SToomas Soome #endif
1634c65ebfc7SToomas Soome sa4.sin_family = AF_INET;
1635c65ebfc7SToomas Soome // sin_port = 0;
1636c65ebfc7SToomas Soome if (!cbh->cb_err) memcpy(&sa4.sin_addr, rdata, rdlen);
1637c65ebfc7SToomas Soome }
1638c65ebfc7SToomas Soome else
1639c65ebfc7SToomas Soome {
1640c65ebfc7SToomas Soome memset(&sa6, 0, sizeof(sa6));
1641c65ebfc7SToomas Soome #ifndef NOT_HAVE_SA_LEN
1642c65ebfc7SToomas Soome sa6.sin6_len = sizeof(struct sockaddr_in6);
1643c65ebfc7SToomas Soome #endif
1644c65ebfc7SToomas Soome sa6.sin6_family = AF_INET6;
1645c65ebfc7SToomas Soome // sin6_port = 0;
1646c65ebfc7SToomas Soome // sin6_flowinfo = 0;
1647c65ebfc7SToomas Soome // sin6_scope_id = 0;
1648c65ebfc7SToomas Soome if (!cbh->cb_err)
1649c65ebfc7SToomas Soome {
1650c65ebfc7SToomas Soome memcpy(&sa6.sin6_addr, rdata, rdlen);
1651c65ebfc7SToomas Soome if (IN6_IS_ADDR_LINKLOCAL(&sa6.sin6_addr)) sa6.sin6_scope_id = cbh->cb_interface;
1652c65ebfc7SToomas Soome }
1653c65ebfc7SToomas Soome }
1654*472cd20dSToomas Soome
1655c65ebfc7SToomas Soome ((DNSServiceGetAddrInfoReply)sdr->AppCallback)(sdr, cbh->cb_flags, cbh->cb_interface, cbh->cb_err, hostname, sa, ttl, sdr->AppContext);
1656*472cd20dSToomas Soome }
1657*472cd20dSToomas Soome else if (cbh->cb_err == kDNSServiceErr_PolicyDenied)
1658*472cd20dSToomas Soome {
1659*472cd20dSToomas Soome ((DNSServiceGetAddrInfoReply)sdr->AppCallback)(sdr, cbh->cb_flags, cbh->cb_interface, cbh->cb_err, hostname, NULL, ttl, sdr->AppContext);
1660c65ebfc7SToomas Soome }
1661c65ebfc7SToomas Soome }
1662c65ebfc7SToomas Soome
DNSServiceGetAddrInfo(DNSServiceRef * sdRef,DNSServiceFlags flags,uint32_t interfaceIndex,uint32_t protocol,const char * hostname,DNSServiceGetAddrInfoReply callBack,void * context)1663c65ebfc7SToomas Soome DNSServiceErrorType DNSSD_API DNSServiceGetAddrInfo
1664c65ebfc7SToomas Soome (
1665c65ebfc7SToomas Soome DNSServiceRef *sdRef,
1666c65ebfc7SToomas Soome DNSServiceFlags flags,
1667c65ebfc7SToomas Soome uint32_t interfaceIndex,
1668c65ebfc7SToomas Soome uint32_t protocol,
1669c65ebfc7SToomas Soome const char *hostname,
1670c65ebfc7SToomas Soome DNSServiceGetAddrInfoReply callBack,
1671c65ebfc7SToomas Soome void *context /* may be NULL */
1672c65ebfc7SToomas Soome )
1673c65ebfc7SToomas Soome {
1674c65ebfc7SToomas Soome char *ptr;
1675c65ebfc7SToomas Soome size_t len;
1676c65ebfc7SToomas Soome ipc_msg_hdr *hdr;
1677c65ebfc7SToomas Soome DNSServiceErrorType err;
1678*472cd20dSToomas Soome #if APPLE_OSX_mDNSResponder
1679*472cd20dSToomas Soome xpc_object_t defaults;
1680*472cd20dSToomas Soome #endif
1681c65ebfc7SToomas Soome
1682c65ebfc7SToomas Soome if (!sdRef || !hostname || !callBack) return kDNSServiceErr_BadParam;
1683c65ebfc7SToomas Soome
16843b436d06SToomas Soome err = ConnectToServer(sdRef, flags, addrinfo_request, handle_addrinfo_response, (void *)callBack, context);
1685c65ebfc7SToomas Soome if (err)
1686c65ebfc7SToomas Soome {
1687c65ebfc7SToomas Soome return err; // On error ConnectToServer leaves *sdRef set to NULL
1688c65ebfc7SToomas Soome }
1689c65ebfc7SToomas Soome
1690c65ebfc7SToomas Soome // Calculate total message length
1691c65ebfc7SToomas Soome len = sizeof(flags);
1692c65ebfc7SToomas Soome len += sizeof(uint32_t); // interfaceIndex
1693c65ebfc7SToomas Soome len += sizeof(uint32_t); // protocol
1694c65ebfc7SToomas Soome len += strlen(hostname) + 1;
1695*472cd20dSToomas Soome #if APPLE_OSX_mDNSResponder
1696*472cd20dSToomas Soome defaults = DNSServiceGetRetainedResolverDefaults();
1697*472cd20dSToomas Soome if (defaults)
1698*472cd20dSToomas Soome {
1699*472cd20dSToomas Soome len += get_required_length_for_defaults(defaults);
1700*472cd20dSToomas Soome }
1701*472cd20dSToomas Soome #endif
1702c65ebfc7SToomas Soome hdr = create_hdr(addrinfo_request, &len, &ptr, (*sdRef)->primary ? 1 : 0, *sdRef);
1703*472cd20dSToomas Soome if (!hdr)
1704*472cd20dSToomas Soome {
1705*472cd20dSToomas Soome DNSServiceRefDeallocate(*sdRef);
1706*472cd20dSToomas Soome *sdRef = NULL;
1707*472cd20dSToomas Soome #if APPLE_OSX_mDNSResponder
1708*472cd20dSToomas Soome xpc_forget(&defaults);
1709*472cd20dSToomas Soome #endif
1710*472cd20dSToomas Soome return kDNSServiceErr_NoMemory;
1711*472cd20dSToomas Soome }
1712c65ebfc7SToomas Soome put_flags(flags, &ptr);
1713c65ebfc7SToomas Soome put_uint32(interfaceIndex, &ptr);
1714c65ebfc7SToomas Soome put_uint32(protocol, &ptr);
1715c65ebfc7SToomas Soome put_string(hostname, &ptr);
1716*472cd20dSToomas Soome #if APPLE_OSX_mDNSResponder
1717*472cd20dSToomas Soome if (defaults)
1718*472cd20dSToomas Soome {
1719*472cd20dSToomas Soome put_tlvs_for_defaults(defaults, hdr, &ptr);
1720*472cd20dSToomas Soome xpc_forget(&defaults);
1721*472cd20dSToomas Soome }
1722*472cd20dSToomas Soome #endif
1723c65ebfc7SToomas Soome err = deliver_request(hdr, *sdRef); // Will free hdr for us
1724*472cd20dSToomas Soome #if CHECK_BUNDLE_VERSION
1725*472cd20dSToomas Soome if (err == kDNSServiceErr_NoAuth && !_should_return_noauth_error())
1726*472cd20dSToomas Soome {
1727*472cd20dSToomas Soome err = kDNSServiceErr_NoError;
1728*472cd20dSToomas Soome }
1729*472cd20dSToomas Soome #endif
1730c65ebfc7SToomas Soome if (err) { DNSServiceRefDeallocate(*sdRef); *sdRef = NULL; }
1731c65ebfc7SToomas Soome return err;
1732c65ebfc7SToomas Soome }
1733c65ebfc7SToomas Soome
handle_browse_response(DNSServiceOp * const sdr,const CallbackHeader * const cbh,const char * data,const char * const end)1734c65ebfc7SToomas Soome static void handle_browse_response(DNSServiceOp *const sdr, const CallbackHeader *const cbh, const char *data, const char *const end)
1735c65ebfc7SToomas Soome {
1736*472cd20dSToomas Soome #if CHECK_BUNDLE_VERSION
1737*472cd20dSToomas Soome if (cbh->cb_err == kDNSServiceErr_PolicyDenied && !_should_return_noauth_error())
1738*472cd20dSToomas Soome {
1739*472cd20dSToomas Soome return;
1740*472cd20dSToomas Soome }
1741*472cd20dSToomas Soome #endif
1742c65ebfc7SToomas Soome char replyName[256], replyType[kDNSServiceMaxDomainName], replyDomain[kDNSServiceMaxDomainName];
1743c65ebfc7SToomas Soome get_string(&data, end, replyName, 256);
1744c65ebfc7SToomas Soome get_string(&data, end, replyType, kDNSServiceMaxDomainName);
1745c65ebfc7SToomas Soome get_string(&data, end, replyDomain, kDNSServiceMaxDomainName);
1746c65ebfc7SToomas Soome if (!data) syslog(LOG_WARNING, "dnssd_clientstub handle_browse_response: error reading result from daemon");
1747c65ebfc7SToomas Soome else ((DNSServiceBrowseReply)sdr->AppCallback)(sdr, cbh->cb_flags, cbh->cb_interface, cbh->cb_err, replyName, replyType, replyDomain, sdr->AppContext);
1748c65ebfc7SToomas Soome // MUST NOT touch sdr after invoking AppCallback -- client is allowed to dispose it from within callback function
1749c65ebfc7SToomas Soome }
1750c65ebfc7SToomas Soome
DNSServiceBrowse(DNSServiceRef * sdRef,DNSServiceFlags flags,uint32_t interfaceIndex,const char * regtype,const char * domain,DNSServiceBrowseReply callBack,void * context)1751c65ebfc7SToomas Soome DNSServiceErrorType DNSSD_API DNSServiceBrowse
1752c65ebfc7SToomas Soome (
1753c65ebfc7SToomas Soome DNSServiceRef *sdRef,
1754c65ebfc7SToomas Soome DNSServiceFlags flags,
1755c65ebfc7SToomas Soome uint32_t interfaceIndex,
1756c65ebfc7SToomas Soome const char *regtype,
1757c65ebfc7SToomas Soome const char *domain,
1758c65ebfc7SToomas Soome DNSServiceBrowseReply callBack,
1759c65ebfc7SToomas Soome void *context
1760c65ebfc7SToomas Soome )
1761c65ebfc7SToomas Soome {
1762c65ebfc7SToomas Soome char *ptr;
1763c65ebfc7SToomas Soome size_t len;
1764c65ebfc7SToomas Soome ipc_msg_hdr *hdr;
1765c65ebfc7SToomas Soome DNSServiceErrorType err;
1766c65ebfc7SToomas Soome
1767c65ebfc7SToomas Soome // NULL domain handled below
1768c65ebfc7SToomas Soome if (!sdRef || !regtype || !callBack) return kDNSServiceErr_BadParam;
1769c65ebfc7SToomas Soome
1770c65ebfc7SToomas Soome if ((interfaceIndex == kDNSServiceInterfaceIndexAny) && includeP2PWithIndexAny())
1771c65ebfc7SToomas Soome flags |= kDNSServiceFlagsIncludeP2P;
1772c65ebfc7SToomas Soome
17733b436d06SToomas Soome err = ConnectToServer(sdRef, flags, browse_request, handle_browse_response, (void *)callBack, context);
1774c65ebfc7SToomas Soome if (err) return err; // On error ConnectToServer leaves *sdRef set to NULL
1775c65ebfc7SToomas Soome
1776c65ebfc7SToomas Soome if (!domain) domain = "";
1777c65ebfc7SToomas Soome len = sizeof(flags);
1778c65ebfc7SToomas Soome len += sizeof(interfaceIndex);
1779c65ebfc7SToomas Soome len += strlen(regtype) + 1;
1780c65ebfc7SToomas Soome len += strlen(domain) + 1;
1781c65ebfc7SToomas Soome
1782c65ebfc7SToomas Soome hdr = create_hdr(browse_request, &len, &ptr, (*sdRef)->primary ? 1 : 0, *sdRef);
1783c65ebfc7SToomas Soome if (!hdr) { DNSServiceRefDeallocate(*sdRef); *sdRef = NULL; return kDNSServiceErr_NoMemory; }
1784c65ebfc7SToomas Soome
1785c65ebfc7SToomas Soome put_flags(flags, &ptr);
1786c65ebfc7SToomas Soome put_uint32(interfaceIndex, &ptr);
1787c65ebfc7SToomas Soome put_string(regtype, &ptr);
1788c65ebfc7SToomas Soome put_string(domain, &ptr);
1789c65ebfc7SToomas Soome
1790c65ebfc7SToomas Soome err = deliver_request(hdr, *sdRef); // Will free hdr for us
1791*472cd20dSToomas Soome #if CHECK_BUNDLE_VERSION
1792*472cd20dSToomas Soome if (err == kDNSServiceErr_NoAuth && !_should_return_noauth_error())
1793*472cd20dSToomas Soome {
1794*472cd20dSToomas Soome err = kDNSServiceErr_NoError;
1795*472cd20dSToomas Soome }
1796*472cd20dSToomas Soome #endif
1797c65ebfc7SToomas Soome if (err) { DNSServiceRefDeallocate(*sdRef); *sdRef = NULL; }
1798c65ebfc7SToomas Soome return err;
1799c65ebfc7SToomas Soome }
1800c65ebfc7SToomas Soome
DNSServiceSetDefaultDomainForUser(DNSServiceFlags flags,const char * domain)1801c65ebfc7SToomas Soome DNSServiceErrorType DNSSD_API DNSServiceSetDefaultDomainForUser(DNSServiceFlags flags, const char *domain)
1802c65ebfc7SToomas Soome {
1803c65ebfc7SToomas Soome DNSServiceErrorType err;
1804c65ebfc7SToomas Soome DNSServiceOp *tmp;
1805c65ebfc7SToomas Soome char *ptr;
1806c65ebfc7SToomas Soome size_t len;
1807c65ebfc7SToomas Soome ipc_msg_hdr *hdr;
1808c65ebfc7SToomas Soome
1809c65ebfc7SToomas Soome if (!domain) return kDNSServiceErr_BadParam;
1810c65ebfc7SToomas Soome len = sizeof(flags) + strlen(domain) + 1;
1811c65ebfc7SToomas Soome
1812c65ebfc7SToomas Soome err = ConnectToServer(&tmp, 0, setdomain_request, NULL, NULL, NULL);
1813c65ebfc7SToomas Soome if (err) return err;
1814c65ebfc7SToomas Soome
1815c65ebfc7SToomas Soome hdr = create_hdr(setdomain_request, &len, &ptr, 0, tmp);
1816c65ebfc7SToomas Soome if (!hdr) { DNSServiceRefDeallocate(tmp); return kDNSServiceErr_NoMemory; }
1817c65ebfc7SToomas Soome
1818c65ebfc7SToomas Soome put_flags(flags, &ptr);
1819c65ebfc7SToomas Soome put_string(domain, &ptr);
1820c65ebfc7SToomas Soome err = deliver_request(hdr, tmp); // Will free hdr for us
1821c65ebfc7SToomas Soome DNSServiceRefDeallocate(tmp);
1822c65ebfc7SToomas Soome return err;
1823c65ebfc7SToomas Soome }
1824c65ebfc7SToomas Soome
handle_regservice_response(DNSServiceOp * const sdr,const CallbackHeader * const cbh,const char * data,const char * const end)1825c65ebfc7SToomas Soome static void handle_regservice_response(DNSServiceOp *const sdr, const CallbackHeader *const cbh, const char *data, const char *const end)
1826c65ebfc7SToomas Soome {
1827*472cd20dSToomas Soome #if CHECK_BUNDLE_VERSION
1828*472cd20dSToomas Soome if (cbh->cb_err == kDNSServiceErr_PolicyDenied && !_should_return_noauth_error())
1829*472cd20dSToomas Soome {
1830*472cd20dSToomas Soome return;
1831*472cd20dSToomas Soome }
1832*472cd20dSToomas Soome #endif
1833c65ebfc7SToomas Soome char name[256], regtype[kDNSServiceMaxDomainName], domain[kDNSServiceMaxDomainName];
1834c65ebfc7SToomas Soome get_string(&data, end, name, 256);
1835c65ebfc7SToomas Soome get_string(&data, end, regtype, kDNSServiceMaxDomainName);
1836c65ebfc7SToomas Soome get_string(&data, end, domain, kDNSServiceMaxDomainName);
1837c65ebfc7SToomas Soome if (!data) syslog(LOG_WARNING, "dnssd_clientstub handle_regservice_response: error reading result from daemon");
1838c65ebfc7SToomas Soome else ((DNSServiceRegisterReply)sdr->AppCallback)(sdr, cbh->cb_flags, cbh->cb_err, name, regtype, domain, sdr->AppContext);
1839c65ebfc7SToomas Soome // MUST NOT touch sdr after invoking AppCallback -- client is allowed to dispose it from within callback function
1840c65ebfc7SToomas Soome }
1841c65ebfc7SToomas Soome
DNSServiceRegister(DNSServiceRef * sdRef,DNSServiceFlags flags,uint32_t interfaceIndex,const char * name,const char * regtype,const char * domain,const char * host,uint16_t PortInNetworkByteOrder,uint16_t txtLen,const void * txtRecord,DNSServiceRegisterReply callBack,void * context)1842c65ebfc7SToomas Soome DNSServiceErrorType DNSSD_API DNSServiceRegister
1843c65ebfc7SToomas Soome (
1844c65ebfc7SToomas Soome DNSServiceRef *sdRef,
1845c65ebfc7SToomas Soome DNSServiceFlags flags,
1846c65ebfc7SToomas Soome uint32_t interfaceIndex,
1847c65ebfc7SToomas Soome const char *name,
1848c65ebfc7SToomas Soome const char *regtype,
1849c65ebfc7SToomas Soome const char *domain,
1850c65ebfc7SToomas Soome const char *host,
1851c65ebfc7SToomas Soome uint16_t PortInNetworkByteOrder,
1852c65ebfc7SToomas Soome uint16_t txtLen,
1853c65ebfc7SToomas Soome const void *txtRecord,
1854c65ebfc7SToomas Soome DNSServiceRegisterReply callBack,
1855c65ebfc7SToomas Soome void *context
1856c65ebfc7SToomas Soome )
1857c65ebfc7SToomas Soome {
1858c65ebfc7SToomas Soome char *ptr;
1859c65ebfc7SToomas Soome size_t len;
1860c65ebfc7SToomas Soome ipc_msg_hdr *hdr;
1861c65ebfc7SToomas Soome DNSServiceErrorType err;
1862c65ebfc7SToomas Soome union { uint16_t s; u_char b[2]; } port = { PortInNetworkByteOrder };
1863c65ebfc7SToomas Soome
1864c65ebfc7SToomas Soome if (!sdRef || !regtype) return kDNSServiceErr_BadParam;
1865c65ebfc7SToomas Soome if (!name) name = "";
1866c65ebfc7SToomas Soome if (!domain) domain = "";
1867c65ebfc7SToomas Soome if (!host) host = "";
1868c65ebfc7SToomas Soome if (!txtRecord) txtRecord = (void*)"";
1869c65ebfc7SToomas Soome
1870c65ebfc7SToomas Soome // No callback must have auto-rename
1871c65ebfc7SToomas Soome if (!callBack && (flags & kDNSServiceFlagsNoAutoRename)) return kDNSServiceErr_BadParam;
1872c65ebfc7SToomas Soome
1873c65ebfc7SToomas Soome if ((interfaceIndex == kDNSServiceInterfaceIndexAny) && includeP2PWithIndexAny())
1874c65ebfc7SToomas Soome flags |= kDNSServiceFlagsIncludeP2P;
1875c65ebfc7SToomas Soome
18763b436d06SToomas Soome err = ConnectToServer(sdRef, flags, reg_service_request, callBack ? handle_regservice_response : NULL, (void *)callBack, context);
1877c65ebfc7SToomas Soome if (err) return err; // On error ConnectToServer leaves *sdRef set to NULL
1878c65ebfc7SToomas Soome
1879c65ebfc7SToomas Soome len = sizeof(DNSServiceFlags);
1880c65ebfc7SToomas Soome len += sizeof(uint32_t); // interfaceIndex
1881c65ebfc7SToomas Soome len += strlen(name) + strlen(regtype) + strlen(domain) + strlen(host) + 4;
1882c65ebfc7SToomas Soome len += 2 * sizeof(uint16_t); // port, txtLen
1883c65ebfc7SToomas Soome len += txtLen;
1884c65ebfc7SToomas Soome
1885c65ebfc7SToomas Soome hdr = create_hdr(reg_service_request, &len, &ptr, (*sdRef)->primary ? 1 : 0, *sdRef);
1886c65ebfc7SToomas Soome if (!hdr) { DNSServiceRefDeallocate(*sdRef); *sdRef = NULL; return kDNSServiceErr_NoMemory; }
1887c65ebfc7SToomas Soome if (!callBack) hdr->ipc_flags |= IPC_FLAGS_NOREPLY;
1888c65ebfc7SToomas Soome
1889c65ebfc7SToomas Soome put_flags(flags, &ptr);
1890c65ebfc7SToomas Soome put_uint32(interfaceIndex, &ptr);
1891c65ebfc7SToomas Soome put_string(name, &ptr);
1892c65ebfc7SToomas Soome put_string(regtype, &ptr);
1893c65ebfc7SToomas Soome put_string(domain, &ptr);
1894c65ebfc7SToomas Soome put_string(host, &ptr);
1895c65ebfc7SToomas Soome *ptr++ = port.b[0];
1896c65ebfc7SToomas Soome *ptr++ = port.b[1];
1897c65ebfc7SToomas Soome put_uint16(txtLen, &ptr);
1898c65ebfc7SToomas Soome put_rdata(txtLen, txtRecord, &ptr);
1899c65ebfc7SToomas Soome
1900c65ebfc7SToomas Soome err = deliver_request(hdr, *sdRef); // Will free hdr for us
1901*472cd20dSToomas Soome #if CHECK_BUNDLE_VERSION
1902*472cd20dSToomas Soome if (err == kDNSServiceErr_NoAuth && !_should_return_noauth_error())
1903*472cd20dSToomas Soome {
1904*472cd20dSToomas Soome err = kDNSServiceErr_NoError;
1905*472cd20dSToomas Soome }
1906*472cd20dSToomas Soome #endif
1907c65ebfc7SToomas Soome if (err) { DNSServiceRefDeallocate(*sdRef); *sdRef = NULL; }
1908c65ebfc7SToomas Soome return err;
1909c65ebfc7SToomas Soome }
1910c65ebfc7SToomas Soome
handle_enumeration_response(DNSServiceOp * const sdr,const CallbackHeader * const cbh,const char * data,const char * const end)1911c65ebfc7SToomas Soome static void handle_enumeration_response(DNSServiceOp *const sdr, const CallbackHeader *const cbh, const char *data, const char *const end)
1912c65ebfc7SToomas Soome {
1913c65ebfc7SToomas Soome char domain[kDNSServiceMaxDomainName];
1914c65ebfc7SToomas Soome get_string(&data, end, domain, kDNSServiceMaxDomainName);
1915c65ebfc7SToomas Soome if (!data) syslog(LOG_WARNING, "dnssd_clientstub handle_enumeration_response: error reading result from daemon");
1916c65ebfc7SToomas Soome else ((DNSServiceDomainEnumReply)sdr->AppCallback)(sdr, cbh->cb_flags, cbh->cb_interface, cbh->cb_err, domain, sdr->AppContext);
1917c65ebfc7SToomas Soome // MUST NOT touch sdr after invoking AppCallback -- client is allowed to dispose it from within callback function
1918c65ebfc7SToomas Soome }
1919c65ebfc7SToomas Soome
DNSServiceEnumerateDomains(DNSServiceRef * sdRef,DNSServiceFlags flags,uint32_t interfaceIndex,DNSServiceDomainEnumReply callBack,void * context)1920c65ebfc7SToomas Soome DNSServiceErrorType DNSSD_API DNSServiceEnumerateDomains
1921c65ebfc7SToomas Soome (
1922c65ebfc7SToomas Soome DNSServiceRef *sdRef,
1923c65ebfc7SToomas Soome DNSServiceFlags flags,
1924c65ebfc7SToomas Soome uint32_t interfaceIndex,
1925c65ebfc7SToomas Soome DNSServiceDomainEnumReply callBack,
1926c65ebfc7SToomas Soome void *context
1927c65ebfc7SToomas Soome )
1928c65ebfc7SToomas Soome {
1929c65ebfc7SToomas Soome char *ptr;
1930c65ebfc7SToomas Soome size_t len;
1931c65ebfc7SToomas Soome ipc_msg_hdr *hdr;
1932c65ebfc7SToomas Soome DNSServiceErrorType err;
1933c65ebfc7SToomas Soome int f1;
1934c65ebfc7SToomas Soome int f2;
1935c65ebfc7SToomas Soome
1936c65ebfc7SToomas Soome if (!sdRef || !callBack) return kDNSServiceErr_BadParam;
1937c65ebfc7SToomas Soome
1938c65ebfc7SToomas Soome f1 = (flags & kDNSServiceFlagsBrowseDomains) != 0;
1939c65ebfc7SToomas Soome f2 = (flags & kDNSServiceFlagsRegistrationDomains) != 0;
1940c65ebfc7SToomas Soome if (f1 + f2 != 1) return kDNSServiceErr_BadParam;
1941c65ebfc7SToomas Soome
19423b436d06SToomas Soome err = ConnectToServer(sdRef, flags, enumeration_request, handle_enumeration_response, (void *)callBack, context);
1943c65ebfc7SToomas Soome if (err) return err; // On error ConnectToServer leaves *sdRef set to NULL
1944c65ebfc7SToomas Soome
1945c65ebfc7SToomas Soome len = sizeof(DNSServiceFlags);
1946c65ebfc7SToomas Soome len += sizeof(uint32_t);
1947c65ebfc7SToomas Soome
1948c65ebfc7SToomas Soome hdr = create_hdr(enumeration_request, &len, &ptr, (*sdRef)->primary ? 1 : 0, *sdRef);
1949c65ebfc7SToomas Soome if (!hdr) { DNSServiceRefDeallocate(*sdRef); *sdRef = NULL; return kDNSServiceErr_NoMemory; }
1950c65ebfc7SToomas Soome
1951c65ebfc7SToomas Soome put_flags(flags, &ptr);
1952c65ebfc7SToomas Soome put_uint32(interfaceIndex, &ptr);
1953c65ebfc7SToomas Soome
1954c65ebfc7SToomas Soome err = deliver_request(hdr, *sdRef); // Will free hdr for us
1955c65ebfc7SToomas Soome if (err) { DNSServiceRefDeallocate(*sdRef); *sdRef = NULL; }
1956c65ebfc7SToomas Soome return err;
1957c65ebfc7SToomas Soome }
1958c65ebfc7SToomas Soome
ConnectionResponse(DNSServiceOp * const sdr,const CallbackHeader * const cbh,const char * const data,const char * const end)1959c65ebfc7SToomas Soome static void ConnectionResponse(DNSServiceOp *const sdr, const CallbackHeader *const cbh, const char *const data, const char *const end)
1960c65ebfc7SToomas Soome {
1961c65ebfc7SToomas Soome (void)data; // Unused
1962c65ebfc7SToomas Soome
1963c65ebfc7SToomas Soome //printf("ConnectionResponse got %d\n", cbh->ipc_hdr.op);
1964c65ebfc7SToomas Soome if (cbh->ipc_hdr.op != reg_record_reply_op)
1965c65ebfc7SToomas Soome {
1966c65ebfc7SToomas Soome // When using kDNSServiceFlagsShareConnection, need to search the list of associated DNSServiceOps
1967c65ebfc7SToomas Soome // to find the one this response is intended for, and then call through to its ProcessReply handler.
1968c65ebfc7SToomas Soome // We start with our first subordinate DNSServiceRef -- don't want to accidentally match the parent DNSServiceRef.
1969c65ebfc7SToomas Soome DNSServiceOp *op = sdr->next;
1970c65ebfc7SToomas Soome while (op && (op->uid.u32[0] != cbh->ipc_hdr.client_context.u32[0] || op->uid.u32[1] != cbh->ipc_hdr.client_context.u32[1]))
1971c65ebfc7SToomas Soome op = op->next;
1972c65ebfc7SToomas Soome // Note: We may sometimes not find a matching DNSServiceOp, in the case where the client has
1973c65ebfc7SToomas Soome // cancelled the subordinate DNSServiceOp, but there are still messages in the pipeline from the daemon
1974c65ebfc7SToomas Soome if (op && op->ProcessReply) op->ProcessReply(op, cbh, data, end);
1975c65ebfc7SToomas Soome // WARNING: Don't touch op or sdr after this -- client may have called DNSServiceRefDeallocate
1976c65ebfc7SToomas Soome return;
1977c65ebfc7SToomas Soome }
1978c65ebfc7SToomas Soome else
1979c65ebfc7SToomas Soome {
1980*472cd20dSToomas Soome #if CHECK_BUNDLE_VERSION
1981*472cd20dSToomas Soome if (cbh->cb_err == kDNSServiceErr_PolicyDenied && !_should_return_noauth_error())
1982*472cd20dSToomas Soome {
1983*472cd20dSToomas Soome return;
1984*472cd20dSToomas Soome }
1985*472cd20dSToomas Soome #endif
1986c65ebfc7SToomas Soome DNSRecordRef rec;
1987c65ebfc7SToomas Soome for (rec = sdr->rec; rec; rec = rec->recnext)
1988c65ebfc7SToomas Soome {
1989c65ebfc7SToomas Soome if (rec->uid.u32[0] == cbh->ipc_hdr.client_context.u32[0] && rec->uid.u32[1] == cbh->ipc_hdr.client_context.u32[1])
1990c65ebfc7SToomas Soome break;
1991c65ebfc7SToomas Soome }
1992c65ebfc7SToomas Soome // The record might have been freed already and hence not an
1993c65ebfc7SToomas Soome // error if the record is not found.
1994c65ebfc7SToomas Soome if (!rec)
1995c65ebfc7SToomas Soome {
1996*472cd20dSToomas Soome syslog(LOG_INFO, "dnssd_clientstub ConnectionResponse: Record not found");
1997c65ebfc7SToomas Soome return;
1998c65ebfc7SToomas Soome }
1999c65ebfc7SToomas Soome if (rec->sdr != sdr)
2000c65ebfc7SToomas Soome {
2001*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub ConnectionResponse: Record sdr mismatch: rec %p sdr %p", rec->sdr, sdr);
2002c65ebfc7SToomas Soome return;
2003c65ebfc7SToomas Soome }
2004c65ebfc7SToomas Soome
2005c65ebfc7SToomas Soome if (sdr->op == connection_request || sdr->op == connection_delegate_request)
2006c65ebfc7SToomas Soome {
2007c65ebfc7SToomas Soome rec->AppCallback(rec->sdr, rec, cbh->cb_flags, cbh->cb_err, rec->AppContext);
2008c65ebfc7SToomas Soome }
2009c65ebfc7SToomas Soome else
2010c65ebfc7SToomas Soome {
2011c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub ConnectionResponse: sdr->op != connection_request");
2012c65ebfc7SToomas Soome rec->AppCallback(rec->sdr, rec, 0, kDNSServiceErr_Unknown, rec->AppContext);
2013c65ebfc7SToomas Soome }
2014c65ebfc7SToomas Soome // MUST NOT touch sdr after invoking AppCallback -- client is allowed to dispose it from within callback function
2015c65ebfc7SToomas Soome }
2016c65ebfc7SToomas Soome }
2017c65ebfc7SToomas Soome
DNSServiceCreateConnection(DNSServiceRef * sdRef)2018c65ebfc7SToomas Soome DNSServiceErrorType DNSSD_API DNSServiceCreateConnection(DNSServiceRef *sdRef)
2019c65ebfc7SToomas Soome {
2020c65ebfc7SToomas Soome DNSServiceErrorType err;
2021c65ebfc7SToomas Soome char *ptr;
2022c65ebfc7SToomas Soome size_t len = 0;
2023c65ebfc7SToomas Soome ipc_msg_hdr *hdr;
2024c65ebfc7SToomas Soome
2025c65ebfc7SToomas Soome if (!sdRef) return kDNSServiceErr_BadParam;
2026c65ebfc7SToomas Soome err = ConnectToServer(sdRef, 0, connection_request, ConnectionResponse, NULL, NULL);
2027c65ebfc7SToomas Soome if (err) return err; // On error ConnectToServer leaves *sdRef set to NULL
2028c65ebfc7SToomas Soome
2029c65ebfc7SToomas Soome hdr = create_hdr(connection_request, &len, &ptr, 0, *sdRef);
2030c65ebfc7SToomas Soome if (!hdr) { DNSServiceRefDeallocate(*sdRef); *sdRef = NULL; return kDNSServiceErr_NoMemory; }
2031c65ebfc7SToomas Soome
2032c65ebfc7SToomas Soome err = deliver_request(hdr, *sdRef); // Will free hdr for us
2033c65ebfc7SToomas Soome if (err) { DNSServiceRefDeallocate(*sdRef); *sdRef = NULL; }
2034c65ebfc7SToomas Soome return err;
2035c65ebfc7SToomas Soome }
2036c65ebfc7SToomas Soome
2037*472cd20dSToomas Soome #if APPLE_OSX_mDNSResponder && !TARGET_OS_SIMULATOR
DNSServiceCreateDelegateConnection(DNSServiceRef * sdRef,int32_t pid,uuid_t uuid)2038c65ebfc7SToomas Soome DNSServiceErrorType DNSSD_API DNSServiceCreateDelegateConnection(DNSServiceRef *sdRef, int32_t pid, uuid_t uuid)
2039c65ebfc7SToomas Soome {
2040c65ebfc7SToomas Soome char *ptr;
2041c65ebfc7SToomas Soome size_t len = 0;
2042c65ebfc7SToomas Soome ipc_msg_hdr *hdr;
2043c65ebfc7SToomas Soome
2044c65ebfc7SToomas Soome if (!sdRef) return kDNSServiceErr_BadParam;
2045c65ebfc7SToomas Soome DNSServiceErrorType err = ConnectToServer(sdRef, 0, connection_delegate_request, ConnectionResponse, NULL, NULL);
2046c65ebfc7SToomas Soome if (err)
2047c65ebfc7SToomas Soome {
2048c65ebfc7SToomas Soome return err; // On error ConnectToServer leaves *sdRef set to NULL
2049c65ebfc7SToomas Soome }
2050c65ebfc7SToomas Soome
2051c65ebfc7SToomas Soome // Only one of the two options can be set. If pid is zero, uuid is used.
2052c65ebfc7SToomas Soome // If both are specified only pid will be used. We send across the pid
2053c65ebfc7SToomas Soome // so that the daemon knows what to read from the socket.
2054c65ebfc7SToomas Soome
2055c65ebfc7SToomas Soome len += sizeof(int32_t);
2056c65ebfc7SToomas Soome
2057c65ebfc7SToomas Soome hdr = create_hdr(connection_delegate_request, &len, &ptr, 0, *sdRef);
2058c65ebfc7SToomas Soome if (!hdr)
2059c65ebfc7SToomas Soome {
2060c65ebfc7SToomas Soome DNSServiceRefDeallocate(*sdRef);
2061c65ebfc7SToomas Soome *sdRef = NULL;
2062c65ebfc7SToomas Soome return kDNSServiceErr_NoMemory;
2063c65ebfc7SToomas Soome }
2064c65ebfc7SToomas Soome
2065c65ebfc7SToomas Soome if (pid && setsockopt((*sdRef)->sockfd, SOL_SOCKET, SO_DELEGATED, &pid, sizeof(pid)) == -1)
2066c65ebfc7SToomas Soome {
2067*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceCreateDelegateConnection: Could not setsockopt() for PID[%d], no entitlements or process(pid) invalid errno:%d (%s)", pid, errno, strerror(errno));
2068c65ebfc7SToomas Soome // Free the hdr in case we return before calling deliver_request()
2069c65ebfc7SToomas Soome if (hdr)
2070c65ebfc7SToomas Soome free(hdr);
2071c65ebfc7SToomas Soome DNSServiceRefDeallocate(*sdRef);
2072c65ebfc7SToomas Soome *sdRef = NULL;
2073c65ebfc7SToomas Soome return kDNSServiceErr_NoAuth;
2074c65ebfc7SToomas Soome }
2075c65ebfc7SToomas Soome
2076c65ebfc7SToomas Soome if (!pid && setsockopt((*sdRef)->sockfd, SOL_SOCKET, SO_DELEGATED_UUID, uuid, sizeof(uuid_t)) == -1)
2077c65ebfc7SToomas Soome {
2078*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceCreateDelegateConnection: Could not setsockopt() for UUID, no entitlements or process(uuid) invalid errno:%d (%s) ", errno, strerror(errno));
2079c65ebfc7SToomas Soome // Free the hdr in case we return before calling deliver_request()
2080c65ebfc7SToomas Soome if (hdr)
2081c65ebfc7SToomas Soome free(hdr);
2082c65ebfc7SToomas Soome DNSServiceRefDeallocate(*sdRef);
2083c65ebfc7SToomas Soome *sdRef = NULL;
2084c65ebfc7SToomas Soome return kDNSServiceErr_NoAuth;
2085c65ebfc7SToomas Soome }
2086c65ebfc7SToomas Soome
2087c65ebfc7SToomas Soome put_uint32(pid, &ptr);
2088c65ebfc7SToomas Soome
2089c65ebfc7SToomas Soome err = deliver_request(hdr, *sdRef); // Will free hdr for us
2090c65ebfc7SToomas Soome if (err)
2091c65ebfc7SToomas Soome {
2092c65ebfc7SToomas Soome DNSServiceRefDeallocate(*sdRef);
2093c65ebfc7SToomas Soome *sdRef = NULL;
2094c65ebfc7SToomas Soome }
2095c65ebfc7SToomas Soome return err;
2096c65ebfc7SToomas Soome }
2097*472cd20dSToomas Soome #elif TARGET_OS_SIMULATOR // This hack is for Simulator platform only
DNSServiceCreateDelegateConnection(DNSServiceRef * sdRef,int32_t pid,uuid_t uuid)2098c65ebfc7SToomas Soome DNSServiceErrorType DNSSD_API DNSServiceCreateDelegateConnection(DNSServiceRef *sdRef, int32_t pid, uuid_t uuid)
2099c65ebfc7SToomas Soome {
2100c65ebfc7SToomas Soome (void) pid;
2101c65ebfc7SToomas Soome (void) uuid;
2102c65ebfc7SToomas Soome return DNSServiceCreateConnection(sdRef);
2103c65ebfc7SToomas Soome }
2104c65ebfc7SToomas Soome #endif
2105c65ebfc7SToomas Soome
DNSServiceRegisterRecord(DNSServiceRef sdRef,DNSRecordRef * RecordRef,DNSServiceFlags flags,uint32_t interfaceIndex,const char * fullname,uint16_t rrtype,uint16_t rrclass,uint16_t rdlen,const void * rdata,uint32_t ttl,DNSServiceRegisterRecordReply callBack,void * context)2106c65ebfc7SToomas Soome DNSServiceErrorType DNSSD_API DNSServiceRegisterRecord
2107c65ebfc7SToomas Soome (
2108c65ebfc7SToomas Soome DNSServiceRef sdRef,
2109c65ebfc7SToomas Soome DNSRecordRef *RecordRef,
2110c65ebfc7SToomas Soome DNSServiceFlags flags,
2111c65ebfc7SToomas Soome uint32_t interfaceIndex,
2112c65ebfc7SToomas Soome const char *fullname,
2113c65ebfc7SToomas Soome uint16_t rrtype,
2114c65ebfc7SToomas Soome uint16_t rrclass,
2115c65ebfc7SToomas Soome uint16_t rdlen,
2116c65ebfc7SToomas Soome const void *rdata,
2117c65ebfc7SToomas Soome uint32_t ttl,
2118c65ebfc7SToomas Soome DNSServiceRegisterRecordReply callBack,
2119c65ebfc7SToomas Soome void *context
2120c65ebfc7SToomas Soome )
2121c65ebfc7SToomas Soome {
2122*472cd20dSToomas Soome DNSServiceErrorType err;
2123c65ebfc7SToomas Soome char *ptr;
2124c65ebfc7SToomas Soome size_t len;
2125c65ebfc7SToomas Soome ipc_msg_hdr *hdr = NULL;
2126c65ebfc7SToomas Soome DNSRecordRef rref = NULL;
2127c65ebfc7SToomas Soome DNSRecord **p;
2128*472cd20dSToomas Soome // Verify that only one of the following flags is set.
2129c65ebfc7SToomas Soome int f1 = (flags & kDNSServiceFlagsShared) != 0;
2130c65ebfc7SToomas Soome int f2 = (flags & kDNSServiceFlagsUnique) != 0;
2131*472cd20dSToomas Soome int f3 = (flags & kDNSServiceFlagsKnownUnique) != 0;
2132*472cd20dSToomas Soome if (f1 + f2 + f3 != 1) return kDNSServiceErr_BadParam;
2133c65ebfc7SToomas Soome
2134c65ebfc7SToomas Soome if ((interfaceIndex == kDNSServiceInterfaceIndexAny) && includeP2PWithIndexAny())
2135c65ebfc7SToomas Soome flags |= kDNSServiceFlagsIncludeP2P;
2136c65ebfc7SToomas Soome
2137c65ebfc7SToomas Soome if (!sdRef || !RecordRef || !fullname || (!rdata && rdlen) || !callBack)
2138c65ebfc7SToomas Soome {
2139c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceRegisterRecord called with NULL parameter");
2140c65ebfc7SToomas Soome return kDNSServiceErr_BadParam;
2141c65ebfc7SToomas Soome }
2142c65ebfc7SToomas Soome
2143c65ebfc7SToomas Soome if (!DNSServiceRefValid(sdRef))
2144c65ebfc7SToomas Soome {
2145c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceRegisterRecord called with invalid DNSServiceRef %p %08X %08X", sdRef, sdRef->sockfd, sdRef->validator);
2146c65ebfc7SToomas Soome return kDNSServiceErr_BadReference;
2147c65ebfc7SToomas Soome }
2148c65ebfc7SToomas Soome
2149c65ebfc7SToomas Soome if (sdRef->op != connection_request && sdRef->op != connection_delegate_request)
2150c65ebfc7SToomas Soome {
2151c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceRegisterRecord called with non-DNSServiceCreateConnection DNSServiceRef %p %d", sdRef, sdRef->op);
2152c65ebfc7SToomas Soome return kDNSServiceErr_BadReference;
2153c65ebfc7SToomas Soome }
2154c65ebfc7SToomas Soome
2155c65ebfc7SToomas Soome *RecordRef = NULL;
2156c65ebfc7SToomas Soome
2157c65ebfc7SToomas Soome len = sizeof(DNSServiceFlags);
2158c65ebfc7SToomas Soome len += 2 * sizeof(uint32_t); // interfaceIndex, ttl
2159c65ebfc7SToomas Soome len += 3 * sizeof(uint16_t); // rrtype, rrclass, rdlen
2160c65ebfc7SToomas Soome len += strlen(fullname) + 1;
2161c65ebfc7SToomas Soome len += rdlen;
2162c65ebfc7SToomas Soome
2163c65ebfc7SToomas Soome // Bump up the uid. Normally for shared operations (kDNSServiceFlagsShareConnection), this
2164c65ebfc7SToomas Soome // is done in ConnectToServer. For DNSServiceRegisterRecord, ConnectToServer has already
2165c65ebfc7SToomas Soome // been called. As multiple DNSServiceRegisterRecords can be multiplexed over a single
2166c65ebfc7SToomas Soome // connection, we need a way to demultiplex the response so that the callback corresponding
2167c65ebfc7SToomas Soome // to the right DNSServiceRegisterRecord instance can be called. Use the same mechanism that
2168c65ebfc7SToomas Soome // is used by kDNSServiceFlagsShareConnection. create_hdr copies the uid value to ipc
2169c65ebfc7SToomas Soome // hdr->client_context which will be returned in the ipc response.
2170c65ebfc7SToomas Soome if (++sdRef->uid.u32[0] == 0)
2171c65ebfc7SToomas Soome ++sdRef->uid.u32[1];
2172c65ebfc7SToomas Soome hdr = create_hdr(reg_record_request, &len, &ptr, 1, sdRef);
2173c65ebfc7SToomas Soome if (!hdr) return kDNSServiceErr_NoMemory;
2174c65ebfc7SToomas Soome
2175c65ebfc7SToomas Soome put_flags(flags, &ptr);
2176c65ebfc7SToomas Soome put_uint32(interfaceIndex, &ptr);
2177c65ebfc7SToomas Soome put_string(fullname, &ptr);
2178c65ebfc7SToomas Soome put_uint16(rrtype, &ptr);
2179c65ebfc7SToomas Soome put_uint16(rrclass, &ptr);
2180c65ebfc7SToomas Soome put_uint16(rdlen, &ptr);
2181c65ebfc7SToomas Soome put_rdata(rdlen, rdata, &ptr);
2182c65ebfc7SToomas Soome put_uint32(ttl, &ptr);
2183c65ebfc7SToomas Soome
2184c65ebfc7SToomas Soome rref = malloc(sizeof(DNSRecord));
2185c65ebfc7SToomas Soome if (!rref) { free(hdr); return kDNSServiceErr_NoMemory; }
2186c65ebfc7SToomas Soome rref->AppContext = context;
2187c65ebfc7SToomas Soome rref->AppCallback = callBack;
2188c65ebfc7SToomas Soome rref->record_index = sdRef->max_index++;
2189c65ebfc7SToomas Soome rref->sdr = sdRef;
2190c65ebfc7SToomas Soome rref->recnext = NULL;
2191c65ebfc7SToomas Soome *RecordRef = rref;
2192c65ebfc7SToomas Soome // Remember the uid that we are sending across so that we can match
2193c65ebfc7SToomas Soome // when the response comes back.
2194c65ebfc7SToomas Soome rref->uid = sdRef->uid;
2195c65ebfc7SToomas Soome hdr->reg_index = rref->record_index;
2196c65ebfc7SToomas Soome
2197c65ebfc7SToomas Soome p = &(sdRef)->rec;
2198c65ebfc7SToomas Soome while (*p) p = &(*p)->recnext;
2199c65ebfc7SToomas Soome *p = rref;
2200c65ebfc7SToomas Soome
2201*472cd20dSToomas Soome err = deliver_request(hdr, sdRef); // Will free hdr for us
2202*472cd20dSToomas Soome #if CHECK_BUNDLE_VERSION
2203*472cd20dSToomas Soome if (err == kDNSServiceErr_NoAuth && !_should_return_noauth_error())
2204*472cd20dSToomas Soome {
2205*472cd20dSToomas Soome err = kDNSServiceErr_NoError;
2206*472cd20dSToomas Soome }
2207*472cd20dSToomas Soome #endif
2208*472cd20dSToomas Soome return err;
2209c65ebfc7SToomas Soome }
2210c65ebfc7SToomas Soome
2211c65ebfc7SToomas Soome // sdRef returned by DNSServiceRegister()
DNSServiceAddRecord(DNSServiceRef sdRef,DNSRecordRef * RecordRef,DNSServiceFlags flags,uint16_t rrtype,uint16_t rdlen,const void * rdata,uint32_t ttl)2212c65ebfc7SToomas Soome DNSServiceErrorType DNSSD_API DNSServiceAddRecord
2213c65ebfc7SToomas Soome (
2214c65ebfc7SToomas Soome DNSServiceRef sdRef,
2215c65ebfc7SToomas Soome DNSRecordRef *RecordRef,
2216c65ebfc7SToomas Soome DNSServiceFlags flags,
2217c65ebfc7SToomas Soome uint16_t rrtype,
2218c65ebfc7SToomas Soome uint16_t rdlen,
2219c65ebfc7SToomas Soome const void *rdata,
2220c65ebfc7SToomas Soome uint32_t ttl
2221c65ebfc7SToomas Soome )
2222c65ebfc7SToomas Soome {
2223c65ebfc7SToomas Soome ipc_msg_hdr *hdr;
2224c65ebfc7SToomas Soome size_t len = 0;
2225c65ebfc7SToomas Soome char *ptr;
2226c65ebfc7SToomas Soome DNSRecordRef rref;
2227c65ebfc7SToomas Soome DNSRecord **p;
2228c65ebfc7SToomas Soome
2229c65ebfc7SToomas Soome if (!sdRef || !RecordRef || (!rdata && rdlen))
2230c65ebfc7SToomas Soome {
2231c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceAddRecord called with NULL parameter");
2232c65ebfc7SToomas Soome return kDNSServiceErr_BadParam;
2233c65ebfc7SToomas Soome }
2234c65ebfc7SToomas Soome if (sdRef->op != reg_service_request)
2235c65ebfc7SToomas Soome {
2236c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceAddRecord called with non-DNSServiceRegister DNSServiceRef %p %d", sdRef, sdRef->op);
2237c65ebfc7SToomas Soome return kDNSServiceErr_BadReference;
2238c65ebfc7SToomas Soome }
2239c65ebfc7SToomas Soome
2240c65ebfc7SToomas Soome if (!DNSServiceRefValid(sdRef))
2241c65ebfc7SToomas Soome {
2242c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceAddRecord called with invalid DNSServiceRef %p %08X %08X", sdRef, sdRef->sockfd, sdRef->validator);
2243c65ebfc7SToomas Soome return kDNSServiceErr_BadReference;
2244c65ebfc7SToomas Soome }
2245c65ebfc7SToomas Soome
2246c65ebfc7SToomas Soome *RecordRef = NULL;
2247c65ebfc7SToomas Soome
2248c65ebfc7SToomas Soome len += 2 * sizeof(uint16_t); // rrtype, rdlen
2249c65ebfc7SToomas Soome len += rdlen;
2250c65ebfc7SToomas Soome len += sizeof(uint32_t);
2251c65ebfc7SToomas Soome len += sizeof(DNSServiceFlags);
2252c65ebfc7SToomas Soome
2253c65ebfc7SToomas Soome hdr = create_hdr(add_record_request, &len, &ptr, 1, sdRef);
2254c65ebfc7SToomas Soome if (!hdr) return kDNSServiceErr_NoMemory;
2255c65ebfc7SToomas Soome put_flags(flags, &ptr);
2256c65ebfc7SToomas Soome put_uint16(rrtype, &ptr);
2257c65ebfc7SToomas Soome put_uint16(rdlen, &ptr);
2258c65ebfc7SToomas Soome put_rdata(rdlen, rdata, &ptr);
2259c65ebfc7SToomas Soome put_uint32(ttl, &ptr);
2260c65ebfc7SToomas Soome
2261c65ebfc7SToomas Soome rref = malloc(sizeof(DNSRecord));
2262c65ebfc7SToomas Soome if (!rref) { free(hdr); return kDNSServiceErr_NoMemory; }
2263c65ebfc7SToomas Soome rref->AppContext = NULL;
2264c65ebfc7SToomas Soome rref->AppCallback = NULL;
2265c65ebfc7SToomas Soome rref->record_index = sdRef->max_index++;
2266c65ebfc7SToomas Soome rref->sdr = sdRef;
2267c65ebfc7SToomas Soome rref->recnext = NULL;
2268c65ebfc7SToomas Soome *RecordRef = rref;
2269c65ebfc7SToomas Soome hdr->reg_index = rref->record_index;
2270c65ebfc7SToomas Soome
2271c65ebfc7SToomas Soome p = &(sdRef)->rec;
2272c65ebfc7SToomas Soome while (*p) p = &(*p)->recnext;
2273c65ebfc7SToomas Soome *p = rref;
2274c65ebfc7SToomas Soome
2275c65ebfc7SToomas Soome return deliver_request(hdr, sdRef); // Will free hdr for us
2276c65ebfc7SToomas Soome }
2277c65ebfc7SToomas Soome
2278c65ebfc7SToomas Soome // DNSRecordRef returned by DNSServiceRegisterRecord or DNSServiceAddRecord
DNSServiceUpdateRecord(DNSServiceRef sdRef,DNSRecordRef RecordRef,DNSServiceFlags flags,uint16_t rdlen,const void * rdata,uint32_t ttl)2279c65ebfc7SToomas Soome DNSServiceErrorType DNSSD_API DNSServiceUpdateRecord
2280c65ebfc7SToomas Soome (
2281c65ebfc7SToomas Soome DNSServiceRef sdRef,
2282c65ebfc7SToomas Soome DNSRecordRef RecordRef,
2283c65ebfc7SToomas Soome DNSServiceFlags flags,
2284c65ebfc7SToomas Soome uint16_t rdlen,
2285c65ebfc7SToomas Soome const void *rdata,
2286c65ebfc7SToomas Soome uint32_t ttl
2287c65ebfc7SToomas Soome )
2288c65ebfc7SToomas Soome {
2289c65ebfc7SToomas Soome ipc_msg_hdr *hdr;
2290c65ebfc7SToomas Soome size_t len = 0;
2291c65ebfc7SToomas Soome char *ptr;
2292c65ebfc7SToomas Soome
2293c65ebfc7SToomas Soome if (!sdRef || (!rdata && rdlen))
2294c65ebfc7SToomas Soome {
2295c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceUpdateRecord called with NULL parameter");
2296c65ebfc7SToomas Soome return kDNSServiceErr_BadParam;
2297c65ebfc7SToomas Soome }
2298c65ebfc7SToomas Soome
2299c65ebfc7SToomas Soome if (!DNSServiceRefValid(sdRef))
2300c65ebfc7SToomas Soome {
2301c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceUpdateRecord called with invalid DNSServiceRef %p %08X %08X", sdRef, sdRef->sockfd, sdRef->validator);
2302c65ebfc7SToomas Soome return kDNSServiceErr_BadReference;
2303c65ebfc7SToomas Soome }
2304c65ebfc7SToomas Soome
2305c65ebfc7SToomas Soome // Note: RecordRef is allowed to be NULL
2306c65ebfc7SToomas Soome
2307c65ebfc7SToomas Soome len += sizeof(uint16_t);
2308c65ebfc7SToomas Soome len += rdlen;
2309c65ebfc7SToomas Soome len += sizeof(uint32_t);
2310c65ebfc7SToomas Soome len += sizeof(DNSServiceFlags);
2311c65ebfc7SToomas Soome
2312c65ebfc7SToomas Soome hdr = create_hdr(update_record_request, &len, &ptr, 1, sdRef);
2313c65ebfc7SToomas Soome if (!hdr) return kDNSServiceErr_NoMemory;
2314c65ebfc7SToomas Soome hdr->reg_index = RecordRef ? RecordRef->record_index : TXT_RECORD_INDEX;
2315c65ebfc7SToomas Soome put_flags(flags, &ptr);
2316c65ebfc7SToomas Soome put_uint16(rdlen, &ptr);
2317c65ebfc7SToomas Soome put_rdata(rdlen, rdata, &ptr);
2318c65ebfc7SToomas Soome put_uint32(ttl, &ptr);
2319c65ebfc7SToomas Soome return deliver_request(hdr, sdRef); // Will free hdr for us
2320c65ebfc7SToomas Soome }
2321c65ebfc7SToomas Soome
DNSServiceRemoveRecord(DNSServiceRef sdRef,DNSRecordRef RecordRef,DNSServiceFlags flags)2322c65ebfc7SToomas Soome DNSServiceErrorType DNSSD_API DNSServiceRemoveRecord
2323c65ebfc7SToomas Soome (
2324c65ebfc7SToomas Soome DNSServiceRef sdRef,
2325c65ebfc7SToomas Soome DNSRecordRef RecordRef,
2326c65ebfc7SToomas Soome DNSServiceFlags flags
2327c65ebfc7SToomas Soome )
2328c65ebfc7SToomas Soome {
2329c65ebfc7SToomas Soome ipc_msg_hdr *hdr;
2330c65ebfc7SToomas Soome size_t len = 0;
2331c65ebfc7SToomas Soome char *ptr;
2332c65ebfc7SToomas Soome DNSServiceErrorType err;
2333c65ebfc7SToomas Soome
2334c65ebfc7SToomas Soome if (!sdRef) { syslog(LOG_WARNING, "dnssd_clientstub DNSServiceRemoveRecord called with NULL DNSServiceRef"); return kDNSServiceErr_BadParam; }
2335c65ebfc7SToomas Soome if (!RecordRef) { syslog(LOG_WARNING, "dnssd_clientstub DNSServiceRemoveRecord called with NULL DNSRecordRef"); return kDNSServiceErr_BadParam; }
2336c65ebfc7SToomas Soome if (!sdRef->max_index) { syslog(LOG_WARNING, "dnssd_clientstub DNSServiceRemoveRecord called with bad DNSServiceRef"); return kDNSServiceErr_BadReference; }
2337c65ebfc7SToomas Soome
2338c65ebfc7SToomas Soome if (!DNSServiceRefValid(sdRef))
2339c65ebfc7SToomas Soome {
2340c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceRemoveRecord called with invalid DNSServiceRef %p %08X %08X", sdRef, sdRef->sockfd, sdRef->validator);
2341c65ebfc7SToomas Soome return kDNSServiceErr_BadReference;
2342c65ebfc7SToomas Soome }
2343c65ebfc7SToomas Soome
2344c65ebfc7SToomas Soome len += sizeof(flags);
2345c65ebfc7SToomas Soome hdr = create_hdr(remove_record_request, &len, &ptr, 1, sdRef);
2346c65ebfc7SToomas Soome if (!hdr) return kDNSServiceErr_NoMemory;
2347c65ebfc7SToomas Soome hdr->reg_index = RecordRef->record_index;
2348c65ebfc7SToomas Soome put_flags(flags, &ptr);
2349c65ebfc7SToomas Soome err = deliver_request(hdr, sdRef); // Will free hdr for us
2350c65ebfc7SToomas Soome if (!err)
2351c65ebfc7SToomas Soome {
2352c65ebfc7SToomas Soome // This RecordRef could have been allocated in DNSServiceRegisterRecord or DNSServiceAddRecord.
2353c65ebfc7SToomas Soome // If so, delink from the list before freeing
2354c65ebfc7SToomas Soome DNSRecord **p = &sdRef->rec;
2355c65ebfc7SToomas Soome while (*p && *p != RecordRef) p = &(*p)->recnext;
2356c65ebfc7SToomas Soome if (*p) *p = RecordRef->recnext;
2357c65ebfc7SToomas Soome free(RecordRef);
2358c65ebfc7SToomas Soome }
2359c65ebfc7SToomas Soome return err;
2360c65ebfc7SToomas Soome }
2361c65ebfc7SToomas Soome
DNSServiceReconfirmRecord(DNSServiceFlags flags,uint32_t interfaceIndex,const char * fullname,uint16_t rrtype,uint16_t rrclass,uint16_t rdlen,const void * rdata)2362c65ebfc7SToomas Soome DNSServiceErrorType DNSSD_API DNSServiceReconfirmRecord
2363c65ebfc7SToomas Soome (
2364c65ebfc7SToomas Soome DNSServiceFlags flags,
2365c65ebfc7SToomas Soome uint32_t interfaceIndex,
2366c65ebfc7SToomas Soome const char *fullname,
2367c65ebfc7SToomas Soome uint16_t rrtype,
2368c65ebfc7SToomas Soome uint16_t rrclass,
2369c65ebfc7SToomas Soome uint16_t rdlen,
2370c65ebfc7SToomas Soome const void *rdata
2371c65ebfc7SToomas Soome )
2372c65ebfc7SToomas Soome {
2373c65ebfc7SToomas Soome DNSServiceErrorType err;
2374c65ebfc7SToomas Soome char *ptr;
2375c65ebfc7SToomas Soome size_t len;
2376c65ebfc7SToomas Soome ipc_msg_hdr *hdr;
2377c65ebfc7SToomas Soome DNSServiceOp *tmp = NULL;
2378c65ebfc7SToomas Soome
2379c65ebfc7SToomas Soome if (!fullname || (!rdata && rdlen)) return kDNSServiceErr_BadParam;
2380c65ebfc7SToomas Soome
2381c65ebfc7SToomas Soome err = ConnectToServer(&tmp, flags, reconfirm_record_request, NULL, NULL, NULL);
2382c65ebfc7SToomas Soome if (err) return err;
2383c65ebfc7SToomas Soome
2384c65ebfc7SToomas Soome len = sizeof(DNSServiceFlags);
2385c65ebfc7SToomas Soome len += sizeof(uint32_t);
2386c65ebfc7SToomas Soome len += strlen(fullname) + 1;
2387c65ebfc7SToomas Soome len += 3 * sizeof(uint16_t);
2388c65ebfc7SToomas Soome len += rdlen;
2389c65ebfc7SToomas Soome hdr = create_hdr(reconfirm_record_request, &len, &ptr, 0, tmp);
2390c65ebfc7SToomas Soome if (!hdr) { DNSServiceRefDeallocate(tmp); return kDNSServiceErr_NoMemory; }
2391c65ebfc7SToomas Soome
2392c65ebfc7SToomas Soome put_flags(flags, &ptr);
2393c65ebfc7SToomas Soome put_uint32(interfaceIndex, &ptr);
2394c65ebfc7SToomas Soome put_string(fullname, &ptr);
2395c65ebfc7SToomas Soome put_uint16(rrtype, &ptr);
2396c65ebfc7SToomas Soome put_uint16(rrclass, &ptr);
2397c65ebfc7SToomas Soome put_uint16(rdlen, &ptr);
2398c65ebfc7SToomas Soome put_rdata(rdlen, rdata, &ptr);
2399c65ebfc7SToomas Soome
2400c65ebfc7SToomas Soome err = deliver_request(hdr, tmp); // Will free hdr for us
2401c65ebfc7SToomas Soome DNSServiceRefDeallocate(tmp);
2402c65ebfc7SToomas Soome return err;
2403c65ebfc7SToomas Soome }
2404c65ebfc7SToomas Soome
2405c65ebfc7SToomas Soome
handle_port_mapping_response(DNSServiceOp * const sdr,const CallbackHeader * const cbh,const char * data,const char * const end)2406c65ebfc7SToomas Soome static void handle_port_mapping_response(DNSServiceOp *const sdr, const CallbackHeader *const cbh, const char *data, const char *const end)
2407c65ebfc7SToomas Soome {
2408c65ebfc7SToomas Soome union { uint32_t l; u_char b[4]; } addr;
2409c65ebfc7SToomas Soome uint8_t protocol;
2410c65ebfc7SToomas Soome union { uint16_t s; u_char b[2]; } internalPort;
2411c65ebfc7SToomas Soome union { uint16_t s; u_char b[2]; } externalPort;
2412c65ebfc7SToomas Soome uint32_t ttl;
2413c65ebfc7SToomas Soome
2414c65ebfc7SToomas Soome if (!data || data + 13 > end) goto fail;
2415c65ebfc7SToomas Soome
2416c65ebfc7SToomas Soome addr.b[0] = *data++;
2417c65ebfc7SToomas Soome addr.b[1] = *data++;
2418c65ebfc7SToomas Soome addr.b[2] = *data++;
2419c65ebfc7SToomas Soome addr.b[3] = *data++;
2420c65ebfc7SToomas Soome protocol = *data++;
2421c65ebfc7SToomas Soome internalPort.b[0] = *data++;
2422c65ebfc7SToomas Soome internalPort.b[1] = *data++;
2423c65ebfc7SToomas Soome externalPort.b[0] = *data++;
2424c65ebfc7SToomas Soome externalPort.b[1] = *data++;
2425c65ebfc7SToomas Soome ttl = get_uint32(&data, end);
2426c65ebfc7SToomas Soome if (!data) goto fail;
2427c65ebfc7SToomas Soome
2428c65ebfc7SToomas Soome ((DNSServiceNATPortMappingReply)sdr->AppCallback)(sdr, cbh->cb_flags, cbh->cb_interface, cbh->cb_err, addr.l, protocol, internalPort.s, externalPort.s, ttl, sdr->AppContext);
2429c65ebfc7SToomas Soome return;
2430c65ebfc7SToomas Soome // MUST NOT touch sdr after invoking AppCallback -- client is allowed to dispose it from within callback function
2431c65ebfc7SToomas Soome
2432c65ebfc7SToomas Soome fail :
2433c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub handle_port_mapping_response: error reading result from daemon");
2434c65ebfc7SToomas Soome }
2435c65ebfc7SToomas Soome
DNSServiceNATPortMappingCreate(DNSServiceRef * sdRef,DNSServiceFlags flags,uint32_t interfaceIndex,uint32_t protocol,uint16_t internalPortInNetworkByteOrder,uint16_t externalPortInNetworkByteOrder,uint32_t ttl,DNSServiceNATPortMappingReply callBack,void * context)2436c65ebfc7SToomas Soome DNSServiceErrorType DNSSD_API DNSServiceNATPortMappingCreate
2437c65ebfc7SToomas Soome (
2438c65ebfc7SToomas Soome DNSServiceRef *sdRef,
2439c65ebfc7SToomas Soome DNSServiceFlags flags,
2440c65ebfc7SToomas Soome uint32_t interfaceIndex,
2441c65ebfc7SToomas Soome uint32_t protocol, /* TCP and/or UDP */
2442c65ebfc7SToomas Soome uint16_t internalPortInNetworkByteOrder,
2443c65ebfc7SToomas Soome uint16_t externalPortInNetworkByteOrder,
2444c65ebfc7SToomas Soome uint32_t ttl, /* time to live in seconds */
2445c65ebfc7SToomas Soome DNSServiceNATPortMappingReply callBack,
2446c65ebfc7SToomas Soome void *context /* may be NULL */
2447c65ebfc7SToomas Soome )
2448c65ebfc7SToomas Soome {
2449c65ebfc7SToomas Soome char *ptr;
2450c65ebfc7SToomas Soome size_t len;
2451c65ebfc7SToomas Soome ipc_msg_hdr *hdr;
2452c65ebfc7SToomas Soome union { uint16_t s; u_char b[2]; } internalPort = { internalPortInNetworkByteOrder };
2453c65ebfc7SToomas Soome union { uint16_t s; u_char b[2]; } externalPort = { externalPortInNetworkByteOrder };
2454c65ebfc7SToomas Soome
24553b436d06SToomas Soome DNSServiceErrorType err = ConnectToServer(sdRef, flags, port_mapping_request, handle_port_mapping_response, (void *)callBack, context);
2456c65ebfc7SToomas Soome if (err) return err; // On error ConnectToServer leaves *sdRef set to NULL
2457c65ebfc7SToomas Soome
2458c65ebfc7SToomas Soome len = sizeof(flags);
2459c65ebfc7SToomas Soome len += sizeof(interfaceIndex);
2460c65ebfc7SToomas Soome len += sizeof(protocol);
2461c65ebfc7SToomas Soome len += sizeof(internalPort);
2462c65ebfc7SToomas Soome len += sizeof(externalPort);
2463c65ebfc7SToomas Soome len += sizeof(ttl);
2464c65ebfc7SToomas Soome
2465c65ebfc7SToomas Soome hdr = create_hdr(port_mapping_request, &len, &ptr, (*sdRef)->primary ? 1 : 0, *sdRef);
2466c65ebfc7SToomas Soome if (!hdr) { DNSServiceRefDeallocate(*sdRef); *sdRef = NULL; return kDNSServiceErr_NoMemory; }
2467c65ebfc7SToomas Soome
2468c65ebfc7SToomas Soome put_flags(flags, &ptr);
2469c65ebfc7SToomas Soome put_uint32(interfaceIndex, &ptr);
2470c65ebfc7SToomas Soome put_uint32(protocol, &ptr);
2471c65ebfc7SToomas Soome *ptr++ = internalPort.b[0];
2472c65ebfc7SToomas Soome *ptr++ = internalPort.b[1];
2473c65ebfc7SToomas Soome *ptr++ = externalPort.b[0];
2474c65ebfc7SToomas Soome *ptr++ = externalPort.b[1];
2475c65ebfc7SToomas Soome put_uint32(ttl, &ptr);
2476c65ebfc7SToomas Soome
2477c65ebfc7SToomas Soome err = deliver_request(hdr, *sdRef); // Will free hdr for us
2478c65ebfc7SToomas Soome if (err) { DNSServiceRefDeallocate(*sdRef); *sdRef = NULL; }
2479c65ebfc7SToomas Soome return err;
2480c65ebfc7SToomas Soome }
2481c65ebfc7SToomas Soome
2482c65ebfc7SToomas Soome #if _DNS_SD_LIBDISPATCH
DNSServiceSetDispatchQueue(DNSServiceRef service,dispatch_queue_t queue)2483c65ebfc7SToomas Soome DNSServiceErrorType DNSSD_API DNSServiceSetDispatchQueue
2484c65ebfc7SToomas Soome (
2485c65ebfc7SToomas Soome DNSServiceRef service,
2486c65ebfc7SToomas Soome dispatch_queue_t queue
2487c65ebfc7SToomas Soome )
2488c65ebfc7SToomas Soome {
2489c65ebfc7SToomas Soome int dnssd_fd = DNSServiceRefSockFD(service);
2490c65ebfc7SToomas Soome if (dnssd_fd == dnssd_InvalidSocket) return kDNSServiceErr_BadParam;
2491c65ebfc7SToomas Soome if (!queue)
2492c65ebfc7SToomas Soome {
2493c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub: DNSServiceSetDispatchQueue dispatch queue NULL");
2494c65ebfc7SToomas Soome return kDNSServiceErr_BadParam;
2495c65ebfc7SToomas Soome }
2496c65ebfc7SToomas Soome if (service->disp_queue)
2497c65ebfc7SToomas Soome {
2498c65ebfc7SToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceSetDispatchQueue dispatch queue set already");
2499c65ebfc7SToomas Soome return kDNSServiceErr_BadParam;
2500c65ebfc7SToomas Soome }
2501c65ebfc7SToomas Soome if (service->disp_source)
2502c65ebfc7SToomas Soome {
2503*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceSetDispatchQueue dispatch source set already");
2504c65ebfc7SToomas Soome return kDNSServiceErr_BadParam;
2505c65ebfc7SToomas Soome }
2506c65ebfc7SToomas Soome service->disp_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, dnssd_fd, 0, queue);
2507c65ebfc7SToomas Soome if (!service->disp_source)
2508c65ebfc7SToomas Soome {
2509*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceSetDispatchQueue dispatch_source_create failed");
2510c65ebfc7SToomas Soome return kDNSServiceErr_NoMemory;
2511c65ebfc7SToomas Soome }
2512c65ebfc7SToomas Soome service->disp_queue = queue;
2513c65ebfc7SToomas Soome dispatch_source_set_event_handler(service->disp_source, ^{DNSServiceProcessResult(service);});
2514c65ebfc7SToomas Soome dispatch_source_set_cancel_handler(service->disp_source, ^{dnssd_close(dnssd_fd);});
2515c65ebfc7SToomas Soome dispatch_resume(service->disp_source);
2516c65ebfc7SToomas Soome return kDNSServiceErr_NoError;
2517c65ebfc7SToomas Soome }
2518c65ebfc7SToomas Soome #endif // _DNS_SD_LIBDISPATCH
2519c65ebfc7SToomas Soome
2520c65ebfc7SToomas Soome #if !defined(_WIN32)
2521c65ebfc7SToomas Soome
SleepKeepaliveCallback(DNSServiceRef sdRef,DNSRecordRef rec,const DNSServiceFlags flags,DNSServiceErrorType errorCode,void * context)2522c65ebfc7SToomas Soome static void DNSSD_API SleepKeepaliveCallback(DNSServiceRef sdRef, DNSRecordRef rec, const DNSServiceFlags flags,
2523c65ebfc7SToomas Soome DNSServiceErrorType errorCode, void *context)
2524c65ebfc7SToomas Soome {
2525c65ebfc7SToomas Soome SleepKAContext *ka = (SleepKAContext *)context;
2526c65ebfc7SToomas Soome (void)rec; // Unused
2527c65ebfc7SToomas Soome (void)flags; // Unused
2528c65ebfc7SToomas Soome
2529c65ebfc7SToomas Soome if (sdRef->kacontext != context)
2530*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub SleepKeepaliveCallback context mismatch");
2531c65ebfc7SToomas Soome
2532c65ebfc7SToomas Soome if (ka->AppCallback)
2533c65ebfc7SToomas Soome ((DNSServiceSleepKeepaliveReply)ka->AppCallback)(sdRef, errorCode, ka->AppContext);
2534c65ebfc7SToomas Soome }
2535c65ebfc7SToomas Soome
2536*472cd20dSToomas Soome static DNSServiceErrorType _DNSServiceSleepKeepalive_sockaddr
2537*472cd20dSToomas Soome (
2538*472cd20dSToomas Soome DNSServiceRef * sdRef,
2539*472cd20dSToomas Soome DNSServiceFlags flags,
2540*472cd20dSToomas Soome const struct sockaddr * localAddr,
2541*472cd20dSToomas Soome const struct sockaddr * remoteAddr,
2542*472cd20dSToomas Soome unsigned int timeout,
2543*472cd20dSToomas Soome DNSServiceSleepKeepaliveReply callBack,
2544*472cd20dSToomas Soome void * context
2545*472cd20dSToomas Soome );
2546*472cd20dSToomas Soome
DNSServiceSleepKeepalive(DNSServiceRef * sdRef,DNSServiceFlags flags,int fd,unsigned int timeout,DNSServiceSleepKeepaliveReply callBack,void * context)2547c65ebfc7SToomas Soome DNSServiceErrorType DNSSD_API DNSServiceSleepKeepalive
2548c65ebfc7SToomas Soome (
2549c65ebfc7SToomas Soome DNSServiceRef *sdRef,
2550c65ebfc7SToomas Soome DNSServiceFlags flags,
2551c65ebfc7SToomas Soome int fd,
2552c65ebfc7SToomas Soome unsigned int timeout,
2553c65ebfc7SToomas Soome DNSServiceSleepKeepaliveReply callBack,
2554c65ebfc7SToomas Soome void *context
2555c65ebfc7SToomas Soome )
2556c65ebfc7SToomas Soome {
2557c65ebfc7SToomas Soome struct sockaddr_storage lss;
2558c65ebfc7SToomas Soome struct sockaddr_storage rss;
2559c65ebfc7SToomas Soome socklen_t len1, len2;
2560*472cd20dSToomas Soome
2561*472cd20dSToomas Soome len1 = sizeof(lss);
2562*472cd20dSToomas Soome if (getsockname(fd, (struct sockaddr *)&lss, &len1) < 0)
2563*472cd20dSToomas Soome {
2564*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceSleepKeepalive: getsockname %d\n", errno);
2565*472cd20dSToomas Soome return kDNSServiceErr_BadParam;
2566*472cd20dSToomas Soome }
2567*472cd20dSToomas Soome
2568*472cd20dSToomas Soome len2 = sizeof(rss);
2569*472cd20dSToomas Soome if (getpeername(fd, (struct sockaddr *)&rss, &len2) < 0)
2570*472cd20dSToomas Soome {
2571*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceSleepKeepalive: getpeername %d\n", errno);
2572*472cd20dSToomas Soome return kDNSServiceErr_BadParam;
2573*472cd20dSToomas Soome }
2574*472cd20dSToomas Soome
2575*472cd20dSToomas Soome if (len1 != len2)
2576*472cd20dSToomas Soome {
2577*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceSleepKeepalive local/remote info not same");
2578*472cd20dSToomas Soome return kDNSServiceErr_Unknown;
2579*472cd20dSToomas Soome }
2580*472cd20dSToomas Soome return _DNSServiceSleepKeepalive_sockaddr(sdRef, flags, (const struct sockaddr *)&lss, (const struct sockaddr *)&rss,
2581*472cd20dSToomas Soome timeout, callBack, context);
2582*472cd20dSToomas Soome }
2583*472cd20dSToomas Soome
DNSServiceSleepKeepalive_sockaddr(DNSServiceRef * sdRef,DNSServiceFlags flags,const struct sockaddr * localAddr,const struct sockaddr * remoteAddr,unsigned int timeout,DNSServiceSleepKeepaliveReply callBack,void * context)2584*472cd20dSToomas Soome DNSServiceErrorType DNSSD_API DNSServiceSleepKeepalive_sockaddr
2585*472cd20dSToomas Soome (
2586*472cd20dSToomas Soome DNSServiceRef * sdRef,
2587*472cd20dSToomas Soome DNSServiceFlags flags,
2588*472cd20dSToomas Soome const struct sockaddr * localAddr,
2589*472cd20dSToomas Soome const struct sockaddr * remoteAddr,
2590*472cd20dSToomas Soome unsigned int timeout,
2591*472cd20dSToomas Soome DNSServiceSleepKeepaliveReply callBack,
2592*472cd20dSToomas Soome void * context
2593*472cd20dSToomas Soome )
2594*472cd20dSToomas Soome {
2595*472cd20dSToomas Soome return _DNSServiceSleepKeepalive_sockaddr(sdRef, flags, localAddr, remoteAddr, timeout, callBack, context );
2596*472cd20dSToomas Soome }
2597*472cd20dSToomas Soome
_DNSServiceSleepKeepalive_sockaddr(DNSServiceRef * sdRef,DNSServiceFlags flags,const struct sockaddr * localAddr,const struct sockaddr * remoteAddr,unsigned int timeout,DNSServiceSleepKeepaliveReply callBack,void * context)2598*472cd20dSToomas Soome static DNSServiceErrorType _DNSServiceSleepKeepalive_sockaddr
2599*472cd20dSToomas Soome (
2600*472cd20dSToomas Soome DNSServiceRef * sdRef,
2601*472cd20dSToomas Soome DNSServiceFlags flags,
2602*472cd20dSToomas Soome const struct sockaddr * localAddr,
2603*472cd20dSToomas Soome const struct sockaddr * remoteAddr,
2604*472cd20dSToomas Soome unsigned int timeout,
2605*472cd20dSToomas Soome DNSServiceSleepKeepaliveReply callBack,
2606*472cd20dSToomas Soome void * context
2607*472cd20dSToomas Soome )
2608*472cd20dSToomas Soome {
2609*472cd20dSToomas Soome char source_str[INET6_ADDRSTRLEN];
2610*472cd20dSToomas Soome char target_str[INET6_ADDRSTRLEN];
2611c65ebfc7SToomas Soome unsigned int len, proxyreclen;
2612c65ebfc7SToomas Soome char buf[256];
2613c65ebfc7SToomas Soome DNSServiceErrorType err;
2614c65ebfc7SToomas Soome DNSRecordRef record = NULL;
2615c65ebfc7SToomas Soome char name[10];
2616c65ebfc7SToomas Soome char recname[128];
2617c65ebfc7SToomas Soome SleepKAContext *ka;
2618c65ebfc7SToomas Soome unsigned int i, unique;
2619c65ebfc7SToomas Soome
2620c65ebfc7SToomas Soome (void) flags; //unused
2621c65ebfc7SToomas Soome if (!timeout) return kDNSServiceErr_BadParam;
2622c65ebfc7SToomas Soome
2623c65ebfc7SToomas Soome unique = 0;
2624*472cd20dSToomas Soome if ((localAddr->sa_family == AF_INET) && (remoteAddr->sa_family == AF_INET))
2625c65ebfc7SToomas Soome {
2626*472cd20dSToomas Soome const struct sockaddr_in *sl = (const struct sockaddr_in *)localAddr;
2627*472cd20dSToomas Soome const struct sockaddr_in *sr = (const struct sockaddr_in *)remoteAddr;
2628c65ebfc7SToomas Soome unsigned char *ptr = (unsigned char *)&sl->sin_addr;
2629c65ebfc7SToomas Soome
2630c65ebfc7SToomas Soome if (!inet_ntop(AF_INET, (const void *)&sr->sin_addr, target_str, sizeof (target_str)))
2631c65ebfc7SToomas Soome {
2632*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceSleepKeepalive remote info failed %d", errno);
2633c65ebfc7SToomas Soome return kDNSServiceErr_Unknown;
2634c65ebfc7SToomas Soome }
2635c65ebfc7SToomas Soome if (!inet_ntop(AF_INET, (const void *)&sl->sin_addr, source_str, sizeof (source_str)))
2636c65ebfc7SToomas Soome {
2637*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceSleepKeepalive local info failed %d", errno);
2638c65ebfc7SToomas Soome return kDNSServiceErr_Unknown;
2639c65ebfc7SToomas Soome }
2640c65ebfc7SToomas Soome // Sum of all bytes in the local address and port should result in a unique
2641c65ebfc7SToomas Soome // number in the local network
2642c65ebfc7SToomas Soome for (i = 0; i < sizeof(struct in_addr); i++)
2643c65ebfc7SToomas Soome unique += ptr[i];
2644c65ebfc7SToomas Soome unique += sl->sin_port;
2645c65ebfc7SToomas Soome len = snprintf(buf+1, sizeof(buf) - 1, "t=%u h=%s d=%s l=%u r=%u", timeout, source_str, target_str, ntohs(sl->sin_port), ntohs(sr->sin_port));
2646c65ebfc7SToomas Soome }
2647*472cd20dSToomas Soome else if ((localAddr->sa_family == AF_INET6) && (remoteAddr->sa_family == AF_INET6))
2648c65ebfc7SToomas Soome {
2649*472cd20dSToomas Soome const struct sockaddr_in6 *sl6 = (const struct sockaddr_in6 *)localAddr;
2650*472cd20dSToomas Soome const struct sockaddr_in6 *sr6 = (const struct sockaddr_in6 *)remoteAddr;
2651c65ebfc7SToomas Soome unsigned char *ptr = (unsigned char *)&sl6->sin6_addr;
2652c65ebfc7SToomas Soome
2653c65ebfc7SToomas Soome if (!inet_ntop(AF_INET6, (const void *)&sr6->sin6_addr, target_str, sizeof (target_str)))
2654c65ebfc7SToomas Soome {
2655*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceSleepKeepalive remote6 info failed %d", errno);
2656c65ebfc7SToomas Soome return kDNSServiceErr_Unknown;
2657c65ebfc7SToomas Soome }
2658c65ebfc7SToomas Soome if (!inet_ntop(AF_INET6, (const void *)&sl6->sin6_addr, source_str, sizeof (source_str)))
2659c65ebfc7SToomas Soome {
2660*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceSleepKeepalive local6 info failed %d", errno);
2661c65ebfc7SToomas Soome return kDNSServiceErr_Unknown;
2662c65ebfc7SToomas Soome }
2663c65ebfc7SToomas Soome for (i = 0; i < sizeof(struct in6_addr); i++)
2664c65ebfc7SToomas Soome unique += ptr[i];
2665c65ebfc7SToomas Soome unique += sl6->sin6_port;
2666c65ebfc7SToomas Soome len = snprintf(buf+1, sizeof(buf) - 1, "t=%u H=%s D=%s l=%u r=%u", timeout, source_str, target_str, ntohs(sl6->sin6_port), ntohs(sr6->sin6_port));
2667c65ebfc7SToomas Soome }
2668*472cd20dSToomas Soome else
2669*472cd20dSToomas Soome {
2670*472cd20dSToomas Soome return kDNSServiceErr_BadParam;
2671*472cd20dSToomas Soome }
2672c65ebfc7SToomas Soome
2673c65ebfc7SToomas Soome if (len >= (sizeof(buf) - 1))
2674c65ebfc7SToomas Soome {
2675*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceSleepKeepalive could not fit local/remote info");
2676c65ebfc7SToomas Soome return kDNSServiceErr_Unknown;
2677c65ebfc7SToomas Soome }
2678c65ebfc7SToomas Soome // Include the NULL byte also in the first byte. The total length of the record includes the
2679c65ebfc7SToomas Soome // first byte also.
2680c65ebfc7SToomas Soome buf[0] = len + 1;
2681c65ebfc7SToomas Soome proxyreclen = len + 2;
2682c65ebfc7SToomas Soome
2683c65ebfc7SToomas Soome len = snprintf(name, sizeof(name), "%u", unique);
2684c65ebfc7SToomas Soome if (len >= sizeof(name))
2685c65ebfc7SToomas Soome {
2686*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceSleepKeepalive could not fit unique");
2687c65ebfc7SToomas Soome return kDNSServiceErr_Unknown;
2688c65ebfc7SToomas Soome }
2689c65ebfc7SToomas Soome
2690c65ebfc7SToomas Soome len = snprintf(recname, sizeof(recname), "%s.%s", name, "_keepalive._dns-sd._udp.local");
2691c65ebfc7SToomas Soome if (len >= sizeof(recname))
2692c65ebfc7SToomas Soome {
2693*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceSleepKeepalive could not fit name");
2694c65ebfc7SToomas Soome return kDNSServiceErr_Unknown;
2695c65ebfc7SToomas Soome }
2696c65ebfc7SToomas Soome
2697c65ebfc7SToomas Soome ka = malloc(sizeof(SleepKAContext));
2698c65ebfc7SToomas Soome if (!ka) return kDNSServiceErr_NoMemory;
26993b436d06SToomas Soome ka->AppCallback = (void *)callBack;
2700c65ebfc7SToomas Soome ka->AppContext = context;
2701c65ebfc7SToomas Soome
2702c65ebfc7SToomas Soome err = DNSServiceCreateConnection(sdRef);
2703c65ebfc7SToomas Soome if (err)
2704c65ebfc7SToomas Soome {
2705*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceSleepKeepalive cannot create connection");
2706c65ebfc7SToomas Soome free(ka);
2707c65ebfc7SToomas Soome return err;
2708c65ebfc7SToomas Soome }
2709c65ebfc7SToomas Soome
2710c65ebfc7SToomas Soome // we don't care about the "record". When sdRef gets deallocated later, it will be freed too
2711c65ebfc7SToomas Soome err = DNSServiceRegisterRecord(*sdRef, &record, kDNSServiceFlagsUnique, 0, recname,
2712c65ebfc7SToomas Soome kDNSServiceType_NULL, kDNSServiceClass_IN, proxyreclen, buf, kDNSServiceInterfaceIndexAny, SleepKeepaliveCallback, ka);
2713c65ebfc7SToomas Soome if (err)
2714c65ebfc7SToomas Soome {
2715*472cd20dSToomas Soome syslog(LOG_WARNING, "dnssd_clientstub DNSServiceSleepKeepalive cannot create connection");
2716c65ebfc7SToomas Soome free(ka);
2717c65ebfc7SToomas Soome return err;
2718c65ebfc7SToomas Soome }
2719c65ebfc7SToomas Soome (*sdRef)->kacontext = ka;
2720c65ebfc7SToomas Soome return kDNSServiceErr_NoError;
2721c65ebfc7SToomas Soome }
2722c65ebfc7SToomas Soome #endif
2723