inet_addr.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* Legacy IPv4 text-to-address functions.
  2. Copyright (C) 2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  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. * 4. Neither the name of the University nor the names of its contributors
  28. * may be used to endorse or promote products derived from this software
  29. * without specific prior written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  32. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  35. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  36. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  37. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  39. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  40. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  41. * SUCH DAMAGE.
  42. */
  43. /*
  44. * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  45. *
  46. * Permission to use, copy, modify, and distribute this software for any
  47. * purpose with or without fee is hereby granted, provided that the above
  48. * copyright notice and this permission notice appear in all copies, and that
  49. * the name of Digital Equipment Corporation not be used in advertising or
  50. * publicity pertaining to distribution of the document or software without
  51. * specific, written prior permission.
  52. *
  53. * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  54. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  55. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  56. * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  57. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  58. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  59. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  60. * SOFTWARE.
  61. */
  62. /*
  63. * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
  64. *
  65. * Permission to use, copy, modify, and distribute this software for any
  66. * purpose with or without fee is hereby granted, provided that the above
  67. * copyright notice and this permission notice appear in all copies.
  68. *
  69. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  70. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  71. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  72. * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  73. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  74. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  75. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  76. * SOFTWARE.
  77. */
  78. #include <sys/types.h>
  79. #include <sys/param.h>
  80. #include <netinet/in.h>
  81. #include <arpa/inet.h>
  82. #include <ctype.h>
  83. #include <endian.h>
  84. #include <stdint.h>
  85. #include <stdlib.h>
  86. #include <limits.h>
  87. #include <errno.h>
  88. /* Check whether "cp" is a valid ASCII representation of an IPv4
  89. Internet address and convert it to a binary address. Returns 1 if
  90. the address is valid, 0 if not. This replaces inet_addr, the
  91. return value from which cannot distinguish between failure and a
  92. local broadcast address. Write a pointer to the first
  93. non-converted character to *endp. */
  94. static int
  95. inet_aton_end (const char *cp, struct in_addr *addr, const char **endp)
  96. {
  97. static const in_addr_t max[4] = { 0xffffffff, 0xffffff, 0xffff, 0xff };
  98. in_addr_t val;
  99. char c;
  100. union iaddr
  101. {
  102. uint8_t bytes[4];
  103. uint32_t word;
  104. } res;
  105. uint8_t *pp = res.bytes;
  106. int digit;
  107. int saved_errno = errno;
  108. __set_errno (0);
  109. res.word = 0;
  110. c = *cp;
  111. for (;;)
  112. {
  113. /* Collect number up to ``.''. Values are specified as for C:
  114. 0x=hex, 0=octal, isdigit=decimal. */
  115. if (!isdigit (c))
  116. goto ret_0;
  117. {
  118. char *endp;
  119. unsigned long ul = strtoul (cp, &endp, 0);
  120. if (ul == ULONG_MAX && errno == ERANGE)
  121. goto ret_0;
  122. if (ul > 0xfffffffful)
  123. goto ret_0;
  124. val = ul;
  125. digit = cp != endp;
  126. cp = endp;
  127. }
  128. c = *cp;
  129. if (c == '.')
  130. {
  131. /* Internet format:
  132. a.b.c.d
  133. a.b.c (with c treated as 16 bits)
  134. a.b (with b treated as 24 bits). */
  135. if (pp > res.bytes + 2 || val > 0xff)
  136. goto ret_0;
  137. *pp++ = val;
  138. c = *++cp;
  139. }
  140. else
  141. break;
  142. }
  143. /* Check for trailing characters. */
  144. if (c != '\0' && (!isascii (c) || !isspace (c)))
  145. goto ret_0;
  146. /* Did we get a valid digit? */
  147. if (!digit)
  148. goto ret_0;
  149. /* Check whether the last part is in its limits depending on the
  150. number of parts in total. */
  151. if (val > max[pp - res.bytes])
  152. goto ret_0;
  153. if (addr != NULL)
  154. addr->s_addr = res.word | htonl (val);
  155. *endp = cp;
  156. __set_errno (saved_errno);
  157. return 1;
  158. ret_0:
  159. __set_errno (saved_errno);
  160. return 0;
  161. }
  162. int
  163. __inet_aton_exact (const char *cp, struct in_addr *addr)
  164. {
  165. struct in_addr val;
  166. const char *endp;
  167. /* Check that inet_aton_end parsed the entire string. */
  168. if (inet_aton_end (cp, &val, &endp) != 0 && *endp == 0)
  169. {
  170. *addr = val;
  171. return 1;
  172. }
  173. else
  174. return 0;
  175. }
  176. libc_hidden_def (__inet_aton_exact)
  177. /* inet_aton ignores trailing garbage. */
  178. int
  179. __inet_aton_ignore_trailing (const char *cp, struct in_addr *addr)
  180. {
  181. const char *endp;
  182. return inet_aton_end (cp, addr, &endp);
  183. }
  184. weak_alias (__inet_aton_ignore_trailing, inet_aton)
  185. /* ASCII IPv4 Internet address interpretation routine. The value
  186. returned is in network order. */
  187. in_addr_t
  188. __inet_addr (const char *cp)
  189. {
  190. struct in_addr val;
  191. const char *endp;
  192. if (inet_aton_end (cp, &val, &endp))
  193. return val.s_addr;
  194. return INADDR_NONE;
  195. }
  196. weak_alias (__inet_addr, inet_addr)