inet_pton.c 5.0 KB

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