inet_aton.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright (c) 2001 Charles Mott <cm@linktel.net>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. #include <sys/cdefs.h>
  29. __FBSDID("$FreeBSD$");
  30. #include <sys/param.h>
  31. #include <sys/ctype.h>
  32. #include <sys/limits.h>
  33. #include <sys/systm.h>
  34. #include <netinet/in.h>
  35. int
  36. inet_aton(const char *cp, struct in_addr *addr)
  37. {
  38. u_long parts[4];
  39. in_addr_t val;
  40. const char *c;
  41. char *endptr;
  42. int gotend, n;
  43. c = (const char *)cp;
  44. n = 0;
  45. /*
  46. * Run through the string, grabbing numbers until
  47. * the end of the string, or some error
  48. */
  49. gotend = 0;
  50. while (!gotend) {
  51. unsigned long l;
  52. l = strtoul(c, &endptr, 0);
  53. if (l == ULONG_MAX || (l == 0 && endptr == c))
  54. return (0);
  55. val = (in_addr_t)l;
  56. /*
  57. * If the whole string is invalid, endptr will equal
  58. * c.. this way we can make sure someone hasn't
  59. * gone '.12' or something which would get past
  60. * the next check.
  61. */
  62. if (endptr == c)
  63. return (0);
  64. parts[n] = val;
  65. c = endptr;
  66. /* Check the next character past the previous number's end */
  67. switch (*c) {
  68. case '.' :
  69. /* Make sure we only do 3 dots .. */
  70. if (n == 3) /* Whoops. Quit. */
  71. return (0);
  72. n++;
  73. c++;
  74. break;
  75. case '\0':
  76. gotend = 1;
  77. break;
  78. default:
  79. if (isspace((unsigned char)*c)) {
  80. gotend = 1;
  81. break;
  82. } else {
  83. /* Invalid character, then fail. */
  84. return (0);
  85. }
  86. }
  87. }
  88. /* Concoct the address according to the number of parts specified. */
  89. switch (n) {
  90. case 0: /* a -- 32 bits */
  91. /*
  92. * Nothing is necessary here. Overflow checking was
  93. * already done in strtoul().
  94. */
  95. break;
  96. case 1: /* a.b -- 8.24 bits */
  97. if (val > 0xffffff || parts[0] > 0xff)
  98. return (0);
  99. val |= parts[0] << 24;
  100. break;
  101. case 2: /* a.b.c -- 8.8.16 bits */
  102. if (val > 0xffff || parts[0] > 0xff || parts[1] > 0xff)
  103. return (0);
  104. val |= (parts[0] << 24) | (parts[1] << 16);
  105. break;
  106. case 3: /* a.b.c.d -- 8.8.8.8 bits */
  107. if (val > 0xff || parts[0] > 0xff || parts[1] > 0xff ||
  108. parts[2] > 0xff)
  109. return (0);
  110. val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
  111. break;
  112. }
  113. if (addr != NULL)
  114. addr->s_addr = htonl(val);
  115. return (1);
  116. }