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
hostapd_ip_txt(const struct hostapd_ip_addr * addr,char * buf,size_t buflen)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
hostapd_parse_ip_addr(const char * txt,struct hostapd_ip_addr * addr)36 int hostapd_parse_ip_addr(const char *txt, struct hostapd_ip_addr *addr)
37 {
38 #ifndef CONFIG_NATIVE_WINDOWS
39 if (inet_aton(txt, &addr->u.v4)) {
40 addr->af = AF_INET;
41 return 0;
42 }
43
44 #ifdef CONFIG_IPV6
45 if (inet_pton(AF_INET6, txt, &addr->u.v6) > 0) {
46 addr->af = AF_INET6;
47 return 0;
48 }
49 #endif /* CONFIG_IPV6 */
50 #endif /* CONFIG_NATIVE_WINDOWS */
51
52 return -1;
53 }
54
55
hostapd_ip_equal(const struct hostapd_ip_addr * a,const struct hostapd_ip_addr * b)56 bool hostapd_ip_equal(const struct hostapd_ip_addr *a,
57 const struct hostapd_ip_addr *b)
58 {
59 if (a->af != b->af)
60 return false;
61
62 if (a->af == AF_INET && a->u.v4.s_addr == b->u.v4.s_addr)
63 return true;
64
65 #ifdef CONFIG_IPV6
66 if (a->af == AF_INET6 &&
67 os_memcmp(&a->u.v6, &b->u.v6, sizeof(a->u.v6)) == 0)
68 return true;
69 #endif /* CONFIG_IPV6 */
70
71 return false;
72 }
73