-
Notifications
You must be signed in to change notification settings - Fork 1
/
mac_list.c
477 lines (398 loc) · 13.2 KB
/
mac_list.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/* mac_list.c - maintain a list of mac addresses and related data in a file
*
* Copyright (C) 2012, Greg Johnson
* Released under the terms of the GNU GPL v2.0.
*
* 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.
*
* $Id: mac_list.c,v 1.7 2012-02-22 18:55:25 greg Exp $
*/
static const char Version[] = "Version "
"$Id: mac_list.c,v 1.7 2012-02-22 18:55:25 greg Exp $";
/* maintain a list of mac addresses in a file.
* the mac addresses have expiration times (granularity usec), and if they
* are not refreshed soon enough they will be timed out and expunged
* from the file.
*
* this is an API that is usable generically and is not specific to the
* cloud_hub project.
*
* the mac addresses can have extra information associated with them;
* when a mac_list is initialized its specified type indicates what
* extra information should be kept with each mac address:
*
* mac_list_beacon:
* no extra information; just a file of mac addresses
*
* mac_list_beacon_name:
* a second mac address
*
* mac_list_beacon_desc:
* a string with no blanks, giving a name or description of the mac address
*
* mac_list_beacon_signal_strength:
* an integer value (i.e., a signal strength)
*/
#include <sys/time.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include "mac_list.h"
#include "util.h"
// #define DEBUG
#ifdef DEBUG
static FILE *debug_file;
static bool_t debug_file_inited = false;
#endif
/* for mac_lists with write_delay true, write to disk at most once
* per second.
*/
#define WRITE_DELAY 1000000
/* find and delete any mac addresses that have not been refreshed since
* the expiration time for the mac_list.
* (time granularity is usecs.)
*/
int mac_list_expire_timed_macs(mac_list_t *mac_list)
{
struct timeval tv;
struct timezone tz;
if (mac_list->timeout == -1) { return 0; }
if (gettimeofday(&tv, &tz)) {
fprintf(stderr, "expire_timed_macs: gettimeofday failed\n");
return -1;
}
while (1) {
int i;
int did_something = 0;
for (i = 0; i < mac_list->next_beacon; i++) {
if (usec_diff(tv.tv_sec, tv.tv_usec,
mac_list->beacons[i].tv_sec, mac_list->beacons[i].tv_usec)
> mac_list->timeout)
{
int j;
#ifdef DEBUG
if (debug_file_inited) {
fprintf(debug_file, "%s; timeout ", mac_list->fname);
mac_print_no_eoln(debug_file,
mac_list->beacons[i].mac_addr);
fprintf(debug_file, " at ");
util_print_time(debug_file, &tv);
fprintf(debug_file, "\n");
fflush(debug_file);
}
#endif
for (j = i; j < mac_list->next_beacon - 1; j++) {
mac_list->beacons[j] = mac_list->beacons[j + 1];
}
mac_list->next_beacon--;
did_something = 1;
break;
}
}
if (!did_something) break;
}
return 0;
}
/* read the mac_list in from its associated file.
* return 0 on success, -1 on failure.
* read up to a maximum of MAX_CLOUD mac addresses,
* and read the extra info associated with each mac address, based on
* the type of the mac_list.
* initialize the mac address times to "now".
*/
int mac_list_read(mac_list_t *mac_list)
{
int result = 0;
struct timeval tv;
struct timezone tz;
FILE *f;
if (gettimeofday(&tv, &tz)) {
fprintf(stderr, "mac_list_read; gettimeofday failed\n");
return -1;
}
f = fopen(mac_list->fname, "r");
if (f == NULL) {
perror("mac_list_read fopen failed");
fprintf(stderr, " could not open %s\n", mac_list->fname);
result = -1;
goto done;
}
mac_list->next_beacon = 0;
while (1) {
mac_address_t mac_addr;
if (1 != mac_read(f, mac_addr)) {
break;
}
if (mac_list->next_beacon >= MAX_CLOUD) {
fprintf(stderr, "mac_list_read: too many entries in file\n");
result = -1;
goto done;
}
mac_copy(mac_list->beacons[mac_list->next_beacon].mac_addr, mac_addr);
mac_list->beacons[mac_list->next_beacon].tv_sec = tv.tv_sec;
mac_list->beacons[mac_list->next_beacon].tv_usec = tv.tv_usec;
/* check scanf results etc. to bullet-proof */
switch (mac_list->type) {
case mac_list_beacon_desc :
fscanf(f, "%s", mac_list->desc[mac_list->next_beacon]);
break;
case mac_list_beacon_name :
mac_read(f, mac_list->names[mac_list->next_beacon]);
break;
case mac_list_beacon_signal_strength :
fscanf(f, " %d\n",
&mac_list->signal_strength[mac_list->next_beacon]);
break;
case mac_list_beacon :
break;
default :
fprintf(stderr, "mac_list_read: invalid type %d\n",
mac_list->type);
break;
}
mac_list->next_beacon++;
}
done:
if (f != NULL) { fclose(f); }
return result;
}
/* write the mac address list to stream f, including optional extra
* info with each mac address depending on mac_list's type.
*/
int mac_list_print(FILE *f, mac_list_t *mac_list)
{
int i;
for (i = 0; i < mac_list->next_beacon; i++) {
mac_print_no_eoln(f, mac_list->beacons[i].mac_addr);
switch (mac_list->type) {
case mac_list_beacon_desc :
fprintf(f, " %s", mac_list->desc[i]);
if (strchr(mac_list->desc[i], '\n') == NULL) {
fprintf(f, "\n");
}
break;
case mac_list_beacon_name :
fprintf(f, " ");
mac_print(f, mac_list->names[i]);
break;
case mac_list_beacon_signal_strength :
fprintf(f, " %d\n", mac_list->signal_strength[i]);
break;
case mac_list_beacon :
fprintf(f, "\n");
break;
default :
fprintf(stderr, "mac_list_print: invalid type %d\n",
mac_list->type);
break;
}
}
return 0;
}
/* write the mac address list out to its specified file on disk,
* including optional extra info with each mac address depending on
* mac_list's type.
*
* if the mac_list has write_delay == true , this routine is a
* no-op if it's too soon since the last time we did the write.
* see WRITE_DELAY above; max write frequency is currently once per second.
*/
int mac_list_write(mac_list_t *mac_list)
{
struct timeval tv;
struct timezone tz;
char tmp_fname[PATH_MAX];
FILE *f;
int i;
if (gettimeofday(&tv, &tz)) {
fprintf(stderr, "mac_list_write: gettimeofday failed\n");
return -1;
}
if (mac_list->write_delay
&& usec_diff(tv.tv_sec, tv.tv_usec,
mac_list->last_time_sec, mac_list->last_time_usec) < WRITE_DELAY)
{
return 0;
}
snprintf(tmp_fname, PATH_MAX, "/tmp/mac_list.%d", getpid());
f = fopen(tmp_fname, "w");
if (f == NULL) {
fprintf(stderr, "mac_list_write: fopen(\"%s\") failed\n", tmp_fname);
return -1;
}
for (i = 0; i < mac_list->next_beacon; i++) {
mac_print_no_eoln(f, mac_list->beacons[i].mac_addr);
switch (mac_list->type) {
case mac_list_beacon_name :
fprintf(f, " ");
mac_print(f, mac_list->names[i]);
break;
case mac_list_beacon_signal_strength :
fprintf(f, " %d\n", mac_list->signal_strength[i]);
break;
case mac_list_beacon :
fprintf(f, "\n");
break;
case mac_list_beacon_desc :
fprintf(f, " %s", mac_list->desc[i]);
if (strchr(mac_list->desc[i], '\n') == NULL) {
fprintf(f, "\n");
}
break;
default :
fprintf(stderr, "mac_list_write: invalid type %d\n",
mac_list->type);
break;
}
}
fclose(f);
if (rename(tmp_fname, mac_list->fname)) {
fprintf(stderr, "mac_list_write: "
"could not create %s: %d\n", mac_list->fname, errno);
perror("error: ");
unlink(tmp_fname);
return -1;
}
mac_list->last_time_sec = tv.tv_sec;
mac_list->last_time_usec = tv.tv_usec;
return 0;
}
/* create a new mac_list that will be saved on disk in /tmp/fname,
* and saves extra information with each mac address as specified
* by "type". Mac addresses that are not refreshed within "timeout"
* usecs are deleted. (if timeout is -1, they don't ever time out.)
* if write_delay is true, mac_list will be written to disk at most
* once per second.
* mac list (including disk version) is initially empty.
*/
int mac_list_init(mac_list_t *mac_list, char *fname, mac_list_type_t type,
long long timeout, bool_t write_delay)
{
#ifdef DEBUG
if (!debug_file_inited) {
if ((debug_file = fopen("/tmp/mac_list_debug", "w")) == NULL) {
fprintf(stderr, "mac_list_init; could not open debug: %s\n",
strerror(errno));
} else {
debug_file_inited = true;
}
}
#endif
mac_list->next_beacon = 0;
mac_list->last_time_sec = 0;
mac_list->last_time_usec = 0;
mac_list->write_delay = write_delay;
if (strlen(fname) >= (PATH_MAX - 1)) {
fprintf(stderr, "mac_list_init: file name %s too long\n", fname);
return -1;
}
strcpy(mac_list->fname, fname);
mac_list->type = type;
mac_list->timeout = timeout;
mac_list_write(mac_list);
return 0;
}
/* initialize a mac_list as above, but assume the file already has stuff
* in it, and read that stuff in.
*/
int mac_list_init_read(mac_list_t *mac_list, char *fname, mac_list_type_t type,
long long timeout, bool_t write_delay)
{
mac_list->next_beacon = 0;
mac_list->last_time_sec = 0;
mac_list->last_time_usec = 0;
mac_list->write_delay = write_delay;
if (strlen(fname) >= (PATH_MAX - 1)) {
fprintf(stderr, "mac_list_init: file name %s too long\n", fname);
return -1;
}
strcpy(mac_list->fname, fname);
mac_list->type = type;
mac_list->timeout = timeout;
mac_list_read(mac_list);
return 0;
}
/* expire any stale mac addresses from the mac_list.
* add mac_address to the list, or if it's already there refresh its
* time to "now".
*
* include name, signal_strength, and desc in the record for this mac address.
*/
void mac_list_add(mac_list_t *mac_list, mac_address_t mac_addr,
mac_address_t name, int signal_strength, char *desc, bool_t write_um)
{
int i, this_beacon;
struct timeval tv;
struct timezone tz;
if (gettimeofday(&tv, &tz)) {
fprintf(stderr, "mac_list_add; gettimeofday failed\n");
return;
}
if (mac_list_expire_timed_macs(mac_list)) {
fprintf(stderr, "mac_list_add; mac_list_expire_timed_macs failed\n");
return;
}
this_beacon = -1;
for (i = 0; i < mac_list->next_beacon; i++) {
if (mac_equal(mac_addr, mac_list->beacons[i].mac_addr)) {
this_beacon = i;
break;
}
}
if (this_beacon == -1) {
if (mac_list->next_beacon == MAX_CLOUD) {
fprintf(stderr, "mac_list_add; too many mac addresses\n");
return;
}
mac_copy(mac_list->beacons[mac_list->next_beacon].mac_addr, mac_addr);
this_beacon = mac_list->next_beacon;
mac_list->next_beacon++;
}
mac_copy(mac_list->names[this_beacon], name);
if (desc != NULL) {
strncpy(mac_list->desc[this_beacon], desc, MAX_DESC);
}
if (signal_strength != NO_SIGNAL_STRENGTH) {
mac_list->signal_strength[this_beacon] = signal_strength;
}
mac_list->beacons[this_beacon].tv_sec = tv.tv_sec;
mac_list->beacons[this_beacon].tv_usec = tv.tv_usec;
#ifdef DEBUG
if (debug_file_inited) {
fprintf(debug_file, "%s; time update ", mac_list->fname);
mac_print_no_eoln(debug_file,
mac_list->beacons[this_beacon].mac_addr);
fprintf(debug_file, " at ");
util_print_time(debug_file, &tv);
fprintf(debug_file, "\n");
fflush(debug_file);
}
#endif
if (write_um) {
if (mac_list_write(mac_list)) {
fprintf(stderr, "mac_list_add; write_beacons failed\n");
}
}
}
/* print "msg" to debug_file, together with time */
void mac_list_db_msg(char *msg)
{
#ifdef DEBUG
struct timeval tv;
struct timezone tz;
if (gettimeofday(&tv, &tz)) {
fprintf(stderr, "mac_list_db_msg; gettimeofday failed\n");
return;
}
if (debug_file_inited) {
fprintf(debug_file, "%s at ", msg);
util_print_time(debug_file, &tv);
fprintf(debug_file, "\n");
fflush(debug_file);
}
#endif
}