Missing dependency spotted by Robert P Day.
[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 <arpa/inet.h>
13 #include <string.h>
14 #include <net/if_arp.h>
15
16 #include "rt_names.h"
17 #include "utils.h"
18
19 #include "libbb.h"
20
21 const char *ll_addr_n2a(unsigned char *addr, int alen, int type, char *buf, int blen)
22 {
23         int i;
24         int l;
25
26         if (alen == 4 &&
27             (type == ARPHRD_TUNNEL || type == ARPHRD_SIT || type == ARPHRD_IPGRE)) {
28                 return inet_ntop(AF_INET, addr, buf, blen);
29         }
30         l = 0;
31         for (i=0; i<alen; i++) {
32                 if (i==0) {
33                         snprintf(buf+l, blen, "%02x", addr[i]);
34                         blen -= 2;
35                         l += 2;
36                 } else {
37                         snprintf(buf+l, blen, ":%02x", addr[i]);
38                         blen -= 3;
39                         l += 3;
40                 }
41         }
42         return buf;
43 }
44
45 int ll_addr_a2n(unsigned char *lladdr, int len, char *arg)
46 {
47         if (strchr(arg, '.')) {
48                 inet_prefix pfx;
49                 if (get_addr_1(&pfx, arg, AF_INET)) {
50                         bb_error_msg("\"%s\" is invalid lladdr.", arg);
51                         return -1;
52                 }
53                 if (len < 4) {
54                         return -1;
55                 }
56                 memcpy(lladdr, pfx.data, 4);
57                 return 4;
58         } else {
59                 int i;
60
61                 for (i=0; i<len; i++) {
62                         int temp;
63                         char *cp = strchr(arg, ':');
64                         if (cp) {
65                                 *cp = 0;
66                                 cp++;
67                         }
68                         if (sscanf(arg, "%x", &temp) != 1) {
69                                 bb_error_msg("\"%s\" is invalid lladdr.", arg);
70                                 return -1;
71                         }
72                         if (temp < 0 || temp > 255) {
73                                 bb_error_msg("\"%s\" is invalid lladdr.", arg);
74                                 return -1;
75                         }
76                         lladdr[i] = temp;
77                         if (!cp) {
78                                 break;
79                         }
80                         arg = cp;
81                 }
82                 return i+1;
83         }
84 }