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/bacnet.c
Line
Count
Source
1
/*
2
 * bacnet.c
3
 *
4
 * Building Automation and Control Network
5
 *
6
 * Copyright (C) 2023 - ntop.org
7
 *
8
 * nDPI is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * nDPI is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with nDPI.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
24
#include "ndpi_protocol_ids.h"
25
26
#define NDPI_CURRENT_PROTO NDPI_PROTOCOL_BACNET
27
28
#include "ndpi_api.h"
29
#include "ndpi_private.h"
30
31
// BVLC (BACnet Virtual Link Control) Annex is part of BVLL (BACnet Virtual Link Layer).
32
// See: https://www.ashrae.org/file%20library/technical%20resources/standards%20and%20guidelines/standards%20addenda/135-1995_addendum-a.pdf
33
PACK_ON
34
struct bvlc_header {
35
  uint8_t type;
36
  uint8_t function;
37
  uint16_t length;
38
} PACK_OFF;
39
40
static void ndpi_int_bacnet_add_connection(struct ndpi_detection_module_struct * const ndpi_struct,
41
                                           struct ndpi_flow_struct * const flow)
42
262
{
43
262
  NDPI_LOG_INFO(ndpi_struct, "found BACnet\n");
44
45
262
  ndpi_set_detected_protocol(ndpi_struct, flow,
46
262
                             NDPI_PROTOCOL_BACNET,
47
262
                             NDPI_PROTOCOL_UNKNOWN,
48
262
                             NDPI_CONFIDENCE_DPI);
49
262
}
50
51
/* ***************************************************** */
52
53
static void ndpi_search_bacnet(struct ndpi_detection_module_struct *ndpi_struct,
54
                               struct ndpi_flow_struct *flow)
55
537k
{
56
537k
  struct ndpi_packet_struct const * const packet = &ndpi_struct->packet;
57
537k
  struct bvlc_header const * const bvlc = (struct bvlc_header *)&packet->payload[0];
58
59
537k
  NDPI_LOG_DBG(ndpi_struct, "search BACnet\n");
60
61
537k
  if (packet->payload_packet_len < sizeof(*bvlc))
62
9.60k
  {
63
9.60k
    NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow);
64
9.60k
    return;
65
9.60k
  }
66
67
527k
  if (bvlc->type != 0x81)
68
523k
  {
69
523k
    NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow);
70
523k
    return;
71
523k
  }
72
73
3.90k
  if (bvlc->function > 0x0b)
74
2.57k
  {
75
2.57k
    NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow);
76
2.57k
    return;
77
2.57k
  }
78
79
1.32k
  if (ntohs(bvlc->length) != packet->payload_packet_len)
80
1.06k
  {
81
1.06k
    NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow);
82
1.06k
    return;
83
1.06k
  }
84
85
262
  ndpi_int_bacnet_add_connection(ndpi_struct, flow);
86
262
}
87
88
/* ***************************************************** */
89
  
90
void init_bacnet_dissector(struct ndpi_detection_module_struct *ndpi_struct)
91
12.3k
{
92
12.3k
  register_dissector("BACnet", ndpi_struct,
93
12.3k
                     ndpi_search_bacnet,
94
12.3k
                     NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_UDP_WITH_PAYLOAD,
95
12.3k
                     1, NDPI_PROTOCOL_BACNET);
96
12.3k
}