dissector_80211: start of 80211 collection
[netsniff-ng-old.git] / src / dissector_80211.c
bloba2313f488bd4087335f0ffef231fa8c0365c9a95
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
5 * Subject to the GPL, version 2.
6 */
8 #include <stdint.h>
10 #include "hash.h"
11 #include "protos.h"
12 #include "pkt_buff.h"
13 #include "dissector.h"
14 #include "dissector_80211.h"
15 #include "xmalloc.h"
16 #include "xstring.h"
18 struct hash_table ieee80211_lay2;
20 static struct hash_table ieee80211_oui;
22 struct vendor_id {
23 unsigned int id;
24 char *vendor;
25 struct vendor_id *next;
28 /* Note: this macro only applies to the lookup_* functions here in this file,
29 * mainly to remove redundand code. */
30 #define __do_lookup_inline(id, struct_name, hash_ptr, struct_member) \
31 ({ \
32 struct struct_name *entry = lookup_hash(id, hash_ptr); \
33 while (entry && id != entry->id) \
34 entry = entry->next; \
35 (entry && id == entry->id ? entry->struct_member : "Unknown");\
38 char *lookup_vendor(unsigned int id)
40 return __do_lookup_inline(id, vendor_id, &ieee80211_oui, vendor);
43 static inline void dissector_init_entry(int type)
45 dissector_set_print_type(&ieee80211_mac_ops, type);
48 static inline void dissector_init_exit(int type)
50 dissector_set_print_type(&none_ops, type);
53 static void dissector_init_layer_2(int type)
55 init_hash(&ieee80211_lay2);
56 // INSERT_HASH_PROTOS(arp_ops, eth_lay2);
57 // INSERT_HASH_PROTOS(vlan_ops, eth_lay2);
58 // INSERT_HASH_PROTOS(ipv4_ops, eth_lay2);
59 // INSERT_HASH_PROTOS(ipv6_ops, eth_lay2);
60 for_each_hash_int(&ieee80211_lay2, dissector_set_print_type, type);
63 static void dissector_init_oui(void)
65 FILE *fp;
66 char buff[512], *ptr;
67 struct vendor_id *ven;
68 void **pos;
69 fp = fopen("/etc/netsniff-ng/oui.conf", "r");
70 if (!fp)
71 panic("No /etc/netsniff-ng/oui.conf found!\n");
72 memset(buff, 0, sizeof(buff));
73 while (fgets(buff, sizeof(buff), fp) != NULL) {
74 buff[sizeof(buff) - 1] = 0;
75 ven = xmalloc(sizeof(*ven));
76 ptr = buff;
77 ptr = skips(ptr);
78 ptr = getuint(ptr, &ven->id);
79 ptr = skips(ptr);
80 ptr = skipchar(ptr, ',');
81 ptr = skips(ptr);
82 ptr = strtrim_right(ptr, '\n');
83 ptr = strtrim_right(ptr, ' ');
84 ven->vendor = xstrdup(ptr);
85 ven->next = NULL;
86 pos = insert_hash(ven->id, ven, &ieee80211_oui);
87 if (pos) {
88 ven->next = *pos;
89 *pos = ven;
91 memset(buff, 0, sizeof(buff));
93 fclose(fp);
96 static int dissector_cleanup_oui(void *ptr)
98 struct vendor_id *tmp, *v = ptr;
99 if (!ptr)
100 return 0;
101 while ((tmp = v->next)) {
102 xfree(v->vendor);
103 xfree(v);
104 v = tmp;
106 xfree(v->vendor);
107 xfree(v);
108 return 0;
111 void dissector_init_ieee80211(int fnttype)
113 dissector_init_entry(fnttype);
114 dissector_init_layer_2(fnttype);
115 dissector_init_exit(fnttype);
116 dissector_init_oui();
119 void dissector_cleanup_ieee80211(void)
121 free_hash(&ieee80211_lay2);
122 for_each_hash(&ieee80211_oui, dissector_cleanup_oui);
123 free_hash(&ieee80211_oui);