]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSShared/dnssd_clientlib.c
mDNSResponder-108.tar.gz
[apple/mdnsresponder.git] / mDNSShared / dnssd_clientlib.c
1 /*
2 * Copyright (c) 2004, Apple Computer, 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 Computer, 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 Change History (most recent first):
28
29 $Log: dnssd_clientlib.c,v $
30 Revision 1.10 2005/04/06 02:06:56 shersche
31 Add DNSSD_API macro to TXTRecord API calls
32
33 Revision 1.9 2004/10/06 02:22:19 cheshire
34 Changed MacRoman copyright symbol (should have been UTF-8 in any case :-) to ASCII-compatible "(c)"
35
36 Revision 1.8 2004/10/01 22:15:55 rpantos
37 rdar://problem/3824265: Replace APSL in client lib with BSD license.
38
39 Revision 1.7 2004/06/26 03:16:34 shersche
40 clean up warning messages on Win32 platform
41
42 Submitted by: herscher
43
44 Revision 1.6 2004/06/12 01:09:45 cheshire
45 To be callable from the broadest range of clients on Windows (e.g. Visual Basic, C#, etc.)
46 API routines have to be declared as "__stdcall", instead of the C default, "__cdecl"
47
48 Revision 1.5 2004/05/25 18:29:33 cheshire
49 Move DNSServiceConstructFullName() from dnssd_clientstub.c to dnssd_clientlib.c,
50 so that it's also accessible to dnssd_clientshim.c (single address space) clients.
51
52 Revision 1.4 2004/05/25 17:08:55 cheshire
53 Fix compiler warning (doesn't make sense for function return type to be const)
54
55 Revision 1.3 2004/05/21 21:41:35 cheshire
56 Add TXT record building and parsing APIs
57
58 Revision 1.2 2004/05/20 22:22:21 cheshire
59 Enable code that was bracketed by "#if 0"
60
61 Revision 1.1 2004/03/12 21:30:29 cheshire
62 Build a System-Context Shared Library from mDNSCore, for the benefit of developers
63 like Muse Research who want to be able to use mDNS/DNS-SD from GPL-licensed code.
64
65 */
66
67 #include <stdlib.h>
68 #include <string.h>
69
70 #include "dns_sd.h"
71
72 #if MDNS_BUILDINGSHAREDLIBRARY || MDNS_BUILDINGSTUBLIBRARY
73 #pragma export on
74 #endif
75
76 #if defined(_WIN32)
77 // disable warning "conversion from <data> to uint16_t"
78 #pragma warning(disable:4244)
79 #endif
80
81 /*********************************************************************************************
82 *
83 * Supporting Functions
84 *
85 *********************************************************************************************/
86
87 #define mdnsIsDigit(X) ((X) >= '0' && (X) <= '9')
88
89 static int DomainEndsInDot(const char *dom)
90 {
91 while (dom[0] && dom[1])
92 {
93 if (dom[0] == '\\') // advance past escaped byte sequence
94 {
95 if (mdnsIsDigit(dom[1]) && mdnsIsDigit(dom[2]) && mdnsIsDigit(dom[3]))
96 dom += 4; // If "\ddd" then skip four
97 else dom += 2; // else if "\x" then skip two
98 }
99 else dom++; // else goto next character
100 }
101 return (dom[0] == '.');
102 }
103
104 static uint8_t *InternalTXTRecordSearch
105 (
106 uint16_t txtLen,
107 const void *txtRecord,
108 const char *key,
109 unsigned long *keylen
110 )
111 {
112 uint8_t *p = (uint8_t*)txtRecord;
113 uint8_t *e = p + txtLen;
114 *keylen = (unsigned long) strlen(key);
115 while (p<e)
116 {
117 uint8_t *x = p;
118 p += 1 + p[0];
119 if (p <= e && *keylen <= x[0] && !strncmp(key, (char*)x+1, *keylen))
120 if (*keylen == x[0] || x[1+*keylen] == '=') return(x);
121 }
122 return(NULL);
123 }
124
125 /*********************************************************************************************
126 *
127 * General Utility Functions
128 *
129 *********************************************************************************************/
130
131 int DNSSD_API DNSServiceConstructFullName
132 (
133 char *fullName,
134 const char *service, /* may be NULL */
135 const char *regtype,
136 const char *domain
137 )
138 {
139 unsigned long len;
140 unsigned char c;
141 char *fn = fullName;
142 const char *s = service;
143 const char *r = regtype;
144 const char *d = domain;
145
146 if (service)
147 {
148 while(*s)
149 {
150 c = (unsigned char)*s++;
151 if (c == '.' || (c == '\\')) *fn++ = '\\'; // escape dot and backslash literals
152 else if (c <= ' ') // escape non-printable characters
153 {
154 *fn++ = '\\';
155 *fn++ = (char) ('0' + (c / 100));
156 *fn++ = (char) ('0' + (c / 10) % 10);
157 c = (unsigned char)('0' + (c % 10));
158 }
159 *fn++ = (char)c;
160 }
161 *fn++ = '.';
162 }
163
164 if (!regtype) return -1;
165 len = (unsigned long) strlen(regtype);
166 if (DomainEndsInDot(regtype)) len--;
167 if (len < 6) return -1; // regtype must be at least "x._udp" or "x._tcp"
168 if (strncmp((regtype + len - 4), "_tcp", 4) && strncmp((regtype + len - 4), "_udp", 4)) return -1;
169 while(*r) *fn++ = *r++;
170 if (!DomainEndsInDot(regtype)) *fn++ = '.';
171
172 if (!domain || !domain[0]) return -1;
173 while(*d) *fn++ = *d++;
174 if (!DomainEndsInDot(domain)) *fn++ = '.';
175 *fn = '\0';
176 return 0;
177 }
178
179 /*********************************************************************************************
180 *
181 * TXT Record Construction Functions
182 *
183 *********************************************************************************************/
184
185 typedef struct _TXTRecordRefRealType
186 {
187 uint8_t *buffer; // Pointer to data
188 uint16_t buflen; // Length of buffer
189 uint16_t datalen; // Length currently in use
190 uint16_t malloced; // Non-zero if buffer was allocated via malloc()
191 } TXTRecordRefRealType;
192
193 #define txtRec ((TXTRecordRefRealType*)txtRecord)
194
195 // The opaque storage defined in the public dns_sd.h header is 16 bytes;
196 // make sure we don't exceed that.
197 struct dnssd_clientlib_CompileTimeAssertionCheck
198 {
199 char assert0[(sizeof(TXTRecordRefRealType) <= 16) ? 1 : -1];
200 };
201
202 void DNSSD_API TXTRecordCreate
203 (
204 TXTRecordRef *txtRecord,
205 uint16_t bufferLen,
206 void *buffer
207 )
208 {
209 txtRec->buffer = buffer;
210 txtRec->buflen = buffer ? bufferLen : (uint16_t)0;
211 txtRec->datalen = 0;
212 txtRec->malloced = 0;
213 }
214
215 void DNSSD_API TXTRecordDeallocate(TXTRecordRef *txtRecord)
216 {
217 if (txtRec->malloced) free(txtRec->buffer);
218 }
219
220 DNSServiceErrorType DNSSD_API TXTRecordSetValue
221 (
222 TXTRecordRef *txtRecord,
223 const char *key,
224 uint8_t valueSize,
225 const void *value
226 )
227 {
228 uint8_t *start, *p;
229 const char *k;
230 unsigned long keysize, keyvalsize;
231
232 for (k = key; *k; k++) if (*k < 0x20 || *k > 0x7E || *k == '=') return(kDNSServiceErr_Invalid);
233 keysize = (unsigned long)(k - key);
234 keyvalsize = 1 + keysize + (value ? (1 + valueSize) : 0);
235 if (keysize < 1 || keyvalsize > 255) return(kDNSServiceErr_Invalid);
236 (void)TXTRecordRemoveValue(txtRecord, key);
237 if (txtRec->datalen + keyvalsize > txtRec->buflen)
238 {
239 unsigned char *newbuf;
240 unsigned long newlen = txtRec->datalen + keyvalsize;
241 if (newlen > 0xFFFF) return(kDNSServiceErr_Invalid);
242 newbuf = malloc((size_t)newlen);
243 if (!newbuf) return(kDNSServiceErr_NoMemory);
244 memcpy(newbuf, txtRec->buffer, txtRec->datalen);
245 if (txtRec->malloced) free(txtRec->buffer);
246 txtRec->buffer = newbuf;
247 txtRec->buflen = (uint16_t)(newlen);
248 txtRec->malloced = 1;
249 }
250 start = txtRec->buffer + txtRec->datalen;
251 p = start + 1;
252 memcpy(p, key, keysize);
253 p += keysize;
254 if (value)
255 {
256 *p++ = '=';
257 memcpy(p, value, valueSize);
258 p += valueSize;
259 }
260 *start = (uint8_t)(p - start - 1);
261 txtRec->datalen += p - start;
262 return(kDNSServiceErr_NoError);
263 }
264
265 DNSServiceErrorType DNSSD_API TXTRecordRemoveValue
266 (
267 TXTRecordRef *txtRecord,
268 const char *key
269 )
270 {
271 unsigned long keylen, itemlen, remainder;
272 uint8_t *item = InternalTXTRecordSearch(txtRec->datalen, txtRec->buffer, key, &keylen);
273 if (!item) return(kDNSServiceErr_NoSuchKey);
274 itemlen = (unsigned long)(1 + item[0]);
275 remainder = (unsigned long)((txtRec->buffer + txtRec->datalen) - (item + itemlen));
276 // Use memmove because memcpy behaviour is undefined for overlapping regions
277 memmove(item, item + itemlen, remainder);
278 txtRec->datalen -= itemlen;
279 return(kDNSServiceErr_NoError);
280 }
281
282 uint16_t DNSSD_API TXTRecordGetLength (const TXTRecordRef *txtRecord) { return(txtRec->datalen); }
283 const void * DNSSD_API TXTRecordGetBytesPtr(const TXTRecordRef *txtRecord) { return(txtRec->buffer); }
284
285 /*********************************************************************************************
286 *
287 * TXT Record Parsing Functions
288 *
289 *********************************************************************************************/
290
291 int DNSSD_API TXTRecordContainsKey
292 (
293 uint16_t txtLen,
294 const void *txtRecord,
295 const char *key
296 )
297 {
298 unsigned long keylen;
299 return (InternalTXTRecordSearch(txtLen, txtRecord, key, &keylen) ? 1 : 0);
300 }
301
302 const void * DNSSD_API TXTRecordGetValuePtr
303 (
304 uint16_t txtLen,
305 const void *txtRecord,
306 const char *key,
307 uint8_t *valueLen
308 )
309 {
310 unsigned long keylen;
311 uint8_t *item = InternalTXTRecordSearch(txtLen, txtRecord, key, &keylen);
312 if (!item || item[0] <= keylen) return(NULL); // If key not found, or found with no value, return NULL
313 *valueLen = (uint8_t)(item[0] - (keylen + 1));
314 return (item + 1 + keylen + 1);
315 }
316
317 uint16_t DNSSD_API TXTRecordGetCount
318 (
319 uint16_t txtLen,
320 const void *txtRecord
321 )
322 {
323 uint16_t count = 0;
324 uint8_t *p = (uint8_t*)txtRecord;
325 uint8_t *e = p + txtLen;
326 while (p<e) { p += 1 + p[0]; count++; }
327 return((p>e) ? (uint16_t)0 : count);
328 }
329
330 DNSServiceErrorType DNSSD_API TXTRecordGetItemAtIndex
331 (
332 uint16_t txtLen,
333 const void *txtRecord,
334 uint16_t index,
335 uint16_t keyBufLen,
336 char *key,
337 uint8_t *valueLen,
338 const void **value
339 )
340 {
341 uint16_t count = 0;
342 uint8_t *p = (uint8_t*)txtRecord;
343 uint8_t *e = p + txtLen;
344 while (p<e && count<index) { p += 1 + p[0]; count++; } // Find requested item
345 if (p<e && p + 1 + p[0] <= e) // If valid
346 {
347 uint8_t *x = p+1;
348 unsigned long len = 0;
349 e = p + 1 + p[0];
350 while (x+len<e && x[len] != '=') len++;
351 if (len >= keyBufLen) return(kDNSServiceErr_NoMemory);
352 memcpy(key, x, len);
353 key[len] = 0;
354 if (x+len<e) // If we found '='
355 {
356 *value = x + len + 1;
357 *valueLen = (uint8_t)(p[0] - (len + 1));
358 }
359 else
360 {
361 *value = NULL;
362 *valueLen = 0;
363 }
364 return(kDNSServiceErr_NoError);
365 }
366 return(kDNSServiceErr_Invalid);
367 }