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