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