Remove all $Id$ from top of file
[metze/wireshark/wip.git] / epan / dissectors / packet-ipsi-ctl.c
1 /* packet-ipsi-ctl.c
2  * Routines for Avaya IPSI Control packet disassembly
3  * Traffic is encapsulated Avaya proprietary CCMS
4  * (Control Channel Message Set) between PCD and SIM
5  *
6  * Copyright 2008, Randy McEoin <rmceoin@ahbelo.com>
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26
27 #include "config.h"
28
29 #include <glib.h>
30 #include <epan/packet.h>
31
32 void proto_register_ipsictl(void);
33 void proto_reg_handoff_ipsictl(void);
34
35 #define IPSICTL_PORT            5010
36 #define IPSICTL_PDU_MAGIC       0x0300
37
38 static int proto_ipsictl = -1;
39
40 static int hf_ipsictl_pdu = -1;
41 static int hf_ipsictl_magic = -1;
42 static int hf_ipsictl_length = -1;
43 static int hf_ipsictl_type = -1;
44 static int hf_ipsictl_sequence = -1;
45 static int hf_ipsictl_field1 = -1;
46 static int hf_ipsictl_data = -1;
47
48 static gint ett_ipsictl = -1;
49 static gint ett_ipsictl_pdu = -1;
50
51 static void dissect_ipsictl(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
52 {
53
54   proto_tree   *ipsictl_tree = NULL;
55   proto_tree   *pdu_tree = NULL;
56   proto_item   *ti;
57   int           offset = 0;
58   int           loffset = 0;
59   int           llength = 0;
60   int           remaining_length;
61   guint16       magic;
62   guint16       length;
63   guint16       type=0;
64   guint16       sequence=0;
65   int           first_sequence=-1;
66   int           last_sequence=-1;
67   guint16       field1=0;
68   guint16       pdu=0;
69   int           haspdus=0;
70
71   remaining_length=tvb_reported_length_remaining(tvb, offset);
72
73   if (tree) {
74
75       ti = proto_tree_add_item(tree, proto_ipsictl, tvb, offset, remaining_length, ENC_NA);
76       ipsictl_tree = proto_item_add_subtree(ti, ett_ipsictl);
77
78   }
79
80   magic = tvb_get_ntohs(tvb, offset);
81   if (magic == IPSICTL_PDU_MAGIC)
82   {
83     haspdus=1;
84   }
85
86   while (haspdus &&
87     ((remaining_length=tvb_reported_length_remaining(tvb, offset)) > 6))
88   {
89     loffset = offset;
90
91     magic = tvb_get_ntohs(tvb, loffset); loffset+=2;
92     length = tvb_get_ntohs(tvb, loffset); loffset+=2;
93     llength=length;
94     remaining_length-=4;
95     if (remaining_length>=2)
96     {
97       type = tvb_get_ntohs(tvb, loffset); loffset+=2;
98       remaining_length-=2;
99       llength-=2;
100     }
101     if (remaining_length>=2)
102     {
103       sequence = tvb_get_ntohs(tvb, loffset); loffset+=2;
104       remaining_length-=2;
105       llength-=2;
106       if (first_sequence==-1)
107       {
108         first_sequence=sequence;
109       }else{
110         last_sequence=sequence;
111       }
112     }
113     if (remaining_length>=2)
114     {
115       field1 = tvb_get_ntohs(tvb, loffset);
116       llength-=2;
117     }
118
119     if (tree) {
120
121       ti = proto_tree_add_uint(ipsictl_tree, hf_ipsictl_pdu, tvb,
122            offset, (length+4), pdu);
123
124       pdu_tree = proto_item_add_subtree(ti, ett_ipsictl_pdu);
125     }
126
127     loffset=offset;
128     remaining_length=tvb_reported_length_remaining(tvb, offset);
129
130     if (tree) {
131       proto_tree_add_uint(pdu_tree, hf_ipsictl_magic, tvb, loffset, 2, magic);
132     }
133     loffset+=2; remaining_length-=2;
134     if (tree) {
135       proto_tree_add_uint(pdu_tree, hf_ipsictl_length, tvb, loffset, 2, length);
136     }
137     loffset+=2; remaining_length-=2;
138
139     if (remaining_length>=2)
140     {
141       if (tree) {
142         proto_tree_add_uint(pdu_tree, hf_ipsictl_type, tvb, loffset, 2, type);
143       }
144       loffset+=2; remaining_length-=2;
145     }
146     if (remaining_length>=2)
147     {
148       if (tree) {
149         proto_tree_add_uint(pdu_tree, hf_ipsictl_sequence, tvb, loffset, 2, sequence);
150       }
151       loffset+=2; remaining_length-=2;
152     }
153     if (remaining_length>=2)
154     {
155       if (tree) {
156         proto_tree_add_uint(pdu_tree, hf_ipsictl_field1, tvb, loffset, 2, field1);
157       }
158       loffset+=2; remaining_length-=2;
159     }
160     if (remaining_length>=2)
161     {
162       if (tree) {
163         proto_tree_add_item(pdu_tree, hf_ipsictl_data, tvb, loffset, llength, ENC_NA);
164       }
165       loffset+=llength;
166     }
167
168     offset=loffset;
169     pdu++;
170   }
171
172   if (!haspdus)
173   {
174     if (tree) {
175       proto_tree_add_item(ipsictl_tree, hf_ipsictl_data, tvb, offset, -1, ENC_NA);
176     }
177   }
178
179   col_set_str(pinfo->cinfo, COL_PROTOCOL, "IPSICTL");
180
181   if (haspdus)
182   {
183     if (last_sequence==-1)
184     {
185       col_add_fstr(pinfo->cinfo, COL_INFO, "PDUS=%d, Seq=0x%04x",
186         pdu,first_sequence);
187     }else{
188       col_add_fstr(pinfo->cinfo, COL_INFO, "PDUS=%d, Seq=0x%04x-0x%04x",
189         pdu,first_sequence,last_sequence);
190     }
191   }else{
192     col_set_str(pinfo->cinfo, COL_INFO, "Initialization");
193   }
194
195
196 } /* dissect_ipsictl */
197
198 void proto_register_ipsictl(void)
199 {
200
201   static hf_register_info hf[] = {
202     { &hf_ipsictl_pdu,
203       { "PDU",  "ipsictl.pdu",
204         FT_UINT16,      BASE_DEC,       NULL,   0x0,
205         "IPSICTL PDU", HFILL }},
206     { &hf_ipsictl_magic,
207       { "Magic",        "ipsictl.magic",
208         FT_UINT16,      BASE_HEX,       NULL,   0x0,
209         "IPSICTL Magic", HFILL }},
210     { &hf_ipsictl_length,
211       { "Length",       "ipsictl.length",
212         FT_UINT16,      BASE_HEX,       NULL,   0x0,
213         "IPSICTL Length", HFILL }},
214     { &hf_ipsictl_type,
215       { "Type", "ipsictl.type",
216         FT_UINT16,      BASE_HEX,       NULL,   0x0,
217         "IPSICTL Type", HFILL }},
218     { &hf_ipsictl_sequence,
219       { "Sequence",     "ipsictl.sequence",
220         FT_UINT16,      BASE_HEX,       NULL,   0x0,
221         "IPSICTL Sequence", HFILL }},
222     { &hf_ipsictl_field1,
223       { "Field1",       "ipsictl.field1",
224         FT_UINT16,      BASE_HEX,       NULL,   0x0,
225         "IPSICTL Field1", HFILL }},
226     { &hf_ipsictl_data,
227       { "Data", "ipsictl.data",
228         FT_BYTES,       BASE_NONE,      NULL,   0x0,
229         "IPSICTL data", HFILL }},
230   };
231
232   static gint *ett[] = {
233     &ett_ipsictl,
234     &ett_ipsictl_pdu
235   };
236
237   proto_ipsictl = proto_register_protocol("IPSICTL", "IPSICTL", "ipsictl");
238   proto_register_field_array(proto_ipsictl, hf, array_length(hf));
239   proto_register_subtree_array(ett, array_length(ett));
240
241 }
242
243 void proto_reg_handoff_ipsictl(void)
244 {
245
246   dissector_handle_t ipsictl_handle = NULL;
247
248   ipsictl_handle = create_dissector_handle(dissect_ipsictl, proto_ipsictl);
249
250   dissector_add_uint("tcp.port", IPSICTL_PORT, ipsictl_handle);
251
252 }
253