blob: 33a54ea6ca2751bf06bdae198c58e985d75fd4ea [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 */
10
Eric Andersenab4e19a2003-01-14 08:54:08 +000011#include <net/if_arp.h>
Bernhard Reutner-Fischer9a67ca32006-04-02 21:14:19 +000012
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000013#include "libbb.h"
Bernhard Reutner-Fischer9a67ca32006-04-02 21:14:19 +000014#include "rt_names.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000015#include "utils.h"
Bernhard Reutner-Fischer9a67ca32006-04-02 21:14:19 +000016
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000017
Denys Vlasenkod31575a2009-10-13 17:58:24 +020018const 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 +000019{
20 int i;
21 int l;
22
Denys Vlasenko6b9f1632010-01-28 02:24:24 +010023 if (alen == 4
24 && (type == ARPHRD_TUNNEL || type == ARPHRD_SIT || type == ARPHRD_IPGRE)
25 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000026 return inet_ntop(AF_INET, addr, buf, blen);
27 }
28 l = 0;
Denys Vlasenkod31575a2009-10-13 17:58:24 +020029 for (i = 0; i < alen; i++) {
30 if (i == 0) {
31 snprintf(buf + l, blen, ":%02x"+1, addr[i]);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000032 blen -= 2;
33 l += 2;
34 } else {
Denys Vlasenkod31575a2009-10-13 17:58:24 +020035 snprintf(buf + l, blen, ":%02x", addr[i]);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000036 blen -= 3;
37 l += 3;
38 }
39 }
40 return buf;
41}
42
Denys Vlasenkod31575a2009-10-13 17:58:24 +020043int FAST_FUNC ll_addr_a2n(unsigned char *lladdr, int len, char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000044{
Denis Vlasenkoefb545b2008-12-08 22:56:18 +000045 int i;
46
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000047 if (strchr(arg, '.')) {
48 inet_prefix pfx;
49 if (get_addr_1(&pfx, arg, AF_INET)) {
Denis Vlasenkod3d004d2006-10-27 09:02:31 +000050 bb_error_msg("\"%s\" is invalid lladdr", arg);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000051 return -1;
52 }
Glenn L McGrath1b0813a2002-11-28 12:39:19 +000053 if (len < 4) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000054 return -1;
Glenn L McGrath1b0813a2002-11-28 12:39:19 +000055 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000056 memcpy(lladdr, pfx.data, 4);
57 return 4;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000058 }
Denis Vlasenkoefb545b2008-12-08 22:56:18 +000059
60 for (i = 0; i < len; i++) {
61 int temp;
62 char *cp = strchr(arg, ':');
63 if (cp) {
64 *cp = 0;
65 cp++;
66 }
67 if (sscanf(arg, "%x", &temp) != 1 || (temp < 0 || temp > 255)) {
68 bb_error_msg("\"%s\" is invalid lladdr", arg);
69 return -1;
70 }
71 lladdr[i] = temp;
72 if (!cp) {
73 break;
74 }
75 arg = cp;
76 }
77 return i+1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000078}