/src/ndpi/src/lib/protocols/trdp.c
Line | Count | Source |
1 | | /* |
2 | | * trdp.c |
3 | | * |
4 | | * Train Real Time Data Protocol (IEC61375-2-3) |
5 | | * |
6 | | * Copyright (C) 2024 - ntop.org |
7 | | * Copyright (C) 2024 - V.G <v.gavrilov@securitycode.ru> |
8 | | * |
9 | | * This file is part of nDPI, an open source deep packet inspection |
10 | | * library based on the OpenDPI and PACE technology by ipoque GmbH |
11 | | * |
12 | | * nDPI is free software: you can redistribute it and/or modify |
13 | | * it under the terms of the GNU Lesser General Public License as published by |
14 | | * the Free Software Foundation, either version 3 of the License, or |
15 | | * (at your option) any later version. |
16 | | * |
17 | | * nDPI is distributed in the hope that it will be useful, |
18 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 | | * GNU Lesser General Public License for more details. |
21 | | * |
22 | | * You should have received a copy of the GNU Lesser General Public License |
23 | | * along with nDPI. If not, see <http://www.gnu.org/licenses/>. |
24 | | * |
25 | | */ |
26 | | |
27 | | #include "ndpi_protocol_ids.h" |
28 | | |
29 | | #define NDPI_CURRENT_PROTO NDPI_PROTOCOL_TRDP |
30 | | |
31 | | #include "ndpi_api.h" |
32 | | #include "ndpi_private.h" |
33 | | |
34 | 2.80M | #define TRDP_MD_HDR_LEN 116 |
35 | 2.66M | #define TRDP_PD_HDR_LEN 40 |
36 | | |
37 | | static void ndpi_int_trdp_add_connection(struct ndpi_detection_module_struct *ndpi_struct, |
38 | | struct ndpi_flow_struct *flow) |
39 | 213 | { |
40 | 213 | NDPI_LOG_INFO(ndpi_struct, "found TRDP\n"); |
41 | 213 | ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_TRDP, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI); |
42 | 213 | } |
43 | | |
44 | | static void ndpi_search_trdp(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) |
45 | 2.66M | { |
46 | 2.66M | struct ndpi_packet_struct const * const packet = &ndpi_struct->packet; |
47 | | |
48 | 2.66M | NDPI_LOG_DBG(ndpi_struct, "search TRDP\n"); |
49 | | |
50 | 2.66M | if (packet->payload_packet_len >= TRDP_PD_HDR_LEN) |
51 | 1.40M | { |
52 | 1.40M | u_int32_t header_fcs = 0; |
53 | 1.40M | u_int32_t dataset_len = 0; |
54 | | |
55 | 1.40M | if (!packet->tcp && packet->payload[6] == 'P') { /* Process Data */ |
56 | 1.04k | dataset_len = ntohl(get_u_int32_t(packet->payload, 20)); |
57 | 1.04k | if ((u_int32_t)(packet->payload_packet_len-TRDP_PD_HDR_LEN) == dataset_len && |
58 | 205 | get_u_int32_t(packet->payload, 24) == 0) /* Reserved, must be zero */ |
59 | 141 | { |
60 | 141 | header_fcs = ndpi_crc32(packet->payload, TRDP_PD_HDR_LEN-4, 0); |
61 | 141 | if (header_fcs == le32toh(get_u_int32_t(packet->payload, TRDP_PD_HDR_LEN-4))) { |
62 | 69 | ndpi_int_trdp_add_connection(ndpi_struct, flow); |
63 | 69 | return; |
64 | 69 | } |
65 | 141 | } |
66 | 1.04k | } |
67 | | |
68 | 1.40M | if (packet->payload_packet_len >= TRDP_MD_HDR_LEN && packet->payload[6] == 'M') { /* Message Data */ |
69 | 1.99k | dataset_len = ntohl(get_u_int32_t(packet->payload, 20)); |
70 | 1.99k | u_int32_t padding = (4 - (dataset_len % 4)) % 4; |
71 | | |
72 | 1.99k | if ((u_int32_t)(packet->payload_packet_len - TRDP_MD_HDR_LEN - padding) == dataset_len) |
73 | 290 | { |
74 | 290 | header_fcs = ndpi_crc32(packet->payload, TRDP_MD_HDR_LEN-4, 0); |
75 | 290 | if (header_fcs == le32toh(get_u_int32_t(packet->payload, TRDP_MD_HDR_LEN-4))) { |
76 | 144 | ndpi_int_trdp_add_connection(ndpi_struct, flow); |
77 | 144 | return; |
78 | 144 | } |
79 | 290 | } |
80 | 1.99k | } |
81 | 1.40M | } |
82 | | |
83 | 2.66M | NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
84 | 2.66M | } |
85 | | |
86 | | void init_trdp_dissector(struct ndpi_detection_module_struct *ndpi_struct) |
87 | 13.1k | { |
88 | 13.1k | register_dissector("TRDP", ndpi_struct, |
89 | 13.1k | ndpi_search_trdp, |
90 | 13.1k | NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_OR_UDP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION, |
91 | 13.1k | 1, NDPI_PROTOCOL_TRDP); |
92 | 13.1k | } |