"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 | /* |
| 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 Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 13 | #include <net/if_arp.h> |
Bernhard Reutner-Fischer | 9a67ca3 | 2006-04-02 21:14:19 +0000 | [diff] [blame] | 14 | |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 15 | #include "libbb.h" |
Bernhard Reutner-Fischer | 9a67ca3 | 2006-04-02 21:14:19 +0000 | [diff] [blame] | 16 | #include "rt_names.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 17 | #include "utils.h" |
Bernhard Reutner-Fischer | 9a67ca3 | 2006-04-02 21:14:19 +0000 | [diff] [blame] | 18 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 19 | |
| 20 | const char *ll_addr_n2a(unsigned char *addr, int alen, int type, char *buf, int blen) |
| 21 | { |
| 22 | int i; |
| 23 | int l; |
| 24 | |
| 25 | if (alen == 4 && |
| 26 | (type == ARPHRD_TUNNEL || type == ARPHRD_SIT || type == ARPHRD_IPGRE)) { |
| 27 | return inet_ntop(AF_INET, addr, buf, blen); |
| 28 | } |
| 29 | l = 0; |
| 30 | for (i=0; i<alen; i++) { |
| 31 | if (i==0) { |
Denis Vlasenko | 3a34d0c | 2007-01-12 22:10:34 +0000 | [diff] [blame] | 32 | snprintf(buf+l, blen, ":%02x"+1, addr[i]); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 33 | blen -= 2; |
| 34 | l += 2; |
| 35 | } else { |
| 36 | snprintf(buf+l, blen, ":%02x", addr[i]); |
| 37 | blen -= 3; |
| 38 | l += 3; |
| 39 | } |
| 40 | } |
| 41 | return buf; |
| 42 | } |
| 43 | |
| 44 | int ll_addr_a2n(unsigned char *lladdr, int len, char *arg) |
| 45 | { |
| 46 | if (strchr(arg, '.')) { |
| 47 | inet_prefix pfx; |
| 48 | if (get_addr_1(&pfx, arg, AF_INET)) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 49 | bb_error_msg("\"%s\" is invalid lladdr", arg); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 50 | return -1; |
| 51 | } |
Glenn L McGrath | 1b0813a | 2002-11-28 12:39:19 +0000 | [diff] [blame] | 52 | if (len < 4) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 53 | return -1; |
Glenn L McGrath | 1b0813a | 2002-11-28 12:39:19 +0000 | [diff] [blame] | 54 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 55 | memcpy(lladdr, pfx.data, 4); |
| 56 | return 4; |
| 57 | } else { |
| 58 | int i; |
| 59 | |
| 60 | for (i=0; i<len; i++) { |
| 61 | int temp; |
| 62 | char *cp = strchr(arg, ':'); |
| 63 | if (cp) { |
| 64 | *cp = 0; |
| 65 | cp++; |
| 66 | } |
Bernhard Reutner-Fischer | b8635e2 | 2008-05-16 16:55:17 +0000 | [diff] [blame] | 67 | if (sscanf(arg, "%x", &temp) != 1 || (temp < 0 || temp > 255)) { |
Denis Vlasenko | d3d004d | 2006-10-27 09:02:31 +0000 | [diff] [blame] | 68 | bb_error_msg("\"%s\" is invalid lladdr", arg); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 69 | return -1; |
| 70 | } |
| 71 | lladdr[i] = temp; |
Glenn L McGrath | 1b0813a | 2002-11-28 12:39:19 +0000 | [diff] [blame] | 72 | if (!cp) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 73 | break; |
Glenn L McGrath | 1b0813a | 2002-11-28 12:39:19 +0000 | [diff] [blame] | 74 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 75 | arg = cp; |
| 76 | } |
| 77 | return i+1; |
| 78 | } |
| 79 | } |