1*472cd20dSToomas Soome /*
2*472cd20dSToomas Soome  * Copyright (c) 2003-2020 Apple Inc. All rights reserved.
34b22b933Srs  *
45ffb0c9bSToomas Soome  * Redistribution and use in source and binary forms, with or without
54b22b933Srs  * modification, are permitted provided that the following conditions are met:
64b22b933Srs  *
75ffb0c9bSToomas Soome  * 1.  Redistributions of source code must retain the above copyright notice,
85ffb0c9bSToomas Soome  *     this list of conditions and the following disclaimer.
95ffb0c9bSToomas Soome  * 2.  Redistributions in binary form must reproduce the above copyright notice,
105ffb0c9bSToomas Soome  *     this list of conditions and the following disclaimer in the documentation
115ffb0c9bSToomas Soome  *     and/or other materials provided with the distribution.
12cda73f64SToomas Soome  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of its
135ffb0c9bSToomas Soome  *     contributors may be used to endorse or promote products derived from this
145ffb0c9bSToomas Soome  *     software without specific prior written permission.
154b22b933Srs  *
165ffb0c9bSToomas Soome  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
175ffb0c9bSToomas Soome  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
185ffb0c9bSToomas Soome  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
195ffb0c9bSToomas Soome  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
205ffb0c9bSToomas Soome  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
215ffb0c9bSToomas Soome  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
225ffb0c9bSToomas Soome  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
235ffb0c9bSToomas Soome  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
245ffb0c9bSToomas Soome  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
254b22b933Srs  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
264b22b933Srs  */
274b22b933Srs 
284b22b933Srs #include "dnssd_ipc.h"
29*472cd20dSToomas Soome #if APPLE_OSX_mDNSResponder
30*472cd20dSToomas Soome #include "mdns_tlv.h"
31*472cd20dSToomas Soome #endif
324b22b933Srs 
335ffb0c9bSToomas Soome #if defined(_WIN32)
345ffb0c9bSToomas Soome 
win32_strerror(int inErrorCode)355ffb0c9bSToomas Soome char *win32_strerror(int inErrorCode)
365ffb0c9bSToomas Soome {
375ffb0c9bSToomas Soome     static char buffer[1024];
385ffb0c9bSToomas Soome     DWORD n;
395ffb0c9bSToomas Soome     memset(buffer, 0, sizeof(buffer));
405ffb0c9bSToomas Soome     n = FormatMessageA(
415ffb0c9bSToomas Soome         FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
425ffb0c9bSToomas Soome         NULL,
435ffb0c9bSToomas Soome         (DWORD) inErrorCode,
445ffb0c9bSToomas Soome         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
455ffb0c9bSToomas Soome         buffer,
465ffb0c9bSToomas Soome         sizeof(buffer),
475ffb0c9bSToomas Soome         NULL);
485ffb0c9bSToomas Soome     if (n > 0)
495ffb0c9bSToomas Soome     {
505ffb0c9bSToomas Soome         // Remove any trailing CR's or LF's since some messages have them.
515ffb0c9bSToomas Soome         while ((n > 0) && isspace(((unsigned char *) buffer)[n - 1]))
525ffb0c9bSToomas Soome             buffer[--n] = '\0';
535ffb0c9bSToomas Soome     }
545ffb0c9bSToomas Soome     return buffer;
555ffb0c9bSToomas Soome }
565ffb0c9bSToomas Soome 
575ffb0c9bSToomas Soome #endif
585ffb0c9bSToomas Soome 
put_uint32(const uint32_t l,char ** ptr)595ffb0c9bSToomas Soome void put_uint32(const uint32_t l, char **ptr)
605ffb0c9bSToomas Soome {
615ffb0c9bSToomas Soome     (*ptr)[0] = (char)((l >> 24) &  0xFF);
625ffb0c9bSToomas Soome     (*ptr)[1] = (char)((l >> 16) &  0xFF);
635ffb0c9bSToomas Soome     (*ptr)[2] = (char)((l >>  8) &  0xFF);
645ffb0c9bSToomas Soome     (*ptr)[3] = (char)((l      ) &  0xFF);
655ffb0c9bSToomas Soome     *ptr += sizeof(uint32_t);
665ffb0c9bSToomas Soome }
675ffb0c9bSToomas Soome 
get_uint32(const char ** ptr,const char * end)685ffb0c9bSToomas Soome uint32_t get_uint32(const char **ptr, const char *end)
695ffb0c9bSToomas Soome {
705ffb0c9bSToomas Soome     if (!*ptr || *ptr + sizeof(uint32_t) > end)
715ffb0c9bSToomas Soome     {
725ffb0c9bSToomas Soome         *ptr = NULL;
735ffb0c9bSToomas Soome         return(0);
745ffb0c9bSToomas Soome     }
755ffb0c9bSToomas Soome     else
765ffb0c9bSToomas Soome     {
775ffb0c9bSToomas Soome         uint8_t *p = (uint8_t*) *ptr;
785ffb0c9bSToomas Soome         *ptr += sizeof(uint32_t);
795ffb0c9bSToomas Soome         return((uint32_t) ((uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 | p[3]));
805ffb0c9bSToomas Soome     }
815ffb0c9bSToomas Soome }
825ffb0c9bSToomas Soome 
put_uint16(uint16_t s,char ** ptr)835ffb0c9bSToomas Soome void put_uint16(uint16_t s, char **ptr)
845ffb0c9bSToomas Soome {
855ffb0c9bSToomas Soome     (*ptr)[0] = (char)((s >>  8) &  0xFF);
865ffb0c9bSToomas Soome     (*ptr)[1] = (char)((s      ) &  0xFF);
875ffb0c9bSToomas Soome     *ptr += sizeof(uint16_t);
885ffb0c9bSToomas Soome }
895ffb0c9bSToomas Soome 
get_uint16(const char ** ptr,const char * end)905ffb0c9bSToomas Soome uint16_t get_uint16(const char **ptr, const char *end)
915ffb0c9bSToomas Soome {
925ffb0c9bSToomas Soome     if (!*ptr || *ptr + sizeof(uint16_t) > end)
935ffb0c9bSToomas Soome     {
945ffb0c9bSToomas Soome         *ptr = NULL;
955ffb0c9bSToomas Soome         return(0);
965ffb0c9bSToomas Soome     }
975ffb0c9bSToomas Soome     else
985ffb0c9bSToomas Soome     {
995ffb0c9bSToomas Soome         uint8_t *p = (uint8_t*) *ptr;
1005ffb0c9bSToomas Soome         *ptr += sizeof(uint16_t);
1015ffb0c9bSToomas Soome         return((uint16_t) ((uint16_t)p[0] << 8 | p[1]));
1025ffb0c9bSToomas Soome     }
1035ffb0c9bSToomas Soome }
1044b22b933Srs 
put_string(const char * str,char ** ptr)1054b22b933Srs int put_string(const char *str, char **ptr)
1065ffb0c9bSToomas Soome {
107*472cd20dSToomas Soome     size_t len;
1085ffb0c9bSToomas Soome     if (!str) str = "";
109*472cd20dSToomas Soome     len = strlen(str) + 1;
110*472cd20dSToomas Soome     memcpy(*ptr, str, len);
111*472cd20dSToomas Soome     *ptr += len;
1125ffb0c9bSToomas Soome     return 0;
1135ffb0c9bSToomas Soome }
1145ffb0c9bSToomas Soome 
get_string(const char ** ptr,const char * const end,char * buffer,int buflen)1155ffb0c9bSToomas Soome int get_string(const char **ptr, const char *const end, char *buffer, int buflen)
1165ffb0c9bSToomas Soome {
1175ffb0c9bSToomas Soome     if (!*ptr)
1185ffb0c9bSToomas Soome     {
1195ffb0c9bSToomas Soome         *buffer = 0;
1205ffb0c9bSToomas Soome         return(-1);
1215ffb0c9bSToomas Soome     }
1225ffb0c9bSToomas Soome     else
1235ffb0c9bSToomas Soome     {
1245ffb0c9bSToomas Soome         char *lim = buffer + buflen;    // Calculate limit
1255ffb0c9bSToomas Soome         while (*ptr < end && buffer < lim)
1265ffb0c9bSToomas Soome         {
1275ffb0c9bSToomas Soome             char c = *buffer++ = *(*ptr)++;
1285ffb0c9bSToomas Soome             if (c == 0) return(0);      // Success
1295ffb0c9bSToomas Soome         }
1305ffb0c9bSToomas Soome         if (buffer == lim) buffer--;
1315ffb0c9bSToomas Soome         *buffer = 0;                    // Failed, so terminate string,
1325ffb0c9bSToomas Soome         *ptr = NULL;                    // clear pointer,
1335ffb0c9bSToomas Soome         return(-1);                     // and return failure indication
1345ffb0c9bSToomas Soome     }
1355ffb0c9bSToomas Soome }
1364b22b933Srs 
put_rdata(const int rdlen,const unsigned char * rdata,char ** ptr)1374b22b933Srs void put_rdata(const int rdlen, const unsigned char *rdata, char **ptr)
1385ffb0c9bSToomas Soome {
1395ffb0c9bSToomas Soome     memcpy(*ptr, rdata, rdlen);
1405ffb0c9bSToomas Soome     *ptr += rdlen;
1415ffb0c9bSToomas Soome }
1425ffb0c9bSToomas Soome 
get_rdata(const char ** ptr,const char * end,int rdlen)1435ffb0c9bSToomas Soome const char *get_rdata(const char **ptr, const char *end, int rdlen)
1445ffb0c9bSToomas Soome {
1455ffb0c9bSToomas Soome     if (!*ptr || *ptr + rdlen > end)
1465ffb0c9bSToomas Soome     {
1475ffb0c9bSToomas Soome         *ptr = NULL;
1485ffb0c9bSToomas Soome         return(0);
1495ffb0c9bSToomas Soome     }
1505ffb0c9bSToomas Soome     else
1515ffb0c9bSToomas Soome     {
1525ffb0c9bSToomas Soome         const char *rd = *ptr;
1535ffb0c9bSToomas Soome         *ptr += rdlen;
1545ffb0c9bSToomas Soome         return rd;
1555ffb0c9bSToomas Soome     }
1565ffb0c9bSToomas Soome }
1574b22b933Srs 
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)1724b22b933Srs void ConvertHeaderBytes(ipc_msg_hdr *hdr)
1735ffb0c9bSToomas Soome {
1745ffb0c9bSToomas Soome     hdr->version   = htonl(hdr->version);
1755ffb0c9bSToomas Soome     hdr->datalen   = htonl(hdr->datalen);
1765ffb0c9bSToomas Soome     hdr->ipc_flags = htonl(hdr->ipc_flags);
1775ffb0c9bSToomas Soome     hdr->op        = htonl(hdr->op );
1785ffb0c9bSToomas Soome     hdr->reg_index = htonl(hdr->reg_index);
1795ffb0c9bSToomas Soome }
180