Coverage Report

Created: 2024-01-13 07:07

/src/ndpi/src/lib/protocols/kcp.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * kcp.c
3
 *
4
 * Copyright (C) 2024 - ntop.org
5
 *
6
 * nDPI is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * nDPI is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with nDPI.  If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 */
20
21
22
#include "ndpi_protocol_ids.h"
23
24
147k
#define NDPI_CURRENT_PROTO NDPI_PROTOCOL_KCP
25
26
#include "ndpi_api.h"
27
#include "ndpi_private.h"
28
29
PACK_ON
30
struct kcp_header {
31
  uint32_t conversation_id;
32
  uint8_t command;
33
  uint8_t fragment_count;
34
  uint16_t window_size;
35
  uint32_t timestamp;
36
  uint32_t serial_number;
37
  uint32_t unacknowledged_serial_number;
38
  uint32_t length;
39
  uint8_t data[0];
40
} PACK_OFF;
41
42
enum kcp_commands {
43
  IKCP_CMD_PUSH = 81,
44
  IKCP_CMD_ACK  = 82,
45
  IKCP_CMD_WASK = 83,
46
  IKCP_CMD_WINS = 84
47
};
48
49
static void ndpi_int_kcp_add_connection(struct ndpi_detection_module_struct * const ndpi_struct,
50
                                        struct ndpi_flow_struct * const flow)
51
2
{
52
2
  NDPI_LOG_INFO(ndpi_struct, "found kcp\n");
53
2
  ndpi_set_detected_protocol(ndpi_struct, flow,
54
2
                             NDPI_PROTOCOL_KCP,
55
2
                             NDPI_PROTOCOL_UNKNOWN,
56
2
                             NDPI_CONFIDENCE_DPI);
57
2
}
58
59
static void ndpi_search_kcp(struct ndpi_detection_module_struct *ndpi_struct,
60
                            struct ndpi_flow_struct *flow)
61
147k
{
62
147k
  struct ndpi_packet_struct const * const packet = &ndpi_struct->packet;
63
147k
  struct kcp_header const * const kcp_header = (struct kcp_header *)packet->payload;
64
65
147k
  NDPI_LOG_INFO(ndpi_struct, "search kcp\n");
66
67
147k
  if (packet->payload_packet_len < sizeof(*kcp_header))
68
59.5k
  {
69
59.5k
    NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
70
59.5k
    return;
71
59.5k
  }
72
73
88.0k
  switch (kcp_header->command)
74
88.0k
  {
75
98
    case IKCP_CMD_PUSH:
76
390
    case IKCP_CMD_ACK:
77
568
    case IKCP_CMD_WASK:
78
1.39k
    case IKCP_CMD_WINS:
79
1.39k
      break;
80
86.6k
    default:
81
86.6k
      NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
82
86.6k
      return;
83
88.0k
  }
84
85
1.39k
  uint32_t const kcp_pdu_length = le32toh(kcp_header->length);
86
1.39k
  if (kcp_pdu_length + sizeof(*kcp_header) != packet->payload_packet_len)
87
1.39k
  {
88
1.39k
    NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
89
1.39k
    return;
90
1.39k
  }
91
92
2
  ndpi_int_kcp_add_connection(ndpi_struct, flow);
93
2
}
94
95
void init_kcp_dissector(struct ndpi_detection_module_struct *ndpi_struct,
96
                        u_int32_t *id)
97
16.7k
{
98
16.7k
  ndpi_set_bitmask_protocol_detection("KCP", ndpi_struct, *id,
99
16.7k
    NDPI_PROTOCOL_KCP,
100
16.7k
    ndpi_search_kcp,
101
16.7k
    NDPI_SELECTION_BITMASK_PROTOCOL_V4_V6_TCP_OR_UDP_WITH_PAYLOAD_WITHOUT_RETRANSMISSION,
102
16.7k
    SAVE_DETECTION_BITMASK_AS_UNKNOWN,
103
16.7k
    ADD_TO_DETECTION_BITMASK
104
16.7k
  );
105
106
16.7k
  *id += 1;
107
16.7k
}