diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2002-11-10 01:33:55 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2002-11-10 01:33:55 +0000 |
commit | 9a2d27249cc2235f7e001a9ea8d4605406bc5f38 (patch) | |
tree | b7b2917c3cf46ac3fa25df5f9a27a9a9fbfb0398 /networking/libiproute/ll_addr.c | |
parent | 021fa7db9139bff3b4bf404dfd7d2b1541ed71f8 (diff) | |
download | busybox-w32-9a2d27249cc2235f7e001a9ea8d4605406bc5f38.tar.gz busybox-w32-9a2d27249cc2235f7e001a9ea8d4605406bc5f38.tar.bz2 busybox-w32-9a2d27249cc2235f7e001a9ea8d4605406bc5f38.zip |
IP applet by Bastian Blank <waldi@debian.org>
Diffstat (limited to 'networking/libiproute/ll_addr.c')
-rw-r--r-- | networking/libiproute/ll_addr.c | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/networking/libiproute/ll_addr.c b/networking/libiproute/ll_addr.c new file mode 100644 index 000000000..51ff13b8c --- /dev/null +++ b/networking/libiproute/ll_addr.c | |||
@@ -0,0 +1,92 @@ | |||
1 | /* | ||
2 | * ll_addr.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 | #include <stdio.h> | ||
13 | #include <stdlib.h> | ||
14 | #include <unistd.h> | ||
15 | #include <syslog.h> | ||
16 | #include <fcntl.h> | ||
17 | #include <sys/ioctl.h> | ||
18 | #include <sys/socket.h> | ||
19 | #include <sys/ioctl.h> | ||
20 | #include <netinet/in.h> | ||
21 | #include <arpa/inet.h> | ||
22 | #include <string.h> | ||
23 | |||
24 | #include <linux/netdevice.h> | ||
25 | #include <linux/if_arp.h> | ||
26 | #include <linux/sockios.h> | ||
27 | |||
28 | #include "utils.h" | ||
29 | |||
30 | |||
31 | const char *ll_addr_n2a(unsigned char *addr, int alen, int type, char *buf, int blen) | ||
32 | { | ||
33 | int i; | ||
34 | int l; | ||
35 | |||
36 | if (alen == 4 && | ||
37 | (type == ARPHRD_TUNNEL || type == ARPHRD_SIT || type == ARPHRD_IPGRE)) { | ||
38 | return inet_ntop(AF_INET, addr, buf, blen); | ||
39 | } | ||
40 | l = 0; | ||
41 | for (i=0; i<alen; i++) { | ||
42 | if (i==0) { | ||
43 | snprintf(buf+l, blen, "%02x", addr[i]); | ||
44 | blen -= 2; | ||
45 | l += 2; | ||
46 | } else { | ||
47 | snprintf(buf+l, blen, ":%02x", addr[i]); | ||
48 | blen -= 3; | ||
49 | l += 3; | ||
50 | } | ||
51 | } | ||
52 | return buf; | ||
53 | } | ||
54 | |||
55 | int ll_addr_a2n(unsigned char *lladdr, int len, char *arg) | ||
56 | { | ||
57 | if (strchr(arg, '.')) { | ||
58 | inet_prefix pfx; | ||
59 | if (get_addr_1(&pfx, arg, AF_INET)) { | ||
60 | fprintf(stderr, "\"%s\" is invalid lladdr.\n", arg); | ||
61 | return -1; | ||
62 | } | ||
63 | if (len < 4) | ||
64 | return -1; | ||
65 | memcpy(lladdr, pfx.data, 4); | ||
66 | return 4; | ||
67 | } else { | ||
68 | int i; | ||
69 | |||
70 | for (i=0; i<len; i++) { | ||
71 | int temp; | ||
72 | char *cp = strchr(arg, ':'); | ||
73 | if (cp) { | ||
74 | *cp = 0; | ||
75 | cp++; | ||
76 | } | ||
77 | if (sscanf(arg, "%x", &temp) != 1) { | ||
78 | fprintf(stderr, "\"%s\" is invalid lladdr.\n", arg); | ||
79 | return -1; | ||
80 | } | ||
81 | if (temp < 0 || temp > 255) { | ||
82 | fprintf(stderr, "\"%s\" is invalid lladdr.\n", arg); | ||
83 | return -1; | ||
84 | } | ||
85 | lladdr[i] = temp; | ||
86 | if (!cp) | ||
87 | break; | ||
88 | arg = cp; | ||
89 | } | ||
90 | return i+1; | ||
91 | } | ||
92 | } | ||