ip_addr.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. #include "includes.h"
  9. #include "common.h"
  10. #include "ip_addr.h"
  11. const char * hostapd_ip_txt(const struct hostapd_ip_addr *addr, char *buf,
  12. size_t buflen)
  13. {
  14. if (buflen == 0 || addr == NULL)
  15. return NULL;
  16. if (addr->af == AF_INET) {
  17. os_strlcpy(buf, inet_ntoa(addr->u.v4), buflen);
  18. } else {
  19. buf[0] = '\0';
  20. }
  21. #ifdef CONFIG_IPV6
  22. if (addr->af == AF_INET6) {
  23. if (inet_ntop(AF_INET6, &addr->u.v6, buf, buflen) == NULL)
  24. buf[0] = '\0';
  25. }
  26. #endif /* CONFIG_IPV6 */
  27. return buf;
  28. }
  29. int hostapd_parse_ip_addr(const char *txt, struct hostapd_ip_addr *addr)
  30. {
  31. #ifndef CONFIG_NATIVE_WINDOWS
  32. if (inet_aton(txt, &addr->u.v4)) {
  33. addr->af = AF_INET;
  34. return 0;
  35. }
  36. #ifdef CONFIG_IPV6
  37. if (inet_pton(AF_INET6, txt, &addr->u.v6) > 0) {
  38. addr->af = AF_INET6;
  39. return 0;
  40. }
  41. #endif /* CONFIG_IPV6 */
  42. #endif /* CONFIG_NATIVE_WINDOWS */
  43. return -1;
  44. }