Files
nDPI/src/lib/protocols/zattoo.c
T
Ivan Nardi 730c2360bd Remove struct ndpi_packet_struct from struct ndpi_flow_struct (#1319)
There are no real reasons to embed `struct ndpi_packet_struct` (i.e. "packet")
in `struct ndpi_flow_struct` (i.e. "flow"). In other words, we can avoid
saving dissection information of "current packet" into the "flow" state,
i.e. in the flow management table.

The nDPI detection module processes only one packet at the time, so it is
safe to save packet dissection information in `struct ndpi_detection_module_struct`,
reusing always the same "packet" instance and saving a huge amount of memory.
Bottom line: we need only one copy of "packet" (for detection module),
not one for each "flow".

It is not clear how/why "packet" ended up in "flow" in the first place.
It has been there since the beginning of the GIT history, but in the original
OpenDPI code `struct ipoque_packet_struct` was embedded in
`struct ipoque_detection_module_struct`, i.e. there was the same exact
situation this commit wants to achieve.

Most of the changes in this PR are some boilerplate to update something
like "flow->packet" into something like "module->packet" throughout the code.
Some attention has been paid to update `ndpi_init_packet()` since we need
to reset some "packet" fields before starting to process another packet.

There has been one important change, though, in ndpi_detection_giveup().
Nothing changed for the applications/users, but this function can't access
"packet" anymore.
The reason is that this function can be called "asynchronously" with respect
to the data processing, i.e in context where there is no valid notion of
"current packet"; for example ndpiReader calls it after having processed all
the traffic, iterating the entire session table.

Mining LRU stuff seems a bit odd (even before this patch): probably we need
to rethink it, as a follow-up.
2021-10-05 15:49:52 +02:00

246 lines
9.1 KiB
C

/*
* zattoo.c
*
* Copyright (C) 2016-21 - ntop.org
*
* This file is part of nDPI, an open source deep packet inspection
* library based on the OpenDPI and PACE technology by ipoque GmbH
*
* nDPI is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* nDPI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with nDPI. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "ndpi_protocol_ids.h"
#define NDPI_CURRENT_PROTO NDPI_PROTOCOL_ZATTOO
#include "ndpi_api.h"
#if !defined(WIN32)
static inline
#elif defined(MINGW_GCC)
__mingw_forceinline static
#else
__forceinline static
#endif
u_int8_t ndpi_int_zattoo_user_agent_set(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow)
{
struct ndpi_packet_struct *packet = &ndpi_struct->packet;
if(packet->user_agent_line.ptr != NULL && packet->user_agent_line.len == 111) {
if(memcmp(packet->user_agent_line.ptr + packet->user_agent_line.len - 25, "Zattoo/4", sizeof("Zattoo/4") - 1) == 0) {
NDPI_LOG_DBG(ndpi_struct, "found zattoo useragent\n");
return 1;
}
}
return 0;
}
#define ZATTOO_DETECTED \
if (src != NULL) \
src->zattoo_ts = packet->current_time_ms; \
if (dst != NULL) \
dst->zattoo_ts = packet->current_time_ms; \
\
ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_ZATTOO, NDPI_PROTOCOL_UNKNOWN)
void ndpi_search_zattoo(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow)
{
struct ndpi_packet_struct *packet = &ndpi_struct->packet;
struct ndpi_id_struct *src = flow->src;
struct ndpi_id_struct *dst = flow->dst;
u_int16_t i;
NDPI_LOG_DBG(ndpi_struct, "search ZATTOO\n");
if(flow->detected_protocol_stack[0] == NDPI_PROTOCOL_ZATTOO) {
if(src != NULL && ((u_int32_t) (packet->current_time_ms - src->zattoo_ts) < ndpi_struct->zattoo_connection_timeout))
src->zattoo_ts = packet->current_time_ms;
if (dst != NULL && ((u_int32_t) (packet->current_time_ms - dst->zattoo_ts) < ndpi_struct->zattoo_connection_timeout))
dst->zattoo_ts = packet->current_time_ms;
return;
}
/* search over TCP */
if(packet->tcp != NULL) {
if(packet->payload_packet_len > 50 && memcmp(packet->payload, "GET /frontdoor/fd?brand=Zattoo&v=", 33) == 0) {
NDPI_LOG_INFO(ndpi_struct, "found zattoo. add connection over tcp with pattern GET /frontdoor/fd?brand=Zattoo&v=\n");
ZATTOO_DETECTED;
return;
}
if(packet->payload_packet_len > 50 && memcmp(packet->payload, "GET /ZattooAdRedirect/redirect.jsp?user=", 40) == 0) {
NDPI_LOG_INFO(ndpi_struct, "found zattoo. add connection over tcp with pattern GET /ZattooAdRedirect/redirect.jsp?user=\n");
ZATTOO_DETECTED;
return;
}
if(packet->payload_packet_len > 50 && (memcmp(packet->payload, "POST /channelserver/player/channel/update HTTP/1.1", 50) == 0
|| memcmp(packet->payload, "GET /epg/query", 14) == 0)) {
ndpi_parse_packet_line_info(ndpi_struct, flow);
for(i = 0; i < packet->parsed_lines; i++) {
if(packet->line[i].len >= 18 && (memcmp(packet->line[i].ptr, "User-Agent: Zattoo", 18) == 0)) {
NDPI_LOG_INFO(ndpi_struct, "found zattoo. add connection over tcp with pattern POST /channelserver/player/channel/update HTTP/1.1\n");
ZATTOO_DETECTED;
return;
}
}
} else if(packet->payload_packet_len > 50 && (memcmp(packet->payload, "GET /", 5) == 0 || memcmp(packet->payload, "POST /", NDPI_STATICSTRING_LEN("POST /")) == 0)) {
/* TODO to avoid searching currently only a specific length and offset is used
* that might be changed later */
ndpi_parse_packet_line_info(ndpi_struct, flow);
if(ndpi_int_zattoo_user_agent_set(ndpi_struct, flow)) {
NDPI_LOG_INFO(ndpi_struct, "found zattoo. add connection over tcp with pattern GET / or POST /\n");
ZATTOO_DETECTED;
return;
}
} else if(packet->payload_packet_len > 50 && memcmp(packet->payload, "POST http://", 12) == 0) {
ndpi_parse_packet_line_info(ndpi_struct, flow);
// test for unique character of the zattoo header
if(packet->parsed_lines == 4 && packet->host_line.ptr != NULL) {
u_int32_t ip;
u_int16_t bytes_read = 0;
ip = ndpi_bytestream_to_ipv4(&packet->payload[12], packet->payload_packet_len, &bytes_read);
// and now test the firt 5 bytes of the payload for zattoo pattern
if(ip == packet->iph->daddr
&& packet->empty_line_position_set != 0
&& ((packet->payload_packet_len - packet->empty_line_position) > 10)
&& packet->payload[packet->empty_line_position + 2] ==
0x03
&& packet->payload[packet->empty_line_position + 3] ==
0x04
&& packet->payload[packet->empty_line_position + 4] ==
0x00
&& packet->payload[packet->empty_line_position + 5] ==
0x04
&& packet->payload[packet->empty_line_position + 6] ==
0x0a && packet->payload[packet->empty_line_position + 7] == 0x00) {
NDPI_LOG_INFO(ndpi_struct, "found zattoo. add connection over tcp with pattern POST http://\n");
ZATTOO_DETECTED;
return;
}
}
} else if(flow->zattoo_stage == 0) {
if(packet->payload_packet_len > 50
&& packet->payload[0] == 0x03
&& packet->payload[1] == 0x04
&& packet->payload[2] == 0x00
&& packet->payload[3] == 0x04 && packet->payload[4] == 0x0a && packet->payload[5] == 0x00) {
flow->zattoo_stage = 1 + packet->packet_direction;
NDPI_LOG_DBG2(ndpi_struct, "need next packet, seen pattern 0x030400040a00\n");
return;
}
/* the following is searching for flash, not for zattoo. */
} else if(flow->zattoo_stage == 2 - packet->packet_direction && packet->payload_packet_len > 50 && packet->payload[0] == 0x03 && packet->payload[1] == 0x04) {
NDPI_LOG_INFO(ndpi_struct, "found zattoo. add connection over tcp with 0x0304\n");
ZATTOO_DETECTED;
return;
} else if(flow->zattoo_stage == 1 + packet->packet_direction) {
if(packet->payload_packet_len > 500 && packet->payload[0] == 0x00 && packet->payload[1] == 0x00) {
flow->zattoo_stage = 3 + packet->packet_direction;
NDPI_LOG_DBG2(ndpi_struct, "need next packet, seen pattern 0x0000\n");
return;
}
#if 0
if(packet->payload_packet_len > 50
&& packet->payload[0] == 0x03
&& packet->payload[1] == 0x04
&& packet->payload[2] == 0x00
&& packet->payload[3] == 0x04 && packet->payload[4] == 0x0a && packet->payload[5] == 0x00) {
}
#endif
NDPI_LOG_DBG2(ndpi_struct, "need next packet, seen pattern 0x030400040a00\n");
return;
} else if(flow->zattoo_stage == 4 - packet->packet_direction && packet->payload_packet_len > 50 && packet->payload[0] == 0x03 && packet->payload[1] == 0x04) {
NDPI_LOG_INFO(ndpi_struct, "found zattoo. add connection over tcp with 0x0304\n");
ZATTOO_DETECTED;
return;
} else if(flow->zattoo_stage == 5 + packet->packet_direction && (packet->payload_packet_len == 125)) {
NDPI_LOG_INFO(ndpi_struct, "found zattoo\n");
ZATTOO_DETECTED;
return;
} else if(flow->zattoo_stage == 6 - packet->packet_direction && packet->payload_packet_len == 1412) {
NDPI_LOG_INFO(ndpi_struct, "found zattoo\n");
ZATTOO_DETECTED;
return;
}
NDPI_LOG_DBG2(ndpi_struct,
"ZATTOO: discarded the flow (TCP): packet_size: %u; Flowstage: %u\n",
packet->payload_packet_len, flow->zattoo_stage);
}
/* search over UDP */
else if(packet->udp != NULL) {
if(packet->payload_packet_len > 20 && (packet->udp->dest == htons(5003) || packet->udp->source == htons(5003))
&& (get_u_int16_t(packet->payload, 0) == htons(0x037a)
|| get_u_int16_t(packet->payload, 0) == htons(0x0378)
|| get_u_int16_t(packet->payload, 0) == htons(0x0305)
|| get_u_int32_t(packet->payload, 0) == htonl(0x03040004)
|| get_u_int32_t(packet->payload, 0) == htonl(0x03010005))) {
if(++flow->zattoo_stage == 2) {
NDPI_LOG_INFO(ndpi_struct, "found zattoo. add connection over udp\n");
ZATTOO_DETECTED;
return;
}
NDPI_LOG_DBG2(ndpi_struct, "need next packet udp\n");
return;
}
NDPI_LOG_DBG2(ndpi_struct,
"ZATTOO: discarded the flow (UDP): packet_size: %u; Flowstage: %u\n",
packet->payload_packet_len, flow->zattoo_stage);
}
NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
}
void init_zattoo_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask)
{
ndpi_set_bitmask_protocol_detection("Zattoo", ndpi_struct, detection_bitmask, *id,
NDPI_PROTOCOL_ZATTOO,
ndpi_search_zattoo,
NDPI_SELECTION_BITMASK_PROTOCOL_TCP_OR_UDP_WITH_PAYLOAD,
SAVE_DETECTION_BITMASK_AS_UNKNOWN,
ADD_TO_DETECTION_BITMASK);
*id += 1;
}