ntop.c 8.6 KB

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