summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-srp.c
blob: 557c9dfb935aba31246f4a8343c4e2e8ca2c5e81 (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
/* packet-srp.c
 * Routines for H.324/SRP dissection
 * 2004 Richard van der Hoff <richardv@mxtelecom.com>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <epan/packet.h>
#include <epan/crc16-tvb.h>

void proto_register_srp(void);
void proto_register_ccsrl(void);
void proto_reg_handoff_srp(void);

/* Wireshark ID of the protocols */
static int proto_srp = -1;
static int proto_ccsrl = -1;

/* The following hf_* variables are used to hold the Wireshark IDs of
 * our header fields; they are filled out when we call
 * proto_register_field_array() in proto_register_srp()
 */
static int hf_srp_header = -1;
static int hf_srp_seqno = -1;
static int hf_srp_crc = -1;
static int hf_srp_crc_bad = -1;
static int hf_ccsrl_ls = -1;

/* These are the ids of the subtrees that we may be creating */
static gint ett_srp = -1;
static gint ett_ccsrl = -1;

static dissector_handle_t ccsrl_handle;
static dissector_handle_t h245dg_handle;

/*****************************************************************************/
#define SRP_SRP_COMMAND 249
#define SRP_SRP_RESPONSE 251
#define SRP_NSRP_RESPONSE 247

/* WNSRP definitions */
#define WNSRP_COMMAND_HEADER 241
#define WNSRP_RESPONSE_HEADER 243

static const value_string srp_frame_types[] = {
    {SRP_SRP_COMMAND, "SRP command"},
    {SRP_SRP_RESPONSE, "SRP response"},
    {SRP_NSRP_RESPONSE, "NSRP response"},
    {WNSRP_COMMAND_HEADER, "WNSRP command"},
    {WNSRP_RESPONSE_HEADER, "WNSRP response"},
    {0,NULL}
};

static const value_string ccsrl_ls_vals[] = {
    {0xFF, "Yes"},
    {0x00, "No"},
    {0,NULL}
};

/*****************************************************************************/

static int dissect_ccsrl(tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree, void* data _U_)
{
    proto_item *ccsrl_item;
    proto_tree *ccsrl_tree=NULL;
    guint8 lastseg = tvb_get_guint8(tvb,0);
    tvbuff_t *next_tvb;

    /* add the 'ccsrl' tree to the main tree */
    if (tree) {
        ccsrl_item = proto_tree_add_item (tree, proto_ccsrl, tvb, 0, -1, ENC_NA);
        ccsrl_tree = proto_item_add_subtree (ccsrl_item, ett_ccsrl);
        proto_tree_add_uint(ccsrl_tree,hf_ccsrl_ls,tvb,0,1,lastseg);
    }

    /* XXX add support for reassembly of fragments */

    /* XXX currently, we always dissect as H245. It's not necessarily
        that though.
    */
    next_tvb = tvb_new_subset_remaining(tvb, 1);
    call_dissector( h245dg_handle, next_tvb, pinfo, ccsrl_tree );
    return tvb_captured_length(tvb);
}

static void dissect_srp_command(tvbuff_t * tvb, packet_info * pinfo, proto_tree * srp_tree)
{
    tvbuff_t *next_tvb;
    guint payload_len;

    if( srp_tree )
        proto_tree_add_item(srp_tree,hf_srp_seqno,tvb,1,1,ENC_BIG_ENDIAN);

    payload_len = tvb_reported_length_remaining(tvb,4);
    next_tvb = tvb_new_subset_length(tvb, 2, payload_len);

    /* XXX currently, we always dissect as CCSRL. It's only that in
     * H324/Annex C though.
     */
    call_dissector(ccsrl_handle, next_tvb, pinfo, srp_tree );
}

static int dissect_srp (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree, void* data _U_)
{
    proto_item *srp_item = NULL;
    proto_tree *srp_tree = NULL;
    proto_item *hidden_item;

    guint8 header = tvb_get_guint8(tvb,0);

    /* add the 'srp' tree to the main tree */
    if (tree) {
        srp_item = proto_tree_add_item (tree, proto_srp, tvb, 0, -1, ENC_NA);
        srp_tree = proto_item_add_subtree (srp_item, ett_srp);
        proto_tree_add_uint(srp_tree,hf_srp_header,tvb,0,1,header);
    }

    switch( header ) {
        case SRP_SRP_COMMAND:
        case WNSRP_COMMAND_HEADER:
            dissect_srp_command(tvb,pinfo,srp_tree);
            break;

        case SRP_SRP_RESPONSE:
            break;

        case SRP_NSRP_RESPONSE:
        case WNSRP_RESPONSE_HEADER:
            if( srp_tree )
                proto_tree_add_item(srp_tree,hf_srp_seqno,tvb,1,1,ENC_BIG_ENDIAN);
            break;

        default:
            break;
    }

    if( srp_tree ) {
        guint16 crc, calc_crc;
        guint crc_offset = tvb_reported_length(tvb)-2;
        crc = tvb_get_letohs(tvb,-2);

        /* crc includes the header */
        calc_crc = crc16_ccitt_tvb(tvb,crc_offset);

        if( crc == calc_crc ) {
            proto_tree_add_uint_format_value(srp_tree, hf_srp_crc, tvb,
                                       crc_offset, 2, crc,
                                       "0x%04x (correct)", crc);
        } else {
            hidden_item = proto_tree_add_boolean(srp_tree, hf_srp_crc_bad, tvb,
                                          crc_offset, 2, TRUE);
            proto_item_set_hidden(hidden_item);
            proto_tree_add_uint_format_value(srp_tree, hf_srp_crc, tvb,
                                       crc_offset, 2, crc,
                                       "0x%04x (incorrect, should be 0x%04x)",
                                       crc,
                                       calc_crc);
        }
    }

    return tvb_captured_length(tvb);
}

void proto_register_ccsrl (void)
{
    static hf_register_info hf[] = {
        { &hf_ccsrl_ls,
          { "Last Segment","ccsrl.ls",FT_UINT8, BASE_HEX, VALS(ccsrl_ls_vals), 0x0,
            "Last segment indicator", HFILL}},
    };

    static gint *ett[] = {
        &ett_ccsrl,
    };

    proto_ccsrl = proto_register_protocol ("H.324/CCSRL", "CCSRL", "ccsrl");
    proto_register_field_array (proto_ccsrl, hf, array_length (hf));
    proto_register_subtree_array (ett, array_length (ett));
    register_dissector("ccsrl", dissect_ccsrl, proto_ccsrl);
}

void proto_register_srp (void)
{
    static hf_register_info hf[] = {
        {&hf_srp_header,
         { "Header", "srp.header", FT_UINT8, BASE_DEC, VALS(srp_frame_types), 0x0,
           "SRP header octet", HFILL }},
        {&hf_srp_seqno,
         { "Sequence Number", "srp.seqno", FT_UINT8, BASE_DEC, NULL, 0x0,
           NULL, HFILL }},
        {&hf_srp_crc,
         { "CRC", "srp.crc", FT_UINT16, BASE_HEX, NULL, 0x0,
           NULL, HFILL }},
        { &hf_srp_crc_bad,
          { "Bad CRC","srp.crc_bad", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
            NULL, HFILL }},
    };

    static gint *ett[] = {
        &ett_srp,
    };

    proto_srp = proto_register_protocol ("H.324/SRP", "SRP", "srp");
    proto_register_field_array (proto_srp, hf, array_length (hf));
    proto_register_subtree_array (ett, array_length (ett));
    register_dissector("srp", dissect_srp, proto_srp);

    /* register our init routine to be called at the start of a capture,
       to clear out our hash tables etc */
    /* register_init_routine(&srp_init_protocol); */

}


void proto_reg_handoff_srp(void) {
    ccsrl_handle = find_dissector_add_dependency("ccsrl", proto_srp);
    h245dg_handle = find_dissector_add_dependency("h245dg", proto_srp);
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */