get_iface_info.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * @file get_iface_info.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef BADVPN_GETIFACEINFO_H
  30. #define BADVPN_GETIFACEINFO_H
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <stdint.h>
  34. #include <unistd.h>
  35. #include <sys/socket.h>
  36. #include <net/if.h>
  37. #include <net/if_arp.h>
  38. #include <sys/ioctl.h>
  39. #include <misc/debug.h>
  40. /**
  41. * Returns information about a network interface with the given name.
  42. *
  43. * @param ifname name of interface to get information for
  44. * @param out_mac the MAC address will be returned here, unless NULL
  45. * @param out_mtu the MTU will be returned here, unless NULL
  46. * @param out_ifindex the interface index will be returned here, unless NULL
  47. * @return 1 on success, 0 on failure
  48. */
  49. static int badvpn_get_iface_info (const char *ifname, uint8_t *out_mac, int *out_mtu, int *out_ifindex) WARN_UNUSED;
  50. static int badvpn_get_iface_info (const char *ifname, uint8_t *out_mac, int *out_mtu, int *out_ifindex)
  51. {
  52. ASSERT(ifname)
  53. struct ifreq ifr;
  54. int s = socket(AF_INET, SOCK_DGRAM, 0);
  55. if (s < 0) {
  56. goto fail0;
  57. }
  58. // get MAC
  59. if (out_mac) {
  60. memset(&ifr, 0, sizeof(ifr));
  61. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname);
  62. if (ioctl(s, SIOCGIFHWADDR, &ifr)) {
  63. goto fail1;
  64. }
  65. if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
  66. goto fail1;
  67. }
  68. memcpy(out_mac, ifr.ifr_hwaddr.sa_data, 6);
  69. }
  70. // get MTU
  71. if (out_mtu) {
  72. memset(&ifr, 0, sizeof(ifr));
  73. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname);
  74. if (ioctl(s, SIOCGIFMTU, &ifr)) {
  75. goto fail1;
  76. }
  77. *out_mtu = ifr.ifr_mtu;
  78. }
  79. // get interface index
  80. if (out_ifindex) {
  81. memset(&ifr, 0, sizeof(ifr));
  82. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname);
  83. if (ioctl(s, SIOCGIFINDEX, &ifr)) {
  84. goto fail1;
  85. }
  86. *out_ifindex = ifr.ifr_ifindex;
  87. }
  88. close(s);
  89. return 1;
  90. fail1:
  91. close(s);
  92. fail0:
  93. return 0;
  94. }
  95. #endif