Coverage Report

Created: 2025-10-10 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ndpi/src/lib/protocols/melsec.c
Line
Count
Source
1
/*
2
 * melsec.c
3
 *
4
 * MELSEC Communication Protocol
5
 * 
6
 * Copyright (C) 2025 - ntop.org
7
 * Copyright (C) 2025 - 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_MELSEC
30
31
#include "ndpi_api.h"
32
#include "ndpi_private.h"
33
34
static void ndpi_search_melsec(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow)
35
3.83M
{
36
3.83M
  struct ndpi_packet_struct const * const packet = &ndpi_struct->packet;
37
38
3.83M
  NDPI_LOG_DBG(ndpi_struct, "search MELSEC\n");
39
40
3.83M
  if (packet->payload_packet_len > 40 &&
41
2.07M
      (packet->payload[0] == 0x57 || packet->payload[0] == 0xD7))
42
7.52k
  {
43
7.52k
    u_int16_t melsec_payload_len = packet->payload_packet_len - 21;
44
7.52k
    if (le16toh(get_u_int16_t(packet->payload, 19)) == melsec_payload_len &&
45
7.52k
        ntohl(get_u_int32_t(packet->payload, 3)) == 0x00001111)
46
88
    {
47
88
      NDPI_LOG_INFO(ndpi_struct, "found MELSEC\n");
48
88
      ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_MELSEC, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI);
49
88
    }
50
7.52k
  }
51
52
3.83M
  if (flow->packet_counter > 2) {
53
373k
    NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow);
54
373k
  }
55
3.83M
}
56
57
void init_melsec_dissector(struct ndpi_detection_module_struct *ndpi_struct)
58
12.3k
{
59
12.3k
  register_dissector("MELSEC", ndpi_struct,
60
12.3k
                     ndpi_search_melsec,
61
12.3k
                     NDPI_SELECTION_BITMASK_PROTOCOL_TCP_OR_UDP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION,
62
12.3k
                      1, NDPI_PROTOCOL_MELSEC);
63
12.3k
}