net-if.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #include <netinet/in.h>
  26. #ifndef _GNU_SOURCE
  27. #define _GNU_SOURCE
  28. #define __NET_IF_GNU_SOURCE__
  29. #endif //_GNU_SOURCE
  30. #endif //__FreeBSD__
  31. #include <ifaddrs.h>
  32. #include <netdb.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <unistd.h>
  36. #include <arpa/inet.h>
  37. #include <sys/socket.h>
  38. #ifdef __FreeBSD__
  39. #ifdef ___NET_IF_GNU_SOURCE__
  40. #undef ___NET_IF_GNU_SOURCE__
  41. #undef _GNU_SOURCE
  42. #endif
  43. #endif
  44. #endif
  45. struct netif_saddr_item {
  46. char *name;
  47. char *addr;
  48. };
  49. struct netif_saddr_data {
  50. DARRAY(struct netif_saddr_item) addrs;
  51. };
  52. static inline void netif_saddr_data_free(struct netif_saddr_data *data)
  53. {
  54. for (size_t i = 0; i < data->addrs.num; i++) {
  55. bfree(data->addrs.array[i].name);
  56. bfree(data->addrs.array[i].addr);
  57. }
  58. da_free(data->addrs);
  59. }
  60. extern bool netif_str_to_addr(struct sockaddr_storage *out, int *addr_len,
  61. const char *addr);
  62. extern void netif_get_addrs(struct netif_saddr_data *ifaddrs);
  63. extern void netif_log_saddrs(struct netif_saddr_data *sd);