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