ntop.c 8.7 KB

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