xref: /freebsd/contrib/wpa/src/utils/ip_addr.c (revision ca987d4641cdcd7f27e153db17c5bf064934faf5)
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_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