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