Move a pile of protocol-related headers from the top-level source
[obnox/wireshark/wip.git] / plugins / rdm / packet-rdm.c
1 /* packet-rdm.c
2  * Routines for RDM packet disassembly
3  *
4  * $Id$
5  *
6  * Copyright (c) 2003 by Erwin Rol <erwin@erwinrol.com>
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1999 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 /* Include files */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include "moduleinfo.h"
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <gmodule.h>
38 #include <ctype.h>
39 #include <time.h>
40 #include <string.h>
41 #include <epan/packet.h>
42 #include <epan/addr_resolv.h>
43 #include <epan/etypes.h>
44 #include <epan/strutil.h>
45
46 /* Define version if we are not building ethereal statically */
47
48 #ifndef ENABLE_STATIC
49 G_MODULE_EXPORT const gchar version[] = VERSION;
50 #endif
51
52 /*
53  * See http://www.esta.org/tsp/E1-20inst.htm
54  *
55  */
56
57 void proto_reg_handoff_rdm(void);
58
59 /* Define the rdm proto */
60 static int proto_rdm = -1;
61
62 /* Header */
63 static int hf_rdm_sub_start_code = -1;
64 static int hf_rdm_slot_count = -1;
65 static int hf_rdm_dest_uid = -1;
66 static int hf_rdm_src_uid = -1;
67 static int hf_rdm_seq_nr = -1;
68 static int hf_rdm_res_type = -1;
69 static int hf_rdm_msg_count = -1;
70 static int hf_rdm_sub_device = -1;
71 static int hf_rdm_mdb = -1;
72 static int hf_rdm_checksum = -1;
73
74 /* Define the tree for rdm */
75 static int ett_rdm = -1;
76
77 static void
78 dissect_rdm(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
79   gint offset = 0, start_offset,i;
80   guint16 checksum,calc_checksum;
81   gint mdb_size;
82   proto_tree *ti=NULL,*rdm_tree=NULL;
83   proto_item* item;
84   
85   /* Set the protocol column */
86   if(check_col(pinfo->cinfo,COL_PROTOCOL)){
87     col_set_str(pinfo->cinfo,COL_PROTOCOL,"RDM");
88   }
89                                                                                                                                                                                                      
90   /* Clear out stuff in the info column */
91   if(check_col(pinfo->cinfo,COL_INFO)){
92     col_clear(pinfo->cinfo,COL_INFO);
93   }
94                                                                                                                                                                                                      
95   if (tree) {
96     ti = proto_tree_add_item(tree, proto_rdm, tvb, offset, -1, FALSE);
97     rdm_tree = proto_item_add_subtree(ti, ett_rdm);
98   }
99                                                                                                                                                                                                      
100   if (check_col(pinfo->cinfo, COL_INFO)) {
101     col_append_fstr(pinfo->cinfo, COL_INFO, "%s",
102                     "RDM");
103   }
104                                                                                                                                                                                                      
105   if (tree)
106   {
107     start_offset = offset;
108   
109     proto_tree_add_item(rdm_tree, hf_rdm_sub_start_code, tvb,
110                         offset, 1, FALSE);
111     offset += 1;
112
113     mdb_size = tvb_get_guint8(tvb, offset) - 19;
114
115     if (mdb_size < 20) {
116       proto_tree_add_text(rdm_tree, tvb, offset, 1, "Invalid MDB size: %d", 
117                         mdb_size + 19);
118       return;
119     }
120
121     proto_tree_add_item(rdm_tree, hf_rdm_slot_count, tvb,
122                         offset, 1, FALSE);
123     offset += 1;
124
125     proto_tree_add_item(rdm_tree, hf_rdm_dest_uid, tvb,
126                         offset, 6, FALSE);
127     offset += 6;
128
129     proto_tree_add_item(rdm_tree, hf_rdm_src_uid, tvb,
130                         offset, 6, FALSE);
131     offset += 6;
132
133     proto_tree_add_item(rdm_tree, hf_rdm_seq_nr, tvb,
134                         offset, 1, FALSE);
135     offset += 1;
136
137     proto_tree_add_item(rdm_tree, hf_rdm_res_type, tvb,
138                         offset, 1, FALSE);
139     offset += 1;
140
141     proto_tree_add_item(rdm_tree, hf_rdm_msg_count, tvb,
142                         offset, 1, FALSE);
143     offset += 1;
144
145     proto_tree_add_item(rdm_tree, hf_rdm_sub_device, tvb,
146                         offset, 1, FALSE);
147     offset += 1;
148
149     tvb_ensure_bytes_exist(tvb, offset, mdb_size);
150     proto_tree_add_item(rdm_tree, hf_rdm_mdb, tvb,
151                         offset, mdb_size, FALSE);
152     offset += mdb_size;
153
154     calc_checksum = 0x00f0;
155     for( i = start_offset; i < offset; i++)
156     {
157       calc_checksum += tvb_get_guint8( tvb, i );    
158     }
159
160     checksum = tvb_get_ntohs( tvb, offset );
161     item = proto_tree_add_item(rdm_tree, hf_rdm_checksum, tvb,
162                         offset, 2, FALSE);
163
164     if( calc_checksum != checksum )
165       proto_item_append_text( item, " ( INCORRECT should be 0x%04x )", calc_checksum );
166     else
167       proto_item_append_text( item, " ( CORRECT )" );
168   
169     offset += 2;
170   }
171 }
172
173 void
174 proto_register_rdm(void) {
175   static hf_register_info hf[] = {
176     { &hf_rdm_sub_start_code,
177       { "Sub Start Code",
178         "rdm.sub_start_code",
179         FT_UINT8, BASE_HEX, NULL, 0x0,
180         "Sub Start Code", HFILL }},
181         
182     { &hf_rdm_slot_count,
183       { "Slot Count",
184         "rdm.slot_count",
185         FT_UINT8, BASE_DEC, NULL, 0x0,
186         "Slot Count", HFILL }},
187         
188    { &hf_rdm_dest_uid,
189       { "Dest. UID",
190         "rdm.dest_uid",
191         FT_BYTES, BASE_HEX, NULL, 0x0,
192         "Dest. UID", HFILL }},        
193
194    { &hf_rdm_src_uid,
195       { "Source UID",
196         "rdm.src_uid",
197         FT_BYTES, BASE_HEX, NULL, 0x0,
198         "Source UID", HFILL }}, 
199         
200     { &hf_rdm_seq_nr,
201       { "Sequence Number",
202         "rdm.seq_nr",
203         FT_UINT8, BASE_DEC, NULL, 0x0,
204         "Sequence Number", HFILL }},
205
206     { &hf_rdm_res_type,
207       { "Response Type",
208         "rdm.res_type",
209         FT_UINT8, BASE_HEX, NULL, 0x0,
210         "Response Type", HFILL }},
211
212     { &hf_rdm_msg_count,
213       { "Message Count",
214         "rdm.msg_count",
215         FT_UINT8, BASE_DEC, NULL, 0x0,
216         "Message Count", HFILL }},
217
218     { &hf_rdm_sub_device,
219       { "Sub Device",
220         "rdm.sub_device",
221         FT_UINT8, BASE_HEX, NULL, 0x0,
222         "Sub Device", HFILL }},
223         
224    { &hf_rdm_mdb,
225       { "MDB",
226         "rdm.mdb",
227         FT_BYTES, BASE_HEX, NULL, 0x0,
228         "MDB", HFILL }}, 
229
230     { &hf_rdm_checksum,
231       { "Checksum",
232         "rdm.checksum",
233         FT_UINT16, BASE_HEX, NULL, 0x0,
234         "Checksum", HFILL }}
235   };
236
237   static gint *ett[] = {
238     &ett_rdm,
239   };
240
241   proto_rdm = proto_register_protocol("RDM","RDM","rdm");
242
243   proto_register_field_array(proto_rdm,hf,array_length(hf));
244   proto_register_subtree_array(ett,array_length(ett));
245
246   register_dissector("rdm", dissect_rdm, proto_rdm);
247 }
248
249 /* The registration hand-off routing */
250
251 void
252 proto_reg_handoff_rdm(void) {
253   static int rdm_initialized = FALSE;
254   static dissector_handle_t rdm_handle;
255      
256   if(!rdm_initialized) {
257     rdm_handle = create_dissector_handle(dissect_rdm,proto_rdm);
258     rdm_initialized = TRUE;
259   } else {
260     dissector_delete("udp.port",0,rdm_handle);
261   }
262                            
263   dissector_add("udp.port",0,rdm_handle);
264 }
265
266 /* Start the functions we need for the plugin stuff */
267
268 #ifndef ENABLE_STATIC
269
270 G_MODULE_EXPORT void
271 plugin_register(void)
272 {
273   /* register the new protocol, protocol fields, and subtrees */
274   if (proto_rdm == -1) { /* execute protocol initialization only once */
275     proto_register_rdm();
276   }
277 }
278
279 G_MODULE_EXPORT void
280 plugin_reg_handoff(void){
281   proto_reg_handoff_rdm();
282 }
283
284 #endif
285
286 /* End the functions we need for plugin stuff */
287