Trivial warning fixes
[obnox/wireshark/wip.git] / epan / dissectors / packet-ipars.c
1 /* packet-ipars.c
2  * Routines for IPARS/ALC (International Passenger Airline Reservation System/Airline Link Control) WAN protocol dissection
3  * Copyright 2007, Fulko Hew, SITA INC Canada, Inc.
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 /* Use tabstops = 4 */
27
28 /* NOTE:        This should be rewritten to be more in line with how packet-uts.c is
29  *                      written so that there are filterable fields available for IPARS too.
30  */
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <glib.h>
37 #include <epan/packet.h>
38 #include <epan/emem.h>
39
40 static int              proto_ipars             = -1;
41 static guint8   ipars_eomtype   = -1;
42 static gint             ett_ipars               = -1;
43
44 #define S1              (0x00)
45 #define S2              (0x20)
46 #define GA              (0x03)
47 #define EOMpb   (0x10)
48 #define EOMc    (0x11)
49 #define EOMu    (0x12)
50 #define EOMi    (0x13)
51
52 #define MAX_EOM_MSG_SIZE        (16)                    /* max size of an EOMx indicator string */
53
54 static void
55 dissect_ipars(tvbuff_t *tvb, packet_info *pinfo _U_ , proto_tree *tree)
56 {
57         proto_tree  *ipars_tree = NULL;
58         proto_item  *ti;
59         int                     bytes;
60         guint8          ia = 0, ta = 0, cmd = 0, la = 0;
61         tvbuff_t        *next_tvb;
62         int                     offset = 0;
63         gchar           *eom_msg;
64
65         eom_msg = ep_alloc(MAX_EOM_MSG_SIZE);
66         eom_msg[0] = 0;
67
68         if (check_col(pinfo->cinfo, COL_INFO))
69                 col_clear(pinfo->cinfo, COL_INFO);
70
71         if (check_col(pinfo->cinfo, COL_PROTOCOL))                                                                                              /* set the protocol column on summary display */
72                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "IPARS");
73
74         if (tvb_length_remaining(tvb, 0) >= 2 ) {
75                 ia = tvb_get_guint8(tvb, 0) & 0x3f;
76                 ta = tvb_get_guint8(tvb, 1) & 0x3f;
77                 if (ia == S1 && ta == S2) {                                                                                                                     /* if the first two bytes are S1/S2 skip over them */
78                         offset = 2;
79                 }
80         }
81         if (tvb_length_remaining(tvb, offset) >= 1) ia = tvb_get_guint8(tvb, offset + 0);
82         if (tvb_length_remaining(tvb, offset) >= 2) ta = tvb_get_guint8(tvb, offset + 1);
83
84         if (ia == 0x83 || ia == 0x43 || ia == GA) {                                                                                             /* if its an FPGA or 'corresponsdance code' 'go ahead'... */
85                 if (tvb_length_remaining(tvb, offset) > 2) {                                                                            /* if the msg is long, it must have been a 'poll' */
86                         if (check_col(pinfo->cinfo, COL_INFO))
87                                 col_add_fstr(pinfo->cinfo, COL_INFO, "Poll IA: %2.2X", ta);
88                 } else {                                                                                                                                                        /* if its short, then it was a 'no traffic' response */
89                         if (tvb_length_remaining(tvb, offset) >= 2 ) {
90                                 if (check_col(pinfo->cinfo, COL_INFO))
91                                         col_add_fstr(pinfo->cinfo, COL_INFO, "GoAhead NextIA (0x%2.2X)", ta);
92                         } else {
93                                 if (check_col(pinfo->cinfo, COL_INFO))
94                                         col_add_str(pinfo->cinfo, COL_INFO, "GoAhead NextIA");
95                         }
96                 }
97         } else {                                                                                                                                                /* if its not a 'go ahead'... it must be some kind of data message */
98                 ia &= 0x3f;
99                 ta &= 0x3f;
100                 if (ta == 0x20) {
101                         if (check_col(pinfo->cinfo, COL_INFO))
102                                 col_add_fstr(pinfo->cinfo, COL_INFO, "Reset IA: %2.2X", ia);                                            /* the TA character was the 'reset' command */
103                 }
104                 if (tvb_length_remaining(tvb, offset) >= 3) cmd = tvb_get_guint8(tvb, offset + 2) & 0x3f;       /* get the first two bytes of the data message */
105                 if (tvb_length_remaining(tvb, offset) >= 4) la  = tvb_get_guint8(tvb, offset + 3) & 0x3f;
106                 if (cmd == 0x1f && la == 0x38) {
107                         if (check_col(pinfo->cinfo, COL_INFO))
108                                 col_add_fstr(pinfo->cinfo, COL_INFO, "Please Resend - IA: %2.2X TA: %2.2X", ia, ta);                            /* light the resend indicator */
109                 } else if (cmd == 0x2a && la == 0x05) {
110                         if (check_col(pinfo->cinfo, COL_INFO))
111                                 col_add_fstr(pinfo->cinfo, COL_INFO, "Unsolicited Msg Indicator - IA: %2.2X TA: %2.2X", ia, ta);        /* light the unsolicited msg indicator */
112                 } else {
113                         if (check_col(pinfo->cinfo, COL_INFO))
114                                 col_add_fstr(pinfo->cinfo, COL_INFO, "Data Msg - IA: %2.2X TA: %2.2X", ia, ta);                                         /* it was a data message (display or printer */
115                 }
116         }
117
118         if (tree) {
119                 bytes = tvb_length_remaining(tvb, 0);
120                 if (bytes > 0) {
121                         ia = tvb_get_guint8(tvb, 0) & 0x3f;
122
123                         ti = proto_tree_add_protocol_format(tree, proto_ipars, tvb, 0, -1, "Ipars");
124                         ipars_tree = proto_item_add_subtree(ti, ett_ipars);
125
126                         if (ia == 0x03) {
127                                 proto_tree_add_protocol_format(ipars_tree, proto_ipars, tvb, 0, 1, "GoAhead Next IA");
128                                 if (check_col(pinfo->cinfo, COL_INFO))
129                                         col_add_str(pinfo->cinfo, COL_INFO, "GoAhead");
130                                 return;
131                         } else if (ia != S1) {
132                                 proto_tree_add_protocol_format(ipars_tree, proto_ipars, tvb,
133                                         0,
134                                         bytes, "Unknown format - Data (%d byte%s)", bytes,
135                                         plurality(bytes, "", "s"));
136                                 return;
137                         }
138                         proto_tree_add_protocol_format(ipars_tree, proto_ipars, tvb, 0, 1, "S1");
139                         ia = tvb_get_guint8(tvb, 1) & 0x3f;
140                         if (ia != S2) {
141                                 proto_tree_add_protocol_format(ipars_tree, proto_ipars, tvb,
142                                         0,
143                                         bytes, "Unknown format - Data (%d byte%s)", bytes,
144                                         plurality(bytes, "", "s"));
145                                 return;
146                         }
147                         proto_tree_add_protocol_format(ipars_tree, proto_ipars, tvb, 1, 1, "S2");
148                         ia = tvb_get_guint8(tvb, 2) & 0x3f;
149                         if (ia == GA) {
150                                 ia = tvb_get_guint8(tvb, 3) & 0x3f;
151                                 proto_tree_add_protocol_format(ipars_tree, proto_ipars, tvb, 2, 2, "GoAhead IA: %2.2X", ia);
152                                 ipars_eomtype = tvb_get_guint8(tvb, 4) & 0x3f;
153                                 switch (ipars_eomtype) {
154                                         case EOMc:      g_snprintf(eom_msg, MAX_EOM_MSG_SIZE, "EOMc");                                                          break;
155                                         case EOMi:      g_snprintf(eom_msg, MAX_EOM_MSG_SIZE, "EOMi");                                                          break;
156                                         case EOMu:      g_snprintf(eom_msg, MAX_EOM_MSG_SIZE, "EOMu");                                                          break;
157                                         case EOMpb:     g_snprintf(eom_msg, MAX_EOM_MSG_SIZE, "EOMpb");                                                         break;
158                                         default:        g_snprintf(eom_msg, MAX_EOM_MSG_SIZE, "Unknown EOM type (0x%2.2X)", ia);        break;
159                                 }
160                                 proto_tree_add_protocol_format(ipars_tree, proto_ipars, tvb, 4, 1, "%s", eom_msg);
161                                 ia = tvb_get_guint8(tvb, 5) & 0x3f;
162                                 proto_tree_add_protocol_format(ipars_tree, proto_ipars, tvb, 5, 1, "Good BCC");
163                         } else {
164                                 next_tvb = tvb_new_subset(tvb, 3, -1, -1);
165                                 proto_tree_add_protocol_format(ipars_tree, proto_ipars, next_tvb,
166                                         0,
167                                         bytes, "Data (%d byte%s)", bytes,
168                                         plurality(bytes, "", "s"));
169                                 /* tvb_free(next_tvb);  */                              /* having this uncommented seems to cause a segfault (Fulko Hew, Nov 6/07) */
170                                 return;
171
172                         }
173                 }
174         }
175 }
176
177 void
178 proto_register_ipars(void)
179 {
180         static gint *ett[] = {
181                 &ett_ipars,
182         };
183
184         proto_ipars = proto_register_protocol("International Passenger Airline Reservation System ", "IPARS", "ipars");         /* name, short name, abbrev */
185         proto_register_subtree_array(ett, array_length(ett));
186
187         register_dissector("ipars", dissect_ipars, proto_ipars);
188 }