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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [common/] [current/] [src/] [inet_addr.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      src/inet_addr.c
4
//
5
//      INET address parse functions
6
//
7
//==========================================================================
8
// ####BSDCOPYRIGHTBEGIN####                                    
9
// -------------------------------------------                  
10
// This file is part of eCos, the Embedded Configurable Operating System.
11
//
12
// Portions of this software may have been derived from FreeBSD, OpenBSD,
13
// or other sources, and if so are covered by the appropriate copyright
14
// and license included herein.                                 
15
//
16
// Portions created by the Free Software Foundation are         
17
// Copyright (C) 2002 Free Software Foundation, Inc.            
18
// -------------------------------------------                  
19
// ####BSDCOPYRIGHTEND####                                      
20
//==========================================================================
21
//#####DESCRIPTIONBEGIN####
22
//
23
// Author(s):    gthomas
24
// Contributors: gthomas
25
// Date:         2000-01-10
26
// Purpose:      
27
// Description:  
28
//              
29
//
30
//####DESCRIPTIONEND####
31
//
32
//==========================================================================
33
 
34
/*      $OpenBSD: inet_addr.c,v 1.6 1999/05/03 22:31:14 yanick Exp $    */
35
 
36
/*
37
 * ++Copyright++ 1983, 1990, 1993
38
 * -
39
 * Copyright (c) 1983, 1990, 1993
40
 *    The Regents of the University of California.  All rights reserved.
41
 *
42
 * Redistribution and use in source and binary forms, with or without
43
 * modification, are permitted provided that the following conditions
44
 * are met:
45
 * 1. Redistributions of source code must retain the above copyright
46
 *    notice, this list of conditions and the following disclaimer.
47
 * 2. Redistributions in binary form must reproduce the above copyright
48
 *    notice, this list of conditions and the following disclaimer in the
49
 *    documentation and/or other materials provided with the distribution.
50
 * 3. All advertising materials mentioning features or use of this software
51
 *    must display the following acknowledgement:
52
 *      This product includes software developed by the University of
53
 *      California, Berkeley and its contributors.
54
 * 4. Neither the name of the University nor the names of its contributors
55
 *    may be used to endorse or promote products derived from this software
56
 *    without specific prior written permission.
57
 *
58
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68
 * SUCH DAMAGE.
69
 * -
70
 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
71
 *
72
 * Permission to use, copy, modify, and distribute this software for any
73
 * purpose with or without fee is hereby granted, provided that the above
74
 * copyright notice and this permission notice appear in all copies, and that
75
 * the name of Digital Equipment Corporation not be used in advertising or
76
 * publicity pertaining to distribution of the document or software without
77
 * specific, written prior permission.
78
 *
79
 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
80
 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
81
 * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
82
 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
83
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
84
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
85
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
86
 * SOFTWARE.
87
 * -
88
 * --Copyright--
89
 */
90
 
91
#if defined(LIBC_SCCS) && !defined(lint)
92
#if 0
93
static char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93";
94
static char rcsid[] = "$From: inet_addr.c,v 8.5 1996/08/05 08:31:35 vixie Exp $";
95
#else
96
static char rcsid[] = "$OpenBSD: inet_addr.c,v 1.6 1999/05/03 22:31:14 yanick Exp $";
97
#endif
98
#endif /* LIBC_SCCS and not lint */
99
 
100
#ifdef __ECOS
101
#include <pkgconf/net.h>
102
#undef _KERNEL
103
#include <sys/param.h>
104
#include <sys/socket.h>
105
#include <sys/ioctl.h>
106
#include <errno.h>
107
 
108
#include <net/if.h>
109
#include <netinet/in.h>
110
#else
111
#include <sys/types.h>
112
#include <sys/param.h>
113
#include <netinet/in.h>
114
#endif
115
#include <arpa/inet.h>
116
#include <ctype.h>
117
 
118
#ifdef __ECOS
119
#define isascii(c) (((unsigned char)(c) >= 0x20) && ((unsigned char)(c) <= 0x7F))
120
#endif
121
 
122
/*
123
 * Ascii internet address interpretation routine.
124
 * The value returned is in network order.
125
 */
126
in_addr_t
127
inet_addr(const char *cp)
128
{
129
        struct in_addr val;
130
 
131
        if (inet_aton(cp, &val))
132
                return (val.s_addr);
133
        return (INADDR_NONE);
134
}
135
 
136
/*
137
 * Check whether "cp" is a valid ascii representation
138
 * of an Internet address and convert to a binary address.
139
 * Returns 1 if the address is valid, 0 if not.
140
 * This replaces inet_addr, the return value from which
141
 * cannot distinguish between failure and a local broadcast address.
142
 */
143
int
144
inet_aton(const char *cp, struct in_addr *addr)
145
{
146
        register in_addr_t val;
147
        register int base, n;
148
        register char c;
149
        u_int parts[4];
150
        register u_int *pp = parts;
151
 
152
        c = *cp;
153
        for (;;) {
154
                /*
155
                 * Collect number up to ``.''.
156
                 * Values are specified as for C:
157
                 * 0x=hex, 0=octal, isdigit=decimal.
158
                 */
159
                if (!isdigit(c))
160
                        return (0);
161
                val = 0; base = 10;
162
                if (c == '0') {
163
                        c = *++cp;
164
                        if (c == 'x' || c == 'X')
165
                                base = 16, c = *++cp;
166
                        else
167
                                base = 8;
168
                }
169
                for (;;) {
170
                        if (isascii(c) && isdigit(c)) {
171
                                val = (val * base) + (c - '0');
172
                                c = *++cp;
173
                        } else if (base == 16 && isascii(c) && isxdigit(c)) {
174
                                val = (val << 4) |
175
                                        (c + 10 - (islower(c) ? 'a' : 'A'));
176
                                c = *++cp;
177
                        } else
178
                                break;
179
                }
180
                if (c == '.') {
181
                        /*
182
                         * Internet format:
183
                         *      a.b.c.d
184
                         *      a.b.c   (with c treated as 16 bits)
185
                         *      a.b     (with b treated as 24 bits)
186
                         */
187
                        if (pp >= parts + 3)
188
                                return (0);
189
                        *pp++ = val;
190
                        c = *++cp;
191
                } else
192
                        break;
193
        }
194
        /*
195
         * Check for trailing characters.
196
         */
197
        if (c != '\0' && (!isascii(c) || !isspace(c)))
198
                return (0);
199
        /*
200
         * Concoct the address according to
201
         * the number of parts specified.
202
         */
203
        n = pp - parts + 1;
204
        switch (n) {
205
 
206
        case 0:
207
                return (0);              /* initial nondigit */
208
 
209
        case 1:                         /* a -- 32 bits */
210
                break;
211
 
212
        case 2:                         /* a.b -- 8.24 bits */
213
                if ((val > 0xffffff) || (parts[0] > 0xff))
214
                        return (0);
215
                val |= parts[0] << 24;
216
                break;
217
 
218
        case 3:                         /* a.b.c -- 8.8.16 bits */
219
                if ((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff))
220
                        return (0);
221
                val |= (parts[0] << 24) | (parts[1] << 16);
222
                break;
223
 
224
        case 4:                         /* a.b.c.d -- 8.8.8.8 bits */
225
                if ((val > 0xff) || (parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff))
226
                        return (0);
227
                val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
228
                break;
229
        }
230
        if (addr)
231
                addr->s_addr = htonl(val);
232
        return (1);
233
}

powered by: WebSVN 2.1.0

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