4
0

inet_aton.c 6.1 KB

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