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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [userland/] [route/] [lib/] [util-ank.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 745 simons
/*
2
 * utils.c
3
 *
4
 *              This program is free software; you can redistribute it and/or
5
 *              modify it under the terms of the GNU General Public License
6
 *              as published by the Free Software Foundation; either version
7
 *              2 of the License, or (at your option) any later version.
8
 *
9
 * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10
 *
11
 *
12
 * Changes:
13
 *
14
 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
15
 */
16
 
17
#include <stdio.h>
18
#include <stdlib.h>
19
#include <unistd.h>
20
#include <syslog.h>
21
#include <fcntl.h>
22
#include <sys/socket.h>
23
#include <netinet/in.h>
24
#include <string.h>
25
#include <netdb.h>
26
#include <arpa/inet.h>
27
#include <resolv.h>
28
#ifdef __UCLINUX__
29
#include <limits.h>
30
#define  UING_MAX       (~0U)
31
#endif
32
 
33
#include "intl.h"
34
#include "util-ank.h"
35
 
36
#ifndef AF_INET6
37
#define AF_INET6        10
38
#endif
39
 
40
int scan_number(char *arg, unsigned *val)
41
{
42
        unsigned long res;
43
        char *ptr;
44
 
45
        if (!arg || !*arg)
46
                return -1;
47
        res = strtoul(arg, &ptr, 0);
48
        if (!ptr || ptr == arg || *ptr || res > UINT_MAX)
49
                return -1;
50
        *val = res;
51
        return 0;
52
}
53
 
54
int get_integer(int *val, char *arg, int base)
55
{
56
        long res;
57
        char *ptr;
58
 
59
        if (!arg || !*arg)
60
                return -1;
61
        res = strtol(arg, &ptr, base);
62
        if (!ptr || ptr == arg || *ptr || res > INT_MAX || res < INT_MIN)
63
                return -1;
64
        *val = res;
65
        return 0;
66
}
67
 
68
int get_unsigned(unsigned *val, char *arg, int base)
69
{
70
        unsigned long res;
71
        char *ptr;
72
 
73
        if (!arg || !*arg)
74
                return -1;
75
        res = strtoul(arg, &ptr, base);
76
        if (!ptr || ptr == arg || *ptr || res > UINT_MAX)
77
                return -1;
78
        *val = res;
79
        return 0;
80
}
81
 
82
int get_u32(__u32 *val, char *arg, int base)
83
{
84
        unsigned long res;
85
        char *ptr;
86
 
87
        if (!arg || !*arg)
88
                return -1;
89
        res = strtoul(arg, &ptr, base);
90
        if (!ptr || ptr == arg || *ptr || res > 0xFFFFFFFFUL)
91
                return -1;
92
        *val = res;
93
        return 0;
94
}
95
 
96
int get_u16(__u16 *val, char *arg, int base)
97
{
98
        unsigned long res;
99
        char *ptr;
100
 
101
        if (!arg || !*arg)
102
                return -1;
103
        res = strtoul(arg, &ptr, base);
104
        if (!ptr || ptr == arg || *ptr || res > 0xFFFF)
105
                return -1;
106
        *val = res;
107
        return 0;
108
}
109
 
110
int get_u8(__u8 *val, char *arg, int base)
111
{
112
        unsigned long res;
113
        char *ptr;
114
 
115
        if (!arg || !*arg)
116
                return -1;
117
        res = strtoul(arg, &ptr, base);
118
        if (!ptr || ptr == arg || *ptr || res > 0xFF)
119
                return -1;
120
        *val = res;
121
        return 0;
122
}
123
 
124
int get_s16(__s16 *val, char *arg, int base)
125
{
126
        long res;
127
        char *ptr;
128
 
129
        if (!arg || !*arg)
130
                return -1;
131
        res = strtol(arg, &ptr, base);
132
        if (!ptr || ptr == arg || *ptr || res > 0x7FFF || res < -0x8000)
133
                return -1;
134
        *val = res;
135
        return 0;
136
}
137
 
138
int get_s8(__s8 *val, char *arg, int base)
139
{
140
        long res;
141
        char *ptr;
142
 
143
        if (!arg || !*arg)
144
                return -1;
145
        res = strtol(arg, &ptr, base);
146
        if (!ptr || ptr == arg || *ptr || res > 0x7F || res < -0x80)
147
                return -1;
148
        *val = res;
149
        return 0;
150
}
151
 
152
int get_addr_1(inet_prefix *addr, char *name, int family)
153
{
154
        char *cp;
155
        unsigned char *ap = (unsigned char*)addr->data;
156
        int i;
157
 
158
        memset(addr, 0, sizeof(*addr));
159
 
160
        if (strcmp(name, "default") == 0 || strcmp(name, "any") == 0) {
161
                addr->family = family;
162
                addr->bytelen = (family == AF_INET6 ? 16 : 4);
163
                addr->bitlen = -1;
164
                return 0;
165
        }
166
 
167
        if (strchr(name, ':')) {
168
                addr->family = AF_INET6;
169
                if (family != AF_UNSPEC && family != AF_INET6)
170
                        return -1;
171
                if (inet_pton(AF_INET6, name, addr->data) <= 0)
172
                        return -1;
173
                addr->bytelen = 16;
174
                addr->bitlen = -1;
175
                return 0;
176
        }
177
 
178
        addr->family = AF_INET;
179
        if (family != AF_UNSPEC && family != AF_INET)
180
                return -1;
181
        addr->bytelen = 4;
182
        addr->bitlen = -1;
183
        for (cp=name, i=0; *cp; cp++) {
184
                if (*cp <= '9' && *cp >= '0') {
185
                        ap[i] = 10*ap[i] + (*cp-'0');
186
                        continue;
187
                }
188
                if (*cp == '.' && ++i <= 3)
189
                        continue;
190
                return -1;
191
        }
192
        return 0;
193
}
194
 
195
int get_prefix_1(inet_prefix *dst, char *arg, int family)
196
{
197
        int err;
198
        unsigned plen;
199
        char *slash;
200
 
201
        memset(dst, 0, sizeof(*dst));
202
 
203
        if (strcmp(arg, "default") == 0 || strcmp(arg, "any") == 0) {
204
                dst->family = family;
205
                dst->bytelen = 0;
206
                dst->bitlen = 0;
207
                return 0;
208
        }
209
 
210
        slash = strchr(arg, '/');
211
        if (slash)
212
                *slash = 0;
213
        err = get_addr_1(dst, arg, family);
214
        if (err == 0) {
215
                dst->bitlen = (dst->family == AF_INET6 ? 128 : 32);
216
                if (slash) {
217
                        if (scan_number(slash+1, &plen) || plen > dst->bitlen) {
218
                                err = -1;
219
                                goto done;
220
                        }
221
                        dst->bitlen = plen;
222
                }
223
        }
224
done:
225
        if (slash)
226
                *slash = '/';
227
        return err;
228
}
229
 
230
int get_addr(inet_prefix *dst, char *arg, int family)
231
{
232
        if (get_addr_1(dst, arg, family)) {
233
                fprintf(stderr, _("ip: %s is invalid inet address\n"), arg);
234
                exit(1);
235
        }
236
        return 0;
237
}
238
 
239
int get_prefix(inet_prefix *dst, char *arg, int family)
240
{
241
        if (get_prefix_1(dst, arg, family)) {
242
                fprintf(stderr, _("ip: %s is invalid inet prefix\n"), arg);
243
                exit(1);
244
        }
245
        return 0;
246
}
247
 
248
__u32 get_addr32(char *name)
249
{
250
        inet_prefix addr;
251
        if (get_addr_1(&addr, name, AF_INET)) {
252
                fprintf(stderr, _("ip: %s is invalid IPv4 address\n"), name);
253
                exit(1);
254
        }
255
        return addr.data[0];
256
}
257
 
258
void invarg(char *msg)
259
{
260
        fprintf(stderr, _("ip: argument is wrong: %s\n"), msg);
261
        exit(1);
262
}
263
 
264
int matches(char *cmd, char *pattern)
265
{
266
        int len = strlen(cmd);
267
        if (len > strlen(pattern))
268
                return -1;
269
        return memcmp(pattern, cmd, len);
270
}
271
 
272
int inet_addr_match(inet_prefix *a, inet_prefix *b, int bits)
273
{
274
        __u32 *a1 = a->data;
275
        __u32 *a2 = b->data;
276
        int words = bits >> 0x05;
277
 
278
        bits &= 0x1f;
279
 
280
        if (words)
281
                if (memcmp(a1, a2, words << 2))
282
                        return -1;
283
 
284
        if (bits) {
285
                __u32 w1, w2;
286
                __u32 mask;
287
 
288
                w1 = a1[words];
289
                w2 = a2[words];
290
 
291
                mask = htonl((0xffffffff) << (0x20 - bits));
292
 
293
                if ((w1 ^ w2) & mask)
294
                        return 1;
295
        }
296
 
297
        return 0;
298
}
299
 
300
const char *format_host(int af, void *addr, __u8 *abuf, int alen)
301
{
302
#ifdef RESOLVE_HOSTNAMES
303
        if (resolve_hosts) {
304
                int addrlen = 0;
305
                struct hostent *h_ent;
306
                switch (af) {
307
                case AF_INET:
308
                        addrlen = 4;
309
                        break;
310
                case AF_INET6:
311
                        addrlen = 16;
312
                        break;
313
                }
314
                if (addrlen &&
315
                    (h_ent = gethostbyaddr(addr, addrlen, af)) != NULL) {
316
                        snprintf(abuf, alen-1, "%s", h_ent->h_name);
317
                        return abuf;
318
                }
319
        }
320
#endif
321
        return inet_ntop(af, addr, abuf, alen);
322
}

powered by: WebSVN 2.1.0

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