~ubuntu-branches/ubuntu/precise/stellarium/precise

« back to all changes in this revision

Viewing changes to src/external/kdewin32/bind/inet_pton.c

  • Committer: Bazaar Package Importer
  • Author(s): Cédric Delfosse
  • Date: 2009-03-13 20:07:22 UTC
  • mfrom: (1.1.8 upstream) (4.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090313200722-l66s4zy2s3e8up0s
Tags: 0.10.2-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
 
3
 * Copyright (c) 1996,1999 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 ISC DISCLAIMS ALL WARRANTIES
 
10
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 
11
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
 
12
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 
13
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 
14
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 
15
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
16
 */
 
17
/* modified to make it compile with kdewin32 */
 
18
 
 
19
//#include "kdewin32/port_before.h"
 
20
#include <sys/param.h>
 
21
#include "kdewin32/sys/types.h"
 
22
#include "kdewin32/sys/socket.h"
 
23
#include "kdewin32/netinet/in.h"
 
24
#include "kdewin32/arpa/inet.h"
 
25
//#include "kdewin32/arpa/nameser.h"
 
26
#include "kdewin32/string.h"
 
27
#include "kdewin32/errno.h"
 
28
//#include "kdewin32/port_after.h"
 
29
#define __P(x) x
 
30
#define NS_INADDRSZ     4       /*%< IPv4 T_A */
 
31
#define NS_IN6ADDRSZ    16      /*%< IPv6 T_AAAA */
 
32
#define NS_INT16SZ      2       /*%< #/bytes of data in a u_int16_t */
 
33
 
 
34
/*%
 
35
 * WARNING: Don't even consider trying to compile this on a system where
 
36
 * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
 
37
 */
 
38
 
 
39
static int      inet_pton4 __P((const char *src, u_char *dst));
 
40
static int      inet_pton6 __P((const char *src, u_char *dst));
 
41
 
 
42
/* int
 
43
 * inet_pton(af, src, dst)
 
44
 *      convert from presentation format (which usually means ASCII printable)
 
45
 *      to network format (which is usually some kind of binary format).
 
46
 * return:
 
47
 *      1 if the address was valid for the specified address family
 
48
 *      0 if the address wasn't valid (`dst' is untouched in this case)
 
49
 *      -1 if some other error occurred (`dst' is untouched in this case, too)
 
50
 * author:
 
51
 *      Paul Vixie, 1996.
 
52
 */
 
53
int
 
54
kde_inet_pton(af, src, dst)
 
55
        int af;
 
56
        const char *src;
 
57
        void *dst;
 
58
{
 
59
        switch (af) {
 
60
        case AF_INET:
 
61
                return (inet_pton4(src, dst));
 
62
        case AF_INET6:
 
63
                return (inet_pton6(src, dst));
 
64
        default:
 
65
                errno = EAFNOSUPPORT;
 
66
                return (-1);
 
67
        }
 
68
        /* NOTREACHED */
 
69
}
 
70
 
 
71
/* int
 
72
 * inet_pton4(src, dst)
 
73
 *      like inet_aton() but without all the hexadecimal and shorthand.
 
74
 * return:
 
75
 *      1 if `src' is a valid dotted quad, else 0.
 
76
 * notice:
 
77
 *      does not touch `dst' unless it's returning 1.
 
78
 * author:
 
79
 *      Paul Vixie, 1996.
 
80
 */
 
81
static int
 
82
inet_pton4(src, dst)
 
83
        const char *src;
 
84
        u_char *dst;
 
85
{
 
86
        static const char digits[] = "0123456789";
 
87
        int saw_digit, octets, ch;
 
88
        u_char tmp[NS_INADDRSZ], *tp;
 
89
 
 
90
        saw_digit = 0;
 
91
        octets = 0;
 
92
        *(tp = tmp) = 0;
 
93
        while ((ch = *src++) != '\0') {
 
94
                const char *pch;
 
95
 
 
96
                if ((pch = strchr(digits, ch)) != NULL) {
 
97
                        u_int new = *tp * 10 + (pch - digits);
 
98
 
 
99
                        if (saw_digit && *tp == 0)
 
100
                                return (0);
 
101
                        if (new > 255)
 
102
                                return (0);
 
103
                        *tp = new;
 
104
                        if (!saw_digit) {
 
105
                                if (++octets > 4)
 
106
                                        return (0);
 
107
                                saw_digit = 1;
 
108
                        }
 
109
                } else if (ch == '.' && saw_digit) {
 
110
                        if (octets == 4)
 
111
                                return (0);
 
112
                        *++tp = 0;
 
113
                        saw_digit = 0;
 
114
                } else
 
115
                        return (0);
 
116
        }
 
117
        if (octets < 4)
 
118
                return (0);
 
119
        memcpy(dst, tmp, NS_INADDRSZ);
 
120
        return (1);
 
121
}
 
122
 
 
123
/* int
 
124
 * inet_pton6(src, dst)
 
125
 *      convert presentation level address to network order binary form.
 
126
 * return:
 
127
 *      1 if `src' is a valid [RFC1884 2.2] address, else 0.
 
128
 * notice:
 
129
 *      (1) does not touch `dst' unless it's returning 1.
 
130
 *      (2) :: in a full address is silently ignored.
 
131
 * credit:
 
132
 *      inspired by Mark Andrews.
 
133
 * author:
 
134
 *      Paul Vixie, 1996.
 
135
 */
 
136
static int
 
137
inet_pton6(src, dst)
 
138
        const char *src;
 
139
        u_char *dst;
 
140
{
 
141
        static const char xdigits_l[] = "0123456789abcdef",
 
142
                          xdigits_u[] = "0123456789ABCDEF";
 
143
        u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
 
144
        const char *xdigits, *curtok;
 
145
        int ch, seen_xdigits;
 
146
        u_int val;
 
147
 
 
148
        memset((tp = tmp), '\0', NS_IN6ADDRSZ);
 
149
        endp = tp + NS_IN6ADDRSZ;
 
150
        colonp = NULL;
 
151
        /* Leading :: requires some special handling. */
 
152
        if (*src == ':')
 
153
                if (*++src != ':')
 
154
                        return (0);
 
155
        curtok = src;
 
156
        seen_xdigits = 0;
 
157
        val = 0;
 
158
        while ((ch = *src++) != '\0') {
 
159
                const char *pch;
 
160
 
 
161
                if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
 
162
                        pch = strchr((xdigits = xdigits_u), ch);
 
163
                if (pch != NULL) {
 
164
                        val <<= 4;
 
165
                        val |= (pch - xdigits);
 
166
                        if (++seen_xdigits > 4)
 
167
                                return (0);
 
168
                        continue;
 
169
                }
 
170
                if (ch == ':') {
 
171
                        curtok = src;
 
172
                        if (!seen_xdigits) {
 
173
                                if (colonp)
 
174
                                        return (0);
 
175
                                colonp = tp;
 
176
                                continue;
 
177
                        } else if (*src == '\0') {
 
178
                                return (0);
 
179
                        }
 
180
                        if (tp + NS_INT16SZ > endp)
 
181
                                return (0);
 
182
                        *tp++ = (u_char) (val >> 8) & 0xff;
 
183
                        *tp++ = (u_char) val & 0xff;
 
184
                        seen_xdigits = 0;
 
185
                        val = 0;
 
186
                        continue;
 
187
                }
 
188
                if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
 
189
                    inet_pton4(curtok, tp) > 0) {
 
190
                        tp += NS_INADDRSZ;
 
191
                        seen_xdigits = 0;
 
192
                        break;  /*%< '\\0' was seen by inet_pton4(). */
 
193
                }
 
194
                return (0);
 
195
        }
 
196
        if (seen_xdigits) {
 
197
                if (tp + NS_INT16SZ > endp)
 
198
                        return (0);
 
199
                *tp++ = (u_char) (val >> 8) & 0xff;
 
200
                *tp++ = (u_char) val & 0xff;
 
201
        }
 
202
        if (colonp != NULL) {
 
203
                /*
 
204
                 * Since some memmove()'s erroneously fail to handle
 
205
                 * overlapping regions, we'll do the shift by hand.
 
206
                 */
 
207
                const int n = tp - colonp;
 
208
                int i;
 
209
 
 
210
                if (tp == endp)
 
211
                        return (0);
 
212
                for (i = 1; i <= n; i++) {
 
213
                        endp[- i] = colonp[n - i];
 
214
                        colonp[n - i] = 0;
 
215
                }
 
216
                tp = endp;
 
217
        }
 
218
        if (tp != endp)
 
219
                return (0);
 
220
        memcpy(dst, tmp, NS_IN6ADDRSZ);
 
221
        return (1);
 
222
}
 
223
 
 
224
/*! \file */