2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2002-11-10 07:03:55 +05:30
|
|
|
/*
|
|
|
|
* ll_addr.c
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
|
|
|
|
*/
|
|
|
|
|
2003-01-14 14:24:08 +05:30
|
|
|
#include <net/if_arp.h>
|
2006-04-03 02:44:19 +05:30
|
|
|
|
2007-06-01 04:12:12 +05:30
|
|
|
#include "libbb.h"
|
2006-04-03 02:44:19 +05:30
|
|
|
#include "rt_names.h"
|
2002-11-10 07:03:55 +05:30
|
|
|
#include "utils.h"
|
2006-04-03 02:44:19 +05:30
|
|
|
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2009-10-13 21:28:24 +05:30
|
|
|
const char* FAST_FUNC ll_addr_n2a(unsigned char *addr, int alen, int type, char *buf, int blen)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int l;
|
|
|
|
|
|
|
|
if (alen == 4 &&
|
|
|
|
(type == ARPHRD_TUNNEL || type == ARPHRD_SIT || type == ARPHRD_IPGRE)) {
|
|
|
|
return inet_ntop(AF_INET, addr, buf, blen);
|
|
|
|
}
|
|
|
|
l = 0;
|
2009-10-13 21:28:24 +05:30
|
|
|
for (i = 0; i < alen; i++) {
|
|
|
|
if (i == 0) {
|
|
|
|
snprintf(buf + l, blen, ":%02x"+1, addr[i]);
|
2002-11-10 07:03:55 +05:30
|
|
|
blen -= 2;
|
|
|
|
l += 2;
|
|
|
|
} else {
|
2009-10-13 21:28:24 +05:30
|
|
|
snprintf(buf + l, blen, ":%02x", addr[i]);
|
2002-11-10 07:03:55 +05:30
|
|
|
blen -= 3;
|
|
|
|
l += 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2009-10-13 21:28:24 +05:30
|
|
|
int FAST_FUNC ll_addr_a2n(unsigned char *lladdr, int len, char *arg)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
2008-12-09 04:26:18 +05:30
|
|
|
int i;
|
|
|
|
|
2002-11-10 07:03:55 +05:30
|
|
|
if (strchr(arg, '.')) {
|
|
|
|
inet_prefix pfx;
|
|
|
|
if (get_addr_1(&pfx, arg, AF_INET)) {
|
2006-10-27 14:32:31 +05:30
|
|
|
bb_error_msg("\"%s\" is invalid lladdr", arg);
|
2002-11-10 07:03:55 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2002-11-28 18:09:19 +05:30
|
|
|
if (len < 4) {
|
2002-11-10 07:03:55 +05:30
|
|
|
return -1;
|
2002-11-28 18:09:19 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
memcpy(lladdr, pfx.data, 4);
|
|
|
|
return 4;
|
2008-12-09 04:26:18 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2008-12-09 04:26:18 +05:30
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
int temp;
|
|
|
|
char *cp = strchr(arg, ':');
|
|
|
|
if (cp) {
|
|
|
|
*cp = 0;
|
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
if (sscanf(arg, "%x", &temp) != 1 || (temp < 0 || temp > 255)) {
|
|
|
|
bb_error_msg("\"%s\" is invalid lladdr", arg);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
lladdr[i] = temp;
|
|
|
|
if (!cp) {
|
|
|
|
break;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2008-12-09 04:26:18 +05:30
|
|
|
arg = cp;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2008-12-09 04:26:18 +05:30
|
|
|
return i+1;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|