From 68832ab86531996952bd6446f6bcf3c565608fe9 Mon Sep 17 00:00:00 2001 From: Robert Swiecki Date: Thu, 2 Apr 2026 01:31:47 +0200 Subject: [PATCH] nstun: remove dead code, and change some functions to static --- .clangd | 2 +- nstun/icmp.cc | 6 +++--- nstun/icmp.h | 3 +-- nstun/net_defs.h | 1 + nstun/policy.cc | 5 ----- nstun/policy.h | 3 --- nstun/tcp.cc | 2 +- nstun/tcp.h | 2 -- nstun/tun.cc | 18 ------------------ nstun/tun.h | 1 - nstun/udp.cc | 8 ++++---- nstun/udp.h | 3 --- 12 files changed, 11 insertions(+), 43 deletions(-) diff --git a/.clangd b/.clangd index b616835..ced2f16 100644 --- a/.clangd +++ b/.clangd @@ -1,2 +1,2 @@ CompileFlags: - Add: [-std=c++17, -Ikafel/include, -I/usr/include/libnl3, -fno-exceptions, -Wno-unused, -Wno-unused-parameter] + Add: [-std=c++20, -Ikafel/include, -I/usr/include/libnl3, -fno-exceptions, -Wno-unused, -Wno-unused-parameter] diff --git a/nstun/icmp.cc b/nstun/icmp.cc index b694828..8dffbd3 100644 --- a/nstun/icmp.cc +++ b/nstun/icmp.cc @@ -17,7 +17,7 @@ namespace nstun { -void icmp_destroy_flow(Context* ctx, IcmpFlow* flow) { +static void icmp_destroy_flow(Context* ctx, IcmpFlow* flow) { if (flow->host_fd != -1) { epoll_ctl(ctx->epoll_fd, EPOLL_CTL_DEL, flow->host_fd, nullptr); ctx->flows_by_fd.erase(flow->host_fd); @@ -46,7 +46,7 @@ static void icmp_send_packet4(Context* ctx, uint32_t saddr, uint32_t daddr, uint icmp4_hdr* r_icmp = reinterpret_cast(header_buf + sizeof(ip4_hdr)); /* IPv4 */ - r_ip->ihl_version = (4 << 4) | (sizeof(ip4_hdr) / 4); + ip4_set_ihl_version(r_ip, 4, sizeof(ip4_hdr) / 4); r_ip->tos = 0; r_ip->tot_len = htons(frame_len); r_ip->id = 0; @@ -389,7 +389,7 @@ void handle_icmp4(Context* ctx, const ip4_hdr* ip, std::span payl } } -void handle_host_icmp(Context* ctx, IcmpFlow* flow) { +static void handle_host_icmp(Context* ctx, IcmpFlow* flow) { int fd = flow->host_fd; flow->last_active = time(NULL); diff --git a/nstun/icmp.h b/nstun/icmp.h index 20e72d6..55c1364 100644 --- a/nstun/icmp.h +++ b/nstun/icmp.h @@ -12,8 +12,7 @@ namespace nstun { void handle_icmp4(Context* ctx, const ip4_hdr* ip, std::span payload); void handle_icmp6(Context* ctx, const ip6_hdr* ip, std::span payload); -void handle_host_icmp(Context* ctx, IcmpFlow* flow); -void icmp_destroy_flow(Context* ctx, IcmpFlow* flow); + void send_icmp4_error( Context* ctx, const ip4_hdr* req_ip, size_t tot_len, uint8_t type, uint8_t code); void send_icmp6_error( diff --git a/nstun/net_defs.h b/nstun/net_defs.h index 7c23113..69e964a 100644 --- a/nstun/net_defs.h +++ b/nstun/net_defs.h @@ -107,6 +107,7 @@ inline uint8_t ip_version(const uint8_t* ptr) { return ptr[0] >> 4; } + inline uint8_t ip4_version(const ip4_hdr* h) { return h->ihl_version >> 4; } diff --git a/nstun/policy.cc b/nstun/policy.cc index 30ada1f..af2b960 100644 --- a/nstun/policy.cc +++ b/nstun/policy.cc @@ -1,13 +1,11 @@ #include "policy.h" - #include #include "core.h" #include "logs.h" #include "nstun.h" - /* Pull in the protobuf types for NstunRule enums */ #include "config.pb.h" #include "nsjail.h" @@ -139,7 +137,4 @@ RuleParseStatus fill_rule_common(const RuleMsg& r, nstun_rule_t* nr) { template RuleParseStatus fill_rule_common( const nsjail::NsJailConfig_UserNet_NstunRule& r, nstun_rule_t* nr); - - - } /* namespace nstun */ diff --git a/nstun/policy.h b/nstun/policy.h index 4a77aeb..1a59898 100644 --- a/nstun/policy.h +++ b/nstun/policy.h @@ -21,9 +21,6 @@ RuleResult evaluate_rules6(Context* ctx, nstun_direction_t dir, nstun_proto_t pr template RuleParseStatus fill_rule_common(const RuleMsg& r, nstun_rule_t* nr); - - - } /* namespace nstun */ #endif /* NSTUN_POLICY_H_ */ diff --git a/nstun/tcp.cc b/nstun/tcp.cc index 4daecfa..78901d7 100644 --- a/nstun/tcp.cc +++ b/nstun/tcp.cc @@ -94,7 +94,7 @@ void tcp_send_packet4(Context* ctx, TcpFlow* flow, uint8_t flags, const uint8_t* uint8_t* r_opt = frame_buf + sizeof(ip4_hdr) + sizeof(tcp_hdr); /* IPv4 */ - r_ip->ihl_version = (4 << 4) | (sizeof(ip4_hdr) / 4); + ip4_set_ihl_version(r_ip, 4, sizeof(ip4_hdr) / 4); r_ip->tos = 0; r_ip->tot_len = htons(sizeof(ip4_hdr) + sizeof(tcp_hdr) + opt_len + len); r_ip->id = 0; diff --git a/nstun/tcp.h b/nstun/tcp.h index 4920296..c9622a9 100644 --- a/nstun/tcp.h +++ b/nstun/tcp.h @@ -43,8 +43,6 @@ struct TcpFlow : public Flow { uint32_t seq_from_guest = 0; uint32_t ack_to_guest = 0; - - /* Buffer for data from host to guest (not yet ACKed) */ /* In a real TCP stack, this would handle retransmissions. */ /* Here, we just queue it to send. */ diff --git a/nstun/tun.cc b/nstun/tun.cc index 0c34d3e..e64cd2e 100644 --- a/nstun/tun.cc +++ b/nstun/tun.cc @@ -15,24 +15,6 @@ namespace nstun { -bool send_to_guest(Context* ctx, const void* data, size_t len) { - ssize_t written = TEMP_FAILURE_RETRY(write(ctx->tap_fd, data, len)); - if (written < 0) { - if (errno == EAGAIN || errno == EWOULDBLOCK) { - /* Saturated queue, drop packet normally */ - return false; - } - PLOG_E("write(tap_fd) failed"); - return false; - } - if ((size_t)written != len) { - LOG_E("write(tap_fd) partial write: %zd of %zu", written, len); - return false; - } - - return true; -} - bool send_to_guest_v( Context* ctx, const void* header, size_t header_len, const void* payload, size_t payload_len) { if (header_len > NSTUN_MTU || payload_len > NSTUN_MTU - header_len) { diff --git a/nstun/tun.h b/nstun/tun.h index f827e33..76da2b9 100644 --- a/nstun/tun.h +++ b/nstun/tun.h @@ -8,7 +8,6 @@ namespace nstun { -bool send_to_guest(Context* ctx, const void* data, size_t len); bool send_to_guest_v( Context* ctx, const void* header, size_t header_len, const void* payload, size_t payload_len); void handle_tun_frame(Context* ctx, const uint8_t* buf, size_t len); diff --git a/nstun/udp.cc b/nstun/udp.cc index 6575bea..1288ffb 100644 --- a/nstun/udp.cc +++ b/nstun/udp.cc @@ -20,7 +20,7 @@ namespace nstun { -void udp_destroy_flow(Context* ctx, UdpFlow* flow) { +static void udp_destroy_flow(Context* ctx, UdpFlow* flow) { if (flow->host_fd != -1 && !flow->host_fd_is_listener) { epoll_ctl(ctx->epoll_fd, EPOLL_CTL_DEL, flow->host_fd, nullptr); ctx->flows_by_fd.erase(flow->host_fd); @@ -50,7 +50,7 @@ static void udp_send_packet4(Context* ctx, uint32_t saddr, uint32_t daddr, uint1 udp_hdr* r_udp = reinterpret_cast(header_buf + sizeof(ip4_hdr)); /* IPv4 */ - r_ip->ihl_version = (4 << 4) | (sizeof(ip4_hdr) / 4); + ip4_set_ihl_version(r_ip, 4, sizeof(ip4_hdr) / 4); r_ip->tos = 0; r_ip->tot_len = htons(sizeof(ip4_hdr) + sizeof(udp_hdr) + len); r_ip->id = 0; @@ -138,7 +138,7 @@ static void udp_push_to_guest(Context* ctx, UdpFlow* flow, const uint8_t* data, } } -void handle_host_udp_control(Context* ctx, UdpFlow* flow, uint32_t events) { +static void handle_host_udp_control(Context* ctx, UdpFlow* flow, uint32_t events) { int fd = flow->tcp_fd; flow->last_active = time(NULL); @@ -501,7 +501,7 @@ void handle_udp4(Context* ctx, const ip4_hdr* ip, std::span paylo } } -void handle_host_udp(Context* ctx, UdpFlow* flow) { +static void handle_host_udp(Context* ctx, UdpFlow* flow) { int fd = flow->host_fd; flow->last_active = time(NULL); diff --git a/nstun/udp.h b/nstun/udp.h index 0dcfc4d..4de5540 100644 --- a/nstun/udp.h +++ b/nstun/udp.h @@ -12,10 +12,7 @@ namespace nstun { void handle_udp4(Context* ctx, const ip4_hdr* ip, std::span payload); void handle_udp6(Context* ctx, const ip6_hdr* ip, std::span payload); -void handle_host_udp(Context* ctx, UdpFlow* flow); -void handle_host_udp_control(Context* ctx, UdpFlow* flow, uint32_t events); void handle_host_udp_accept(Context* ctx, int listen_fd, const nstun_rule_t& rule); -void udp_destroy_flow(Context* ctx, UdpFlow* flow); } // namespace nstun