Coverage Report

Created: 2025-08-26 07:06

/src/ndpi/src/lib/protocols/melsec.c
Line
Count
Source (jump to first uncovered line)
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
2.53k
{
36
2.53k
  struct ndpi_packet_struct const * const packet = &ndpi_struct->packet;
37
38
2.53k
  NDPI_LOG_DBG(ndpi_struct, "search MELSEC\n");
39
40
2.53k
  if (packet->payload_packet_len > 40 &&
41
2.53k
      (packet->payload[0] == 0x57 || packet->payload[0] == 0xD7))
42
10
  {
43
10
    u_int16_t melsec_payload_len = packet->payload_packet_len - 21;
44
10
    if (le16toh(get_u_int16_t(packet->payload, 19)) == melsec_payload_len &&
45
10
        ntohl(get_u_int32_t(packet->payload, 3)) == 0x00001111)
46
0
    {
47
0
      NDPI_LOG_INFO(ndpi_struct, "found MELSEC\n");
48
0
      ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_MELSEC, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI);
49
0
    }
50
10
  }
51
52
2.53k
  if (flow->packet_counter > 2) {
53
0
    NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow);
54
0
  }
55
2.53k
}
56
57
void init_melsec_dissector(struct ndpi_detection_module_struct *ndpi_struct)
58
7.91k
{
59
7.91k
  register_dissector("MELSEC", ndpi_struct,
60
7.91k
                     ndpi_search_melsec,
61
7.91k
                     NDPI_SELECTION_BITMASK_PROTOCOL_TCP_OR_UDP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION,
62
7.91k
                      1, NDPI_PROTOCOL_MELSEC);
63
7.91k
}