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