]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSShared/dnssd_ipc.c
mDNSResponder-1096.0.2.tar.gz
[apple/mdnsresponder.git] / mDNSShared / dnssd_ipc.c
1 /*
2 * Copyright (c) 2003-2019 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
30 #if defined(_WIN32)
31
32 char *win32_strerror(int inErrorCode)
33 {
34 static char buffer[1024];
35 DWORD n;
36 memset(buffer, 0, sizeof(buffer));
37 n = FormatMessageA(
38 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
39 NULL,
40 (DWORD) inErrorCode,
41 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
42 buffer,
43 sizeof(buffer),
44 NULL);
45 if (n > 0)
46 {
47 // Remove any trailing CR's or LF's since some messages have them.
48 while ((n > 0) && isspace(((unsigned char *) buffer)[n - 1]))
49 buffer[--n] = '\0';
50 }
51 return buffer;
52 }
53
54 #endif
55
56 void put_uint32(const uint32_t l, char **ptr)
57 {
58 (*ptr)[0] = (char)((l >> 24) & 0xFF);
59 (*ptr)[1] = (char)((l >> 16) & 0xFF);
60 (*ptr)[2] = (char)((l >> 8) & 0xFF);
61 (*ptr)[3] = (char)((l ) & 0xFF);
62 *ptr += sizeof(uint32_t);
63 }
64
65 uint32_t get_uint32(const char **ptr, const char *end)
66 {
67 if (!*ptr || *ptr + sizeof(uint32_t) > end)
68 {
69 *ptr = NULL;
70 return(0);
71 }
72 else
73 {
74 uint8_t *p = (uint8_t*) *ptr;
75 *ptr += sizeof(uint32_t);
76 return((uint32_t) ((uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 | p[3]));
77 }
78 }
79
80 void put_uint16(uint16_t s, char **ptr)
81 {
82 (*ptr)[0] = (char)((s >> 8) & 0xFF);
83 (*ptr)[1] = (char)((s ) & 0xFF);
84 *ptr += sizeof(uint16_t);
85 }
86
87 uint16_t get_uint16(const char **ptr, const char *end)
88 {
89 if (!*ptr || *ptr + sizeof(uint16_t) > end)
90 {
91 *ptr = NULL;
92 return(0);
93 }
94 else
95 {
96 uint8_t *p = (uint8_t*) *ptr;
97 *ptr += sizeof(uint16_t);
98 return((uint16_t) ((uint16_t)p[0] << 8 | p[1]));
99 }
100 }
101
102 int put_string(const char *str, char **ptr)
103 {
104 size_t len;
105 if (!str) str = "";
106 len = strlen(str) + 1;
107 memcpy(*ptr, str, len);
108 *ptr += len;
109 return 0;
110 }
111
112 int get_string(const char **ptr, const char *const end, char *buffer, int buflen)
113 {
114 if (!*ptr)
115 {
116 *buffer = 0;
117 return(-1);
118 }
119 else
120 {
121 char *lim = buffer + buflen; // Calculate limit
122 while (*ptr < end && buffer < lim)
123 {
124 char c = *buffer++ = *(*ptr)++;
125 if (c == 0) return(0); // Success
126 }
127 if (buffer == lim) buffer--;
128 *buffer = 0; // Failed, so terminate string,
129 *ptr = NULL; // clear pointer,
130 return(-1); // and return failure indication
131 }
132 }
133
134 void put_rdata(const int rdlen, const unsigned char *rdata, char **ptr)
135 {
136 memcpy(*ptr, rdata, rdlen);
137 *ptr += rdlen;
138 }
139
140 const char *get_rdata(const char **ptr, const char *end, int rdlen)
141 {
142 if (!*ptr || *ptr + rdlen > end)
143 {
144 *ptr = NULL;
145 return(0);
146 }
147 else
148 {
149 const char *rd = *ptr;
150 *ptr += rdlen;
151 return rd;
152 }
153 }
154
155 void ConvertHeaderBytes(ipc_msg_hdr *hdr)
156 {
157 hdr->version = htonl(hdr->version);
158 hdr->datalen = htonl(hdr->datalen);
159 hdr->ipc_flags = htonl(hdr->ipc_flags);
160 hdr->op = htonl(hdr->op );
161 hdr->reg_index = htonl(hdr->reg_index);
162 }