docs: minor update in readme description, several sections
[netsniff-ng.git] / src / oui.c
blobcf672e90ed90fb2fe3e109df62c26ad01f58e6a7
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 /* Note: this macro only applies to the lookup_* functions here in this file,
25 * mainly to remove redundand code. */
26 #define __do_lookup_inline(id, struct_name, hash_ptr, struct_member) \
27 ({ \
28 struct struct_name *entry = lookup_hash(id, hash_ptr); \
29 while (entry && id != entry->id) \
30 entry = entry->next; \
31 (entry && id == entry->id ? entry->struct_member : NULL); \
34 const char *lookup_vendor(unsigned int id)
36 return __do_lookup_inline(id, vendor_id, &oui, vendor);
39 void dissector_init_oui(void)
41 FILE *fp;
42 char buff[512], *ptr;
43 struct vendor_id *ven;
44 void **pos;
46 if (initialized)
47 return;
49 fp = fopen("/etc/netsniff-ng/oui.conf", "r");
50 if (!fp)
51 panic("No /etc/netsniff-ng/oui.conf found!\n");
53 memset(buff, 0, sizeof(buff));
54 while (fgets(buff, sizeof(buff), fp) != NULL) {
55 buff[sizeof(buff) - 1] = 0;
57 ven = xmalloc(sizeof(*ven));
58 ptr = buff;
59 ptr = skips(ptr);
60 ptr = getuint(ptr, &ven->id);
61 ptr = skips(ptr);
62 ptr = skipchar(ptr, ',');
63 ptr = skips(ptr);
64 ptr = strtrim_right(ptr, '\n');
65 ptr = strtrim_right(ptr, ' ');
66 ven->vendor = xstrdup(ptr);
67 ven->next = NULL;
68 pos = insert_hash(ven->id, ven, &oui);
69 if (pos) {
70 ven->next = *pos;
71 *pos = ven;
73 memset(buff, 0, sizeof(buff));
75 fclose(fp);
77 initialized = 1;
80 static int __dissector_cleanup_oui(void *ptr)
82 struct vendor_id *tmp, *v = ptr;
83 if (!ptr)
84 return 0;
85 while ((tmp = v->next)) {
86 xfree(v->vendor);
87 xfree(v);
88 v = tmp;
90 xfree(v->vendor);
91 xfree(v);
93 return 0;
96 void dissector_cleanup_oui(void)
98 for_each_hash(&oui, __dissector_cleanup_oui);
99 free_hash(&oui);
101 initialized = 0;