]> git.proxmox.com Git - mirror_iproute2.git/blob - lib/ll_addr.c
(Logical change 1.3)
[mirror_iproute2.git] / lib / ll_addr.c
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>
20 #include <linux/netdevice.h>
21 #include <linux/if_arp.h>
22 #include <linux/sockios.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <string.h>
26
27 #include "utils.h"
28
29
30 const char *ll_addr_n2a(unsigned char *addr, int alen, int type, char *buf, int blen)
31 {
32 int i;
33 int l;
34
35 if (alen == 4 &&
36 (type == ARPHRD_TUNNEL || type == ARPHRD_SIT || type == ARPHRD_IPGRE)) {
37 return inet_ntop(AF_INET, addr, buf, blen);
38 }
39 l = 0;
40 for (i=0; i<alen; i++) {
41 if (i==0) {
42 snprintf(buf+l, blen, "%02x", addr[i]);
43 blen -= 2;
44 l += 2;
45 } else {
46 snprintf(buf+l, blen, ":%02x", addr[i]);
47 blen -= 3;
48 l += 3;
49 }
50 }
51 return buf;
52 }
53
54 int ll_addr_a2n(unsigned char *lladdr, int len, char *arg)
55 {
56 if (strchr(arg, '.')) {
57 inet_prefix pfx;
58 if (get_addr_1(&pfx, arg, AF_INET)) {
59 fprintf(stderr, "\"%s\" is invalid lladdr.\n", arg);
60 return -1;
61 }
62 if (len < 4)
63 return -1;
64 memcpy(lladdr, pfx.data, 4);
65 return 4;
66 } else {
67 int i;
68
69 for (i=0; i<len; i++) {
70 int temp;
71 char *cp = strchr(arg, ':');
72 if (cp) {
73 *cp = 0;
74 cp++;
75 }
76 if (sscanf(arg, "%x", &temp) != 1) {
77 fprintf(stderr, "\"%s\" is invalid lladdr.\n", arg);
78 return -1;
79 }
80 if (temp < 0 || temp > 255) {
81 fprintf(stderr, "\"%s\" is invalid lladdr.\n", arg);
82 return -1;
83 }
84 lladdr[i] = temp;
85 if (!cp)
86 break;
87 arg = cp;
88 }
89 return i+1;
90 }
91 }