name change
[obnox/wireshark/wip.git] / plugins / irda / packet-ircomm.c
1 /* packet-ircomm.c
2  * Routines for IrCOMM dissection
3  * By Jan Kiszka <jan.kiszka@web.de>
4  * Copyright 2003 Jan Kiszka
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@unicom.net>
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include "moduleinfo.h"
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include <glib.h>
38 #include <gmodule.h>
39 #include <epan/packet.h>
40 #include <epan/proto.h>
41
42
43 /*
44  * See
45  *
46  *      http://www.irda.org/standards/specifications.asp
47  *
48  * for various IrDA specifications.
49  */
50
51 #include "irda-appl.h"
52
53
54 /* Parameters common to all service types */
55 #define IRCOMM_SERVICE_TYPE     0x00
56 #define IRCOMM_PORT_TYPE        0x01 /* Only used in LM-IAS */
57 #define IRCOMM_PORT_NAME        0x02 /* Only used in LM-IAS */
58
59 /* Parameters for both 3 wire and 9 wire */
60 #define IRCOMM_DATA_RATE        0x10
61 #define IRCOMM_DATA_FORMAT      0x11
62 #define IRCOMM_FLOW_CONTROL     0x12
63 #define IRCOMM_XON_XOFF         0x13
64 #define IRCOMM_ENQ_ACK          0x14
65 #define IRCOMM_LINE_STATUS      0x15
66 #define IRCOMM_BREAK            0x16
67
68 /* Parameters for 9 wire */
69 #define IRCOMM_DTE              0x20
70 #define IRCOMM_DCE              0x21
71 #define IRCOMM_POLL             0x22
72
73 /* Service type (details) */
74 #define IRCOMM_3_WIRE_RAW       0x01
75 #define IRCOMM_3_WIRE           0x02
76 #define IRCOMM_9_WIRE           0x04
77 #define IRCOMM_CENTRONICS       0x08
78
79 /* Port type (details) */
80 #define IRCOMM_SERIAL           0x01
81 #define IRCOMM_PARALLEL         0x02
82
83 /* Data format (details) */
84 #define IRCOMM_WSIZE_5          0x00
85 #define IRCOMM_WSIZE_6          0x01
86 #define IRCOMM_WSIZE_7          0x02
87 #define IRCOMM_WSIZE_8          0x03
88
89 #define IRCOMM_1_STOP_BIT       0x00
90 #define IRCOMM_2_STOP_BIT       0x04 /* 1.5 if char len 5 */
91
92 #define IRCOMM_PARITY_DISABLE   0x00
93 #define IRCOMM_PARITY_ENABLE    0x08
94
95 #define IRCOMM_PARITY_ODD       0x00
96 #define IRCOMM_PARITY_EVEN      0x10
97 #define IRCOMM_PARITY_MARK      0x20
98 #define IRCOMM_PARITY_SPACE     0x30
99
100 /* Flow control */
101 #define IRCOMM_XON_XOFF_IN      0x01
102 #define IRCOMM_XON_XOFF_OUT     0x02
103 #define IRCOMM_RTS_CTS_IN       0x04
104 #define IRCOMM_RTS_CTS_OUT      0x08
105 #define IRCOMM_DSR_DTR_IN       0x10
106 #define IRCOMM_DSR_DTR_OUT      0x20
107 #define IRCOMM_ENQ_ACK_IN       0x40
108 #define IRCOMM_ENQ_ACK_OUT      0x80
109
110 /* Line status */
111 #define IRCOMM_OVERRUN_ERROR    0x02
112 #define IRCOMM_PARITY_ERROR     0x04
113 #define IRCOMM_FRAMING_ERROR    0x08
114
115 /* DTE (Data terminal equipment) line settings */
116 #define IRCOMM_DELTA_DTR        0x01
117 #define IRCOMM_DELTA_RTS        0x02
118 #define IRCOMM_DTR              0x04
119 #define IRCOMM_RTS              0x08
120
121 /* DCE (Data communications equipment) line settings */
122 #define IRCOMM_DELTA_CTS        0x01  /* Clear to send has changed */
123 #define IRCOMM_DELTA_DSR        0x02  /* Data set ready has changed */
124 #define IRCOMM_DELTA_RI         0x04  /* Ring indicator has changed */
125 #define IRCOMM_DELTA_CD         0x08  /* Carrier detect has changed */
126 #define IRCOMM_CTS              0x10  /* Clear to send is high */
127 #define IRCOMM_DSR              0x20  /* Data set ready is high */
128 #define IRCOMM_RI               0x40  /* Ring indicator is high */
129 #define IRCOMM_CD               0x80  /* Carrier detect is high */
130 #define IRCOMM_DCE_DELTA_ANY    0x0f
131
132 /* Initialize the subtree pointers */
133 static gint ett_ircomm = -1;
134 static gint ett_ircomm_ctrl = -1;
135
136 #define MAX_PARAMETERS          32
137 static gint ett_param[MAX_IAP_ENTRIES * MAX_PARAMETERS];
138
139
140 static int proto_ircomm = -1;
141 static int hf_ircomm_param = -1;
142 static int hf_param_pi = -1;
143 static int hf_param_pl = -1;
144 static int hf_param_pv = -1;
145 static int hf_control = -1;
146 static int hf_control_len = -1;
147
148 static gboolean dissect_ircomm_parameters(tvbuff_t* tvb, unsigned offset, packet_info* pinfo,
149                                           proto_tree* tree, unsigned list_index, guint8 attr_type);
150 static gboolean dissect_ircomm_ttp_lsap(tvbuff_t* tvb, unsigned offset, packet_info* pinfo,
151                                         proto_tree* tree, unsigned list_index, guint8 attr_type);
152 static gboolean dissect_ircomm_lmp_lsap(tvbuff_t* tvb, unsigned offset, packet_info* pinfo,
153                                         proto_tree* tree, unsigned list_index, guint8 attr_type);
154
155 ias_attr_dissector_t ircomm_attr_dissector[] = {
156 /* IrDA:IrCOMM attribute dissectors */
157     { "Parameters",             dissect_ircomm_parameters },
158     { "IrDA:TinyTP:LsapSel",    dissect_ircomm_ttp_lsap },
159     { NULL,                     NULL }
160 };
161
162 ias_attr_dissector_t irlpt_attr_dissector[] = {
163 /* IrLPT attribute dissectors */
164     { "IrDA:IrLMP:LsapSel", dissect_ircomm_lmp_lsap },
165     { "IrDA:IrLMP:LSAPSel", dissect_ircomm_lmp_lsap },  /* according to IrCOMM V1.0, p25 */
166     { NULL, NULL }
167 };
168
169
170 /*
171  * Dissect the cooked IrCOMM protocol
172  */
173 static void dissect_cooked_ircomm(tvbuff_t* tvb, packet_info* pinfo, proto_tree* root)
174 {
175     unsigned offset = 0;
176     unsigned clen;
177
178
179     if (tvb_length(tvb) == 0)
180         return;
181
182     /* Make entries in Protocol column on summary display */
183     if (check_col(pinfo->cinfo, COL_PROTOCOL))
184         col_set_str(pinfo->cinfo, COL_PROTOCOL, "IrCOMM");
185
186     clen = tvb_get_guint8(tvb, offset);
187
188     if (check_col(pinfo->cinfo, COL_INFO))
189     {
190         char        buf[128];
191         unsigned    len = tvb_length(tvb) - 1 - clen;
192
193
194         if (len > 0)
195             sprintf(buf, "Clen=%d, UserData: %d byte%s", clen, len, (len > 1)? "s": "");
196         else
197             sprintf(buf, "Clen=%d", clen);
198         col_add_str(pinfo->cinfo, COL_INFO, buf);
199     }
200
201     if (root)
202     {
203         /* create display subtree for the protocol */
204         proto_item* ti   = proto_tree_add_item(root, proto_ircomm, tvb, 0, -1, FALSE);
205         proto_tree* tree = proto_item_add_subtree(ti, ett_ircomm);
206
207         proto_tree* ctrl_tree;
208
209
210         ti        = proto_tree_add_item(tree, hf_control, tvb, 0, clen + 1, FALSE);
211         ctrl_tree = proto_item_add_subtree(ti, ett_ircomm_ctrl);
212         proto_tree_add_item(ctrl_tree, hf_control_len, tvb, offset, 1, FALSE);
213         offset++;
214         {
215             tvbuff_t *cvalue = tvb_new_subset(tvb, offset, clen, clen);
216             call_dissector(data_handle, cvalue, pinfo, ctrl_tree);
217             offset += clen;
218         }
219
220         tvb = tvb_new_subset(tvb, offset, -1, -1);
221         call_dissector(data_handle, tvb, pinfo, tree);
222     }
223 }
224
225
226 /*
227  * Dissect the raw IrCOMM/IrLPT protocol
228  */
229 static void dissect_raw_ircomm(tvbuff_t* tvb, packet_info* pinfo, proto_tree* root)
230 {
231     unsigned len = tvb_length(tvb);
232
233
234     if (len == 0)
235         return;
236
237     /* Make entries in Protocol column on summary display */
238     if (check_col(pinfo->cinfo, COL_PROTOCOL))
239         col_set_str(pinfo->cinfo, COL_PROTOCOL, "IrCOMM");
240
241     if (check_col(pinfo->cinfo, COL_INFO))
242     {
243         char    buf[128];
244
245
246         sprintf(buf, "User Data: %d byte%s", len, (len > 1)? "s": "");
247         col_add_str(pinfo->cinfo, COL_INFO, buf);
248     }
249
250     if (root)
251     {
252         /* create display subtree for the protocol */
253         proto_item* ti   = proto_tree_add_item(root, proto_ircomm, tvb, 0, -1, FALSE);
254         proto_tree* tree = proto_item_add_subtree(ti, ett_ircomm);
255
256         call_dissector(data_handle, tvb, pinfo, tree);
257     }
258 }
259
260
261 /*
262  * Dissect IrCOMM IAS "Parameters" attribute
263  */
264 static gboolean dissect_ircomm_parameters(tvbuff_t* tvb, unsigned offset, packet_info* pinfo _U_,
265                                           proto_tree* tree, unsigned list_index, guint8 attr_type)
266 {
267     unsigned    len;
268     unsigned    n = 0;
269     proto_item* ti;
270     proto_tree* p_tree;
271     char        buf[256];
272     guint8      pv;
273
274
275     if (!check_iap_octet_result(tvb, tree, offset, "Parameters", attr_type))
276         return TRUE;
277
278     if (tree)
279     {
280         len = tvb_get_ntohs(tvb, offset) + offset + 2;
281         offset += 2;
282
283         while (offset < len)
284         {
285             guint8  p_len = tvb_get_guint8(tvb, offset + 1);
286
287
288             ti = proto_tree_add_item(tree, hf_ircomm_param, tvb, offset, p_len + 2, FALSE);
289             p_tree = proto_item_add_subtree(ti, ett_param[list_index * MAX_PARAMETERS + n]);
290
291             buf[0] = 0;
292
293             switch (tvb_get_guint8(tvb, offset))
294             {
295                 case IRCOMM_SERVICE_TYPE:
296                     proto_item_append_text(ti, ": Service Type (");
297
298                     pv = tvb_get_guint8(tvb, offset+2);
299                     if (pv & IRCOMM_3_WIRE_RAW)
300                         strcat(buf, ", 3-Wire raw");
301                     if (pv & IRCOMM_3_WIRE)
302                         strcat(buf, ", 3-Wire");
303                     if (pv & IRCOMM_9_WIRE)
304                         strcat(buf, ", 9-Wire");
305                     if (pv & IRCOMM_CENTRONICS)
306                         strcat(buf, ", Centronics");
307
308                     strcat(buf, ")");
309
310                     proto_item_append_text(ti, buf+2);
311
312                     break;
313
314                 case IRCOMM_PORT_TYPE:
315                     proto_item_append_text(ti, ": Port Type (");
316
317                     pv = tvb_get_guint8(tvb, offset+2);
318                     if (pv & IRCOMM_SERIAL)
319                         strcat(buf, ", serial");
320                     if (pv & IRCOMM_PARALLEL)
321                         strcat(buf, ", parallel");
322
323                     strcat(buf, ")");
324
325                     proto_item_append_text(ti, buf+2);
326
327                     break;
328
329                 case IRCOMM_PORT_NAME:
330                     /* XXX - the IrCOMM V1.0 spec says this "Normally
331                        human readable text, but not required". */
332                     proto_item_append_text(ti, ": Port Name (\"%s\")",
333                         tvb_format_text(tvb, offset+2, p_len));
334
335                     break;
336
337                 default:
338                     proto_item_append_text(ti, ": unknown");
339             }
340
341             offset = dissect_param_tuple(tvb, p_tree, offset);
342             n++;
343         }
344
345     }
346
347     return TRUE;
348 }
349
350
351 /*
352  * Dissect IrCOMM IAS "IrDA:TinyTP:LsapSel" attribute
353  */
354 static gboolean dissect_ircomm_ttp_lsap(tvbuff_t* tvb, unsigned offset, packet_info* pinfo,
355                                         proto_tree* tree, unsigned list_index _U_, guint8 attr_type)
356 {
357     guint8 dlsap;
358
359
360     if ((dlsap = check_iap_lsap_result(tvb, tree, offset, "IrDA:TinyTP:LsapSel", attr_type)) == 0)
361         return FALSE;
362
363     add_lmp_conversation(pinfo, dlsap, TRUE, dissect_cooked_ircomm);
364
365     return FALSE;
366 }
367
368
369 /*
370  * Dissect IrCOMM/IrLPT IAS "IrDA:IrLMP:LsapSel" attribute
371  */
372 static gboolean dissect_ircomm_lmp_lsap(tvbuff_t* tvb, unsigned offset, packet_info* pinfo,
373                                         proto_tree* tree, unsigned list_index _U_, guint8 attr_type)
374 {
375     guint8 dlsap;
376
377
378     if ((dlsap = check_iap_lsap_result(tvb, tree, offset, "IrDA:IrLMP:LsapSel", attr_type)) == 0)
379         return FALSE;
380
381     add_lmp_conversation(pinfo, dlsap, FALSE, dissect_raw_ircomm);
382
383     return FALSE;
384 }
385
386
387 /*
388  * Register the IrCOMM protocol
389  */
390 void proto_register_ircomm(void)
391 {
392     unsigned i;
393
394     /* Setup list of header fields */
395     static hf_register_info hf_ircomm[] = {
396         { &hf_ircomm_param,
397             { "IrCOMM Parameter", "ircomm.parameter",
398                 FT_NONE, BASE_NONE, NULL, 0,
399                 "", HFILL }},
400         { &hf_param_pi,
401             { "Parameter Identifier", "ircomm.pi",
402                 FT_UINT8, BASE_HEX, NULL, 0,
403                 "", HFILL }},
404         { &hf_param_pl,
405             { "Parameter Length", "ircomm.pl",
406                 FT_UINT8, BASE_HEX, NULL, 0,
407                 "", HFILL }},
408         { &hf_param_pv,
409             { "Parameter Value", "ircomm.pv",
410                 FT_BYTES, BASE_HEX, NULL, 0,
411                 "", HFILL }},
412         { &hf_control,
413             { "Control Channel", "ircomm.control",
414                 FT_NONE, BASE_NONE, NULL, 0,
415                 "", HFILL }},
416         { &hf_control_len,
417             { "Clen", "ircomm.control.len",
418                 FT_UINT8, BASE_DEC, NULL, 0,
419                 "", HFILL }},
420     };
421
422     /* Setup protocol subtree arrays */
423     static gint* ett[] = {
424         &ett_ircomm,
425         &ett_ircomm_ctrl
426     };
427
428     static gint* ett_p[MAX_IAP_ENTRIES * MAX_PARAMETERS];
429
430
431     /* Register protocol names and descriptions */
432     proto_ircomm = proto_register_protocol("IrCOMM Protocol", "IrCOMM", "ircomm");
433
434     /* Required function calls to register the header fields */
435     proto_register_field_array(proto_ircomm, hf_ircomm, array_length(hf_ircomm));
436
437     /* Register subtrees */
438     proto_register_subtree_array(ett, array_length(ett));
439     for (i = 0; i < MAX_IAP_ENTRIES * MAX_PARAMETERS; i++)
440     {
441         ett_param[i] = -1;
442         ett_p[i]     = &ett_param[i];
443     }
444     proto_register_subtree_array(ett_p, MAX_IAP_ENTRIES * MAX_PARAMETERS);
445 }