2018-12-12 01:32:10 +00:00
|
|
|
#include <link/utp.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
|
2019-02-11 19:45:42 +00:00
|
|
|
#include <crypto/crypto.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <link/server.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <messages/discard.hpp>
|
|
|
|
#include <messages/link_intro.hpp>
|
2019-02-11 19:45:42 +00:00
|
|
|
#include <router/abstractrouter.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/buffer.hpp>
|
|
|
|
#include <util/endian.hpp>
|
2018-09-04 12:41:25 +00:00
|
|
|
|
2018-09-06 20:31:58 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
#include <linux/errqueue.h>
|
|
|
|
#include <netinet/ip_icmp.h>
|
|
|
|
#endif
|
|
|
|
|
2018-09-25 08:31:29 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
2018-11-19 07:55:19 +00:00
|
|
|
#include <wspiapi.h>
|
2018-09-25 08:31:29 +00:00
|
|
|
#endif
|
|
|
|
|
2018-11-20 08:18:29 +00:00
|
|
|
#ifndef IP_DONTFRAGMENT
|
|
|
|
#define IP_DONTFRAGMENT IP_DONTFRAG
|
2018-09-25 08:31:29 +00:00
|
|
|
#endif
|
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
#include <link/utp_internal.hpp>
|
|
|
|
|
2018-09-04 12:41:25 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace utp
|
|
|
|
{
|
2019-01-26 15:40:58 +00:00
|
|
|
using namespace std::placeholders;
|
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
bool
|
|
|
|
InboundMessage::IsExpired(llarp_time_t now) const
|
2018-12-16 20:34:04 +00:00
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
return now > lastActive && now - lastActive >= 2000;
|
|
|
|
}
|
2018-12-16 20:34:04 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
bool
|
|
|
|
InboundMessage::AppendData(const byte_t* ptr, uint16_t sz)
|
2018-09-04 12:41:25 +00:00
|
|
|
{
|
2019-02-17 12:13:34 +00:00
|
|
|
if(buffer.size_left() < sz)
|
2018-12-17 20:46:08 +00:00
|
|
|
return false;
|
|
|
|
memcpy(buffer.cur, ptr, sz);
|
|
|
|
buffer.cur += sz;
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-04 19:15:06 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
void
|
|
|
|
Session::OnLinkEstablished(LinkLayer* p)
|
|
|
|
{
|
|
|
|
parent = p;
|
|
|
|
EnterState(eLinkEstablished);
|
2019-02-11 19:45:42 +00:00
|
|
|
LogDebug("link established with ", remoteAddr);
|
2018-12-17 20:46:08 +00:00
|
|
|
}
|
2018-09-04 12:41:25 +00:00
|
|
|
|
2019-02-11 19:45:42 +00:00
|
|
|
Crypto*
|
|
|
|
Session::OurCrypto()
|
2018-12-17 20:46:08 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
return parent->OurCrypto();
|
2018-12-17 20:46:08 +00:00
|
|
|
}
|
2018-09-06 11:46:19 +00:00
|
|
|
|
2019-02-11 19:45:42 +00:00
|
|
|
Crypto*
|
|
|
|
LinkLayer::OurCrypto()
|
2018-12-17 20:46:08 +00:00
|
|
|
{
|
|
|
|
return _crypto;
|
|
|
|
}
|
2018-09-04 12:41:25 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
/// pump tx queue
|
|
|
|
void
|
|
|
|
Session::PumpWrite()
|
|
|
|
{
|
|
|
|
if(!sock)
|
|
|
|
return;
|
|
|
|
ssize_t expect = 0;
|
|
|
|
std::vector< utp_iovec > vecs;
|
|
|
|
for(const auto& vec : vecq)
|
2018-09-04 12:41:25 +00:00
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
expect += vec.iov_len;
|
|
|
|
vecs.push_back(vec);
|
2018-09-04 12:41:25 +00:00
|
|
|
}
|
2018-12-17 20:46:08 +00:00
|
|
|
if(expect)
|
2018-09-04 12:41:25 +00:00
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
ssize_t s = utp_writev(sock, vecs.data(), vecs.size());
|
2019-02-25 12:46:29 +00:00
|
|
|
if(s < 0)
|
|
|
|
return;
|
2019-02-15 22:19:19 +00:00
|
|
|
m_TXRate += s;
|
2019-02-25 12:46:29 +00:00
|
|
|
size_t sz = s;
|
2019-02-25 12:51:01 +00:00
|
|
|
while(vecq.size() && sz >= vecq.front().iov_len)
|
2018-09-12 13:29:42 +00:00
|
|
|
{
|
2019-02-25 12:46:29 +00:00
|
|
|
sz -= vecq.front().iov_len;
|
2018-12-17 20:46:08 +00:00
|
|
|
vecq.pop_front();
|
|
|
|
sendq.pop_front();
|
2018-09-12 13:29:42 +00:00
|
|
|
}
|
2018-12-17 20:46:08 +00:00
|
|
|
if(vecq.size())
|
2018-09-12 13:29:42 +00:00
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
auto& front = vecq.front();
|
2019-02-25 12:46:29 +00:00
|
|
|
front.iov_len -= sz;
|
|
|
|
front.iov_base = ((byte_t*)front.iov_base) + sz;
|
2018-09-12 13:29:42 +00:00
|
|
|
}
|
2018-09-04 12:41:25 +00:00
|
|
|
}
|
2018-12-17 20:46:08 +00:00
|
|
|
}
|
2018-09-04 12:41:25 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
/// prune expired inbound messages
|
|
|
|
void
|
|
|
|
Session::PruneInboundMessages(llarp_time_t now)
|
|
|
|
{
|
|
|
|
auto itr = m_RecvMsgs.begin();
|
|
|
|
while(itr != m_RecvMsgs.end())
|
2018-09-06 11:46:19 +00:00
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
if(itr->second.IsExpired(now))
|
|
|
|
itr = m_RecvMsgs.erase(itr);
|
|
|
|
else
|
|
|
|
++itr;
|
2018-09-06 11:46:19 +00:00
|
|
|
}
|
2018-12-17 20:46:08 +00:00
|
|
|
}
|
2018-09-06 11:46:19 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
void
|
|
|
|
Session::Connect()
|
|
|
|
{
|
|
|
|
utp_connect(sock, remoteAddr, remoteAddr.SockLen());
|
|
|
|
EnterState(eConnecting);
|
|
|
|
}
|
2018-09-06 11:46:19 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
void
|
|
|
|
Session::OutboundLinkEstablished(LinkLayer* p)
|
|
|
|
{
|
|
|
|
OnLinkEstablished(p);
|
2018-09-09 11:23:21 +00:00
|
|
|
OutboundHandshake();
|
2018-12-17 20:46:08 +00:00
|
|
|
}
|
2018-09-06 11:46:19 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
bool
|
|
|
|
Session::DoKeyExchange(transport_dh_func dh, SharedSecret& K,
|
|
|
|
const KeyExchangeNonce& n, const PubKey& other,
|
2019-01-02 01:04:04 +00:00
|
|
|
const SecretKey& secret)
|
2018-12-17 20:46:08 +00:00
|
|
|
{
|
|
|
|
ShortHash t_h;
|
2019-01-02 01:04:04 +00:00
|
|
|
static constexpr size_t TMP_SIZE = 64;
|
|
|
|
static_assert(SharedSecret::SIZE + KeyExchangeNonce::SIZE == TMP_SIZE,
|
|
|
|
"Invalid sizes");
|
|
|
|
|
|
|
|
AlignedBuffer< TMP_SIZE > tmp;
|
2019-01-02 01:03:53 +00:00
|
|
|
std::copy(K.begin(), K.end(), tmp.begin());
|
|
|
|
std::copy(n.begin(), n.end(), tmp.begin() + K.size());
|
2018-12-17 20:46:08 +00:00
|
|
|
// t_h = HS(K + L.n)
|
2019-02-11 19:45:42 +00:00
|
|
|
if(!OurCrypto()->shorthash(t_h, llarp_buffer_t(tmp)))
|
2018-09-04 12:41:25 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError("failed to mix key to ", remoteAddr);
|
2018-12-17 20:46:08 +00:00
|
|
|
return false;
|
2018-09-04 12:41:25 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
// K = TKE(a.p, B_a.e, sk, t_h)
|
2019-01-02 01:04:04 +00:00
|
|
|
if(!dh(K, other, secret, t_h))
|
2018-12-16 20:34:04 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError("key exchange with ", other, " failed");
|
2018-12-17 20:46:08 +00:00
|
|
|
return false;
|
2018-12-16 20:34:04 +00:00
|
|
|
}
|
2019-02-11 19:45:42 +00:00
|
|
|
LogDebug("keys mixed with session to ", remoteAddr);
|
2018-12-17 20:46:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-12-16 20:34:04 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
bool
|
|
|
|
Session::MutateKey(SharedSecret& K, const AlignedBuffer< 24 >& A)
|
|
|
|
{
|
|
|
|
AlignedBuffer< 56 > tmp;
|
2019-02-02 23:12:42 +00:00
|
|
|
llarp_buffer_t buf{tmp};
|
2019-01-02 01:03:53 +00:00
|
|
|
std::copy(K.begin(), K.end(), buf.cur);
|
2018-12-17 20:46:08 +00:00
|
|
|
buf.cur += K.size();
|
2019-01-02 01:04:04 +00:00
|
|
|
std::copy(A.begin(), A.end(), buf.cur);
|
2018-12-17 20:46:08 +00:00
|
|
|
buf.cur = buf.base;
|
2019-02-11 19:45:42 +00:00
|
|
|
return OurCrypto()->shorthash(K, buf);
|
2018-12-17 20:46:08 +00:00
|
|
|
}
|
2018-09-04 12:41:25 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
void
|
|
|
|
Session::TickImpl(llarp_time_t now)
|
|
|
|
{
|
|
|
|
PruneInboundMessages(now);
|
2019-02-15 22:19:19 +00:00
|
|
|
m_TXRate = 0;
|
|
|
|
m_RXRate = 0;
|
2018-12-17 20:46:08 +00:00
|
|
|
}
|
2018-09-04 12:41:25 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
/// low level read
|
|
|
|
bool
|
|
|
|
Session::Recv(const byte_t* buf, size_t sz)
|
|
|
|
{
|
|
|
|
// mark we are alive
|
|
|
|
Alive();
|
2019-02-15 22:19:19 +00:00
|
|
|
m_RXRate += sz;
|
2018-12-17 20:46:08 +00:00
|
|
|
size_t s = sz;
|
|
|
|
// process leftovers
|
|
|
|
if(recvBufOffset)
|
2018-09-04 12:41:25 +00:00
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
auto left = FragmentBufferSize - recvBufOffset;
|
|
|
|
if(s >= left)
|
2018-09-07 17:41:49 +00:00
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
// yes it fills it
|
2019-02-11 19:45:42 +00:00
|
|
|
LogDebug("process leftovers, offset=", recvBufOffset, " sz=", s,
|
|
|
|
" left=", left);
|
2019-01-02 01:03:53 +00:00
|
|
|
std::copy(buf, buf + left, recvBuf.begin() + recvBufOffset);
|
2018-12-17 20:46:08 +00:00
|
|
|
s -= left;
|
2018-09-07 20:36:06 +00:00
|
|
|
recvBufOffset = 0;
|
2018-12-17 20:46:08 +00:00
|
|
|
buf += left;
|
2019-01-02 01:04:04 +00:00
|
|
|
if(!VerifyThenDecrypt(recvBuf.data()))
|
2018-09-07 17:41:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-09-04 12:41:25 +00:00
|
|
|
}
|
2018-12-17 20:46:08 +00:00
|
|
|
// process full fragments
|
|
|
|
while(s >= FragmentBufferSize)
|
2018-09-04 12:41:25 +00:00
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
recvBufOffset = 0;
|
2019-02-11 19:45:42 +00:00
|
|
|
LogDebug("process full sz=", s);
|
2018-12-17 20:46:08 +00:00
|
|
|
if(!VerifyThenDecrypt(buf))
|
2018-09-04 12:41:25 +00:00
|
|
|
return false;
|
2018-12-17 20:46:08 +00:00
|
|
|
buf += FragmentBufferSize;
|
|
|
|
s -= FragmentBufferSize;
|
2018-09-04 12:41:25 +00:00
|
|
|
}
|
2018-12-17 20:46:08 +00:00
|
|
|
if(s)
|
2018-09-04 12:41:25 +00:00
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
// hold onto leftovers
|
2019-02-11 19:45:42 +00:00
|
|
|
LogDebug("leftovers sz=", s);
|
2019-01-02 01:03:53 +00:00
|
|
|
std::copy(buf, buf + s, recvBuf.begin() + recvBufOffset);
|
2018-12-17 20:46:08 +00:00
|
|
|
recvBufOffset += s;
|
2018-09-04 12:41:25 +00:00
|
|
|
}
|
2018-12-17 20:46:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-09-04 12:41:25 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
bool
|
|
|
|
Session::IsTimedOut(llarp_time_t now) const
|
|
|
|
{
|
2019-03-22 18:23:33 +00:00
|
|
|
if(sendq.size() >= MaxSendQueueSize)
|
|
|
|
{
|
|
|
|
return now - lastActive > 5000;
|
|
|
|
}
|
2019-03-22 11:44:28 +00:00
|
|
|
// let utp manage this
|
|
|
|
return state == eClose;
|
2018-12-17 20:46:08 +00:00
|
|
|
}
|
2018-09-04 12:41:25 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
const PubKey&
|
|
|
|
Session::RemotePubKey() const
|
|
|
|
{
|
|
|
|
return remoteRC.pubkey;
|
|
|
|
}
|
2018-09-04 12:41:25 +00:00
|
|
|
|
2018-12-19 16:17:41 +00:00
|
|
|
Addr
|
|
|
|
Session::RemoteEndpoint()
|
2018-09-04 12:41:25 +00:00
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
return remoteAddr;
|
|
|
|
}
|
2018-09-04 12:41:25 +00:00
|
|
|
|
2019-03-07 22:53:36 +00:00
|
|
|
uint64
|
|
|
|
LinkLayer::OnConnect(utp_callback_arguments* arg)
|
|
|
|
{
|
|
|
|
LinkLayer* l =
|
|
|
|
static_cast< LinkLayer* >(utp_context_get_userdata(arg->context));
|
|
|
|
Session* session = static_cast< Session* >(utp_get_userdata(arg->socket));
|
|
|
|
if(session && l)
|
|
|
|
session->OutboundLinkEstablished(l);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
uint64
|
|
|
|
LinkLayer::SendTo(utp_callback_arguments* arg)
|
|
|
|
{
|
|
|
|
LinkLayer* l =
|
|
|
|
static_cast< LinkLayer* >(utp_context_get_userdata(arg->context));
|
|
|
|
if(l == nullptr)
|
|
|
|
return 0;
|
2019-02-11 19:45:42 +00:00
|
|
|
LogDebug("utp_sendto ", Addr(*arg->address), " ", arg->len, " bytes");
|
2018-12-17 20:46:08 +00:00
|
|
|
// For whatever reason, the UTP_UDP_DONTFRAG flag is set
|
|
|
|
// on the socket itself....which isn't correct and causes
|
|
|
|
// winsock (at minimum) to reeee
|
|
|
|
// here, we check its value, then set fragmentation the _right_
|
|
|
|
// way. Naturally, Linux has its own special procedure.
|
|
|
|
// Of course, the flag itself is cleared. -rick
|
2018-11-20 06:12:56 +00:00
|
|
|
#ifndef _WIN32
|
2018-12-17 20:46:08 +00:00
|
|
|
// No practical method of doing this on NetBSD or Darwin
|
|
|
|
// without resorting to raw sockets
|
2018-11-22 17:46:52 +00:00
|
|
|
#if !(__NetBSD__ || __OpenBSD__ || (__APPLE__ && __MACH__))
|
2018-11-20 08:18:29 +00:00
|
|
|
#ifndef __linux__
|
2018-12-17 20:46:08 +00:00
|
|
|
if(arg->flags == 2)
|
|
|
|
{
|
|
|
|
int val = 1;
|
|
|
|
setsockopt(l->m_udp.fd, IPPROTO_IP, IP_DONTFRAGMENT, &val, sizeof(val));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int val = 0;
|
|
|
|
setsockopt(l->m_udp.fd, IPPROTO_IP, IP_DONTFRAGMENT, &val, sizeof(val));
|
|
|
|
}
|
2018-11-20 08:18:29 +00:00
|
|
|
#else
|
2018-12-17 20:46:08 +00:00
|
|
|
if(arg->flags == 2)
|
|
|
|
{
|
|
|
|
int val = IP_PMTUDISC_DO;
|
|
|
|
setsockopt(l->m_udp.fd, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int val = IP_PMTUDISC_DONT;
|
|
|
|
setsockopt(l->m_udp.fd, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val));
|
|
|
|
}
|
2018-11-22 17:46:52 +00:00
|
|
|
#endif
|
2018-11-20 08:18:29 +00:00
|
|
|
#endif
|
2018-12-17 20:46:08 +00:00
|
|
|
arg->flags = 0;
|
|
|
|
if(::sendto(l->m_udp.fd, (char*)arg->buf, arg->len, arg->flags,
|
|
|
|
arg->address, arg->address_len)
|
|
|
|
== -1
|
|
|
|
&& errno)
|
2018-11-20 06:12:56 +00:00
|
|
|
#else
|
2018-12-17 20:46:08 +00:00
|
|
|
if(arg->flags == 2)
|
|
|
|
{
|
|
|
|
char val = 1;
|
|
|
|
setsockopt(l->m_udp.fd, IPPROTO_IP, IP_DONTFRAGMENT, &val, sizeof(val));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char val = 0;
|
|
|
|
setsockopt(l->m_udp.fd, IPPROTO_IP, IP_DONTFRAGMENT, &val, sizeof(val));
|
|
|
|
}
|
|
|
|
arg->flags = 0;
|
|
|
|
if(::sendto(l->m_udp.fd, (char*)arg->buf, arg->len, arg->flags,
|
|
|
|
arg->address, arg->address_len)
|
|
|
|
== -1)
|
2018-11-20 06:12:56 +00:00
|
|
|
#endif
|
2018-12-17 20:46:08 +00:00
|
|
|
{
|
2018-11-20 15:38:46 +00:00
|
|
|
#ifdef _WIN32
|
2018-12-17 20:46:08 +00:00
|
|
|
char buf[1024];
|
|
|
|
int err = WSAGetLastError();
|
|
|
|
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, err, LANG_NEUTRAL,
|
|
|
|
buf, 1024, nullptr);
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError("sendto failed: ", buf);
|
2018-11-20 15:38:46 +00:00
|
|
|
#else
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError("sendto failed: ", strerror(errno));
|
2018-11-20 15:38:46 +00:00
|
|
|
#endif
|
2018-09-04 12:41:25 +00:00
|
|
|
}
|
2018-12-17 20:46:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-09-04 12:41:25 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
uint64
|
|
|
|
LinkLayer::OnError(utp_callback_arguments* arg)
|
|
|
|
{
|
|
|
|
Session* session = static_cast< Session* >(utp_get_userdata(arg->socket));
|
2018-09-04 12:41:25 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
LinkLayer* link =
|
|
|
|
static_cast< LinkLayer* >(utp_context_get_userdata(arg->context));
|
2018-09-04 12:41:25 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
if(session && link)
|
2018-09-06 20:31:58 +00:00
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
link->HandleTimeout(session);
|
|
|
|
llarp::LogError(utp_error_code_names[arg->error_code], " via ",
|
|
|
|
session->remoteAddr);
|
2019-02-15 13:45:56 +00:00
|
|
|
if(arg->error_code == UTP_ETIMEDOUT)
|
|
|
|
utp_close(arg->socket);
|
|
|
|
else
|
2019-01-07 16:35:25 +00:00
|
|
|
session->Close();
|
2018-09-06 20:31:58 +00:00
|
|
|
}
|
2018-12-17 20:46:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-09-06 20:31:58 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
uint64
|
|
|
|
LinkLayer::OnLog(utp_callback_arguments* arg)
|
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogDebug(arg->buf);
|
2018-12-17 20:46:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-09-04 12:41:25 +00:00
|
|
|
|
2019-02-11 19:45:42 +00:00
|
|
|
LinkLayer::LinkLayer(Crypto* crypto, const SecretKey& routerEncSecret,
|
|
|
|
GetRCFunc getrc, LinkMessageHandler h,
|
|
|
|
SignBufferFunc sign,
|
|
|
|
SessionEstablishedHandler established,
|
|
|
|
SessionRenegotiateHandler reneg,
|
|
|
|
TimeoutHandler timeout, SessionClosedHandler closed)
|
2018-12-19 16:17:41 +00:00
|
|
|
: ILinkLayer(routerEncSecret, getrc, h, sign, established, reneg,
|
|
|
|
timeout, closed)
|
2018-12-17 20:46:08 +00:00
|
|
|
{
|
|
|
|
_crypto = crypto;
|
|
|
|
_utp_ctx = utp_init(2);
|
|
|
|
utp_context_set_userdata(_utp_ctx, this);
|
|
|
|
utp_set_callback(_utp_ctx, UTP_SENDTO, &LinkLayer::SendTo);
|
|
|
|
utp_set_callback(_utp_ctx, UTP_ON_ACCEPT, &LinkLayer::OnAccept);
|
2019-03-07 22:53:36 +00:00
|
|
|
utp_set_callback(_utp_ctx, UTP_ON_CONNECT, &LinkLayer::OnConnect);
|
2018-12-17 20:46:08 +00:00
|
|
|
utp_set_callback(_utp_ctx, UTP_ON_STATE_CHANGE,
|
|
|
|
&LinkLayer::OnStateChange);
|
|
|
|
utp_set_callback(_utp_ctx, UTP_ON_READ, &LinkLayer::OnRead);
|
|
|
|
utp_set_callback(_utp_ctx, UTP_ON_ERROR, &LinkLayer::OnError);
|
|
|
|
utp_set_callback(_utp_ctx, UTP_LOG, &LinkLayer::OnLog);
|
|
|
|
utp_context_set_option(_utp_ctx, UTP_LOG_NORMAL, 1);
|
|
|
|
utp_context_set_option(_utp_ctx, UTP_LOG_MTU, 1);
|
|
|
|
utp_context_set_option(_utp_ctx, UTP_LOG_DEBUG, 1);
|
|
|
|
utp_context_set_option(_utp_ctx, UTP_SNDBUF, MAX_LINK_MSG_SIZE * 16);
|
|
|
|
utp_context_set_option(_utp_ctx, UTP_RCVBUF, MAX_LINK_MSG_SIZE * 64);
|
|
|
|
}
|
2018-09-04 12:41:25 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
LinkLayer::~LinkLayer()
|
|
|
|
{
|
|
|
|
utp_destroy(_utp_ctx);
|
|
|
|
}
|
2018-09-04 12:41:25 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
uint16_t
|
|
|
|
LinkLayer::Rank() const
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LinkLayer::RecvFrom(const Addr& from, const void* buf, size_t sz)
|
|
|
|
{
|
|
|
|
utp_process_udp(_utp_ctx, (const byte_t*)buf, sz, from, from.SockLen());
|
|
|
|
}
|
2018-09-04 12:41:25 +00:00
|
|
|
|
2018-09-06 20:31:58 +00:00
|
|
|
#ifdef __linux__
|
2019-03-07 22:53:36 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
void
|
|
|
|
LinkLayer::ProcessICMP()
|
|
|
|
{
|
2019-03-07 22:53:36 +00:00
|
|
|
#ifndef TESTNET
|
2018-12-17 20:46:08 +00:00
|
|
|
do
|
2018-09-06 20:31:58 +00:00
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
byte_t vec_buf[4096], ancillary_buf[4096];
|
|
|
|
struct iovec iov = {vec_buf, sizeof(vec_buf)};
|
|
|
|
struct sockaddr_in remote;
|
|
|
|
struct msghdr msg;
|
|
|
|
ssize_t len;
|
|
|
|
struct cmsghdr* cmsg;
|
|
|
|
struct sock_extended_err* e;
|
|
|
|
struct sockaddr* icmp_addr;
|
|
|
|
struct sockaddr_in* icmp_sin;
|
|
|
|
|
|
|
|
memset(&msg, 0, sizeof(msg));
|
|
|
|
|
|
|
|
msg.msg_name = &remote;
|
|
|
|
msg.msg_namelen = sizeof(remote);
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
msg.msg_flags = 0;
|
|
|
|
msg.msg_control = ancillary_buf;
|
|
|
|
msg.msg_controllen = sizeof(ancillary_buf);
|
|
|
|
|
|
|
|
len = recvmsg(m_udp.fd, &msg, MSG_ERRQUEUE | MSG_DONTWAIT);
|
|
|
|
if(len < 0)
|
|
|
|
{
|
|
|
|
if(errno == EAGAIN || errno == EWOULDBLOCK)
|
|
|
|
errno = 0;
|
|
|
|
else
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError("failed to read icmp for utp ", strerror(errno));
|
2018-12-17 20:46:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
|
2018-09-06 20:31:58 +00:00
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
if(cmsg->cmsg_type != IP_RECVERR)
|
2018-09-06 20:31:58 +00:00
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
continue;
|
2018-09-06 20:31:58 +00:00
|
|
|
}
|
2018-12-17 20:46:08 +00:00
|
|
|
if(cmsg->cmsg_level != SOL_IP)
|
2018-09-06 20:31:58 +00:00
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
continue;
|
2018-09-06 20:31:58 +00:00
|
|
|
}
|
2018-12-17 20:46:08 +00:00
|
|
|
e = (struct sock_extended_err*)CMSG_DATA(cmsg);
|
|
|
|
if(!e)
|
|
|
|
continue;
|
|
|
|
if(e->ee_origin != SO_EE_ORIGIN_ICMP)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
icmp_addr = (struct sockaddr*)SO_EE_OFFENDER(e);
|
|
|
|
icmp_sin = (struct sockaddr_in*)icmp_addr;
|
|
|
|
if(icmp_sin->sin_port != 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(e->ee_type == 3 && e->ee_code == 4)
|
|
|
|
{
|
|
|
|
utp_process_icmp_fragmentation(_utp_ctx, vec_buf, len,
|
|
|
|
(struct sockaddr*)&remote,
|
|
|
|
sizeof(remote), e->ee_info);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
utp_process_icmp_error(_utp_ctx, vec_buf, len,
|
|
|
|
(struct sockaddr*)&remote, sizeof(remote));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while(true);
|
2019-03-07 22:53:36 +00:00
|
|
|
#endif
|
2018-12-17 20:46:08 +00:00
|
|
|
}
|
2018-09-06 20:31:58 +00:00
|
|
|
#endif
|
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
void
|
|
|
|
LinkLayer::Pump()
|
|
|
|
{
|
2018-09-06 20:31:58 +00:00
|
|
|
#ifdef __linux__
|
2018-12-17 20:46:08 +00:00
|
|
|
ProcessICMP();
|
2018-09-06 20:31:58 +00:00
|
|
|
#endif
|
2018-12-17 20:46:08 +00:00
|
|
|
std::set< RouterID > sessions;
|
|
|
|
{
|
2019-03-03 15:01:05 +00:00
|
|
|
Lock l(&m_AuthedLinksMutex);
|
2018-12-17 20:46:08 +00:00
|
|
|
auto itr = m_AuthedLinks.begin();
|
|
|
|
while(itr != m_AuthedLinks.end())
|
2018-09-10 13:43:36 +00:00
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
sessions.insert(itr->first);
|
|
|
|
++itr;
|
2018-09-10 13:43:36 +00:00
|
|
|
}
|
2018-12-17 20:46:08 +00:00
|
|
|
}
|
|
|
|
ILinkLayer::Pump();
|
|
|
|
{
|
2019-03-03 15:01:05 +00:00
|
|
|
Lock l(&m_AuthedLinksMutex);
|
2018-12-17 20:46:08 +00:00
|
|
|
for(const auto& pk : sessions)
|
2018-09-10 13:43:36 +00:00
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
if(m_AuthedLinks.count(pk) == 0)
|
2018-09-10 13:43:36 +00:00
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
// all sessions were removed
|
|
|
|
SessionClosed(pk);
|
2018-09-10 13:43:36 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-06 20:31:58 +00:00
|
|
|
}
|
2019-03-07 22:53:36 +00:00
|
|
|
utp_issue_deferred_acks(_utp_ctx);
|
2018-12-17 20:46:08 +00:00
|
|
|
}
|
2018-11-05 13:19:08 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
void
|
|
|
|
LinkLayer::Stop()
|
|
|
|
{
|
2018-12-19 16:17:41 +00:00
|
|
|
ForEachSession([](ILinkSession* s) { s->SendClose(); });
|
2018-12-17 20:46:08 +00:00
|
|
|
}
|
2018-09-06 11:46:19 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
bool
|
|
|
|
LinkLayer::KeyGen(SecretKey& k)
|
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
OurCrypto()->encryption_keygen(k);
|
2018-12-17 20:46:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-09-06 20:31:58 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
void
|
|
|
|
LinkLayer::Tick(llarp_time_t now)
|
|
|
|
{
|
|
|
|
utp_check_timeouts(_utp_ctx);
|
|
|
|
ILinkLayer::Tick(now);
|
|
|
|
}
|
2018-09-06 20:31:58 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
utp_socket*
|
|
|
|
LinkLayer::NewSocket()
|
|
|
|
{
|
|
|
|
return utp_create_socket(_utp_ctx);
|
|
|
|
}
|
2018-09-04 12:41:25 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
const char*
|
|
|
|
LinkLayer::Name() const
|
|
|
|
{
|
|
|
|
return "utp";
|
|
|
|
}
|
2018-09-04 12:41:25 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
std::unique_ptr< ILinkLayer >
|
2019-02-11 19:45:42 +00:00
|
|
|
NewServer(Crypto* crypto, const SecretKey& routerEncSecret, GetRCFunc getrc,
|
|
|
|
LinkMessageHandler h, SessionEstablishedHandler est,
|
|
|
|
SessionRenegotiateHandler reneg, SignBufferFunc sign,
|
|
|
|
TimeoutHandler timeout, SessionClosedHandler closed)
|
2018-12-17 20:46:08 +00:00
|
|
|
{
|
2018-12-19 16:17:41 +00:00
|
|
|
return std::unique_ptr< ILinkLayer >(
|
|
|
|
new LinkLayer(crypto, routerEncSecret, getrc, h, sign, est, reneg,
|
|
|
|
timeout, closed));
|
2018-12-17 20:46:08 +00:00
|
|
|
}
|
2018-09-04 12:41:25 +00:00
|
|
|
|
|
|
|
std::unique_ptr< ILinkLayer >
|
2019-02-11 19:45:42 +00:00
|
|
|
NewServerFromRouter(AbstractRouter* r)
|
2018-09-04 12:41:25 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
using namespace std::placeholders;
|
2018-12-17 20:46:08 +00:00
|
|
|
return NewServer(
|
2019-02-11 19:45:42 +00:00
|
|
|
r->crypto(), r->encryption(), std::bind(&AbstractRouter::rc, r),
|
|
|
|
std::bind(&AbstractRouter::HandleRecvLinkMessageBuffer, r, _1, _2),
|
|
|
|
std::bind(&AbstractRouter::OnSessionEstablished, r, _1),
|
|
|
|
std::bind(&AbstractRouter::CheckRenegotiateValid, r, _1, _2),
|
|
|
|
std::bind(&AbstractRouter::Sign, r, _1, _2),
|
|
|
|
std::bind(&AbstractRouter::OnConnectTimeout, r, _1),
|
|
|
|
std::bind(&AbstractRouter::SessionClosed, r, _1));
|
2018-09-04 12:41:25 +00:00
|
|
|
}
|
|
|
|
|
2018-12-16 20:34:04 +00:00
|
|
|
/// base constructor
|
2018-12-17 20:46:08 +00:00
|
|
|
Session::Session(LinkLayer* p)
|
2018-09-04 12:41:25 +00:00
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
m_NextTXMsgID = 0;
|
|
|
|
m_NextRXMsgID = 0;
|
|
|
|
parent = p;
|
2018-09-07 17:41:49 +00:00
|
|
|
remoteTransportPubKey.Zero();
|
2018-09-08 15:53:20 +00:00
|
|
|
|
2018-09-18 20:56:22 +00:00
|
|
|
SendQueueBacklog = [&]() -> size_t { return sendq.size(); };
|
|
|
|
|
2019-03-18 12:25:32 +00:00
|
|
|
ShouldPing = [&]() -> bool {
|
2019-03-18 12:30:07 +00:00
|
|
|
auto dlt = parent->Now() - lastActive;
|
2019-03-18 12:25:32 +00:00
|
|
|
return dlt >= 10000;
|
|
|
|
};
|
|
|
|
|
2018-09-06 20:31:58 +00:00
|
|
|
SendKeepAlive = [&]() -> bool {
|
2019-03-07 22:53:36 +00:00
|
|
|
if(state == eSessionReady)
|
2018-09-06 20:31:58 +00:00
|
|
|
{
|
|
|
|
DiscardMessage msg;
|
2019-02-02 23:12:42 +00:00
|
|
|
std::array< byte_t, 128 > tmp;
|
|
|
|
llarp_buffer_t buf(tmp);
|
2018-09-06 20:31:58 +00:00
|
|
|
if(!msg.BEncode(&buf))
|
|
|
|
return false;
|
2018-12-03 18:28:16 +00:00
|
|
|
buf.sz = buf.cur - buf.base;
|
2018-09-06 20:31:58 +00:00
|
|
|
buf.cur = buf.base;
|
|
|
|
if(!this->QueueWriteBuffers(buf))
|
|
|
|
return false;
|
2019-03-07 22:53:36 +00:00
|
|
|
PumpWrite();
|
2018-09-06 20:31:58 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2018-12-03 18:28:16 +00:00
|
|
|
gotLIM = false;
|
2018-09-06 11:46:19 +00:00
|
|
|
recvBufOffset = 0;
|
2018-12-27 18:42:23 +00:00
|
|
|
TimedOut = std::bind(&Session::IsTimedOut, this, std::placeholders::_1);
|
|
|
|
GetPubKey = std::bind(&Session::RemotePubKey, this);
|
2019-02-11 19:45:42 +00:00
|
|
|
GetRemoteRC = [&]() -> RouterContact { return this->remoteRC; };
|
2018-12-17 20:46:08 +00:00
|
|
|
GetLinkLayer = std::bind(&Session::GetParent, this);
|
|
|
|
|
|
|
|
lastActive = parent->Now();
|
2018-12-16 20:34:04 +00:00
|
|
|
|
2019-01-04 12:43:41 +00:00
|
|
|
Pump = std::bind(&Session::DoPump, this);
|
2018-12-17 20:46:08 +00:00
|
|
|
Tick = std::bind(&Session::TickImpl, this, std::placeholders::_1);
|
|
|
|
SendMessageBuffer =
|
|
|
|
std::bind(&Session::QueueWriteBuffers, this, std::placeholders::_1);
|
2018-09-07 17:41:49 +00:00
|
|
|
|
|
|
|
IsEstablished = [=]() {
|
|
|
|
return this->state == eSessionReady || this->state == eLinkEstablished;
|
2018-09-06 11:46:19 +00:00
|
|
|
};
|
2018-09-07 17:41:49 +00:00
|
|
|
|
2018-12-19 16:17:41 +00:00
|
|
|
SendClose = std::bind(&Session::Close, this);
|
|
|
|
GetRemoteEndpoint = std::bind(&Session::RemoteEndpoint, this);
|
|
|
|
RenegotiateSession = std::bind(&Session::Rehandshake, this);
|
2018-09-06 11:46:19 +00:00
|
|
|
}
|
2018-09-04 12:41:25 +00:00
|
|
|
|
2018-12-16 20:34:04 +00:00
|
|
|
/// outbound session
|
2018-12-17 20:46:08 +00:00
|
|
|
Session::Session(LinkLayer* p, utp_socket* s, const RouterContact& rc,
|
|
|
|
const AddressInfo& addr)
|
|
|
|
: Session(p)
|
2018-09-04 12:41:25 +00:00
|
|
|
{
|
2018-09-06 11:46:19 +00:00
|
|
|
remoteTransportPubKey = addr.pubkey;
|
2018-12-03 18:28:16 +00:00
|
|
|
remoteRC = rc;
|
2018-12-17 20:46:08 +00:00
|
|
|
RouterID rid = remoteRC.pubkey;
|
2019-02-11 19:45:42 +00:00
|
|
|
OurCrypto()->shorthash(txKey, llarp_buffer_t(rid));
|
2019-01-02 01:04:04 +00:00
|
|
|
rid = p->GetOurRC().pubkey;
|
2019-02-11 19:45:42 +00:00
|
|
|
OurCrypto()->shorthash(rxKey, llarp_buffer_t(rid));
|
2018-12-17 20:46:08 +00:00
|
|
|
|
|
|
|
sock = s;
|
2018-09-06 11:46:19 +00:00
|
|
|
assert(utp_set_userdata(sock, this) == this);
|
2018-09-06 13:16:24 +00:00
|
|
|
assert(s == sock);
|
2018-09-06 11:46:19 +00:00
|
|
|
remoteAddr = addr;
|
2018-12-17 20:46:08 +00:00
|
|
|
Start = std::bind(&Session::Connect, this);
|
|
|
|
GotLIM = std::bind(&Session::OutboundLIM, this, std::placeholders::_1);
|
2018-09-06 11:46:19 +00:00
|
|
|
}
|
2018-09-04 12:41:25 +00:00
|
|
|
|
2018-12-16 20:34:04 +00:00
|
|
|
/// inbound session
|
2018-12-17 20:46:08 +00:00
|
|
|
Session::Session(LinkLayer* p, utp_socket* s, const Addr& addr) : Session(p)
|
2018-09-06 11:46:19 +00:00
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
RouterID rid = p->GetOurRC().pubkey;
|
2019-02-11 19:45:42 +00:00
|
|
|
OurCrypto()->shorthash(rxKey, llarp_buffer_t(rid));
|
2018-09-06 11:46:19 +00:00
|
|
|
remoteRC.Clear();
|
|
|
|
sock = s;
|
2018-09-06 13:16:24 +00:00
|
|
|
assert(s == sock);
|
2018-09-06 11:46:19 +00:00
|
|
|
assert(utp_set_userdata(sock, this) == this);
|
|
|
|
remoteAddr = addr;
|
2018-12-03 18:28:16 +00:00
|
|
|
Start = []() {};
|
2018-12-17 20:46:08 +00:00
|
|
|
GotLIM = std::bind(&Session::InboundLIM, this, std::placeholders::_1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ILinkLayer*
|
|
|
|
Session::GetParent()
|
|
|
|
{
|
|
|
|
return parent;
|
2018-09-08 15:53:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-12-17 20:46:08 +00:00
|
|
|
Session::InboundLIM(const LinkIntroMessage* msg)
|
2018-09-08 15:53:20 +00:00
|
|
|
{
|
|
|
|
if(gotLIM && remoteRC.pubkey != msg->rc.pubkey)
|
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
Close();
|
2018-09-08 15:53:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-12-16 20:34:04 +00:00
|
|
|
if(!gotLIM)
|
|
|
|
{
|
|
|
|
remoteRC = msg->rc;
|
2019-02-11 19:45:42 +00:00
|
|
|
OurCrypto()->shorthash(txKey, llarp_buffer_t(remoteRC.pubkey));
|
2018-12-17 20:46:08 +00:00
|
|
|
|
2019-02-11 19:45:42 +00:00
|
|
|
if(!DoKeyExchange(std::bind(&Crypto::transport_dh_server, OurCrypto(),
|
|
|
|
_1, _2, _3, _4),
|
2019-01-26 15:40:58 +00:00
|
|
|
rxKey, msg->N, remoteRC.enckey,
|
|
|
|
parent->TransportSecretKey()))
|
2018-12-16 20:34:04 +00:00
|
|
|
return false;
|
2018-12-17 20:46:08 +00:00
|
|
|
|
2019-02-02 23:12:42 +00:00
|
|
|
std::array< byte_t, LinkIntroMessage::MaxSize > tmp;
|
|
|
|
llarp_buffer_t buf(tmp);
|
2018-12-17 20:46:08 +00:00
|
|
|
LinkIntroMessage replymsg;
|
|
|
|
replymsg.rc = parent->GetOurRC();
|
2019-02-11 19:45:42 +00:00
|
|
|
if(!replymsg.rc.Verify(OurCrypto(), parent->Now()))
|
2018-12-17 20:46:08 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError("our RC is invalid? closing session to", remoteAddr);
|
2018-12-17 20:46:08 +00:00
|
|
|
Close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
replymsg.N.Randomize();
|
|
|
|
replymsg.P = DefaultLinkSessionLifetime;
|
|
|
|
if(!replymsg.Sign(parent->Sign))
|
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError("failed to sign LIM for inbound handshake from ",
|
|
|
|
remoteAddr);
|
2018-12-17 20:46:08 +00:00
|
|
|
Close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// encode
|
|
|
|
if(!replymsg.BEncode(&buf))
|
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError("failed to encode LIM for handshake from ", remoteAddr);
|
2018-12-17 20:46:08 +00:00
|
|
|
Close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// rewind
|
|
|
|
buf.sz = buf.cur - buf.base;
|
|
|
|
buf.cur = buf.base;
|
|
|
|
// send
|
|
|
|
if(!SendMessageBuffer(buf))
|
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError("failed to repl to handshake from ", remoteAddr);
|
2018-12-17 20:46:08 +00:00
|
|
|
Close();
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-11 19:45:42 +00:00
|
|
|
if(!DoKeyExchange(std::bind(&Crypto::transport_dh_client, OurCrypto(),
|
|
|
|
_1, _2, _3, _4),
|
2019-01-26 15:40:58 +00:00
|
|
|
txKey, replymsg.N, remoteRC.enckey,
|
|
|
|
parent->RouterEncryptionSecret()))
|
2018-12-17 22:43:16 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
return false;
|
2019-02-11 19:45:42 +00:00
|
|
|
LogDebug("Sent reply LIM");
|
2018-12-17 20:46:08 +00:00
|
|
|
gotLIM = true;
|
|
|
|
EnterState(eSessionReady);
|
2018-12-27 19:10:38 +00:00
|
|
|
/// future LIM are used for session renegotiation
|
|
|
|
GotLIM = std::bind(&Session::GotSessionRenegotiate, this,
|
|
|
|
std::placeholders::_1);
|
2018-12-16 20:34:04 +00:00
|
|
|
}
|
2018-09-08 15:53:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-04 12:43:41 +00:00
|
|
|
void
|
|
|
|
Session::DoPump()
|
|
|
|
{
|
|
|
|
// pump write queue
|
|
|
|
PumpWrite();
|
|
|
|
// prune inbound messages
|
|
|
|
PruneInboundMessages(parent->Now());
|
|
|
|
}
|
|
|
|
|
2018-10-29 16:48:36 +00:00
|
|
|
bool
|
2019-02-01 01:58:06 +00:00
|
|
|
Session::QueueWriteBuffers(const llarp_buffer_t& buf)
|
2018-10-29 16:48:36 +00:00
|
|
|
{
|
|
|
|
if(sendq.size() >= MaxSendQueueSize)
|
|
|
|
return false;
|
2018-12-17 20:46:08 +00:00
|
|
|
size_t sz = buf.sz;
|
|
|
|
byte_t* ptr = buf.base;
|
|
|
|
uint32_t msgid = m_NextTXMsgID++;
|
2018-10-29 16:48:36 +00:00
|
|
|
while(sz)
|
|
|
|
{
|
|
|
|
uint32_t s = std::min(FragmentBodyPayloadSize, sz);
|
2018-12-17 20:46:08 +00:00
|
|
|
if(!EncryptThenHash(ptr, msgid, s, sz - s))
|
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError("EncryptThenHash failed?!");
|
2018-12-17 20:46:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-02-11 19:45:42 +00:00
|
|
|
LogDebug("encrypted ", s, " bytes");
|
2018-10-29 16:48:36 +00:00
|
|
|
ptr += s;
|
|
|
|
sz -= s;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-08 15:53:20 +00:00
|
|
|
bool
|
2018-12-17 20:46:08 +00:00
|
|
|
Session::OutboundLIM(const LinkIntroMessage* msg)
|
2018-09-08 15:53:20 +00:00
|
|
|
{
|
|
|
|
if(gotLIM && remoteRC.pubkey != msg->rc.pubkey)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
remoteRC = msg->rc;
|
2018-12-03 18:28:16 +00:00
|
|
|
gotLIM = true;
|
2019-01-26 15:40:58 +00:00
|
|
|
|
2019-02-11 19:45:42 +00:00
|
|
|
if(!DoKeyExchange(std::bind(&Crypto::transport_dh_server, OurCrypto(), _1,
|
|
|
|
_2, _3, _4),
|
|
|
|
rxKey, msg->N, remoteRC.enckey,
|
|
|
|
parent->RouterEncryptionSecret()))
|
2018-12-17 20:46:08 +00:00
|
|
|
{
|
|
|
|
Close();
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-27 19:10:38 +00:00
|
|
|
/// future LIM are used for session renegotiation
|
2019-01-02 01:03:53 +00:00
|
|
|
GotLIM = std::bind(&Session::GotSessionRenegotiate, this,
|
|
|
|
std::placeholders::_1);
|
2019-02-27 12:55:26 +00:00
|
|
|
EnterState(eSessionReady);
|
2018-12-17 20:46:08 +00:00
|
|
|
return true;
|
2018-09-08 15:53:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-12-17 20:46:08 +00:00
|
|
|
Session::OutboundHandshake()
|
2018-09-08 15:53:20 +00:00
|
|
|
{
|
2019-02-02 23:12:42 +00:00
|
|
|
std::array< byte_t, LinkIntroMessage::MaxSize > tmp;
|
|
|
|
llarp_buffer_t buf(tmp);
|
2018-09-09 11:23:21 +00:00
|
|
|
// build our RC
|
2018-09-08 15:53:20 +00:00
|
|
|
LinkIntroMessage msg;
|
2018-12-17 20:46:08 +00:00
|
|
|
msg.rc = parent->GetOurRC();
|
2019-02-11 19:45:42 +00:00
|
|
|
if(!msg.rc.Verify(OurCrypto(), parent->Now()))
|
2018-09-09 11:23:21 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError("our RC is invalid? closing session to", remoteAddr);
|
2018-09-09 11:23:21 +00:00
|
|
|
Close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
msg.N.Randomize();
|
|
|
|
msg.P = DefaultLinkSessionLifetime;
|
2018-12-17 20:46:08 +00:00
|
|
|
if(!msg.Sign(parent->Sign))
|
2018-09-08 15:53:20 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError("failed to sign LIM for outbound handshake to ", remoteAddr);
|
2018-09-08 15:53:20 +00:00
|
|
|
Close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// encode
|
|
|
|
if(!msg.BEncode(&buf))
|
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError("failed to encode LIM for handshake to ", remoteAddr);
|
2018-09-08 15:53:20 +00:00
|
|
|
Close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// rewind
|
2018-12-03 18:28:16 +00:00
|
|
|
buf.sz = buf.cur - buf.base;
|
2018-09-08 15:53:20 +00:00
|
|
|
buf.cur = buf.base;
|
|
|
|
// send
|
|
|
|
if(!SendMessageBuffer(buf))
|
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError("failed to send handshake to ", remoteAddr);
|
2018-09-08 15:53:20 +00:00
|
|
|
Close();
|
|
|
|
return;
|
|
|
|
}
|
2019-01-26 15:40:58 +00:00
|
|
|
|
2019-02-11 19:45:42 +00:00
|
|
|
if(!DoKeyExchange(std::bind(&Crypto::transport_dh_client, OurCrypto(), _1,
|
|
|
|
_2, _3, _4),
|
|
|
|
txKey, msg.N, remoteTransportPubKey,
|
|
|
|
parent->RouterEncryptionSecret()))
|
2018-09-08 15:53:20 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError("failed to mix keys for outbound session to ", remoteAddr);
|
2018-09-08 15:53:20 +00:00
|
|
|
Close();
|
2018-09-09 11:23:21 +00:00
|
|
|
return;
|
2018-09-08 15:53:20 +00:00
|
|
|
}
|
2018-09-04 12:41:25 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
Session::~Session()
|
2018-09-04 12:41:25 +00:00
|
|
|
{
|
2018-09-06 13:16:24 +00:00
|
|
|
if(sock)
|
|
|
|
{
|
|
|
|
utp_set_userdata(sock, nullptr);
|
2019-03-07 22:53:36 +00:00
|
|
|
sock = nullptr;
|
2018-09-06 13:16:24 +00:00
|
|
|
}
|
2018-09-04 12:41:25 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 13:16:24 +00:00
|
|
|
ILinkSession*
|
2018-09-04 12:41:25 +00:00
|
|
|
LinkLayer::NewOutboundSession(const RouterContact& rc,
|
|
|
|
const AddressInfo& addr)
|
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
return new Session(this, utp_create_socket(_utp_ctx), rc, addr);
|
2018-09-06 11:46:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64
|
|
|
|
LinkLayer::OnRead(utp_callback_arguments* arg)
|
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
Session* self = static_cast< Session* >(utp_get_userdata(arg->socket));
|
2018-09-07 17:41:49 +00:00
|
|
|
|
2018-09-06 11:46:19 +00:00
|
|
|
if(self)
|
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
if(self->state == Session::eClose)
|
2018-09-06 13:16:24 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2018-09-08 15:53:20 +00:00
|
|
|
if(!self->Recv(arg->buf, arg->len))
|
2018-09-06 20:31:58 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogDebug("recv fail for ", self->remoteAddr);
|
2018-09-08 15:53:20 +00:00
|
|
|
self->Close();
|
|
|
|
return 0;
|
2018-09-06 11:46:19 +00:00
|
|
|
}
|
2018-09-08 15:53:20 +00:00
|
|
|
utp_read_drained(arg->socket);
|
2018-09-06 11:46:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogWarn("utp_socket got data with no underlying session");
|
2018-09-18 12:29:27 +00:00
|
|
|
utp_close(arg->socket);
|
2018-09-06 11:46:19 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2018-09-04 12:41:25 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 11:46:19 +00:00
|
|
|
uint64
|
|
|
|
LinkLayer::OnStateChange(utp_callback_arguments* arg)
|
2018-09-04 12:41:25 +00:00
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
Session* session = static_cast< Session* >(utp_get_userdata(arg->socket));
|
2018-09-06 13:16:24 +00:00
|
|
|
if(session)
|
2018-09-06 11:46:19 +00:00
|
|
|
{
|
2019-03-07 22:53:36 +00:00
|
|
|
if(arg->state == UTP_STATE_WRITABLE)
|
2018-09-06 20:31:58 +00:00
|
|
|
{
|
2018-09-12 13:29:42 +00:00
|
|
|
session->PumpWrite();
|
2018-09-06 20:31:58 +00:00
|
|
|
}
|
2018-09-06 13:16:24 +00:00
|
|
|
else if(arg->state == UTP_STATE_EOF)
|
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogDebug("got eof from ", session->remoteAddr);
|
2018-09-07 17:41:49 +00:00
|
|
|
session->Close();
|
2018-09-06 13:16:24 +00:00
|
|
|
}
|
2018-09-06 11:46:19 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2018-09-04 12:41:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64
|
|
|
|
LinkLayer::OnAccept(utp_callback_arguments* arg)
|
|
|
|
{
|
|
|
|
LinkLayer* self =
|
|
|
|
static_cast< LinkLayer* >(utp_context_get_userdata(arg->context));
|
|
|
|
Addr remote(*arg->address);
|
2019-02-11 19:45:42 +00:00
|
|
|
LogDebug("utp accepted from ", remote);
|
2018-12-17 20:46:08 +00:00
|
|
|
Session* session = new Session(self, arg->socket, remote);
|
2019-01-04 12:43:41 +00:00
|
|
|
if(!self->PutSession(session))
|
|
|
|
{
|
|
|
|
session->Close();
|
|
|
|
delete session;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
session->OnLinkEstablished(self);
|
|
|
|
|
2018-09-04 12:41:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
bool
|
|
|
|
Session::EncryptThenHash(const byte_t* ptr, uint32_t msgid, uint16_t length,
|
|
|
|
uint16_t remaining)
|
2018-09-06 11:46:19 +00:00
|
|
|
|
|
|
|
{
|
2018-09-08 15:53:20 +00:00
|
|
|
sendq.emplace_back();
|
|
|
|
auto& buf = sendq.back();
|
2018-09-12 13:29:42 +00:00
|
|
|
vecq.emplace_back();
|
2018-12-03 18:28:16 +00:00
|
|
|
auto& vec = vecq.back();
|
2019-01-02 01:04:04 +00:00
|
|
|
vec.iov_base = buf.data();
|
2018-12-03 18:28:16 +00:00
|
|
|
vec.iov_len = FragmentBufferSize;
|
2018-09-06 11:46:19 +00:00
|
|
|
buf.Randomize();
|
2019-01-02 01:04:06 +00:00
|
|
|
byte_t* noncePtr = buf.data() + FragmentHashSize;
|
|
|
|
byte_t* body = noncePtr + FragmentNonceSize;
|
|
|
|
byte_t* base = body;
|
|
|
|
AlignedBuffer< 24 > A(base);
|
2018-12-17 20:46:08 +00:00
|
|
|
// skip inner nonce
|
|
|
|
body += A.size();
|
|
|
|
// put msgid
|
|
|
|
htobe32buf(body, msgid);
|
2018-09-06 11:46:19 +00:00
|
|
|
body += sizeof(uint32_t);
|
2018-12-17 20:46:08 +00:00
|
|
|
// put length
|
|
|
|
htobe16buf(body, length);
|
|
|
|
body += sizeof(uint16_t);
|
|
|
|
// put remaining
|
|
|
|
htobe16buf(body, remaining);
|
|
|
|
body += sizeof(uint16_t);
|
|
|
|
// put body
|
|
|
|
memcpy(body, ptr, length);
|
2018-09-07 20:36:06 +00:00
|
|
|
|
2019-02-02 23:12:42 +00:00
|
|
|
llarp_buffer_t payload(base, base,
|
|
|
|
FragmentBufferSize - FragmentOverheadSize);
|
2018-09-07 20:36:06 +00:00
|
|
|
|
2019-01-02 01:04:06 +00:00
|
|
|
TunnelNonce nonce(noncePtr);
|
|
|
|
|
2018-09-07 20:36:06 +00:00
|
|
|
// encrypt
|
2019-02-11 19:45:42 +00:00
|
|
|
if(!OurCrypto()->xchacha20(payload, txKey, nonce))
|
2018-12-17 20:46:08 +00:00
|
|
|
return false;
|
2018-09-07 20:36:06 +00:00
|
|
|
|
2019-01-02 01:04:06 +00:00
|
|
|
payload.base = noncePtr;
|
2018-12-03 18:28:16 +00:00
|
|
|
payload.cur = payload.base;
|
|
|
|
payload.sz = FragmentBufferSize - FragmentHashSize;
|
2018-09-07 20:36:06 +00:00
|
|
|
// key'd hash
|
2019-02-11 19:45:42 +00:00
|
|
|
if(!OurCrypto()->hmac(buf.data(), payload, txKey))
|
2018-12-17 20:46:08 +00:00
|
|
|
return false;
|
2018-12-19 16:17:41 +00:00
|
|
|
return MutateKey(txKey, A);
|
2018-09-06 11:46:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-12-17 20:46:08 +00:00
|
|
|
Session::EnterState(State st)
|
2018-09-06 11:46:19 +00:00
|
|
|
{
|
2018-09-06 13:16:24 +00:00
|
|
|
state = st;
|
2018-11-21 17:46:33 +00:00
|
|
|
Alive();
|
2018-09-06 11:46:19 +00:00
|
|
|
if(st == eSessionReady)
|
|
|
|
{
|
2019-01-02 01:03:53 +00:00
|
|
|
parent->MapAddr(remoteRC.pubkey.as_array(), this);
|
2019-02-27 12:55:26 +00:00
|
|
|
if(!parent->SessionEstablished(this))
|
|
|
|
Close();
|
2018-09-06 11:46:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 22:19:19 +00:00
|
|
|
util::StatusObject
|
|
|
|
Session::ExtractStatus() const
|
|
|
|
{
|
2019-02-17 12:13:34 +00:00
|
|
|
return {{"client", !remoteRC.IsPublicRouter()},
|
|
|
|
{"sendBacklog", uint64_t(SendQueueBacklog())},
|
|
|
|
{"tx", m_TXRate},
|
|
|
|
{"rx", m_RXRate},
|
|
|
|
{"remoteAddr", remoteAddr.ToString()},
|
|
|
|
{"pubkey", remoteRC.pubkey.ToHex()}};
|
2019-02-15 22:19:19 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 16:17:41 +00:00
|
|
|
bool
|
|
|
|
Session::GotSessionRenegotiate(const LinkIntroMessage* msg)
|
|
|
|
{
|
|
|
|
// check with parent and possibly process and store new rc
|
|
|
|
if(!parent->SessionRenegotiate(msg->rc, remoteRC))
|
|
|
|
{
|
|
|
|
// failed to renegotiate
|
|
|
|
Close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// set remote rc
|
|
|
|
remoteRC = msg->rc;
|
2019-02-02 23:12:42 +00:00
|
|
|
// recalculate rx key
|
2019-01-26 15:40:58 +00:00
|
|
|
return DoKeyExchange(
|
2019-02-11 19:45:42 +00:00
|
|
|
std::bind(&Crypto::transport_dh_server, OurCrypto(), _1, _2, _3, _4),
|
2019-01-26 15:40:58 +00:00
|
|
|
rxKey, msg->N, remoteRC.enckey, parent->RouterEncryptionSecret());
|
2018-12-19 16:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Session::Rehandshake()
|
|
|
|
{
|
|
|
|
LinkIntroMessage lim;
|
|
|
|
lim.rc = parent->GetOurRC();
|
|
|
|
lim.N.Randomize();
|
|
|
|
lim.P = 60 * 1000 * 10;
|
|
|
|
if(!lim.Sign(parent->Sign))
|
|
|
|
return false;
|
2019-02-02 23:12:42 +00:00
|
|
|
|
|
|
|
std::array< byte_t, LinkIntroMessage::MaxSize > tmp;
|
|
|
|
llarp_buffer_t buf(tmp);
|
2018-12-19 16:17:41 +00:00
|
|
|
if(!lim.BEncode(&buf))
|
|
|
|
return false;
|
|
|
|
// rewind and resize buffer
|
|
|
|
buf.sz = buf.cur - buf.base;
|
|
|
|
buf.cur = buf.base;
|
|
|
|
// send message
|
|
|
|
if(!SendMessageBuffer(buf))
|
|
|
|
return false;
|
|
|
|
// regen our tx Key
|
2019-01-26 15:40:58 +00:00
|
|
|
return DoKeyExchange(
|
2019-02-11 19:45:42 +00:00
|
|
|
std::bind(&Crypto::transport_dh_client, OurCrypto(), _1, _2, _3, _4),
|
2019-01-26 15:40:58 +00:00
|
|
|
txKey, lim.N, remoteRC.enckey, parent->RouterEncryptionSecret());
|
2018-12-19 16:17:41 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 11:46:19 +00:00
|
|
|
bool
|
2018-12-17 22:43:16 +00:00
|
|
|
Session::VerifyThenDecrypt(const byte_t* ptr)
|
2018-09-06 11:46:19 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogDebug("verify then decrypt ", remoteAddr);
|
2018-09-06 11:46:19 +00:00
|
|
|
ShortHash digest;
|
2018-09-06 20:31:58 +00:00
|
|
|
|
2019-02-02 23:12:42 +00:00
|
|
|
llarp_buffer_t hbuf(ptr + FragmentHashSize,
|
|
|
|
FragmentBufferSize - FragmentHashSize);
|
2019-02-11 19:45:42 +00:00
|
|
|
if(!OurCrypto()->hmac(digest.data(), hbuf, rxKey))
|
2018-09-06 11:46:19 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError("keyed hash failed");
|
2018-09-06 11:46:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-12-17 22:43:16 +00:00
|
|
|
ShortHash expected(ptr);
|
2018-09-07 17:41:49 +00:00
|
|
|
if(expected != digest)
|
2018-09-06 11:46:19 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError("Message Integrity Failed: got ", digest, " from ", remoteAddr,
|
|
|
|
" instead of ", expected);
|
2018-12-19 17:48:29 +00:00
|
|
|
Close();
|
2018-09-06 11:46:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-02 23:12:42 +00:00
|
|
|
llarp_buffer_t in(ptr + FragmentOverheadSize,
|
|
|
|
FragmentBufferSize - FragmentOverheadSize);
|
2018-12-16 20:34:04 +00:00
|
|
|
|
2019-02-03 00:31:10 +00:00
|
|
|
llarp_buffer_t out(rxFragBody);
|
2018-12-16 20:34:04 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
// decrypt
|
2019-02-11 19:45:42 +00:00
|
|
|
if(!OurCrypto()->xchacha20_alt(out, in, rxKey, ptr + FragmentHashSize))
|
2018-12-17 20:46:08 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError("failed to decrypt message from ", remoteAddr);
|
2018-12-17 20:46:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// get inner nonce
|
2019-01-02 01:04:06 +00:00
|
|
|
AlignedBuffer< 24 > A(out.base);
|
2018-12-17 20:46:08 +00:00
|
|
|
// advance buffer
|
|
|
|
out.cur += A.size();
|
|
|
|
// read msgid
|
2018-12-16 20:34:04 +00:00
|
|
|
uint32_t msgid;
|
2019-02-17 12:13:34 +00:00
|
|
|
if(!out.read_uint32(msgid))
|
2018-12-17 20:46:08 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError("failed to read msgid");
|
2018-12-16 20:34:04 +00:00
|
|
|
return false;
|
2018-12-17 20:46:08 +00:00
|
|
|
}
|
|
|
|
// read length and remaining
|
|
|
|
uint16_t length, remaining;
|
2019-02-17 12:13:34 +00:00
|
|
|
if(!(out.read_uint16(length) && out.read_uint16(remaining)))
|
2018-12-17 20:46:08 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError("failed to read the rest of the header");
|
2018-09-06 11:46:19 +00:00
|
|
|
return false;
|
2018-12-17 20:46:08 +00:00
|
|
|
}
|
2019-02-05 00:41:33 +00:00
|
|
|
if(length > (out.sz - (out.cur - out.base)))
|
2018-09-06 11:46:19 +00:00
|
|
|
{
|
2018-12-16 20:34:04 +00:00
|
|
|
// too big length
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError("fragment body too big");
|
2018-09-06 11:46:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-12-17 22:43:16 +00:00
|
|
|
if(msgid < m_NextRXMsgID)
|
|
|
|
return false;
|
|
|
|
m_NextRXMsgID = msgid;
|
2018-12-16 20:34:04 +00:00
|
|
|
|
2018-12-17 20:46:08 +00:00
|
|
|
// get message
|
2018-12-17 22:43:16 +00:00
|
|
|
if(m_RecvMsgs.find(msgid) == m_RecvMsgs.end())
|
2019-01-26 15:40:58 +00:00
|
|
|
{
|
2019-02-05 00:43:37 +00:00
|
|
|
m_RecvMsgs.emplace(msgid, InboundMessage{});
|
2019-01-26 15:40:58 +00:00
|
|
|
}
|
2018-12-17 22:43:16 +00:00
|
|
|
|
|
|
|
auto itr = m_RecvMsgs.find(msgid);
|
2018-12-17 20:46:08 +00:00
|
|
|
// add message activity
|
2018-12-17 22:43:16 +00:00
|
|
|
itr->second.lastActive = parent->Now();
|
2018-12-17 20:46:08 +00:00
|
|
|
// append data
|
2018-12-17 22:43:16 +00:00
|
|
|
if(!itr->second.AppendData(out.cur, length))
|
2018-12-17 20:46:08 +00:00
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError("inbound buffer is full");
|
2018-12-17 20:46:08 +00:00
|
|
|
return false; // not enough room
|
|
|
|
}
|
2018-12-19 16:17:41 +00:00
|
|
|
// mutate key
|
|
|
|
if(!MutateKey(rxKey, A))
|
|
|
|
{
|
2019-02-11 19:45:42 +00:00
|
|
|
LogError("failed to mutate rx key");
|
2018-12-19 16:17:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-01-02 01:03:53 +00:00
|
|
|
|
2018-12-16 20:34:04 +00:00
|
|
|
if(remaining == 0)
|
2018-09-06 11:46:19 +00:00
|
|
|
{
|
2018-12-27 19:10:38 +00:00
|
|
|
// we done with this guy, prune next tick
|
|
|
|
itr->second.lastActive = 0;
|
2019-02-03 00:48:10 +00:00
|
|
|
ManagedBuffer buf(itr->second.buffer);
|
2018-12-17 20:46:08 +00:00
|
|
|
// resize
|
2019-02-02 23:12:42 +00:00
|
|
|
buf.underlying.sz = buf.underlying.cur - buf.underlying.base;
|
2018-12-16 20:34:04 +00:00
|
|
|
// rewind
|
2019-02-02 23:12:42 +00:00
|
|
|
buf.underlying.cur = buf.underlying.base;
|
2018-12-17 20:46:08 +00:00
|
|
|
// process buffer
|
2019-02-11 19:45:42 +00:00
|
|
|
LogDebug("got message ", msgid, " from ", remoteAddr);
|
2019-02-23 17:54:35 +00:00
|
|
|
parent->HandleMessage(this, buf.underlying);
|
2018-12-17 20:46:08 +00:00
|
|
|
}
|
2018-12-27 19:10:38 +00:00
|
|
|
return true;
|
2018-09-06 11:46:19 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 20:31:58 +00:00
|
|
|
void
|
2018-12-17 20:46:08 +00:00
|
|
|
Session::Close()
|
2018-09-06 20:31:58 +00:00
|
|
|
{
|
|
|
|
if(state != eClose)
|
|
|
|
{
|
2018-09-07 20:36:06 +00:00
|
|
|
if(sock)
|
|
|
|
{
|
2019-01-07 16:13:16 +00:00
|
|
|
if(state == eLinkEstablished || state == eSessionReady)
|
|
|
|
{
|
2019-01-07 16:35:25 +00:00
|
|
|
// only call shutdown and close when we are actually connected
|
2019-01-07 16:13:16 +00:00
|
|
|
utp_shutdown(sock, SHUT_RDWR);
|
2019-01-07 16:35:25 +00:00
|
|
|
utp_close(sock);
|
2019-01-07 16:13:16 +00:00
|
|
|
}
|
2019-02-11 19:45:42 +00:00
|
|
|
LogDebug("utp_close ", remoteAddr);
|
2018-09-07 20:36:06 +00:00
|
|
|
}
|
2018-09-06 20:31:58 +00:00
|
|
|
}
|
|
|
|
EnterState(eClose);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-12-17 20:46:08 +00:00
|
|
|
Session::Alive()
|
2018-09-06 20:31:58 +00:00
|
|
|
{
|
2018-12-17 20:46:08 +00:00
|
|
|
lastActive = parent->Now();
|
2018-09-06 11:46:19 +00:00
|
|
|
}
|
|
|
|
|
2018-09-04 12:41:25 +00:00
|
|
|
} // namespace utp
|
|
|
|
|
|
|
|
} // namespace llarp
|