2018-06-21 14:28:34 +00:00
|
|
|
# This file is part of the Trezor project.
|
2016-11-25 21:53:55 +00:00
|
|
|
#
|
2018-06-21 14:28:34 +00:00
|
|
|
# Copyright (C) 2012-2018 SatoshiLabs and contributors
|
2016-11-25 21:53:55 +00:00
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
2018-06-21 14:28:34 +00:00
|
|
|
# it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
# as published by the Free Software Foundation.
|
2016-11-25 21:53:55 +00:00
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
2018-06-21 14:28:34 +00:00
|
|
|
# You should have received a copy of the License along with this library.
|
|
|
|
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
|
2016-11-25 21:53:55 +00:00
|
|
|
|
2016-04-30 00:37:18 +00:00
|
|
|
import socket
|
|
|
|
|
2018-03-02 14:44:24 +00:00
|
|
|
from . import Transport, TransportException
|
2018-08-13 16:21:24 +00:00
|
|
|
from ..protocol_v1 import ProtocolV1
|
2017-08-24 12:29:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UdpTransport(Transport):
|
|
|
|
|
2018-08-13 16:21:24 +00:00
|
|
|
DEFAULT_HOST = "127.0.0.1"
|
2017-08-24 12:29:27 +00:00
|
|
|
DEFAULT_PORT = 21324
|
2018-08-13 16:21:24 +00:00
|
|
|
PATH_PREFIX = "udp"
|
2017-08-24 12:29:27 +00:00
|
|
|
|
|
|
|
def __init__(self, device=None, protocol=None):
|
|
|
|
super(UdpTransport, self).__init__()
|
|
|
|
|
|
|
|
if not device:
|
|
|
|
host = UdpTransport.DEFAULT_HOST
|
|
|
|
port = UdpTransport.DEFAULT_PORT
|
|
|
|
else:
|
2018-08-13 16:21:24 +00:00
|
|
|
devparts = device.split(":")
|
2017-11-13 21:15:09 +00:00
|
|
|
host = devparts[0]
|
|
|
|
port = int(devparts[1]) if len(devparts) > 1 else UdpTransport.DEFAULT_PORT
|
2017-08-24 12:29:27 +00:00
|
|
|
if not protocol:
|
2018-02-02 17:29:20 +00:00
|
|
|
protocol = ProtocolV1()
|
2017-08-24 12:29:27 +00:00
|
|
|
self.device = (host, port)
|
|
|
|
self.protocol = protocol
|
2016-04-30 00:37:18 +00:00
|
|
|
self.socket = None
|
|
|
|
|
2018-02-06 20:10:30 +00:00
|
|
|
def get_path(self):
|
2018-02-04 10:44:20 +00:00
|
|
|
return "%s:%s:%s" % ((self.PATH_PREFIX,) + self.device)
|
2017-07-01 15:59:11 +00:00
|
|
|
|
2018-03-02 14:44:24 +00:00
|
|
|
def find_debug(self):
|
|
|
|
host, port = self.device
|
2018-08-13 16:21:24 +00:00
|
|
|
return UdpTransport("{}:{}".format(host, port + 1), self.protocol)
|
2018-03-02 14:44:24 +00:00
|
|
|
|
2018-03-02 17:22:33 +00:00
|
|
|
@classmethod
|
|
|
|
def _try_path(cls, path):
|
|
|
|
d = cls(path)
|
|
|
|
try:
|
|
|
|
d.open()
|
|
|
|
if d._ping():
|
|
|
|
return d
|
|
|
|
else:
|
2018-08-13 16:21:24 +00:00
|
|
|
raise TransportException(
|
|
|
|
"No TREZOR device found at address {}".format(path)
|
|
|
|
)
|
2018-03-02 17:22:33 +00:00
|
|
|
finally:
|
|
|
|
d.close()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def enumerate(cls):
|
2018-08-13 16:21:24 +00:00
|
|
|
default_path = "{}:{}".format(cls.DEFAULT_HOST, cls.DEFAULT_PORT)
|
2018-03-02 17:22:33 +00:00
|
|
|
try:
|
|
|
|
return [cls._try_path(default_path)]
|
|
|
|
except TransportException:
|
|
|
|
return []
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def find_by_path(cls, path, prefix_search=False):
|
|
|
|
if prefix_search:
|
|
|
|
return super().find_by_path(path, prefix_search)
|
|
|
|
else:
|
2018-08-13 16:21:24 +00:00
|
|
|
path = path.replace("{}:".format(cls.PATH_PREFIX), "")
|
2018-03-02 17:22:33 +00:00
|
|
|
return cls._try_path(path)
|
2018-02-02 17:29:20 +00:00
|
|
|
|
2017-08-24 12:29:27 +00:00
|
|
|
def open(self):
|
2016-04-30 00:37:18 +00:00
|
|
|
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
self.socket.connect(self.device)
|
2016-06-26 22:14:19 +00:00
|
|
|
self.socket.settimeout(10)
|
2017-08-24 12:29:27 +00:00
|
|
|
self.protocol.session_begin(self)
|
2016-04-30 00:37:18 +00:00
|
|
|
|
2017-08-24 12:29:27 +00:00
|
|
|
def close(self):
|
|
|
|
if self.socket:
|
|
|
|
self.protocol.session_end(self)
|
|
|
|
self.socket.close()
|
|
|
|
self.socket = None
|
2016-04-30 00:37:18 +00:00
|
|
|
|
2018-02-02 17:29:20 +00:00
|
|
|
def _ping(self):
|
2018-08-13 16:21:24 +00:00
|
|
|
"""Test if the device is listening."""
|
2018-02-02 17:29:20 +00:00
|
|
|
resp = None
|
|
|
|
try:
|
2018-08-13 16:21:24 +00:00
|
|
|
self.socket.sendall(b"PINGPING")
|
2018-02-02 17:29:20 +00:00
|
|
|
resp = self.socket.recv(8)
|
2018-08-10 14:05:14 +00:00
|
|
|
except Exception:
|
2018-02-02 17:29:20 +00:00
|
|
|
pass
|
2018-08-13 16:21:24 +00:00
|
|
|
return resp == b"PONGPONG"
|
2018-02-02 17:29:20 +00:00
|
|
|
|
2017-08-24 12:29:27 +00:00
|
|
|
def read(self):
|
|
|
|
return self.protocol.read(self)
|
2016-04-30 00:37:18 +00:00
|
|
|
|
2017-08-24 12:29:27 +00:00
|
|
|
def write(self, msg):
|
|
|
|
return self.protocol.write(self, msg)
|
2016-06-26 19:29:29 +00:00
|
|
|
|
2017-08-24 12:29:27 +00:00
|
|
|
def write_chunk(self, chunk):
|
|
|
|
if len(chunk) != 64:
|
2018-08-13 16:21:24 +00:00
|
|
|
raise TransportException("Unexpected data length")
|
2016-06-26 19:29:29 +00:00
|
|
|
self.socket.sendall(chunk)
|
|
|
|
|
2017-08-24 12:29:27 +00:00
|
|
|
def read_chunk(self):
|
2016-07-14 13:57:05 +00:00
|
|
|
while True:
|
|
|
|
try:
|
2017-08-24 12:29:27 +00:00
|
|
|
chunk = self.socket.recv(64)
|
2016-07-14 13:57:05 +00:00
|
|
|
break
|
|
|
|
except socket.timeout:
|
|
|
|
continue
|
2017-08-24 12:29:27 +00:00
|
|
|
if len(chunk) != 64:
|
2018-08-13 16:21:24 +00:00
|
|
|
raise TransportException("Unexpected chunk size: %d" % len(chunk))
|
2017-08-24 12:29:27 +00:00
|
|
|
return bytearray(chunk)
|
2018-05-24 17:14:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
TRANSPORT = UdpTransport
|