strtoaddr.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (c) 1996,1999 by Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  15. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include <netdissect-stdinc.h>
  21. #include <stddef.h>
  22. #include <string.h>
  23. #include "strtoaddr.h"
  24. #ifndef NS_INADDRSZ
  25. #define NS_INADDRSZ 4 /* IPv4 T_A */
  26. #endif
  27. #ifndef NS_IN6ADDRSZ
  28. #define NS_IN6ADDRSZ 16 /* IPv6 T_AAAA */
  29. #endif
  30. #ifndef NS_INT16SZ
  31. #define NS_INT16SZ 2 /* #/bytes of data in a uint16_t */
  32. #endif
  33. /*%
  34. * WARNING: Don't even consider trying to compile this on a system where
  35. * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
  36. */
  37. #ifndef NS_IN6ADDRSZ
  38. #define NS_IN6ADDRSZ 16 /* IPv6 T_AAAA */
  39. #endif
  40. /* int
  41. * strtoaddr(src, dst)
  42. * convert presentation level IPv4 address to network order binary form.
  43. * return:
  44. * 1 if `src' is a valid input, else 0.
  45. * notice:
  46. * does not touch `dst' unless it's returning 1.
  47. * author:
  48. * Paul Vixie, 1996.
  49. */
  50. int
  51. strtoaddr(const char *src, void *dst)
  52. {
  53. uint32_t val;
  54. u_int digit;
  55. ptrdiff_t n;
  56. unsigned char c;
  57. u_int parts[4];
  58. u_int *pp = parts;
  59. c = *src;
  60. for (;;) {
  61. /*
  62. * Collect number up to ``.''.
  63. * Values are specified as for C:
  64. * 0x=hex, 0=octal, isdigit=decimal.
  65. */
  66. if (!isdigit(c))
  67. return (0);
  68. val = 0;
  69. if (c == '0') {
  70. c = *++src;
  71. if (c == 'x' || c == 'X')
  72. return (0);
  73. else if (isdigit(c) && c != '9')
  74. return (0);
  75. }
  76. for (;;) {
  77. if (isdigit(c)) {
  78. digit = c - '0';
  79. if (digit >= 10)
  80. break;
  81. val = (val * 10) + digit;
  82. c = *++src;
  83. } else
  84. break;
  85. }
  86. if (c == '.') {
  87. /*
  88. * Internet format:
  89. * a.b.c.d
  90. * a.b.c (with c treated as 16 bits)
  91. * a.b (with b treated as 24 bits)
  92. * a (with a treated as 32 bits)
  93. */
  94. if (pp >= parts + 3)
  95. return (0);
  96. *pp++ = val;
  97. c = *++src;
  98. } else
  99. break;
  100. }
  101. /*
  102. * Check for trailing characters.
  103. */
  104. if (c != '\0' && !isspace(c))
  105. return (0);
  106. /*
  107. * Find the number of parts specified.
  108. * It must be 4; we only support dotted quads, we don't
  109. * support shorthand.
  110. */
  111. n = pp - parts + 1;
  112. if (n != 4)
  113. return (0);
  114. /*
  115. * parts[0-2] were set to the first 3 parts of the address;
  116. * val was set to the 4th part.
  117. *
  118. * Check if any part is bigger than 255.
  119. */
  120. if ((parts[0] | parts[1] | parts[2] | val) > 0xff)
  121. return (0);
  122. /*
  123. * Add the other three parts to val.
  124. */
  125. val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
  126. if (dst) {
  127. val = htonl(val);
  128. memcpy(dst, &val, NS_INADDRSZ);
  129. }
  130. return (1);
  131. }
  132. /* int
  133. * strtoaddr6(src, dst)
  134. * convert presentation level IPv6 address to network order binary form.
  135. * return:
  136. * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
  137. * notice:
  138. * (1) does not touch `dst' unless it's returning 1.
  139. * (2) :: in a full address is silently ignored.
  140. * credit:
  141. * inspired by Mark Andrews.
  142. * author:
  143. * Paul Vixie, 1996.
  144. */
  145. int
  146. strtoaddr6(const char *src, void *dst)
  147. {
  148. static const char xdigits_l[] = "0123456789abcdef",
  149. xdigits_u[] = "0123456789ABCDEF";
  150. u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
  151. const char *xdigits, *curtok;
  152. int ch, seen_xdigits;
  153. u_int val;
  154. memset((tp = tmp), '\0', NS_IN6ADDRSZ);
  155. endp = tp + NS_IN6ADDRSZ;
  156. colonp = NULL;
  157. /* Leading :: requires some special handling. */
  158. if (*src == ':')
  159. if (*++src != ':')
  160. return (0);
  161. curtok = src;
  162. seen_xdigits = 0;
  163. val = 0;
  164. while ((ch = *src++) != '\0') {
  165. const char *pch;
  166. if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
  167. pch = strchr((xdigits = xdigits_u), ch);
  168. if (pch != NULL) {
  169. val <<= 4;
  170. val |= (int)(pch - xdigits);
  171. if (++seen_xdigits > 4)
  172. return (0);
  173. continue;
  174. }
  175. if (ch == ':') {
  176. curtok = src;
  177. if (!seen_xdigits) {
  178. if (colonp)
  179. return (0);
  180. colonp = tp;
  181. continue;
  182. } else if (*src == '\0')
  183. return (0);
  184. if (tp + NS_INT16SZ > endp)
  185. return (0);
  186. *tp++ = (u_char) (val >> 8) & 0xff;
  187. *tp++ = (u_char) val & 0xff;
  188. seen_xdigits = 0;
  189. val = 0;
  190. continue;
  191. }
  192. if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
  193. strtoaddr(curtok, tp) > 0) {
  194. tp += NS_INADDRSZ;
  195. seen_xdigits = 0;
  196. break; /*%< '\\0' was seen by strtoaddr(). */
  197. }
  198. return (0);
  199. }
  200. if (seen_xdigits) {
  201. if (tp + NS_INT16SZ > endp)
  202. return (0);
  203. *tp++ = (u_char) (val >> 8) & 0xff;
  204. *tp++ = (u_char) val & 0xff;
  205. }
  206. if (colonp != NULL) {
  207. /*
  208. * Since some memmove()'s erroneously fail to handle
  209. * overlapping regions, we'll do the shift by hand.
  210. */
  211. const ptrdiff_t n = tp - colonp;
  212. int i;
  213. if (tp == endp)
  214. return (0);
  215. for (i = 1; i <= n; i++) {
  216. endp[- i] = colonp[n - i];
  217. colonp[n - i] = 0;
  218. }
  219. tp = endp;
  220. }
  221. if (tp != endp)
  222. return (0);
  223. memcpy(dst, tmp, NS_IN6ADDRSZ);
  224. return (1);
  225. }