Skip to content

Commit 8fd3ed5

Browse files
committed
nstun: remove dead code, and change some functions to static
1 parent 1984f83 commit 8fd3ed5

9 files changed

Lines changed: 13 additions & 31 deletions

File tree

.clangd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
CompileFlags:
2-
Add: [-std=c++17, -Ikafel/include, -I/usr/include/libnl3, -fno-exceptions, -Wno-unused, -Wno-unused-parameter]
2+
Add: [-std=c++20, -Ikafel/include, -I/usr/include/libnl3, -fno-exceptions, -Wno-unused, -Wno-unused-parameter]

nstun/icmp.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
namespace nstun {
1919

20-
void icmp_destroy_flow(Context* ctx, IcmpFlow* flow) {
20+
static void icmp_destroy_flow(Context* ctx, IcmpFlow* flow) {
2121
if (flow->host_fd != -1) {
2222
epoll_ctl(ctx->epoll_fd, EPOLL_CTL_DEL, flow->host_fd, nullptr);
2323
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
4646
icmp4_hdr* r_icmp = reinterpret_cast<icmp4_hdr*>(header_buf + sizeof(ip4_hdr));
4747

4848
/* IPv4 */
49-
r_ip->ihl_version = (4 << 4) | (sizeof(ip4_hdr) / 4);
49+
ip4_set_ihl_version(r_ip, 4, sizeof(ip4_hdr) / 4);
5050
r_ip->tos = 0;
5151
r_ip->tot_len = htons(frame_len);
5252
r_ip->id = 0;
@@ -389,7 +389,7 @@ void handle_icmp4(Context* ctx, const ip4_hdr* ip, std::span<const uint8_t> payl
389389
}
390390
}
391391

392-
void handle_host_icmp(Context* ctx, IcmpFlow* flow) {
392+
static void handle_host_icmp(Context* ctx, IcmpFlow* flow) {
393393
int fd = flow->host_fd;
394394
flow->last_active = time(NULL);
395395

nstun/icmp.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ namespace nstun {
1212

1313
void handle_icmp4(Context* ctx, const ip4_hdr* ip, std::span<const uint8_t> payload);
1414
void handle_icmp6(Context* ctx, const ip6_hdr* ip, std::span<const uint8_t> payload);
15-
void handle_host_icmp(Context* ctx, IcmpFlow* flow);
16-
void icmp_destroy_flow(Context* ctx, IcmpFlow* flow);
15+
1716
void send_icmp4_error(
1817
Context* ctx, const ip4_hdr* req_ip, size_t tot_len, uint8_t type, uint8_t code);
1918
void send_icmp6_error(

nstun/net_defs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ inline uint8_t ip_version(const uint8_t* ptr) {
107107
return ptr[0] >> 4;
108108
}
109109

110+
110111
inline uint8_t ip4_version(const ip4_hdr* h) {
111112
return h->ihl_version >> 4;
112113
}

nstun/tcp.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ void tcp_send_packet4(Context* ctx, TcpFlow* flow, uint8_t flags, const uint8_t*
9494
uint8_t* r_opt = frame_buf + sizeof(ip4_hdr) + sizeof(tcp_hdr);
9595

9696
/* IPv4 */
97-
r_ip->ihl_version = (4 << 4) | (sizeof(ip4_hdr) / 4);
97+
ip4_set_ihl_version(r_ip, 4, sizeof(ip4_hdr) / 4);
9898
r_ip->tos = 0;
9999
r_ip->tot_len = htons(sizeof(ip4_hdr) + sizeof(tcp_hdr) + opt_len + len);
100100
r_ip->id = 0;

nstun/tun.cc

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,8 @@
1515

1616
namespace nstun {
1717

18-
bool send_to_guest(Context* ctx, const void* data, size_t len) {
19-
ssize_t written = TEMP_FAILURE_RETRY(write(ctx->tap_fd, data, len));
20-
if (written < 0) {
21-
if (errno == EAGAIN || errno == EWOULDBLOCK) {
22-
/* Saturated queue, drop packet normally */
23-
return false;
24-
}
25-
PLOG_E("write(tap_fd) failed");
26-
return false;
27-
}
28-
if ((size_t)written != len) {
29-
LOG_E("write(tap_fd) partial write: %zd of %zu", written, len);
30-
return false;
31-
}
3218

33-
return true;
34-
}
19+
3520

3621
bool send_to_guest_v(
3722
Context* ctx, const void* header, size_t header_len, const void* payload, size_t payload_len) {

nstun/tun.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
namespace nstun {
1010

11-
bool send_to_guest(Context* ctx, const void* data, size_t len);
1211
bool send_to_guest_v(
1312
Context* ctx, const void* header, size_t header_len, const void* payload, size_t payload_len);
1413
void handle_tun_frame(Context* ctx, const uint8_t* buf, size_t len);

nstun/udp.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
namespace nstun {
2222

23-
void udp_destroy_flow(Context* ctx, UdpFlow* flow) {
23+
static void udp_destroy_flow(Context* ctx, UdpFlow* flow) {
2424
if (flow->host_fd != -1 && !flow->host_fd_is_listener) {
2525
epoll_ctl(ctx->epoll_fd, EPOLL_CTL_DEL, flow->host_fd, nullptr);
2626
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
5050
udp_hdr* r_udp = reinterpret_cast<udp_hdr*>(header_buf + sizeof(ip4_hdr));
5151

5252
/* IPv4 */
53-
r_ip->ihl_version = (4 << 4) | (sizeof(ip4_hdr) / 4);
53+
ip4_set_ihl_version(r_ip, 4, sizeof(ip4_hdr) / 4);
5454
r_ip->tos = 0;
5555
r_ip->tot_len = htons(sizeof(ip4_hdr) + sizeof(udp_hdr) + len);
5656
r_ip->id = 0;
@@ -138,7 +138,7 @@ static void udp_push_to_guest(Context* ctx, UdpFlow* flow, const uint8_t* data,
138138
}
139139
}
140140

141-
void handle_host_udp_control(Context* ctx, UdpFlow* flow, uint32_t events) {
141+
static void handle_host_udp_control(Context* ctx, UdpFlow* flow, uint32_t events) {
142142
int fd = flow->tcp_fd;
143143
flow->last_active = time(NULL);
144144

@@ -501,7 +501,7 @@ void handle_udp4(Context* ctx, const ip4_hdr* ip, std::span<const uint8_t> paylo
501501
}
502502
}
503503

504-
void handle_host_udp(Context* ctx, UdpFlow* flow) {
504+
static void handle_host_udp(Context* ctx, UdpFlow* flow) {
505505
int fd = flow->host_fd;
506506
flow->last_active = time(NULL);
507507

nstun/udp.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ namespace nstun {
1212

1313
void handle_udp4(Context* ctx, const ip4_hdr* ip, std::span<const uint8_t> payload);
1414
void handle_udp6(Context* ctx, const ip6_hdr* ip, std::span<const uint8_t> payload);
15-
void handle_host_udp(Context* ctx, UdpFlow* flow);
16-
void handle_host_udp_control(Context* ctx, UdpFlow* flow, uint32_t events);
1715
void handle_host_udp_accept(Context* ctx, int listen_fd, const nstun_rule_t& rule);
18-
void udp_destroy_flow(Context* ctx, UdpFlow* flow);
16+
1917

2018
} // namespace nstun
2119

0 commit comments

Comments
 (0)