blob: 92a3590390331c8ef16b9df75e97f764e73874fa [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * IP address processing
3 * Copyright (c) 2003-2006, Jouni Malinen <j@w1.fi>
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "ip_addr.h"
13
14const 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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036int 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}