PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/netbsd/src/external/bsd/libbind/dist/resolv/res_comp.c

https://bitbucket.org/killerpenguinassassins/open_distrib_devel
C | 267 lines | 126 code | 25 blank | 116 comment | 48 complexity | 271b1dfd2a9f18f231d5f76f64e0971a MD5 | raw file
Possible License(s): CC0-1.0, MIT, LGPL-2.0, LGPL-3.0, WTFPL, GPL-2.0, BSD-2-Clause, AGPL-3.0, CC-BY-SA-3.0, MPL-2.0, JSON, BSD-3-Clause-No-Nuclear-License-2014, LGPL-2.1, CPL-1.0, AGPL-1.0, 0BSD, ISC, Apache-2.0, GPL-3.0, IPL-1.0, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /* $NetBSD: res_comp.c,v 1.1.1.1 2009/04/12 15:33:55 christos Exp $ */
  2. /*
  3. * Copyright (c) 1985, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. All advertising materials mentioning features or use of this software
  15. * must display the following acknowledgement:
  16. * This product includes software developed by the University of
  17. * California, Berkeley and its contributors.
  18. * 4. Neither the name of the University nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  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. * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
  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 ISC DISCLAIMS ALL WARRANTIES
  62. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  63. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
  64. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  65. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  66. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  67. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  68. */
  69. #if defined(LIBC_SCCS) && !defined(lint)
  70. static const char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93";
  71. static const char rcsid[] = "Id: res_comp.c,v 1.5 2005/07/28 06:51:50 marka Exp";
  72. #endif /* LIBC_SCCS and not lint */
  73. #include "port_before.h"
  74. #include <sys/types.h>
  75. #include <sys/param.h>
  76. #include <netinet/in.h>
  77. #include <arpa/nameser.h>
  78. #include <ctype.h>
  79. #include <resolv.h>
  80. #include <stdio.h>
  81. #include <string.h>
  82. #include <unistd.h>
  83. #include "port_after.h"
  84. /*%
  85. * Expand compressed domain name 'src' to full domain name.
  86. *
  87. * \li 'msg' is a pointer to the begining of the message,
  88. * \li 'eom' points to the first location after the message,
  89. * \li 'dst' is a pointer to a buffer of size 'dstsiz' for the result.
  90. * \li Return size of compressed name or -1 if there was an error.
  91. */
  92. int
  93. dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
  94. char *dst, int dstsiz)
  95. {
  96. int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
  97. if (n > 0 && dst[0] == '.')
  98. dst[0] = '\0';
  99. return (n);
  100. }
  101. /*%
  102. * Pack domain name 'exp_dn' in presentation form into 'comp_dn'.
  103. *
  104. * \li Return the size of the compressed name or -1.
  105. * \li 'length' is the size of the array pointed to by 'comp_dn'.
  106. */
  107. int
  108. dn_comp(const char *src, u_char *dst, int dstsiz,
  109. u_char **dnptrs, u_char **lastdnptr)
  110. {
  111. return (ns_name_compress(src, dst, (size_t)dstsiz,
  112. (const u_char **)dnptrs,
  113. (const u_char **)lastdnptr));
  114. }
  115. /*%
  116. * Skip over a compressed domain name. Return the size or -1.
  117. */
  118. int
  119. dn_skipname(const u_char *ptr, const u_char *eom) {
  120. const u_char *saveptr = ptr;
  121. if (ns_name_skip(&ptr, eom) == -1)
  122. return (-1);
  123. return (ptr - saveptr);
  124. }
  125. /*%
  126. * Verify that a domain name uses an acceptable character set.
  127. *
  128. * Note the conspicuous absence of ctype macros in these definitions. On
  129. * non-ASCII hosts, we can't depend on string literals or ctype macros to
  130. * tell us anything about network-format data. The rest of the BIND system
  131. * is not careful about this, but for some reason, we're doing it right here.
  132. */
  133. #define PERIOD 0x2e
  134. #define hyphenchar(c) ((c) == 0x2d)
  135. #define bslashchar(c) ((c) == 0x5c)
  136. #define periodchar(c) ((c) == PERIOD)
  137. #define asterchar(c) ((c) == 0x2a)
  138. #define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \
  139. || ((c) >= 0x61 && (c) <= 0x7a))
  140. #define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)
  141. #define borderchar(c) (alphachar(c) || digitchar(c))
  142. #define middlechar(c) (borderchar(c) || hyphenchar(c))
  143. #define domainchar(c) ((c) > 0x20 && (c) < 0x7f)
  144. int
  145. res_hnok(const char *dn) {
  146. int pch = PERIOD, ch = *dn++;
  147. while (ch != '\0') {
  148. int nch = *dn++;
  149. if (periodchar(ch)) {
  150. (void)NULL;
  151. } else if (periodchar(pch)) {
  152. if (!borderchar(ch))
  153. return (0);
  154. } else if (periodchar(nch) || nch == '\0') {
  155. if (!borderchar(ch))
  156. return (0);
  157. } else {
  158. if (!middlechar(ch))
  159. return (0);
  160. }
  161. pch = ch, ch = nch;
  162. }
  163. return (1);
  164. }
  165. /*%
  166. * hostname-like (A, MX, WKS) owners can have "*" as their first label
  167. * but must otherwise be as a host name.
  168. */
  169. int
  170. res_ownok(const char *dn) {
  171. if (asterchar(dn[0])) {
  172. if (periodchar(dn[1]))
  173. return (res_hnok(dn+2));
  174. if (dn[1] == '\0')
  175. return (1);
  176. }
  177. return (res_hnok(dn));
  178. }
  179. /*%
  180. * SOA RNAMEs and RP RNAMEs can have any printable character in their first
  181. * label, but the rest of the name has to look like a host name.
  182. */
  183. int
  184. res_mailok(const char *dn) {
  185. int ch, escaped = 0;
  186. /* "." is a valid missing representation */
  187. if (*dn == '\0')
  188. return (1);
  189. /* otherwise <label>.<hostname> */
  190. while ((ch = *dn++) != '\0') {
  191. if (!domainchar(ch))
  192. return (0);
  193. if (!escaped && periodchar(ch))
  194. break;
  195. if (escaped)
  196. escaped = 0;
  197. else if (bslashchar(ch))
  198. escaped = 1;
  199. }
  200. if (periodchar(ch))
  201. return (res_hnok(dn));
  202. return (0);
  203. }
  204. /*%
  205. * This function is quite liberal, since RFC1034's character sets are only
  206. * recommendations.
  207. */
  208. int
  209. res_dnok(const char *dn) {
  210. int ch;
  211. while ((ch = *dn++) != '\0')
  212. if (!domainchar(ch))
  213. return (0);
  214. return (1);
  215. }
  216. #ifdef BIND_4_COMPAT
  217. /*%
  218. * This module must export the following externally-visible symbols:
  219. * ___putlong
  220. * ___putshort
  221. * __getlong
  222. * __getshort
  223. * Note that one _ comes from C and the others come from us.
  224. */
  225. #ifdef SOLARIS2
  226. #ifdef __putlong
  227. #undef __putlong
  228. #endif
  229. #ifdef __putshort
  230. #undef __putshort
  231. #endif
  232. #pragma weak putlong = __putlong
  233. #pragma weak putshort = __putshort
  234. #endif /* SOLARIS2 */
  235. void __putlong(u_int32_t src, u_char *dst) { ns_put32(src, dst); }
  236. void __putshort(u_int16_t src, u_char *dst) { ns_put16(src, dst); }
  237. #ifndef __ultrix__
  238. u_int32_t _getlong(const u_char *src) { return (ns_get32(src)); }
  239. u_int16_t _getshort(const u_char *src) { return (ns_get16(src)); }
  240. #endif /*__ultrix__*/
  241. #endif /*BIND_4_COMPAT*/
  242. /*! \file */