ntop.c 8.6 KB

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