Skip to content

Commit d1fa4ef

Browse files
zhaojh329gl-zhaojianhui
authored andcommitted
Add support KCP protocol
https://github.com/skywind3000/kcp/blob/master/README.en.md Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
1 parent 1cfc938 commit d1fa4ef

File tree

15 files changed

+969
-30
lines changed

15 files changed

+969
-30
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
[submodule "src/log"]
88
path = src/log
99
url = https://github.com/zhaojh329/log.git
10+
[submodule "src/kcp"]
11+
path = src/kcp
12+
url = https://github.com/skywind3000/kcp.git

src/CMakeLists.txt

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,43 @@ set(RTTY_VERSION_MAJOR 9)
33
set(RTTY_VERSION_MINOR 0)
44
set(RTTY_VERSION_PATCH 2)
55

6+
option(KCP_SUPPORT "KCP support" ON)
7+
68
# Check the third party Libraries
79
find_package(Libev REQUIRED)
810

9-
aux_source_directory(. SRCS)
1011
aux_source_directory(log SRCS)
1112
aux_source_directory(buffer SRCS)
1213

14+
add_subdirectory(ssl)
15+
16+
list(APPEND SRCS main.c rtty.c net.c utils.c file.c filectl.c command.c http.c)
17+
18+
if(KCP_SUPPORT)
19+
if (NOT SSL_SUPPORT)
20+
message(WARNING "SSL Not enabled, disable KCP")
21+
set(KCP_SUPPORT OFF)
22+
else()
23+
list(APPEND SRCS kcp.c crc32.c crypt.c)
24+
endif()
25+
endif()
26+
1327
add_executable(rtty ${SRCS})
1428
target_compile_definitions(rtty PRIVATE _GNU_SOURCE)
1529
target_compile_options(rtty PRIVATE -O -Wall -Werror --std=gnu99)
1630
target_include_directories(rtty PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/buffer ${LIBEV_INCLUDE_DIR})
1731
target_link_libraries(rtty PRIVATE ${LIBEV_LIBRARY} util crypt m)
1832

19-
add_subdirectory(ssl)
20-
2133
if(SSL_SUPPORT)
34+
target_compile_definitions(rtty PRIVATE ${SSL_DEFINE})
2235
target_link_libraries(rtty PRIVATE ${SSL_TARGET})
2336
endif()
2437

38+
if(KCP_SUPPORT)
39+
add_subdirectory(kcp)
40+
target_link_libraries(rtty PRIVATE kcp)
41+
endif()
42+
2543
# configure a header file to pass some of the CMake settings to the source code
2644
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)
2745

src/config.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@
3131
#define RTTY_VERSION_STRING "@RTTY_VERSION_MAJOR@.@RTTY_VERSION_MINOR@.@RTTY_VERSION_PATCH@"
3232

3333
#cmakedefine SSL_SUPPORT
34+
#cmakedefine KCP_SUPPORT
3435

3536
#endif

src/crc32.c

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2019 Jianhui Zhao <zhaojh329@gmail.com>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
#include <sys/types.h>
26+
#include <stdbool.h>
27+
#include <string.h>
28+
#include <stdint.h>
29+
30+
#define IEEE 0xedb88320
31+
32+
#define slicing8_cutoff 16
33+
34+
typedef uint32_t table[256];
35+
36+
typedef struct slicing8_table {
37+
table tables[8];
38+
} slicing8_table;
39+
40+
static slicing8_table tab;
41+
42+
static void simple_populate_table(uint32_t poly, table t)
43+
{
44+
int i, j;
45+
46+
for (i = 0; i < 256; i++) {
47+
uint32_t crc = i;
48+
for (j = 0; j < 8; j++) {
49+
if (crc & 1) {
50+
crc = (crc >> 1) ^ poly;
51+
} else {
52+
crc >>= 1;
53+
}
54+
}
55+
t[i] = crc;
56+
}
57+
}
58+
59+
static void slicing_make_table(uint32_t poly)
60+
{
61+
int i, j;
62+
63+
simple_populate_table(poly, tab.tables[0]);
64+
65+
for (i = 0; i < 256; i++) {
66+
uint32_t crc = tab.tables[0][i];
67+
for (j = 1; j < 8; j++) {
68+
crc = tab.tables[0][crc & 0xFF] ^ (crc >> 8);
69+
tab.tables[j][i] = crc;
70+
}
71+
}
72+
}
73+
74+
static uint32_t simple_update(uint32_t crc, const table tab, const uint8_t *p, size_t len)
75+
{
76+
size_t i;
77+
78+
crc = ~crc;
79+
80+
for (i = 0; i < len; i++)
81+
crc = tab[(uint8_t)crc ^ p[i]] ^ (crc >> 8);
82+
83+
return ~crc;
84+
}
85+
86+
uint32_t crc32_checksum_ieee(const uint8_t* p, size_t len)
87+
{
88+
static bool inited;
89+
uint32_t crc = 0;
90+
91+
if (!inited) {
92+
inited = true;
93+
slicing_make_table(IEEE);
94+
}
95+
96+
if (len >= slicing8_cutoff) {
97+
crc = ~crc;
98+
while (len >= 8) {
99+
uint32_t n;
100+
memcpy(&n, p, sizeof(n));
101+
crc ^= n;
102+
crc = tab.tables[0][p[7]] ^
103+
tab.tables[1][p[6]] ^
104+
tab.tables[2][p[5]] ^
105+
tab.tables[3][p[4]] ^
106+
tab.tables[4][crc >> 24] ^
107+
tab.tables[5][(crc >> 16) & 0xFF] ^
108+
tab.tables[6][(crc >> 8) & 0xFF] ^
109+
tab.tables[7][crc & 0xFF];
110+
p += 8;
111+
len -= 8;
112+
}
113+
crc = ~crc;
114+
}
115+
116+
if (len == 0)
117+
return crc;
118+
119+
return simple_update(crc, tab.tables[0], p, len);
120+
}

src/crc32.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2019 Jianhui Zhao <zhaojh329@gmail.com>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
#ifndef RTTY_CRC32_H
26+
#define RTTY_CRC32_H
27+
28+
#include <sys/types.h>
29+
#include <stdint.h>
30+
31+
uint32_t crc32_checksum_ieee(const uint8_t *p, size_t len);
32+
33+
#endif

0 commit comments

Comments
 (0)