]>
Commit | Line | Data |
---|---|---|
aba5acdf SH |
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> | |
aba5acdf SH |
15 | #include <fcntl.h> |
16 | #include <sys/ioctl.h> | |
17 | #include <sys/socket.h> | |
aba5acdf SH |
18 | #include <netinet/in.h> |
19 | #include <arpa/inet.h> | |
20 | #include <string.h> | |
21 | ||
ea7436fb SH |
22 | #include <linux/netdevice.h> |
23 | #include <linux/if_arp.h> | |
24 | #include <linux/sockios.h> | |
25 | ||
26 | #include "rt_names.h" | |
aba5acdf SH |
27 | #include "utils.h" |
28 | ||
07b20a61 SH |
29 | const char *ll_addr_n2a(const unsigned char *addr, int alen, int type, |
30 | char *buf, int blen) | |
aba5acdf SH |
31 | { |
32 | int i; | |
33 | int l; | |
34 | ||
35 | if (alen == 4 && | |
07b20a61 SH |
36 | (type == ARPHRD_TUNNEL || type == ARPHRD_SIT |
37 | || type == ARPHRD_IPGRE)) | |
aba5acdf | 38 | return inet_ntop(AF_INET, addr, buf, blen); |
07b20a61 SH |
39 | |
40 | if (alen == 16 && (type == ARPHRD_TUNNEL6 || type == ARPHRD_IP6GRE)) | |
0280ef85 | 41 | return inet_ntop(AF_INET6, addr, buf, blen); |
07b20a61 | 42 | |
f63ed3e6 PS |
43 | snprintf(buf, blen, "%02x", addr[0]); |
44 | for (i = 1, l = 2; i < alen && l < blen; i++, l += 3) | |
45 | snprintf(buf + l, blen - l, ":%02x", addr[i]); | |
aba5acdf SH |
46 | return buf; |
47 | } | |
48 | ||
f332d169 | 49 | /*NB: lladdr is char * (rather than u8 *) because sa_data is char * (1003.1g) */ |
46ac8a55 | 50 | int ll_addr_a2n(char *lladdr, int len, const char *arg) |
aba5acdf SH |
51 | { |
52 | if (strchr(arg, '.')) { | |
53 | inet_prefix pfx; | |
54 | if (get_addr_1(&pfx, arg, AF_INET)) { | |
55 | fprintf(stderr, "\"%s\" is invalid lladdr.\n", arg); | |
56 | return -1; | |
57 | } | |
58 | if (len < 4) | |
59 | return -1; | |
60 | memcpy(lladdr, pfx.data, 4); | |
61 | return 4; | |
62 | } else { | |
63 | int i; | |
64 | ||
07b20a61 | 65 | for (i = 0; i < len; i++) { |
aba5acdf SH |
66 | int temp; |
67 | char *cp = strchr(arg, ':'); | |
68 | if (cp) { | |
69 | *cp = 0; | |
70 | cp++; | |
71 | } | |
72 | if (sscanf(arg, "%x", &temp) != 1) { | |
07b20a61 SH |
73 | fprintf(stderr, "\"%s\" is invalid lladdr.\n", |
74 | arg); | |
aba5acdf SH |
75 | return -1; |
76 | } | |
77 | if (temp < 0 || temp > 255) { | |
07b20a61 SH |
78 | fprintf(stderr, "\"%s\" is invalid lladdr.\n", |
79 | arg); | |
aba5acdf SH |
80 | return -1; |
81 | } | |
82 | lladdr[i] = temp; | |
83 | if (!cp) | |
84 | break; | |
85 | arg = cp; | |
86 | } | |
07b20a61 | 87 | return i + 1; |
aba5acdf SH |
88 | } |
89 | } |