diff options
author | Tobias Klauser <tklauser@distanz.ch> | 2015-11-16 08:46:00 +0100 |
---|---|---|
committer | Tobias Klauser <tklauser@distanz.ch> | 2015-11-16 08:46:00 +0100 |
commit | 04b2adac4e5d460f39454b59ed4f2d3374cb796b (patch) | |
tree | caf1aeb824fcfeba9437dea02741dd9921ad718b | |
parent | 7cec7def47bf55762c422508bac1b6e2e142b24e (diff) |
lookup: Use lookup module for OUI lookup as well
Do not needlessly duplicate code between the oui and the lookup module.
Instead, add an additional lookup table for OUIs to the lookup module.
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
-rw-r--r-- | dissector_80211.c | 7 | ||||
-rw-r--r-- | dissector_eth.c | 7 | ||||
-rw-r--r-- | dissector_sll.c | 6 | ||||
-rw-r--r-- | flowtop/Makefile | 1 | ||||
-rw-r--r-- | lookup.c | 6 | ||||
-rw-r--r-- | lookup.h | 7 | ||||
-rw-r--r-- | netsniff-ng/Makefile | 1 | ||||
-rw-r--r-- | oui.c | 118 | ||||
-rw-r--r-- | oui.h | 13 | ||||
-rw-r--r-- | proto_80211_mac_hdr.c | 2 | ||||
-rw-r--r-- | proto_ethernet.c | 1 | ||||
-rw-r--r-- | proto_lldp.c | 2 |
12 files changed, 23 insertions, 148 deletions
diff --git a/dissector_80211.c b/dissector_80211.c index 28f5526..ecaf355 100644 --- a/dissector_80211.c +++ b/dissector_80211.c @@ -10,8 +10,7 @@ #include "protos.h" #include "dissector.h" #include "dissector_80211.h" -#include "xmalloc.h" -#include "oui.h" +#include "lookup.h" struct hash_table ieee80211_lay2; @@ -37,11 +36,11 @@ void dissector_init_ieee80211(int fnttype) dissector_init_entry(fnttype); dissector_init_layer_2(fnttype); dissector_init_exit(fnttype); - dissector_init_oui(); + lookup_init(LT_OUI); } void dissector_cleanup_ieee80211(void) { free_hash(&ieee80211_lay2); - dissector_cleanup_oui(); + lookup_cleanup(LT_OUI); } diff --git a/dissector_eth.c b/dissector_eth.c index b2c44df..7041aac 100644 --- a/dissector_eth.c +++ b/dissector_eth.c @@ -7,7 +7,6 @@ #include <stdint.h> #include "hash.h" -#include "oui.h" #include "proto.h" #include "protos.h" #include "dissector.h" @@ -68,11 +67,10 @@ void dissector_init_ethernet(int fnttype) dissector_init_layer_3(fnttype); dissector_init_exit(fnttype); - dissector_init_oui(); - lookup_init(LT_PORTS_UDP); lookup_init(LT_PORTS_TCP); lookup_init(LT_ETHERTYPES); + lookup_init(LT_OUI); } void dissector_cleanup_ethernet(void) @@ -80,9 +78,8 @@ void dissector_cleanup_ethernet(void) free_hash(ð_lay2); free_hash(ð_lay3); + lookup_cleanup(LT_OUI); lookup_cleanup(LT_ETHERTYPES); lookup_cleanup(LT_PORTS_TCP); lookup_cleanup(LT_PORTS_UDP); - - dissector_cleanup_oui(); } diff --git a/dissector_sll.c b/dissector_sll.c index ac8f017..cde0d54 100644 --- a/dissector_sll.c +++ b/dissector_sll.c @@ -3,13 +3,13 @@ * Subject to the GPL, version 2. */ -#include "oui.h" #include "protos.h" #include "pcap_io.h" #include "pkt_buff.h" #include "dissector.h" #include "dissector_sll.h" #include "dissector_eth.h" +#include "lookup.h" static char *pkt_type2str(uint8_t pkttype) { @@ -98,10 +98,10 @@ void dissector_init_sll(int fnttype) { dissector_set_print_type(&sll_ops, fnttype); dissector_set_print_type(&none_ops, fnttype); - dissector_init_oui(); + lookup_init(LT_OUI); } void dissector_cleanup_sll(void) { - dissector_cleanup_oui(); + lookup_cleanup(LT_OUI); } diff --git a/flowtop/Makefile b/flowtop/Makefile index bc7afb5..ddb031c 100644 --- a/flowtop/Makefile +++ b/flowtop/Makefile @@ -11,7 +11,6 @@ flowtop-libs += -lGeoIP \ endif flowtop-objs = xmalloc.o \ - oui.o \ str.o \ sig.o \ sock.o \ @@ -21,6 +21,7 @@ static const char * const lookup_files[] = { [LT_PORTS_UDP] = ETCDIRE_STRING "/udp.conf", [LT_PORTS_TCP] = ETCDIRE_STRING "/tcp.conf", [LT_ETHERTYPES] = ETCDIRE_STRING "/ether.conf", + [LT_OUI] = ETCDIRE_STRING "/oui.conf", }; struct lookup_entry { @@ -150,3 +151,8 @@ char *lookup_port_tcp(unsigned int id) { return __do_lookup_inline(id, &lookup_tables[LT_PORTS_TCP]); } + +char *lookup_vendor(unsigned int id) +{ + return __do_lookup_inline(id, &lookup_tables[LT_OUI]); +} @@ -12,6 +12,7 @@ enum lookup_type { LT_PORTS_UDP, LT_PORTS_TCP, LT_ETHERTYPES, + LT_OUI, LT_MAX, }; @@ -21,5 +22,11 @@ extern void lookup_cleanup(enum lookup_type which); extern char *lookup_port_udp(unsigned int id); extern char *lookup_port_tcp(unsigned int id); extern char *lookup_ether_type(unsigned int id); +extern char *lookup_vendor(unsigned int id); + +static inline const char *lookup_vendor_str(unsigned int id) +{ + return lookup_vendor(id) ? : "Unknown"; +} #endif /* LOOKUP_H */ diff --git a/netsniff-ng/Makefile b/netsniff-ng/Makefile index 03e306e..6f9d0b2 100644 --- a/netsniff-ng/Makefile +++ b/netsniff-ng/Makefile @@ -55,7 +55,6 @@ netsniff-ng-objs = dissector.o \ xmalloc.o \ hash.o \ bpf.o \ - oui.o \ pcap_rw.o \ pcap_sg.o \ pcap_mm.o \ @@ -1,118 +0,0 @@ -/* - * netsniff-ng - the packet sniffing beast - * Copyright 2009 - 2013 Daniel Borkmann. - * Subject to the GPL, version 2. - */ - -#include <stdint.h> -#include <stdbool.h> - -#include "hash.h" -#include "xmalloc.h" -#include "oui.h" -#include "str.h" - -static struct hash_table oui; - -static bool initialized = false; - -struct vendor_id { - unsigned int id; - char *vendor; - struct vendor_id *next; -}; - -const char *lookup_vendor(unsigned int id) -{ - struct vendor_id *v; - - v = lookup_hash(id, &oui); - while (v && id != v->id) - v = v->next; - - return (v && id == v->id ? v->vendor : NULL); -} - -void dissector_init_oui(void) -{ - FILE *fp; - char buff[128], *ptr, *end; - struct vendor_id *v; - void **pos; - - if (initialized) - return; - - fp = fopen(ETCDIRE_STRING "/oui.conf", "r"); - if (!fp) - panic("No oui.conf found!\n"); - - memset(buff, 0, sizeof(buff)); - - while (fgets(buff, sizeof(buff), fp) != NULL) { - buff[sizeof(buff) - 1] = 0; - ptr = buff; - - v = xmalloc(sizeof(*v)); - v->id = strtol(ptr, &end, 0); - /* not a valid line, skip */ - if (v->id == 0 && end == ptr) { - xfree(v); - continue; - } - - ptr = strstr(buff, ", "); - /* likewise */ - if (!ptr) { - xfree(v); - continue; - } - - ptr += strlen(", "); - ptr = strtrim_right(ptr, '\n'); - ptr = strtrim_right(ptr, ' '); - - v->vendor = xstrdup(ptr); - v->next = NULL; - - pos = insert_hash(v->id, v, &oui); - if (pos) { - v->next = *pos; - *pos = v; - } - - memset(buff, 0, sizeof(buff)); - } - - fclose(fp); - initialized = true; -} - -static int dissector_cleanup_oui_hash(void *ptr) -{ - struct vendor_id *tmp, *v = ptr; - - if (!ptr) - return 0; - - while ((tmp = v->next)) { - xfree(v->vendor); - xfree(v); - v = tmp; - } - - xfree(v->vendor); - xfree(v); - - return 0; -} - -void dissector_cleanup_oui(void) -{ - if (!initialized) - return; - - for_each_hash(&oui, dissector_cleanup_oui_hash); - free_hash(&oui); - initialized = false; -} @@ -1,13 +0,0 @@ -#ifndef OUI_H -#define OUI_H - -extern const char *lookup_vendor(unsigned int id); -extern void dissector_init_oui(void); -extern void dissector_cleanup_oui(void); - -static inline const char *lookup_vendor_str(unsigned int id) -{ - return lookup_vendor(id) ? : "Unknown"; -} - -#endif /* OUI_H */ diff --git a/proto_80211_mac_hdr.c b/proto_80211_mac_hdr.c index c7b5b15..14e136d 100644 --- a/proto_80211_mac_hdr.c +++ b/proto_80211_mac_hdr.c @@ -21,8 +21,8 @@ #include "dissector_80211.h" #include "built_in.h" #include "pkt_buff.h" -#include "oui.h" #include "linktype.h" +#include "lookup.h" #define TU 0.001024 diff --git a/proto_ethernet.c b/proto_ethernet.c index a8e10cf..47730fc 100644 --- a/proto_ethernet.c +++ b/proto_ethernet.c @@ -14,7 +14,6 @@ #include "dissector_eth.h" #include "lookup.h" #include "pkt_buff.h" -#include "oui.h" static inline bool is_multicast_ether_addr(const uint8_t *mac) { diff --git a/proto_lldp.c b/proto_lldp.c index 86d6bc8..4f71ec6 100644 --- a/proto_lldp.c +++ b/proto_lldp.c @@ -9,7 +9,7 @@ #include <netinet/in.h> /* for ntohs()/ntohl() */ #include "built_in.h" -#include "oui.h" +#include "lookup.h" #include "pkt_buff.h" #include "proto.h" #include "protos.h" |