Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ll_addr.c |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 10 | */ |
| 11 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 12 | #include <arpa/inet.h> |
| 13 | #include <string.h> |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 14 | #include <net/if_arp.h> |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 15 | #include "utils.h" |
Glenn L McGrath | 1b0813a | 2002-11-28 12:39:19 +0000 | [diff] [blame] | 16 | #include "libbb.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 17 | |
| 18 | const char *ll_addr_n2a(unsigned char *addr, int alen, int type, char *buf, int blen) |
| 19 | { |
| 20 | int i; |
| 21 | int l; |
| 22 | |
| 23 | if (alen == 4 && |
| 24 | (type == ARPHRD_TUNNEL || type == ARPHRD_SIT || type == ARPHRD_IPGRE)) { |
| 25 | return inet_ntop(AF_INET, addr, buf, blen); |
| 26 | } |
| 27 | l = 0; |
| 28 | for (i=0; i<alen; i++) { |
| 29 | if (i==0) { |
| 30 | snprintf(buf+l, blen, "%02x", addr[i]); |
| 31 | blen -= 2; |
| 32 | l += 2; |
| 33 | } else { |
| 34 | snprintf(buf+l, blen, ":%02x", addr[i]); |
| 35 | blen -= 3; |
| 36 | l += 3; |
| 37 | } |
| 38 | } |
| 39 | return buf; |
| 40 | } |
| 41 | |
| 42 | int ll_addr_a2n(unsigned char *lladdr, int len, char *arg) |
| 43 | { |
| 44 | if (strchr(arg, '.')) { |
| 45 | inet_prefix pfx; |
| 46 | if (get_addr_1(&pfx, arg, AF_INET)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 47 | bb_error_msg("\"%s\" is invalid lladdr.", arg); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 48 | return -1; |
| 49 | } |
Glenn L McGrath | 1b0813a | 2002-11-28 12:39:19 +0000 | [diff] [blame] | 50 | if (len < 4) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 51 | return -1; |
Glenn L McGrath | 1b0813a | 2002-11-28 12:39:19 +0000 | [diff] [blame] | 52 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 53 | memcpy(lladdr, pfx.data, 4); |
| 54 | return 4; |
| 55 | } else { |
| 56 | int i; |
| 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) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 66 | bb_error_msg("\"%s\" is invalid lladdr.", arg); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 67 | return -1; |
| 68 | } |
| 69 | if (temp < 0 || temp > 255) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 70 | bb_error_msg("\"%s\" is invalid lladdr.", arg); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 71 | return -1; |
| 72 | } |
| 73 | lladdr[i] = temp; |
Glenn L McGrath | 1b0813a | 2002-11-28 12:39:19 +0000 | [diff] [blame] | 74 | if (!cp) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 75 | break; |
Glenn L McGrath | 1b0813a | 2002-11-28 12:39:19 +0000 | [diff] [blame] | 76 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 77 | arg = cp; |
| 78 | } |
| 79 | return i+1; |
| 80 | } |
| 81 | } |