Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #28

Merged
merged 44 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
b51b626
working on it
jpihl Oct 28, 2024
d24130f
work
jpihl Oct 28, 2024
76bfd91
work
jpihl Oct 28, 2024
86464a0
fix
jpihl Oct 28, 2024
b053779
fix
jpihl Oct 28, 2024
641c82d
fix
jpihl Oct 28, 2024
bfb5e06
fix
jpihl Oct 28, 2024
69fc1fc
fix
jpihl Oct 28, 2024
1578806
fix
jpihl Oct 28, 2024
5050696
fix
jpihl Oct 28, 2024
32c1fcd
fix
jpihl Oct 28, 2024
032bff2
fix
jpihl Oct 28, 2024
45b07ba
fix
jpihl Oct 28, 2024
fe040a1
fix
jpihl Oct 28, 2024
29273a7
use scoped file descriptor
jpihl Oct 28, 2024
c6398df
work
jpihl Oct 30, 2024
9d56dc5
- Super
jpihl Oct 31, 2024
31de78d
fix
jpihl Oct 31, 2024
8c6f842
clean
jpihl Oct 31, 2024
f968547
fix
jpihl Oct 31, 2024
b266dae
work
jpihl Oct 31, 2024
3f04c64
work
jpihl Oct 31, 2024
3686a45
merge tun/tap
jpihl Oct 31, 2024
e933dd9
fix
jpihl Oct 31, 2024
50c7b3a
fix
jpihl Oct 31, 2024
031084c
fix
jpihl Oct 31, 2024
d08c365
fix
jpihl Oct 31, 2024
1ea2514
fix
jpihl Oct 31, 2024
9374667
fix
jpihl Oct 31, 2024
bf427bf
fix
jpihl Oct 31, 2024
eb90b93
fix
jpihl Oct 31, 2024
f468b78
fix
jpihl Oct 31, 2024
bb36465
better errors
jpihl Oct 31, 2024
441b9ab
fix
jpihl Oct 31, 2024
a1de0a4
fix?
jpihl Oct 31, 2024
e248598
work
jpihl Nov 1, 2024
d93f1ee
improve test
jpihl Nov 1, 2024
8b795fe
work
jpihl Nov 1, 2024
233e370
update deps
jpihl Nov 1, 2024
a2c1e06
trigger
jpihl Nov 1, 2024
a218c95
fix style
jpihl Nov 4, 2024
8656852
move test
jpihl Nov 4, 2024
0c326a1
fix windows
jpihl Nov 4, 2024
a8b872f
only build examples and apps on supported platforms
jpihl Nov 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ every change, see the Git log.

Latest
------
* tbd
* Major: Merged the tun_interface and tap_interface classes into a single
class called interface. The interface class can now create both TUN and TAP
interfaces based on the interface_type parameter provided to the create methods
config argument.

15.0.0
------
* Minor: Add initial macos/Darwin support.
* Major: change interface from seperate args to a struct
* Minor: Add initial macos/Darwin support.
* Major: Change interface from seperate args to a struct

14.1.1
------
Expand Down
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ tunnel
.. |Cppcheck| image:: https://github.com/steinwurf/tunnel/actions/workflows/cppcheck.yml/badge.svg
:target: https://github.com/steinwurf/tunnel/actions/workflows/cppcheck.yml

The tunnel lib allows you to create and manipulate TUN interfaces on Linux.
The tunnel lib allows you to create and manipulate TUN and TAP interfaces on Linux and MacOS.
A tun interface is essentially a virtual network interface on the IP (layer 3).
A tap interface is essentially a virtual network interface on the Ethernet (layer 2).


.. contents:: Table of Contents:
Expand Down
210 changes: 100 additions & 110 deletions apps/app/tunnel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,94 +8,61 @@
#include <cstddef>
#include <cstdint>
#include <system_error>
#include <tunnel/tap_interface.hpp>
#include <tunnel/tun_interface.hpp>
#include <tunnel/interface.hpp>

#if defined(PLATFORM_LINUX)
#include <sys/resource.h>
inline void enable_core_dumps()
void enable_core_dumps()
{
// core dumps may be disallowed by the parent of this process; change that
struct rlimit core_limits;
core_limits.rlim_cur = core_limits.rlim_max = RLIM_INFINITY;
setrlimit(RLIMIT_CORE, &core_limits);
}
#else
inline void enable_core_dumps()
void enable_core_dumps()
{
// do nothing
}
#endif

inline auto parse_ip(const std::string& address) -> asio::ip::address
auto to_endpoint(const std::string& endpoint_str) -> asio::ip::udp::endpoint
{
// If there is a port
std::size_t found = address.rfind(':');

if (found != std::string::npos)
{

// Make address and check if it is valid
asio::error_code ec;
asio::ip::address addr =
asio::ip::make_address(address.substr(0, found), ec);
if (ec)
{
throw std::runtime_error("Invalid address: " + address);
}
return addr;
}
else
std::size_t found = endpoint_str.rfind(':');
if (found == std::string::npos)
{
asio::error_code ec;
asio::ip::address addr = asio::ip::make_address(address, ec);
if (ec)
{
throw std::runtime_error("Invalid address: " + address);
}
return addr;
throw std::runtime_error("Invalid endpoint: " + endpoint_str);
}
}

/// Parse port from string, while ignoring the ip.
/// 10.0.0.1 -> error::invalid_argument
/// 10.0.0.1:9900 -> 9900
inline auto parse_port(const std::string& address) -> uint16_t
{
// If there is a port
std::size_t found = address.rfind(':');
if (found != std::string::npos)
{
auto port = address.substr(found + 1, std::string::npos);
return std::atoi(port.c_str());
}
else
asio::error_code ec;
asio::ip::address addr =
asio::ip::make_address(endpoint_str.substr(0, found), ec);
if (ec)
{
return 0;
throw std::runtime_error("Invalid address: " + endpoint_str);
}
}
inline auto
to_udp_endpoint(const std::string& address) -> asio::ip::udp::endpoint
{
asio::ip::address addr = parse_ip(address);
uint16_t port = parse_port(address);

uint16_t port = std::atoi(endpoint_str.substr(found + 1).c_str());

return {addr, port};
}
asio::ip::udp::endpoint peer;

auto rx_udp_tx_tun(asio::ip::udp::socket& rx,
asio::posix::stream_descriptor& tx) -> void
asio::posix::stream_descriptor& tx,
asio::ip::udp::endpoint& peer) -> void
{
static uint8_t buffer[2000];
rx.async_receive_from(asio::buffer(buffer, sizeof(buffer)), peer,
[&](const std::error_code& err, std::size_t len)

Check warning on line 56 in apps/app/tunnel.cpp

View workflow job for this annotation

GitHub Actions / No Assertions / No Assertions

unused parameter ‘err’ [-Wunused-parameter]
{
assert(!err);
tx.write_some(asio::buffer(buffer, len));
rx_udp_tx_tun(rx, tx);
rx_udp_tx_tun(rx, tx, peer);
});
}
auto rx_tun_tx_udp(asio::posix::stream_descriptor& rx,
asio::ip::udp::socket& tx) -> void
asio::ip::udp::socket& tx,
const asio::ip::udp::endpoint& peer) -> void
{
static uint8_t buffer[2000];
rx.async_read_some(asio::buffer(buffer, sizeof(buffer)),
Expand All @@ -105,85 +72,108 @@
{
tx.send_to(asio::buffer(buffer, len), peer);
}
rx_tun_tx_udp(rx, tx);
rx_tun_tx_udp(rx, tx, peer);
});
}

struct EndpointValidator : public CLI::Validator
{
EndpointValidator()
{
name_ = "ENDPOINT";
func_ = [](const std::string& str) -> std::string
{
try
{
to_endpoint(str);
}
catch (const std::exception& e)
{
return e.what();
}
return std::string();
};
}
};

int main(int argc, char** argv)
{
enable_core_dumps();

CLI::App app{"Tunnel back-to-back test app"};

std::string mode = "";
std::string tunnel_address = "";
std::string local_address = "";
std::string remote_address = "";

app.add_option("-m", mode, "Tunnel mode tun/tap")->required();
app.add_option("-a", tunnel_address, "Tunnel address")->required();
app.add_option("-l", local_address, "UDP local address");
app.add_option("-r", remote_address, "UDP remote address");
auto log1 = [](auto, const std::string& message, auto)
{ std::cout << "tunnel_iface: " << message << std::endl; };
CLI::App app{"Tunnel back-to-back test app"};

app.add_option("-m,--mode", mode, "Tunnel mode tun/tap")
->check(CLI::IsMember({"tun",
#if defined(PLATFORM_LINUX) // Tap is only supported on Linux
"tap"
#endif
}))
->required();
app.add_option("-a,--tunnel", tunnel_address,
"Tunnel address, e.g. 11.11.11.11")
->check(CLI::ValidIPV4)
->required();

auto local_option =
app.add_option("-l,--local", local_address, "UDP local endpoint")
->check(EndpointValidator());
auto remote_option =
app.add_option("-r,--remote", remote_address, "UDP remote endpoint")
->check(EndpointValidator());

// Make sure that local and remote address are mutually exclusive
local_option->excludes(remote_option);
remote_option->excludes(local_option);

// Add a validation callback to enforce that either local or remote address
// is specified
app.final_callback(
[&]()
{
if (local_address.empty() && remote_address.empty())
{
throw CLI::RequiredError("Either --local or --remote");
}
});

CLI11_PARSE(app, argc, argv);
assert((!local_address.empty() || !remote_address.empty()) &&
"Either remote or local address must be specified");
assert(!tunnel_address.empty() && "Empty tunnel address");
asio::io_context io;
asio::ip::udp::socket udp_socket(io, asio::ip::udp::v4());
asio::ip::udp::endpoint peer;
if (!local_address.empty())
{
auto ep = to_udp_endpoint(local_address);
udp_socket.bind(ep);
udp_socket.bind(to_endpoint(local_address));
}
else if (!remote_address.empty())
{
auto ep = to_udp_endpoint(remote_address);
peer = ep;
peer = to_endpoint(remote_address);
}

if (mode == "tun")
{
tunnel::tun_interface iface1;
iface1.create({});
iface1.set_log_callback(log1);
iface1.monitor().enable_log();
iface1.set_ipv4(tunnel_address);
iface1.set_ipv4_netmask("255.255.255.0");
iface1.set_mtu(1500);
iface1.up();
auto in_fd = iface1.native_handle();
assert(in_fd > 0 && "Invalid file descriptor");
auto rx = asio::posix::stream_descriptor(io);
rx.assign(in_fd);
rx_tun_tx_udp(rx, udp_socket);
rx_udp_tx_tun(udp_socket, rx);
io.run();
}
else if (mode == "tap")
{
#if defined(PLATFORM_MAC)
std::cerr << "Tap mode is not supported on MacOS" << std::endl;
exit(-1);
#endif
tunnel::tap_interface iface1;
iface1.create({});
iface1.set_log_callback(log1);
iface1.monitor().enable_log();
iface1.set_ipv4(tunnel_address);
iface1.set_ipv4_netmask("255.255.255.0");
iface1.set_mtu(1500);
iface1.up();
auto in_fd = iface1.native_handle();
assert(in_fd > 0 && "Invalid file descriptor");
auto rx = asio::posix::stream_descriptor(io);
rx.assign(in_fd);
rx_tun_tx_udp(rx, udp_socket);
rx_udp_tx_tun(udp_socket, rx);
io.run();
}
auto log1 = [](auto, const std::string& message, auto)
{ std::cout << "tunnel_iface: " << message << std::endl; };
tunnel::interface::config config;
config.interface_type = mode == "tun" ? tunnel::interface::type::tun
: tunnel::interface::type::tap;
tunnel::interface iface1;
iface1.create(config);
iface1.set_log_callback(log1);
iface1.monitor().enable_log();
iface1.set_ipv4(tunnel_address);
iface1.set_ipv4_netmask("255.255.255.0");
iface1.set_mtu(1500);
iface1.up();
auto in_fd = iface1.native_handle();
assert(in_fd > 0 && "Invalid file descriptor");
auto rx = asio::posix::stream_descriptor(io);
rx.assign(in_fd);
rx_tun_tx_udp(rx, udp_socket, peer);
rx_udp_tx_tun(udp_socket, rx, peer);
io.run();

return 0;
}
26 changes: 15 additions & 11 deletions examples/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//
// Distributed under the "BSD License". See the accompanying LICENSE.rst file.

#include <tunnel/tun_interface.hpp>
#include <tunnel/interface.hpp>

#include <cassert>
#include <iostream>
Expand All @@ -14,24 +14,32 @@ int main()
auto log = [](auto, const std::string& message, auto)
{ std::cout << message << std::endl; };

tunnel::tun_interface iface;
tunnel::interface iface;
iface.set_log_callback(log);
iface.monitor().enable_log();

#if defined(PLATFORM_LINUX)
iface.create({"tuniface"});
#elif defined(PLATFORM_MAC)
iface.create({});
#endif

iface.create({tunnel::interface::type::tun, "tuniface"});
if (iface.is_up())
{
iface.set_non_persistent();
return 0;
}
#elif defined(PLATFORM_MAC)
iface.create({});
if (iface.is_up())
{
iface.down();
}
#endif

#if defined(PLATFORM_LINUX)
assert(iface.interface_name() == "tuniface");

assert(iface.is_persistent() == false);
iface.set_persistent();
assert(iface.is_persistent() == true);
iface.set_non_persistent();
#elif defined(PLATFORM_MAC)
const std::string expected_interface_name =
"utun"; // on macOS the interface name is not known in advance and must
Expand All @@ -40,10 +48,6 @@ int main()
std::string::npos);
#endif

assert(iface.is_persistent() == false);
iface.set_persistent();
assert(iface.is_persistent() == true);

iface.set_mtu(1000);
assert(iface.mtu() == 1000);

Expand Down
5 changes: 2 additions & 3 deletions examples/read_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//
// Distributed under the "BSD License". See the accompanying LICENSE.rst file.

#include <tunnel/tun_interface.hpp>
#include <tunnel/interface.hpp>

#include <cassert>
#include <iostream>
Expand All @@ -25,14 +25,13 @@ int main()
auto log = [](auto, const std::string& message, auto)
{ std::cout << message << std::endl; };

tunnel::tun_interface iface;
tunnel::interface iface;
iface.set_log_callback(log);
iface.monitor().enable_log();

iface.create({});
iface.set_ipv4("10.0.0.1");
iface.set_ipv4_netmask("255.255.255.0");
iface.set_persistent();
iface.set_mtu(1500);
iface.up();

Expand Down
Loading
Loading