ntop.c 8.9 KB

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