]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSShared/dnssd_ipc.h
mDNSResponder-258.21.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
29 #ifndef DNSSD_IPC_H
30 #define DNSSD_IPC_H
31
32 #include "dns_sd.h"
33
34 //
35 // Common cross platform services
36 //
37 #if defined(WIN32)
38 # include <winsock2.h>
39 # define dnssd_InvalidSocket INVALID_SOCKET
40 # define dnssd_SocketValid(s) ((s) != INVALID_SOCKET)
41 # define dnssd_EWOULDBLOCK WSAEWOULDBLOCK
42 # define dnssd_EINTR WSAEINTR
43 # define dnssd_sock_t SOCKET
44 # define dnssd_socklen_t int
45 # define dnssd_close(sock) closesocket(sock)
46 # define dnssd_errno WSAGetLastError()
47 # define dnssd_strerror(X) win32_strerror(X)
48 # define ssize_t int
49 # define getpid _getpid
50 # define unlink _unlink
51 extern char *win32_strerror(int inErrorCode);
52 #else
53 # include <sys/types.h>
54 # include <unistd.h>
55 # include <sys/un.h>
56 # include <string.h>
57 # include <stdio.h>
58 # include <stdlib.h>
59 # include <sys/stat.h>
60 # include <sys/socket.h>
61 # include <netinet/in.h>
62 # define dnssd_InvalidSocket -1
63 # define dnssd_SocketValid(s) ((s) >= 0)
64 # define dnssd_EWOULDBLOCK EWOULDBLOCK
65 # define dnssd_EINTR EINTR
66 # define dnssd_EPIPE EPIPE
67 # define dnssd_sock_t int
68 # define dnssd_socklen_t unsigned int
69 # define dnssd_close(sock) close(sock)
70 # define dnssd_errno errno
71 # define dnssd_strerror(X) strerror(X)
72 #endif
73
74 #if defined(USE_TCP_LOOPBACK)
75 # define AF_DNSSD AF_INET
76 # define MDNS_TCP_SERVERADDR "127.0.0.1"
77 # define MDNS_TCP_SERVERPORT 5354
78 # define LISTENQ 5
79 # define dnssd_sockaddr_t struct sockaddr_in
80 #else
81 # define AF_DNSSD AF_LOCAL
82 # ifndef MDNS_UDS_SERVERPATH
83 # define MDNS_UDS_SERVERPATH "/var/run/mDNSResponder"
84 # endif
85 # define LISTENQ 100
86 // longest legal control path length
87 # define MAX_CTLPATH 256
88 # define dnssd_sockaddr_t struct sockaddr_un
89 #endif
90
91 // Compatibility workaround
92 #ifndef AF_LOCAL
93 #define AF_LOCAL AF_UNIX
94 #endif
95
96 // General UDS constants
97 #define TXT_RECORD_INDEX ((uint32_t)(-1)) // record index for default text record
98
99 // IPC data encoding constants and types
100 #define VERSION 1
101 #define IPC_FLAGS_NOREPLY 1 // set flag if no asynchronous replies are to be sent to client
102
103 // Structure packing macro. If we're not using GNUC, it's not fatal. Most compilers naturally pack the on-the-wire
104 // structures correctly anyway, so a plain "struct" is usually fine. In the event that structures are not packed
105 // correctly, our compile-time assertion checks will catch it and prevent inadvertent generation of non-working code.
106 #ifndef packedstruct
107 #if ((__GNUC__ > 2) || ((__GNUC__ == 2) && (__GNUC_MINOR__ >= 9)))
108 #define packedstruct struct __attribute__((__packed__))
109 #define packedunion union __attribute__((__packed__))
110 #else
111 #define packedstruct struct
112 #define packedunion union
113 #endif
114 #endif
115
116 typedef enum
117 {
118 request_op_none = 0, // No request yet received on this connection
119 connection_request = 1, // connected socket via DNSServiceCreateConnection()
120 reg_record_request, // reg/remove record only valid for connected sockets
121 remove_record_request,
122 enumeration_request,
123 reg_service_request,
124 browse_request,
125 resolve_request,
126 query_request,
127 reconfirm_record_request,
128 add_record_request,
129 update_record_request,
130 setdomain_request, // Up to here is in Tiger and B4W 1.0.3
131 getproperty_request, // New in B4W 1.0.4
132 port_mapping_request, // New in Leopard and B4W 2.0
133 addrinfo_request,
134 send_bpf, // New in SL
135
136 cancel_request = 63
137 } request_op_t;
138
139 typedef enum
140 {
141 enumeration_reply_op = 64,
142 reg_service_reply_op,
143 browse_reply_op,
144 resolve_reply_op,
145 query_reply_op,
146 reg_record_reply_op, // Up to here is in Tiger and B4W 1.0.3
147 getproperty_reply_op, // New in B4W 1.0.4
148 port_mapping_reply_op, // New in Leopard and B4W 2.0
149 addrinfo_reply_op
150 } reply_op_t;
151
152 #if defined(_WIN64)
153 # pragma pack(4)
154 #endif
155
156 // Define context object big enough to hold a 64-bit pointer,
157 // to accomodate 64-bit clients communicating with 32-bit daemon.
158 // There's no reason for the daemon to ever be a 64-bit process, but its clients might be
159 typedef packedunion
160 {
161 void *context;
162 uint32_t u32[2];
163 } client_context_t;
164
165 typedef packedstruct
166 {
167 uint32_t version;
168 uint32_t datalen;
169 uint32_t ipc_flags;
170 uint32_t op; // request_op_t or reply_op_t
171 client_context_t client_context; // context passed from client, returned by server in corresponding reply
172 uint32_t reg_index; // identifier for a record registered via DNSServiceRegisterRecord() on a
173 // socket connected by DNSServiceCreateConnection(). Must be unique in the scope of the connection, such that and
174 // index/socket pair uniquely identifies a record. (Used to select records for removal by DNSServiceRemoveRecord())
175 } ipc_msg_hdr;
176
177 // routines to write to and extract data from message buffers.
178 // caller responsible for bounds checking.
179 // ptr is the address of the pointer to the start of the field.
180 // it is advanced to point to the next field, or the end of the message
181
182 void put_uint32(const uint32_t l, char **ptr);
183 uint32_t get_uint32(const char **ptr, const char *end);
184
185 void put_uint16(uint16_t s, char **ptr);
186 uint16_t get_uint16(const char **ptr, const char *end);
187
188 #define put_flags put_uint32
189 #define get_flags get_uint32
190
191 #define put_error_code put_uint32
192 #define get_error_code get_uint32
193
194 int put_string(const char *str, char **ptr);
195 int get_string(const char **ptr, const char *const end, char *buffer, int buflen);
196
197 void put_rdata(const int rdlen, const unsigned char *rdata, char **ptr);
198 const char *get_rdata(const char **ptr, const char *end, int rdlen); // return value is rdata pointed to by *ptr -
199 // rdata is not copied from buffer.
200
201 void ConvertHeaderBytes(ipc_msg_hdr *hdr);
202
203 struct CompileTimeAssertionChecks_dnssd_ipc
204 {
205 // Check that the compiler generated our on-the-wire packet format structure definitions
206 // properly packed, without adding padding bytes to align fields on 32-bit or 64-bit boundaries.
207 char assert0[(sizeof(client_context_t) == 8) ? 1 : -1];
208 char assert1[(sizeof(ipc_msg_hdr) == 28) ? 1 : -1];
209 };
210
211 #endif // DNSSD_IPC_H