2 * Copyright (c) 2004, Apple Computer, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
27 #include "mdnsderived.h"
31 #define strncasecmp _strnicmp
37 // DomainEndsInDot returns 1 if name ends with a dot, 0 otherwise
38 // (DNSServiceConstructFullName depends this returning 1 for true, rather than any non-zero value meaning true)
39 static int DomainEndsInDot(const char *dom)
41 while (dom[0] && dom[1]) {
42 if (dom[0] == '\\') { // advance past escaped byte sequence
43 if ('0' <= dom[1] && dom[1] <= '9' &&
44 '0' <= dom[2] && dom[2] <= '9' &&
45 '0' <= dom[3] && dom[3] <= '9')
47 dom += 4; // If "\ddd" then skip four
49 dom += 2; // else if "\x" then skip two
52 dom++; // else goto next character
55 return (dom[0] == '.');
58 // Note: Need to make sure we don't write more than kDNSServiceMaxDomainName (1009) bytes to fullName
59 // In earlier builds this constant was defined to be 1005, so to avoid buffer overruns on clients
60 // compiled with that constant we'll actually limit the output to 1005 bytes.
61 DNSServiceErrorType myDNSServiceConstructFullName(char *const fullName,
62 const char *const service, // May be NULL
63 const char *const regtype,
64 const char *const domain)
66 const size_t len = !regtype ? 0 : strlen(regtype) - DomainEndsInDot(regtype);
68 char *const lim = fullName + 1005;
69 const char *s = service;
70 const char *r = regtype;
71 const char *d = domain;
73 // regtype must be at least "x._udp" or "x._tcp"
74 if (len < 6 || !domain || !domain[0]) return kDNSServiceErr_BadParam;
75 if (strncasecmp((regtype + len - 4), "_tcp", 4) && strncasecmp((regtype + len - 4), "_udp", 4)) return kDNSServiceErr_BadParam;
77 if (service && *service)
81 unsigned char c = *s++; // Needs to be unsigned, or values like 0xFF will be interpreted as < 32
82 if (c <= ' ') // Escape non-printable characters
84 if (fn + 4 >= lim) goto fail;
86 *fn++ = '0' + (c / 100);
87 *fn++ = '0' + (c / 10) % 10;
90 else if (c == '.' || (c == '\\')) // Escape dot and backslash literals
92 if (fn + 2 >= lim) goto fail;
96 if (fn + 1 >= lim) goto fail;
102 while (*r) if (fn + 1 >= lim) goto fail; else *fn++ = *r++;
103 if (!DomainEndsInDot(regtype)) { if (fn + 1 >= lim) goto fail; else *fn++ = '.'; }
105 while (*d) if (fn + 1 >= lim) goto fail; else *fn++ = *d++;
106 if (!DomainEndsInDot(domain)) { if (fn + 1 >= lim) goto fail; else *fn++ = '.'; }
109 return kDNSServiceErr_NoError;
113 return kDNSServiceErr_BadParam;
116 uint16_t txtRecordGetCount(uint16_t txtLen, const void *txtRecord)
119 uint8_t *p = (uint8_t*)txtRecord;
120 uint8_t *e = p + txtLen;
121 while (p<e) { p += 1 + p[0]; count++; }
122 return((p>e) ? (uint16_t)0 : count);
125 DNSServiceErrorType txtRecordGetItemAtIndex(uint16_t txtLen, const void *txtRecord,
126 uint16_t itemIndex, uint16_t keyBufLen, char *key, uint8_t *valueLen, const void **value)
129 uint8_t *p = (uint8_t*)txtRecord;
130 uint8_t *e = p + txtLen;
131 while (p<e && count<itemIndex) { p += 1 + p[0]; count++; } // Find requested item
132 if (p<e && p + 1 + p[0] <= e) { // If valid
134 unsigned long len = 0;
136 while (x+len<e && x[len] != '=') len++;
137 if (len >= keyBufLen) return(kDNSServiceErr_NoMemory);
140 if (x+len<e) { // If we found '='
141 *value = x + len + 1;
142 *valueLen = (uint8_t)(p[0] - (len + 1));
147 return(kDNSServiceErr_NoError);
149 return(kDNSServiceErr_Invalid);
153 } // namespace ZeroConf
154 } // namespace Internal