-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
258 lines (202 loc) · 6.36 KB
/
main.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
/*
* Copyright (C) 2015 Bryan Christ <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2, as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#ifdef __linux
#include <linux/if_packet.h>
#endif
#ifdef __FreeBSD__
#include <net/if_dl.h>
#include <netinet/in.h>
#include <net/if_types.h>
#define AF_PACKET AF_LINK // kludge for AF_PACKET on *bsd
#endif
#include <net/if.h>
#define FLAG_SHOW_IPV4 (1 << 0)
#define FLAG_SHOW_IPV6 (1 << 1)
#define FLAG_SHOW_MAC (1 << 2)
#ifdef __linux
typedef struct sockaddr_ll sockinfo_t;
#endif
#ifdef __FreeBSD__
typedef struct sockaddr_dl sockinfo_t;
#endif
struct _config_s
{
char *iface;
unsigned int flags;
};
static int
parse_commands(int argc, char **argv, struct _config_s *config);
static void
print_usage(void);
int
main(int argc, char **argv)
{
struct ifaddrs *ifaddr;
struct ifaddrs *ifa;
// struct sockaddr_ll *saddr;
sockinfo_t *saddr;
int family;
char host[NI_MAXHOST];
char mac[20];
struct _config_s config;
unsigned char byte;
char *pos;
int len;
int i;
memset(&config, 0, sizeof(config));
if (getifaddrs(&ifaddr) == -1)
{
perror("getifaddrs");
exit(EXIT_FAILURE);
}
parse_commands(argc, argv, &config);
/*
Walk through linked list, maintaining head pointer so we
can free list later
*/
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr == NULL)
continue;
family = ifa->ifa_addr->sa_family;
/*
Display interface name and family (including symbolic
form of the latter for the common families)
*/
if (config.iface != NULL)
{
// user specified an inteface of intrest. skip all others.
if (strcmp(config.iface, ifa->ifa_name) !=0 ) continue;
}
if (family == AF_PACKET && config.flags & FLAG_SHOW_MAC)
{
saddr = (sockinfo_t*)ifa->ifa_addr;
memset(mac, 0 , sizeof(mac));
len = 0;
#ifdef __linux
// skip loopback devices. they dont have a hwaddr
if (ifa->ifa_flags & IFF_LOOPBACK) continue;
// saddr = (struct sockaddr_ll*)ifa->ifa_addr;
for(i = 0;i < 6;i++)
{
byte = (unsigned char)saddr->sll_addr[i];
len += sprintf(mac + len, "%02X%s",
byte, i < 5 ? ":":"");
}
#endif
#ifdef __FreeBSD__
// skip loopback devices. they dont have a hwaddr
if (saddr->sdl_type == IFT_LOOP) continue;
for(i = 0;i < 6;i++)
{
byte = (unsigned char)LLADDR(saddr)[i];
len += sprintf(mac + len, "%02X%s",
byte, i < 5 ? ":":"");
}
#endif
printf("%s %s\n", ifa->ifa_name, mac);
}
if (family == AF_INET || family == AF_INET6)
{
if ((family == AF_INET) && !(config.flags & FLAG_SHOW_IPV4))
continue;
if ((family == AF_INET6) && !(config.flags & FLAG_SHOW_IPV6))
continue;
pos = NULL;
memset(host, 0, sizeof(host));
// TODO: check return value and handle gracefully
getnameinfo(ifa->ifa_addr, (family == AF_INET) ?
sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6),
host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
// linux inserts a %<ifname> suffix on IPv6 addresses. trim out.
pos = strchr(host, '%');
if(pos != NULL) *pos = '\0';
printf("%s %s\n", ifa->ifa_name, host);
}
}
freeifaddrs(ifaddr);
exit(EXIT_SUCCESS);
}
static int
parse_commands(int argc, char **argv, struct _config_s *config)
{
static struct option long_options[] =
{
{"iface", required_argument, 0, 'i'},
{"ipv4", required_argument, 0, '4'},
{"ipv6", required_argument, 0, '6'},
{"mac", required_argument, 0, 'm'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
int c;
optind = 0;
for (;;)
{
c = getopt_long(argc, argv, "i:46mh", long_options, NULL);
if (c == -1) break;
switch (c)
{
case 'h':
print_usage();
exit(0);
case 'i':
config->iface = strdup(optarg);
break;
case '4':
config->flags |= FLAG_SHOW_IPV4;
break;
case '6':
config->flags |= FLAG_SHOW_IPV6;
break;
case 'm':
config->flags |= FLAG_SHOW_MAC;
break;
default:
break;
}
}
return 0;
}
static void
print_usage(void)
{
fprintf(stderr, "Usage: ifaddr [options]\n"
"\n"
" -i --iface [ifname] the inteface to query. all if ommited.\n"
" examples: eth0, p0p1\n"
"\n"
" -4, --ipv4 show IP version 4 address\n"
" -6, --ipv6 show IP version 6 address\n"
" -m, --mac show hardware address of interface\n"
"\n"
" -h, --help show this help\n"
"\n"
"Notice that options are separated from their arguments by\n"
"a space and not an equal sign.\n" "\n");
return;
}