2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
25 Change History (most recent first):
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.
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)
35 Revision 1.3 2004/05/21 21:41:35 cheshire
36 Add TXT record building and parsing APIs
38 Revision 1.2 2004/05/20 22:22:21 cheshire
39 Enable code that was bracketed by "#if 0"
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.
52 #if MDNS_BUILDINGSHAREDLIBRARY || MDNS_BUILDINGSTUBLIBRARY
56 /*********************************************************************************************
58 * Supporting Functions
60 *********************************************************************************************/
62 #define mdnsIsDigit(X) ((X) >= '0' && (X) <= '9')
64 static int DomainEndsInDot(const char *dom
)
66 while (dom
[0] && dom
[1])
68 if (dom
[0] == '\\') // advance past escaped byte sequence
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
74 else dom
++; // else goto next character
76 return (dom
[0] == '.');
79 static uint8_t *InternalTXTRecordSearch
82 const void *txtRecord
,
87 uint8_t *p
= (uint8_t*)txtRecord
;
88 uint8_t *e
= p
+ txtLen
;
89 *keylen
= strlen(key
);
94 if (p
<= e
&& *keylen
<= x
[0] && !strncmp(key
, (char*)x
+1, *keylen
))
95 if (*keylen
== x
[0] || x
[1+*keylen
] == '=') return(x
);
100 /*********************************************************************************************
102 * General Utility Functions
104 *********************************************************************************************/
106 int DNSServiceConstructFullName
109 const char *service
, /* may be NULL */
117 const char *s
= service
;
118 const char *r
= regtype
;
119 const char *d
= domain
;
125 c
= (unsigned char)*s
++;
126 if (c
== '.' || (c
== '\\')) *fn
++ = '\\'; // escape dot and backslash literals
127 else if (c
<= ' ') // escape non-printable characters
130 *fn
++ = (char) ('0' + (c
/ 100));
131 *fn
++ = (char) ('0' + (c
/ 10) % 10);
132 c
= (unsigned char)('0' + (c
% 10));
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
++ = '.';
147 if (!domain
|| !domain
[0]) return -1;
148 while(*d
) *fn
++ = *d
++;
149 if (!DomainEndsInDot(domain
)) *fn
++ = '.';
154 /*********************************************************************************************
156 * TXT Record Construction Functions
158 *********************************************************************************************/
160 typedef struct _TXTRecordRefRealType
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
;
168 #define txtRec ((TXTRecordRefRealType*)txtRecord)
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
174 char assert0
[(sizeof(TXTRecordRefRealType
) <= 16) ? 1 : -1];
179 TXTRecordRef
*txtRecord
,
184 txtRec
->buffer
= buffer
;
185 txtRec
->buflen
= buffer
? bufferLen
: (uint16_t)0;
187 txtRec
->malloced
= 0;
190 void TXTRecordDeallocate(TXTRecordRef
*txtRecord
)
192 if (txtRec
->malloced
) free(txtRec
->buffer
);
195 DNSServiceErrorType TXTRecordSetValue
197 TXTRecordRef
*txtRecord
,
205 unsigned long keysize
, keyvalsize
;
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
)
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;
225 start
= txtRec
->buffer
+ txtRec
->datalen
;
227 memcpy(p
, key
, keysize
);
232 memcpy(p
, value
, valueSize
);
235 *start
= (uint8_t)(p
- start
- 1);
236 txtRec
->datalen
+= p
- start
;
237 return(kDNSServiceErr_NoError
);
240 DNSServiceErrorType TXTRecordRemoveValue
242 TXTRecordRef
*txtRecord
,
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
);
257 uint16_t TXTRecordGetLength (const TXTRecordRef
*txtRecord
) { return(txtRec
->datalen
); }
258 const void * TXTRecordGetBytesPtr(const TXTRecordRef
*txtRecord
) { return(txtRec
->buffer
); }
260 /*********************************************************************************************
262 * TXT Record Parsing Functions
264 *********************************************************************************************/
266 int TXTRecordContainsKey
269 const void *txtRecord
,
273 unsigned long keylen
;
274 return (InternalTXTRecordSearch(txtLen
, txtRecord
, key
, &keylen
) ? 1 : 0);
277 const void * TXTRecordGetValuePtr
280 const void *txtRecord
,
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);
292 uint16_t TXTRecordGetCount
295 const void *txtRecord
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
);
305 DNSServiceErrorType TXTRecordGetItemAtIndex
308 const void *txtRecord
,
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
323 unsigned long len
= 0;
325 while (x
+len
<e
&& x
[len
] != '=') len
++;
326 if (len
>= keyBufLen
) return(kDNSServiceErr_NoMemory
);
329 if (x
+len
<e
) // If we found '='
331 *value
= x
+ len
+ 1;
332 *valueLen
= (uint8_t)(p
[0] - (len
+ 1));
339 return(kDNSServiceErr_NoError
);
341 return(kDNSServiceErr_Invalid
);