docs: fixed minor typo
[netsniff-ng.git] / src / dissector.c
blobe57c8efb9446f1a54ed7e13f6691d0da367d046a
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
5 * Subject to the GPL, version 2.
6 */
8 #include <stdint.h>
9 #include <stdlib.h>
10 #include <unistd.h>
12 #include "compiler.h"
13 #include "tprintf.h"
14 #include "dissector.h"
15 #include "dissector_eth.h"
16 #include "proto_struct.h"
18 /* TODO: Refactoring duplicate code, clean up code, also in protos */
19 int dissector_set_print_norm(void *ptr)
21 struct protocol *proto = (struct protocol *) ptr;
22 while (proto != NULL) {
23 proto->process = proto->print_full;
24 proto = proto->next;
26 return 0;
29 int dissector_set_print_less(void *ptr)
31 struct protocol *proto = (struct protocol *) ptr;
32 while (proto != NULL) {
33 proto->process = proto->print_less;
34 proto = proto->next;
36 return 0;
39 int dissector_set_print_none(void *ptr)
41 struct protocol *proto = (struct protocol *) ptr;
42 while (proto != NULL) {
43 proto->process = NULL;
44 proto = proto->next;
46 return 0;
49 int dissector_set_print_payload(void *ptr)
51 struct protocol *proto = (struct protocol *) ptr;
52 while (proto != NULL) {
53 proto->process = proto->print_pay_ascii;
54 proto = proto->next;
56 return 0;
59 int dissector_set_print_payload_hex(void *ptr)
61 struct protocol *proto = (struct protocol *) ptr;
62 while (proto != NULL) {
63 proto->process = proto->print_pay_hex;
64 proto = proto->next;
66 return 0;
69 int dissector_set_print_c_style(void *ptr)
71 struct protocol *proto = (struct protocol *) ptr;
72 while (proto != NULL) {
73 proto->process = proto->print_all_cstyle;
74 proto = proto->next;
76 return 0;
79 int dissector_set_print_all_hex(void *ptr)
81 struct protocol *proto = (struct protocol *) ptr;
82 while (proto != NULL) {
83 proto->process = proto->print_all_hex;
84 proto = proto->next;
86 return 0;
89 int dissector_set_print_no_payload(void *ptr)
91 struct protocol *proto = (struct protocol *) ptr;
92 while (proto != NULL) {
93 proto->process = proto->print_pay_none;
94 proto = proto->next;
96 return 0;
100 * The main loop of the dissector. This is designed generic, so it doesn't
101 * know the underlying linktype.
103 static void dissector_main(uint8_t *packet, size_t len,
104 struct protocol *start, struct protocol *end)
106 size_t off = 0;
107 unsigned int key;
108 struct hash_table *table;
109 struct protocol *proto = start;
111 while (proto != NULL) {
112 len -= off;
113 packet += off;
114 if (unlikely(!proto->process))
115 break;
116 off = proto->offset;
117 if (!off)
118 off = len;
119 proto->process(packet, off);
120 if (unlikely(!proto->proto_next))
121 break;
122 proto->proto_next(packet, len, &table, &key, &off);
123 if (unlikely(!table))
124 break;
125 proto = lookup_hash(key, table);
126 while (proto && key != proto->key)
127 proto = proto->next;
129 len -= off;
130 packet += off;
131 if (end != NULL)
132 if (likely(end->process))
133 end->process(packet, len);
134 tprintf_flush();
138 * This is the entry point for the packet dissector machine. It is
139 * developed for being as generic as possible, so that other linktypes
140 * can be implemented, too. Only the direct entry point functions
141 * that are linktype specific are called.
143 void dissector_entry_point(uint8_t *packet, size_t len, int linktype)
145 struct protocol *proto_start = NULL;
146 struct protocol *proto_end = NULL;
148 switch (linktype) {
149 case LINKTYPE_EN10MB:
150 proto_start = dissector_get_ethernet_entry_point();
151 proto_end = dissector_get_ethernet_exit_point();
152 break;
153 default:
154 return;
157 dissector_main(packet, len, proto_start, proto_end);
161 * Initialization routines for all linktypes.
163 void dissector_init_all(int fnttype)
165 dissector_init_ethernet(fnttype);
169 * Garbage collection routines for all linktypes.
171 void dissector_cleanup_all(void)
173 dissector_cleanup_ethernet();