OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [net/] [common/] [v2_0/] [src/] [inet_pton.c] - Blame information for rev 334

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      src/inet_pton.c
4
//
5
//==========================================================================
6
//####BSDCOPYRIGHTBEGIN####
7
//
8
// -------------------------------------------
9
//
10
// Portions of this software may have been derived from OpenBSD, 
11
// FreeBSD or other sources, and are covered by the appropriate
12
// copyright disclaimers included herein.
13
//
14
// Portions created by Red Hat are
15
// Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
16
//
17
// -------------------------------------------
18
//
19
//####BSDCOPYRIGHTEND####
20
//==========================================================================
21
 
22
/*      $KAME: inet_pton.c,v 1.5 2001/08/20 02:32:40 itojun Exp $       */
23
 
24
/*
25
 * Copyright (c) 1996,1999 by Internet Software Consortium.
26
 *
27
 * Permission to use, copy, modify, and distribute this software for any
28
 * purpose with or without fee is hereby granted, provided that the above
29
 * copyright notice and this permission notice appear in all copies.
30
 *
31
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
32
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
33
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
34
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
35
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
36
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
37
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
38
 * SOFTWARE.
39
 */
40
 
41
//#if defined(LIBC_SCCS) && !defined(lint)
42
//static char rcsid[] = "$Id: inet_pton.c,v 1.1.1.1 2004-02-14 13:32:54 phoenix Exp $";
43
//#endif /* LIBC_SCCS and not lint */
44
 
45
//#ifdef HAVE_CONFIG_H
46
//#include <config.h>
47
//#endif 
48
 
49
#include <sys/param.h>
50
#include <sys/types.h>
51
#include <sys/socket.h>
52
#include <netinet/in.h>
53
#include <arpa/inet.h>
54
#include <arpa/nameser.h>
55
#include <string.h>
56
#include <errno.h>
57
 
58
//#ifndef HAVE_PORTABLE_PROTOTYPE
59
//#include "cdecl_ext.h"
60
//#endif 
61
 
62
//#ifndef HAVE_U_INT16_T
63
//#include "bittypes.h"
64
//#endif 
65
 
66
//#if !(defined(HAVE_INADDRSZ) && defined(HAVE_IN6ADDRSZ))
67
//#include "addrsize.h"
68
//#endif
69
 
70
#define NS_INADDRSZ     4       /* IPv4 T_A */
71
#define NS_IN6ADDRSZ    16      /* IPv6 T_AAAA */
72
 
73
#ifndef NS_INADDRSZ
74
#define NS_INADDRSZ     INADDRSZ
75
#endif
76
#ifndef NS_IN6ADDRSZ
77
#define NS_IN6ADDRSZ    IN6ADDRSZ
78
#endif
79
#ifndef NS_INT16SZ
80
#define NS_INT16SZ      sizeof(u_int16_t)
81
#endif
82
 
83
/*
84
 * WARNING: Don't even consider trying to compile this on a system where
85
 * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
86
 */
87
 
88
static int      inet_pton4 __P((const char *src, u_char *dst));
89
#ifdef CYGPKG_NET_INET6
90
static int      inet_pton6 __P((const char *src, u_char *dst));
91
#endif
92
 
93
/* int
94
 * inet_pton(af, src, dst)
95
 *      convert from presentation format (which usually means ASCII printable)
96
 *      to network format (which is usually some kind of binary format).
97
 * return:
98
 *      1 if the address was valid for the specified address family
99
 *      0 if the address wasn't valid (`dst' is untouched in this case)
100
 *      -1 if some other error occurred (`dst' is untouched in this case, too)
101
 * author:
102
 *      Paul Vixie, 1996.
103
 */
104
int
105
inet_pton(int af, const char *src, void *dst)
106
{
107
        switch (af) {
108
        case AF_INET:
109
                return (inet_pton4(src, dst));
110
#ifdef CYGPKG_NET_INET6
111
        case AF_INET6:
112
                return (inet_pton6(src, dst));
113
#endif 
114
        default:
115
                errno = EAFNOSUPPORT;
116
                return (-1);
117
        }
118
        /* NOTREACHED */
119
}
120
 
121
/* int
122
 * inet_pton4(src, dst)
123
 *      like inet_aton() but without all the hexadecimal and shorthand.
124
 * return:
125
 *      1 if `src' is a valid dotted quad, else 0.
126
 * notice:
127
 *      does not touch `dst' unless it's returning 1.
128
 * author:
129
 *      Paul Vixie, 1996.
130
 */
131
static int
132
inet_pton4(const char *src, u_char *dst)
133
{
134
        static const char digits[] = "0123456789";
135
        int saw_digit, octets, ch;
136
        u_char tmp[NS_INADDRSZ], *tp;
137
 
138
        saw_digit = 0;
139
        octets = 0;
140
        *(tp = tmp) = 0;
141
        while ((ch = *src++) != '\0') {
142
                const char *pch;
143
 
144
                if ((pch = strchr(digits, ch)) != NULL) {
145
                        u_int new = *tp * 10 + (pch - digits);
146
 
147
                        if (new > 255)
148
                                return (0);
149
                        *tp = new;
150
                        if (! saw_digit) {
151
                                if (++octets > 4)
152
                                        return (0);
153
                                saw_digit = 1;
154
                        }
155
                } else if (ch == '.' && saw_digit) {
156
                        if (octets == 4)
157
                                return (0);
158
                        *++tp = 0;
159
                        saw_digit = 0;
160
                } else
161
                        return (0);
162
        }
163
        if (octets < 4)
164
                return (0);
165
        memcpy(dst, tmp, NS_INADDRSZ);
166
        return (1);
167
}
168
 
169
#ifdef CYGPKG_NET_INET6
170
/* int
171
 * inet_pton6(src, dst)
172
 *      convert presentation level address to network order binary form.
173
 * return:
174
 *      1 if `src' is a valid [RFC1884 2.2] address, else 0.
175
 * notice:
176
 *      (1) does not touch `dst' unless it's returning 1.
177
 *      (2) :: in a full address is silently ignored.
178
 * credit:
179
 *      inspired by Mark Andrews.
180
 * author:
181
 *      Paul Vixie, 1996.
182
 */
183
static int
184
inet_pton6(const char *src, u_char *dst)
185
{
186
        static const char xdigits_l[] = "0123456789abcdef",
187
                          xdigits_u[] = "0123456789ABCDEF";
188
        u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
189
        const char *xdigits, *curtok;
190
        int ch, saw_xdigit;
191
        u_int val;
192
 
193
        memset((tp = tmp), '\0', NS_IN6ADDRSZ);
194
        endp = tp + NS_IN6ADDRSZ;
195
        colonp = NULL;
196
        /* Leading :: requires some special handling. */
197
        if (*src == ':')
198
                if (*++src != ':')
199
                        return (0);
200
        curtok = src;
201
        saw_xdigit = 0;
202
        val = 0;
203
        while ((ch = *src++) != '\0') {
204
                const char *pch;
205
 
206
                if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
207
                        pch = strchr((xdigits = xdigits_u), ch);
208
                if (pch != NULL) {
209
                        val <<= 4;
210
                        val |= (pch - xdigits);
211
                        if (val > 0xffff)
212
                                return (0);
213
                        saw_xdigit = 1;
214
                        continue;
215
                }
216
                if (ch == ':') {
217
                        curtok = src;
218
                        if (!saw_xdigit) {
219
                                if (colonp)
220
                                        return (0);
221
                                colonp = tp;
222
                                continue;
223
                        } else if (*src == '\0') {
224
                                return (0);
225
                        }
226
                        if (tp + NS_INT16SZ > endp)
227
                                return (0);
228
                        *tp++ = (u_char) (val >> 8) & 0xff;
229
                        *tp++ = (u_char) val & 0xff;
230
                        saw_xdigit = 0;
231
                        val = 0;
232
                        continue;
233
                }
234
                if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
235
                    inet_pton4(curtok, tp) > 0) {
236
                        tp += NS_INADDRSZ;
237
                        saw_xdigit = 0;
238
                        break;  /* '\0' was seen by inet_pton4(). */
239
                }
240
                return (0);
241
        }
242
        if (saw_xdigit) {
243
                if (tp + NS_INT16SZ > endp)
244
                        return (0);
245
                *tp++ = (u_char) (val >> 8) & 0xff;
246
                *tp++ = (u_char) val & 0xff;
247
        }
248
        if (colonp != NULL) {
249
                /*
250
                 * Since some memmove()'s erroneously fail to handle
251
                 * overlapping regions, we'll do the shift by hand.
252
                 */
253
                const int n = tp - colonp;
254
                int i;
255
 
256
                if (tp == endp)
257
                        return (0);
258
                for (i = 1; i <= n; i++) {
259
                        endp[- i] = colonp[n - i];
260
                        colonp[n - i] = 0;
261
                }
262
                tp = endp;
263
        }
264
        if (tp != endp)
265
                return (0);
266
        memcpy(dst, tmp, NS_IN6ADDRSZ);
267
        return (1);
268
}
269
#endif /*INET6*/

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.