net-if.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /******************************************************************************
  2. Copyright (C) 2016 B. Lee <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #pragma once
  15. #include <util/darray.h>
  16. #ifdef _WIN32
  17. # include <ws2tcpip.h>
  18. # include <winsock2.h>
  19. # include <ws2ipdef.h>
  20. # include <iphlpapi.h>
  21. #else
  22. # ifdef __linux__
  23. # include <linux/if_link.h>
  24. # elif __FreeBSD__
  25. # ifndef _GNU_SOURCE
  26. # define _GNU_SOURCE
  27. # define __NET_IF_GNU_SOURCE__
  28. # endif //_GNU_SOURCE
  29. # endif //__FreeBSD__
  30. # include <ifaddrs.h>
  31. # include <netdb.h>
  32. # include <stdio.h>
  33. # include <stdlib.h>
  34. # include <unistd.h>
  35. # include <arpa/inet.h>
  36. # include <sys/socket.h>
  37. # ifdef __FreeBSD__
  38. # ifdef ___NET_IF_GNU_SOURCE__
  39. # undef ___NET_IF_GNU_SOURCE__
  40. # undef _GNU_SOURCE
  41. # endif
  42. # endif
  43. #endif
  44. struct netif_saddr_item {
  45. char *name;
  46. char *addr;
  47. };
  48. struct netif_saddr_data {
  49. DARRAY(struct netif_saddr_item) addrs;
  50. };
  51. static inline void netif_saddr_data_free(struct netif_saddr_data *data)
  52. {
  53. for (size_t i = 0; i < data->addrs.num; i++) {
  54. bfree(data->addrs.array[i].name);
  55. bfree(data->addrs.array[i].addr);
  56. }
  57. da_free(data->addrs);
  58. }
  59. extern bool netif_str_to_addr(struct sockaddr_storage *out, int *addr_len,
  60. const char *addr);
  61. extern void netif_get_addrs(struct netif_saddr_data *ifaddrs);
  62. extern void netif_log_saddrs(struct netif_saddr_data *sd);