-
Notifications
You must be signed in to change notification settings - Fork 240
/
channel.py
283 lines (233 loc) · 9.1 KB
/
channel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# channel.py
#
# Support for a message passing channel that can send bytes or pickled
# Python objects on a stream. Compatible with the Connection class in the
# multiprocessing module, but rewritten for a purely asynchronous runtime.
__all__ = ['Channel']
# -- Standard Library
import os
import pickle
import struct
import hmac
import multiprocessing.connection as mpc
import logging
log = logging.getLogger(__name__)
# -- Curio
from . import socket
from .errors import CurioError, TaskTimeout
from .io import StreamBase, FileStream
from . import thread
from .time import timeout_after, sleep
# Authentication parameters (copied from multiprocessing)
AUTH_MESSAGE_LENGTH = mpc.MESSAGE_LENGTH # 20
CHALLENGE = mpc.CHALLENGE # b'#CHALLENGE#'
WELCOME = mpc.WELCOME # b'#WELCOME#'
FAILURE = mpc.FAILURE # b'#FAILURE#'
class ConnectionError(CurioError):
pass
class AuthenticationError(ConnectionError):
pass
class Connection(object):
'''
A communication channel for sending size-prefixed messages of bytes
or pickled Python objects. Must be passed a pair of reader/writer
streams for performing the underlying communication.
'''
def __init__(self, reader, writer):
assert isinstance(reader, StreamBase) and isinstance(writer, StreamBase)
self._reader = reader
self._writer = writer
@classmethod
def from_Connection(cls, conn):
'''
Creates a channel from a multiprocessing Connection. Note: The
multiprocessing connection is detached by having its handle set to None.
This method can be used to make curio talk over Pipes as created by
multiprocessing. For example:
p1, p2 = multiprocessing.Pipe()
p1 = Connection.from_Connection(p1)
p2 = Connection.from_Connection(p2)
'''
assert isinstance(conn, mpc._ConnectionBase)
reader = FileStream(open(conn._handle, 'rb', buffering=0))
writer = FileStream(open(conn._handle, 'wb', buffering=0, closefd=False))
conn._handle = None
return cls(reader, writer)
async def __aenter__(self):
return self
async def __aexit__(self, *args):
await self.close()
def __enter__(self):
return thread.AWAIT(self.__aenter__())
def __exit__(self, *args):
return thread.AWAIT(self.__aexit__(*args))
async def close(self):
await self._reader.close()
if self._reader != self._writer:
await self._writer.close()
async def send_bytes(self, buf, offset=0, size=None):
'''
Send a buffer of bytes as a single message
'''
m = memoryview(buf)
if m.itemsize > 1:
m = memoryview(bytes(m))
n = len(m)
if offset < 0:
raise ValueError("offset is negative")
if n < offset:
raise ValueError("buffer length < offset")
if size is None:
size = n - offset
elif size < 0:
raise ValueError("size is negative")
elif offset + size > n:
raise ValueError("buffer length < offset + size")
header = struct.pack('!i', size)
if size >= 16384:
await self._writer.write(header)
await self._writer.write(m[offset:offset + size])
else:
msg = header + bytes(m[offset:offset + size])
await self._writer.write(msg)
return size
async def recv_bytes(self, maxlength=None):
'''
Receive a message of bytes as a single message.
'''
header = await self._reader.read_exactly(4)
size, = struct.unpack('!i', header)
if maxlength is not None:
if size > maxlength:
raise IOError(f'Message too large. {size} bytes > {maxlength} maxlength')
msg = await self._reader.read_exactly(size)
return msg
async def recv_bytes_into(self, buf, offset=0):
'''
Receive bytes into a writable memory buffer. The buffer must be large enough to
hold the message. The number of bytes received in the message is returned.
'''
header = await self._reader.read_exactly(4)
size, = struct.unpack('!i', header)
with memoryview(buf).cast('B') as m:
if size > (len(m) - offset):
# Message is too large to fit in allotted space
# Drain the I/O and raise an error
while size > 0:
data = await self._reader.read(size)
if not data:
break
size -= len(data)
raise IOError('Message is too large to fit')
nread = await self._reader.readinto(m[offset:offset+size])
if nread != size:
raise EOFError('Expected end of data')
return nread
async def send(self, obj):
'''
Send an arbitrary Python object. Uses pickle to serialize.
'''
await self.send_bytes(pickle.dumps(obj, pickle.HIGHEST_PROTOCOL))
async def recv(self):
'''
Receive a Python object. Uses pickle to unserialize.
'''
msg = await self.recv_bytes()
return pickle.loads(msg)
async def _deliver_challenge(self, authkey):
message = os.urandom(AUTH_MESSAGE_LENGTH)
await self.send_bytes(CHALLENGE + message)
digest = hmac.new(authkey, message, 'md5').digest()
response = await self.recv_bytes(maxlength=256)
if response == digest:
await self.send_bytes(WELCOME)
else:
await self.send_bytes(FAILURE)
raise AuthenticationError('digest received was wrong')
async def _answer_challenge(self, authkey):
message = await self.recv_bytes(maxlength=256)
assert message[:len(CHALLENGE)] == CHALLENGE, f'message = {message!r}'
message = message[len(CHALLENGE):]
digest = hmac.new(authkey, message, 'md5').digest()
await self.send_bytes(digest)
response = await self.recv_bytes(maxlength=256)
if response != WELCOME:
raise AuthenticationError('digest sent was rejected')
async def authenticate_server(self, authkey):
await self._deliver_challenge(authkey)
await self._answer_challenge(authkey)
async def authenticate_client(self, authkey):
await self._answer_challenge(authkey)
await self._deliver_challenge(authkey)
class Channel(object):
def __init__(self, address, family=socket.AF_INET, check_address=None):
self.address = address
self.family = family
self.sock = None
if check_address:
self.check_address = check_address
def __repr__(self):
return f'Channel({self.address!r}, {self.family!r})'
async def __aenter__(self):
return self
async def __aexit__(self, ty, val, tb):
await self.close()
def __getstate__(self):
return (self.address, self.family)
def __setstate__(self, state):
self.address, self.family = state
self.sock = None
def bind(self):
self.sock = socket.socket(self.family, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
self.sock.bind(self.address)
self.sock.listen(5)
self.address = self.sock.getsockname()
def check_address(self, addr):
return True
async def accept(self, *, authkey=None):
if self.sock is None:
self.bind()
while True:
client, addr = await self.sock.accept()
if not self.check_address(addr):
log.warning('Channel connection from %s rejected', addr)
await client.close()
del client
continue
client_stream = client.as_stream()
c = Connection(client_stream, client_stream)
c.address = addr
try:
async with timeout_after(1):
if authkey:
await c.authenticate_server(authkey)
break
except (TaskTimeout, AuthenticationError, EOFError):
log.warning('Channel connection from %s failed', addr, exc_info=True)
await c.close()
del c
del client_stream
del client
return c
async def connect(self, *, authkey=None):
sock = socket.socket(self.family, socket.SOCK_STREAM)
await sock.connect(self.address)
sock_stream = sock.as_stream()
c = Connection(sock_stream, sock_stream)
try:
async with timeout_after(1):
if authkey:
await c.authenticate_client(authkey)
return c
except TaskTimeout:
log.warning('Channel connection to %s timed out', self.address)
await c.close()
del c
del sock_stream
# Note: Raising an OSError.
raise TimeoutError("Connection timed out")
async def close(self):
if self.sock:
await self.sock.close()
self.sock = None