This commit was generated by cvs2svn to compensate for changes in r2587,
[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 <netinet/in.h>
21 #include <arpa/inet.h>
22 #include <string.h>
23
24 #include <linux/netdevice.h>
25 #include <linux/if_arp.h>
26 #include <linux/sockios.h>
27
28 #include "rt_names.h"
29 #include "utils.h"
30
31
32 const char *ll_addr_n2a(unsigned char *addr, int alen, int type, char *buf, int blen)
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         }
41         l = 0;
42         for (i=0; i<alen; i++) {
43                 if (i==0) {
44                         snprintf(buf+l, blen, "%02x", addr[i]);
45                         blen -= 2;
46                         l += 2;
47                 } else {
48                         snprintf(buf+l, blen, ":%02x", addr[i]);
49                         blen -= 3;
50                         l += 3;
51                 }
52         }
53         return buf;
54 }
55
56 /*NB: lladdr is char * (rather than u8 *) because sa_data is char * (1003.1g) */
57 int ll_addr_a2n(char *lladdr, int len, char *arg)
58 {
59         if (strchr(arg, '.')) {
60                 inet_prefix pfx;
61                 if (get_addr_1(&pfx, arg, AF_INET)) {
62                         fprintf(stderr, "\"%s\" is invalid lladdr.\n", arg);
63                         return -1;
64                 }
65                 if (len < 4)
66                         return -1;
67                 memcpy(lladdr, pfx.data, 4);
68                 return 4;
69         } else {
70                 int i;
71
72                 for (i=0; i<len; i++) {
73                         int temp;
74                         char *cp = strchr(arg, ':');
75                         if (cp) {
76                                 *cp = 0;
77                                 cp++;
78                         }
79                         if (sscanf(arg, "%x", &temp) != 1) {
80                                 fprintf(stderr, "\"%s\" is invalid lladdr.\n", arg);
81                                 return -1;
82                         }
83                         if (temp < 0 || temp > 255) {
84                                 fprintf(stderr, "\"%s\" is invalid lladdr.\n", arg);
85                                 return -1;
86                         }
87                         lladdr[i] = temp;
88                         if (!cp)
89                                 break;
90                         arg = cp;
91                 }
92                 return i+1;
93         }
94 }