2018-07-30 04:38:14 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define NOMINMAX
|
|
|
|
#endif
|
|
|
|
|
2018-07-09 04:12:43 +00:00
|
|
|
#include "llarp/iwp/frame_state.hpp"
|
2018-08-08 17:43:46 +00:00
|
|
|
#include <algorithm>
|
2018-07-09 04:12:43 +00:00
|
|
|
#include "buffer.hpp"
|
|
|
|
#include "llarp/crypto.hpp"
|
2018-07-24 07:06:42 +00:00
|
|
|
#include "llarp/endian.h"
|
|
|
|
#include "llarp/iwp/inbound_message.hpp"
|
|
|
|
#include "llarp/iwp/session.hpp"
|
2018-07-09 04:12:43 +00:00
|
|
|
#include "llarp/logger.hpp"
|
2018-07-19 04:58:39 +00:00
|
|
|
#include "mem.hpp"
|
2018-07-09 04:12:43 +00:00
|
|
|
#include "router.hpp"
|
|
|
|
|
2018-07-09 14:24:44 +00:00
|
|
|
llarp_router *
|
|
|
|
frame_state::Router()
|
|
|
|
{
|
|
|
|
return parent->Router();
|
|
|
|
}
|
|
|
|
|
2018-07-09 04:12:43 +00:00
|
|
|
bool
|
|
|
|
frame_state::process_inbound_queue()
|
|
|
|
{
|
2018-08-02 04:34:46 +00:00
|
|
|
uint64_t last = 0;
|
2018-08-17 19:49:58 +00:00
|
|
|
recvqueue.Process([&](const std::unique_ptr< InboundMessage > &msg) {
|
|
|
|
if(last != msg->msgid)
|
2018-07-20 04:50:28 +00:00
|
|
|
{
|
2018-08-17 19:49:58 +00:00
|
|
|
auto buffer = msg->Buffer();
|
2018-07-20 04:50:28 +00:00
|
|
|
if(!Router()->HandleRecvLinkMessage(parent, buffer))
|
|
|
|
{
|
2018-08-17 19:49:58 +00:00
|
|
|
llarp::LogWarn("failed to process inbound message ", msg->msgid);
|
2018-07-20 04:50:28 +00:00
|
|
|
llarp::DumpBuffer< llarp_buffer_t, 128 >(buffer);
|
|
|
|
}
|
2018-08-17 19:49:58 +00:00
|
|
|
last = msg->msgid;
|
2018-07-09 04:12:43 +00:00
|
|
|
}
|
2018-08-02 04:34:46 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp::LogWarn("duplicate inbound message ", last);
|
|
|
|
}
|
2018-08-17 19:49:58 +00:00
|
|
|
});
|
2018-07-09 04:12:43 +00:00
|
|
|
// TODO: this isn't right
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
frame_state::flags_agree(byte_t flags) const
|
|
|
|
{
|
|
|
|
return ((rxflags & flags) & (txflags & flags)) == flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
frame_state::clear()
|
|
|
|
{
|
|
|
|
auto _rx = rx;
|
|
|
|
auto _tx = tx;
|
|
|
|
for(auto &item : _rx)
|
|
|
|
delete item.second;
|
|
|
|
for(auto &item : _tx)
|
|
|
|
delete item.second;
|
|
|
|
rx.clear();
|
|
|
|
tx.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
frame_state::got_xmit(frame_header hdr, size_t sz)
|
|
|
|
{
|
|
|
|
if(hdr.size() > sz)
|
|
|
|
{
|
|
|
|
// overflow
|
2018-07-09 14:24:44 +00:00
|
|
|
llarp::LogWarn("invalid XMIT frame size ", hdr.size(), " > ", sz);
|
2018-07-09 04:12:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
sz = hdr.size();
|
|
|
|
|
|
|
|
// extract xmit data
|
|
|
|
xmit x(hdr.data());
|
|
|
|
|
|
|
|
const auto bufsz = sizeof(x.buffer);
|
|
|
|
|
|
|
|
if(sz - bufsz < x.lastfrag())
|
|
|
|
{
|
|
|
|
// bad size of last fragment
|
2018-07-09 14:24:44 +00:00
|
|
|
llarp::LogWarn("XMIT frag size missmatch ", sz - bufsz, " < ",
|
|
|
|
x.lastfrag());
|
2018-07-09 04:12:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check LSB set on flags
|
|
|
|
if(x.flags() & 0x01)
|
|
|
|
{
|
|
|
|
auto id = x.msgid();
|
2018-07-19 04:58:39 +00:00
|
|
|
auto h = x.hash();
|
|
|
|
auto itr = rx.find(h);
|
2018-07-09 04:12:43 +00:00
|
|
|
if(itr == rx.end())
|
|
|
|
{
|
2018-07-19 04:58:39 +00:00
|
|
|
auto msg = new transit_message(x);
|
|
|
|
rx[h] = msg;
|
|
|
|
rxIDs[id] = h;
|
2018-07-09 14:24:44 +00:00
|
|
|
llarp::LogDebug("got message XMIT with ", (int)x.numfrags(),
|
2018-07-16 21:26:16 +00:00
|
|
|
" fragment"
|
|
|
|
"s");
|
2018-07-09 04:12:43 +00:00
|
|
|
// inserted, put last fragment
|
|
|
|
msg->put_lastfrag(hdr.data() + sizeof(x.buffer), x.lastfrag());
|
|
|
|
push_ackfor(id, 0);
|
|
|
|
if(x.numfrags() == 0)
|
|
|
|
{
|
|
|
|
return inbound_frame_complete(id);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
2018-07-23 21:59:43 +00:00
|
|
|
llarp::LogWarn("duplicate XMIT h=", llarp::ShortHash(h));
|
2018-07-09 04:12:43 +00:00
|
|
|
}
|
|
|
|
else
|
2018-07-09 14:24:44 +00:00
|
|
|
llarp::LogWarn("LSB not set on flags");
|
2018-07-09 04:12:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
frame_state::got_frag(frame_header hdr, size_t sz)
|
|
|
|
{
|
|
|
|
if(hdr.size() > sz)
|
|
|
|
{
|
|
|
|
// overflow
|
2018-07-09 14:24:44 +00:00
|
|
|
llarp::LogWarn("invalid FRAG frame size ", hdr.size(), " > ", sz);
|
2018-07-09 04:12:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
sz = hdr.size();
|
|
|
|
|
|
|
|
if(sz <= 9)
|
|
|
|
{
|
|
|
|
// underflow
|
2018-07-09 14:24:44 +00:00
|
|
|
llarp::LogWarn("invalid FRAG frame size ", sz, " <= 9");
|
2018-07-09 04:12:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t msgid;
|
|
|
|
byte_t fragno;
|
2018-07-29 23:29:36 +00:00
|
|
|
msgid = bufbe64toh(hdr.data());
|
|
|
|
fragno = hdr.data()[8];
|
2018-07-19 04:58:39 +00:00
|
|
|
auto idItr = rxIDs.find(msgid);
|
|
|
|
if(idItr == rxIDs.end())
|
|
|
|
{
|
2018-07-30 01:29:22 +00:00
|
|
|
push_ackfor(msgid, ~0);
|
2018-07-19 04:58:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
auto itr = rx.find(idItr->second);
|
2018-07-09 04:12:43 +00:00
|
|
|
if(itr == rx.end())
|
|
|
|
{
|
2018-07-30 01:29:22 +00:00
|
|
|
push_ackfor(msgid, ~0);
|
2018-07-09 04:12:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
auto fragsize = itr->second->msginfo.fragsize();
|
|
|
|
if(fragsize != sz - 9)
|
|
|
|
{
|
2018-07-09 14:24:44 +00:00
|
|
|
llarp::LogWarn("RX fragment size missmatch ", fragsize, " != ", sz - 9);
|
2018-07-09 04:12:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-07-09 14:24:44 +00:00
|
|
|
llarp::LogDebug("RX got fragment ", (int)fragno, " msgid=", msgid);
|
2018-07-09 04:12:43 +00:00
|
|
|
if(!itr->second->put_frag(fragno, hdr.data() + 9))
|
|
|
|
{
|
2018-07-09 14:24:44 +00:00
|
|
|
llarp::LogWarn("inbound message does not have fragment msgid=", msgid,
|
|
|
|
" fragno=", (int)fragno);
|
2018-07-09 04:12:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto mask = itr->second->get_bitmask();
|
|
|
|
if(itr->second->completed())
|
|
|
|
{
|
|
|
|
push_ackfor(msgid, mask);
|
|
|
|
return inbound_frame_complete(msgid);
|
|
|
|
}
|
|
|
|
else if(itr->second->should_send_ack(llarp_time_now_ms()))
|
|
|
|
{
|
|
|
|
push_ackfor(msgid, mask);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
frame_state::push_ackfor(uint64_t id, uint32_t bitmask)
|
|
|
|
{
|
2018-07-09 14:24:44 +00:00
|
|
|
llarp::LogDebug("ACK for msgid=", id, " mask=", bitmask);
|
2018-08-17 19:49:58 +00:00
|
|
|
auto pkt = std::unique_ptr< sendbuf_t >(new sendbuf_t(12 + 6));
|
|
|
|
auto body_ptr = init_sendbuf(pkt.get(), eACKS, 12, txflags);
|
2018-07-24 07:06:42 +00:00
|
|
|
htobe64buf(body_ptr, id);
|
|
|
|
htobe32buf(body_ptr + 8, bitmask);
|
2018-07-20 04:50:28 +00:00
|
|
|
sendqueue.Put(pkt);
|
2018-07-09 04:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
frame_state::inbound_frame_complete(uint64_t id)
|
|
|
|
{
|
|
|
|
bool success = false;
|
|
|
|
std::vector< byte_t > msg;
|
2018-07-19 04:58:39 +00:00
|
|
|
auto rxmsg = rx[rxIDs[id]];
|
|
|
|
llarp::ShortHash digest;
|
2018-07-09 04:12:43 +00:00
|
|
|
if(rxmsg->reassemble(msg))
|
|
|
|
{
|
2018-07-09 14:24:44 +00:00
|
|
|
auto router = Router();
|
2018-07-19 04:58:39 +00:00
|
|
|
auto buf = llarp::Buffer< decltype(msg) >(msg);
|
2018-07-09 04:12:43 +00:00
|
|
|
router->crypto.shorthash(digest, buf);
|
|
|
|
if(memcmp(digest, rxmsg->msginfo.hash(), 32))
|
|
|
|
{
|
2018-07-09 14:24:44 +00:00
|
|
|
llarp::LogWarn("message hash missmatch ",
|
2018-07-17 04:37:50 +00:00
|
|
|
llarp::AlignedBuffer< 32 >(digest),
|
|
|
|
" != ", llarp::AlignedBuffer< 32 >(rxmsg->msginfo.hash()));
|
2018-07-09 04:12:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-11 16:11:19 +00:00
|
|
|
llarp_link_session *impl = parent;
|
|
|
|
|
|
|
|
if(id == 0)
|
|
|
|
{
|
|
|
|
success = router->HandleRecvLinkMessage(parent, buf);
|
|
|
|
if(impl->CheckRCValid())
|
2018-07-09 04:12:43 +00:00
|
|
|
{
|
2018-07-11 16:11:19 +00:00
|
|
|
if(!impl->IsEstablished())
|
2018-07-09 04:12:43 +00:00
|
|
|
{
|
2018-07-11 16:11:19 +00:00
|
|
|
impl->send_LIM();
|
|
|
|
impl->session_established();
|
2018-07-09 04:12:43 +00:00
|
|
|
}
|
2018-07-11 16:11:19 +00:00
|
|
|
++nextMsgID;
|
2018-07-09 04:12:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-07-11 16:11:19 +00:00
|
|
|
llarp::PubKey k = impl->remote_router.pubkey;
|
|
|
|
llarp::LogWarn("spoofed LIM from ", k);
|
|
|
|
impl->close();
|
|
|
|
success = false;
|
2018-07-09 04:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-17 19:49:58 +00:00
|
|
|
std::unique_ptr< InboundMessage > m =
|
|
|
|
std::unique_ptr< InboundMessage >(new InboundMessage(id, msg));
|
|
|
|
recvqueue.Put(m);
|
2018-07-09 04:12:43 +00:00
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
}
|
2018-07-11 16:11:19 +00:00
|
|
|
|
2018-07-09 04:12:43 +00:00
|
|
|
delete rxmsg;
|
2018-07-19 04:58:39 +00:00
|
|
|
rxIDs.erase(id);
|
|
|
|
rx.erase(digest);
|
2018-07-09 04:12:43 +00:00
|
|
|
|
|
|
|
if(!success)
|
2018-07-09 14:24:44 +00:00
|
|
|
llarp::LogWarn("Failed to process inbound message ", id);
|
2018-07-09 04:12:43 +00:00
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
frame_state::got_acks(frame_header hdr, size_t sz)
|
|
|
|
{
|
|
|
|
if(hdr.size() > sz)
|
|
|
|
{
|
2018-07-09 14:24:44 +00:00
|
|
|
llarp::LogError("invalid ACKS frame size ", hdr.size(), " > ", sz);
|
2018-07-09 04:12:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
sz = hdr.size();
|
|
|
|
if(sz < 12)
|
|
|
|
{
|
2018-07-09 14:24:44 +00:00
|
|
|
llarp::LogError("invalid ACKS frame size ", sz, " < 12");
|
2018-07-09 04:12:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-24 22:34:46 +00:00
|
|
|
auto ptr = hdr.data();
|
|
|
|
uint64_t msgid = bufbe64toh(ptr);
|
|
|
|
uint32_t bitmask = bufbe32toh(ptr + 8);
|
2018-07-09 04:12:43 +00:00
|
|
|
|
|
|
|
auto itr = tx.find(msgid);
|
|
|
|
if(itr == tx.end())
|
|
|
|
{
|
2018-07-09 14:24:44 +00:00
|
|
|
llarp::LogDebug("ACK for missing TX frame msgid=", msgid);
|
2018-07-09 04:12:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-30 01:29:22 +00:00
|
|
|
auto now = llarp_time_now_ms();
|
2018-07-09 04:12:43 +00:00
|
|
|
|
2018-07-30 01:29:22 +00:00
|
|
|
transit_message *msg = itr->second;
|
2018-07-09 04:12:43 +00:00
|
|
|
|
2018-07-30 01:29:22 +00:00
|
|
|
if(bitmask == ~(0U))
|
2018-07-09 04:12:43 +00:00
|
|
|
{
|
|
|
|
tx.erase(msgid);
|
|
|
|
delete msg;
|
|
|
|
}
|
2018-07-30 01:29:22 +00:00
|
|
|
else
|
2018-07-09 04:12:43 +00:00
|
|
|
{
|
2018-07-30 01:29:22 +00:00
|
|
|
msg->ack(bitmask);
|
2018-07-09 04:12:43 +00:00
|
|
|
|
2018-07-30 01:29:22 +00:00
|
|
|
if(msg->completed())
|
|
|
|
{
|
|
|
|
llarp::LogDebug("message transmitted msgid=", msgid);
|
|
|
|
tx.erase(msgid);
|
|
|
|
delete msg;
|
|
|
|
}
|
|
|
|
else if(msg->should_resend_frags(now))
|
|
|
|
{
|
|
|
|
llarp::LogDebug("message ", msgid, " retransmit fragments");
|
|
|
|
msg->retransmit_frags(sendqueue, txflags);
|
|
|
|
}
|
|
|
|
}
|
2018-07-09 04:12:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
frame_state::process(byte_t *buf, size_t sz)
|
|
|
|
{
|
|
|
|
frame_header hdr(buf);
|
|
|
|
if(hdr.flags() & eSessionInvalidated)
|
|
|
|
{
|
|
|
|
rxflags |= eSessionInvalidated;
|
|
|
|
}
|
|
|
|
switch(hdr.msgtype())
|
|
|
|
{
|
|
|
|
case eALIV:
|
2018-07-19 04:58:39 +00:00
|
|
|
// llarp::LogDebug("iwp_link::frame_state::process Got alive");
|
2018-07-09 04:12:43 +00:00
|
|
|
if(rxflags & eSessionInvalidated)
|
|
|
|
{
|
|
|
|
txflags |= eSessionInvalidated;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case eXMIT:
|
2018-07-09 14:24:44 +00:00
|
|
|
llarp::LogDebug("iwp_link::frame_state::process Got xmit");
|
2018-07-09 04:12:43 +00:00
|
|
|
return got_xmit(hdr, sz - 6);
|
|
|
|
case eACKS:
|
2018-07-09 14:24:44 +00:00
|
|
|
llarp::LogDebug("iwp_link::frame_state::process Got ack");
|
2018-07-09 04:12:43 +00:00
|
|
|
return got_acks(hdr, sz - 6);
|
|
|
|
case msgtype::eFRAG:
|
2018-07-09 14:24:44 +00:00
|
|
|
llarp::LogDebug("iwp_link::frame_state::process Got frag");
|
2018-07-09 04:12:43 +00:00
|
|
|
return got_frag(hdr, sz - 6);
|
|
|
|
default:
|
2018-07-09 14:24:44 +00:00
|
|
|
llarp::LogWarn(
|
2018-07-09 04:12:43 +00:00
|
|
|
"iwp_link::frame_state::process - unknown header message type: ",
|
|
|
|
(int)hdr.msgtype());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-20 04:50:28 +00:00
|
|
|
/*
|
2018-07-09 04:12:43 +00:00
|
|
|
bool
|
|
|
|
frame_state::next_frame(llarp_buffer_t *buf)
|
|
|
|
{
|
|
|
|
auto left = sendqueue.size();
|
|
|
|
if(left)
|
|
|
|
{
|
2018-07-18 20:58:16 +00:00
|
|
|
llarp::LogDebug("next frame, ", left, " frames left in send queue");
|
2018-07-11 16:11:19 +00:00
|
|
|
auto &send = sendqueue.front();
|
|
|
|
buf->base = send->data();
|
|
|
|
buf->cur = send->data();
|
|
|
|
buf->sz = send->size();
|
2018-07-09 04:12:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
frame_state::pop_next_frame()
|
|
|
|
{
|
2018-07-11 16:11:19 +00:00
|
|
|
auto &buf = sendqueue.front();
|
2018-07-09 04:12:43 +00:00
|
|
|
delete buf;
|
2018-07-11 16:11:19 +00:00
|
|
|
sendqueue.pop();
|
2018-07-09 04:12:43 +00:00
|
|
|
}
|
2018-07-20 04:50:28 +00:00
|
|
|
*/
|
2018-07-09 04:12:43 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
frame_state::queue_tx(uint64_t id, transit_message *msg)
|
|
|
|
{
|
|
|
|
tx.insert(std::make_pair(id, msg));
|
|
|
|
msg->generate_xmit(sendqueue, txflags);
|
2018-07-23 07:38:29 +00:00
|
|
|
// msg->retransmit_frags(sendqueue, txflags);
|
2018-07-09 04:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
frame_state::retransmit(llarp_time_t now)
|
|
|
|
{
|
|
|
|
for(auto &item : tx)
|
|
|
|
{
|
|
|
|
if(item.second->should_resend_xmit(now))
|
|
|
|
{
|
|
|
|
item.second->generate_xmit(sendqueue, txflags);
|
|
|
|
}
|
|
|
|
item.second->retransmit_frags(sendqueue, txflags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
frame_state::alive()
|
|
|
|
{
|
|
|
|
lastEvent = llarp_time_now_ms();
|
2018-08-17 19:49:58 +00:00
|
|
|
}
|