lokinet/include/llarp/handlers/tun.hpp

127 lines
3.4 KiB
C++
Raw Normal View History

#ifndef LLARP_HANDLERS_TUN_HPP
#define LLARP_HANDLERS_TUN_HPP
2018-08-16 14:34:15 +00:00
#include <llarp/ev.h>
#include <llarp/codel.hpp>
#include <llarp/ip.hpp>
2018-08-16 14:34:15 +00:00
#include <llarp/service/endpoint.hpp>
#include <llarp/threading.hpp>
namespace llarp
{
namespace handlers
{
static const int DefaultTunNetmask = 16;
static const char DefaultTunIfname[] = "lokinet0";
static const char DefaultTunDstAddr[] = "10.10.0.1";
static const char DefaultTunSrcAddr[] = "10.10.0.2";
2018-08-16 14:34:15 +00:00
struct TunEndpoint : public service::Endpoint
{
2018-08-16 14:34:15 +00:00
TunEndpoint(const std::string& nickname, llarp_router* r);
~TunEndpoint();
bool
SetOption(const std::string& k, const std::string& v);
void
Tick(llarp_time_t now);
void
TickTun(llarp_time_t now);
2018-08-22 15:52:10 +00:00
bool
MapAddress(const service::Address& remote, uint32_t ip);
2018-08-16 14:34:15 +00:00
bool
Start();
/// set up tun interface, blocking
bool
SetupTun();
/// overrides Endpoint
bool
SetupNetworking();
2018-08-18 14:01:21 +00:00
/// overrides Endpoint
/// handle inbound traffic
2018-08-18 14:01:21 +00:00
void
HandleDataMessage(service::ProtocolMessage* msg);
#ifndef _MINGW32_NO_THREADS
2018-08-18 14:01:21 +00:00
/// overrides Endpount
bool
IsolationFailed()
{
m_TunSetupResult.set_value(false);
return false;
}
2018-08-21 18:40:42 +00:00
#endif
2018-08-18 14:01:21 +00:00
2018-08-16 14:34:15 +00:00
llarp_tun_io tunif;
/// called before writing to tun interface
2018-08-16 14:34:15 +00:00
static void
tunifBeforeWrite(llarp_tun_io* t);
2018-08-16 14:34:15 +00:00
2018-08-22 15:52:10 +00:00
/// handle user to network send buffer flush
/// called in router logic thread
static void
handleNetSend(void*);
/// called every time we wish to read a packet from the tun interface
2018-08-16 14:34:15 +00:00
static void
tunifRecvPkt(llarp_tun_io* t, const void* pkt, ssize_t sz);
/// called in the endpoint logic thread
2018-08-16 14:34:15 +00:00
static void
handleTickTun(void* u);
protected:
typedef llarp::util::CoDelQueue<
net::IPv4Packet, net::IPv4Packet::GetTime, net::IPv4Packet::PutTime,
net::IPv4Packet::CompareOrder >
PacketQueue_t;
/// queue for sending packets over the network from us
PacketQueue_t m_UserToNetworkPktQueue;
/// queue for sending packets to user from network
PacketQueue_t m_NetworkToUserPktQueue;
/// return true if we have a remote loki address for this ip address
bool
2018-08-20 19:12:12 +00:00
HasRemoteForIP(const uint32_t& ipv4) const;
/// get ip address for service address unconditionally
2018-08-20 19:12:12 +00:00
uint32_t
ObtainIPForAddr(const service::Address& addr);
/// mark this address as active
void
MarkIPActive(uint32_t ip);
2018-08-22 15:52:10 +00:00
void
FlushSend();
2018-08-16 14:34:15 +00:00
private:
#ifndef _MINGW32_NO_THREADS
/// handles setup, given value true on success and false on failure to set
/// up interface
2018-08-16 14:34:15 +00:00
std::promise< bool > m_TunSetupResult;
2018-08-21 18:39:18 +00:00
#endif
/// maps ip to service address
2018-08-20 19:12:12 +00:00
std::unordered_map< uint32_t, service::Address > m_IPToAddr;
/// maps service address to ip
2018-08-20 19:12:12 +00:00
std::unordered_map< service::Address, uint32_t, service::Address::Hash >
m_AddrToIP;
/// maps ip address to timestamp last active
std::unordered_map< uint32_t, llarp_time_t > m_IPActivity;
/// our ip address
2018-08-20 19:12:12 +00:00
uint32_t m_OurIP;
/// next ip address to allocate
2018-08-20 19:12:12 +00:00
uint32_t m_NextIP;
/// highest ip address to allocate
uint32_t m_MaxIP;
};
} // namespace handlers
} // namespace llarp
2018-08-16 14:34:15 +00:00
#endif