inet_pton.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #ifndef NS_INADDRSZ
  21. # define NS_INADDRSZ 4
  22. #endif
  23. #ifndef NS_IN6ADDRSZ
  24. # define NS_IN6ADDRSZ 16
  25. #endif
  26. #ifndef NS_INT16SZ
  27. # define NS_INT16SZ 2
  28. #endif
  29. /* int inet_pton4(src, dst)
  30. * like inet_aton() but without all the hexadecimal, octal (with the
  31. * exception of 0) and shorthand.
  32. * return:
  33. * 1 if `src' is a valid dotted quad, else 0.
  34. * notice:
  35. * does not touch `dst' unless it's returning 1.
  36. * author:
  37. * Paul Vixie, 1996.
  38. */
  39. int inet_pton4 (const char *src, int len, void *dstp)
  40. {
  41. unsigned char *dst = (unsigned char *) dstp;
  42. const char *end = src + len;
  43. int saw_digit, octets, ch;
  44. unsigned char tmp[NS_INADDRSZ], *tp;
  45. saw_digit = 0;
  46. octets = 0;
  47. *(tp = tmp) = 0;
  48. while (src < end && (ch = *src++) != '\0') {
  49. if (ch >= '0' && ch <= '9') {
  50. uint32_t new = *tp * 10 + (ch - '0');
  51. if (saw_digit && *tp == 0)
  52. return (0);
  53. if (new > 255)
  54. return (0);
  55. *tp = new;
  56. if (! saw_digit) {
  57. if (++octets > 4)
  58. return (0);
  59. saw_digit = 1;
  60. }
  61. } else if (ch == '.' && saw_digit) {
  62. if (octets == 4)
  63. return (0);
  64. *++tp = 0;
  65. saw_digit = 0;
  66. } else
  67. return (0);
  68. }
  69. if (octets < 4)
  70. return (0);
  71. memcpy (dst, tmp, NS_INADDRSZ);
  72. return (1);
  73. }
  74. static int tolower (char c)
  75. {
  76. return c >= 'A' && c < 'Z' ? c + ('a' - 'A') : c;
  77. }
  78. /* int inet_pton6(src, dst)
  79. * convert presentation level address to network order binary form.
  80. * return:
  81. * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
  82. * notice:
  83. * (1) does not touch `dst' unless it's returning 1.
  84. * (2) :: in a full address is silently ignored.
  85. * credit:
  86. * inspired by Mark Andrews.
  87. * author:
  88. * Paul Vixie, 1996.
  89. */
  90. int inet_pton6 (const char *src, int len, void *dstp)
  91. {
  92. unsigned char *dst = (unsigned char *) dstp;
  93. const char *end = src + len;
  94. static const char xdigits[] = "0123456789abcdef";
  95. unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
  96. const char *curtok;
  97. int ch, saw_xdigit;
  98. unsigned int val;
  99. tp = memset (tmp, '\0', NS_IN6ADDRSZ);
  100. endp = tp + NS_IN6ADDRSZ;
  101. colonp = NULL;
  102. /* Leading :: requires some special handling. */
  103. if (*src == ':')
  104. if (*++src != ':')
  105. return (0);
  106. curtok = src;
  107. saw_xdigit = 0;
  108. val = 0;
  109. while (src < end && (ch = tolower (*src++)) != '\0') {
  110. const char *pch;
  111. pch = strchr (xdigits, ch);
  112. if (pch != NULL) {
  113. val <<= 4;
  114. val |= (pch - xdigits);
  115. if (val > 0xffff)
  116. return (0);
  117. saw_xdigit = 1;
  118. continue;
  119. }
  120. if (ch == ':') {
  121. curtok = src;
  122. if (!saw_xdigit) {
  123. if (colonp)
  124. return (0);
  125. colonp = tp;
  126. continue;
  127. } else if (*src == '\0') {
  128. return (0);
  129. }
  130. if (tp + NS_INT16SZ > endp)
  131. return (0);
  132. *tp++ = (unsigned char) (val >> 8) & 0xff;
  133. *tp++ = (unsigned char) val & 0xff;
  134. saw_xdigit = 0;
  135. val = 0;
  136. continue;
  137. }
  138. if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
  139. inet_pton4(curtok, end - curtok, tp) > 0) {
  140. tp += NS_INADDRSZ;
  141. saw_xdigit = 0;
  142. break; /* '\0' was seen by inet_pton4(). */
  143. }
  144. return (0);
  145. }
  146. if (saw_xdigit) {
  147. if (tp + NS_INT16SZ > endp)
  148. return (0);
  149. *tp++ = (unsigned char) (val >> 8) & 0xff;
  150. *tp++ = (unsigned char) val & 0xff;
  151. }
  152. if (colonp != NULL) {
  153. /*
  154. * Since some memmove()'s erroneously fail to handle
  155. * overlapping regions, we'll do the shift by hand.
  156. */
  157. const int n = tp - colonp;
  158. int i;
  159. if (tp == endp)
  160. return (0);
  161. for (i = 1; i <= n; i++) {
  162. endp[- i] = colonp[n - i];
  163. colonp[n - i] = 0;
  164. }
  165. tp = endp;
  166. }
  167. if (tp != endp)
  168. return (0);
  169. memcpy (dst, tmp, NS_IN6ADDRSZ);
  170. return (1);
  171. }