trafgen: proto: Update field value at runtime
[netsniff-ng-new.git] / dissector.c
blob6aa253dfa9da71c901aea60eee0cd6886c53cd7b
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_sll.h"
18 #include "dissector_80211.h"
19 #include "dissector_netlink.h"
20 #include "linktype.h"
22 int dissector_set_print_type(void *ptr, int type)
24 struct protocol *proto;
26 for (proto = ptr; proto; proto = proto->next) {
27 switch (type) {
28 case PRINT_NORM:
29 proto->process = proto->print_full;
30 break;
31 case PRINT_LESS:
32 proto->process = proto->print_less;
33 break;
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 *dissector;
48 if (!start)
49 return;
51 for (pkt->dissector = start; pkt->dissector; ) {
52 if (unlikely(!pkt->dissector->process))
53 break;
55 dissector = pkt->dissector;
56 pkt->dissector = NULL;
57 dissector->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,
65 struct sockaddr_ll *sll)
67 struct protocol *proto_start, *proto_end;
68 struct pkt_buff *pkt;
70 if (mode == PRINT_NONE)
71 return;
73 pkt = pkt_alloc(packet, len);
74 pkt->link_type = linktype;
75 pkt->sll = sll;
77 switch (linktype) {
78 case LINKTYPE_EN10MB:
79 case ___constant_swab32(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_RADIOTAP:
84 case ___constant_swab32(LINKTYPE_IEEE802_11_RADIOTAP):
85 case LINKTYPE_IEEE802_11:
86 case ___constant_swab32(LINKTYPE_IEEE802_11):
87 proto_start = dissector_get_ieee80211_entry_point();
88 proto_end = dissector_get_ieee80211_exit_point();
89 break;
90 case LINKTYPE_NETLINK:
91 case ___constant_swab32(LINKTYPE_NETLINK):
92 proto_start = dissector_get_netlink_entry_point();
93 proto_end = dissector_get_netlink_exit_point();
94 break;
95 case LINKTYPE_LINUX_SLL:
96 case ___constant_swab32(LINKTYPE_LINUX_SLL):
97 proto_start = dissector_get_sll_entry_point();
98 proto_end = dissector_get_sll_exit_point();
99 break;
100 default:
101 proto_start = &none_ops;
102 proto_end = NULL;
103 break;
106 dissector_main(pkt, proto_start, proto_end);
108 switch (mode) {
109 case PRINT_HEX:
110 hex(pkt);
111 break;
112 case PRINT_ASCII:
113 ascii(pkt);
114 break;
115 case PRINT_HEX_ASCII:
116 hex_ascii(pkt);
117 break;
120 tprintf_flush();
121 pkt_free(pkt);
124 void dissector_init_all(int fnttype)
126 dissector_init_ethernet(fnttype);
127 dissector_init_ieee80211(fnttype);
128 dissector_init_netlink(fnttype);
129 dissector_init_sll(fnttype);
132 void dissector_cleanup_all(void)
134 dissector_cleanup_ethernet();
135 dissector_cleanup_ieee80211();
136 dissector_cleanup_netlink();
137 dissector_cleanup_sll();