2016-11-25 21:53:55 +00:00
|
|
|
# This file is part of the TREZOR project.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2012-2016 Marek Palatinus <slush@satoshilabs.com>
|
|
|
|
# Copyright (C) 2012-2016 Pavol Rusnak <stick@satoshilabs.com>
|
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
# along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-08-24 12:29:27 +00:00
|
|
|
from __future__ import absolute_import
|
2016-04-30 00:37:18 +00:00
|
|
|
|
|
|
|
import socket
|
|
|
|
|
2017-08-24 12:29:27 +00:00
|
|
|
from .protocol_v2 import ProtocolV2
|
|
|
|
from .transport import Transport
|
|
|
|
|
|
|
|
|
|
|
|
class UdpTransport(Transport):
|
|
|
|
|
|
|
|
DEFAULT_HOST = '127.0.0.1'
|
|
|
|
DEFAULT_PORT = 21324
|
|
|
|
|
|
|
|
def __init__(self, device=None, protocol=None):
|
|
|
|
super(UdpTransport, self).__init__()
|
|
|
|
|
|
|
|
if not device:
|
|
|
|
host = UdpTransport.DEFAULT_HOST
|
|
|
|
port = UdpTransport.DEFAULT_PORT
|
|
|
|
else:
|
|
|
|
host = device.split(':').get(0)
|
|
|
|
port = device.split(':').get(1, UdpTransport.DEFAULT_PORT)
|
|
|
|
port = int(port)
|
|
|
|
if not protocol:
|
|
|
|
protocol = ProtocolV2()
|
|
|
|
self.device = (host, port)
|
|
|
|
self.protocol = protocol
|
2016-04-30 00:37:18 +00:00
|
|
|
self.socket = None
|
|
|
|
|
2017-08-24 12:29:27 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.device
|
2017-07-01 15:59:11 +00:00
|
|
|
|
2017-08-24 12:29:27 +00:00
|
|
|
@staticmethod
|
|
|
|
def enumerate():
|
|
|
|
raise NotImplementedError('This transport cannot enumerate devices')
|
2017-07-01 15:59:11 +00:00
|
|
|
|
2017-08-24 12:29:27 +00:00
|
|
|
@staticmethod
|
|
|
|
def find_by_path(path=None):
|
|
|
|
return UdpTransport(path)
|
|
|
|
|
|
|
|
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
|
|
|
|
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:
|
|
|
|
raise Exception('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:
|
|
|
|
raise Exception('Unexpected chunk size: %d' % len(chunk))
|
|
|
|
return bytearray(chunk)
|