ntop.c 9.3 KB

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