inet_net_pton.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
  4. * Copyright (c) 1996,1999 by Internet Software Consortium.
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  16. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include "setup.h"
  19. #ifdef HAVE_SYS_SOCKET_H
  20. # include <sys/socket.h>
  21. #endif
  22. #ifdef HAVE_NETINET_IN_H
  23. # include <netinet/in.h>
  24. #endif
  25. #ifdef HAVE_ARPA_INET_H
  26. # include <arpa/inet.h>
  27. #endif
  28. #ifdef HAVE_ARPA_NAMESER_H
  29. # include <arpa/nameser.h>
  30. #else
  31. # include "nameser.h"
  32. #endif
  33. #ifdef HAVE_ARPA_NAMESER_COMPAT_H
  34. # include <arpa/nameser_compat.h>
  35. #endif
  36. #include <ctype.h>
  37. #include <errno.h>
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include <stdlib.h>
  41. #include "ares_ipv6.h"
  42. #include "inet_net_pton.h"
  43. #if !defined(HAVE_INET_NET_PTON) || !defined(HAVE_INET_NET_PTON_IPV6)
  44. /*
  45. * static int
  46. * inet_net_pton_ipv4(src, dst, size)
  47. * convert IPv4 network number from presentation to network format.
  48. * accepts hex octets, hex strings, decimal octets, and /CIDR.
  49. * "size" is in bytes and describes "dst".
  50. * return:
  51. * number of bits, either imputed classfully or specified with /CIDR,
  52. * or -1 if some failure occurred (check errno). ENOENT means it was
  53. * not an IPv4 network specification.
  54. * note:
  55. * network byte order assumed. this means 192.5.5.240/28 has
  56. * 0b11110000 in its fourth octet.
  57. * note:
  58. * On Windows we store the error in the thread errno, not
  59. * in the winsock error code. This is to avoid loosing the
  60. * actual last winsock error. So use macro ERRNO to fetch the
  61. * errno this funtion sets when returning (-1), not SOCKERRNO.
  62. * author:
  63. * Paul Vixie (ISC), June 1996
  64. */
  65. static int
  66. inet_net_pton_ipv4(const char *src, unsigned char *dst, size_t size)
  67. {
  68. static const char xdigits[] = "0123456789abcdef";
  69. static const char digits[] = "0123456789";
  70. int n, ch, tmp = 0, dirty, bits;
  71. const unsigned char *odst = dst;
  72. ch = *src++;
  73. if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
  74. && ISXDIGIT(src[1])) {
  75. /* Hexadecimal: Eat nybble string. */
  76. if (size <= 0U)
  77. goto emsgsize;
  78. dirty = 0;
  79. src++; /* skip x or X. */
  80. while ((ch = *src++) != '\0' && ISXDIGIT(ch)) {
  81. if (ISUPPER(ch))
  82. ch = tolower(ch);
  83. n = (int)(strchr(xdigits, ch) - xdigits);
  84. if (dirty == 0)
  85. tmp = n;
  86. else
  87. tmp = (tmp << 4) | n;
  88. if (++dirty == 2) {
  89. if (size-- <= 0U)
  90. goto emsgsize;
  91. *dst++ = (unsigned char) tmp;
  92. dirty = 0;
  93. }
  94. }
  95. if (dirty) { /* Odd trailing nybble? */
  96. if (size-- <= 0U)
  97. goto emsgsize;
  98. *dst++ = (unsigned char) (tmp << 4);
  99. }
  100. } else if (ISDIGIT(ch)) {
  101. /* Decimal: eat dotted digit string. */
  102. for (;;) {
  103. tmp = 0;
  104. do {
  105. n = (int)(strchr(digits, ch) - digits);
  106. tmp *= 10;
  107. tmp += n;
  108. if (tmp > 255)
  109. goto enoent;
  110. } while ((ch = *src++) != '\0' &&
  111. ISDIGIT(ch));
  112. if (size-- <= 0U)
  113. goto emsgsize;
  114. *dst++ = (unsigned char) tmp;
  115. if (ch == '\0' || ch == '/')
  116. break;
  117. if (ch != '.')
  118. goto enoent;
  119. ch = *src++;
  120. if (!ISDIGIT(ch))
  121. goto enoent;
  122. }
  123. } else
  124. goto enoent;
  125. bits = -1;
  126. if (ch == '/' &&
  127. ISDIGIT(src[0]) && dst > odst) {
  128. /* CIDR width specifier. Nothing can follow it. */
  129. ch = *src++; /* Skip over the /. */
  130. bits = 0;
  131. do {
  132. n = (int)(strchr(digits, ch) - digits);
  133. bits *= 10;
  134. bits += n;
  135. } while ((ch = *src++) != '\0' && ISDIGIT(ch));
  136. if (ch != '\0')
  137. goto enoent;
  138. if (bits > 32)
  139. goto emsgsize;
  140. }
  141. /* Firey death and destruction unless we prefetched EOS. */
  142. if (ch != '\0')
  143. goto enoent;
  144. /* If nothing was written to the destination, we found no address. */
  145. if (dst == odst)
  146. goto enoent;
  147. /* If no CIDR spec was given, infer width from net class. */
  148. if (bits == -1) {
  149. if (*odst >= 240) /* Class E */
  150. bits = 32;
  151. else if (*odst >= 224) /* Class D */
  152. bits = 8;
  153. else if (*odst >= 192) /* Class C */
  154. bits = 24;
  155. else if (*odst >= 128) /* Class B */
  156. bits = 16;
  157. else /* Class A */
  158. bits = 8;
  159. /* If imputed mask is narrower than specified octets, widen. */
  160. if (bits < ((dst - odst) * 8))
  161. bits = (int)(dst - odst) * 8;
  162. /*
  163. * If there are no additional bits specified for a class D
  164. * address adjust bits to 4.
  165. */
  166. if (bits == 8 && *odst == 224)
  167. bits = 4;
  168. }
  169. /* Extend network to cover the actual mask. */
  170. while (bits > ((dst - odst) * 8)) {
  171. if (size-- <= 0U)
  172. goto emsgsize;
  173. *dst++ = '\0';
  174. }
  175. return (bits);
  176. enoent:
  177. SET_ERRNO(ENOENT);
  178. return (-1);
  179. emsgsize:
  180. SET_ERRNO(EMSGSIZE);
  181. return (-1);
  182. }
  183. static int
  184. getbits(const char *src, int *bitsp)
  185. {
  186. static const char digits[] = "0123456789";
  187. int n;
  188. int val;
  189. char ch;
  190. val = 0;
  191. n = 0;
  192. while ((ch = *src++) != '\0') {
  193. const char *pch;
  194. pch = strchr(digits, ch);
  195. if (pch != NULL) {
  196. if (n++ != 0 && val == 0) /* no leading zeros */
  197. return (0);
  198. val *= 10;
  199. val += (pch - digits);
  200. if (val > 128) /* range */
  201. return (0);
  202. continue;
  203. }
  204. return (0);
  205. }
  206. if (n == 0)
  207. return (0);
  208. *bitsp = val;
  209. return (1);
  210. }
  211. static int
  212. getv4(const char *src, unsigned char *dst, int *bitsp)
  213. {
  214. static const char digits[] = "0123456789";
  215. unsigned char *odst = dst;
  216. int n;
  217. unsigned int val;
  218. char ch;
  219. val = 0;
  220. n = 0;
  221. while ((ch = *src++) != '\0') {
  222. const char *pch;
  223. pch = strchr(digits, ch);
  224. if (pch != NULL) {
  225. if (n++ != 0 && val == 0) /* no leading zeros */
  226. return (0);
  227. val *= 10;
  228. val += (pch - digits);
  229. if (val > 255) /* range */
  230. return (0);
  231. continue;
  232. }
  233. if (ch == '.' || ch == '/') {
  234. if (dst - odst > 3) /* too many octets? */
  235. return (0);
  236. *dst++ = (unsigned char)val;
  237. if (ch == '/')
  238. return (getbits(src, bitsp));
  239. val = 0;
  240. n = 0;
  241. continue;
  242. }
  243. return (0);
  244. }
  245. if (n == 0)
  246. return (0);
  247. if (dst - odst > 3) /* too many octets? */
  248. return (0);
  249. *dst++ = (unsigned char)val;
  250. return (1);
  251. }
  252. static int
  253. inet_net_pton_ipv6(const char *src, unsigned char *dst, size_t size)
  254. {
  255. static const char xdigits_l[] = "0123456789abcdef",
  256. xdigits_u[] = "0123456789ABCDEF";
  257. unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
  258. const char *xdigits, *curtok;
  259. int ch, saw_xdigit;
  260. unsigned int val;
  261. int digits;
  262. int bits;
  263. size_t bytes;
  264. int words;
  265. int ipv4;
  266. memset((tp = tmp), '\0', NS_IN6ADDRSZ);
  267. endp = tp + NS_IN6ADDRSZ;
  268. colonp = NULL;
  269. /* Leading :: requires some special handling. */
  270. if (*src == ':')
  271. if (*++src != ':')
  272. goto enoent;
  273. curtok = src;
  274. saw_xdigit = 0;
  275. val = 0;
  276. digits = 0;
  277. bits = -1;
  278. ipv4 = 0;
  279. while ((ch = *src++) != '\0') {
  280. const char *pch;
  281. if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
  282. pch = strchr((xdigits = xdigits_u), ch);
  283. if (pch != NULL) {
  284. val <<= 4;
  285. val |= (pch - xdigits);
  286. if (++digits > 4)
  287. goto enoent;
  288. saw_xdigit = 1;
  289. continue;
  290. }
  291. if (ch == ':') {
  292. curtok = src;
  293. if (!saw_xdigit) {
  294. if (colonp)
  295. goto enoent;
  296. colonp = tp;
  297. continue;
  298. } else if (*src == '\0')
  299. goto enoent;
  300. if (tp + NS_INT16SZ > endp)
  301. return (0);
  302. *tp++ = (unsigned char)((val >> 8) & 0xff);
  303. *tp++ = (unsigned char)(val & 0xff);
  304. saw_xdigit = 0;
  305. digits = 0;
  306. val = 0;
  307. continue;
  308. }
  309. if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
  310. getv4(curtok, tp, &bits) > 0) {
  311. tp += NS_INADDRSZ;
  312. saw_xdigit = 0;
  313. ipv4 = 1;
  314. break; /* '\0' was seen by inet_pton4(). */
  315. }
  316. if (ch == '/' && getbits(src, &bits) > 0)
  317. break;
  318. goto enoent;
  319. }
  320. if (saw_xdigit) {
  321. if (tp + NS_INT16SZ > endp)
  322. goto enoent;
  323. *tp++ = (unsigned char)((val >> 8) & 0xff);
  324. *tp++ = (unsigned char)(val & 0xff);
  325. }
  326. if (bits == -1)
  327. bits = 128;
  328. words = (bits + 15) / 16;
  329. if (words < 2)
  330. words = 2;
  331. if (ipv4)
  332. words = 8;
  333. endp = tmp + 2 * words;
  334. if (colonp != NULL) {
  335. /*
  336. * Since some memmove()'s erroneously fail to handle
  337. * overlapping regions, we'll do the shift by hand.
  338. */
  339. const int n = (int)(tp - colonp);
  340. int i;
  341. if (tp == endp)
  342. goto enoent;
  343. for (i = 1; i <= n; i++) {
  344. endp[- i] = colonp[n - i];
  345. colonp[n - i] = 0;
  346. }
  347. tp = endp;
  348. }
  349. if (tp != endp)
  350. goto enoent;
  351. bytes = (bits + 7) / 8;
  352. if (bytes > size)
  353. goto emsgsize;
  354. memcpy(dst, tmp, bytes);
  355. return (bits);
  356. enoent:
  357. SET_ERRNO(ENOENT);
  358. return (-1);
  359. emsgsize:
  360. SET_ERRNO(EMSGSIZE);
  361. return (-1);
  362. }
  363. /*
  364. * int
  365. * inet_net_pton(af, src, dst, size)
  366. * convert network number from presentation to network format.
  367. * accepts hex octets, hex strings, decimal octets, and /CIDR.
  368. * "size" is in bytes and describes "dst".
  369. * return:
  370. * number of bits, either imputed classfully or specified with /CIDR,
  371. * or -1 if some failure occurred (check errno). ENOENT means it was
  372. * not a valid network specification.
  373. * note:
  374. * On Windows we store the error in the thread errno, not
  375. * in the winsock error code. This is to avoid loosing the
  376. * actual last winsock error. So use macro ERRNO to fetch the
  377. * errno this funtion sets when returning (-1), not SOCKERRNO.
  378. * author:
  379. * Paul Vixie (ISC), June 1996
  380. */
  381. int
  382. ares_inet_net_pton(int af, const char *src, void *dst, size_t size)
  383. {
  384. switch (af) {
  385. case AF_INET:
  386. return (inet_net_pton_ipv4(src, dst, size));
  387. case AF_INET6:
  388. return (inet_net_pton_ipv6(src, dst, size));
  389. default:
  390. SET_ERRNO(EAFNOSUPPORT);
  391. return (-1);
  392. }
  393. }
  394. #endif
  395. #ifndef HAVE_INET_PTON
  396. int ares_inet_pton(int af, const char *src, void *dst)
  397. {
  398. int size, result;
  399. if (af == AF_INET)
  400. size = sizeof(struct in_addr);
  401. else if (af == AF_INET6)
  402. size = sizeof(struct in6_addr);
  403. else
  404. {
  405. SET_ERRNO(EAFNOSUPPORT);
  406. return -1;
  407. }
  408. result = ares_inet_net_pton(af, src, dst, size);
  409. if (result == -1 && ERRNO == ENOENT)
  410. return 0;
  411. return (result > -1 ? 1 : -1);
  412. }
  413. #endif