1/*
2 * Copyright (C) 1996-2001  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
9 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
10 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
11 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
13 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
14 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
15 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17/*
18 * Original code by Paul Vixie. "curlified" by Gisle Vanem.
19 */
20
21#include "setup.h"
22
23#ifndef HAVE_INET_NTOP
24
25#ifdef HAVE_SYS_PARAM_H
26#include <sys/param.h>
27#endif
28#ifdef HAVE_SYS_SOCKET_H
29#include <sys/socket.h>
30#endif
31#ifdef HAVE_NETINET_IN_H
32#include <netinet/in.h>
33#endif
34#ifdef HAVE_ARPA_INET_H
35#include <arpa/inet.h>
36#endif
37#include <string.h>
38#include <errno.h>
39
40#define _MPRINTF_REPLACE /* use our functions only */
41#include <curl/mprintf.h>
42
43#include "inet_ntop.h"
44
45#define IN6ADDRSZ       16
46#define INADDRSZ         4
47#define INT16SZ          2
48
49/*
50 * Format an IPv4 address, more or less like inet_ntoa().
51 *
52 * Returns `dst' (as a const)
53 * Note:
54 *  - uses no statics
55 *  - takes a unsigned char* not an in_addr as input
56 */
57static char *inet_ntop4 (const unsigned char *src, char *dst, size_t size)
58{
59  char tmp[sizeof "255.255.255.255"];
60  size_t len;
61
62  DEBUGASSERT(size >= 16);
63
64  tmp[0] = '\0';
65  (void)snprintf(tmp, sizeof(tmp), "%d.%d.%d.%d",
66          ((int)((unsigned char)src[0])) & 0xff,
67          ((int)((unsigned char)src[1])) & 0xff,
68          ((int)((unsigned char)src[2])) & 0xff,
69          ((int)((unsigned char)src[3])) & 0xff);
70
71  len = strlen(tmp);
72  if(len == 0 || len >= size) {
73    SET_ERRNO(ENOSPC);
74    return (NULL);
75  }
76  strcpy(dst, tmp);
77  return dst;
78}
79
80#ifdef ENABLE_IPV6
81/*
82 * Convert IPv6 binary address into presentation (printable) format.
83 */
84static char *inet_ntop6 (const unsigned char *src, char *dst, size_t size)
85{
86  /*
87   * Note that int32_t and int16_t need only be "at least" large enough
88   * to contain a value of the specified size.  On some systems, like
89   * Crays, there is no such thing as an integer variable with 16 bits.
90   * Keep this in mind if you think this function should have been coded
91   * to use pointer overlays.  All the world's not a VAX.
92   */
93  char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
94  char *tp;
95  struct {
96    long base;
97    long len;
98  } best, cur;
99  unsigned long words[IN6ADDRSZ / INT16SZ];
100  int i;
101
102  /* Preprocess:
103   *  Copy the input (bytewise) array into a wordwise array.
104   *  Find the longest run of 0x00's in src[] for :: shorthanding.
105   */
106  memset(words, '\0', sizeof(words));
107  for(i = 0; i < IN6ADDRSZ; i++)
108    words[i/2] |= (src[i] << ((1 - (i % 2)) << 3));
109
110  best.base = -1;
111  cur.base  = -1;
112  best.len = 0;
113  cur.len = 0;
114
115  for(i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
116    if(words[i] == 0) {
117      if(cur.base == -1)
118        cur.base = i, cur.len = 1;
119      else
120        cur.len++;
121    }
122    else if(cur.base != -1) {
123      if(best.base == -1 || cur.len > best.len)
124        best = cur;
125      cur.base = -1;
126    }
127  }
128  if((cur.base != -1) && (best.base == -1 || cur.len > best.len))
129    best = cur;
130  if(best.base != -1 && best.len < 2)
131    best.base = -1;
132  /* Format the result. */
133  tp = tmp;
134  for(i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
135    /* Are we inside the best run of 0x00's? */
136    if(best.base != -1 && i >= best.base && i < (best.base + best.len)) {
137      if(i == best.base)
138        *tp++ = ':';
139      continue;
140    }
141
142    /* Are we following an initial run of 0x00s or any real hex?
143     */
144    if(i != 0)
145      *tp++ = ':';
146
147    /* Is this address an encapsulated IPv4?
148     */
149    if(i == 6 && best.base == 0 &&
150        (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
151      if(!inet_ntop4(src+12, tp, sizeof(tmp) - (tp - tmp))) {
152        SET_ERRNO(ENOSPC);
153        return (NULL);
154      }
155      tp += strlen(tp);
156      break;
157    }
158    tp += snprintf(tp, 5, "%lx", words[i]);
159  }
160
161  /* Was it a trailing run of 0x00's?
162   */
163  if(best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
164     *tp++ = ':';
165  *tp++ = '\0';
166
167  /* Check for overflow, copy, and we're done.
168   */
169  if((size_t)(tp - tmp) > size) {
170    SET_ERRNO(ENOSPC);
171    return (NULL);
172  }
173  strcpy(dst, tmp);
174  return dst;
175}
176#endif  /* ENABLE_IPV6 */
177
178/*
179 * Convert a network format address to presentation format.
180 *
181 * Returns pointer to presentation format address (`buf').
182 * Returns NULL on error and errno set with the specific
183 * error, EAFNOSUPPORT or ENOSPC.
184 *
185 * On Windows we store the error in the thread errno, not
186 * in the winsock error code. This is to avoid losing the
187 * actual last winsock error. So use macro ERRNO to fetch the
188 * errno this function sets when returning NULL, not SOCKERRNO.
189 */
190char *Curl_inet_ntop(int af, const void *src, char *buf, size_t size)
191{
192  switch (af) {
193  case AF_INET:
194    return inet_ntop4((const unsigned char*)src, buf, size);
195#ifdef ENABLE_IPV6
196  case AF_INET6:
197    return inet_ntop6((const unsigned char*)src, buf, size);
198#endif
199  default:
200    SET_ERRNO(EAFNOSUPPORT);
201    return NULL;
202  }
203}
204#endif  /* HAVE_INET_NTOP */
205