aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/xml/packet-xml.c
blob: 1868d15b1875dc26f0ba85e8f1fa29da2578a583 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* packet-xml.c
* an XML dissector for ethereal 
*
* Copyright 2004, Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
*
* $Id$
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
* Copyright 1998 Gerald Combs
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <string.h>
#include <ctype.h>
#include <stdlib.h>

#include <glib.h>

#include <epan/packet.h>
#include <epan/strutil.h>
#include <epan/prefs.h>

#include "packet-xml.h"

#ifdef DEBUG_XML
static const value_string xml_token_types[] =
{
	{XML_WHITESPACE,	"white space"},
	{XML_PROPERTY,		"property"},
	{XML_COMMENT_START, "comment start"},
	{XML_COMMENT_END,   "comment end"},
	{XML_METATAG_START, "metatag start"},
	{XML_METATAG_END,   "metatag end"},
	{XML_TAG_START,		"tag start"},
	{XML_TAG_END,		"tag end"},
	{XML_CLOSE_TAG_END,	"close tag end"},
	{XML_NAME,			"name"},
	{XML_TEXT,			"text"},
	{XML_GARBLED,		"garbled"},
	{0, NULL}
};

static const value_string xml_ctx_types[] =
{
	{XML_CTX_OUT, "no_ctx"},
	{XML_CTX_COMMENT, "comment"},
	{XML_CTX_TAG, "tag"},
	{XML_CTX_METATAG, "meta-tag"},
	{XML_CTX_CLOSETAG, "close-tag"},
	{0, NULL}
};

static int hf_xml_token = -1;
static int hf_xml_token_type = -1;
static int hf_xml_ctx_type = -1;
static int ett_xml_tok = -1;

#endif /* DEBUG_XML */

static int proto_xml = -1;
static int ett_xml = -1;
static int hf_xml_metatag = -1;
static int hf_xml_tag = -1;
static int hf_xml_text = -1;

gboolean is_soap;

static proto_item* proto_tree_add_xml_item(proto_tree* tree, tvbuff_t* tvb, int offset, int len, xml_token_t* xi) {
	proto_item* pi;
	gchar* txt;
	int hfid = 0;
	
	switch (xi->type) {
		case XML_TAG_END: if (xi->ctx == XML_CTX_TAG) hfid = hf_xml_tag; break;
		case XML_METATAG_END: hfid = hf_xml_metatag; break;
		case XML_TEXT: hfid = hf_xml_text; break;
		default: break;
	}

	txt = tvb_get_string(tvb,offset,len);

	if ( hfid ) {
		pi = proto_tree_add_string_format(tree,hfid,tvb,offset,len,txt,"%s",format_text(txt, len));
	} else {
		pi = proto_tree_add_text(tree,tvb,offset,len,"%s",format_text(txt, len));
	}
	
	g_free(txt);
	
	return pi;
}


static void dissect_xml(tvbuff_t* tvb, packet_info* pinfo _U_, proto_tree* tree) {
	xml_token_t* xml_items ;
	xml_token_t* xi;
	xml_token_t* next_xi;
	proto_item* pi = NULL;
	int curr_offset = 0;
	int curr_len = 0;
	GPtrArray* stack;
#ifdef DEBUG_XML
	proto_tree* tree2 = NULL;
	proto_tree* pt = NULL;
#endif
	
	is_soap = FALSE;
	
#define push() { g_ptr_array_add(stack,tree); tree = proto_item_add_subtree(pi, ett_xml); }
#define pop()  { tree = g_ptr_array_remove_index(stack,stack->len - 1); }
	
	if (tree) {
		pi = proto_tree_add_item(tree, proto_xml, tvb, 0, tvb->length, FALSE);
		
#ifdef DEBUG_XML
		tree = proto_item_add_subtree(pi, ett_xml);
		
		pi = proto_tree_add_item(tree, proto_xml, tvb, 0, tvb->length, FALSE);
		tree2 = proto_item_add_subtree(pi, ett_xml);
#else
		tree = proto_item_add_subtree(pi, ett_xml);
#endif /* DEBUG_XML */
		
		xml_items = scan_tvb_for_xml_items(tvb, 0, tvb->length);
		
		stack = g_ptr_array_new();
		
		for (xi = xml_items; xi; xi = xi->next) {
			
#ifdef DEBUG_XML
			pi = proto_tree_add_item(tree2,hf_xml_token,tvb,xi->offset,xi->len,FALSE);
			pt = proto_item_add_subtree(pi, ett_xml);
			proto_tree_add_uint(pt,hf_xml_token_type,tvb,0,0,xi->type);
			proto_tree_add_uint(pt,hf_xml_ctx_type,tvb,0,0,xi->ctx);
			proto_tree_add_text(pt,tvb,0,0,"[%i,%i] (%i,%i): '%s'",curr_offset,curr_len,xi->offset,xi->len,xi->text);
#endif /* DEBUG_XML */
			
			switch (xi->type) {
				case XML_COMMENT_START:
				case XML_METATAG_START:
				case XML_CLOSE_TAG_START:
				case XML_TAG_START:
					curr_offset = xi->offset;
				case XML_PROPERTY:
				case XML_NAME:
					curr_len += xi->len;
					break;
				case XML_WHITESPACE:
					if (xi->ctx == XML_CTX_OUT && curr_len == 0) {
						curr_offset += xi->len;
					} else {
						curr_len += xi->len;						
					}
					break;
				case XML_COMMENT_END:
				case XML_METATAG_END:
				case XML_CLOSE_TAG_END:
				case XML_TEXT:
					curr_len += xi->len;
					proto_tree_add_xml_item(tree,tvb,curr_offset,curr_len,xi);
					curr_offset = curr_offset + curr_len;
					curr_len = 0;
					break;
				case XML_TAG_END:
					curr_len += xi->len;
					if (xi->ctx == XML_CTX_CLOSETAG) pop();
					pi = proto_tree_add_xml_item(tree,tvb,curr_offset,curr_len,xi);
					if (xi->ctx == XML_CTX_TAG) push();
					curr_offset = curr_offset + curr_len;
					curr_len = 0;
					break;
				case XML_GARBLED:
					break;
			}
			
		}
		
		for (xi = xml_items; xi; xi = next_xi) {
			next_xi = xi->next;
			
			if (xi->text) g_free(xi->text);
			g_free(xi);
		}
		
		g_ptr_array_free(stack,FALSE);
	}
	
}

void
proto_register_xml(void)
{
	static hf_register_info hf[] = {
#ifdef DEBUG_XML
    { &hf_xml_token,
	{ "XML Token",
		"xml.token", FT_STRING, BASE_NONE,NULL,0x0,
		"An XML token", HFILL }},
    { &hf_xml_token_type,
	{ "XML Token Type",
		"xml.token.type", FT_UINT32, BASE_DEC,xml_token_types,0x0,
		"the type of an XML token", HFILL }},
	{ &hf_xml_ctx_type,
	{ "XML Context Type",
			"xml.ctx.type", FT_UINT32, BASE_DEC,xml_ctx_types,0x0,
			"the context of an XML token", HFILL }},
#endif /* DEBUG_XML */
	{ &hf_xml_metatag,
	{ "XML Meta Tag",
		"xml.meta_tag", FT_STRING, BASE_NONE, NULL, 0x0,
		"XML Meta Tag", HFILL }},
	{ &hf_xml_tag,
	{ "XML Tag",
		"xml.tag", FT_STRING, BASE_NONE, NULL, 0x0,
		"XML Tag", HFILL }},
	{ &hf_xml_text,
	{ "XML Text",
		"xml.text", FT_STRING, BASE_NONE, NULL, 0x0,
		"Text in XML", HFILL }}
	};
	
	static gint *ett[] = {
#ifdef DEBUG_XML
		&ett_xml_tok,
#endif /* DEBUG_XML */
		&ett_xml
	};
	
	proto_xml = proto_register_protocol("eXtensible Markup Language", "XML", "xml");
	proto_register_field_array(proto_xml, hf, array_length(hf));
	proto_register_subtree_array(ett, array_length(ett));
	register_dissector("xml", dissect_xml, proto_xml);

}

void
proto_reg_handoff_xml(void)
{
	dissector_handle_t xml_handle;
	
	xml_handle = find_dissector("xml");

	dissector_add_string("media_type", "text/xml", xml_handle);
	dissector_add_string("media_type", "application/smil", xml_handle);
	dissector_add_string("media_type", "text/xml", xml_handle);
	dissector_add_string("media_type", "application/xml", xml_handle);
	dissector_add_string("media_type", "application/soap+xml", xml_handle);
	
}