proto_ipv4: don't trim length of pkt_buff
[netsniff-ng.git] / src / dissector.c
blobad3a31550d23ccc762260958b7c9dc1442a90849
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010, 2011, 2012 Daniel Borkmann.
5 * Subject to the GPL, version 2.
6 */
8 #include <stdint.h>
9 #include <stdlib.h>
10 #include <unistd.h>
12 #include "built_in.h"
13 #include "tprintf.h"
14 #include "pkt_buff.h"
15 #include "proto.h"
16 #include "protos.h"
17 #include "dissector.h"
18 #include "dissector_eth.h"
19 #include "dissector_80211.h"
21 int dissector_set_print_type(void *ptr, int type)
23 struct protocol *proto;
25 for (proto = (struct protocol *) ptr; proto; proto = proto->next) {
26 switch (type) {
27 case FNTTYPE_PRINT_NORM:
28 proto->process = proto->print_full;
29 break;
30 case FNTTYPE_PRINT_LESS:
31 proto->process = proto->print_less;
32 break;
33 case FNTTYPE_PRINT_HEX:
34 case FNTTYPE_PRINT_ASCII:
35 case FNTTYPE_PRINT_HEX_ASCII:
36 case FNTTYPE_PRINT_NONE:
37 default:
38 proto->process = NULL;
39 break;
43 return 0;
46 static void dissector_main(struct pkt_buff *pkt, struct protocol *start,
47 struct protocol *end)
49 struct protocol *proto;
51 if (!start)
52 return;
54 for (pkt->proto = start; pkt->proto; ) {
55 if (unlikely(!pkt->proto->process))
56 break;
58 proto = pkt->proto;
59 pkt->proto = NULL;
60 proto->process(pkt);
63 if (end && likely(end->process))
64 end->process(pkt);
67 void dissector_entry_point(uint8_t *packet, size_t len, int linktype, int mode)
69 struct protocol *proto_start = NULL;
70 struct protocol *proto_end = NULL;
71 struct pkt_buff *pkt = NULL;
73 if (mode == FNTTYPE_PRINT_NONE)
74 return;
76 pkt = pkt_alloc(packet, len);
78 switch (linktype) {
79 case LINKTYPE_EN10MB:
80 proto_start = dissector_get_ethernet_entry_point();
81 proto_end = dissector_get_ethernet_exit_point();
82 break;
83 case LINKTYPE_IEEE802_11:
84 proto_start = dissector_get_ieee80211_entry_point();
85 proto_end = dissector_get_ieee80211_exit_point();
86 break;
87 default:
88 panic("Linktype not supported!\n");
91 dissector_main(pkt, proto_start, proto_end);
93 switch (mode) {
94 case FNTTYPE_PRINT_HEX:
95 hex(pkt);
96 break;
97 case FNTTYPE_PRINT_ASCII:
98 ascii(pkt);
99 break;
100 case FNTTYPE_PRINT_HEX_ASCII:
101 hex_ascii(pkt);
102 break;
105 tprintf_flush();
106 pkt_free(pkt);
109 void dissector_init_all(int fnttype)
111 dissector_init_ethernet(fnttype);
112 dissector_init_ieee80211(fnttype);
115 void dissector_cleanup_all(void)
117 dissector_cleanup_ethernet();
118 dissector_cleanup_ieee80211();