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