bpf: add minimal BPF JIT emitted image disassembler
[netsniff-ng.git] / dissector.c
blob167d946511f91519095ea08b7871f04aa39a373c
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010, 2011, 2012 Daniel Borkmann.
4 * Subject to the GPL, version 2.
5 */
7 #include <stdint.h>
8 #include <stdlib.h>
9 #include <unistd.h>
11 #include "built_in.h"
12 #include "tprintf.h"
13 #include "pkt_buff.h"
14 #include "proto.h"
15 #include "protos.h"
16 #include "dissector.h"
17 #include "dissector_eth.h"
18 #include "dissector_80211.h"
20 int dissector_set_print_type(void *ptr, int type)
22 struct protocol *proto;
24 for (proto = ptr; proto; proto = proto->next) {
25 switch (type) {
26 case PRINT_NORM:
27 proto->process = proto->print_full;
28 break;
29 case PRINT_LESS:
30 proto->process = proto->print_less;
31 break;
32 default:
33 proto->process = NULL;
34 break;
38 return 0;
41 static void dissector_main(struct pkt_buff *pkt, struct protocol *start,
42 struct protocol *end)
44 struct protocol *proto;
46 if (!start)
47 return;
49 for (pkt->proto = start; pkt->proto; ) {
50 if (unlikely(!pkt->proto->process))
51 break;
53 proto = pkt->proto;
54 pkt->proto = NULL;
55 proto->process(pkt);
58 if (end && likely(end->process))
59 end->process(pkt);
62 void dissector_entry_point(uint8_t *packet, size_t len, int linktype, int mode)
64 struct protocol *proto_start, *proto_end;
65 struct pkt_buff *pkt = NULL;
67 if (mode == PRINT_NONE)
68 return;
70 pkt = pkt_alloc(packet, len);
72 switch (linktype) {
73 case LINKTYPE_EN10MB:
74 case ___constant_swab32(LINKTYPE_EN10MB):
75 proto_start = dissector_get_ethernet_entry_point();
76 proto_end = dissector_get_ethernet_exit_point();
77 break;
78 case LINKTYPE_IEEE802_11:
79 case ___constant_swab32(LINKTYPE_IEEE802_11):
80 proto_start = dissector_get_ieee80211_entry_point();
81 proto_end = dissector_get_ieee80211_exit_point();
82 break;
83 default:
84 panic("Linktype not supported!\n");
87 dissector_main(pkt, proto_start, proto_end);
89 switch (mode) {
90 case PRINT_HEX:
91 hex(pkt);
92 break;
93 case PRINT_ASCII:
94 ascii(pkt);
95 break;
96 case PRINT_HEX_ASCII:
97 hex_ascii(pkt);
98 break;
101 tprintf_flush();
102 pkt_free(pkt);
105 void dissector_init_all(int fnttype)
107 dissector_init_ethernet(fnttype);
108 dissector_init_ieee80211(fnttype);
111 void dissector_cleanup_all(void)
113 dissector_cleanup_ethernet();
114 dissector_cleanup_ieee80211();