inet_pton.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c) 1996,1999 by Internet Software Consortium.
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  9. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  10. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  11. * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  13. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  14. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  15. * SOFTWARE.
  16. */
  17. #include "api.h"
  18. #include <netinet/in.h>
  19. #include <arpa/inet.h>
  20. #include <arpa/nameser.h>
  21. #include <asm-errno.h>
  22. /*
  23. * WARNING: Don't even consider trying to compile this on a system where
  24. * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
  25. */
  26. static int inet_pton4 (const char *src, unsigned char *dst);
  27. static int inet_pton6 (const char *src, unsigned char *dst);
  28. /* int inet_pton(af, src, dst)
  29. * convert from presentation format (which usually means ASCII printable)
  30. * to network format (which is usually some kind of binary format).
  31. * return:
  32. * 1 if the address was valid for the specified address family
  33. * 0 if the address wasn't valid (`dst' is untouched in this case)
  34. * -1 if some other error occurred (`dst' is untouched in this case, too)
  35. * author:
  36. * Paul Vixie, 1996.
  37. */
  38. int inet_pton (int af, const char *src, void *dst)
  39. {
  40. switch (af) {
  41. case AF_INET:
  42. return (inet_pton4(src, dst));
  43. case AF_INET6:
  44. return (inet_pton6(src, dst));
  45. default:
  46. return -EAFNOSUPPORT;
  47. }
  48. /* NOTREACHED */
  49. }
  50. /* int inet_pton4(src, dst)
  51. * like inet_aton() but without all the hexadecimal, octal (with the
  52. * exception of 0) and shorthand.
  53. * return:
  54. * 1 if `src' is a valid dotted quad, else 0.
  55. * notice:
  56. * does not touch `dst' unless it's returning 1.
  57. * author:
  58. * Paul Vixie, 1996.
  59. */
  60. static int inet_pton4 (const char *src, unsigned char *dst)
  61. {
  62. int saw_digit, octets, ch;
  63. unsigned char tmp[NS_INADDRSZ], *tp;
  64. saw_digit = 0;
  65. octets = 0;
  66. *(tp = tmp) = 0;
  67. while ((ch = *src++) != '\0') {
  68. if (ch >= '0' && ch <= '9') {
  69. u_int new = *tp * 10 + (ch - '0');
  70. if (saw_digit && *tp == 0)
  71. return (0);
  72. if (new > 255)
  73. return (0);
  74. *tp = new;
  75. if (! saw_digit) {
  76. if (++octets > 4)
  77. return (0);
  78. saw_digit = 1;
  79. }
  80. } else if (ch == '.' && saw_digit) {
  81. if (octets == 4)
  82. return (0);
  83. *++tp = 0;
  84. saw_digit = 0;
  85. } else
  86. return (0);
  87. }
  88. if (octets < 4)
  89. return (0);
  90. memcpy (dst, tmp, NS_INADDRSZ);
  91. return (1);
  92. }
  93. static int tolower (char c)
  94. {
  95. return c >= 'A' && c < 'Z' ? c + ('a' - 'A') : c;
  96. }
  97. /* int inet_pton6(src, dst)
  98. * convert presentation level address to network order binary form.
  99. * return:
  100. * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
  101. * notice:
  102. * (1) does not touch `dst' unless it's returning 1.
  103. * (2) :: in a full address is silently ignored.
  104. * credit:
  105. * inspired by Mark Andrews.
  106. * author:
  107. * Paul Vixie, 1996.
  108. */
  109. static int inet_pton6 (const char *src, unsigned char *dst)
  110. {
  111. static const char xdigits[] = "0123456789abcdef";
  112. unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
  113. const char *curtok;
  114. int ch, saw_xdigit;
  115. unsigned int val;
  116. tp = memset (tmp, '\0', NS_IN6ADDRSZ);
  117. endp = tp + NS_IN6ADDRSZ;
  118. colonp = NULL;
  119. /* Leading :: requires some special handling. */
  120. if (*src == ':')
  121. if (*++src != ':')
  122. return (0);
  123. curtok = src;
  124. saw_xdigit = 0;
  125. val = 0;
  126. while ((ch = tolower (*src++)) != '\0') {
  127. const char *pch;
  128. pch = strchr (xdigits, ch);
  129. if (pch != NULL) {
  130. val <<= 4;
  131. val |= (pch - xdigits);
  132. if (val > 0xffff)
  133. return (0);
  134. saw_xdigit = 1;
  135. continue;
  136. }
  137. if (ch == ':') {
  138. curtok = src;
  139. if (!saw_xdigit) {
  140. if (colonp)
  141. return (0);
  142. colonp = tp;
  143. continue;
  144. } else if (*src == '\0') {
  145. return (0);
  146. }
  147. if (tp + NS_INT16SZ > endp)
  148. return (0);
  149. *tp++ = (unsigned char) (val >> 8) & 0xff;
  150. *tp++ = (unsigned char) val & 0xff;
  151. saw_xdigit = 0;
  152. val = 0;
  153. continue;
  154. }
  155. if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
  156. inet_pton4(curtok, tp) > 0) {
  157. tp += NS_INADDRSZ;
  158. saw_xdigit = 0;
  159. break; /* '\0' was seen by inet_pton4(). */
  160. }
  161. return (0);
  162. }
  163. if (saw_xdigit) {
  164. if (tp + NS_INT16SZ > endp)
  165. return (0);
  166. *tp++ = (unsigned char) (val >> 8) & 0xff;
  167. *tp++ = (unsigned char) val & 0xff;
  168. }
  169. if (colonp != NULL) {
  170. /*
  171. * Since some memmove()'s erroneously fail to handle
  172. * overlapping regions, we'll do the shift by hand.
  173. */
  174. const int n = tp - colonp;
  175. int i;
  176. if (tp == endp)
  177. return (0);
  178. for (i = 1; i <= n; i++) {
  179. endp[- i] = colonp[n - i];
  180. colonp[n - i] = 0;
  181. }
  182. tp = endp;
  183. }
  184. if (tp != endp)
  185. return (0);
  186. memcpy (dst, tmp, NS_IN6ADDRSZ);
  187. return (1);
  188. }