30 #ifndef ELECTRONEUM_TRANSPORT_H
31 #define ELECTRONEUM_TRANSPORT_H
34 #include <boost/asio.hpp>
35 #include <boost/asio/deadline_timer.hpp>
36 #include <boost/array.hpp>
37 #include <boost/utility/string_ref.hpp>
40 #include <type_traits>
41 #include "net/http_client.h"
43 #include "rapidjson/document.h"
44 #include "rapidjson/writer.h"
45 #include "rapidjson/stringbuffer.h"
51 #include "messages/messages.pb.h"
52 #include "messages/messages-common.pb.h"
53 #include "messages/messages-management.pb.h"
54 #include "messages/messages-electroneum.pb.h"
59 using json = rapidjson::Document;
61 namespace http = epee::net_utils::http;
65 uint64_t
pack_version(uint32_t major, uint32_t minor=0, uint32_t patch=0);
68 bool t_serialize(
const std::string & in, std::string & out);
72 bool t_deserialize(
const std::string & in, std::string & out);
76 template<
class t_req,
class t_res,
class t_transport>
77 bool invoke_bridge_http(
const boost::string_ref uri,
const t_req & out_struct, t_res & result_struct, t_transport&
transport,
const boost::string_ref method =
"POST", std::chrono::milliseconds timeout = std::chrono::seconds(180))
79 std::string req_param;
82 http::fields_list additional_params;
83 additional_params.push_back(std::make_pair(
"Origin",
"https://electroneum.trezor.io"));
84 additional_params.push_back(std::make_pair(
"Content-Type",
"application/json; charset=utf-8"));
86 const http::http_response_info* pri =
nullptr;
87 if(!
transport.invoke(uri, method, req_param, timeout, &pri, std::move(additional_params)))
89 MERROR(
"Failed to invoke http request to " << uri);
95 MERROR(
"Failed to invoke http request to " << uri <<
", internal error (null response ptr)");
99 if(pri->m_response_code != 200)
101 MERROR(
"Failed to invoke http request to " << uri <<
", wrong response code: " << pri->m_response_code
102 <<
" Response Body: " << pri->m_body);
121 virtual void read(
Transport &
transport, std::shared_ptr<google::protobuf::Message> & msg, messages::MessageType * msg_type=
nullptr)= 0;
130 void read(
Transport &
transport, std::shared_ptr<google::protobuf::Message> & msg, messages::MessageType * msg_type=
nullptr)
override;
142 virtual bool ping() {
return false; };
143 virtual std::string
get_path()
const {
return ""; };
147 virtual void write(
const google::protobuf::Message & req) =0;
148 virtual void read(std::shared_ptr<google::protobuf::Message> & msg, messages::MessageType * msg_type=
nullptr) =0;
149 virtual std::shared_ptr<Transport>
find_debug() {
return nullptr; };
152 virtual size_t read_chunk(
void * buff,
size_t size) {
return 0; };
153 virtual std::ostream&
dump(std::ostream& o)
const {
return o <<
"Transport<>"; }
165 boost::optional<std::string> device_path = boost::none,
166 boost::optional<std::string> bridge_host = boost::none);
172 std::string
get_path()
const override;
175 void open()
override;
176 void close()
override;
178 void write(
const google::protobuf::Message &req)
override;
179 void read(std::shared_ptr<google::protobuf::Message> & msg, messages::MessageType * msg_type=
nullptr)
override;
182 std::ostream&
dump(std::ostream& o)
const override;
194 using boost::asio::ip::udp;
200 boost::optional<std::string> device_path=boost::none,
201 boost::optional<std::shared_ptr<Protocol>> proto=boost::none);
209 bool ping()
override;
210 std::string
get_path()
const override;
213 void open()
override;
214 void close()
override;
215 std::shared_ptr<Transport>
find_debug()
override;
217 void write(
const google::protobuf::Message &req)
override;
218 void read(std::shared_ptr<google::protobuf::Message> & msg, messages::MessageType * msg_type=
nullptr)
override;
220 void write_chunk(
const void * buff,
size_t size)
override;
221 size_t read_chunk(
void * buff,
size_t size)
override;
223 std::ostream&
dump(std::ostream& o)
const override;
227 ssize_t
receive(
void * buff,
size_t size, boost::system::error_code * error_code=
nullptr,
bool no_throw=
false, boost::posix_time::time_duration timeout=boost::posix_time::seconds(10));
229 static void handle_receive(
const boost::system::error_code& ec, std::size_t length,
230 boost::system::error_code* out_ec, std::size_t* out_length);
231 bool ping_int(boost::posix_time::time_duration timeout=boost::posix_time::milliseconds(1500));
243 #ifdef WITH_DEVICE_TREZOR_WEBUSB
246 class WebUsbTransport :
public Transport {
249 explicit WebUsbTransport(
250 boost::optional<libusb_device_descriptor*> descriptor = boost::none,
251 boost::optional<std::shared_ptr<Protocol>> proto = boost::none
254 virtual ~WebUsbTransport();
256 static const char * PATH_PREFIX;
258 std::string get_path()
const override;
261 void open()
override;
262 void close()
override;
263 std::shared_ptr<Transport> find_debug()
override;
265 void write(
const google::protobuf::Message &req)
override;
266 void read(std::shared_ptr<google::protobuf::Message> & msg, messages::MessageType * msg_type=
nullptr)
override;
268 void write_chunk(
const void * buff,
size_t size)
override;
269 size_t read_chunk(
void * buff,
size_t size)
override;
271 std::ostream& dump(std::ostream& o)
const override;
274 void require_device()
const;
275 void require_connected()
const;
276 int get_interface()
const;
277 unsigned char get_endpoint()
const;
279 std::shared_ptr<Protocol> m_proto;
281 libusb_context *m_usb_session;
282 libusb_device *m_usb_device;
283 libusb_device_handle *m_usb_device_handle;
284 std::unique_ptr<libusb_device_descriptor> m_usb_device_desc;
285 std::vector<uint8_t> m_port_numbers;
289 #ifdef WITH_TREZOR_DEBUGGING
313 std::shared_ptr<Transport>
transport(
const std::string & path);
318 template<
class t_transport=Transport>
325 return std::dynamic_pointer_cast<t_transport>(t);
333 std::shared_ptr<google::protobuf::Message>
recvMsg;
339 const std::shared_ptr<google::protobuf::Message> &
recvMsg)
341 reason = std::string(
"Trezor returned unexpected message: ") + std::to_string(
recvType);
360 hw::trezor::messages::MessageType
m_type;
361 std::shared_ptr<google::protobuf::Message>
m_msg;
371 template<
class t_message=google::protobuf::Message>
372 std::shared_ptr<t_message>
374 boost::optional<messages::MessageType> resp_type = boost::none)
377 BOOST_STATIC_ASSERT(boost::is_base_of<google::protobuf::Message, t_message>::value);
383 std::shared_ptr<google::protobuf::Message> msg_resp;
384 hw::trezor::messages::MessageType msg_resp_type;
385 transport.read(msg_resp, &msg_resp_type);
388 messages::MessageType required_type = resp_type ? resp_type.get() : MessageMapper::get_message_wire_number<t_message>();
390 if (msg_resp_type == required_type) {
391 return message_ptr_retype<t_message>(msg_resp);
392 }
else if (msg_resp_type == messages::MessageType_Failure){
400 std::ostream&
operator<<(std::ostream& o, std::shared_ptr<hw::trezor::Transport>
const& t);
static void close()
Definition: blockchain_blackball.cpp:284
static void open(MDB_env *&env, const boost::filesystem::path &path, uint64_t db_flags, bool readonly)
Definition: blockchain_prune.cpp:64
Definition: transport.hpp:162
std::string m_bridge_host
Definition: transport.hpp:186
virtual ~BridgeTransport()=default
static const char * PATH_PREFIX
Definition: transport.hpp:170
void close() override
Definition: transport.cpp:404
void write(const google::protobuf::Message &req) override
Definition: transport.cpp:425
BridgeTransport(boost::optional< std::string > device_path=boost::none, boost::optional< std::string > bridge_host=boost::none)
Definition: transport.cpp:315
boost::optional< std::string > m_response
Definition: transport.hpp:189
boost::optional< std::string > m_session
Definition: transport.hpp:188
const boost::optional< json > & device_info() const
Definition: transport.cpp:476
boost::optional< json > m_device_info
Definition: transport.hpp:190
boost::optional< std::string > m_device_path
Definition: transport.hpp:187
std::string get_path() const override
Definition: transport.cpp:338
void enumerate(t_transport_vect &res) override
Definition: transport.cpp:347
void read(std::shared_ptr< google::protobuf::Message > &msg, messages::MessageType *msg_type=nullptr) override
Definition: transport.cpp:448
std::ostream & dump(std::ostream &o) const override
Definition: transport.cpp:480
epee::net_utils::http::http_simple_client m_http_client
Definition: transport.hpp:185
void open() override
Definition: transport.cpp:383
Definition: transport.hpp:354
bool m_empty
Definition: transport.hpp:362
std::shared_ptr< google::protobuf::Message > m_msg
Definition: transport.hpp:361
GenericMessage()
Definition: transport.hpp:356
bool empty() const
Definition: transport.hpp:358
GenericMessage(messages::MessageType m_type, const std::shared_ptr< google::protobuf::Message > &m_msg)
hw::trezor::messages::MessageType m_type
Definition: transport.hpp:360
Definition: transport.hpp:124
void write(Transport &transport, const google::protobuf::Message &req) override
Definition: transport.cpp:192
void read(Transport &transport, std::shared_ptr< google::protobuf::Message > &msg, messages::MessageType *msg_type=nullptr) override
Definition: transport.cpp:223
virtual ~ProtocolV1()=default
Definition: transport.hpp:114
virtual void session_end(Transport &transport)
Definition: transport.hpp:119
virtual void session_begin(Transport &transport)
Definition: transport.hpp:118
virtual ~Protocol()=default
virtual void read(Transport &transport, std::shared_ptr< google::protobuf::Message > &msg, messages::MessageType *msg_type=nullptr)=0
virtual void write(Transport &transport, const google::protobuf::Message &req)=0
Definition: transport.hpp:137
virtual std::shared_ptr< Transport > find_debug()
Definition: transport.hpp:149
virtual void close()
Definition: transport.hpp:146
virtual void enumerate(t_transport_vect &res)
Definition: transport.hpp:144
virtual void read(std::shared_ptr< google::protobuf::Message > &msg, messages::MessageType *msg_type=nullptr)=0
Transport()
Definition: transport.cpp:275
virtual void write_chunk(const void *buff, size_t size)
Definition: transport.hpp:151
virtual size_t read_chunk(void *buff, size_t size)
Definition: transport.hpp:152
virtual std::ostream & dump(std::ostream &o) const
Definition: transport.hpp:153
virtual bool pre_open()
Definition: transport.cpp:279
virtual std::string get_path() const
Definition: transport.hpp:143
virtual bool pre_close()
Definition: transport.cpp:295
virtual void open()
Definition: transport.hpp:145
virtual bool ping()
Definition: transport.hpp:142
virtual void write(const google::protobuf::Message &req)=0
long m_open_counter
Definition: transport.hpp:155
virtual ~Transport()=default
Definition: transport.hpp:196
int m_device_port
Definition: transport.hpp:235
boost::asio::io_service m_io_service
Definition: transport.hpp:238
bool ping() override
Definition: transport.cpp:546
void write(const google::protobuf::Message &req) override
Definition: transport.cpp:725
std::shared_ptr< Protocol > m_proto
Definition: transport.hpp:233
udp::endpoint m_endpoint
Definition: transport.hpp:240
static const char * PATH_PREFIX
Definition: transport.hpp:205
void enumerate(t_transport_vect &res) override
Definition: transport.cpp:566
std::ostream & dump(std::ostream &o) const override
Definition: transport.cpp:766
void check_deadline()
Definition: transport.cpp:733
void open() override
Definition: transport.cpp:582
void write_chunk(const void *buff, size_t size) override
Definition: transport.cpp:629
static const char * DEFAULT_HOST
Definition: transport.hpp:206
size_t read_chunk(void *buff, size_t size) override
Definition: transport.cpp:642
void require_socket()
Definition: transport.cpp:540
bool ping_int(boost::posix_time::time_duration timeout=boost::posix_time::milliseconds(1500))
Definition: transport.cpp:550
void close() override
Definition: transport.cpp:601
virtual ~UdpTransport()=default
std::string m_device_host
Definition: transport.hpp:234
std::unique_ptr< udp::socket > m_socket
Definition: transport.hpp:237
static const int DEFAULT_PORT
Definition: transport.hpp:207
ssize_t receive(void *buff, size_t size, boost::system::error_code *error_code=nullptr, bool no_throw=false, boost::posix_time::time_duration timeout=boost::posix_time::seconds(10))
Definition: transport.cpp:676
std::string get_path() const override
Definition: transport.cpp:535
UdpTransport(boost::optional< std::string > device_path=boost::none, boost::optional< std::shared_ptr< Protocol >> proto=boost::none)
Definition: transport.cpp:510
std::shared_ptr< Transport > find_debug() override
Definition: transport.cpp:616
boost::asio::deadline_timer m_deadline
Definition: transport.hpp:239
static void handle_receive(const boost::system::error_code &ec, std::size_t length, boost::system::error_code *out_ec, std::size_t *out_length)
Definition: transport.cpp:760
void read(std::shared_ptr< google::protobuf::Message > &msg, messages::MessageType *msg_type=nullptr) override
Definition: transport.cpp:729
Definition: exceptions.hpp:115
ProtocolException()
Definition: exceptions.hpp:118
boost::optional< std::string > reason
Definition: exceptions.hpp:62
Definition: transport.hpp:330
UnexpectedMessageException()
Definition: transport.hpp:337
hw::trezor::messages::MessageType recvType
Definition: transport.hpp:332
std::shared_ptr< google::protobuf::Message > recvMsg
Definition: transport.hpp:333
UnexpectedMessageException(hw::trezor::messages::MessageType recvType, const std::shared_ptr< google::protobuf::Message > &recvMsg)
Definition: transport.hpp:338
void sort_transports_by_env(t_transport_vect &res)
Definition: transport.cpp:1171
std::shared_ptr< t_message > exchange_message(Transport &transport, const google::protobuf::Message &req, boost::optional< messages::MessageType > resp_type=boost::none)
Definition: transport.hpp:373
std::ostream & operator<<(std::ostream &o, hw::trezor::Transport const &t)
Definition: transport.cpp:1251
rapidjson::Document json
Definition: transport.hpp:59
void throw_failure_exception(const messages::common::Failure *failure)
Definition: transport.cpp:1217
bool t_serialize(const std::string &in, std::string &out)
Definition: transport.cpp:55
bool t_deserialize(const std::string &in, std::string &out)
Definition: transport.cpp:74
const std::string DEFAULT_BRIDGE
Definition: transport.hpp:63
rapidjson::Value json_val
Definition: transport.hpp:60
std::shared_ptr< Transport > transport(const std::string &path)
Definition: transport.cpp:1204
uint64_t pack_version(uint32_t major, uint32_t minor, uint32_t patch)
Definition: transport.cpp:90
bool invoke_bridge_http(const boost::string_ref uri, const t_req &out_struct, t_res &result_struct, t_transport &transport, const boost::string_ref method="POST", std::chrono::milliseconds timeout=std::chrono::seconds(180))
Definition: transport.hpp:77
std::shared_ptr< t_transport > transport_typed(const std::string &path)
Definition: transport.hpp:319
std::vector< std::shared_ptr< Transport > > t_transport_vect
Definition: transport.hpp:135
void enumerate(t_transport_vect &res)
Definition: transport.cpp:1144
Definition: device.cpp:38
#define true
Definition: stdbool.h:37