osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +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 | |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <unistd.h> |
| 15 | #include <syslog.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <sys/ioctl.h> |
| 18 | #include <sys/socket.h> |
| 19 | #include <sys/ioctl.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 20 | #include <netinet/in.h> |
| 21 | #include <arpa/inet.h> |
| 22 | #include <string.h> |
| 23 | |
osdl.org!shemminger | ea7436f | 2004-06-09 22:53:56 +0000 | [diff] [blame] | 24 | #include <linux/netdevice.h> |
| 25 | #include <linux/if_arp.h> |
| 26 | #include <linux/sockios.h> |
| 27 | |
| 28 | #include "rt_names.h" |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 29 | #include "utils.h" |
| 30 | |
| 31 | |
Eric W. Biederman | 71b4d59 | 2015-03-15 14:49:10 -0500 | [diff] [blame] | 32 | const char *ll_addr_n2a(const unsigned char *addr, int alen, int type, char *buf, int blen) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 33 | { |
| 34 | int i; |
| 35 | int l; |
| 36 | |
| 37 | if (alen == 4 && |
| 38 | (type == ARPHRD_TUNNEL || type == ARPHRD_SIT || type == ARPHRD_IPGRE)) { |
| 39 | return inet_ntop(AF_INET, addr, buf, blen); |
| 40 | } |
David Lamparter | 0280ef8 | 2007-06-09 15:53:58 +0200 | [diff] [blame] | 41 | if (alen == 16 && type == ARPHRD_TUNNEL6) { |
| 42 | return inet_ntop(AF_INET6, addr, buf, blen); |
| 43 | } |
Phil Sutter | f63ed3e | 2016-03-22 19:35:19 +0100 | [diff] [blame] | 44 | snprintf(buf, blen, "%02x", addr[0]); |
| 45 | for (i = 1, l = 2; i < alen && l < blen; i++, l += 3) |
| 46 | snprintf(buf + l, blen - l, ":%02x", addr[i]); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 47 | return buf; |
| 48 | } |
| 49 | |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 50 | /*NB: lladdr is char * (rather than u8 *) because sa_data is char * (1003.1g) */ |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 51 | int ll_addr_a2n(char *lladdr, int len, const char *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 52 | { |
| 53 | if (strchr(arg, '.')) { |
| 54 | inet_prefix pfx; |
| 55 | if (get_addr_1(&pfx, arg, AF_INET)) { |
| 56 | fprintf(stderr, "\"%s\" is invalid lladdr.\n", arg); |
| 57 | return -1; |
| 58 | } |
| 59 | if (len < 4) |
| 60 | return -1; |
| 61 | memcpy(lladdr, pfx.data, 4); |
| 62 | return 4; |
| 63 | } else { |
| 64 | int i; |
| 65 | |
| 66 | for (i=0; i<len; i++) { |
| 67 | int temp; |
| 68 | char *cp = strchr(arg, ':'); |
| 69 | if (cp) { |
| 70 | *cp = 0; |
| 71 | cp++; |
| 72 | } |
| 73 | if (sscanf(arg, "%x", &temp) != 1) { |
| 74 | fprintf(stderr, "\"%s\" is invalid lladdr.\n", arg); |
| 75 | return -1; |
| 76 | } |
| 77 | if (temp < 0 || temp > 255) { |
| 78 | fprintf(stderr, "\"%s\" is invalid lladdr.\n", arg); |
| 79 | return -1; |
| 80 | } |
| 81 | lladdr[i] = temp; |
| 82 | if (!cp) |
| 83 | break; |
| 84 | arg = cp; |
| 85 | } |
| 86 | return i+1; |
| 87 | } |
| 88 | } |