Coverage Report

Created: 2024-01-13 07:07

/src/ndpi/src/lib/protocols/ethersbus.c
Line
Count
Source
1
/*
2
 * ethersbus.c
3
 *
4
 * Copyright (C) 2023 - ntop.org
5
 * Copyright (C) 2023 - V.G <jacendi@protonmail.com>
6
 *
7
 * This file is part of nDPI, an open source deep packet inspection
8
 * library based on the OpenDPI and PACE technology by ipoque GmbH
9
 *
10
 * nDPI is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Lesser General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * nDPI is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Lesser General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Lesser General Public License
21
 * along with nDPI.  If not, see <http://www.gnu.org/licenses/>.
22
 * 
23
 */
24
25
#include "ndpi_protocol_ids.h"
26
27
65.6k
#define NDPI_CURRENT_PROTO NDPI_PROTOCOL_ETHERSBUS
28
29
#include "ndpi_api.h"
30
#include "ndpi_private.h"
31
32
static void ndpi_int_ethersbus_add_connection(struct ndpi_detection_module_struct *ndpi_struct,
33
                                              struct ndpi_flow_struct *flow)
34
34
{
35
34
  NDPI_LOG_INFO(ndpi_struct, "found Ether-S-Bus\n");
36
34
  ndpi_set_detected_protocol(ndpi_struct, flow,
37
34
                             NDPI_PROTOCOL_ETHERSBUS, NDPI_PROTOCOL_UNKNOWN,
38
34
                             NDPI_CONFIDENCE_DPI);
39
34
}
40
41
static void ndpi_search_ethersbus(struct ndpi_detection_module_struct *ndpi_struct,
42
                                  struct ndpi_flow_struct *flow)
43
65.7k
{
44
65.7k
  struct ndpi_packet_struct const * const packet = &ndpi_struct->packet;
45
46
65.7k
  NDPI_LOG_DBG(ndpi_struct, "search Ether-S-Bus\n");
47
48
65.7k
  if ((packet->payload_packet_len > 12) &&
49
65.7k
      (ntohl(get_u_int32_t(packet->payload,0)) == packet->payload_packet_len) &&
50
65.7k
      (packet->payload[4] < 2) && (packet->payload[5] == 0))
51
63
  {
52
63
    u_int16_t crc = ndpi_crc16_xmodem(packet->payload,packet->payload_packet_len-2);
53
63
    if (get_u_int16_t(packet->payload,packet->payload_packet_len-2) == htons(crc))
54
34
    {
55
34
      ndpi_int_ethersbus_add_connection(ndpi_struct, flow);
56
34
      return;
57
34
    }
58
63
  }
59
60
65.6k
  NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
61
65.6k
}
62
63
void init_ethersbus_dissector(struct ndpi_detection_module_struct *ndpi_struct,
64
                              u_int32_t *id)
65
16.7k
{
66
16.7k
  ndpi_set_bitmask_protocol_detection("Ether-S-Bus", ndpi_struct, *id,
67
16.7k
                                      NDPI_PROTOCOL_ETHERSBUS,
68
16.7k
                                      ndpi_search_ethersbus,
69
16.7k
                                      NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_UDP_WITH_PAYLOAD,
70
16.7k
                                      SAVE_DETECTION_BITMASK_AS_UNKNOWN,
71
16.7k
                                      ADD_TO_DETECTION_BITMASK);
72
73
16.7k
  *id += 1;
74
16.7k
}