Don't do fcn calls in arg of g_?to??(); Macro may very well eval args multiple times.
[obnox/wireshark/wip.git] / epan / dissectors / packet-sita.c
1 /* packet-sita.c
2  * Routines for SITA protocol dissection (ALC, UTS, Frame Relay, X.25)
3  * with a SITA specific link layer information header
4  *
5  * Copyright 2007, Fulko Hew, SITA INC Canada, Inc.
6  *
7  * $Id$
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 1998 Gerald Combs
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
26  */
27
28 /* Use tabstops = 4 */
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <stdlib.h>
35 #include <string.h>
36 #include <glib.h>
37 #include <epan/packet.h>
38 #include <wiretap/wtap.h>
39 #include <epan/emem.h>
40
41 static dissector_table_t        sita_dissector_table;
42 static dissector_handle_t       data_handle;
43 static gint                                     ett_sita                        = -1;
44 static gint                                     ett_sita_flags          = -1;
45 static gint                                     ett_sita_signals        = -1;
46 static gint                                     ett_sita_errors1        = -1;
47 static gint                                     ett_sita_errors2        = -1;
48 static int                                      proto_sita                      = -1;           /* Initialize the protocol and registered fields */
49 static int                                      hf_dir                          = -1;
50 static int                                      hf_framing                      = -1;
51 static int                                      hf_parity                       = -1;
52 static int                                      hf_collision            = -1;
53 static int                                      hf_longframe            = -1;
54 static int                                      hf_shortframe           = -1;
55 static int                                      hf_droppedframe         = -1;
56 static int                                      hf_nonaligned           = -1;
57 static int                                      hf_abort                        = -1;
58 static int                                      hf_lostcd                       = -1;
59 static int                                      hf_lostcts                      = -1;
60 static int                                      hf_rxdpll                       = -1;
61 static int                                      hf_overrun                      = -1;
62 static int                                      hf_length                       = -1;
63 static int                                      hf_crc                          = -1;
64 static int                                      hf_break                        = -1;
65 static int                                      hf_underrun                     = -1;
66 static int                                      hf_uarterror            = -1;
67 static int                                      hf_rtxlimit                     = -1;
68 static int                                      hf_proto                        = -1;
69 static int                                      hf_dsr                          = -1;
70 static int                                      hf_dtr                          = -1;
71 static int                                      hf_cts                          = -1;
72 static int                                      hf_rts                          = -1;
73 static int                                      hf_dcd                          = -1;
74
75 #define MAX_FLAGS_LEN 64                                                                        /* max size of a 'flags' decoded string */
76 #define IOP                                     "Local"
77 #define REMOTE                          "Remote"
78
79 static gchar *
80 format_flags_string(guchar value, const gchar *array[])
81 {
82         int                     i;
83         guint           bpos;
84         emem_strbuf_t   *buf;
85         const char      *sep = "";
86
87         buf = ep_strbuf_sized_new(MAX_FLAGS_LEN, MAX_FLAGS_LEN);
88         for (i = 0; i < 8; i++) {
89                 bpos = 1 << i;
90                 if (value & bpos) {
91                         if (array[i][0]) {
92                                 /* there is a string to emit... */
93                                 ep_strbuf_append_printf(buf, "%s%s", sep,
94                                     array[i]);
95                                 sep = ", ";
96                         }
97                 }
98         }
99         return buf->str;
100 }
101
102 static void
103 dissect_sita(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
104 {
105         proto_item      *ti;
106         guchar          flags, signals, errors1, errors2, proto;
107         gchar           *errors1_string, *errors2_string, *signals_string, *flags_string;
108         proto_tree      *sita_tree                      = NULL;
109         proto_tree      *sita_flags_tree        = NULL;
110         proto_tree      *sita_errors1_tree      = NULL;
111         proto_tree      *sita_errors2_tree      = NULL;
112         proto_tree      *sita_signals_tree      = NULL;
113         const gchar     *rx_errors1_str[]       = {"Framing",           "Parity",       "Collision",    "Long-frame",   "Short-frame",  "",                     "",             ""                              };
114         const gchar     *rx_errors2_str[]       = {"Non-Aligned",       "Abort",        "CD-lost",              "DPLL",                 "Overrun",              "Length",       "CRC",  "Break"                 };
115         /*const gchar   *tx_errors1_str[]       = {"",                          "",                     "",                             "",                             "",                             "",                     "",             ""                              }; */
116         const gchar     *tx_errors2_str[]       = {"Underrun",          "CTS-lost",     "UART",                 "ReTx-limit",   "",                             "",                     "",             ""                              };
117         const gchar     *signals_str[]          = {"DSR",                       "DTR",          "CTS",                  "RTS",                  "DCD",                  "",                     "",             ""                              };
118         const gchar     *flags_str[]            = {"",                          "",                     "",                             "",                             "",                             "",                     "",             "No-buffers"    };
119
120         col_clear(pinfo->cinfo, COL_PROTOCOL);          /* erase the protocol */
121         col_clear(pinfo->cinfo, COL_INFO);                      /* and info columns so that the next decoder can fill them in */
122
123         flags   = pinfo->pseudo_header->sita.sita_flags;
124         signals = pinfo->pseudo_header->sita.sita_signals;
125         errors1 = pinfo->pseudo_header->sita.sita_errors1;
126         errors2 = pinfo->pseudo_header->sita.sita_errors2;
127         proto   = pinfo->pseudo_header->sita.sita_proto;
128
129         if (check_col(pinfo->cinfo, COL_DEF_SRC)) {
130                 if ((flags & SITA_FRAME_DIR) == SITA_FRAME_DIR_TXED) {
131                         col_set_str(pinfo->cinfo, COL_DEF_SRC, IOP);                            /* set the source (direction) column accordingly */
132                 } else {
133                         col_set_str(pinfo->cinfo, COL_DEF_SRC, REMOTE);
134                 }
135         }
136
137         if (check_col(pinfo->cinfo, COL_INFO))
138                 col_set_str(pinfo->cinfo, COL_INFO, "");
139
140         if (tree) {
141                 ti = proto_tree_add_protocol_format(tree, proto_sita, tvb, 0, 0, "Link Layer");
142                 sita_tree = proto_item_add_subtree(ti, ett_sita);
143
144                 proto_tree_add_uint(sita_tree, hf_proto, tvb, 0, 0, proto);
145
146                 flags_string = format_flags_string(flags, flags_str);
147                 ti = proto_tree_add_text(sita_tree, tvb, 0, 0, "Flags: 0x%02x (From %s)%s%s",
148                                 flags,
149                                 ((flags & SITA_FRAME_DIR) == SITA_FRAME_DIR_TXED) ? IOP : REMOTE,
150                                 strlen(flags_string) ? ", " : "",
151                                 flags_string);
152                 sita_flags_tree = proto_item_add_subtree(ti, ett_sita_flags);
153                 proto_tree_add_boolean(sita_flags_tree, hf_droppedframe,        tvb, 0, 0, flags);
154                 proto_tree_add_boolean(sita_flags_tree, hf_dir,                         tvb, 0, 0, flags);
155
156                 signals_string = format_flags_string(signals, signals_str);
157                 ti = proto_tree_add_text(sita_tree, tvb, 0, 0, "Signals: 0x%02x %s", signals, signals_string);
158                 sita_signals_tree = proto_item_add_subtree(ti, ett_sita_signals);
159                 proto_tree_add_boolean(sita_signals_tree, hf_dcd,               tvb, 0, 0, signals);
160                 proto_tree_add_boolean(sita_signals_tree, hf_rts,               tvb, 0, 0, signals);
161                 proto_tree_add_boolean(sita_signals_tree, hf_cts,               tvb, 0, 0, signals);
162                 proto_tree_add_boolean(sita_signals_tree, hf_dtr,               tvb, 0, 0, signals);
163                 proto_tree_add_boolean(sita_signals_tree, hf_dsr,               tvb, 0, 0, signals);
164
165                 if ((flags & SITA_FRAME_DIR) == SITA_FRAME_DIR_RXED) {
166                         errors1_string = format_flags_string(errors1, rx_errors1_str);
167                         ti = proto_tree_add_text(sita_tree, tvb, 0, 0, "Receive Status: 0x%02x %s", errors1, errors1_string);
168                         sita_errors1_tree = proto_item_add_subtree(ti, ett_sita_errors1);
169                         proto_tree_add_boolean(sita_errors1_tree, hf_shortframe,        tvb, 0, 0, errors1);
170                         proto_tree_add_boolean(sita_errors1_tree, hf_longframe,         tvb, 0, 0, errors1);
171                         proto_tree_add_boolean(sita_errors1_tree, hf_collision,         tvb, 0, 0, errors1);
172                         proto_tree_add_boolean(sita_errors1_tree, hf_parity,            tvb, 0, 0, errors1);
173                         proto_tree_add_boolean(sita_errors1_tree, hf_framing,           tvb, 0, 0, errors1);
174
175                         errors2_string = format_flags_string(errors2, rx_errors2_str);
176                         ti = proto_tree_add_text(sita_tree, tvb, 0, 0, "Receive Status: 0x%02x %s", errors2, errors2_string);
177                         sita_errors2_tree = proto_item_add_subtree(ti, ett_sita_errors2);
178                         proto_tree_add_boolean(sita_errors2_tree, hf_break,                     tvb, 0, 0, errors2);
179                         proto_tree_add_boolean(sita_errors2_tree, hf_crc,                       tvb, 0, 0, errors2);
180                         proto_tree_add_boolean(sita_errors2_tree, hf_length,            tvb, 0, 0, errors2);
181                         proto_tree_add_boolean(sita_errors2_tree, hf_overrun,           tvb, 0, 0, errors2);
182                         proto_tree_add_boolean(sita_errors2_tree, hf_rxdpll,            tvb, 0, 0, errors2);
183                         proto_tree_add_boolean(sita_errors2_tree, hf_lostcd,            tvb, 0, 0, errors2);
184                         proto_tree_add_boolean(sita_errors2_tree, hf_abort,                     tvb, 0, 0, errors2);
185                         proto_tree_add_boolean(sita_errors2_tree, hf_nonaligned,        tvb, 0, 0, errors2);
186                 } else {
187                         errors2_string = format_flags_string(errors2, tx_errors2_str);
188                         ti = proto_tree_add_text(sita_tree, tvb, 0, 0, "Transmit Status: 0x%02x %s", errors2, errors2_string);
189                         sita_errors1_tree = proto_item_add_subtree(ti, ett_sita_errors1);
190                         proto_tree_add_boolean(sita_errors1_tree, hf_rtxlimit,          tvb, 0, 0, errors2);
191                         proto_tree_add_boolean(sita_errors1_tree, hf_uarterror,         tvb, 0, 0, errors2);
192                         proto_tree_add_boolean(sita_errors1_tree, hf_lostcts,           tvb, 0, 0, errors2);
193                         proto_tree_add_boolean(sita_errors1_tree, hf_underrun,          tvb, 0, 0, errors2);
194                 }
195         }
196
197         if (!dissector_try_uint(sita_dissector_table, pinfo->pseudo_header->sita.sita_proto, tvb, pinfo, tree)) {               /* try to find and run an applicable dissector */
198                 if (check_col(pinfo->cinfo, COL_PROTOCOL))                                                                                                                              /* if one can't be found... tell them we don't */
199                         col_set_str(pinfo->cinfo, COL_PROTOCOL, "UNKNOWN");                                                                                                     /* know how to decode this protocol */
200                 if (check_col(pinfo->cinfo, COL_INFO))
201                         col_add_fstr(pinfo->cinfo, COL_INFO, "IOP protocol number: %u", pinfo->pseudo_header->sita.sita_proto); /* and give them the details then */
202                 call_dissector(data_handle, tvb, pinfo, tree);                                                                                                                  /* call the generic (hex display) decoder instead */
203         }
204 }
205
206 static const true_false_string tfs_sita_flags           = { "From Remote",      "From Local"    };
207 static const true_false_string tfs_sita_error           = { "Error",            ""                              };
208 static const true_false_string tfs_sita_violation       = { "Violation",        ""                              };
209 static const true_false_string tfs_sita_received        = { "Received",         ""                              };
210 static const true_false_string tfs_sita_lost            = { "Lost",                     ""                              };
211 static const true_false_string tfs_sita_exceeded        = { "Exceeded",         ""                              };
212 static const true_false_string tfs_sita_on_off          = { "On",                       "Off"                   };
213
214 static const value_string tfs_sita_proto[] = {
215         { SITA_PROTO_UNUSED,            "Unused"                                },
216         { SITA_PROTO_BOP_LAPB,          "LAPB"                                  },
217         { SITA_PROTO_ETHERNET,          "Ethernet"                              },
218         { SITA_PROTO_ASYNC_INTIO,       "Async (Interrupt I/O)" },
219         { SITA_PROTO_ASYNC_BLKIO,       "Async (Block I/O)"             },
220         { SITA_PROTO_ALC,                       "IPARS"                                 },
221         { SITA_PROTO_UTS,                       "UTS"                                   },
222         { SITA_PROTO_PPP_HDLC,          "PPP/HDLC"                              },
223         { SITA_PROTO_SDLC,                      "SDLC"                                  },
224         { SITA_PROTO_TOKENRING,         "Token Ring"                    },
225         { SITA_PROTO_I2C,                       "I2C"                                   },
226         { SITA_PROTO_DPM_LINK,          "DPM Link"                              },
227         { SITA_PROTO_BOP_FRL,           "Frame Relay"                   },
228         { 0,                                            NULL                                    }
229 };
230
231 void
232 proto_register_sita(void)
233 {
234         static hf_register_info hf[] = {
235                 { &hf_proto,            { "Protocol",           "sita.errors.protocol",         FT_UINT8,   BASE_HEX, VALS(tfs_sita_proto),     0,                                      "Protocol value",                                               HFILL }},
236
237                 { &hf_dir,                      { "Direction",          "sita.flags.flags",                     FT_BOOLEAN, 8, TFS(&tfs_sita_flags),    SITA_FRAME_DIR,                 "TRUE 'from Remote', FALSE 'from Local'",       HFILL }},
238                 { &hf_droppedframe,     { "No Buffers",         "sita.flags.droppedframe",      FT_BOOLEAN, 8, TFS(&tfs_sita_error),    SITA_ERROR_NO_BUFFER,                   "TRUE if Buffer Failure",               HFILL }},
239
240                 { &hf_framing,          { "Framing",            "sita.errors.framing",          FT_BOOLEAN, 8, TFS(&tfs_sita_error),    SITA_ERROR_RX_FRAMING,                  "TRUE if Framing Error",                HFILL }},
241                 { &hf_parity,           { "Parity",                     "sita.errors.parity",           FT_BOOLEAN, 8, TFS(&tfs_sita_error),    SITA_ERROR_RX_PARITY,                   "TRUE if Parity Error",                 HFILL }},
242                 { &hf_collision,        { "Collision",          "sita.errors.collision",        FT_BOOLEAN, 8, TFS(&tfs_sita_error),    SITA_ERROR_RX_COLLISION,                "TRUE if Collision",                    HFILL }},
243                 { &hf_longframe,        { "Long Frame",         "sita.errors.longframe",                FT_BOOLEAN, 8, TFS(&tfs_sita_error),    SITA_ERROR_RX_FRAME_LONG,               "TRUE if Long Frame Received",  HFILL }},
244                 { &hf_shortframe,       { "Short Frame",        "sita.errors.shortframe",       FT_BOOLEAN, 8, TFS(&tfs_sita_error),    SITA_ERROR_RX_FRAME_SHORT,              "TRUE if Short Frame",                  HFILL }},
245                 { &hf_nonaligned,       { "NonAligned",         "sita.errors.nonaligned",       FT_BOOLEAN, 8, TFS(&tfs_sita_error),    SITA_ERROR_RX_NONOCTET_ALIGNED, "TRUE if NonAligned Frame",             HFILL }},
246                 { &hf_abort,            { "Abort",                      "sita.errors.abort",            FT_BOOLEAN, 8, TFS(&tfs_sita_received), SITA_ERROR_RX_ABORT,                    "TRUE if Abort Received",               HFILL }},
247                 { &hf_lostcd,           { "Carrier",            "sita.errors.lostcd",           FT_BOOLEAN, 8, TFS(&tfs_sita_lost),             SITA_ERROR_RX_CD_LOST,                  "TRUE if Carrier Lost",                 HFILL }},
248                 { &hf_rxdpll,           { "DPLL",                       "sita.errors.rxdpll",           FT_BOOLEAN, 8, TFS(&tfs_sita_error),    SITA_ERROR_RX_DPLL,                             "TRUE if DPLL Error",                   HFILL }},
249                 { &hf_overrun,          { "Overrun",            "sita.errors.overrun",          FT_BOOLEAN, 8, TFS(&tfs_sita_error),    SITA_ERROR_RX_OVERRUN,                  "TRUE if Overrun Error",                HFILL }},
250                 { &hf_length,           { "Length",                     "sita.errors.length",           FT_BOOLEAN, 8, TFS(&tfs_sita_violation),SITA_ERROR_RX_FRAME_LEN_VIOL,   "TRUE if Length Violation",             HFILL }},
251                 { &hf_crc,                      { "CRC",                        "sita.errors.crc",                      FT_BOOLEAN, 8, TFS(&tfs_sita_error),    SITA_ERROR_RX_CRC,                              "TRUE if CRC Error",                    HFILL }},
252                 { &hf_break,            { "Break",                      "sita.errors.break",            FT_BOOLEAN, 8, TFS(&tfs_sita_received), SITA_ERROR_RX_BREAK,                    "TRUE if Break Received",               HFILL }},
253
254                 { &hf_underrun,         { "Underrun",           "sita.errors.underrun",         FT_BOOLEAN, 8, TFS(&tfs_sita_error),    SITA_ERROR_TX_UNDERRUN,                 "TRUE if Tx Underrun",                  HFILL }},
255                 { &hf_lostcts,          { "Clear To Send",      "sita.errors.lostcts",          FT_BOOLEAN, 8, TFS(&tfs_sita_lost),             SITA_ERROR_TX_CTS_LOST,                 "TRUE if Clear To Send Lost",   HFILL }},
256                 { &hf_uarterror,        { "UART",                       "sita.errors.uarterror",        FT_BOOLEAN, 8, TFS(&tfs_sita_error),    SITA_ERROR_TX_UART_ERROR,               "TRUE if UART Error",                   HFILL }},
257                 { &hf_rtxlimit,         { "Retx Limit",         "sita.errors.rtxlimit",         FT_BOOLEAN, 8, TFS(&tfs_sita_exceeded), SITA_ERROR_TX_RETX_LIMIT,       "TRUE if Retransmit Limit reached",     HFILL }},
258
259                 { &hf_dsr,                      { "DSR",                        "sita.signals.dsr",                     FT_BOOLEAN, 8, TFS(&tfs_sita_on_off),   SITA_SIG_DSR,                                   "TRUE if Data Set Ready",               HFILL }},
260                 { &hf_dtr,                      { "DTR",                        "sita.signals.dtr",                     FT_BOOLEAN, 8, TFS(&tfs_sita_on_off),   SITA_SIG_DTR,                                   "TRUE if Data Terminal Ready",  HFILL }},
261                 { &hf_cts,                      { "CTS",                        "sita.signals.cts",                     FT_BOOLEAN, 8, TFS(&tfs_sita_on_off),   SITA_SIG_CTS,                                   "TRUE if Clear To Send",                HFILL }},
262                 { &hf_rts,                      { "RTS",                        "sita.signals.rts",                     FT_BOOLEAN, 8, TFS(&tfs_sita_on_off),   SITA_SIG_RTS,                                   "TRUE if Request To Send",              HFILL }},
263                 { &hf_dcd,                      { "DCD",                        "sita.signals.dcd",                     FT_BOOLEAN, 8, TFS(&tfs_sita_on_off),   SITA_SIG_DCD,                                   "TRUE if Data Carrier Detect",  HFILL }},
264
265         };
266
267         static gint *ett[] = {
268                 &ett_sita,
269                 &ett_sita_flags,
270                 &ett_sita_signals,
271                 &ett_sita_errors1,
272                 &ett_sita_errors2,
273         };
274
275         proto_sita = proto_register_protocol("Societe Internationale de Telecommunications Aeronautiques", "SITA", "sita");     /* name, short name,abbreviation */
276         sita_dissector_table = register_dissector_table("sita.proto", "SITA protocol number", FT_UINT8, BASE_HEX);
277         proto_register_field_array(proto_sita, hf, array_length(hf));
278         proto_register_subtree_array(ett, array_length(ett));
279         register_dissector("sita", dissect_sita, proto_sita);
280 }
281
282 void
283 proto_reg_handoff_sita(void)
284 {
285         dissector_handle_t      lapb_handle;
286         dissector_handle_t      frame_relay_handle;
287         dissector_handle_t      uts_handle;
288         dissector_handle_t      ipars_handle;
289         dissector_handle_t      sita_handle;
290
291         lapb_handle             = find_dissector("lapb");
292         frame_relay_handle      = find_dissector("fr");
293         uts_handle              = find_dissector("uts");
294         ipars_handle            = find_dissector("ipars");
295         sita_handle             = find_dissector("sita");
296         data_handle             = find_dissector("data");
297
298         dissector_add_uint("sita.proto", SITA_PROTO_BOP_LAPB,   lapb_handle);
299         dissector_add_uint("sita.proto", SITA_PROTO_BOP_FRL,            frame_relay_handle);
300         dissector_add_uint("sita.proto", SITA_PROTO_UTS,                uts_handle);
301         dissector_add_uint("sita.proto", SITA_PROTO_ALC,                ipars_handle);
302         dissector_add_uint("wtap_encap", WTAP_ENCAP_SITA,               sita_handle);
303 }