Home | History | Annotate | Line # | Download | only in mDNSShared
      1 /* -*- Mode: C; tab-width: 4 -*-
      2  *
      3  * Copyright (c) 2004-2018 Apple Inc. All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are met:
      7  *
      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 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.
     16  *
     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.
     27  */
     28 
     29 #include <stdlib.h>
     30 #include <string.h>
     31 
     32 #include "dns_sd.h"
     33 #include "mdns_strict.h"
     34 
     35 #if defined(_WIN32)
     36 // disable warning "conversion from <data> to uint16_t"
     37 #pragma warning(disable:4244)
     38 #define strncasecmp _strnicmp
     39 #define strcasecmp _stricmp
     40 #endif
     41 
     42 /*********************************************************************************************
     43 *
     44 *  Supporting Functions
     45 *
     46 *********************************************************************************************/
     47 
     48 #define mDNSIsDigit(X)     ((X) >= '0' && (X) <= '9')
     49 
     50 // DomainEndsInDot returns 1 if name ends with a dot, 0 otherwise
     51 // (DNSServiceConstructFullName depends this returning 1 for true, rather than any non-zero value meaning true)
     52 
     53 static int DomainEndsInDot(const char *dom)
     54 {
     55     while (dom[0] && dom[1])
     56     {
     57         if (dom[0] == '\\') // advance past escaped byte sequence
     58         {
     59             if (mDNSIsDigit(dom[1]) && mDNSIsDigit(dom[2]) && mDNSIsDigit(dom[3]))
     60                 dom += 4;           // If "\ddd"    then skip four
     61             else dom += 2;          // else if "\x" then skip two
     62         }
     63         else dom++;                 // else goto next character
     64     }
     65     return (dom[0] == '.');
     66 }
     67 
     68 static const uint8_t *InternalTXTRecordSearch
     69 (
     70     uint16_t txtLen,
     71     const void       *txtRecord,
     72     const char       *key,
     73     unsigned long    *keylen
     74 )
     75 {
     76     const uint8_t *p = (const uint8_t*)txtRecord;
     77     const uint8_t *e = p + txtLen;
     78     *keylen = (unsigned long) strlen(key);
     79     while (p<e)
     80     {
     81         const uint8_t *x = p;
     82         p += 1 + p[0];
     83         if (p <= e && *keylen <= x[0] && !strncasecmp(key, (const char*)x+1, *keylen))
     84             if (*keylen == x[0] || x[1+*keylen] == '=') return(x);
     85     }
     86     return(NULL);
     87 }
     88 
     89 /*********************************************************************************************
     90 *
     91 *  General Utility Functions
     92 *
     93 *********************************************************************************************/
     94 
     95 // Note: Need to make sure we don't write more than kDNSServiceMaxDomainName (1009) bytes to fullName
     96 // In earlier builds this constant was defined to be 1005, so to avoid buffer overruns on clients
     97 // compiled with that constant we'll actually limit the output to 1005 bytes.
     98 
     99 DNSServiceErrorType DNSSD_API DNSServiceConstructFullName
    100 (
    101     char       *const fullName,
    102     const char *const service,      // May be NULL
    103     const char *const regtype,
    104     const char *const domain
    105 )
    106 {
    107     const size_t len = !regtype ? 0 : strlen(regtype) - DomainEndsInDot(regtype);
    108     char       *fn   = fullName;
    109     char *const lim  = fullName + 1005;
    110     const char *s    = service;
    111     const char *r    = regtype;
    112     const char *d    = domain;
    113 
    114     // regtype must be at least "x._udp" or "x._tcp"
    115     if (len < 6 || !domain || !domain[0]) return kDNSServiceErr_BadParam;
    116     if (strncasecmp((regtype + len - 4), "_tcp", 4) && strncasecmp((regtype + len - 4), "_udp", 4)) return kDNSServiceErr_BadParam;
    117 
    118     if (service && *service)
    119     {
    120         while (*s)
    121         {
    122             unsigned char c = *s++;             // Needs to be unsigned, or values like 0xFF will be interpreted as < 32
    123             if (c <= ' ')                       // Escape non-printable characters
    124             {
    125                 if (fn+4 >= lim) goto fail;
    126                 *fn++ = '\\';
    127                 *fn++ = '0' + (c / 100);
    128                 *fn++ = '0' + (c /  10) % 10;
    129                 c     = '0' + (c      ) % 10;
    130             }
    131             else if (c == '.' || (c == '\\'))   // Escape dot and backslash literals
    132             {
    133                 if (fn+2 >= lim) goto fail;
    134                 *fn++ = '\\';
    135             }
    136             else
    137             if (fn+1 >= lim) goto fail;
    138             *fn++ = (char)c;
    139         }
    140         *fn++ = '.';
    141     }
    142 
    143     while (*r) if (fn+1 >= lim) goto fail;else *fn++ = *r++;
    144     if (!DomainEndsInDot(regtype)) { if (fn+1 >= lim) goto fail;else *fn++ = '.';}
    145 
    146     while (*d) if (fn+1 >= lim) goto fail;else *fn++ = *d++;
    147     if (!DomainEndsInDot(domain)) { if (fn+1 >= lim) goto fail;else *fn++ = '.';}
    148 
    149     *fn = '\0';
    150     return kDNSServiceErr_NoError;
    151 
    152 fail:
    153     *fn = '\0';
    154     return kDNSServiceErr_BadParam;
    155 }
    156 
    157 /*********************************************************************************************
    158 *
    159 *   TXT Record Construction Functions
    160 *
    161 *********************************************************************************************/
    162 
    163 typedef struct _TXTRecordRefRealType
    164 {
    165     uint8_t  *buffer;       // Pointer to data
    166     uint16_t buflen;        // Length of buffer
    167     uint16_t datalen;       // Length currently in use
    168     uint16_t malloced;  // Non-zero if buffer was allocated via malloc()
    169 } TXTRecordRefRealType;
    170 
    171 #define txtRec ((TXTRecordRefRealType*)txtRecord)
    172 
    173 // The opaque storage defined in the public dns_sd.h header is 16 bytes;
    174 // make sure we don't exceed that.
    175 struct CompileTimeAssertionCheck_dnssd_clientlib
    176 {
    177     char assert0[(sizeof(TXTRecordRefRealType) <= 16) ? 1 : -1];
    178 };
    179 
    180 void DNSSD_API TXTRecordCreate
    181 (
    182     TXTRecordRef     *txtRecord,
    183     uint16_t bufferLen,
    184     void             *buffer
    185 )
    186 {
    187     txtRec->buffer   = buffer;
    188     txtRec->buflen   = buffer ? bufferLen : (uint16_t)0;
    189     txtRec->datalen  = 0;
    190     txtRec->malloced = 0;
    191 }
    192 
    193 void DNSSD_API TXTRecordDeallocate(TXTRecordRef *txtRecord)
    194 {
    195     if (txtRec->malloced) mdns_free(txtRec->buffer);
    196 }
    197 
    198 DNSServiceErrorType DNSSD_API TXTRecordSetValue
    199 (
    200     TXTRecordRef     *txtRecord,
    201     const char       *key,
    202     uint8_t valueSize,
    203     const void       *value
    204 )
    205 {
    206     uint8_t *start, *p;
    207     const char *k;
    208     unsigned long keysize, keyvalsize;
    209 
    210     for (k = key; *k; k++) if (*k < 0x20 || *k > 0x7E || *k == '=') return(kDNSServiceErr_Invalid);
    211     keysize = (unsigned long)(k - key);
    212     keyvalsize = 1 + keysize + (value ? (1 + valueSize) : 0);
    213     if (keysize < 1 || keyvalsize > 255) return(kDNSServiceErr_Invalid);
    214     (void)TXTRecordRemoveValue(txtRecord, key);
    215     if (txtRec->datalen + keyvalsize > txtRec->buflen)
    216     {
    217         unsigned char *newbuf;
    218         unsigned long newlen = txtRec->datalen + keyvalsize;
    219         if (newlen > 0xFFFF) return(kDNSServiceErr_Invalid);
    220         newbuf = mdns_malloc((size_t)newlen);
    221         if (!newbuf) return(kDNSServiceErr_NoMemory);
    222         memcpy(newbuf, txtRec->buffer, txtRec->datalen);
    223         if (txtRec->malloced) mdns_free(txtRec->buffer);
    224         txtRec->buffer = newbuf;
    225         txtRec->buflen = (uint16_t)(newlen);
    226         txtRec->malloced = 1;
    227     }
    228     start = txtRec->buffer + txtRec->datalen;
    229     p = start + 1;
    230     memcpy(p, key, keysize);
    231     p += keysize;
    232     if (value)
    233     {
    234         *p++ = '=';
    235         memcpy(p, value, valueSize);
    236         p += valueSize;
    237     }
    238     *start = (uint8_t)(p - start - 1);
    239     txtRec->datalen += p - start;
    240     return(kDNSServiceErr_NoError);
    241 }
    242 
    243 DNSServiceErrorType DNSSD_API TXTRecordRemoveValue
    244 (
    245     TXTRecordRef     *txtRecord,
    246     const char       *key
    247 )
    248 {
    249     unsigned long keylen, itemlen, remainder;
    250     uint8_t *item = (uint8_t *)InternalTXTRecordSearch(txtRec->datalen, txtRec->buffer, key, &keylen);
    251     if (!item) return(kDNSServiceErr_NoSuchKey);
    252     itemlen   = (unsigned long)(1 + item[0]);
    253     remainder = (unsigned long)((txtRec->buffer + txtRec->datalen) - (item + itemlen));
    254     // Use memmove because memcpy behaviour is undefined for overlapping regions
    255     memmove(item, item + itemlen, remainder);
    256     txtRec->datalen -= itemlen;
    257     return(kDNSServiceErr_NoError);
    258 }
    259 
    260 uint16_t DNSSD_API TXTRecordGetLength(const TXTRecordRef *txtRecord) { return(((const TXTRecordRefRealType*)txtRecord)->datalen); }
    261 const void * DNSSD_API TXTRecordGetBytesPtr(const TXTRecordRef *txtRecord) { return(((const TXTRecordRefRealType*)txtRecord)->buffer); }
    262 
    263 /*********************************************************************************************
    264 *
    265 *   TXT Record Parsing Functions
    266 *
    267 *********************************************************************************************/
    268 
    269 int DNSSD_API TXTRecordContainsKey
    270 (
    271     uint16_t txtLen,
    272     const void       *txtRecord,
    273     const char       *key
    274 )
    275 {
    276     unsigned long keylen;
    277     return (InternalTXTRecordSearch(txtLen, txtRecord, key, &keylen) ? 1 : 0);
    278 }
    279 
    280 const void * DNSSD_API TXTRecordGetValuePtr
    281 (
    282     uint16_t txtLen,
    283     const void       *txtRecord,
    284     const char       *key,
    285     uint8_t          *valueLen
    286 )
    287 {
    288     unsigned long keylen;
    289     const uint8_t *item = InternalTXTRecordSearch(txtLen, txtRecord, key, &keylen);
    290     if (!item || item[0] <= keylen) return(NULL);   // If key not found, or found with no value, return NULL
    291     *valueLen = (uint8_t)(item[0] - (keylen + 1));
    292     return (item + 1 + keylen + 1);
    293 }
    294 
    295 uint16_t DNSSD_API TXTRecordGetCount
    296 (
    297     uint16_t txtLen,
    298     const void       *txtRecord
    299 )
    300 {
    301     uint16_t count = 0;
    302     const uint8_t *p = (const uint8_t*)txtRecord;
    303     const uint8_t *e = p + txtLen;
    304     while (p<e) { p += 1 + p[0]; count++; }
    305     return((p>e) ? (uint16_t)0 : count);
    306 }
    307 
    308 DNSServiceErrorType DNSSD_API TXTRecordGetItemAtIndex
    309 (
    310     uint16_t txtLen,
    311     const void       *txtRecord,
    312     uint16_t itemIndex,
    313     uint16_t keyBufLen,
    314     char             *key,
    315     uint8_t          *valueLen,
    316     const void       **value
    317 )
    318 {
    319     uint16_t count = 0;
    320     const uint8_t *p = (const uint8_t*)txtRecord;
    321     const uint8_t *e = p + txtLen;
    322     while (p<e && count<itemIndex) { p += 1 + p[0]; count++; }  // Find requested item
    323     if (p<e && p + 1 + p[0] <= e)   // If valid
    324     {
    325         const uint8_t *x = p+1;
    326         unsigned long len = 0;
    327         e = p + 1 + p[0];
    328         while (x+len<e && x[len] != '=') len++;
    329         if (len >= keyBufLen) return(kDNSServiceErr_NoMemory);
    330         memcpy(key, x, len);
    331         key[len] = 0;
    332         if (x+len<e)        // If we found '='
    333         {
    334             *value = x + len + 1;
    335             *valueLen = (uint8_t)(p[0] - (len + 1));
    336         }
    337         else
    338         {
    339             *value = NULL;
    340             *valueLen = 0;
    341         }
    342         return(kDNSServiceErr_NoError);
    343     }
    344     return(kDNSServiceErr_Invalid);
    345 }
    346 
    347 /*********************************************************************************************
    348 *
    349 *   SCCS-compatible version string
    350 *
    351 *********************************************************************************************/
    352 
    353 // For convenience when using the "strings" command, this is the last thing in the file
    354 
    355 // Note: The C preprocessor stringify operator ('#') makes a string from its argument, without macro expansion
    356 // e.g. If "version" is #define'd to be "4", then STRINGIFY_AWE(version) will return the string "version", not "4"
    357 // To expand "version" to its value before making the string, use STRINGIFY(version) instead
    358 #define STRINGIFY_ARGUMENT_WITHOUT_EXPANSION(s) # s
    359 #define STRINGIFY(s) STRINGIFY_ARGUMENT_WITHOUT_EXPANSION(s)
    360 
    361 // The "used" variable attribute prevents a non-exported variable from being stripped, even if its visibility is hidden,
    362 // e.g., when compiling with -fvisibility=hidden.
    363 #if defined(__GNUC__)
    364 #define DNSSD_USED __attribute__((used))
    365 #else
    366 #define DNSSD_USED
    367 #endif
    368 
    369 #if defined(__GNUC__)
    370 #pragma GCC diagnostic push
    371 #pragma GCC diagnostic ignored "-Wdate-time"
    372 #endif
    373 // NOT static -- otherwise the compiler may optimize it out
    374 // The "@(#) " pattern is a special prefix the "what" command looks for
    375 const char VersionString_SCCS_libdnssd[] DNSSD_USED = "@(#) libdns_sd " STRINGIFY(mDNSResponderVersion)
    376 #ifndef MDNS_VERSIONSTR_NODTS
    377     " (" __DATE__ " " __TIME__ ")"
    378 #endif
    379 ;
    380 #if defined(__GNUC__)
    381 #pragma GCC diagnostic pop
    382 #endif
    383