inet_pton.c 4.9 KB

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