Coverage Report

Created: 2025-08-28 06:24

/src/ndpi/src/lib/protocols/nats.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2020 - ntop.org
3
 *
4
 * This file is part of nDPI, an open source deep packet inspection
5
 * library based on the OpenDPI and PACE technology by ipoque GmbH
6
 *
7
 * nDPI is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Lesser General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * nDPI is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public License
18
 * along with nDPI.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
#include "ndpi_protocol_ids.h"
23
#define NDPI_CURRENT_PROTO NDPI_PROTOCOL_NATS
24
#include "ndpi_api.h"
25
#include "ndpi_private.h"
26
27
static const char* commands[] =
28
  {
29
   "INFO {",
30
   "CONNECT {",
31
   "PUB ",
32
   "SUB ",
33
   "UNSUB ",
34
   "MSG ",
35
   "PING",
36
   "PONG",
37
38
   NULL
39
  };
40
41
static void ndpi_search_nats_tcp(struct ndpi_detection_module_struct *ndpi_struct,
42
0
                                 struct ndpi_flow_struct *flow) {
43
0
  struct ndpi_packet_struct *packet = &ndpi_struct->packet;
44
45
  /* Check connection over TCP */
46
0
  NDPI_LOG_DBG(ndpi_struct, "search NATS\n");
47
48
0
  if(packet->tcp) {
49
0
    int i;
50
51
0
    if(packet->payload_packet_len <= 4)
52
0
      NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow);
53
54
0
    for(i=0; commands[i] != NULL; i++) {
55
0
      int len = ndpi_min(strlen(commands[i]), packet->payload_packet_len);
56
0
      int rc = strncmp((const char *)packet->payload, commands[i], len);
57
      
58
0
      if(rc != 0) continue;
59
60
0
      if(ndpi_strnstr((const char *)packet->payload,
61
0
          "\r\n",
62
0
          packet->payload_packet_len) != NULL) {
63
0
  NDPI_LOG_INFO(ndpi_struct, "found NATS\n");
64
65
0
  ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_NATS, NDPI_PROTOCOL_UNKNOWN, NDPI_CONFIDENCE_DPI);
66
0
  return;
67
0
      }
68
0
    }
69
70
0
    NDPI_EXCLUDE_DISSECTOR(ndpi_struct, flow);
71
0
  }
72
0
}
73
74
75
76
1
void init_nats_dissector(struct ndpi_detection_module_struct *ndpi_struct) {
77
1
  register_dissector("Nats", ndpi_struct,
78
1
                     ndpi_search_nats_tcp,
79
1
                     NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION,
80
1
                     1, NDPI_PROTOCOL_NATS);
81
1
}