addrtostr.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 1999 Kungliga Tekniska Högskolan
  3. * (Royal Institute of Technology, Stockholm, Sweden).
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * 3. All advertising materials mentioning features or use of this software
  18. * must display the following acknowledgement:
  19. * This product includes software developed by the Kungliga Tekniska
  20. * Högskolan and its contributors.
  21. *
  22. * 4. Neither the name of the Institute nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  27. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  36. * SUCH DAMAGE.
  37. */
  38. #ifdef HAVE_CONFIG_H
  39. #include "config.h"
  40. #endif
  41. #include <netdissect-stdinc.h>
  42. #include "addrtostr.h"
  43. #include <stdio.h>
  44. #include <string.h>
  45. /*
  46. *
  47. */
  48. #ifndef IN6ADDRSZ
  49. #define IN6ADDRSZ 16 /* IPv6 T_AAAA */
  50. #endif
  51. #ifndef INT16SZ
  52. #define INT16SZ 2 /* word size */
  53. #endif
  54. const char *
  55. addrtostr (const void *src, char *dst, size_t size)
  56. {
  57. const u_char *srcaddr = (const u_char *)src;
  58. const char digits[] = "0123456789";
  59. int i;
  60. const char *orig_dst = dst;
  61. if (size < INET_ADDRSTRLEN) {
  62. errno = ENOSPC;
  63. return NULL;
  64. }
  65. for (i = 0; i < 4; ++i) {
  66. int n = *srcaddr++;
  67. int non_zerop = 0;
  68. if (non_zerop || n / 100 > 0) {
  69. *dst++ = digits[n / 100];
  70. n %= 100;
  71. non_zerop = 1;
  72. }
  73. if (non_zerop || n / 10 > 0) {
  74. *dst++ = digits[n / 10];
  75. n %= 10;
  76. non_zerop = 1;
  77. }
  78. *dst++ = digits[n];
  79. if (i != 3)
  80. *dst++ = '.';
  81. }
  82. *dst++ = '\0';
  83. return orig_dst;
  84. }
  85. /*
  86. * Convert IPv6 binary address into presentation (printable) format.
  87. */
  88. const char *
  89. addrtostr6 (const void *src, char *dst, size_t size)
  90. {
  91. /*
  92. * Note that int32_t and int16_t need only be "at least" large enough
  93. * to contain a value of the specified size. On some systems, like
  94. * Crays, there is no such thing as an integer variable with 16 bits.
  95. * Keep this in mind if you think this function should have been coded
  96. * to use pointer overlays. All the world's not a VAX.
  97. */
  98. const u_char *srcaddr = (const u_char *)src;
  99. char *dp;
  100. size_t space_left, added_space;
  101. int snprintfed;
  102. struct {
  103. int base;
  104. int len;
  105. } best, cur;
  106. uint16_t words [IN6ADDRSZ / INT16SZ];
  107. int i;
  108. /* Preprocess:
  109. * Copy the input (bytewise) array into a wordwise array.
  110. * Find the longest run of 0x00's in src[] for :: shorthanding.
  111. */
  112. for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
  113. words[i] = (srcaddr[2*i] << 8) | srcaddr[2*i + 1];
  114. best.len = 0;
  115. best.base = -1;
  116. cur.len = 0;
  117. cur.base = -1;
  118. for (i = 0; i < (int)(IN6ADDRSZ / INT16SZ); i++)
  119. {
  120. if (words[i] == 0)
  121. {
  122. if (cur.base == -1)
  123. cur.base = i, cur.len = 1;
  124. else cur.len++;
  125. }
  126. else if (cur.base != -1)
  127. {
  128. if (best.base == -1 || cur.len > best.len)
  129. best = cur;
  130. cur.base = -1;
  131. }
  132. }
  133. if ((cur.base != -1) && (best.base == -1 || cur.len > best.len))
  134. best = cur;
  135. if (best.base != -1 && best.len < 2)
  136. best.base = -1;
  137. /* Format the result.
  138. */
  139. dp = dst;
  140. space_left = size;
  141. #define APPEND_CHAR(c) \
  142. { \
  143. if (space_left == 0) { \
  144. errno = ENOSPC; \
  145. return (NULL); \
  146. } \
  147. *dp++ = c; \
  148. space_left--; \
  149. }
  150. for (i = 0; i < (int)(IN6ADDRSZ / INT16SZ); i++)
  151. {
  152. /* Are we inside the best run of 0x00's?
  153. */
  154. if (best.base != -1 && i >= best.base && i < (best.base + best.len))
  155. {
  156. if (i == best.base)
  157. APPEND_CHAR(':');
  158. continue;
  159. }
  160. /* Are we following an initial run of 0x00s or any real hex?
  161. */
  162. if (i != 0)
  163. APPEND_CHAR(':');
  164. /* Is this address an encapsulated IPv4?
  165. */
  166. if (i == 6 && best.base == 0 &&
  167. (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
  168. {
  169. if (!addrtostr(srcaddr+12, dp, space_left))
  170. {
  171. errno = ENOSPC;
  172. return (NULL);
  173. }
  174. added_space = strlen(dp);
  175. dp += added_space;
  176. space_left -= added_space;
  177. break;
  178. }
  179. snprintfed = snprintf (dp, space_left, "%x", words[i]);
  180. if (snprintfed < 0)
  181. return (NULL);
  182. if ((size_t) snprintfed >= space_left)
  183. {
  184. errno = ENOSPC;
  185. return (NULL);
  186. }
  187. dp += snprintfed;
  188. space_left -= snprintfed;
  189. }
  190. /* Was it a trailing run of 0x00's?
  191. */
  192. if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
  193. APPEND_CHAR(':');
  194. APPEND_CHAR('\0');
  195. return (dst);
  196. }