Debian Codegraph @

interfaces/zeroconf/embed/dnssd_clientlib.c

raw symbols SCIP
1 /* -*- Mode: C; tab-width: 4 -*-
2 *
3 * Copyright (c) 2004, Apple Computer, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "dns_sd_types.h"
33
34 #if MDNS_BUILDINGSHAREDLIBRARY || MDNS_BUILDINGSTUBLIBRARY
35 //#pragma export on
36 #endif
37
38 #if defined(_WIN32)
39 // disable warning "conversion from <data> to uint16_t"
40 #pragma warning(disable:4244)
41 #define strncasecmp _strnicmp
42 #define strcasecmp _stricmp
43 #endif
44
45 /*********************************************************************************************
46 *
47 * Supporting Functions
48 *
49 *********************************************************************************************/
50 namespace ZeroConf { namespace embeddedLib {
51
52 #include "dns_sd_funct.h"
53
54 #define mDNSIsDigit(X) ((X) >= '0' && (X) <= '9')
55
56 // DomainEndsInDot returns 1 if name ends with a dot, 0 otherwise
57 // (DNSServiceConstructFullName depends this returning 1 for true, rather than any non-zero value meaning true)
58
59 static int DomainEndsInDot(const char *dom)
60 {
61 while (dom[0] && dom[1])
62 {
63 if (dom[0] == '\\') // advance past escaped byte sequence
64 {
65 if (mDNSIsDigit(dom[1]) && mDNSIsDigit(dom[2]) && mDNSIsDigit(dom[3]))
66 dom += 4; // If "\ddd" then skip four
67 else dom += 2; // else if "\x" then skip two
68 }
69 else dom++; // else goto next character
70 }
71 return (dom[0] == '.');
72 }
73
74 static uint8_t *InternalTXTRecordSearch
75 (
76 uint16_t txtLen,
77 const void *txtRecord,
78 const char *key,
79 unsigned long *keylen
80 )
81 {
82 uint8_t *p = (uint8_t*)txtRecord;
83 uint8_t *e = p + txtLen;
84 *keylen = (unsigned long) strlen(key);
85 while (p<e)
86 {
87 uint8_t *x = p;
88 p += 1 + p[0];
89 if (p <= e && *keylen <= x[0] && !strncasecmp(key, (char*)x+1, *keylen))
90 if (*keylen == x[0] || x[1+*keylen] == '=') return(x);
91 }
92 return(NULL);
93 }
94
95 /*********************************************************************************************
96 *
97 * General Utility Functions
98 *
99 *********************************************************************************************/
100
101 // Note: Need to make sure we don't write more than kDNSServiceMaxDomainName (1009) bytes to fullName
102 // In earlier builds this constant was defined to be 1005, so to avoid buffer overruns on clients
103 // compiled with that constant we'll actually limit the output to 1005 bytes.
104
105 DNSServiceErrorType DNSSD_API DNSServiceConstructFullName
106 (
107 char *const fullName,
108 const char *const service, // May be NULL
109 const char *const regtype,
110 const char *const domain
111 )
112 {
113 const size_t len = !regtype ? 0 : strlen(regtype) - DomainEndsInDot(regtype);
114 char *fn = fullName;
115 char *const lim = fullName + 1005;
116 const char *s = service;
117 const char *r = regtype;
118 const char *d = domain;
119
120 // regtype must be at least "x._udp" or "x._tcp"
121 if (len < 6 || !domain || !domain[0]) return kDNSServiceErr_BadParam;
122 if (strncasecmp((regtype + len - 4), "_tcp", 4) && strncasecmp((regtype + len - 4), "_udp", 4)) return kDNSServiceErr_BadParam;
123
124 if (service && *service)
125 {
126 while (*s)
127 {
128 unsigned char c = *s++; // Needs to be unsigned, or values like 0xFF will be interpreted as < 32
129 if (c <= ' ') // Escape non-printable characters
130 {
131 if (fn+4 >= lim) goto fail;
132 *fn++ = '\\';
133 *fn++ = '0' + (c / 100);
134 *fn++ = '0' + (c / 10) % 10;
135 c = '0' + (c ) % 10;
136 }
137 else if (c == '.' || (c == '\\')) // Escape dot and backslash literals
138 {
139 if (fn+2 >= lim) goto fail;
140 *fn++ = '\\';
141 }
142 else
143 if (fn+1 >= lim) goto fail;
144 *fn++ = (char)c;
145 }
146 *fn++ = '.';
147 }
148
149 while (*r) if (fn+1 >= lim) goto fail; else *fn++ = *r++;
150 if (!DomainEndsInDot(regtype)) { if (fn+1 >= lim) goto fail; else *fn++ = '.'; }
151
152 while (*d) if (fn+1 >= lim) goto fail; else *fn++ = *d++;
153 if (!DomainEndsInDot(domain)) { if (fn+1 >= lim) goto fail; else *fn++ = '.'; }
154
155 *fn = '\0';
156 return kDNSServiceErr_NoError;
157
158 fail:
159 *fn = '\0';
160 return kDNSServiceErr_BadParam;
161 }
162
163 /*********************************************************************************************
164 *
165 * TXT Record Construction Functions
166 *
167 *********************************************************************************************/
168
169 typedef struct _TXTRecordRefRealType
170 {
171 uint8_t *buffer; // Pointer to data
172 uint16_t buflen; // Length of buffer
173 uint16_t datalen; // Length currently in use
174 uint16_t malloced; // Non-zero if buffer was allocated via malloc()
175 } TXTRecordRefRealType;
176
177 #define txtRec ((TXTRecordRefRealType*)txtRecord)
178
179 // The opaque storage defined in the public dns_sd.h header is 16 bytes;
180 // make sure we don't exceed that.
181 struct CompileTimeAssertionCheck_dnssd_clientlib
182 {
183 char assert0[(sizeof(TXTRecordRefRealType) <= 16) ? 1 : -1];
184 };
185
186 void DNSSD_API TXTRecordCreate
187 (
188 TXTRecordRef *txtRecord,
189 uint16_t bufferLen,
190 void *buffer
191 )
192 {
193 txtRec->buffer = static_cast<uint8_t *>(buffer);
194 txtRec->buflen = buffer ? bufferLen : (uint16_t)0;
195 txtRec->datalen = 0;
196 txtRec->malloced = 0;
197 }
198
199 void DNSSD_API TXTRecordDeallocate(TXTRecordRef *txtRecord)
200 {
201 if (txtRec->malloced) free(txtRec->buffer);
202 }
203
204 DNSServiceErrorType DNSSD_API TXTRecordSetValue
205 (
206 TXTRecordRef *txtRecord,
207 const char *key,
208 uint8_t valueSize,
209 const void *value
210 )
211 {
212 uint8_t *start, *p;
213 const char *k;
214 unsigned long keysize, keyvalsize;
215
216 for (k = key; *k; k++) if (*k < 0x20 || *k > 0x7E || *k == '=') return(kDNSServiceErr_Invalid);
217 keysize = (unsigned long)(k - key);
218 keyvalsize = 1 + keysize + (value ? (1 + valueSize) : 0);
219 if (keysize < 1 || keyvalsize > 255) return(kDNSServiceErr_Invalid);
220 (void)TXTRecordRemoveValue(txtRecord, key);
221 if (txtRec->datalen + keyvalsize > txtRec->buflen)
222 {
223 unsigned char *newbuf;
224 unsigned long newlen = txtRec->datalen + keyvalsize;
225 if (newlen > 0xFFFF) return(kDNSServiceErr_Invalid);
226 newbuf = static_cast<unsigned char *>(malloc((size_t)newlen));
227 if (!newbuf) return(kDNSServiceErr_NoMemory);
228 memcpy(newbuf, txtRec->buffer, txtRec->datalen);
229 if (txtRec->malloced) free(txtRec->buffer);
230 txtRec->buffer = newbuf;
231 txtRec->buflen = (uint16_t)(newlen);
232 txtRec->malloced = 1;
233 }
234 start = txtRec->buffer + txtRec->datalen;
235 p = start + 1;
236 memcpy(p, key, keysize);
237 p += keysize;
238 if (value)
239 {
240 *p++ = '=';
241 memcpy(p, value, valueSize);
242 p += valueSize;
243 }
244 *start = (uint8_t)(p - start - 1);
245 txtRec->datalen += p - start;
246 return(kDNSServiceErr_NoError);
247 }
248
249 DNSServiceErrorType DNSSD_API TXTRecordRemoveValue
250 (
251 TXTRecordRef *txtRecord,
252 const char *key
253 )
254 {
255 unsigned long keylen, itemlen, remainder;
256 uint8_t *item = InternalTXTRecordSearch(txtRec->datalen, txtRec->buffer, key, &keylen);
257 if (!item) return(kDNSServiceErr_NoSuchKey);
258 itemlen = (unsigned long)(1 + item[0]);
259 remainder = (unsigned long)((txtRec->buffer + txtRec->datalen) - (item + itemlen));
260 // Use memmove because memcpy behaviour is undefined for overlapping regions
261 memmove(item, item + itemlen, remainder);
262 txtRec->datalen -= itemlen;
263 return(kDNSServiceErr_NoError);
264 }
265
266 uint16_t DNSSD_API TXTRecordGetLength (const TXTRecordRef *txtRecord) { return(txtRec->datalen); }
267 const void * DNSSD_API TXTRecordGetBytesPtr(const TXTRecordRef *txtRecord) { return(txtRec->buffer); }
268
269 /*********************************************************************************************
270 *
271 * TXT Record Parsing Functions
272 *
273 *********************************************************************************************/
274
275 int DNSSD_API TXTRecordContainsKey
276 (
277 uint16_t txtLen,
278 const void *txtRecord,
279 const char *key
280 )
281 {
282 unsigned long keylen;
283 return (InternalTXTRecordSearch(txtLen, txtRecord, key, &keylen) ? 1 : 0);
284 }
285
286 const void * DNSSD_API TXTRecordGetValuePtr
287 (
288 uint16_t txtLen,
289 const void *txtRecord,
290 const char *key,
291 uint8_t *valueLen
292 )
293 {
294 unsigned long keylen;
295 uint8_t *item = InternalTXTRecordSearch(txtLen, txtRecord, key, &keylen);
296 if (!item || item[0] <= keylen) return(NULL); // If key not found, or found with no value, return NULL
297 *valueLen = (uint8_t)(item[0] - (keylen + 1));
298 return (item + 1 + keylen + 1);
299 }
300
301 uint16_t DNSSD_API TXTRecordGetCount
302 (
303 uint16_t txtLen,
304 const void *txtRecord
305 )
306 {
307 uint16_t count = 0;
308 uint8_t *p = (uint8_t*)txtRecord;
309 uint8_t *e = p + txtLen;
310 while (p<e) { p += 1 + p[0]; count++; }
311 return((p>e) ? (uint16_t)0 : count);
312 }
313
314 DNSServiceErrorType DNSSD_API TXTRecordGetItemAtIndex
315 (
316 uint16_t txtLen,
317 const void *txtRecord,
318 uint16_t itemIndex,
319 uint16_t keyBufLen,
320 char *key,
321 uint8_t *valueLen,
322 const void **value
323 )
324 {
325 uint16_t count = 0;
326 uint8_t *p = (uint8_t*)txtRecord;
327 uint8_t *e = p + txtLen;
328 while (p<e && count<itemIndex) { p += 1 + p[0]; count++; } // Find requested item
329 if (p<e && p + 1 + p[0] <= e) // If valid
330 {
331 uint8_t *x = p+1;
332 unsigned long len = 0;
333 e = p + 1 + p[0];
334 while (x+len<e && x[len] != '=') len++;
335 if (len >= keyBufLen) return(kDNSServiceErr_NoMemory);
336 memcpy(key, x, len);
337 key[len] = 0;
338 if (x+len<e) // If we found '='
339 {
340 *value = x + len + 1;
341 *valueLen = (uint8_t)(p[0] - (len + 1));
342 }
343 else
344 {
345 *value = NULL;
346 *valueLen = 0;
347 }
348 return(kDNSServiceErr_NoError);
349 }
350 return(kDNSServiceErr_Invalid);
351 }
352
353 /*********************************************************************************************
354 *
355 * SCCS-compatible version string
356 *
357 *********************************************************************************************/
358
359 // For convenience when using the "strings" command, this is the last thing in the file
360
361 // Note: The C preprocessor stringify operator ('#') makes a string from its argument, without macro expansion
362 // e.g. If "version" is #define'd to be "4", then STRINGIFY_AWE(version) will return the string "version", not "4"
363 // To expand "version" to its value before making the string, use STRINGIFY(version) instead
364 #define STRINGIFY_ARGUMENT_WITHOUT_EXPANSION(s) #s
365 #define STRINGIFY(s) STRINGIFY_ARGUMENT_WITHOUT_EXPANSION(s)
366
367 // NOT static -- otherwise the compiler may optimize it out
368 // The "@(#) " pattern is a special prefix the "what" command looks for
369 const char VersionString_SCCS_libdnssd[] = "@(#) libdns_sd " STRINGIFY(mDNSResponderVersion) " (" __DATE__ " " __TIME__ ")";
370 }}