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