~ubuntu-branches/ubuntu/precise/wget/precise-proposed

« back to all changes in this revision

Viewing changes to lib/inet_ntop.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Langasek
  • Date: 2011-10-19 00:00:09 UTC
  • mfrom: (2.1.13 sid)
  • Revision ID: james.westby@ubuntu.com-20111019000009-8p33w3wz4b1rdri0
Tags: 1.13-1ubuntu1
* Merge from Debian unstable, remaining changes:
  - Add wget-udeb to ship wget.gnu as alternative to busybox wget
    implementation.
  - Depend on libssl-dev 0.9.8k-7ubuntu4 (LP: #503339)
* Dropped changes, superseded in Debian:
  - Keep build dependencies in main:
    + debian/control: remove info2man build-dep
    + debian/patches/series: disable wget-infopod_generated_manpage
  - Mark wget Multi-Arch: foreign, so packages that aren't of the same arch
    can depend on it.
* Pass --with-ssl=openssl; we don't want to use gnutls, there's no udeb for
  it.
* Add a second build pass for the udeb, so we can build without libidn.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- buffer-read-only: t -*- vi: set ro: */
 
2
/* DO NOT EDIT! GENERATED AUTOMATICALLY! */
 
3
/* inet_ntop.c -- convert IPv4 and IPv6 addresses from binary to text form
 
4
 
 
5
   Copyright (C) 2005-2006, 2008-2011 Free Software Foundation, Inc.
 
6
 
 
7
   This program is free software; you can redistribute it and/or modify
 
8
   it under the terms of the GNU General Public License as published by
 
9
   the Free Software Foundation; either version 3, or (at your option)
 
10
   any later version.
 
11
 
 
12
   This program is distributed in the hope that it will be useful,
 
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
   GNU General Public License for more details.
 
16
 
 
17
   You should have received a copy of the GNU General Public License
 
18
   along with this program; if not, write to the Free Software Foundation,
 
19
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
20
 
 
21
/*
 
22
 * Copyright (c) 1996-1999 by Internet Software Consortium.
 
23
 *
 
24
 * Permission to use, copy, modify, and distribute this software for any
 
25
 * purpose with or without fee is hereby granted, provided that the above
 
26
 * copyright notice and this permission notice appear in all copies.
 
27
 *
 
28
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 
29
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 
30
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 
31
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 
32
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 
33
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 
34
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 
35
 * SOFTWARE.
 
36
 */
 
37
 
 
38
#include <config.h>
 
39
 
 
40
/* Specification.  */
 
41
#include <arpa/inet.h>
 
42
 
 
43
#include <stdio.h>
 
44
#include <string.h>
 
45
#include <errno.h>
 
46
 
 
47
#define NS_IN6ADDRSZ 16
 
48
#define NS_INT16SZ 2
 
49
 
 
50
/*
 
51
 * WARNING: Don't even consider trying to compile this on a system where
 
52
 * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
 
53
 */
 
54
typedef int verify_int_size[4 <= sizeof (int) ? 1 : -1];
 
55
 
 
56
static const char *inet_ntop4 (const unsigned char *src, char *dst, socklen_t size);
 
57
#if HAVE_IPV6
 
58
static const char *inet_ntop6 (const unsigned char *src, char *dst, socklen_t size);
 
59
#endif
 
60
 
 
61
 
 
62
/* char *
 
63
 * inet_ntop(af, src, dst, size)
 
64
 *      convert a network format address to presentation format.
 
65
 * return:
 
66
 *      pointer to presentation format address (`dst'), or NULL (see errno).
 
67
 * author:
 
68
 *      Paul Vixie, 1996.
 
69
 */
 
70
const char *
 
71
inet_ntop (int af, const void *restrict src,
 
72
           char *restrict dst, socklen_t cnt)
 
73
{
 
74
  switch (af)
 
75
    {
 
76
#if HAVE_IPV4
 
77
    case AF_INET:
 
78
      return (inet_ntop4 (src, dst, cnt));
 
79
#endif
 
80
 
 
81
#if HAVE_IPV6
 
82
    case AF_INET6:
 
83
      return (inet_ntop6 (src, dst, cnt));
 
84
#endif
 
85
 
 
86
    default:
 
87
      errno = EAFNOSUPPORT;
 
88
      return (NULL);
 
89
    }
 
90
  /* NOTREACHED */
 
91
}
 
92
 
 
93
/* const char *
 
94
 * inet_ntop4(src, dst, size)
 
95
 *      format an IPv4 address
 
96
 * return:
 
97
 *      `dst' (as a const)
 
98
 * notes:
 
99
 *      (1) uses no statics
 
100
 *      (2) takes a u_char* not an in_addr as input
 
101
 * author:
 
102
 *      Paul Vixie, 1996.
 
103
 */
 
104
static const char *
 
105
inet_ntop4 (const unsigned char *src, char *dst, socklen_t size)
 
106
{
 
107
  char tmp[sizeof "255.255.255.255"];
 
108
  int len;
 
109
 
 
110
  len = sprintf (tmp, "%u.%u.%u.%u", src[0], src[1], src[2], src[3]);
 
111
  if (len < 0)
 
112
    return NULL;
 
113
 
 
114
  if (len > size)
 
115
    {
 
116
      errno = ENOSPC;
 
117
      return NULL;
 
118
    }
 
119
 
 
120
  return strcpy (dst, tmp);
 
121
}
 
122
 
 
123
#if HAVE_IPV6
 
124
 
 
125
/* const char *
 
126
 * inet_ntop6(src, dst, size)
 
127
 *      convert IPv6 binary address into presentation (printable) format
 
128
 * author:
 
129
 *      Paul Vixie, 1996.
 
130
 */
 
131
static const char *
 
132
inet_ntop6 (const unsigned char *src, char *dst, socklen_t size)
 
133
{
 
134
  /*
 
135
   * Note that int32_t and int16_t need only be "at least" large enough
 
136
   * to contain a value of the specified size.  On some systems, like
 
137
   * Crays, there is no such thing as an integer variable with 16 bits.
 
138
   * Keep this in mind if you think this function should have been coded
 
139
   * to use pointer overlays.  All the world's not a VAX.
 
140
   */
 
141
  char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
 
142
  struct
 
143
  {
 
144
    int base, len;
 
145
  } best, cur;
 
146
  unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
 
147
  int i;
 
148
 
 
149
  /*
 
150
   * Preprocess:
 
151
   *      Copy the input (bytewise) array into a wordwise array.
 
152
   *      Find the longest run of 0x00's in src[] for :: shorthanding.
 
153
   */
 
154
  memset (words, '\0', sizeof words);
 
155
  for (i = 0; i < NS_IN6ADDRSZ; i += 2)
 
156
    words[i / 2] = (src[i] << 8) | src[i + 1];
 
157
  best.base = -1;
 
158
  cur.base = -1;
 
159
  for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
 
160
    {
 
161
      if (words[i] == 0)
 
162
        {
 
163
          if (cur.base == -1)
 
164
            cur.base = i, cur.len = 1;
 
165
          else
 
166
            cur.len++;
 
167
        }
 
168
      else
 
169
        {
 
170
          if (cur.base != -1)
 
171
            {
 
172
              if (best.base == -1 || cur.len > best.len)
 
173
                best = cur;
 
174
              cur.base = -1;
 
175
            }
 
176
        }
 
177
    }
 
178
  if (cur.base != -1)
 
179
    {
 
180
      if (best.base == -1 || cur.len > best.len)
 
181
        best = cur;
 
182
    }
 
183
  if (best.base != -1 && best.len < 2)
 
184
    best.base = -1;
 
185
 
 
186
  /*
 
187
   * Format the result.
 
188
   */
 
189
  tp = tmp;
 
190
  for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
 
191
    {
 
192
      /* Are we inside the best run of 0x00's? */
 
193
      if (best.base != -1 && i >= best.base && i < (best.base + best.len))
 
194
        {
 
195
          if (i == best.base)
 
196
            *tp++ = ':';
 
197
          continue;
 
198
        }
 
199
      /* Are we following an initial run of 0x00s or any real hex? */
 
200
      if (i != 0)
 
201
        *tp++ = ':';
 
202
      /* Is this address an encapsulated IPv4? */
 
203
      if (i == 6 && best.base == 0 &&
 
204
          (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
 
205
        {
 
206
          if (!inet_ntop4 (src + 12, tp, sizeof tmp - (tp - tmp)))
 
207
            return (NULL);
 
208
          tp += strlen (tp);
 
209
          break;
 
210
        }
 
211
      {
 
212
        int len = sprintf (tp, "%x", words[i]);
 
213
        if (len < 0)
 
214
          return NULL;
 
215
        tp += len;
 
216
      }
 
217
    }
 
218
  /* Was it a trailing run of 0x00's? */
 
219
  if (best.base != -1 && (best.base + best.len) ==
 
220
      (NS_IN6ADDRSZ / NS_INT16SZ))
 
221
    *tp++ = ':';
 
222
  *tp++ = '\0';
 
223
 
 
224
  /*
 
225
   * Check for overflow, copy, and we're done.
 
226
   */
 
227
  if ((socklen_t) (tp - tmp) > size)
 
228
    {
 
229
      errno = ENOSPC;
 
230
      return NULL;
 
231
    }
 
232
 
 
233
  return strcpy (dst, tmp);
 
234
}
 
235
 
 
236
#endif