2021-09-23 18:01:04 +00:00
|
|
|
#include <lokinet.h>
|
|
|
|
#include <llarp.hpp>
|
2021-04-02 15:10:37 +00:00
|
|
|
#include <llarp/config/config.hpp>
|
|
|
|
#include <llarp/crypto/crypto_libsodium.hpp>
|
2021-03-08 19:19:20 +00:00
|
|
|
|
2021-04-01 11:13:39 +00:00
|
|
|
#include <llarp/router/abstractrouter.hpp>
|
|
|
|
#include <llarp/service/context.hpp>
|
|
|
|
#include <llarp/quic/tunnel.hpp>
|
2021-04-02 15:10:37 +00:00
|
|
|
#include <llarp/nodedb.hpp>
|
2021-04-01 11:13:39 +00:00
|
|
|
|
2022-07-16 00:41:14 +00:00
|
|
|
#include <llarp/util/logging.hpp>
|
2021-09-20 22:07:47 +00:00
|
|
|
#include <llarp/util/logging/buffer.hpp>
|
2022-07-16 00:41:14 +00:00
|
|
|
#include <llarp/util/logging/callback_sink.hpp>
|
2021-09-20 22:07:47 +00:00
|
|
|
|
2022-02-17 18:44:31 +00:00
|
|
|
#include <oxenc/base32z.h>
|
2021-09-21 20:04:05 +00:00
|
|
|
|
2021-04-01 11:13:39 +00:00
|
|
|
#include <mutex>
|
2021-09-23 18:01:04 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <chrono>
|
2022-07-19 16:19:35 +00:00
|
|
|
#include <stdexcept>
|
2021-04-01 11:13:39 +00:00
|
|
|
|
2021-04-19 11:19:07 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define EHOSTDOWN ENETDOWN
|
|
|
|
#endif
|
|
|
|
|
2021-04-02 15:10:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
struct Context : public llarp::Context
|
|
|
|
{
|
|
|
|
using llarp::Context::Context;
|
|
|
|
|
|
|
|
std::shared_ptr<llarp::NodeDB>
|
|
|
|
makeNodeDB() override
|
|
|
|
{
|
|
|
|
return std::make_shared<llarp::NodeDB>();
|
|
|
|
}
|
|
|
|
};
|
2021-09-23 18:01:04 +00:00
|
|
|
|
|
|
|
struct UDPFlow
|
|
|
|
{
|
|
|
|
using Clock_t = std::chrono::steady_clock;
|
|
|
|
void* m_FlowUserData;
|
|
|
|
std::chrono::seconds m_FlowTimeout;
|
|
|
|
std::chrono::time_point<Clock_t> m_ExpiresAt;
|
|
|
|
lokinet_udp_flowinfo m_FlowInfo;
|
|
|
|
lokinet_udp_flow_recv_func m_Recv;
|
|
|
|
|
|
|
|
/// call timeout hook for this flow
|
|
|
|
void
|
|
|
|
TimedOut(lokinet_udp_flow_timeout_func timeout)
|
|
|
|
{
|
|
|
|
timeout(&m_FlowInfo, m_FlowUserData);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// mark this flow as active
|
|
|
|
/// updates the expires at timestamp
|
|
|
|
void
|
|
|
|
MarkActive()
|
|
|
|
{
|
|
|
|
m_ExpiresAt = Clock_t::now() + m_FlowTimeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// returns true if we think this flow is expired
|
|
|
|
bool
|
|
|
|
IsExpired() const
|
|
|
|
{
|
|
|
|
return Clock_t::now() >= m_ExpiresAt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HandlePacket(const llarp::net::IPPacket& pkt)
|
|
|
|
{
|
|
|
|
if (auto maybe = pkt.L4Data())
|
|
|
|
{
|
|
|
|
MarkActive();
|
|
|
|
m_Recv(&m_FlowInfo, maybe->first, maybe->second, m_FlowUserData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct UDPHandler
|
|
|
|
{
|
|
|
|
using AddressVariant_t = llarp::vpn::AddressVariant_t;
|
|
|
|
int m_SocketID;
|
|
|
|
llarp::nuint16_t m_LocalPort;
|
|
|
|
lokinet_udp_flow_filter m_Filter;
|
|
|
|
lokinet_udp_flow_recv_func m_Recv;
|
|
|
|
lokinet_udp_flow_timeout_func m_Timeout;
|
|
|
|
void* m_User;
|
|
|
|
std::weak_ptr<llarp::service::Endpoint> m_Endpoint;
|
|
|
|
|
|
|
|
std::unordered_map<AddressVariant_t, UDPFlow> m_Flows;
|
|
|
|
|
|
|
|
std::mutex m_Access;
|
|
|
|
|
|
|
|
explicit UDPHandler(
|
|
|
|
int socketid,
|
|
|
|
llarp::nuint16_t localport,
|
|
|
|
lokinet_udp_flow_filter filter,
|
|
|
|
lokinet_udp_flow_recv_func recv,
|
|
|
|
lokinet_udp_flow_timeout_func timeout,
|
|
|
|
void* user,
|
|
|
|
std::weak_ptr<llarp::service::Endpoint> ep)
|
|
|
|
: m_SocketID{socketid}
|
|
|
|
, m_LocalPort{localport}
|
|
|
|
, m_Filter{filter}
|
|
|
|
, m_Recv{recv}
|
|
|
|
, m_Timeout{timeout}
|
|
|
|
, m_User{user}
|
2022-07-16 00:41:14 +00:00
|
|
|
, m_Endpoint{std::move(ep)}
|
2021-09-23 18:01:04 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
void
|
|
|
|
KillAllFlows()
|
|
|
|
{
|
|
|
|
std::unique_lock lock{m_Access};
|
|
|
|
for (auto& item : m_Flows)
|
|
|
|
{
|
|
|
|
item.second.TimedOut(m_Timeout);
|
|
|
|
}
|
|
|
|
m_Flows.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AddFlow(
|
|
|
|
const AddressVariant_t& from,
|
|
|
|
const lokinet_udp_flowinfo& flow_addr,
|
|
|
|
void* flow_userdata,
|
2021-10-13 12:54:19 +00:00
|
|
|
int flow_timeoutseconds,
|
|
|
|
std::optional<llarp::net::IPPacket> firstPacket = std::nullopt)
|
2021-09-23 18:01:04 +00:00
|
|
|
{
|
|
|
|
std::unique_lock lock{m_Access};
|
|
|
|
auto& flow = m_Flows[from];
|
|
|
|
flow.m_FlowInfo = flow_addr;
|
|
|
|
flow.m_FlowTimeout = std::chrono::seconds{flow_timeoutseconds};
|
|
|
|
flow.m_FlowUserData = flow_userdata;
|
2021-09-30 17:17:06 +00:00
|
|
|
flow.m_Recv = m_Recv;
|
2021-10-13 12:54:19 +00:00
|
|
|
if (firstPacket)
|
|
|
|
flow.HandlePacket(*firstPacket);
|
2021-09-23 18:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ExpireOldFlows()
|
|
|
|
{
|
|
|
|
std::unique_lock lock{m_Access};
|
|
|
|
for (auto itr = m_Flows.begin(); itr != m_Flows.end();)
|
|
|
|
{
|
|
|
|
if (itr->second.IsExpired())
|
|
|
|
{
|
|
|
|
itr->second.TimedOut(m_Timeout);
|
|
|
|
itr = m_Flows.erase(itr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HandlePacketFrom(AddressVariant_t from, llarp::net::IPPacket pkt)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::unique_lock lock{m_Access};
|
2021-10-13 12:54:19 +00:00
|
|
|
if (m_Flows.count(from))
|
2021-09-23 18:01:04 +00:00
|
|
|
{
|
2021-10-13 12:54:19 +00:00
|
|
|
m_Flows[from].HandlePacket(pkt);
|
2021-09-23 18:01:04 +00:00
|
|
|
return;
|
2021-10-13 12:54:19 +00:00
|
|
|
}
|
2021-09-23 18:01:04 +00:00
|
|
|
}
|
2021-10-13 12:54:19 +00:00
|
|
|
lokinet_udp_flowinfo flow_addr{};
|
|
|
|
// set flow remote address
|
2022-02-06 18:27:19 +00:00
|
|
|
std::string addrstr = var::visit([](auto&& from) { return from.ToString(); }, from);
|
2021-10-13 12:54:19 +00:00
|
|
|
|
|
|
|
std::copy_n(
|
|
|
|
addrstr.data(),
|
|
|
|
std::min(addrstr.size(), sizeof(flow_addr.remote_host)),
|
|
|
|
flow_addr.remote_host);
|
|
|
|
// set socket id
|
|
|
|
flow_addr.socket_id = m_SocketID;
|
|
|
|
// get source port
|
|
|
|
if (const auto srcport = pkt.SrcPort())
|
2021-09-23 18:01:04 +00:00
|
|
|
{
|
2021-10-13 12:54:19 +00:00
|
|
|
flow_addr.remote_port = ToHost(*srcport).h;
|
2021-09-23 18:01:04 +00:00
|
|
|
}
|
2021-10-13 12:54:19 +00:00
|
|
|
else
|
|
|
|
return; // invalid data so we bail
|
|
|
|
void* flow_userdata = nullptr;
|
|
|
|
int flow_timeoutseconds{};
|
|
|
|
// got a new flow, let's check if we want it
|
|
|
|
if (m_Filter(m_User, &flow_addr, &flow_userdata, &flow_timeoutseconds))
|
|
|
|
return;
|
|
|
|
AddFlow(from, flow_addr, flow_userdata, flow_timeoutseconds, pkt);
|
2021-09-23 18:01:04 +00:00
|
|
|
}
|
|
|
|
};
|
2021-04-02 15:10:37 +00:00
|
|
|
} // namespace
|
|
|
|
|
2021-03-08 19:19:20 +00:00
|
|
|
struct lokinet_context
|
|
|
|
{
|
2021-04-01 11:13:39 +00:00
|
|
|
std::mutex m_access;
|
|
|
|
|
2022-07-16 00:41:14 +00:00
|
|
|
std::shared_ptr<llarp::Context> impl = std::make_shared<Context>();
|
|
|
|
std::shared_ptr<llarp::Config> config = llarp::Config::EmbeddedConfig();
|
2021-03-08 19:19:20 +00:00
|
|
|
|
|
|
|
std::unique_ptr<std::thread> runner;
|
|
|
|
|
2022-07-16 00:41:14 +00:00
|
|
|
int _socket_id = 0;
|
2021-03-08 19:19:20 +00:00
|
|
|
|
|
|
|
~lokinet_context()
|
|
|
|
{
|
|
|
|
if (runner)
|
|
|
|
runner->join();
|
|
|
|
}
|
2021-04-01 11:13:39 +00:00
|
|
|
|
2021-09-23 18:01:04 +00:00
|
|
|
int
|
|
|
|
next_socket_id()
|
|
|
|
{
|
|
|
|
int id = ++_socket_id;
|
|
|
|
// handle overflow
|
|
|
|
if (id < 0)
|
|
|
|
{
|
|
|
|
_socket_id = 0;
|
|
|
|
id = ++_socket_id;
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// make a udp handler and hold onto it
|
|
|
|
/// return its id
|
2021-09-23 18:31:47 +00:00
|
|
|
[[nodiscard]] std::optional<int>
|
2021-09-23 18:01:04 +00:00
|
|
|
make_udp_handler(
|
|
|
|
const std::shared_ptr<llarp::service::Endpoint>& ep,
|
2022-04-07 20:44:23 +00:00
|
|
|
llarp::net::port_t exposePort,
|
2021-09-23 18:01:04 +00:00
|
|
|
lokinet_udp_flow_filter filter,
|
|
|
|
lokinet_udp_flow_recv_func recv,
|
|
|
|
lokinet_udp_flow_timeout_func timeout,
|
|
|
|
void* user)
|
|
|
|
{
|
|
|
|
if (udp_sockets.empty())
|
|
|
|
{
|
|
|
|
// start udp flow expiration timer
|
|
|
|
impl->router->loop()->call_every(1s, std::make_shared<int>(0), [this]() {
|
|
|
|
std::unique_lock lock{m_access};
|
|
|
|
for (auto& item : udp_sockets)
|
|
|
|
{
|
|
|
|
item.second->ExpireOldFlows();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-04-07 20:44:23 +00:00
|
|
|
std::weak_ptr<llarp::service::Endpoint> weak{ep};
|
2021-09-23 18:31:47 +00:00
|
|
|
auto udp = std::make_shared<UDPHandler>(
|
2022-04-07 20:44:23 +00:00
|
|
|
next_socket_id(), exposePort, filter, recv, timeout, user, weak);
|
2021-09-23 18:01:04 +00:00
|
|
|
auto id = udp->m_SocketID;
|
2021-09-23 18:31:47 +00:00
|
|
|
std::promise<bool> result;
|
|
|
|
|
2021-09-24 13:35:33 +00:00
|
|
|
impl->router->loop()->call([ep, &result, udp, exposePort]() {
|
|
|
|
if (auto pkt = ep->EgresPacketRouter())
|
2021-09-23 18:31:47 +00:00
|
|
|
{
|
2022-04-07 20:44:23 +00:00
|
|
|
pkt->AddUDPHandler(llarp::net::ToHost(exposePort), [udp](auto from, auto pkt) {
|
2021-09-30 17:17:06 +00:00
|
|
|
udp->HandlePacketFrom(std::move(from), std::move(pkt));
|
2021-09-23 18:31:47 +00:00
|
|
|
});
|
|
|
|
result.set_value(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result.set_value(false);
|
2021-09-23 18:01:04 +00:00
|
|
|
});
|
2021-09-23 18:31:47 +00:00
|
|
|
|
|
|
|
if (result.get_future().get())
|
|
|
|
{
|
|
|
|
udp_sockets[udp->m_SocketID] = std::move(udp);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
2021-09-23 18:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
remove_udp_handler(int socket_id)
|
|
|
|
{
|
2021-09-23 18:31:47 +00:00
|
|
|
std::shared_ptr<UDPHandler> udp;
|
2021-09-23 18:01:04 +00:00
|
|
|
{
|
|
|
|
std::unique_lock lock{m_access};
|
|
|
|
if (auto itr = udp_sockets.find(socket_id); itr != udp_sockets.end())
|
|
|
|
{
|
|
|
|
udp = std::move(itr->second);
|
|
|
|
udp_sockets.erase(itr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (udp)
|
2021-09-23 18:31:47 +00:00
|
|
|
{
|
2021-09-23 18:01:04 +00:00
|
|
|
udp->KillAllFlows();
|
2021-09-23 18:31:47 +00:00
|
|
|
// remove packet handler
|
2021-09-24 13:35:33 +00:00
|
|
|
impl->router->loop()->call(
|
|
|
|
[ep = udp->m_Endpoint.lock(), localport = llarp::ToHost(udp->m_LocalPort)]() {
|
|
|
|
if (auto pkt = ep->EgresPacketRouter())
|
|
|
|
pkt->RemoveUDPHandler(localport);
|
|
|
|
});
|
2021-09-23 18:31:47 +00:00
|
|
|
}
|
2021-09-23 18:01:04 +00:00
|
|
|
}
|
|
|
|
|
2021-04-01 11:13:39 +00:00
|
|
|
/// acquire mutex for accessing this context
|
|
|
|
[[nodiscard]] auto
|
|
|
|
acquire()
|
|
|
|
{
|
|
|
|
return std::unique_lock{m_access};
|
|
|
|
}
|
|
|
|
|
2021-04-02 16:08:06 +00:00
|
|
|
[[nodiscard]] auto
|
2021-04-03 12:19:46 +00:00
|
|
|
endpoint(std::string name = "default") const
|
2021-04-02 16:08:06 +00:00
|
|
|
{
|
2021-04-03 12:19:46 +00:00
|
|
|
return impl->router->hiddenServiceContext().GetEndpointByName(name);
|
2021-04-02 16:08:06 +00:00
|
|
|
}
|
|
|
|
|
2021-04-01 11:13:39 +00:00
|
|
|
std::unordered_map<int, bool> streams;
|
2021-09-23 18:31:47 +00:00
|
|
|
std::unordered_map<int, std::shared_ptr<UDPHandler>> udp_sockets;
|
2021-04-01 11:13:39 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
inbound_stream(int id)
|
|
|
|
{
|
|
|
|
streams[id] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
outbound_stream(int id)
|
|
|
|
{
|
|
|
|
streams[id] = false;
|
|
|
|
}
|
2021-03-08 19:19:20 +00:00
|
|
|
};
|
|
|
|
|
2021-04-01 11:13:39 +00:00
|
|
|
namespace
|
|
|
|
{
|
2021-04-01 16:51:02 +00:00
|
|
|
void
|
|
|
|
stream_error(lokinet_stream_result* result, int err)
|
2021-04-01 11:13:39 +00:00
|
|
|
{
|
2021-04-02 15:10:37 +00:00
|
|
|
std::memset(result, 0, sizeof(lokinet_stream_result));
|
2021-04-01 16:51:02 +00:00
|
|
|
result->error = err;
|
2021-04-01 11:13:39 +00:00
|
|
|
}
|
|
|
|
|
2021-04-01 16:51:02 +00:00
|
|
|
void
|
|
|
|
stream_okay(lokinet_stream_result* result, std::string host, int port, int stream_id)
|
2021-04-01 11:13:39 +00:00
|
|
|
{
|
2021-04-01 16:51:02 +00:00
|
|
|
stream_error(result, 0);
|
2021-04-01 11:13:39 +00:00
|
|
|
std::copy_n(
|
|
|
|
host.c_str(),
|
|
|
|
std::min(host.size(), sizeof(result->local_address) - 1),
|
|
|
|
result->local_address);
|
|
|
|
result->local_port = port;
|
|
|
|
result->stream_id = stream_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<std::string, int>
|
|
|
|
split_host_port(std::string data, std::string proto = "tcp")
|
|
|
|
{
|
|
|
|
std::string host, portStr;
|
|
|
|
if (auto pos = data.find(":"); pos != std::string::npos)
|
|
|
|
{
|
|
|
|
host = data.substr(0, pos);
|
|
|
|
portStr = data.substr(pos + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw EINVAL;
|
|
|
|
|
|
|
|
if (auto* serv = getservbyname(portStr.c_str(), proto.c_str()))
|
|
|
|
{
|
|
|
|
return {host, serv->s_port};
|
|
|
|
}
|
2022-07-16 00:41:14 +00:00
|
|
|
return {host, std::stoi(portStr)};
|
2021-04-01 11:13:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
accept_port(const char* remote, uint16_t port, void* ptr)
|
|
|
|
{
|
|
|
|
(void)remote;
|
|
|
|
if (port == *static_cast<uint16_t*>(ptr))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-04-15 11:55:05 +00:00
|
|
|
std::optional<lokinet_srv_record>
|
2021-04-12 11:39:07 +00:00
|
|
|
SRVFromData(const llarp::dns::SRVData& data, std::string name)
|
|
|
|
{
|
|
|
|
// TODO: implement me
|
|
|
|
(void)data;
|
|
|
|
(void)name;
|
2021-04-15 11:55:05 +00:00
|
|
|
return std::nullopt;
|
2021-04-12 11:39:07 +00:00
|
|
|
}
|
|
|
|
|
2021-04-01 11:13:39 +00:00
|
|
|
} // namespace
|
2021-03-08 19:19:20 +00:00
|
|
|
|
2021-04-12 11:39:07 +00:00
|
|
|
struct lokinet_srv_lookup_private
|
|
|
|
{
|
|
|
|
std::vector<lokinet_srv_record> results;
|
|
|
|
|
|
|
|
int
|
|
|
|
LookupSRV(std::string host, std::string service, lokinet_context* ctx)
|
|
|
|
{
|
|
|
|
std::promise<int> promise;
|
|
|
|
{
|
|
|
|
auto lock = ctx->acquire();
|
|
|
|
if (ctx->impl and ctx->impl->IsUp())
|
|
|
|
{
|
2021-11-14 14:45:18 +00:00
|
|
|
ctx->impl->CallSafe([host, service, &promise, ctx, this]() {
|
2021-04-12 11:39:07 +00:00
|
|
|
auto ep = ctx->endpoint();
|
|
|
|
if (ep == nullptr)
|
|
|
|
{
|
|
|
|
promise.set_value(ENOTSUP);
|
|
|
|
return;
|
|
|
|
}
|
2021-11-14 14:45:18 +00:00
|
|
|
ep->LookupServiceAsync(host, service, [this, &promise, host](auto results) {
|
2021-04-15 11:55:05 +00:00
|
|
|
for (const auto& result : results)
|
|
|
|
{
|
|
|
|
if (auto maybe = SRVFromData(result, host))
|
2021-11-14 14:45:18 +00:00
|
|
|
this->results.emplace_back(*maybe);
|
2021-04-15 11:55:05 +00:00
|
|
|
}
|
2021-04-12 11:39:07 +00:00
|
|
|
promise.set_value(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
promise.set_value(EHOSTDOWN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto future = promise.get_future();
|
|
|
|
return future.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IterateAll(std::function<void(lokinet_srv_record*)> visit)
|
|
|
|
{
|
2022-07-16 00:41:14 +00:00
|
|
|
for (auto& result : results)
|
|
|
|
visit(&result);
|
2021-04-12 11:39:07 +00:00
|
|
|
// null terminator
|
|
|
|
visit(nullptr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-08 19:19:20 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
2021-06-08 12:32:01 +00:00
|
|
|
void EXPORT
|
2021-04-03 12:19:46 +00:00
|
|
|
lokinet_set_netid(const char* netid)
|
|
|
|
{
|
|
|
|
llarp::NetID::DefaultValue() = llarp::NetID{reinterpret_cast<const byte_t*>(netid)};
|
|
|
|
}
|
|
|
|
|
2021-06-08 12:32:01 +00:00
|
|
|
const char* EXPORT
|
2021-04-03 12:19:46 +00:00
|
|
|
lokinet_get_netid()
|
|
|
|
{
|
|
|
|
const auto netid = llarp::NetID::DefaultValue().ToString();
|
|
|
|
return strdup(netid.c_str());
|
|
|
|
}
|
|
|
|
|
2022-07-16 00:41:14 +00:00
|
|
|
static auto last_log_set = llarp::log::Level::info;
|
|
|
|
|
2021-06-08 12:32:01 +00:00
|
|
|
int EXPORT
|
2021-04-03 12:19:46 +00:00
|
|
|
lokinet_log_level(const char* level)
|
|
|
|
{
|
2022-07-19 16:19:35 +00:00
|
|
|
try
|
2021-04-03 12:19:46 +00:00
|
|
|
{
|
2022-07-19 16:19:35 +00:00
|
|
|
auto new_level = llarp::log::level_from_string(level);
|
|
|
|
llarp::log::reset_level(new_level);
|
|
|
|
last_log_set = new_level;
|
2021-04-03 12:19:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2022-07-19 16:19:35 +00:00
|
|
|
catch (std::invalid_argument& e)
|
|
|
|
{
|
|
|
|
llarp::LogError(e.what());
|
|
|
|
}
|
2021-04-03 12:19:46 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-06-08 12:32:01 +00:00
|
|
|
char* EXPORT
|
2021-04-01 16:51:02 +00:00
|
|
|
lokinet_address(struct lokinet_context* ctx)
|
|
|
|
{
|
|
|
|
if (not ctx)
|
|
|
|
return nullptr;
|
|
|
|
auto lock = ctx->acquire();
|
2021-04-02 16:08:06 +00:00
|
|
|
auto ep = ctx->endpoint();
|
2021-04-01 16:51:02 +00:00
|
|
|
const auto addr = ep->GetIdentity().pub.Addr();
|
|
|
|
const auto addrStr = addr.ToString();
|
|
|
|
return strdup(addrStr.c_str());
|
2021-03-08 19:19:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 12:32:01 +00:00
|
|
|
int EXPORT
|
2021-04-02 15:10:37 +00:00
|
|
|
lokinet_add_bootstrap_rc(const char* data, size_t datalen, struct lokinet_context* ctx)
|
|
|
|
{
|
2021-09-20 22:22:15 +00:00
|
|
|
if (data == nullptr or datalen == 0)
|
|
|
|
return -3;
|
2021-04-02 15:10:37 +00:00
|
|
|
llarp_buffer_t buf{data, datalen};
|
|
|
|
if (ctx == nullptr)
|
|
|
|
return -3;
|
|
|
|
auto lock = ctx->acquire();
|
|
|
|
// add a temp cryptography implementation here so rc.Verify works
|
|
|
|
llarp::CryptoManager instance{new llarp::sodium::CryptoLibSodium{}};
|
2021-09-20 21:47:06 +00:00
|
|
|
if (data[0] == 'l')
|
|
|
|
{
|
2021-09-20 21:55:07 +00:00
|
|
|
if (not ctx->config->bootstrap.routers.BDecode(&buf))
|
2021-09-20 22:07:47 +00:00
|
|
|
{
|
2021-09-20 22:12:08 +00:00
|
|
|
llarp::LogError("Cannot decode bootstrap list: ", llarp::buffer_printer{buf});
|
2021-09-20 21:47:06 +00:00
|
|
|
return -1;
|
2021-09-20 22:07:47 +00:00
|
|
|
}
|
2021-09-20 21:57:55 +00:00
|
|
|
for (const auto& rc : ctx->config->bootstrap.routers)
|
2021-09-20 21:47:06 +00:00
|
|
|
{
|
|
|
|
if (not rc.Verify(llarp::time_now_ms()))
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-20 22:12:08 +00:00
|
|
|
llarp::RouterContact rc{};
|
2021-09-20 21:47:06 +00:00
|
|
|
if (not rc.BDecode(&buf))
|
2021-09-20 22:12:08 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("failed to decode signle RC: ", llarp::buffer_printer{buf});
|
2021-09-20 21:47:06 +00:00
|
|
|
return -1;
|
2021-09-20 22:12:08 +00:00
|
|
|
}
|
2021-09-20 21:47:06 +00:00
|
|
|
if (not rc.Verify(llarp::time_now_ms()))
|
|
|
|
return -2;
|
|
|
|
ctx->config->bootstrap.routers.insert(std::move(rc));
|
|
|
|
}
|
2021-04-02 15:10:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-08 12:32:01 +00:00
|
|
|
struct lokinet_context* EXPORT
|
2021-03-08 19:19:20 +00:00
|
|
|
lokinet_context_new()
|
|
|
|
{
|
|
|
|
return new lokinet_context{};
|
|
|
|
}
|
|
|
|
|
2021-06-08 12:32:01 +00:00
|
|
|
void EXPORT
|
2021-03-08 19:19:20 +00:00
|
|
|
lokinet_context_free(struct lokinet_context* ctx)
|
|
|
|
{
|
2021-04-01 11:13:39 +00:00
|
|
|
lokinet_context_stop(ctx);
|
2021-03-08 19:19:20 +00:00
|
|
|
delete ctx;
|
|
|
|
}
|
|
|
|
|
2021-06-08 12:32:01 +00:00
|
|
|
int EXPORT
|
2021-03-08 19:19:20 +00:00
|
|
|
lokinet_context_start(struct lokinet_context* ctx)
|
|
|
|
{
|
2021-04-01 11:13:39 +00:00
|
|
|
if (not ctx)
|
2021-04-02 15:10:37 +00:00
|
|
|
return -1;
|
2021-04-01 11:13:39 +00:00
|
|
|
auto lock = ctx->acquire();
|
2021-04-03 12:19:46 +00:00
|
|
|
ctx->config->router.m_netId = lokinet_get_netid();
|
2022-07-16 00:41:14 +00:00
|
|
|
ctx->config->logging.m_logLevel = last_log_set;
|
2021-03-08 19:19:20 +00:00
|
|
|
ctx->runner = std::make_unique<std::thread>([ctx]() {
|
2021-04-01 16:51:02 +00:00
|
|
|
llarp::util::SetThreadName("llarp-mainloop");
|
2021-04-02 15:10:37 +00:00
|
|
|
ctx->impl->Configure(ctx->config);
|
2021-03-08 19:19:20 +00:00
|
|
|
const llarp::RuntimeOptions opts{};
|
2021-04-01 16:51:02 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
ctx->impl->Setup(opts);
|
2021-04-02 15:10:37 +00:00
|
|
|
#ifdef SIG_PIPE
|
|
|
|
signal(SIG_PIPE, SIGIGN);
|
|
|
|
#endif
|
2021-04-01 16:51:02 +00:00
|
|
|
ctx->impl->Run(opts);
|
|
|
|
}
|
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
|
|
|
std::cerr << ex.what() << std::endl;
|
|
|
|
ctx->impl->CloseAsync();
|
|
|
|
}
|
2021-03-08 19:19:20 +00:00
|
|
|
});
|
2021-04-01 16:51:02 +00:00
|
|
|
while (not ctx->impl->IsUp())
|
|
|
|
{
|
|
|
|
if (ctx->impl->IsStopping())
|
2021-04-02 15:10:37 +00:00
|
|
|
return -1;
|
2021-04-02 16:08:06 +00:00
|
|
|
std::this_thread::sleep_for(50ms);
|
2021-04-01 16:51:02 +00:00
|
|
|
}
|
2021-04-02 15:10:37 +00:00
|
|
|
return 0;
|
2021-03-08 19:19:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 12:32:01 +00:00
|
|
|
int EXPORT
|
2021-04-04 11:33:17 +00:00
|
|
|
lokinet_status(struct lokinet_context* ctx)
|
|
|
|
{
|
|
|
|
if (ctx == nullptr)
|
|
|
|
return -3;
|
|
|
|
auto lock = ctx->acquire();
|
|
|
|
if (not ctx->impl->IsUp())
|
|
|
|
return -3;
|
|
|
|
if (not ctx->impl->LooksAlive())
|
|
|
|
return -2;
|
|
|
|
return ctx->endpoint()->IsReady() ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2021-06-08 12:32:01 +00:00
|
|
|
int EXPORT
|
2021-04-02 16:08:06 +00:00
|
|
|
lokinet_wait_for_ready(int ms, struct lokinet_context* ctx)
|
|
|
|
{
|
|
|
|
if (ctx == nullptr)
|
|
|
|
return -1;
|
|
|
|
auto lock = ctx->acquire();
|
|
|
|
auto ep = ctx->endpoint();
|
|
|
|
int iterations = ms / 10;
|
|
|
|
if (iterations <= 0)
|
|
|
|
{
|
|
|
|
ms = 10;
|
|
|
|
iterations = 1;
|
|
|
|
}
|
|
|
|
while (not ep->IsReady() and iterations > 0)
|
|
|
|
{
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{ms / 10});
|
|
|
|
iterations--;
|
|
|
|
}
|
|
|
|
return ep->IsReady() ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2021-06-08 12:32:01 +00:00
|
|
|
void EXPORT
|
2021-03-08 19:19:20 +00:00
|
|
|
lokinet_context_stop(struct lokinet_context* ctx)
|
|
|
|
{
|
2021-04-01 11:13:39 +00:00
|
|
|
if (not ctx)
|
|
|
|
return;
|
|
|
|
auto lock = ctx->acquire();
|
|
|
|
|
2021-09-23 18:01:04 +00:00
|
|
|
if (ctx->impl->IsStopping())
|
|
|
|
return;
|
|
|
|
|
|
|
|
ctx->impl->CloseAsync();
|
|
|
|
ctx->impl->Wait();
|
2021-04-01 11:13:39 +00:00
|
|
|
|
|
|
|
if (ctx->runner)
|
|
|
|
ctx->runner->join();
|
|
|
|
|
|
|
|
ctx->runner.reset();
|
|
|
|
}
|
|
|
|
|
2021-06-08 12:32:01 +00:00
|
|
|
void EXPORT
|
2021-04-01 16:51:02 +00:00
|
|
|
lokinet_outbound_stream(
|
|
|
|
struct lokinet_stream_result* result,
|
|
|
|
const char* remote,
|
|
|
|
const char* local,
|
|
|
|
struct lokinet_context* ctx)
|
2021-04-01 11:13:39 +00:00
|
|
|
{
|
|
|
|
if (ctx == nullptr)
|
2021-04-01 16:51:02 +00:00
|
|
|
{
|
|
|
|
stream_error(result, EHOSTDOWN);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::promise<void> promise;
|
2021-04-01 11:13:39 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
auto lock = ctx->acquire();
|
|
|
|
|
|
|
|
if (not ctx->impl->IsUp())
|
2021-04-01 16:51:02 +00:00
|
|
|
{
|
|
|
|
stream_error(result, EHOSTDOWN);
|
|
|
|
return;
|
|
|
|
}
|
2021-04-01 11:13:39 +00:00
|
|
|
std::string remotehost;
|
|
|
|
int remoteport;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto [h, p] = split_host_port(remote);
|
|
|
|
remotehost = h;
|
|
|
|
remoteport = p;
|
|
|
|
}
|
|
|
|
catch (int err)
|
|
|
|
{
|
2021-04-01 16:51:02 +00:00
|
|
|
stream_error(result, err);
|
|
|
|
return;
|
2021-04-01 11:13:39 +00:00
|
|
|
}
|
|
|
|
// TODO: make configurable (?)
|
|
|
|
std::string endpoint{"default"};
|
|
|
|
|
|
|
|
llarp::SockAddr localAddr;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (local)
|
|
|
|
localAddr = llarp::SockAddr{std::string{local}};
|
|
|
|
else
|
|
|
|
localAddr = llarp::SockAddr{"127.0.0.1:0"};
|
|
|
|
}
|
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
2021-04-01 16:51:02 +00:00
|
|
|
stream_error(result, EINVAL);
|
|
|
|
return;
|
2021-04-01 11:13:39 +00:00
|
|
|
}
|
|
|
|
auto call = [&promise,
|
|
|
|
ctx,
|
2021-04-01 16:51:02 +00:00
|
|
|
result,
|
2021-04-01 11:13:39 +00:00
|
|
|
router = ctx->impl->router,
|
|
|
|
remotehost,
|
|
|
|
remoteport,
|
|
|
|
endpoint,
|
|
|
|
localAddr]() {
|
2021-04-02 16:08:06 +00:00
|
|
|
auto ep = ctx->endpoint();
|
2021-04-01 11:13:39 +00:00
|
|
|
if (ep == nullptr)
|
|
|
|
{
|
2021-04-01 16:51:02 +00:00
|
|
|
stream_error(result, ENOTSUP);
|
|
|
|
promise.set_value();
|
2021-04-01 11:13:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto* quic = ep->GetQUICTunnel();
|
|
|
|
if (quic == nullptr)
|
|
|
|
{
|
2021-04-01 16:51:02 +00:00
|
|
|
stream_error(result, ENOTSUP);
|
|
|
|
promise.set_value();
|
2021-04-01 11:13:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto [addr, id] = quic->open(
|
2021-04-01 16:51:02 +00:00
|
|
|
remotehost, remoteport, [](auto) {}, localAddr);
|
2022-07-16 00:41:14 +00:00
|
|
|
auto [host, port] = split_host_port(addr.ToString());
|
2021-04-01 11:13:39 +00:00
|
|
|
ctx->outbound_stream(id);
|
2021-04-01 16:51:02 +00:00
|
|
|
stream_okay(result, host, port, id);
|
2021-04-01 11:13:39 +00:00
|
|
|
}
|
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
2021-04-01 16:51:02 +00:00
|
|
|
std::cout << ex.what() << std::endl;
|
|
|
|
stream_error(result, ECANCELED);
|
2021-04-01 11:13:39 +00:00
|
|
|
}
|
|
|
|
catch (int err)
|
|
|
|
{
|
2021-04-01 16:51:02 +00:00
|
|
|
stream_error(result, err);
|
2021-04-01 11:13:39 +00:00
|
|
|
}
|
2021-04-01 16:51:02 +00:00
|
|
|
promise.set_value();
|
2021-04-01 11:13:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ctx->impl->CallSafe([call]() {
|
|
|
|
// we dont want the mainloop to die in case setting the value on the promise fails
|
|
|
|
try
|
|
|
|
{
|
|
|
|
call();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
auto future = promise.get_future();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (auto status = future.wait_for(std::chrono::seconds{10});
|
|
|
|
status == std::future_status::ready)
|
|
|
|
{
|
2021-04-01 16:51:02 +00:00
|
|
|
future.get();
|
2021-04-01 11:13:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-04-01 16:51:02 +00:00
|
|
|
stream_error(result, ETIMEDOUT);
|
2021-04-01 11:13:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
2021-04-01 16:51:02 +00:00
|
|
|
stream_error(result, EBADF);
|
2021-04-01 11:13:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 12:32:01 +00:00
|
|
|
int EXPORT
|
2021-04-01 11:13:39 +00:00
|
|
|
lokinet_inbound_stream(uint16_t port, struct lokinet_context* ctx)
|
|
|
|
{
|
|
|
|
/// FIXME: delete pointer later
|
|
|
|
return lokinet_inbound_stream_filter(&accept_port, (void*)new std::uintptr_t{port}, ctx);
|
|
|
|
}
|
|
|
|
|
2021-06-08 12:32:01 +00:00
|
|
|
int EXPORT
|
2021-04-01 11:13:39 +00:00
|
|
|
lokinet_inbound_stream_filter(
|
|
|
|
lokinet_stream_filter acceptFilter, void* user, struct lokinet_context* ctx)
|
|
|
|
{
|
|
|
|
if (acceptFilter == nullptr)
|
|
|
|
{
|
|
|
|
acceptFilter = [](auto, auto, auto) { return 0; };
|
|
|
|
}
|
|
|
|
if (not ctx)
|
|
|
|
return -1;
|
|
|
|
std::promise<int> promise;
|
|
|
|
{
|
|
|
|
auto lock = ctx->acquire();
|
|
|
|
if (not ctx->impl->IsUp())
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-04-02 16:08:06 +00:00
|
|
|
ctx->impl->CallSafe([ctx, acceptFilter, user, &promise]() {
|
|
|
|
auto ep = ctx->endpoint();
|
2021-04-01 11:13:39 +00:00
|
|
|
auto* quic = ep->GetQUICTunnel();
|
|
|
|
auto id = quic->listen(
|
|
|
|
[acceptFilter, user](auto remoteAddr, auto port) -> std::optional<llarp::SockAddr> {
|
|
|
|
std::string remote{remoteAddr};
|
|
|
|
if (auto result = acceptFilter(remote.c_str(), port, user))
|
|
|
|
{
|
|
|
|
if (result == -1)
|
|
|
|
{
|
|
|
|
throw std::invalid_argument{"rejected"};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return llarp::SockAddr{"127.0.0.1:" + std::to_string(port)};
|
|
|
|
return std::nullopt;
|
|
|
|
});
|
|
|
|
promise.set_value(id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
auto ftr = promise.get_future();
|
|
|
|
auto id = ftr.get();
|
|
|
|
{
|
|
|
|
auto lock = ctx->acquire();
|
|
|
|
ctx->inbound_stream(id);
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2021-09-21 20:04:05 +00:00
|
|
|
char* EXPORT
|
|
|
|
lokinet_hex_to_base32z(const char* hex)
|
|
|
|
{
|
2021-10-13 12:54:45 +00:00
|
|
|
std::string_view hexview{hex};
|
2022-02-17 18:44:31 +00:00
|
|
|
if (not oxenc::is_hex(hexview))
|
2021-10-13 12:54:45 +00:00
|
|
|
return nullptr;
|
|
|
|
|
2022-07-16 00:41:14 +00:00
|
|
|
const size_t b32z_len = oxenc::to_base32z_size(oxenc::from_hex_size(hexview.size()));
|
2021-10-13 12:54:45 +00:00
|
|
|
auto buf = std::make_unique<char[]>(b32z_len + 1);
|
2022-07-16 00:41:14 +00:00
|
|
|
buf[b32z_len] = '\0'; // null terminate
|
|
|
|
|
|
|
|
oxenc::hex_decoder decode{hexview.begin(), hexview.end()};
|
|
|
|
oxenc::base32z_encoder encode{decode, decode.end()};
|
|
|
|
std::copy(encode, encode.end(), buf.get());
|
2021-10-13 12:54:45 +00:00
|
|
|
return buf.release(); // leak the buffer to the caller
|
2021-09-21 20:04:05 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 12:32:01 +00:00
|
|
|
void EXPORT
|
2021-04-01 11:13:39 +00:00
|
|
|
lokinet_close_stream(int stream_id, struct lokinet_context* ctx)
|
|
|
|
{
|
|
|
|
if (not ctx)
|
|
|
|
return;
|
|
|
|
auto lock = ctx->acquire();
|
|
|
|
if (not ctx->impl->IsUp())
|
|
|
|
return;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::promise<void> promise;
|
|
|
|
bool inbound = ctx->streams.at(stream_id);
|
2021-04-02 16:08:06 +00:00
|
|
|
ctx->impl->CallSafe([stream_id, inbound, ctx, &promise]() {
|
|
|
|
auto ep = ctx->endpoint();
|
2021-04-01 11:13:39 +00:00
|
|
|
auto* quic = ep->GetQUICTunnel();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (inbound)
|
|
|
|
quic->forget(stream_id);
|
|
|
|
else
|
|
|
|
quic->close(stream_id);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{}
|
|
|
|
promise.set_value();
|
|
|
|
});
|
|
|
|
promise.get_future().get();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{}
|
2021-03-08 19:19:20 +00:00
|
|
|
}
|
2021-04-12 11:39:07 +00:00
|
|
|
|
2021-06-08 12:32:01 +00:00
|
|
|
int EXPORT
|
2021-04-12 11:39:07 +00:00
|
|
|
lokinet_srv_lookup(
|
|
|
|
char* host,
|
|
|
|
char* service,
|
|
|
|
struct lokinet_srv_lookup_result* result,
|
|
|
|
struct lokinet_context* ctx)
|
|
|
|
{
|
|
|
|
if (result == nullptr or ctx == nullptr or host == nullptr or service == nullptr)
|
|
|
|
return -1;
|
|
|
|
// sanity check, if the caller has not free()'d internals yet free them
|
|
|
|
if (result->internal)
|
|
|
|
delete result->internal;
|
|
|
|
result->internal = new lokinet_srv_lookup_private{};
|
|
|
|
return result->internal->LookupSRV(host, service, ctx);
|
|
|
|
}
|
|
|
|
|
2021-06-08 12:32:01 +00:00
|
|
|
void EXPORT
|
2021-04-12 11:39:07 +00:00
|
|
|
lokinet_for_each_srv_record(
|
|
|
|
struct lokinet_srv_lookup_result* result, lokinet_srv_record_iterator iter, void* user)
|
|
|
|
{
|
|
|
|
if (result and result->internal)
|
|
|
|
{
|
|
|
|
result->internal->IterateAll([iter, user](auto* result) { iter(result, user); });
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
iter(nullptr, user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 12:32:01 +00:00
|
|
|
void EXPORT
|
2021-04-12 11:39:07 +00:00
|
|
|
lokinet_srv_lookup_done(struct lokinet_srv_lookup_result* result)
|
|
|
|
{
|
|
|
|
if (result == nullptr or result->internal == nullptr)
|
|
|
|
return;
|
|
|
|
delete result->internal;
|
|
|
|
result->internal = nullptr;
|
|
|
|
}
|
2021-09-23 18:01:04 +00:00
|
|
|
|
|
|
|
int EXPORT
|
|
|
|
lokinet_udp_bind(
|
|
|
|
uint16_t exposedPort,
|
|
|
|
lokinet_udp_flow_filter filter,
|
|
|
|
lokinet_udp_flow_recv_func recv,
|
|
|
|
lokinet_udp_flow_timeout_func timeout,
|
|
|
|
void* user,
|
|
|
|
struct lokinet_udp_bind_result* result,
|
|
|
|
struct lokinet_context* ctx)
|
|
|
|
{
|
|
|
|
if (filter == nullptr or recv == nullptr or timeout == nullptr or result == nullptr
|
|
|
|
or ctx == nullptr)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
auto lock = ctx->acquire();
|
|
|
|
if (auto ep = ctx->endpoint())
|
|
|
|
{
|
2022-04-07 20:44:23 +00:00
|
|
|
if (auto maybe = ctx->make_udp_handler(
|
|
|
|
ep, llarp::net::port_t::from_host(exposedPort), filter, recv, timeout, user))
|
2021-09-23 18:31:47 +00:00
|
|
|
{
|
|
|
|
result->socket_id = *maybe;
|
|
|
|
return 0;
|
|
|
|
}
|
2021-09-23 18:01:04 +00:00
|
|
|
}
|
2021-09-23 18:31:47 +00:00
|
|
|
return EINVAL;
|
2021-09-23 18:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EXPORT
|
|
|
|
lokinet_udp_close(int socket_id, struct lokinet_context* ctx)
|
|
|
|
{
|
|
|
|
if (ctx)
|
2021-09-23 18:31:47 +00:00
|
|
|
{
|
2021-09-23 18:01:04 +00:00
|
|
|
ctx->remove_udp_handler(socket_id);
|
2021-09-23 18:31:47 +00:00
|
|
|
}
|
2021-09-23 18:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int EXPORT
|
|
|
|
lokinet_udp_flow_send(
|
|
|
|
const struct lokinet_udp_flowinfo* remote,
|
|
|
|
const void* ptr,
|
|
|
|
size_t len,
|
|
|
|
struct lokinet_context* ctx)
|
|
|
|
{
|
|
|
|
if (remote == nullptr or remote->remote_port == 0 or ptr == nullptr or len == 0
|
|
|
|
or ctx == nullptr)
|
|
|
|
return EINVAL;
|
|
|
|
std::shared_ptr<llarp::EndpointBase> ep;
|
|
|
|
llarp::nuint16_t srcport{0};
|
2022-04-07 20:44:23 +00:00
|
|
|
auto dstport = llarp::net::port_t::from_host(remote->remote_port);
|
2021-09-23 18:01:04 +00:00
|
|
|
{
|
|
|
|
auto lock = ctx->acquire();
|
|
|
|
if (auto itr = ctx->udp_sockets.find(remote->socket_id); itr != ctx->udp_sockets.end())
|
|
|
|
{
|
|
|
|
ep = itr->second->m_Endpoint.lock();
|
|
|
|
srcport = itr->second->m_LocalPort;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return EHOSTUNREACH;
|
|
|
|
}
|
|
|
|
if (auto maybe = llarp::service::ParseAddress(std::string{remote->remote_host}))
|
|
|
|
{
|
|
|
|
llarp::net::IPPacket pkt = llarp::net::IPPacket::UDP(
|
|
|
|
llarp::nuint32_t{0},
|
|
|
|
srcport,
|
|
|
|
llarp::nuint32_t{0},
|
|
|
|
dstport,
|
|
|
|
llarp_buffer_t{reinterpret_cast<const uint8_t*>(ptr), len});
|
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
if (pkt.empty())
|
2021-09-23 18:01:04 +00:00
|
|
|
return EINVAL;
|
|
|
|
std::promise<int> ret;
|
2021-09-23 18:31:47 +00:00
|
|
|
ctx->impl->router->loop()->call([addr = *maybe, pkt = std::move(pkt), ep, &ret]() {
|
2021-09-23 18:01:04 +00:00
|
|
|
if (auto tag = ep->GetBestConvoTagFor(addr))
|
|
|
|
{
|
|
|
|
if (ep->SendToOrQueue(*tag, pkt.ConstBuffer(), llarp::service::ProtocolType::TrafficV4))
|
|
|
|
{
|
|
|
|
ret.set_value(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret.set_value(ENETUNREACH);
|
|
|
|
});
|
|
|
|
return ret.get_future().get();
|
|
|
|
}
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int EXPORT
|
|
|
|
lokinet_udp_establish(
|
|
|
|
lokinet_udp_create_flow_func create_flow,
|
|
|
|
void* user,
|
|
|
|
const struct lokinet_udp_flowinfo* remote,
|
|
|
|
struct lokinet_context* ctx)
|
|
|
|
{
|
|
|
|
if (create_flow == nullptr or remote == nullptr or ctx == nullptr)
|
|
|
|
return EINVAL;
|
|
|
|
std::shared_ptr<llarp::EndpointBase> ep;
|
|
|
|
{
|
|
|
|
auto lock = ctx->acquire();
|
2021-09-23 18:31:47 +00:00
|
|
|
if (ctx->impl->router->loop()->inEventLoop())
|
|
|
|
{
|
2021-10-13 11:37:25 +00:00
|
|
|
llarp::LogError("cannot call udp_establish from internal event loop");
|
2021-09-23 18:31:47 +00:00
|
|
|
return EINVAL;
|
|
|
|
}
|
2021-09-23 18:01:04 +00:00
|
|
|
if (auto itr = ctx->udp_sockets.find(remote->socket_id); itr != ctx->udp_sockets.end())
|
|
|
|
{
|
|
|
|
ep = itr->second->m_Endpoint.lock();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return EHOSTUNREACH;
|
|
|
|
}
|
|
|
|
if (auto maybe = llarp::service::ParseAddress(std::string{remote->remote_host}))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// check for pre existing flow
|
|
|
|
auto lock = ctx->acquire();
|
|
|
|
if (auto itr = ctx->udp_sockets.find(remote->socket_id); itr != ctx->udp_sockets.end())
|
|
|
|
{
|
|
|
|
auto& udp = itr->second;
|
|
|
|
if (udp->m_Flows.count(*maybe))
|
|
|
|
{
|
|
|
|
// we already have a flow.
|
|
|
|
return EADDRINUSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::promise<bool> gotten;
|
2021-09-23 18:31:47 +00:00
|
|
|
ctx->impl->router->loop()->call([addr = *maybe, ep, &gotten]() {
|
2021-09-30 17:17:06 +00:00
|
|
|
ep->MarkAddressOutbound(addr);
|
|
|
|
auto res = ep->EnsurePathTo(
|
2021-09-23 18:01:04 +00:00
|
|
|
addr, [&gotten](auto result) { gotten.set_value(result.has_value()); }, 5s);
|
2021-09-30 17:17:06 +00:00
|
|
|
if (not res)
|
|
|
|
{
|
|
|
|
gotten.set_value(false);
|
|
|
|
}
|
2021-09-23 18:01:04 +00:00
|
|
|
});
|
|
|
|
if (gotten.get_future().get())
|
|
|
|
{
|
|
|
|
void* flow_data{nullptr};
|
|
|
|
int flow_timeoutseconds{};
|
|
|
|
create_flow(user, &flow_data, &flow_timeoutseconds);
|
|
|
|
{
|
|
|
|
auto lock = ctx->acquire();
|
|
|
|
if (auto itr = ctx->udp_sockets.find(remote->socket_id); itr != ctx->udp_sockets.end())
|
|
|
|
{
|
|
|
|
itr->second->AddFlow(*maybe, *remote, flow_data, flow_timeoutseconds);
|
|
|
|
return 0;
|
|
|
|
}
|
2022-07-16 00:41:14 +00:00
|
|
|
return EADDRINUSE;
|
2021-09-23 18:01:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return ETIMEDOUT;
|
|
|
|
}
|
|
|
|
return EINVAL;
|
|
|
|
}
|
2021-09-25 16:54:43 +00:00
|
|
|
|
2022-07-16 00:41:14 +00:00
|
|
|
void EXPORT
|
|
|
|
lokinet_set_syncing_logger(lokinet_logger_func func, lokinet_logger_sync sync, void* user)
|
|
|
|
{
|
|
|
|
llarp::log::clear_sinks();
|
|
|
|
llarp::log::add_sink(std::make_shared<llarp::logging::CallbackSink_mt>(func, sync, user));
|
|
|
|
}
|
|
|
|
|
2021-09-25 16:54:43 +00:00
|
|
|
void EXPORT
|
|
|
|
lokinet_set_logger(lokinet_logger_func func, void* user)
|
|
|
|
{
|
2022-07-16 00:41:14 +00:00
|
|
|
lokinet_set_syncing_logger(func, nullptr, user);
|
2021-09-25 16:54:43 +00:00
|
|
|
}
|
2021-03-08 19:19:20 +00:00
|
|
|
}
|