PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/nbase/inet_ntop.c

https://github.com/prakashgamit/nmap
C | 251 lines | 161 code | 18 blank | 72 comment | 47 complexity | 517e1a5abddaba77de6d15df3d5f5574 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-2.0, LGPL-2.1
  1. /* Copyright (c) 1996 by Internet Software Consortium.
  2. *
  3. * Permission to use, copy, modify, and distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  8. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  9. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  10. * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  12. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  13. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  14. * SOFTWARE.
  15. */
  16. /* Modified by Fyodor (fyodor@nmap.org) for inclusion in the Nmap
  17. * Security Scanner.
  18. *
  19. * $Id$
  20. */
  21. #include "nbase.h"
  22. #if HAVE_SYS_TYPES_H
  23. #include <sys/types.h>
  24. #endif
  25. #if HAVE_SYS_SOCKET_H
  26. #include <sys/socket.h>
  27. #endif
  28. #if HAVE_NETINET_IN_H
  29. #include <netinet/in.h>
  30. #endif
  31. #if HAVE_ARPA_INET_H
  32. #include <arpa/inet.h>
  33. #endif
  34. #include <string.h>
  35. #if HAVE_ERRNO_H
  36. #include <errno.h>
  37. #endif
  38. #include <stdio.h>
  39. #ifndef IN6ADDRSZ
  40. #define IN6ADDRSZ 16
  41. #endif
  42. #ifndef INT16SZ
  43. #define INT16SZ sizeof(u16)
  44. #endif
  45. #if !defined(EAFNOSUPPORT) && defined(WSAEAFNOSUPPORT)
  46. #define EAFNOSUPPORT WSAEAFNOSUPPORT
  47. #endif
  48. /*
  49. * WARNING: Don't even consider trying to compile this on a system where
  50. * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
  51. */
  52. static const char *inet_ntop4(const unsigned char *src, char *dst, size_t size);
  53. #if HAVE_IPV6
  54. static const char *inet_ntop6(const unsigned char *src, char *dst, size_t size);
  55. #endif
  56. /* char *
  57. * inet_ntop(af, src, dst, size)
  58. * convert a network format address to presentation format.
  59. * return:
  60. * pointer to presentation format address (`dst'), or NULL (see errno).
  61. * author:
  62. * Paul Vixie, 1996.
  63. */
  64. const char *
  65. inet_ntop(int af, const void *src, char *dst, size_t size)
  66. {
  67. switch (af) {
  68. case AF_INET:
  69. return (inet_ntop4((const unsigned char *) src, dst, size));
  70. #if HAVE_IPV6
  71. case AF_INET6:
  72. return (inet_ntop6((const unsigned char *) src, dst, size));
  73. #endif
  74. default:
  75. #ifndef WIN32
  76. errno = EAFNOSUPPORT;
  77. #endif
  78. return (NULL);
  79. }
  80. /* NOTREACHED */
  81. }
  82. /* const char *
  83. * inet_ntop4(src, dst, size)
  84. * format an IPv4 address, more or less like inet_ntoa()
  85. * return:
  86. * `dst' (as a const)
  87. * notes:
  88. * (1) uses no statics
  89. * (2) takes a u_char* not an in_addr as input
  90. * author:
  91. * Paul Vixie, 1996.
  92. */
  93. static const char *
  94. inet_ntop4(const unsigned char *src, char *dst, size_t size)
  95. {
  96. const size_t MIN_SIZE = 16; /* space for 255.255.255.255\0 */
  97. int n = 0;
  98. char *next = dst;
  99. if (size < MIN_SIZE) {
  100. #ifndef WIN32
  101. errno = ENOSPC;
  102. #endif
  103. return NULL;
  104. }
  105. do {
  106. unsigned char u = *src++;
  107. if (u > 99) {
  108. *next++ = '0' + u/100;
  109. u %= 100;
  110. *next++ = '0' + u/10;
  111. u %= 10;
  112. }
  113. else if (u > 9) {
  114. *next++ = '0' + u/10;
  115. u %= 10;
  116. }
  117. *next++ = '0' + u;
  118. *next++ = '.';
  119. n++;
  120. } while (n < 4);
  121. *--next = 0;
  122. return dst;
  123. }
  124. #if HAVE_IPV6
  125. /* const char *
  126. * inet_ntop6(src, dst, size)
  127. * convert IPv6 binary address into presentation (printable) format
  128. * author:
  129. * Paul Vixie, 1996.
  130. */
  131. static const char *
  132. inet_ntop6(const unsigned char *src, char *dst, size_t size)
  133. {
  134. /*
  135. * Note that int32_t and int16_t need only be "at least" large enough
  136. * to contain a value of the specified size. On some systems, like
  137. * Crays, there is no such thing as an integer variable with 16 bits.
  138. * Keep this in mind if you think this function should have been coded
  139. * to use pointer overlays. All the world's not a VAX.
  140. */
  141. char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
  142. struct { int base, len; } best, cur;
  143. u16 words[IN6ADDRSZ / INT16SZ];
  144. int i;
  145. const unsigned char *next_src, *src_end;
  146. u16 *next_dest;
  147. /*
  148. * Preprocess:
  149. * Copy the input (bytewise) array into a wordwise array.
  150. * Find the longest run of 0x00's in src[] for :: shorthanding.
  151. */
  152. next_src = src;
  153. src_end = src + IN6ADDRSZ;
  154. next_dest = words;
  155. best.base = -1;
  156. cur.base = -1;
  157. i = 0;
  158. do {
  159. unsigned int next_word = (unsigned int)*next_src++;
  160. next_word <<= 8;
  161. next_word |= (unsigned int)*next_src++;
  162. *next_dest++ = next_word;
  163. if (next_word == 0) {
  164. if (cur.base == -1) {
  165. cur.base = i;
  166. cur.len = 1;
  167. }
  168. else {
  169. cur.len++;
  170. }
  171. } else {
  172. if (cur.base != -1) {
  173. if (best.base == -1 || cur.len > best.len) {
  174. best = cur;
  175. }
  176. cur.base = -1;
  177. }
  178. }
  179. i++;
  180. } while (next_src < src_end);
  181. if (cur.base != -1) {
  182. if (best.base == -1 || cur.len > best.len) {
  183. best = cur;
  184. }
  185. }
  186. if (best.base != -1 && best.len < 2) {
  187. best.base = -1;
  188. }
  189. /*
  190. * Format the result.
  191. */
  192. tp = tmp;
  193. for (i = 0; i < (IN6ADDRSZ / INT16SZ);) {
  194. /* Are we inside the best run of 0x00's? */
  195. if (i == best.base) {
  196. *tp++ = ':';
  197. i += best.len;
  198. continue;
  199. }
  200. /* Are we following an initial run of 0x00s or any real hex? */
  201. if (i != 0) {
  202. *tp++ = ':';
  203. }
  204. /* Is this address an encapsulated IPv4? */
  205. if (i == 6 && best.base == 0 &&
  206. (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
  207. if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp))) {
  208. return (NULL);
  209. }
  210. tp += strlen(tp);
  211. break;
  212. }
  213. tp += Snprintf(tp, sizeof tmp - (tp - tmp), "%x", words[i]);
  214. i++;
  215. }
  216. /* Was it a trailing run of 0x00's? */
  217. if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) {
  218. *tp++ = ':';
  219. }
  220. *tp++ = '\0';
  221. /*
  222. * Check for overflow, copy, and we're done.
  223. */
  224. if ((size_t)(tp - tmp) > size) {
  225. #ifndef WIN32
  226. errno = ENOSPC;
  227. #endif
  228. return (NULL);
  229. }
  230. strncpy(dst, tmp, size);
  231. return (dst);
  232. }
  233. #endif