xref: /netbsd-src/external/bsd/wpa/dist/src/utils/ip_addr.c (revision bb6183629cf165db498d8e1f4e2de129f7efb21c)
18dbcf02cSchristos /*
28dbcf02cSchristos  * IP address processing
38dbcf02cSchristos  * Copyright (c) 2003-2006, Jouni Malinen <j@w1.fi>
48dbcf02cSchristos  *
5e604d861Schristos  * This software may be distributed under the terms of the BSD license.
6e604d861Schristos  * See README for more details.
78dbcf02cSchristos  */
88dbcf02cSchristos 
98dbcf02cSchristos #include "includes.h"
108dbcf02cSchristos 
118dbcf02cSchristos #include "common.h"
128dbcf02cSchristos #include "ip_addr.h"
138dbcf02cSchristos 
148dbcf02cSchristos const char * hostapd_ip_txt(const struct hostapd_ip_addr *addr, char *buf,
158dbcf02cSchristos 			    size_t buflen)
168dbcf02cSchristos {
178dbcf02cSchristos 	if (buflen == 0 || addr == NULL)
188dbcf02cSchristos 		return NULL;
198dbcf02cSchristos 
208dbcf02cSchristos 	if (addr->af == AF_INET) {
218dbcf02cSchristos 		os_strlcpy(buf, inet_ntoa(addr->u.v4), buflen);
228dbcf02cSchristos 	} else {
238dbcf02cSchristos 		buf[0] = '\0';
248dbcf02cSchristos 	}
258dbcf02cSchristos #ifdef CONFIG_IPV6
268dbcf02cSchristos 	if (addr->af == AF_INET6) {
278dbcf02cSchristos 		if (inet_ntop(AF_INET6, &addr->u.v6, buf, buflen) == NULL)
288dbcf02cSchristos 			buf[0] = '\0';
298dbcf02cSchristos 	}
308dbcf02cSchristos #endif /* CONFIG_IPV6 */
318dbcf02cSchristos 
328dbcf02cSchristos 	return buf;
338dbcf02cSchristos }
348dbcf02cSchristos 
358dbcf02cSchristos 
368dbcf02cSchristos int hostapd_parse_ip_addr(const char *txt, struct hostapd_ip_addr *addr)
378dbcf02cSchristos {
388dbcf02cSchristos #ifndef CONFIG_NATIVE_WINDOWS
398dbcf02cSchristos 	if (inet_aton(txt, &addr->u.v4)) {
408dbcf02cSchristos 		addr->af = AF_INET;
418dbcf02cSchristos 		return 0;
428dbcf02cSchristos 	}
438dbcf02cSchristos 
448dbcf02cSchristos #ifdef CONFIG_IPV6
458dbcf02cSchristos 	if (inet_pton(AF_INET6, txt, &addr->u.v6) > 0) {
468dbcf02cSchristos 		addr->af = AF_INET6;
478dbcf02cSchristos 		return 0;
488dbcf02cSchristos 	}
498dbcf02cSchristos #endif /* CONFIG_IPV6 */
508dbcf02cSchristos #endif /* CONFIG_NATIVE_WINDOWS */
518dbcf02cSchristos 
528dbcf02cSchristos 	return -1;
538dbcf02cSchristos }
54*bb618362Schristos 
55*bb618362Schristos 
56*bb618362Schristos bool hostapd_ip_equal(const struct hostapd_ip_addr *a,
57*bb618362Schristos 		      const struct hostapd_ip_addr *b)
58*bb618362Schristos {
59*bb618362Schristos 	if (a->af != b->af)
60*bb618362Schristos 		return false;
61*bb618362Schristos 
62*bb618362Schristos 	if (a->af == AF_INET && a->u.v4.s_addr == b->u.v4.s_addr)
63*bb618362Schristos 		return true;
64*bb618362Schristos 
65*bb618362Schristos #ifdef CONFIG_IPV6
66*bb618362Schristos 	if (a->af == AF_INET6 &&
67*bb618362Schristos 	    os_memcmp(&a->u.v6, &b->u.v6, sizeof(a->u.v6)) == 0)
68*bb618362Schristos 		return true;
69*bb618362Schristos #endif /* CONFIG_IPV6 */
70*bb618362Schristos 
71*bb618362Schristos 	return false;
72*bb618362Schristos }
73