aboutsummaryrefslogtreecommitdiff
path: root/lib/ipx_pton.c
blob: 3dca2713719ad2eaeb19698339693d0b4afaaa8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include "utils.h"

static u_int32_t hexget(char c)
{
	if (c >= 'A' && c <= 'F')
		return c - 'A' + 10;
	if (c >= 'a' && c <= 'f')
		return c - 'a' + 10;
	if (c >= '0' && c <= '9')
		return c - '0';

	return 0xf0;
}

static int ipx_getnet(u_int32_t *net, const char *str)
{
	int i;
	u_int32_t tmp;

	for(i = 0; *str && (i < 8); i++) {

		if ((tmp = hexget(*str)) & 0xf0) {
			if (*str == '.')
				return 0;
			else
				return -1;
		}

		str++;
		(*net) <<= 4;
		(*net) |= tmp;
	}

	if (*str == 0)
		return 0;

	return -1;
}

static int ipx_getnode(u_int8_t *node, const char *str)
{
	int i;
	u_int32_t tmp;

	for(i = 0; i < 6; i++) {
		if ((tmp = hexget(*str++)) & 0xf0)
			return -1;
		node[i] = (u_int8_t)tmp;
		node[i] <<= 4;
		if ((tmp = hexget(*str++)) & 0xf0)
			return -1;
		node[i] |= (u_int8_t)tmp;
		if (*str == ':')
			str++;
	}

	return 0;
}

static int ipx_pton1(const char *src, struct ipx_addr *addr)
{
	char *sep = (char *)src;
	int no_node = 0;

	memset(addr, 0, sizeof(struct ipx_addr));

	while(*sep && (*sep != '.'))
		sep++;

	if (*sep != '.')
		no_node = 1;

	if (ipx_getnet(&addr->ipx_net, src))
		return 0;

	addr->ipx_net = htonl(addr->ipx_net);

	if (no_node)
		return 1;

	if (ipx_getnode(addr->ipx_node, sep + 1))
		return 0;

	return 1;
}

int ipx_pton(int af, const char *src, void *addr)
{
	int err;

	switch (af) {
	case AF_IPX:
		errno = 0;
		err = ipx_pton1(src, (struct ipx_addr *)addr);
		break;
	default:
		errno = EAFNOSUPPORT;
		err = -1;
	}

	return err;
}