| 1 |
/* |
| 2 |
* Copyright (c) 1985, 1993 |
| 3 |
* The Regents of the University of California. 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 |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* 3. Neither the name of the University nor the names of its contributors |
| 14 |
* may be used to endorse or promote products derived from this software |
| 15 |
* without specific prior written permission. |
| 16 |
* |
| 17 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 18 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 21 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 |
* SUCH DAMAGE. |
| 28 |
*/ |
| 29 |
|
| 30 |
/* |
| 31 |
* Portions Copyright (c) 1993 by Digital Equipment Corporation. |
| 32 |
* |
| 33 |
* Permission to use, copy, modify, and distribute this software for any |
| 34 |
* purpose with or without fee is hereby granted, provided that the above |
| 35 |
* copyright notice and this permission notice appear in all copies, and that |
| 36 |
* the name of Digital Equipment Corporation not be used in advertising or |
| 37 |
* publicity pertaining to distribution of the document or software without |
| 38 |
* specific, written prior permission. |
| 39 |
* |
| 40 |
* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL |
| 41 |
* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES |
| 42 |
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT |
| 43 |
* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
| 44 |
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 45 |
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
| 46 |
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
| 47 |
* SOFTWARE. |
| 48 |
*/ |
| 49 |
|
| 50 |
/* |
| 51 |
* Portions Copyright (c) 1996-1999 by Internet Software Consortium. |
| 52 |
* |
| 53 |
* Permission to use, copy, modify, and distribute this software for any |
| 54 |
* purpose with or without fee is hereby granted, provided that the above |
| 55 |
* copyright notice and this permission notice appear in all copies. |
| 56 |
* |
| 57 |
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS |
| 58 |
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES |
| 59 |
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE |
| 60 |
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
| 61 |
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 62 |
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
| 63 |
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
| 64 |
* SOFTWARE. |
| 65 |
*/ |
| 66 |
|
| 67 |
/* Original copyright ISC as above. Code modified specifically for ircd use from the following |
| 68 |
* orginal files in bind ... res_comp.c ns_name.c ns_netint.c res_init.c - Dianora */ |
| 69 |
|
| 70 |
#include "mowgli.h" |
| 71 |
|
| 72 |
#ifdef _WIN32 |
| 73 |
# define EMSGSIZE WSAEMSGSIZE |
| 74 |
#endif |
| 75 |
|
| 76 |
#define MOWGLI_DNS_NS_TYPE_ELT 0x40 /* EDNS0 extended label type */ |
| 77 |
#define MOWGLI_DNS_LABELTYPE_BITSTRING 0x41 |
| 78 |
|
| 79 |
/* from Hybrid Id: reslib.c 177 2005-10-22 09:05:05Z michael $ */ |
| 80 |
|
| 81 |
static const char digitvalue[256] = |
| 82 |
{ |
| 83 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 16 */ |
| 84 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 32 */ |
| 85 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 48 */ |
| 86 |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 64 */ |
| 87 |
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 80 */ |
| 88 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 96 */ |
| 89 |
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 112 */ |
| 90 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 128 */ |
| 91 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 92 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 93 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 94 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 95 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 96 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 97 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 98 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 256 */ |
| 99 |
}; |
| 100 |
|
| 101 |
static const char digits[] = "0123456789"; |
| 102 |
|
| 103 |
static int labellen(const unsigned char *lp); |
| 104 |
static bool mowgli_dns_is_special(int ch); |
| 105 |
static bool mowgli_dns_is_printable(int ch); |
| 106 |
static int mowgli_dns_decode_bitstring(const char **cpp, char *dn, const char *eom); |
| 107 |
static int mowgli_dns_ns_name_compress(const char *src, unsigned char *dst, size_t dstsiz, unsigned char **dnptrs, unsigned char **lastdnptr); |
| 108 |
static int mowgli_dns_dn_find(const unsigned char *, const unsigned char *, const unsigned char *const *, const unsigned char *const *); |
| 109 |
static int mowgli_dns_encode_bitsring(const char **, const char *, unsigned char **, unsigned char **, const char *); |
| 110 |
static int mowgli_dns_ns_name_uncompress(const unsigned char *, const unsigned char *, const unsigned char *, char *, size_t); |
| 111 |
static int mowgli_dns_ns_name_unpack(const unsigned char *, const unsigned char *, const unsigned char *, unsigned char *, size_t); |
| 112 |
static int mowgli_dns_ns_name_ntop(const char *, char *, size_t); |
| 113 |
static int mowgli_dns_ns_name_skip(const unsigned char **, const unsigned char *); |
| 114 |
static int mowgli_dns_mklower(int ch); |
| 115 |
|
| 116 |
/* |
| 117 |
* Expand compressed domain name 'comp_dn' to full domain name. |
| 118 |
* 'rmsg' is a pointer to the begining of the message, |
| 119 |
* 'eomorig' points to the first location after the message, |
| 120 |
* 'exp_dn' is a pointer to a buffer of size 'length' for the result. |
| 121 |
* Return size of compressed name or -1 if there was an error. |
| 122 |
*/ |
| 123 |
int |
| 124 |
mowgli_dns_dn_expand(const unsigned char *rmsg, const unsigned char *eom, const unsigned char *src, char *dst, int dstsiz) |
| 125 |
{ |
| 126 |
int n = mowgli_dns_ns_name_uncompress(rmsg, eom, src, dst, (size_t) dstsiz); |
| 127 |
|
| 128 |
if ((n > 0) && (dst[0] == '.')) |
| 129 |
dst[0] = '\0'; |
| 130 |
|
| 131 |
return n; |
| 132 |
} |
| 133 |
|
| 134 |
/* |
| 135 |
* mowgli_dns_ns_name_uncompress(rmsg, eom, src, dst, dstsiz) |
| 136 |
* Expand compressed domain name to presentation format. |
| 137 |
* return: |
| 138 |
* Number of bytes read out of `src', or -1 (with errno set). |
| 139 |
* note: |
| 140 |
* Root domain returns as "." not "". |
| 141 |
*/ |
| 142 |
static int |
| 143 |
mowgli_dns_ns_name_uncompress(const unsigned char *rmsg, const unsigned char *eom, const unsigned char *src, char *dst, size_t dstsiz) |
| 144 |
{ |
| 145 |
unsigned char tmp[MOWGLI_DNS_NS_MAXCDNAME]; |
| 146 |
int n; |
| 147 |
|
| 148 |
if ((n = mowgli_dns_ns_name_unpack(rmsg, eom, src, tmp, sizeof tmp)) == -1) |
| 149 |
return -1; |
| 150 |
|
| 151 |
if (mowgli_dns_ns_name_ntop((char *) tmp, dst, dstsiz) == -1) |
| 152 |
return -1; |
| 153 |
|
| 154 |
return n; |
| 155 |
} |
| 156 |
|
| 157 |
/* |
| 158 |
* mowgli_dns_ns_name_unpack(rmsg, eom, src, dst, dstsiz) |
| 159 |
* Unpack a domain name from a message, source may be compressed. |
| 160 |
* return: |
| 161 |
* -1 if it fails, or consumed octets if it succeeds. |
| 162 |
*/ |
| 163 |
static int |
| 164 |
mowgli_dns_ns_name_unpack(const unsigned char *rmsg, const unsigned char *eom, const unsigned char *src, unsigned char *dst, size_t dstsiz) |
| 165 |
{ |
| 166 |
const unsigned char *srcp, *dstlim; |
| 167 |
unsigned char *dstp; |
| 168 |
int n, len, checked, l; |
| 169 |
|
| 170 |
len = -1; |
| 171 |
checked = 0; |
| 172 |
dstp = dst; |
| 173 |
srcp = src; |
| 174 |
dstlim = dst + dstsiz; |
| 175 |
|
| 176 |
if ((srcp < rmsg) || (srcp >= eom)) |
| 177 |
{ |
| 178 |
errno = EMSGSIZE; |
| 179 |
return -1; |
| 180 |
} |
| 181 |
|
| 182 |
/* Fetch next label in domain name. */ |
| 183 |
while ((n = *srcp++) != 0) |
| 184 |
{ |
| 185 |
/* Check for indirection. */ |
| 186 |
switch (n & MOWGLI_DNS_NS_CMPRSFLAGS) |
| 187 |
{ |
| 188 |
case 0: |
| 189 |
case MOWGLI_DNS_NS_TYPE_ELT: |
| 190 |
|
| 191 |
/* Limit checks. */ |
| 192 |
if ((l = labellen(srcp - 1)) < 0) |
| 193 |
{ |
| 194 |
errno = EMSGSIZE; |
| 195 |
return -1; |
| 196 |
} |
| 197 |
|
| 198 |
if ((dstp + l + 1 >= dstlim) || (srcp + l >= eom)) |
| 199 |
{ |
| 200 |
errno = EMSGSIZE; |
| 201 |
return -1; |
| 202 |
} |
| 203 |
|
| 204 |
checked += l + 1; |
| 205 |
*dstp++ = n; |
| 206 |
memcpy(dstp, srcp, l); |
| 207 |
dstp += l; |
| 208 |
srcp += l; |
| 209 |
break; |
| 210 |
|
| 211 |
case MOWGLI_DNS_NS_CMPRSFLAGS: |
| 212 |
|
| 213 |
if (srcp >= eom) |
| 214 |
{ |
| 215 |
errno = EMSGSIZE; |
| 216 |
return -1; |
| 217 |
} |
| 218 |
|
| 219 |
if (len < 0) |
| 220 |
len = srcp - src + 1; |
| 221 |
|
| 222 |
srcp = rmsg + (((n & 0x3f) << 8) | (*srcp & 0xff)); |
| 223 |
|
| 224 |
if ((srcp < rmsg) || (srcp >= eom)) /* Out of range. */ |
| 225 |
{ |
| 226 |
errno = EMSGSIZE; |
| 227 |
return -1; |
| 228 |
} |
| 229 |
|
| 230 |
checked += 2; |
| 231 |
|
| 232 |
/* |
| 233 |
* Check for loops in the compressed name; |
| 234 |
* if we've looked at the whole message, |
| 235 |
* there must be a loop. |
| 236 |
*/ |
| 237 |
if (checked >= eom - rmsg) |
| 238 |
{ |
| 239 |
errno = EMSGSIZE; |
| 240 |
return -1; |
| 241 |
} |
| 242 |
|
| 243 |
break; |
| 244 |
|
| 245 |
default: |
| 246 |
errno = EMSGSIZE; |
| 247 |
return -1; /* flag error */ |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
*dstp = '\0'; |
| 252 |
|
| 253 |
if (len < 0) |
| 254 |
len = srcp - src; |
| 255 |
|
| 256 |
return len; |
| 257 |
} |
| 258 |
|
| 259 |
/* |
| 260 |
* mowgli_dns_ns_name_ntop(src, dst, dstsiz) |
| 261 |
* Convert an encoded domain name to mowgli_dns_is_printable ascii as per RFC1035. |
| 262 |
* return: |
| 263 |
* Number of bytes written to buffer, or -1 (with errno set) |
| 264 |
* notes: |
| 265 |
* The root is returned as "." |
| 266 |
* All other domains are returned in non absolute form |
| 267 |
*/ |
| 268 |
static int |
| 269 |
mowgli_dns_ns_name_ntop(const char *src, char *dst, size_t dstsiz) |
| 270 |
{ |
| 271 |
const char *cp; |
| 272 |
char *dn, *eom; |
| 273 |
unsigned char c; |
| 274 |
unsigned int n; |
| 275 |
int l; |
| 276 |
|
| 277 |
cp = src; |
| 278 |
dn = dst; |
| 279 |
eom = dst + dstsiz; |
| 280 |
|
| 281 |
while ((n = *cp++) != 0) |
| 282 |
{ |
| 283 |
if ((n & MOWGLI_DNS_NS_CMPRSFLAGS) == MOWGLI_DNS_NS_CMPRSFLAGS) |
| 284 |
{ |
| 285 |
/* Some kind of compression pointer. */ |
| 286 |
errno = EMSGSIZE; |
| 287 |
return -1; |
| 288 |
} |
| 289 |
|
| 290 |
if (dn != dst) |
| 291 |
{ |
| 292 |
if (dn >= eom) |
| 293 |
{ |
| 294 |
errno = EMSGSIZE; |
| 295 |
return -1; |
| 296 |
} |
| 297 |
|
| 298 |
*dn++ = '.'; |
| 299 |
} |
| 300 |
|
| 301 |
if ((l = labellen((const unsigned char *) (cp - 1))) < 0) |
| 302 |
{ |
| 303 |
errno = EMSGSIZE; /* XXX */ |
| 304 |
return -1; |
| 305 |
} |
| 306 |
|
| 307 |
if (dn + l >= eom) |
| 308 |
{ |
| 309 |
errno = EMSGSIZE; |
| 310 |
return -1; |
| 311 |
} |
| 312 |
|
| 313 |
if ((n & MOWGLI_DNS_NS_CMPRSFLAGS) == MOWGLI_DNS_NS_TYPE_ELT) |
| 314 |
{ |
| 315 |
int m; |
| 316 |
|
| 317 |
if (n != MOWGLI_DNS_LABELTYPE_BITSTRING) |
| 318 |
{ |
| 319 |
/* XXX: labellen should reject this case */ |
| 320 |
errno = EINVAL; |
| 321 |
return -1; |
| 322 |
} |
| 323 |
|
| 324 |
if ((m = mowgli_dns_decode_bitstring(&cp, dn, eom)) < 0) |
| 325 |
{ |
| 326 |
errno = EMSGSIZE; |
| 327 |
return -1; |
| 328 |
} |
| 329 |
|
| 330 |
dn += m; |
| 331 |
continue; |
| 332 |
} |
| 333 |
|
| 334 |
for ((void) NULL; l > 0; l--) |
| 335 |
{ |
| 336 |
c = *cp++; |
| 337 |
|
| 338 |
if (mowgli_dns_is_special(c)) |
| 339 |
{ |
| 340 |
if (dn + 1 >= eom) |
| 341 |
{ |
| 342 |
errno = EMSGSIZE; |
| 343 |
return -1; |
| 344 |
} |
| 345 |
|
| 346 |
*dn++ = '\\'; |
| 347 |
*dn++ = (char) c; |
| 348 |
} |
| 349 |
else if (!mowgli_dns_is_printable(c)) |
| 350 |
{ |
| 351 |
if (dn + 3 >= eom) |
| 352 |
{ |
| 353 |
errno = EMSGSIZE; |
| 354 |
return -1; |
| 355 |
} |
| 356 |
|
| 357 |
*dn++ = '\\'; |
| 358 |
*dn++ = digits[c / 100]; |
| 359 |
*dn++ = digits[(c % 100) / 10]; |
| 360 |
*dn++ = digits[c % 10]; |
| 361 |
} |
| 362 |
else |
| 363 |
{ |
| 364 |
if (dn >= eom) |
| 365 |
{ |
| 366 |
errno = EMSGSIZE; |
| 367 |
return -1; |
| 368 |
} |
| 369 |
|
| 370 |
*dn++ = (char) c; |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
if (dn == dst) |
| 376 |
{ |
| 377 |
if (dn >= eom) |
| 378 |
{ |
| 379 |
errno = EMSGSIZE; |
| 380 |
return -1; |
| 381 |
} |
| 382 |
|
| 383 |
*dn++ = '.'; |
| 384 |
} |
| 385 |
|
| 386 |
if (dn >= eom) |
| 387 |
{ |
| 388 |
errno = EMSGSIZE; |
| 389 |
return -1; |
| 390 |
} |
| 391 |
|
| 392 |
*dn++ = '\0'; |
| 393 |
return dn - dst; |
| 394 |
} |
| 395 |
|
| 396 |
/* |
| 397 |
* Pack domain name 'exp_dn' in presentation form into 'comp_dn'. |
| 398 |
* Return the size of the compressed name or -1. |
| 399 |
* 'length' is the size of the array pointed to by 'comp_dn'. |
| 400 |
*/ |
| 401 |
static int |
| 402 |
mowgli_dns_dn_comp(const char *src, unsigned char *dst, int dstsiz, unsigned char **dnptrs, unsigned char **lastdnptr) |
| 403 |
{ |
| 404 |
return mowgli_dns_ns_name_compress(src, dst, (size_t) dstsiz, dnptrs, lastdnptr); |
| 405 |
} |
| 406 |
|
| 407 |
/* |
| 408 |
* Skip over a compressed domain name. Return the size or -1. |
| 409 |
*/ |
| 410 |
int |
| 411 |
mowgli_dns_dn_skipname(const unsigned char *ptr, const unsigned char *eom) |
| 412 |
{ |
| 413 |
const unsigned char *saveptr = ptr; |
| 414 |
|
| 415 |
if (mowgli_dns_ns_name_skip(&ptr, eom) == -1) |
| 416 |
return -1; |
| 417 |
|
| 418 |
return ptr - saveptr; |
| 419 |
} |
| 420 |
|
| 421 |
/* |
| 422 |
* ns_name_skip(ptrptr, eom) |
| 423 |
* Advance *ptrptr to skip over the compressed name it points at. |
| 424 |
* return: |
| 425 |
* 0 on success, -1 (with errno set) on failure. |
| 426 |
*/ |
| 427 |
static int |
| 428 |
mowgli_dns_ns_name_skip(const unsigned char **ptrptr, const unsigned char *eom) |
| 429 |
{ |
| 430 |
const unsigned char *cp; |
| 431 |
unsigned int n; |
| 432 |
int l; |
| 433 |
|
| 434 |
cp = *ptrptr; |
| 435 |
|
| 436 |
while (cp < eom && (n = *cp++) != 0) |
| 437 |
{ |
| 438 |
/* Check for indirection. */ |
| 439 |
switch (n & MOWGLI_DNS_NS_CMPRSFLAGS) |
| 440 |
{ |
| 441 |
case 0: /* normal case, n == len */ |
| 442 |
cp += n; |
| 443 |
continue; |
| 444 |
case MOWGLI_DNS_NS_TYPE_ELT: /* EDNS0 extended label */ |
| 445 |
|
| 446 |
if ((l = labellen(cp - 1)) < 0) |
| 447 |
{ |
| 448 |
errno = EMSGSIZE; /* XXX */ |
| 449 |
return -1; |
| 450 |
} |
| 451 |
|
| 452 |
cp += l; |
| 453 |
continue; |
| 454 |
case MOWGLI_DNS_NS_CMPRSFLAGS: /* indirection */ |
| 455 |
cp++; |
| 456 |
break; |
| 457 |
default:/* illegal type */ |
| 458 |
errno = EMSGSIZE; |
| 459 |
return -1; |
| 460 |
} |
| 461 |
|
| 462 |
break; |
| 463 |
} |
| 464 |
|
| 465 |
if (cp > eom) |
| 466 |
{ |
| 467 |
errno = EMSGSIZE; |
| 468 |
return -1; |
| 469 |
} |
| 470 |
|
| 471 |
*ptrptr = cp; |
| 472 |
return 0; |
| 473 |
} |
| 474 |
|
| 475 |
unsigned int |
| 476 |
mowgli_dns_ns_get16(const unsigned char *src) |
| 477 |
{ |
| 478 |
unsigned int dst; |
| 479 |
|
| 480 |
MOWGLI_DNS_NS_GET16(dst, src); |
| 481 |
return dst; |
| 482 |
} |
| 483 |
|
| 484 |
unsigned long |
| 485 |
mowgli_dns_ns_get32(const unsigned char *src) |
| 486 |
{ |
| 487 |
unsigned long dst; |
| 488 |
|
| 489 |
MOWGLI_DNS_NS_GET32(dst, src); |
| 490 |
return dst; |
| 491 |
} |
| 492 |
|
| 493 |
void |
| 494 |
mowgli_dns_ns_put16(unsigned int src, unsigned char *dst) |
| 495 |
{ |
| 496 |
MOWGLI_DNS_NS_PUT16(src, dst); |
| 497 |
} |
| 498 |
|
| 499 |
void |
| 500 |
mowgli_dns_ns_put32(unsigned long src, unsigned char *dst) |
| 501 |
{ |
| 502 |
MOWGLI_DNS_NS_PUT32(src, dst); |
| 503 |
} |
| 504 |
|
| 505 |
/* From ns_name.c */ |
| 506 |
|
| 507 |
/* |
| 508 |
* mowgli_dns_is_special(ch) |
| 509 |
* Thinking in noninternationalized USASCII (per the DNS spec), |
| 510 |
* is this characted mowgli_dns_is_special ("in need of quoting") ? |
| 511 |
* return: |
| 512 |
* boolean. |
| 513 |
*/ |
| 514 |
static bool |
| 515 |
mowgli_dns_is_special(int ch) |
| 516 |
{ |
| 517 |
switch (ch) |
| 518 |
{ |
| 519 |
case 0x22: /* '"' */ |
| 520 |
case 0x2E: /* '.' */ |
| 521 |
case 0x3B: /* ';' */ |
| 522 |
case 0x5C: /* '\\' */ |
| 523 |
case 0x28: /* '(' */ |
| 524 |
case 0x29: /* ')' */ |
| 525 |
/* Special modifiers in zone files. */ |
| 526 |
case 0x40: /* '@' */ |
| 527 |
case 0x24: /* '$' */ |
| 528 |
return true; |
| 529 |
default: |
| 530 |
return false; |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
static int |
| 535 |
labellen(const unsigned char *lp) |
| 536 |
{ |
| 537 |
int bitlen; |
| 538 |
unsigned char l = *lp; |
| 539 |
|
| 540 |
if ((l & MOWGLI_DNS_NS_CMPRSFLAGS) == MOWGLI_DNS_NS_CMPRSFLAGS) |
| 541 |
/* should be avoided by the caller */ |
| 542 |
return -1; |
| 543 |
|
| 544 |
if ((l & MOWGLI_DNS_NS_CMPRSFLAGS) == MOWGLI_DNS_NS_TYPE_ELT) |
| 545 |
{ |
| 546 |
if (l == MOWGLI_DNS_LABELTYPE_BITSTRING) |
| 547 |
{ |
| 548 |
if ((bitlen = *(lp + 1)) == 0) |
| 549 |
bitlen = 256; |
| 550 |
|
| 551 |
return (bitlen + 7) / 8 + 1; |
| 552 |
} |
| 553 |
|
| 554 |
return -1; /* unknwon ELT */ |
| 555 |
} |
| 556 |
|
| 557 |
return l; |
| 558 |
} |
| 559 |
|
| 560 |
/* |
| 561 |
* mowgli_dns_is_printable(ch) |
| 562 |
* Thinking in noninternationalized USASCII (per the DNS spec), |
| 563 |
* is this character visible and not a space when printed ? |
| 564 |
* return: |
| 565 |
* boolean. |
| 566 |
*/ |
| 567 |
static bool |
| 568 |
mowgli_dns_is_printable(int ch) |
| 569 |
{ |
| 570 |
return (ch > 0x20 && ch < 0x7f) ? true : false; |
| 571 |
} |
| 572 |
|
| 573 |
static int |
| 574 |
mowgli_dns_decode_bitstring(const char **cpp, char *dn, const char *eom) |
| 575 |
{ |
| 576 |
const char *cp = *cpp; |
| 577 |
char *beg = dn, tc; |
| 578 |
int b, blen, plen; |
| 579 |
|
| 580 |
if ((blen = (*cp & 0xff)) == 0) |
| 581 |
blen = 256; |
| 582 |
|
| 583 |
plen = (blen + 3) / 4; |
| 584 |
plen += sizeof("\\[x/]") + (blen > 99 ? 3 : (blen > 9) ? 2 : 1); |
| 585 |
|
| 586 |
if (dn + plen >= eom) |
| 587 |
return -1; |
| 588 |
|
| 589 |
cp++; |
| 590 |
dn += sprintf(dn, "\\[x"); |
| 591 |
|
| 592 |
for (b = blen; b > 7; b -= 8, cp++) |
| 593 |
dn += sprintf(dn, "%02x", (unsigned int) (*cp & 0xFFU)); |
| 594 |
|
| 595 |
if (b > 4) |
| 596 |
{ |
| 597 |
tc = *cp++; |
| 598 |
dn += sprintf(dn, "%02x", (unsigned int) (tc & (0xFFU << (8U - b)))); |
| 599 |
} |
| 600 |
else if (b > 0) |
| 601 |
{ |
| 602 |
tc = *cp++; |
| 603 |
dn += sprintf(dn, "%1x", (unsigned int) (((tc >> 4U) & 0x0FU) & (0x0FU << (4U - b)))); |
| 604 |
} |
| 605 |
|
| 606 |
dn += sprintf(dn, "/%d]", blen); |
| 607 |
|
| 608 |
*cpp = cp; |
| 609 |
return dn - beg; |
| 610 |
} |
| 611 |
|
| 612 |
/* |
| 613 |
* mowgli_dns_ns_name_pton(src, dst, dstsiz) |
| 614 |
* Convert a ascii string into an encoded domain name as per RFC1035. |
| 615 |
* return: |
| 616 |
* -1 if it fails |
| 617 |
* 1 if string was fully qualified |
| 618 |
* 0 is string was not fully qualified |
| 619 |
* notes: |
| 620 |
* Enforces label and domain length limits. |
| 621 |
*/ |
| 622 |
static int |
| 623 |
mowgli_dns_ns_name_pton(const char *src, unsigned char *dst, size_t dstsiz) |
| 624 |
{ |
| 625 |
unsigned char *label, *bp, *eom; |
| 626 |
char *cp; |
| 627 |
int c, n, escaped, e = 0; |
| 628 |
|
| 629 |
escaped = 0; |
| 630 |
bp = dst; |
| 631 |
eom = dst + dstsiz; |
| 632 |
label = bp++; |
| 633 |
|
| 634 |
while ((c = *src++) != 0) |
| 635 |
{ |
| 636 |
if (escaped) |
| 637 |
{ |
| 638 |
if (c == '[') /* start a bit string label */ |
| 639 |
{ |
| 640 |
if ((cp = strchr(src, ']')) == NULL) |
| 641 |
{ |
| 642 |
errno = EINVAL; /* ??? */ |
| 643 |
return -1; |
| 644 |
} |
| 645 |
|
| 646 |
if ((e = mowgli_dns_encode_bitsring(&src, cp + 2, &label, &bp, (const char *) eom)) != 0) |
| 647 |
{ |
| 648 |
errno = e; |
| 649 |
return -1; |
| 650 |
} |
| 651 |
|
| 652 |
escaped = 0; |
| 653 |
label = bp++; |
| 654 |
|
| 655 |
if ((c = *src++) == 0) |
| 656 |
{ |
| 657 |
goto done; |
| 658 |
} |
| 659 |
else if (c != '.') |
| 660 |
{ |
| 661 |
errno = EINVAL; |
| 662 |
return -1; |
| 663 |
} |
| 664 |
|
| 665 |
continue; |
| 666 |
} |
| 667 |
else if ((cp = strchr(digits, c)) != NULL) |
| 668 |
{ |
| 669 |
n = (cp - digits) * 100; |
| 670 |
|
| 671 |
if (((c = *src++) == 0) || ((cp = strchr(digits, c)) == NULL)) |
| 672 |
{ |
| 673 |
errno = EMSGSIZE; |
| 674 |
return -1; |
| 675 |
} |
| 676 |
|
| 677 |
n += (cp - digits) * 10; |
| 678 |
|
| 679 |
if (((c = *src++) == 0) || ((cp = strchr(digits, c)) == NULL)) |
| 680 |
{ |
| 681 |
errno = EMSGSIZE; |
| 682 |
return -1; |
| 683 |
} |
| 684 |
|
| 685 |
n += (cp - digits); |
| 686 |
|
| 687 |
if (n > 255) |
| 688 |
{ |
| 689 |
errno = EMSGSIZE; |
| 690 |
return -1; |
| 691 |
} |
| 692 |
|
| 693 |
c = n; |
| 694 |
} |
| 695 |
|
| 696 |
escaped = 0; |
| 697 |
} |
| 698 |
else if (c == '\\') |
| 699 |
{ |
| 700 |
escaped = 1; |
| 701 |
continue; |
| 702 |
} |
| 703 |
else if (c == '.') |
| 704 |
{ |
| 705 |
c = (bp - label - 1); |
| 706 |
|
| 707 |
if ((c & MOWGLI_DNS_NS_CMPRSFLAGS) != 0)/* Label too big. */ |
| 708 |
{ |
| 709 |
errno = EMSGSIZE; |
| 710 |
return -1; |
| 711 |
} |
| 712 |
|
| 713 |
if (label >= eom) |
| 714 |
{ |
| 715 |
errno = EMSGSIZE; |
| 716 |
return -1; |
| 717 |
} |
| 718 |
|
| 719 |
*label = c; |
| 720 |
|
| 721 |
/* Fully qualified ? */ |
| 722 |
if (*src == '\0') |
| 723 |
{ |
| 724 |
if (c != 0) |
| 725 |
{ |
| 726 |
if (bp >= eom) |
| 727 |
{ |
| 728 |
errno = EMSGSIZE; |
| 729 |
return -1; |
| 730 |
} |
| 731 |
|
| 732 |
*bp++ = '\0'; |
| 733 |
} |
| 734 |
|
| 735 |
if ((bp - dst) > MOWGLI_DNS_NS_MAXCDNAME) |
| 736 |
{ |
| 737 |
errno = EMSGSIZE; |
| 738 |
return -1; |
| 739 |
} |
| 740 |
|
| 741 |
return 1; |
| 742 |
} |
| 743 |
|
| 744 |
if ((c == 0) || (*src == '.')) |
| 745 |
{ |
| 746 |
errno = EMSGSIZE; |
| 747 |
return -1; |
| 748 |
} |
| 749 |
|
| 750 |
label = bp++; |
| 751 |
continue; |
| 752 |
} |
| 753 |
|
| 754 |
if (bp >= eom) |
| 755 |
{ |
| 756 |
errno = EMSGSIZE; |
| 757 |
return -1; |
| 758 |
} |
| 759 |
|
| 760 |
*bp++ = (unsigned char) c; |
| 761 |
} |
| 762 |
|
| 763 |
c = (bp - label - 1); |
| 764 |
|
| 765 |
if ((c & MOWGLI_DNS_NS_CMPRSFLAGS) != 0)/* Label too big. */ |
| 766 |
{ |
| 767 |
errno = EMSGSIZE; |
| 768 |
return -1; |
| 769 |
} |
| 770 |
|
| 771 |
done: |
| 772 |
|
| 773 |
if (label >= eom) |
| 774 |
{ |
| 775 |
errno = EMSGSIZE; |
| 776 |
return -1; |
| 777 |
} |
| 778 |
|
| 779 |
*label = c; |
| 780 |
|
| 781 |
if (c != 0) |
| 782 |
{ |
| 783 |
if (bp >= eom) |
| 784 |
{ |
| 785 |
errno = EMSGSIZE; |
| 786 |
return -1; |
| 787 |
} |
| 788 |
|
| 789 |
*bp++ = 0; |
| 790 |
} |
| 791 |
|
| 792 |
if ((bp - dst) > MOWGLI_DNS_NS_MAXCDNAME) /* src too big */ |
| 793 |
{ |
| 794 |
errno = EMSGSIZE; |
| 795 |
return -1; |
| 796 |
} |
| 797 |
|
| 798 |
return 0; |
| 799 |
} |
| 800 |
|
| 801 |
/* |
| 802 |
* mowgli_dns_ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr) |
| 803 |
* Pack domain name 'domain' into 'comp_dn'. |
| 804 |
* return: |
| 805 |
* Size of the compressed name, or -1. |
| 806 |
* notes: |
| 807 |
* 'dnptrs' is an array of pointers to previous compressed names. |
| 808 |
* dnptrs[0] is a pointer to the beginning of the message. The array |
| 809 |
* ends with NULL. |
| 810 |
* 'lastdnptr' is a pointer to the end of the array pointed to |
| 811 |
* by 'dnptrs'. |
| 812 |
* Side effects: |
| 813 |
* The list of pointers in dnptrs is updated for labels inserted into |
| 814 |
* the message as we compress the name. If 'dnptr' is NULL, we don't |
| 815 |
* try to compress names. If 'lastdnptr' is NULL, we don't update the |
| 816 |
* list. |
| 817 |
*/ |
| 818 |
static int |
| 819 |
mowgli_dns_ns_name_pack(const unsigned char *src, unsigned char *dst, int dstsiz, unsigned char **dnptrs, unsigned char **lastdnptr) |
| 820 |
{ |
| 821 |
unsigned char *dstp; |
| 822 |
unsigned char **cpp, **lpp; |
| 823 |
const unsigned char *eob, *rmsg; |
| 824 |
const unsigned char *srcp; |
| 825 |
int n, l, first = 1; |
| 826 |
|
| 827 |
srcp = src; |
| 828 |
dstp = dst; |
| 829 |
eob = dstp + dstsiz; |
| 830 |
lpp = cpp = NULL; |
| 831 |
|
| 832 |
if (dnptrs != NULL) |
| 833 |
{ |
| 834 |
if ((rmsg = *dnptrs++) != NULL) |
| 835 |
{ |
| 836 |
for (cpp = dnptrs; *cpp != NULL; cpp++) |
| 837 |
(void) NULL; |
| 838 |
|
| 839 |
lpp = cpp; /* end of list to search */ |
| 840 |
} |
| 841 |
} |
| 842 |
else |
| 843 |
{ |
| 844 |
rmsg = NULL; |
| 845 |
} |
| 846 |
|
| 847 |
/* make sure the domain we are about to add is legal */ |
| 848 |
l = 0; |
| 849 |
|
| 850 |
do |
| 851 |
{ |
| 852 |
int l0; |
| 853 |
|
| 854 |
n = *srcp; |
| 855 |
|
| 856 |
if ((n & MOWGLI_DNS_NS_CMPRSFLAGS) == MOWGLI_DNS_NS_CMPRSFLAGS) |
| 857 |
{ |
| 858 |
errno = EMSGSIZE; |
| 859 |
return -1; |
| 860 |
} |
| 861 |
|
| 862 |
if ((l0 = labellen(srcp)) < 0) |
| 863 |
{ |
| 864 |
errno = EINVAL; |
| 865 |
return -1; |
| 866 |
} |
| 867 |
|
| 868 |
l += l0 + 1; |
| 869 |
|
| 870 |
if (l > MOWGLI_DNS_NS_MAXCDNAME) |
| 871 |
{ |
| 872 |
errno = EMSGSIZE; |
| 873 |
return -1; |
| 874 |
} |
| 875 |
|
| 876 |
srcp += l0 + 1; |
| 877 |
} while (n != 0); |
| 878 |
|
| 879 |
/* from here on we need to reset compression pointer array on error */ |
| 880 |
srcp = src; |
| 881 |
|
| 882 |
do |
| 883 |
{ |
| 884 |
/* Look to see if we can use pointers. */ |
| 885 |
n = *srcp; |
| 886 |
|
| 887 |
if ((n != 0) && (rmsg != NULL)) |
| 888 |
{ |
| 889 |
l = mowgli_dns_dn_find(srcp, rmsg, (const unsigned char *const *) dnptrs, |
| 890 |
(const unsigned char *const *) lpp); |
| 891 |
|
| 892 |
if (l >= 0) |
| 893 |
{ |
| 894 |
if (dstp + 1 >= eob) |
| 895 |
goto cleanup; |
| 896 |
|
| 897 |
*dstp++ = (l >> 8) | MOWGLI_DNS_NS_CMPRSFLAGS; |
| 898 |
*dstp++ = l % 256; |
| 899 |
return dstp - dst; |
| 900 |
} |
| 901 |
|
| 902 |
/* Not found, save it. */ |
| 903 |
if ((lastdnptr != NULL) && (cpp < lastdnptr - 1) && ((dstp - rmsg) < 0x4000) && first) |
| 904 |
{ |
| 905 |
*cpp++ = dstp; |
| 906 |
*cpp = NULL; |
| 907 |
first = 0; |
| 908 |
} |
| 909 |
} |
| 910 |
|
| 911 |
/* copy label to buffer */ |
| 912 |
if ((n & MOWGLI_DNS_NS_CMPRSFLAGS) == MOWGLI_DNS_NS_CMPRSFLAGS) |
| 913 |
/* Should not happen. */ |
| 914 |
goto cleanup; |
| 915 |
|
| 916 |
n = labellen(srcp); |
| 917 |
|
| 918 |
if (dstp + 1 + n >= eob) |
| 919 |
goto cleanup; |
| 920 |
|
| 921 |
memcpy(dstp, srcp, n + 1); |
| 922 |
srcp += n + 1; |
| 923 |
dstp += n + 1; |
| 924 |
} while (n != 0); |
| 925 |
|
| 926 |
if (dstp > eob) |
| 927 |
{ |
| 928 |
cleanup: |
| 929 |
|
| 930 |
if (rmsg != NULL) |
| 931 |
*lpp = NULL; |
| 932 |
|
| 933 |
errno = EMSGSIZE; |
| 934 |
return -1; |
| 935 |
} |
| 936 |
|
| 937 |
return dstp - dst; |
| 938 |
} |
| 939 |
|
| 940 |
static int |
| 941 |
mowgli_dns_ns_name_compress(const char *src, unsigned char *dst, size_t dstsiz, unsigned char **dnptrs, unsigned char **lastdnptr) |
| 942 |
{ |
| 943 |
unsigned char tmp[MOWGLI_DNS_NS_MAXCDNAME]; |
| 944 |
|
| 945 |
if (mowgli_dns_ns_name_pton(src, tmp, sizeof tmp) == -1) |
| 946 |
return -1; |
| 947 |
|
| 948 |
return mowgli_dns_ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr); |
| 949 |
} |
| 950 |
|
| 951 |
static int |
| 952 |
mowgli_dns_encode_bitsring(const char **bp, const char *end, unsigned char **labelp, unsigned char **dst, const char *eom) |
| 953 |
{ |
| 954 |
int afterslash = 0; |
| 955 |
const char *cp = *bp; |
| 956 |
char *tp, c; |
| 957 |
const char *beg_blen; |
| 958 |
char *end_blen = NULL; |
| 959 |
int value = 0, count = 0, tbcount = 0, blen = 0; |
| 960 |
|
| 961 |
beg_blen = end_blen = NULL; |
| 962 |
|
| 963 |
/* a bitstring must contain at least 2 characters */ |
| 964 |
if (end - cp < 2) |
| 965 |
return EINVAL; |
| 966 |
|
| 967 |
/* XXX: currently, only hex strings are supported */ |
| 968 |
if (*cp++ != 'x') |
| 969 |
return EINVAL; |
| 970 |
|
| 971 |
if (!isxdigit((*cp) & 0xff)) /* reject '\[x/BLEN]' */ |
| 972 |
return EINVAL; |
| 973 |
|
| 974 |
for (tp = (char *) (dst + 1); cp < end && tp < eom; cp++) |
| 975 |
{ |
| 976 |
switch ((c = *cp)) |
| 977 |
{ |
| 978 |
case ']': /* end of the bitstring */ |
| 979 |
|
| 980 |
if (afterslash) |
| 981 |
{ |
| 982 |
if (beg_blen == NULL) |
| 983 |
return EINVAL; |
| 984 |
|
| 985 |
blen = (int) strtol(beg_blen, &end_blen, 10); |
| 986 |
|
| 987 |
if (*end_blen != ']') |
| 988 |
return EINVAL; |
| 989 |
} |
| 990 |
|
| 991 |
if (count) |
| 992 |
*tp++ = ((value << 4) & 0xff); |
| 993 |
|
| 994 |
cp++; /* skip ']' */ |
| 995 |
goto done; |
| 996 |
case '/': |
| 997 |
afterslash = 1; |
| 998 |
break; |
| 999 |
default: |
| 1000 |
|
| 1001 |
if (afterslash) |
| 1002 |
{ |
| 1003 |
if (!isdigit(c & 0xff)) |
| 1004 |
return EINVAL; |
| 1005 |
|
| 1006 |
if (beg_blen == NULL) |
| 1007 |
{ |
| 1008 |
if (c == '0') |
| 1009 |
/* blen never begings with 0 */ |
| 1010 |
return EINVAL; |
| 1011 |
|
| 1012 |
beg_blen = cp; |
| 1013 |
} |
| 1014 |
} |
| 1015 |
else |
| 1016 |
{ |
| 1017 |
if (!isxdigit(c & 0xff)) |
| 1018 |
return EINVAL; |
| 1019 |
|
| 1020 |
value <<= 4; |
| 1021 |
value += digitvalue[(int) c]; |
| 1022 |
count += 4; |
| 1023 |
tbcount += 4; |
| 1024 |
|
| 1025 |
if (tbcount > 256) |
| 1026 |
return EINVAL; |
| 1027 |
|
| 1028 |
if (count == 8) |
| 1029 |
{ |
| 1030 |
*tp++ = value; |
| 1031 |
count = 0; |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
break; |
| 1036 |
} |
| 1037 |
} |
| 1038 |
|
| 1039 |
done: |
| 1040 |
|
| 1041 |
if ((cp >= end) || (tp >= eom)) |
| 1042 |
return EMSGSIZE; |
| 1043 |
|
| 1044 |
/* |
| 1045 |
* bit length validation: |
| 1046 |
* If a <length> is present, the number of digits in the <bit-data> |
| 1047 |
* MUST be just sufficient to contain the number of bits specified |
| 1048 |
* by the <length>. If there are insignificant bits in a final |
| 1049 |
* hexadecimal or octal digit, they MUST be zero. |
| 1050 |
* RFC 2673, Section 3.2. |
| 1051 |
*/ |
| 1052 |
if (blen > 0) |
| 1053 |
{ |
| 1054 |
int traillen; |
| 1055 |
|
| 1056 |
if (((blen + 3) & ~3) != tbcount) |
| 1057 |
return EINVAL; |
| 1058 |
|
| 1059 |
traillen = tbcount - blen; /* between 0 and 3 */ |
| 1060 |
|
| 1061 |
if (((value << (8 - traillen)) & 0xff) != 0) |
| 1062 |
return EINVAL; |
| 1063 |
} |
| 1064 |
else |
| 1065 |
{ |
| 1066 |
blen = tbcount; |
| 1067 |
} |
| 1068 |
|
| 1069 |
if (blen == 256) |
| 1070 |
blen = 0; |
| 1071 |
|
| 1072 |
/* encode the type and the significant bit fields */ |
| 1073 |
**labelp = MOWGLI_DNS_LABELTYPE_BITSTRING; |
| 1074 |
**dst = blen; |
| 1075 |
|
| 1076 |
*bp = cp; |
| 1077 |
*dst = (unsigned char *) tp; |
| 1078 |
|
| 1079 |
return 0; |
| 1080 |
} |
| 1081 |
|
| 1082 |
/* |
| 1083 |
* dn_find(domain, rmsg, dnptrs, lastdnptr) |
| 1084 |
* Search for the counted-label name in an array of compressed names. |
| 1085 |
* return: |
| 1086 |
* offset from rmsg if found, or -1. |
| 1087 |
* notes: |
| 1088 |
* dnptrs is the pointer to the first name on the list, |
| 1089 |
* not the pointer to the start of the message. |
| 1090 |
*/ |
| 1091 |
static int |
| 1092 |
mowgli_dns_dn_find(const unsigned char *domain, const unsigned char *rmsg, const unsigned char *const *dnptrs, const unsigned char *const *lastdnptr) |
| 1093 |
{ |
| 1094 |
const unsigned char *dn, *cp, *sp; |
| 1095 |
const unsigned char *const *cpp; |
| 1096 |
unsigned int n; |
| 1097 |
|
| 1098 |
for (cpp = dnptrs; cpp < lastdnptr; cpp++) |
| 1099 |
{ |
| 1100 |
sp = *cpp; |
| 1101 |
|
| 1102 |
/* |
| 1103 |
* terminate search on: |
| 1104 |
* root label |
| 1105 |
* compression pointer |
| 1106 |
* unusable offset |
| 1107 |
*/ |
| 1108 |
while (*sp != 0 && (*sp & MOWGLI_DNS_NS_CMPRSFLAGS) == 0 && (sp - rmsg) < 0x4000) |
| 1109 |
{ |
| 1110 |
dn = domain; |
| 1111 |
cp = sp; |
| 1112 |
|
| 1113 |
while ((n = *cp++) != 0) |
| 1114 |
{ |
| 1115 |
/* |
| 1116 |
* check for indirection |
| 1117 |
*/ |
| 1118 |
switch (n & MOWGLI_DNS_NS_CMPRSFLAGS) |
| 1119 |
{ |
| 1120 |
case 0: /* normal case, n == len */ |
| 1121 |
n = labellen(cp - 1); /* XXX */ |
| 1122 |
|
| 1123 |
if (n != *dn++) |
| 1124 |
goto next; |
| 1125 |
|
| 1126 |
for (; n > 0; n--) |
| 1127 |
if (mowgli_dns_mklower(*dn++) != mowgli_dns_mklower(*cp++)) |
| 1128 |
goto next; |
| 1129 |
|
| 1130 |
/* Is next root for both ? */ |
| 1131 |
if ((*dn == '\0') && (*cp == '\0')) |
| 1132 |
return sp - rmsg; |
| 1133 |
|
| 1134 |
if (*dn) |
| 1135 |
continue; |
| 1136 |
|
| 1137 |
goto next; |
| 1138 |
case MOWGLI_DNS_NS_CMPRSFLAGS: /* indirection */ |
| 1139 |
cp = rmsg + (((n & 0x3f) << 8) | *cp); |
| 1140 |
break; |
| 1141 |
|
| 1142 |
default:/* illegal type */ |
| 1143 |
errno = EMSGSIZE; |
| 1144 |
return -1; |
| 1145 |
} |
| 1146 |
} |
| 1147 |
|
| 1148 |
next:; |
| 1149 |
sp += *sp + 1; |
| 1150 |
} |
| 1151 |
} |
| 1152 |
|
| 1153 |
errno = ENOENT; |
| 1154 |
return -1; |
| 1155 |
} |
| 1156 |
|
| 1157 |
/* |
| 1158 |
* Thinking in noninternationalized USASCII (per the DNS spec), |
| 1159 |
* convert this character to lower case if it's upper case. |
| 1160 |
*/ |
| 1161 |
static int |
| 1162 |
mowgli_dns_mklower(int ch) |
| 1163 |
{ |
| 1164 |
if ((ch >= 0x41) && (ch <= 0x5A)) |
| 1165 |
return ch + 0x20; |
| 1166 |
|
| 1167 |
return ch; |
| 1168 |
} |
| 1169 |
|
| 1170 |
/* From resolv/mkquery.c */ |
| 1171 |
|
| 1172 |
/* |
| 1173 |
* Form all types of queries. |
| 1174 |
* Returns the size of the result or -1. |
| 1175 |
*/ |
| 1176 |
int |
| 1177 |
mowgli_dns_res_mkquery(const char *dname, /* domain name */ |
| 1178 |
int query_class, int type, /* class and type of query */ |
| 1179 |
unsigned char *buf, /* buffer to put query */ |
| 1180 |
int buflen) /* size of buffer */ |
| 1181 |
{ |
| 1182 |
mowgli_dns_resheader_t *hp; |
| 1183 |
unsigned char *cp; |
| 1184 |
int n; |
| 1185 |
unsigned char *dnptrs[20], **dpp, **lastdnptr; |
| 1186 |
|
| 1187 |
/* |
| 1188 |
* Initialize header fields. |
| 1189 |
*/ |
| 1190 |
if ((buf == NULL) || (buflen < MOWGLI_DNS_HFIXEDSIZE)) |
| 1191 |
return -1; |
| 1192 |
|
| 1193 |
memset(buf, 0, MOWGLI_DNS_HFIXEDSIZE); |
| 1194 |
hp = (mowgli_dns_resheader_t *) buf; |
| 1195 |
|
| 1196 |
hp->id = 0; |
| 1197 |
hp->opcode = MOWGLI_DNS_QUERY; |
| 1198 |
hp->rd = 1; /* recurse */ |
| 1199 |
hp->rcode = MOWGLI_DNS_NO_ERRORS; |
| 1200 |
cp = buf + MOWGLI_DNS_HFIXEDSIZE; |
| 1201 |
buflen -= MOWGLI_DNS_HFIXEDSIZE; |
| 1202 |
dpp = dnptrs; |
| 1203 |
*dpp++ = buf; |
| 1204 |
*dpp++ = NULL; |
| 1205 |
lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0]; |
| 1206 |
|
| 1207 |
if ((buflen -= MOWGLI_DNS_QFIXEDSIZE) < 0) |
| 1208 |
return -1; |
| 1209 |
|
| 1210 |
if ((n = mowgli_dns_dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0) |
| 1211 |
return -1; |
| 1212 |
|
| 1213 |
cp += n; |
| 1214 |
buflen -= n; |
| 1215 |
MOWGLI_DNS_NS_PUT16(type, cp); |
| 1216 |
MOWGLI_DNS_NS_PUT16(query_class, cp); |
| 1217 |
hp->qdcount = htons(1); |
| 1218 |
|
| 1219 |
return cp - buf; |
| 1220 |
} |