4b22b9337f359bfd063322244f5336cc7c6ffcfars/* -*- Mode: C; tab-width: 4 -*-
cda73f64f20b8a0afc4909f5ea1f055ec7913856Toomas Soome * Copyright (c) 2004-2011 Apple Inc. All rights reserved.
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * Redistribution and use in source and binary forms, with or without
4b22b9337f359bfd063322244f5336cc7c6ffcfars * modification, are permitted provided that the following conditions are met:
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * 1. Redistributions of source code must retain the above copyright notice,
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * this list of conditions and the following disclaimer.
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * 2. Redistributions in binary form must reproduce the above copyright notice,
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * this list of conditions and the following disclaimer in the documentation
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * and/or other materials provided with the distribution.
cda73f64f20b8a0afc4909f5ea1f055ec7913856Toomas Soome * 3. Neither the name of Apple Inc. ("Apple") nor the names of its
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * contributors may be used to endorse or promote products derived from this
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * software without specific prior written permission.
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
4b22b9337f359bfd063322244f5336cc7c6ffcfars * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4b22b9337f359bfd063322244f5336cc7c6ffcfars#if MDNS_BUILDINGSHAREDLIBRARY || MDNS_BUILDINGSTUBLIBRARY
4b22b9337f359bfd063322244f5336cc7c6ffcfars#if defined(_WIN32)
4b22b9337f359bfd063322244f5336cc7c6ffcfars// disable warning "conversion from <data> to uint16_t"
4b22b9337f359bfd063322244f5336cc7c6ffcfars/*********************************************************************************************
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome* Supporting Functions
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome*********************************************************************************************/
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome#define mDNSIsDigit(X) ((X) >= '0' && (X) <= '9')
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome// DomainEndsInDot returns 1 if name ends with a dot, 0 otherwise
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome// (DNSServiceConstructFullName depends this returning 1 for true, rather than any non-zero value meaning true)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (dom[0] == '\\') // advance past escaped byte sequence
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (mDNSIsDigit(dom[1]) && mDNSIsDigit(dom[2]) && mDNSIsDigit(dom[3]))
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome const char *key,
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome unsigned long *keylen
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome p += 1 + p[0];
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (p <= e && *keylen <= x[0] && !strncasecmp(key, (char*)x+1, *keylen))
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (*keylen == x[0] || x[1+*keylen] == '=') return(x);
4b22b9337f359bfd063322244f5336cc7c6ffcfars/*********************************************************************************************
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome* General Utility Functions
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome*********************************************************************************************/
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome// Note: Need to make sure we don't write more than kDNSServiceMaxDomainName (1009) bytes to fullName
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome// In earlier builds this constant was defined to be 1005, so to avoid buffer overruns on clients
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome// compiled with that constant we'll actually limit the output to 1005 bytes.
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas SoomeDNSServiceErrorType DNSSD_API DNSServiceConstructFullName
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome const char *const regtype,
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome const char *const domain
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome const size_t len = !regtype ? 0 : strlen(regtype) - DomainEndsInDot(regtype);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome const char *s = service;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome const char *r = regtype;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome const char *d = domain;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome // regtype must be at least "x._udp" or "x._tcp"
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (len < 6 || !domain || !domain[0]) return kDNSServiceErr_BadParam;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (strncasecmp((regtype + len - 4), "_tcp", 4) && strncasecmp((regtype + len - 4), "_udp", 4)) return kDNSServiceErr_BadParam;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome unsigned char c = *s++; // Needs to be unsigned, or values like 0xFF will be interpreted as < 32
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (c <= ' ') // Escape non-printable characters
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome else if (c == '.' || (c == '\\')) // Escape dot and backslash literals
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome *fn++ = (char)c;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome while (*r) if (fn+1 >= lim) goto fail;else *fn++ = *r++;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (!DomainEndsInDot(regtype)) { if (fn+1 >= lim) goto fail;else *fn++ = '.';}
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome while (*d) if (fn+1 >= lim) goto fail;else *fn++ = *d++;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (!DomainEndsInDot(domain)) { if (fn+1 >= lim) goto fail;else *fn++ = '.';}
4b22b9337f359bfd063322244f5336cc7c6ffcfars/*********************************************************************************************
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome* TXT Record Construction Functions
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome*********************************************************************************************/
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome uint16_t malloced; // Non-zero if buffer was allocated via malloc()
4b22b9337f359bfd063322244f5336cc7c6ffcfars// The opaque storage defined in the public dns_sd.h header is 16 bytes;
4b22b9337f359bfd063322244f5336cc7c6ffcfars// make sure we don't exceed that.
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soomestruct CompileTimeAssertionCheck_dnssd_clientlib
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome char assert0[(sizeof(TXTRecordRefRealType) <= 16) ? 1 : -1];
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome txtRec->buflen = buffer ? bufferLen : (uint16_t)0;
4b22b9337f359bfd063322244f5336cc7c6ffcfarsvoid DNSSD_API TXTRecordDeallocate(TXTRecordRef *txtRecord)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome const char *key,
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome const char *k;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome for (k = key; *k; k++) if (*k < 0x20 || *k > 0x7E || *k == '=') return(kDNSServiceErr_Invalid);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome keyvalsize = 1 + keysize + (value ? (1 + valueSize) : 0);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (keysize < 1 || keyvalsize > 255) return(kDNSServiceErr_Invalid);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (txtRec->datalen + keyvalsize > txtRec->buflen)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome unsigned long newlen = txtRec->datalen + keyvalsize;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (newlen > 0xFFFF) return(kDNSServiceErr_Invalid);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome memcpy(newbuf, txtRec->buffer, txtRec->datalen);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome uint8_t *item = InternalTXTRecordSearch(txtRec->datalen, txtRec->buffer, key, &keylen);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome remainder = (unsigned long)((txtRec->buffer + txtRec->datalen) - (item + itemlen));
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome // Use memmove because memcpy behaviour is undefined for overlapping regions
4b22b9337f359bfd063322244f5336cc7c6ffcfarsuint16_t DNSSD_API TXTRecordGetLength (const TXTRecordRef *txtRecord) { return(txtRec->datalen); }
4b22b9337f359bfd063322244f5336cc7c6ffcfarsconst void * DNSSD_API TXTRecordGetBytesPtr(const TXTRecordRef *txtRecord) { return(txtRec->buffer); }
4b22b9337f359bfd063322244f5336cc7c6ffcfars/*********************************************************************************************
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome* TXT Record Parsing Functions
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome*********************************************************************************************/
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome return (InternalTXTRecordSearch(txtLen, txtRecord, key, &keylen) ? 1 : 0);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome const char *key,
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome uint8_t *item = InternalTXTRecordSearch(txtLen, txtRecord, key, &keylen);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (!item || item[0] <= keylen) return(NULL); // If key not found, or found with no value, return NULL
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome while (p<e && count<itemIndex) { p += 1 + p[0]; count++; } // Find requested item
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome e = p + 1 + p[0];
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (len >= keyBufLen) return(kDNSServiceErr_NoMemory);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome/*********************************************************************************************
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome* SCCS-compatible version string
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome*********************************************************************************************/
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome// For convenience when using the "strings" command, this is the last thing in the file
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome// Note: The C preprocessor stringify operator ('#') makes a string from its argument, without macro expansion
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome// e.g. If "version" is #define'd to be "4", then STRINGIFY_AWE(version) will return the string "version", not "4"
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome// To expand "version" to its value before making the string, use STRINGIFY(version) instead
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome#define STRINGIFY_ARGUMENT_WITHOUT_EXPANSION(s) # s
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome#define STRINGIFY(s) STRINGIFY_ARGUMENT_WITHOUT_EXPANSION(s)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome// NOT static -- otherwise the compiler may optimize it out
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome// The "@(#) " pattern is a special prefix the "what" command looks for
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soomeconst char VersionString_SCCS_libdnssd[] = "@(#) libdns_sd " STRINGIFY(mDNSResponderVersion) " (" __DATE__ " " __TIME__ ")";
c1de7575521deda5ee777b8054a9c5b12f15b1eeRichard Loweconst char VersionString_SCCS_libdnssd[] = "@(#) libdns_sd " STRINGIFY(mDNSResponderVersion);