1
2
3
4
5
6
7
8
9
10
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <unistd.h>
15#include <fcntl.h>
16#include <sys/ioctl.h>
17#include <sys/socket.h>
18#include <netinet/in.h>
19#include <arpa/inet.h>
20#include <string.h>
21
22#include <linux/netdevice.h>
23#include <linux/if_arp.h>
24#include <linux/sockios.h>
25
26#include "rt_names.h"
27#include "utils.h"
28
29const char *ll_addr_n2a(const unsigned char *addr, int alen, int type,
30 char *buf, int blen)
31{
32 int i;
33 int l;
34
35 if (alen == 4 &&
36 (type == ARPHRD_TUNNEL || type == ARPHRD_SIT
37 || type == ARPHRD_IPGRE))
38 return inet_ntop(AF_INET, addr, buf, blen);
39
40 if (alen == 16 && (type == ARPHRD_TUNNEL6 || type == ARPHRD_IP6GRE))
41 return inet_ntop(AF_INET6, addr, buf, blen);
42 if (alen == 7 && type == ARPHRD_AX25)
43 return ax25_ntop(AF_AX25, addr, buf, blen);
44 if (alen == 7 && type == ARPHRD_NETROM)
45 return netrom_ntop(AF_NETROM, addr, buf, blen);
46 if (alen == 5 && type == ARPHRD_ROSE)
47 return rose_ntop(AF_ROSE, addr, buf, blen);
48
49 snprintf(buf, blen, "%02x", addr[0]);
50 for (i = 1, l = 2; i < alen && l < blen; i++, l += 3)
51 snprintf(buf + l, blen - l, ":%02x", addr[i]);
52 return buf;
53}
54
55
56int ll_addr_a2n(char *lladdr, int len, const char *arg)
57{
58 if (strchr(arg, '.')) {
59 inet_prefix pfx;
60 if (get_addr_1(&pfx, arg, AF_INET)) {
61 fprintf(stderr, "\"%s\" is invalid lladdr.\n", arg);
62 return -1;
63 }
64 if (len < 4)
65 return -1;
66 memcpy(lladdr, pfx.data, 4);
67 return 4;
68 } else {
69 int i;
70
71 for (i = 0; i < len; i++) {
72 int temp;
73 char *cp = strchr(arg, ':');
74 if (cp) {
75 *cp = 0;
76 cp++;
77 }
78 if (sscanf(arg, "%x", &temp) != 1) {
79 fprintf(stderr, "\"%s\" is invalid lladdr.\n",
80 arg);
81 return -1;
82 }
83 if (temp < 0 || temp > 255) {
84 fprintf(stderr, "\"%s\" is invalid lladdr.\n",
85 arg);
86 return -1;
87 }
88 lladdr[i] = temp;
89 if (!cp)
90 break;
91 arg = cp;
92 }
93 return i + 1;
94 }
95}
96