/src/ndpi/src/lib/protocols/ookla.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * ookla.c |
3 | | * |
4 | | * Copyright (C) 2018-22 - ntop.org |
5 | | * |
6 | | * nDPI is free software: you can redistribute it and/or modify |
7 | | * it under the terms of the GNU Lesser General Public License as published by |
8 | | * the Free Software Foundation, either version 3 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * nDPI is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public License |
17 | | * along with nDPI. If not, see <http://www.gnu.org/licenses/>. |
18 | | * |
19 | | */ |
20 | | #include "ndpi_protocol_ids.h" |
21 | | |
22 | | #define NDPI_CURRENT_PROTO NDPI_PROTOCOL_OOKLA |
23 | | |
24 | | #include "ndpi_api.h" |
25 | | #include "ndpi_private.h" |
26 | | |
27 | | /* #define DEBUG_OOKLA_LRU */ |
28 | | |
29 | | const u_int16_t ookla_port = 8080; |
30 | | |
31 | | /* ************************************************************* */ |
32 | | |
33 | | static u_int64_t get_ookla_key(struct ndpi_flow_struct *flow) |
34 | 0 | { |
35 | 0 | if(flow->is_ipv6) |
36 | 0 | return ndpi_quick_hash64((const char *)flow->c_address.v6, 16); |
37 | 0 | else |
38 | 0 | return flow->c_address.v4; |
39 | 0 | } |
40 | | |
41 | | /* ************************************************************* */ |
42 | | |
43 | | int ookla_search_into_cache(struct ndpi_detection_module_struct *ndpi_struct, |
44 | | struct ndpi_flow_struct *flow) |
45 | 0 | { |
46 | 0 | u_int64_t key; |
47 | 0 | u_int16_t dummy; |
48 | |
|
49 | 0 | if(ndpi_struct->ookla_cache) { |
50 | 0 | key = get_ookla_key(flow); |
51 | |
|
52 | 0 | if(ndpi_lru_find_cache(ndpi_struct->ookla_cache, key, |
53 | 0 | &dummy, 0 /* Don't remove it as it can be used for other connections */, |
54 | 0 | ndpi_get_current_time(flow))) { |
55 | | #ifdef DEBUG_OOKLA_LRU |
56 | | printf("[LRU OOKLA] Found %lu [%u <-> %u]\n", key, ntohs(flow->c_port), ntohs(flow->s_port)); |
57 | | #endif |
58 | 0 | return 1; |
59 | 0 | } else { |
60 | | #ifdef DEBUG_OOKLA_LRU |
61 | | printf("[LRU OOKLA] Not found %lu [%u <-> %u]\n", key, ntohs(flow->c_port), ntohs(flow->s_port)); |
62 | | #endif |
63 | 0 | } |
64 | 0 | } |
65 | | |
66 | 0 | return 0; |
67 | 0 | } |
68 | | |
69 | | /* ************************************************************* */ |
70 | | |
71 | | void ookla_add_to_cache(struct ndpi_detection_module_struct *ndpi_struct, |
72 | | struct ndpi_flow_struct *flow) |
73 | 0 | { |
74 | 0 | u_int64_t key; |
75 | |
|
76 | 0 | if(ndpi_struct->ookla_cache) { |
77 | 0 | key = get_ookla_key(flow); |
78 | | #ifdef DEBUG_OOKLA_LRU |
79 | | printf("[LRU OOKLA] ADDING %lu [%u <-> %u]\n", key, ntohs(flow->c_port), ntohs(flow->s_port)); |
80 | | #endif |
81 | 0 | ndpi_lru_add_to_cache(ndpi_struct->ookla_cache, key, 1 /* dummy */, |
82 | 0 | ndpi_get_current_time(flow)); |
83 | 0 | } |
84 | |
|
85 | 0 | } |
86 | | |
87 | | /* ************************************************************* */ |
88 | | |
89 | 0 | void ndpi_search_ookla(struct ndpi_detection_module_struct* ndpi_struct, struct ndpi_flow_struct* flow) { |
90 | 0 | struct ndpi_packet_struct *packet = &ndpi_struct->packet; |
91 | |
|
92 | 0 | NDPI_LOG_DBG(ndpi_struct, "Ookla detection\n"); |
93 | |
|
94 | 0 | if(ntohs(flow->s_port) != ookla_port && ntohs(flow->c_port) != ookla_port) { |
95 | 0 | NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
96 | 0 | return; |
97 | 0 | } |
98 | | |
99 | 0 | if(flow->packet_counter == 1 && |
100 | 0 | packet->payload_packet_len >= NDPI_STATICSTRING_LEN("HI") && |
101 | 0 | memcmp(packet->payload, "HI", NDPI_STATICSTRING_LEN("HI")) == 0) { |
102 | 0 | flow->ookla_stage = 1; |
103 | 0 | return; |
104 | 0 | } |
105 | | |
106 | 0 | if(flow->packet_counter == 2 && |
107 | 0 | flow->ookla_stage == 1 && |
108 | 0 | packet->payload_packet_len >= NDPI_STATICSTRING_LEN("HELLO") && |
109 | 0 | memcmp(packet->payload, "HELLO", NDPI_STATICSTRING_LEN("HELLO")) == 0) { |
110 | 0 | NDPI_LOG_INFO(ndpi_struct, "found ookla (Hi + Hello)\n"); |
111 | 0 | ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_OOKLA, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI); |
112 | 0 | ookla_add_to_cache(ndpi_struct, flow); |
113 | 0 | return; |
114 | 0 | } |
115 | | |
116 | 0 | NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow); |
117 | 0 | } |
118 | | |
119 | | /* ************************************************************* */ |
120 | | |
121 | 0 | void init_ookla_dissector(struct ndpi_detection_module_struct *ndpi_struct) { |
122 | 0 | register_dissector("Ookla", ndpi_struct, |
123 | 0 | ndpi_search_ookla, |
124 | 0 | NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_OR_UDP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION, |
125 | 0 | 1, NDPI_PROTOCOL_OOKLA); |
126 | 0 | } |