trafgen: moved cleanup into parser
[netsniff-ng-old.git] / src / dissector.c
blobaf920cb8a6bac73f8993e8162d79b1faf18df768
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_struct.h"
16 #define __without_ops
17 #include "proto_none.h"
18 #include "dissector.h"
19 #include "dissector_eth.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 for (pkt->proto = start; pkt->proto; ) {
52 if (unlikely(!pkt->proto->process))
53 break;
55 proto = pkt->proto;
56 pkt->proto = NULL;
57 proto->process(pkt);
60 if (end && likely(end->process))
61 end->process(pkt);
64 void dissector_entry_point(uint8_t *packet, size_t len, int linktype, int mode)
66 struct protocol *proto_start = NULL;
67 struct protocol *proto_end = NULL;
68 struct pkt_buff *pkt = NULL;
70 if (mode == FNTTYPE_PRINT_NONE)
71 return;
73 pkt = pkt_alloc(packet, len);
75 switch (linktype) {
76 case LINKTYPE_EN10MB:
77 proto_start = dissector_get_ethernet_entry_point();
78 proto_end = dissector_get_ethernet_exit_point();
79 break;
80 default:
81 panic("Linktype not supported!\n");
84 dissector_main(pkt, proto_start, proto_end);
86 switch (mode) {
87 case FNTTYPE_PRINT_HEX:
88 hex(pkt);
89 break;
90 case FNTTYPE_PRINT_ASCII:
91 ascii(pkt);
92 break;
93 case FNTTYPE_PRINT_HEX_ASCII:
94 hex_ascii(pkt);
95 break;
98 tprintf_flush();
99 pkt_free(pkt);
102 void dissector_init_all(int fnttype)
104 dissector_init_ethernet(fnttype);
107 void dissector_cleanup_all(void)
109 dissector_cleanup_ethernet();