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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [inet/] [addr.c] - Blame information for rev 1771

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

Line No. Rev Author Line
1 1325 phoenix
/* Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk>
2
 * This file is part of the Linux-8086 C library and is distributed
3
 * under the GNU Library General Public License.
4
 */
5
 
6
/*
7
 * Manuel Novoa III       Dec 2000
8
 *
9
 * Converted to use my new (un)signed long (long) to string routines, which
10
 * are smaller than the previous functions and don't require static buffers.
11
 * In the process, removed the reference to strcat and cut object size of
12
 * inet_ntoa in half (from 190 bytes down to 94).
13
 *
14
 * Manuel Novoa III       Feb 2002
15
 *
16
 * Changed to use _int10tostr.
17
 */
18
 
19
#define __FORCE_GLIBC
20
#include <features.h>
21
#define _STDIO_UTILITY                  /* For _int10tostr. */
22
#include <stdio.h>
23
#include <string.h>
24
#include <ctype.h>
25
#include <netinet/in.h>
26
 
27
int inet_aton(const char *cp, struct in_addr *addrptr);
28
 
29
#ifdef L_inet_aton
30
int inet_aton(cp, addrptr)
31
const char *cp;
32
struct in_addr *addrptr;
33
{
34
        in_addr_t addr;
35
        int value;
36
        int part;
37
 
38
        addr = 0;
39
        for (part = 1; part <= 4; part++) {
40
 
41
                if (!isdigit(*cp))
42
                        return 0;
43
 
44
                value = 0;
45
                while (isdigit(*cp)) {
46
                        value *= 10;
47
                        value += *cp++ - '0';
48
                        if (value > 255)
49
                                return 0;
50
                }
51
 
52
                if (part < 4) {
53
                        if (*cp++ != '.')
54
                                return 0;
55
                } else {
56
                        char c = *cp++;
57
                        if (c != '\0' && !isspace(c))
58
                        return 0;
59
                }
60
 
61
                addr <<= 8;
62
                addr |= value;
63
        }
64
 
65
        /*  W. Richard Stevens in his book UNIX Network Programming,
66
         *  Volume 1, second edition, on page 71 says:
67
         *
68
         *  An undocumented feature of inet_aton is that if addrptr is
69
         *  a null pointer, the function still performs it validation
70
         *  of the input string, but does not store the result.
71
         */
72
        if (addrptr) {
73
            addrptr->s_addr = htonl(addr);
74
        }
75
 
76
        return 1;
77
}
78
#endif
79
 
80
#ifdef L_inet_addr
81
in_addr_t inet_addr(const char *cp)
82
{
83
        struct in_addr a;
84
 
85
        if (!inet_aton(cp, &a))
86
                return INADDR_NONE;
87
        else
88
                return a.s_addr;
89
}
90
#endif
91
 
92
#ifdef L_inet_ntoa
93
 
94
#define INET_NTOA_MAX_LEN       16      /* max 12 digits + 3 '.'s + 1 nul */
95
 
96
char *inet_ntoa_r(struct in_addr in, char buf[INET_NTOA_MAX_LEN])
97
{
98
        in_addr_t addr = ntohl(in.s_addr);
99
        int i;
100
        char *p, *q;
101
 
102
        q = 0;
103
        p = buf + INET_NTOA_MAX_LEN - 1; /* cannot use sizeof(buf) here */
104
        for (i=0 ; i < 4 ; i++ ) {
105
                p = _int10tostr(p, addr & 0xff) - 1;
106
                addr >>= 8;
107
                if (q) {
108
                        *q = '.';
109
                }
110
                q = p;
111
        }
112
 
113
        return p+1;
114
}
115
 
116
char *inet_ntoa(struct in_addr in)
117
{
118
        static char buf[INET_NTOA_MAX_LEN];
119
        return(inet_ntoa_r(in, buf));
120
}
121
#endif
122
 
123
#ifdef L_inet_makeaddr
124
/*
125
 * Formulate an Internet address from network + host.  Used in
126
 * building addresses stored in the ifnet structure.
127
 */
128
struct in_addr inet_makeaddr(in_addr_t net, in_addr_t host)
129
{
130
        in_addr_t addr;
131
 
132
        if (net < 128)
133
                addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST);
134
        else if (net < 65536)
135
                addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST);
136
        else if (net < 16777216UL)
137
                addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST);
138
        else
139
                addr = net | host;
140
        addr = htonl(addr);
141
        return (*(struct in_addr *)&addr);
142
}
143
 
144
#endif
145
 
146
#ifdef L_inet_lnaof
147
/*
148
 * Return the local network address portion of an
149
 * internet address; handles class a/b/c network
150
 * number formats.
151
 */
152
in_addr_t inet_lnaof(struct in_addr in)
153
{
154
        in_addr_t i = ntohl(in.s_addr);
155
 
156
        if (IN_CLASSA(i))
157
                return ((i)&IN_CLASSA_HOST);
158
        else if (IN_CLASSB(i))
159
                return ((i)&IN_CLASSB_HOST);
160
        else
161
                return ((i)&IN_CLASSC_HOST);
162
}
163
#endif
164
 
165
#ifdef L_inet_netof
166
 
167
/*
168
 * Return the network number from an internet
169
 * address; handles class a/b/c network #'s.
170
 */
171
in_addr_t
172
inet_netof(struct in_addr in)
173
{
174
        in_addr_t i = ntohl(in.s_addr);
175
 
176
        if (IN_CLASSA(i))
177
                return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
178
        else if (IN_CLASSB(i))
179
                return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
180
        else
181
                return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
182
}
183
 
184
#endif

powered by: WebSVN 2.1.0

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