Merge branch 'master' of git://github.com/borkmann/netsniff-ng
[netsniff-ng.git] / src / dissector.c
blob568b36296f64c2c8ccfba1e7651b6cbe54c587de
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 = (struct protocol *) ptr;
23 while (proto != NULL) {
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 default:
32 case FNTTYPE_PRINT_NONE:
33 proto->process = NULL;
34 break;
37 proto = proto->next;
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 != NULL;) {
49 if (unlikely(!pkt->proto->process))
50 break;
52 proto = pkt->proto;
53 pkt->proto = NULL;
54 proto->process(pkt);
57 if (end != NULL)
58 if (likely(end->process))
59 end->process(pkt);
61 tprintf_flush();
64 void dissector_entry_point(uint8_t *packet, size_t len, int linktype)
66 struct protocol *proto_start = NULL;
67 struct protocol *proto_end = NULL;
68 struct pkt_buff *pkt = pkt_alloc(packet, len);
70 switch (linktype) {
71 case LINKTYPE_EN10MB:
72 proto_start = dissector_get_ethernet_entry_point();
73 proto_end = dissector_get_ethernet_exit_point();
74 break;
75 default:
76 panic("Linktype not supported!\n");
79 dissector_main(pkt, proto_start, proto_end);
81 pkt_free(pkt);
84 void dissector_init_all(int fnttype)
86 dissector_init_ethernet(fnttype);
89 void dissector_cleanup_all(void)
91 dissector_cleanup_ethernet();