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.40 2007/09/07 20:56:03 cheshire
32 Renamed uint32_t field in client_context_t from "ptr64" to more accurate name "u32"
34 Revision 1.39 2007/08/18 01:02:04 mcguire
35 <rdar://problem/5415593> No Bonjour services are getting registered at boot
37 Revision 1.38 2007/08/08 22:34:59 mcguire
38 <rdar://problem/5197869> Security: Run mDNSResponder as user id mdnsresponder instead of root
40 Revision 1.37 2007/07/28 00:00:43 cheshire
41 Renamed CompileTimeAssertionCheck structure for consistency with others
43 Revision 1.36 2007/07/23 22:12:53 cheshire
44 <rdar://problem/5352299> Make mDNSResponder more defensive against malicious local clients
46 Revision 1.35 2007/05/23 18:59:22 cheshire
47 Remove unnecessary IPC_FLAGS_REUSE_SOCKET
49 Revision 1.34 2007/05/22 01:07:42 cheshire
50 <rdar://problem/3563675> API: Need a way to get version/feature information
52 Revision 1.33 2007/05/18 23:55:22 cheshire
53 <rdar://problem/4454655> Allow multiple register/browse/resolve operations to share single Unix Domain Socket
55 Revision 1.32 2007/05/18 20:31:20 cheshire
56 Rename port_mapping_create_request to port_mapping_request
58 Revision 1.31 2007/05/18 17:56:20 cheshire
59 Rename port_mapping_create_reply_op to port_mapping_reply_op
61 Revision 1.30 2007/05/16 01:06:52 cheshire
62 <rdar://problem/4471320> Improve reliability of kDNSServiceFlagsMoreComing flag on multiprocessor machines
64 Revision 1.29 2007/05/15 21:57:16 cheshire
65 <rdar://problem/4608220> Use dnssd_SocketValid(x) macro instead of just
66 assuming that all negative values (or zero!) are invalid socket numbers
68 Revision 1.28 2007/03/21 19:01:57 cheshire
69 <rdar://problem/5078494> IPC code not 64-bit-savvy: assumes long=32bits, and short=16bits
71 Revision 1.27 2006/10/27 00:38:22 cheshire
72 Strip accidental trailing whitespace from lines
74 Revision 1.26 2006/09/27 00:44:36 herscher
75 <rdar://problem/4249761> API: Need DNSServiceGetAddrInfo()
77 Revision 1.25 2006/09/26 01:51:07 herscher
78 <rdar://problem/4245016> NAT Port Mapping API (for both NAT-PMP and UPnP Gateway Protocol)
80 Revision 1.24 2006/09/18 19:21:42 cheshire
81 <rdar://problem/4737048> gcc's structure padding breaks Bonjour APIs on
82 64-bit clients; need to declare ipc_msg_hdr structure "packed"
84 Revision 1.23 2006/08/14 23:05:53 cheshire
85 Added "tab-width" emacs header line
87 Revision 1.22 2006/06/28 08:56:26 cheshire
88 Added "_op" to the end of the operation code enum values,
89 to differentiate them from the routines with the same names
91 Revision 1.21 2005/09/29 06:38:13 herscher
92 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.
94 Revision 1.20 2005/03/21 00:39:31 shersche
95 <rdar://problem/4021486> Fix build warnings on Win32 platform
97 Revision 1.19 2005/02/02 02:25:22 cheshire
98 <rdar://problem/3980388> /var/run/mDNSResponder should be /var/run/mdnsd on Linux
100 Revision 1.18 2005/01/27 22:57:56 cheshire
101 Fix compile errors on gcc4
103 Revision 1.17 2004/11/23 03:39:47 cheshire
104 Let interface name/index mapping capability live directly in JNISupport.c,
105 instead of having to call through to the daemon via IPC to get this information.
107 Revision 1.16 2004/11/12 03:21:41 rpantos
108 rdar://problem/3809541 Add DNSSDMapIfIndexToName, DNSSDMapNameToIfIndex.
110 Revision 1.15 2004/10/06 02:22:20 cheshire
111 Changed MacRoman copyright symbol (should have been UTF-8 in any case :-) to ASCII-compatible "(c)"
113 Revision 1.14 2004/10/01 22:15:55 rpantos
114 rdar://problem/3824265: Replace APSL in client lib with BSD license.
116 Revision 1.13 2004/09/16 23:14:25 cheshire
117 Changes for Windows compatibility
119 Revision 1.12 2004/09/16 21:46:38 ksekar
120 <rdar://problem/3665304> Need SPI for LoginWindow to associate a UID with a Wide Area domain
122 Revision 1.11 2004/08/10 06:24:56 cheshire
123 Use types with precisely defined sizes for 'op' and 'reg_index', for better
124 compatibility if the daemon and the client stub are built using different compilers
126 Revision 1.10 2004/07/07 17:39:25 shersche
127 Change MDNS_SERVERPORT from 5533 to 5354.
129 Revision 1.9 2004/06/25 00:26:27 rpantos
130 Changes to fix the Posix build on Solaris.
132 Revision 1.8 2004/06/18 04:56:51 rpantos
133 Add layer for platform code
135 Revision 1.7 2004/06/12 01:08:14 cheshire
136 Changes for Windows compatibility
138 Revision 1.6 2003/08/12 19:56:25 cheshire
149 // Common cross platform services
152 # include <winsock2.h>
153 # define dnssd_InvalidSocket INVALID_SOCKET
154 # define dnssd_SocketValid(s) ((s) != INVALID_SOCKET)
155 # define dnssd_EWOULDBLOCK WSAEWOULDBLOCK
156 # define dnssd_EINTR WSAEINTR
157 # define dnssd_sock_t SOCKET
158 # define dnssd_socklen_t int
159 # define dnssd_close(sock) closesocket(sock)
160 # define dnssd_errno() WSAGetLastError()
162 # define getpid _getpid
164 # include <sys/types.h>
170 # include <sys/stat.h>
171 # include <sys/socket.h>
172 # include <netinet/in.h>
173 # define dnssd_InvalidSocket -1
174 # define dnssd_SocketValid(s) ((s) >= 0)
175 # define dnssd_EWOULDBLOCK EWOULDBLOCK
176 # define dnssd_EINTR EINTR
177 # define dnssd_EPIPE EPIPE
178 # define dnssd_sock_t int
179 # define dnssd_socklen_t unsigned int
180 # define dnssd_close(sock) close(sock)
181 # define dnssd_errno() errno
184 #if defined(USE_TCP_LOOPBACK)
185 # define AF_DNSSD AF_INET
186 # define MDNS_TCP_SERVERADDR "127.0.0.1"
187 # define MDNS_TCP_SERVERPORT 5354
189 # define dnssd_sockaddr_t struct sockaddr_in
191 # define AF_DNSSD AF_LOCAL
192 # ifndef MDNS_UDS_SERVERPATH
193 # define MDNS_UDS_SERVERPATH "/var/run/mDNSResponder"
196 // longest legal control path length
197 # define MAX_CTLPATH 256
198 # define dnssd_sockaddr_t struct sockaddr_un
201 // Compatibility workaround
203 #define AF_LOCAL AF_UNIX
206 // General UDS constants
207 #define TXT_RECORD_INDEX ((uint32_t)(-1)) // record index for default text record
209 // IPC data encoding constants and types
211 #define IPC_FLAGS_NOREPLY 1 // set flag if no asynchronous replies are to be sent to client
213 // Structure packing macro. If we're not using GNUC, it's not fatal. Most compilers naturally pack the on-the-wire
214 // structures correctly anyway, so a plain "struct" is usually fine. In the event that structures are not packed
215 // correctly, our compile-time assertion checks will catch it and prevent inadvertent generation of non-working code.
217 #if ((__GNUC__ > 2) || ((__GNUC__ == 2) && (__GNUC_MINOR__ >= 9)))
218 #define packedstruct struct __attribute__((__packed__))
219 #define packedunion union __attribute__((__packed__))
221 #define packedstruct struct
222 #define packedunion union
228 request_op_none
= 0, // No request yet received on this connection
229 connection_request
= 1, // connected socket via DNSServiceConnect()
230 reg_record_request
, // reg/remove record only valid for connected sockets
231 remove_record_request
,
237 reconfirm_record_request
,
239 update_record_request
,
240 setdomain_request
, // Up to here is in Tiger and B4W 1.0.3
241 getproperty_request
, // New in B4W 1.0.4
242 port_mapping_request
, // New in Leopard and B4W 2.0
250 enumeration_reply_op
= 64,
251 reg_service_reply_op
,
255 reg_record_reply_op
, // Up to here is in Tiger and B4W 1.0.3
256 getproperty_reply_op
, // New in B4W 1.0.4
257 port_mapping_reply_op
, // New in Leopard and B4W 2.0
265 // Define context object big enough to hold a 64-bit pointer,
266 // to accomodate 64-bit clients communicating with 32-bit daemon.
267 // There's no reason for the daemon to ever be a 64-bit process, but its clients might be
279 uint32_t op
; // request_op_t or reply_op_t
280 client_context_t client_context
; // context passed from client, returned by server in corresponding reply
281 uint32_t reg_index
; // identifier for a record registered via DNSServiceRegisterRecord() on a
282 // socket connected by DNSServiceConnect(). Must be unique in the scope of the connection, such that and
283 // index/socket pair uniquely identifies a record. (Used to select records for removal by DNSServiceRemoveRecord())
286 // routines to write to and extract data from message buffers.
287 // caller responsible for bounds checking.
288 // ptr is the address of the pointer to the start of the field.
289 // it is advanced to point to the next field, or the end of the message
291 void put_uint32(const uint32_t l
, char **ptr
);
292 uint32_t get_uint32(char **ptr
, char *end
);
294 void put_uint16(uint16_t s
, char **ptr
);
295 uint16_t get_uint16(char **ptr
, char *end
);
297 #define put_flags put_uint32
298 #define get_flags get_uint32
300 #define put_error_code put_uint32
301 #define get_error_code get_uint32
303 int put_string(const char *str
, char **ptr
);
304 int get_string(char **ptr
, char *end
, char *buffer
, int buflen
);
306 void put_rdata(const int rdlen
, const unsigned char *rdata
, char **ptr
);
307 char *get_rdata(char **ptr
, char *end
, int rdlen
); // return value is rdata pointed to by *ptr -
308 // rdata is not copied from buffer.
310 void ConvertHeaderBytes(ipc_msg_hdr
*hdr
);
312 struct CompileTimeAssertionChecks_dnssd_ipc
314 // Check that the compiler generated our on-the-wire packet format structure definitions
315 // properly packed, without adding padding bytes to align fields on 32-bit or 64-bit boundaries.
316 char assert0
[(sizeof(client_context_t
) == 8) ? 1 : -1];
317 char assert1
[(sizeof(ipc_msg_hdr
) == 28) ? 1 : -1];
320 #endif // DNSSD_IPC_H