name change
[gd/wireshark/.git] / plugins / h223 / packet-srp.c
1 /* packet-srp.c
2  * Routines for H.324/SRP dissection
3  * 2004 Richard van der Hoff <richardv@mxtelecom.com>
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 <gmodule.h>
31 #include <glib.h>
32 #include <epan/bitswap.h>
33 #include <epan/circuit.h>
34 #include <epan/packet.h>
35 #include <epan/stream.h>
36 #include <epan/reassemble.h>
37 #include <epan/crc16.h>
38
39 #include "packet-srp.h"
40
41 /* Ethereal ID of the protocols */
42 static int proto_srp = -1;
43 static int proto_ccsrl = -1;
44
45 /* The following hf_* variables are used to hold the ethereal IDs of
46  * our header fields; they are filled out when we call
47  * proto_register_field_array() in proto_register_srp()
48  */
49 static int hf_srp_header = -1;
50 static int hf_srp_seqno = -1;
51 static int hf_srp_crc = -1;
52 static int hf_srp_crc_bad = -1;
53 static int hf_ccsrl_ls = -1;
54
55 /* These are the ids of the subtrees that we may be creating */
56 static gint ett_srp = -1;
57 static gint ett_ccsrl = -1;
58
59 static dissector_handle_t data_handle=NULL;
60 static dissector_handle_t ccsrl_handle=NULL;
61 static dissector_handle_t h245dg_handle=NULL;
62
63 /*****************************************************************************/
64 #define SRP_SRP_COMMAND 249
65 #define SRP_SRP_RESPONSE 251
66 #define SRP_NSRP_RESPONSE 247
67
68 static const value_string srp_frame_types[] = {
69   {SRP_SRP_COMMAND, "SRP command"},
70   {SRP_SRP_RESPONSE, "SRP response"},
71   {SRP_NSRP_RESPONSE, "NSRP response"},
72   {0,NULL}
73 };
74
75 static const value_string ccsrl_ls_vals[] = {
76   {0xFF, "Yes"},
77   {0x00, "No"},
78   {0,NULL}
79 };
80
81 /*****************************************************************************/
82
83 static void dissect_ccsrl(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
84 {
85     proto_item *ccsrl_item;
86     proto_tree *ccsrl_tree=NULL;
87     guint8 lastseg = tvb_get_guint8(tvb,0);
88     tvbuff_t *next_tvb;
89
90     /* add the 'ccsrl' tree to the main tree */
91     if (tree) {
92         ccsrl_item = proto_tree_add_item (tree, proto_ccsrl, tvb, 0, -1, FALSE);
93         ccsrl_tree = proto_item_add_subtree (ccsrl_item, ett_ccsrl);
94         proto_tree_add_uint(ccsrl_tree,hf_ccsrl_ls,tvb,0,1,lastseg);
95     }
96
97     /* XXX add support for reassembly of fragments */
98     
99     /* XXX currently, we always dissect as H245. It's not necessarily
100         that though.
101     */
102     next_tvb = tvb_new_subset(tvb, 1, -1, -1 );
103     call_dissector( h245dg_handle, next_tvb, pinfo, ccsrl_tree );
104 }
105
106 static void dissect_srp_command(tvbuff_t * tvb, packet_info * pinfo, proto_tree * srp_tree)
107 {
108     tvbuff_t *next_tvb;
109     guint payload_len;
110
111     if( srp_tree )
112         proto_tree_add_item(srp_tree,hf_srp_seqno,tvb,1,1,FALSE);
113
114     payload_len = tvb_reported_length_remaining(tvb,4);
115     next_tvb = tvb_new_subset(tvb, 2, payload_len, payload_len );
116
117     /* XXX currently, we always dissect as CCSRL. It's only that in
118      * H324/Annex C though.
119      */
120     call_dissector(ccsrl_handle, next_tvb, pinfo, srp_tree );
121 }
122
123 static void dissect_srp (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
124 {
125     proto_item *srp_item = NULL;
126     proto_tree *srp_tree = NULL;
127
128     guint8 header = tvb_get_guint8(tvb,0);
129     
130     /* add the 'srp' tree to the main tree */
131     if (tree) {
132         srp_item = proto_tree_add_item (tree, proto_srp, tvb, 0, -1, FALSE);
133         srp_tree = proto_item_add_subtree (srp_item, ett_srp);
134         proto_tree_add_uint(srp_tree,hf_srp_header,tvb,0,1,header);
135     }
136
137     switch( header ) {
138         case SRP_SRP_COMMAND:
139             dissect_srp_command(tvb,pinfo,srp_tree);
140             break;
141
142         case SRP_SRP_RESPONSE:
143             break;
144
145         case SRP_NSRP_RESPONSE:
146             if( srp_tree )
147                 proto_tree_add_item(srp_tree,hf_srp_seqno,tvb,1,1,FALSE);
148             break;
149
150         default:
151             break;
152     }
153
154     if( srp_tree ) {
155         guint16 crc, calc_crc;
156         guint crc_offset = tvb_reported_length(tvb)-2;
157         crc = tvb_get_letohs(tvb,-2);
158
159         /* crc includes the header */
160         calc_crc = crc16_ccitt_tvb(tvb,crc_offset);
161         
162         if( crc == calc_crc ) {
163             proto_tree_add_uint_format(srp_tree, hf_srp_crc, tvb,
164                                        crc_offset, 2, crc,
165                                        "CRC: 0x%04x (correct)", crc);
166         } else {
167             proto_tree_add_boolean_hidden(srp_tree, hf_srp_crc_bad, tvb,
168                                           crc_offset, 2, TRUE);
169             proto_tree_add_uint_format(srp_tree, hf_srp_crc, tvb,
170                                        crc_offset, 2, crc,
171                                        "CRC: 0x%04x (incorrect, should be 0x%04x)",
172                                        crc,
173                                        calc_crc);
174         }
175     }
176
177 }
178
179 void proto_register_ccsrl (void)
180 {
181     static hf_register_info hf[] = {
182         { &hf_ccsrl_ls,
183           { "Last Segment","ccsrl.ls",FT_UINT8, BASE_HEX, ccsrl_ls_vals, 0x0,
184             "Last segment indicator", HFILL}},
185     };
186
187     static gint *ett[] = {
188         &ett_ccsrl,
189     };
190
191     if (proto_ccsrl == -1) { /* execute protocol initialization only once */
192         proto_ccsrl =
193             proto_register_protocol ("H.324/CCSRL", "CCSRL", "ccsrl");
194
195         proto_register_field_array (proto_ccsrl, hf, array_length (hf));
196         proto_register_subtree_array (ett, array_length (ett));
197         register_dissector("ccsrl", dissect_ccsrl, proto_ccsrl);
198     }
199 }
200
201 void proto_register_srp (void)
202 {
203     static hf_register_info hf[] = {
204         {&hf_srp_header,
205          { "Header", "srp.header", FT_UINT8, BASE_DEC, srp_frame_types, 0x0,
206            "SRP header octet", HFILL }},
207         {&hf_srp_seqno,
208          { "Sequence Number", "srp.seqno", FT_UINT8, BASE_DEC, NULL, 0x0,
209            "Sequence Number", HFILL }},
210         {&hf_srp_crc,
211          { "CRC", "srp.crc", FT_UINT16, BASE_HEX, NULL, 0x0,
212            "CRC", HFILL }},
213         { &hf_srp_crc_bad,
214           { "Bad CRC","srp.crc_bad", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
215             "", HFILL }},
216     };
217
218     static gint *ett[] = {
219         &ett_srp,
220     };
221
222     if (proto_srp == -1) { /* execute protocol initialization only once */
223         proto_srp =
224             proto_register_protocol ("H.324/SRP", "SRP", "srp");
225
226         proto_register_field_array (proto_srp, hf, array_length (hf));
227         proto_register_subtree_array (ett, array_length (ett));
228         register_dissector("srp", dissect_srp, proto_srp);
229
230         /* register our init routine to be called at the start of a capture,
231            to clear out our hash tables etc */
232         /* register_init_routine(&srp_init_protocol); */
233
234     }
235 }
236
237
238 void proto_reg_handoff_srp(void) {
239     data_handle = find_dissector("data");
240     ccsrl_handle = find_dissector("ccsrl");
241     h245dg_handle = find_dissector("h245dg");
242 }