man: improvements to mausezahn.8: CoS (PCP), DEI (CFI)
[netsniff-ng.git] / oui.c
blob2f6ec72a3dd1bf81c94e0ec65c55338bc8965412
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009 - 2013 Daniel Borkmann.
4 * Subject to the GPL, version 2.
5 */
7 #include <stdint.h>
8 #include <stdbool.h>
10 #include "hash.h"
11 #include "xmalloc.h"
12 #include "oui.h"
13 #include "str.h"
15 static struct hash_table oui;
17 static bool initialized = false;
19 struct vendor_id {
20 unsigned int id;
21 char *vendor;
22 struct vendor_id *next;
25 const char *lookup_vendor(unsigned int id)
27 struct vendor_id *v;
29 v = lookup_hash(id, &oui);
30 while (v && id != v->id)
31 v = v->next;
33 return (v && id == v->id ? v->vendor : NULL);
36 void dissector_init_oui(void)
38 FILE *fp;
39 char buff[128], *ptr, *end;
40 struct vendor_id *v;
41 void **pos;
43 if (initialized)
44 return;
46 fp = fopen(PREFIX_STRING "/etc/netsniff-ng/oui.conf", "r");
47 if (!fp)
48 panic("No oui.conf found!\n");
50 memset(buff, 0, sizeof(buff));
52 while (fgets(buff, sizeof(buff), fp) != NULL) {
53 buff[sizeof(buff) - 1] = 0;
54 ptr = buff;
56 v = xmalloc(sizeof(*v));
57 v->id = strtol(ptr, &end, 0);
58 /* No valid line, skip */
59 if (v->id == 0 && end == ptr)
60 continue;
62 ptr = strstr(buff, ", ");
63 if (!ptr)
64 continue;
66 ptr += strlen(", ");
67 ptr = strtrim_right(ptr, '\n');
68 ptr = strtrim_right(ptr, ' ');
70 v->vendor = xstrdup(ptr);
71 v->next = NULL;
73 pos = insert_hash(v->id, v, &oui);
74 if (pos) {
75 v->next = *pos;
76 *pos = v;
79 memset(buff, 0, sizeof(buff));
82 fclose(fp);
83 initialized = true;
86 static int dissector_cleanup_oui_hash(void *ptr)
88 struct vendor_id *tmp, *v = ptr;
90 if (!ptr)
91 return 0;
93 while ((tmp = v->next)) {
94 xfree(v->vendor);
95 xfree(v);
96 v = tmp;
99 xfree(v->vendor);
100 xfree(v);
102 return 0;
105 void dissector_cleanup_oui(void)
107 for_each_hash(&oui, dissector_cleanup_oui_hash);
108 free_hash(&oui);
109 initialized = false;