2019-06-15 14:55:13 +00:00
|
|
|
#include <messages/link_message_parser.hpp>
|
|
|
|
|
|
|
|
#include <messages/dht_immediate.hpp>
|
|
|
|
#include <messages/discard.hpp>
|
|
|
|
#include <messages/link_intro.hpp>
|
|
|
|
#include <messages/link_message.hpp>
|
|
|
|
#include <messages/relay_commit.hpp>
|
2019-06-04 18:31:17 +00:00
|
|
|
#include <messages/relay_status.hpp>
|
2019-06-15 14:55:13 +00:00
|
|
|
#include <messages/relay.hpp>
|
|
|
|
#include <router_contact.hpp>
|
|
|
|
#include <util/buffer.hpp>
|
2019-09-01 12:10:49 +00:00
|
|
|
#include <util/logging/logger.hpp>
|
2019-06-15 14:55:13 +00:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2019-06-19 20:48:25 +00:00
|
|
|
struct LinkMessageParser::msg_holder_t
|
2019-06-15 14:55:13 +00:00
|
|
|
{
|
|
|
|
LinkIntroMessage i;
|
|
|
|
RelayDownstreamMessage d;
|
|
|
|
RelayUpstreamMessage u;
|
|
|
|
DHTImmediateMessage m;
|
|
|
|
LR_CommitMessage c;
|
2019-06-04 18:31:17 +00:00
|
|
|
LR_StatusMessage s;
|
2019-06-15 14:55:13 +00:00
|
|
|
DiscardMessage x;
|
|
|
|
|
|
|
|
msg_holder_t() = default;
|
|
|
|
};
|
|
|
|
|
2019-06-19 20:48:25 +00:00
|
|
|
LinkMessageParser::LinkMessageParser(AbstractRouter* _router)
|
2019-06-15 14:55:14 +00:00
|
|
|
: router(_router)
|
|
|
|
, from(nullptr)
|
|
|
|
, msg(nullptr)
|
|
|
|
, holder(std::make_unique< msg_holder_t >())
|
2019-06-15 14:55:13 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-07-30 23:42:13 +00:00
|
|
|
LinkMessageParser::~LinkMessageParser() = default;
|
2019-06-15 14:55:13 +00:00
|
|
|
|
|
|
|
bool
|
2019-06-19 20:48:25 +00:00
|
|
|
LinkMessageParser::operator()(llarp_buffer_t* buffer, llarp_buffer_t* key)
|
2019-06-15 14:55:13 +00:00
|
|
|
{
|
|
|
|
// we are reading the first key
|
|
|
|
if(firstkey)
|
|
|
|
{
|
|
|
|
llarp_buffer_t strbuf;
|
|
|
|
// check for empty dict
|
|
|
|
if(!key)
|
|
|
|
return false;
|
|
|
|
// we are expecting the first key to be 'a'
|
|
|
|
if(!(*key == "a"))
|
|
|
|
{
|
|
|
|
llarp::LogWarn("message has no message type");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!bencode_read_string(buffer, &strbuf))
|
|
|
|
{
|
|
|
|
llarp::LogWarn("could not read value of message type");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// bad key size
|
|
|
|
if(strbuf.sz != 1)
|
|
|
|
{
|
|
|
|
llarp::LogWarn("bad mesage type size: ", strbuf.sz);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// create the message to parse based off message type
|
|
|
|
llarp::LogDebug("inbound message ", *strbuf.cur);
|
|
|
|
switch(*strbuf.cur)
|
|
|
|
{
|
|
|
|
case 'i':
|
De-abseil, part 2: mutex, locks, (most) time
- util::Mutex is now a std::shared_timed_mutex, which is capable of
exclusive and shared locks.
- util::Lock is still present as a std::lock_guard<util::Mutex>.
- the locking annotations are preserved, but updated to the latest
supported by clang rather than using abseil's older/deprecated ones.
- ACQUIRE_LOCK macro is gone since we don't pass mutexes by pointer into
locks anymore (WTF abseil).
- ReleasableLock is gone. Instead there are now some llarp::util helper
methods to obtain unique and/or shared locks:
- `auto lock = util::unique_lock(mutex);` gets an RAII-but-also
unlockable object (std::unique_lock<T>, with T inferred from
`mutex`).
- `auto lock = util::shared_lock(mutex);` gets an RAII shared (i.e.
"reader") lock of the mutex.
- `auto lock = util::unique_locks(mutex1, mutex2, mutex3);` can be
used to atomically lock multiple mutexes at once (returning a
tuple of the locks).
This are templated on the mutex which makes them a bit more flexible
than using a concrete type: they can be used for any type of lockable
mutex, not only util::Mutex. (Some of the code here uses them for
getting locks around a std::mutex). Until C++17, using the RAII types
is painfully verbose:
```C++
// pre-C++17 - needing to figure out the mutex type here is annoying:
std::unique_lock<util::Mutex> lock(mutex);
// pre-C++17 and even more verbose (but at least the type isn't needed):
std::unique_lock<decltype(mutex)> lock(mutex);
// our compromise:
auto lock = util::unique_lock(mutex);
// C++17:
std::unique_lock lock(mutex);
```
All of these functions will also warn (under gcc or clang) if you
discard the return value. You can also do fancy things like
`auto l = util::unique_lock(mutex, std::adopt_lock)` (which lets a
lock take over an already-locked mutex).
- metrics code is gone, which also removes a big pile of code that was
only used by metrics:
- llarp::util::Scheduler
- llarp::thread::TimerQueue
- llarp::util::Stopwatch
2020-02-21 17:21:11 +00:00
|
|
|
msg = &holder->i;
|
2019-06-15 14:55:13 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
msg = &holder->d;
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
msg = &holder->u;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
msg = &holder->m;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
msg = &holder->c;
|
|
|
|
break;
|
2019-06-04 18:31:17 +00:00
|
|
|
case 's':
|
|
|
|
msg = &holder->s;
|
|
|
|
break;
|
2019-06-15 14:55:13 +00:00
|
|
|
case 'x':
|
|
|
|
msg = &holder->x;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg->session = from;
|
|
|
|
firstkey = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// check for last element
|
|
|
|
if(!key)
|
|
|
|
return MessageDone();
|
|
|
|
|
|
|
|
return msg->DecodeKey(*key, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-06-19 20:48:25 +00:00
|
|
|
LinkMessageParser::MessageDone()
|
2019-06-15 14:55:13 +00:00
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
if(msg)
|
|
|
|
{
|
|
|
|
result = msg->HandleMessage(router);
|
|
|
|
}
|
|
|
|
Reset();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-06-19 20:48:25 +00:00
|
|
|
LinkMessageParser::ProcessFrom(ILinkSession* src, const llarp_buffer_t& buf)
|
2019-06-15 14:55:13 +00:00
|
|
|
{
|
|
|
|
if(!src)
|
|
|
|
{
|
|
|
|
llarp::LogWarn("no link session");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
from = src;
|
|
|
|
firstkey = true;
|
|
|
|
ManagedBuffer copy(buf);
|
|
|
|
return bencode_read_dict(*this, ©.underlying);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-06-19 20:48:25 +00:00
|
|
|
LinkMessageParser::Reset()
|
2019-06-15 14:55:13 +00:00
|
|
|
{
|
|
|
|
if(msg)
|
|
|
|
msg->Clear();
|
|
|
|
msg = nullptr;
|
|
|
|
}
|
|
|
|
} // namespace llarp
|