blob: c2c01305d070905de011f7f2c964b3a9e0736fa9 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00002/*
3 * ll_addr.c
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 *
10 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
11 */
12
Eric Andersenab4e19a2003-01-14 08:54:08 +000013#include <net/if_arp.h>
Bernhard Reutner-Fischer9a67ca32006-04-02 21:14:19 +000014
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000015#include "libbb.h"
Bernhard Reutner-Fischer9a67ca32006-04-02 21:14:19 +000016#include "rt_names.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000017#include "utils.h"
Bernhard Reutner-Fischer9a67ca32006-04-02 21:14:19 +000018
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000019
Denys Vlasenkod31575a2009-10-13 17:58:24 +020020const char* FAST_FUNC ll_addr_n2a(unsigned char *addr, int alen, int type, char *buf, int blen)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000021{
22 int i;
23 int l;
24
Denys Vlasenko6b9f1632010-01-28 02:24:24 +010025 if (alen == 4
26 && (type == ARPHRD_TUNNEL || type == ARPHRD_SIT || type == ARPHRD_IPGRE)
27 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000028 return inet_ntop(AF_INET, addr, buf, blen);
29 }
30 l = 0;
Denys Vlasenkod31575a2009-10-13 17:58:24 +020031 for (i = 0; i < alen; i++) {
32 if (i == 0) {
33 snprintf(buf + l, blen, ":%02x"+1, addr[i]);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000034 blen -= 2;
35 l += 2;
36 } else {
Denys Vlasenkod31575a2009-10-13 17:58:24 +020037 snprintf(buf + l, blen, ":%02x", addr[i]);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000038 blen -= 3;
39 l += 3;
40 }
41 }
42 return buf;
43}
44
Denys Vlasenkod31575a2009-10-13 17:58:24 +020045int FAST_FUNC ll_addr_a2n(unsigned char *lladdr, int len, char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000046{
Denis Vlasenkoefb545b2008-12-08 22:56:18 +000047 int i;
48
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000049 if (strchr(arg, '.')) {
50 inet_prefix pfx;
51 if (get_addr_1(&pfx, arg, AF_INET)) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +000052 bb_error_msg("\"%s\" is invalid lladdr", arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000053 return -1;
54 }
Glenn L McGrath1b0813a2002-11-28 12:39:19 +000055 if (len < 4) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000056 return -1;
Glenn L McGrath1b0813a2002-11-28 12:39:19 +000057 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000058 memcpy(lladdr, pfx.data, 4);
59 return 4;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000060 }
Denis Vlasenkoefb545b2008-12-08 22:56:18 +000061
62 for (i = 0; i < len; i++) {
63 int temp;
64 char *cp = strchr(arg, ':');
65 if (cp) {
66 *cp = 0;
67 cp++;
68 }
69 if (sscanf(arg, "%x", &temp) != 1 || (temp < 0 || temp > 255)) {
70 bb_error_msg("\"%s\" is invalid lladdr", arg);
71 return -1;
72 }
73 lladdr[i] = temp;
74 if (!cp) {
75 break;
76 }
77 arg = cp;
78 }
79 return i+1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000080}