]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSShared/dnssd_ipc.h
mDNSResponder-176.3.tar.gz
[apple/mdnsresponder.git] / mDNSShared / dnssd_ipc.h
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.40 2007/09/07 20:56:03 cheshire
32 Renamed uint32_t field in client_context_t from "ptr64" to more accurate name "u32"
33
34 Revision 1.39 2007/08/18 01:02:04 mcguire
35 <rdar://problem/5415593> No Bonjour services are getting registered at boot
36
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
39
40 Revision 1.37 2007/07/28 00:00:43 cheshire
41 Renamed CompileTimeAssertionCheck structure for consistency with others
42
43 Revision 1.36 2007/07/23 22:12:53 cheshire
44 <rdar://problem/5352299> Make mDNSResponder more defensive against malicious local clients
45
46 Revision 1.35 2007/05/23 18:59:22 cheshire
47 Remove unnecessary IPC_FLAGS_REUSE_SOCKET
48
49 Revision 1.34 2007/05/22 01:07:42 cheshire
50 <rdar://problem/3563675> API: Need a way to get version/feature information
51
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
54
55 Revision 1.32 2007/05/18 20:31:20 cheshire
56 Rename port_mapping_create_request to port_mapping_request
57
58 Revision 1.31 2007/05/18 17:56:20 cheshire
59 Rename port_mapping_create_reply_op to port_mapping_reply_op
60
61 Revision 1.30 2007/05/16 01:06:52 cheshire
62 <rdar://problem/4471320> Improve reliability of kDNSServiceFlagsMoreComing flag on multiprocessor machines
63
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
67
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
70
71 Revision 1.27 2006/10/27 00:38:22 cheshire
72 Strip accidental trailing whitespace from lines
73
74 Revision 1.26 2006/09/27 00:44:36 herscher
75 <rdar://problem/4249761> API: Need DNSServiceGetAddrInfo()
76
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)
79
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"
83
84 Revision 1.23 2006/08/14 23:05:53 cheshire
85 Added "tab-width" emacs header line
86
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
90
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.
93
94 Revision 1.20 2005/03/21 00:39:31 shersche
95 <rdar://problem/4021486> Fix build warnings on Win32 platform
96
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
99
100 Revision 1.18 2005/01/27 22:57:56 cheshire
101 Fix compile errors on gcc4
102
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.
106
107 Revision 1.16 2004/11/12 03:21:41 rpantos
108 rdar://problem/3809541 Add DNSSDMapIfIndexToName, DNSSDMapNameToIfIndex.
109
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)"
112
113 Revision 1.14 2004/10/01 22:15:55 rpantos
114 rdar://problem/3824265: Replace APSL in client lib with BSD license.
115
116 Revision 1.13 2004/09/16 23:14:25 cheshire
117 Changes for Windows compatibility
118
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
121
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
125
126 Revision 1.10 2004/07/07 17:39:25 shersche
127 Change MDNS_SERVERPORT from 5533 to 5354.
128
129 Revision 1.9 2004/06/25 00:26:27 rpantos
130 Changes to fix the Posix build on Solaris.
131
132 Revision 1.8 2004/06/18 04:56:51 rpantos
133 Add layer for platform code
134
135 Revision 1.7 2004/06/12 01:08:14 cheshire
136 Changes for Windows compatibility
137
138 Revision 1.6 2003/08/12 19:56:25 cheshire
139 Update to APSL 2.0
140
141 */
142
143 #ifndef DNSSD_IPC_H
144 #define DNSSD_IPC_H
145
146 #include "dns_sd.h"
147
148 //
149 // Common cross platform services
150 //
151 #if defined(WIN32)
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()
161 # define ssize_t int
162 # define getpid _getpid
163 #else
164 # include <sys/types.h>
165 # include <unistd.h>
166 # include <sys/un.h>
167 # include <string.h>
168 # include <stdio.h>
169 # include <stdlib.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
182 #endif
183
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
188 # define LISTENQ 5
189 # define dnssd_sockaddr_t struct sockaddr_in
190 #else
191 # define AF_DNSSD AF_LOCAL
192 # ifndef MDNS_UDS_SERVERPATH
193 # define MDNS_UDS_SERVERPATH "/var/run/mDNSResponder"
194 # endif
195 # define LISTENQ 100
196 // longest legal control path length
197 # define MAX_CTLPATH 256
198 # define dnssd_sockaddr_t struct sockaddr_un
199 #endif
200
201 // Compatibility workaround
202 #ifndef AF_LOCAL
203 #define AF_LOCAL AF_UNIX
204 #endif
205
206 // General UDS constants
207 #define TXT_RECORD_INDEX ((uint32_t)(-1)) // record index for default text record
208
209 // IPC data encoding constants and types
210 #define VERSION 1
211 #define IPC_FLAGS_NOREPLY 1 // set flag if no asynchronous replies are to be sent to client
212
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.
216 #ifndef packedstruct
217 #if ((__GNUC__ > 2) || ((__GNUC__ == 2) && (__GNUC_MINOR__ >= 9)))
218 #define packedstruct struct __attribute__((__packed__))
219 #define packedunion union __attribute__((__packed__))
220 #else
221 #define packedstruct struct
222 #define packedunion union
223 #endif
224 #endif
225
226 typedef enum
227 {
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,
232 enumeration_request,
233 reg_service_request,
234 browse_request,
235 resolve_request,
236 query_request,
237 reconfirm_record_request,
238 add_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
243 addrinfo_request,
244
245 cancel_request = 63
246 } request_op_t;
247
248 typedef enum
249 {
250 enumeration_reply_op = 64,
251 reg_service_reply_op,
252 browse_reply_op,
253 resolve_reply_op,
254 query_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
258 addrinfo_reply_op
259 } reply_op_t;
260
261 #if defined(_WIN64)
262 # pragma pack(4)
263 #endif
264
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
268 typedef packedunion
269 {
270 void *context;
271 uint32_t u32[2];
272 } client_context_t;
273
274 typedef packedstruct
275 {
276 uint32_t version;
277 uint32_t datalen;
278 uint32_t ipc_flags;
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())
284 } ipc_msg_hdr;
285
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
290
291 void put_uint32(const uint32_t l, char **ptr);
292 uint32_t get_uint32(char **ptr, char *end);
293
294 void put_uint16(uint16_t s, char **ptr);
295 uint16_t get_uint16(char **ptr, char *end);
296
297 #define put_flags put_uint32
298 #define get_flags get_uint32
299
300 #define put_error_code put_uint32
301 #define get_error_code get_uint32
302
303 int put_string(const char *str, char **ptr);
304 int get_string(char **ptr, char *end, char *buffer, int buflen);
305
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.
309
310 void ConvertHeaderBytes(ipc_msg_hdr *hdr);
311
312 struct CompileTimeAssertionChecks_dnssd_ipc
313 {
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];
318 };
319
320 #endif // DNSSD_IPC_H