more changes

pull/35/head
Jeff 6 years ago
parent 71172fe72b
commit cc3908f025

@ -12,7 +12,6 @@ namespace llarp
/// encrypted buffer base type
struct Encrypted
{
static const size_t MAX_SIZE = (1024 * 8);
Encrypted(Encrypted&&) = delete;
Encrypted(const Encrypted& other);
@ -24,13 +23,13 @@ namespace llarp
bool
BEncode(llarp_buffer_t* buf) const
{
return bencode_write_bytestring(buf, _data, _sz);
return bencode_write_bytestring(buf, data(), size());
}
bool
operator==(const Encrypted& other) const
{
return _sz == other._sz && memcmp(_data, other._data, _sz) == 0;
return size() == other.size() && memcmp(data(), other.data(), size()) == 0;
}
bool
@ -48,12 +47,10 @@ namespace llarp
Encrypted&
operator=(const llarp_buffer_t& buf)
{
if(buf.sz > MAX_SIZE)
return *this;
_sz = buf.sz;
if(_sz)
_data.resize(buf.sz);
if(buf.sz)
{
memcpy(_data, buf.base, _sz);
memcpy(data(), buf.base, buf.sz);
}
UpdateBuffer();
return *this;
@ -62,6 +59,7 @@ namespace llarp
void
Fill(byte_t fill)
{
size_t _sz = size();
size_t idx = 0;
while(idx < _sz)
_data[idx++] = fill;
@ -70,8 +68,9 @@ namespace llarp
void
Randomize()
{
size_t _sz = size();
if(_sz)
randombytes(_data, _sz);
randombytes(data(), _sz);
}
bool
@ -82,10 +81,8 @@ namespace llarp
return false;
if(strbuf.sz == 0)
return false;
if(strbuf.sz > MAX_SIZE)
return false;
_sz = strbuf.sz;
memcpy(_data, strbuf.base, _sz);
_data.resize(strbuf.sz);
memcpy(data(), strbuf.base, size());
UpdateBuffer();
return true;
}
@ -105,21 +102,27 @@ namespace llarp
size_t
size()
{
return _sz;
return _data.size();
}
size_t
size() const
{
return _sz;
return _data.size();
}
byte_t*
data()
{
return _data;
return _data.data();
}
const byte_t *
data() const
{
return _data.data();
}
protected:
void
UpdateBuffer()
@ -128,8 +131,7 @@ namespace llarp
m_Buffer.cur = data();
m_Buffer.sz = size();
}
byte_t _data[MAX_SIZE];
size_t _sz = 0;
std::vector<byte_t> _data;
llarp_buffer_t m_Buffer;
};
} // namespace llarp

@ -18,7 +18,7 @@ namespace llarp
}
EncryptedFrame(const EncryptedFrame& other)
: EncryptedFrame(other._data, other._sz)
: EncryptedFrame(other.data(), other.size())
{
}
@ -33,11 +33,8 @@ namespace llarp
EncryptedFrame&
operator=(const EncryptedFrame& other)
{
if(other._sz <= MAX_SIZE)
{
_sz = other._sz;
memcpy(_data, other._data, _sz);
}
_data.resize(other.size());
memcpy(data(), other.data(), size());
return *this;
}

@ -11,20 +11,17 @@ namespace llarp
}
Encrypted::Encrypted(const Encrypted& other)
: Encrypted(other._data, other._sz)
: Encrypted(other.data(), other.size())
{
}
Encrypted::Encrypted(const byte_t* buf, size_t sz) : _sz(sz)
Encrypted::Encrypted(const byte_t* buf, size_t sz) : _data(sz)
{
if(sz <= MAX_SIZE)
{
if(buf)
memcpy(data(), buf, sz);
else
llarp::Zero(data(), sz);
UpdateBuffer();
}
if(buf)
memcpy(data(), buf, sz);
else
llarp::Zero(data(), sz);
UpdateBuffer();
}
Encrypted::~Encrypted()

@ -334,7 +334,7 @@ namespace llarp
}
if(IsExpired(now))
{
llarp::LogWarn("introset expired");
llarp::LogWarn("introset expired: ", *this);
return false;
}
return true;

@ -903,7 +903,7 @@ namespace llarp
auto itr = m_ServiceLookupFails.find(endpoint);
if(itr != m_ServiceLookupFails.end())
{
if(itr->second % 2)
if(itr->second % 2 == 0)
{
// get far router
path = GetEstablishedPathClosestTo(~endpoint);
@ -919,6 +919,7 @@ namespace llarp
path = PickRandomEstablishedPath();
if(!path)
{
llarp::LogError(Name(), "no working paths for lookup");
hook(remote, nullptr);
return false;
}

Loading…
Cancel
Save