python-trezor/trezorlib/transport_socket.py

111 lines
3.3 KiB
Python
Raw Normal View History

2012-12-05 19:31:21 +00:00
'''SocketTransport implements TCP socket interface for Transport.'''
import socket
from select import select
2016-05-05 01:16:17 +00:00
from .transport import Transport
2012-12-05 19:31:21 +00:00
class SocketTransportClient(Transport):
def __init__(self, device, *args, **kwargs):
device = device.split(':')
if len(device) < 2:
device = ('0.0.0.0', int(device[0]))
else:
device = (device[0], int(device[1]))
2016-02-10 15:46:58 +00:00
self.socket = None
2012-12-05 19:31:21 +00:00
super(SocketTransportClient, self).__init__(device, *args, **kwargs)
2016-02-10 15:46:58 +00:00
2012-12-05 19:31:21 +00:00
def _open(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect(self.device)
self.filelike = self.socket.makefile()
2016-02-10 15:46:58 +00:00
2012-12-05 19:31:21 +00:00
def _close(self):
self.socket.close()
self.socket = None
self.filelike = None
2016-02-10 15:46:58 +00:00
2012-12-05 19:31:21 +00:00
def ready_to_read(self):
rlist, _, _ = select([self.socket], [], [], 0)
2016-02-10 15:46:58 +00:00
return len(rlist) > 0
2014-07-26 14:27:28 +00:00
def _write(self, msg, protobuf_msg):
2012-12-05 19:31:21 +00:00
self.socket.sendall(msg)
2016-02-10 15:46:58 +00:00
2012-12-05 19:31:21 +00:00
def _read(self):
try:
(msg_type, datalen) = self._read_headers(self.filelike)
return (msg_type, self.filelike.read(datalen))
except socket.error:
2016-05-05 01:16:17 +00:00
print("Failed to read from device")
return None
2012-12-05 19:31:21 +00:00
class SocketTransport(Transport):
def __init__(self, device, *args, **kwargs):
device = device.split(':')
if len(device) < 2:
device = ('0.0.0.0', int(device[0]))
else:
device = (device[0], int(device[1]))
self.socket = None
self.client = None
self.filelike = None
super(SocketTransport, self).__init__(device, *args, **kwargs)
2016-02-10 15:46:58 +00:00
2012-12-05 19:31:21 +00:00
def _open(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#self.socket.setblocking(0)
2016-02-10 15:46:58 +00:00
2012-12-05 19:31:21 +00:00
self.socket.bind(self.device)
2012-12-09 13:53:09 +00:00
self.socket.listen(5)
2016-02-10 15:46:58 +00:00
2012-12-05 19:31:21 +00:00
def _disconnect_client(self):
2016-05-05 01:16:17 +00:00
print("Disconnecting client")
2012-12-05 19:31:21 +00:00
if self.client != None:
self.client.close()
2012-12-05 19:31:21 +00:00
self.client = None
self.filelike = None
2016-02-10 15:46:58 +00:00
2012-12-05 19:31:21 +00:00
def _close(self):
self._disconnect_client()
self.socket.close()
self.socket = None
2016-02-10 15:46:58 +00:00
2012-12-05 19:31:21 +00:00
def ready_to_read(self):
if self.filelike:
2012-12-05 19:31:21 +00:00
# Connected
rlist, _, _ = select([self.client], [], [], 0)
return len(rlist) > 0
else:
# Waiting for connection
rlist, _, _ = select([self.socket], [], [], 0)
if len(rlist) > 0:
2012-12-09 13:53:09 +00:00
(self.client, ipaddr) = self.socket.accept()
2016-05-05 01:16:17 +00:00
print("Connected", ipaddr[0])
2013-03-10 16:52:36 +00:00
self.filelike = self.client.makefile()
2012-12-05 19:31:21 +00:00
return self.ready_to_read()
return False
2016-02-10 15:46:58 +00:00
2014-07-26 14:27:28 +00:00
def _write(self, msg, protobuf_msg):
2012-12-05 19:31:21 +00:00
if self.filelike:
# None on disconnected client
2016-02-10 15:46:58 +00:00
try:
self.filelike.write(msg)
self.filelike.flush()
except socket.error:
2016-05-05 01:16:17 +00:00
print("Socket error")
self._disconnect_client()
2016-02-10 15:46:58 +00:00
2012-12-05 19:31:21 +00:00
def _read(self):
try:
(msg_type, datalen) = self._read_headers(self.filelike)
2012-12-09 13:53:09 +00:00
return (msg_type, self.filelike.read(datalen))
2012-12-05 19:31:21 +00:00
except Exception:
2016-05-05 01:16:17 +00:00
print("Failed to read from device")
2012-12-05 19:31:21 +00:00
self._disconnect_client()
2014-07-26 14:27:28 +00:00
return None