4ca278333e404d26ef5d1f9013b141679fa95cf9
[obnox/wireshark/wip.git] / epan / dissectors / packet-data.c
1 /* packet-data.c
2  * Routines for raw data (default case)
3  * Gilbert Ramirez <gram@alumni.rice.edu>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <glib.h>
31 #include <epan/packet.h>
32 #include <epan/prefs.h>
33 #include <epan/crypt/crypt-md5.h>
34 #include "packet-data.h"
35
36 /* proto_data cannot be static because it's referenced in the
37  * print routines
38  */
39 int proto_data = -1;
40
41 static int hf_data_data = -1;
42 static int hf_data_text = -1;
43 static int hf_data_len = -1;
44 static int hf_data_md5_hash = -1;
45
46 static gboolean new_pane = FALSE;
47 static gboolean show_as_text = FALSE;
48 static gboolean generate_md5_hash = FALSE;
49
50 static gint ett_data = -1;
51
52 void proto_reg_handoff_data(void);
53
54 static void
55 dissect_data(tvbuff_t *tvb, packet_info *pinfo _U_ , proto_tree *tree)
56 {
57         gint bytes;
58
59         if (tree) {
60                 bytes = tvb_length_remaining(tvb, 0);
61                 if (bytes > 0) {
62                         tvbuff_t *data_tvb;
63                         proto_item *ti;
64                         proto_tree *data_tree;
65                         if (new_pane) {
66                                 guint8 *real_data = tvb_memdup(tvb, 0, bytes);
67                                 data_tvb = tvb_new_child_real_data(tvb,real_data,bytes,bytes);
68                                 tvb_set_free_cb(data_tvb, g_free);
69                                 add_new_data_source(pinfo, data_tvb, "Not dissected data bytes");
70                         } else {
71                                 data_tvb = tvb;
72                         }
73                         ti = proto_tree_add_protocol_format(tree, proto_data, tvb,
74                                 0,
75                                 bytes, "Data (%d byte%s)", bytes,
76                                 plurality(bytes, "", "s"));
77                         data_tree = proto_item_add_subtree(ti, ett_data);
78
79                         proto_tree_add_item(data_tree, hf_data_data, data_tvb, 0, bytes, ENC_NA);
80
81                         if (show_as_text) {
82                                 proto_tree_add_item(data_tree, hf_data_text, data_tvb, 0, bytes, ENC_ASCII|ENC_NA);
83                         }
84
85                         if(generate_md5_hash) {
86                                 const guint8 *cp;
87                                 md5_state_t md_ctx;
88                                 md5_byte_t digest[16];
89                                 gchar *digest_string;
90
91                                 cp = tvb_get_ptr(tvb, 0, bytes);
92
93                                 md5_init(&md_ctx);
94                                 md5_append(&md_ctx, cp, bytes);
95                                 md5_finish(&md_ctx, digest);
96
97                                 digest_string = bytestring_to_str(digest, 16, '\0');
98                                 ti = proto_tree_add_string(data_tree, hf_data_md5_hash, tvb, 0, 0, digest_string);
99                                 PROTO_ITEM_SET_GENERATED(ti);
100                         }
101
102                         ti = proto_tree_add_int(data_tree, hf_data_len, data_tvb, 0, 0, bytes);
103                         PROTO_ITEM_SET_GENERATED (ti);
104                 }
105         }
106 }
107
108 void
109 proto_register_data(void)
110 {
111         static hf_register_info hf[] = {
112                 { &hf_data_data,
113                   { "Data", "data.data", FT_BYTES, BASE_NONE, NULL, 0x0, NULL, HFILL } },
114                 { &hf_data_text,
115                   { "Text", "data.text", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL } },
116                 { &hf_data_md5_hash,
117                   { "Payload MD5 hash", "data.md5_hash", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL } },
118                 { &hf_data_len,
119                   { "Length", "data.len", FT_INT32, BASE_DEC, NULL, 0x0, NULL, HFILL } }
120         };
121
122         static gint *ett[] = {
123                 &ett_data
124         };
125         
126         module_t *module_data;
127         
128         proto_data = proto_register_protocol (
129                 "Data",         /* name */
130                 "Data",         /* short name */
131                 "data"          /* abbrev */
132                 );
133
134         register_dissector("data", dissect_data, proto_data);
135
136         proto_register_field_array(proto_data, hf, array_length(hf));
137         proto_register_subtree_array(ett, array_length(ett));
138         
139         module_data = prefs_register_protocol( proto_data, proto_reg_handoff_data);
140         prefs_register_bool_preference(module_data,
141                 "datapref.newpane",
142                 "Show not dissected data on new Packet Bytes pane",
143                 "Show not dissected data on new Packet Bytes pane",
144                 &new_pane);
145         prefs_register_bool_preference(module_data,
146                 "show_as_text",
147                 "Show data as text",
148                 "Show data as text in the Packet Details pane",
149                 &show_as_text);
150         prefs_register_bool_preference(module_data,
151                                        "md5_hash",
152                                        "Generate MD5 hash",
153                                        "Whether or not MD5 hashes should be generated and shown for each payload.",
154                                        &generate_md5_hash);
155
156         /*
157          * "Data" is used to dissect something whose normal dissector
158          * is disabled, so it cannot itself be disabled.
159          */
160         proto_set_cant_toggle(proto_data);
161 }
162
163 void
164 proto_reg_handoff_data(void)
165 {
166 }