blob: afdd81ffb81a115c3c8ad59ca21bca303e1459ce [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/*
Denys Vlasenkofb132e42010-10-29 11:46:52 +02003 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License
5 * as published by the Free Software Foundation; either version
6 * 2 of the License, or (at your option) any later version.
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00007 *
Denys Vlasenkofb132e42010-10-29 11:46:52 +02008 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00009 */
Eric Andersenab4e19a2003-01-14 08:54:08 +000010#include <net/if_arp.h>
Bernhard Reutner-Fischer9a67ca32006-04-02 21:14:19 +000011
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000012#include "libbb.h"
Bernhard Reutner-Fischer9a67ca32006-04-02 21:14:19 +000013#include "rt_names.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000014#include "utils.h"
Bernhard Reutner-Fischer9a67ca32006-04-02 21:14:19 +000015
Denys Vlasenkod31575a2009-10-13 17:58:24 +020016const 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 +000017{
18 int i;
19 int l;
20
Denys Vlasenko6b9f1632010-01-28 02:24:24 +010021 if (alen == 4
22 && (type == ARPHRD_TUNNEL || type == ARPHRD_SIT || type == ARPHRD_IPGRE)
23 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000024 return inet_ntop(AF_INET, addr, buf, blen);
25 }
26 l = 0;
Denys Vlasenkod31575a2009-10-13 17:58:24 +020027 for (i = 0; i < alen; i++) {
28 if (i == 0) {
29 snprintf(buf + l, blen, ":%02x"+1, addr[i]);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000030 blen -= 2;
31 l += 2;
32 } else {
Denys Vlasenkod31575a2009-10-13 17:58:24 +020033 snprintf(buf + l, blen, ":%02x", addr[i]);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000034 blen -= 3;
35 l += 3;
36 }
37 }
38 return buf;
39}
40
Denys Vlasenkod31575a2009-10-13 17:58:24 +020041int FAST_FUNC ll_addr_a2n(unsigned char *lladdr, int len, char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000042{
Denis Vlasenkoefb545b2008-12-08 22:56:18 +000043 int i;
44
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000045 if (strchr(arg, '.')) {
46 inet_prefix pfx;
47 if (get_addr_1(&pfx, arg, AF_INET)) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +000048 bb_error_msg("\"%s\" is invalid lladdr", arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000049 return -1;
50 }
Glenn L McGrath1b0813a2002-11-28 12:39:19 +000051 if (len < 4) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000052 return -1;
Glenn L McGrath1b0813a2002-11-28 12:39:19 +000053 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000054 memcpy(lladdr, pfx.data, 4);
55 return 4;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000056 }
Denis Vlasenkoefb545b2008-12-08 22:56:18 +000057
58 for (i = 0; i < len; i++) {
59 int temp;
60 char *cp = strchr(arg, ':');
61 if (cp) {
62 *cp = 0;
63 cp++;
64 }
65 if (sscanf(arg, "%x", &temp) != 1 || (temp < 0 || temp > 255)) {
66 bb_error_msg("\"%s\" is invalid lladdr", arg);
67 return -1;
68 }
69 lladdr[i] = temp;
70 if (!cp) {
71 break;
72 }
73 arg = cp;
74 }
75 return i+1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000076}