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 <arpa/inet.h>
  18. #include <netinet/in.h>
  19. #include "api.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, size_t len, void* dstp) {
  40. unsigned char* dst = (unsigned char*)dstp;
  41. const char* end = src + len;
  42. int saw_digit, octets, ch;
  43. unsigned char tmp[NS_INADDRSZ], *tp;
  44. saw_digit = 0;
  45. octets = 0;
  46. *(tp = tmp) = 0;
  47. while (src < end && (ch = *src++) != '\0') {
  48. if (ch >= '0' && ch <= '9') {
  49. uint32_t new = *tp * 10 + (ch - '0');
  50. if (saw_digit && *tp == 0)
  51. return 0;
  52. if (new > 255)
  53. return 0;
  54. *tp = new;
  55. if (!saw_digit) {
  56. if (++octets > 4)
  57. return 0;
  58. saw_digit = 1;
  59. }
  60. } else if (ch == '.' && saw_digit) {
  61. if (octets == 4)
  62. return 0;
  63. *++tp = 0;
  64. saw_digit = 0;
  65. } else {
  66. return 0;
  67. }
  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. return c >= 'A' && c < 'Z' ? c + ('a' - 'A') : c;
  76. }
  77. /* int inet_pton6(src, dst)
  78. * convert presentation level address to network order binary form.
  79. * return:
  80. * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
  81. * notice:
  82. * (1) does not touch `dst' unless it's returning 1.
  83. * (2) :: in a full address is silently ignored.
  84. * credit:
  85. * inspired by Mark Andrews.
  86. * author:
  87. * Paul Vixie, 1996.
  88. */
  89. int inet_pton6(const char* src, size_t len, void* dstp) {
  90. unsigned char* dst = (unsigned char*)dstp;
  91. const char* end = src + len;
  92. static const char xdigits[] = "0123456789abcdef";
  93. unsigned char tmp[NS_IN6ADDRSZ];
  94. unsigned char* tp;
  95. unsigned char* endp;
  96. unsigned char* colonp;
  97. const char* curtok;
  98. int ch, saw_xdigit;
  99. unsigned int val;
  100. tp = memset(tmp, '\0', NS_IN6ADDRSZ);
  101. endp = tp + NS_IN6ADDRSZ;
  102. colonp = NULL;
  103. /* Leading :: requires some special handling. */
  104. if (*src == ':')
  105. if (*++src != ':')
  106. return 0;
  107. curtok = src;
  108. saw_xdigit = 0;
  109. val = 0;
  110. while (src < end && (ch = tolower(*src++)) != '\0') {
  111. const char* pch;
  112. pch = strchr(xdigits, ch);
  113. if (pch != NULL) {
  114. val <<= 4;
  115. val |= (pch - xdigits);
  116. if (val > 0xffff)
  117. return 0;
  118. saw_xdigit = 1;
  119. continue;
  120. }
  121. if (ch == ':') {
  122. curtok = src;
  123. if (!saw_xdigit) {
  124. if (colonp)
  125. return 0;
  126. colonp = tp;
  127. continue;
  128. } else if (*src == '\0') {
  129. return 0;
  130. }
  131. if (tp + NS_INT16SZ > endp)
  132. return 0;
  133. *tp++ = (unsigned char)(val >> 8) & 0xff;
  134. *tp++ = (unsigned char)val & 0xff;
  135. saw_xdigit = 0;
  136. val = 0;
  137. continue;
  138. }
  139. if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) && 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. }