ntop.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. #define __FORCE_GLIBC
  18. #include <features.h>
  19. #include <sys/param.h>
  20. #include <sys/types.h>
  21. #include <sys/socket.h>
  22. #include <netinet/in.h>
  23. #include <arpa/inet.h>
  24. #include <arpa/nameser.h>
  25. #include <errno.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <ctype.h>
  29. /*
  30. * WARNING: Don't even consider trying to compile this on a system where
  31. * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
  32. */
  33. /* const char *
  34. * inet_ntop4(src, dst, size)
  35. * format an IPv4 address
  36. * return:
  37. * `dst' (as a const)
  38. * notes:
  39. * (1) uses no statics
  40. * (2) takes a u_char* not an in_addr as input
  41. * author:
  42. * Paul Vixie, 1996.
  43. */
  44. static const char *
  45. inet_ntop4(const u_char *src, char *dst, size_t size)
  46. {
  47. char tmp[sizeof ("255.255.255.255") + 1] = "\0";
  48. int octet;
  49. int i;
  50. i = 0;
  51. for (octet = 0; octet <= 3; octet++) {
  52. if (src[octet] > 255) {
  53. __set_errno (ENOSPC);
  54. return (NULL);
  55. }
  56. tmp[i++] = '0' + src[octet] / 100;
  57. if (tmp[i - 1] == '0') {
  58. tmp[i - 1] = '0' + (src[octet] / 10 % 10);
  59. if (tmp[i - 1] == '0') i--;
  60. } else {
  61. tmp[i++] = '0' + (src[octet] / 10 % 10);
  62. }
  63. tmp[i++] = '0' + src[octet] % 10;
  64. tmp[i++] = '.';
  65. }
  66. tmp[i - 1] = '\0';
  67. if (strlen (tmp) > size) {
  68. __set_errno (ENOSPC);
  69. return (NULL);
  70. }
  71. return strcpy(dst, tmp);
  72. }
  73. /* const char *
  74. * inet_ntop6(src, dst, size)
  75. * convert IPv6 binary address into presentation (printable) format
  76. * author:
  77. * Paul Vixie, 1996.
  78. */
  79. #ifdef __UCLIBC_HAS_IPV6__
  80. static const char *
  81. inet_ntop6(const u_char *src, char *dst, size_t size)
  82. {
  83. /*
  84. * Note that int32_t and int16_t need only be "at least" large enough
  85. * to contain a value of the specified size. On some systems, like
  86. * Crays, there is no such thing as an integer variable with 16 bits.
  87. * Keep this in mind if you think this function should have been coded
  88. * to use pointer overlays. All the world's not a VAX.
  89. */
  90. char tmp[sizeof ("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")], *tp;
  91. struct { int base, len; } best, cur;
  92. u_int words[8];
  93. int i;
  94. /*
  95. * Preprocess:
  96. * Copy the input (bytewise) array into a wordwise array.
  97. * Find the longest run of 0x00's in src[] for :: shorthanding.
  98. */
  99. memset(words, '\0', sizeof words);
  100. for (i = 0; i < 16; i += 2)
  101. words[i / 2] = (src[i] << 8) | src[i + 1];
  102. best.base = -1;
  103. cur.base = -1;
  104. for (i = 0; i < 8; i++) {
  105. if (words[i] == 0) {
  106. if (cur.base == -1)
  107. cur.base = i, cur.len = 1;
  108. else
  109. cur.len++;
  110. } else {
  111. if (cur.base != -1) {
  112. if (best.base == -1 || cur.len > best.len)
  113. best = cur;
  114. cur.base = -1;
  115. }
  116. }
  117. }
  118. if (cur.base != -1) {
  119. if (best.base == -1 || cur.len > best.len)
  120. best = cur;
  121. }
  122. if (best.base != -1 && best.len < 2)
  123. best.base = -1;
  124. /*
  125. * Format the result.
  126. */
  127. tp = tmp;
  128. for (i = 0; i < 8; i++) {
  129. /* Are we inside the best run of 0x00's? */
  130. if (best.base != -1 && i >= best.base &&
  131. i < (best.base + best.len)) {
  132. if (i == best.base)
  133. *tp++ = ':';
  134. continue;
  135. }
  136. /* Are we following an initial run of 0x00s or any real hex? */
  137. if (i != 0)
  138. *tp++ = ':';
  139. /* Is this address an encapsulated IPv4? */
  140. if (i == 6 && best.base == 0 &&
  141. (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
  142. if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
  143. return (NULL);
  144. tp += strlen(tp);
  145. break;
  146. }
  147. tp += sprintf(tp, "%x", words[i]);
  148. }
  149. /* Was it a trailing run of 0x00's? */
  150. if (best.base != -1 && (best.base + best.len) == 8)
  151. *tp++ = ':';
  152. *tp++ = '\0';
  153. /*
  154. * Check for overflow, copy, and we're done.
  155. */
  156. if ((size_t)(tp - tmp) > size) {
  157. __set_errno (ENOSPC);
  158. return (NULL);
  159. }
  160. return strcpy(dst, tmp);
  161. }
  162. #endif /* __UCLIBC_HAS_IPV6__ */
  163. /* int
  164. * inet_pton4(src, dst)
  165. * like inet_aton() but without all the hexadecimal and shorthand.
  166. * return:
  167. * 1 if `src' is a valid dotted quad, else 0.
  168. * notice:
  169. * does not touch `dst' unless it's returning 1.
  170. * author:
  171. * Paul Vixie, 1996.
  172. */
  173. static int
  174. inet_pton4(const char *src, u_char *dst)
  175. {
  176. int saw_digit, octets, ch;
  177. u_char tmp[4], *tp;
  178. saw_digit = 0;
  179. octets = 0;
  180. *(tp = tmp) = 0;
  181. while ((ch = *src++) != '\0') {
  182. if (ch >= '0' && ch <= '9') {
  183. u_int new = *tp * 10 + (ch - '0');
  184. if (new > 255)
  185. return (0);
  186. *tp = new;
  187. if (! saw_digit) {
  188. if (++octets > 4)
  189. return (0);
  190. saw_digit = 1;
  191. }
  192. } else if (ch == '.' && saw_digit) {
  193. if (octets == 4)
  194. return (0);
  195. *++tp = 0;
  196. saw_digit = 0;
  197. } else
  198. return (0);
  199. }
  200. if (octets < 4)
  201. return (0);
  202. memcpy(dst, tmp, 4);
  203. return (1);
  204. }
  205. /* int
  206. * inet_pton6(src, dst)
  207. * convert presentation level address to network order binary form.
  208. * return:
  209. * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
  210. * notice:
  211. * (1) does not touch `dst' unless it's returning 1.
  212. * (2) :: in a full address is silently ignored.
  213. * credit:
  214. * inspired by Mark Andrews.
  215. * author:
  216. * Paul Vixie, 1996.
  217. */
  218. #ifdef __UCLIBC_HAS_IPV6__
  219. /* We cannot use the macro version of tolower() or very bad
  220. * things happen when '*src++' gets evaluated multiple times.
  221. * So * undef it here so we get the function version of tolower
  222. * instead.
  223. */
  224. #undef tolower
  225. static int
  226. inet_pton6(const char *src, u_char *dst)
  227. {
  228. static const char xdigits[] = "0123456789abcdef";
  229. u_char tmp[16], *tp, *endp, *colonp;
  230. const char *curtok;
  231. int ch, saw_xdigit;
  232. u_int val;
  233. tp = memset(tmp, '\0', 16);
  234. endp = tp + 16;
  235. colonp = NULL;
  236. /* Leading :: requires some special handling. */
  237. if (*src == ':')
  238. if (*++src != ':')
  239. return (0);
  240. curtok = src;
  241. saw_xdigit = 0;
  242. val = 0;
  243. while ((ch = tolower (*src++)) != '\0') {
  244. const char *pch;
  245. pch = strchr(xdigits, ch);
  246. if (pch != NULL) {
  247. val <<= 4;
  248. val |= (pch - xdigits);
  249. if (val > 0xffff)
  250. return (0);
  251. saw_xdigit = 1;
  252. continue;
  253. }
  254. if (ch == ':') {
  255. curtok = src;
  256. if (!saw_xdigit) {
  257. if (colonp)
  258. return (0);
  259. colonp = tp;
  260. continue;
  261. } else if (*src == '\0') {
  262. return (0);
  263. }
  264. if (tp + 2 > endp)
  265. return (0);
  266. *tp++ = (u_char) (val >> 8) & 0xff;
  267. *tp++ = (u_char) val & 0xff;
  268. saw_xdigit = 0;
  269. val = 0;
  270. continue;
  271. }
  272. if (ch == '.' && ((tp + 4) <= endp) &&
  273. inet_pton4(curtok, tp) > 0) {
  274. tp += 4;
  275. saw_xdigit = 0;
  276. break; /* '\0' was seen by inet_pton4(). */
  277. }
  278. return (0);
  279. }
  280. if (saw_xdigit) {
  281. if (tp + 2 > endp)
  282. return (0);
  283. *tp++ = (u_char) (val >> 8) & 0xff;
  284. *tp++ = (u_char) val & 0xff;
  285. }
  286. if (colonp != NULL) {
  287. /*
  288. * Since some memmove()'s erroneously fail to handle
  289. * overlapping regions, we'll do the shift by hand.
  290. */
  291. const int n = tp - colonp;
  292. int i;
  293. if (tp == endp)
  294. return (0);
  295. for (i = 1; i <= n; i++) {
  296. endp[- i] = colonp[n - i];
  297. colonp[n - i] = 0;
  298. }
  299. tp = endp;
  300. }
  301. if (tp != endp)
  302. return (0);
  303. memcpy(dst, tmp, 16);
  304. return (1);
  305. }
  306. #endif /* __UCLIBC_HAS_IPV6__ */
  307. /* char *
  308. * inet_ntop(af, src, dst, size)
  309. * convert a network format address to presentation format.
  310. * return:
  311. * pointer to presentation format address (`dst'), or NULL (see errno).
  312. * author:
  313. * Paul Vixie, 1996.
  314. */
  315. extern const char *
  316. inet_ntop(af, src, dst, size)
  317. int af;
  318. const void *src;
  319. char *dst;
  320. socklen_t size;
  321. {
  322. switch (af) {
  323. case AF_INET:
  324. return (inet_ntop4(src, dst, size));
  325. #ifdef __UCLIBC_HAS_IPV6__
  326. case AF_INET6:
  327. return (inet_ntop6(src, dst, size));
  328. #endif
  329. default:
  330. __set_errno (EAFNOSUPPORT);
  331. return (NULL);
  332. }
  333. /* NOTREACHED */
  334. }
  335. /* int
  336. * inet_pton(af, src, dst)
  337. * convert from presentation format (which usually means ASCII printable)
  338. * to network format (which is usually some kind of binary format).
  339. * return:
  340. * 1 if the address was valid for the specified address family
  341. * 0 if the address wasn't valid (`dst' is untouched in this case)
  342. * -1 if some other error occurred (`dst' is untouched in this case, too)
  343. * author:
  344. * Paul Vixie, 1996.
  345. */
  346. extern int
  347. inet_pton(af, src, dst)
  348. int af;
  349. const char *src;
  350. void *dst;
  351. {
  352. switch (af) {
  353. case AF_INET:
  354. return (inet_pton4(src, dst));
  355. #ifdef __UCLIBC_HAS_IPV6__
  356. case AF_INET6:
  357. return (inet_pton6(src, dst));
  358. #endif
  359. default:
  360. __set_errno (EAFNOSUPPORT);
  361. return (-1);
  362. }
  363. /* NOTREACHED */
  364. }