51ff13b8c0ec431fdbe6a2c2750eb09ea3301212
[oweals/busybox.git] / networking / libiproute / ll_addr.c
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 }