aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-uaudp.c
blob: e4db699f91b047090d584d935daa281c5216936f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* packet-uaudp.c
* Routines for UA/UDP (Universal Alcatel UDP) packet dissection.
* Copyright 2011, Marek Tews <marek@trx.com.pl>
*
* $Id$
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* Copied from WHATEVER_FILE_YOU_USED (where "WHATEVER_FILE_YOU_USED"
* is a dissector file; if you just copied this from README.developer,
* don't bother with the "Copied from" - you don't even need to put
* in a "Copied from" if you copied an existing dissector, especially
* if the bulk of the code in the new dissector is your code)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <epan/packet.h>
#include <epan/prefs.h>

#include "packet-ua.h"

/*
* Here are the global variables associated with
* the various user definable characteristics of the dissection
*/
static gboolean use_heuristic_dissector = TRUE;
static range_t *global_uaudp_port_range;

/* Define the UAUDP proto */
static int proto_uaudp = -1;
static dissector_handle_t uaudp_handle;
static dissector_table_t uaudp_dissector_table;

/* Define many header fields for UAUDP */
static int hf_uaudp_opcode = -1;
static int hf_uaudp_expected = -1;
static int hf_uaudp_send = -1;

/*
* Define the trees for UAUDP
*/
static int ett_uaudp_header = -1;

/**
* Opcode
*/
static const value_string szUaOpcode[] =
{
    { 0, "Connect" },
    { 1, "Connect ACK" },
    { 2, "Release" },
    { 3, "Release ACK" },
    { 4, "Keepalive" },
    { 5, "Keepalive ACK" },
    { 6, "NACK" },
    { 7, "Data" },
    { 0, NULL }
};

/*
* dissect_uaudp - The dissector for the UA/UDP protocol
*/
static int dissect_uaudp(tvbuff_t *pTvb, packet_info *pInfo, proto_tree *pTree)
{
    gint nLen;
    guint8 u8Opcode;
    proto_item *pUAUDP, *pHeaderSubTree;

    /* PROTOCOL column */
    col_set_str(pInfo->cinfo, COL_PROTOCOL, "UAUDP");

    nLen = tvb_reported_length(pTvb);
    u8Opcode = tvb_get_guint8(pTvb, 0);

    /* INFO column */
    col_set_str(pInfo->cinfo, COL_INFO, val_to_str_const(u8Opcode, szUaOpcode, "Unknown"));

    /* opcode "UA/UDP Protocol, ..." */
    pUAUDP = proto_tree_add_item(pTree, proto_uaudp, pTvb, 0, -1, ENC_NA);
    proto_item_append_text(pUAUDP, ", %s (%d)", val_to_str_const(u8Opcode, szUaOpcode, "Unknown"), u8Opcode);

    pHeaderSubTree = proto_item_add_subtree(pUAUDP, ett_uaudp_header);
    proto_tree_add_item(pHeaderSubTree, hf_uaudp_opcode, pTvb, 0, 1, ENC_BIG_ENDIAN);

    switch(u8Opcode)
    {
    case 6:
        {
            /* Sequence Number (expected) */
            proto_tree_add_item(pHeaderSubTree, hf_uaudp_expected, pTvb, 1, 2, ENC_BIG_ENDIAN);
            break;
        }
    case 7:
        {
            int iOffs = 1;

            /* Sequence Number (expected) */
            proto_tree_add_item(pHeaderSubTree, hf_uaudp_expected, pTvb, iOffs, 2, ENC_BIG_ENDIAN);
            iOffs += 2;

            /* Sequence Number (sent) */
            proto_tree_add_item(pHeaderSubTree, hf_uaudp_send, pTvb, iOffs, 2, ENC_BIG_ENDIAN);
            iOffs += 2;

            /* Create the tvbuffer for the next dissector */
            if(nLen > iOffs)
            {
                if(dissector_try_uint(uaudp_dissector_table, 7, tvb_new_subset_remaining(pTvb, iOffs), pInfo, pTree))
                    iOffs = nLen;
                return iOffs;
            }
            else
            {
                col_append_str(pInfo->cinfo, COL_INFO, " ACK");
            }
            break;
        }
    }
    return nLen;
}

/*
 * UAUDP-over-UDP
 */
static gboolean 
dissect_uaudp_heur(tvbuff_t *pTvb, packet_info *pInfo, proto_tree *pTree)
{
    guint8 u8Opcode;

    if(!use_heuristic_dissector)
        return FALSE;

    /* The opcode must be in range */
    u8Opcode = tvb_get_guint8(pTvb, 0);
    if(u8Opcode > 7)
        return FALSE;

    /* The minimum length of a UAUDP message */
    switch(u8Opcode)
    {
    case 4:
    case 5:
        {
            if(tvb_reported_length(pTvb) != 1)
                return FALSE;
            break;
        }
    case 6:
        {
            if(tvb_reported_length(pTvb) != 3)
                return FALSE;
            break;
        }
    case 7:
        {
            guint nLen = tvb_reported_length(pTvb);
            if(nLen < 5)
                return FALSE;

            if(nLen > 5 && !is_ua(tvb_new_subset_remaining(pTvb, 5)))
                return FALSE;

            break;
        }
    /*
     * There I met with other opcodes 
     * and do not know how much data is transmitted.
     */
    default: return FALSE;
    }

    dissect_uaudp(pTvb, pInfo, pTree);
    return TRUE;
}

/* The registration hand-off routine is called at startup */
static void range_delete_callback(guint32 port)
{
    dissector_delete_uint("udp.port", port, uaudp_handle);
}

static void range_add_callback (guint32 port)
{
    dissector_add_uint("udp.port", port, uaudp_handle);
}

void proto_reg_handoff_uaudp(void)
{
    static range_t *uaudp_port_range;
    static gboolean uaudp_initialized = FALSE;

    if (!uaudp_initialized)
    {
        /*
         * For UAUDP-over-UDP.
         */
        heur_dissector_add("udp", dissect_uaudp_heur, proto_uaudp);

        uaudp_handle = find_dissector("uaudp");
        uaudp_initialized = TRUE;
    }
    else
    {
        range_foreach(uaudp_port_range, range_delete_callback);
        g_free(uaudp_port_range);
    }

    uaudp_port_range = range_copy(global_uaudp_port_range);
    range_foreach(uaudp_port_range, range_add_callback);
}

/* Register all the bits needed by the filtering engine */
void proto_register_uaudp(void)
{
    static hf_register_info hf[] =
    {
        { &hf_uaudp_opcode,
            { "Opcode", "uaudp.opcode",
                FT_UINT8, BASE_DEC, VALS(szUaOpcode), 0x0,
                "UA/UDP Opcode", HFILL }
        },
        { &hf_uaudp_expected,
            { "Sequence Number (expected)", "uaudp.expected",
                FT_UINT16, BASE_DEC, NULL, 0x0,
                NULL, HFILL }
        },
        { &hf_uaudp_send,
            { "Sequence Number (sent)", "uaudp.sent",
                FT_UINT16, BASE_DEC, NULL, 0x0,
                NULL, HFILL }
        }
    };
    static gint *ett[] =
    {
        &ett_uaudp_header,
    };

    module_t* uaudp_module;

    proto_uaudp = proto_register_protocol("Universal Alcatel UDP Protocol", "UAUDP", "uaudp");

    proto_register_field_array(proto_uaudp, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));

    new_register_dissector("uaudp", dissect_uaudp, proto_uaudp);

    /* Register our configuration options */
    uaudp_module = prefs_register_protocol(proto_uaudp, proto_reg_handoff_uaudp);
    prefs_register_bool_preference(uaudp_module, "use_heuristic_dissector",
        "Use heuristic dissector",
        "Use to decode a packet a heuristic dissector. "
        "Otherwise, they are decoded only those packets that will come from the specified ports.",
        &use_heuristic_dissector);
    prefs_register_range_preference(uaudp_module, "udp_ports",
        "UAUDP port numbers",
        "Port numbers used for UAUDP traffic (examples: 5001, 32512)",
        &global_uaudp_port_range, MAX_UDP_PORT);

    uaudp_dissector_table = register_dissector_table("uaudp.opcode", "UA/UDP Opcode", FT_UINT8, BASE_DEC);
}
/*
* Editor modelines - http://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* ex: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/