PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/nbsd_libc/resolv/res_comp.c

http://github.com/vivekp/minix-nbsd
C | 285 lines | 143 code | 26 blank | 116 comment | 48 complexity | 7e0aeeb797502452198adf1b7df9a52e MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause
  1. /* $NetBSD: res_comp.c,v 1.10 2009/04/12 17:07:17 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. #include <sys/cdefs.h>
  70. #if defined(LIBC_SCCS) && !defined(lint)
  71. #ifdef notdef
  72. static const char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93";
  73. static const char rcsid[] = "Id: res_comp.c,v 1.5 2005/07/28 06:51:50 marka Exp";
  74. #else
  75. __RCSID("$NetBSD: res_comp.c,v 1.10 2009/04/12 17:07:17 christos Exp $");
  76. #endif
  77. #endif /* LIBC_SCCS and not lint */
  78. #include "port_before.h"
  79. #include "namespace.h"
  80. #include <sys/types.h>
  81. #include <sys/param.h>
  82. #include <netinet/in.h>
  83. #include <arpa/nameser.h>
  84. #include <ctype.h>
  85. #include <resolv.h>
  86. #include <stdio.h>
  87. #include <string.h>
  88. #include <unistd.h>
  89. #include "port_after.h"
  90. #ifdef __weak_alias
  91. __weak_alias(dn_expand,_dn_expand)
  92. __weak_alias(dn_comp,__dn_comp)
  93. #ifdef __minix
  94. __weak_alias(dn_skipname,__dn_skipname)
  95. __weak_alias(res_hnok,__res_hnok)
  96. __weak_alias(res_ownok,__res_ownok)
  97. __weak_alias(res_mailok,__res_mailok)
  98. __weak_alias(res_dnok,__res_dnok)
  99. #endif
  100. #endif
  101. /*%
  102. * Expand compressed domain name 'src' to full domain name.
  103. *
  104. * \li 'msg' is a pointer to the begining of the message,
  105. * \li 'eom' points to the first location after the message,
  106. * \li 'dst' is a pointer to a buffer of size 'dstsiz' for the result.
  107. * \li Return size of compressed name or -1 if there was an error.
  108. */
  109. int
  110. dn_expand(const u_char *msg, const u_char *eom, const u_char *src,
  111. char *dst, int dstsiz)
  112. {
  113. int n = ns_name_uncompress(msg, eom, src, dst, (size_t)dstsiz);
  114. if (n > 0 && dst[0] == '.')
  115. dst[0] = '\0';
  116. return (n);
  117. }
  118. /*%
  119. * Pack domain name 'exp_dn' in presentation form into 'comp_dn'.
  120. *
  121. * \li Return the size of the compressed name or -1.
  122. * \li 'length' is the size of the array pointed to by 'comp_dn'.
  123. */
  124. int
  125. dn_comp(const char *src, u_char *dst, int dstsiz,
  126. u_char **dnptrs, u_char **lastdnptr)
  127. {
  128. return (ns_name_compress(src, dst, (size_t)dstsiz,
  129. (const u_char **)dnptrs,
  130. (const u_char **)lastdnptr));
  131. }
  132. /*%
  133. * Skip over a compressed domain name. Return the size or -1.
  134. */
  135. int
  136. dn_skipname(const u_char *ptr, const u_char *eom) {
  137. const u_char *saveptr = ptr;
  138. if (ns_name_skip(&ptr, eom) == -1)
  139. return (-1);
  140. return (ptr - saveptr);
  141. }
  142. /*%
  143. * Verify that a domain name uses an acceptable character set.
  144. *
  145. * Note the conspicuous absence of ctype macros in these definitions. On
  146. * non-ASCII hosts, we can't depend on string literals or ctype macros to
  147. * tell us anything about network-format data. The rest of the BIND system
  148. * is not careful about this, but for some reason, we're doing it right here.
  149. */
  150. #define PERIOD 0x2e
  151. #define hyphenchar(c) ((c) == 0x2d)
  152. #define bslashchar(c) ((c) == 0x5c)
  153. #define periodchar(c) ((c) == PERIOD)
  154. #define asterchar(c) ((c) == 0x2a)
  155. #define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \
  156. || ((c) >= 0x61 && (c) <= 0x7a))
  157. #define digitchar(c) ((c) >= 0x30 && (c) <= 0x39)
  158. #define borderchar(c) (alphachar(c) || digitchar(c))
  159. #define middlechar(c) (borderchar(c) || hyphenchar(c))
  160. #define domainchar(c) ((c) > 0x20 && (c) < 0x7f)
  161. int
  162. res_hnok(const char *dn) {
  163. int pch = PERIOD, ch = *dn++;
  164. while (ch != '\0') {
  165. int nch = *dn++;
  166. if (periodchar(ch)) {
  167. ;
  168. } else if (periodchar(pch)) {
  169. if (!borderchar(ch))
  170. return (0);
  171. } else if (periodchar(nch) || nch == '\0') {
  172. if (!borderchar(ch))
  173. return (0);
  174. } else {
  175. if (!middlechar(ch))
  176. return (0);
  177. }
  178. pch = ch, ch = nch;
  179. }
  180. return (1);
  181. }
  182. /*%
  183. * hostname-like (A, MX, WKS) owners can have "*" as their first label
  184. * but must otherwise be as a host name.
  185. */
  186. int
  187. res_ownok(const char *dn) {
  188. if (asterchar(dn[0])) {
  189. if (periodchar(dn[1]))
  190. return (res_hnok(dn+2));
  191. if (dn[1] == '\0')
  192. return (1);
  193. }
  194. return (res_hnok(dn));
  195. }
  196. /*%
  197. * SOA RNAMEs and RP RNAMEs can have any printable character in their first
  198. * label, but the rest of the name has to look like a host name.
  199. */
  200. int
  201. res_mailok(const char *dn) {
  202. int ch, escaped = 0;
  203. /* "." is a valid missing representation */
  204. if (*dn == '\0')
  205. return (1);
  206. /* otherwise <label>.<hostname> */
  207. while ((ch = *dn++) != '\0') {
  208. if (!domainchar(ch))
  209. return (0);
  210. if (!escaped && periodchar(ch))
  211. break;
  212. if (escaped)
  213. escaped = 0;
  214. else if (bslashchar(ch))
  215. escaped = 1;
  216. }
  217. if (periodchar(ch))
  218. return (res_hnok(dn));
  219. return (0);
  220. }
  221. /*%
  222. * This function is quite liberal, since RFC1034's character sets are only
  223. * recommendations.
  224. */
  225. int
  226. res_dnok(const char *dn) {
  227. int ch;
  228. while ((ch = *dn++) != '\0')
  229. if (!domainchar(ch))
  230. return (0);
  231. return (1);
  232. }
  233. #ifdef BIND_4_COMPAT
  234. /*%
  235. * This module must export the following externally-visible symbols:
  236. * ___putlong
  237. * ___putshort
  238. * __getlong
  239. * __getshort
  240. * Note that one _ comes from C and the others come from us.
  241. */
  242. #ifdef SOLARIS2
  243. #ifdef __putlong
  244. #undef __putlong
  245. #endif
  246. #ifdef __putshort
  247. #undef __putshort
  248. #endif
  249. #pragma weak putlong = __putlong
  250. #pragma weak putshort = __putshort
  251. #endif /* SOLARIS2 */
  252. void __putlong(u_int32_t src, u_char *dst) { ns_put32(src, dst); }
  253. void __putshort(u_int16_t src, u_char *dst) { ns_put16(src, dst); }
  254. #ifndef __ultrix__
  255. u_int32_t _getlong(const u_char *src) { return (ns_get32(src)); }
  256. u_int16_t _getshort(const u_char *src) { return (ns_get16(src)); }
  257. #endif /*__ultrix__*/
  258. #endif /*BIND_4_COMPAT*/
  259. /*! \file */