xref: /illumos-gate/usr/src/contrib/mDNSResponder/mDNSShared/dnssd_ipc.c (revision 472cd20d26008f77084ade4c2048159b98c2b705)
1 /*
2  * Copyright (c) 2003-2020 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1.  Redistributions of source code must retain the above copyright notice,
8  *     this list of conditions and the following disclaimer.
9  * 2.  Redistributions in binary form must reproduce the above copyright notice,
10  *     this list of conditions and the following disclaimer in the documentation
11  *     and/or other materials provided with the distribution.
12  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of its
13  *     contributors may be used to endorse or promote products derived from this
14  *     software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "dnssd_ipc.h"
29 #if APPLE_OSX_mDNSResponder
30 #include "mdns_tlv.h"
31 #endif
32 
33 #if defined(_WIN32)
34 
win32_strerror(int inErrorCode)35 char *win32_strerror(int inErrorCode)
36 {
37     static char buffer[1024];
38     DWORD n;
39     memset(buffer, 0, sizeof(buffer));
40     n = FormatMessageA(
41         FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
42         NULL,
43         (DWORD) inErrorCode,
44         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
45         buffer,
46         sizeof(buffer),
47         NULL);
48     if (n > 0)
49     {
50         // Remove any trailing CR's or LF's since some messages have them.
51         while ((n > 0) && isspace(((unsigned char *) buffer)[n - 1]))
52             buffer[--n] = '\0';
53     }
54     return buffer;
55 }
56 
57 #endif
58 
put_uint32(const uint32_t l,char ** ptr)59 void put_uint32(const uint32_t l, char **ptr)
60 {
61     (*ptr)[0] = (char)((l >> 24) &  0xFF);
62     (*ptr)[1] = (char)((l >> 16) &  0xFF);
63     (*ptr)[2] = (char)((l >>  8) &  0xFF);
64     (*ptr)[3] = (char)((l      ) &  0xFF);
65     *ptr += sizeof(uint32_t);
66 }
67 
get_uint32(const char ** ptr,const char * end)68 uint32_t get_uint32(const char **ptr, const char *end)
69 {
70     if (!*ptr || *ptr + sizeof(uint32_t) > end)
71     {
72         *ptr = NULL;
73         return(0);
74     }
75     else
76     {
77         uint8_t *p = (uint8_t*) *ptr;
78         *ptr += sizeof(uint32_t);
79         return((uint32_t) ((uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 | p[3]));
80     }
81 }
82 
put_uint16(uint16_t s,char ** ptr)83 void put_uint16(uint16_t s, char **ptr)
84 {
85     (*ptr)[0] = (char)((s >>  8) &  0xFF);
86     (*ptr)[1] = (char)((s      ) &  0xFF);
87     *ptr += sizeof(uint16_t);
88 }
89 
get_uint16(const char ** ptr,const char * end)90 uint16_t get_uint16(const char **ptr, const char *end)
91 {
92     if (!*ptr || *ptr + sizeof(uint16_t) > end)
93     {
94         *ptr = NULL;
95         return(0);
96     }
97     else
98     {
99         uint8_t *p = (uint8_t*) *ptr;
100         *ptr += sizeof(uint16_t);
101         return((uint16_t) ((uint16_t)p[0] << 8 | p[1]));
102     }
103 }
104 
put_string(const char * str,char ** ptr)105 int put_string(const char *str, char **ptr)
106 {
107     size_t len;
108     if (!str) str = "";
109     len = strlen(str) + 1;
110     memcpy(*ptr, str, len);
111     *ptr += len;
112     return 0;
113 }
114 
get_string(const char ** ptr,const char * const end,char * buffer,int buflen)115 int get_string(const char **ptr, const char *const end, char *buffer, int buflen)
116 {
117     if (!*ptr)
118     {
119         *buffer = 0;
120         return(-1);
121     }
122     else
123     {
124         char *lim = buffer + buflen;    // Calculate limit
125         while (*ptr < end && buffer < lim)
126         {
127             char c = *buffer++ = *(*ptr)++;
128             if (c == 0) return(0);      // Success
129         }
130         if (buffer == lim) buffer--;
131         *buffer = 0;                    // Failed, so terminate string,
132         *ptr = NULL;                    // clear pointer,
133         return(-1);                     // and return failure indication
134     }
135 }
136 
put_rdata(const int rdlen,const unsigned char * rdata,char ** ptr)137 void put_rdata(const int rdlen, const unsigned char *rdata, char **ptr)
138 {
139     memcpy(*ptr, rdata, rdlen);
140     *ptr += rdlen;
141 }
142 
get_rdata(const char ** ptr,const char * end,int rdlen)143 const char *get_rdata(const char **ptr, const char *end, int rdlen)
144 {
145     if (!*ptr || *ptr + rdlen > end)
146     {
147         *ptr = NULL;
148         return(0);
149     }
150     else
151     {
152         const char *rd = *ptr;
153         *ptr += rdlen;
154         return rd;
155     }
156 }
157 
158 #if APPLE_OSX_mDNSResponder
get_required_tlv16_length(const uint16_t valuelen)159 size_t get_required_tlv16_length(const uint16_t valuelen)
160 {
161     return mdns_tlv16_get_required_length(valuelen);
162 }
163 
put_tlv16(const uint16_t type,const uint16_t length,const uint8_t * value,char ** ptr)164 void put_tlv16(const uint16_t type, const uint16_t length, const uint8_t *value, char **ptr)
165 {
166 	uint8_t *dst = (uint8_t *)*ptr;
167 	mdns_tlv16_set(dst, NULL, type, length, value, &dst);
168     *ptr = (char *)dst;
169 }
170 #endif
171 
ConvertHeaderBytes(ipc_msg_hdr * hdr)172 void ConvertHeaderBytes(ipc_msg_hdr *hdr)
173 {
174     hdr->version   = htonl(hdr->version);
175     hdr->datalen   = htonl(hdr->datalen);
176     hdr->ipc_flags = htonl(hdr->ipc_flags);
177     hdr->op        = htonl(hdr->op );
178     hdr->reg_index = htonl(hdr->reg_index);
179 }
180