| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
|
| 32 |
#include "dns_sd_types.h" |
| 33 |
|
| 34 |
#if MDNS_BUILDINGSHAREDLIBRARY || MDNS_BUILDINGSTUBLIBRARY |
| 35 |
|
| 36 |
#endif |
| 37 |
|
| 38 |
#if defined(_WIN32) |
| 39 |
|
| 40 |
#pragma warning(disable:4244) |
| 41 |
#define strncasecmp _strnicmp |
| 42 |
#define strcasecmp _stricmp |
| 43 |
#endif |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
namespace ZeroConf { namespace embeddedLib { |
| 51 |
|
| 52 |
#include "dns_sd_funct.h" |
| 53 |
|
| 54 |
#define mDNSIsDigit(X) ((X) >= '0' && (X) <= '9') |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
static int DomainEndsInDot(const char *dom) |
| 60 |
{ |
| 61 |
while (dom[0] && dom[1]) |
| 62 |
{ |
| 63 |
if (dom[0] == '\\') |
| 64 |
{ |
| 65 |
if (mDNSIsDigit(dom[1]) && mDNSIsDigit(dom[2]) && mDNSIsDigit(dom[3])) |
| 66 |
dom += 4; |
| 67 |
else dom += 2; |
| 68 |
} |
| 69 |
else dom++; |
| 70 |
} |
| 71 |
return (dom[0] == '.'); |
| 72 |
} |
| 73 |
|
| 74 |
static uint8_t *InternalTXTRecordSearch |
| 75 |
( |
| 76 |
uint16_t txtLen, |
| 77 |
const void *txtRecord, |
| 78 |
const char *key, |
| 79 |
unsigned long *keylen |
| 80 |
) |
| 81 |
{ |
| 82 |
uint8_t *p = (uint8_t*)txtRecord; |
| 83 |
uint8_t *e = p + txtLen; |
| 84 |
*keylen = (unsigned long) strlen(key); |
| 85 |
while (p<e) |
| 86 |
{ |
| 87 |
uint8_t *x = p; |
| 88 |
p += 1 + p[0]; |
| 89 |
if (p <= e && *keylen <= x[0] && !strncasecmp(key, (char*)x+1, *keylen)) |
| 90 |
if (*keylen == x[0] || x[1+*keylen] == '=') return(x); |
| 91 |
} |
| 92 |
return(NULL); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
DNSServiceErrorType DNSSD_API DNSServiceConstructFullName |
| 106 |
( |
| 107 |
char *const fullName, |
| 108 |
const char *const service, |
| 109 |
const char *const regtype, |
| 110 |
const char *const domain |
| 111 |
) |
| 112 |
{ |
| 113 |
const size_t len = !regtype ? 0 : strlen(regtype) - DomainEndsInDot(regtype); |
| 114 |
char *fn = fullName; |
| 115 |
char *const lim = fullName + 1005; |
| 116 |
const char *s = service; |
| 117 |
const char *r = regtype; |
| 118 |
const char *d = domain; |
| 119 |
|
| 120 |
|
| 121 |
if (len < 6 || !domain || !domain[0]) return kDNSServiceErr_BadParam; |
| 122 |
if (strncasecmp((regtype + len - 4), "_tcp", 4) && strncasecmp((regtype + len - 4), "_udp", 4)) return kDNSServiceErr_BadParam; |
| 123 |
|
| 124 |
if (service && *service) |
| 125 |
{ |
| 126 |
while (*s) |
| 127 |
{ |
| 128 |
unsigned char c = *s++; |
| 129 |
if (c <= ' ') |
| 130 |
{ |
| 131 |
if (fn+4 >= lim) goto fail; |
| 132 |
*fn++ = '\\'; |
| 133 |
*fn++ = '0' + (c / 100); |
| 134 |
*fn++ = '0' + (c / 10) % 10; |
| 135 |
c = '0' + (c ) % 10; |
| 136 |
} |
| 137 |
else if (c == '.' || (c == '\\')) |
| 138 |
{ |
| 139 |
if (fn+2 >= lim) goto fail; |
| 140 |
*fn++ = '\\'; |
| 141 |
} |
| 142 |
else |
| 143 |
if (fn+1 >= lim) goto fail; |
| 144 |
*fn++ = (char)c; |
| 145 |
} |
| 146 |
*fn++ = '.'; |
| 147 |
} |
| 148 |
|
| 149 |
while (*r) if (fn+1 >= lim) goto fail; else *fn++ = *r++; |
| 150 |
if (!DomainEndsInDot(regtype)) { if (fn+1 >= lim) goto fail; else *fn++ = '.'; } |
| 151 |
|
| 152 |
while (*d) if (fn+1 >= lim) goto fail; else *fn++ = *d++; |
| 153 |
if (!DomainEndsInDot(domain)) { if (fn+1 >= lim) goto fail; else *fn++ = '.'; } |
| 154 |
|
| 155 |
*fn = '\0'; |
| 156 |
return kDNSServiceErr_NoError; |
| 157 |
|
| 158 |
fail: |
| 159 |
*fn = '\0'; |
| 160 |
return kDNSServiceErr_BadParam; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
typedef struct _TXTRecordRefRealType |
| 170 |
{ |
| 171 |
uint8_t *buffer; |
| 172 |
uint16_t buflen; |
| 173 |
uint16_t datalen; |
| 174 |
uint16_t malloced; |
| 175 |
} TXTRecordRefRealType; |
| 176 |
|
| 177 |
#define txtRec ((TXTRecordRefRealType*)txtRecord) |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
struct CompileTimeAssertionCheck_dnssd_clientlib |
| 182 |
{ |
| 183 |
char assert0[(sizeof(TXTRecordRefRealType) <= 16) ? 1 : -1]; |
| 184 |
}; |
| 185 |
|
| 186 |
void DNSSD_API TXTRecordCreate |
| 187 |
( |
| 188 |
TXTRecordRef *txtRecord, |
| 189 |
uint16_t bufferLen, |
| 190 |
void *buffer |
| 191 |
) |
| 192 |
{ |
| 193 |
txtRec->buffer = static_cast<uint8_t *>(buffer); |
| 194 |
txtRec->buflen = buffer ? bufferLen : (uint16_t)0; |
| 195 |
txtRec->datalen = 0; |
| 196 |
txtRec->malloced = 0; |
| 197 |
} |
| 198 |
|
| 199 |
void DNSSD_API TXTRecordDeallocate(TXTRecordRef *txtRecord) |
| 200 |
{ |
| 201 |
if (txtRec->malloced) free(txtRec->buffer); |
| 202 |
} |
| 203 |
|
| 204 |
DNSServiceErrorType DNSSD_API TXTRecordSetValue |
| 205 |
( |
| 206 |
TXTRecordRef *txtRecord, |
| 207 |
const char *key, |
| 208 |
uint8_t valueSize, |
| 209 |
const void *value |
| 210 |
) |
| 211 |
{ |
| 212 |
uint8_t *start, *p; |
| 213 |
const char *k; |
| 214 |
unsigned long keysize, keyvalsize; |
| 215 |
|
| 216 |
for (k = key; *k; k++) if (*k < 0x20 || *k > 0x7E || *k == '=') return(kDNSServiceErr_Invalid); |
| 217 |
keysize = (unsigned long)(k - key); |
| 218 |
keyvalsize = 1 + keysize + (value ? (1 + valueSize) : 0); |
| 219 |
if (keysize < 1 || keyvalsize > 255) return(kDNSServiceErr_Invalid); |
| 220 |
(void)TXTRecordRemoveValue(txtRecord, key); |
| 221 |
if (txtRec->datalen + keyvalsize > txtRec->buflen) |
| 222 |
{ |
| 223 |
unsigned char *newbuf; |
| 224 |
unsigned long newlen = txtRec->datalen + keyvalsize; |
| 225 |
if (newlen > 0xFFFF) return(kDNSServiceErr_Invalid); |
| 226 |
newbuf = static_cast<unsigned char *>(malloc((size_t)newlen)); |
| 227 |
if (!newbuf) return(kDNSServiceErr_NoMemory); |
| 228 |
memcpy(newbuf, txtRec->buffer, txtRec->datalen); |
| 229 |
if (txtRec->malloced) free(txtRec->buffer); |
| 230 |
txtRec->buffer = newbuf; |
| 231 |
txtRec->buflen = (uint16_t)(newlen); |
| 232 |
txtRec->malloced = 1; |
| 233 |
} |
| 234 |
start = txtRec->buffer + txtRec->datalen; |
| 235 |
p = start + 1; |
| 236 |
memcpy(p, key, keysize); |
| 237 |
p += keysize; |
| 238 |
if (value) |
| 239 |
{ |
| 240 |
*p++ = '='; |
| 241 |
memcpy(p, value, valueSize); |
| 242 |
p += valueSize; |
| 243 |
} |
| 244 |
*start = (uint8_t)(p - start - 1); |
| 245 |
txtRec->datalen += p - start; |
| 246 |
return(kDNSServiceErr_NoError); |
| 247 |
} |
| 248 |
|
| 249 |
DNSServiceErrorType DNSSD_API TXTRecordRemoveValue |
| 250 |
( |
| 251 |
TXTRecordRef *txtRecord, |
| 252 |
const char *key |
| 253 |
) |
| 254 |
{ |
| 255 |
unsigned long keylen, itemlen, remainder; |
| 256 |
uint8_t *item = InternalTXTRecordSearch(txtRec->datalen, txtRec->buffer, key, &keylen); |
| 257 |
if (!item) return(kDNSServiceErr_NoSuchKey); |
| 258 |
itemlen = (unsigned long)(1 + item[0]); |
| 259 |
remainder = (unsigned long)((txtRec->buffer + txtRec->datalen) - (item + itemlen)); |
| 260 |
|
| 261 |
memmove(item, item + itemlen, remainder); |
| 262 |
txtRec->datalen -= itemlen; |
| 263 |
return(kDNSServiceErr_NoError); |
| 264 |
} |
| 265 |
|
| 266 |
uint16_t DNSSD_API TXTRecordGetLength (const TXTRecordRef *txtRecord) { return(txtRec->datalen); } |
| 267 |
const void * DNSSD_API TXTRecordGetBytesPtr(const TXTRecordRef *txtRecord) { return(txtRec->buffer); } |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
int DNSSD_API TXTRecordContainsKey |
| 276 |
( |
| 277 |
uint16_t txtLen, |
| 278 |
const void *txtRecord, |
| 279 |
const char *key |
| 280 |
) |
| 281 |
{ |
| 282 |
unsigned long keylen; |
| 283 |
return (InternalTXTRecordSearch(txtLen, txtRecord, key, &keylen) ? 1 : 0); |
| 284 |
} |
| 285 |
|
| 286 |
const void * DNSSD_API TXTRecordGetValuePtr |
| 287 |
( |
| 288 |
uint16_t txtLen, |
| 289 |
const void *txtRecord, |
| 290 |
const char *key, |
| 291 |
uint8_t *valueLen |
| 292 |
) |
| 293 |
{ |
| 294 |
unsigned long keylen; |
| 295 |
uint8_t *item = InternalTXTRecordSearch(txtLen, txtRecord, key, &keylen); |
| 296 |
if (!item || item[0] <= keylen) return(NULL); |
| 297 |
*valueLen = (uint8_t)(item[0] - (keylen + 1)); |
| 298 |
return (item + 1 + keylen + 1); |
| 299 |
} |
| 300 |
|
| 301 |
uint16_t DNSSD_API TXTRecordGetCount |
| 302 |
( |
| 303 |
uint16_t txtLen, |
| 304 |
const void *txtRecord |
| 305 |
) |
| 306 |
{ |
| 307 |
uint16_t count = 0; |
| 308 |
uint8_t *p = (uint8_t*)txtRecord; |
| 309 |
uint8_t *e = p + txtLen; |
| 310 |
while (p<e) { p += 1 + p[0]; count++; } |
| 311 |
return((p>e) ? (uint16_t)0 : count); |
| 312 |
} |
| 313 |
|
| 314 |
DNSServiceErrorType DNSSD_API TXTRecordGetItemAtIndex |
| 315 |
( |
| 316 |
uint16_t txtLen, |
| 317 |
const void *txtRecord, |
| 318 |
uint16_t itemIndex, |
| 319 |
uint16_t keyBufLen, |
| 320 |
char *key, |
| 321 |
uint8_t *valueLen, |
| 322 |
const void **value |
| 323 |
) |
| 324 |
{ |
| 325 |
uint16_t count = 0; |
| 326 |
uint8_t *p = (uint8_t*)txtRecord; |
| 327 |
uint8_t *e = p + txtLen; |
| 328 |
while (p<e && count<itemIndex) { p += 1 + p[0]; count++; } |
| 329 |
if (p<e && p + 1 + p[0] <= e) |
| 330 |
{ |
| 331 |
uint8_t *x = p+1; |
| 332 |
unsigned long len = 0; |
| 333 |
e = p + 1 + p[0]; |
| 334 |
while (x+len<e && x[len] != '=') len++; |
| 335 |
if (len >= keyBufLen) return(kDNSServiceErr_NoMemory); |
| 336 |
memcpy(key, x, len); |
| 337 |
key[len] = 0; |
| 338 |
if (x+len<e) |
| 339 |
{ |
| 340 |
*value = x + len + 1; |
| 341 |
*valueLen = (uint8_t)(p[0] - (len + 1)); |
| 342 |
} |
| 343 |
else |
| 344 |
{ |
| 345 |
*value = NULL; |
| 346 |
*valueLen = 0; |
| 347 |
} |
| 348 |
return(kDNSServiceErr_NoError); |
| 349 |
} |
| 350 |
return(kDNSServiceErr_Invalid); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
#define STRINGIFY_ARGUMENT_WITHOUT_EXPANSION(s) #s |
| 365 |
#define STRINGIFY(s) STRINGIFY_ARGUMENT_WITHOUT_EXPANSION(s) |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
const char VersionString_SCCS_libdnssd[] = "@(#) libdns_sd " STRINGIFY(mDNSResponderVersion) " (" __DATE__ " " __TIME__ ")"; |
| 370 |
}} |