Skip to content

Commit 5a190cb

Browse files
committed
fix compilation
1 parent 3127dcc commit 5a190cb

11 files changed

Lines changed: 58 additions & 84 deletions

File tree

.clang-format

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ IncludeCategories:
2727
Priority: 3
2828
- Regex: '^<protocol.pb.h>'
2929
Priority: 2
30-
- Regex: '^<tuntap++.*>'
31-
Priority: 2
3230
- Regex: '^<ntp_client.hpp>'
3331
Priority: 2
3432
- Regex: '^<blockingconcurrentqueue.h>'

.clang-tidy

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@
7272
# Despite what the documentation says, this warning appears after
7373
# `ASSERT_TRUE(variable)` or `ASSERT_TRUE(variable.has_value())`.
7474
#
75+
# -clang-diagnostic-keyword-macro: linux_tun_device.h intentionally does
76+
# `#define class class_` around the kernel UAPI header <linux/virtio_net.h>
77+
# (which declares a C member named `class`) and `#undef`s it right after.
78+
# Redefining a keyword is deliberate and confined to that include.
79+
#
7580
# TODO(#14162): Enable clang-tidy checks. We initially omitted these checks
7681
# because they require large cleanup efforts or were blocking the clang-tidy
7782
# X update.
@@ -114,7 +119,8 @@ Checks: >
114119
-bugprone-unchecked-optional-access,
115120
-bugprone-unused-local-non-trivial-variable,
116121
-bugprone-unused-return-value,
117-
-portability-template-virtual-member-function
122+
-portability-template-virtual-member-function,
123+
-clang-diagnostic-keyword-macro
118124
119125
# Turn all the warnings from the checks above into errors.
120126
WarningsAsErrors: "*"

.github/workflows/main.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
cppcheck --force --error-exitcode=1 --enable=all --check-level=exhaustive --language=c++ --suppress=unusedFunction --inline-suppr --suppress=missingIncludeSystem --suppress=unknownMacro --suppress=unmatchedSuppression -I ./src/fptn-client/ -I ./src/fptn-server/ -I ./src/fptn-passwd/ -I ./src/fptn-client-protocol-lib -I ./src/ ./src/ ./tests/
4242
- name: Run cmake-format
4343
run: |
44-
cmake-format -i CMakeLists.txt src/fptn-client/CMakeLists.txt src/fptn-passwd/CMakeLists.txt src/fptn-server/CMakeLists.txt src/fptn-protocol-lib/CMakeLists.txt depends/cmake/FetchBase64.cmake depends/cmake/FetchLibTunTap.cmake depends/cmake/FetchWintun.cmake
44+
cmake-format -i CMakeLists.txt src/fptn-client/CMakeLists.txt src/fptn-passwd/CMakeLists.txt src/fptn-server/CMakeLists.txt src/fptn-protocol-lib/CMakeLists.txt depends/cmake/FetchBase64.cmake depends/cmake/FetchWintun.cmake
4545
- name: Build
4646
run: |
4747
cd build
@@ -113,7 +113,7 @@ jobs:
113113
cppcheck --force --error-exitcode=1 --enable=all --check-level=exhaustive --language=c++ --suppress=unusedFunction --inline-suppr --suppress=missingIncludeSystem --suppress=unmatchedSuppression -I ./src/fptn-client/ -I ./src/fptn-server/ -I ./src/fptn-passwd/ -I ./src/fptn-client-protocol-lib -I ./src/ ./src/ ./tests/
114114
- name: Run cmake-format
115115
run: |
116-
cmake-format -i CMakeLists.txt src/fptn-client/CMakeLists.txt src/fptn-passwd/CMakeLists.txt src/fptn-server/CMakeLists.txt src/fptn-protocol-lib/CMakeLists.txt depends/cmake/FetchBase64.cmake depends/cmake/FetchLibTunTap.cmake depends/cmake/FetchWintun.cmake
116+
cmake-format -i CMakeLists.txt src/fptn-client/CMakeLists.txt src/fptn-passwd/CMakeLists.txt src/fptn-server/CMakeLists.txt src/fptn-protocol-lib/CMakeLists.txt depends/cmake/FetchBase64.cmake depends/cmake/FetchWintun.cmake
117117
- name: Build
118118
run: |
119119
cd build
@@ -185,7 +185,7 @@ jobs:
185185
cppcheck --force --error-exitcode=1 --enable=all --check-level=exhaustive --language=c++ --suppress=unusedFunction --inline-suppr --suppress=missingIncludeSystem --suppress=unknownMacro --suppress=unmatchedSuppression -I ./src/fptn-client/ -I ./src/fptn-server/ -I ./src/fptn-passwd/ -I ./src/fptn-client-protocol-lib -I ./src/ ./src/ ./tests/
186186
- name: Run cmake-format
187187
run: |
188-
cmake-format -i CMakeLists.txt src/fptn-client/CMakeLists.txt src/fptn-passwd/CMakeLists.txt src/fptn-server/CMakeLists.txt src/fptn-protocol-lib/CMakeLists.txt depends/cmake/FetchBase64.cmake depends/cmake/FetchLibTunTap.cmake depends/cmake/FetchWintun.cmake
188+
cmake-format -i CMakeLists.txt src/fptn-client/CMakeLists.txt src/fptn-passwd/CMakeLists.txt src/fptn-server/CMakeLists.txt src/fptn-protocol-lib/CMakeLists.txt depends/cmake/FetchBase64.cmake depends/cmake/FetchWintun.cmake
189189
- name: Build
190190
run: |
191191
cd build
@@ -250,7 +250,7 @@ jobs:
250250
- name: Run cmake-format
251251
shell: powershell
252252
run: |
253-
cmake-format -i CMakeLists.txt src/fptn-client/CMakeLists.txt src/fptn-passwd/CMakeLists.txt src/fptn-server/CMakeLists.txt src/fptn-protocol-lib/CMakeLists.txt depends/cmake/FetchBase64.cmake depends/cmake/FetchLibTunTap.cmake depends/cmake/FetchWintun.cmake
253+
cmake-format -i CMakeLists.txt src/fptn-client/CMakeLists.txt src/fptn-passwd/CMakeLists.txt src/fptn-server/CMakeLists.txt src/fptn-protocol-lib/CMakeLists.txt depends/cmake/FetchBase64.cmake depends/cmake/FetchWintun.cmake
254254
- name: Build
255255
shell: powershell
256256
run: |

CMakeLists.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,9 @@ include(depends/cmake/NtpClient.cmake)
8080
include(depends/cmake/CamouflageTLS.cmake)
8181
8282
if (NOT FPTN_BUILD_ONLY_FPTN_LIB)
83-
if (APPLE OR UNIX)
84-
include(depends/cmake/FetchLibTunTap.cmake)
85-
elseif (WIN32)
83+
if (WIN32)
8684
include(depends/cmake/FetchWintun.cmake)
87-
else ()
85+
elseif (NOT (APPLE OR UNIX))
8886
message(FATAL_ERROR "Unsupported platform")
8987
endif ()
9088
endif ()

depends/cmake/FetchLibTunTap.cmake

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/common/network/tun/linux_tun_device.h

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,37 @@ Distributed under the MIT License (https://opensource.org/licenses/MIT)
77

88
#pragma once
99

10-
#include <fcntl.h>
11-
#include <linux/if_tun.h>
12-
// <linux/virtio_net.h> declares a struct member named `class`, valid in C
13-
// but a reserved word in C++. Rename it for the duration of the include so
14-
// we can still use the system UAPI header (struct virtio_net_hdr et al).
15-
#define class class_
16-
#include <linux/virtio_net.h>
17-
#undef class
18-
#include <net/if.h>
19-
#include <netinet/in.h>
20-
#include <sys/ioctl.h>
21-
#include <sys/socket.h>
22-
#include <sys/uio.h>
23-
#include <unistd.h>
10+
#include <fcntl.h> // NOLINT(build/include_order)
11+
#include <linux/if_tun.h> // NOLINT(build/include_order)
12+
#include <unistd.h> // NOLINT(build/include_order)
2413

14+
#if defined(__clang__)
15+
#pragma clang diagnostic push
16+
#pragma clang diagnostic ignored "-Wkeyword-macro"
17+
#endif
18+
#define class class_
19+
#if defined(__clang__)
20+
#pragma clang diagnostic pop
21+
#endif
2522
#include <arpa/inet.h>
23+
#include <linux/ipv6.h> // NOLINT(build/include_order)
24+
#include <linux/virtio_net.h> // NOLINT(build/include_order)
25+
#include <net/if.h> // NOLINT(build/include_order)
26+
#include <netinet/in.h> // NOLINT(build/include_order)
27+
#include <sys/ioctl.h> // NOLINT(build/include_order)
28+
#include <sys/socket.h> // NOLINT(build/include_order)
29+
#include <sys/uio.h> // NOLINT(build/include_order)
30+
#undef class
2631

2732
#include <atomic>
2833
#include <cerrno>
2934
#include <cstdio>
3035
#include <cstring>
36+
#include <deque>
3137
#include <string>
3238
#include <utility>
3339
#include <vector>
3440

35-
#include <deque>
36-
3741
#include <spdlog/spdlog.h> // NOLINT(build/include_order)
3842

3943
#include "common/network/ip_utils.h"
@@ -153,18 +157,11 @@ class LinuxTunDevice {
153157
return false;
154158
}
155159

156-
// struct in6_ifreq from <linux/ipv6.h> (defined locally to avoid
157-
// conflicts between kernel and libc headers)
158-
struct In6Ifreq {
159-
struct in6_addr addr;
160-
std::uint32_t prefixlen;
161-
int ifindex;
162-
} req = {};
163-
164-
req.ifindex = static_cast<int>(::if_nametoindex(name_.c_str()));
165-
req.prefixlen = static_cast<std::uint32_t>(prefixlen);
166-
if (req.ifindex == 0 ||
167-
::inet_pton(AF_INET6, addr.c_str(), &req.addr) != 1 ||
160+
struct in6_ifreq req = {};
161+
req.ifr6_ifindex = static_cast<int>(::if_nametoindex(name_.c_str()));
162+
req.ifr6_prefixlen = static_cast<std::uint32_t>(prefixlen);
163+
if (req.ifr6_ifindex == 0 ||
164+
::inet_pton(AF_INET6, addr.c_str(), &req.ifr6_addr) != 1 ||
168165
::ioctl(sock, SIOCSIFADDR, &req) < 0) {
169166
SPDLOG_WARN("IPv6 SIOCSIFADDR({}) failed: {}", addr, strerror(errno));
170167
::close(sock);
@@ -177,8 +174,8 @@ class LinuxTunDevice {
177174
void SetNonBlocking(bool enabled) {
178175
const int flags = ::fcntl(fd_, F_GETFL, 0);
179176
if (flags >= 0) {
180-
::fcntl(fd_, F_SETFL,
181-
enabled ? (flags | O_NONBLOCK) : (flags & ~O_NONBLOCK));
177+
::fcntl(
178+
fd_, F_SETFL, enabled ? (flags | O_NONBLOCK) : (flags & ~O_NONBLOCK));
182179
}
183180
}
184181

@@ -294,7 +291,8 @@ class LinuxTunDevice {
294291
// csum_start is used as the network-header length and hdr.hdr_len from
295292
// the kernel is not trusted (it can be the whole first packet length on
296293
// the FORWARD path).
297-
bool SegmentGsoFrame(const std::uint8_t* frame, std::size_t size,
294+
bool SegmentGsoFrame(const std::uint8_t* frame,
295+
std::size_t size,
298296
const struct virtio_net_hdr& hdr) {
299297
// Non-GSO frame: pass through. With VIRTIO_NET_HDR_F_NEEDS_CSUM
300298
// (CHECKSUM_PARTIAL) the kernel stored the pseudo-header sum at the
@@ -311,9 +309,15 @@ class LinuxTunDevice {
311309
const std::uint32_t initial = ReadU16Be(p + csum_at);
312310
p[csum_at] = 0;
313311
p[csum_at + 1] = 0;
314-
WriteU16Be(p + csum_at,
315-
Rfc1071(p + hdr.csum_start,
316-
static_cast<int>(size - hdr.csum_start), initial));
312+
std::uint16_t ck = Rfc1071(p + hdr.csum_start,
313+
static_cast<int>(size - hdr.csum_start), initial);
314+
// RFC 768/8200: a UDP checksum computed as 0 goes on the wire as
315+
// 0xFFFF (proto at byte 9 for IPv4, next-header at byte 6 for IPv6).
316+
const std::uint8_t proto = (p[0] >> 4) == 6u ? p[6] : p[9];
317+
if (ck == 0u && proto == IPPROTO_UDP) {
318+
ck = 0xFFFFu;
319+
}
320+
WriteU16Be(p + csum_at, ck);
317321
} else {
318322
pending_.emplace_back(frame, frame + size);
319323
}
@@ -436,9 +440,12 @@ class LinuxTunDevice {
436440
// Transport checksum: pseudo-header seed + one pass over L4 data.
437441
tr[hdr.csum_offset] = 0;
438442
tr[hdr.csum_offset + 1] = 0;
439-
WriteU16Be(tr + hdr.csum_offset,
440-
Rfc1071(tr, static_cast<int>(l4_len),
441-
addr_sum + proto + static_cast<std::uint32_t>(l4_len)));
443+
std::uint16_t ck = Rfc1071(tr, static_cast<int>(l4_len),
444+
addr_sum + proto + static_cast<std::uint32_t>(l4_len));
445+
if (!tcp && ck == 0u) {
446+
ck = 0xFFFFu; // RFC 768/8200: UDP puts 0xFFFF on the wire, not 0.
447+
}
448+
WriteU16Be(tr + hdr.csum_offset, ck);
442449

443450
pending_.push_back(std::move(seg));
444451
offset += chunk;

src/fptn-client/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ project(fptn-client)
22

33
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
44

5-
if(APPLE OR UNIX)
6-
set(TUNTAP_LIB tuntap++)
7-
else()
5+
if(WIN32)
86
set(TUNTAP_LIB Wintun rpcrt4 iphlpapi Kernel32.lib)
97
endif()
108

src/fptn-server/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,5 @@ target_link_libraries(
8888
jwt-cpp::jwt-cpp
8989
spdlog::spdlog
9090
fmt::fmt
91-
tuntap++
9291
fptn-protocol-lib_static
9392
prometheus-cpp::prometheus-cpp)

tests/common/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ find_package(Boost REQUIRED COMPONENTS random filesystem)
1010
if(WIN32)
1111
set(TUNTAP_LIB Wintun rpcrt4)
1212
add_definitions(-D_WIN32_WINNT=0x0601)
13-
else()
14-
set(TUNTAP_LIB tuntap++)
1513
endif()
1614

1715
set(LIBS

tests/fptnlib/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ find_package(Boost REQUIRED COMPONENTS random filesystem)
1010
if(WIN32)
1111
set(TUNTAP_LIB Wintun rpcrt4)
1212
add_definitions(-D_WIN32_WINNT=0x0601)
13-
else()
14-
set(TUNTAP_LIB tuntap++)
1513
endif()
1614

1715
set(LIBS

0 commit comments

Comments
 (0)