| 1 | /* |
| 2 | * Copyright (c) 2004, Apple Computer, Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * 1. Redistributions of source code must retain the above copyright notice, |
| 8 | * this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 10 | * this list of conditions and the following disclaimer in the documentation |
| 11 | * and/or other materials provided with the distribution. |
| 12 | * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of its |
| 13 | * contributors may be used to endorse or promote products derived from this |
| 14 | * software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | #include "mdnsderived.h" |
| 28 | #include "cstddef" |
| 29 | #include "cstring" |
| 30 | #ifdef _WIN32 |
| 31 | #define strncasecmp _strnicmp |
| 32 | #endif |
| 33 | |
| 34 | namespace ZeroConf { |
| 35 | namespace Internal { |
| 36 | |
| 37 | // DomainEndsInDot returns 1 if name ends with a dot, 0 otherwise |
| 38 | // (DNSServiceConstructFullName depends this returning 1 for true, rather than any non-zero value meaning true) |
| 39 | static int DomainEndsInDot(const char *dom) |
| 40 | { |
| 41 | while (dom[0] && dom[1]) { |
| 42 | if (dom[0] == '\\') { // advance past escaped byte sequence |
| 43 | if ('0' <= dom[1] && dom[1] <= '9' && |
| 44 | '0' <= dom[2] && dom[2] <= '9' && |
| 45 | '0' <= dom[3] && dom[3] <= '9') |
| 46 | { |
| 47 | dom += 4; // If "\ddd" then skip four |
| 48 | } else { |
| 49 | dom += 2; // else if "\x" then skip two |
| 50 | } |
| 51 | } else { |
| 52 | dom++; // else goto next character |
| 53 | } |
| 54 | } |
| 55 | return (dom[0] == '.'); |
| 56 | } |
| 57 | |
| 58 | // Note: Need to make sure we don't write more than kDNSServiceMaxDomainName (1009) bytes to fullName |
| 59 | // In earlier builds this constant was defined to be 1005, so to avoid buffer overruns on clients |
| 60 | // compiled with that constant we'll actually limit the output to 1005 bytes. |
| 61 | DNSServiceErrorType myDNSServiceConstructFullName(char *const fullName, |
| 62 | const char *const service, // May be NULL |
| 63 | const char *const regtype, |
| 64 | const char *const domain) |
| 65 | { |
| 66 | const size_t len = !regtype ? 0 : strlen(regtype) - DomainEndsInDot(regtype); |
| 67 | char *fn = fullName; |
| 68 | char *const lim = fullName + 1005; |
| 69 | const char *s = service; |
| 70 | const char *r = regtype; |
| 71 | const char *d = domain; |
| 72 | |
| 73 | // regtype must be at least "x._udp" or "x._tcp" |
| 74 | if (len < 6 || !domain || !domain[0]) return kDNSServiceErr_BadParam; |
| 75 | if (strncasecmp((regtype + len - 4), "_tcp", 4) && strncasecmp((regtype + len - 4), "_udp", 4)) return kDNSServiceErr_BadParam; |
| 76 | |
| 77 | if (service && *service) |
| 78 | { |
| 79 | while (*s) |
| 80 | { |
| 81 | unsigned char c = *s++; // Needs to be unsigned, or values like 0xFF will be interpreted as < 32 |
| 82 | if (c <= ' ') // Escape non-printable characters |
| 83 | { |
| 84 | if (fn + 4 >= lim) goto fail; |
| 85 | *fn++ = '\\'; |
| 86 | *fn++ = '0' + (c / 100); |
| 87 | *fn++ = '0' + (c / 10) % 10; |
| 88 | c = '0' + (c ) % 10; |
| 89 | } |
| 90 | else if (c == '.' || (c == '\\')) // Escape dot and backslash literals |
| 91 | { |
| 92 | if (fn + 2 >= lim) goto fail; |
| 93 | *fn++ = '\\'; |
| 94 | } |
| 95 | else |
| 96 | if (fn + 1 >= lim) goto fail; |
| 97 | *fn++ = (char)c; |
| 98 | } |
| 99 | *fn++ = '.'; |
| 100 | } |
| 101 | |
| 102 | while (*r) if (fn + 1 >= lim) goto fail; else *fn++ = *r++; |
| 103 | if (!DomainEndsInDot(regtype)) { if (fn + 1 >= lim) goto fail; else *fn++ = '.'; } |
| 104 | |
| 105 | while (*d) if (fn + 1 >= lim) goto fail; else *fn++ = *d++; |
| 106 | if (!DomainEndsInDot(domain)) { if (fn + 1 >= lim) goto fail; else *fn++ = '.'; } |
| 107 | |
| 108 | *fn = '\0'; |
| 109 | return kDNSServiceErr_NoError; |
| 110 | |
| 111 | fail: |
| 112 | *fn = '\0'; |
| 113 | return kDNSServiceErr_BadParam; |
| 114 | } |
| 115 | |
| 116 | uint16_t txtRecordGetCount(uint16_t txtLen, const void *txtRecord) |
| 117 | { |
| 118 | uint16_t count = 0; |
| 119 | uint8_t *p = (uint8_t*)txtRecord; |
| 120 | uint8_t *e = p + txtLen; |
| 121 | while (p<e) { p += 1 + p[0]; count++; } |
| 122 | return((p>e) ? (uint16_t)0 : count); |
| 123 | } |
| 124 | |
| 125 | DNSServiceErrorType txtRecordGetItemAtIndex(uint16_t txtLen, const void *txtRecord, |
| 126 | uint16_t itemIndex, uint16_t keyBufLen, char *key, uint8_t *valueLen, const void **value) |
| 127 | { |
| 128 | uint16_t count = 0; |
| 129 | uint8_t *p = (uint8_t*)txtRecord; |
| 130 | uint8_t *e = p + txtLen; |
| 131 | while (p<e && count<itemIndex) { p += 1 + p[0]; count++; } // Find requested item |
| 132 | if (p<e && p + 1 + p[0] <= e) { // If valid |
| 133 | uint8_t *x = p+1; |
| 134 | unsigned long len = 0; |
| 135 | e = p + 1 + p[0]; |
| 136 | while (x+len<e && x[len] != '=') len++; |
| 137 | if (len >= keyBufLen) return(kDNSServiceErr_NoMemory); |
| 138 | memcpy(key, x, len); |
| 139 | key[len] = 0; |
| 140 | if (x+len<e) { // If we found '=' |
| 141 | *value = x + len + 1; |
| 142 | *valueLen = (uint8_t)(p[0] - (len + 1)); |
| 143 | } else { |
| 144 | *value = NULL; |
| 145 | *valueLen = 0; |
| 146 | } |
| 147 | return(kDNSServiceErr_NoError); |
| 148 | } |
| 149 | return(kDNSServiceErr_Invalid); |
| 150 | } |
| 151 | |
| 152 | |
| 153 | } // namespace ZeroConf |
| 154 | } // namespace Internal |