/* -*- Mode: C; tab-width: 4 -*- * * Copyright (c) 2004-2018 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of Apple Inc. ("Apple") nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include "dns_sd.h" #include "mdns_strict.h" #if defined(_WIN32) // disable warning "conversion from to uint16_t" #pragma warning(disable:4244) #define strncasecmp _strnicmp #define strcasecmp _stricmp #endif /********************************************************************************************* * * Supporting Functions * *********************************************************************************************/ #define mDNSIsDigit(X) ((X) >= '0' && (X) <= '9') // DomainEndsInDot returns 1 if name ends with a dot, 0 otherwise // (DNSServiceConstructFullName depends this returning 1 for true, rather than any non-zero value meaning true) static int DomainEndsInDot(const char *dom) { while (dom[0] && dom[1]) { if (dom[0] == '\\') // advance past escaped byte sequence { if (mDNSIsDigit(dom[1]) && mDNSIsDigit(dom[2]) && mDNSIsDigit(dom[3])) dom += 4; // If "\ddd" then skip four else dom += 2; // else if "\x" then skip two } else dom++; // else goto next character } return (dom[0] == '.'); } static const uint8_t *InternalTXTRecordSearch ( uint16_t txtLen, const void *txtRecord, const char *key, unsigned long *keylen ) { const uint8_t *p = (const uint8_t*)txtRecord; const uint8_t *e = p + txtLen; *keylen = (unsigned long) strlen(key); while (p= lim) goto fail; *fn++ = '\\'; *fn++ = '0' + (c / 100); *fn++ = '0' + (c / 10) % 10; c = '0' + (c ) % 10; } else if (c == '.' || (c == '\\')) // Escape dot and backslash literals { if (fn+2 >= lim) goto fail; *fn++ = '\\'; } else if (fn+1 >= lim) goto fail; *fn++ = (char)c; } *fn++ = '.'; } while (*r) if (fn+1 >= lim) goto fail;else *fn++ = *r++; if (!DomainEndsInDot(regtype)) { if (fn+1 >= lim) goto fail;else *fn++ = '.';} while (*d) if (fn+1 >= lim) goto fail;else *fn++ = *d++; if (!DomainEndsInDot(domain)) { if (fn+1 >= lim) goto fail;else *fn++ = '.';} *fn = '\0'; return kDNSServiceErr_NoError; fail: *fn = '\0'; return kDNSServiceErr_BadParam; } /********************************************************************************************* * * TXT Record Construction Functions * *********************************************************************************************/ typedef struct _TXTRecordRefRealType { uint8_t *buffer; // Pointer to data uint16_t buflen; // Length of buffer uint16_t datalen; // Length currently in use uint16_t malloced; // Non-zero if buffer was allocated via malloc() } TXTRecordRefRealType; #define txtRec ((TXTRecordRefRealType*)txtRecord) // The opaque storage defined in the public dns_sd.h header is 16 bytes; // make sure we don't exceed that. struct CompileTimeAssertionCheck_dnssd_clientlib { char assert0[(sizeof(TXTRecordRefRealType) <= 16) ? 1 : -1]; }; void DNSSD_API TXTRecordCreate ( TXTRecordRef *txtRecord, uint16_t bufferLen, void *buffer ) { txtRec->buffer = buffer; txtRec->buflen = buffer ? bufferLen : (uint16_t)0; txtRec->datalen = 0; txtRec->malloced = 0; } void DNSSD_API TXTRecordDeallocate(TXTRecordRef *txtRecord) { if (txtRec->malloced) mdns_free(txtRec->buffer); } DNSServiceErrorType DNSSD_API TXTRecordSetValue ( TXTRecordRef *txtRecord, const char *key, uint8_t valueSize, const void *value ) { uint8_t *start, *p; const char *k; unsigned long keysize, keyvalsize; for (k = key; *k; k++) if (*k < 0x20 || *k > 0x7E || *k == '=') return(kDNSServiceErr_Invalid); keysize = (unsigned long)(k - key); keyvalsize = 1 + keysize + (value ? (1 + valueSize) : 0); if (keysize < 1 || keyvalsize > 255) return(kDNSServiceErr_Invalid); (void)TXTRecordRemoveValue(txtRecord, key); if (txtRec->datalen + keyvalsize > txtRec->buflen) { unsigned char *newbuf; unsigned long newlen = txtRec->datalen + keyvalsize; if (newlen > 0xFFFF) return(kDNSServiceErr_Invalid); newbuf = mdns_malloc((size_t)newlen); if (!newbuf) return(kDNSServiceErr_NoMemory); memcpy(newbuf, txtRec->buffer, txtRec->datalen); if (txtRec->malloced) mdns_free(txtRec->buffer); txtRec->buffer = newbuf; txtRec->buflen = (uint16_t)(newlen); txtRec->malloced = 1; } start = txtRec->buffer + txtRec->datalen; p = start + 1; memcpy(p, key, keysize); p += keysize; if (value) { *p++ = '='; memcpy(p, value, valueSize); p += valueSize; } *start = (uint8_t)(p - start - 1); txtRec->datalen += p - start; return(kDNSServiceErr_NoError); } DNSServiceErrorType DNSSD_API TXTRecordRemoveValue ( TXTRecordRef *txtRecord, const char *key ) { unsigned long keylen, itemlen, remainder; uint8_t *item = (uint8_t *)InternalTXTRecordSearch(txtRec->datalen, txtRec->buffer, key, &keylen); if (!item) return(kDNSServiceErr_NoSuchKey); itemlen = (unsigned long)(1 + item[0]); remainder = (unsigned long)((txtRec->buffer + txtRec->datalen) - (item + itemlen)); // Use memmove because memcpy behaviour is undefined for overlapping regions memmove(item, item + itemlen, remainder); txtRec->datalen -= itemlen; return(kDNSServiceErr_NoError); } uint16_t DNSSD_API TXTRecordGetLength(const TXTRecordRef *txtRecord) { return(((const TXTRecordRefRealType*)txtRecord)->datalen); } const void * DNSSD_API TXTRecordGetBytesPtr(const TXTRecordRef *txtRecord) { return(((const TXTRecordRefRealType*)txtRecord)->buffer); } /********************************************************************************************* * * TXT Record Parsing Functions * *********************************************************************************************/ int DNSSD_API TXTRecordContainsKey ( uint16_t txtLen, const void *txtRecord, const char *key ) { unsigned long keylen; return (InternalTXTRecordSearch(txtLen, txtRecord, key, &keylen) ? 1 : 0); } const void * DNSSD_API TXTRecordGetValuePtr ( uint16_t txtLen, const void *txtRecord, const char *key, uint8_t *valueLen ) { unsigned long keylen; const uint8_t *item = InternalTXTRecordSearch(txtLen, txtRecord, key, &keylen); if (!item || item[0] <= keylen) return(NULL); // If key not found, or found with no value, return NULL *valueLen = (uint8_t)(item[0] - (keylen + 1)); return (item + 1 + keylen + 1); } uint16_t DNSSD_API TXTRecordGetCount ( uint16_t txtLen, const void *txtRecord ) { uint16_t count = 0; const uint8_t *p = (const uint8_t*)txtRecord; const uint8_t *e = p + txtLen; while (pe) ? (uint16_t)0 : count); } DNSServiceErrorType DNSSD_API TXTRecordGetItemAtIndex ( uint16_t txtLen, const void *txtRecord, uint16_t itemIndex, uint16_t keyBufLen, char *key, uint8_t *valueLen, const void **value ) { uint16_t count = 0; const uint8_t *p = (const uint8_t*)txtRecord; const uint8_t *e = p + txtLen; while (p= keyBufLen) return(kDNSServiceErr_NoMemory); memcpy(key, x, len); key[len] = 0; if (x+len