/src/ndpi/src/lib/protocols/tplink_shp.c
Line | Count | Source |
1 | | /* |
2 | | * tplink_shp.c |
3 | | * |
4 | | * TP-LINK Smart Home Protocol |
5 | | * |
6 | | * Copyright (C) 2022-23 - ntop.org |
7 | | * |
8 | | * nDPI is free software: you can redistribute it and/or modify |
9 | | * it under the terms of the GNU Lesser General Public License as published by |
10 | | * the Free Software Foundation, either version 3 of the License, or |
11 | | * (at your option) any later version. |
12 | | * |
13 | | * nDPI is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU Lesser General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public License |
19 | | * along with nDPI. If not, see <http://www.gnu.org/licenses/>. |
20 | | * |
21 | | */ |
22 | | |
23 | | |
24 | | #include "ndpi_protocol_ids.h" |
25 | | |
26 | | #define NDPI_CURRENT_PROTO NDPI_PROTOCOL_TPLINK_SHP |
27 | | |
28 | | #include "ndpi_api.h" |
29 | | #include "ndpi_private.h" |
30 | | |
31 | 9.46M | #define _TPLSHP_MIN_LEN 2 |
32 | 2.24M | #define _TPLSHP_TCP_LEN_HDR 4 |
33 | | |
34 | | static void ndpi_int_tplink_shp_add_connection(struct ndpi_detection_module_struct * const ndpi_struct, |
35 | | struct ndpi_flow_struct * const flow) |
36 | 601 | { |
37 | 601 | NDPI_LOG_INFO(ndpi_struct, "found TPLINK SHP\n"); |
38 | | |
39 | 601 | ndpi_set_detected_protocol(ndpi_struct, flow, |
40 | 601 | NDPI_PROTOCOL_TPLINK_SHP, |
41 | 601 | NDPI_PROTOCOL_UNKNOWN, |
42 | 601 | NDPI_CONFIDENCE_DPI); |
43 | 601 | } |
44 | | |
45 | | /* ***************************************************** */ |
46 | | |
47 | | static void ndpi_search_tplink_shp(struct ndpi_detection_module_struct *ndpi_struct, |
48 | | struct ndpi_flow_struct *flow) |
49 | 2.79M | { |
50 | 2.79M | struct ndpi_packet_struct const * const packet = &ndpi_struct->packet; |
51 | | |
52 | 2.79M | NDPI_LOG_DBG(ndpi_struct, "search TPLINK SHP\n"); |
53 | | |
54 | 2.79M | u_int16_t offset = 0; |
55 | | /* Skip length header (TCP payloads only) */ |
56 | 2.79M | if(packet->tcp != NULL) offset = _TPLSHP_TCP_LEN_HDR; |
57 | | |
58 | 2.79M | if (packet->payload_packet_len - offset < _TPLSHP_MIN_LEN) |
59 | 567k | { |
60 | 567k | NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
61 | 567k | return; |
62 | 567k | } |
63 | | |
64 | 2.22M | u_int16_t i; |
65 | 2.22M | u_int8_t k = 171, b[_TPLSHP_MIN_LEN]; |
66 | | |
67 | 6.67M | for (i = 0 ; i < _TPLSHP_MIN_LEN; i++) |
68 | 4.44M | { |
69 | 4.44M | b[i] = packet->payload[i + offset] ^ k; |
70 | 4.44M | k = packet->payload[i + offset]; |
71 | 4.44M | } |
72 | | |
73 | 2.22M | if (b[0] != '{' || (b[1] != '}' && b[1] != '"')) |
74 | 2.22M | { |
75 | 2.22M | NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
76 | 2.22M | return; |
77 | 2.22M | } |
78 | | |
79 | 601 | ndpi_int_tplink_shp_add_connection(ndpi_struct, flow); |
80 | 601 | } |
81 | | |
82 | | /* ***************************************************** */ |
83 | | |
84 | | void init_tplink_shp_dissector(struct ndpi_detection_module_struct *ndpi_struct) |
85 | 6.74k | { |
86 | 6.74k | register_dissector("TPLINK SHP", ndpi_struct, |
87 | 6.74k | ndpi_search_tplink_shp, |
88 | 6.74k | NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_OR_UDP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION, |
89 | 6.74k | 1, NDPI_PROTOCOL_TPLINK_SHP); |
90 | 6.74k | } |