1189251Ssam/*
2189251Ssam * IP address processing
3189251Ssam * Copyright (c) 2003-2006, Jouni Malinen <j@w1.fi>
4189251Ssam *
5252726Srpaulo * This software may be distributed under the terms of the BSD license.
6252726Srpaulo * See README for more details.
7189251Ssam */
8189251Ssam
9189251Ssam#include "includes.h"
10189251Ssam
11189251Ssam#include "common.h"
12189251Ssam#include "ip_addr.h"
13189251Ssam
14189251Ssamconst char * hostapd_ip_txt(const struct hostapd_ip_addr *addr, char *buf,
15189251Ssam			    size_t buflen)
16189251Ssam{
17189251Ssam	if (buflen == 0 || addr == NULL)
18189251Ssam		return NULL;
19189251Ssam
20189251Ssam	if (addr->af == AF_INET) {
21189251Ssam		os_strlcpy(buf, inet_ntoa(addr->u.v4), buflen);
22189251Ssam	} else {
23189251Ssam		buf[0] = '\0';
24189251Ssam	}
25189251Ssam#ifdef CONFIG_IPV6
26189251Ssam	if (addr->af == AF_INET6) {
27189251Ssam		if (inet_ntop(AF_INET6, &addr->u.v6, buf, buflen) == NULL)
28189251Ssam			buf[0] = '\0';
29189251Ssam	}
30189251Ssam#endif /* CONFIG_IPV6 */
31189251Ssam
32189251Ssam	return buf;
33189251Ssam}
34189251Ssam
35189251Ssam
36189251Ssamint hostapd_ip_diff(struct hostapd_ip_addr *a, struct hostapd_ip_addr *b)
37189251Ssam{
38189251Ssam	if (a == NULL && b == NULL)
39189251Ssam		return 0;
40189251Ssam	if (a == NULL || b == NULL)
41189251Ssam		return 1;
42189251Ssam
43189251Ssam	switch (a->af) {
44189251Ssam	case AF_INET:
45189251Ssam		if (a->u.v4.s_addr != b->u.v4.s_addr)
46189251Ssam			return 1;
47189251Ssam		break;
48189251Ssam#ifdef CONFIG_IPV6
49189251Ssam	case AF_INET6:
50189251Ssam		if (os_memcmp(&a->u.v6, &b->u.v6, sizeof(a->u.v6)) != 0)
51189251Ssam			return 1;
52189251Ssam		break;
53189251Ssam#endif /* CONFIG_IPV6 */
54189251Ssam	}
55189251Ssam
56189251Ssam	return 0;
57189251Ssam}
58189251Ssam
59189251Ssam
60189251Ssamint hostapd_parse_ip_addr(const char *txt, struct hostapd_ip_addr *addr)
61189251Ssam{
62189251Ssam#ifndef CONFIG_NATIVE_WINDOWS
63189251Ssam	if (inet_aton(txt, &addr->u.v4)) {
64189251Ssam		addr->af = AF_INET;
65189251Ssam		return 0;
66189251Ssam	}
67189251Ssam
68189251Ssam#ifdef CONFIG_IPV6
69189251Ssam	if (inet_pton(AF_INET6, txt, &addr->u.v6) > 0) {
70189251Ssam		addr->af = AF_INET6;
71189251Ssam		return 0;
72189251Ssam	}
73189251Ssam#endif /* CONFIG_IPV6 */
74189251Ssam#endif /* CONFIG_NATIVE_WINDOWS */
75189251Ssam
76189251Ssam	return -1;
77189251Ssam}
78