ip_addr.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * IP address processing
  3. * Copyright (c) 2003-2006, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "ip_addr.h"
  17. const char * hostapd_ip_txt(const struct hostapd_ip_addr *addr, char *buf,
  18. size_t buflen)
  19. {
  20. if (buflen == 0 || addr == NULL)
  21. return NULL;
  22. if (addr->af == AF_INET) {
  23. os_strlcpy(buf, inet_ntoa(addr->u.v4), buflen);
  24. } else {
  25. buf[0] = '\0';
  26. }
  27. #ifdef CONFIG_IPV6
  28. if (addr->af == AF_INET6) {
  29. if (inet_ntop(AF_INET6, &addr->u.v6, buf, buflen) == NULL)
  30. buf[0] = '\0';
  31. }
  32. #endif /* CONFIG_IPV6 */
  33. return buf;
  34. }
  35. int hostapd_ip_diff(struct hostapd_ip_addr *a, struct hostapd_ip_addr *b)
  36. {
  37. if (a == NULL && b == NULL)
  38. return 0;
  39. if (a == NULL || b == NULL)
  40. return 1;
  41. switch (a->af) {
  42. case AF_INET:
  43. if (a->u.v4.s_addr != b->u.v4.s_addr)
  44. return 1;
  45. break;
  46. #ifdef CONFIG_IPV6
  47. case AF_INET6:
  48. if (os_memcmp(&a->u.v6, &b->u.v6, sizeof(a->u.v6)) != 0)
  49. return 1;
  50. break;
  51. #endif /* CONFIG_IPV6 */
  52. }
  53. return 0;
  54. }
  55. int hostapd_parse_ip_addr(const char *txt, struct hostapd_ip_addr *addr)
  56. {
  57. #ifndef CONFIG_NATIVE_WINDOWS
  58. if (inet_aton(txt, &addr->u.v4)) {
  59. addr->af = AF_INET;
  60. return 0;
  61. }
  62. #ifdef CONFIG_IPV6
  63. if (inet_pton(AF_INET6, txt, &addr->u.v6) > 0) {
  64. addr->af = AF_INET6;
  65. return 0;
  66. }
  67. #endif /* CONFIG_IPV6 */
  68. #endif /* CONFIG_NATIVE_WINDOWS */
  69. return -1;
  70. }