oui: cleanup parser
[netsniff-ng.git] / oui.c
blob5212f15d6c9e3d40e0bdf448fbb4055fdf399edb
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009 - 2013 Daniel Borkmann.
5 * Subject to the GPL, version 2.
6 */
8 #include <stdint.h>
9 #include <stdbool.h>
11 #include "hash.h"
12 #include "xmalloc.h"
13 #include "xutils.h"
14 #include "oui.h"
16 static struct hash_table oui;
18 static bool initialized = false;
20 struct vendor_id {
21 unsigned int id;
22 char *vendor;
23 struct vendor_id *next;
26 const char *lookup_vendor(unsigned int id)
28 struct vendor_id *entry;
30 entry = lookup_hash(id, &oui);
31 while (entry && id != entry->id)
32 entry = entry->next;
34 return (entry && id == entry->id ?
35 entry->vendor : NULL);
38 void dissector_init_oui(void)
40 FILE *fp;
41 char buff[512], *ptr;
42 struct vendor_id *v;
43 void **pos;
45 if (initialized)
46 return;
48 fp = fopen("/etc/netsniff-ng/oui.conf", "r");
49 if (!fp)
50 panic("No oui.conf found!\n");
52 memset(buff, 0, sizeof(buff));
54 while (fgets(buff, sizeof(buff), fp) != NULL) {
55 buff[sizeof(buff) - 1] = 0;
56 ptr = buff;
58 v = xmalloc(sizeof(*v));
59 v->id = strtol(ptr, &ptr, 0);
61 if ((ptr = strstr(buff, ", ")))
62 ptr += strlen(", ");
63 ptr = strtrim_right(ptr, '\n');
64 ptr = strtrim_right(ptr, ' ');
66 v->vendor = xstrdup(ptr);
67 v->next = NULL;
69 pos = insert_hash(v->id, v, &oui);
70 if (pos) {
71 v->next = *pos;
72 *pos = v;
75 memset(buff, 0, sizeof(buff));
78 fclose(fp);
79 initialized = true;
82 static int dissector_cleanup_oui_hash(void *ptr)
84 struct vendor_id *tmp, *v = ptr;
86 if (!ptr)
87 return 0;
89 while ((tmp = v->next)) {
90 xfree(v->vendor);
91 xfree(v);
92 v = tmp;
95 xfree(v->vendor);
96 xfree(v);
98 return 0;
101 void dissector_cleanup_oui(void)
103 for_each_hash(&oui, dissector_cleanup_oui_hash);
104 free_hash(&oui);
106 initialized = 0;