lokinet/include/llarp/encrypted_frame.hpp
despair d425b5d308 implement tcp evloop on win32
(with its now-obsolete distinction between socketfd and fd)

sockets and file descriptors used to be distinct objects....back in the
16-bit Winsock 1.1 era, which needlessly complicated the 32-bit port
back then. these days one can use [Read|Write]File(2) to operate on
sockfds...which also have some of the semantics of [read|write]v(2)
i.e. the scatter-gather thing it's known for is done in async handler

-rick

variants are nice

added note to self

clang-format

link abyss properly

oops

shut up

*shrugs*

oops forgot to start winsock

moved our async io status flags to the base class

let derived classes override them as needed

this is probably a synchronous op _anyway_

fix typo

wtf
2018-10-30 00:16:33 -05:00

140 lines
3.3 KiB
C++

#ifndef LLARP_ENCRYPTED_FRAME_HPP
#define LLARP_ENCRYPTED_FRAME_HPP
#include <llarp/crypto.h>
#include <llarp/mem.h>
#include <llarp/threadpool.h>
#include <llarp/encrypted.hpp>
namespace llarp
{
struct EncryptedFrame : public Encrypted
{
static constexpr size_t OverheadSize =
PUBKEYSIZE + TUNNONCESIZE + SHORTHASHSIZE;
EncryptedFrame() : EncryptedFrame(256)
{
}
EncryptedFrame(const EncryptedFrame& other)
: EncryptedFrame(other.data(), other.size())
{
}
EncryptedFrame(const byte_t* buf, size_t sz) : Encrypted(buf, sz)
{
}
EncryptedFrame(size_t sz)
: Encrypted(sz + PUBKEYSIZE + TUNNONCESIZE + SHORTHASHSIZE)
{
}
EncryptedFrame&
operator=(const EncryptedFrame& other)
{
_data.resize(other.size());
memcpy(data(), other.data(), size());
return *this;
}
bool
DecryptInPlace(const byte_t* seckey, llarp_crypto* crypto);
bool
EncryptInPlace(const byte_t* seckey, const byte_t* other,
llarp_crypto* crypto);
};
/// TOOD: can only handle 1 frame at a time
template < typename User >
struct AsyncFrameEncrypter
{
typedef void (*EncryptHandler)(EncryptedFrame*, User*);
static void
Encrypt(void* user)
{
AsyncFrameEncrypter< User >* ctx =
static_cast< AsyncFrameEncrypter< User >* >(user);
if(ctx->frame->EncryptInPlace(ctx->seckey, ctx->otherKey, ctx->crypto))
ctx->handler(ctx->frame, ctx->user);
else
{
ctx->handler(nullptr, ctx->user);
}
}
llarp_crypto* crypto;
byte_t* secretkey;
EncryptHandler handler;
EncryptedFrame* frame;
User* user;
byte_t* otherKey;
AsyncFrameEncrypter(llarp_crypto* c, byte_t* seckey, EncryptHandler h)
: crypto(c), secretkey(seckey), handler(h)
{
}
void
AsyncEncrypt(llarp_threadpool* worker, llarp_buffer_t buf, byte_t* other,
User* u)
{
// TODO: should we own otherKey?
otherKey = other;
frame = new EncryptedFrame(buf.sz);
memcpy(frame->data() + PUBKEYSIZE + TUNNONCESIZE + SHORTHASHSIZE,
buf.base, buf.sz);
user = u;
llarp_threadpool_queue_job(worker, {this, &Encrypt});
}
};
/// TOOD: can only handle 1 frame at a time
template < typename User >
struct AsyncFrameDecrypter
{
typedef void (*DecryptHandler)(llarp_buffer_t*, User*);
static void
Decrypt(void* user)
{
AsyncFrameDecrypter< User >* ctx =
static_cast< AsyncFrameDecrypter< User >* >(user);
if(ctx->target->DecryptInPlace(ctx->seckey, ctx->crypto))
{
auto buf = ctx->target->Buffer();
buf->cur = buf->base + EncryptedFrame::OverheadSize;
ctx->result(buf, ctx->context);
}
else
ctx->result(nullptr, ctx->context);
}
AsyncFrameDecrypter(llarp_crypto* c, const byte_t* secretkey,
DecryptHandler h)
: result(h), crypto(c), seckey(secretkey)
{
}
DecryptHandler result;
User* context;
llarp_crypto* crypto;
const byte_t* seckey;
EncryptedFrame* target;
void
AsyncDecrypt(llarp_threadpool* worker, EncryptedFrame* frame, User* user)
{
target = frame;
context = user;
llarp_threadpool_queue_job(worker, {this, &Decrypt});
}
};
} // namespace llarp
#endif