proto_icmpv6.h: intermediate state
[netsniff-ng.git] / src / dissector.c
blob4afb8552e05b14cb36c8914f98eb6081b8e449a9
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 "dissector.h"
15 #include "dissector_eth.h"
16 #include "pkt_buff.h"
17 #include "proto_struct.h"
19 int dissector_set_print_type(void *ptr, int type)
21 struct protocol *proto;
23 for (proto = (struct protocol *) ptr; proto; proto = proto->next) {
24 switch (type) {
25 case FNTTYPE_PRINT_NORM:
26 proto->process = proto->print_full;
27 break;
28 case FNTTYPE_PRINT_LESS:
29 proto->process = proto->print_less;
30 break;
31 case FNTTYPE_PRINT_HEX:
32 case FNTTYPE_PRINT_ASCII:
33 case FNTTYPE_PRINT_NONE:
34 default:
35 proto->process = NULL;
36 break;
40 return 0;
43 static void dissector_main(struct pkt_buff *pkt, struct protocol *start,
44 struct protocol *end)
46 struct protocol *proto;
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 = NULL;
64 struct protocol *proto_end = NULL;
65 struct pkt_buff *pkt = NULL;
67 if (mode == FNTTYPE_PRINT_NONE)
68 return;
70 pkt = pkt_alloc(packet, len);
72 switch (linktype) {
73 case LINKTYPE_EN10MB:
74 proto_start = dissector_get_ethernet_entry_point();
75 proto_end = dissector_get_ethernet_exit_point();
76 break;
77 default:
78 panic("Linktype not supported!\n");
81 dissector_main(pkt, proto_start, proto_end);
83 switch (mode) {
84 case FNTTYPE_PRINT_HEX:
85 hex(pkt);
86 break;
87 case FNTTYPE_PRINT_ASCII:
88 ascii(pkt);
89 break;
92 tprintf_flush();
93 pkt_free(pkt);
96 void dissector_init_all(int fnttype)
98 dissector_init_ethernet(fnttype);
101 void dissector_cleanup_all(void)
103 dissector_cleanup_ethernet();