2018-05-22 15:54:19 +00:00
|
|
|
#include <llarp/crypto_async.h>
|
2018-05-18 14:40:06 +00:00
|
|
|
#include <llarp/iwp.h>
|
|
|
|
#include <llarp/net.h>
|
2018-05-19 17:21:56 +00:00
|
|
|
#include <llarp/time.h>
|
2018-05-25 17:52:10 +00:00
|
|
|
#include "link/encoder.hpp"
|
2018-05-18 14:40:06 +00:00
|
|
|
|
2018-05-23 20:37:43 +00:00
|
|
|
#include <sodium/crypto_sign_ed25519.h>
|
|
|
|
|
2018-06-01 17:54:21 +00:00
|
|
|
#include <algorithm>
|
2018-05-22 15:54:19 +00:00
|
|
|
#include <bitset>
|
2018-05-18 16:08:47 +00:00
|
|
|
#include <cassert>
|
2018-05-18 14:48:30 +00:00
|
|
|
#include <fstream>
|
2018-05-22 15:54:19 +00:00
|
|
|
#include <list>
|
2018-05-18 14:40:06 +00:00
|
|
|
#include <map>
|
2018-05-21 14:28:15 +00:00
|
|
|
#include <mutex>
|
2018-05-22 15:54:19 +00:00
|
|
|
#include <queue>
|
|
|
|
#include <set>
|
2018-05-29 16:11:32 +00:00
|
|
|
#include <unordered_map>
|
2018-05-22 15:54:19 +00:00
|
|
|
#include <vector>
|
2018-05-18 14:48:30 +00:00
|
|
|
|
2018-05-25 17:52:10 +00:00
|
|
|
#include "buffer.hpp"
|
2018-05-18 14:40:06 +00:00
|
|
|
#include "crypto.hpp"
|
|
|
|
#include "fs.hpp"
|
2018-05-27 14:04:30 +00:00
|
|
|
#include "logger.hpp"
|
2018-05-19 17:21:56 +00:00
|
|
|
#include "mem.hpp"
|
2018-05-18 14:40:06 +00:00
|
|
|
#include "net.hpp"
|
2018-05-25 17:52:10 +00:00
|
|
|
#include "router.hpp"
|
2018-05-29 12:15:48 +00:00
|
|
|
#include "str.hpp"
|
2018-05-18 14:40:06 +00:00
|
|
|
|
|
|
|
namespace iwp
|
|
|
|
{
|
2018-05-29 13:40:26 +00:00
|
|
|
// session activity timeout is 5s
|
|
|
|
constexpr llarp_time_t SESSION_TIMEOUT = 5000;
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-05-29 17:45:33 +00:00
|
|
|
constexpr size_t MAX_PAD = 128;
|
2018-05-28 20:51:15 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
enum msgtype
|
2018-05-19 17:21:56 +00:00
|
|
|
{
|
2018-05-22 15:54:19 +00:00
|
|
|
eALIV = 0x00,
|
|
|
|
eXMIT = 0x01,
|
|
|
|
eACKS = 0x02,
|
|
|
|
eFRAG = 0x03
|
|
|
|
};
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-05-25 17:52:10 +00:00
|
|
|
typedef std::vector< byte_t > sendbuf_t;
|
|
|
|
|
|
|
|
enum header_flag
|
|
|
|
{
|
|
|
|
eSessionInvalidated = (1 << 0),
|
|
|
|
eHighPacketDrop = (1 << 1),
|
|
|
|
eHighMTUDetected = (1 << 2),
|
|
|
|
eProtoUpgrade = (1 << 3)
|
|
|
|
};
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
/** plaintext frame header */
|
|
|
|
struct frame_header
|
2018-05-19 17:21:56 +00:00
|
|
|
{
|
2018-05-24 16:04:34 +00:00
|
|
|
byte_t *ptr;
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-05-24 16:04:34 +00:00
|
|
|
frame_header(byte_t *buf) : ptr(buf)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
|
|
|
}
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-05-24 16:04:34 +00:00
|
|
|
byte_t *
|
2018-05-22 15:54:19 +00:00
|
|
|
data()
|
|
|
|
{
|
2018-05-24 16:04:34 +00:00
|
|
|
return ptr + 6;
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
uint8_t &
|
|
|
|
version()
|
|
|
|
{
|
|
|
|
return ptr[0];
|
|
|
|
}
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
uint8_t &
|
|
|
|
msgtype()
|
|
|
|
{
|
|
|
|
return ptr[1];
|
|
|
|
}
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
uint16_t
|
|
|
|
size() const
|
|
|
|
{
|
2018-05-25 17:52:10 +00:00
|
|
|
uint16_t sz;
|
|
|
|
memcpy(&sz, ptr + 2, 2);
|
2018-05-22 15:54:19 +00:00
|
|
|
return sz;
|
|
|
|
}
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
setsize(uint16_t sz)
|
|
|
|
{
|
2018-05-25 17:52:10 +00:00
|
|
|
memcpy(ptr + 2, &sz, 2);
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-06-02 13:18:11 +00:00
|
|
|
uint8_t &
|
|
|
|
flags()
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-05-24 16:04:34 +00:00
|
|
|
return ptr[5];
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
setflag(header_flag f)
|
|
|
|
{
|
2018-05-24 16:04:34 +00:00
|
|
|
ptr[5] |= f;
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
|
|
|
};
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-05-25 17:52:10 +00:00
|
|
|
byte_t *
|
|
|
|
init_sendbuf(sendbuf_t &buf, msgtype t, uint16_t sz, uint8_t flags)
|
|
|
|
{
|
|
|
|
buf.resize(6 + sz);
|
|
|
|
frame_header hdr(buf.data());
|
|
|
|
hdr.version() = 0;
|
|
|
|
hdr.msgtype() = t;
|
|
|
|
hdr.setsize(sz);
|
|
|
|
buf[4] = 0;
|
|
|
|
buf[5] = flags;
|
|
|
|
return hdr.data();
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
/** xmit header */
|
|
|
|
struct xmit
|
2018-05-19 17:21:56 +00:00
|
|
|
{
|
2018-05-24 16:04:34 +00:00
|
|
|
byte_t buffer[48];
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
xmit()
|
|
|
|
{
|
|
|
|
}
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-05-24 16:04:34 +00:00
|
|
|
xmit(byte_t *ptr)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-05-24 16:04:34 +00:00
|
|
|
memcpy(buffer, ptr, sizeof(buffer));
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
xmit(const xmit &other)
|
|
|
|
{
|
2018-05-24 16:04:34 +00:00
|
|
|
memcpy(buffer, other.buffer, sizeof(buffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
set_info(const byte_t *hash, uint64_t id, uint16_t fragsz, uint16_t lastsz,
|
|
|
|
uint8_t numfrags, uint8_t flags = 0x01)
|
|
|
|
{
|
|
|
|
// big endian assumed
|
|
|
|
// TODO: implement little endian
|
|
|
|
memcpy(buffer, hash, 32);
|
|
|
|
memcpy(buffer + 32, &id, 8);
|
|
|
|
memcpy(buffer + 40, &fragsz, 2);
|
|
|
|
memcpy(buffer + 42, &lastsz, 2);
|
|
|
|
buffer[44] = 0;
|
|
|
|
buffer[45] = 0;
|
|
|
|
buffer[46] = numfrags;
|
|
|
|
buffer[47] = flags;
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
uint64_t
|
|
|
|
msgid() const
|
|
|
|
{
|
|
|
|
// big endian assumed
|
|
|
|
// TODO: implement little endian
|
2018-05-24 16:04:34 +00:00
|
|
|
const byte_t *start = buffer + 32;
|
2018-05-22 15:54:19 +00:00
|
|
|
const uint64_t *msgid = (const uint64_t *)start;
|
|
|
|
return *msgid;
|
|
|
|
}
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
// size of each full fragment
|
|
|
|
uint16_t
|
|
|
|
fragsize() const
|
|
|
|
{
|
|
|
|
// big endian assumed
|
|
|
|
// TODO: implement little endian
|
2018-05-24 16:04:34 +00:00
|
|
|
const byte_t *start = buffer + 40;
|
|
|
|
const uint16_t *fragsz = (uint16_t *)start;
|
|
|
|
return *fragsz;
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
// number of full fragments
|
|
|
|
uint8_t
|
|
|
|
numfrags() const
|
|
|
|
{
|
2018-05-24 16:04:34 +00:00
|
|
|
return buffer[46];
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// size of the entire message
|
|
|
|
size_t
|
|
|
|
totalsize() const
|
|
|
|
{
|
|
|
|
return (fragsize() * numfrags()) + lastfrag();
|
|
|
|
}
|
|
|
|
|
|
|
|
// size of the last fragment
|
2018-05-24 16:04:34 +00:00
|
|
|
uint16_t
|
2018-05-22 15:54:19 +00:00
|
|
|
lastfrag() const
|
|
|
|
{
|
|
|
|
// big endian assumed
|
|
|
|
// TODO: implement little endian
|
2018-05-24 16:04:34 +00:00
|
|
|
const byte_t *start = buffer + 42;
|
|
|
|
const uint16_t *lastsz = (uint16_t *)start;
|
|
|
|
return *lastsz;
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t
|
2018-05-24 16:04:34 +00:00
|
|
|
flags()
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-05-24 16:04:34 +00:00
|
|
|
return buffer[47];
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector< uint8_t > fragment_t;
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
// forward declare
|
|
|
|
struct session;
|
2018-05-29 18:32:47 +00:00
|
|
|
struct server;
|
2018-05-22 15:54:19 +00:00
|
|
|
|
2018-05-24 16:04:34 +00:00
|
|
|
struct transit_message
|
2018-05-19 17:21:56 +00:00
|
|
|
{
|
2018-05-22 15:54:19 +00:00
|
|
|
session *parent = nullptr;
|
|
|
|
xmit msginfo;
|
2018-05-29 15:47:10 +00:00
|
|
|
std::bitset< 32 > status;
|
2018-05-22 15:54:19 +00:00
|
|
|
|
2018-05-25 17:52:10 +00:00
|
|
|
std::map< uint8_t, fragment_t > frags;
|
2018-05-22 15:54:19 +00:00
|
|
|
fragment_t lastfrag;
|
|
|
|
|
2018-05-24 16:04:34 +00:00
|
|
|
transit_message()
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-05-27 12:49:10 +00:00
|
|
|
~transit_message()
|
|
|
|
{
|
|
|
|
frags.clear();
|
|
|
|
}
|
|
|
|
|
2018-05-29 15:47:10 +00:00
|
|
|
// calculate acked bitmask
|
|
|
|
uint32_t
|
|
|
|
get_bitmask() const
|
|
|
|
{
|
|
|
|
uint32_t bitmask = 0;
|
|
|
|
uint8_t idx = 0;
|
|
|
|
while(idx < 32)
|
|
|
|
{
|
|
|
|
bitmask |= (status.test(idx) ? (1 << idx) : 0);
|
|
|
|
++idx;
|
|
|
|
}
|
|
|
|
return bitmask;
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
// inbound
|
2018-05-24 16:04:34 +00:00
|
|
|
transit_message(const xmit &x) : msginfo(x)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
|
|
|
}
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
// outbound
|
2018-05-24 16:04:34 +00:00
|
|
|
transit_message(session *s) : parent(s)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-05-29 15:47:10 +00:00
|
|
|
/// ack packets based off a bitmask
|
2018-05-25 17:52:10 +00:00
|
|
|
void
|
|
|
|
ack(uint32_t bitmask)
|
|
|
|
{
|
|
|
|
uint8_t idx = 0;
|
2018-05-29 15:47:10 +00:00
|
|
|
while(idx < 32)
|
2018-05-25 17:52:10 +00:00
|
|
|
{
|
|
|
|
if(bitmask & (1 << idx))
|
|
|
|
{
|
|
|
|
status.set(idx);
|
|
|
|
}
|
|
|
|
++idx;
|
|
|
|
}
|
2018-05-29 15:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
should_send_ack() const
|
|
|
|
{
|
|
|
|
if(msginfo.numfrags() == 0)
|
|
|
|
return true;
|
2018-06-03 12:35:50 +00:00
|
|
|
return status.count() % (1 + (msginfo.numfrags() / 3)) == 0;
|
2018-05-25 17:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
completed() const
|
|
|
|
{
|
|
|
|
for(const auto &item : frags)
|
|
|
|
{
|
|
|
|
if(!status.test(item.first))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template < typename T >
|
|
|
|
void
|
2018-06-02 13:18:11 +00:00
|
|
|
generate_xmit(T &queue, byte_t flags = 0)
|
2018-05-25 17:52:10 +00:00
|
|
|
{
|
|
|
|
queue.emplace();
|
|
|
|
auto &xmitbuf = queue.back();
|
2018-06-02 13:18:11 +00:00
|
|
|
auto body_ptr = init_sendbuf(
|
|
|
|
xmitbuf, eXMIT, sizeof(msginfo.buffer) + lastfrag.size(), flags);
|
2018-05-25 17:52:10 +00:00
|
|
|
memcpy(body_ptr, msginfo.buffer, sizeof(msginfo.buffer));
|
|
|
|
body_ptr += sizeof(msginfo.buffer);
|
|
|
|
memcpy(body_ptr, lastfrag.data(), lastfrag.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
template < typename T >
|
|
|
|
void
|
2018-06-02 13:18:11 +00:00
|
|
|
retransmit_frags(T &queue, byte_t flags = 0)
|
2018-05-25 17:52:10 +00:00
|
|
|
{
|
|
|
|
auto msgid = msginfo.msgid();
|
|
|
|
auto fragsize = msginfo.fragsize();
|
|
|
|
for(auto &frag : frags)
|
|
|
|
{
|
|
|
|
if(status.test(frag.first))
|
|
|
|
continue;
|
|
|
|
queue.emplace();
|
|
|
|
auto &fragbuf = queue.back();
|
2018-06-02 13:18:11 +00:00
|
|
|
auto body_ptr = init_sendbuf(fragbuf, eFRAG, 9 + fragsize, flags);
|
2018-05-25 17:52:10 +00:00
|
|
|
memcpy(body_ptr, &msgid, 8);
|
|
|
|
body_ptr[8] = frag.first;
|
|
|
|
memcpy(body_ptr + 9, frag.second.data(), fragsize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 12:27:54 +00:00
|
|
|
bool
|
|
|
|
reassemble(std::vector< byte_t > &buffer)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-05-25 17:52:10 +00:00
|
|
|
auto total = msginfo.totalsize();
|
|
|
|
buffer.resize(total);
|
|
|
|
auto fragsz = msginfo.fragsize();
|
|
|
|
auto ptr = buffer.data();
|
|
|
|
for(const auto &frag : frags)
|
|
|
|
{
|
|
|
|
memcpy(ptr, frag.second.data(), fragsz);
|
|
|
|
ptr += fragsz;
|
|
|
|
}
|
|
|
|
memcpy(ptr, lastfrag.data(), lastfrag.size());
|
|
|
|
return true;
|
2018-05-24 16:04:34 +00:00
|
|
|
}
|
2018-05-25 12:27:54 +00:00
|
|
|
|
2018-05-24 16:04:34 +00:00
|
|
|
void
|
|
|
|
put_message(llarp_buffer_t &buf, const byte_t *hash, uint64_t id,
|
|
|
|
uint16_t mtu = 1024)
|
|
|
|
{
|
|
|
|
status.reset();
|
2018-05-25 17:52:10 +00:00
|
|
|
uint8_t fragid = 0;
|
2018-05-24 16:04:34 +00:00
|
|
|
uint16_t fragsize = mtu;
|
|
|
|
while((buf.cur - buf.base) > fragsize)
|
|
|
|
{
|
|
|
|
fragment_t frag(fragsize);
|
|
|
|
memcpy(frag.data(), buf.cur, fragsize);
|
|
|
|
buf.cur += fragsize;
|
|
|
|
frags[fragid++] = frag;
|
|
|
|
}
|
2018-05-25 17:52:10 +00:00
|
|
|
uint16_t lastfrag = buf.sz - (buf.cur - buf.base);
|
2018-05-24 16:04:34 +00:00
|
|
|
// set info for xmit
|
2018-05-25 17:52:10 +00:00
|
|
|
msginfo.set_info(hash, id, fragsize, lastfrag, frags.size());
|
2018-05-24 16:04:34 +00:00
|
|
|
// copy message hash
|
|
|
|
memcpy(msginfo.buffer, hash, 32);
|
2018-05-25 17:52:10 +00:00
|
|
|
put_lastfrag(buf.cur, lastfrag);
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-05-29 15:47:10 +00:00
|
|
|
put_lastfrag(byte_t *buf, size_t sz)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
|
|
|
lastfrag.resize(sz);
|
|
|
|
memcpy(lastfrag.data(), buf, sz);
|
|
|
|
}
|
2018-05-29 15:47:10 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
put_frag(byte_t fragno, byte_t *buf)
|
|
|
|
{
|
|
|
|
auto itr = frags.find(fragno);
|
|
|
|
if(itr == frags.end())
|
|
|
|
return false;
|
|
|
|
memcpy(itr->second.data(), buf, msginfo.fragsize());
|
2018-06-03 12:35:50 +00:00
|
|
|
status.set(fragno);
|
2018-05-29 15:47:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-22 15:54:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct frame_state
|
2018-05-19 17:21:56 +00:00
|
|
|
{
|
2018-06-02 13:18:11 +00:00
|
|
|
byte_t rxflags = 0;
|
|
|
|
byte_t txflags = 0;
|
2018-05-24 16:04:34 +00:00
|
|
|
uint64_t rxids = 0;
|
|
|
|
uint64_t txids = 0;
|
2018-05-22 15:54:19 +00:00
|
|
|
llarp_time_t lastEvent = 0;
|
2018-05-24 16:04:34 +00:00
|
|
|
std::map< uint64_t, transit_message > rx;
|
|
|
|
std::map< uint64_t, transit_message * > tx;
|
2018-05-22 15:54:19 +00:00
|
|
|
|
2018-05-25 17:52:10 +00:00
|
|
|
typedef std::queue< sendbuf_t > sendqueue_t;
|
2018-05-22 15:54:19 +00:00
|
|
|
|
2018-05-25 17:52:10 +00:00
|
|
|
llarp_router *router = nullptr;
|
|
|
|
llarp_link_session *parent = nullptr;
|
|
|
|
|
|
|
|
sendqueue_t sendqueue;
|
2018-05-24 16:04:34 +00:00
|
|
|
|
2018-06-02 13:18:11 +00:00
|
|
|
/// return true if both sides have the same state flags
|
|
|
|
bool
|
|
|
|
flags_agree(byte_t flags) const
|
|
|
|
{
|
2018-06-02 14:41:30 +00:00
|
|
|
return ((rxflags & flags) & (txflags & flags)) == flags;
|
2018-06-02 13:18:11 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 16:04:34 +00:00
|
|
|
void
|
2018-05-25 17:52:10 +00:00
|
|
|
clear()
|
|
|
|
{
|
|
|
|
rx.clear();
|
|
|
|
for(auto &item : tx)
|
|
|
|
delete item.second;
|
|
|
|
tx.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-05-26 18:31:45 +00:00
|
|
|
inbound_frame_complete(uint64_t id);
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
2018-05-24 16:04:34 +00:00
|
|
|
push_ackfor(uint64_t id, uint32_t bitmask)
|
2018-05-19 17:21:56 +00:00
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Debug("ACK for msgid=", id, " mask=", bitmask);
|
2018-05-25 17:52:10 +00:00
|
|
|
sendqueue.emplace();
|
|
|
|
auto &buf = sendqueue.back();
|
2018-06-02 13:18:11 +00:00
|
|
|
init_sendbuf(buf, eACKS, 12, txflags);
|
2018-05-22 15:54:19 +00:00
|
|
|
// TODO: this assumes big endian
|
2018-05-24 16:04:34 +00:00
|
|
|
memcpy(buf.data() + 6, &id, 8);
|
|
|
|
memcpy(buf.data() + 14, &bitmask, 4);
|
2018-05-19 17:21:56 +00:00
|
|
|
}
|
2018-05-22 15:54:19 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
got_xmit(frame_header &hdr, size_t sz)
|
2018-05-19 17:21:56 +00:00
|
|
|
{
|
2018-05-22 15:54:19 +00:00
|
|
|
if(hdr.size() > sz)
|
2018-05-19 17:21:56 +00:00
|
|
|
{
|
2018-05-22 15:54:19 +00:00
|
|
|
// overflow
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Warn("invalid XMIT frame size ", hdr.size(), " > ", sz);
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
sz = hdr.size();
|
|
|
|
|
|
|
|
// extract xmit data
|
|
|
|
xmit x(hdr.data());
|
|
|
|
|
2018-05-24 16:04:34 +00:00
|
|
|
const auto bufsz = sizeof(x.buffer);
|
|
|
|
|
|
|
|
if(sz - bufsz < x.lastfrag())
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
|
|
|
// bad size of last fragment
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Warn("XMIT frag size missmatch ", sz - bufsz, " < ",
|
2018-05-27 14:04:30 +00:00
|
|
|
x.lastfrag());
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-24 16:04:34 +00:00
|
|
|
// check LSB set on flags
|
|
|
|
if(x.flags() & 0x01)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-05-25 17:52:10 +00:00
|
|
|
auto id = x.msgid();
|
2018-05-29 14:57:13 +00:00
|
|
|
auto itr = rx.emplace(id, x);
|
2018-05-25 17:52:10 +00:00
|
|
|
if(itr.second)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-05-25 17:52:10 +00:00
|
|
|
// inserted, put last fragment
|
|
|
|
itr.first->second.put_lastfrag(hdr.data() + sizeof(x.buffer),
|
|
|
|
x.lastfrag());
|
|
|
|
if(x.numfrags() == 0)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-05-25 17:52:10 +00:00
|
|
|
push_ackfor(id, 0);
|
|
|
|
return inbound_frame_complete(id);
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-05-25 17:52:10 +00:00
|
|
|
return true;
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
|
|
|
else
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Warn("duplicate XMIT msgid=", x.msgid());
|
2018-05-19 17:21:56 +00:00
|
|
|
}
|
|
|
|
else
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Warn("LSB not set on flags");
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
2018-05-19 17:21:56 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
alive()
|
|
|
|
{
|
|
|
|
lastEvent = llarp_time_now_ms();
|
|
|
|
}
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
|
|
|
got_frag(frame_header &hdr, size_t sz)
|
|
|
|
{
|
2018-05-29 15:47:10 +00:00
|
|
|
if(hdr.size() > sz)
|
|
|
|
{
|
|
|
|
// overflow
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Warn("invalid FRAG frame size ", hdr.size(), " > ", sz);
|
2018-05-29 15:47:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
sz = hdr.size();
|
|
|
|
|
|
|
|
if(sz <= 9)
|
|
|
|
{
|
|
|
|
// underflow
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Warn("invalid FRAG frame size ", sz, " <= 9");
|
2018-05-29 15:47:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t msgid;
|
|
|
|
byte_t fragno;
|
|
|
|
// assumes big endian
|
|
|
|
// TODO: implement little endian
|
|
|
|
memcpy(&msgid, hdr.data(), 8);
|
|
|
|
memcpy(&fragno, hdr.data() + 8, 1);
|
|
|
|
|
|
|
|
auto itr = rx.find(msgid);
|
|
|
|
if(itr == rx.end())
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Warn("no such RX fragment, msgid=", msgid);
|
2018-05-29 15:47:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto fragsize = itr->second.msginfo.fragsize();
|
|
|
|
if(fragsize != sz - 9)
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Warn("RX fragment size missmatch ", fragsize, " != ", sz - 9);
|
2018-05-29 15:47:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!itr->second.put_frag(fragno, hdr.data() + 9))
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Warn("inbound message does not have fragment msgid=", msgid,
|
2018-05-29 15:47:10 +00:00
|
|
|
" fragno=", (int)fragno);
|
|
|
|
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())
|
|
|
|
{
|
|
|
|
push_ackfor(msgid, mask);
|
|
|
|
}
|
|
|
|
return true;
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
2018-05-26 18:31:45 +00:00
|
|
|
got_acks(frame_header &hdr, size_t sz);
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
// queue new outbound message
|
|
|
|
void
|
2018-05-25 12:24:31 +00:00
|
|
|
queue_tx(uint64_t id, transit_message *msg)
|
2018-05-19 17:21:56 +00:00
|
|
|
{
|
2018-05-29 14:57:13 +00:00
|
|
|
auto itr = tx.emplace(id, msg);
|
2018-05-25 17:52:10 +00:00
|
|
|
if(itr.second)
|
|
|
|
{
|
2018-06-02 13:18:11 +00:00
|
|
|
msg->generate_xmit(sendqueue, txflags);
|
|
|
|
msg->retransmit_frags(sendqueue, txflags);
|
2018-05-25 17:52:10 +00:00
|
|
|
}
|
|
|
|
else // duplicate
|
|
|
|
delete msg;
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get next frame to encrypt and transmit
|
|
|
|
bool
|
|
|
|
next_frame(llarp_buffer_t &buf)
|
|
|
|
{
|
|
|
|
if(sendqueue.size())
|
|
|
|
{
|
|
|
|
auto &send = sendqueue.front();
|
|
|
|
buf.base = send.data();
|
|
|
|
buf.cur = send.data();
|
|
|
|
buf.sz = send.size();
|
|
|
|
return true;
|
|
|
|
}
|
2018-05-19 17:21:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
pop_next_frame()
|
|
|
|
{
|
|
|
|
sendqueue.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
process(uint8_t *buf, size_t sz)
|
|
|
|
{
|
|
|
|
frame_header hdr(buf);
|
2018-06-02 14:44:26 +00:00
|
|
|
if(hdr.flags() & eSessionInvalidated)
|
2018-06-02 13:18:11 +00:00
|
|
|
{
|
|
|
|
rxflags |= eSessionInvalidated;
|
|
|
|
}
|
2018-05-22 15:54:19 +00:00
|
|
|
switch(hdr.msgtype())
|
|
|
|
{
|
|
|
|
case eALIV:
|
2018-06-02 13:18:11 +00:00
|
|
|
if(rxflags & eSessionInvalidated)
|
|
|
|
{
|
|
|
|
txflags |= eSessionInvalidated;
|
|
|
|
}
|
2018-05-22 15:54:19 +00:00
|
|
|
return true;
|
|
|
|
case eXMIT:
|
2018-05-25 17:52:10 +00:00
|
|
|
return got_xmit(hdr, sz - 6);
|
2018-05-22 15:54:19 +00:00
|
|
|
case eACKS:
|
2018-05-25 17:52:10 +00:00
|
|
|
return got_acks(hdr, sz - 6);
|
2018-05-22 15:54:19 +00:00
|
|
|
case eFRAG:
|
2018-05-25 17:52:10 +00:00
|
|
|
return got_frag(hdr, sz - 6);
|
2018-05-22 15:54:19 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-05-18 17:10:48 +00:00
|
|
|
};
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
struct session
|
2018-05-18 16:08:47 +00:00
|
|
|
{
|
2018-05-22 15:54:19 +00:00
|
|
|
llarp_udp_io *udp;
|
|
|
|
llarp_crypto *crypto;
|
|
|
|
llarp_async_iwp *iwp;
|
|
|
|
llarp_logic *logic;
|
2018-05-25 17:52:10 +00:00
|
|
|
|
|
|
|
llarp_link_session *parent = nullptr;
|
2018-05-29 18:32:47 +00:00
|
|
|
server *serv = nullptr;
|
2018-05-25 17:52:10 +00:00
|
|
|
|
|
|
|
llarp_rc *our_router = nullptr;
|
|
|
|
llarp_rc remote_router;
|
2018-05-24 16:04:34 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
llarp_seckey_t eph_seckey;
|
|
|
|
llarp_pubkey_t remote;
|
|
|
|
llarp_sharedkey_t sessionkey;
|
2018-05-18 17:10:48 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
llarp_link_establish_job *establish_job = nullptr;
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-06-02 13:18:11 +00:00
|
|
|
uint32_t establish_job_id = 0;
|
|
|
|
uint32_t frames = 0;
|
2018-05-25 17:52:10 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
llarp::Addr addr;
|
|
|
|
iwp_async_intro intro;
|
|
|
|
iwp_async_introack introack;
|
|
|
|
iwp_async_session_start start;
|
|
|
|
frame_state frame;
|
|
|
|
|
|
|
|
byte_t token[32];
|
2018-05-28 20:51:15 +00:00
|
|
|
byte_t workbuf[MAX_PAD + 128];
|
2018-05-22 15:54:19 +00:00
|
|
|
|
|
|
|
enum State
|
|
|
|
{
|
|
|
|
eInitial,
|
2018-05-23 20:37:43 +00:00
|
|
|
eIntroRecv,
|
2018-05-22 15:54:19 +00:00
|
|
|
eIntroSent,
|
|
|
|
eIntroAckSent,
|
|
|
|
eIntroAckRecv,
|
2018-05-25 17:52:10 +00:00
|
|
|
eSessionStartSent,
|
|
|
|
eLIMSent,
|
2018-05-22 15:54:19 +00:00
|
|
|
eEstablished,
|
|
|
|
eTimeout
|
2018-05-19 17:21:56 +00:00
|
|
|
};
|
2018-05-18 17:10:48 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
State state;
|
|
|
|
|
2018-05-28 20:51:15 +00:00
|
|
|
session(llarp_udp_io *u, llarp_async_iwp *i, llarp_crypto *c,
|
|
|
|
llarp_logic *l, const byte_t *seckey, const llarp::Addr &a)
|
|
|
|
: udp(u), crypto(c), iwp(i), logic(l), addr(a), state(eInitial)
|
2018-05-19 17:21:56 +00:00
|
|
|
{
|
2018-05-23 20:37:43 +00:00
|
|
|
if(seckey)
|
|
|
|
memcpy(eph_seckey, seckey, sizeof(llarp_seckey_t));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c->encryption_keygen(eph_seckey);
|
|
|
|
}
|
2018-05-25 17:52:10 +00:00
|
|
|
|
|
|
|
llarp::Zero(&remote_router, sizeof(llarp_rc));
|
2018-05-19 17:21:56 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
~session()
|
|
|
|
{
|
2018-05-25 17:52:10 +00:00
|
|
|
llarp_rc_free(&remote_router);
|
|
|
|
frame.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
static llarp_rc *
|
|
|
|
get_remote_router(llarp_link_session *s)
|
|
|
|
{
|
|
|
|
session *self = static_cast< session * >(s->impl);
|
|
|
|
return &self->remote_router;
|
2018-05-18 17:10:48 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
static bool
|
|
|
|
sendto(llarp_link_session *s, llarp_buffer_t msg)
|
|
|
|
{
|
2018-05-24 16:04:34 +00:00
|
|
|
session *self = static_cast< session * >(s->impl);
|
|
|
|
transit_message *m = new transit_message(self);
|
|
|
|
auto id = self->frame.txids++;
|
|
|
|
llarp_hash_t digest;
|
|
|
|
self->crypto->hash(digest, msg);
|
|
|
|
m->put_message(msg, digest, id);
|
|
|
|
self->add_outbound_message(id, m);
|
2018-05-22 15:54:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-18 17:10:48 +00:00
|
|
|
|
2018-05-24 16:04:34 +00:00
|
|
|
void
|
2018-05-25 12:24:31 +00:00
|
|
|
add_outbound_message(uint64_t id, transit_message *msg)
|
2018-05-24 16:04:34 +00:00
|
|
|
{
|
|
|
|
frame.queue_tx(id, msg);
|
|
|
|
pump();
|
|
|
|
}
|
|
|
|
|
2018-06-02 13:18:11 +00:00
|
|
|
static void
|
|
|
|
handle_invalidate_timer(void *user);
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
pump()
|
|
|
|
{
|
|
|
|
llarp_buffer_t buf;
|
|
|
|
while(frame.next_frame(buf))
|
|
|
|
{
|
|
|
|
encrypt_frame_async_send(buf.base, buf.sz);
|
2018-05-25 17:52:10 +00:00
|
|
|
frame.pop_next_frame();
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-18 17:10:48 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
// this is called from net thread
|
|
|
|
void
|
|
|
|
recv(const void *buf, size_t sz)
|
|
|
|
{
|
|
|
|
switch(state)
|
|
|
|
{
|
2018-05-23 20:37:43 +00:00
|
|
|
case eInitial:
|
|
|
|
// got intro
|
|
|
|
on_intro(buf, sz);
|
|
|
|
return;
|
2018-05-22 15:54:19 +00:00
|
|
|
case eIntroSent:
|
|
|
|
// got intro ack
|
|
|
|
on_intro_ack(buf, sz);
|
|
|
|
return;
|
2018-05-24 16:04:34 +00:00
|
|
|
case eIntroAckSent:
|
|
|
|
// probably a session start
|
|
|
|
on_session_start(buf, sz);
|
|
|
|
return;
|
2018-05-25 17:52:10 +00:00
|
|
|
|
|
|
|
case eSessionStartSent:
|
|
|
|
case eLIMSent:
|
2018-05-22 15:54:19 +00:00
|
|
|
case eEstablished:
|
|
|
|
// session is started
|
|
|
|
decrypt_frame(buf, sz);
|
|
|
|
default:
|
|
|
|
// invalid state?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-05-19 13:36:42 +00:00
|
|
|
|
2018-05-24 16:04:34 +00:00
|
|
|
static void
|
|
|
|
handle_verify_session_start(iwp_async_session_start *s)
|
|
|
|
{
|
|
|
|
session *self = static_cast< session * >(s->user);
|
|
|
|
if(!s->buf)
|
|
|
|
{
|
|
|
|
// verify fail
|
|
|
|
// TODO: remove session?
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Warn("session start verify failed");
|
2018-05-24 16:04:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-05-25 17:52:10 +00:00
|
|
|
self->send_LIM();
|
|
|
|
}
|
2018-05-25 12:27:54 +00:00
|
|
|
|
2018-05-25 17:52:10 +00:00
|
|
|
void
|
|
|
|
send_LIM()
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Debug("send LIM");
|
2018-05-25 17:52:10 +00:00
|
|
|
llarp_shorthash_t digest;
|
|
|
|
// 64 bytes overhead for link message
|
|
|
|
byte_t tmp[MAX_RC_SIZE + 64];
|
|
|
|
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
|
|
|
// return a llarp_buffer_t of encoded link message
|
|
|
|
if(llarp::EncodeLIM(&buf, our_router))
|
|
|
|
{
|
|
|
|
// rewind message buffer
|
|
|
|
buf.sz = buf.cur - buf.base;
|
|
|
|
buf.cur = buf.base;
|
|
|
|
auto msg = new transit_message;
|
|
|
|
// hash message buffer
|
|
|
|
crypto->shorthash(digest, buf);
|
|
|
|
// put message buffer
|
|
|
|
auto id = frame.txids++;
|
|
|
|
msg->put_message(buf, digest, id);
|
|
|
|
// put into outbound send queue
|
|
|
|
add_outbound_message(id, msg);
|
|
|
|
EnterState(eLIMSent);
|
|
|
|
}
|
|
|
|
else
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Error("LIM Encode failed");
|
2018-05-25 17:52:10 +00:00
|
|
|
}
|
2018-05-24 16:04:34 +00:00
|
|
|
|
2018-05-26 18:31:45 +00:00
|
|
|
static void
|
2018-06-02 13:18:11 +00:00
|
|
|
send_keepalive(void *user);
|
2018-05-26 18:31:45 +00:00
|
|
|
|
2018-06-02 13:18:11 +00:00
|
|
|
// return true if we should be removed
|
|
|
|
bool
|
|
|
|
Tick(uint64_t now)
|
2018-05-26 18:31:45 +00:00
|
|
|
{
|
2018-06-02 13:18:11 +00:00
|
|
|
if(timedout(now, SESSION_TIMEOUT))
|
2018-05-27 12:49:10 +00:00
|
|
|
{
|
2018-06-02 13:18:11 +00:00
|
|
|
// we are timed out
|
|
|
|
// when we are done doing stuff with all of our frames from the crypto
|
|
|
|
// workers we are done
|
2018-06-02 14:40:41 +00:00
|
|
|
llarp::Debug(addr, " timed out with ", frames, " frames left");
|
2018-06-02 13:18:11 +00:00
|
|
|
return frames == 0;
|
2018-05-27 12:49:10 +00:00
|
|
|
}
|
2018-06-02 13:18:11 +00:00
|
|
|
if(is_invalidated())
|
2018-05-27 12:49:10 +00:00
|
|
|
{
|
2018-06-02 13:18:11 +00:00
|
|
|
// both sides agreeed to session invalidation
|
|
|
|
// terminate our session when all of our frames from the crypto workers
|
|
|
|
// are done
|
2018-06-02 14:40:41 +00:00
|
|
|
llarp::Debug(addr, " invaldiated session with ", frames,
|
|
|
|
" frames left");
|
2018-06-02 13:18:11 +00:00
|
|
|
return frames == 0;
|
2018-05-27 12:49:10 +00:00
|
|
|
}
|
2018-06-02 14:40:41 +00:00
|
|
|
// send keepalive if we are established or a session is made
|
|
|
|
if(state == eEstablished || state == eLIMSent)
|
2018-06-02 13:46:35 +00:00
|
|
|
send_keepalive(this);
|
2018-05-26 18:31:45 +00:00
|
|
|
|
2018-06-02 13:18:11 +00:00
|
|
|
// TODO: determine if we are too idle
|
|
|
|
return false;
|
2018-05-26 18:31:45 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 17:52:10 +00:00
|
|
|
void
|
|
|
|
session_established()
|
|
|
|
{
|
|
|
|
EnterState(eEstablished);
|
|
|
|
llarp_logic_cancel_call(logic, establish_job_id);
|
2018-05-24 16:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
on_session_start(const void *buf, size_t sz)
|
|
|
|
{
|
2018-06-03 12:17:06 +00:00
|
|
|
if(sz > sizeof(workbuf))
|
|
|
|
{
|
|
|
|
llarp::Debug("session start too big");
|
|
|
|
return;
|
|
|
|
}
|
2018-05-24 16:04:34 +00:00
|
|
|
// own the buffer
|
|
|
|
memcpy(workbuf, buf, sz);
|
|
|
|
// verify session start
|
|
|
|
start.buf = workbuf;
|
|
|
|
start.sz = sz;
|
|
|
|
start.nonce = workbuf + 32;
|
|
|
|
start.token = token;
|
|
|
|
start.remote_pubkey = remote;
|
|
|
|
start.secretkey = eph_seckey;
|
|
|
|
start.sessionkey = sessionkey;
|
|
|
|
start.user = this;
|
|
|
|
start.hook = &handle_verify_session_start;
|
|
|
|
iwp_call_async_verify_session_start(iwp, &start);
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
|
|
|
timedout(llarp_time_t now, llarp_time_t timeout = SESSION_TIMEOUT)
|
|
|
|
{
|
2018-05-26 18:31:45 +00:00
|
|
|
auto diff = now - frame.lastEvent;
|
|
|
|
return diff >= timeout;
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
static bool
|
|
|
|
is_timedout(llarp_link_session *s)
|
|
|
|
{
|
|
|
|
auto now = llarp_time_now_ms();
|
|
|
|
return static_cast< session * >(s->impl)->timedout(now);
|
|
|
|
}
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-05-26 18:31:45 +00:00
|
|
|
static void
|
|
|
|
handle_session_established(void *user)
|
|
|
|
{
|
|
|
|
session *impl = static_cast< session * >(user);
|
|
|
|
impl->session_established();
|
|
|
|
}
|
|
|
|
|
2018-05-25 17:52:10 +00:00
|
|
|
static void
|
|
|
|
set_established(llarp_link_session *s)
|
|
|
|
{
|
2018-05-26 18:31:45 +00:00
|
|
|
session *impl = static_cast< session * >(s->impl);
|
|
|
|
llarp_logic_queue_job(impl->logic, {impl, &handle_session_established});
|
2018-05-25 17:52:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
static void
|
|
|
|
close(llarp_link_session *s)
|
2018-05-19 17:21:56 +00:00
|
|
|
{
|
2018-06-02 13:18:11 +00:00
|
|
|
session *impl = static_cast< session * >(s->impl);
|
|
|
|
// set our side invalidated and close async when the other side also marks
|
|
|
|
// as session invalidated
|
|
|
|
impl->frame.txflags |= eSessionInvalidated;
|
|
|
|
// TODO: add timer for session invalidation
|
|
|
|
llarp_logic_queue_job(impl->logic, {impl, &send_keepalive});
|
2018-05-19 17:21:56 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
static void
|
|
|
|
handle_verify_introack(iwp_async_introack *introack)
|
2018-05-19 17:21:56 +00:00
|
|
|
{
|
2018-05-22 15:54:19 +00:00
|
|
|
session *link = static_cast< session * >(introack->user);
|
|
|
|
if(introack->buf == nullptr)
|
|
|
|
{
|
|
|
|
// invalid signature
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Error("introack verify failed");
|
2018-05-22 15:54:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
link->EnterState(eIntroAckRecv);
|
|
|
|
// copy decrypted token
|
|
|
|
memcpy(link->token, introack->token, 32);
|
|
|
|
link->session_start();
|
2018-05-19 17:21:56 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
static void
|
|
|
|
handle_generated_session_start(iwp_async_session_start *start)
|
|
|
|
{
|
|
|
|
session *link = static_cast< session * >(start->user);
|
|
|
|
llarp_ev_udp_sendto(link->udp, link->addr, start->buf, start->sz);
|
2018-05-25 17:52:10 +00:00
|
|
|
link->EnterState(eSessionStartSent);
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-05-19 17:21:56 +00:00
|
|
|
|
2018-06-02 13:18:11 +00:00
|
|
|
bool
|
|
|
|
is_invalidated() const
|
|
|
|
{
|
|
|
|
return frame.flags_agree(eSessionInvalidated);
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
session_start()
|
|
|
|
{
|
2018-05-28 20:51:15 +00:00
|
|
|
size_t w2sz = rand() % MAX_PAD;
|
2018-05-22 15:54:19 +00:00
|
|
|
start.buf = workbuf;
|
|
|
|
start.sz = w2sz + (32 * 3);
|
|
|
|
start.nonce = workbuf + 32;
|
|
|
|
crypto->randbytes(start.nonce, 32);
|
|
|
|
start.token = token;
|
|
|
|
memcpy(start.buf + 64, token, 32);
|
|
|
|
if(w2sz)
|
|
|
|
crypto->randbytes(start.buf + (32 * 3), w2sz);
|
2018-05-23 20:37:43 +00:00
|
|
|
start.remote_pubkey = remote;
|
|
|
|
start.secretkey = eph_seckey;
|
|
|
|
start.sessionkey = sessionkey;
|
|
|
|
start.user = this;
|
|
|
|
start.hook = &handle_generated_session_start;
|
2018-05-22 15:54:19 +00:00
|
|
|
iwp_call_async_gen_session_start(iwp, &start);
|
|
|
|
}
|
2018-05-18 17:10:48 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
static void
|
|
|
|
handle_frame_decrypt(iwp_async_frame *frame)
|
|
|
|
{
|
|
|
|
session *self = static_cast< session * >(frame->user);
|
2018-06-02 13:34:06 +00:00
|
|
|
llarp::Debug("rx ", frame->sz, " frames=", self->frames);
|
2018-05-22 15:54:19 +00:00
|
|
|
if(frame->success)
|
|
|
|
{
|
2018-05-25 17:52:10 +00:00
|
|
|
if(self->frame.process(frame->buf + 64, frame->sz - 64))
|
|
|
|
{
|
2018-05-26 18:31:45 +00:00
|
|
|
self->frame.alive();
|
2018-05-25 17:52:10 +00:00
|
|
|
self->pump();
|
|
|
|
}
|
|
|
|
else
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Error("invalid frame");
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
|
|
|
else
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Error("decrypt frame fail");
|
2018-05-18 20:08:57 +00:00
|
|
|
|
2018-05-24 16:04:34 +00:00
|
|
|
delete frame;
|
2018-05-29 18:56:00 +00:00
|
|
|
--self->frames;
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-05-18 14:40:06 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
decrypt_frame(const void *buf, size_t sz)
|
|
|
|
{
|
|
|
|
if(sz > 64)
|
|
|
|
{
|
|
|
|
auto frame = alloc_frame(buf, sz);
|
|
|
|
frame->hook = &handle_frame_decrypt;
|
|
|
|
iwp_call_async_frame_decrypt(iwp, frame);
|
|
|
|
}
|
|
|
|
else
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Warn("short packet of ", sz, " bytes");
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-05-21 14:28:15 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
static void
|
|
|
|
handle_frame_encrypt(iwp_async_frame *frame)
|
|
|
|
{
|
|
|
|
session *self = static_cast< session * >(frame->user);
|
2018-06-02 13:34:06 +00:00
|
|
|
llarp::Debug("tx ", frame->sz, " frames=", self->frames);
|
2018-05-22 15:54:19 +00:00
|
|
|
llarp_ev_udp_sendto(self->udp, self->addr, frame->buf, frame->sz);
|
2018-05-24 16:04:34 +00:00
|
|
|
delete frame;
|
2018-05-29 18:56:00 +00:00
|
|
|
--self->frames;
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-05-18 20:08:57 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
iwp_async_frame *
|
|
|
|
alloc_frame(const void *buf, size_t sz)
|
|
|
|
{
|
2018-05-25 12:24:31 +00:00
|
|
|
// TODO don't hard code 1500
|
|
|
|
if(sz > 1500)
|
|
|
|
return nullptr;
|
2018-05-25 12:27:54 +00:00
|
|
|
|
2018-05-24 16:04:34 +00:00
|
|
|
iwp_async_frame *frame = new iwp_async_frame;
|
2018-05-25 12:24:31 +00:00
|
|
|
if(buf)
|
|
|
|
memcpy(frame->buf, buf, sz);
|
2018-05-22 15:54:19 +00:00
|
|
|
frame->sz = sz;
|
|
|
|
frame->user = this;
|
|
|
|
frame->sessionkey = sessionkey;
|
2018-05-29 18:56:00 +00:00
|
|
|
++frames;
|
2018-05-22 15:54:19 +00:00
|
|
|
return frame;
|
|
|
|
}
|
2018-05-21 14:28:15 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
encrypt_frame_async_send(const void *buf, size_t sz)
|
2018-05-21 14:28:15 +00:00
|
|
|
{
|
2018-05-25 12:24:31 +00:00
|
|
|
// 64 bytes frame overhead for nonce and hmac
|
2018-05-25 12:27:54 +00:00
|
|
|
auto frame = alloc_frame(nullptr, sz + 64);
|
2018-05-25 17:52:10 +00:00
|
|
|
memcpy(frame->buf + 64, buf, sz);
|
2018-05-22 15:54:19 +00:00
|
|
|
frame->hook = &handle_frame_encrypt;
|
|
|
|
iwp_call_async_frame_encrypt(iwp, frame);
|
2018-05-21 14:28:15 +00:00
|
|
|
}
|
2018-05-22 15:54:19 +00:00
|
|
|
|
2018-05-23 20:37:43 +00:00
|
|
|
static void
|
|
|
|
handle_verify_intro(iwp_async_intro *intro)
|
|
|
|
{
|
|
|
|
session *self = static_cast< session * >(intro->user);
|
|
|
|
if(!intro->buf)
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Error("intro verify failed");
|
2018-05-29 18:08:39 +00:00
|
|
|
delete self;
|
2018-05-23 20:37:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
self->intro_ack();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-05-29 18:08:39 +00:00
|
|
|
handle_introack_generated(iwp_async_introack *i);
|
2018-05-23 20:37:43 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
intro_ack()
|
|
|
|
{
|
2018-05-28 20:51:15 +00:00
|
|
|
uint16_t w1sz = rand() % MAX_PAD;
|
2018-05-23 20:37:43 +00:00
|
|
|
introack.buf = workbuf;
|
|
|
|
introack.sz = (32 * 3) + w1sz;
|
|
|
|
// randomize padding
|
|
|
|
if(w1sz)
|
|
|
|
crypto->randbytes(introack.buf + (32 * 3), w1sz);
|
|
|
|
|
|
|
|
// randomize nonce
|
|
|
|
introack.nonce = introack.buf + 32;
|
|
|
|
crypto->randbytes(introack.nonce, 32);
|
|
|
|
// randomize token
|
|
|
|
introack.token = token;
|
|
|
|
crypto->randbytes(introack.token, 32);
|
|
|
|
|
|
|
|
// keys
|
|
|
|
introack.remote_pubkey = remote;
|
|
|
|
introack.secretkey = eph_seckey;
|
|
|
|
|
|
|
|
// call
|
|
|
|
introack.user = this;
|
|
|
|
introack.hook = &handle_introack_generated;
|
|
|
|
iwp_call_async_gen_introack(iwp, &introack);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
on_intro(const void *buf, size_t sz)
|
|
|
|
{
|
|
|
|
if(sz >= sizeof(workbuf))
|
|
|
|
{
|
|
|
|
// too big?
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Error("intro too big");
|
2018-05-23 20:37:43 +00:00
|
|
|
// TOOD: session destroy ?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// copy so we own it
|
|
|
|
memcpy(workbuf, buf, sz);
|
|
|
|
intro.buf = workbuf;
|
|
|
|
intro.sz = sz;
|
|
|
|
// give secret key
|
|
|
|
intro.secretkey = eph_seckey;
|
|
|
|
// and nonce
|
|
|
|
intro.nonce = intro.buf + 32;
|
|
|
|
intro.user = this;
|
|
|
|
// set call back hook
|
|
|
|
intro.hook = &handle_verify_intro;
|
|
|
|
// put remote pubkey into this buffer
|
|
|
|
intro.remote_pubkey = remote;
|
|
|
|
|
|
|
|
// call
|
|
|
|
EnterState(eIntroRecv);
|
|
|
|
iwp_call_async_verify_intro(iwp, &intro);
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
on_intro_ack(const void *buf, size_t sz)
|
2018-05-21 14:28:15 +00:00
|
|
|
{
|
2018-05-22 15:54:19 +00:00
|
|
|
if(sz >= sizeof(workbuf))
|
2018-05-21 14:28:15 +00:00
|
|
|
{
|
2018-05-22 15:54:19 +00:00
|
|
|
// too big?
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Error("introack too big");
|
2018-05-22 15:54:19 +00:00
|
|
|
// TOOD: session destroy ?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// copy buffer so we own it
|
|
|
|
memcpy(workbuf, buf, sz);
|
|
|
|
// set intro ack parameters
|
|
|
|
introack.buf = workbuf;
|
|
|
|
introack.sz = sz;
|
|
|
|
introack.nonce = workbuf + 32;
|
|
|
|
introack.remote_pubkey = remote;
|
2018-05-23 20:37:43 +00:00
|
|
|
introack.token = token;
|
2018-05-22 15:54:19 +00:00
|
|
|
introack.secretkey = eph_seckey;
|
|
|
|
introack.user = this;
|
|
|
|
introack.hook = &handle_verify_introack;
|
|
|
|
// async verify
|
|
|
|
iwp_call_async_verify_introack(iwp, &introack);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_generated_intro(iwp_async_intro *i)
|
|
|
|
{
|
|
|
|
session *link = static_cast< session * >(i->user);
|
|
|
|
if(i->buf)
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Debug("send intro");
|
2018-05-29 13:40:26 +00:00
|
|
|
if(llarp_ev_udp_sendto(link->udp, link->addr, i->buf, i->sz) == -1)
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Warn("send intro failed");
|
2018-05-29 13:40:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-05-22 15:54:19 +00:00
|
|
|
link->EnterState(eIntroSent);
|
2018-05-21 14:28:15 +00:00
|
|
|
}
|
2018-05-29 12:15:48 +00:00
|
|
|
else
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Warn("failed to generate intro");
|
2018-05-29 12:15:48 +00:00
|
|
|
}
|
2018-05-21 14:28:15 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 17:52:10 +00:00
|
|
|
static void
|
2018-05-29 18:32:47 +00:00
|
|
|
handle_establish_timeout(void *user, uint64_t orig, uint64_t left);
|
2018-05-25 17:52:10 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
introduce(uint8_t *pub)
|
|
|
|
{
|
|
|
|
memcpy(remote, pub, 32);
|
|
|
|
intro.buf = workbuf;
|
2018-05-28 20:51:15 +00:00
|
|
|
size_t w0sz = (rand() % MAX_PAD);
|
2018-05-22 15:54:19 +00:00
|
|
|
intro.sz = (32 * 3) + w0sz;
|
|
|
|
// randomize w0
|
|
|
|
if(w0sz)
|
|
|
|
{
|
|
|
|
crypto->randbytes(intro.buf + (32 * 3), w0sz);
|
|
|
|
}
|
|
|
|
|
2018-05-23 20:37:43 +00:00
|
|
|
intro.nonce = intro.buf + 32;
|
|
|
|
intro.secretkey = eph_seckey;
|
|
|
|
// copy in pubkey
|
2018-05-22 15:54:19 +00:00
|
|
|
intro.remote_pubkey = remote;
|
|
|
|
// randomize nonce
|
|
|
|
crypto->randbytes(intro.nonce, 32);
|
|
|
|
// async generate intro packet
|
|
|
|
intro.user = this;
|
|
|
|
intro.hook = &handle_generated_intro;
|
|
|
|
iwp_call_async_gen_intro(iwp, &intro);
|
2018-05-25 17:52:10 +00:00
|
|
|
// start introduce timer
|
|
|
|
establish_job_id = llarp_logic_call_later(
|
|
|
|
logic, {5000, this, &handle_establish_timeout});
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
|
|
|
|
2018-06-02 13:18:11 +00:00
|
|
|
// handle session being over
|
|
|
|
// called right before deallocation
|
|
|
|
void
|
|
|
|
done();
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
EnterState(State st)
|
|
|
|
{
|
2018-05-25 17:52:10 +00:00
|
|
|
frame.alive();
|
2018-05-22 15:54:19 +00:00
|
|
|
state = st;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct server
|
2018-05-18 14:40:06 +00:00
|
|
|
{
|
2018-05-22 15:54:19 +00:00
|
|
|
typedef std::mutex mtx_t;
|
|
|
|
typedef std::lock_guard< mtx_t > lock_t;
|
|
|
|
|
2018-05-25 17:52:10 +00:00
|
|
|
llarp_router *router;
|
2018-05-22 15:54:19 +00:00
|
|
|
llarp_logic *logic;
|
|
|
|
llarp_crypto *crypto;
|
|
|
|
llarp_ev_loop *netloop;
|
|
|
|
llarp_async_iwp *iwp;
|
2018-05-29 18:32:47 +00:00
|
|
|
llarp_link *parent = nullptr;
|
2018-05-22 15:54:19 +00:00
|
|
|
llarp_udp_io udp;
|
2018-05-23 13:49:00 +00:00
|
|
|
llarp::Addr addr;
|
2018-05-22 15:54:19 +00:00
|
|
|
char keyfile[255];
|
|
|
|
uint32_t timeout_job_id;
|
|
|
|
|
2018-05-29 16:11:32 +00:00
|
|
|
typedef std::unordered_map< llarp::Addr, llarp_link_session,
|
|
|
|
llarp::addrhash >
|
|
|
|
LinkMap_t;
|
2018-05-22 15:54:19 +00:00
|
|
|
|
|
|
|
LinkMap_t m_sessions;
|
|
|
|
mtx_t m_sessions_Mutex;
|
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
typedef std::unordered_map< llarp::pubkey, llarp::Addr, llarp::pubkeyhash >
|
|
|
|
SessionMap_t;
|
|
|
|
|
|
|
|
SessionMap_t m_Connected;
|
|
|
|
mtx_t m_Connected_Mutex;
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
llarp_seckey_t seckey;
|
|
|
|
|
2018-05-25 17:52:10 +00:00
|
|
|
server(llarp_router *r, llarp_crypto *c, llarp_logic *l,
|
|
|
|
llarp_threadpool *w)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-05-25 17:52:10 +00:00
|
|
|
router = r;
|
2018-05-22 15:54:19 +00:00
|
|
|
crypto = c;
|
|
|
|
logic = l;
|
2018-05-25 17:52:10 +00:00
|
|
|
iwp = llarp_async_iwp_new(crypto, logic, w);
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 19:19:06 +00:00
|
|
|
~server()
|
|
|
|
{
|
|
|
|
llarp_async_iwp_free(iwp);
|
|
|
|
}
|
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
// set that src address has identity pubkey
|
|
|
|
void
|
|
|
|
MapAddr(const llarp::Addr &src, llarp::pubkey identity)
|
|
|
|
{
|
|
|
|
lock_t lock(m_Connected_Mutex);
|
|
|
|
m_Connected[identity] = src;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
HasSessionToRouter(llarp_link *l, const byte_t *pubkey)
|
|
|
|
{
|
|
|
|
server *serv = static_cast< server * >(l->impl);
|
|
|
|
lock_t lock(serv->m_Connected_Mutex);
|
|
|
|
return serv->m_Connected.find(pubkey) != serv->m_Connected.end();
|
|
|
|
}
|
|
|
|
|
2018-06-02 13:22:08 +00:00
|
|
|
void
|
|
|
|
TickSessions()
|
2018-06-02 13:18:11 +00:00
|
|
|
{
|
2018-06-02 13:22:08 +00:00
|
|
|
auto now = llarp_time_now_ms();
|
2018-06-02 13:18:11 +00:00
|
|
|
{
|
2018-06-02 13:22:08 +00:00
|
|
|
lock_t lock(m_sessions_Mutex);
|
2018-06-02 13:18:11 +00:00
|
|
|
std::set< llarp::Addr > remove;
|
2018-06-02 13:22:08 +00:00
|
|
|
auto itr = m_sessions.begin();
|
|
|
|
while(itr != m_sessions.end())
|
2018-06-02 13:36:59 +00:00
|
|
|
{
|
2018-06-02 13:18:11 +00:00
|
|
|
if(static_cast< session * >(itr->second.impl)->Tick(now))
|
|
|
|
remove.insert(itr->first);
|
2018-06-02 13:36:59 +00:00
|
|
|
++itr;
|
|
|
|
}
|
2018-06-02 13:18:11 +00:00
|
|
|
|
|
|
|
for(const auto &addr : remove)
|
2018-06-02 13:22:08 +00:00
|
|
|
RemoveSessionByAddr(addr);
|
2018-06-02 13:18:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
static bool
|
|
|
|
SendToSession(llarp_link *l, const byte_t *pubkey, llarp_buffer_t buf)
|
|
|
|
{
|
|
|
|
server *serv = static_cast< server * >(l->impl);
|
|
|
|
{
|
|
|
|
lock_t lock(serv->m_Connected_Mutex);
|
|
|
|
auto itr = serv->m_Connected.find(pubkey);
|
|
|
|
if(itr != serv->m_Connected.end())
|
|
|
|
{
|
|
|
|
lock_t innerlock(serv->m_sessions_Mutex);
|
|
|
|
auto inner_itr = serv->m_sessions.find(itr->second);
|
|
|
|
if(inner_itr != serv->m_sessions.end())
|
|
|
|
{
|
|
|
|
auto link = &inner_itr->second;
|
|
|
|
return link->sendto(link, buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnmapAddr(const llarp::Addr &src)
|
|
|
|
{
|
|
|
|
lock_t lock(m_Connected_Mutex);
|
|
|
|
auto itr = std::find_if(
|
|
|
|
m_Connected.begin(), m_Connected.end(),
|
|
|
|
[src](const auto &item) -> bool { return src == item.second; });
|
|
|
|
if(itr == std::end(m_Connected))
|
|
|
|
return;
|
2018-06-01 21:35:17 +00:00
|
|
|
|
2018-06-02 13:18:11 +00:00
|
|
|
// tell router we are done with this session
|
|
|
|
router->SessionClosed(itr->first);
|
2018-06-01 21:35:17 +00:00
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
m_Connected.erase(itr);
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
session *
|
2018-05-23 20:37:43 +00:00
|
|
|
create_session(const llarp::Addr &src, const byte_t *seckey)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-05-29 18:32:47 +00:00
|
|
|
auto s = new session(&udp, iwp, crypto, logic, seckey, src);
|
|
|
|
s->serv = this;
|
|
|
|
return s;
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
has_session_to(const llarp::Addr &dst)
|
2018-05-21 14:28:15 +00:00
|
|
|
{
|
|
|
|
lock_t lock(m_sessions_Mutex);
|
2018-05-22 15:54:19 +00:00
|
|
|
return m_sessions.find(dst) != m_sessions.end();
|
|
|
|
}
|
|
|
|
|
2018-05-29 13:45:47 +00:00
|
|
|
session *
|
|
|
|
find_session(const llarp::Addr &addr)
|
|
|
|
{
|
|
|
|
lock_t lock(m_sessions_Mutex);
|
|
|
|
auto itr = m_sessions.find(addr);
|
|
|
|
if(itr == m_sessions.end())
|
|
|
|
return nullptr;
|
|
|
|
else
|
|
|
|
return static_cast< session * >(itr->second.impl);
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
put_session(const llarp::Addr &src, session *impl)
|
|
|
|
{
|
|
|
|
llarp_link_session s = {};
|
2018-05-23 13:49:00 +00:00
|
|
|
s.impl = impl;
|
|
|
|
s.sendto = &session::sendto;
|
|
|
|
s.timeout = &session::is_timedout;
|
|
|
|
s.close = &session::close;
|
2018-05-25 17:52:10 +00:00
|
|
|
s.get_remote_router = &session::get_remote_router;
|
|
|
|
s.established = &session::set_established;
|
2018-05-21 14:28:15 +00:00
|
|
|
{
|
2018-05-22 15:54:19 +00:00
|
|
|
lock_t lock(m_sessions_Mutex);
|
|
|
|
m_sessions[src] = s;
|
2018-05-25 17:52:10 +00:00
|
|
|
impl->parent = &m_sessions[src];
|
2018-05-21 14:28:15 +00:00
|
|
|
}
|
2018-05-25 17:52:10 +00:00
|
|
|
impl->frame.router = router;
|
|
|
|
impl->frame.parent = impl->parent;
|
|
|
|
impl->our_router = &router->rc;
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 13:49:00 +00:00
|
|
|
void
|
|
|
|
clear_sessions()
|
|
|
|
{
|
|
|
|
lock_t lock(m_sessions_Mutex);
|
|
|
|
auto itr = m_sessions.begin();
|
|
|
|
while(itr != m_sessions.end())
|
|
|
|
{
|
|
|
|
session *s = static_cast< session * >(itr->second.impl);
|
|
|
|
delete s;
|
|
|
|
itr = m_sessions.erase(itr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-02 13:18:11 +00:00
|
|
|
void
|
|
|
|
RemoveSessionByAddr(const llarp::Addr &addr)
|
|
|
|
{
|
|
|
|
auto itr = m_sessions.find(addr);
|
|
|
|
if(itr != m_sessions.end())
|
|
|
|
{
|
|
|
|
llarp::Debug("removing session ", addr);
|
|
|
|
UnmapAddr(addr);
|
|
|
|
session *s = static_cast< session * >(itr->second.impl);
|
|
|
|
m_sessions.erase(itr);
|
|
|
|
s->done();
|
|
|
|
if(s->frames)
|
|
|
|
{
|
|
|
|
llarp::Warn("session has ", s->frames,
|
|
|
|
" left but is idle, not deallocating session so we "
|
|
|
|
"leak but don't die");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
uint8_t *
|
|
|
|
pubkey()
|
2018-05-18 14:48:30 +00:00
|
|
|
{
|
2018-05-22 15:54:19 +00:00
|
|
|
return llarp_seckey_topublic(seckey);
|
2018-05-18 14:48:30 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
|
|
|
ensure_privkey()
|
2018-05-18 14:48:30 +00:00
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Debug("ensure transport private key at ", keyfile);
|
2018-05-22 15:54:19 +00:00
|
|
|
std::error_code ec;
|
|
|
|
if(!fs::exists(keyfile, ec))
|
|
|
|
{
|
|
|
|
if(!keygen(keyfile))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::ifstream f(keyfile);
|
|
|
|
if(f.is_open())
|
|
|
|
{
|
2018-05-23 20:37:43 +00:00
|
|
|
f.read((char *)seckey, sizeof(llarp_seckey_t));
|
2018-05-22 15:54:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2018-05-18 14:48:30 +00:00
|
|
|
}
|
2018-05-22 15:54:19 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
keygen(const char *fname)
|
2018-05-18 14:40:06 +00:00
|
|
|
{
|
2018-05-23 20:37:43 +00:00
|
|
|
crypto->encryption_keygen(seckey);
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Info("new transport key generated");
|
2018-05-22 15:54:19 +00:00
|
|
|
std::ofstream f(fname);
|
|
|
|
if(f.is_open())
|
|
|
|
{
|
2018-05-23 20:37:43 +00:00
|
|
|
f.write((char *)seckey, sizeof(llarp_seckey_t));
|
2018-05-22 15:54:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2018-05-18 14:40:06 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
static void
|
|
|
|
handle_cleanup_timer(void *l, uint64_t orig, uint64_t left)
|
|
|
|
{
|
2018-06-02 13:22:08 +00:00
|
|
|
if(left)
|
|
|
|
return;
|
2018-05-22 15:54:19 +00:00
|
|
|
server *link = static_cast< server * >(l);
|
|
|
|
link->timeout_job_id = 0;
|
2018-06-02 13:22:08 +00:00
|
|
|
link->TickSessions();
|
|
|
|
// TODO: exponential backoff for cleanup timer ?
|
|
|
|
link->issue_cleanup_timer(orig);
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-05-18 14:40:06 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
// this is called in net threadpool
|
|
|
|
static void
|
|
|
|
handle_recvfrom(struct llarp_udp_io *udp, const struct sockaddr *saddr,
|
|
|
|
const void *buf, ssize_t sz)
|
2018-05-18 14:40:06 +00:00
|
|
|
{
|
2018-05-23 20:37:43 +00:00
|
|
|
server *link = static_cast< server * >(udp->user);
|
2018-05-29 18:08:39 +00:00
|
|
|
|
|
|
|
session *s = link->find_session(*saddr);
|
|
|
|
if(s == nullptr)
|
|
|
|
{
|
|
|
|
// new inbound session
|
2018-05-29 18:32:47 +00:00
|
|
|
s = link->create_session(*saddr, link->seckey);
|
2018-05-29 18:08:39 +00:00
|
|
|
}
|
2018-05-22 15:54:19 +00:00
|
|
|
s->recv(buf, sz);
|
2018-05-18 14:40:06 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
cancel_timer()
|
|
|
|
{
|
|
|
|
if(timeout_job_id)
|
|
|
|
{
|
|
|
|
llarp_logic_cancel_call(logic, timeout_job_id);
|
|
|
|
}
|
|
|
|
timeout_job_id = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
issue_cleanup_timer(uint64_t timeout)
|
|
|
|
{
|
|
|
|
timeout_job_id = llarp_logic_call_later(
|
|
|
|
logic, {timeout, this, &server::handle_cleanup_timer});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-26 18:31:45 +00:00
|
|
|
bool
|
|
|
|
frame_state::inbound_frame_complete(uint64_t id)
|
|
|
|
{
|
|
|
|
bool success = false;
|
|
|
|
std::vector< byte_t > msg;
|
|
|
|
if(rx[id].reassemble(msg))
|
|
|
|
{
|
|
|
|
auto buf = llarp::Buffer< decltype(msg) >(msg);
|
|
|
|
success = router->HandleRecvLinkMessage(parent, buf);
|
|
|
|
if(success)
|
|
|
|
{
|
|
|
|
session *impl = static_cast< session * >(parent->impl);
|
2018-06-01 17:47:37 +00:00
|
|
|
if(id == 0)
|
2018-05-26 18:31:45 +00:00
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
// send our LIM if we are an outbound session
|
|
|
|
if(impl->state == session::eSessionStartSent)
|
|
|
|
{
|
|
|
|
impl->send_LIM();
|
|
|
|
}
|
|
|
|
impl->serv->MapAddr(impl->addr, impl->remote_router.pubkey);
|
2018-05-26 18:31:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Warn("failed to reassemble message ", id);
|
2018-05-26 18:31:45 +00:00
|
|
|
}
|
|
|
|
rx.erase(id);
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2018-06-02 13:18:11 +00:00
|
|
|
void
|
|
|
|
session::done()
|
|
|
|
{
|
|
|
|
if(establish_job_id)
|
|
|
|
{
|
|
|
|
llarp_logic_remove_call(logic, establish_job_id);
|
|
|
|
}
|
|
|
|
if(establish_job)
|
|
|
|
{
|
|
|
|
auto job = establish_job;
|
|
|
|
job->link = serv->parent;
|
|
|
|
job->session = nullptr;
|
|
|
|
job->result(job);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
session::send_keepalive(void *user)
|
|
|
|
{
|
|
|
|
session *self = static_cast< session * >(user);
|
|
|
|
// if both sides agree on invalidation
|
|
|
|
if(self->is_invalidated())
|
|
|
|
{
|
|
|
|
// don't send keepalive
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// all zeros means keepalive
|
|
|
|
byte_t tmp[MAX_PAD + 8] = {0};
|
|
|
|
// set flags for tx
|
|
|
|
frame_header hdr(tmp);
|
|
|
|
hdr.flags() = self->frame.txflags;
|
|
|
|
// 8 bytes iwp header overhead
|
|
|
|
int padsz = rand() % (sizeof(tmp) - 8);
|
|
|
|
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
|
|
|
if(padsz)
|
|
|
|
self->crypto->randbytes(buf.base + 8, padsz);
|
|
|
|
buf.sz -= padsz;
|
|
|
|
// send frame after encrypting
|
|
|
|
self->encrypt_frame_async_send(buf.base, buf.sz);
|
|
|
|
}
|
|
|
|
|
2018-05-26 18:31:45 +00:00
|
|
|
bool
|
|
|
|
frame_state::got_acks(frame_header &hdr, size_t sz)
|
|
|
|
{
|
|
|
|
if(hdr.size() > sz)
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Error("invalid ACKS frame size ", hdr.size(), " > ", sz);
|
2018-05-26 18:31:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
sz = hdr.size();
|
|
|
|
if(sz < 12)
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Error("invalid ACKS frame size ", sz, " < 12");
|
2018-05-26 18:31:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = hdr.data();
|
|
|
|
uint64_t msgid;
|
|
|
|
uint32_t bitmask;
|
|
|
|
memcpy(&msgid, ptr, 8);
|
|
|
|
memcpy(&bitmask, ptr + 8, 4);
|
|
|
|
|
|
|
|
auto itr = tx.find(msgid);
|
|
|
|
if(itr == tx.end())
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Error("ACK for missing TX frame msgid=", msgid);
|
2018-05-26 18:31:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
itr->second->ack(bitmask);
|
|
|
|
|
|
|
|
if(itr->second->completed())
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Debug("message transmitted msgid=", msgid);
|
2018-05-26 18:31:45 +00:00
|
|
|
delete itr->second;
|
|
|
|
tx.erase(itr);
|
|
|
|
session *impl = static_cast< session * >(parent->impl);
|
|
|
|
if(impl->state == session::eLIMSent && msgid == 0)
|
|
|
|
{
|
|
|
|
// first message acked we are established?
|
|
|
|
impl->session_established();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Debug("message ", msgid, " retransmit fragments");
|
2018-05-26 18:31:45 +00:00
|
|
|
itr->second->retransmit_frags(sendqueue);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
server *
|
2018-05-25 17:52:10 +00:00
|
|
|
link_alloc(struct llarp_router *router, const char *keyfile,
|
|
|
|
struct llarp_crypto *crypto, struct llarp_logic *logic,
|
|
|
|
struct llarp_threadpool *worker)
|
2018-05-18 14:40:06 +00:00
|
|
|
{
|
2018-05-25 17:52:10 +00:00
|
|
|
server *link = new server(router, crypto, logic, worker);
|
2018-05-23 20:37:43 +00:00
|
|
|
llarp::Zero(link->keyfile, sizeof(link->keyfile));
|
2018-05-18 14:40:06 +00:00
|
|
|
strncpy(link->keyfile, keyfile, sizeof(link->keyfile));
|
|
|
|
return link;
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
const char *
|
|
|
|
link_name()
|
|
|
|
{
|
|
|
|
return "IWP";
|
|
|
|
}
|
2018-05-18 14:40:06 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
link_get_addr(struct llarp_link *l, struct llarp_ai *addr)
|
|
|
|
{
|
|
|
|
server *link = static_cast< server * >(l->impl);
|
2018-05-23 13:49:00 +00:00
|
|
|
addr->rank = 1;
|
2018-05-22 15:54:19 +00:00
|
|
|
strncpy(addr->dialect, link_name(), sizeof(addr->dialect));
|
|
|
|
memcpy(addr->enc_key, link->pubkey(), 32);
|
2018-05-23 13:49:00 +00:00
|
|
|
memcpy(addr->ip.s6_addr, link->addr.addr6(), 16);
|
|
|
|
addr->port = link->addr.port();
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-05-18 14:40:06 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
|
|
|
link_configure(struct llarp_link *l, struct llarp_ev_loop *netloop,
|
|
|
|
const char *ifname, int af, uint16_t port)
|
|
|
|
{
|
|
|
|
server *link = static_cast< server * >(l->impl);
|
2018-05-18 14:40:06 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
if(!link->ensure_privkey())
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Error("failed to ensure private key");
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-05-20 17:45:47 +00:00
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Debug("configure link ifname=", ifname, " af=", af, " port=", port);
|
2018-05-22 15:54:19 +00:00
|
|
|
// bind
|
2018-05-23 13:49:00 +00:00
|
|
|
sockaddr_in ip4addr;
|
|
|
|
sockaddr_in6 ip6addr;
|
|
|
|
sockaddr *addr = nullptr;
|
|
|
|
switch(af)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
2018-05-29 12:15:48 +00:00
|
|
|
addr = (sockaddr *)&ip4addr;
|
|
|
|
llarp::Zero(addr, sizeof(ip4addr));
|
2018-05-23 13:49:00 +00:00
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2018-05-29 12:15:48 +00:00
|
|
|
addr = (sockaddr *)&ip6addr;
|
|
|
|
llarp::Zero(addr, sizeof(ip6addr));
|
2018-05-23 13:49:00 +00:00
|
|
|
break;
|
|
|
|
// TODO: AF_PACKET
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-23 20:37:43 +00:00
|
|
|
|
2018-05-29 12:15:48 +00:00
|
|
|
addr->sa_family = af;
|
|
|
|
|
|
|
|
if(!llarp::StrEq(ifname, "*"))
|
2018-05-23 13:49:00 +00:00
|
|
|
{
|
2018-05-29 12:15:48 +00:00
|
|
|
if(!llarp_getifaddr(ifname, af, addr))
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Error("failed to get address of network interface ", ifname);
|
2018-05-29 12:15:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-05-23 13:49:00 +00:00
|
|
|
}
|
2018-05-23 20:37:43 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
switch(af)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
2018-05-23 13:49:00 +00:00
|
|
|
ip4addr.sin_port = htons(port);
|
2018-05-22 15:54:19 +00:00
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2018-05-23 13:49:00 +00:00
|
|
|
ip6addr.sin6_port = htons(port);
|
2018-05-22 15:54:19 +00:00
|
|
|
break;
|
|
|
|
// TODO: AF_PACKET
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-23 13:49:00 +00:00
|
|
|
|
2018-05-27 14:04:30 +00:00
|
|
|
link->addr = *addr;
|
2018-05-22 15:54:19 +00:00
|
|
|
link->netloop = netloop;
|
|
|
|
link->udp.recvfrom = &server::handle_recvfrom;
|
|
|
|
link->udp.user = link;
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Debug("bind IWP link to ", link->addr);
|
2018-05-29 12:15:48 +00:00
|
|
|
if(llarp_ev_add_udp(link->netloop, &link->udp, link->addr) == -1)
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Error("failed to bind to ", link->addr);
|
2018-05-29 12:15:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2018-05-18 14:40:06 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
|
|
|
link_start(struct llarp_link *l, struct llarp_logic *logic)
|
|
|
|
{
|
2018-05-25 17:52:10 +00:00
|
|
|
server *link = static_cast< server * >(l->impl);
|
|
|
|
// give link implementations
|
2018-05-29 18:32:47 +00:00
|
|
|
link->parent = l;
|
2018-05-22 15:54:19 +00:00
|
|
|
link->timeout_job_id = 0;
|
|
|
|
link->logic = logic;
|
|
|
|
// start cleanup timer
|
|
|
|
link->issue_cleanup_timer(1000);
|
|
|
|
return true;
|
|
|
|
}
|
2018-05-18 14:40:06 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
|
|
|
link_stop(struct llarp_link *l)
|
|
|
|
{
|
|
|
|
server *link = static_cast< server * >(l->impl);
|
|
|
|
link->cancel_timer();
|
2018-05-28 20:51:15 +00:00
|
|
|
llarp_ev_close_udp(&link->udp);
|
|
|
|
link->clear_sessions();
|
2018-05-22 15:54:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-18 14:40:06 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
link_iter_sessions(struct llarp_link *l, struct llarp_link_session_iter iter)
|
|
|
|
{
|
|
|
|
server *link = static_cast< server * >(l->impl);
|
|
|
|
iter.link = l;
|
|
|
|
// TODO: race condition with cleanup timer
|
|
|
|
for(auto &item : link->m_sessions)
|
|
|
|
if(!iter.visit(&iter, &item.second))
|
|
|
|
return;
|
|
|
|
}
|
2018-05-18 14:40:06 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
|
|
|
link_try_establish(struct llarp_link *l, struct llarp_link_establish_job *job)
|
|
|
|
{
|
|
|
|
server *link = static_cast< server * >(l->impl);
|
|
|
|
{
|
2018-05-23 20:37:43 +00:00
|
|
|
llarp::Addr dst(job->ai);
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Debug("establish session to ", dst);
|
2018-05-29 13:45:47 +00:00
|
|
|
session *s = link->find_session(dst);
|
|
|
|
if(s == nullptr)
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-05-29 13:45:47 +00:00
|
|
|
s = link->create_session(dst, nullptr);
|
|
|
|
link->put_session(dst, s);
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
|
|
|
s->establish_job = job;
|
2018-05-29 18:36:40 +00:00
|
|
|
s->frame.alive();
|
2018-05-22 15:54:19 +00:00
|
|
|
s->introduce(job->ai.enc_key);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2018-05-18 14:40:06 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
link_mark_session_active(struct llarp_link *link,
|
|
|
|
struct llarp_link_session *s)
|
|
|
|
{
|
2018-05-23 13:49:00 +00:00
|
|
|
static_cast< session * >(s->impl)->frame.alive();
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-05-18 14:40:06 +00:00
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
|
|
|
link_free(struct llarp_link *l)
|
|
|
|
{
|
|
|
|
server *link = static_cast< server * >(l->impl);
|
|
|
|
delete link;
|
|
|
|
}
|
2018-05-29 18:08:39 +00:00
|
|
|
|
2018-05-29 18:32:47 +00:00
|
|
|
void
|
|
|
|
session::handle_establish_timeout(void *user, uint64_t orig, uint64_t left)
|
|
|
|
{
|
|
|
|
session *self = static_cast< session * >(user);
|
|
|
|
self->establish_job_id = 0;
|
|
|
|
if(self->establish_job)
|
|
|
|
{
|
|
|
|
self->establish_job->link = self->serv->parent;
|
|
|
|
if(left)
|
|
|
|
{
|
|
|
|
// timer cancelled
|
|
|
|
self->establish_job->session = self->parent;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// timer timeout
|
|
|
|
self->establish_job->session = nullptr;
|
|
|
|
}
|
|
|
|
self->establish_job->result(self->establish_job);
|
|
|
|
self->establish_job = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-29 18:08:39 +00:00
|
|
|
void
|
|
|
|
session::handle_introack_generated(iwp_async_introack *i)
|
|
|
|
{
|
|
|
|
session *link = static_cast< session * >(i->user);
|
|
|
|
if(i->buf)
|
|
|
|
{
|
|
|
|
// track it with the server here
|
2018-05-29 18:32:47 +00:00
|
|
|
if(link->serv->has_session_to(link->addr))
|
2018-05-29 18:08:39 +00:00
|
|
|
{
|
|
|
|
// duplicate session
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Warn("duplicate session to ", link->addr);
|
2018-05-29 18:08:39 +00:00
|
|
|
delete link;
|
|
|
|
return;
|
|
|
|
}
|
2018-05-29 18:32:47 +00:00
|
|
|
link->serv->put_session(link->addr, link);
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Debug("send introack");
|
2018-05-29 18:08:39 +00:00
|
|
|
if(llarp_ev_udp_sendto(link->udp, link->addr, i->buf, i->sz) == -1)
|
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Warn("sendto failed");
|
2018-05-29 18:08:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Debug("sent");
|
2018-05-29 18:08:39 +00:00
|
|
|
link->EnterState(eIntroAckSent);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// failed to generate?
|
2018-06-01 17:47:37 +00:00
|
|
|
llarp::Warn("failed to generate introack");
|
2018-05-29 18:08:39 +00:00
|
|
|
delete link;
|
|
|
|
}
|
|
|
|
}
|
2018-05-18 14:40:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
2018-05-22 15:54:19 +00:00
|
|
|
void
|
2018-05-25 17:52:10 +00:00
|
|
|
iwp_link_init(struct llarp_link *link, struct llarp_iwp_args args)
|
2018-05-18 14:40:06 +00:00
|
|
|
{
|
2018-05-25 17:52:10 +00:00
|
|
|
link->impl = iwp::link_alloc(args.router, args.keyfile, args.crypto,
|
2018-05-22 15:54:19 +00:00
|
|
|
args.logic, args.cryptoworker);
|
2018-05-18 14:40:06 +00:00
|
|
|
link->name = iwp::link_name;
|
2018-05-22 15:54:19 +00:00
|
|
|
link->get_our_address = iwp::link_get_addr;
|
|
|
|
link->configure = iwp::link_configure;
|
|
|
|
link->start_link = iwp::link_start;
|
|
|
|
link->stop_link = iwp::link_stop;
|
|
|
|
link->iter_sessions = iwp::link_iter_sessions;
|
|
|
|
link->try_establish = iwp::link_try_establish;
|
2018-06-01 17:47:37 +00:00
|
|
|
link->has_session_to = iwp::server::HasSessionToRouter;
|
|
|
|
link->sendto = iwp::server::SendToSession;
|
2018-05-18 14:40:06 +00:00
|
|
|
link->mark_session_active = iwp::link_mark_session_active;
|
2018-05-22 15:54:19 +00:00
|
|
|
link->free_impl = iwp::link_free;
|
2018-05-18 14:40:06 +00:00
|
|
|
}
|
|
|
|
}
|