dnssd_clientstub.c revision 5ffb0c9b03b5149ff4f5821a62be4a52408ada2a
/* -*- Mode: C; tab-width: 4 -*-
*
* Copyright (c) 2003-2004, Apple Computer, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <errno.h>
#include <stdlib.h>
#include <fcntl.h>
#include <TargetConditionals.h>
#endif
#include "dnssd_ipc.h"
#if defined(_WIN32)
#define _SSIZE_T
#include <CommonServices.h>
#include <DebugServices.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <stdarg.h>
#include <stdio.h>
#define sockaddr_mdns sockaddr_in
// Disable warning: "'type cast' : from data pointer 'void *' to function pointer"
extern BOOL IsSystemServiceDisabled();
static int g_initWinsock = 0;
#define LOG_WARNING kDebugLevelWarning
#define LOG_INFO kDebugLevelInfo
{
int len;
char * buffer;
(void) priority;
WSASetLastError( err );
}
#else
#include <syslog.h>
#define sockaddr_mdns sockaddr_un
#endif
// <rdar://problem/4096913> Specifies how many times we'll try and connect to the server.
#define DNSSD_CLIENT_MAXTRIES 4
// Uncomment the line below to use the old error return mechanism of creating a temporary named socket (e.g. in /var/tmp)
//#define USE_NAMED_ERROR_RETURN_SOCKET 1
// If the UDS client has not received a response from the daemon in 60 secs, it is unlikely to get one
// Note: Timeout of 3 secs should be sufficient in normal scenarios, but 60 secs is chosen as a safeguard since
// some clients may come up before mDNSResponder itself after a BOOT and on rare ocassions IOPM/Keychain/D2D calls
// in mDNSResponder's INIT may take a much longer time to return
#define DNSSD_CLIENT_TIMEOUT 60
#ifndef CTL_PATH_PREFIX
#define CTL_PATH_PREFIX "/var/tmp/dnssd_result_socket."
#endif
typedef struct
{
typedef struct _DNSServiceRef_t DNSServiceOp;
typedef struct _DNSRecordRef_t DNSRecord;
#if !defined(_WIN32)
typedef struct
{
void *AppCallback; // Client callback function and context
void *AppContext;
#endif
// client stub callback to process message from server and deliver results to client application
typedef void (*ProcessReplyFn)(DNSServiceOp *const sdr, const CallbackHeader *const cbh, const char *msg, const char *const end);
#define ValidatorBits 0x12345678
#define DNSServiceRefValid(X) (dnssd_SocketValid((X)->sockfd) && (((X)->sockfd ^ (X)->validator) == ValidatorBits))
// When using kDNSServiceFlagsShareConnection, there is one primary _DNSServiceOp_t, and zero or more subordinates
// For the primary, the 'next' field points to the first subordinate, and its 'next' field points to the next, and so on.
// For the primary, the 'primary' field is NULL; for subordinates the 'primary' field points back to the associated primary
//
// _DNS_SD_LIBDISPATCH is defined where libdispatch/GCD is available. This does not mean that the application will use the
// DNSServiceSetDispatchQueue API. Hence any new code guarded with _DNS_SD_LIBDISPATCH should still be backwards compatible.
struct _DNSServiceRef_t
{
client_context_t uid; // For shared connection requests, each subordinate DNSServiceRef has its own ID,
// unique within the scope of the same shared parent DNSServiceRef
int *moreptr; // Set while DNSServiceProcessResult working on this particular DNSServiceRef
void *AppCallback; // Client callback function and context
void *AppContext;
#endif
void *kacontext;
};
struct _DNSRecordRef_t
{
void *AppContext;
};
// Write len bytes. Return 0 on success, -1 on error
{
// Don't use "MSG_WAITALL"; it returns "Invalid argument" on some Linux versions; use an explicit while() loop instead.
//if (send(sd, buf, len, MSG_WAITALL) != len) return -1;
while (len)
{
{
// Should never happen. If it does, it indicates some OS bug,
// or that the mDNSResponder daemon crashed (which should never happen).
#if !defined(__ppc__) && defined(SO_ISDEFUNCT)
int defunct;
syslog(LOG_WARNING, "dnssd_clientstub write_all: SO_ISDEFUNCT failed %d %s", dnssd_errno, dnssd_strerror(dnssd_errno));
if (!defunct)
(long)num_written, (long)len,
(num_written < 0) ? dnssd_errno : 0,
else
#else
(long)num_written, (long)len,
(num_written < 0) ? dnssd_errno : 0,
#endif
return -1;
}
buf += num_written;
len -= num_written;
}
return 0;
}
// Read len bytes. Return 0 on success, read_all_fail on error, or read_all_wouldblock for
{
// Don't use "MSG_WAITALL"; it returns "Invalid argument" on some Linux versions; use an explicit while() loop instead.
//if (recv(sd, buf, len, MSG_WAITALL) != len) return -1;
while (len)
{
// It is valid to get an interrupted system call error e.g., somebody attaching
// in a debugger, retry without failing
{
continue;
}
{
int printWarn = 0;
int defunct = 0;
// Should never happen. If it does, it indicates some OS bug,
// or that the mDNSResponder daemon crashed (which should never happen).
#if defined(WIN32)
// <rdar://problem/7481776> Suppress logs for "A non-blocking socket operation
// could not be completed immediately"
if (WSAGetLastError() != WSAEWOULDBLOCK)
printWarn = 1;
#endif
#if !defined(__ppc__) && defined(SO_ISDEFUNCT)
{
syslog(LOG_WARNING, "dnssd_clientstub read_all: SO_ISDEFUNCT failed %d %s", dnssd_errno, dnssd_strerror(dnssd_errno));
}
if (!defunct)
printWarn = 1;
#endif
if (printWarn)
(num_read < 0) ? dnssd_errno : 0,
else if (defunct)
}
}
return read_all_success;
}
// Returns 1 if more bytes remain to be read on socket descriptor sd, 0 otherwise
{
int ret;
if (sd < FD_SETSIZE)
{
}
else
{
// Compute the number of integers needed for storing "sd". Internally fd_set is stored
// as an array of ints with one bit for each fd and hence we need to compute
// the number of ints needed rather than the number of bytes. If "sd" is 32, we need
// two ints and not just one.
int nfdbits = sizeof (int) * 8;
{
return 0;
}
}
return (ret > 0);
}
// set_waitlimit() implements a timeout using select. It is called from deliver_request() before recv() OR accept()
// to ensure the UDS clients are not blocked in these system calls indefinitely.
// Note: Ideally one should never be blocked here, because it indicates either mDNSResponder daemon is not yet up/hung/
// (primarily 3rd party firewall software) interfering with proper functioning of the TCP protocol stack it is possible
// the next operation on this socket(recv/accept) is blocked since we depend on TCP to communicate with the system service.
{
int gDaemonErr = kDNSServiceErr_NoError;
// To prevent stack corruption since select does not work with timeout if fds > FD_SETSIZE(1024)
{
{
// Ideally one should never hit this case: See comments before set_waitlimit()
syslog(LOG_WARNING, "dnssd_clientstub set_waitlimit:_daemon timed out (%d secs) without any response: Socket %d", timeout, sock);
}
}
return gDaemonErr;
}
/* create_hdr
*
* allocate and initialize an ipc message header. Value of len should initially be the
* length of the data, and is set to the value of the data plus the header. data_start
* is set to point to the beginning of the data section. SeparateReturnSocket should be
* non-zero for calls that can't receive an immediate error return value on their primary
* socket, and therefore require a separate return path for the error code result.
* if zero, the path to a control socket is appended at the beginning of the message buffer.
* data_start is set past this string.
*/
static ipc_msg_hdr *create_hdr(uint32_t op, size_t *len, char **data_start, int SeparateReturnSocket, DNSServiceOp *ref)
{
int datalen;
#if !defined(USE_TCP_LOOPBACK)
#endif
if (SeparateReturnSocket)
{
#if defined(USE_TCP_LOOPBACK)
#elif defined(USE_NAMED_ERROR_RETURN_SOCKET)
{ syslog(LOG_WARNING, "dnssd_clientstub create_hdr: gettimeofday failed %d %s", dnssd_errno, dnssd_strerror(dnssd_errno)); return NULL; }
#else
#endif
}
*len += sizeof(ipc_msg_hdr);
// Write message to buffer
#if defined(USE_TCP_LOOPBACK)
// Put dummy data in for the port, since we don't know what it is yet.
// The data will get filled in before we send the message. This happens in deliver_request().
#else
#endif
return hdr;
}
{
while (rec)
{
}
}
static void FreeDNSServiceOp(DNSServiceOp *x)
{
// We don't use our DNSServiceRefValid macro here because if we're cleaning up after a socket() call failed
// then sockfd could legitimately contain a failing value (e.g. dnssd_InvalidSocket)
syslog(LOG_WARNING, "dnssd_clientstub attempt to dispose invalid DNSServiceRef %p %08X %08X", x, x->sockfd, x->validator);
else
{
x->sockfd = dnssd_InvalidSocket;
x->validator = 0xDDDDDDDD;
x->op = request_op_none;
x->max_index = 0;
x->logcounter = 0;
x->ProcessReply = NULL;
x->AppCallback = NULL;
x->AppContext = NULL;
x->disp_source = NULL;
x->disp_queue = NULL;
#endif
// DNSRecords may have been added to subordinate sdRef e.g., DNSServiceRegister/DNSServiceAddRecord
// or on the main sdRef e.g., DNSServiceCreateConnection/DNSServiveRegisterRecord. DNSRecords may have
// been freed if the application called DNSRemoveRecord
FreeDNSRecords(x);
if (x->kacontext)
{
}
free(x);
}
}
// Return a connected service ref (deallocate with DNSServiceRefDeallocate)
static DNSServiceErrorType ConnectToServer(DNSServiceRef *ref, DNSServiceFlags flags, uint32_t op, ProcessReplyFn ProcessReply, void *AppCallback, void *AppContext)
{
int NumTries = 0;
if (!ref)
{
return kDNSServiceErr_BadParam;
}
{
if (!*ref)
{
syslog(LOG_WARNING, "dnssd_clientstub kDNSServiceFlagsShareConnection used with NULL DNSServiceRef");
return kDNSServiceErr_BadParam;
}
if (!DNSServiceRefValid(*ref) || ((*ref)->op != connection_request && (*ref)->op != connection_delegate_request) || (*ref)->primary)
{
syslog(LOG_WARNING, "dnssd_clientstub kDNSServiceFlagsShareConnection used with invalid DNSServiceRef %p %08X %08X op %d",
return kDNSServiceErr_BadReference;
}
}
#if defined(_WIN32)
if (!g_initWinsock)
{
g_initWinsock = 1;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) { *ref = NULL; return kDNSServiceErr_ServiceNotRunning; }
}
// <rdar://problem/4096913> If the system service is disabled, we only want to try to connect once
if (IsSystemServiceDisabled())
#endif
if (!sdr)
{
return kDNSServiceErr_NoMemory;
}
sdr->logcounter = 0;
#endif
{
while (*p)
p = &(*p)->next;
*p = sdr;
// Preincrement counter before we use it -- it helps with debugging if we know the all-zeroes ID should never appear
//printf("ConnectToServer sharing socket %d\n", sdr->sockfd);
}
else
{
#ifdef SO_NOSIGPIPE
const unsigned long optval = 1;
#endif
#ifndef USE_TCP_LOOPBACK
if (uds_serverpath == NULL)
#endif
{
syslog(LOG_WARNING, "dnssd_clientstub ConnectToServer: socket failed %d %s", dnssd_errno, dnssd_strerror(dnssd_errno));
return kDNSServiceErr_NoMemory;
}
#ifdef SO_NOSIGPIPE
// Some environments (e.g. OS X) support turning off SIGPIPE for a socket
syslog(LOG_WARNING, "dnssd_clientstub ConnectToServer: SO_NOSIGPIPE failed %d %s", dnssd_errno, dnssd_strerror(dnssd_errno));
#endif
#if defined(USE_TCP_LOOPBACK)
#else
#if !defined(__ppc__) && defined(SO_DEFUNCTOK)
{
int defunct = 1;
syslog(LOG_WARNING, "dnssd_clientstub ConnectToServer: SO_DEFUNCTOK failed %d %s", dnssd_errno, dnssd_strerror(dnssd_errno));
}
#endif
#endif
while (1)
{
if (!err)
break; // If we succeeded, return sdr
// If we failed, then it may be because the daemon is still launching.
// This can happen for processes that launch early in the boot process, while the
// daemon is still coming up. Rather than fail here, we wait 1 sec and try again.
// If, after DNSSD_CLIENT_MAXTRIES, we still can't connect to the daemon,
// then we give up and return a failure code.
if (++NumTries < DNSSD_CLIENT_MAXTRIES)
{
}
else
{
syslog(LOG_WARNING, "dnssd_clientstub ConnectToServer: connect() failed path:%s Socket:%d Err:%d Errno:%d %s",
return kDNSServiceErr_ServiceNotRunning;
}
}
//printf("ConnectToServer opened socket %d\n", sdr->sockfd);
}
return kDNSServiceErr_NoError;
}
#define deliver_request_bailout(MSG) \
do { syslog(LOG_WARNING, "dnssd_clientstub deliver_request: %s failed %d (%s)", (MSG), dnssd_errno, dnssd_strerror(dnssd_errno)); goto cleanup; } while(0)
{
uint32_t datalen = hdr->datalen; // We take a copy here because we're going to convert hdr->datalen to network byte order
#if defined(USE_TCP_LOOPBACK) || defined(USE_NAMED_ERROR_RETURN_SOCKET)
#endif
int MakeSeparateReturnSocket = 0;
// Note: need to check hdr->op, not sdr->op.
// hdr->op contains the code for the specific operation we're currently doing, whereas sdr->op
// contains the original parent DNSServiceOp (e.g. for an add_record_request, hdr->op will be
// add_record_request but the parent sdr->op will be connection_request or reg_service_request)
hdr->op == reg_record_request || hdr->op == add_record_request || hdr->op == update_record_request || hdr->op == remove_record_request)
if (!DNSServiceRefValid(sdr))
{
if (hdr)
syslog(LOG_WARNING, "dnssd_clientstub deliver_request: invalid DNSServiceRef %p %08X %08X", sdr, sdr->sockfd, sdr->validator);
return kDNSServiceErr_BadReference;
}
if (!hdr)
{
return kDNSServiceErr_Unknown;
}
{
#if defined(USE_TCP_LOOPBACK)
{
if (bind(listenfd, (struct sockaddr*) &caddr, sizeof(caddr)) < 0) deliver_request_bailout("TCP bind");
if (getsockname(listenfd, (struct sockaddr*) &caddr, &len) < 0) deliver_request_bailout("TCP getsockname");
}
#elif defined(USE_NAMED_ERROR_RETURN_SOCKET)
{
int bindresult;
// According to Stevens (section 3.2), there is no portable way to
// determine whether sa_len is defined on a particular platform.
#ifndef NOT_HAVE_SA_LEN
#endif
}
#else
{
else
{
#if !defined(__ppc__) && defined(SO_DEFUNCTOK)
{
int defunct = 1;
syslog(LOG_WARNING, "dnssd_clientstub ConnectToServer: SO_DEFUNCTOK failed %d %s", dnssd_errno, dnssd_strerror(dnssd_errno));
}
#endif
}
}
#endif
}
#if !defined(USE_TCP_LOOPBACK) && !defined(USE_NAMED_ERROR_RETURN_SOCKET)
// If we're going to make a separate error return socket, and pass it to the daemon
// using sendmsg, then we'll hold back one data byte to go with it.
// On some versions of Unix (including Leopard) sending a control message without
// any associated data does not work reliably -- e.g. one particular issue we ran
// into is that if the receiving program is in a kqueue loop waiting to be notified
// of the received message, it doesn't get woken up when the control message arrives.
datalen--; // Okay to use sdr->op when checking for op == send_bpf
#endif
// At this point, our listening socket is set up and waiting, if necessary, for the daemon to connect back to
//syslog(LOG_WARNING, "dnssd_clientstub deliver_request writing %lu bytes", (unsigned long)(datalen + sizeof(ipc_msg_hdr)));
//if (MakeSeparateReturnSocket) syslog(LOG_WARNING, "dnssd_clientstub deliver_request name is %s", data);
unsigned int i;
for (i=0; i<datalen + sizeof(ipc_msg_hdr); i++)
{
usleep(10000);
}
#else
{
// write_all already prints an error message if there is an error writing to
// the socket except for DEFUNCT. Logging here is unnecessary and also wrong
// in the case of DEFUNCT sockets
goto cleanup;
}
#endif
if (!MakeSeparateReturnSocket)
if (MakeSeparateReturnSocket || sdr->op == send_bpf) // Okay to use sdr->op when checking for op == send_bpf
{
#if defined(USE_TCP_LOOPBACK) || defined(USE_NAMED_ERROR_RETURN_SOCKET)
// At this point we may wait in accept for a few milliseconds waiting for the daemon to connect back to us,
// but that's okay -- the daemon should not take more than a few milliseconds to respond.
// set_waitlimit() ensures we do not block indefinitely just in case something is wrong
goto cleanup;
if (!dnssd_SocketValid(errsd))
deliver_request_bailout("accept");
#else
struct iovec vec = { ((char *)hdr) + sizeof(ipc_msg_hdr) + datalen, 1 }; // Send the last byte along with the SCM_RIGHTS
msg.msg_namelen = 0;
if (MakeSeparateReturnSocket || sdr->op == send_bpf) // Okay to use sdr->op when checking for op == send_bpf
{
{
int i;
for (i=0; i<100; i++)
{
//if (dnssd_SocketValid(listenfd)) syslog(LOG_WARNING, "Sending fd %d for %s", listenfd, p);
}
}
}
sleep(1);
#endif
sizeof(struct cmsghdr) + sizeof(dnssd_sock_t),
#endif // DEBUG_64BIT_SCM_RIGHTS
{
syslog(LOG_WARNING, "dnssd_clientstub deliver_request ERROR: sendmsg failed read sd=%d write sd=%d errno %d (%s)",
goto cleanup;
}
#endif // DEBUG_64BIT_SCM_RIGHTS
#endif
// Close our end of the socketpair *before* calling read_all() to get the four-byte error code.
// Otherwise, if the daemon closes our socket (or crashes), we will have to wait for a timeout
// in read_all() because the socket is not closed (we still have an open reference to it)
// Note: listenfd is overwritten in the case of send_bpf above and that will be closed here
// for send_bpf operation.
listenfd = dnssd_InvalidSocket; // Make sure we don't close it a second time in the cleanup handling below
}
// At this point we may wait in read_all for a few milliseconds waiting for the daemon to send us the error code,
// but that's okay -- the daemon should not take more than a few milliseconds to respond.
// set_waitlimit() ensures we do not block indefinitely just in case something is wrong
{
err = kDNSServiceErr_ServiceNotRunning; // On failure read_all will have written a message to syslog for us
else
}
//syslog(LOG_WARNING, "dnssd_clientstub deliver_request: retrieved error code %d", err);
{
#if defined(USE_NAMED_ERROR_RETURN_SOCKET)
// syslog(LOG_WARNING, "dnssd_clientstub deliver_request: removing UDS: %s", data);
syslog(LOG_WARNING, "dnssd_clientstub WARNING: unlink(\"%s\") failed errno %d (%s)", data, dnssd_errno, dnssd_strerror(dnssd_errno));
// else syslog(LOG_WARNING, "dnssd_clientstub deliver_request: removed UDS: %s", data);
#endif
}
return err;
}
{
if (!sdRef) { syslog(LOG_WARNING, "dnssd_clientstub DNSServiceRefSockFD called with NULL DNSServiceRef"); return dnssd_InvalidSocket; }
if (!DNSServiceRefValid(sdRef))
{
syslog(LOG_WARNING, "dnssd_clientstub DNSServiceRefSockFD called with invalid DNSServiceRef %p %08X %08X",
return dnssd_InvalidSocket;
}
{
syslog(LOG_WARNING, "dnssd_clientstub DNSServiceRefSockFD undefined for kDNSServiceFlagsShareConnection subordinate DNSServiceRef %p", sdRef);
return dnssd_InvalidSocket;
}
}
{
int morebytes;
while (sdr)
{
// We can't touch the sdr after the callback as it can be deallocated in the callback
morebytes = 1;
{
case resolve_request:
if (sdr->AppCallback) ((DNSServiceResolveReply) sdr->AppCallback)(sdr, 0, 0, error, NULL, 0, 0, 0, NULL, sdr->AppContext);
break;
case query_request:
if (sdr->AppCallback) ((DNSServiceQueryRecordReply)sdr->AppCallback)(sdr, 0, 0, error, NULL, 0, 0, 0, NULL, 0, sdr->AppContext);
break;
case addrinfo_request:
if (sdr->AppCallback) ((DNSServiceGetAddrInfoReply)sdr->AppCallback)(sdr, 0, 0, error, NULL, NULL, 0, sdr->AppContext);
break;
case browse_request:
if (sdr->AppCallback) ((DNSServiceBrowseReply) sdr->AppCallback)(sdr, 0, 0, error, NULL, 0, NULL, sdr->AppContext);
break;
case reg_service_request:
if (sdr->AppCallback) ((DNSServiceRegisterReply) sdr->AppCallback)(sdr, 0, error, NULL, 0, NULL, sdr->AppContext);
break;
case enumeration_request:
if (sdr->AppCallback) ((DNSServiceDomainEnumReply) sdr->AppCallback)(sdr, 0, 0, error, NULL, sdr->AppContext);
break;
case connection_request:
// This means Register Record, walk the list of DNSRecords to do the callback
while (rec)
{
if (rec->AppCallback) ((DNSServiceRegisterRecordReply)rec->AppCallback)(sdr, 0, 0, error, rec->AppContext);
// The Callback can call DNSServiceRefDeallocate which in turn frees sdr and all the records.
// Detect that and return early
if (!morebytes) {syslog(LOG_WARNING, "dnssdclientstub:Record: CallbackwithError morebytes zero"); return;}
}
break;
case port_mapping_request:
if (sdr->AppCallback) ((DNSServiceNATPortMappingReply)sdr->AppCallback)(sdr, 0, 0, error, 0, 0, 0, 0, 0, sdr->AppContext);
break;
default:
}
// If DNSServiceRefDeallocate was called in the callback, morebytes will be zero. As the sdRef
// (and its subordinates) have been freed, we should not proceed further. Note that when we
// call the callback with a subordinate sdRef the application can call DNSServiceRefDeallocate
// on the main sdRef and DNSServiceRefDeallocate handles this case by walking all the sdRefs and
// clears the moreptr so that we can terminate here.
//
// If DNSServiceRefDeallocate was not called in the callback, then set moreptr to NULL so that
// we don't access the stack variable after we return from this function.
if (!morebytes) {syslog(LOG_WARNING, "dnssdclientstub:sdRef: CallbackwithError morebytes zero sdr %p", sdr); return;}
}
}
#endif // _DNS_SD_LIBDISPATCH
// Handle reply from server, calling application client callback. If there is no reply
// from the daemon on the socket contained in sdRef, the call will block.
{
int morebytes = 0;
if (!sdRef) { syslog(LOG_WARNING, "dnssd_clientstub DNSServiceProcessResult called with NULL DNSServiceRef"); return kDNSServiceErr_BadParam; }
if (!DNSServiceRefValid(sdRef))
{
syslog(LOG_WARNING, "dnssd_clientstub DNSServiceProcessResult called with invalid DNSServiceRef %p %08X %08X", sdRef, sdRef->sockfd, sdRef->validator);
return kDNSServiceErr_BadReference;
}
{
syslog(LOG_WARNING, "dnssd_clientstub DNSServiceProcessResult undefined for kDNSServiceFlagsShareConnection subordinate DNSServiceRef %p", sdRef);
return kDNSServiceErr_BadReference;
}
if (!sdRef->ProcessReply)
{
static int num_logs = 0;
if (num_logs < 10) syslog(LOG_WARNING, "dnssd_clientstub DNSServiceProcessResult called with DNSServiceRef with no ProcessReply function");
return kDNSServiceErr_BadReference;
}
do
{
char *data;
// return NoError on EWOULDBLOCK. This will handle the case
// where a non-blocking socket is told there is data, but it was a false positive.
// On error, read_all will write a message to syslog for us, so don't need to duplicate that here
// Note: If we want to properly support using non-blocking sockets in the future
if (result == read_all_fail)
{
// Set the ProcessReply to NULL before callback as the sdRef can get deallocated
// in the callback.
// Call the callbacks with an error if using the dispatch API, as DNSServiceProcessResult
// is not called by the application and hence need to communicate the error. Cancel the
// source so that we don't get any more events
// Note: read_all fails if we could not read from the daemon which can happen if the
// daemon dies or the file descriptor is disconnected (defunct).
if (sdRef->disp_source)
{
}
#endif
// Don't touch sdRef anymore as it might have been deallocated
return kDNSServiceErr_ServiceNotRunning;
}
else if (result == read_all_wouldblock)
{
{
sdRef->logcounter++;
syslog(LOG_WARNING, "dnssd_clientstub DNSServiceProcessResult error: select indicated data was waiting but read_all returned EWOULDBLOCK");
}
return kDNSServiceErr_NoError;
}
{
syslog(LOG_WARNING, "dnssd_clientstub DNSServiceProcessResult daemon version %d does not match client version %d", cbh.ipc_hdr.version, VERSION);
return kDNSServiceErr_Incompatible;
}
if (!data) return kDNSServiceErr_NoMemory;
if (read_all(sdRef->sockfd, data, cbh.ipc_hdr.datalen) < 0) // On error, read_all will write a message to syslog for us
{
// Set the ProcessReply to NULL before callback as the sdRef can get deallocated
// in the callback.
// Call the callbacks with an error if using the dispatch API, as DNSServiceProcessResult
// is not called by the application and hence need to communicate the error. Cancel the
// source so that we don't get any more events
if (sdRef->disp_source)
{
}
#endif
// Don't touch sdRef anymore as it might have been deallocated
return kDNSServiceErr_ServiceNotRunning;
}
else
{
// CAUTION: We have to handle the case where the client calls DNSServiceRefDeallocate from within the callback function.
// To do this we set moreptr to point to morebytes. If the client does call DNSServiceRefDeallocate(),
// then that routine will clear morebytes for us, and cause us to exit our loop.
if (morebytes)
{
}
// Careful code here:
// If morebytes is non-zero, that means we set sdRef->moreptr above, and the operation was not
// cancelled out from under us, so now we need to clear sdRef->moreptr so we don't leave a stray
// dangling pointer pointing to a long-gone stack variable.
// If morebytes is zero, then one of two thing happened:
// (a) morebytes was 0 above, so we didn't set sdRef->moreptr, so we don't need to clear it
// (b) morebytes was 1 above, and we set sdRef->moreptr, but the operation was cancelled (with DNSServiceRefDeallocate()),
// so we MUST NOT try to dereference our stale sdRef pointer.
}
} while (morebytes);
return kDNSServiceErr_NoError;
}
{
if (!sdRef) { syslog(LOG_WARNING, "dnssd_clientstub DNSServiceRefDeallocate called with NULL DNSServiceRef"); return; }
{
syslog(LOG_WARNING, "dnssd_clientstub DNSServiceRefDeallocate called with invalid DNSServiceRef %p %08X %08X", sdRef, sdRef->sockfd, sdRef->validator);
return;
}
// 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
{
if (*p)
{
char *ptr;
if (hdr)
{
}
}
}
else // else, make sure to terminate all subordinates as well
{
// The cancel handler will close the fd if a dispatch source has been set
if (sdRef->disp_source)
{
// By setting the ProcessReply to NULL, we make sure that we never call
// the application callbacks ever, after returning from this function. We
// assume that DNSServiceRefDeallocate is called from the serial queue
// that was passed to DNSServiceSetDispatchQueue. Hence, dispatch_source_cancel
// should cancel all the blocks on the queue and hence there should be no more
// callbacks when we return from this function. Setting ProcessReply to NULL
// provides extra protection.
}
// if disp_queue is set, it means it used the DNSServiceSetDispatchQueue API. In that case,
// when the source was cancelled, the fd was closed in the handler. Currently the source
// is cancelled only when the mDNSResponder daemon dies
#else
#endif
// Free DNSRecords added in DNSRegisterRecord if they have not
// been freed in DNSRemoveRecord
while (sdRef)
{
DNSServiceOp *p = sdRef;
// When there is an error reading from the daemon e.g., bad fd, CallbackWithError
// is called which sets moreptr. It might set the moreptr on a subordinate sdRef
// but the application might call DNSServiceRefDeallocate with the main sdRef from
// the callback. Hence, when we loop through the subordinate sdRefs, we need
// to clear the moreptr so that CallbackWithError can terminate itself instead of
// walking through the freed sdRefs.
FreeDNSServiceOp(p);
}
}
}
DNSServiceErrorType DNSSD_API DNSServiceGetProperty(const char *property, void *result, uint32_t *size)
{
char *ptr;
// Swap version result back to local process byte order
*size = actualsize;
return kDNSServiceErr_NoError;
}
{
char *ptr;
if (err)
return err;
if (!hdr)
{
return kDNSServiceErr_NoMemory;
}
{
return kDNSServiceErr_ServiceNotRunning;
}
return kDNSServiceErr_NoError;
}
static void handle_resolve_response(DNSServiceOp *const sdr, const CallbackHeader *const cbh, const char *data, const char *end)
{
char fullname[kDNSServiceMaxDomainName];
char target[kDNSServiceMaxDomainName];
unsigned char *txtrecord;
((DNSServiceResolveReply)sdr->AppCallback)(sdr, cbh->cb_flags, cbh->cb_interface, cbh->cb_err, fullname, target, port.s, txtlen, txtrecord, sdr->AppContext);
return;
// MUST NOT touch sdr after invoking AppCallback -- client is allowed to dispose it from within callback function
fail:
}
static int32_t libSystemVersion = 0;
// Return true if the iOS application linked against a version of libsystem where P2P
// interfaces were included by default when using kDNSServiceInterfaceIndexAny.
// Using 160.0.0 == 0xa00000 as the version threshold.
static int includeP2PWithIndexAny()
{
if (libSystemVersion == 0)
if (libSystemVersion < 0xa00000)
return 1;
else
return 0;
}
#else // TARGET_OS_EMBEDDED
// always return false for non iOS platforms
static int includeP2PWithIndexAny()
{
return 0;
}
#endif // TARGET_OS_EMBEDDED
(
const char *name,
const char *regtype,
const char *domain,
void *context
)
{
char *ptr;
// Need a real InterfaceID for WakeOnResolve
if ((flags & kDNSServiceFlagsWakeOnResolve) != 0 &&
{
return kDNSServiceErr_BadParam;
}
err = ConnectToServer(sdRef, flags, resolve_request, handle_resolve_response, (void *)callBack, context);
// Calculate total message length
len += sizeof(interfaceIndex);
return err;
}
static void handle_query_response(DNSServiceOp *const sdr, const CallbackHeader *const cbh, const char *data, const char *const end)
{
char name[kDNSServiceMaxDomainName];
const char *rdata;
if (!data) syslog(LOG_WARNING, "dnssd_clientstub handle_query_response: error reading result from daemon");
else ((DNSServiceQueryRecordReply)sdr->AppCallback)(sdr, cbh->cb_flags, cbh->cb_interface, cbh->cb_err, name, rrtype, rrclass, rdlen, rdata, ttl, sdr->AppContext);
// MUST NOT touch sdr after invoking AppCallback -- client is allowed to dispose it from within callback function
}
(
const char *name,
void *context
)
{
char *ptr;
err = ConnectToServer(sdRef, flags, query_request, handle_query_response, (void *)callBack, context);
// Calculate total message length
return err;
}
static void handle_addrinfo_response(DNSServiceOp *const sdr, const CallbackHeader *const cbh, const char *data, const char *const end)
{
char hostname[kDNSServiceMaxDomainName];
const char *rdata;
// We only generate client callbacks for A and AAAA results (including NXDOMAIN results for
// those types, if the client has requested those with the kDNSServiceFlagsReturnIntermediates).
// Other result types, specifically CNAME referrals, are not communicated to the client, because
// the DNSServiceGetAddrInfoReply interface doesn't have any meaningful way to communiate CNAME referrals.
if (!data) syslog(LOG_WARNING, "dnssd_clientstub handle_addrinfo_response: error reading result from daemon");
{
struct sockaddr_in sa4;
struct sockaddr_in6 sa6;
const struct sockaddr *const sa = (rrtype == kDNSServiceType_A) ? (struct sockaddr*)&sa4 : (struct sockaddr*)&sa6;
if (rrtype == kDNSServiceType_A)
{
#ifndef NOT_HAVE_SA_LEN
#endif
// sin_port = 0;
}
else
{
#ifndef NOT_HAVE_SA_LEN
#endif
// sin6_port = 0;
// sin6_flowinfo = 0;
// sin6_scope_id = 0;
{
}
}
// Validation results are always delivered separately from the actual results of the
// DNSServiceGetAddrInfo. Set the "addr" to NULL as per the documentation.
//
// Note: If we deliver validation results along with the "addr" in the future, we need
// a way to differentiate the negative response from validation-only response as both
// has zero address.
((DNSServiceGetAddrInfoReply)sdr->AppCallback)(sdr, cbh->cb_flags, cbh->cb_interface, cbh->cb_err, hostname, sa, ttl, sdr->AppContext);
else
((DNSServiceGetAddrInfoReply)sdr->AppCallback)(sdr, cbh->cb_flags, cbh->cb_interface, cbh->cb_err, hostname, NULL, 0, sdr->AppContext);
// MUST NOT touch sdr after invoking AppCallback -- client is allowed to dispose it from within callback function
}
}
(
const char *hostname,
void *context /* may be NULL */
)
{
char *ptr;
if (!hostname) return kDNSServiceErr_BadParam;
err = ConnectToServer(sdRef, flags, addrinfo_request, handle_addrinfo_response, (void *)callBack, context);
if (err)
{
return err; // On error ConnectToServer leaves *sdRef set to NULL
}
// Calculate total message length
return err;
}
static void handle_browse_response(DNSServiceOp *const sdr, const CallbackHeader *const cbh, const char *data, const char *const end)
{
if (!data) syslog(LOG_WARNING, "dnssd_clientstub handle_browse_response: error reading result from daemon");
else ((DNSServiceBrowseReply)sdr->AppCallback)(sdr, cbh->cb_flags, cbh->cb_interface, cbh->cb_err, replyName, replyType, replyDomain, sdr->AppContext);
// MUST NOT touch sdr after invoking AppCallback -- client is allowed to dispose it from within callback function
}
(
const char *regtype,
const char *domain,
void *context
)
{
char *ptr;
err = ConnectToServer(sdRef, flags, browse_request, handle_browse_response, (void *)callBack, context);
len += sizeof(interfaceIndex);
return err;
}
DNSServiceErrorType DNSSD_API DNSServiceSetDefaultDomainForUser(DNSServiceFlags flags, const char *domain);
DNSServiceErrorType DNSSD_API DNSServiceSetDefaultDomainForUser(DNSServiceFlags flags, const char *domain)
{
char *ptr;
return err;
}
static void handle_regservice_response(DNSServiceOp *const sdr, const CallbackHeader *const cbh, const char *data, const char *const end)
{
if (!data) syslog(LOG_WARNING, "dnssd_clientstub handle_regservice_response: error reading result from daemon");
else ((DNSServiceRegisterReply)sdr->AppCallback)(sdr, cbh->cb_flags, cbh->cb_err, name, regtype, domain, sdr->AppContext);
// MUST NOT touch sdr after invoking AppCallback -- client is allowed to dispose it from within callback function
}
(
const char *name,
const char *regtype,
const char *domain,
const char *host,
const void *txtRecord,
void *context
)
{
char *ptr;
if (!regtype) return kDNSServiceErr_BadParam;
// No callback must have auto-rename
err = ConnectToServer(sdRef, flags, reg_service_request, callBack ? handle_regservice_response : NULL, (void *)callBack, context);
len = sizeof(DNSServiceFlags);
// If it is going over a shared connection, then don't set the IPC_FLAGS_NOREPLY
// as it affects all the operations over the shared connection. This is not
// a normal case and hence receiving the response back from the daemon and
// discarding it in ConnectionResponse is okay.
return err;
}
static void handle_enumeration_response(DNSServiceOp *const sdr, const CallbackHeader *const cbh, const char *data, const char *const end)
{
char domain[kDNSServiceMaxDomainName];
if (!data) syslog(LOG_WARNING, "dnssd_clientstub handle_enumeration_response: error reading result from daemon");
else ((DNSServiceDomainEnumReply)sdr->AppCallback)(sdr, cbh->cb_flags, cbh->cb_interface, cbh->cb_err, domain, sdr->AppContext);
// MUST NOT touch sdr after invoking AppCallback -- client is allowed to dispose it from within callback function
}
(
void *context
)
{
char *ptr;
err = ConnectToServer(sdRef, flags, enumeration_request, handle_enumeration_response, (void *)callBack, context);
len = sizeof(DNSServiceFlags);
return err;
}
static void ConnectionResponse(DNSServiceOp *const sdr, const CallbackHeader *const cbh, const char *const data, const char *const end)
{
(void)data; // Unused
//printf("ConnectionResponse got %d\n", cbh->ipc_hdr.op);
{
// When using kDNSServiceFlagsShareConnection, need to search the list of associated DNSServiceOps
// to find the one this response is intended for, and then call through to its ProcessReply handler.
// We start with our first subordinate DNSServiceRef -- don't want to accidentally match the parent DNSServiceRef.
while (op && (op->uid.u32[0] != cbh->ipc_hdr.client_context.u32[0] || op->uid.u32[1] != cbh->ipc_hdr.client_context.u32[1]))
// Note: We may sometimes not find a matching DNSServiceOp, in the case where the client has
// cancelled the subordinate DNSServiceOp, but there are still messages in the pipeline from the daemon
// WARNING: Don't touch op or sdr after this -- client may have called DNSServiceRefDeallocate
return;
}
else
{
{
if (rec->uid.u32[0] == cbh->ipc_hdr.client_context.u32[0] && rec->uid.u32[1] == cbh->ipc_hdr.client_context.u32[1])
break;
}
// The record might have been freed already and hence not an
// error if the record is not found.
if (!rec)
{
return;
}
{
return;
}
{
}
else
{
}
// MUST NOT touch sdr after invoking AppCallback -- client is allowed to dispose it from within callback function
}
}
{
char *ptr;
DNSServiceErrorType err = ConnectToServer(sdRef, 0, connection_request, ConnectionResponse, NULL, NULL);
return err;
}
DNSServiceErrorType DNSSD_API DNSServiceCreateDelegateConnection(DNSServiceRef *sdRef, int32_t pid, uuid_t uuid)
{
char *ptr;
DNSServiceErrorType err = ConnectToServer(sdRef, 0, connection_delegate_request, ConnectionResponse, NULL, NULL);
if (err)
{
return err; // On error ConnectToServer leaves *sdRef set to NULL
}
// Only one of the two options can be set. If pid is zero, uuid is used.
// If both are specified only pid will be used. We send across the pid
// so that the daemon knows what to read from the socket.
if (!hdr)
{
return kDNSServiceErr_NoMemory;
}
{
syslog(LOG_WARNING, "dnssdclientstub: Could not setsockopt() for PID[%d], no entitlements or process(pid) invalid errno:%d (%s)", pid, errno, strerror(errno));
// Free the hdr in case we return before calling deliver_request()
if (hdr)
return kDNSServiceErr_NoAuth;
}
if (!pid && setsockopt((*sdRef)->sockfd, SOL_SOCKET, SO_DELEGATED_UUID, uuid, sizeof(uuid_t)) == -1)
{
syslog(LOG_WARNING, "dnssdclientstub: Could not setsockopt() for UUID, no entitlements or process(uuid) invalid errno:%d (%s) ", errno, strerror(errno));
// Free the hdr in case we return before calling deliver_request()
if (hdr)
return kDNSServiceErr_NoAuth;
}
if (err)
{
}
return err;
}
DNSServiceErrorType DNSSD_API DNSServiceCreateDelegateConnection(DNSServiceRef *sdRef, int32_t pid, uuid_t uuid)
{
(void) pid;
(void) uuid;
return DNSServiceCreateConnection(sdRef);
}
#endif
(
const char *fullname,
const void *rdata,
void *context
)
{
char *ptr;
DNSRecord **p;
if (!sdRef) { syslog(LOG_WARNING, "dnssd_clientstub DNSServiceRegisterRecord called with NULL DNSServiceRef"); return kDNSServiceErr_BadParam; }
if (!DNSServiceRefValid(sdRef))
{
syslog(LOG_WARNING, "dnssd_clientstub DNSServiceRegisterRecord called with invalid DNSServiceRef %p %08X %08X", sdRef, sdRef->sockfd, sdRef->validator);
return kDNSServiceErr_BadReference;
}
{
syslog(LOG_WARNING, "dnssd_clientstub DNSServiceRegisterRecord called with non-DNSServiceCreateConnection DNSServiceRef %p %d", sdRef, sdRef->op);
return kDNSServiceErr_BadReference;
}
len = sizeof(DNSServiceFlags);
// Bump up the uid. Normally for shared operations (kDNSServiceFlagsShareConnection), this
// is done in ConnectToServer. For DNSServiceRegisterRecord, ConnectToServer has already
// been called. As multiple DNSServiceRegisterRecords can be multiplexed over a single
// connection, we need a way to demultiplex the response so that the callback corresponding
// to the right DNSServiceRegisterRecord instance can be called. Use the same mechanism that
// is used by kDNSServiceFlagsShareConnection. create_hdr copies the uid value to ipc
// hdr->client_context which will be returned in the ipc response.
if (!hdr) return kDNSServiceErr_NoMemory;
// Remember the uid that we are sending across so that we can match
// when the response comes back.
while (*p) p = &(*p)->recnext;
*p = rref;
}
// sdRef returned by DNSServiceRegister()
(
const void *rdata,
)
{
char *ptr;
DNSRecord **p;
if (!sdRef) { syslog(LOG_WARNING, "dnssd_clientstub DNSServiceAddRecord called with NULL DNSServiceRef"); return kDNSServiceErr_BadParam; }
if (!RecordRef) { syslog(LOG_WARNING, "dnssd_clientstub DNSServiceAddRecord called with NULL DNSRecordRef pointer"); return kDNSServiceErr_BadParam; }
{
syslog(LOG_WARNING, "dnssd_clientstub DNSServiceAddRecord called with non-DNSServiceRegister DNSServiceRef %p %d", sdRef, sdRef->op);
return kDNSServiceErr_BadReference;
}
if (!DNSServiceRefValid(sdRef))
{
syslog(LOG_WARNING, "dnssd_clientstub DNSServiceAddRecord called with invalid DNSServiceRef %p %08X %08X", sdRef, sdRef->sockfd, sdRef->validator);
return kDNSServiceErr_BadReference;
}
len += sizeof(DNSServiceFlags);
if (!hdr) return kDNSServiceErr_NoMemory;
while (*p) p = &(*p)->recnext;
*p = rref;
}
// DNSRecordRef returned by DNSServiceRegisterRecord or DNSServiceAddRecord
(
const void *rdata,
)
{
char *ptr;
if (!sdRef) { syslog(LOG_WARNING, "dnssd_clientstub DNSServiceUpdateRecord called with NULL DNSServiceRef"); return kDNSServiceErr_BadParam; }
if (!DNSServiceRefValid(sdRef))
{
syslog(LOG_WARNING, "dnssd_clientstub DNSServiceUpdateRecord called with invalid DNSServiceRef %p %08X %08X", sdRef, sdRef->sockfd, sdRef->validator);
return kDNSServiceErr_BadReference;
}
// Note: RecordRef is allowed to be NULL
len += sizeof(DNSServiceFlags);
if (!hdr) return kDNSServiceErr_NoMemory;
}
(
)
{
char *ptr;
if (!sdRef) { syslog(LOG_WARNING, "dnssd_clientstub DNSServiceRemoveRecord called with NULL DNSServiceRef"); return kDNSServiceErr_BadParam; }
if (!RecordRef) { syslog(LOG_WARNING, "dnssd_clientstub DNSServiceRemoveRecord called with NULL DNSRecordRef"); return kDNSServiceErr_BadParam; }
if (!sdRef->max_index) { syslog(LOG_WARNING, "dnssd_clientstub DNSServiceRemoveRecord called with bad DNSServiceRef"); return kDNSServiceErr_BadReference; }
if (!DNSServiceRefValid(sdRef))
{
syslog(LOG_WARNING, "dnssd_clientstub DNSServiceRemoveRecord called with invalid DNSServiceRef %p %08X %08X", sdRef, sdRef->sockfd, sdRef->validator);
return kDNSServiceErr_BadReference;
}
if (!hdr) return kDNSServiceErr_NoMemory;
if (!err)
{
// This RecordRef could have been allocated in DNSServiceRegisterRecord or DNSServiceAddRecord.
// If so, delink from the list before freeing
}
return err;
}
(
const char *fullname,
const void *rdata
)
{
char *ptr;
len = sizeof(DNSServiceFlags);
return err;
}
static void handle_port_mapping_response(DNSServiceOp *const sdr, const CallbackHeader *const cbh, const char *data, const char *const end)
{
internalPort.b[0] = *data++;
externalPort.b[0] = *data++;
((DNSServiceNATPortMappingReply)sdr->AppCallback)(sdr, cbh->cb_flags, cbh->cb_interface, cbh->cb_err, addr.l, protocol, internalPort.s, externalPort.s, ttl, sdr->AppContext);
return;
// MUST NOT touch sdr after invoking AppCallback -- client is allowed to dispose it from within callback function
fail :
syslog(LOG_WARNING, "dnssd_clientstub handle_port_mapping_response: error reading result from daemon");
}
(
void *context /* may be NULL */
)
{
char *ptr;
DNSServiceErrorType err = ConnectToServer(sdRef, flags, port_mapping_request, handle_port_mapping_response, (void *)callBack, context);
len += sizeof(interfaceIndex);
len += sizeof(internalPort);
len += sizeof(externalPort);
*ptr++ = internalPort.b[0];
*ptr++ = externalPort.b[0];
return err;
}
(
)
{
if (!queue)
{
return kDNSServiceErr_BadParam;
}
if (service->disp_queue)
{
return kDNSServiceErr_BadParam;
}
if (service->disp_source)
{
return kDNSServiceErr_BadParam;
}
if (!service->disp_source)
{
return kDNSServiceErr_NoMemory;
}
return kDNSServiceErr_NoError;
}
#endif // _DNS_SD_LIBDISPATCH
#if !defined(_WIN32)
static void DNSSD_API SleepKeepaliveCallback(DNSServiceRef sdRef, DNSRecordRef rec, const DNSServiceFlags flags,
{
(void)rec; // Unused
(void)flags; // Unused
if (ka->AppCallback)
}
(
int fd,
unsigned int timeout,
void *context
)
{
char source_str[INET6_ADDRSTRLEN];
char target_str[INET6_ADDRSTRLEN];
struct sockaddr_storage lss;
struct sockaddr_storage rss;
unsigned int len, proxyreclen;
char buf[256];
char name[10];
char recname[128];
unsigned int i, unique;
(void) flags; //unused
if (!timeout) return kDNSServiceErr_BadParam;
{
return kDNSServiceErr_BadParam;
}
{
return kDNSServiceErr_BadParam;
}
{
return kDNSServiceErr_Unknown;
}
unique = 0;
{
{
return kDNSServiceErr_Unknown;
}
{
return kDNSServiceErr_Unknown;
}
// Sum of all bytes in the local address and port should result in a unique
// number in the local network
for (i = 0; i < sizeof(struct in_addr); i++)
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));
}
else
{
{
return kDNSServiceErr_Unknown;
}
{
return kDNSServiceErr_Unknown;
}
for (i = 0; i < sizeof(struct in6_addr); i++)
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));
}
{
return kDNSServiceErr_Unknown;
}
// Include the NULL byte also in the first byte. The total length of the record includes the
// first byte also.
{
return kDNSServiceErr_Unknown;
}
{
return kDNSServiceErr_Unknown;
}
if (!ka) return kDNSServiceErr_NoMemory;
if (err)
{
return err;
}
// we don't care about the "record". When sdRef gets deallocated later, it will be freed too
kDNSServiceType_NULL, kDNSServiceClass_IN, proxyreclen, buf, kDNSServiceInterfaceIndexAny, SleepKeepaliveCallback, ka);
if (err)
{
return err;
}
return kDNSServiceErr_NoError;
}
#endif