2018-11-12 16:43:40 +00:00
|
|
|
#ifndef LLARP_HANDLERS_EXIT_HPP
|
|
|
|
#define LLARP_HANDLERS_EXIT_HPP
|
|
|
|
|
2018-12-12 01:06:46 +00:00
|
|
|
#include <exit/endpoint.hpp>
|
2018-12-12 01:12:59 +00:00
|
|
|
#include <handlers/tun.hpp>
|
2018-12-13 16:14:44 +00:00
|
|
|
#include <dns/server.hpp>
|
2018-11-12 16:43:40 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace handlers
|
|
|
|
{
|
2018-12-13 16:14:44 +00:00
|
|
|
struct ExitEndpoint : public llarp::dns::IQueryHandler
|
2018-11-12 16:43:40 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
ExitEndpoint(const std::string& name, llarp::Router* r);
|
2018-11-12 16:43:40 +00:00
|
|
|
~ExitEndpoint();
|
|
|
|
|
|
|
|
void
|
2018-11-15 13:13:19 +00:00
|
|
|
Tick(llarp_time_t now);
|
2018-11-12 16:43:40 +00:00
|
|
|
|
|
|
|
bool
|
2018-11-15 13:13:19 +00:00
|
|
|
SetOption(const std::string& k, const std::string& v);
|
2018-11-12 16:43:40 +00:00
|
|
|
|
2018-11-19 13:39:35 +00:00
|
|
|
std::string
|
2018-11-15 13:13:19 +00:00
|
|
|
Name() const;
|
2018-11-14 19:53:03 +00:00
|
|
|
|
2018-12-13 16:14:44 +00:00
|
|
|
bool
|
|
|
|
ShouldHookDNSMessage(const llarp::dns::Message& msg) const override;
|
|
|
|
|
|
|
|
bool
|
|
|
|
HandleHookedDNSMessage(
|
|
|
|
llarp::dns::Message,
|
|
|
|
std::function< void(llarp::dns::Message) >) override;
|
|
|
|
|
2018-11-14 12:23:08 +00:00
|
|
|
bool
|
2018-11-15 15:46:50 +00:00
|
|
|
AllocateNewExit(const llarp::PubKey pk, const llarp::PathID_t& path,
|
2018-11-14 12:23:08 +00:00
|
|
|
bool permitInternet);
|
|
|
|
|
|
|
|
llarp::exit::Endpoint*
|
|
|
|
FindEndpointByPath(const llarp::PathID_t& path);
|
|
|
|
|
2018-11-15 13:13:19 +00:00
|
|
|
llarp::exit::Endpoint*
|
|
|
|
FindEndpointByIP(huint32_t ip);
|
|
|
|
|
2018-11-14 12:23:08 +00:00
|
|
|
bool
|
|
|
|
UpdateEndpointPath(const llarp::PubKey& remote,
|
|
|
|
const llarp::PathID_t& next);
|
|
|
|
|
2018-11-15 13:13:19 +00:00
|
|
|
/// handle ip packet from outside
|
|
|
|
void
|
|
|
|
OnInetPacket(llarp_buffer_t buf);
|
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::Router*
|
2018-11-15 13:13:19 +00:00
|
|
|
Router();
|
|
|
|
|
2018-11-28 12:32:38 +00:00
|
|
|
llarp_time_t
|
|
|
|
Now() const;
|
|
|
|
|
2018-12-11 00:53:11 +00:00
|
|
|
llarp::Crypto*
|
2018-11-15 13:13:19 +00:00
|
|
|
Crypto();
|
|
|
|
|
2018-11-14 18:02:27 +00:00
|
|
|
template < typename Stats >
|
|
|
|
void
|
|
|
|
CalculateTrafficStats(Stats& stats)
|
|
|
|
{
|
|
|
|
auto itr = m_ActiveExits.begin();
|
|
|
|
while(itr != m_ActiveExits.end())
|
|
|
|
{
|
2018-11-15 21:47:05 +00:00
|
|
|
stats[itr->first].first += itr->second->TxRate();
|
|
|
|
stats[itr->first].second += itr->second->RxRate();
|
2018-11-14 18:02:27 +00:00
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:13:19 +00:00
|
|
|
/// DO NOT CALL ME
|
2018-11-14 12:23:08 +00:00
|
|
|
void
|
2018-11-15 22:10:12 +00:00
|
|
|
DelEndpointInfo(const llarp::PathID_t& path);
|
2018-11-14 12:23:08 +00:00
|
|
|
|
2018-11-15 13:13:19 +00:00
|
|
|
/// DO NOT CALL ME
|
2018-11-14 18:02:27 +00:00
|
|
|
void
|
|
|
|
RemoveExit(const llarp::exit::Endpoint* ep);
|
|
|
|
|
2018-11-15 13:13:19 +00:00
|
|
|
bool
|
|
|
|
QueueOutboundTraffic(llarp_buffer_t buf);
|
|
|
|
|
|
|
|
/// sets up networking and starts traffic
|
|
|
|
bool
|
|
|
|
Start();
|
|
|
|
|
2018-11-15 16:19:24 +00:00
|
|
|
bool
|
|
|
|
HasLocalMappedAddrFor(const llarp::PubKey& pk) const;
|
|
|
|
|
2018-11-15 13:13:19 +00:00
|
|
|
huint32_t
|
|
|
|
GetIfAddr() const;
|
|
|
|
|
2018-11-12 16:43:40 +00:00
|
|
|
void
|
2018-11-28 16:38:20 +00:00
|
|
|
Flush();
|
2018-11-12 16:43:40 +00:00
|
|
|
|
|
|
|
private:
|
2018-11-15 13:13:19 +00:00
|
|
|
huint32_t
|
2018-11-15 14:44:57 +00:00
|
|
|
GetIPForIdent(const llarp::PubKey pk);
|
2018-11-15 13:13:19 +00:00
|
|
|
|
|
|
|
huint32_t
|
|
|
|
AllocateNewAddress();
|
|
|
|
|
2018-12-13 16:14:44 +00:00
|
|
|
/// obtain ip for service node session, creates a new session if one does
|
|
|
|
/// not existing already
|
|
|
|
huint32_t
|
|
|
|
ObtainServiceNodeIP(const llarp::RouterID& router);
|
|
|
|
|
|
|
|
bool
|
|
|
|
QueueSNodePacket(llarp_buffer_t buf, llarp::huint32_t from);
|
|
|
|
|
2018-11-15 13:13:19 +00:00
|
|
|
void
|
|
|
|
MarkIPActive(llarp::huint32_t ip);
|
|
|
|
|
|
|
|
void
|
|
|
|
KickIdentOffExit(const llarp::PubKey& pk);
|
|
|
|
|
2018-12-10 16:26:46 +00:00
|
|
|
llarp::Router* m_Router;
|
2018-12-13 16:14:44 +00:00
|
|
|
llarp::dns::Proxy m_Resolver;
|
2018-11-15 21:47:05 +00:00
|
|
|
bool m_ShouldInitTun;
|
2018-11-12 16:43:40 +00:00
|
|
|
std::string m_Name;
|
2018-11-14 12:23:08 +00:00
|
|
|
bool m_PermitExit;
|
|
|
|
std::unordered_map< llarp::PathID_t, llarp::PubKey,
|
|
|
|
llarp::PathID_t::Hash >
|
|
|
|
m_Paths;
|
2018-11-15 21:47:05 +00:00
|
|
|
std::unordered_multimap< llarp::PubKey,
|
|
|
|
std::unique_ptr< llarp::exit::Endpoint >,
|
2018-11-12 16:43:40 +00:00
|
|
|
llarp::PubKey::Hash >
|
|
|
|
m_ActiveExits;
|
2018-11-15 13:13:19 +00:00
|
|
|
|
2018-11-15 21:47:05 +00:00
|
|
|
using KeyMap_t = std::unordered_map< llarp::PubKey, llarp::huint32_t,
|
|
|
|
llarp::PubKey::Hash >;
|
2018-11-15 16:05:31 +00:00
|
|
|
|
|
|
|
KeyMap_t m_KeyToIP;
|
2018-11-15 13:13:19 +00:00
|
|
|
|
2018-12-13 16:14:44 +00:00
|
|
|
using SNodes_t = std::set< llarp::PubKey >;
|
|
|
|
/// set of pubkeys we treat as snodes
|
|
|
|
SNodes_t m_SNodeKeys;
|
|
|
|
|
|
|
|
using SNodeSessions_t =
|
|
|
|
std::unordered_map< llarp::RouterID,
|
|
|
|
std::unique_ptr< llarp::exit::SNodeSession >,
|
|
|
|
llarp::RouterID::Hash >;
|
|
|
|
/// snode sessions we are talking to directly
|
|
|
|
SNodeSessions_t m_SNodeSessions;
|
|
|
|
|
2018-11-15 13:13:19 +00:00
|
|
|
std::unordered_map< llarp::huint32_t, llarp::PubKey,
|
|
|
|
llarp::huint32_t::Hash >
|
|
|
|
m_IPToKey;
|
|
|
|
|
|
|
|
huint32_t m_IfAddr;
|
|
|
|
huint32_t m_HigestAddr;
|
|
|
|
huint32_t m_NextAddr;
|
2018-12-13 16:14:44 +00:00
|
|
|
llarp::IPRange m_OurRange;
|
2018-11-15 13:13:19 +00:00
|
|
|
|
|
|
|
std::unordered_map< llarp::huint32_t, llarp_time_t,
|
|
|
|
llarp::huint32_t::Hash >
|
|
|
|
m_IPActivity;
|
|
|
|
|
|
|
|
llarp_tun_io m_Tun;
|
|
|
|
|
2018-12-13 16:14:44 +00:00
|
|
|
llarp::Addr m_LocalResolverAddr;
|
|
|
|
std::vector< llarp::Addr > m_UpstreamResolvers;
|
|
|
|
|
2018-11-15 13:13:19 +00:00
|
|
|
using Pkt_t = llarp::net::IPv4Packet;
|
|
|
|
using PacketQueue_t =
|
|
|
|
llarp::util::CoDelQueue< Pkt_t, Pkt_t::GetTime, Pkt_t::PutTime,
|
|
|
|
Pkt_t::CompareOrder, Pkt_t::GetNow,
|
|
|
|
llarp::util::DummyMutex,
|
|
|
|
llarp::util::DummyLock, 5, 100, 1024 >;
|
|
|
|
|
|
|
|
/// internet to llarp packet queue
|
|
|
|
PacketQueue_t m_InetToNetwork;
|
2018-11-12 16:43:40 +00:00
|
|
|
};
|
|
|
|
} // namespace handlers
|
|
|
|
} // namespace llarp
|
2018-11-15 01:33:00 +00:00
|
|
|
#endif
|