~ubuntu-branches/ubuntu/quantal/pgbouncer/quantal-security

« back to all changes in this revision

Viewing changes to lib/usual/socket_ntop.c

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Berg
  • Date: 2011-04-12 13:42:40 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20110412134240-rm2kawn5svh81y2g
Tags: 1.4.1-1
* New upstream release.
* Remove the endian patch, implemented upstream.
* Use source format 3.0 (quilt) because the upstream tarball has its own
  debian/ dir. Ignore lib/usual/config.h in source/options.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*      $OpenBSD: inet_ntop.c,v 1.8 2008/12/09 19:38:38 otto Exp $      */
 
2
 
 
3
/* Copyright (c) 1996 by Internet Software Consortium.
 
4
 *
 
5
 * Permission to use, copy, modify, and distribute this software for any
 
6
 * purpose with or without fee is hereby granted, provided that the above
 
7
 * copyright notice and this permission notice appear in all copies.
 
8
 *
 
9
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 
10
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 
11
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 
12
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 
13
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 
14
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 
15
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 
16
 * SOFTWARE.
 
17
 */
 
18
 
 
19
#include <usual/socket.h>
 
20
#include <usual/string.h>
 
21
 
 
22
#ifndef HAVE_INET_NTOP
 
23
 
 
24
#ifndef INADDRSZ
 
25
#define INADDRSZ 4
 
26
#endif
 
27
#ifndef IN6ADDRSZ
 
28
#define IN6ADDRSZ 16
 
29
#endif
 
30
#ifndef INT16SZ
 
31
#define INT16SZ 2
 
32
#endif
 
33
 
 
34
#define u_char uint8_t
 
35
#define u_int unsigned int
 
36
 
 
37
/*
 
38
 * WARNING: Don't even consider trying to compile this on a system where
 
39
 * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
 
40
 */
 
41
 
 
42
static const char *inet_ntop4(const u_char *src, char *dst, int size);
 
43
static const char *inet_ntop6(const u_char *src, char *dst, int size);
 
44
 
 
45
/* char *
 
46
 * inet_ntop(af, src, dst, size)
 
47
 *      convert a network format address to presentation format.
 
48
 * return:
 
49
 *      pointer to presentation format address (`dst'), or NULL (see errno).
 
50
 * author:
 
51
 *      Paul Vixie, 1996.
 
52
 */
 
53
const char *
 
54
inet_ntop(int af, const void *src, char *dst, int size)
 
55
{
 
56
        if (size < 0) {
 
57
                errno = ENOSPC;
 
58
                return NULL;
 
59
        }
 
60
        switch (af) {
 
61
        case AF_INET:
 
62
                return (inet_ntop4(src, dst, size));
 
63
        case AF_INET6:
 
64
                return (inet_ntop6(src, dst, size));
 
65
        default:
 
66
                errno = EAFNOSUPPORT;
 
67
                return (NULL);
 
68
        }
 
69
        /* NOTREACHED */
 
70
}
 
71
 
 
72
/* const char *
 
73
 * inet_ntop4(src, dst, size)
 
74
 *      format an IPv4 address, more or less like inet_ntoa()
 
75
 * return:
 
76
 *      `dst' (as a const)
 
77
 * notes:
 
78
 *      (1) uses no statics
 
79
 *      (2) takes a u_char* not an in_addr as input
 
80
 * author:
 
81
 *      Paul Vixie, 1996.
 
82
 */
 
83
static const char *
 
84
inet_ntop4(const u_char *src, char *dst, int size)
 
85
{
 
86
        static const char fmt[] = "%u.%u.%u.%u";
 
87
        char tmp[sizeof "255.255.255.255"];
 
88
        int l;
 
89
 
 
90
        l = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
 
91
        if (l <= 0 || l >= size) {
 
92
                errno = ENOSPC;
 
93
                return (NULL);
 
94
        }
 
95
        strlcpy(dst, tmp, size);
 
96
        return (dst);
 
97
}
 
98
 
 
99
/* const char *
 
100
 * inet_ntop6(src, dst, size)
 
101
 *      convert IPv6 binary address into presentation (printable) format
 
102
 * author:
 
103
 *      Paul Vixie, 1996.
 
104
 */
 
105
static const char *
 
106
inet_ntop6(const u_char *src, char *dst, int size)
 
107
{
 
108
        /*
 
109
         * Note that int32_t and int16_t need only be "at least" large enough
 
110
         * to contain a value of the specified size.  On some systems, like
 
111
         * Crays, there is no such thing as an integer variable with 16 bits.
 
112
         * Keep this in mind if you think this function should have been coded
 
113
         * to use pointer overlays.  All the world's not a VAX.
 
114
         */
 
115
        char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
 
116
        char *tp, *ep;
 
117
        struct { int base, len; } best, cur;
 
118
        u_int words[IN6ADDRSZ / INT16SZ];
 
119
        int i;
 
120
        int advance;
 
121
 
 
122
        /*
 
123
         * Preprocess:
 
124
         *      Copy the input (bytewise) array into a wordwise array.
 
125
         *      Find the longest run of 0x00's in src[] for :: shorthanding.
 
126
         */
 
127
        memset(words, '\0', sizeof words);
 
128
        for (i = 0; i < IN6ADDRSZ; i++)
 
129
                words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
 
130
        best.base = -1;
 
131
        cur.base = -1;
 
132
        for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
 
133
                if (words[i] == 0) {
 
134
                        if (cur.base == -1)
 
135
                                cur.base = i, cur.len = 1;
 
136
                        else
 
137
                                cur.len++;
 
138
                } else {
 
139
                        if (cur.base != -1) {
 
140
                                if (best.base == -1 || cur.len > best.len)
 
141
                                        best = cur;
 
142
                                cur.base = -1;
 
143
                        }
 
144
                }
 
145
        }
 
146
        if (cur.base != -1) {
 
147
                if (best.base == -1 || cur.len > best.len)
 
148
                        best = cur;
 
149
        }
 
150
        if (best.base != -1 && best.len < 2)
 
151
                best.base = -1;
 
152
 
 
153
        /*
 
154
         * Format the result.
 
155
         */
 
156
        tp = tmp;
 
157
        ep = tmp + sizeof(tmp);
 
158
        for (i = 0; i < (IN6ADDRSZ / INT16SZ) && tp < ep; i++) {
 
159
                /* Are we inside the best run of 0x00's? */
 
160
                if (best.base != -1 && i >= best.base &&
 
161
                    i < (best.base + best.len)) {
 
162
                        if (i == best.base) {
 
163
                                if (tp + 1 >= ep)
 
164
                                        return (NULL);
 
165
                                *tp++ = ':';
 
166
                        }
 
167
                        continue;
 
168
                }
 
169
                /* Are we following an initial run of 0x00s or any real hex? */
 
170
                if (i != 0) {
 
171
                        if (tp + 1 >= ep)
 
172
                                return (NULL);
 
173
                        *tp++ = ':';
 
174
                }
 
175
                /* Is this address an encapsulated IPv4? */
 
176
                if (i == 6 && best.base == 0 &&
 
177
                    (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
 
178
                        if (!inet_ntop4(src+12, tp, (size_t)(ep - tp)))
 
179
                                return (NULL);
 
180
                        tp += strlen(tp);
 
181
                        break;
 
182
                }
 
183
                advance = snprintf(tp, ep - tp, "%x", words[i]);
 
184
                if (advance <= 0 || advance >= ep - tp)
 
185
                        return (NULL);
 
186
                tp += advance;
 
187
        }
 
188
        /* Was it a trailing run of 0x00's? */
 
189
        if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) {
 
190
                if (tp + 1 >= ep)
 
191
                        return (NULL);
 
192
                *tp++ = ':';
 
193
        }
 
194
        if (tp + 1 >= ep)
 
195
                return (NULL);
 
196
        *tp++ = '\0';
 
197
 
 
198
        /*
 
199
         * Check for overflow, copy, and we're done.
 
200
         */
 
201
        if ((tp - tmp) > size) {
 
202
                errno = ENOSPC;
 
203
                return (NULL);
 
204
        }
 
205
        strlcpy(dst, tmp, size);
 
206
        return (dst);
 
207
}
 
208
 
 
209
#endif