summaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-h1.c
blob: 430e6385fe2e97dc60e8e47912216f856538ff37 (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
/* packet-h1.c
 * Routines for Sinec H1 packet disassembly
 * Gerrit Gehnen <G.Gehnen@atrie.de>
 *
 * 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/exceptions.h>

void proto_register_h1(void);
void proto_reg_handoff_h1(void);

static int proto_h1 = -1;
static int hf_h1_header = -1;
static int hf_h1_len = -1;
static int hf_h1_block_type = -1;
static int hf_h1_block_len = -1;
static int hf_h1_opcode = -1;
static int hf_h1_dbnr = -1;
static int hf_h1_dwnr = -1;
static int hf_h1_dlen = -1;
static int hf_h1_org = -1;
static int hf_h1_response_value = -1;


#define EMPTY_BLOCK     0xFF
#define OPCODE_BLOCK    0x01
#define REQUEST_BLOCK   0x03
#define RESPONSE_BLOCK  0x0F

static const value_string block_type_vals[] = {
    { EMPTY_BLOCK,    "Empty Block" },
    { OPCODE_BLOCK,   "Opcode Block" },
    { REQUEST_BLOCK,  "Request Block" },
    { RESPONSE_BLOCK, "Response Block" },
    {0, NULL}
};


static const value_string opcode_vals[] = {
    {3, "Write Request"},
    {4, "Write Response"},
    {5, "Read Request"},
    {6, "Read Response"},
    {0, NULL}
};

static const value_string org_vals[] = {
    {0x01, "DB"},
    {0x02, "MB"},
    {0x03, "EB"},
    {0x04, "AB"},
    {0x05, "PB"},
    {0x06, "ZB"},
    {0x07, "TB"},
    {0x08, "BS"},
    {0x09, "AS"},
    {0x0a, "DX"},
    {0x10, "DE"},
    {0x11, "QB"},
    {0, NULL}
};

static const value_string returncode_vals[] = {
    {0x00, "No error"},
    {0x02, "Requested block does not exist"},
    {0x03, "Requested block too small"},
    {0xFF, "Error, reason unknown"},
    {0, NULL}
};

static gint ett_h1 = -1;
static gint ett_block = -1;

static gboolean dissect_h1(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
{
    proto_tree *h1_tree, *block_tree;
    proto_item *h1_ti, *block_ti;
    gint offset = 0, offset_block_start;
    guint8 h1_len;
    guint8 block_type, block_len;
    tvbuff_t *next_tvb;

    if (tvb_captured_length(tvb) < 2) {
        /* Not enough data captured to hold the "S5" header; don't try
           to interpret it as H1. */
        return FALSE;
    }

    if (!(tvb_get_guint8(tvb, 0) == 'S' && tvb_get_guint8(tvb, 1) == '5')) {
        return FALSE;
    }

    col_set_str (pinfo->cinfo, COL_PROTOCOL, "H1");
    col_set_str(pinfo->cinfo, COL_INFO, "S5: ");

    h1_ti = proto_tree_add_item(tree, proto_h1, tvb, offset, -1, ENC_NA);
    h1_tree = proto_item_add_subtree(h1_ti, ett_h1);

    proto_tree_add_item(h1_tree, hf_h1_header, tvb, offset, 2, ENC_BIG_ENDIAN);
    offset += 2;

    h1_len = tvb_get_guint8(tvb, offset);
    proto_tree_add_item(h1_tree, hf_h1_len, tvb, offset, 1, ENC_BIG_ENDIAN);
    proto_item_set_len(h1_ti, h1_len);
    offset++;

    while (offset < h1_len) {
        offset_block_start = offset;

        block_type = tvb_get_guint8(tvb, offset);
        block_len = tvb_get_guint8(tvb, offset+1);

        if (!try_val_to_str(block_type, block_type_vals)) {
            /* XXX - should we skip unknown blocks? */
            return FALSE;
        }
        if (block_len == 0) {
            /* XXX - expert info */
            break;
        }

        block_tree = proto_tree_add_subtree_format(h1_tree,
                tvb, offset, -1, ett_block, &block_ti, "%s",
                val_to_str_const(block_type, block_type_vals, "Unknown block"));

        proto_tree_add_item(block_tree, hf_h1_block_type,
                tvb, offset, 1, ENC_BIG_ENDIAN);
        /* we keep increasing offset as we go though the block
           however, to find the beginning of the next block,
           we use the current block's start offset and its length field */
        offset++;
        proto_tree_add_item(block_tree, hf_h1_block_len,
                tvb, offset, 1, ENC_BIG_ENDIAN);
        proto_item_set_len(block_ti, block_len);
        offset++;

        switch (block_type) {
            case OPCODE_BLOCK:
                proto_tree_add_item(block_tree, hf_h1_opcode,
                        tvb, offset, 1, ENC_BIG_ENDIAN);
                col_append_str (pinfo->cinfo, COL_INFO,
                        val_to_str (tvb_get_guint8(tvb,  offset),
                        opcode_vals, "Unknown Opcode (0x%2.2x)"));
                break;

            case REQUEST_BLOCK:
                proto_tree_add_item(block_tree, hf_h1_org, tvb,
                        offset, 1, ENC_BIG_ENDIAN);
                col_append_fstr(pinfo->cinfo, COL_INFO, " %s",
                        val_to_str (tvb_get_guint8(tvb,  offset),
                            org_vals,"Unknown Type (0x%2.2x)"));
                offset++;

                proto_tree_add_item(block_tree, hf_h1_dbnr, tvb,
                        offset, 1, ENC_BIG_ENDIAN);
                col_append_fstr(pinfo->cinfo, COL_INFO, " %d",
                        tvb_get_guint8(tvb,  offset));
                offset++;

                proto_tree_add_item(block_tree, hf_h1_dwnr, tvb,
                        offset, 2, ENC_BIG_ENDIAN);
                col_append_fstr (pinfo->cinfo, COL_INFO, " DW %d",
                        tvb_get_ntohs(tvb, offset));
                offset += 2;

                proto_tree_add_item(block_tree, hf_h1_dlen, tvb,
                        offset, 2, ENC_BIG_ENDIAN);
                col_append_fstr (pinfo->cinfo, COL_INFO, " Count %d",
                        tvb_get_ntohs(tvb, offset));
                break;

            case RESPONSE_BLOCK:
                proto_tree_add_item(block_tree, hf_h1_response_value,
                        tvb, offset, 1, ENC_BIG_ENDIAN);
                col_append_fstr (pinfo->cinfo, COL_INFO, " %s",
                        val_to_str (tvb_get_guint8(tvb,  offset),
                            returncode_vals,"Unknown Returncode (0x%2.2x"));
                break;
        }

        offset = offset_block_start + block_len; /* see the comment above */
    }

    if (tvb_reported_length_remaining(tvb, offset) > 0) {
        next_tvb = tvb_new_subset_remaining(tvb,  offset);
        call_data_dissector(next_tvb, pinfo, tree);
    }

    return TRUE;
}


void
proto_register_h1 (void)
{
    static hf_register_info hf[] = {
        {&hf_h1_header,
            {"H1-Header", "h1.header", FT_UINT16, BASE_HEX, NULL, 0x0,
                NULL, HFILL }},
        {&hf_h1_len,
            {"Length indicator", "h1.len", FT_UINT16, BASE_DEC, NULL, 0x0,
                NULL, HFILL }},
        {&hf_h1_block_type,
            {"Block type", "h1.block_type", FT_UINT8, BASE_HEX, VALS(block_type_vals), 0x0,
                NULL, HFILL }},
        {&hf_h1_block_len,
            {"Block length", "h1.block_len", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
        {&hf_h1_opcode,
            {"Opcode", "h1.opcode", FT_UINT8, BASE_HEX, VALS (opcode_vals), 0x0,
                NULL, HFILL }},
        {&hf_h1_org,
            {"Memory type", "h1.org", FT_UINT8, BASE_HEX, VALS (org_vals), 0x0,
                NULL, HFILL }},
        {&hf_h1_dbnr,
            {"Memory block number", "h1.dbnr", FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
        {&hf_h1_dwnr,
            {"Address within memory block", "h1.dwnr", FT_UINT16, BASE_DEC, NULL, 0x0,
                NULL, HFILL }},
        {&hf_h1_dlen,
            {"Length in words", "h1.dlen", FT_INT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
        {&hf_h1_response_value,
            {"Response value", "h1.resvalue", FT_UINT8, BASE_DEC,
                VALS (returncode_vals), 0x0, NULL, HFILL }}
    };

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

    proto_h1 = proto_register_protocol ("Sinec H1 Protocol", "H1", "h1");
    proto_register_field_array (proto_h1, hf, array_length (hf));
    proto_register_subtree_array (ett, array_length (ett));
}

void
proto_reg_handoff_h1(void)
{
    heur_dissector_add("cotp", dissect_h1,
            "Sinec H1 over COTP", "hi_cotp", proto_h1, HEURISTIC_ENABLE);
    heur_dissector_add("cotp_is", dissect_h1,
            "Sinec H1 over COTP (inactive subset)", "hi_cotp_is", proto_h1, HEURISTIC_ENABLE);
    heur_dissector_add("tcp", dissect_h1,
            "Sinec H1 over TCP", "hi_tcp", proto_h1, HEURISTIC_ENABLE);
}

/*
 * Editor modelines  -  https://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:
 */