netsniff-ng: add comment wrt NOATIME and fix whitespace
[netsniff-ng.git] / dissector.c
blob4cad588fa60ef3a5dd25b8af2b951a1b39669227
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"
18 #include "dissector_netlink.h"
19 #include "linktype.h"
21 int dissector_set_print_type(void *ptr, int type)
23 struct protocol *proto;
25 for (proto = ptr; proto; proto = proto->next) {
26 switch (type) {
27 case PRINT_NORM:
28 proto->process = proto->print_full;
29 break;
30 case PRINT_LESS:
31 proto->process = proto->print_less;
32 break;
33 default:
34 proto->process = NULL;
35 break;
39 return 0;
42 static void dissector_main(struct pkt_buff *pkt, struct protocol *start,
43 struct protocol *end)
45 struct protocol *dissector;
47 if (!start)
48 return;
50 for (pkt->dissector = start; pkt->dissector; ) {
51 if (unlikely(!pkt->dissector->process))
52 break;
54 dissector = pkt->dissector;
55 pkt->dissector = NULL;
56 dissector->process(pkt);
59 if (end && likely(end->process))
60 end->process(pkt);
63 void dissector_entry_point(uint8_t *packet, size_t len, int linktype, int mode,
64 uint16_t proto)
66 struct protocol *proto_start, *proto_end;
67 struct pkt_buff *pkt;
69 if (mode == PRINT_NONE)
70 return;
72 pkt = pkt_alloc(packet, len);
73 pkt->link_type = linktype;
74 pkt->proto = proto;
76 switch (linktype) {
77 case LINKTYPE_EN10MB:
78 case ___constant_swab32(LINKTYPE_EN10MB):
79 proto_start = dissector_get_ethernet_entry_point();
80 proto_end = dissector_get_ethernet_exit_point();
81 break;
82 case LINKTYPE_IEEE802_11_RADIOTAP:
83 case ___constant_swab32(LINKTYPE_IEEE802_11_RADIOTAP):
84 case LINKTYPE_IEEE802_11:
85 case ___constant_swab32(LINKTYPE_IEEE802_11):
86 proto_start = dissector_get_ieee80211_entry_point();
87 proto_end = dissector_get_ieee80211_exit_point();
88 break;
89 case LINKTYPE_NETLINK:
90 case ___constant_swab32(LINKTYPE_NETLINK):
91 proto_start = dissector_get_netlink_entry_point();
92 proto_end = dissector_get_netlink_exit_point();
93 break;
94 default:
95 proto_start = &none_ops;
96 proto_end = NULL;
97 break;
100 dissector_main(pkt, proto_start, proto_end);
102 switch (mode) {
103 case PRINT_HEX:
104 hex(pkt);
105 break;
106 case PRINT_ASCII:
107 ascii(pkt);
108 break;
109 case PRINT_HEX_ASCII:
110 hex_ascii(pkt);
111 break;
114 tprintf_flush();
115 pkt_free(pkt);
118 void dissector_init_all(int fnttype)
120 dissector_init_ethernet(fnttype);
121 dissector_init_ieee80211(fnttype);
122 dissector_init_netlink(fnttype);
125 void dissector_cleanup_all(void)
127 dissector_cleanup_ethernet();
128 dissector_cleanup_ieee80211();
129 dissector_cleanup_netlink();