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

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

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

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

@ -279,7 +279,7 @@ namespace llarp
return; return;
} }
// generate hash of hop key for nonce mutation // 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; using namespace std::placeholders;
if(self->record.work if(self->record.work
&& self->record.work->IsValid( && self->record.work->IsValid(

@ -57,7 +57,7 @@ namespace llarp
return; return;
} }
// generate nonceXOR valueself->hop->pathKey // 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; ++ctx->idx;
bool isFarthestHop = ctx->idx == ctx->path->hops.size(); bool isFarthestHop = ctx->idx == ctx->path->hops.size();

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

@ -107,7 +107,7 @@ struct llarp_buffer_t
} }
template < typename 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(); random.Randomize();
Signature sig; 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.sign(sig, secret, buf));
ASSERT_TRUE(crypto.verify(secret.toPublic(), buf, sig)); ASSERT_TRUE(crypto.verify(secret.toPublic(), buf, sig));
// mangle sig // mangle sig

Loading…
Cancel
Save