BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 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 | |||
30 | #ifndef BADVPN_GETIFACEINFO_H |
||
31 | #define BADVPN_GETIFACEINFO_H |
||
32 | |||
33 | #include <stdio.h> |
||
34 | #include <string.h> |
||
35 | #include <stdint.h> |
||
36 | #include <unistd.h> |
||
37 | #include <sys/socket.h> |
||
38 | #include <net/if.h> |
||
39 | #include <net/if_arp.h> |
||
40 | #include <sys/ioctl.h> |
||
41 | |||
42 | #include <misc/debug.h> |
||
43 | |||
44 | /** |
||
45 | * Returns information about a network interface with the given name. |
||
46 | * |
||
47 | * @param ifname name of interface to get information for |
||
48 | * @param out_mac the MAC address will be returned here, unless NULL |
||
49 | * @param out_mtu the MTU will be returned here, unless NULL |
||
50 | * @param out_ifindex the interface index will be returned here, unless NULL |
||
51 | * @return 1 on success, 0 on failure |
||
52 | */ |
||
53 | static int badvpn_get_iface_info (const char *ifname, uint8_t *out_mac, int *out_mtu, int *out_ifindex) WARN_UNUSED; |
||
54 | |||
55 | |||
56 | static int badvpn_get_iface_info (const char *ifname, uint8_t *out_mac, int *out_mtu, int *out_ifindex) |
||
57 | { |
||
58 | ASSERT(ifname) |
||
59 | |||
60 | struct ifreq ifr; |
||
61 | |||
62 | int s = socket(AF_INET, SOCK_DGRAM, 0); |
||
63 | if (s < 0) { |
||
64 | goto fail0; |
||
65 | } |
||
66 | |||
67 | // get MAC |
||
68 | if (out_mac) { |
||
69 | memset(&ifr, 0, sizeof(ifr)); |
||
70 | snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname); |
||
71 | if (ioctl(s, SIOCGIFHWADDR, &ifr)) { |
||
72 | goto fail1; |
||
73 | } |
||
74 | if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) { |
||
75 | goto fail1; |
||
76 | } |
||
77 | memcpy(out_mac, ifr.ifr_hwaddr.sa_data, 6); |
||
78 | } |
||
79 | |||
80 | // get MTU |
||
81 | if (out_mtu) { |
||
82 | memset(&ifr, 0, sizeof(ifr)); |
||
83 | snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname); |
||
84 | if (ioctl(s, SIOCGIFMTU, &ifr)) { |
||
85 | goto fail1; |
||
86 | } |
||
87 | *out_mtu = ifr.ifr_mtu; |
||
88 | } |
||
89 | |||
90 | // get interface index |
||
91 | if (out_ifindex) { |
||
92 | memset(&ifr, 0, sizeof(ifr)); |
||
93 | snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", ifname); |
||
94 | if (ioctl(s, SIOCGIFINDEX, &ifr)) { |
||
95 | goto fail1; |
||
96 | } |
||
97 | *out_ifindex = ifr.ifr_ifindex; |
||
98 | } |
||
99 | |||
100 | close(s); |
||
101 | |||
102 | return 1; |
||
103 | |||
104 | fail1: |
||
105 | close(s); |
||
106 | fail0: |
||
107 | return 0; |
||
108 | } |
||
109 | |||
110 | #endif |