dissector: icmpv6: Fix compiler warnings
[netsniff-ng.git] / oui.c
blobb537e1e8022c425bbe4e3a99885c4fa92eeef836
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 /* not a valid line, skip */
59 if (v->id == 0 && end == ptr) {
60 xfree(v);
61 continue;
64 ptr = strstr(buff, ", ");
65 /* likewise */
66 if (!ptr) {
67 xfree(v);
68 continue;
71 ptr += strlen(", ");
72 ptr = strtrim_right(ptr, '\n');
73 ptr = strtrim_right(ptr, ' ');
75 v->vendor = xstrdup(ptr);
76 v->next = NULL;
78 pos = insert_hash(v->id, v, &oui);
79 if (pos) {
80 v->next = *pos;
81 *pos = v;
84 memset(buff, 0, sizeof(buff));
87 fclose(fp);
88 initialized = true;
91 static int dissector_cleanup_oui_hash(void *ptr)
93 struct vendor_id *tmp, *v = ptr;
95 if (!ptr)
96 return 0;
98 while ((tmp = v->next)) {
99 xfree(v->vendor);
100 xfree(v);
101 v = tmp;
104 xfree(v->vendor);
105 xfree(v);
107 return 0;
110 void dissector_cleanup_oui(void)
112 for_each_hash(&oui, dissector_cleanup_oui_hash);
113 free_hash(&oui);
114 initialized = false;