man: netsniff-ng: Fix a typo
[netsniff-ng.git] / oui.c
blobe735c159c7031e792483cd1cbe8f7433073b8dc6
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 "xutils.h"
13 #include "oui.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;
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, &ptr, 0);
59 if ((ptr = strstr(buff, ", ")))
60 ptr += strlen(", ");
61 ptr = strtrim_right(ptr, '\n');
62 ptr = strtrim_right(ptr, ' ');
64 v->vendor = xstrdup(ptr);
65 v->next = NULL;
67 pos = insert_hash(v->id, v, &oui);
68 if (pos) {
69 v->next = *pos;
70 *pos = v;
73 memset(buff, 0, sizeof(buff));
76 fclose(fp);
77 initialized = true;
80 static int dissector_cleanup_oui_hash(void *ptr)
82 struct vendor_id *tmp, *v = ptr;
84 if (!ptr)
85 return 0;
87 while ((tmp = v->next)) {
88 xfree(v->vendor);
89 xfree(v);
90 v = tmp;
93 xfree(v->vendor);
94 xfree(v);
96 return 0;
99 void dissector_cleanup_oui(void)
101 for_each_hash(&oui, dissector_cleanup_oui_hash);
102 free_hash(&oui);
103 initialized = false;