inet_aton.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * inet_aton.c -- provides inet_aton() if necessary.
  3. *
  4. */
  5. #include "common.h"
  6. #include "inet_aton.h"
  7. #ifndef HAVE_ISASCII
  8. # define inet_isascii(x) 1 /* Let checks succeed if we don't have isascii(). */
  9. #else
  10. # define inet_isascii(x) egg_isascii(x)
  11. #endif
  12. #ifndef HAVE_INET_ATON
  13. /*
  14. * ++Copyright++ 1983, 1990, 1993
  15. * -
  16. * Copyright (c) 1983, 1990, 1993
  17. * The Regents of the University of California. All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions
  21. * are met:
  22. * 1. Redistributions of source code must retain the above copyright
  23. * notice, this list of conditions and the following disclaimer.
  24. * 2. Redistributions in binary form must reproduce the above copyright
  25. * notice, this list of conditions and the following disclaimer in the
  26. * documentation and/or other materials provided with the distribution.
  27. * 3. All advertising materials mentioning features or use of this software
  28. * must display the following acknowledgement:
  29. * This product includes software developed by the University of
  30. * California, Berkeley and its contributors.
  31. * 4. Neither the name of the University nor the names of its contributors
  32. * may be used to endorse or promote products derived from this software
  33. * without specific prior written permission.
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  36. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  38. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  39. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  40. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  41. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  42. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  43. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  44. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  45. * SUCH DAMAGE.
  46. * -
  47. * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  48. *
  49. * Permission to use, copy, modify, and distribute this software for any
  50. * purpose with or without fee is hereby granted, provided that the above
  51. * copyright notice and this permission notice appear in all copies, and that
  52. * the name of Digital Equipment Corporation not be used in advertising or
  53. * publicity pertaining to distribution of the document or software without
  54. * specific, written prior permission.
  55. *
  56. * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  57. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  58. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  59. * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  60. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  61. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  62. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  63. * SOFTWARE.
  64. * --Copyright--
  65. */
  66. #if defined(LIBC_SCCS) && !defined(lint)
  67. static char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93";
  68. static char rcsid[] = "$-Id: inet_addr.c,v 1.11 1999/04/29 18:19:53 drepper Exp $";
  69. #endif /* LIBC_SCCS and not lint */
  70. #include <sys/types.h>
  71. #include <sys/param.h>
  72. #include <ctype.h>
  73. /*
  74. * Check whether "cp" is a valid ascii representation
  75. * of an Internet address and convert to a binary address.
  76. * Returns 1 if the address is valid, 0 if not.
  77. * This replaces inet_addr, the return value from which
  78. * cannot distinguish between failure and a local broadcast address.
  79. */
  80. int
  81. egg_inet_aton(cp, addr)
  82. const char *cp;
  83. struct in_addr *addr;
  84. {
  85. static const u_32bit_t max[4] = { 0xffffffff, 0xffffff, 0xffff, 0xff };
  86. register u_32bit_t val; /* changed from u_long --david */
  87. register int base;
  88. register int n;
  89. register char c;
  90. u_32bit_t parts[4];
  91. register u_32bit_t *pp = parts;
  92. egg_bzero(parts, sizeof (parts));
  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 (!egg_isdigit(c))
  101. goto ret_0;
  102. base = 10;
  103. if (c == '0') {
  104. c = *++cp;
  105. if (c == 'x' || c == 'X')
  106. base = 16, c = *++cp;
  107. else
  108. base = 8;
  109. }
  110. val = 0;
  111. for (;;) {
  112. if (inet_isascii(c) && egg_isdigit(c)) {
  113. val = (val * base) + (c - '0');
  114. c = *++cp;
  115. } else if (base == 16 && inet_isascii(c) && egg_isxdigit(c)) {
  116. val = (val << 4) |
  117. (c + 10 - (egg_islower(c) ? 'a' : 'A'));
  118. c = *++cp;
  119. } else
  120. break;
  121. }
  122. if (c == '.') {
  123. /*
  124. * Internet format:
  125. * a.b.c.d
  126. * a.b.c (with c treated as 16 bits)
  127. * a.b (with b treated as 24 bits)
  128. */
  129. if (pp >= parts + 3)
  130. goto ret_0;
  131. *pp++ = val;
  132. c = *++cp;
  133. } else
  134. break;
  135. }
  136. /*
  137. * Check for trailing characters.
  138. */
  139. if (c != '\0' && (!inet_isascii(c) || !egg_isspace(c)))
  140. goto ret_0;
  141. /*
  142. * Concoct the address according to
  143. * the number of parts specified.
  144. */
  145. n = pp - parts + 1;
  146. if (n == 0 /* initial nondigit */
  147. || parts[0] > 0xff || parts[1] > 0xff || parts[2] > 0xff
  148. || val > max[n - 1])
  149. goto ret_0;
  150. val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
  151. if (addr)
  152. addr->s_addr = htonl(val);
  153. return (1);
  154. ret_0:
  155. return (0);
  156. }
  157. #endif /* HAVE_INET_ATON */