inet_net_pton.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 <sys/types.h>
  18. #include <sys/socket.h>
  19. #include <netinet/in.h>
  20. #include <arpa/inet.h>
  21. #include <assert.h>
  22. #include <ctype.h>
  23. #include <errno.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #ifdef SPRINTF_CHAR
  28. # define SPRINTF(x) strlen(sprintf/**/x)
  29. #else
  30. # define SPRINTF(x) ((size_t)sprintf x)
  31. #endif
  32. static int inet_net_pton_ipv4 (const char *src, u_char *dst,
  33. size_t size) __THROW;
  34. /*
  35. * static int
  36. * inet_net_pton(af, src, dst, size)
  37. * convert network number from presentation to network format.
  38. * accepts hex octets, hex strings, decimal octets, and /CIDR.
  39. * "size" is in bytes and describes "dst".
  40. * return:
  41. * number of bits, either imputed classfully or specified with /CIDR,
  42. * or -1 if some failure occurred (check errno). ENOENT means it was
  43. * not a valid network specification.
  44. * author:
  45. * Paul Vixie (ISC), June 1996
  46. */
  47. int
  48. inet_net_pton (int af, const char *src, void *dst, size_t size)
  49. {
  50. switch (af) {
  51. case AF_INET:
  52. return (inet_net_pton_ipv4(src, dst, size));
  53. default:
  54. __set_errno (EAFNOSUPPORT);
  55. return (-1);
  56. }
  57. }
  58. /*
  59. * static int
  60. * inet_net_pton_ipv4(src, dst, size)
  61. * convert IPv4 network number from presentation to network format.
  62. * accepts hex octets, hex strings, decimal octets, and /CIDR.
  63. * "size" is in bytes and describes "dst".
  64. * return:
  65. * number of bits, either imputed classfully or specified with /CIDR,
  66. * or -1 if some failure occurred (check errno). ENOENT means it was
  67. * not an IPv4 network specification.
  68. * note:
  69. * network byte order assumed. this means 192.5.5.240/28 has
  70. * 0b11110000 in its fourth octet.
  71. * author:
  72. * Paul Vixie (ISC), June 1996
  73. */
  74. static int
  75. inet_net_pton_ipv4 (const char *src, u_char *dst, size_t size)
  76. {
  77. static const char xdigits[] = "0123456789abcdef";
  78. int n, ch, tmp, dirty, bits;
  79. const u_char *odst = dst;
  80. ch = *src++;
  81. if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
  82. && isascii(src[1]) && isxdigit(src[1])) {
  83. /* Hexadecimal: Eat nybble string. */
  84. if (size <= 0)
  85. goto emsgsize;
  86. dirty = 0;
  87. tmp = 0; /* To calm down gcc. */
  88. src++; /* skip x or X. */
  89. while (isxdigit((ch = *src++))) {
  90. ch = _tolower(ch);
  91. n = (const char *) __rawmemchr(xdigits, ch) - xdigits;
  92. assert(n >= 0 && n <= 15);
  93. if (dirty == 0)
  94. tmp = n;
  95. else
  96. tmp = (tmp << 4) | n;
  97. if (++dirty == 2) {
  98. if (size-- <= 0)
  99. goto emsgsize;
  100. *dst++ = (u_char) tmp;
  101. dirty = 0;
  102. }
  103. }
  104. if (dirty) { /* Odd trailing nybble? */
  105. if (size-- <= 0)
  106. goto emsgsize;
  107. *dst++ = (u_char) (tmp << 4);
  108. }
  109. } else if (isascii(ch) && isdigit(ch)) {
  110. /* Decimal: eat dotted digit string. */
  111. for (;;) {
  112. tmp = 0;
  113. do {
  114. n = ((const char *) __rawmemchr(xdigits, ch)
  115. - xdigits);
  116. assert(n >= 0 && n <= 9);
  117. tmp *= 10;
  118. tmp += n;
  119. if (tmp > 255)
  120. goto enoent;
  121. } while (isascii((ch = *src++)) && isdigit(ch));
  122. if (size-- <= 0)
  123. goto emsgsize;
  124. *dst++ = (u_char) tmp;
  125. if (ch == '\0' || ch == '/')
  126. break;
  127. if (ch != '.')
  128. goto enoent;
  129. ch = *src++;
  130. if (!isascii(ch) || !isdigit(ch))
  131. goto enoent;
  132. }
  133. } else
  134. goto enoent;
  135. bits = -1;
  136. if (ch == '/' && isascii(src[0]) && isdigit(src[0]) && dst > odst) {
  137. /* CIDR width specifier. Nothing can follow it. */
  138. ch = *src++; /* Skip over the /. */
  139. bits = 0;
  140. do {
  141. n = (const char *) __rawmemchr(xdigits, ch) - xdigits;
  142. assert(n >= 0 && n <= 9);
  143. bits *= 10;
  144. bits += n;
  145. } while (isascii((ch = *src++)) && isdigit(ch));
  146. if (ch != '\0')
  147. goto enoent;
  148. if (bits > 32)
  149. goto emsgsize;
  150. }
  151. /* Firey death and destruction unless we prefetched EOS. */
  152. if (ch != '\0')
  153. goto enoent;
  154. /* If nothing was written to the destination, we found no address. */
  155. if (dst == odst)
  156. goto enoent;
  157. /* If no CIDR spec was given, infer width from net class. */
  158. if (bits == -1) {
  159. if (*odst >= 240) /* Class E */
  160. bits = 32;
  161. else if (*odst >= 224) /* Class D */
  162. bits = 4;
  163. else if (*odst >= 192) /* Class C */
  164. bits = 24;
  165. else if (*odst >= 128) /* Class B */
  166. bits = 16;
  167. else /* Class A */
  168. bits = 8;
  169. /* If imputed mask is narrower than specified octets, widen. */
  170. if (bits >= 8 && bits < ((dst - odst) * 8))
  171. bits = (dst - odst) * 8;
  172. }
  173. /* Extend network to cover the actual mask. */
  174. while (bits > ((dst - odst) * 8)) {
  175. if (size-- <= 0)
  176. goto emsgsize;
  177. *dst++ = '\0';
  178. }
  179. return (bits);
  180. enoent:
  181. __set_errno (ENOENT);
  182. return (-1);
  183. emsgsize:
  184. __set_errno (EMSGSIZE);
  185. return (-1);
  186. }