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