"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 2 | /* |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 3 | * 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 McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 7 | * |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 8 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 9 | */ |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 10 | #include <net/if_arp.h> |
Bernhard Reutner-Fischer | 9a67ca3 | 2006-04-02 21:14:19 +0000 | [diff] [blame] | 11 | |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 12 | #include "libbb.h" |
Bernhard Reutner-Fischer | 9a67ca3 | 2006-04-02 21:14:19 +0000 | [diff] [blame] | 13 | #include "rt_names.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 14 | #include "utils.h" |
Bernhard Reutner-Fischer | 9a67ca3 | 2006-04-02 21:14:19 +0000 | [diff] [blame] | 15 | |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 16 | const char* FAST_FUNC ll_addr_n2a(unsigned char *addr, int alen, int type, char *buf, int blen) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 17 | { |
| 18 | int i; |
| 19 | int l; |
| 20 | |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 21 | if (alen == 4 |
| 22 | && (type == ARPHRD_TUNNEL || type == ARPHRD_SIT || type == ARPHRD_IPGRE) |
| 23 | ) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 24 | return inet_ntop(AF_INET, addr, buf, blen); |
| 25 | } |
| 26 | l = 0; |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 27 | for (i = 0; i < alen; i++) { |
| 28 | if (i == 0) { |
| 29 | snprintf(buf + l, blen, ":%02x"+1, addr[i]); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 30 | blen -= 2; |
| 31 | l += 2; |
| 32 | } else { |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 33 | snprintf(buf + l, blen, ":%02x", addr[i]); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 34 | blen -= 3; |
| 35 | l += 3; |
| 36 | } |
| 37 | } |
| 38 | return buf; |
| 39 | } |
| 40 | |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 41 | int FAST_FUNC ll_addr_a2n(unsigned char *lladdr, int len, char *arg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 42 | { |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 43 | int i; |
| 44 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 45 | if (strchr(arg, '.')) { |
| 46 | inet_prefix pfx; |
| 47 | if (get_addr_1(&pfx, arg, AF_INET)) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 48 | bb_error_msg("\"%s\" is invalid lladdr", arg); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 49 | return -1; |
| 50 | } |
Glenn L McGrath | 1b0813a | 2002-11-28 12:39:19 +0000 | [diff] [blame] | 51 | if (len < 4) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 52 | return -1; |
Glenn L McGrath | 1b0813a | 2002-11-28 12:39:19 +0000 | [diff] [blame] | 53 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 54 | memcpy(lladdr, pfx.data, 4); |
| 55 | return 4; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 56 | } |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 57 | |
| 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 McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 76 | } |