aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dissectors/packet-exablaze.c
blob: 6c20992b2473bead93da55eb25f5b824bb2d14d0 (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
/* packet-exablaze.c
 * Routines for dissection of Exablaze trailers
 * Copyright 2018 Exablaze
 *
 * 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 <math.h>

#include <epan/packet.h>

void proto_register_exablaze(void);
void proto_reg_handoff_exablaze(void);

static int proto_exablaze;

static int hf_exablaze_original_fcs;
static int hf_exablaze_device;
static int hf_exablaze_port;
static int hf_exablaze_timestamp;
static int hf_exablaze_timestamp_integer;
static int hf_exablaze_timestamp_fractional;

static gint ett_exablaze;
static gint ett_exablaze_timestamp;

static int
dissect_exablaze(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
        void *data _U_)
{
    proto_item *ti;
    proto_tree *exablaze_tree;
    proto_tree *timestamp_tree;

    guint trailer_length;
    guint fcs_length;
    guint offset;
    gboolean trailer_found;

    guint8 device;
    guint8 port;
    guint32 timestamp_sec;
    guint64 timestamp_frac;

    nstime_t timestamp;
    double timestamp_frac_double;
    struct tm *tm;

    trailer_length = tvb_reported_length(tvb);

    if (trailer_length != tvb_captured_length(tvb)) {
        /* The heuristics require the whole trailer to be captured */
        return 0;
    }

    /* Try matching with and without FCS */
    trailer_found = FALSE;
    for (fcs_length = 0; fcs_length <= 4; fcs_length += 4)
    {
        if (trailer_length < fcs_length + 16)
            continue;

        offset = trailer_length - fcs_length - 16;

        device = tvb_get_guint8(tvb, offset + 4);
        port = tvb_get_guint8(tvb, offset + 5);
        timestamp_sec = tvb_get_ntohl(tvb, offset + 6);
        timestamp_frac = tvb_get_ntoh40(tvb, offset + 10);

        /* If the capture time and timestamp differ by more than a week,
         * then this is probably not a valid Exablaze trailer */
        if (timestamp_sec > (guint)pinfo->abs_ts.secs) {
            if (timestamp_sec - pinfo->abs_ts.secs > 604800)
                continue;
        } else {
            if (pinfo->abs_ts.secs - timestamp_sec > 604800)
                continue;
        }

        trailer_found = TRUE;
        break;
    }

    if (!trailer_found)
        return 0;

    /* Fractional part is a 40 bit binary fraction of a second */
    timestamp.secs = timestamp_sec;
    timestamp_frac_double = ldexp((double)timestamp_frac, -40);
    timestamp.nsecs = (int)(timestamp_frac_double * 1000000000);

    ti = proto_tree_add_item(tree, proto_exablaze, tvb, offset, 16, ENC_NA);
    proto_item_append_text(ti, ", Device: %u, Port: %u, Timestamp: ",
            device, port);

    tm = localtime(&timestamp.secs);
    if (tm)
        proto_item_append_text(ti, "%02u:%02u:%02.12f",
                tm->tm_hour, tm->tm_min, tm->tm_sec + timestamp_frac_double);
    else
        proto_item_append_text(ti, "<Not representable>");

    exablaze_tree = proto_item_add_subtree(ti, ett_exablaze);
    proto_tree_add_item(exablaze_tree, hf_exablaze_original_fcs, tvb,
            offset, 4, ENC_BIG_ENDIAN);
    proto_tree_add_item(exablaze_tree, hf_exablaze_device, tvb,
            offset + 4, 1, ENC_BIG_ENDIAN);
    proto_tree_add_item(exablaze_tree, hf_exablaze_port, tvb,
            offset + 5, 1, ENC_BIG_ENDIAN);

    ti = proto_tree_add_time(exablaze_tree, hf_exablaze_timestamp, tvb,
            offset + 6, 9, &timestamp);
    timestamp_tree = proto_item_add_subtree(ti, ett_exablaze_timestamp);

    proto_tree_add_item(timestamp_tree, hf_exablaze_timestamp_integer, tvb,
            offset + 6, 4, ENC_BIG_ENDIAN);
    proto_tree_add_double_format_value(timestamp_tree,
            hf_exablaze_timestamp_fractional, tvb, offset + 10, 5,
            timestamp_frac_double, "%.12f", timestamp_frac_double);

    return offset + 16;
}

void
proto_register_exablaze(void)
{
    static hf_register_info hf[] = {
        {
            &hf_exablaze_original_fcs,
            {
                "Original FCS", "exablaze.original_fcs",
                FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL
            }
        },
        {
            &hf_exablaze_device,
            {
                "Device ID", "exablaze.device",
                FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL
            }
        },
        {
            &hf_exablaze_port,
            {
                "Port", "exablaze.port",
                FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL
            }
        },
        {
            &hf_exablaze_timestamp,
            {
                "Timestamp", "exablaze.timestamp",
                FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL, NULL, 0x0, NULL, HFILL
            }
        },
        {   &hf_exablaze_timestamp_integer,
            {
                "Seconds since epoch", "exablaze.timestamp.seconds",
                FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL
            }
        },
        {   &hf_exablaze_timestamp_fractional,
            {
                "Fractional seconds",
                "exablaze.timestamp.fractional_seconds",
                FT_DOUBLE, BASE_NONE, NULL, 0x0, NULL, HFILL
            }
        }
    };

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

    proto_exablaze = proto_register_protocol("Exablaze trailer", "Exablaze",
            "exablaze");
    proto_register_field_array(proto_exablaze, hf, array_length(hf));
    proto_register_subtree_array(ett, array_length(ett));
}

void
proto_reg_handoff_exablaze(void)
{
    heur_dissector_add("eth.trailer", dissect_exablaze, "Exablaze trailer",
            "exablaze_eth", proto_exablaze, HEURISTIC_DISABLE);
}

/*
 * 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:
 */