1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2003-2004, Apple Computer, Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 Change History (most recent first):
31 Revision 1.46 2009/05/27 22:20:44 cheshire
32 Removed unused dnssd_errno_assign() (we have no business writing to errno -- we should only read it)
34 Revision 1.45 2009/05/26 21:31:07 herscher
35 Fix compile errors on Windows
37 Revision 1.44 2009/02/12 20:28:31 cheshire
38 Added some missing "const" declarations
40 Revision 1.43 2008/10/23 23:21:31 cheshire
41 Moved definition of dnssd_strerror() to be with the definition of dnssd_errno, in dnssd_ipc.h
43 Revision 1.42 2008/10/23 23:06:17 cheshire
44 Removed () from dnssd_errno macro definition -- it's not a function and doesn't need any arguments
46 Revision 1.41 2008/09/27 01:04:09 cheshire
47 Added "send_bpf" to list of request_op_t operation codes
49 Revision 1.40 2007/09/07 20:56:03 cheshire
50 Renamed uint32_t field in client_context_t from "ptr64" to more accurate name "u32"
52 Revision 1.39 2007/08/18 01:02:04 mcguire
53 <rdar://problem/5415593> No Bonjour services are getting registered at boot
55 Revision 1.38 2007/08/08 22:34:59 mcguire
56 <rdar://problem/5197869> Security: Run mDNSResponder as user id mdnsresponder instead of root
58 Revision 1.37 2007/07/28 00:00:43 cheshire
59 Renamed CompileTimeAssertionCheck structure for consistency with others
61 Revision 1.36 2007/07/23 22:12:53 cheshire
62 <rdar://problem/5352299> Make mDNSResponder more defensive against malicious local clients
64 Revision 1.35 2007/05/23 18:59:22 cheshire
65 Remove unnecessary IPC_FLAGS_REUSE_SOCKET
67 Revision 1.34 2007/05/22 01:07:42 cheshire
68 <rdar://problem/3563675> API: Need a way to get version/feature information
70 Revision 1.33 2007/05/18 23:55:22 cheshire
71 <rdar://problem/4454655> Allow multiple register/browse/resolve operations to share single Unix Domain Socket
73 Revision 1.32 2007/05/18 20:31:20 cheshire
74 Rename port_mapping_create_request to port_mapping_request
76 Revision 1.31 2007/05/18 17:56:20 cheshire
77 Rename port_mapping_create_reply_op to port_mapping_reply_op
79 Revision 1.30 2007/05/16 01:06:52 cheshire
80 <rdar://problem/4471320> Improve reliability of kDNSServiceFlagsMoreComing flag on multiprocessor machines
82 Revision 1.29 2007/05/15 21:57:16 cheshire
83 <rdar://problem/4608220> Use dnssd_SocketValid(x) macro instead of just
84 assuming that all negative values (or zero!) are invalid socket numbers
86 Revision 1.28 2007/03/21 19:01:57 cheshire
87 <rdar://problem/5078494> IPC code not 64-bit-savvy: assumes long=32bits, and short=16bits
89 Revision 1.27 2006/10/27 00:38:22 cheshire
90 Strip accidental trailing whitespace from lines
92 Revision 1.26 2006/09/27 00:44:36 herscher
93 <rdar://problem/4249761> API: Need DNSServiceGetAddrInfo()
95 Revision 1.25 2006/09/26 01:51:07 herscher
96 <rdar://problem/4245016> NAT Port Mapping API (for both NAT-PMP and UPnP Gateway Protocol)
98 Revision 1.24 2006/09/18 19:21:42 cheshire
99 <rdar://problem/4737048> gcc's structure padding breaks Bonjour APIs on
100 64-bit clients; need to declare ipc_msg_hdr structure "packed"
102 Revision 1.23 2006/08/14 23:05:53 cheshire
103 Added "tab-width" emacs header line
105 Revision 1.22 2006/06/28 08:56:26 cheshire
106 Added "_op" to the end of the operation code enum values,
107 to differentiate them from the routines with the same names
109 Revision 1.21 2005/09/29 06:38:13 herscher
110 Remove #define MSG_WAITALL on Windows. We don't use this macro anymore, and it's presence causes warnings to be emitted when compiling against the latest Microsoft Platform SDK.
112 Revision 1.20 2005/03/21 00:39:31 shersche
113 <rdar://problem/4021486> Fix build warnings on Win32 platform
115 Revision 1.19 2005/02/02 02:25:22 cheshire
116 <rdar://problem/3980388> /var/run/mDNSResponder should be /var/run/mdnsd on Linux
118 Revision 1.18 2005/01/27 22:57:56 cheshire
119 Fix compile errors on gcc4
121 Revision 1.17 2004/11/23 03:39:47 cheshire
122 Let interface name/index mapping capability live directly in JNISupport.c,
123 instead of having to call through to the daemon via IPC to get this information.
125 Revision 1.16 2004/11/12 03:21:41 rpantos
126 rdar://problem/3809541 Add DNSSDMapIfIndexToName, DNSSDMapNameToIfIndex.
128 Revision 1.15 2004/10/06 02:22:20 cheshire
129 Changed MacRoman copyright symbol (should have been UTF-8 in any case :-) to ASCII-compatible "(c)"
131 Revision 1.14 2004/10/01 22:15:55 rpantos
132 rdar://problem/3824265: Replace APSL in client lib with BSD license.
134 Revision 1.13 2004/09/16 23:14:25 cheshire
135 Changes for Windows compatibility
137 Revision 1.12 2004/09/16 21:46:38 ksekar
138 <rdar://problem/3665304> Need SPI for LoginWindow to associate a UID with a Wide Area domain
140 Revision 1.11 2004/08/10 06:24:56 cheshire
141 Use types with precisely defined sizes for 'op' and 'reg_index', for better
142 compatibility if the daemon and the client stub are built using different compilers
144 Revision 1.10 2004/07/07 17:39:25 shersche
145 Change MDNS_SERVERPORT from 5533 to 5354.
147 Revision 1.9 2004/06/25 00:26:27 rpantos
148 Changes to fix the Posix build on Solaris.
150 Revision 1.8 2004/06/18 04:56:51 rpantos
151 Add layer for platform code
153 Revision 1.7 2004/06/12 01:08:14 cheshire
154 Changes for Windows compatibility
156 Revision 1.6 2003/08/12 19:56:25 cheshire
167 // Common cross platform services
170 # include <winsock2.h>
171 # define dnssd_InvalidSocket INVALID_SOCKET
172 # define dnssd_SocketValid(s) ((s) != INVALID_SOCKET)
173 # define dnssd_EWOULDBLOCK WSAEWOULDBLOCK
174 # define dnssd_EINTR WSAEINTR
175 # define dnssd_sock_t SOCKET
176 # define dnssd_socklen_t int
177 # define dnssd_close(sock) closesocket(sock)
178 # define dnssd_errno WSAGetLastError()
179 # define dnssd_strerror(X) win32_strerror(X)
181 # define getpid _getpid
183 # include <sys/types.h>
189 # include <sys/stat.h>
190 # include <sys/socket.h>
191 # include <netinet/in.h>
192 # define dnssd_InvalidSocket -1
193 # define dnssd_SocketValid(s) ((s) >= 0)
194 # define dnssd_EWOULDBLOCK EWOULDBLOCK
195 # define dnssd_EINTR EINTR
196 # define dnssd_EPIPE EPIPE
197 # define dnssd_sock_t int
198 # define dnssd_socklen_t unsigned int
199 # define dnssd_close(sock) close(sock)
200 # define dnssd_errno errno
201 # define dnssd_strerror(X) strerror(X)
204 #if defined(USE_TCP_LOOPBACK)
205 # define AF_DNSSD AF_INET
206 # define MDNS_TCP_SERVERADDR "127.0.0.1"
207 # define MDNS_TCP_SERVERPORT 5354
209 # define dnssd_sockaddr_t struct sockaddr_in
211 # define AF_DNSSD AF_LOCAL
212 # ifndef MDNS_UDS_SERVERPATH
213 # define MDNS_UDS_SERVERPATH "/var/run/mDNSResponder"
216 // longest legal control path length
217 # define MAX_CTLPATH 256
218 # define dnssd_sockaddr_t struct sockaddr_un
221 // Compatibility workaround
223 #define AF_LOCAL AF_UNIX
226 // General UDS constants
227 #define TXT_RECORD_INDEX ((uint32_t)(-1)) // record index for default text record
229 // IPC data encoding constants and types
231 #define IPC_FLAGS_NOREPLY 1 // set flag if no asynchronous replies are to be sent to client
233 // Structure packing macro. If we're not using GNUC, it's not fatal. Most compilers naturally pack the on-the-wire
234 // structures correctly anyway, so a plain "struct" is usually fine. In the event that structures are not packed
235 // correctly, our compile-time assertion checks will catch it and prevent inadvertent generation of non-working code.
237 #if ((__GNUC__ > 2) || ((__GNUC__ == 2) && (__GNUC_MINOR__ >= 9)))
238 #define packedstruct struct __attribute__((__packed__))
239 #define packedunion union __attribute__((__packed__))
241 #define packedstruct struct
242 #define packedunion union
248 request_op_none
= 0, // No request yet received on this connection
249 connection_request
= 1, // connected socket via DNSServiceConnect()
250 reg_record_request
, // reg/remove record only valid for connected sockets
251 remove_record_request
,
257 reconfirm_record_request
,
259 update_record_request
,
260 setdomain_request
, // Up to here is in Tiger and B4W 1.0.3
261 getproperty_request
, // New in B4W 1.0.4
262 port_mapping_request
, // New in Leopard and B4W 2.0
264 send_bpf
, // New in SL
271 enumeration_reply_op
= 64,
272 reg_service_reply_op
,
276 reg_record_reply_op
, // Up to here is in Tiger and B4W 1.0.3
277 getproperty_reply_op
, // New in B4W 1.0.4
278 port_mapping_reply_op
, // New in Leopard and B4W 2.0
286 // Define context object big enough to hold a 64-bit pointer,
287 // to accomodate 64-bit clients communicating with 32-bit daemon.
288 // There's no reason for the daemon to ever be a 64-bit process, but its clients might be
300 uint32_t op
; // request_op_t or reply_op_t
301 client_context_t client_context
; // context passed from client, returned by server in corresponding reply
302 uint32_t reg_index
; // identifier for a record registered via DNSServiceRegisterRecord() on a
303 // socket connected by DNSServiceConnect(). Must be unique in the scope of the connection, such that and
304 // index/socket pair uniquely identifies a record. (Used to select records for removal by DNSServiceRemoveRecord())
307 // routines to write to and extract data from message buffers.
308 // caller responsible for bounds checking.
309 // ptr is the address of the pointer to the start of the field.
310 // it is advanced to point to the next field, or the end of the message
312 void put_uint32(const uint32_t l
, char **ptr
);
313 uint32_t get_uint32(const char **ptr
, const char *end
);
315 void put_uint16(uint16_t s
, char **ptr
);
316 uint16_t get_uint16(const char **ptr
, const char *end
);
318 #define put_flags put_uint32
319 #define get_flags get_uint32
321 #define put_error_code put_uint32
322 #define get_error_code get_uint32
324 int put_string(const char *str
, char **ptr
);
325 int get_string(const char **ptr
, const char *const end
, char *buffer
, int buflen
);
327 void put_rdata(const int rdlen
, const unsigned char *rdata
, char **ptr
);
328 const char *get_rdata(const char **ptr
, const char *end
, int rdlen
); // return value is rdata pointed to by *ptr -
329 // rdata is not copied from buffer.
331 void ConvertHeaderBytes(ipc_msg_hdr
*hdr
);
333 struct CompileTimeAssertionChecks_dnssd_ipc
335 // Check that the compiler generated our on-the-wire packet format structure definitions
336 // properly packed, without adding padding bytes to align fields on 32-bit or 64-bit boundaries.
337 char assert0
[(sizeof(client_context_t
) == 8) ? 1 : -1];
338 char assert1
[(sizeof(ipc_msg_hdr
) == 28) ? 1 : -1];
341 #endif // DNSSD_IPC_H