Sync usage with man page.
[netbsd-mini2440.git] / external / apache2 / mDNSResponder / dist / mDNSShared / dnssd_clientlib.c
blobe571908f6e9f10481cb39000263058b95938b60a
1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2004, Apple Computer, Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 Change History (most recent first):
30 Log: dnssd_clientlib.c,v $
31 Revision 1.21 2009/04/01 21:10:11 herscher
32 <rdar://problem/5925472> Current Bonjour code does not compile on Windows. Use _stricmp and _strnicmp.
34 Revision 1.20 2008/11/26 20:57:37 cheshire
35 For consistency with other similar macros, renamed mdnsIsDigit/mdnsIsLetter/mdnsValidHostChar
36 to mDNSIsDigit/mDNSIsLetter/mDNSValidHostChar
38 Revision 1.19 2008/11/04 21:15:18 cheshire
39 <rdar://problem/5969564> Potential buffer overflows in DNSServiceConstructFullName
41 Revision 1.18 2007/11/30 23:06:10 cheshire
42 Fixed compile warning: declaration of 'index' shadows a global declaration
44 Revision 1.17 2007/10/02 19:36:04 cheshire
45 <rdar://problem/5516444> TXTRecordGetValuePtr should be case-insenstive
47 Revision 1.16 2007/09/18 19:09:02 cheshire
48 <rdar://problem/5489549> mDNSResponderHelper (and other binaries) missing SCCS version strings
50 Revision 1.15 2007/07/28 00:00:43 cheshire
51 Renamed CompileTimeAssertionCheck structure for consistency with others
53 Revision 1.14 2007/03/20 17:07:16 cheshire
54 Rename "struct uDNS_TCPSocket_struct" to "TCPSocket", "struct uDNS_UDPSocket_struct" to "UDPSocket"
56 Revision 1.13 2007/02/27 00:25:03 cheshire
57 <rdar://problem/5010640> DNSServiceConstructFullName() doesn't handle empty string for instance name
59 Revision 1.12 2007/02/07 19:32:00 cheshire
60 <rdar://problem/4980353> All mDNSResponder components should contain version strings in SCCS-compatible format
62 Revision 1.11 2006/08/14 23:05:53 cheshire
63 Added "tab-width" emacs header line
65 Revision 1.10 2005/04/06 02:06:56 shersche
66 Add DNSSD_API macro to TXTRecord API calls
68 Revision 1.9 2004/10/06 02:22:19 cheshire
69 Changed MacRoman copyright symbol (should have been UTF-8 in any case :-) to ASCII-compatible "(c)"
71 Revision 1.8 2004/10/01 22:15:55 rpantos
72 rdar://problem/3824265: Replace APSL in client lib with BSD license.
74 Revision 1.7 2004/06/26 03:16:34 shersche
75 clean up warning messages on Win32 platform
77 Submitted by: herscher
79 Revision 1.6 2004/06/12 01:09:45 cheshire
80 To be callable from the broadest range of clients on Windows (e.g. Visual Basic, C#, etc.)
81 API routines have to be declared as "__stdcall", instead of the C default, "__cdecl"
83 Revision 1.5 2004/05/25 18:29:33 cheshire
84 Move DNSServiceConstructFullName() from dnssd_clientstub.c to dnssd_clientlib.c,
85 so that it's also accessible to dnssd_clientshim.c (single address space) clients.
87 Revision 1.4 2004/05/25 17:08:55 cheshire
88 Fix compiler warning (doesn't make sense for function return type to be const)
90 Revision 1.3 2004/05/21 21:41:35 cheshire
91 Add TXT record building and parsing APIs
93 Revision 1.2 2004/05/20 22:22:21 cheshire
94 Enable code that was bracketed by "#if 0"
96 Revision 1.1 2004/03/12 21:30:29 cheshire
97 Build a System-Context Shared Library from mDNSCore, for the benefit of developers
98 like Muse Research who want to be able to use mDNS/DNS-SD from GPL-licensed code.
102 #include <stdlib.h>
103 #include <string.h>
105 #include "dns_sd.h"
107 #if MDNS_BUILDINGSHAREDLIBRARY || MDNS_BUILDINGSTUBLIBRARY
108 #pragma export on
109 #endif
111 #if defined(_WIN32)
112 // disable warning "conversion from <data> to uint16_t"
113 #pragma warning(disable:4244)
114 #define strncasecmp _strnicmp
115 #define strcasecmp _stricmp
116 #endif
118 /*********************************************************************************************
120 * Supporting Functions
122 *********************************************************************************************/
124 #define mDNSIsDigit(X) ((X) >= '0' && (X) <= '9')
126 // DomainEndsInDot returns 1 if name ends with a dot, 0 otherwise
127 // (DNSServiceConstructFullName depends this returning 1 for true, rather than any non-zero value meaning true)
129 static int DomainEndsInDot(const char *dom)
131 while (dom[0] && dom[1])
133 if (dom[0] == '\\') // advance past escaped byte sequence
135 if (mDNSIsDigit(dom[1]) && mDNSIsDigit(dom[2]) && mDNSIsDigit(dom[3]))
136 dom += 4; // If "\ddd" then skip four
137 else dom += 2; // else if "\x" then skip two
139 else dom++; // else goto next character
141 return (dom[0] == '.');
144 static uint8_t *InternalTXTRecordSearch
146 uint16_t txtLen,
147 const void *txtRecord,
148 const char *key,
149 unsigned long *keylen
152 uint8_t *p = (uint8_t*)txtRecord;
153 uint8_t *e = p + txtLen;
154 *keylen = (unsigned long) strlen(key);
155 while (p<e)
157 uint8_t *x = p;
158 p += 1 + p[0];
159 if (p <= e && *keylen <= x[0] && !strncasecmp(key, (char*)x+1, *keylen))
160 if (*keylen == x[0] || x[1+*keylen] == '=') return(x);
162 return(NULL);
165 /*********************************************************************************************
167 * General Utility Functions
169 *********************************************************************************************/
171 // Note: Need to make sure we don't write more than kDNSServiceMaxDomainName (1009) bytes to fullName
172 // In earlier builds this constant was defined to be 1005, so to avoid buffer overruns on clients
173 // compiled with that constant we'll actually limit the output to 1005 bytes.
175 DNSServiceErrorType DNSSD_API DNSServiceConstructFullName
177 char *const fullName,
178 const char *const service, // May be NULL
179 const char *const regtype,
180 const char *const domain
183 const size_t len = !regtype ? 0 : strlen(regtype) - DomainEndsInDot(regtype);
184 char *fn = fullName;
185 char *const lim = fullName + 1005;
186 const char *s = service;
187 const char *r = regtype;
188 const char *d = domain;
190 // regtype must be at least "x._udp" or "x._tcp"
191 if (len < 6 || !domain || !domain[0]) return kDNSServiceErr_BadParam;
192 if (strncasecmp((regtype + len - 4), "_tcp", 4) && strncasecmp((regtype + len - 4), "_udp", 4)) return kDNSServiceErr_BadParam;
194 if (service && *service)
196 while (*s)
198 unsigned char c = *s++; // Needs to be unsigned, or values like 0xFF will be interpreted as < 32
199 if (c <= ' ') // Escape non-printable characters
201 if (fn+4 >= lim) goto fail;
202 *fn++ = '\\';
203 *fn++ = '0' + (c / 100);
204 *fn++ = '0' + (c / 10) % 10;
205 c = '0' + (c ) % 10;
207 else if (c == '.' || (c == '\\')) // Escape dot and backslash literals
209 if (fn+2 >= lim) goto fail;
210 *fn++ = '\\';
212 else
213 if (fn+1 >= lim) goto fail;
214 *fn++ = (char)c;
216 *fn++ = '.';
219 while (*r) if (fn+1 >= lim) goto fail; else *fn++ = *r++;
220 if (!DomainEndsInDot(regtype)) { if (fn+1 >= lim) goto fail; else *fn++ = '.'; }
222 while (*d) if (fn+1 >= lim) goto fail; else *fn++ = *d++;
223 if (!DomainEndsInDot(domain)) { if (fn+1 >= lim) goto fail; else *fn++ = '.'; }
225 *fn = '\0';
226 return kDNSServiceErr_NoError;
228 fail:
229 *fn = '\0';
230 return kDNSServiceErr_BadParam;
233 /*********************************************************************************************
235 * TXT Record Construction Functions
237 *********************************************************************************************/
239 typedef struct _TXTRecordRefRealType
241 uint8_t *buffer; // Pointer to data
242 uint16_t buflen; // Length of buffer
243 uint16_t datalen; // Length currently in use
244 uint16_t malloced; // Non-zero if buffer was allocated via malloc()
245 } TXTRecordRefRealType;
247 #define txtRec ((TXTRecordRefRealType*)txtRecord)
249 // The opaque storage defined in the public dns_sd.h header is 16 bytes;
250 // make sure we don't exceed that.
251 struct CompileTimeAssertionCheck_dnssd_clientlib
253 char assert0[(sizeof(TXTRecordRefRealType) <= 16) ? 1 : -1];
256 void DNSSD_API TXTRecordCreate
258 TXTRecordRef *txtRecord,
259 uint16_t bufferLen,
260 void *buffer
263 txtRec->buffer = buffer;
264 txtRec->buflen = buffer ? bufferLen : (uint16_t)0;
265 txtRec->datalen = 0;
266 txtRec->malloced = 0;
269 void DNSSD_API TXTRecordDeallocate(TXTRecordRef *txtRecord)
271 if (txtRec->malloced) free(txtRec->buffer);
274 DNSServiceErrorType DNSSD_API TXTRecordSetValue
276 TXTRecordRef *txtRecord,
277 const char *key,
278 uint8_t valueSize,
279 const void *value
282 uint8_t *start, *p;
283 const char *k;
284 unsigned long keysize, keyvalsize;
286 for (k = key; *k; k++) if (*k < 0x20 || *k > 0x7E || *k == '=') return(kDNSServiceErr_Invalid);
287 keysize = (unsigned long)(k - key);
288 keyvalsize = 1 + keysize + (value ? (1 + valueSize) : 0);
289 if (keysize < 1 || keyvalsize > 255) return(kDNSServiceErr_Invalid);
290 (void)TXTRecordRemoveValue(txtRecord, key);
291 if (txtRec->datalen + keyvalsize > txtRec->buflen)
293 unsigned char *newbuf;
294 unsigned long newlen = txtRec->datalen + keyvalsize;
295 if (newlen > 0xFFFF) return(kDNSServiceErr_Invalid);
296 newbuf = malloc((size_t)newlen);
297 if (!newbuf) return(kDNSServiceErr_NoMemory);
298 memcpy(newbuf, txtRec->buffer, txtRec->datalen);
299 if (txtRec->malloced) free(txtRec->buffer);
300 txtRec->buffer = newbuf;
301 txtRec->buflen = (uint16_t)(newlen);
302 txtRec->malloced = 1;
304 start = txtRec->buffer + txtRec->datalen;
305 p = start + 1;
306 memcpy(p, key, keysize);
307 p += keysize;
308 if (value)
310 *p++ = '=';
311 memcpy(p, value, valueSize);
312 p += valueSize;
314 *start = (uint8_t)(p - start - 1);
315 txtRec->datalen += p - start;
316 return(kDNSServiceErr_NoError);
319 DNSServiceErrorType DNSSD_API TXTRecordRemoveValue
321 TXTRecordRef *txtRecord,
322 const char *key
325 unsigned long keylen, itemlen, remainder;
326 uint8_t *item = InternalTXTRecordSearch(txtRec->datalen, txtRec->buffer, key, &keylen);
327 if (!item) return(kDNSServiceErr_NoSuchKey);
328 itemlen = (unsigned long)(1 + item[0]);
329 remainder = (unsigned long)((txtRec->buffer + txtRec->datalen) - (item + itemlen));
330 // Use memmove because memcpy behaviour is undefined for overlapping regions
331 memmove(item, item + itemlen, remainder);
332 txtRec->datalen -= itemlen;
333 return(kDNSServiceErr_NoError);
336 uint16_t DNSSD_API TXTRecordGetLength (const TXTRecordRef *txtRecord) { return(txtRec->datalen); }
337 const void * DNSSD_API TXTRecordGetBytesPtr(const TXTRecordRef *txtRecord) { return(txtRec->buffer); }
339 /*********************************************************************************************
341 * TXT Record Parsing Functions
343 *********************************************************************************************/
345 int DNSSD_API TXTRecordContainsKey
347 uint16_t txtLen,
348 const void *txtRecord,
349 const char *key
352 unsigned long keylen;
353 return (InternalTXTRecordSearch(txtLen, txtRecord, key, &keylen) ? 1 : 0);
356 const void * DNSSD_API TXTRecordGetValuePtr
358 uint16_t txtLen,
359 const void *txtRecord,
360 const char *key,
361 uint8_t *valueLen
364 unsigned long keylen;
365 uint8_t *item = InternalTXTRecordSearch(txtLen, txtRecord, key, &keylen);
366 if (!item || item[0] <= keylen) return(NULL); // If key not found, or found with no value, return NULL
367 *valueLen = (uint8_t)(item[0] - (keylen + 1));
368 return (item + 1 + keylen + 1);
371 uint16_t DNSSD_API TXTRecordGetCount
373 uint16_t txtLen,
374 const void *txtRecord
377 uint16_t count = 0;
378 uint8_t *p = (uint8_t*)txtRecord;
379 uint8_t *e = p + txtLen;
380 while (p<e) { p += 1 + p[0]; count++; }
381 return((p>e) ? (uint16_t)0 : count);
384 DNSServiceErrorType DNSSD_API TXTRecordGetItemAtIndex
386 uint16_t txtLen,
387 const void *txtRecord,
388 uint16_t itemIndex,
389 uint16_t keyBufLen,
390 char *key,
391 uint8_t *valueLen,
392 const void **value
395 uint16_t count = 0;
396 uint8_t *p = (uint8_t*)txtRecord;
397 uint8_t *e = p + txtLen;
398 while (p<e && count<itemIndex) { p += 1 + p[0]; count++; } // Find requested item
399 if (p<e && p + 1 + p[0] <= e) // If valid
401 uint8_t *x = p+1;
402 unsigned long len = 0;
403 e = p + 1 + p[0];
404 while (x+len<e && x[len] != '=') len++;
405 if (len >= keyBufLen) return(kDNSServiceErr_NoMemory);
406 memcpy(key, x, len);
407 key[len] = 0;
408 if (x+len<e) // If we found '='
410 *value = x + len + 1;
411 *valueLen = (uint8_t)(p[0] - (len + 1));
413 else
415 *value = NULL;
416 *valueLen = 0;
418 return(kDNSServiceErr_NoError);
420 return(kDNSServiceErr_Invalid);
423 /*********************************************************************************************
425 * SCCS-compatible version string
427 *********************************************************************************************/
429 // For convenience when using the "strings" command, this is the last thing in the file
431 // Note: The C preprocessor stringify operator ('#') makes a string from its argument, without macro expansion
432 // e.g. If "version" is #define'd to be "4", then STRINGIFY_AWE(version) will return the string "version", not "4"
433 // To expand "version" to its value before making the string, use STRINGIFY(version) instead
434 #define STRINGIFY_ARGUMENT_WITHOUT_EXPANSION(s) #s
435 #define STRINGIFY(s) STRINGIFY_ARGUMENT_WITHOUT_EXPANSION(s)
437 // NOT static -- otherwise the compiler may optimize it out
438 // The "@(#) " pattern is a special prefix the "what" command looks for
439 const char VersionString_SCCS_libdnssd[] = "@(#) libdns_sd " STRINGIFY(mDNSResponderVersion) " (" __DATE__ " " __TIME__ ")";