Simplify some code

pull/266/head
Michael 5 years ago
parent 6f0ce07571
commit 47380ba64b
No known key found for this signature in database
GPG Key ID: 2D51757B47E2434C

@ -84,7 +84,7 @@ namespace llarp
Encrypted&
operator=(const Encrypted& other)
{
return (*this) = other.Buffer().underlying;
return Encrypted::operator=(llarp_buffer_t(other));
}
Encrypted&
@ -133,12 +133,6 @@ namespace llarp
return &m_Buffer;
}
CopyableBuffer
Buffer() const
{
return CopyableBuffer{m_Buffer};
}
size_t
size()
{

@ -234,8 +234,7 @@ namespace llarp
hdr.seqno = seqno;
hdr.cmd = XMIT;
AlignedBuffer< fragoverhead + fragsize > frag;
CopyableBuffer copiedBuffer(frag.as_buffer());
auto &buf = copiedBuffer.underlying;
llarp_buffer_t buf(frag);
const byte_t *ptr = msg.data();
Fragno_t idx = 0;
FragLen_t len = sz;
@ -282,8 +281,7 @@ namespace llarp
hdr.fraglen = 0;
hdr.fragno = 0;
AlignedBuffer< fragoverhead > frag;
CopyableBuffer copiedBuffer(frag.as_buffer());
auto &buf = copiedBuffer.underlying;
llarp_buffer_t buf(frag);
if(!hdr.Encode(&buf, llarp_buffer_t(nullptr, nullptr, 0)))
return false;
return write_pkt(buf.base, buf.sz) == int(buf.sz);

@ -622,9 +622,9 @@ namespace llarp
remoteTransportPubKey = addr.pubkey;
remoteRC = rc;
RouterID rid = remoteRC.pubkey;
Crypto()->shorthash(txKey, rid.as_buffer().underlying);
Crypto()->shorthash(txKey, llarp_buffer_t(rid));
rid = p->GetOurRC().pubkey;
Crypto()->shorthash(rxKey, rid.as_buffer().underlying);
Crypto()->shorthash(rxKey, llarp_buffer_t(rid));
sock = s;
assert(utp_set_userdata(sock, this) == this);
@ -638,7 +638,7 @@ namespace llarp
Session::Session(LinkLayer* p, utp_socket* s, const Addr& addr) : Session(p)
{
RouterID rid = p->GetOurRC().pubkey;
Crypto()->shorthash(rxKey, rid.as_buffer().underlying);
Crypto()->shorthash(rxKey, llarp_buffer_t(rid));
remoteRC.Clear();
sock = s;
assert(s == sock);
@ -665,7 +665,7 @@ namespace llarp
if(!gotLIM)
{
remoteRC = msg->rc;
Crypto()->shorthash(txKey, remoteRC.pubkey.as_buffer().underlying);
Crypto()->shorthash(txKey, llarp_buffer_t(remoteRC.pubkey));
if(!DoKeyExchange(std::bind(&Crypto::transport_dh_server, Crypto(), _1,
_2, _3, _4),
@ -1054,36 +1054,36 @@ namespace llarp
llarp_buffer_t in(ptr + FragmentOverheadSize,
FragmentBufferSize - FragmentOverheadSize);
CopyableBuffer out = rxFragBody.as_buffer();
llarp_buffer_t out(rxFragBody);
// decrypt
if(!Crypto()->xchacha20_alt(out.underlying, in, rxKey,
if(!Crypto()->xchacha20_alt(out, in, rxKey,
ptr + FragmentHashSize))
{
llarp::LogError("failed to decrypt message from ", remoteAddr);
return false;
}
// get inner nonce
AlignedBuffer< 24 > A(out.underlying.base);
AlignedBuffer< 24 > A(out.base);
// advance buffer
out.underlying.cur += A.size();
out.cur += A.size();
// read msgid
uint32_t msgid;
if(!llarp_buffer_read_uint32(&out.underlying, &msgid))
if(!llarp_buffer_read_uint32(&out, &msgid))
{
llarp::LogError("failed to read msgid");
return false;
}
// read length and remaining
uint16_t length, remaining;
if(!(llarp_buffer_read_uint16(&out.underlying, &length)
&& llarp_buffer_read_uint16(&out.underlying, &remaining)))
if(!(llarp_buffer_read_uint16(&out, &length)
&& llarp_buffer_read_uint16(&out, &remaining)))
{
llarp::LogError("failed to read the rest of the header");
return false;
}
if(length
> (out.underlying.sz - (out.underlying.cur - out.underlying.base)))
> (out.sz - (out.cur - out.base)))
{
// too big length
llarp::LogError("fragment body too big");
@ -1103,7 +1103,7 @@ namespace llarp
// add message activity
itr->second.lastActive = parent->Now();
// append data
if(!itr->second.AppendData(out.underlying.cur, length))
if(!itr->second.AppendData(out.cur, length))
{
llarp::LogError("inbound buffer is full");
return false; // not enough room

@ -63,7 +63,7 @@ namespace llarp
auto path = r->paths.GetByDownstream(session->GetPubKey(), pathid);
if(path)
{
return path->HandleUpstream(X.Buffer().underlying, Y, r);
return path->HandleUpstream(llarp_buffer_t(X), Y, r);
}
return false;
}
@ -126,7 +126,7 @@ namespace llarp
auto path = r->paths.GetByUpstream(session->GetPubKey(), pathid);
if(path)
{
return path->HandleDownstream(X.Buffer().underlying, Y, r);
return path->HandleDownstream(llarp_buffer_t(X), Y, r);
}
llarp::LogWarn("unhandled downstream message");
return false;

@ -279,7 +279,7 @@ namespace llarp
return;
}
// generate hash of hop key for nonce mutation
crypto->shorthash(self->hop->nonceXOR, self->hop->pathKey.as_buffer().underlying);
crypto->shorthash(self->hop->nonceXOR, llarp_buffer_t(self->hop->pathKey));
using namespace std::placeholders;
if(self->record.work
&& self->record.work->IsValid(

@ -57,7 +57,7 @@ namespace llarp
return;
}
// generate nonceXOR valueself->hop->pathKey
ctx->crypto->shorthash(hop.nonceXOR, hop.shared.as_buffer().underlying);
ctx->crypto->shorthash(hop.nonceXOR, llarp_buffer_t(hop.shared));
++ctx->idx;
bool isFarthestHop = ctx->idx == ctx->path->hops.size();

@ -227,12 +227,6 @@ namespace llarp
return as_array().cend();
}
CopyableBuffer
as_buffer()
{
return CopyableBuffer(llarp_buffer_t(as_array()));
}
bool
BEncode(llarp_buffer_t* buf) const
{

@ -107,7 +107,7 @@ struct llarp_buffer_t
}
template < typename T >
llarp_buffer_t(const T &t) : base(t.data()), cur(t.data()), sz(t.size())
llarp_buffer_t(const T &t) : llarp_buffer_t(t.data(), t.size())
{
}

@ -36,7 +36,7 @@ namespace llarp
random.Randomize();
Signature sig;
const llarp_buffer_t& buf = random.as_buffer().underlying;
llarp_buffer_t buf(random);
ASSERT_TRUE(crypto.sign(sig, secret, buf));
ASSERT_TRUE(crypto.verify(secret.toPublic(), buf, sig));
// mangle sig

Loading…
Cancel
Save