Electroneum
hw::trezor::UdpTransport Class Reference

#include <transport.hpp>

Inheritance diagram for hw::trezor::UdpTransport:
Collaboration diagram for hw::trezor::UdpTransport:

Public Member Functions

 UdpTransport (boost::optional< std::string > device_path=boost::none, boost::optional< std::shared_ptr< Protocol >> proto=boost::none)
 
virtual ~UdpTransport ()=default
 
bool ping () override
 
std::string get_path () const override
 
void enumerate (t_transport_vect &res) override
 
void open () override
 
void close () override
 
std::shared_ptr< Transportfind_debug () override
 
void write (const google::protobuf::Message &req) override
 
void read (std::shared_ptr< google::protobuf::Message > &msg, messages::MessageType *msg_type=nullptr) override
 
void write_chunk (const void *buff, size_t size) override
 
size_t read_chunk (void *buff, size_t size) override
 
std::ostream & dump (std::ostream &o) const override
 
- Public Member Functions inherited from hw::trezor::Transport
 Transport ()
 
virtual ~Transport ()=default
 

Static Public Attributes

static const char * PATH_PREFIX = "udp:"
 
static const char * DEFAULT_HOST = "127.0.0.1"
 
static const int DEFAULT_PORT = 21324
 

Additional Inherited Members

- Protected Member Functions inherited from hw::trezor::Transport
virtual bool pre_open ()
 
virtual bool pre_close ()
 
- Protected Attributes inherited from hw::trezor::Transport
long m_open_counter
 

Detailed Description

Definition at line 196 of file transport.hpp.

Constructor & Destructor Documentation

◆ UdpTransport()

hw::trezor::UdpTransport::UdpTransport ( boost::optional< std::string >  device_path = boost::none,
boost::optional< std::shared_ptr< Protocol >>  proto = boost::none 
)
explicit

Definition at line 510 of file transport.cpp.

511  :
512  m_io_service(), m_deadline(m_io_service)
513  {
514  m_device_host = DEFAULT_HOST;
515  m_device_port = DEFAULT_PORT;
516  const char *env_trezor_path = nullptr;
517 
518  if (device_path) {
519  parse_udp_path(m_device_host, m_device_port, device_path.get());
520  } else if ((env_trezor_path = getenv("TREZOR_PATH")) != nullptr && boost::starts_with(env_trezor_path, UdpTransport::PATH_PREFIX)){
521  parse_udp_path(m_device_host, m_device_port, std::string(env_trezor_path));
522  MDEBUG("Applied TREZOR_PATH: " << m_device_host << ":" << m_device_port);
523  } else {
524  m_device_host = DEFAULT_HOST;
525  }
526 
527  assert_port_number((uint32_t)m_device_port);
528  if (m_device_host != "localhost" && m_device_host != DEFAULT_HOST){
529  throw std::invalid_argument("Local endpoint allowed only");
530  }
531 
532  m_proto = proto ? proto.get() : std::make_shared<ProtocolV1>();
533  }
::std::string string
Definition: gtest-port.h:1097
#define MDEBUG(x)
Definition: misc_log_ex.h:76
unsigned int uint32_t
Definition: stdint.h:126
static const int DEFAULT_PORT
Definition: transport.hpp:207
static const char * PATH_PREFIX
Definition: transport.hpp:205
static const char * DEFAULT_HOST
Definition: transport.hpp:206

◆ ~UdpTransport()

virtual hw::trezor::UdpTransport::~UdpTransport ( )
virtualdefault

Member Function Documentation

◆ close()

void hw::trezor::UdpTransport::close ( )
overridevirtual

Reimplemented from hw::trezor::Transport.

Definition at line 601 of file transport.cpp.

601  {
602  if (!pre_close()){
603  return;
604  }
605 
606  MTRACE("Closing Trezor:UdpTransport");
607  if (!m_socket) {
608  throw exc::CommunicationException("Socket is already closed");
609  }
610 
611  m_proto->session_end(*this);
612  m_socket->close();
613  m_socket = nullptr;
614  }
#define MTRACE(x)
Definition: misc_log_ex.h:77
virtual bool pre_close()
Definition: transport.cpp:295
Here is the call graph for this function:

◆ dump()

std::ostream & hw::trezor::UdpTransport::dump ( std::ostream &  o) const
overridevirtual

Reimplemented from hw::trezor::Transport.

Definition at line 766 of file transport.cpp.

766  {
767  return o << "UdpTransport<path=" << get_path()
768  << ", socket_alive=" << (m_socket ? "true" : "false")
769  << ">";
770  }
std::string get_path() const override
Definition: transport.cpp:535
Here is the call graph for this function:

◆ enumerate()

void hw::trezor::UdpTransport::enumerate ( t_transport_vect res)
overridevirtual

Reimplemented from hw::trezor::Transport.

Definition at line 566 of file transport.cpp.

566  {
567  std::shared_ptr<UdpTransport> t = std::make_shared<UdpTransport>();
568  bool t_works = false;
569 
570  try{
571  t->open();
572  t_works = t->ping();
573  } catch(...) {
574 
575  }
576  t->close();
577  if (t_works){
578  res.push_back(t);
579  }
580  }
const char * res
Definition: hmac_keccak.cpp:41
Here is the caller graph for this function:

◆ find_debug()

std::shared_ptr< Transport > hw::trezor::UdpTransport::find_debug ( )
overridevirtual

Reimplemented from hw::trezor::Transport.

Definition at line 616 of file transport.cpp.

616  {
617 #ifdef WITH_TREZOR_DEBUGGING
618  std::shared_ptr<UdpTransport> t = std::make_shared<UdpTransport>();
619  t->m_proto = std::make_shared<ProtocolV1>();
620  t->m_device_host = m_device_host;
621  t->m_device_port = m_device_port + 1;
622  return t;
623 #else
624  MINFO("Debug link is disabled in production");
625  return nullptr;
626 #endif
627  }
#define MINFO(x)
Definition: misc_log_ex.h:75

◆ get_path()

std::string hw::trezor::UdpTransport::get_path ( ) const
overridevirtual

Reimplemented from hw::trezor::Transport.

Definition at line 535 of file transport.cpp.

535  {
536  std::string path(PATH_PREFIX);
537  return path + m_device_host + ":" + std::to_string(m_device_port);
538  }
::std::string string
Definition: gtest-port.h:1097
std::string to_string(t_connection_type type)
static const char * PATH_PREFIX
Definition: transport.hpp:205
Here is the call graph for this function:
Here is the caller graph for this function:

◆ open()

void hw::trezor::UdpTransport::open ( )
overridevirtual

Reimplemented from hw::trezor::Transport.

Definition at line 582 of file transport.cpp.

582  {
583  if (!pre_open()){
584  return;
585  }
586 
587  udp::resolver resolver(m_io_service);
588  udp::resolver::query query(udp::v4(), m_device_host, std::to_string(m_device_port));
589  m_endpoint = *resolver.resolve(query);
590 
591  m_socket.reset(new udp::socket(m_io_service));
592  m_socket->open(udp::v4());
593 
594  m_deadline.expires_at(boost::posix_time::pos_infin);
595  check_deadline();
596 
597  m_proto->session_begin(*this);
598  m_open_counter = 1;
599  }
virtual bool pre_open()
Definition: transport.cpp:279
std::unique_ptr< void, close > socket
Unique ZMQ socket handle, calls zmq_close on destruction.
Definition: zmq.h:101
std::string to_string(t_connection_type type)
Here is the call graph for this function:

◆ ping()

bool hw::trezor::UdpTransport::ping ( )
overridevirtual

Reimplemented from hw::trezor::Transport.

Definition at line 546 of file transport.cpp.

546  {
547  return ping_int();
548  }

◆ read()

void hw::trezor::UdpTransport::read ( std::shared_ptr< google::protobuf::Message > &  msg,
messages::MessageType *  msg_type = nullptr 
)
overridevirtual

Implements hw::trezor::Transport.

Definition at line 729 of file transport.cpp.

729  {
730  m_proto->read(*this, msg, msg_type);
731  }

◆ read_chunk()

size_t hw::trezor::UdpTransport::read_chunk ( void *  buff,
size_t  size 
)
overridevirtual

Reimplemented from hw::trezor::Transport.

Definition at line 642 of file transport.cpp.

642  {
643  require_socket();
644  if (size < 64){
645  throw std::invalid_argument("Buffer too small");
646  }
647 
648  ssize_t len;
649  while(true) {
650  try {
651  boost::system::error_code ec;
652  len = receive(buff, size, &ec, true);
653  if (ec == boost::asio::error::operation_aborted) {
654  continue;
655  } else if (ec) {
656  throw exc::CommunicationException(std::string("Comm error: ") + ec.message());
657  }
658 
659  if (len != 64) {
660  throw exc::CommunicationException("Invalid chunk size");
661  }
662 
663  break;
664 
665  } catch(exc::CommunicationException const& e){
666  throw;
667  } catch(std::exception const& e){
668  MWARNING("Error reading chunk, reason: " << e.what());
669  throw exc::CommunicationException(std::string("Chunk read error: ") + std::string(e.what()));
670  }
671  }
672 
673  return static_cast<size_t>(len);
674  }
::std::string string
Definition: gtest-port.h:1097
expect< std::string > receive(void *const socket, const int flags)
Definition: zmq.cpp:175
#define MWARNING(x)
Definition: misc_log_ex.h:74

◆ write()

void hw::trezor::UdpTransport::write ( const google::protobuf::Message &  req)
overridevirtual

Implements hw::trezor::Transport.

Definition at line 725 of file transport.cpp.

725  {
726  m_proto->write(*this, req);
727  }

◆ write_chunk()

void hw::trezor::UdpTransport::write_chunk ( const void *  buff,
size_t  size 
)
overridevirtual

Reimplemented from hw::trezor::Transport.

Definition at line 629 of file transport.cpp.

629  {
630  require_socket();
631 
632  if (size != 64){
633  throw exc::CommunicationException("Invalid chunk size");
634  }
635 
636  auto written = m_socket->send_to(boost::asio::buffer(buff, size), m_endpoint);
637  if (size != written){
638  throw exc::CommunicationException("Could not send the whole chunk");
639  }
640  }

Member Data Documentation

◆ DEFAULT_HOST

const char * hw::trezor::UdpTransport::DEFAULT_HOST = "127.0.0.1"
static

Definition at line 206 of file transport.hpp.

◆ DEFAULT_PORT

const int hw::trezor::UdpTransport::DEFAULT_PORT = 21324
static

Definition at line 207 of file transport.hpp.

◆ PATH_PREFIX

const char * hw::trezor::UdpTransport::PATH_PREFIX = "udp:"
static

Definition at line 205 of file transport.hpp.


The documentation for this class was generated from the following files: