inet_pton.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. if (octets < 4)
  69. return (0);
  70. memcpy(dst, tmp, NS_INADDRSZ);
  71. return (1);
  72. }
  73. static int tolower(char c) {
  74. return c >= 'A' && c < 'Z' ? c + ('a' - 'A') : c;
  75. }
  76. /* int inet_pton6(src, dst)
  77. * convert presentation level address to network order binary form.
  78. * return:
  79. * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
  80. * notice:
  81. * (1) does not touch `dst' unless it's returning 1.
  82. * (2) :: in a full address is silently ignored.
  83. * credit:
  84. * inspired by Mark Andrews.
  85. * author:
  86. * Paul Vixie, 1996.
  87. */
  88. int inet_pton6(const char* src, size_t len, void* dstp) {
  89. unsigned char* dst = (unsigned char*)dstp;
  90. const char* end = src + len;
  91. static const char xdigits[] = "0123456789abcdef";
  92. unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
  93. const char* curtok;
  94. int ch, saw_xdigit;
  95. unsigned int val;
  96. tp = memset(tmp, '\0', NS_IN6ADDRSZ);
  97. endp = tp + NS_IN6ADDRSZ;
  98. colonp = NULL;
  99. /* Leading :: requires some special handling. */
  100. if (*src == ':')
  101. if (*++src != ':')
  102. return (0);
  103. curtok = src;
  104. saw_xdigit = 0;
  105. val = 0;
  106. while (src < end && (ch = tolower(*src++)) != '\0') {
  107. const char* pch;
  108. pch = strchr(xdigits, ch);
  109. if (pch != NULL) {
  110. val <<= 4;
  111. val |= (pch - xdigits);
  112. if (val > 0xffff)
  113. return (0);
  114. saw_xdigit = 1;
  115. continue;
  116. }
  117. if (ch == ':') {
  118. curtok = src;
  119. if (!saw_xdigit) {
  120. if (colonp)
  121. return (0);
  122. colonp = tp;
  123. continue;
  124. } else if (*src == '\0') {
  125. return (0);
  126. }
  127. if (tp + NS_INT16SZ > endp)
  128. return (0);
  129. *tp++ = (unsigned char)(val >> 8) & 0xff;
  130. *tp++ = (unsigned char)val & 0xff;
  131. saw_xdigit = 0;
  132. val = 0;
  133. continue;
  134. }
  135. if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) && inet_pton4(curtok, end - curtok, tp) > 0) {
  136. tp += NS_INADDRSZ;
  137. saw_xdigit = 0;
  138. break; /* '\0' was seen by inet_pton4(). */
  139. }
  140. return (0);
  141. }
  142. if (saw_xdigit) {
  143. if (tp + NS_INT16SZ > endp)
  144. return (0);
  145. *tp++ = (unsigned char)(val >> 8) & 0xff;
  146. *tp++ = (unsigned char)val & 0xff;
  147. }
  148. if (colonp != NULL) {
  149. /*
  150. * Since some memmove()'s erroneously fail to handle
  151. * overlapping regions, we'll do the shift by hand.
  152. */
  153. const int n = tp - colonp;
  154. int i;
  155. if (tp == endp)
  156. return (0);
  157. for (i = 1; i <= n; i++) {
  158. endp[-i] = colonp[n - i];
  159. colonp[n - i] = 0;
  160. }
  161. tp = endp;
  162. }
  163. if (tp != endp)
  164. return (0);
  165. memcpy(dst, tmp, NS_IN6ADDRSZ);
  166. return (1);
  167. }