ip_addr.c revision 8d520ff1dc2da35cdca849e982051b86468016d8
1/*
2 * IP address processing
3 * Copyright (c) 2003-2006, Jouni Malinen <j@w1.fi>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15#include "includes.h"
16
17#include "common.h"
18#include "ip_addr.h"
19
20const char * hostapd_ip_txt(const struct hostapd_ip_addr *addr, char *buf,
21			    size_t buflen)
22{
23	if (buflen == 0 || addr == NULL)
24		return NULL;
25
26	if (addr->af == AF_INET) {
27		os_strlcpy(buf, inet_ntoa(addr->u.v4), buflen);
28	} else {
29		buf[0] = '\0';
30	}
31#ifdef CONFIG_IPV6
32	if (addr->af == AF_INET6) {
33		if (inet_ntop(AF_INET6, &addr->u.v6, buf, buflen) == NULL)
34			buf[0] = '\0';
35	}
36#endif /* CONFIG_IPV6 */
37
38	return buf;
39}
40
41
42int hostapd_ip_diff(struct hostapd_ip_addr *a, struct hostapd_ip_addr *b)
43{
44	if (a == NULL && b == NULL)
45		return 0;
46	if (a == NULL || b == NULL)
47		return 1;
48
49	switch (a->af) {
50	case AF_INET:
51		if (a->u.v4.s_addr != b->u.v4.s_addr)
52			return 1;
53		break;
54#ifdef CONFIG_IPV6
55	case AF_INET6:
56		if (os_memcmp(&a->u.v6, &b->u.v6, sizeof(a->u.v6)) != 0)
57			return 1;
58		break;
59#endif /* CONFIG_IPV6 */
60	}
61
62	return 0;
63}
64
65
66int hostapd_parse_ip_addr(const char *txt, struct hostapd_ip_addr *addr)
67{
68#ifndef CONFIG_NATIVE_WINDOWS
69	if (inet_aton(txt, &addr->u.v4)) {
70		addr->af = AF_INET;
71		return 0;
72	}
73
74#ifdef CONFIG_IPV6
75	if (inet_pton(AF_INET6, txt, &addr->u.v6) > 0) {
76		addr->af = AF_INET6;
77		return 0;
78	}
79#endif /* CONFIG_IPV6 */
80#endif /* CONFIG_NATIVE_WINDOWS */
81
82	return -1;
83}
84