Convert llarp_buffer_t to be a class with methods

pull/316/head
Michael 5 years ago
parent 7df2a227d3
commit a00d6afc5e
No known key found for this signature in database
GPG Key ID: 2D51757B47E2434C

@ -33,7 +33,7 @@ namespace llarp
// first key
if(dec->firstKey)
{
if(!llarp_buffer_eq(*key, "A"))
if(!(*key == "A"))
return false;
if(!bencode_read_string(r->buffer, &strbuf))
return false;

@ -100,7 +100,7 @@ namespace llarp
{
llarp_buffer_t strbuf;
if(llarp_buffer_eq(key, "E"))
if(key == "E")
{
uint64_t result;
if(!bencode_read_integer(val, &result))
@ -110,7 +110,7 @@ namespace llarp
return true;
}
if(llarp_buffer_eq(key, "I"))
if(key == "I")
{
uint64_t result;
if(!bencode_read_integer(val, &result))
@ -119,7 +119,7 @@ namespace llarp
iterative = result != 0;
return true;
}
if(llarp_buffer_eq(key, "K"))
if(key == "K")
{
if(!bencode_read_string(val, &strbuf))
return false;
@ -129,11 +129,11 @@ namespace llarp
std::copy(strbuf.base, strbuf.base + K.SIZE, K.begin());
return true;
}
if(llarp_buffer_eq(key, "T"))
if(key == "T")
{
return bencode_read_integer(val, &txid);
}
if(llarp_buffer_eq(key, "V"))
if(key == "V")
{
return bencode_read_integer(val, &version);
}

@ -83,11 +83,11 @@ namespace llarp
bool
GotIntroMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *buf)
{
if(llarp_buffer_eq(key, "I"))
if(key == "I")
{
return BEncodeReadList(I, buf);
}
if(llarp_buffer_eq(key, "K"))
if(key == "K")
{
if(K) // duplicate key?
return false;

@ -52,22 +52,22 @@ namespace llarp
bool
GotRouterMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *val)
{
if(llarp_buffer_eq(key, "K"))
if(key == "K")
{
if(K) // duplicate key?
return false;
K.reset(new dht::Key_t());
return K->BDecode(val);
}
if(llarp_buffer_eq(key, "N"))
if(key == "N")
{
return BEncodeReadList(N, val);
}
if(llarp_buffer_eq(key, "R"))
if(key == "R")
{
return BEncodeReadList(R, val);
}
if(llarp_buffer_eq(key, "T"))
if(key == "T")
{
return bencode_read_integer(val, &txid);
}

@ -19,7 +19,7 @@ namespace llarp
llarp_buffer_t *val)
{
bool read = false;
if(llarp_buffer_eq(key, "E"))
if(key == "E")
{
return BEncodeReadList(E, val);
}
@ -27,7 +27,7 @@ namespace llarp
return false;
if(!BEncodeMaybeReadDictInt("R", R, read, key, val))
return false;
if(llarp_buffer_eq(key, "S"))
if(key == "S")
{
read = true;
hasS = true;

@ -313,17 +313,17 @@ extern "C"
uint16_t fields;
// reads as HOST byte order
if(!llarp_buffer_read_uint16(buffer, &hdr->id))
if(!buffer->read_uint16(hdr->id))
return false;
if(!llarp_buffer_read_uint16(buffer, &fields))
if(!buffer->read_uint16(fields))
return false;
if(!llarp_buffer_read_uint16(buffer, &hdr->qdCount))
if(!buffer->read_uint16(hdr->qdCount))
return false;
if(!llarp_buffer_read_uint16(buffer, &hdr->anCount))
if(!buffer->read_uint16(hdr->anCount))
return false;
if(!llarp_buffer_read_uint16(buffer, &hdr->nsCount))
if(!buffer->read_uint16(hdr->nsCount))
return false;
if(!llarp_buffer_read_uint16(buffer, &hdr->arCount))
if(!buffer->read_uint16(hdr->arCount))
return false;
// decode fields into hdr

@ -14,33 +14,33 @@ namespace llarp
bool
MessageHeader::Encode(llarp_buffer_t* buf) const
{
if(!llarp_buffer_put_uint16(buf, id))
if(!buf->put_uint16(id))
return false;
if(!llarp_buffer_put_uint16(buf, fields))
if(!buf->put_uint16(fields))
return false;
if(!llarp_buffer_put_uint16(buf, qd_count))
if(!buf->put_uint16(qd_count))
return false;
if(!llarp_buffer_put_uint16(buf, an_count))
if(!buf->put_uint16(an_count))
return false;
if(!llarp_buffer_put_uint16(buf, ns_count))
if(!buf->put_uint16(ns_count))
return false;
return llarp_buffer_put_uint16(buf, ar_count);
return buf->put_uint16(ar_count);
}
bool
MessageHeader::Decode(llarp_buffer_t* buf)
{
if(!llarp_buffer_read_uint16(buf, &id))
if(!buf->read_uint16(id))
return false;
if(!llarp_buffer_read_uint16(buf, &fields))
if(!buf->read_uint16(fields))
return false;
if(!llarp_buffer_read_uint16(buf, &qd_count))
if(!buf->read_uint16(qd_count))
return false;
if(!llarp_buffer_read_uint16(buf, &an_count))
if(!buf->read_uint16(an_count))
return false;
if(!llarp_buffer_read_uint16(buf, &ns_count))
if(!buf->read_uint16(ns_count))
return false;
return llarp_buffer_read_uint16(buf, &ar_count);
return buf->read_uint16(ar_count);
}
Message::Message(Message&& other)
@ -232,7 +232,7 @@ namespace llarp
rec.ttl = ttl;
std::array< byte_t, 512 > tmp = {{0}};
llarp_buffer_t buf(tmp);
llarp_buffer_put_uint16(&buf, priority);
buf.put_uint16(priority);
if(EncodeName(&buf, name))
{
buf.sz = buf.cur - buf.base;

@ -11,7 +11,7 @@ namespace llarp
bool
DecodeName(llarp_buffer_t* buf, Name_t& name)
{
if(llarp_buffer_size_left(*buf) < 1)
if(buf->size_left() < 1)
return false;
std::stringstream ss;
size_t l;
@ -27,7 +27,7 @@ namespace llarp
llarp::DumpBuffer(*buf);
return false;
}
if(llarp_buffer_size_left(*buf) < l)
if(buf->size_left() < l)
return false;
ss << Name_t((const char*)buf->cur, l);
@ -56,7 +56,7 @@ namespace llarp
return false;
*(buf->cur) = l;
buf->cur++;
if(llarp_buffer_size_left(*buf) < l)
if(buf->size_left() < l)
return false;
if(l)
{

@ -22,9 +22,9 @@ namespace llarp
{
if(!EncodeName(buf, qname))
return false;
if(!llarp_buffer_put_uint16(buf, qtype))
if(!buf->put_uint16(qtype))
return false;
return llarp_buffer_put_uint16(buf, qclass);
return buf->put_uint16(qclass);
}
bool
@ -35,12 +35,12 @@ namespace llarp
llarp::LogError("failed to decode name");
return false;
}
if(!llarp_buffer_read_uint16(buf, &qtype))
if(!buf->read_uint16(qtype))
{
llarp::LogError("failed to decode type");
return false;
}
if(!llarp_buffer_read_uint16(buf, &qclass))
if(!buf->read_uint16(qclass))
{
llarp::LogError("failed to decode class");
return false;

@ -31,15 +31,15 @@ namespace llarp
{
return false;
}
if(!llarp_buffer_put_uint16(buf, rr_type))
if(!buf->put_uint16(rr_type))
{
return false;
}
if(!llarp_buffer_put_uint16(buf, rr_class))
if(!buf->put_uint16(rr_class))
{
return false;
}
if(!llarp_buffer_put_uint32(buf, ttl))
if(!buf->put_uint32(ttl))
{
return false;
}
@ -58,17 +58,17 @@ namespace llarp
llarp::LogError("failed to decode rr name");
return false;
}
if(!llarp_buffer_read_uint16(buf, &rr_type))
if(!buf->read_uint16(rr_type))
{
llarp::LogError("failed to decode rr type");
return false;
}
if(!llarp_buffer_read_uint16(buf, &rr_class))
if(!buf->read_uint16(rr_class))
{
llarp::LogError("failed to decode rr class");
return false;
}
if(!llarp_buffer_read_uint32(buf, &ttl))
if(!buf->read_uint32(ttl))
{
llarp::LogError("failed to decode ttl");
return false;

@ -11,9 +11,9 @@ namespace llarp
if(v.size() > 65536)
return false;
uint16_t len = v.size();
if(!llarp_buffer_put_uint16(buf, len))
if(!buf->put_uint16(len))
return false;
if(llarp_buffer_size_left(*buf) < len)
if(buf->size_left() < len)
return false;
memcpy(buf->cur, v.data(), len);
buf->cur += len;
@ -24,9 +24,9 @@ namespace llarp
DecodeRData(llarp_buffer_t* buf, std::vector< byte_t >& v)
{
uint16_t len;
if(!llarp_buffer_read_uint16(buf, &len))
if(!buf->read_uint16(len))
return false;
size_t left = llarp_buffer_size_left(*buf);
size_t left = buf->size_left();
if(left < len)
return false;
v.resize(size_t(len));

@ -28,6 +28,13 @@ namespace llarp
const AddressInfo &ai);
~Session();
util::StatusObject
ExtractStatus() const override
{
// TODO: fill me in.
return {};
}
void
PumpIO();
@ -124,7 +131,7 @@ namespace llarp
bool
Decode(llarp_buffer_t *buf)
{
if(llarp_buffer_size_left(*buf) < fragoverhead)
if(buf->size_left() < fragoverhead)
return false;
version = *buf->cur;
if(version != LLARP_PROTO_VERSION)
@ -136,8 +143,8 @@ namespace llarp
buf->cur++;
fragno = *buf->cur;
buf->cur++;
llarp_buffer_read_uint16(buf, &fraglen);
llarp_buffer_read_uint32(buf, &seqno);
buf->read_uint16(fraglen);
buf->read_uint32(seqno);
return fraglen <= fragsize;
}
@ -147,7 +154,7 @@ namespace llarp
if(body.sz > fragsize)
return false;
fraglen = body.sz;
if(llarp_buffer_size_left(*buf) < (fragoverhead + fraglen))
if(buf->size_left() < (fragoverhead + fraglen))
return false;
*buf->cur = LLARP_PROTO_VERSION;
buf->cur++;
@ -157,8 +164,8 @@ namespace llarp
buf->cur++;
*buf->cur = fragno;
buf->cur++;
llarp_buffer_put_uint16(buf, fraglen);
llarp_buffer_put_uint32(buf, seqno);
buf->put_uint16(fraglen);
buf->put_uint32(seqno);
if(fraglen)
memcpy(buf->cur, body.base, fraglen);
buf->cur += fraglen;

@ -40,7 +40,7 @@ namespace llarp
bool
InboundMessage::AppendData(const byte_t* ptr, uint16_t sz)
{
if(llarp_buffer_size_left(buffer) < sz)
if(buffer.size_left() < sz)
return false;
memcpy(buffer.cur, ptr, sz);
buffer.cur += sz;
@ -867,7 +867,6 @@ namespace llarp
}
else
{
LogWarn("utp_socket got data with no underlying session");
utp_shutdown(arg->socket, SHUT_RDWR);
utp_close(arg->socket);
@ -986,14 +985,12 @@ namespace llarp
util::StatusObject
Session::ExtractStatus() const
{
return {
{"client", !remoteRC.IsPublicRouter()},
{"sendBacklog", uint64_t(SendQueueBacklog())},
{"tx", m_TXRate},
{"rx", m_RXRate},
{"remoteAddr", remoteAddr.ToString()},
{"pubkey", remoteRC.pubkey.ToHex()}
};
return {{"client", !remoteRC.IsPublicRouter()},
{"sendBacklog", uint64_t(SendQueueBacklog())},
{"tx", m_TXRate},
{"rx", m_RXRate},
{"remoteAddr", remoteAddr.ToString()},
{"pubkey", remoteRC.pubkey.ToHex()}};
}
bool
@ -1079,15 +1076,14 @@ namespace llarp
out.cur += A.size();
// read msgid
uint32_t msgid;
if(!llarp_buffer_read_uint32(&out, &msgid))
if(!out.read_uint32(msgid))
{
LogError("failed to read msgid");
return false;
}
// read length and remaining
uint16_t length, remaining;
if(!(llarp_buffer_read_uint16(&out, &length)
&& llarp_buffer_read_uint16(&out, &remaining)))
if(!(out.read_uint16(length) && out.read_uint16(remaining)))
{
LogError("failed to read the rest of the header");
return false;

@ -17,10 +17,10 @@ namespace llarp
bool
DHTImmediateMessage::DecodeKey(const llarp_buffer_t &key, llarp_buffer_t *buf)
{
if(llarp_buffer_eq(key, "m"))
if(key == "m")
return llarp::dht::DecodeMesssageList(dht::Key_t(session->GetPubKey()),
buf, msgs);
if(llarp_buffer_eq(key, "v"))
if(key == "v")
{
if(!bencode_read_integer(buf, &version))
return false;

@ -15,7 +15,7 @@ namespace llarp
bool
LinkIntroMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf)
{
if(llarp_buffer_eq(key, "a"))
if(key == "a")
{
llarp_buffer_t strbuf;
if(!bencode_read_string(buf, &strbuf))
@ -24,18 +24,18 @@ namespace llarp
return false;
return *strbuf.cur == 'i';
}
if(llarp_buffer_eq(key, "n"))
else if(key == "n")
{
if(N.BDecode(buf))
return true;
llarp::LogWarn("failed to decode nonce in LIM");
return false;
}
if(llarp_buffer_eq(key, "p"))
else if(key == "p")
{
return bencode_read_integer(buf, &P);
}
if(llarp_buffer_eq(key, "r"))
else if(key == "r")
{
if(rc.BDecode(buf))
return true;
@ -43,7 +43,7 @@ namespace llarp
llarp::DumpBuffer(*buf);
return false;
}
else if(llarp_buffer_eq(key, "v"))
else if(key == "v")
{
if(!bencode_read_integer(buf, &version))
return false;
@ -56,7 +56,7 @@ namespace llarp
llarp::LogDebug("LIM version ", version);
return true;
}
else if(llarp_buffer_eq(key, "z"))
else if(key == "z")
{
return Z.BDecode(buf);
}

@ -48,7 +48,7 @@ namespace llarp
if(!key)
return false;
// we are expecting the first key to be 'a'
if(!llarp_buffer_eq(*key, "a"))
if(!(*key == "a"))
{
llarp::LogWarn("message has no message type");
return false;

@ -17,7 +17,7 @@ namespace llarp
bool
LR_CommitMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf)
{
if(llarp_buffer_eq(key, "c"))
if(key == "c")
{
return BEncodeReadArray(frames, buf);
}
@ -132,7 +132,7 @@ namespace llarp
if(!BEncodeMaybeReadVersion("v", self->version, LLARP_PROTO_VERSION, read,
*key, r->buffer))
return false;
if(llarp_buffer_eq(*key, "w"))
if(*key == "w")
{
// check for duplicate
if(self->work)

@ -50,7 +50,7 @@ namespace llarp
llarp_buffer_t strbuf;
// rank
if(llarp_buffer_eq(key, "c"))
if(key == "c")
{
if(!bencode_read_integer(buf, &i))
return false;
@ -63,7 +63,7 @@ namespace llarp
}
// dialect
if(llarp_buffer_eq(key, "d"))
if(key == "d")
{
if(!bencode_read_string(buf, &strbuf))
return false;
@ -76,13 +76,13 @@ namespace llarp
}
// encryption public key
if(llarp_buffer_eq(key, "e"))
if(key == "e")
{
return pubkey.BDecode(buf);
}
// ip address
if(llarp_buffer_eq(key, "i"))
if(key == "i")
{
if(!bencode_read_string(buf, &strbuf))
return false;
@ -96,7 +96,7 @@ namespace llarp
}
// port
if(llarp_buffer_eq(key, "p"))
if(key == "p")
{
if(!bencode_read_integer(buf, &i))
return false;
@ -109,7 +109,7 @@ namespace llarp
}
// version
if(llarp_buffer_eq(key, "v"))
if(key == "v")
{
if(!bencode_read_integer(buf, &i))
return false;

@ -76,9 +76,9 @@ namespace llarp
return false;
if(!BEncodeMaybeReadDictInt("v", version, read, k, buf))
return false;
if(llarp_buffer_eq(k, "a"))
if(k == "a")
return bdecode_ip_string(buf, address);
if(llarp_buffer_eq(k, "b"))
if(k == "b")
return bdecode_ip_string(buf, netmask);
return read;
}

@ -236,17 +236,19 @@ namespace llarp
util::StatusObject obj{{"dht", _dht->impl.ExtractStatus()},
{"services", hiddenServiceContext.ExtractStatus()},
{"exit", _exitContext.ExtractStatus()}};
std::vector<util::StatusObject> ob_links, ib_links;
std::transform(inboundLinks.begin(), inboundLinks.end(), std::back_inserter(ib_links), [](const auto & link) -> util::StatusObject {
return link->ExtractStatus();
});
std::transform(outboundLinks.begin(), outboundLinks.end(), std::back_inserter(ob_links), [](const auto & link) -> util::StatusObject {
return link->ExtractStatus();
});
obj.Put("links", util::StatusObject{
{"outbound", ob_links},
{"inbound", ib_links}
});
std::vector< util::StatusObject > ob_links, ib_links;
std::transform(inboundLinks.begin(), inboundLinks.end(),
std::back_inserter(ib_links),
[](const auto &link) -> util::StatusObject {
return link->ExtractStatus();
});
std::transform(outboundLinks.begin(), outboundLinks.end(),
std::back_inserter(ob_links),
[](const auto &link) -> util::StatusObject {
return link->ExtractStatus();
});
obj.Put("links",
util::StatusObject{{"outbound", ob_links}, {"inbound", ib_links}});
return obj;
}
@ -1108,7 +1110,7 @@ namespace llarp
if(!msg->BEncode(&buf))
{
LogWarn("failed to encode outbound message, buffer size left: ",
llarp_buffer_size_left(buf));
buf.size_left());
return;
}
// set size of message
@ -1255,13 +1257,14 @@ namespace llarp
}
void
Router::SetRouterWhitelist(const std::vector<RouterID> & routers)
Router::SetRouterWhitelist(const std::vector< RouterID > &routers)
{
lokinetRouters.clear();
for(const auto & router : routers)
lokinetRouters.emplace(router, std::numeric_limits<llarp_time_t>::max());
LogInfo("lokinet service node list now has ", lokinetRouters.size(),
" routers");
for(const auto &router : routers)
lokinetRouters.emplace(router,
std::numeric_limits< llarp_time_t >::max());
LogInfo("lokinet service node list now has ", lokinetRouters.size(),
" routers");
}
bool

@ -176,7 +176,7 @@ namespace llarp
if(!BEncodeMaybeReadDictEntry("k", pubkey, read, key, buf))
return false;
if(llarp_buffer_eq(key, "n"))
if(key == "n")
{
llarp_buffer_t strbuf;
if(!bencode_read_string(buf, &strbuf))

@ -16,15 +16,15 @@ namespace llarp
{
llarp::dht::Key_t from;
from.Zero();
if(llarp_buffer_eq(key, "M"))
if(key == "M")
{
return llarp::dht::DecodeMesssageList(from, val, M, true);
}
else if(llarp_buffer_eq(key, "S"))
else if(key == "S")
{
return bencode_read_integer(val, &S);
}
else if(llarp_buffer_eq(key, "V"))
else if(key == "V")
{
return bencode_read_integer(val, &V);
}

@ -60,7 +60,7 @@ namespace llarp
if(self->firstKey)
{
llarp_buffer_t strbuf;
if(!llarp_buffer_eq(*key, "A"))
if(!(*key == "A"))
return false;
if(!bencode_read_string(r->buffer, &strbuf))
return false;

@ -34,7 +34,7 @@ namespace llarp
bool read = false;
if(!BEncodeMaybeReadDictEntry("e", enckey, read, key, buf))
return false;
if(llarp_buffer_eq(key, "q"))
if(key == "q")
{
llarp_buffer_t str;
if(!bencode_read_string(buf, &str))

@ -34,7 +34,7 @@ namespace llarp
if(!BEncodeMaybeReadDictEntry("a", A, read, key, buf))
return false;
if(llarp_buffer_eq(key, "i"))
if(key == "i")
{
return BEncodeReadList(I, buf);
}
@ -47,7 +47,7 @@ namespace llarp
if(!BEncodeMaybeReadDictInt("t", T, read, key, buf))
return false;
if(llarp_buffer_eq(key, "w"))
if(key == "w")
{
if(W)
delete W;

@ -44,7 +44,7 @@ namespace llarp
bool read = false;
if(!BEncodeMaybeReadDictInt("a", proto, read, k, buf))
return false;
if(llarp_buffer_eq(k, "d"))
if(k == "d")
{
llarp_buffer_t strbuf;
if(!bencode_read_string(buf, &strbuf))
@ -126,7 +126,7 @@ namespace llarp
ProtocolFrame::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val)
{
bool read = false;
if(llarp_buffer_eq(key, "A"))
if(key == "A")
{
llarp_buffer_t strbuf;
if(!bencode_read_string(val, &strbuf))

@ -15,8 +15,7 @@ bencode_read_integer(struct llarp_buffer_t* buffer, uint64_t* result)
buffer->cur++;
len =
llarp_buffer_read_until(buffer, 'e', (byte_t*)numbuf, sizeof(numbuf) - 1);
len = buffer->read_until('e', (byte_t*)numbuf, sizeof(numbuf) - 1);
if(!len)
{
return false;
@ -34,8 +33,7 @@ bencode_read_string(llarp_buffer_t* buffer, llarp_buffer_t* result)
{
char numbuf[10];
size_t len =
llarp_buffer_read_until(buffer, ':', (byte_t*)numbuf, sizeof(numbuf) - 1);
size_t len = buffer->read_until(':', (byte_t*)numbuf, sizeof(numbuf) - 1);
if(!len)
return false;
@ -50,7 +48,7 @@ bencode_read_string(llarp_buffer_t* buffer, llarp_buffer_t* result)
buffer->cur++;
len = llarp_buffer_size_left(*buffer);
len = buffer->size_left();
if(len < slen)
{
return false;
@ -66,42 +64,55 @@ bencode_read_string(llarp_buffer_t* buffer, llarp_buffer_t* result)
bool
bencode_write_bytestring(llarp_buffer_t* buff, const void* data, size_t sz)
{
if(!llarp_buffer_writef(buff, "%zu:", sz))
if(!buff->writef("%zu:", sz))
{
return false;
}
return llarp_buffer_write(buff, data, sz);
return buff->write(reinterpret_cast< const char* >(data),
reinterpret_cast< const char* >(data) + sz);
}
bool
bencode_write_uint64(llarp_buffer_t* buff, uint64_t i)
{
return llarp_buffer_writef(buff, "i%llu", i)
&& llarp_buffer_write(buff, "e", 1);
if(!buff->writef("i%llu", i))
{
return false;
}
static const char letter[1] = {'e'};
assert(std::distance(std::begin(letter), std::end(letter)) == 1);
return buff->write(std::begin(letter), std::end(letter));
}
bool
bencode_write_version_entry(llarp_buffer_t* buff)
{
return llarp_buffer_writef(buff, "1:vi%de", LLARP_PROTO_VERSION);
return buff->writef("1:vi%de", LLARP_PROTO_VERSION);
}
bool
bencode_start_list(llarp_buffer_t* buff)
{
return llarp_buffer_write(buff, "l", 1);
static const char letter[1] = {'l'};
assert(std::distance(std::begin(letter), std::end(letter)) == 1);
return buff->write(std::begin(letter), std::end(letter));
}
bool
bencode_start_dict(llarp_buffer_t* buff)
{
return llarp_buffer_write(buff, "d", 1);
static const char letter[1] = {'d'};
assert(std::distance(std::begin(letter), std::end(letter)) == 1);
return buff->write(std::begin(letter), std::end(letter));
}
bool
bencode_end(llarp_buffer_t* buff)
{
return llarp_buffer_write(buff, "e", 1);
static const char letter[1] = {'e'};
assert(std::distance(std::begin(letter), std::end(letter)) == 1);
return buff->write(std::begin(letter), std::end(letter));
}
bool
@ -112,7 +123,7 @@ bencode_read_dict(llarp_buffer_t* buff, struct dict_reader* r)
if(*r->buffer->cur != 'd') // ensure is a dictionary
return false;
r->buffer->cur++;
while(llarp_buffer_size_left(*r->buffer) && *r->buffer->cur != 'e')
while(r->buffer->size_left() && *r->buffer->cur != 'e')
{
if(bencode_read_string(r->buffer, &strbuf))
{
@ -145,7 +156,7 @@ bencode_read_list(llarp_buffer_t* buff, struct list_reader* r)
}
r->buffer->cur++;
while(llarp_buffer_size_left(*r->buffer) && *r->buffer->cur != 'e')
while(r->buffer->size_left() && *r->buffer->cur != 'e')
{
if(!r->on_item(r, true)) // check for early abort
return false;

@ -46,7 +46,7 @@ namespace llarp
BEncodeMaybeReadDictList(const char* k, List_t& item, bool& read,
const llarp_buffer_t& key, llarp_buffer_t* buf)
{
if(llarp_buffer_eq(key, k))
if(key == k)
{
if(!BEncodeReadList(item, buf))
{
@ -62,7 +62,7 @@ namespace llarp
BEncodeMaybeReadDictEntry(const char* k, Item_t& item, bool& read,
const llarp_buffer_t& key, llarp_buffer_t* buf)
{
if(llarp_buffer_eq(key, k))
if(key == k)
{
if(!item.BDecode(buf))
{
@ -81,7 +81,7 @@ namespace llarp
BEncodeMaybeReadDictInt(const char* k, Int_t& i, bool& read,
const llarp_buffer_t& key, llarp_buffer_t* buf)
{
if(llarp_buffer_eq(key, k))
if(key == k)
{
if(!bencode_read_integer(buf, &i))
{
@ -100,7 +100,7 @@ namespace llarp
bool& read, const llarp_buffer_t& key,
llarp_buffer_t* buf)
{
if(llarp_buffer_eq(key, k))
if(key == k)
{
if(!bencode_read_integer(buf, &item))
return false;
@ -149,7 +149,7 @@ namespace llarp
buf->cur++;
size_t idx = 0;
while(llarp_buffer_size_left(*buf) && *buf->cur != 'e')
while(buf->size_left() && *buf->cur != 'e')
{
if(idx >= array.size())
return false;
@ -184,7 +184,7 @@ namespace llarp
return false;
buf->cur++;
while(llarp_buffer_size_left(*buf) && *buf->cur != 'e')
while(buf->size_left() && *buf->cur != 'e')
{
if(!result.emplace(result.end())->BDecode(buf))
return false;
@ -203,7 +203,7 @@ namespace llarp
return false;
buf->cur++;
while(llarp_buffer_size_left(*buf) && *buf->cur != 'e')
while(buf->size_left() && *buf->cur != 'e')
{
T item;
if(!item.BDecode(buf))

@ -5,72 +5,99 @@
#include <stdio.h>
size_t
llarp_buffer_size_left(const llarp_buffer_t& buff)
llarp_buffer_t::size_left() const
{
size_t diff = buff.cur - buff.base;
if(diff > buff.sz)
size_t diff = cur - base;
if(diff > sz)
{
return 0;
}
else
return buff.sz - diff;
return sz - diff;
}
bool
llarp_buffer_writef(llarp_buffer_t* buff, const char* fmt, ...)
llarp_buffer_t::writef(const char* fmt, ...)
{
int written;
ssize_t sz = llarp_buffer_size_left(*buff);
ssize_t sz = size_left();
va_list args;
va_start(args, fmt);
written = vsnprintf((char*)buff->cur, sz, fmt, args);
written = vsnprintf(reinterpret_cast< char* >(cur), sz, fmt, args);
va_end(args);
if(written <= 0)
return false;
if(sz < written)
return false;
buff->cur += written;
cur += written;
return true;
}
bool
llarp_buffer_write(llarp_buffer_t* buff, const void* data, size_t sz)
llarp_buffer_t::put_uint16(uint16_t i)
{
size_t left = llarp_buffer_size_left(*buff);
if(left >= sz)
{
memcpy(buff->cur, data, sz);
buff->cur += sz;
return true;
}
return false;
if(size_left() < sizeof(uint16_t))
return false;
htobe16buf(cur, i);
cur += sizeof(uint16_t);
return true;
}
bool
llarp_buffer_t::put_uint32(uint32_t i)
{
if(size_left() < sizeof(uint32_t))
return false;
htobe32buf(cur, i);
cur += sizeof(uint32_t);
return true;
}
bool
llarp_buffer_t::read_uint16(uint16_t& i)
{
if(size_left() < sizeof(uint16_t))
return false;
i = bufbe16toh(cur);
cur += sizeof(uint16_t);
return true;
}
bool
llarp_buffer_t::read_uint32(uint32_t& i)
{
if(size_left() < sizeof(uint32_t))
return false;
i = bufbe32toh(cur);
cur += sizeof(uint32_t);
return true;
}
size_t
llarp_buffer_read_until(llarp_buffer_t* buff, char delim, byte_t* result,
size_t resultsize)
llarp_buffer_t::read_until(char delim, byte_t* result, size_t resultsize)
{
size_t read = 0;
// do the bound check first, to avoid over running
while((buff->cur != buff->base + buff->sz) && *buff->cur != delim
&& resultsize)
while((cur != base + sz) && *cur != delim && resultsize)
{
*result = *buff->cur;
buff->cur++;
*result = *cur;
cur++;
result++;
resultsize--;
read++;
}
if(llarp_buffer_size_left(*buff))
if(size_left())
return read;
else
return 0;
}
bool
llarp_buffer_eq(const llarp_buffer_t& buf, const char* str)
operator==(const llarp_buffer_t& buff, const char* str)
{
ManagedBuffer copy{buf};
ManagedBuffer copy{buff};
while(*str
&& copy.underlying.cur != (copy.underlying.base + copy.underlying.sz))
{
@ -81,43 +108,3 @@ llarp_buffer_eq(const llarp_buffer_t& buf, const char* str)
}
return *str == 0;
}
bool
llarp_buffer_put_uint16(llarp_buffer_t* buf, uint16_t i)
{
if(llarp_buffer_size_left(*buf) < sizeof(uint16_t))
return false;
htobe16buf(buf->cur, i);
buf->cur += sizeof(uint16_t);
return true;
}
bool
llarp_buffer_put_uint32(llarp_buffer_t* buf, uint32_t i)
{
if(llarp_buffer_size_left(*buf) < sizeof(uint32_t))
return false;
htobe32buf(buf->cur, i);
buf->cur += sizeof(uint32_t);
return true;
}
bool
llarp_buffer_read_uint16(llarp_buffer_t* buf, uint16_t* i)
{
if(llarp_buffer_size_left(*buf) < sizeof(uint16_t))
return false;
*i = bufbe16toh(buf->cur);
buf->cur += sizeof(uint16_t);
return true;
}
bool
llarp_buffer_read_uint32(llarp_buffer_t* buf, uint32_t* i)
{
if(llarp_buffer_size_left(*buf) < sizeof(uint32_t))
return false;
*i = bufbe32toh(buf->cur);
buf->cur += sizeof(uint32_t);
return true;
}

@ -6,6 +6,7 @@
#include <util/types.hpp>
#include <cassert>
#include <iterator>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@ -112,12 +113,52 @@ struct llarp_buffer_t
{
}
size_t
size_left() const;
template < typename InputIt >
bool
write(InputIt begin, InputIt end);
bool
writef(const char *fmt, ...) __attribute__((format(printf, 2, 3)));
;
bool
put_uint16(uint16_t i);
bool
put_uint32(uint32_t i);
bool
read_uint16(uint16_t &i);
bool
read_uint32(uint32_t &i);
size_t
read_until(char delim, byte_t *result, size_t resultlen);
private:
friend struct ManagedBuffer;
llarp_buffer_t(const llarp_buffer_t &) = default;
llarp_buffer_t(llarp_buffer_t &&) = default;
};
bool
operator==(const llarp_buffer_t &buff, const char *data);
template < typename InputIt >
bool
llarp_buffer_t::write(InputIt begin, InputIt end)
{
auto dist = std::distance(begin, end);
if(static_cast< decltype(dist) >(size_left()) >= dist)
{
cur = std::copy(begin, end, cur);
return true;
}
return false;
}
/**
Provide a copyable/moveable wrapper around `llarp_buffer_t`.
*/
@ -140,40 +181,4 @@ struct ManagedBuffer
}
};
/// how much room is left in buffer
size_t
llarp_buffer_size_left(const llarp_buffer_t &buff);
/// write a chunk of data size "sz"
bool
llarp_buffer_write(llarp_buffer_t *buff, const void *data, size_t sz);
/// write multiple strings
bool
llarp_buffer_writef(llarp_buffer_t *buff, const char *fmt, ...);
/// read buffer upto character delimiter
size_t
llarp_buffer_read_until(llarp_buffer_t *buff, char delim, byte_t *result,
size_t resultlen);
/// compare buffers, true if equal else false
bool
llarp_buffer_eq(const llarp_buffer_t &buff, const char *data);
/// put big endian unsigned 16 bit integer
bool
llarp_buffer_put_uint16(llarp_buffer_t *buf, uint16_t i);
/// put big endian unsigned 32 bit integer
bool
llarp_buffer_put_uint32(llarp_buffer_t *buf, uint32_t i);
/// read big endian unsigned 16 bit integer
bool
llarp_buffer_read_uint16(llarp_buffer_t *buf, uint16_t *i);
/// read big endian unsigned 32 bit integer
bool
llarp_buffer_read_uint32(llarp_buffer_t *buf, uint32_t *i);
#endif

Loading…
Cancel
Save