oui: Inline __do_lookup_inline() macro
[netsniff-ng.git] / src / oui.c
blob45bceeec5d843461f60f94a304de88f23f97ad7a
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
5 * Subject to the GPL, version 2.
6 */
8 #include <stdint.h>
10 #include "hash.h"
11 #include "xmalloc.h"
12 #include "xutils.h"
13 #include "oui.h"
15 static struct hash_table oui;
16 static int initialized = 0;
18 struct vendor_id {
19 unsigned int id;
20 char *vendor;
21 struct vendor_id *next;
24 const char *lookup_vendor(unsigned int id)
26 struct vendor_id *entry = lookup_hash(id, &oui);
28 while (entry && id != entry->id)
29 entry = entry->next;
31 return (entry && id == entry->id ? entry->vendor : NULL);
34 void dissector_init_oui(void)
36 FILE *fp;
37 char buff[512], *ptr;
38 struct vendor_id *ven;
39 void **pos;
41 if (initialized)
42 return;
44 fp = fopen("/etc/netsniff-ng/oui.conf", "r");
45 if (!fp)
46 panic("No /etc/netsniff-ng/oui.conf found!\n");
48 memset(buff, 0, sizeof(buff));
49 while (fgets(buff, sizeof(buff), fp) != NULL) {
50 buff[sizeof(buff) - 1] = 0;
52 ven = xmalloc(sizeof(*ven));
53 ptr = buff;
54 ptr = skips(ptr);
55 ptr = getuint(ptr, &ven->id);
56 ptr = skips(ptr);
57 ptr = skipchar(ptr, ',');
58 ptr = skips(ptr);
59 ptr = strtrim_right(ptr, '\n');
60 ptr = strtrim_right(ptr, ' ');
61 ven->vendor = xstrdup(ptr);
62 ven->next = NULL;
63 pos = insert_hash(ven->id, ven, &oui);
64 if (pos) {
65 ven->next = *pos;
66 *pos = ven;
68 memset(buff, 0, sizeof(buff));
70 fclose(fp);
72 initialized = 1;
75 static int __dissector_cleanup_oui(void *ptr)
77 struct vendor_id *tmp, *v = ptr;
78 if (!ptr)
79 return 0;
80 while ((tmp = v->next)) {
81 xfree(v->vendor);
82 xfree(v);
83 v = tmp;
85 xfree(v->vendor);
86 xfree(v);
88 return 0;
91 void dissector_cleanup_oui(void)
93 for_each_hash(&oui, __dissector_cleanup_oui);
94 free_hash(&oui);
96 initialized = 0;