Tagging module iproute2 - iproute2-2.6.16-2
[iproute2.git] / lib / ipx_ntop.c
1 #include <errno.h>
2 #include <sys/types.h>
3 #include <netinet/in.h>
4
5 #include "utils.h"
6
7 static __inline__ int do_digit(char *str, u_int32_t addr, u_int32_t scale, size_t *pos, size_t len)
8 {
9         u_int32_t tmp = addr >> (scale * 4);
10
11         if (*pos == len)
12                 return 1;
13
14         tmp &= 0x0f;
15         if (tmp > 9)
16                 *str = tmp + 'A' - 10;
17         else
18                 *str = tmp + '0';
19         (*pos)++;
20
21         return 0;
22 }
23
24 static const char *ipx_ntop1(const struct ipx_addr *addr, char *str, size_t len)
25 {
26         int i;
27         size_t pos = 0;
28
29         if (len == 0)
30                 return str;
31
32         for(i = 7; i >= 0; i--)
33                 if (do_digit(str + pos, ntohl(addr->ipx_net), i, &pos, len))
34                         return str;
35
36         if (pos == len)
37                 return str;
38
39         *(str + pos) = '.';
40         pos++;
41         
42         for(i = 0; i < 6; i++) {
43                 if (do_digit(str + pos, addr->ipx_node[i], 1, &pos, len))
44                         return str;
45                 if (do_digit(str + pos, addr->ipx_node[i], 0, &pos, len))
46                         return str;
47         }
48
49         if (pos == len)
50                 return str;
51
52         *(str + pos) = 0;
53
54         return str;
55 }
56
57
58 const char *ipx_ntop(int af, const void *addr, char *str, size_t len)
59 {
60         switch(af) {
61                 case AF_IPX:
62                         errno = 0;
63                         return ipx_ntop1((struct ipx_addr *)addr, str, len);
64                 default:
65                         errno = EAFNOSUPPORT;
66         }
67
68         return NULL;
69 }
70
71