[1765] | 1 | /* vi: set sw=4 ts=4: */
|
---|
[821] | 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 |
|
---|
| 13 | #include <net/if_arp.h>
|
---|
| 14 |
|
---|
[1765] | 15 | #include "libbb.h"
|
---|
[821] | 16 | #include "rt_names.h"
|
---|
| 17 | #include "utils.h"
|
---|
| 18 |
|
---|
| 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) {
|
---|
[1765] | 32 | snprintf(buf+l, blen, ":%02x"+1, addr[i]);
|
---|
[821] | 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)) {
|
---|
[1765] | 49 | bb_error_msg("\"%s\" is invalid lladdr", arg);
|
---|
[821] | 50 | return -1;
|
---|
| 51 | }
|
---|
| 52 | if (len < 4) {
|
---|
| 53 | return -1;
|
---|
| 54 | }
|
---|
| 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 | }
|
---|
| 67 | if (sscanf(arg, "%x", &temp) != 1) {
|
---|
[1765] | 68 | bb_error_msg("\"%s\" is invalid lladdr", arg);
|
---|
[821] | 69 | return -1;
|
---|
| 70 | }
|
---|
| 71 | if (temp < 0 || temp > 255) {
|
---|
[1765] | 72 | bb_error_msg("\"%s\" is invalid lladdr", arg);
|
---|
[821] | 73 | return -1;
|
---|
| 74 | }
|
---|
| 75 | lladdr[i] = temp;
|
---|
| 76 | if (!cp) {
|
---|
| 77 | break;
|
---|
| 78 | }
|
---|
| 79 | arg = cp;
|
---|
| 80 | }
|
---|
| 81 | return i+1;
|
---|
| 82 | }
|
---|
| 83 | }
|
---|