astraceroute: zsh: add auto-completion entry
[netsniff-ng.git] / dissector.c
blob549ce1e0b9f62bbf84c03202c1b7a939f7d67ea6
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 "protos.h"
16 #include "dissector.h"
17 #include "dissector_eth.h"
18 #include "dissector_80211.h"
20 int dissector_set_print_type(void *ptr, int type)
22 struct protocol *proto;
24 for (proto = ptr; proto; proto = proto->next) {
25 switch (type) {
26 case PRINT_NORM:
27 proto->process = proto->print_full;
28 break;
29 case PRINT_LESS:
30 proto->process = proto->print_less;
31 break;
32 default:
33 proto->process = NULL;
34 break;
38 return 0;
41 static void dissector_main(struct pkt_buff *pkt, struct protocol *start,
42 struct protocol *end)
44 struct protocol *proto;
46 if (!start)
47 return;
49 for (pkt->proto = start; pkt->proto; ) {
50 if (unlikely(!pkt->proto->process))
51 break;
53 proto = pkt->proto;
54 pkt->proto = NULL;
55 proto->process(pkt);
58 if (end && likely(end->process))
59 end->process(pkt);
62 void dissector_entry_point(uint8_t *packet, size_t len, int linktype, int mode)
64 struct protocol *proto_start, *proto_end;
65 struct pkt_buff *pkt;
67 if (mode == PRINT_NONE)
68 return;
70 pkt = pkt_alloc(packet, len);
72 switch (linktype) {
73 case LINKTYPE_EN10MB:
74 case ___constant_swab32(LINKTYPE_EN10MB):
75 proto_start = dissector_get_ethernet_entry_point();
76 proto_end = dissector_get_ethernet_exit_point();
77 break;
78 case LINKTYPE_IEEE802_11:
79 case ___constant_swab32(LINKTYPE_IEEE802_11):
80 proto_start = dissector_get_ieee80211_entry_point();
81 proto_end = dissector_get_ieee80211_exit_point();
82 break;
83 default:
84 proto_start = &none_ops;
85 proto_end = NULL;
86 break;
89 dissector_main(pkt, proto_start, proto_end);
91 switch (mode) {
92 case PRINT_HEX:
93 hex(pkt);
94 break;
95 case PRINT_ASCII:
96 ascii(pkt);
97 break;
98 case PRINT_HEX_ASCII:
99 hex_ascii(pkt);
100 break;
103 tprintf_flush();
104 pkt_free(pkt);
107 void dissector_init_all(int fnttype)
109 dissector_init_ethernet(fnttype);
110 dissector_init_ieee80211(fnttype);
113 void dissector_cleanup_all(void)
115 dissector_cleanup_ethernet();
116 dissector_cleanup_ieee80211();