lokinet/llarp/link/iwp_internal.hpp

370 lines
9.6 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>
#include <crypto/types.hpp>
2019-01-02 14:14:02 +00:00
#include <link/server.hpp>
#include <link/session.hpp>
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;
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();
void
PumpIO();
void
2019-01-05 13:45:05 +00:00
TickIO(llarp_time_t now);
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();
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
struct FragmentHeader
{
/// protocol version, always LLARP_PROTO_VERSION
Proto_t version = LLARP_PROTO_VERSION;
/// fragment command type
Cmd_t cmd = 0;
2019-01-04 12:43:53 +00:00
/// if cmd is XMIT this is the number of additional fragments this
/// message has
/// if cmd is FACK this is the fragment bitfield of the
/// messages acked otherwise 0
2019-01-03 21:10:40 +00:00
Flags_t flags = 0;
/// if cmd is XMIT this is the fragment index
/// if cmd is FACK this is set to 0xff to indicate message drop
/// otherwise set to 0
/// any other cmd it is set to 0
Fragno_t fragno = 0;
/// if cmd is XMIT then this is the size of the current fragment
/// if cmd is FACK then this MUST be set to 0
FragLen_t fraglen = 0;
/// if cmd is XMIT or FACK this is the sequence number of the message
/// otherwise it's 0
Seqno_t seqno = 0;
bool
Decode(llarp_buffer_t *buf)
{
if(llarp_buffer_size_left(*buf) < fragoverhead)
return false;
version = *buf->cur;
if(version != LLARP_PROTO_VERSION)
return false;
buf->cur++;
cmd = *buf->cur;
buf->cur++;
flags = *buf->cur;
buf->cur++;
fragno = *buf->cur;
buf->cur++;
llarp_buffer_read_uint16(buf, &fraglen);
llarp_buffer_read_uint32(buf, &seqno);
return fraglen <= fragsize;
}
bool
2019-02-02 23:12:42 +00:00
Encode(llarp_buffer_t *buf, const llarp_buffer_t &body)
2019-01-03 21:10:40 +00:00
{
if(body.sz > fragsize)
return false;
fraglen = body.sz;
if(llarp_buffer_size_left(*buf) < (fragoverhead + fraglen))
return false;
*buf->cur = LLARP_PROTO_VERSION;
buf->cur++;
*buf->cur = cmd;
buf->cur++;
*buf->cur = flags;
buf->cur++;
*buf->cur = fragno;
buf->cur++;
llarp_buffer_put_uint16(buf, fraglen);
llarp_buffer_put_uint32(buf, seqno);
2019-01-04 12:43:53 +00:00
if(fraglen)
memcpy(buf->cur, body.base, fraglen);
2019-01-03 21:10:40 +00:00
buf->cur += fraglen;
return true;
}
};
struct MessageState
{
/// default
MessageState(){};
/// inbound
MessageState(Flags_t numfrags)
{
acks.set();
if(numfrags <= maxfrags)
{
while(numfrags)
acks.reset(maxfrags - (numfrags--));
}
else // invalid value
return;
}
/// outbound
MessageState(const llarp_buffer_t &buf)
2019-01-03 21:10:40 +00:00
{
sz = std::min(buf.sz, MAX_LINK_MSG_SIZE);
memcpy(msg.data(), buf.base, sz);
size_t idx = 0;
acks.set();
while(idx * fragsize < sz)
acks.reset(idx++);
};
/// which fragments have we got
std::bitset< maxfrags > acks;
/// the message buffer
MessageBuffer_t msg;
/// the message's size
FragLen_t sz;
/// the last activity we have had
llarp_time_t lastActiveAt;
/// return true if this message is to be removed
/// because of inactivity
bool
IsExpired(llarp_time_t now) const
{
return now > lastActiveAt && now - lastActiveAt > 2000;
}
bool
IsDone() const
{
return acks.all();
}
bool
ShouldRetransmit(llarp_time_t now) const
{
if(IsDone())
return false;
return now > lastActiveAt && now - lastActiveAt > 500;
}
2019-01-05 13:45:05 +00:00
template < typename write_pkt_func >
2019-01-04 12:43:53 +00:00
bool
2019-01-05 13:45:05 +00:00
TransmitUnacked(write_pkt_func write_pkt, Seqno_t seqno) const
2019-01-03 21:10:40 +00:00
{
2019-01-05 13:45:05 +00:00
static FragLen_t maxfragsize = fragsize;
2019-01-04 12:43:53 +00:00
FragmentHeader hdr;
hdr.seqno = seqno;
hdr.cmd = XMIT;
AlignedBuffer< fragoverhead + fragsize > frag;
2019-02-03 00:31:10 +00:00
llarp_buffer_t buf(frag);
2019-01-04 12:43:53 +00:00
const byte_t *ptr = msg.data();
Fragno_t idx = 0;
FragLen_t len = sz;
while(idx < maxfrags)
{
2019-01-05 13:45:05 +00:00
const FragLen_t l = std::min(len, maxfragsize);
2019-01-04 12:43:53 +00:00
if(!acks.test(idx))
{
hdr.fragno = idx;
hdr.fraglen = l;
2019-02-02 23:12:42 +00:00
if(!hdr.Encode(&buf, llarp_buffer_t(ptr, l)))
2019-01-04 12:43:53 +00:00
return false;
buf.sz = buf.cur - buf.base;
buf.cur = buf.base;
len -= l;
2019-01-05 13:45:05 +00:00
if(write_pkt(buf.base, buf.sz) != int(buf.sz))
2019-01-04 12:43:53 +00:00
return false;
}
ptr += l;
len -= l;
if(l >= fragsize)
++idx;
else
break;
}
return true;
2019-01-03 21:10:40 +00:00
}
2019-01-05 13:45:05 +00:00
template < typename write_pkt_func >
2019-01-04 12:43:53 +00:00
bool
2019-01-05 13:45:05 +00:00
TransmitAcks(write_pkt_func write_pkt, Seqno_t seqno)
2019-01-03 21:10:40 +00:00
{
2019-01-04 12:43:53 +00:00
FragmentHeader hdr;
hdr.seqno = seqno;
hdr.cmd = FACK;
hdr.flags = 0;
byte_t idx = 0;
while(idx < maxfrags)
{
if(acks.test(idx))
hdr.flags |= 1 << idx;
++idx;
}
hdr.fraglen = 0;
hdr.fragno = 0;
AlignedBuffer< fragoverhead > frag;
2019-02-03 00:31:10 +00:00
llarp_buffer_t buf(frag);
2019-02-02 23:12:42 +00:00
if(!hdr.Encode(&buf, llarp_buffer_t(nullptr, nullptr, 0)))
2019-01-04 12:43:53 +00:00
return false;
2019-01-05 13:45:05 +00:00
return write_pkt(buf.base, buf.sz) == int(buf.sz);
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,
const SecretKey &identitySecretKey, llarp::GetRCFunc getrc,
llarp::LinkMessageHandler h, llarp::SignBufferFunc sign,
2019-01-02 14:14:02 +00:00
llarp::SessionEstablishedHandler established,
llarp::SessionRenegotiateHandler reneg,
llarp::TimeoutHandler timeout,
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;
const byte_t *
IndentityKey() const
{
return m_IdentityKey.data();
}
const AlignedBuffer< 32 > &
CookieSec() const
{
return m_CookieSec;
}
2019-01-05 13:45:05 +00:00
RouterID
GetRouterID() const
{
return m_IdentityKey.toPublic();
}
2019-01-02 14:14:02 +00:00
private:
bool
SignBuffer(llarp::Signature &sig, llarp_buffer_t buf) const
{
return crypto->sign(sig, m_IdentityKey, buf);
}
2019-01-03 21:10:40 +00:00
const llarp::SecretKey m_IdentityKey;
AlignedBuffer< 32 > m_CookieSec;
2019-01-02 14:14:02 +00:00
/// handle ll recv
void
RecvFrom(const llarp::Addr &from, const void *buf, size_t sz) override;
};
2019-01-05 13:45:05 +00:00
} // namespace iwp
2019-01-02 14:14:02 +00:00
} // namespace llarp
#endif