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