Skip to content

Commit

Permalink
Added UDP support
Browse files Browse the repository at this point in the history
  • Loading branch information
gxosty committed Mar 6, 2024
1 parent e2e77f3 commit e086f9f
Show file tree
Hide file tree
Showing 21 changed files with 439 additions and 29 deletions.
7 changes: 7 additions & 0 deletions app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,25 @@ add_library(tunmode SHARED
main.cxx

src/tunmode/tunmode.cxx

src/tunmode/common/inbuffer.cxx
src/tunmode/common/buffer.cxx
src/tunmode/common/packet.cxx
src/tunmode/common/utils.cxx

src/tunmode/session/session.cxx
src/tunmode/session/tcpsession.cxx
src/tunmode/session/udpsession.cxx

src/tunmode/manager/sessionmanager.cxx
src/tunmode/manager/tcpmanager.cxx
src/tunmode/manager/udpmanager.cxx

src/tunmode/socket/socket.cxx
src/tunmode/socket/sessionsocket.cxx
src/tunmode/socket/tunsocket.cxx
src/tunmode/socket/tcpsocket.cxx
src/tunmode/socket/udpsocket.cxx

src/RawSocket/CheckSum.cpp
)
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/cpp/include/tunmode/common/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ namespace tunmode::utils
uint64_t make_udp_id(Packet* packet);

void build_tcp_packet(Packet* packet);
void build_udp_packet(Packet* packet);

void point_headers_tcp(const Packet* packet, ip** ip_header, tcphdr** tcp_header);
void point_headers_udp(const Packet* packet, ip** ip_header, udphdr** udp_header);

void finalize_packet_tcp(Packet* packet);
void finalize_packet_udp(Packet* packet);

void protect_socket(int skt);

void print_packet_tcp(Packet* packet);
void print_packet_udp(Packet* packet);
}
22 changes: 22 additions & 0 deletions app/src/main/cpp/include/tunmode/manager/udpmanager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "../common/packet.hpp"
#include "sessionmanager.hpp"

namespace tunmode
{
class UDPSession;

class UDPManager : public SessionManager
{
public:
UDPManager();

void handle_packet(const Packet& packet) override;

private:
Session* add(uintptr_t id) override;

friend class UDPSession;
};
}
2 changes: 0 additions & 2 deletions app/src/main/cpp/include/tunmode/session/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ namespace tunmode
class Session
{
public:
static in_addr iface_addr;

Session(uint64_t id);
virtual ~Session();

Expand Down
26 changes: 26 additions & 0 deletions app/src/main/cpp/include/tunmode/session/udpsession.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include "session.hpp"
#include "../common/packet.hpp"
#include "../common/buffer.hpp"

#include <cstdint>

namespace tunmode
{
class UDPManager;

class UDPSession : public Session
{
public:
UDPSession(UDPManager* manager, uint64_t id);

private:
UDPManager* manager;
uint32_t poll_timeout;

int poll(struct pollfd fds[2]) override;
void loop() override;
void _loop();
};
}
2 changes: 2 additions & 0 deletions app/src/main/cpp/include/tunmode/socket/sessionsocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace tunmode
SessionSocket();
virtual ~SessionSocket();

virtual size_t send_tun(Packet& packet); // send to tun iface

virtual size_t send(const Packet& packet);
virtual size_t send(const Buffer& buffer) = 0;
virtual size_t recv(Packet& packet);
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/cpp/include/tunmode/socket/tcpsocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace tunmode

int connect(Socket* skt);

size_t send_tun(Packet& packet);
size_t send_tun(Packet& packet) override;
size_t send(const Buffer& buffer) override;
size_t recv(Buffer& buffer) override;

Expand Down
36 changes: 36 additions & 0 deletions app/src/main/cpp/include/tunmode/socket/udpsocket.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include "socket.hpp"
#include "sessionsocket.hpp"
#include "../common/packet.hpp"
#include "../common/buffer.hpp"

#include <netinet/in.h>
#include <cstdint>

namespace tunmode
{
class UDPSocket : public SessionSocket
{
public:
UDPSocket();
~UDPSocket() override;

void init(Socket* skt);

size_t send_tun(Packet& packet) override;
size_t send(const Buffer& buffer) override;
size_t recv(Buffer& buffer) override;

void operator<<(const Buffer& buffer) override;
void operator>>(Buffer& buffer) override;

void close();

private:
in_addr client_addr;
u_short client_port;
in_addr server_addr;
u_short server_port;
};
}
2 changes: 1 addition & 1 deletion app/src/main/cpp/include/tunmode/tunmode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace tunmode
namespace params
{
extern TunSocket tun;
extern in_addr tun_addr;
extern in_addr dns_address;
extern jobject TunModeService_object;
extern std::atomic<bool> stop_flag;
}
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/cpp/main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

extern "C"
JNIEXPORT void JNICALL
Java_git_gxosty_tunmode_interceptor_services_TunModeService_tunnelOpenNative(JNIEnv* env, jclass cls, int fd, jstring dns_address, jstring network_interface)
Java_git_gxosty_tunmode_interceptor_services_TunModeService_tunnelOpenNative(JNIEnv* env, jclass cls, int fd, jstring dns_address)
{
tunmode::params::dns_address.s_addr = 0;
tunmode::params::tun = fd;
// tunmode::params::tun_addr = tun_address;
tunmode::params::tun_addr.s_addr = 0;
tunmode::open_tunnel();
}

Expand Down
28 changes: 23 additions & 5 deletions app/src/main/cpp/src/tunmode/common/packet.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <netinet/ip.h>
#include <netinet/tcp.h>

#include <tunmode/definitions.hpp>

namespace tunmode
{
Packet::Packet() : Buffer()
Expand All @@ -24,14 +26,30 @@ namespace tunmode

InBuffer Packet::get_data() const
{
ip* ip_header;
tcphdr* tcp_header;
if (this->protocol == TUNMODE_PROTOCOL_TCP)
{
ip* ip_header;
tcphdr* tcp_header;

utils::point_headers_tcp((Packet*)this, &ip_header, &tcp_header);

uintptr_t data_start = (uintptr_t)tcp_header + tcp_header->th_off * 4;

return InBuffer((void*)data_start, (size_t)(this->size - (data_start - (uintptr_t)this->buffer)));
}
else if (this->protocol == TUNMODE_PROTOCOL_UDP)
{
ip* ip_header;
udphdr* udp_header;

utils::point_headers_udp((Packet*)this, &ip_header, &udp_header);

utils::point_headers_tcp((Packet*)this, &ip_header, &tcp_header);
uintptr_t data_start = (uintptr_t)(udp_header + 1);

uintptr_t data_start = (uintptr_t)tcp_header + tcp_header->th_off * 4;
return InBuffer((void*)data_start, (size_t)(this->size - (data_start - (uintptr_t)this->buffer)));
}

return InBuffer((void*)data_start, (size_t)(this->size - (data_start - (uintptr_t)this->buffer)));
return InBuffer();
}

void Packet::set_id(uint64_t id)
Expand Down
48 changes: 45 additions & 3 deletions app/src/main/cpp/src/tunmode/common/utils.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ namespace tunmode::utils
ip_header->ip_ttl = 128;
ip_header->ip_p = TUNMODE_PROTOCOL_TCP;

make_tcp_id(packet);

tcp_header->th_seq = 0;
tcp_header->th_ack = 0;
tcp_header->th_off = 5;
Expand All @@ -55,12 +53,37 @@ namespace tunmode::utils
packet->set_size(40);
}

void inline point_headers_tcp(const Packet* packet, ip** ip_header, tcphdr** tcp_header)
void build_udp_packet(Packet* packet)
{
struct ip* ip_header = (struct ip*)packet->get_buffer();
struct udphdr* udp_header = (struct udphdr*)((uintptr_t)packet->get_buffer() + 20);

ip_header->ip_hl = 5;
ip_header->ip_v = 4;
ip_header->ip_len = htons(28);
ip_header->ip_id = (uint16_t)rand();
ip_header->ip_off = 0;
ip_header->ip_ttl = 128;
ip_header->ip_p = TUNMODE_PROTOCOL_UDP;

udp_header->uh_ulen = 0;
udp_header->uh_sum = 0;

packet->set_size(28);
}

void point_headers_tcp(const Packet* packet, ip** ip_header, tcphdr** tcp_header)
{
*ip_header = (ip*)(packet->get_buffer());
*tcp_header = (tcphdr*)(ip_header[0]->ip_hl * 4 + (uintptr_t)packet->get_buffer());
}

void point_headers_udp(const Packet* packet, ip** ip_header, udphdr** udp_header)
{
*ip_header = (ip*)(packet->get_buffer());
*udp_header = (udphdr*)(ip_header[0]->ip_hl * 4 + (uintptr_t)packet->get_buffer());
}

void finalize_packet_tcp(Packet* packet)
{
ip* ip_header;
Expand All @@ -72,6 +95,20 @@ namespace tunmode::utils
tcp_header->th_sum = cksumTcp((struct iphdr*)ip_header, tcp_header);
}

void finalize_packet_udp(Packet* packet)
{
ip* ip_header;
udphdr* udp_header;
point_headers_udp(packet, &ip_header, &udp_header);

InBuffer in_buffer = packet->get_data();

ip_header->ip_len = htons(packet->get_size());
ip_header->ip_sum = cksumIp((struct iphdr*)ip_header);
udp_header->uh_ulen = htons(in_buffer.get_size() + 8);
udp_header->uh_sum = cksumUdp((struct iphdr*)ip_header, udp_header);
}

/* Call only in one thread */
void protect_socket(int skt)
{
Expand Down Expand Up @@ -131,4 +168,9 @@ namespace tunmode::utils
LOGD_("--------------------");
#endif
}

void print_packet_udp(Packet* packet)
{

}
}
23 changes: 23 additions & 0 deletions app/src/main/cpp/src/tunmode/manager/udpmanager.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <tunmode/manager/udpmanager.hpp>
#include <tunmode/session/udpsession.hpp>
#include <tunmode/common/utils.hpp>

namespace tunmode
{
UDPManager::UDPManager() : SessionManager() {}

void UDPManager::handle_packet(const Packet& packet)
{
UDPSession* session = reinterpret_cast<UDPSession*>(this->get_or_add(packet.get_id()));
*(session->get_client_socket()) < packet;
}

Session* UDPManager::add(uint64_t id)
{
UDPSession* session = new UDPSession(this, id);
utils::protect_socket(session->get_server_socket()->get_socket());
this->sessions.insert({id, session});

return session;
}
}
2 changes: 0 additions & 2 deletions app/src/main/cpp/src/tunmode/session/session.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

namespace tunmode
{
in_addr Session::iface_addr = in_addr{0};

Session::Session(uint64_t id)
{
LOGD_("Session::Session");
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/cpp/src/tunmode/session/tcpsession.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,13 @@ namespace tunmode
Socket*& sv_socket = this->server_socket;

LOGD_("Calling connect");
cl_socket->connect(sv_socket);
LOGD_("Connect end");
if (cl_socket->connect(sv_socket))
{
LOGE_("Connection failed");
cl_socket->close();
sv_socket->close();
return;
}

struct pollfd fds[2];

Expand Down
Loading

0 comments on commit e086f9f

Please sign in to comment.