2023-10-24 13:18:03 +00:00
|
|
|
#include "context_wrapper.h"
|
|
|
|
|
|
|
|
#include "context.hpp"
|
|
|
|
#include "vpn_interface.hpp"
|
|
|
|
|
2021-08-25 23:28:58 +00:00
|
|
|
#include <llarp/config/config.hpp>
|
2022-06-22 21:56:35 +00:00
|
|
|
#include <llarp/constants/apple.hpp>
|
2023-10-24 13:18:03 +00:00
|
|
|
#include <llarp/ev/libuv.hpp>
|
|
|
|
#include <llarp/net/ip_packet.hpp>
|
2021-08-25 23:28:58 +00:00
|
|
|
#include <llarp/util/fs.hpp>
|
2022-08-06 18:12:29 +00:00
|
|
|
#include <llarp/util/logging.hpp>
|
|
|
|
#include <llarp/util/logging/buffer.hpp>
|
2022-07-16 00:41:14 +00:00
|
|
|
#include <llarp/util/logging/callback_sink.hpp>
|
2023-10-24 13:18:03 +00:00
|
|
|
|
|
|
|
// #include <uvw/loop.h>
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstring>
|
2021-08-25 23:28:58 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
struct instance_data
|
|
|
|
{
|
|
|
|
llarp::apple::Context context;
|
|
|
|
std::thread runner;
|
2021-08-30 17:55:33 +00:00
|
|
|
packet_writer_callback packet_writer;
|
|
|
|
start_reading_callback start_reading;
|
2021-08-25 23:28:58 +00:00
|
|
|
|
|
|
|
std::weak_ptr<llarp::apple::VPNInterface> iface;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2022-06-22 21:56:35 +00:00
|
|
|
// Expose this with C linkage so that objective-c can use it
|
|
|
|
extern "C" const uint16_t dns_trampoline_port = llarp::apple::dns_trampoline_port;
|
2021-09-01 18:10:08 +00:00
|
|
|
|
2021-08-25 23:28:58 +00:00
|
|
|
void*
|
2021-08-30 17:55:33 +00:00
|
|
|
llarp_apple_init(llarp_apple_config* appleconf)
|
2021-08-25 23:28:58 +00:00
|
|
|
{
|
2022-08-06 18:12:29 +00:00
|
|
|
llarp::log::clear_sinks();
|
|
|
|
llarp::log::add_sink(std::make_shared<llarp::logging::CallbackSink_mt>(
|
2022-07-16 00:41:14 +00:00
|
|
|
[](const char* msg, void* nslog) { reinterpret_cast<ns_logger_callback>(nslog)(msg); },
|
|
|
|
nullptr,
|
2022-08-06 18:12:29 +00:00
|
|
|
reinterpret_cast<void*>(appleconf->ns_logger)));
|
2022-09-29 13:31:38 +00:00
|
|
|
llarp::logRingBuffer = std::make_shared<llarp::log::RingBufferSink>(100);
|
|
|
|
llarp::log::add_sink(llarp::logRingBuffer, llarp::log::DEFAULT_PATTERN_MONO);
|
2021-08-25 23:28:58 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2021-08-30 17:55:33 +00:00
|
|
|
auto config_dir = fs::u8path(appleconf->config_dir);
|
2021-08-25 23:28:58 +00:00
|
|
|
auto config = std::make_shared<llarp::Config>(config_dir);
|
2021-08-26 14:33:46 +00:00
|
|
|
fs::path config_path = config_dir / "lokinet.ini";
|
|
|
|
if (!fs::exists(config_path))
|
2023-11-16 15:29:27 +00:00
|
|
|
llarp::ensure_config(config_dir, config_path, /*overwrite=*/false, /*asRouter=*/false);
|
|
|
|
config->load(config_path);
|
2021-08-25 23:28:58 +00:00
|
|
|
|
|
|
|
// If no range is specified then go look for a free one, set that in the config, and then return
|
|
|
|
// it to the caller via the char* parameters.
|
2023-11-16 15:29:27 +00:00
|
|
|
auto& range = config->network.if_addr;
|
2021-08-25 23:28:58 +00:00
|
|
|
if (!range.addr.h)
|
|
|
|
{
|
2022-07-11 14:13:31 +00:00
|
|
|
if (auto maybe = llarp::net::Platform::Default_ptr()->FindFreeRange())
|
2021-08-25 23:28:58 +00:00
|
|
|
range = *maybe;
|
|
|
|
else
|
|
|
|
throw std::runtime_error{"Could not find any free IP range"};
|
|
|
|
}
|
|
|
|
auto addr = llarp::net::TruncateV6(range.addr).ToString();
|
|
|
|
auto mask = llarp::net::TruncateV6(range.netmask_bits).ToString();
|
|
|
|
if (addr.size() > 15 || mask.size() > 15)
|
|
|
|
throw std::runtime_error{"Unexpected non-IPv4 tunnel range configured"};
|
2021-09-03 18:35:09 +00:00
|
|
|
std::strncpy(appleconf->tunnel_ipv4_ip, addr.c_str(), sizeof(appleconf->tunnel_ipv4_ip));
|
2021-09-03 19:21:08 +00:00
|
|
|
std::strncpy(
|
|
|
|
appleconf->tunnel_ipv4_netmask, mask.c_str(), sizeof(appleconf->tunnel_ipv4_netmask));
|
2021-09-03 18:35:09 +00:00
|
|
|
|
|
|
|
// TODO: in the future we want to do this properly with our pubkey (see issue #1705), but that's
|
|
|
|
// going to take a bit more work because we currently can't *get* the (usually) ephemeral pubkey
|
|
|
|
// at this stage of lokinet configuration. So for now we just stick our IPv4 address into it
|
|
|
|
// until #1705 gets implemented.
|
2021-09-03 19:21:08 +00:00
|
|
|
llarp::huint128_t ipv6{
|
|
|
|
llarp::uint128_t{0xfd2e'6c6f'6b69'0000, llarp::net::TruncateV6(range.addr).h}};
|
|
|
|
std::strncpy(
|
|
|
|
appleconf->tunnel_ipv6_ip, ipv6.ToString().c_str(), sizeof(appleconf->tunnel_ipv6_ip));
|
2021-09-03 18:35:09 +00:00
|
|
|
appleconf->tunnel_ipv6_prefix = 48;
|
2021-08-31 23:15:48 +00:00
|
|
|
|
|
|
|
appleconf->upstream_dns[0] = '\0';
|
2023-11-16 15:29:27 +00:00
|
|
|
for (auto& upstream : config->dns.upstream_dns)
|
2021-09-01 18:31:45 +00:00
|
|
|
{
|
|
|
|
if (upstream.isIPv4())
|
|
|
|
{
|
2021-08-31 23:15:48 +00:00
|
|
|
std::strcpy(appleconf->upstream_dns, upstream.hostString().c_str());
|
|
|
|
appleconf->upstream_dns_port = upstream.getPort();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-08-25 23:28:58 +00:00
|
|
|
|
2022-06-22 21:56:35 +00:00
|
|
|
#ifdef MACOS_SYSTEM_EXTENSION
|
|
|
|
std::strncpy(
|
|
|
|
appleconf->dns_bind_ip,
|
2022-09-15 21:30:33 +00:00
|
|
|
config->dns.m_bind.front().hostString().c_str(),
|
2022-06-22 21:56:35 +00:00
|
|
|
sizeof(appleconf->dns_bind_ip));
|
|
|
|
#endif
|
2021-08-25 23:28:58 +00:00
|
|
|
|
|
|
|
// If no explicit bootstrap then set the system default one included with the app bundle
|
|
|
|
if (config->bootstrap.files.empty())
|
2021-08-30 17:55:33 +00:00
|
|
|
config->bootstrap.files.push_back(fs::u8path(appleconf->default_bootstrap));
|
2021-08-25 23:28:58 +00:00
|
|
|
|
|
|
|
auto inst = std::make_unique<instance_data>();
|
|
|
|
inst->context.Configure(std::move(config));
|
2021-08-30 17:55:33 +00:00
|
|
|
inst->context.route_callbacks = appleconf->route_callbacks;
|
|
|
|
|
|
|
|
inst->packet_writer = appleconf->packet_writer;
|
|
|
|
inst->start_reading = appleconf->start_reading;
|
|
|
|
|
2021-08-25 23:28:58 +00:00
|
|
|
return inst.release();
|
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
2021-09-24 17:03:09 +00:00
|
|
|
llarp::LogError("Failed to initialize lokinet from config: ", e.what());
|
2021-08-25 23:28:58 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2021-09-01 18:31:45 +00:00
|
|
|
llarp_apple_start(void* lokinet, void* callback_context)
|
2021-08-25 23:28:58 +00:00
|
|
|
{
|
|
|
|
auto* inst = static_cast<instance_data*>(lokinet);
|
2021-08-30 17:55:33 +00:00
|
|
|
|
|
|
|
inst->context.callback_context = callback_context;
|
|
|
|
|
2021-09-01 18:31:45 +00:00
|
|
|
inst->context.m_PacketWriter = [inst, callback_context](int af_family, void* data, size_t size) {
|
|
|
|
inst->packet_writer(af_family, data, size, callback_context);
|
|
|
|
return true;
|
2021-08-25 23:28:58 +00:00
|
|
|
};
|
|
|
|
|
2021-09-01 18:31:45 +00:00
|
|
|
inst->context.m_OnReadable = [inst, callback_context](llarp::apple::VPNInterface& iface) {
|
|
|
|
inst->iface = iface.weak_from_this();
|
|
|
|
inst->start_reading(callback_context);
|
|
|
|
};
|
2021-08-25 23:28:58 +00:00
|
|
|
|
|
|
|
std::promise<void> result;
|
|
|
|
inst->runner = std::thread{[inst, &result] {
|
|
|
|
const llarp::RuntimeOptions opts{};
|
|
|
|
try
|
|
|
|
{
|
|
|
|
inst->context.Setup(opts);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
result.set_exception(std::current_exception());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
result.set_value();
|
|
|
|
inst->context.Run(opts);
|
|
|
|
}};
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
result.get_future().get();
|
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
2021-09-24 17:03:09 +00:00
|
|
|
llarp::LogError("Failed to initialize lokinet: ", e.what());
|
2021-08-25 23:28:58 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-31 23:15:48 +00:00
|
|
|
uv_loop_t*
|
|
|
|
llarp_apple_get_uv_loop(void* lokinet)
|
|
|
|
{
|
|
|
|
auto& inst = *static_cast<instance_data*>(lokinet);
|
|
|
|
auto uvw = inst.context.loop->MaybeGetUVWLoop();
|
|
|
|
assert(uvw);
|
|
|
|
return uvw->raw();
|
|
|
|
}
|
|
|
|
|
2021-08-25 23:28:58 +00:00
|
|
|
int
|
2022-06-22 21:56:35 +00:00
|
|
|
llarp_apple_incoming(void* lokinet, const llarp_incoming_packet* packets, size_t size)
|
2021-08-25 23:28:58 +00:00
|
|
|
{
|
|
|
|
auto& inst = *static_cast<instance_data*>(lokinet);
|
|
|
|
|
|
|
|
auto iface = inst.iface.lock();
|
|
|
|
if (!iface)
|
2022-06-22 21:56:35 +00:00
|
|
|
return -1;
|
2021-08-25 23:28:58 +00:00
|
|
|
|
2022-06-22 21:56:35 +00:00
|
|
|
int count = 0;
|
|
|
|
for (size_t i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
llarp_buffer_t buf{static_cast<const uint8_t*>(packets[i].bytes), packets[i].size};
|
|
|
|
if (iface->OfferReadPacket(buf))
|
|
|
|
count++;
|
|
|
|
else
|
|
|
|
llarp::LogError("invalid IP packet: ", llarp::buffer_printer(buf));
|
|
|
|
}
|
2021-08-25 23:28:58 +00:00
|
|
|
|
2022-06-22 21:56:35 +00:00
|
|
|
iface->MaybeWakeUpperLayers();
|
|
|
|
return count;
|
2021-08-25 23:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
llarp_apple_shutdown(void* lokinet)
|
|
|
|
{
|
|
|
|
auto* inst = static_cast<instance_data*>(lokinet);
|
|
|
|
|
|
|
|
inst->context.CloseAsync();
|
|
|
|
inst->context.Wait();
|
|
|
|
inst->runner.join();
|
|
|
|
delete inst;
|
|
|
|
}
|