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