xref: /illumos-gate/usr/src/contrib/mDNSResponder/mDNSShared/dnssd_ipc.h (revision 472cd20d26008f77084ade4c2048159b98c2b705)
1c65ebfc7SToomas Soome /* -*- Mode: C; tab-width: 4 -*-
2c65ebfc7SToomas Soome  *
3*472cd20dSToomas Soome  * Copyright (c) 2003-2020 Apple Inc. All rights reserved.
4c65ebfc7SToomas Soome  *
5c65ebfc7SToomas Soome  * Redistribution and use in source and binary forms, with or without
6c65ebfc7SToomas Soome  * modification, are permitted provided that the following conditions are met:
7c65ebfc7SToomas Soome  *
8c65ebfc7SToomas Soome  * 1.  Redistributions of source code must retain the above copyright notice,
9c65ebfc7SToomas Soome  *     this list of conditions and the following disclaimer.
10c65ebfc7SToomas Soome  * 2.  Redistributions in binary form must reproduce the above copyright notice,
11c65ebfc7SToomas Soome  *     this list of conditions and the following disclaimer in the documentation
12c65ebfc7SToomas Soome  *     and/or other materials provided with the distribution.
13c65ebfc7SToomas Soome  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of its
14c65ebfc7SToomas Soome  *     contributors may be used to endorse or promote products derived from this
15c65ebfc7SToomas Soome  *     software without specific prior written permission.
16c65ebfc7SToomas Soome  *
17c65ebfc7SToomas Soome  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18c65ebfc7SToomas Soome  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19c65ebfc7SToomas Soome  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20c65ebfc7SToomas Soome  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21c65ebfc7SToomas Soome  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22c65ebfc7SToomas Soome  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23c65ebfc7SToomas Soome  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24c65ebfc7SToomas Soome  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25c65ebfc7SToomas Soome  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26c65ebfc7SToomas Soome  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27c65ebfc7SToomas Soome  */
28c65ebfc7SToomas Soome 
29c65ebfc7SToomas Soome #ifndef DNSSD_IPC_H
30c65ebfc7SToomas Soome #define DNSSD_IPC_H
31c65ebfc7SToomas Soome 
32c65ebfc7SToomas Soome #include "dns_sd.h"
33c65ebfc7SToomas Soome 
34c65ebfc7SToomas Soome //
35c65ebfc7SToomas Soome // Common cross platform services
36c65ebfc7SToomas Soome //
37c65ebfc7SToomas Soome #if defined(WIN32)
38c65ebfc7SToomas Soome #   include <winsock2.h>
39c65ebfc7SToomas Soome #   define dnssd_InvalidSocket  INVALID_SOCKET
40c65ebfc7SToomas Soome #   define dnssd_SocketValid(s) ((s) != INVALID_SOCKET)
41c65ebfc7SToomas Soome #   define dnssd_EWOULDBLOCK    WSAEWOULDBLOCK
42c65ebfc7SToomas Soome #   define dnssd_EINTR          WSAEINTR
43c65ebfc7SToomas Soome #   define dnssd_ECONNRESET     WSAECONNRESET
44c65ebfc7SToomas Soome #   define dnssd_socklen_t      int
45c65ebfc7SToomas Soome #   define dnssd_close(sock)    closesocket(sock)
46c65ebfc7SToomas Soome #   define dnssd_errno          WSAGetLastError()
47c65ebfc7SToomas Soome #   define dnssd_strerror(X)    win32_strerror(X)
48c65ebfc7SToomas Soome #   define ssize_t              int
49c65ebfc7SToomas Soome #   define getpid               _getpid
50c65ebfc7SToomas Soome #   define unlink               _unlink
51c65ebfc7SToomas Soome extern char *win32_strerror(int inErrorCode);
52c65ebfc7SToomas Soome #else
53c65ebfc7SToomas Soome #   include <sys/types.h>
54c65ebfc7SToomas Soome #   include <unistd.h>
55c65ebfc7SToomas Soome #   include <sys/un.h>
56c65ebfc7SToomas Soome #   include <string.h>
57c65ebfc7SToomas Soome #   include <stdio.h>
58c65ebfc7SToomas Soome #   include <stdlib.h>
59c65ebfc7SToomas Soome #   include <sys/stat.h>
60c65ebfc7SToomas Soome #   include <sys/socket.h>
61c65ebfc7SToomas Soome #   include <netinet/in.h>
62c65ebfc7SToomas Soome #   include <arpa/inet.h>
63c65ebfc7SToomas Soome #   define dnssd_InvalidSocket  -1
64c65ebfc7SToomas Soome #   define dnssd_SocketValid(s) ((s) >= 0)
65c65ebfc7SToomas Soome #   define dnssd_EWOULDBLOCK    EWOULDBLOCK
66c65ebfc7SToomas Soome #   define dnssd_EINTR          EINTR
67c65ebfc7SToomas Soome #   define dnssd_ECONNRESET     ECONNRESET
68c65ebfc7SToomas Soome #   define dnssd_EPIPE          EPIPE
69c65ebfc7SToomas Soome #   define dnssd_socklen_t      unsigned int
70c65ebfc7SToomas Soome #   define dnssd_close(sock)    close(sock)
71c65ebfc7SToomas Soome #   define dnssd_errno          errno
72c65ebfc7SToomas Soome #   define dnssd_strerror(X)    strerror(X)
73c65ebfc7SToomas Soome #endif
74c65ebfc7SToomas Soome 
75c65ebfc7SToomas Soome #if defined(USE_TCP_LOOPBACK)
76c65ebfc7SToomas Soome #   define AF_DNSSD             AF_INET
77c65ebfc7SToomas Soome #   define MDNS_TCP_SERVERADDR  "127.0.0.1"
78c65ebfc7SToomas Soome #   define MDNS_TCP_SERVERPORT  5354
79c65ebfc7SToomas Soome #   define LISTENQ              5
80c65ebfc7SToomas Soome #   define dnssd_sockaddr_t     struct sockaddr_in
81c65ebfc7SToomas Soome #else
82c65ebfc7SToomas Soome #   define AF_DNSSD             AF_LOCAL
83c65ebfc7SToomas Soome #   ifndef MDNS_UDS_SERVERPATH
84c65ebfc7SToomas Soome #       define MDNS_UDS_SERVERPATH  "/var/run/mDNSResponder"
85c65ebfc7SToomas Soome #   endif
86c65ebfc7SToomas Soome #   define MDNS_UDS_SERVERPATH_ENVVAR "DNSSD_UDS_PATH"
87c65ebfc7SToomas Soome #   define LISTENQ              100
88c65ebfc7SToomas Soome // longest legal control path length
89c65ebfc7SToomas Soome #   define MAX_CTLPATH          (sizeof(((struct sockaddr_un*)0)->sun_path))
90c65ebfc7SToomas Soome #   define dnssd_sockaddr_t     struct sockaddr_un
91c65ebfc7SToomas Soome #endif
92c65ebfc7SToomas Soome 
93c65ebfc7SToomas Soome // Compatibility workaround
94c65ebfc7SToomas Soome #ifndef AF_LOCAL
95c65ebfc7SToomas Soome #define AF_LOCAL    AF_UNIX
96c65ebfc7SToomas Soome #endif
97c65ebfc7SToomas Soome 
98c65ebfc7SToomas Soome // General UDS constants
99c65ebfc7SToomas Soome #define TXT_RECORD_INDEX ((uint32_t)(-1))   // record index for default text record
100c65ebfc7SToomas Soome 
101c65ebfc7SToomas Soome // IPC data encoding constants and types
102c65ebfc7SToomas Soome #define VERSION 1
103*472cd20dSToomas Soome #define IPC_FLAGS_NOREPLY       (1U << 0) // Set flag if no asynchronous replies are to be sent to client.
104*472cd20dSToomas Soome #define IPC_FLAGS_TRAILING_TLVS (1U << 1) // Set flag if TLVs follow the standard request data.
105*472cd20dSToomas Soome 
106*472cd20dSToomas Soome #define IPC_TLV_TYPE_RESOLVER_CONFIG_PLIST_DATA 1   // An nw_resolver_config as a binary property list.
107*472cd20dSToomas Soome #define IPC_TLV_TYPE_REQUIRE_PRIVACY            2   // A uint8. Non-zero means privacy is required, zero means not required.
108c65ebfc7SToomas Soome 
109c65ebfc7SToomas Soome // Structure packing macro. If we're not using GNUC, it's not fatal. Most compilers naturally pack the on-the-wire
110c65ebfc7SToomas Soome // structures correctly anyway, so a plain "struct" is usually fine. In the event that structures are not packed
111c65ebfc7SToomas Soome // correctly, our compile-time assertion checks will catch it and prevent inadvertent generation of non-working code.
112c65ebfc7SToomas Soome #ifndef packedstruct
113c65ebfc7SToomas Soome  #if ((__GNUC__ > 2) || ((__GNUC__ == 2) && (__GNUC_MINOR__ >= 9)))
114c65ebfc7SToomas Soome   #define packedstruct struct __attribute__((__packed__))
115c65ebfc7SToomas Soome   #define packedunion  union  __attribute__((__packed__))
116c65ebfc7SToomas Soome  #else
117c65ebfc7SToomas Soome   #define packedstruct struct
118c65ebfc7SToomas Soome   #define packedunion  union
119c65ebfc7SToomas Soome  #endif
120c65ebfc7SToomas Soome #endif
121c65ebfc7SToomas Soome 
122c65ebfc7SToomas Soome typedef enum
123c65ebfc7SToomas Soome {
124c65ebfc7SToomas Soome     request_op_none = 0,    // No request yet received on this connection
125c65ebfc7SToomas Soome     connection_request = 1, // connected socket via DNSServiceConnect()
126c65ebfc7SToomas Soome     reg_record_request,     // reg/remove record only valid for connected sockets
127c65ebfc7SToomas Soome     remove_record_request,
128c65ebfc7SToomas Soome     enumeration_request,
129c65ebfc7SToomas Soome     reg_service_request,
130c65ebfc7SToomas Soome     browse_request,
131c65ebfc7SToomas Soome     resolve_request,
132c65ebfc7SToomas Soome     query_request,
133c65ebfc7SToomas Soome     reconfirm_record_request,
134c65ebfc7SToomas Soome     add_record_request,
135c65ebfc7SToomas Soome     update_record_request,
136c65ebfc7SToomas Soome     setdomain_request,      // Up to here is in Tiger and B4W 1.0.3
137c65ebfc7SToomas Soome     getproperty_request,    // New in B4W 1.0.4
138c65ebfc7SToomas Soome     port_mapping_request,   // New in Leopard and B4W 2.0
139c65ebfc7SToomas Soome     addrinfo_request,
140c65ebfc7SToomas Soome     send_bpf,               // New in SL
141c65ebfc7SToomas Soome     getpid_request,
142c65ebfc7SToomas Soome     release_request,
143c65ebfc7SToomas Soome     connection_delegate_request,
144c65ebfc7SToomas Soome 
145c65ebfc7SToomas Soome     cancel_request = 63
146c65ebfc7SToomas Soome } request_op_t;
147c65ebfc7SToomas Soome 
148c65ebfc7SToomas Soome typedef enum
149c65ebfc7SToomas Soome {
150c65ebfc7SToomas Soome     enumeration_reply_op = 64,
151c65ebfc7SToomas Soome     reg_service_reply_op,
152c65ebfc7SToomas Soome     browse_reply_op,
153c65ebfc7SToomas Soome     resolve_reply_op,
154c65ebfc7SToomas Soome     query_reply_op,
155c65ebfc7SToomas Soome     reg_record_reply_op,    // Up to here is in Tiger and B4W 1.0.3
156c65ebfc7SToomas Soome     getproperty_reply_op,   // New in B4W 1.0.4
157c65ebfc7SToomas Soome     port_mapping_reply_op,  // New in Leopard and B4W 2.0
158c65ebfc7SToomas Soome     addrinfo_reply_op
159c65ebfc7SToomas Soome } reply_op_t;
160c65ebfc7SToomas Soome 
161c65ebfc7SToomas Soome #if defined(_WIN64)
162c65ebfc7SToomas Soome #   pragma pack(push,4)
163c65ebfc7SToomas Soome #elif !defined(__GNUC__)
164c65ebfc7SToomas Soome #   pragma pack(1)
165c65ebfc7SToomas Soome #endif
166c65ebfc7SToomas Soome 
167c65ebfc7SToomas Soome // Define context object big enough to hold a 64-bit pointer,
168c65ebfc7SToomas Soome // to accomodate 64-bit clients communicating with 32-bit daemon.
169c65ebfc7SToomas Soome // There's no reason for the daemon to ever be a 64-bit process, but its clients might be
170c65ebfc7SToomas Soome typedef packedunion
171c65ebfc7SToomas Soome {
172c65ebfc7SToomas Soome     void *context;
173c65ebfc7SToomas Soome     uint32_t u32[2];
174c65ebfc7SToomas Soome } client_context_t;
175c65ebfc7SToomas Soome 
176c65ebfc7SToomas Soome typedef packedstruct
177c65ebfc7SToomas Soome {
178c65ebfc7SToomas Soome     uint32_t version;
179c65ebfc7SToomas Soome     uint32_t datalen;
180c65ebfc7SToomas Soome     uint32_t ipc_flags;
181c65ebfc7SToomas Soome     uint32_t op;        // request_op_t or reply_op_t
182c65ebfc7SToomas Soome     client_context_t client_context; // context passed from client, returned by server in corresponding reply
183c65ebfc7SToomas Soome     uint32_t reg_index;            // identifier for a record registered via DNSServiceRegisterRecord() on a
184c65ebfc7SToomas Soome     // socket connected by DNSServiceCreateConnection().  Must be unique in the scope of the connection, such that and
185c65ebfc7SToomas Soome     // index/socket pair uniquely identifies a record.  (Used to select records for removal by DNSServiceRemoveRecord())
186c65ebfc7SToomas Soome } ipc_msg_hdr;
187c65ebfc7SToomas Soome 
188c65ebfc7SToomas Soome #if defined(_WIN64)
189c65ebfc7SToomas Soome #   pragma pack(pop)
190c65ebfc7SToomas Soome #elif !defined(__GNUC__)
191c65ebfc7SToomas Soome #   pragma pack()
192c65ebfc7SToomas Soome #endif
193c65ebfc7SToomas Soome 
194c65ebfc7SToomas Soome // routines to write to and extract data from message buffers.
195c65ebfc7SToomas Soome // caller responsible for bounds checking.
196c65ebfc7SToomas Soome // ptr is the address of the pointer to the start of the field.
197c65ebfc7SToomas Soome // it is advanced to point to the next field, or the end of the message
198c65ebfc7SToomas Soome 
199c65ebfc7SToomas Soome void put_uint32(const uint32_t l, char **ptr);
200c65ebfc7SToomas Soome uint32_t get_uint32(const char **ptr, const char *end);
201c65ebfc7SToomas Soome 
202c65ebfc7SToomas Soome void put_uint16(uint16_t s, char **ptr);
203c65ebfc7SToomas Soome uint16_t get_uint16(const char **ptr, const char *end);
204c65ebfc7SToomas Soome 
205c65ebfc7SToomas Soome #define put_flags put_uint32
206c65ebfc7SToomas Soome #define get_flags get_uint32
207c65ebfc7SToomas Soome 
208c65ebfc7SToomas Soome #define put_error_code put_uint32
209c65ebfc7SToomas Soome #define get_error_code get_uint32
210c65ebfc7SToomas Soome 
211c65ebfc7SToomas Soome int put_string(const char *str, char **ptr);
212c65ebfc7SToomas Soome int get_string(const char **ptr, const char *const end, char *buffer, int buflen);
213c65ebfc7SToomas Soome 
214c65ebfc7SToomas Soome void put_rdata(const int rdlen, const unsigned char *rdata, char **ptr);
215c65ebfc7SToomas Soome const char *get_rdata(const char **ptr, const char *end, int rdlen);  // return value is rdata pointed to by *ptr -
216c65ebfc7SToomas Soome // rdata is not copied from buffer.
217c65ebfc7SToomas Soome 
218*472cd20dSToomas Soome #if APPLE_OSX_mDNSResponder
219*472cd20dSToomas Soome size_t get_required_tlv16_length(const uint16_t valuelen);
220*472cd20dSToomas Soome void put_tlv16(const uint16_t type, const uint16_t length, const uint8_t *value, char **ptr);
221*472cd20dSToomas Soome #endif
222*472cd20dSToomas Soome 
223c65ebfc7SToomas Soome void ConvertHeaderBytes(ipc_msg_hdr *hdr);
224c65ebfc7SToomas Soome 
225c65ebfc7SToomas Soome struct CompileTimeAssertionChecks_dnssd_ipc
226c65ebfc7SToomas Soome {
227c65ebfc7SToomas Soome     // Check that the compiler generated our on-the-wire packet format structure definitions
228c65ebfc7SToomas Soome     // properly packed, without adding padding bytes to align fields on 32-bit or 64-bit boundaries.
229c65ebfc7SToomas Soome     char assert0[(sizeof(client_context_t) ==  8) ? 1 : -1];
230c65ebfc7SToomas Soome     char assert1[(sizeof(ipc_msg_hdr)      == 28) ? 1 : -1];
231c65ebfc7SToomas Soome };
232c65ebfc7SToomas Soome 
233c65ebfc7SToomas Soome #endif // DNSSD_IPC_H
234