/contrib/bind9/lib/isc/inet_aton.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 196 lines · 82 code · 12 blank · 102 comment · 39 complexity · 07fdedde6b37df94032e4f726bd683cf MD5 · raw file

  1. /*
  2. * Portions Copyright (C) 2004, 2005, 2007, 2008 Internet Systems Consortium, Inc. ("ISC")
  3. * Portions Copyright (C) 1996-2001 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /*
  18. * Copyright (c) 1983, 1990, 1993
  19. * The Regents of the University of California. All rights reserved.
  20. *
  21. * Redistribution and use in source and binary forms, with or without
  22. * modification, are permitted provided that the following conditions
  23. * are met:
  24. * 1. Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * 2. Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in the
  28. * documentation and/or other materials provided with the distribution.
  29. * 3. All advertising materials mentioning features or use of this software
  30. * must display the following acknowledgement:
  31. * This product includes software developed by the University of
  32. * California, Berkeley and its contributors.
  33. * 4. Neither the name of the University nor the names of its contributors
  34. * may be used to endorse or promote products derived from this software
  35. * without specific prior written permission.
  36. *
  37. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  38. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  39. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  40. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  41. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  42. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  43. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  45. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  46. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. */
  49. /*
  50. * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  51. *
  52. * Permission to use, copy, modify, and distribute this software for any
  53. * purpose with or without fee is hereby granted, provided that the above
  54. * copyright notice and this permission notice appear in all copies, and that
  55. * the name of Digital Equipment Corporation not be used in advertising or
  56. * publicity pertaining to distribution of the document or software without
  57. * specific, written prior permission.
  58. *
  59. * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  60. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  61. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  62. * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  63. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  64. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  65. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  66. * SOFTWARE.
  67. */
  68. /*! \file */
  69. #if defined(LIBC_SCCS) && !defined(lint)
  70. static char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93";
  71. static char rcsid[] = "$Id: inet_aton.c,v 1.23 2008/12/01 23:47:45 tbox Exp $";
  72. #endif /* LIBC_SCCS and not lint */
  73. #include <config.h>
  74. #include <ctype.h>
  75. #include <stddef.h> /* Required for NULL. */
  76. #include <isc/types.h>
  77. #include <isc/net.h>
  78. /*%
  79. * Check whether "cp" is a valid ascii representation
  80. * of an Internet address and convert to a binary address.
  81. * Returns 1 if the address is valid, 0 if not.
  82. * This replaces inet_addr, the return value from which
  83. * cannot distinguish between failure and a local broadcast address.
  84. */
  85. int
  86. isc_net_aton(const char *cp, struct in_addr *addr) {
  87. unsigned long val;
  88. int base, n;
  89. unsigned char c;
  90. isc_uint8_t parts[4];
  91. isc_uint8_t *pp = parts;
  92. int digit;
  93. c = *cp;
  94. for (;;) {
  95. /*
  96. * Collect number up to ``.''.
  97. * Values are specified as for C:
  98. * 0x=hex, 0=octal, isdigit=decimal.
  99. */
  100. if (!isdigit(c & 0xff))
  101. return (0);
  102. val = 0; base = 10; digit = 0;
  103. if (c == '0') {
  104. c = *++cp;
  105. if (c == 'x' || c == 'X')
  106. base = 16, c = *++cp;
  107. else {
  108. base = 8;
  109. digit = 1;
  110. }
  111. }
  112. for (;;) {
  113. /*
  114. * isascii() is valid for all integer values, and
  115. * when it is true, c is known to be in scope
  116. * for isdigit(). No cast necessary. Similar
  117. * comment applies for later ctype uses.
  118. */
  119. if (isascii(c) && isdigit(c)) {
  120. if (base == 8 && (c == '8' || c == '9'))
  121. return (0);
  122. val = (val * base) + (c - '0');
  123. c = *++cp;
  124. digit = 1;
  125. } else if (base == 16 && isascii(c) && isxdigit(c)) {
  126. val = (val << 4) |
  127. (c + 10 - (islower(c) ? 'a' : 'A'));
  128. c = *++cp;
  129. digit = 1;
  130. } else
  131. break;
  132. }
  133. if (c == '.') {
  134. /*
  135. * Internet format:
  136. * a.b.c.d
  137. * a.b.c (with c treated as 16 bits)
  138. * a.b (with b treated as 24 bits)
  139. */
  140. if (pp >= parts + 3 || val > 0xffU)
  141. return (0);
  142. *pp++ = (isc_uint8_t)val;
  143. c = *++cp;
  144. } else
  145. break;
  146. }
  147. /*
  148. * Check for trailing characters.
  149. */
  150. if (c != '\0' && (!isascii(c) || !isspace(c)))
  151. return (0);
  152. /*
  153. * Did we get a valid digit?
  154. */
  155. if (!digit)
  156. return (0);
  157. /*
  158. * Concoct the address according to
  159. * the number of parts specified.
  160. */
  161. n = pp - parts + 1;
  162. switch (n) {
  163. case 1: /* a -- 32 bits */
  164. break;
  165. case 2: /* a.b -- 8.24 bits */
  166. if (val > 0xffffffU)
  167. return (0);
  168. val |= parts[0] << 24;
  169. break;
  170. case 3: /* a.b.c -- 8.8.16 bits */
  171. if (val > 0xffffU)
  172. return (0);
  173. val |= (parts[0] << 24) | (parts[1] << 16);
  174. break;
  175. case 4: /* a.b.c.d -- 8.8.8.8 bits */
  176. if (val > 0xffU)
  177. return (0);
  178. val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
  179. break;
  180. }
  181. if (addr != NULL)
  182. addr->s_addr = htonl(val);
  183. return (1);
  184. }