2018-09-02 18:25:42 +00:00
|
|
|
#ifndef LLARP_LINK_SERVER_HPP
|
|
|
|
#define LLARP_LINK_SERVER_HPP
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <llarp/threading.hpp>
|
|
|
|
#include <llarp/router_contact.hpp>
|
|
|
|
#include <llarp/crypto.hpp>
|
|
|
|
#include <llarp/net.hpp>
|
2018-09-03 12:08:02 +00:00
|
|
|
#include <llarp/ev.h>
|
|
|
|
#include <llarp/link/session.hpp>
|
2018-09-03 13:10:56 +00:00
|
|
|
#include <llarp/logic.h>
|
2018-09-07 20:36:06 +00:00
|
|
|
#include <list>
|
2018-09-03 12:08:02 +00:00
|
|
|
|
|
|
|
struct llarp_router;
|
2018-09-02 18:25:42 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
struct ILinkLayer
|
|
|
|
{
|
2018-09-03 13:10:56 +00:00
|
|
|
virtual ~ILinkLayer();
|
2018-09-02 18:25:42 +00:00
|
|
|
|
|
|
|
bool
|
2018-09-03 13:10:56 +00:00
|
|
|
HasSessionTo(const PubKey& pk);
|
2018-09-02 18:25:42 +00:00
|
|
|
|
|
|
|
bool
|
2018-09-03 13:10:56 +00:00
|
|
|
HasSessionVia(const Addr& addr);
|
2018-09-02 18:25:42 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
udp_tick(llarp_udp_io* udp)
|
|
|
|
{
|
2018-09-03 13:10:56 +00:00
|
|
|
static_cast< ILinkLayer* >(udp->user)->Pump();
|
2018-09-02 18:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
udp_recv_from(llarp_udp_io* udp, const sockaddr* from, const void* buf,
|
|
|
|
const ssize_t sz)
|
|
|
|
{
|
|
|
|
static_cast< ILinkLayer* >(udp->user)->RecvFrom(*from, buf, sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Configure(llarp_ev_loop* loop, const std::string& ifname, int af,
|
2018-09-03 13:10:56 +00:00
|
|
|
uint16_t port);
|
2018-09-02 18:25:42 +00:00
|
|
|
|
2018-09-06 13:16:24 +00:00
|
|
|
virtual ILinkSession*
|
2018-09-04 12:41:25 +00:00
|
|
|
NewOutboundSession(const RouterContact& rc, const AddressInfo& ai) = 0;
|
2018-09-02 18:25:42 +00:00
|
|
|
|
2018-09-04 12:41:25 +00:00
|
|
|
virtual void
|
2018-09-03 13:10:56 +00:00
|
|
|
Pump();
|
2018-09-02 18:25:42 +00:00
|
|
|
|
2018-09-04 12:41:25 +00:00
|
|
|
virtual void
|
|
|
|
RecvFrom(const Addr& from, const void* buf, size_t sz) = 0;
|
2018-09-02 18:25:42 +00:00
|
|
|
|
2018-09-03 13:10:56 +00:00
|
|
|
bool
|
2018-09-04 12:41:25 +00:00
|
|
|
PickAddress(const RouterContact& rc, AddressInfo& picked) const;
|
2018-09-02 18:25:42 +00:00
|
|
|
|
|
|
|
void
|
2018-09-03 13:10:56 +00:00
|
|
|
TryEstablishTo(const RouterContact& rc);
|
2018-09-02 18:25:42 +00:00
|
|
|
|
2018-09-03 13:10:56 +00:00
|
|
|
bool
|
|
|
|
Start(llarp_logic* l);
|
2018-09-02 18:25:42 +00:00
|
|
|
|
2018-09-03 13:10:56 +00:00
|
|
|
void
|
|
|
|
Stop();
|
2018-09-02 18:25:42 +00:00
|
|
|
|
|
|
|
virtual const char*
|
|
|
|
Name() const = 0;
|
|
|
|
|
|
|
|
void
|
2018-09-03 13:10:56 +00:00
|
|
|
CloseSessionTo(const PubKey& remote);
|
2018-09-02 18:25:42 +00:00
|
|
|
|
|
|
|
void
|
2018-09-03 13:10:56 +00:00
|
|
|
KeepAliveSessionTo(const PubKey& remote);
|
|
|
|
|
|
|
|
bool
|
|
|
|
SendTo(const PubKey& remote, llarp_buffer_t buf);
|
|
|
|
|
|
|
|
bool
|
2018-09-04 12:41:25 +00:00
|
|
|
GetOurAddressInfo(AddressInfo& addr) const;
|
2018-09-03 13:10:56 +00:00
|
|
|
|
|
|
|
virtual uint16_t
|
|
|
|
Rank() const = 0;
|
|
|
|
|
|
|
|
virtual bool
|
2018-09-04 12:41:25 +00:00
|
|
|
KeyGen(SecretKey&) = 0;
|
2018-09-03 13:10:56 +00:00
|
|
|
|
|
|
|
const byte_t*
|
|
|
|
TransportPubKey() const;
|
2018-09-02 18:25:42 +00:00
|
|
|
|
2018-09-04 12:41:25 +00:00
|
|
|
const byte_t*
|
|
|
|
TransportSecretKey() const;
|
|
|
|
|
2018-09-02 18:25:42 +00:00
|
|
|
bool
|
2018-09-03 13:10:56 +00:00
|
|
|
EnsureKeys(const char* fpath);
|
|
|
|
|
2018-09-04 19:15:06 +00:00
|
|
|
void
|
2018-09-07 17:41:49 +00:00
|
|
|
MapAddr(const PubKey& pk, ILinkSession* s);
|
2018-09-04 19:15:06 +00:00
|
|
|
|
2018-09-06 20:31:58 +00:00
|
|
|
virtual void
|
|
|
|
Tick(llarp_time_t now)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-09-03 13:10:56 +00:00
|
|
|
private:
|
|
|
|
static void
|
|
|
|
on_timer_tick(void* user, uint64_t orig, uint64_t left)
|
2018-09-02 18:25:42 +00:00
|
|
|
{
|
2018-09-03 13:10:56 +00:00
|
|
|
// timer cancelled
|
|
|
|
if(left)
|
|
|
|
return;
|
2018-09-06 20:31:58 +00:00
|
|
|
static_cast< ILinkLayer* >(user)->OnTick(orig, llarp_time_now_ms());
|
2018-09-02 18:25:42 +00:00
|
|
|
}
|
|
|
|
|
2018-09-03 13:10:56 +00:00
|
|
|
void
|
2018-09-06 20:31:58 +00:00
|
|
|
OnTick(uint64_t interval, llarp_time_t now);
|
2018-09-03 13:10:56 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
ScheduleTick(uint64_t interval);
|
|
|
|
|
|
|
|
uint32_t tick_id;
|
2018-09-02 18:25:42 +00:00
|
|
|
|
|
|
|
protected:
|
2018-09-10 13:43:36 +00:00
|
|
|
typedef util::NullLock Lock;
|
|
|
|
typedef util::NullMutex Mutex;
|
|
|
|
|
2018-09-04 12:41:25 +00:00
|
|
|
void
|
2018-09-10 13:43:36 +00:00
|
|
|
PutSession(ILinkSession* s);
|
2018-09-04 12:41:25 +00:00
|
|
|
|
2018-09-03 13:10:56 +00:00
|
|
|
llarp_logic* m_Logic = nullptr;
|
2018-09-02 18:25:42 +00:00
|
|
|
Addr m_ourAddr;
|
|
|
|
llarp_udp_io m_udp;
|
2018-09-03 13:10:56 +00:00
|
|
|
SecretKey m_SecretKey;
|
2018-09-07 20:36:06 +00:00
|
|
|
|
2018-09-10 13:43:36 +00:00
|
|
|
Mutex m_AuthedLinksMutex;
|
2018-09-07 20:36:06 +00:00
|
|
|
std::unordered_multimap< PubKey, std::unique_ptr< ILinkSession >,
|
|
|
|
PubKey::Hash >
|
|
|
|
m_AuthedLinks;
|
2018-09-10 13:43:36 +00:00
|
|
|
Mutex m_PendingMutex;
|
2018-09-07 20:36:06 +00:00
|
|
|
std::list< std::unique_ptr< ILinkSession > > m_Pending;
|
2018-09-02 18:25:42 +00:00
|
|
|
};
|
|
|
|
} // namespace llarp
|
|
|
|
|
|
|
|
#endif
|