]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSShared/dnssd_clientlib.c
mDNSResponder-66.3.tar.gz
[apple/mdnsresponder.git] / mDNSShared / dnssd_clientlib.c
1 /*
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24
25 Change History (most recent first):
26
27 $Log: dnssd_clientlib.c,v $
28 Revision 1.5 2004/05/25 18:29:33 cheshire
29 Move DNSServiceConstructFullName() from dnssd_clientstub.c to dnssd_clientlib.c,
30 so that it's also accessible to dnssd_clientshim.c (single address space) clients.
31
32 Revision 1.4 2004/05/25 17:08:55 cheshire
33 Fix compiler warning (doesn't make sense for function return type to be const)
34
35 Revision 1.3 2004/05/21 21:41:35 cheshire
36 Add TXT record building and parsing APIs
37
38 Revision 1.2 2004/05/20 22:22:21 cheshire
39 Enable code that was bracketed by "#if 0"
40
41 Revision 1.1 2004/03/12 21:30:29 cheshire
42 Build a System-Context Shared Library from mDNSCore, for the benefit of developers
43 like Muse Research who want to be able to use mDNS/DNS-SD from GPL-licensed code.
44
45 */
46
47 #include <stdlib.h>
48 #include <string.h>
49
50 #include "dns_sd.h"
51
52 #if MDNS_BUILDINGSHAREDLIBRARY || MDNS_BUILDINGSTUBLIBRARY
53 #pragma export on
54 #endif
55
56 /*********************************************************************************************
57 *
58 * Supporting Functions
59 *
60 *********************************************************************************************/
61
62 #define mdnsIsDigit(X) ((X) >= '0' && (X) <= '9')
63
64 static int DomainEndsInDot(const char *dom)
65 {
66 while (dom[0] && dom[1])
67 {
68 if (dom[0] == '\\') // advance past escaped byte sequence
69 {
70 if (mdnsIsDigit(dom[1]) && mdnsIsDigit(dom[2]) && mdnsIsDigit(dom[3]))
71 dom += 4; // If "\ddd" then skip four
72 else dom += 2; // else if "\x" then skip two
73 }
74 else dom++; // else goto next character
75 }
76 return (dom[0] == '.');
77 }
78
79 static uint8_t *InternalTXTRecordSearch
80 (
81 uint16_t txtLen,
82 const void *txtRecord,
83 const char *key,
84 unsigned long *keylen
85 )
86 {
87 uint8_t *p = (uint8_t*)txtRecord;
88 uint8_t *e = p + txtLen;
89 *keylen = strlen(key);
90 while (p<e)
91 {
92 uint8_t *x = p;
93 p += 1 + p[0];
94 if (p <= e && *keylen <= x[0] && !strncmp(key, (char*)x+1, *keylen))
95 if (*keylen == x[0] || x[1+*keylen] == '=') return(x);
96 }
97 return(NULL);
98 }
99
100 /*********************************************************************************************
101 *
102 * General Utility Functions
103 *
104 *********************************************************************************************/
105
106 int DNSServiceConstructFullName
107 (
108 char *fullName,
109 const char *service, /* may be NULL */
110 const char *regtype,
111 const char *domain
112 )
113 {
114 unsigned long len;
115 unsigned char c;
116 char *fn = fullName;
117 const char *s = service;
118 const char *r = regtype;
119 const char *d = domain;
120
121 if (service)
122 {
123 while(*s)
124 {
125 c = (unsigned char)*s++;
126 if (c == '.' || (c == '\\')) *fn++ = '\\'; // escape dot and backslash literals
127 else if (c <= ' ') // escape non-printable characters
128 {
129 *fn++ = '\\';
130 *fn++ = (char) ('0' + (c / 100));
131 *fn++ = (char) ('0' + (c / 10) % 10);
132 c = (unsigned char)('0' + (c % 10));
133 }
134 *fn++ = (char)c;
135 }
136 *fn++ = '.';
137 }
138
139 if (!regtype) return -1;
140 len = strlen(regtype);
141 if (DomainEndsInDot(regtype)) len--;
142 if (len < 6) return -1; // regtype must be at least "x._udp" or "x._tcp"
143 if (strncmp((regtype + len - 4), "_tcp", 4) && strncmp((regtype + len - 4), "_udp", 4)) return -1;
144 while(*r) *fn++ = *r++;
145 if (!DomainEndsInDot(regtype)) *fn++ = '.';
146
147 if (!domain || !domain[0]) return -1;
148 while(*d) *fn++ = *d++;
149 if (!DomainEndsInDot(domain)) *fn++ = '.';
150 *fn = '\0';
151 return 0;
152 }
153
154 /*********************************************************************************************
155 *
156 * TXT Record Construction Functions
157 *
158 *********************************************************************************************/
159
160 typedef struct _TXTRecordRefRealType
161 {
162 uint8_t *buffer; // Pointer to data
163 uint16_t buflen; // Length of buffer
164 uint16_t datalen; // Length currently in use
165 uint16_t malloced; // Non-zero if buffer was allocated via malloc()
166 } TXTRecordRefRealType;
167
168 #define txtRec ((TXTRecordRefRealType*)txtRecord)
169
170 // The opaque storage defined in the public dns_sd.h header is 16 bytes;
171 // make sure we don't exceed that.
172 struct dnssd_clientlib_CompileTimeAssertionCheck
173 {
174 char assert0[(sizeof(TXTRecordRefRealType) <= 16) ? 1 : -1];
175 };
176
177 void TXTRecordCreate
178 (
179 TXTRecordRef *txtRecord,
180 uint16_t bufferLen,
181 void *buffer
182 )
183 {
184 txtRec->buffer = buffer;
185 txtRec->buflen = buffer ? bufferLen : (uint16_t)0;
186 txtRec->datalen = 0;
187 txtRec->malloced = 0;
188 }
189
190 void TXTRecordDeallocate(TXTRecordRef *txtRecord)
191 {
192 if (txtRec->malloced) free(txtRec->buffer);
193 }
194
195 DNSServiceErrorType TXTRecordSetValue
196 (
197 TXTRecordRef *txtRecord,
198 const char *key,
199 uint8_t valueSize,
200 const void *value
201 )
202 {
203 uint8_t *start, *p;
204 const char *k;
205 unsigned long keysize, keyvalsize;
206
207 for (k = key; *k; k++) if (*k < 0x20 || *k > 0x7E || *k == '=') return(kDNSServiceErr_Invalid);
208 keysize = (unsigned long)(k - key);
209 keyvalsize = 1 + keysize + (value ? (1 + valueSize) : 0);
210 if (keysize < 1 || keyvalsize > 255) return(kDNSServiceErr_Invalid);
211 (void)TXTRecordRemoveValue(txtRecord, key);
212 if (txtRec->datalen + keyvalsize > txtRec->buflen)
213 {
214 unsigned char *newbuf;
215 unsigned long newlen = txtRec->datalen + keyvalsize;
216 if (newlen > 0xFFFF) return(kDNSServiceErr_Invalid);
217 newbuf = malloc((size_t)newlen);
218 if (!newbuf) return(kDNSServiceErr_NoMemory);
219 memcpy(newbuf, txtRec->buffer, txtRec->datalen);
220 if (txtRec->malloced) free(txtRec->buffer);
221 txtRec->buffer = newbuf;
222 txtRec->buflen = (uint16_t)(newlen);
223 txtRec->malloced = 1;
224 }
225 start = txtRec->buffer + txtRec->datalen;
226 p = start + 1;
227 memcpy(p, key, keysize);
228 p += keysize;
229 if (value)
230 {
231 *p++ = '=';
232 memcpy(p, value, valueSize);
233 p += valueSize;
234 }
235 *start = (uint8_t)(p - start - 1);
236 txtRec->datalen += p - start;
237 return(kDNSServiceErr_NoError);
238 }
239
240 DNSServiceErrorType TXTRecordRemoveValue
241 (
242 TXTRecordRef *txtRecord,
243 const char *key
244 )
245 {
246 unsigned long keylen, itemlen, remainder;
247 uint8_t *item = InternalTXTRecordSearch(txtRec->datalen, txtRec->buffer, key, &keylen);
248 if (!item) return(kDNSServiceErr_NoSuchKey);
249 itemlen = (unsigned long)(1 + item[0]);
250 remainder = (unsigned long)((txtRec->buffer + txtRec->datalen) - (item + itemlen));
251 // Use memmove because memcpy behaviour is undefined for overlapping regions
252 memmove(item, item + itemlen, remainder);
253 txtRec->datalen -= itemlen;
254 return(kDNSServiceErr_NoError);
255 }
256
257 uint16_t TXTRecordGetLength (const TXTRecordRef *txtRecord) { return(txtRec->datalen); }
258 const void * TXTRecordGetBytesPtr(const TXTRecordRef *txtRecord) { return(txtRec->buffer); }
259
260 /*********************************************************************************************
261 *
262 * TXT Record Parsing Functions
263 *
264 *********************************************************************************************/
265
266 int TXTRecordContainsKey
267 (
268 uint16_t txtLen,
269 const void *txtRecord,
270 const char *key
271 )
272 {
273 unsigned long keylen;
274 return (InternalTXTRecordSearch(txtLen, txtRecord, key, &keylen) ? 1 : 0);
275 }
276
277 const void * TXTRecordGetValuePtr
278 (
279 uint16_t txtLen,
280 const void *txtRecord,
281 const char *key,
282 uint8_t *valueLen
283 )
284 {
285 unsigned long keylen;
286 uint8_t *item = InternalTXTRecordSearch(txtLen, txtRecord, key, &keylen);
287 if (!item || item[0] <= keylen) return(NULL); // If key not found, or found with no value, return NULL
288 *valueLen = (uint8_t)(item[0] - (keylen + 1));
289 return (item + 1 + keylen + 1);
290 }
291
292 uint16_t TXTRecordGetCount
293 (
294 uint16_t txtLen,
295 const void *txtRecord
296 )
297 {
298 uint16_t count = 0;
299 uint8_t *p = (uint8_t*)txtRecord;
300 uint8_t *e = p + txtLen;
301 while (p<e) { p += 1 + p[0]; count++; }
302 return((p>e) ? (uint16_t)0 : count);
303 }
304
305 DNSServiceErrorType TXTRecordGetItemAtIndex
306 (
307 uint16_t txtLen,
308 const void *txtRecord,
309 uint16_t index,
310 uint16_t keyBufLen,
311 char *key,
312 uint8_t *valueLen,
313 const void **value
314 )
315 {
316 uint16_t count = 0;
317 uint8_t *p = (uint8_t*)txtRecord;
318 uint8_t *e = p + txtLen;
319 while (p<e && count<index) { p += 1 + p[0]; count++; } // Find requested item
320 if (p<e && p + 1 + p[0] <= e) // If valid
321 {
322 uint8_t *x = p+1;
323 unsigned long len = 0;
324 e = p + 1 + p[0];
325 while (x+len<e && x[len] != '=') len++;
326 if (len >= keyBufLen) return(kDNSServiceErr_NoMemory);
327 memcpy(key, x, len);
328 key[len] = 0;
329 if (x+len<e) // If we found '='
330 {
331 *value = x + len + 1;
332 *valueLen = (uint8_t)(p[0] - (len + 1));
333 }
334 else
335 {
336 *value = NULL;
337 *valueLen = 0;
338 }
339 return(kDNSServiceErr_NoError);
340 }
341 return(kDNSServiceErr_Invalid);
342 }