lokinet/llarp/link/iwp_internal.hpp

323 lines
8.3 KiB
C++
Raw Normal View History

2019-01-05 13:45:05 +00:00
#ifndef LLARP_LINK_IWP_INTERNAL_HPP
#define LLARP_LINK_IWP_INTERNAL_HPP
#include <constants/link_layer.hpp>
#include <crypto/crypto.hpp>
2019-02-18 19:44:41 +00:00
#include <crypto/encrypted.hpp>
#include <crypto/types.hpp>
2019-01-02 14:14:02 +00:00
#include <link/server.hpp>
#include <link/session.hpp>
2019-03-07 15:17:29 +00:00
#include <array>
2019-01-03 21:10:40 +00:00
#include <bitset>
#include <deque>
2019-01-02 14:14:02 +00:00
namespace llarp
{
struct Crypto;
2019-01-05 13:45:05 +00:00
namespace iwp
2019-01-02 14:14:02 +00:00
{
struct LinkLayer;
2019-02-18 19:44:41 +00:00
using FlowID_t = llarp::AlignedBuffer< 32 >;
using OuterCommand_t = byte_t;
constexpr OuterCommand_t eOCMD_ObtainFlowID = 'O';
constexpr OuterCommand_t eOCMD_GiveFlowID = 'G';
constexpr OuterCommand_t eOCMD_Reject = 'R';
constexpr OuterCommand_t eOCMD_SessionNegotiate = 'S';
constexpr OuterCommand_t eOCMD_TransmitData = 'D';
using InnerCommand_t = byte_t;
constexpr InnerCommand_t eICMD_KeepAlive = 'k';
constexpr InnerCommand_t eICMD_KeepAliveAck = 'l';
constexpr InnerCommand_t eICMD_Congestion = 'c';
constexpr InnerCommand_t eICMD_AntiCongestion = 'd';
constexpr InnerCommand_t eICMD_Transmit = 't';
constexpr InnerCommand_t eICMD_Ack = 'a';
constexpr InnerCommand_t eICMD_RotateKeys = 'r';
constexpr InnerCommand_t eICMD_UpgradeProtocol = 'u';
constexpr InnerCommand_t eICMD_VersionUpgrade = 'v';
struct OuterMessage
{
// required memebers
byte_t command;
FlowID_t flow;
2019-03-07 15:17:29 +00:00
OuterMessage();
~OuterMessage();
// static members
static std::array< byte_t, 6 > obtain_flow_id_magic;
static std::array< byte_t, 6 > give_flow_id_magic;
void
CreateReject(const char *msg, llarp_time_t now, const PubKey &pk);
2019-02-18 19:44:41 +00:00
// optional memebers follow
2019-03-07 15:17:29 +00:00
std::array< byte_t, 6 > magic;
2019-02-18 19:44:41 +00:00
NetID netid;
2019-03-07 15:17:29 +00:00
// either timestamp or counter
uint64_t uinteger;
std::array< byte_t, 14 > reject;
2019-02-18 19:44:41 +00:00
AlignedBuffer< 24 > N;
2019-03-07 15:17:29 +00:00
PubKey pubkey;
std::unique_ptr< AlignedBuffer< 32 > > A;
static constexpr size_t ipv6_mtu = 1280;
static constexpr size_t overhead_size = 16 + 24 + 32;
static constexpr size_t payload_size = ipv6_mtu - overhead_size;
AlignedBuffer< payload_size > X;
2019-02-18 19:44:41 +00:00
size_t Xsize;
2019-03-07 15:17:29 +00:00
ShortHash Zhash;
Signature Zsig;
2019-02-18 19:44:41 +00:00
/// encode to buffer
bool
Encode(llarp_buffer_t *buf) const;
/// decode from buffer
bool
Decode(llarp_buffer_t *buf);
/// clear members
void
Clear();
};
/// TODO: fixme
constexpr size_t MaxFrags = 8;
using MessageBuffer_t = AlignedBuffer< MAX_LINK_MSG_SIZE >;
using FragmentLen_t = uint16_t;
using SequenceNum_t = uint32_t;
using WritePacketFunc = std::function< void(const llarp_buffer_t &) >;
struct MessageState
{
/// default
MessageState();
/// inbound
MessageState(const ShortHash &digest, SequenceNum_t num);
/// outbound
MessageState(const ShortHash &digest, const llarp_buffer_t &buf,
SequenceNum_t num);
/// the expected hash of the message
const ShortHash expectedHash;
/// which fragments have we got
std::bitset< MaxFrags > acks;
/// the message buffer
MessageBuffer_t msg;
/// the message's size
FragmentLen_t sz;
/// the last activity we have had
llarp_time_t lastActiveAt;
// sequence number
const SequenceNum_t seqno;
/// return true if this message is to be removed
/// because of inactivity
bool
IsExpired(llarp_time_t now) const;
/// return true if we have recvieved or sent the underlying message in
/// full.
bool
IsDone() const;
/// return true if we should retransmit some packets
bool
ShouldRetransmit(llarp_time_t now) const;
/// transmit unacked fragments
bool
TransmitUnacked(WritePacketFunc write_pkt) const;
/// transmit acks packet
bool
TransmitAcks(WritePacketFunc write_pkt);
};
2019-01-02 14:14:02 +00:00
struct Session final : public llarp::ILinkSession
{
/// base
Session(LinkLayer *parent);
/// inbound
Session(LinkLayer *parent, const llarp::Addr &from);
/// outbound
Session(LinkLayer *parent, const RouterContact &rc,
const AddressInfo &ai);
~Session();
util::StatusObject
ExtractStatus() const override
{
// TODO: fill me in.
return {};
}
2019-02-18 19:44:41 +00:00
/// pump ll io
2019-01-02 14:14:02 +00:00
void
PumpIO();
2019-02-18 19:44:41 +00:00
/// tick every 1 s
2019-01-02 14:14:02 +00:00
void
2019-01-05 13:45:05 +00:00
TickIO(llarp_time_t now);
2019-02-18 19:44:41 +00:00
/// queue full message
2019-01-05 13:45:05 +00:00
bool
2019-02-02 23:12:42 +00:00
QueueMessageBuffer(const llarp_buffer_t &buf);
2019-01-02 14:14:02 +00:00
2019-01-05 13:45:05 +00:00
/// return true if the session is established and handshaked and all that
/// jazz
2019-01-02 14:14:02 +00:00
bool
2019-01-05 13:45:05 +00:00
SessionIsEstablished();
2019-01-02 14:14:02 +00:00
/// inbound start
void
Accept();
/// sendclose
void
Close();
2019-02-18 19:44:41 +00:00
/// start outbound handshake
2019-01-02 14:14:02 +00:00
void
Connect();
// set tls config
void
Configure();
2019-01-05 13:45:05 +00:00
/// low level recv
2019-01-02 14:14:02 +00:00
void
Recv_ll(const void *buf, size_t sz);
2019-01-05 13:45:05 +00:00
/// verify a lim
bool
VerfiyLIM(const llarp::LinkIntroMessage *msg);
2019-01-03 21:10:40 +00:00
2019-01-05 13:45:05 +00:00
SharedSecret m_TXKey;
SharedSecret m_RXKey;
2019-01-02 14:14:02 +00:00
LinkLayer *m_Parent;
2019-01-03 21:10:40 +00:00
llarp::Crypto *const crypto;
2019-01-02 14:14:02 +00:00
llarp::RouterContact remoteRC;
llarp::Addr remoteAddr;
2019-01-03 21:10:40 +00:00
using MessageBuffer_t = llarp::AlignedBuffer< MAX_LINK_MSG_SIZE >;
using Seqno_t = uint32_t;
using Proto_t = uint8_t;
using FragLen_t = uint16_t;
using Flags_t = uint8_t;
using Fragno_t = uint8_t;
using Cmd_t = uint8_t;
static constexpr size_t fragoverhead = sizeof(Proto_t) + sizeof(Cmd_t)
+ sizeof(Flags_t) + sizeof(Fragno_t) + sizeof(FragLen_t)
+ sizeof(Seqno_t);
/// keepalive command
static constexpr Cmd_t PING = 0;
/// transmit fragment command
static constexpr Cmd_t XMIT = 1;
/// fragment ack command
static constexpr Cmd_t FACK = 2;
/// maximum number of fragments
static constexpr uint8_t maxfrags = 8;
/// maximum fragment size
2019-01-04 12:43:53 +00:00
static constexpr FragLen_t fragsize = MAX_LINK_MSG_SIZE / maxfrags;
2019-01-03 21:10:40 +00:00
using MessageHolder_t = std::unordered_map< Seqno_t, MessageState >;
MessageHolder_t m_Inbound;
MessageHolder_t m_Outbound;
using Buf_t = std::vector< byte_t >;
using IOQueue_t = std::deque< Buf_t >;
IOQueue_t ll_recv;
IOQueue_t ll_send;
2019-01-02 14:14:02 +00:00
};
struct LinkLayer final : public llarp::ILinkLayer
{
2019-01-04 12:43:53 +00:00
LinkLayer(llarp::Crypto *crypto, const SecretKey &encryptionSecretKey,
2019-02-18 19:44:41 +00:00
llarp::GetRCFunc getrc, llarp::LinkMessageHandler h,
2019-01-02 14:14:02 +00:00
llarp::SessionEstablishedHandler established,
llarp::SessionRenegotiateHandler reneg,
2019-02-18 19:44:41 +00:00
llarp::SignBufferFunc sign, llarp::TimeoutHandler timeout,
2019-01-02 14:14:02 +00:00
llarp::SessionClosedHandler closed);
~LinkLayer();
llarp::Crypto *const crypto;
bool
Start(llarp::Logic *l) override;
ILinkSession *
NewOutboundSession(const llarp::RouterContact &rc,
const llarp::AddressInfo &ai) override;
void
Pump() override;
2019-01-03 21:10:40 +00:00
bool
KeyGen(SecretKey &k) override;
const char *
Name() const override;
uint16_t
Rank() const override;
2019-03-07 15:17:29 +00:00
/// verify that a new flow id matches addresses and pubkey
2019-02-18 19:44:41 +00:00
bool
2019-03-07 15:17:29 +00:00
VerifyFlowID(const PubKey &pk, const Addr &from,
const FlowID_t &flow) const;
2019-01-03 21:10:40 +00:00
2019-02-18 19:44:41 +00:00
void
RecvFrom(const llarp::Addr &from, const void *buf, size_t sz) override;
2019-01-05 13:45:05 +00:00
2019-01-02 14:14:02 +00:00
private:
2019-03-07 15:17:29 +00:00
bool
GenFlowIDFor(const PubKey &pk, const Addr &from, FlowID_t &flow) const;
2019-01-02 14:14:02 +00:00
bool
2019-02-18 19:44:41 +00:00
ShouldSendFlowID(const Addr &from) const;
2019-01-02 14:14:02 +00:00
void
2019-03-07 15:17:29 +00:00
SendReject(const Addr &to, const char *msg);
2019-02-18 19:44:41 +00:00
void
SendFlowID(const Addr &to, const FlowID_t &flow);
using ActiveFlows_t =
std::unordered_map< FlowID_t, RouterID, FlowID_t::Hash >;
ActiveFlows_t m_ActiveFlows;
using PendingFlows_t = std::unordered_map< Addr, FlowID_t, Addr::Hash >;
/// flows that are pending authentication
PendingFlows_t m_PendingFlows;
/// cookie used in flow id computation
AlignedBuffer< 32 > m_FlowCookie;
OuterMessage m_OuterMsg;
2019-01-02 14:14:02 +00:00
};
2019-01-05 13:45:05 +00:00
} // namespace iwp
2019-01-02 14:14:02 +00:00
} // namespace llarp
#endif