1 /* -*- Mode: C; tab-width: 4 -*-
2  *
3  * Copyright (c) 2003-2004, Apple Computer, Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
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.
16  *
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.
27 
28     Change History (most recent first):
29 
30 Log: dnssd_ipc.h,v $
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)
33 
34 Revision 1.45  2009/05/26 21:31:07  herscher
35 Fix compile errors on Windows
36 
37 Revision 1.44  2009/02/12 20:28:31  cheshire
38 Added some missing "const" declarations
39 
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
42 
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
45 
46 Revision 1.41  2008/09/27 01:04:09  cheshire
47 Added "send_bpf" to list of request_op_t operation codes
48 
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"
51 
52 Revision 1.39  2007/08/18 01:02:04  mcguire
53 <rdar://problem/5415593> No Bonjour services are getting registered at boot
54 
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
57 
58 Revision 1.37  2007/07/28 00:00:43  cheshire
59 Renamed CompileTimeAssertionCheck structure for consistency with others
60 
61 Revision 1.36  2007/07/23 22:12:53  cheshire
62 <rdar://problem/5352299> Make mDNSResponder more defensive against malicious local clients
63 
64 Revision 1.35  2007/05/23 18:59:22  cheshire
65 Remove unnecessary IPC_FLAGS_REUSE_SOCKET
66 
67 Revision 1.34  2007/05/22 01:07:42  cheshire
68 <rdar://problem/3563675> API: Need a way to get version/feature information
69 
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
72 
73 Revision 1.32  2007/05/18 20:31:20  cheshire
74 Rename port_mapping_create_request to port_mapping_request
75 
76 Revision 1.31  2007/05/18 17:56:20  cheshire
77 Rename port_mapping_create_reply_op to port_mapping_reply_op
78 
79 Revision 1.30  2007/05/16 01:06:52  cheshire
80 <rdar://problem/4471320> Improve reliability of kDNSServiceFlagsMoreComing flag on multiprocessor machines
81 
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
85 
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
88 
89 Revision 1.27  2006/10/27 00:38:22  cheshire
90 Strip accidental trailing whitespace from lines
91 
92 Revision 1.26  2006/09/27 00:44:36  herscher
93 <rdar://problem/4249761> API: Need DNSServiceGetAddrInfo()
94 
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)
97 
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"
101 
102 Revision 1.23  2006/08/14 23:05:53  cheshire
103 Added "tab-width" emacs header line
104 
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
108 
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.
111 
112 Revision 1.20  2005/03/21 00:39:31  shersche
113 <rdar://problem/4021486> Fix build warnings on Win32 platform
114 
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
117 
118 Revision 1.18  2005/01/27 22:57:56  cheshire
119 Fix compile errors on gcc4
120 
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.
124 
125 Revision 1.16  2004/11/12 03:21:41  rpantos
126 rdar://problem/3809541 Add DNSSDMapIfIndexToName, DNSSDMapNameToIfIndex.
127 
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)"
130 
131 Revision 1.14  2004/10/01 22:15:55  rpantos
132 rdar://problem/3824265: Replace APSL in client lib with BSD license.
133 
134 Revision 1.13  2004/09/16 23:14:25  cheshire
135 Changes for Windows compatibility
136 
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
139 
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
143 
144 Revision 1.10  2004/07/07 17:39:25  shersche
145 Change MDNS_SERVERPORT from 5533 to 5354.
146 
147 Revision 1.9  2004/06/25 00:26:27  rpantos
148 Changes to fix the Posix build on Solaris.
149 
150 Revision 1.8  2004/06/18 04:56:51  rpantos
151 Add layer for platform code
152 
153 Revision 1.7  2004/06/12 01:08:14  cheshire
154 Changes for Windows compatibility
155 
156 Revision 1.6  2003/08/12 19:56:25  cheshire
157 Update to APSL 2.0
158 
159  */
160 
161 #ifndef DNSSD_IPC_H
162 #define DNSSD_IPC_H
163 
164 #include "dns_sd.h"
165 
166 //
167 // Common cross platform services
168 //
169 #if defined(WIN32)
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)
180 #	define ssize_t				int
181 #	define getpid				_getpid
182 #else
183 #	include <sys/types.h>
184 #	include <unistd.h>
185 #	include <sys/un.h>
186 #	include <string.h>
187 #	include <stdio.h>
188 #	include <stdlib.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)
202 #endif
203 
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
208 #	define LISTENQ				5
209 #	define dnssd_sockaddr_t		struct sockaddr_in
210 #else
211 #	define AF_DNSSD				AF_LOCAL
212 #	ifndef MDNS_UDS_SERVERPATH
213 #		define MDNS_UDS_SERVERPATH	"/var/run/mDNSResponder"
214 #	endif
215 #	define LISTENQ				100
216     // longest legal control path length
217 #	define MAX_CTLPATH			256
218 #	define dnssd_sockaddr_t		struct sockaddr_un
219 #endif
220 
221 // Compatibility workaround
222 #ifndef AF_LOCAL
223 #define	AF_LOCAL	AF_UNIX
224 #endif
225 
226 // General UDS constants
227 #define TXT_RECORD_INDEX ((uint32_t)(-1))	// record index for default text record
228 
229 // IPC data encoding constants and types
230 #define VERSION 1
231 #define IPC_FLAGS_NOREPLY 1	// set flag if no asynchronous replies are to be sent to client
232 
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.
236 #ifndef packedstruct
237  #ifdef __packed
238   #define packedstruct struct __packed
239   #define packedunion  union  __packed
240  #elif ((__GNUC__ > 2) || ((__GNUC__ == 2) && (__GNUC_MINOR__ >= 9)))
241   #define packedstruct struct __attribute__((__packed__))
242   #define packedunion  union  __attribute__((__packed__))
243  #else
244   #define packedstruct struct
245   #define packedunion  union
246  #endif
247 #endif
248 
249 typedef enum
250     {
251     request_op_none = 0,	// No request yet received on this connection
252     connection_request = 1,	// connected socket via DNSServiceConnect()
253     reg_record_request,		// reg/remove record only valid for connected sockets
254     remove_record_request,
255     enumeration_request,
256     reg_service_request,
257     browse_request,
258     resolve_request,
259     query_request,
260     reconfirm_record_request,
261     add_record_request,
262     update_record_request,
263     setdomain_request,		// Up to here is in Tiger and B4W 1.0.3
264 	getproperty_request,	// New in B4W 1.0.4
265     port_mapping_request,	// New in Leopard and B4W 2.0
266 	addrinfo_request,
267 	send_bpf,				// New in SL
268 
269 	cancel_request = 63
270     } request_op_t;
271 
272 typedef enum
273     {
274     enumeration_reply_op = 64,
275     reg_service_reply_op,
276     browse_reply_op,
277     resolve_reply_op,
278     query_reply_op,
279     reg_record_reply_op,	// Up to here is in Tiger and B4W 1.0.3
280     getproperty_reply_op,	// New in B4W 1.0.4
281     port_mapping_reply_op,	// New in Leopard and B4W 2.0
282 	addrinfo_reply_op
283     } reply_op_t;
284 
285 #if defined(_WIN64)
286 #	pragma pack(4)
287 #endif
288 
289 // Define context object big enough to hold a 64-bit pointer,
290 // to accomodate 64-bit clients communicating with 32-bit daemon.
291 // There's no reason for the daemon to ever be a 64-bit process, but its clients might be
292 typedef packedunion
293     {
294     void *context;
295     uint32_t u32[2];
296     } client_context_t;
297 
298 typedef packedstruct
299     {
300     uint32_t version;
301     uint32_t datalen;
302     uint32_t ipc_flags;
303     uint32_t op;		// request_op_t or reply_op_t
304     client_context_t client_context; // context passed from client, returned by server in corresponding reply
305     uint32_t reg_index;            // identifier for a record registered via DNSServiceRegisterRecord() on a
306     // socket connected by DNSServiceConnect().  Must be unique in the scope of the connection, such that and
307     // index/socket pair uniquely identifies a record.  (Used to select records for removal by DNSServiceRemoveRecord())
308     } ipc_msg_hdr;
309 
310 // routines to write to and extract data from message buffers.
311 // caller responsible for bounds checking.
312 // ptr is the address of the pointer to the start of the field.
313 // it is advanced to point to the next field, or the end of the message
314 
315 void put_uint32(const uint32_t l, char **ptr);
316 uint32_t get_uint32(const char **ptr, const char *end);
317 
318 void put_uint16(uint16_t s, char **ptr);
319 uint16_t get_uint16(const char **ptr, const char *end);
320 
321 #define put_flags put_uint32
322 #define get_flags get_uint32
323 
324 #define put_error_code put_uint32
325 #define get_error_code get_uint32
326 
327 int put_string(const char *str, char **ptr);
328 int get_string(const char **ptr, const char *const end, char *buffer, int buflen);
329 
330 void put_rdata(const int rdlen, const unsigned char *rdata, char **ptr);
331 const char *get_rdata(const char **ptr, const char *end, int rdlen);  // return value is rdata pointed to by *ptr -
332                                          // rdata is not copied from buffer.
333 
334 void ConvertHeaderBytes(ipc_msg_hdr *hdr);
335 
336 struct CompileTimeAssertionChecks_dnssd_ipc
337 	{
338 	// Check that the compiler generated our on-the-wire packet format structure definitions
339 	// properly packed, without adding padding bytes to align fields on 32-bit or 64-bit boundaries.
340 	char assert0[(sizeof(client_context_t) ==  8) ? 1 : -1];
341 	char assert1[(sizeof(ipc_msg_hdr)      == 28) ? 1 : -1];
342 	};
343 
344 #endif // DNSSD_IPC_H
345