-
Notifications
You must be signed in to change notification settings - Fork 3
/
interface.c
291 lines (265 loc) · 8.35 KB
/
interface.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <ifaddrs.h>
#include <net/if.h>
#if defined(MACOS) || defined(__FreeBSD__)
#include <net/if_dl.h>
#include <net/if_types.h>
#endif
#ifdef __linux__
#include <net/if_arp.h>
#include <netpacket/packet.h>
#endif
#include <string.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <net/ethernet.h>
#include "interface.h"
#include "jomon.h"
#define MAX_NUM_INTERFACES 16
static char *get_active_interface(int fd, char *buffer, int len);
struct interface {
char *name; /* interface name */
unsigned short type; /* interface type, e.g. Ethernet, Firewire etc. */
struct sockaddr_in *inaddr; /* IPv4 address */
struct sockaddr_in6 *in6addr; /* IPv6 address */
unsigned char addrlen; /* hardware address length */
unsigned char hwaddr[8]; /* hardware address */
};
void iface_activate(iface_handle_t *handle, char *device, struct bpf_prog *bpf)
{
if (!handle->active) {
handle->op->activate(handle, device, bpf);
handle->active = true;
}
}
void iface_close(iface_handle_t *handle)
{
if (handle->active) {
handle->op->close(handle);
handle->active = false;
}
}
void iface_read_packet(iface_handle_t *handle)
{
handle->op->read_packet(handle);
}
void iface_set_promiscuous(iface_handle_t *handle, char *dev, bool enable)
{
handle->op->set_promiscuous(handle, dev, enable);
}
void list_interfaces(void)
{
struct ifaddrs *ifp, *ifhead;
struct interface iflist[MAX_NUM_INTERFACES];
int i;
int c = 0;
/* On Linux getifaddrs returns one entry per address, on Mac OS X and BSD one
entry per interface */
if (getifaddrs(&ifp) == -1) {
err_sys("getifaddrs error");
}
memset(iflist, 0, sizeof(iflist));
ifhead = ifp;
/* traverse the ifaddrs list and store the interface information in iflist */
while (ifp) {
if (c >= MAX_NUM_INTERFACES) {
break;
}
/* Check if the interface is stored in iflist */
i = -1;
for (int j = 0; j < MAX_NUM_INTERFACES; j++) {
if (iflist[j].name && strcmp(ifp->ifa_name, iflist[j].name) == 0) {
i = j;
break;
}
}
/* new interface -- insert in iflist */
if (i == -1) {
iflist[c].name = ifp->ifa_name;
i = c++;
}
switch (ifp->ifa_addr->sa_family) {
case AF_INET:
iflist[i].inaddr = (struct sockaddr_in *) ifp->ifa_addr;
break;
case AF_INET6:
iflist[i].in6addr = (struct sockaddr_in6 *) ifp->ifa_addr;
break;
#ifdef __linux__
case AF_PACKET:
{
struct sockaddr_ll *ll_addr;
ll_addr = (struct sockaddr_ll *) ifp->ifa_addr;
memcpy(iflist[i].hwaddr, ll_addr->sll_addr, ll_addr->sll_halen);
iflist[i].type = ll_addr->sll_hatype;
iflist[i].addrlen = ll_addr->sll_halen;
break;
}
#endif
#if defined(MACOS) || defined(__FreeBSD__)
case AF_LINK:
{
struct sockaddr_dl *dl_addr;
dl_addr = (struct sockaddr_dl *) ifp->ifa_addr;
memcpy(iflist[i].hwaddr, (char *) LLADDR(dl_addr), dl_addr->sdl_alen);
iflist[i].type = dl_addr->sdl_type;
iflist[i].addrlen = dl_addr->sdl_alen;
}
#endif
default:
break;
}
ifp = ifp->ifa_next;
}
/* print out information for each interface */
for (i = 0; i < c; i++) {
int len = strlen(iflist[i].name);
int width = 8;
if (len >= 8) {
width = len + 1;
printf("%-*s", width, iflist[i].name);
} else {
printf("%-*s", width, iflist[i].name);
}
switch (iflist[i].type) {
#if defined(MACOS) || defined(__FreeBSD__)
case IFT_ETHER:
printf("Ethernet\n");
break;
case IFT_LOOP:
printf("Loopback\n");
break;
case IFT_SLIP:
printf("IP over generic TTY\n");
break;
case IFT_IEEE1394:
printf("IEEE1394 High Performance SerialBus\n");
break;
#endif
#ifdef __linux__
case ARPHRD_ETHER:
printf("Ethernet\n");
break;
case ARPHRD_LOOPBACK:
printf("Loopback\n");
break;
case ARPHRD_IEEE1394:
printf("IEEE1394 High Performance SerialBus\n");
break;
#endif
default:
printf("Unknown type: %d\n", iflist[i].type);
break;
}
if (iflist[i].addrlen == 6) {
char hwaddr[18];
snprintf(hwaddr, 18, "%02x:%02x:%02x:%02x:%02x:%02x",
iflist[i].hwaddr[0], iflist[i].hwaddr[1],
iflist[i].hwaddr[2], iflist[i].hwaddr[3],
iflist[i].hwaddr[4], iflist[i].hwaddr[5]);
printf("\tHW addr: %s\n", hwaddr);
} else {
printf("\tHW addr len: %d\n", iflist[i].addrlen);
}
if (iflist[i].inaddr) {
char inet_addr[INET_ADDRSTRLEN];
struct sockaddr_in *hst_addr;
hst_addr = (struct sockaddr_in *) iflist[i].inaddr;
inet_ntop(AF_INET, &hst_addr->sin_addr, inet_addr, INET_ADDRSTRLEN);
printf("\tinet addr: %s\n", inet_addr);
}
if (iflist[i].in6addr) {
char inet6_addr[INET6_ADDRSTRLEN];
struct sockaddr_in6 *hst6_addr;
hst6_addr = (struct sockaddr_in6 *) iflist[i].in6addr;
inet_ntop(AF_INET6, &hst6_addr->sin6_addr, inet6_addr, INET6_ADDRSTRLEN);
printf("\tinet6 addr: %s\n", inet6_addr);
}
printf("\n");
}
freeifaddrs(ifhead);
}
char *get_default_interface(void)
{
struct ifconf ifc;
int sockfd, len, lastlen;
char *device = NULL;
char *buffer;
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
err_sys("Socket error");
}
lastlen = 0;
len = 10 * sizeof(struct ifreq); /* initial guess of buffer size (10 interfaces) */
while (1) {
buffer = xmalloc(len);
ifc.ifc_len = len;
ifc.ifc_buf = buffer;
/* SIOCGIFCONF takes a struct ifconf *. The ifc_buf field points to a
* buffer of length ifc_len bytes, into which the kernel writes a list
* of type struct ifreq [] */
if (ioctl(sockfd, SIOCGIFCONF, &ifc) == -1) {
if (errno != EINVAL || lastlen != 0) {
err_sys("ioctl error");
}
} else {
device = get_active_interface(sockfd, buffer + lastlen, ifc.ifc_len);
if (device /* active device found */ ||
ifc.ifc_len == lastlen) { /* same value as last time */
break;
}
lastlen = ifc.ifc_len;
len += 10 * sizeof(struct ifreq);
}
free(buffer);
}
free(buffer);
close(sockfd);
return device;
}
/*
* Traverse buffer of struct ifreq and return the name of the first device for
* which the IFF_UP and IFF_RUNNING flags are set
*/
char *get_active_interface(int fd, char *buffer, int len)
{
char *ptr;
char *device;
struct ifreq *ifr;
for (ptr = buffer; ptr < buffer + len; ptr += sizeof(struct ifreq)) {
ifr = (struct ifreq *) ptr;
if (strncmp(ifr->ifr_name, "lo", 2) == 0) { /* ignore the loopback device */
continue;
} else {
if (ioctl(fd, SIOCGIFFLAGS, ifr) == -1) {
err_sys("ioctl error");
}
if (ifr->ifr_flags & IFF_UP && ifr->ifr_flags & IFF_RUNNING) {
size_t namelen = strlen(ifr->ifr_name);
device = xmalloc(namelen + 1);
memcpy(device, ifr->ifr_name, namelen);
device[namelen] = '\0';
return device;
}
}
}
return NULL;
}
void get_local_address(char *dev, struct sockaddr *addr)
{
struct ifreq ifr;
int sockfd;
strlcpy(ifr.ifr_name, dev, IFNAMSIZ);
ifr.ifr_addr.sa_family = AF_INET;
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
err_sys("socket error");
}
if (ioctl(sockfd, SIOCGIFADDR, &ifr) == -1) {
err_sys("ioctl error");
}
memcpy(addr, &ifr.ifr_addr, sizeof(*addr));
close(sockfd);
}