Echo packet dissector.
[obnox/wireshark/wip.git] / packet-echo.c
1 /* packet-echo.c
2  * Routines for ECHO packet disassembly (RFC862)
3  *
4  * Only useful to mark the packets as ECHO in the summary and in the
5  * protocol hierarchy statistics (since not so many fields to decode ;-)
6  *
7  * Laurent Deniel <laurent.deniel@free.fr>
8  *
9  * $Id: packet-echo.c,v 1.1 2003/06/25 13:42:18 deniel Exp $
10  *
11  * Ethereal - Network traffic analyzer
12  * By Gerald Combs <gerald@ethereal.com>
13  * Copyright 1998 Gerald Combs
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <glib.h>
37 #include <epan/packet.h>
38
39 #define ECHO_PORT       7
40
41 static int proto_echo = -1;
42
43 static int hf_echo_data = -1;
44 static int hf_echo_request = -1;
45 static int hf_echo_response = -1;
46
47 static gint ett_echo = -1;
48
49 static void dissect_echo(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
50 {
51   
52   proto_tree *echo_tree = NULL;
53   proto_item *ti;
54   int         offset = 0;
55   gboolean    request = FALSE;
56   const char  *data = tvb_get_ptr(tvb, offset, -1);
57
58   if (pinfo->destport == ECHO_PORT) {
59     request = TRUE;
60   }
61
62   if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
63     col_add_str(pinfo->cinfo, COL_PROTOCOL, "ECHO");
64   }
65
66   if (check_col(pinfo->cinfo, COL_INFO)) {
67     col_add_fstr(pinfo->cinfo, COL_INFO, "%s", 
68                  (request) ? "Request" : "Response");
69   }
70   
71   if (tree) {
72
73     ti = proto_tree_add_item(tree, proto_echo, tvb, offset, -1, FALSE);
74     echo_tree = proto_item_add_subtree(ti, ett_echo);
75
76     if (request) {
77       proto_tree_add_boolean_hidden(echo_tree, hf_echo_request, tvb, 0, 0, 1);
78       
79     } else {
80       proto_tree_add_boolean_hidden(echo_tree, hf_echo_response, tvb, 0, 0, 1);
81     }
82
83     proto_tree_add_bytes(echo_tree, hf_echo_data, tvb, offset, -1, data);
84
85   }
86
87 } /* dissect_echo */
88
89 void proto_register_echo(void) 
90 {
91
92   static hf_register_info hf[] = {
93     { &hf_echo_data,
94       { "Echo data",    "echo.data", 
95         FT_BYTES,       BASE_HEX,       NULL,   0x0,
96         "Echo data", HFILL }},
97     { &hf_echo_request,
98       { "Echo request", "echo.request", 
99         FT_BOOLEAN,     BASE_NONE,      NULL,   0x0,
100         "Echo data", HFILL }},
101     { &hf_echo_response,
102       { "Echo response","echo.response", 
103         FT_BOOLEAN,     BASE_NONE,      NULL,   0x0,
104         "Echo data", HFILL }}
105   };
106
107   static gint *ett[] = {
108     &ett_echo
109   };
110
111   proto_echo = proto_register_protocol("Echo", "ECHO", "echo");
112   proto_register_field_array(proto_echo, hf, array_length(hf));
113   proto_register_subtree_array(ett, array_length(ett));
114
115 }
116
117 void proto_reg_handoff_echo(void) 
118 {
119
120   dissector_handle_t echo_handle = NULL;
121
122   echo_handle = create_dissector_handle(dissect_echo, proto_echo);
123
124   dissector_add("udp.port", ECHO_PORT, echo_handle);
125   dissector_add("tcp.port", ECHO_PORT, echo_handle);
126
127 }
128