nstun: remove dead code, and change some functions to static

This commit is contained in:
Robert Swiecki
2026-04-02 01:31:47 +02:00
parent 1984f83a45
commit 68832ab865
12 changed files with 11 additions and 43 deletions

View File

@@ -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]

View File

@@ -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<icmp4_hdr*>(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<const uint8_t> 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);

View File

@@ -12,8 +12,7 @@ namespace nstun {
void handle_icmp4(Context* ctx, const ip4_hdr* ip, std::span<const uint8_t> payload);
void handle_icmp6(Context* ctx, const ip6_hdr* ip, std::span<const uint8_t> 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(

View File

@@ -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;
}

View File

@@ -1,13 +1,11 @@
#include "policy.h"
#include <string.h>
#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<nsjail::NsJailConfig_UserNet_NstunRule>(
const nsjail::NsJailConfig_UserNet_NstunRule& r, nstun_rule_t* nr);
} /* namespace nstun */

View File

@@ -21,9 +21,6 @@ RuleResult evaluate_rules6(Context* ctx, nstun_direction_t dir, nstun_proto_t pr
template <typename RuleMsg>
RuleParseStatus fill_rule_common(const RuleMsg& r, nstun_rule_t* nr);
} /* namespace nstun */
#endif /* NSTUN_POLICY_H_ */

View File

@@ -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;

View File

@@ -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. */

View File

@@ -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) {

View File

@@ -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);

View File

@@ -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<udp_hdr*>(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<const uint8_t> 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);

View File

@@ -12,10 +12,7 @@ namespace nstun {
void handle_udp4(Context* ctx, const ip4_hdr* ip, std::span<const uint8_t> payload);
void handle_udp6(Context* ctx, const ip6_hdr* ip, std::span<const uint8_t> 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