1 #include <errno.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/socket.h>
5 #include <netinet/in.h>
6 
7 #include "utils.h"
8 
ipx_getnet(u_int32_t * net,const char * str)9 static int ipx_getnet(u_int32_t *net, const char *str)
10 {
11 	int i;
12 	u_int32_t tmp;
13 
14 	for(i = 0; *str && (i < 8); i++) {
15 
16 		if ((tmp = get_hex(*str)) == -1) {
17 			if (*str == '.')
18 				return 0;
19 			else
20 				return -1;
21 		}
22 
23 		str++;
24 		(*net) <<= 4;
25 		(*net) |= tmp;
26 	}
27 
28 	if (*str == 0)
29 		return 0;
30 
31 	return -1;
32 }
33 
ipx_getnode(u_int8_t * node,const char * str)34 static int ipx_getnode(u_int8_t *node, const char *str)
35 {
36 	int i;
37 	u_int32_t tmp;
38 
39 	for(i = 0; i < 6; i++) {
40 		if ((tmp = get_hex(*str++)) == -1)
41 			return -1;
42 		node[i] = (u_int8_t)tmp;
43 		node[i] <<= 4;
44 		if ((tmp = get_hex(*str++)) == -1)
45 			return -1;
46 		node[i] |= (u_int8_t)tmp;
47 		if (*str == ':')
48 			str++;
49 	}
50 
51 	return 0;
52 }
53 
ipx_pton1(const char * src,struct ipx_addr * addr)54 static int ipx_pton1(const char *src, struct ipx_addr *addr)
55 {
56 	char *sep = (char *)src;
57 	int no_node = 0;
58 
59 	memset(addr, 0, sizeof(struct ipx_addr));
60 
61 	while(*sep && (*sep != '.'))
62 		sep++;
63 
64 	if (*sep != '.')
65 		no_node = 1;
66 
67 	if (ipx_getnet(&addr->ipx_net, src))
68 		return 0;
69 
70 	addr->ipx_net = htonl(addr->ipx_net);
71 
72 	if (no_node)
73 		return 1;
74 
75 	if (ipx_getnode(addr->ipx_node, sep + 1))
76 		return 0;
77 
78 	return 1;
79 }
80 
ipx_pton(int af,const char * src,void * addr)81 int ipx_pton(int af, const char *src, void *addr)
82 {
83 	int err;
84 
85 	switch (af) {
86 	case AF_IPX:
87 		errno = 0;
88 		err = ipx_pton1(src, (struct ipx_addr *)addr);
89 		break;
90 	default:
91 		errno = EAFNOSUPPORT;
92 		err = -1;
93 	}
94 
95 	return err;
96 }
97