You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lokinet/llarp/link/utp.cpp

869 lines
23 KiB
C++

#include <llarp/link/utp.hpp>
#include "router.hpp"
#include <llarp/messages/link_intro.hpp>
6 years ago
#include <llarp/messages/discard.hpp>
#include <llarp/buffer.hpp>
#include <llarp/endian.h>
#include <utp.h>
6 years ago
#include <cassert>
6 years ago
#ifdef __linux__
#include <linux/errqueue.h>
#include <netinet/ip_icmp.h>
#endif
namespace llarp
{
namespace utp
{
6 years ago
constexpr size_t FragmentPadMax = 32;
constexpr size_t FragmentHashSize = 32;
constexpr size_t FragmentNonceSize = 32;
constexpr size_t FragmentOverheadSize =
FragmentHashSize + FragmentNonceSize;
6 years ago
constexpr size_t FragmentBodySize = 512;
6 years ago
constexpr size_t FragmentBufferSize =
FragmentHashSize + FragmentNonceSize + FragmentBodySize;
6 years ago
typedef llarp::AlignedBuffer< FragmentBufferSize > FragmentBuffer;
6 years ago
typedef llarp::AlignedBuffer< MAX_LINK_MSG_SIZE > MessageBuffer;
struct LinkLayer;
struct BaseSession : public ILinkSession
{
RouterContact remoteRC;
6 years ago
utp_socket* sock;
LinkLayer* parent;
bool gotLIM;
PubKey remoteTransportPubKey;
Addr remoteAddr;
SharedSecret sessionKey;
llarp_time_t lastActive;
6 years ago
const static llarp_time_t sessionTimeout = 10 * 1000;
std::queue< FragmentBuffer > sendq;
FragmentBuffer recvBuf;
6 years ago
size_t recvBufOffset;
6 years ago
MessageBuffer recvMsg;
size_t recvMsgOffset;
bool stalled = false;
6 years ago
6 years ago
void
6 years ago
Alive();
6 years ago
6 years ago
/// base
BaseSession(llarp_router* r);
6 years ago
/// outbound
BaseSession(llarp_router* r, utp_socket* s, const RouterContact& rc,
const AddressInfo& addr);
/// inbound
BaseSession(llarp_router* r, utp_socket* s, const Addr& remote);
6 years ago
enum State
{
eInitial,
eConnecting,
eLinkEstablished, // when utp connection is established
eCryptoHandshake, // crypto handshake initiated
eSessionReady, // session is ready
eClose // utp connection is closed
};
6 years ago
llarp_router*
Router();
State state;
void
6 years ago
OnLinkEstablished(LinkLayer* p)
{
6 years ago
parent = p;
EnterState(eLinkEstablished);
llarp::LogDebug("link established with ", remoteAddr);
6 years ago
Alive();
}
6 years ago
void
EnterState(State st);
6 years ago
BaseSession();
virtual ~BaseSession();
void
6 years ago
PumpWrite()
{
6 years ago
while(sendq.size() && !stalled)
{
auto& front = sendq.front();
6 years ago
if(write_ll(front.data(), front.size()) == 0)
{
stalled = true;
return;
}
sendq.pop();
}
}
6 years ago
ssize_t
6 years ago
write_ll(void* buf, size_t sz)
{
6 years ago
if(sock == nullptr)
{
llarp::LogWarn("write_ll failed: no socket");
6 years ago
return 0;
}
6 years ago
return utp_write(sock, buf, sz);
}
bool
6 years ago
VerifyThenDecrypt(FragmentBuffer& buf);
void
EncryptThenHash(FragmentBuffer& buf, const byte_t* ptr, uint32_t sz,
6 years ago
bool isLastFragment);
bool
6 years ago
QueueWriteBuffers(llarp_buffer_t buf)
{
6 years ago
llarp::LogDebug("write ", buf.sz, " bytes to ", remoteAddr);
if(state != eSessionReady)
6 years ago
{
6 years ago
llarp::LogWarn("failed to send ", buf.sz,
" bytes on non ready session state=", state);
return false;
6 years ago
}
size_t sz = buf.sz;
while(sz)
{
6 years ago
uint32_t s = std::min(
(FragmentBodySize - (llarp_randint() % FragmentPadMax)), sz);
sendq.emplace();
EncryptThenHash(sendq.back(), buf.cur, s, ((sz - s) == 0));
buf.cur += s;
sz -= s;
}
return true;
}
6 years ago
void
Connect()
{
utp_connect(sock, remoteAddr, remoteAddr.SockLen());
EnterState(eConnecting);
}
void
OutboundLinkEstablished(LinkLayer* p)
{
OnLinkEstablished(p);
KeyExchangeNonce nonce;
nonce.Randomize();
gotLIM = true;
EnterState(eCryptoHandshake);
6 years ago
if(DoKeyExchange(Router()->crypto.transport_dh_client, nonce,
remoteTransportPubKey, Router()->encryption))
6 years ago
{
6 years ago
SendHandshake(nonce, sock);
6 years ago
EnterState(eSessionReady);
}
}
// send our RC to the remote
void
SendHandshake(const KeyExchangeNonce& n, utp_socket* s)
{
auto buf = InitBuffer(recvBuf.data(), recvBuf.size());
// fastforward buffer for handshake to fit before
buf.cur += sizeof(uint32_t) * 2;
byte_t* begin = buf.cur;
LinkIntroMessage msg;
msg.rc = Router()->rc;
6 years ago
msg.N = n;
6 years ago
if(!msg.BEncode(&buf))
{
llarp::LogError("failed to encode our RC for handshake");
6 years ago
Close();
6 years ago
return;
}
uint32_t sz = buf.cur - begin;
llarp::LogDebug("handshake is of size ", sz, " bytes");
// write handshake header
buf.cur = buf.base;
llarp_buffer_put_uint32(&buf, LLARP_PROTO_VERSION);
llarp_buffer_put_uint32(&buf, sz);
// send it
6 years ago
write_ll(recvBuf.data(), sz + (sizeof(uint32_t) * 2));
6 years ago
sock = s;
}
6 years ago
bool
6 years ago
DoKeyExchange(llarp_transport_dh_func dh, const KeyExchangeNonce& n,
const PubKey& other, const SecretKey& secret)
{
6 years ago
PubKey us = llarp::seckey_topublic(secret);
llarp::LogDebug("DH us=", us, " then=", other, " n=", n);
if(!dh(sessionKey, other, secret, n))
{
llarp::LogError("key exchange with ", other, " failed");
6 years ago
Close();
6 years ago
return false;
}
6 years ago
return true;
}
void
6 years ago
TickImpl(llarp_time_t now)
{
}
void
6 years ago
Close(bool remove = true);
6 years ago
void
RecvHandshake(const void* buf, size_t bufsz, LinkLayer* p, utp_socket* s);
bool
Recv(const void* buf, size_t sz)
{
const byte_t* ptr = (const byte_t*)buf;
llarp::LogDebug("utp read ", sz, " from ", remoteAddr);
6 years ago
size_t s = sz;
while(s > 0)
{
6 years ago
auto dlt = recvBuf.size() - recvBufOffset;
if(dlt > 0)
{
llarp::LogDebug("dlt=", dlt, " offset=", recvBufOffset, " s=", s);
memcpy(recvBuf.data() + recvBufOffset, ptr, dlt);
if(!VerifyThenDecrypt(recvBuf))
return false;
recvBufOffset = 0;
ptr += dlt;
s -= dlt;
}
else
{
if(!VerifyThenDecrypt(recvBuf))
return false;
recvBufOffset = 0;
}
}
6 years ago
Alive();
return true;
}
bool
6 years ago
IsTimedOut(llarp_time_t now) const
{
if(now < lastActive)
return false;
6 years ago
return now - lastActive > sessionTimeout;
}
const PubKey&
6 years ago
RemotePubKey() const
{
return remoteRC.pubkey;
}
const Addr&
GetRemoteEndpoint() const
{
return remoteAddr;
}
void
MarkEstablished();
6 years ago
}; // namespace utp
struct LinkLayer : public ILinkLayer
{
utp_context* _utp_ctx = nullptr;
6 years ago
llarp_router* router = nullptr;
static uint64
6 years ago
OnRead(utp_callback_arguments* arg);
static uint64
SendTo(utp_callback_arguments* arg)
{
LinkLayer* l =
static_cast< LinkLayer* >(utp_context_get_userdata(arg->context));
6 years ago
llarp::LogDebug("utp_sendto ", Addr(*arg->address), " ", arg->len,
" bytes");
if(sendto(l->m_udp.fd, arg->buf, arg->len, arg->flags, arg->address,
arg->address_len)
== -1)
{
llarp::LogError("sendto failed: ", strerror(errno));
}
return 0;
}
static uint64
OnError(utp_callback_arguments* arg)
{
llarp::LogError(utp_error_code_names[arg->error_code]);
return 0;
}
static uint64
6 years ago
OnStateChange(utp_callback_arguments*);
static uint64
OnAccept(utp_callback_arguments*);
6 years ago
static uint64
OnLog(utp_callback_arguments* arg)
{
llarp::LogDebug(arg->buf);
return 0;
}
6 years ago
LinkLayer(llarp_router* r) : ILinkLayer()
{
6 years ago
router = r;
_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);
6 years ago
utp_set_callback(_utp_ctx, UTP_ON_STATE_CHANGE,
&LinkLayer::OnStateChange);
utp_set_callback(_utp_ctx, UTP_ON_READ, &LinkLayer::OnRead);
6 years ago
utp_set_callback(_utp_ctx, UTP_ON_ERROR, &LinkLayer::OnError);
utp_set_callback(_utp_ctx, UTP_LOG, &LinkLayer::OnLog);
6 years ago
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);
6 years ago
utp_context_set_option(_utp_ctx, UTP_SNDBUF, MAX_LINK_MSG_SIZE * 64);
utp_context_set_option(_utp_ctx, UTP_RCVBUF, MAX_LINK_MSG_SIZE * 64);
utp_set_callback(
_utp_ctx, UTP_GET_UDP_MTU,
[](utp_callback_arguments*) -> uint64 { return 1392; });
}
~LinkLayer()
{
utp_destroy(_utp_ctx);
}
uint16_t
Rank() const
{
return 1;
}
void
RecvFrom(const Addr& from, const void* buf, size_t sz)
{
utp_process_udp(_utp_ctx, (const byte_t*)buf, sz, from, from.SockLen());
}
6 years ago
#ifdef __linux__
void
ProcessICMP()
{
do
{
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
llarp::LogError("failed to read icmp for utp ", strerror(errno));
return;
}
for(cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
{
if(cmsg->cmsg_type != IP_RECVERR)
{
continue;
}
if(cmsg->cmsg_level != SOL_IP)
{
continue;
}
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);
}
#endif
void
Pump()
{
6 years ago
ReadPump();
6 years ago
ILinkLayer::Pump();
}
6 years ago
void
ReadPump()
{
#ifdef __linux__
ProcessICMP();
#endif
}
void
Stop()
{
}
6 years ago
llarp_router*
GetRouter();
bool
KeyGen(SecretKey& k)
{
6 years ago
router->crypto.encryption_keygen(k);
return true;
}
6 years ago
void
Tick(llarp_time_t now)
{
#ifdef __linux__
ProcessICMP();
#endif
utp_check_timeouts(_utp_ctx);
ILinkLayer::Tick(now);
}
6 years ago
ILinkSession*
NewOutboundSession(const RouterContact& rc, const AddressInfo& addr);
utp_socket*
NewSocket()
{
return utp_create_socket(_utp_ctx);
}
const char*
Name() const
{
return "utp";
}
};
std::unique_ptr< ILinkLayer >
NewServer(llarp_router* r)
{
6 years ago
return std::unique_ptr< LinkLayer >(new LinkLayer(r));
}
6 years ago
BaseSession::BaseSession(llarp_router* r)
{
6 years ago
parent = nullptr;
recvMsgOffset = 0;
SendKeepAlive = [&]() -> bool {
if(sendq.size() == 0)
{
DiscardMessage msg;
byte_t tmp[128] = {0};
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
if(!msg.BEncode(&buf))
return false;
buf.sz = buf.cur - buf.base;
buf.cur = buf.base;
if(!this->QueueWriteBuffers(buf))
return false;
}
return true;
};
6 years ago
recvBufOffset = 0;
TimedOut = [&](llarp_time_t now) -> bool {
return this->IsTimedOut(now);
};
6 years ago
GetPubKey = std::bind(&BaseSession::RemotePubKey, this);
6 years ago
lastActive = llarp_time_now_ms();
6 years ago
Pump = std::bind(&BaseSession::PumpWrite, this);
6 years ago
Tick = std::bind(&BaseSession::TickImpl, this, std::placeholders::_1);
6 years ago
SendMessageBuffer = [&](llarp_buffer_t buf) -> bool {
return this->QueueWriteBuffers(buf);
};
6 years ago
IsEstablished = [&]() { return this->state == eSessionReady; };
HandleLinkIntroMessage = [](const LinkIntroMessage*) -> bool {
return false;
};
6 years ago
SendClose = std::bind(&BaseSession::Close, this, false);
6 years ago
}
6 years ago
BaseSession::BaseSession(llarp_router* r, utp_socket* s,
const RouterContact& rc, const AddressInfo& addr)
: BaseSession(r)
{
6 years ago
remoteRC.Clear();
remoteTransportPubKey = addr.pubkey;
remoteRC = rc;
sock = s;
assert(utp_set_userdata(sock, this) == this);
6 years ago
assert(s == sock);
6 years ago
remoteAddr = addr;
Start = std::bind(&BaseSession::Connect, this);
}
6 years ago
BaseSession::BaseSession(llarp_router* r, utp_socket* s, const Addr& addr)
: BaseSession(r)
{
remoteRC.Clear();
sock = s;
6 years ago
assert(s == sock);
6 years ago
assert(utp_set_userdata(sock, this) == this);
remoteAddr = addr;
Start = []() {};
}
6 years ago
llarp_router*
BaseSession::Router()
{
6 years ago
return parent->router;
}
BaseSession::~BaseSession()
{
6 years ago
if(sock)
{
utp_close(sock);
utp_set_userdata(sock, nullptr);
}
}
6 years ago
ILinkSession*
LinkLayer::NewOutboundSession(const RouterContact& rc,
const AddressInfo& addr)
{
6 years ago
return new BaseSession(router, utp_create_socket(_utp_ctx), rc, addr);
6 years ago
}
uint64
LinkLayer::OnRead(utp_callback_arguments* arg)
{
6 years ago
LinkLayer* parent =
static_cast< LinkLayer* >(utp_context_get_userdata(arg->context));
6 years ago
BaseSession* self =
static_cast< BaseSession* >(utp_get_userdata(arg->socket));
6 years ago
#ifdef __linux__
parent->ProcessICMP();
#endif
6 years ago
if(self)
{
6 years ago
if(self->state == BaseSession::eClose)
{
return 0;
}
else if(self->state == BaseSession::eSessionReady)
6 years ago
{
6 years ago
self->Recv(arg->buf, arg->len);
6 years ago
}
6 years ago
else if(self->state == BaseSession::eLinkEstablished)
{
self->RecvHandshake(arg->buf, arg->len, parent, arg->socket);
}
}
else
{
llarp::LogWarn("utp_socket got data with no underlying session");
}
return 0;
}
6 years ago
uint64
LinkLayer::OnStateChange(utp_callback_arguments* arg)
{
6 years ago
LinkLayer* l =
static_cast< LinkLayer* >(utp_context_get_userdata(arg->context));
BaseSession* session =
static_cast< BaseSession* >(utp_get_userdata(arg->socket));
6 years ago
if(session)
6 years ago
{
6 years ago
if(arg->state == UTP_STATE_CONNECT)
{
if(session->state == BaseSession::eClose)
{
return 0;
}
session->OutboundLinkEstablished(l);
}
6 years ago
else if(arg->state == UTP_STATE_WRITABLE)
{
session->stalled = false;
session->PumpWrite();
}
6 years ago
else if(arg->state == UTP_STATE_EOF)
{
6 years ago
session->Close(false);
6 years ago
}
6 years ago
}
return 0;
}
uint64
LinkLayer::OnAccept(utp_callback_arguments* arg)
{
LinkLayer* self =
static_cast< LinkLayer* >(utp_context_get_userdata(arg->context));
Addr remote(*arg->address);
llarp::LogDebug("utp accepted from ", remote);
6 years ago
if(self->HasSessionVia(remote))
{
// TODO should we do this?
llarp::LogWarn(
"utp socket closed because we already have a session "
"via ",
remote);
utp_close(arg->socket);
return 0;
}
6 years ago
BaseSession* session = new BaseSession(self->router, arg->socket, remote);
self->PutSession(remote, session);
6 years ago
session->OnLinkEstablished(self);
return 0;
}
6 years ago
void
BaseSession::EncryptThenHash(FragmentBuffer& buf, const byte_t* ptr,
uint32_t sz, bool isLastFragment)
{
6 years ago
llarp::LogDebug("encrypt then hash ", sz, " bytes last=", isLastFragment);
6 years ago
buf.Randomize();
const byte_t* nonce = buf.data() + FragmentHashSize;
byte_t* body = buf.data() + FragmentOverheadSize;
byte_t* base = body;
if(isLastFragment)
htobe32buf(body, 0);
body += sizeof(uint32_t);
htobe32buf(body, sz);
body += sizeof(uint32_t);
memcpy(body, ptr, sz);
auto payload = InitBuffer(base, FragmentBodySize);
parent->router->crypto.xchacha20(payload, sessionKey, nonce);
6 years ago
payload.base = buf.data() + FragmentHashSize;
payload.cur = payload.base;
payload.sz = FragmentBufferSize - FragmentHashSize;
parent->router->crypto.hmac(buf.data(), payload, sessionKey);
6 years ago
}
void
BaseSession::EnterState(State st)
{
6 years ago
state = st;
6 years ago
if(st == eSessionReady)
{
6 years ago
llarp::LogInfo("map ", remoteRC.pubkey, " to ", remoteAddr);
6 years ago
parent->MapAddr(this->remoteAddr, remoteRC.pubkey);
Router()->HandleLinkSessionEstablished(remoteRC);
}
6 years ago
Alive();
6 years ago
}
bool
BaseSession::VerifyThenDecrypt(FragmentBuffer& buf)
{
6 years ago
llarp::LogDebug("verify then decrypt ", remoteAddr);
6 years ago
ShortHash digest;
6 years ago
auto hbuf = InitBuffer(buf.data() + FragmentHashSize,
buf.size() - FragmentHashSize);
if(!Router()->crypto.hmac(digest, hbuf, sessionKey))
6 years ago
{
llarp::LogError("keyed hash failed");
return false;
}
6 years ago
if(memcmp(buf.data(), digest.data(), 32))
6 years ago
{
6 years ago
llarp::LogError("Message Integrity Failed: got ", digest, " from ",
remoteAddr);
llarp::DumpBuffer(hbuf);
6 years ago
return false;
}
AlignedBuffer< FragmentNonceSize > nonce(buf.data() + FragmentHashSize);
auto body = InitBuffer(buf.data() + FragmentOverheadSize,
FragmentBufferSize - FragmentOverheadSize);
Router()->crypto.xchacha20(body, sessionKey, nonce);
uint32_t upper, lower;
if(!(llarp_buffer_read_uint32(&body, &upper)
&& llarp_buffer_read_uint32(&body, &lower)))
return false;
bool fragmentEnd = upper == 0;
6 years ago
llarp::LogDebug("fragment size ", lower, " from ", remoteAddr);
if(lower + recvMsgOffset > recvMsg.size())
6 years ago
{
llarp::LogError("Fragment too big: ", lower, " bytes");
return false;
}
6 years ago
byte_t* ptr = recvMsg.data() + recvMsgOffset;
6 years ago
memcpy(ptr, body.cur, lower);
6 years ago
recvMsgOffset += lower;
6 years ago
if(fragmentEnd)
{
// got a message
6 years ago
llarp::LogDebug("end of message from ", remoteAddr);
auto mbuf = InitBuffer(recvMsg.data(), recvMsgOffset);
6 years ago
auto result = Router()->HandleRecvLinkMessageBuffer(this, mbuf);
6 years ago
if(!result)
{
llarp::LogWarn("failed to handle message from ", remoteAddr);
llarp::DumpBuffer(mbuf);
}
recvMsgOffset = 0;
6 years ago
return result;
}
return true;
}
void
BaseSession::RecvHandshake(const void* buf, size_t bufsz, LinkLayer* p,
utp_socket* s)
{
size_t sz = bufsz;
parent = p;
sock = s;
llarp::LogDebug("recv handshake ", sz, " from ", remoteAddr);
if(recvBuf.size() < sz)
{
llarp::LogDebug("handshake too big from ", remoteAddr);
6 years ago
Close();
6 years ago
return;
}
if(sz <= 8)
{
llarp::LogDebug("handshake too small from ", remoteAddr);
6 years ago
Close();
6 years ago
return;
}
memcpy(recvBuf.data(), buf, sz);
// process handshake header
uint8_t* ptr = recvBuf.data();
uint32_t version = bufbe32toh(ptr);
if(version != LLARP_PROTO_VERSION)
{
llarp::LogWarn("protocol version missmatch ", version,
" != ", LLARP_PROTO_VERSION);
6 years ago
Close();
6 years ago
return;
}
ptr += sizeof(uint32_t);
sz -= sizeof(uint32_t);
uint32_t limsz = bufbe32toh(ptr);
ptr += sizeof(uint32_t);
sz -= sizeof(uint32_t);
if(limsz > sz)
{
// not enough data
// TODO: don't bail here, continue reading
llarp::LogDebug("not enough data for handshake, want ", limsz,
" bytes but got ", sz);
6 years ago
Close();
6 years ago
return;
}
llarp::LogInfo("read LIM from ", remoteAddr);
// process LIM
auto mbuf = InitBuffer(ptr, limsz);
LinkIntroMessage msg(this);
if(!msg.BDecode(&mbuf))
{
llarp::LogError("Failed to parse LIM from ", remoteAddr);
llarp::DumpBuffer(mbuf);
6 years ago
Close();
6 years ago
return;
}
if(!msg.HandleMessage(Router()))
{
llarp::LogError("failed to verify signature of rc");
return;
}
remoteRC = msg.rc;
6 years ago
if(!DoKeyExchange(Router()->crypto.transport_dh_server, msg.N,
remoteRC.enckey, parent->TransportSecretKey()))
return;
gotLIM = true;
6 years ago
llarp::LogInfo("we got a new session from ", GetPubKey());
EnterState(eSessionReady);
6 years ago
Alive();
}
void
BaseSession::Close(bool remove)
{
if(state != eClose)
{
utp_shutdown(sock, SHUT_RDWR);
utp_close(sock);
utp_set_userdata(sock, nullptr);
}
EnterState(eClose);
sock = nullptr;
if(remove)
parent->RemoveSessionVia(remoteAddr);
}
void
BaseSession::Alive()
{
lastActive = llarp_time_now_ms();
if(sock)
utp_read_drained(sock);
if(parent)
utp_issue_deferred_acks(parent->_utp_ctx);
6 years ago
}
} // namespace utp
} // namespace llarp