patch-domain_resolver_c 17 KB

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