docs: install: Fix a minor typo
[netsniff-ng.git] / dissector.c
blobed6c4dea026ba100ecb7652336bc14af466937cc
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 "dissector.h"
16 #include "dissector_eth.h"
17 #include "dissector_80211.h"
19 int dissector_set_print_type(void *ptr, int type)
21 struct protocol *proto;
23 for (proto = ptr; proto; proto = proto->next) {
24 switch (type) {
25 case PRINT_NORM:
26 proto->process = proto->print_full;
27 break;
28 case PRINT_LESS:
29 proto->process = proto->print_less;
30 break;
31 default:
32 proto->process = NULL;
33 break;
37 return 0;
40 static void dissector_main(struct pkt_buff *pkt, struct protocol *start,
41 struct protocol *end)
43 struct protocol *proto;
45 if (!start)
46 return;
48 for (pkt->proto = start; pkt->proto; ) {
49 if (unlikely(!pkt->proto->process))
50 break;
52 proto = pkt->proto;
53 pkt->proto = NULL;
54 proto->process(pkt);
57 if (end && likely(end->process))
58 end->process(pkt);
61 void dissector_entry_point(uint8_t *packet, size_t len, int linktype, int mode)
63 struct protocol *proto_start, *proto_end;
64 struct pkt_buff *pkt;
66 if (mode == PRINT_NONE)
67 return;
69 pkt = pkt_alloc(packet, len);
71 switch (linktype) {
72 case LINKTYPE_EN10MB:
73 case ___constant_swab32(LINKTYPE_EN10MB):
74 proto_start = dissector_get_ethernet_entry_point();
75 proto_end = dissector_get_ethernet_exit_point();
76 break;
77 case LINKTYPE_IEEE802_11:
78 case ___constant_swab32(LINKTYPE_IEEE802_11):
79 proto_start = dissector_get_ieee80211_entry_point();
80 proto_end = dissector_get_ieee80211_exit_point();
81 break;
82 default:
83 proto_start = &none_ops;
84 proto_end = NULL;
85 break;
88 dissector_main(pkt, proto_start, proto_end);
90 switch (mode) {
91 case PRINT_HEX:
92 hex(pkt);
93 break;
94 case PRINT_ASCII:
95 ascii(pkt);
96 break;
97 case PRINT_HEX_ASCII:
98 hex_ascii(pkt);
99 break;
102 tprintf_flush();
103 pkt_free(pkt);
106 void dissector_init_all(int fnttype)
108 dissector_init_ethernet(fnttype);
109 dissector_init_ieee80211(fnttype);
112 void dissector_cleanup_all(void)
114 dissector_cleanup_ethernet();
115 dissector_cleanup_ieee80211();