1/** @File
2  Convert a binary network address into a presentable (printable) format.
3
4  Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
5  This program and the accompanying materials are licensed and made available under
6  the terms and conditions of the BSD License that accompanies this distribution.
7  The full text of the license may be found at
8  http://opensource.org/licenses/bsd-license.
9
10  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
14 * Copyright (c) 1996-1999 by Internet Software Consortium.
15 *
16 * Permission to use, copy, modify, and distribute this software for any
17 * purpose with or without fee is hereby granted, provided that the above
18 * copyright notice and this permission notice appear in all copies.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
21 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
22 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
23 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
24 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
25 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
26 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27
28  NetBSD: inet_ntop.c,v 1.3.4.2 2007/05/17 21:25:14 jdc Exp
29  inet_ntop.c,v 1.3.18.2 2005/11/03 23:02:22 marka Exp
30**/
31#include  <LibConfig.h>
32
33//#include "port_before.h"
34
35#include "namespace.h"
36#include <sys/param.h>
37#include <sys/types.h>
38#include <sys/socket.h>
39
40#include <netinet/in.h>
41#include <arpa/inet.h>
42#include <arpa/nameser.h>
43
44#include <assert.h>
45#include <errno.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49
50//#include "port_after.h"
51
52#ifdef __weak_alias
53  __weak_alias(inet_ntop,_inet_ntop)
54#endif
55
56/*%
57 * WARNING: Don't even consider trying to compile this on a system where
58 * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
59 */
60
61static const char *inet_ntop4(const u_char *src, char *dst, socklen_t size);
62static const char *inet_ntop6(const u_char *src, char *dst, socklen_t size);
63
64/* char *
65 * inet_ntop(af, src, dst, size)
66 *  convert a network format address to presentation format.
67 * return:
68 *  pointer to presentation format address (`dst'), or NULL (see errno).
69 * author:
70 *  Paul Vixie, 1996.
71 */
72const char *
73inet_ntop(int af, const void *src, char *dst, socklen_t size)
74{
75
76  _DIAGASSERT(src != NULL);
77  _DIAGASSERT(dst != NULL);
78
79  switch (af) {
80  case AF_INET:
81    return (inet_ntop4(src, dst, size));
82  case AF_INET6:
83    return (inet_ntop6(src, dst, size));
84  default:
85    errno = EAFNOSUPPORT;
86    return (NULL);
87  }
88  /* NOTREACHED */
89}
90
91/* const char *
92 * inet_ntop4(src, dst, size)
93 *  format an IPv4 address, more or less like inet_ntoa()
94 * return:
95 *  `dst' (as a const)
96 * notes:
97 *  (1) uses no statics
98 *  (2) takes a u_char* not an in_addr as input
99 * author:
100 *  Paul Vixie, 1996.
101 */
102static const char *
103inet_ntop4(const u_char *src, char *dst, socklen_t size)
104{
105  char tmp[sizeof "255.255.255.255"];
106  int l;
107
108  _DIAGASSERT(src != NULL);
109  _DIAGASSERT(dst != NULL);
110
111  l = snprintf(tmp, sizeof(tmp), "%u.%u.%u.%u",
112      src[0], src[1], src[2], src[3]);
113  if (l <= 0 || (socklen_t) l >= size) {
114    errno = ENOSPC;
115    return (NULL);
116  }
117  //strlcpy(dst, tmp, size);
118  strncpyX(dst, tmp, (size_t)size);
119  return (dst);
120}
121
122/* const char *
123 * inet_ntop6(src, dst, size)
124 *  convert IPv6 binary address into presentation (printable) format
125 * author:
126 *  Paul Vixie, 1996.
127 */
128static const char *
129inet_ntop6(const u_char *src, char *dst, socklen_t size)
130{
131  /*
132   * Note that int32_t and int16_t need only be "at least" large enough
133   * to contain a value of the specified size.  On some systems, like
134   * Crays, there is no such thing as an integer variable with 16 bits.
135   * Keep this in mind if you think this function should have been coded
136   * to use pointer overlays.  All the world's not a VAX.
137   */
138  char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
139  char *tp, *ep;
140  struct { int base, len; } best, cur;
141  unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
142  int i;
143  int advance;
144
145  _DIAGASSERT(src != NULL);
146  _DIAGASSERT(dst != NULL);
147
148  /*
149   * Preprocess:
150   *  Copy the input (bytewise) array into a wordwise array.
151   *  Find the longest run of 0x00's in src[] for :: shorthanding.
152   */
153  memset(words, '\0', sizeof words);
154  for (i = 0; i < NS_IN6ADDRSZ; i++)
155    words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
156  best.base = -1;
157  best.len = 0;
158  cur.base = -1;
159  cur.len = 0;
160  for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
161    if (words[i] == 0) {
162      if (cur.base == -1)
163        cur.base = i, cur.len = 1;
164      else
165        cur.len++;
166    } else {
167      if (cur.base != -1) {
168        if (best.base == -1 || cur.len > best.len)
169          best = cur;
170        cur.base = -1;
171      }
172    }
173  }
174  if (cur.base != -1) {
175    if (best.base == -1 || cur.len > best.len)
176      best = cur;
177  }
178  if (best.base != -1 && best.len < 2)
179    best.base = -1;
180
181  /*
182   * Format the result.
183   */
184  tp = tmp;
185  ep = tmp + sizeof(tmp);
186  for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
187    /* Are we inside the best run of 0x00's? */
188    if (best.base != -1 && i >= best.base &&
189        i < (best.base + best.len)) {
190      if (i == best.base)
191        *tp++ = ':';
192      continue;
193    }
194    /* Are we following an initial run of 0x00s or any real hex? */
195    if (i != 0) {
196      if (tp + 1 >= ep)
197        return (NULL);
198      *tp++ = ':';
199    }
200    /* Is this address an encapsulated IPv4? */
201    if (i == 6 && best.base == 0 &&
202        (best.len == 6 ||
203        (best.len == 7 && words[7] != 0x0001) ||
204        (best.len == 5 && words[5] == 0xffff))) {
205      if (!inet_ntop4(src+12, tp, (socklen_t)(ep - tp)))
206        return (NULL);
207      tp += strlen(tp);
208      break;
209    }
210    advance = snprintf(tp, (size_t)(ep - tp), "%x", words[i]);
211    if (advance <= 0 || advance >= ep - tp)
212      return (NULL);
213    tp += advance;
214  }
215  /* Was it a trailing run of 0x00's? */
216  if (best.base != -1 && (best.base + best.len) ==
217      (NS_IN6ADDRSZ / NS_INT16SZ)) {
218    if (tp + 1 >= ep)
219      return (NULL);
220    *tp++ = ':';
221  }
222  if (tp + 1 >= ep)
223    return (NULL);
224  *tp++ = '\0';
225
226  /*
227   * Check for overflow, copy, and we're done.
228   */
229  if ((size_t)(tp - tmp) > size) {
230    errno = ENOSPC;
231    return (NULL);
232  }
233  //strlcpy(dst, tmp, size);
234  strncpyX(dst, tmp, (size_t)size);
235  return (dst);
236}
237
238/*! \file */
239