Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

seek for version and set it before deserializing #897

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions llarp/messages/dht_immediate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace llarp
DHTImmediateMessage::Clear()
{
msgs.clear();
version = 0;
}

bool
Expand Down
2 changes: 2 additions & 0 deletions llarp/messages/discard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace llarp
void
Clear() override
{
version = 0;
}

const char*
Expand Down Expand Up @@ -68,6 +69,7 @@ namespace llarp
void
Clear() override
{
version = 0;
}

bool
Expand Down
1 change: 1 addition & 0 deletions llarp/messages/link_intro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ namespace llarp
N.Zero();
rc.Clear();
Z.Zero();
version = 0;
}

bool
Expand Down
8 changes: 8 additions & 0 deletions llarp/messages/link_message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ namespace llarp
bool
BDecode(llarp_buffer_t* buf)
{
// default version if not specified is 0
uint64_t v = 0;
// seek for version and set it if we got it
if(BEncodeSeekDictVersion(v, buf, 'v'))
{
version = v;
}
// when we hit the code path version is set and we can tell how to decode
return bencode_decode_dict(*this, buf);
}

Expand Down
2 changes: 2 additions & 0 deletions llarp/messages/relay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace llarp
pathid.Zero();
X.Clear();
Y.Zero();
version = 0;
}

bool
Expand Down Expand Up @@ -67,6 +68,7 @@ namespace llarp
pathid.Zero();
X.Clear();
Y.Zero();
version = 0;
}

bool
Expand Down
1 change: 1 addition & 0 deletions llarp/messages/relay_commit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace llarp
LR_CommitMessage::Clear()
{
std::for_each(frames.begin(), frames.end(), [](auto& f) { f.Clear(); });
version = 0;
}

bool
Expand Down
1 change: 1 addition & 0 deletions llarp/messages/relay_status.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ namespace llarp
LR_StatusMessage::Clear()
{
std::for_each(frames.begin(), frames.end(), [](auto& f) { f.Clear(); });
version = 0;
}

bool
Expand Down
1 change: 1 addition & 0 deletions llarp/routing/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <constants/proto.hpp>
#include <path/path_types.hpp>
#include <util/bencode.hpp>
#include <util/buffer.hpp>

namespace llarp
Expand Down
10 changes: 9 additions & 1 deletion llarp/routing/message_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ namespace llarp
default:
llarp::LogError("invalid routing message id: ", *strbuf.cur);
}
if(msg)
msg->version = version;
firstKey = false;
return msg != nullptr;
}
Expand All @@ -118,6 +120,11 @@ namespace llarp
firstKey = true;
ManagedBuffer copiedBuf(buf);
auto& copy = copiedBuf.underlying;
uint64_t v = 0;
if(BEncodeSeekDictVersion(v, &copy, 'V'))
{
version = v;
}
if(bencode_read_dict(*this, &copy))
{
msg->from = from;
Expand All @@ -134,7 +141,8 @@ namespace llarp
}
if(msg)
msg->Clear();
msg = nullptr;
msg = nullptr;
version = 0;
return result;
}
} // namespace routing
Expand Down
2 changes: 1 addition & 1 deletion llarp/routing/message_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ namespace llarp
operator()(llarp_buffer_t* buffer, llarp_buffer_t* key);

private:
uint64_t version = 0;
bool firstKey{false};
char ourKey{'\0'};

struct MessageHolder;

IMessage* msg{nullptr};
Expand Down
1 change: 1 addition & 0 deletions llarp/routing/path_confirm_message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace llarp
{
pathLifetime = 0;
pathCreated = 0;
version = 0;
}
};
} // namespace routing
Expand Down
5 changes: 3 additions & 2 deletions llarp/routing/path_latency_message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ namespace llarp
void
Clear() override
{
T = 0;
L = 0;
T = 0;
L = 0;
version = 0;
}

bool
Expand Down
1 change: 1 addition & 0 deletions llarp/routing/path_transfer_message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace llarp
P.Zero();
T.Clear();
Y.Zero();
version = 0;
}
};

Expand Down
3 changes: 2 additions & 1 deletion llarp/routing/transfer_traffic_message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ namespace llarp
Clear() override
{
X.clear();
_size = 0;
_size = 0;
version = 0;
}

size_t
Expand Down
58 changes: 52 additions & 6 deletions llarp/util/bencode.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <util/bencode.h>
#include <util/bencode.hpp>
#include <util/logging/logger.hpp>
#include <cstdlib>
#include <cinttypes>
Expand All @@ -24,7 +24,8 @@ bencode_read_integer(struct llarp_buffer_t* buffer, uint64_t* result)
buffer->cur++;

numbuf[len] = '\0';
*result = std::strtoull(numbuf, nullptr, 10);
if(result)
*result = std::strtoull(numbuf, nullptr, 10);
return true;
}

Expand Down Expand Up @@ -53,10 +54,12 @@ bencode_read_string(llarp_buffer_t* buffer, llarp_buffer_t* result)
{
return false;
}

result->base = buffer->cur;
result->cur = buffer->cur;
result->sz = slen;
if(result)
{
result->base = buffer->cur;
result->cur = buffer->cur;
result->sz = slen;
}
buffer->cur += slen;
return true;
}
Expand Down Expand Up @@ -92,6 +95,49 @@ bencode_write_uint64(llarp_buffer_t* buff, uint64_t i)
return buff->write(std::begin(letter), std::end(letter));
}

bool
bencode_discard(llarp_buffer_t* buf)
{
if(buf->size_left() == 0)
return true;
switch(*buf->cur)
{
case 'l':
return llarp::bencode_read_list(
[](llarp_buffer_t* buffer, bool more) -> bool {
if(more)
{
return bencode_discard(buffer);
}
return true;
},
buf);
case 'i':
return bencode_read_integer(buf, nullptr);
case 'd':
return llarp::bencode_read_dict(
[](llarp_buffer_t* buffer, llarp_buffer_t* key) -> bool {
if(key)
return bencode_discard(buffer);
return true;
},
buf);
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return bencode_read_string(buf, nullptr);
default:
return false;
}
}

bool
bencode_write_version_entry(llarp_buffer_t* buff)
{
Expand Down
4 changes: 4 additions & 0 deletions llarp/util/bencode.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ bencode_start_dict(llarp_buffer_t* buff);
bool
bencode_end(llarp_buffer_t* buff);

/// read next member, discard it and advance buffer
bool
bencode_discard(llarp_buffer_t* buf);

#endif
23 changes: 23 additions & 0 deletions llarp/util/bencode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,29 @@ namespace llarp
return true;
}

/// seek for an int in a dict with a key k used for version
/// set v to parsed value if found
/// this call rewinds the buffer unconditionally before return
/// returns false only if there was
template < typename Int_t >
bool
BEncodeSeekDictVersion(Int_t& v, llarp_buffer_t* buf, const byte_t k)
{
const auto ret = bencode_read_dict(
[&v, k](llarp_buffer_t* buffer, llarp_buffer_t* key) -> bool {
if(key == nullptr)
return true;
if(key->sz == 1 && *key->cur == k)
{
return bencode_read_integer(buffer, &v);
}
return bencode_discard(buffer);
}, buf);
// rewind
buf->cur = buf->base;
return ret;
}

} // namespace llarp

#endif