stddef: add more useful CPP constants
[netsniff-ng.git] / oui.c
blob41b7c1a347211c28ddb108836b8d42f6c3ab31b3
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 *v;
30 v = lookup_hash(id, &oui);
31 while (v && id != v->id)
32 v = v->next;
34 return (v && id == v->id ? v->vendor : NULL);
37 void dissector_init_oui(void)
39 FILE *fp;
40 char buff[128], *ptr;
41 struct vendor_id *v;
42 void **pos;
44 if (initialized)
45 return;
47 fp = fopen("/etc/netsniff-ng/oui.conf", "r");
48 if (!fp)
49 panic("No oui.conf found!\n");
51 memset(buff, 0, sizeof(buff));
53 while (fgets(buff, sizeof(buff), fp) != NULL) {
54 buff[sizeof(buff) - 1] = 0;
55 ptr = buff;
57 v = xmalloc(sizeof(*v));
58 v->id = strtol(ptr, &ptr, 0);
60 if ((ptr = strstr(buff, ", ")))
61 ptr += strlen(", ");
62 ptr = strtrim_right(ptr, '\n');
63 ptr = strtrim_right(ptr, ' ');
65 v->vendor = xstrdup(ptr);
66 v->next = NULL;
68 pos = insert_hash(v->id, v, &oui);
69 if (pos) {
70 v->next = *pos;
71 *pos = v;
74 memset(buff, 0, sizeof(buff));
77 fclose(fp);
78 initialized = true;
81 static int dissector_cleanup_oui_hash(void *ptr)
83 struct vendor_id *tmp, *v = ptr;
85 if (!ptr)
86 return 0;
88 while ((tmp = v->next)) {
89 xfree(v->vendor);
90 xfree(v);
91 v = tmp;
94 xfree(v->vendor);
95 xfree(v);
97 return 0;
100 void dissector_cleanup_oui(void)
102 for_each_hash(&oui, dissector_cleanup_oui_hash);
103 free_hash(&oui);
104 initialized = false;