patch-dns_c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. $Id$
  2. --- dsniff-2.4.orig/dns.c 1970-01-01 00:00:00.000000000 +0100
  3. +++ dsniff-2.4/dns.c 2007-01-23 00:20:48.000000000 +0100
  4. @@ -0,0 +1,677 @@
  5. +/*
  6. + * Copyright (c) 1985, 1993
  7. + * The Regents of the University of California. All rights reserved.
  8. + *
  9. + * Redistribution and use in source and binary forms, with or without
  10. + * modification, are permitted provided that the following conditions
  11. + * are met:
  12. + * 1. Redistributions of source code must retain the above copyright
  13. + * notice, this list of conditions and the following disclaimer.
  14. + * 2. Redistributions in binary form must reproduce the above copyright
  15. + * notice, this list of conditions and the following disclaimer in the
  16. + * documentation and/or other materials provided with the distribution.
  17. + * 4. Neither the name of the University nor the names of its contributors
  18. + * may be used to endorse or promote products derived from this software
  19. + * without specific prior written permission.
  20. + *
  21. + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. + * SUCH DAMAGE.
  32. + */
  33. +
  34. +/*
  35. + * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  36. + *
  37. + * Permission to use, copy, modify, and distribute this software for any
  38. + * purpose with or without fee is hereby granted, provided that the above
  39. + * copyright notice and this permission notice appear in all copies, and that
  40. + * the name of Digital Equipment Corporation not be used in advertising or
  41. + * publicity pertaining to distribution of the document or software without
  42. + * specific, written prior permission.
  43. + *
  44. + * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  45. + * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  46. + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  47. + * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  48. + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  49. + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  50. + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  51. + * SOFTWARE.
  52. + */
  53. +
  54. +/*
  55. + * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
  56. + *
  57. + * Permission to use, copy, modify, and distribute this software for any
  58. + * purpose with or without fee is hereby granted, provided that the above
  59. + * copyright notice and this permission notice appear in all copies.
  60. + *
  61. + * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  62. + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  63. + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  64. + * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  65. + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  66. + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  67. + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  68. + * SOFTWARE.
  69. + */
  70. +
  71. +/*
  72. + * Copyright (c) 1996,1999 by Internet Software Consortium.
  73. + *
  74. + * Permission to use, copy, modify, and distribute this software for any
  75. + * purpose with or without fee is hereby granted, provided that the above
  76. + * copyright notice and this permission notice appear in all copies.
  77. + *
  78. + * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  79. + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  80. + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  81. + * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  82. + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  83. + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  84. + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  85. + * SOFTWARE.
  86. + */
  87. +
  88. +/*
  89. + *
  90. + * DNS helper functions not implemented in uclibc
  91. + *
  92. + */
  93. +
  94. +
  95. +#include <sys/types.h>
  96. +#include <sys/param.h>
  97. +#include <netinet/in.h>
  98. +#include <arpa/nameser.h>
  99. +#include <ctype.h>
  100. +#include <resolv.h>
  101. +#include <stdio.h>
  102. +#include <string.h>
  103. +#include <unistd.h>
  104. +
  105. +static const char digits[] = "0123456789";
  106. +
  107. +/* Forward. */
  108. +
  109. +static int special(int);
  110. +static int printable(int);
  111. +static int dn_find(const u_char *, const u_char *,
  112. + const u_char * const *,
  113. + const u_char * const *);
  114. +
  115. +
  116. +/*
  117. + * ns_name_ntop(src, dst, dstsiz)
  118. + * Convert an encoded domain name to printable ascii as per RFC1035.
  119. + * return:
  120. + * Number of bytes written to buffer, or -1 (with errno set)
  121. + * notes:
  122. + * The root is returned as "."
  123. + * All other domains are returned in non absolute form
  124. + */
  125. +int
  126. +ns_name_ntop(const u_char *src, char *dst, size_t dstsiz) {
  127. + const u_char *cp;
  128. + char *dn, *eom;
  129. + u_char c;
  130. + u_int n;
  131. +
  132. + cp = src;
  133. + dn = dst;
  134. + eom = dst + dstsiz;
  135. +
  136. + while ((n = *cp++) != 0) {
  137. + if ((n & NS_CMPRSFLGS) != 0) {
  138. + /* Some kind of compression pointer. */
  139. + return (-1);
  140. + }
  141. + if (dn != dst) {
  142. + if (dn >= eom) {
  143. + return (-1);
  144. + }
  145. + *dn++ = '.';
  146. + }
  147. + if (dn + n >= eom) {
  148. + return (-1);
  149. + }
  150. + for ((void)NULL; n > 0; n--) {
  151. + c = *cp++;
  152. + if (special(c)) {
  153. + if (dn + 1 >= eom) {
  154. + return (-1);
  155. + }
  156. + *dn++ = '\\';
  157. + *dn++ = (char)c;
  158. + } else if (!printable(c)) {
  159. + if (dn + 3 >= eom) {
  160. + return (-1);
  161. + }
  162. + *dn++ = '\\';
  163. + *dn++ = digits[c / 100];
  164. + *dn++ = digits[(c % 100) / 10];
  165. + *dn++ = digits[c % 10];
  166. + } else {
  167. + if (dn >= eom) {
  168. + return (-1);
  169. + }
  170. + *dn++ = (char)c;
  171. + }
  172. + }
  173. + }
  174. + if (dn == dst) {
  175. + if (dn >= eom) {
  176. + return (-1);
  177. + }
  178. + *dn++ = '.';
  179. + }
  180. + if (dn >= eom) {
  181. + return (-1);
  182. + }
  183. + *dn++ = '\0';
  184. + return (dn - dst);
  185. +}
  186. +
  187. +/*
  188. + * ns_name_pton(src, dst, dstsiz)
  189. + * Convert a ascii string into an encoded domain name as per RFC1035.
  190. + * return:
  191. + * -1 if it fails
  192. + * 1 if string was fully qualified
  193. + * 0 is string was not fully qualified
  194. + * notes:
  195. + * Enforces label and domain length limits.
  196. + */
  197. +
  198. +int
  199. +ns_name_pton(const char *src, u_char *dst, size_t dstsiz) {
  200. + u_char *label, *bp, *eom;
  201. + int c, n, escaped;
  202. + char *cp;
  203. +
  204. + escaped = 0;
  205. + bp = dst;
  206. + eom = dst + dstsiz;
  207. + label = bp++;
  208. +
  209. + while ((c = *src++) != 0) {
  210. + if (escaped) {
  211. + if ((cp = strchr(digits, c)) != NULL) {
  212. + n = (cp - digits) * 100;
  213. + if ((c = *src++) == 0 ||
  214. + (cp = strchr(digits, c)) == NULL) {
  215. + return (-1);
  216. + }
  217. + n += (cp - digits) * 10;
  218. + if ((c = *src++) == 0 ||
  219. + (cp = strchr(digits, c)) == NULL) {
  220. + return (-1);
  221. + }
  222. + n += (cp - digits);
  223. + if (n > 255) {
  224. + return (-1);
  225. + }
  226. + c = n;
  227. + }
  228. + escaped = 0;
  229. + } else if (c == '\\') {
  230. + escaped = 1;
  231. + continue;
  232. + } else if (c == '.') {
  233. + c = (bp - label - 1);
  234. + if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
  235. + return (-1);
  236. + }
  237. + if (label >= eom) {
  238. + return (-1);
  239. + }
  240. + *label = c;
  241. + /* Fully qualified ? */
  242. + if (*src == '\0') {
  243. + if (c != 0) {
  244. + if (bp >= eom) {
  245. + return (-1);
  246. + }
  247. + *bp++ = '\0';
  248. + }
  249. + if ((bp - dst) > MAXCDNAME) {
  250. + return (-1);
  251. + }
  252. + return (1);
  253. + }
  254. + if (c == 0 || *src == '.') {
  255. + return (-1);
  256. + }
  257. + label = bp++;
  258. + continue;
  259. + }
  260. + if (bp >= eom) {
  261. + return (-1);
  262. + }
  263. + *bp++ = (u_char)c;
  264. + }
  265. + c = (bp - label - 1);
  266. + if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
  267. + return (-1);
  268. + }
  269. + if (label >= eom) {
  270. + return (-1);
  271. + }
  272. + *label = c;
  273. + if (c != 0) {
  274. + if (bp >= eom) {
  275. + return (-1);
  276. + }
  277. + *bp++ = 0;
  278. + }
  279. + if ((bp - dst) > MAXCDNAME) { /* src too big */
  280. + return (-1);
  281. + }
  282. + return (0);
  283. +}
  284. +
  285. +/*
  286. + * ns_name_ntol(src, dst, dstsiz)
  287. + * Convert a network strings labels into all lowercase.
  288. + * return:
  289. + * Number of bytes written to buffer, or -1 (with errno set)
  290. + * notes:
  291. + * Enforces label and domain length limits.
  292. + */
  293. +
  294. +int
  295. +ns_name_ntol(const u_char *src, u_char *dst, size_t dstsiz) {
  296. + const u_char *cp;
  297. + u_char *dn, *eom;
  298. + u_char c;
  299. + u_int n;
  300. +
  301. + cp = src;
  302. + dn = dst;
  303. + eom = dst + dstsiz;
  304. +
  305. + while ((n = *cp++) != 0) {
  306. + if ((n & NS_CMPRSFLGS) != 0) {
  307. + /* Some kind of compression pointer. */
  308. + return (-1);
  309. + }
  310. + *dn++ = n;
  311. + if (dn + n >= eom) {
  312. + return (-1);
  313. + }
  314. + for ((void)NULL; n > 0; n--) {
  315. + c = *cp++;
  316. + if (isupper(c))
  317. + *dn++ = tolower(c);
  318. + else
  319. + *dn++ = c;
  320. + }
  321. + }
  322. + *dn++ = '\0';
  323. + return (dn - dst);
  324. +}
  325. +
  326. +/*
  327. + * ns_name_unpack(msg, eom, src, dst, dstsiz)
  328. + * Unpack a domain name from a message, source may be compressed.
  329. + * return:
  330. + * -1 if it fails, or consumed octets if it succeeds.
  331. + */
  332. +int
  333. +ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
  334. + u_char *dst, size_t dstsiz)
  335. +{
  336. + const u_char *srcp, *dstlim;
  337. + u_char *dstp;
  338. + int n, len, checked;
  339. +
  340. + len = -1;
  341. + checked = 0;
  342. + dstp = dst;
  343. + srcp = src;
  344. + dstlim = dst + dstsiz;
  345. + if (srcp < msg || srcp >= eom) {
  346. + return (-1);
  347. + }
  348. + /* Fetch next label in domain name. */
  349. + while ((n = *srcp++) != 0) {
  350. + /* Check for indirection. */
  351. + switch (n & NS_CMPRSFLGS) {
  352. + case 0:
  353. + /* Limit checks. */
  354. + if (dstp + n + 1 >= dstlim || srcp + n >= eom) {
  355. + return (-1);
  356. + }
  357. + checked += n + 1;
  358. + *dstp++ = n;
  359. + memcpy(dstp, srcp, n);
  360. + dstp += n;
  361. + srcp += n;
  362. + break;
  363. +
  364. + case NS_CMPRSFLGS:
  365. + if (srcp >= eom) {
  366. + return (-1);
  367. + }
  368. + if (len < 0)
  369. + len = srcp - src + 1;
  370. + srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
  371. + if (srcp < msg || srcp >= eom) { /* Out of range. */
  372. + return (-1);
  373. + }
  374. + checked += 2;
  375. + /*
  376. + * Check for loops in the compressed name;
  377. + * if we've looked at the whole message,
  378. + * there must be a loop.
  379. + */
  380. + if (checked >= eom - msg) {
  381. + return (-1);
  382. + }
  383. + break;
  384. +
  385. + default:
  386. + return (-1); /* flag error */
  387. + }
  388. + }
  389. + *dstp = '\0';
  390. + if (len < 0)
  391. + len = srcp - src;
  392. + return (len);
  393. +}
  394. +
  395. +/*
  396. + * ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
  397. + * Pack domain name 'domain' into 'comp_dn'.
  398. + * return:
  399. + * Size of the compressed name, or -1.
  400. + * notes:
  401. + * 'dnptrs' is an array of pointers to previous compressed names.
  402. + * dnptrs[0] is a pointer to the beginning of the message. The array
  403. + * ends with NULL.
  404. + * 'lastdnptr' is a pointer to the end of the array pointed to
  405. + * by 'dnptrs'.
  406. + * Side effects:
  407. + * The list of pointers in dnptrs is updated for labels inserted into
  408. + * the message as we compress the name. If 'dnptr' is NULL, we don't
  409. + * try to compress names. If 'lastdnptr' is NULL, we don't update the
  410. + * list.
  411. + */
  412. +int
  413. +ns_name_pack(const u_char *src, u_char *dst, int dstsiz,
  414. + const u_char **dnptrs, const u_char **lastdnptr)
  415. +{
  416. + u_char *dstp;
  417. + const u_char **cpp, **lpp, *eob, *msg;
  418. + const u_char *srcp;
  419. + int n, l, first = 1;
  420. +
  421. + srcp = src;
  422. + dstp = dst;
  423. + eob = dstp + dstsiz;
  424. + lpp = cpp = NULL;
  425. + if (dnptrs != NULL) {
  426. + if ((msg = *dnptrs++) != NULL) {
  427. + for (cpp = dnptrs; *cpp != NULL; cpp++)
  428. + (void)NULL;
  429. + lpp = cpp; /* end of list to search */
  430. + }
  431. + } else
  432. + msg = NULL;
  433. +
  434. + /* make sure the domain we are about to add is legal */
  435. + l = 0;
  436. + do {
  437. + n = *srcp;
  438. + if ((n & NS_CMPRSFLGS) != 0) {
  439. + return (-1);
  440. + }
  441. + l += n + 1;
  442. + if (l > MAXCDNAME) {
  443. + return (-1);
  444. + }
  445. + srcp += n + 1;
  446. + } while (n != 0);
  447. +
  448. + /* from here on we need to reset compression pointer array on error */
  449. + srcp = src;
  450. + do {
  451. + /* Look to see if we can use pointers. */
  452. + n = *srcp;
  453. + if (n != 0 && msg != NULL) {
  454. + l = dn_find(srcp, msg, (const u_char * const *)dnptrs,
  455. + (const u_char * const *)lpp);
  456. + if (l >= 0) {
  457. + if (dstp + 1 >= eob) {
  458. + goto cleanup;
  459. + }
  460. + *dstp++ = (l >> 8) | NS_CMPRSFLGS;
  461. + *dstp++ = l % 256;
  462. + return (dstp - dst);
  463. + }
  464. + /* Not found, save it. */
  465. + if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
  466. + (dstp - msg) < 0x4000 && first) {
  467. + *cpp++ = dstp;
  468. + *cpp = NULL;
  469. + first = 0;
  470. + }
  471. + }
  472. + /* copy label to buffer */
  473. + if (n & NS_CMPRSFLGS) { /* Should not happen. */
  474. + goto cleanup;
  475. + }
  476. + if (dstp + 1 + n >= eob) {
  477. + goto cleanup;
  478. + }
  479. + memcpy(dstp, srcp, n + 1);
  480. + srcp += n + 1;
  481. + dstp += n + 1;
  482. + } while (n != 0);
  483. +
  484. + if (dstp > eob) {
  485. +cleanup:
  486. + if (msg != NULL)
  487. + *lpp = NULL;
  488. + return (-1);
  489. + }
  490. + return (dstp - dst);
  491. +}
  492. +
  493. +/*
  494. + * ns_name_uncompress(msg, eom, src, dst, dstsiz)
  495. + * Expand compressed domain name to presentation format.
  496. + * return:
  497. + * Number of bytes read out of `src', or -1 (with errno set).
  498. + * note:
  499. + * Root domain returns as "." not "".
  500. + */
  501. +int
  502. +ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src,
  503. + char *dst, size_t dstsiz)
  504. +{
  505. + u_char tmp[NS_MAXCDNAME];
  506. + int n;
  507. +
  508. + if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
  509. + return (-1);
  510. + if (ns_name_ntop(tmp, dst, dstsiz) == -1)
  511. + return (-1);
  512. + return (n);
  513. +}
  514. +
  515. +/*
  516. + * ns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr)
  517. + * Compress a domain name into wire format, using compression pointers.
  518. + * return:
  519. + * Number of bytes consumed in `dst' or -1 (with errno set).
  520. + * notes:
  521. + * 'dnptrs' is an array of pointers to previous compressed names.
  522. + * dnptrs[0] is a pointer to the beginning of the message.
  523. + * The list ends with NULL. 'lastdnptr' is a pointer to the end of the
  524. + * array pointed to by 'dnptrs'. Side effect is to update the list of
  525. + * pointers for labels inserted into the message as we compress the name.
  526. + * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
  527. + * is NULL, we don't update the list.
  528. + */
  529. +int
  530. +ns_name_compress(const char *src, u_char *dst, size_t dstsiz,
  531. + const u_char **dnptrs, const u_char **lastdnptr)
  532. +{
  533. + u_char tmp[NS_MAXCDNAME];
  534. +
  535. + if (ns_name_pton(src, tmp, sizeof tmp) == -1)
  536. + return (-1);
  537. + return (ns_name_pack(tmp, dst, dstsiz, dnptrs, lastdnptr));
  538. +}
  539. +
  540. +/*
  541. + * special(ch)
  542. + * Thinking in noninternationalized USASCII (per the DNS spec),
  543. + * is this characted special ("in need of quoting") ?
  544. + * return:
  545. + * boolean.
  546. + */
  547. +static int
  548. +special(int ch) {
  549. + switch (ch) {
  550. + case 0x22: /* '"' */
  551. + case 0x2E: /* '.' */
  552. + case 0x3B: /* ';' */
  553. + case 0x5C: /* '\\' */
  554. + /* Special modifiers in zone files. */
  555. + case 0x40: /* '@' */
  556. + case 0x24: /* '$' */
  557. + return (1);
  558. + default:
  559. + return (0);
  560. + }
  561. +}
  562. +
  563. +/*
  564. + * printable(ch)
  565. + * Thinking in noninternationalized USASCII (per the DNS spec),
  566. + * is this character visible and not a space when printed ?
  567. + * return:
  568. + * boolean.
  569. + */
  570. +static int
  571. +printable(int ch) {
  572. + return (ch > 0x20 && ch < 0x7f);
  573. +}
  574. +
  575. +/*
  576. + * Thinking in noninternationalized USASCII (per the DNS spec),
  577. + * convert this character to lower case if it's upper case.
  578. + */
  579. +static int
  580. +mklower(int ch) {
  581. + if (ch >= 0x41 && ch <= 0x5A)
  582. + return (ch + 0x20);
  583. + return (ch);
  584. +}
  585. +
  586. +/*
  587. + * dn_find(domain, msg, dnptrs, lastdnptr)
  588. + * Search for the counted-label name in an array of compressed names.
  589. + * return:
  590. + * offset from msg if found, or -1.
  591. + * notes:
  592. + * dnptrs is the pointer to the first name on the list,
  593. + * not the pointer to the start of the message.
  594. + */
  595. +static int
  596. +dn_find(const u_char *domain, const u_char *msg,
  597. + const u_char * const *dnptrs,
  598. + const u_char * const *lastdnptr)
  599. +{
  600. + const u_char *dn, *cp, *sp;
  601. + const u_char * const *cpp;
  602. + u_int n;
  603. +
  604. + for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
  605. + sp = *cpp;
  606. + /*
  607. + * terminate search on:
  608. + * root label
  609. + * compression pointer
  610. + * unusable offset
  611. + */
  612. + while (*sp != 0 && (*sp & NS_CMPRSFLGS) == 0 &&
  613. + (sp - msg) < 0x4000) {
  614. + dn = domain;
  615. + cp = sp;
  616. + while ((n = *cp++) != 0) {
  617. + /*
  618. + * check for indirection
  619. + */
  620. + switch (n & NS_CMPRSFLGS) {
  621. + case 0: /* normal case, n == len */
  622. + if (n != *dn++)
  623. + goto next;
  624. + for ((void)NULL; n > 0; n--)
  625. + if (mklower(*dn++) !=
  626. + mklower(*cp++))
  627. + goto next;
  628. + /* Is next root for both ? */
  629. + if (*dn == '\0' && *cp == '\0')
  630. + return (sp - msg);
  631. + if (*dn)
  632. + continue;
  633. + goto next;
  634. +
  635. + case NS_CMPRSFLGS: /* indirection */
  636. + cp = msg + (((n & 0x3f) << 8) | *cp);
  637. + break;
  638. +
  639. + default: /* illegal type */
  640. + return (-1);
  641. + }
  642. + }
  643. + next:
  644. + sp += *sp + 1;
  645. + }
  646. + }
  647. + return (-1);
  648. +}
  649. +
  650. +/*
  651. + * Expand compressed domain name 'comp_dn' to full domain name.
  652. + * 'msg' is a pointer to the begining of the message,
  653. + * 'eomorig' points to the first location after the message,
  654. + * 'exp_dn' is a pointer to a buffer of size 'length' for the result.
  655. + * Return size of compressed name or -1 if there was an error.
  656. + */
  657. +int
  658. +dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
  659. + char *dst, int dstsiz)
  660. +{
  661. + int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
  662. +
  663. + if (n > 0 && dst[0] == '.')
  664. + dst[0] = '\0';
  665. + return (n);
  666. +}
  667. +
  668. +/*
  669. + * Pack domain name 'exp_dn' in presentation form into 'comp_dn'.
  670. + * Return the size of the compressed name or -1.
  671. + * 'length' is the size of the array pointed to by 'comp_dn'.
  672. + */
  673. +int
  674. +dn_comp(const char *src, u_char *dst, int dstsiz,
  675. + u_char **dnptrs, u_char **lastdnptr)
  676. +{
  677. + return (ns_name_compress(src, dst, (size_t)dstsiz,
  678. + (const u_char **)dnptrs,
  679. + (const u_char **)lastdnptr));
  680. +}
  681. +