aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/megaco/packet-megaco.c
blob: d1d8bcb2ce3c7f6860155efbc8f407d64502c67c (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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/* packet-megaco.c
 * Routines for megaco packet disassembly
 * RFC 3015
 *
 * $Id: packet-megaco.c,v 1.3 2003/01/15 00:17:53 guy Exp $
 *
 * Christian Falckenberg, 2002/10/17
 * Copyright (c) 2002 by Christian Falckenberg
 *                       <christian.falckenberg@nortelnetworks.com>
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@ethereal.com>
 * Copyright 1999 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 "plugins/plugin_api.h"

#include "moduleinfo.h"

#include <stdio.h>
#include <stdlib.h>
#include <gmodule.h>
#include <ctype.h>
#include <time.h>
#include <string.h>
#include <epan/packet.h>
#include <epan/resolv.h>
#include "prefs.h"
#include <epan/strutil.h>
#include "sctpppids.h"

#include "plugins/plugin_api_defs.h"

#ifndef __ETHEREAL_STATIC__
G_MODULE_EXPORT const gchar version[] = VERSION;
#endif

#define PORT_MEGACO_TXT 2944
#define PORT_MEGACO_BIN 2945

void proto_reg_handoff_megaco(void);

/* Define the megaco proto */
static int proto_megaco = -1;

/* Define headers for megaco */
static int hf_megaco_version     = -1;
static int hf_megaco_mid         = -1;
static int hf_megaco_transaction = -1;
static int hf_megaco_transid     = -1;

/* Define the tree for megaco */
static int ett_megaco = -1;

/*
 * Here are the global variables associated with
 * the various user definable characteristics of the dissection
 *
 * MEGACO has two kinds of message formats: text and binary
 *
 * global_megaco_raw_text determines whether we are going to display
 * the raw text of the megaco message, much like the HTTP dissector does.
 *
 * global_megaco_dissect_tree determines whether we are going to display
 * a detailed tree that expresses a somewhat more semantically meaningful
 * decode.
 */
static int global_megaco_txt_tcp_port = PORT_MEGACO_TXT;
static int global_megaco_txt_udp_port = PORT_MEGACO_TXT;
#if 0
static int global_megaco_bin_tcp_port = PORT_MEGACO_BIN;
static int global_megaco_bin_udp_port = PORT_MEGACO_BIN;
#endif
static gboolean global_megaco_raw_text = TRUE;
static gboolean global_megaco_dissect_tree = TRUE;

/*
 * Variables to allow for proper deletion of dissector registration when
 * the user changes port from the gui.
 */
static int txt_tcp_port = 0;
static int txt_udp_port = 0;
#if 0
static int bin_tcp_port = 0;
static int bin_udp_port = 0;
#endif

/* Some basic utility functions that are specific to this dissector */
static gint tvb_skip_wsp(tvbuff_t* tvb, gint offset, gint maxlength);

/*
 * The various functions that either dissect some
 * subpart of MEGACO.  These aren't really proto dissectors but they
 * are written in the same style.
 */
static void megaco_raw_text_add(tvbuff_t *tvb,
				proto_tree *tree);

/*
 * dissect_megaco_text - The dissector for the MEGACO Protocol, using
 * text encoding.
 */

static void
dissect_megaco_text(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
    gint        tvb_len, len;
    gint        tvb_offset,tvb_current_offset,tvb_previous_offset, tokenlen;
    proto_tree  *megaco_tree, *ti;
    proto_item* (*my_proto_tree_add_string)(proto_tree*, int, tvbuff_t*, gint,
					  gint, const char*);
    guint8      word[7];
    gint        tvb_linebegin,tvb_lineend,linelen;

    /* Initialize variables */
    tvb_len             = tvb_length(tvb);
    megaco_tree         = NULL;
    ti                  = NULL;
    tvb_previous_offset = 0;
    tvb_current_offset  = 0;

    /*
     * Check to see whether we're really dealing with MEGACO by looking
     * for the MEGACO string.  This needs to be improved when supporting
     * binary encodings.
     */
    if(!tvb_get_nstringz0(tvb,0,6,word)) return;
    if (strncasecmp(word, "MEGACO", 6) != 0) return;

    /* Display MEGACO in protocol column */
    if (check_col(pinfo->cinfo, COL_PROTOCOL))
	col_add_str(pinfo->cinfo, COL_PROTOCOL, "MEGACO");
    
    /* Display transaction in info column */
    tvb_offset = tvb_find_guint8(tvb, 0, tvb_len, ' ');
    tvb_offset = tvb_find_guint8(tvb, tvb_offset, tvb_len, ' ');
    len = tvb_find_guint8(tvb, tvb_offset, tvb_len, '=') - tvb_offset;
    
    if (check_col(pinfo->cinfo, COL_INFO) )
	col_add_fstr(pinfo->cinfo, COL_INFO, "%s",
		     tvb_format_text(tvb,tvb_offset,len));
	
    /* Build the info tree if we've been given a root */
    if (tree)
    {
	/* Create megaco subtree */
	ti = proto_tree_add_item(tree,proto_megaco,tvb,0,0, FALSE);
	megaco_tree = proto_item_add_subtree(ti, ett_megaco);

	if(global_megaco_dissect_tree)
	    my_proto_tree_add_string = proto_tree_add_string;
	else 
	    my_proto_tree_add_string = proto_tree_add_string_hidden;

	/* Find version */

	tvb_previous_offset = tvb_find_guint8(tvb, tvb_previous_offset,
					 tvb_len, '/') + 1;
	tvb_current_offset  = tvb_find_guint8(tvb, tvb_previous_offset,
					  tvb_len, ' ');

	tokenlen = tvb_current_offset - tvb_previous_offset;
	
	if (tree)
	    my_proto_tree_add_string(megaco_tree, hf_megaco_version, tvb,
				     tvb_previous_offset, tokenlen,
				     tvb_format_text(tvb, tvb_previous_offset,
						     tokenlen));
    

	/* Find mId (message originator identifier) */
	tvb_previous_offset = tvb_skip_wsp(tvb, tvb_current_offset,
					   tvb_len);
	tvb_current_offset  = tvb_find_guint8(tvb, tvb_previous_offset,
					      tvb_len, ' ');
	
	tokenlen = tvb_current_offset - tvb_previous_offset;

	my_proto_tree_add_string(megaco_tree, hf_megaco_mid, tvb,
				 tvb_previous_offset, tokenlen,
				 tvb_format_text(tvb, tvb_previous_offset,
						 tokenlen));
	/* Find transaction */
	tvb_previous_offset = tvb_skip_wsp(tvb, tvb_current_offset,
					   tvb_len);
	tvb_current_offset  = tvb_find_guint8(tvb, tvb_previous_offset,
					      tvb_len, '=');
    
	tokenlen = tvb_current_offset - tvb_previous_offset;

	my_proto_tree_add_string(megaco_tree, hf_megaco_transaction, tvb,
				 tvb_previous_offset, tokenlen,
				 tvb_format_text(tvb, tvb_previous_offset,
						 tokenlen));
	if (check_col(pinfo->cinfo, COL_INFO) )
	    col_add_fstr(pinfo->cinfo, COL_INFO, "%s",
			 tvb_format_text(tvb,tvb_previous_offset,tokenlen));
	
	/* Find transaction id */
	tvb_previous_offset = tvb_skip_wsp(tvb, tvb_current_offset,
					   tvb_len) + 1;
	tvb_current_offset  = tvb_find_guint8(tvb, tvb_previous_offset,
					      tvb_len, '{');
	
	tokenlen = tvb_current_offset - tvb_previous_offset;

	my_proto_tree_add_string(megaco_tree, hf_megaco_transid, tvb,
				 tvb_previous_offset, tokenlen,
				 tvb_format_text(tvb, tvb_previous_offset,
						 tokenlen));
	
	/* and finally the whole message as raw text */

	if(global_megaco_raw_text){
	    tvb_linebegin = 0;

	    do {
		tvb_find_line_end(tvb,tvb_linebegin,-1,&tvb_lineend,FALSE);
		linelen = tvb_lineend - tvb_linebegin;
		proto_tree_add_text(tree, tvb, tvb_linebegin, linelen,
				    "%s", tvb_format_text(tvb,tvb_linebegin,
							  linelen));
		tvb_linebegin = tvb_lineend;
	    } while ( tvb_lineend < tvb_len );
	}
    }
}

/* Register all the bits needed with the filtering engine */

void
proto_register_megaco(void)
{
    static hf_register_info hf[] = {
	{ &hf_megaco_version,
	  { "Version", "megaco.version", FT_STRING, BASE_DEC, NULL, 0x0,
	    "MEGACO Version", HFILL }},
	{ &hf_megaco_mid,
	  { "mID", "megaco.mid", FT_STRING, BASE_DEC, NULL, 0x0,
	    "Message Originator", HFILL }},
	{ &hf_megaco_transaction,
	  { "Transaction", "megaco.transaction", FT_STRING, BASE_DEC, NULL, 0x0,
	    "Transaction type of this message", HFILL }},
	{ &hf_megaco_transid,
	  { "Transaction ID", "megaco.transid", FT_STRING, BASE_DEC, NULL, 0x0,
	    "Transaction ID of this message", HFILL }},
	/* Add more fields here */
    };
    static gint *ett[] = {
	&ett_megaco,
    };
    module_t *megaco_module;

    proto_megaco = proto_register_protocol("MEGACO",
					   "MEGACO", "megaco");

    proto_register_field_array(proto_megaco, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));

    /* Register our configuration options, particularly our ports */

    megaco_module = prefs_register_protocol(proto_megaco, proto_reg_handoff_megaco);

    prefs_register_uint_preference(megaco_module, "tcp.txt_port",
				   "MEGACO Text TCP Port",
				   "Set the TCP port for MEGACO text messages",
				   10, &global_megaco_txt_tcp_port);

    prefs_register_uint_preference(megaco_module, "udp.txt_port",
				   "MEGACO Text UDP Port",
				   "Set the UDP port for MEGACO text messages",
				   10, &global_megaco_txt_udp_port);

#if 0
    prefs_register_uint_preference(megaco_module, "tcp.bin_port",
				   "MEGACO Binary TCP Port",
				   "Set the TCP port for MEGACO binary messages",
				   10, &global_megaco_bin_tcp_port);

    prefs_register_uint_preference(megaco_module, "udp.bin_port",
				   "MEGACO Binary UDP Port",
				   "Set the UDP port for MEGACO binary messages",
				   10, &global_megaco_bin_udp_port);
#endif

    prefs_register_bool_preference(megaco_module, "display_raw_text",
				   "Display raw text for MEGACO message",
				   "Specifies that the raw text of the "
				   "MEGACO message should be displayed "
				   "instead of (or in addition to) the "
				   "dissection tree",
				   &global_megaco_raw_text);

    prefs_register_bool_preference(megaco_module, "display_dissect_tree",
				   "Display tree dissection for MEGACO message",
				   "Specifies that the dissection tree of the "
				   "MEGACO message should be displayed "
				   "instead of (or in addition to) the "
				   "raw text",
				   &global_megaco_dissect_tree);
}

/* The registration hand-off routine */
void
proto_reg_handoff_megaco(void)
{
    static int megaco_prefs_initialized = FALSE;
    static dissector_handle_t megaco_text_handle;

    /*
     * Get a handle for the SDP dissector.
     */
    if (!megaco_prefs_initialized) {
	megaco_text_handle = create_dissector_handle(dissect_megaco_text,
						     proto_megaco);
	megaco_prefs_initialized = TRUE;
    }
    else {
	dissector_delete("tcp.port", txt_tcp_port, megaco_text_handle);
	dissector_delete("udp.port", txt_udp_port, megaco_text_handle);
#if 0
	dissector_delete("tcp.port", bin_tcp_port, megaco_bin_handle);
	dissector_delete("udp.port", bin_udp_port, megaco_bin_handle);
#endif
    }

    /* Set our port number for future use */

    txt_tcp_port = global_megaco_txt_tcp_port;
    txt_udp_port = global_megaco_txt_udp_port;

#if 0
    bin_tcp_port = global_megaco_bin_tcp_port;
    bin_udp_port = global_megaco_bin_udp_port;
#endif

    dissector_add("tcp.port", global_megaco_txt_tcp_port, megaco_text_handle);
    dissector_add("udp.port", global_megaco_txt_udp_port, megaco_text_handle);
#if 0
    dissector_add("tcp.port", global_megaco_bin_tcp_port, megaco_bin_handle);
    dissector_add("udp.port", global_megaco_bin_udp_port, megaco_bin_handle);
#endif
    /* XXX - text or binary?  Does that depend on the port number? */
    dissector_add("sctp.ppi", H248_PAYLOAD_PROTOCOL_ID,   megaco_text_handle);
}

/*
 * tvb_skip_wsp - Returns the position in tvb of the first non-whitespace
 *                character following offset or offset + maxlength -1 whichever
 *                is smaller.
 *
 * Parameters:
 * tvb - The tvbuff in which we are skipping whitespace.
 * offset - The offset in tvb from which we begin trying to skip whitespace.
 * maxlength - The maximum distance from offset that we may try to skip
 * whitespace.
 *
 * Returns: The position in tvb of the first non-whitespace
 *          character following offset or offset + maxlength -1 whichever
 *          is smaller.
 */
static gint tvb_skip_wsp(tvbuff_t* tvb, gint offset, gint maxlength){
  gint counter = offset;
  gint end,tvb_len;
  guint8 tempchar;
  tvb_len = tvb_length(tvb);
  end = offset + maxlength;
  if(end >= tvb_len){
    end = tvb_len;
  }
  for(counter = offset; counter < end &&
	((tempchar = tvb_get_guint8(tvb,counter)) == ' ' ||
	tempchar == '\t');counter++);
  return (counter);
}


/* Start the functions we need for the plugin stuff */

#ifndef __ETHEREAL_STATIC__

G_MODULE_EXPORT void
plugin_reg_handoff(void){
    proto_reg_handoff_megaco();
}

G_MODULE_EXPORT void
plugin_init(plugin_address_table_t *pat
#ifndef PLUGINS_NEED_ADDRESS_TABLE
	    _U_
#endif
	    ){
/* initialise the table of pointers needed in Win32 DLLs */
    plugin_address_table_init(pat);
/* register the new protocol, protocol fields, and subtrees */
    if (proto_megaco == -1) { /* execute protocol initialization only once */
	proto_register_megaco();
    }
}

#endif

/* End the functions we need for plugin stuff */