inet_ntop.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. Copyright (c) 2007 - 2010 RIPE NCC - All Rights Reserved
  3. Permission to use, copy, modify, and distribute this software and its
  4. documentation for any purpose and without fee is hereby granted, provided
  5. that the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation, and that the name of the author not be used in advertising or
  8. publicity pertaining to distribution of the software without specific,
  9. written prior permission.
  10. THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  11. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
  12. AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
  13. DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  14. AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /*
  18. * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
  19. *
  20. * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  21. *
  22. * This file contains Original Code and/or Modifications of Original Code
  23. * as defined in and that are subject to the Apple Public Source License
  24. * Version 2.0 (the 'License'). You may not use this file except in
  25. * compliance with the License. The rights granted to you under the License
  26. * may not be used to create, or enable the creation or redistribution of,
  27. * unlawful or unlicensed copies of an Apple operating system, or to
  28. * circumvent, violate, or enable the circumvention or violation of, any
  29. * terms of an Apple operating system software license agreement.
  30. *
  31. * Please obtain a copy of the License at
  32. * http://www.opensource.apple.com/apsl/ and read it before using this file.
  33. *
  34. * The Original Code and all software distributed under the License are
  35. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  36. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  37. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  38. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  39. * Please see the License for the specific language governing rights and
  40. * limitations under the License.
  41. *
  42. * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  43. */
  44. /*
  45. * Copyright 1994, 1995 Massachusetts Institute of Technology
  46. *
  47. * Permission to use, copy, modify, and distribute this software and
  48. * its documentation for any purpose and without fee is hereby
  49. * granted, provided that both the above copyright notice and this
  50. * permission notice appear in all copies, that both the above
  51. * copyright notice and this permission notice appear in all
  52. * supporting documentation, and that the name of M.I.T. not be used
  53. * in advertising or publicity pertaining to distribution of the
  54. * software without specific, written prior permission. M.I.T. makes
  55. * no representations about the suitability of this software for any
  56. * purpose. It is provided "as is" without express or implied
  57. * warranty.
  58. *
  59. * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
  60. * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
  61. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  62. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
  63. * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  64. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  65. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  66. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  67. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  68. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  69. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  70. * SUCH DAMAGE.
  71. */
  72. #include <sys/param.h>
  73. #include <netinet/in.h>
  74. #include <string.h>
  75. #include <stdio.h>
  76. #include <stdbool.h>
  77. #include <assert.h>
  78. #include <sys/types.h>
  79. #include <sys/socket.h>
  80. #include <arpa/inet.h>
  81. #include "util.h"
  82. const char OCTETS[][4] = {
  83. "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
  84. "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31",
  85. "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47",
  86. "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63",
  87. "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79",
  88. "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95",
  89. "96", "97", "98", "99",
  90. "100", "101", "102", "103", "104", "105", "106", "107", "108", "109",
  91. "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121", "122",
  92. "123", "124", "125", "126", "127", "128", "129", "130", "131", "132", "133", "134", "135",
  93. "136", "137", "138", "139", "140", "141", "142", "143", "144", "145", "146", "147", "148",
  94. "149", "150", "151", "152", "153", "154", "155", "156", "157", "158", "159", "160", "161",
  95. "162", "163", "164", "165", "166", "167", "168", "169", "170", "171", "172", "173", "174",
  96. "175", "176", "177", "178", "179", "180", "181", "182", "183", "184", "185", "186", "187",
  97. "188", "189", "190", "191", "192", "193", "194", "195", "196", "197", "198", "199", "200",
  98. "201", "202", "203", "204", "205", "206", "207", "208", "209", "210", "211", "212", "213",
  99. "214", "215", "216", "217", "218", "219", "220", "221", "222", "223", "224", "225", "226",
  100. "227", "228", "229", "230", "231", "232", "233", "234", "235", "236", "237", "238", "239",
  101. "240", "241", "242", "243", "244", "245", "246", "247", "248", "249", "250", "251", "252",
  102. "253", "254", "255"
  103. };
  104. char *fmt_ipv4(BGPDUMP_IP_ADDRESS addr, char *buffer)
  105. {
  106. assert(buffer);
  107. uint8_t *ap = (uint8_t *)&addr.v4_addr.s_addr;
  108. int pos = 0;
  109. int i;
  110. for(i = 0; i < 4; ++i) {
  111. const char *octet = OCTETS[ap[i]];
  112. buffer[pos++] = octet[0];
  113. octet[1] ? buffer[pos++] = octet[1] : (void)0;
  114. octet[2] ? buffer[pos++] = octet[2] : (void)0;
  115. buffer[pos++] = '.';
  116. }
  117. buffer[pos - 1] = '\0';
  118. return buffer;
  119. }
  120. char *fmt_ipv6(BGPDUMP_IP_ADDRESS addr, char *buffer)
  121. {
  122. static const char hexchars[] = "0123456789abcdef";
  123. assert(buffer);
  124. /* check for mapped or compat addresses */
  125. bool m = IN6_IS_ADDR_V4MAPPED(&addr.v6_addr);
  126. bool c = IN6_IS_ADDR_V4COMPAT(&addr.v6_addr);
  127. if (m || c) {
  128. char buffer2[100];
  129. BGPDUMP_IP_ADDRESS mapped = { .v4_addr.s_addr = ((uint32_t *)addr.v6_addr.s6_addr)[3] };
  130. sprintf(buffer, "::%s%s", m ? "ffff:" : "", fmt_ipv4(mapped, buffer2));
  131. return buffer;
  132. }
  133. char hexa[8][5];
  134. int zr[8];
  135. size_t len;
  136. uint8_t x8, hx8;
  137. uint16_t x16;
  138. int i, k = 0;
  139. for (i = 0; i < 16; i += 2) {
  140. int j = 0;
  141. bool skip = 1;
  142. memset(hexa[k], 0, 5);
  143. x8 = addr.v6_addr.s6_addr[i];
  144. hx8 = x8 >> 4;
  145. if (hx8 != 0)
  146. {
  147. skip = 0;
  148. hexa[k][j++] = hexchars[hx8];
  149. }
  150. hx8 = x8 & 0x0f;
  151. if ((skip == 0) || ((skip == 1) && (hx8 != 0)))
  152. {
  153. skip = 0;
  154. hexa[k][j++] = hexchars[hx8];
  155. }
  156. x8 = addr.v6_addr.s6_addr[i + 1];
  157. hx8 = x8 >> 4;
  158. if ((skip == 0) || ((skip == 1) && (hx8 != 0)))
  159. {
  160. hexa[k][j++] = hexchars[hx8];
  161. }
  162. hx8 = x8 & 0x0f;
  163. hexa[k][j] = hexchars[hx8];
  164. k++;
  165. }
  166. /* find runs of zeros for :: convention */
  167. int j = 0;
  168. for (i = 7; i >= 0; i--)
  169. {
  170. zr[i] = j;
  171. x16 = ((uint16_t *)addr.v6_addr.s6_addr)[i];
  172. if (x16 == 0) j++;
  173. else j = 0;
  174. zr[i] = j;
  175. }
  176. /* find longest run of zeros */
  177. k = -1;
  178. j = 0;
  179. for(i = 0; i < 8; i++)
  180. {
  181. if (zr[i] > j)
  182. {
  183. k = i;
  184. j = zr[i];
  185. }
  186. }
  187. for(i = 0; i < 8; i++)
  188. {
  189. if (i != k) zr[i] = 0;
  190. }
  191. len = 0;
  192. for (i = 0; i < 8; i++)
  193. {
  194. if (zr[i] != 0)
  195. {
  196. /* check for leading zero */
  197. if (i == 0)
  198. buffer[len++] = ':';
  199. buffer[len++] = ':';
  200. i += (zr[i] - 1);
  201. continue;
  202. }
  203. for (j = 0; hexa[i][j] != '\0'; j++)
  204. buffer[len++] = hexa[i][j];
  205. if (i != 7)
  206. buffer[len++] = ':';
  207. }
  208. buffer[len] = '\0';
  209. return buffer;
  210. }
  211. static void test_roundtrip(char *str)
  212. {
  213. BGPDUMP_IP_ADDRESS addr;
  214. inet_pton(AF_INET6, str, &addr.v6_addr);
  215. char tmp[1000];
  216. fmt_ipv6(addr, tmp);
  217. printf("%s -> %s [%s]\n", str, tmp, strcmp(str, tmp) ? "ERROR" : "ok");
  218. }
  219. void test_fmt_ip()
  220. {
  221. test_roundtrip("fe80::");
  222. test_roundtrip("2001:db8::1");
  223. test_roundtrip("::ffff:192.168.2.1");
  224. test_roundtrip("::192.168.1.2");
  225. test_roundtrip("2001:7f8:30::2:1:0:8447");
  226. }