-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathutils.c
325 lines (285 loc) · 7.1 KB
/
utils.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* General-purpose utilities used by multiple files.
*/
#include "wavemon.h"
#include "nl80211.h"
#include <stdarg.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ether.h>
#include <net/if_arp.h>
#include <net/ethernet.h>
#include <sys/socket.h>
#include <linux/if.h>
/* Maximum length of a MAC address: 2 * 6 hex digits, 6 - 1 colons, plus '\0' */
#define MAC_ADDR_MAX 18
/** Read up to @buflen-1 contents of file @path into @buf. Returns -1 on error. */
ssize_t read_file(const char *path, char *buf, size_t buflen) {
int fd = open(path, O_RDONLY), i, n;
if (fd < 0)
return -1;
buf[buflen-1] = '\0';
n = read(fd, buf, buflen-1);
if (close(fd) < 0)
return -1;
// Strip trailing whitespace.
for (i = n-1; i >= 0 && isspace(buf[i]); i--)
buf[i] = '\0';
return n;
}
/** Read contents of @path into @num. Return 1 if ok, 0 on format error, -1 on error */
int read_number_file(const char *path, uint32_t *num) {
char buf[64];
int rc = read_file(path, buf, sizeof(buf));
if (rc < 0)
return rc;
return sscanf(buf, "%u", num);
}
/* Return true if all ethernet octets are zero. */
bool ether_addr_is_zero(const struct ether_addr *ea)
{
static const struct ether_addr zero = {{0}};
return memcmp(ea, &zero, sizeof(zero)) == 0;
}
/* Print a mac-address, include leading zeroes (unlike ether_ntoa(3)) */
char *ether_addr(const struct ether_addr *ea)
{
static char mac[MAC_ADDR_MAX];
char *d = mac, *a = ether_ntoa(ea);
next_chunk:
if (a[0] == '\0' || a[1] == '\0' || a[1] == ':')
*d++ = '0';
while ((*d++ = conf.cisco_mac ? (*a == ':' ? '.' : *a) : toupper(*a)))
if (*a++ == ':')
goto next_chunk;
return mac;
}
/* Print mac-address translation from /etc/ethers if available */
char *ether_lookup(const struct ether_addr *ea)
{
static char hostname[BUFSIZ];
if (ether_ntohost(hostname, ea) == 0)
return hostname;
return ether_addr(ea);
}
/* Format an Ethernet mac address */
char *mac_addr(const struct sockaddr *sa)
{
if (sa->sa_family != ARPHRD_ETHER)
return "00:00:00:00:00:00";
return ether_lookup((const struct ether_addr *)sa->sa_data);
}
/* Print into allocated string (reimplementation of asprintf(3), since not standard). */
char *a_sprintf(const char *fmt, ...)
{
char *p = NULL;
int size;
va_list ap;
va_start(ap, fmt);
size = vsnprintf(NULL, 0, fmt, ap);
if (size < 0)
err_quit("unable to determine '%s' string size", fmt);
va_end(ap);
p = malloc(++size); /* Add one for '\0'. */
if (p == NULL)
err_sys("unable to allocate string space");
va_start(ap, fmt);
size = vsnprintf(p, size, fmt, ap);
if (size < 0)
err_quit("unable to format '%s' properly", fmt);
va_end(ap);
return p;
}
/* count bits set in @mask the Brian Kernighan way */
uint8_t bit_count(uint32_t mask)
{
uint8_t bits_set;
for (bits_set = 0; mask; bits_set++)
mask &= mask - 1;
return bits_set;
}
/* netmask = contiguous 1's followed by contiguous 0's */
uint8_t prefix_len(const struct sockaddr *netmask)
{
uint8_t bs = 0;
if (netmask->sa_family == AF_INET)
return bit_count(((const struct sockaddr_in *)netmask)->sin_addr.s_addr);
for (int i = 0; i < 16; i++)
bs += bit_count(((const struct sockaddr_in6 *)netmask)->sin6_addr.s6_addr[i]);
return bs;
}
/* Pretty-print @sec into a string of up to 6 characters. */
const char *pretty_time(const unsigned sec)
{
static char buf[12];
unsigned d = sec / 86400,
h = sec % 86400 / 3600,
m = sec % 3600 / 60;
if (d > 9) {
sprintf(buf, "%u days", d);
} else if (d) {
if (h) {
sprintf(buf, "%ud %uh", d, h);
} else if (m) {
sprintf(buf, "%ud %dm", d, m);
} else {
sprintf(buf, "%u day%s", d, d == 1 ? "" : "s");
}
} else if (h) {
sprintf(buf, "%u:%02uh", h, m);
} else if (m) {
sprintf(buf, "%u:%02um", m, sec % 60);
} else {
sprintf(buf, "%u sec", sec);
}
return buf;
}
/* Like pretty_time, but allow milliseconds */
const char *pretty_time_ms(const unsigned msec)
{
static char buf[12];
if (msec < 1000) {
sprintf(buf, "%u ms", msec);
return buf;
}
return pretty_time(msec/1000);
}
/* Convert (preferred, valid) address lifetime to string. */
const char *lft2str(const uint32_t lifetime)
{
if (lifetime == (uint32_t)-1)
return "forever";
return pretty_time(lifetime);
}
/* Convert log dBm values to linear mW */
double dbm2mw(const double in)
{
return pow(10.0, in / 10.0);
}
char *dbm2units(const double in)
{
static char with_units[0x100];
double val = dbm2mw(in);
if (val < 0.00000001) {
sprintf(with_units, "%.2f pW", val * 1e9);
} else if (val < 0.00001) {
sprintf(with_units, "%.2f nW", val * 1e6);
} else if (val < 0.01) {
sprintf(with_units, "%.2f uW", val * 1e3);
} else {
sprintf(with_units, "%.2f mW", val);
}
return with_units;
}
/* Convert linear mW values to log dBm */
double mw2dbm(const double in)
{
return 10.0 * log10(in);
}
/* Stolen from iw:util.c */
int ieee80211_frequency_to_channel(int freq)
{
/* see 802.11-2007 17.3.8.3.2 and Annex J */
if (freq == 2484)
return 14;
else if (freq < 2484)
return (freq - 2407) / 5;
else if (freq >= 4910 && freq <= 4980)
return (freq - 4000) / 5;
else if (freq <= 45000) /* DMG band lower limit */
return (freq - 5000) / 5;
else if (freq >= 58320 && freq <= 64800)
return (freq - 56160) / 2160;
else
return 0;
}
const char *channel_width_name(enum nl80211_chan_width width)
{
switch (width) {
case NL80211_CHAN_WIDTH_1:
return "1 MHz";
case NL80211_CHAN_WIDTH_2:
return "2 MHz";
case NL80211_CHAN_WIDTH_4:
return "4 MHz";
case NL80211_CHAN_WIDTH_8:
return "8 MHz";
case NL80211_CHAN_WIDTH_16:
return "16 MHz";
case NL80211_CHAN_WIDTH_5:
return "5 MHz";
case NL80211_CHAN_WIDTH_10:
return "10 MHz";
case NL80211_CHAN_WIDTH_20:
return "20 MHz";
case NL80211_CHAN_WIDTH_20_NOHT:
return "20 MHz (no HT)";
case NL80211_CHAN_WIDTH_40:
return "40 MHz";
case NL80211_CHAN_WIDTH_80:
return "80 MHz";
case NL80211_CHAN_WIDTH_80P80:
return "80+80 MHz";
case NL80211_CHAN_WIDTH_160:
return "160 MHz";
case NL80211_CHAN_WIDTH_320:
return "320 MHz";
}
return "unknown";
}
/* stolen from iw:interface.c */
const char *channel_type_name(enum nl80211_channel_type channel_type)
{
switch (channel_type) {
case NL80211_CHAN_NO_HT:
return "NO HT";
case NL80211_CHAN_HT20:
return "HT20";
case NL80211_CHAN_HT40MINUS:
return "HT40-";
case NL80211_CHAN_HT40PLUS:
return "HT40+";
}
return "unknown";
}
/* stolen from iw:util.c */
const char *iftype_name(enum nl80211_iftype iftype)
{
static char modebuf[100];
static const char *ifmodes[NL80211_IFTYPE_MAX + 1] = {
"Unspecified",
"IBSS",
"Managed",
"AP",
"AP/VLAN",
"WDS",
"Monitor",
"Mesh Point",
"P2P-Client",
"P2P-GO",
"P2P-Device",
"Outside of a BSS",
"NAN",
};
if (iftype <= NL80211_IFTYPE_MAX && ifmodes[iftype])
return ifmodes[iftype];
sprintf(modebuf, "Unknown mode (%d)", iftype);
return modebuf;
}
/* stolen from iw:reg.c */
const char *dfs_domain_name(enum nl80211_dfs_regions region)
{
switch (region) {
case NL80211_DFS_UNSET:
return "DFS-UNSET";
case NL80211_DFS_FCC:
return "DFS-FCC";
case NL80211_DFS_ETSI:
return "DFS-ETSI";
case NL80211_DFS_JP:
return "DFS-JP";
default:
return "DFS-invalid";
}
}