/src/ndpi/src/lib/protocols/cassandra.c
Line | Count | Source |
1 | | /* |
2 | | * cassandra.c |
3 | | * |
4 | | * Apache Cassandra CQL Binary protocol |
5 | | * |
6 | | * Copyright (C) 2023 - ntop.org |
7 | | * Copyright (C) 2023 - 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_CASSANDRA |
30 | | |
31 | | #include "ndpi_api.h" |
32 | | #include "ndpi_private.h" |
33 | | |
34 | | static inline int ndpi_validate_cassandra_response(u_int8_t response) |
35 | 2.15M | { |
36 | 2.15M | return (response >= 0x81 && response <= 0x85) ? 1 : -1; |
37 | 2.15M | } |
38 | | |
39 | | static inline int ndpi_validate_cassandra_request(u_int8_t request) |
40 | 2.15M | { |
41 | 2.15M | return (request >= 0x01 && request <= 0x05) ? 1 : -1; |
42 | 2.15M | } |
43 | | |
44 | | static void ndpi_int_cassandra_add_connection(struct ndpi_detection_module_struct *ndpi_struct, |
45 | | struct ndpi_flow_struct *flow) |
46 | 188 | { |
47 | 188 | NDPI_LOG_INFO(ndpi_struct, "found Cassandra CQL\n"); |
48 | | |
49 | 188 | ndpi_set_detected_protocol(ndpi_struct, flow, |
50 | 188 | NDPI_PROTOCOL_CASSANDRA, NDPI_PROTOCOL_UNKNOWN, |
51 | 188 | NDPI_CONFIDENCE_DPI); |
52 | 188 | } |
53 | | |
54 | | static void ndpi_search_cassandra(struct ndpi_detection_module_struct *ndpi_struct, |
55 | | struct ndpi_flow_struct *flow) |
56 | 2.87M | { |
57 | 2.87M | struct ndpi_packet_struct *packet = &ndpi_struct->packet; |
58 | | |
59 | 2.87M | NDPI_LOG_DBG(ndpi_struct, "search Cassandra CQL\n"); |
60 | | |
61 | 2.87M | if (packet->payload_packet_len == 19 && |
62 | 2.87M | ntohl(get_u_int32_t(packet->payload, 0)) == 0xCA552DFA) |
63 | 53 | { |
64 | 53 | NDPI_LOG_INFO(ndpi_struct, "found Cassandra Internode Communication\n"); |
65 | 53 | ndpi_int_cassandra_add_connection(ndpi_struct, flow); |
66 | 53 | return; |
67 | 53 | } |
68 | | |
69 | 2.87M | if ((packet->payload_packet_len < 9) || |
70 | 2.15M | (flow->packet_counter >= 8) || |
71 | 2.15M | (!ndpi_validate_cassandra_response(packet->payload[0]) || |
72 | 2.15M | !ndpi_validate_cassandra_request(packet->payload[0]))) |
73 | 725k | { |
74 | 725k | NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
75 | 725k | return; |
76 | 725k | } |
77 | | |
78 | 2.15M | if (flow->packet_direction_counter[packet->packet_direction] > 2) { |
79 | 142k | NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
80 | 142k | return; |
81 | 142k | } |
82 | | |
83 | | /* Looking for a 'STARTUP' message from the client, |
84 | | * which should always contain the CQL_VERSION string |
85 | | */ |
86 | 2.01M | if (packet->payload_packet_len > 60 && |
87 | 1.28M | memcmp(&packet->payload[packet->payload_packet_len-20], "CQL_VERSION", 11) == 0) |
88 | 135 | { |
89 | 135 | NDPI_LOG_INFO(ndpi_struct, "found Cassandra CQL\n"); |
90 | 135 | ndpi_int_cassandra_add_connection(ndpi_struct, flow); |
91 | 135 | return; |
92 | 135 | } |
93 | 2.01M | } |
94 | | |
95 | 16.3k | void init_cassandra_dissector(struct ndpi_detection_module_struct *ndpi_struct) { |
96 | 16.3k | register_dissector("Cassandra", ndpi_struct, |
97 | 16.3k | ndpi_search_cassandra, |
98 | 16.3k | NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION, |
99 | 16.3k | 1, NDPI_PROTOCOL_CASSANDRA); |
100 | 16.3k | } |