connection.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. #
  2. # A higher level module for using sockets (or Windows named pipes)
  3. #
  4. # multiprocessing/connection.py
  5. #
  6. # Copyright (c) 2006-2008, R Oudkerk
  7. # Licensed to PSF under a Contributor Agreement.
  8. #
  9. __all__ = [ 'Client', 'Listener', 'Pipe', 'wait' ]
  10. import io
  11. import os
  12. import sys
  13. import socket
  14. import struct
  15. import time
  16. import tempfile
  17. import itertools
  18. import _multiprocessing
  19. from . import util
  20. from . import AuthenticationError, BufferTooShort
  21. from .context import reduction
  22. _ForkingPickler = reduction.ForkingPickler
  23. try:
  24. import _winapi
  25. from _winapi import WAIT_OBJECT_0, WAIT_ABANDONED_0, WAIT_TIMEOUT, INFINITE
  26. except ImportError:
  27. if sys.platform == 'win32':
  28. raise
  29. _winapi = None
  30. #
  31. #
  32. #
  33. BUFSIZE = 8192
  34. # A very generous timeout when it comes to local connections...
  35. CONNECTION_TIMEOUT = 20.
  36. _mmap_counter = itertools.count()
  37. default_family = 'AF_INET'
  38. families = ['AF_INET']
  39. if hasattr(socket, 'AF_UNIX'):
  40. default_family = 'AF_UNIX'
  41. families += ['AF_UNIX']
  42. if sys.platform == 'win32':
  43. default_family = 'AF_PIPE'
  44. families += ['AF_PIPE']
  45. def _init_timeout(timeout=CONNECTION_TIMEOUT):
  46. return time.monotonic() + timeout
  47. def _check_timeout(t):
  48. return time.monotonic() > t
  49. #
  50. #
  51. #
  52. def arbitrary_address(family):
  53. '''
  54. Return an arbitrary free address for the given family
  55. '''
  56. if family == 'AF_INET':
  57. return ('localhost', 0)
  58. elif family == 'AF_UNIX':
  59. return tempfile.mktemp(prefix='listener-', dir=util.get_temp_dir())
  60. elif family == 'AF_PIPE':
  61. return tempfile.mktemp(prefix=r'\\.\pipe\pyc-%d-%d-' %
  62. (os.getpid(), next(_mmap_counter)), dir="")
  63. else:
  64. raise ValueError('unrecognized family')
  65. def _validate_family(family):
  66. '''
  67. Checks if the family is valid for the current environment.
  68. '''
  69. if sys.platform != 'win32' and family == 'AF_PIPE':
  70. raise ValueError('Family %s is not recognized.' % family)
  71. if sys.platform == 'win32' and family == 'AF_UNIX':
  72. # double check
  73. if not hasattr(socket, family):
  74. raise ValueError('Family %s is not recognized.' % family)
  75. def address_type(address):
  76. '''
  77. Return the types of the address
  78. This can be 'AF_INET', 'AF_UNIX', or 'AF_PIPE'
  79. '''
  80. if type(address) == tuple:
  81. return 'AF_INET'
  82. elif type(address) is str and address.startswith('\\\\'):
  83. return 'AF_PIPE'
  84. elif type(address) is str:
  85. return 'AF_UNIX'
  86. else:
  87. raise ValueError('address type of %r unrecognized' % address)
  88. #
  89. # Connection classes
  90. #
  91. class _ConnectionBase:
  92. _handle = None
  93. def __init__(self, handle, readable=True, writable=True):
  94. handle = handle.__index__()
  95. if handle < 0:
  96. raise ValueError("invalid handle")
  97. if not readable and not writable:
  98. raise ValueError(
  99. "at least one of `readable` and `writable` must be True")
  100. self._handle = handle
  101. self._readable = readable
  102. self._writable = writable
  103. # XXX should we use util.Finalize instead of a __del__?
  104. def __del__(self):
  105. if self._handle is not None:
  106. self._close()
  107. def _check_closed(self):
  108. if self._handle is None:
  109. raise OSError("handle is closed")
  110. def _check_readable(self):
  111. if not self._readable:
  112. raise OSError("connection is write-only")
  113. def _check_writable(self):
  114. if not self._writable:
  115. raise OSError("connection is read-only")
  116. def _bad_message_length(self):
  117. if self._writable:
  118. self._readable = False
  119. else:
  120. self.close()
  121. raise OSError("bad message length")
  122. @property
  123. def closed(self):
  124. """True if the connection is closed"""
  125. return self._handle is None
  126. @property
  127. def readable(self):
  128. """True if the connection is readable"""
  129. return self._readable
  130. @property
  131. def writable(self):
  132. """True if the connection is writable"""
  133. return self._writable
  134. def fileno(self):
  135. """File descriptor or handle of the connection"""
  136. self._check_closed()
  137. return self._handle
  138. def close(self):
  139. """Close the connection"""
  140. if self._handle is not None:
  141. try:
  142. self._close()
  143. finally:
  144. self._handle = None
  145. def send_bytes(self, buf, offset=0, size=None):
  146. """Send the bytes data from a bytes-like object"""
  147. self._check_closed()
  148. self._check_writable()
  149. m = memoryview(buf)
  150. # HACK for byte-indexing of non-bytewise buffers (e.g. array.array)
  151. if m.itemsize > 1:
  152. m = memoryview(bytes(m))
  153. n = len(m)
  154. if offset < 0:
  155. raise ValueError("offset is negative")
  156. if n < offset:
  157. raise ValueError("buffer length < offset")
  158. if size is None:
  159. size = n - offset
  160. elif size < 0:
  161. raise ValueError("size is negative")
  162. elif offset + size > n:
  163. raise ValueError("buffer length < offset + size")
  164. self._send_bytes(m[offset:offset + size])
  165. def send(self, obj):
  166. """Send a (picklable) object"""
  167. self._check_closed()
  168. self._check_writable()
  169. self._send_bytes(_ForkingPickler.dumps(obj))
  170. def recv_bytes(self, maxlength=None):
  171. """
  172. Receive bytes data as a bytes object.
  173. """
  174. self._check_closed()
  175. self._check_readable()
  176. if maxlength is not None and maxlength < 0:
  177. raise ValueError("negative maxlength")
  178. buf = self._recv_bytes(maxlength)
  179. if buf is None:
  180. self._bad_message_length()
  181. return buf.getvalue()
  182. def recv_bytes_into(self, buf, offset=0):
  183. """
  184. Receive bytes data into a writeable bytes-like object.
  185. Return the number of bytes read.
  186. """
  187. self._check_closed()
  188. self._check_readable()
  189. with memoryview(buf) as m:
  190. # Get bytesize of arbitrary buffer
  191. itemsize = m.itemsize
  192. bytesize = itemsize * len(m)
  193. if offset < 0:
  194. raise ValueError("negative offset")
  195. elif offset > bytesize:
  196. raise ValueError("offset too large")
  197. result = self._recv_bytes()
  198. size = result.tell()
  199. if bytesize < offset + size:
  200. raise BufferTooShort(result.getvalue())
  201. # Message can fit in dest
  202. result.seek(0)
  203. result.readinto(m[offset // itemsize :
  204. (offset + size) // itemsize])
  205. return size
  206. def recv(self):
  207. """Receive a (picklable) object"""
  208. self._check_closed()
  209. self._check_readable()
  210. buf = self._recv_bytes()
  211. return _ForkingPickler.loads(buf.getbuffer())
  212. def poll(self, timeout=0.0):
  213. """Whether there is any input available to be read"""
  214. self._check_closed()
  215. self._check_readable()
  216. return self._poll(timeout)
  217. def __enter__(self):
  218. return self
  219. def __exit__(self, exc_type, exc_value, exc_tb):
  220. self.close()
  221. if _winapi:
  222. class PipeConnection(_ConnectionBase):
  223. """
  224. Connection class based on a Windows named pipe.
  225. Overlapped I/O is used, so the handles must have been created
  226. with FILE_FLAG_OVERLAPPED.
  227. """
  228. _got_empty_message = False
  229. def _close(self, _CloseHandle=_winapi.CloseHandle):
  230. _CloseHandle(self._handle)
  231. def _send_bytes(self, buf):
  232. ov, err = _winapi.WriteFile(self._handle, buf, overlapped=True)
  233. try:
  234. if err == _winapi.ERROR_IO_PENDING:
  235. waitres = _winapi.WaitForMultipleObjects(
  236. [ov.event], False, INFINITE)
  237. assert waitres == WAIT_OBJECT_0
  238. except:
  239. ov.cancel()
  240. raise
  241. finally:
  242. nwritten, err = ov.GetOverlappedResult(True)
  243. assert err == 0
  244. assert nwritten == len(buf)
  245. def _recv_bytes(self, maxsize=None):
  246. if self._got_empty_message:
  247. self._got_empty_message = False
  248. return io.BytesIO()
  249. else:
  250. bsize = 128 if maxsize is None else min(maxsize, 128)
  251. try:
  252. ov, err = _winapi.ReadFile(self._handle, bsize,
  253. overlapped=True)
  254. try:
  255. if err == _winapi.ERROR_IO_PENDING:
  256. waitres = _winapi.WaitForMultipleObjects(
  257. [ov.event], False, INFINITE)
  258. assert waitres == WAIT_OBJECT_0
  259. except:
  260. ov.cancel()
  261. raise
  262. finally:
  263. nread, err = ov.GetOverlappedResult(True)
  264. if err == 0:
  265. f = io.BytesIO()
  266. f.write(ov.getbuffer())
  267. return f
  268. elif err == _winapi.ERROR_MORE_DATA:
  269. return self._get_more_data(ov, maxsize)
  270. except OSError as e:
  271. if e.winerror == _winapi.ERROR_BROKEN_PIPE:
  272. raise EOFError
  273. else:
  274. raise
  275. raise RuntimeError("shouldn't get here; expected KeyboardInterrupt")
  276. def _poll(self, timeout):
  277. if (self._got_empty_message or
  278. _winapi.PeekNamedPipe(self._handle)[0] != 0):
  279. return True
  280. return bool(wait([self], timeout))
  281. def _get_more_data(self, ov, maxsize):
  282. buf = ov.getbuffer()
  283. f = io.BytesIO()
  284. f.write(buf)
  285. left = _winapi.PeekNamedPipe(self._handle)[1]
  286. assert left > 0
  287. if maxsize is not None and len(buf) + left > maxsize:
  288. self._bad_message_length()
  289. ov, err = _winapi.ReadFile(self._handle, left, overlapped=True)
  290. rbytes, err = ov.GetOverlappedResult(True)
  291. assert err == 0
  292. assert rbytes == left
  293. f.write(ov.getbuffer())
  294. return f
  295. class Connection(_ConnectionBase):
  296. """
  297. Connection class based on an arbitrary file descriptor (Unix only), or
  298. a socket handle (Windows).
  299. """
  300. if _winapi:
  301. def _close(self, _close=_multiprocessing.closesocket):
  302. _close(self._handle)
  303. _write = _multiprocessing.send
  304. _read = _multiprocessing.recv
  305. else:
  306. def _close(self, _close=os.close):
  307. _close(self._handle)
  308. _write = os.write
  309. _read = os.read
  310. def _send(self, buf, write=_write):
  311. remaining = len(buf)
  312. while True:
  313. n = write(self._handle, buf)
  314. remaining -= n
  315. if remaining == 0:
  316. break
  317. buf = buf[n:]
  318. def _recv(self, size, read=_read):
  319. buf = io.BytesIO()
  320. handle = self._handle
  321. remaining = size
  322. while remaining > 0:
  323. chunk = read(handle, remaining)
  324. n = len(chunk)
  325. if n == 0:
  326. if remaining == size:
  327. raise EOFError
  328. else:
  329. raise OSError("got end of file during message")
  330. buf.write(chunk)
  331. remaining -= n
  332. return buf
  333. def _send_bytes(self, buf):
  334. n = len(buf)
  335. # For wire compatibility with 3.2 and lower
  336. header = struct.pack("!i", n)
  337. if n > 16384:
  338. # The payload is large so Nagle's algorithm won't be triggered
  339. # and we'd better avoid the cost of concatenation.
  340. self._send(header)
  341. self._send(buf)
  342. else:
  343. # Issue #20540: concatenate before sending, to avoid delays due
  344. # to Nagle's algorithm on a TCP socket.
  345. # Also note we want to avoid sending a 0-length buffer separately,
  346. # to avoid "broken pipe" errors if the other end closed the pipe.
  347. self._send(header + buf)
  348. def _recv_bytes(self, maxsize=None):
  349. buf = self._recv(4)
  350. size, = struct.unpack("!i", buf.getvalue())
  351. if maxsize is not None and size > maxsize:
  352. return None
  353. return self._recv(size)
  354. def _poll(self, timeout):
  355. r = wait([self], timeout)
  356. return bool(r)
  357. #
  358. # Public functions
  359. #
  360. class Listener(object):
  361. '''
  362. Returns a listener object.
  363. This is a wrapper for a bound socket which is 'listening' for
  364. connections, or for a Windows named pipe.
  365. '''
  366. def __init__(self, address=None, family=None, backlog=1, authkey=None):
  367. family = family or (address and address_type(address)) \
  368. or default_family
  369. address = address or arbitrary_address(family)
  370. _validate_family(family)
  371. if family == 'AF_PIPE':
  372. self._listener = PipeListener(address, backlog)
  373. else:
  374. self._listener = SocketListener(address, family, backlog)
  375. if authkey is not None and not isinstance(authkey, bytes):
  376. raise TypeError('authkey should be a byte string')
  377. self._authkey = authkey
  378. def accept(self):
  379. '''
  380. Accept a connection on the bound socket or named pipe of `self`.
  381. Returns a `Connection` object.
  382. '''
  383. if self._listener is None:
  384. raise OSError('listener is closed')
  385. c = self._listener.accept()
  386. if self._authkey:
  387. deliver_challenge(c, self._authkey)
  388. answer_challenge(c, self._authkey)
  389. return c
  390. def close(self):
  391. '''
  392. Close the bound socket or named pipe of `self`.
  393. '''
  394. listener = self._listener
  395. if listener is not None:
  396. self._listener = None
  397. listener.close()
  398. @property
  399. def address(self):
  400. return self._listener._address
  401. @property
  402. def last_accepted(self):
  403. return self._listener._last_accepted
  404. def __enter__(self):
  405. return self
  406. def __exit__(self, exc_type, exc_value, exc_tb):
  407. self.close()
  408. def Client(address, family=None, authkey=None):
  409. '''
  410. Returns a connection to the address of a `Listener`
  411. '''
  412. family = family or address_type(address)
  413. _validate_family(family)
  414. if family == 'AF_PIPE':
  415. c = PipeClient(address)
  416. else:
  417. c = SocketClient(address)
  418. if authkey is not None and not isinstance(authkey, bytes):
  419. raise TypeError('authkey should be a byte string')
  420. if authkey is not None:
  421. answer_challenge(c, authkey)
  422. deliver_challenge(c, authkey)
  423. return c
  424. if sys.platform != 'win32':
  425. def Pipe(duplex=True):
  426. '''
  427. Returns pair of connection objects at either end of a pipe
  428. '''
  429. if duplex:
  430. s1, s2 = socket.socketpair()
  431. s1.setblocking(True)
  432. s2.setblocking(True)
  433. c1 = Connection(s1.detach())
  434. c2 = Connection(s2.detach())
  435. else:
  436. fd1, fd2 = os.pipe()
  437. c1 = Connection(fd1, writable=False)
  438. c2 = Connection(fd2, readable=False)
  439. return c1, c2
  440. else:
  441. def Pipe(duplex=True):
  442. '''
  443. Returns pair of connection objects at either end of a pipe
  444. '''
  445. address = arbitrary_address('AF_PIPE')
  446. if duplex:
  447. openmode = _winapi.PIPE_ACCESS_DUPLEX
  448. access = _winapi.GENERIC_READ | _winapi.GENERIC_WRITE
  449. obsize, ibsize = BUFSIZE, BUFSIZE
  450. else:
  451. openmode = _winapi.PIPE_ACCESS_INBOUND
  452. access = _winapi.GENERIC_WRITE
  453. obsize, ibsize = 0, BUFSIZE
  454. h1 = _winapi.CreateNamedPipe(
  455. address, openmode | _winapi.FILE_FLAG_OVERLAPPED |
  456. _winapi.FILE_FLAG_FIRST_PIPE_INSTANCE,
  457. _winapi.PIPE_TYPE_MESSAGE | _winapi.PIPE_READMODE_MESSAGE |
  458. _winapi.PIPE_WAIT,
  459. 1, obsize, ibsize, _winapi.NMPWAIT_WAIT_FOREVER,
  460. # default security descriptor: the handle cannot be inherited
  461. _winapi.NULL
  462. )
  463. h2 = _winapi.CreateFile(
  464. address, access, 0, _winapi.NULL, _winapi.OPEN_EXISTING,
  465. _winapi.FILE_FLAG_OVERLAPPED, _winapi.NULL
  466. )
  467. _winapi.SetNamedPipeHandleState(
  468. h2, _winapi.PIPE_READMODE_MESSAGE, None, None
  469. )
  470. overlapped = _winapi.ConnectNamedPipe(h1, overlapped=True)
  471. _, err = overlapped.GetOverlappedResult(True)
  472. assert err == 0
  473. c1 = PipeConnection(h1, writable=duplex)
  474. c2 = PipeConnection(h2, readable=duplex)
  475. return c1, c2
  476. #
  477. # Definitions for connections based on sockets
  478. #
  479. class SocketListener(object):
  480. '''
  481. Representation of a socket which is bound to an address and listening
  482. '''
  483. def __init__(self, address, family, backlog=1):
  484. self._socket = socket.socket(getattr(socket, family))
  485. try:
  486. # SO_REUSEADDR has different semantics on Windows (issue #2550).
  487. if os.name == 'posix':
  488. self._socket.setsockopt(socket.SOL_SOCKET,
  489. socket.SO_REUSEADDR, 1)
  490. self._socket.setblocking(True)
  491. self._socket.bind(address)
  492. self._socket.listen(backlog)
  493. self._address = self._socket.getsockname()
  494. except OSError:
  495. self._socket.close()
  496. raise
  497. self._family = family
  498. self._last_accepted = None
  499. if family == 'AF_UNIX':
  500. self._unlink = util.Finalize(
  501. self, os.unlink, args=(address,), exitpriority=0
  502. )
  503. else:
  504. self._unlink = None
  505. def accept(self):
  506. s, self._last_accepted = self._socket.accept()
  507. s.setblocking(True)
  508. return Connection(s.detach())
  509. def close(self):
  510. try:
  511. self._socket.close()
  512. finally:
  513. unlink = self._unlink
  514. if unlink is not None:
  515. self._unlink = None
  516. unlink()
  517. def SocketClient(address):
  518. '''
  519. Return a connection object connected to the socket given by `address`
  520. '''
  521. family = address_type(address)
  522. with socket.socket( getattr(socket, family) ) as s:
  523. s.setblocking(True)
  524. s.connect(address)
  525. return Connection(s.detach())
  526. #
  527. # Definitions for connections based on named pipes
  528. #
  529. if sys.platform == 'win32':
  530. class PipeListener(object):
  531. '''
  532. Representation of a named pipe
  533. '''
  534. def __init__(self, address, backlog=None):
  535. self._address = address
  536. self._handle_queue = [self._new_handle(first=True)]
  537. self._last_accepted = None
  538. util.sub_debug('listener created with address=%r', self._address)
  539. self.close = util.Finalize(
  540. self, PipeListener._finalize_pipe_listener,
  541. args=(self._handle_queue, self._address), exitpriority=0
  542. )
  543. def _new_handle(self, first=False):
  544. flags = _winapi.PIPE_ACCESS_DUPLEX | _winapi.FILE_FLAG_OVERLAPPED
  545. if first:
  546. flags |= _winapi.FILE_FLAG_FIRST_PIPE_INSTANCE
  547. return _winapi.CreateNamedPipe(
  548. self._address, flags,
  549. _winapi.PIPE_TYPE_MESSAGE | _winapi.PIPE_READMODE_MESSAGE |
  550. _winapi.PIPE_WAIT,
  551. _winapi.PIPE_UNLIMITED_INSTANCES, BUFSIZE, BUFSIZE,
  552. _winapi.NMPWAIT_WAIT_FOREVER, _winapi.NULL
  553. )
  554. def accept(self):
  555. self._handle_queue.append(self._new_handle())
  556. handle = self._handle_queue.pop(0)
  557. try:
  558. ov = _winapi.ConnectNamedPipe(handle, overlapped=True)
  559. except OSError as e:
  560. if e.winerror != _winapi.ERROR_NO_DATA:
  561. raise
  562. # ERROR_NO_DATA can occur if a client has already connected,
  563. # written data and then disconnected -- see Issue 14725.
  564. else:
  565. try:
  566. res = _winapi.WaitForMultipleObjects(
  567. [ov.event], False, INFINITE)
  568. except:
  569. ov.cancel()
  570. _winapi.CloseHandle(handle)
  571. raise
  572. finally:
  573. _, err = ov.GetOverlappedResult(True)
  574. assert err == 0
  575. return PipeConnection(handle)
  576. @staticmethod
  577. def _finalize_pipe_listener(queue, address):
  578. util.sub_debug('closing listener with address=%r', address)
  579. for handle in queue:
  580. _winapi.CloseHandle(handle)
  581. def PipeClient(address):
  582. '''
  583. Return a connection object connected to the pipe given by `address`
  584. '''
  585. t = _init_timeout()
  586. while 1:
  587. try:
  588. _winapi.WaitNamedPipe(address, 1000)
  589. h = _winapi.CreateFile(
  590. address, _winapi.GENERIC_READ | _winapi.GENERIC_WRITE,
  591. 0, _winapi.NULL, _winapi.OPEN_EXISTING,
  592. _winapi.FILE_FLAG_OVERLAPPED, _winapi.NULL
  593. )
  594. except OSError as e:
  595. if e.winerror not in (_winapi.ERROR_SEM_TIMEOUT,
  596. _winapi.ERROR_PIPE_BUSY) or _check_timeout(t):
  597. raise
  598. else:
  599. break
  600. else:
  601. raise
  602. _winapi.SetNamedPipeHandleState(
  603. h, _winapi.PIPE_READMODE_MESSAGE, None, None
  604. )
  605. return PipeConnection(h)
  606. #
  607. # Authentication stuff
  608. #
  609. MESSAGE_LENGTH = 20
  610. CHALLENGE = b'#CHALLENGE#'
  611. WELCOME = b'#WELCOME#'
  612. FAILURE = b'#FAILURE#'
  613. def deliver_challenge(connection, authkey):
  614. import hmac
  615. if not isinstance(authkey, bytes):
  616. raise ValueError(
  617. "Authkey must be bytes, not {0!s}".format(type(authkey)))
  618. message = os.urandom(MESSAGE_LENGTH)
  619. connection.send_bytes(CHALLENGE + message)
  620. digest = hmac.new(authkey, message, 'md5').digest()
  621. response = connection.recv_bytes(256) # reject large message
  622. if response == digest:
  623. connection.send_bytes(WELCOME)
  624. else:
  625. connection.send_bytes(FAILURE)
  626. raise AuthenticationError('digest received was wrong')
  627. def answer_challenge(connection, authkey):
  628. import hmac
  629. if not isinstance(authkey, bytes):
  630. raise ValueError(
  631. "Authkey must be bytes, not {0!s}".format(type(authkey)))
  632. message = connection.recv_bytes(256) # reject large message
  633. assert message[:len(CHALLENGE)] == CHALLENGE, 'message = %r' % message
  634. message = message[len(CHALLENGE):]
  635. digest = hmac.new(authkey, message, 'md5').digest()
  636. connection.send_bytes(digest)
  637. response = connection.recv_bytes(256) # reject large message
  638. if response != WELCOME:
  639. raise AuthenticationError('digest sent was rejected')
  640. #
  641. # Support for using xmlrpclib for serialization
  642. #
  643. class ConnectionWrapper(object):
  644. def __init__(self, conn, dumps, loads):
  645. self._conn = conn
  646. self._dumps = dumps
  647. self._loads = loads
  648. for attr in ('fileno', 'close', 'poll', 'recv_bytes', 'send_bytes'):
  649. obj = getattr(conn, attr)
  650. setattr(self, attr, obj)
  651. def send(self, obj):
  652. s = self._dumps(obj)
  653. self._conn.send_bytes(s)
  654. def recv(self):
  655. s = self._conn.recv_bytes()
  656. return self._loads(s)
  657. def _xml_dumps(obj):
  658. return xmlrpclib.dumps((obj,), None, None, None, 1).encode('utf-8')
  659. def _xml_loads(s):
  660. (obj,), method = xmlrpclib.loads(s.decode('utf-8'))
  661. return obj
  662. class XmlListener(Listener):
  663. def accept(self):
  664. global xmlrpclib
  665. import xmlrpc.client as xmlrpclib
  666. obj = Listener.accept(self)
  667. return ConnectionWrapper(obj, _xml_dumps, _xml_loads)
  668. def XmlClient(*args, **kwds):
  669. global xmlrpclib
  670. import xmlrpc.client as xmlrpclib
  671. return ConnectionWrapper(Client(*args, **kwds), _xml_dumps, _xml_loads)
  672. #
  673. # Wait
  674. #
  675. if sys.platform == 'win32':
  676. def _exhaustive_wait(handles, timeout):
  677. # Return ALL handles which are currently signalled. (Only
  678. # returning the first signalled might create starvation issues.)
  679. L = list(handles)
  680. ready = []
  681. while L:
  682. res = _winapi.WaitForMultipleObjects(L, False, timeout)
  683. if res == WAIT_TIMEOUT:
  684. break
  685. elif WAIT_OBJECT_0 <= res < WAIT_OBJECT_0 + len(L):
  686. res -= WAIT_OBJECT_0
  687. elif WAIT_ABANDONED_0 <= res < WAIT_ABANDONED_0 + len(L):
  688. res -= WAIT_ABANDONED_0
  689. else:
  690. raise RuntimeError('Should not get here')
  691. ready.append(L[res])
  692. L = L[res+1:]
  693. timeout = 0
  694. return ready
  695. _ready_errors = {_winapi.ERROR_BROKEN_PIPE, _winapi.ERROR_NETNAME_DELETED}
  696. def wait(object_list, timeout=None):
  697. '''
  698. Wait till an object in object_list is ready/readable.
  699. Returns list of those objects in object_list which are ready/readable.
  700. '''
  701. if timeout is None:
  702. timeout = INFINITE
  703. elif timeout < 0:
  704. timeout = 0
  705. else:
  706. timeout = int(timeout * 1000 + 0.5)
  707. object_list = list(object_list)
  708. waithandle_to_obj = {}
  709. ov_list = []
  710. ready_objects = set()
  711. ready_handles = set()
  712. try:
  713. for o in object_list:
  714. try:
  715. fileno = getattr(o, 'fileno')
  716. except AttributeError:
  717. waithandle_to_obj[o.__index__()] = o
  718. else:
  719. # start an overlapped read of length zero
  720. try:
  721. ov, err = _winapi.ReadFile(fileno(), 0, True)
  722. except OSError as e:
  723. ov, err = None, e.winerror
  724. if err not in _ready_errors:
  725. raise
  726. if err == _winapi.ERROR_IO_PENDING:
  727. ov_list.append(ov)
  728. waithandle_to_obj[ov.event] = o
  729. else:
  730. # If o.fileno() is an overlapped pipe handle and
  731. # err == 0 then there is a zero length message
  732. # in the pipe, but it HAS NOT been consumed...
  733. if ov and sys.getwindowsversion()[:2] >= (6, 2):
  734. # ... except on Windows 8 and later, where
  735. # the message HAS been consumed.
  736. try:
  737. _, err = ov.GetOverlappedResult(False)
  738. except OSError as e:
  739. err = e.winerror
  740. if not err and hasattr(o, '_got_empty_message'):
  741. o._got_empty_message = True
  742. ready_objects.add(o)
  743. timeout = 0
  744. ready_handles = _exhaustive_wait(waithandle_to_obj.keys(), timeout)
  745. finally:
  746. # request that overlapped reads stop
  747. for ov in ov_list:
  748. ov.cancel()
  749. # wait for all overlapped reads to stop
  750. for ov in ov_list:
  751. try:
  752. _, err = ov.GetOverlappedResult(True)
  753. except OSError as e:
  754. err = e.winerror
  755. if err not in _ready_errors:
  756. raise
  757. if err != _winapi.ERROR_OPERATION_ABORTED:
  758. o = waithandle_to_obj[ov.event]
  759. ready_objects.add(o)
  760. if err == 0:
  761. # If o.fileno() is an overlapped pipe handle then
  762. # a zero length message HAS been consumed.
  763. if hasattr(o, '_got_empty_message'):
  764. o._got_empty_message = True
  765. ready_objects.update(waithandle_to_obj[h] for h in ready_handles)
  766. return [o for o in object_list if o in ready_objects]
  767. else:
  768. import selectors
  769. # poll/select have the advantage of not requiring any extra file
  770. # descriptor, contrarily to epoll/kqueue (also, they require a single
  771. # syscall).
  772. if hasattr(selectors, 'PollSelector'):
  773. _WaitSelector = selectors.PollSelector
  774. else:
  775. _WaitSelector = selectors.SelectSelector
  776. def wait(object_list, timeout=None):
  777. '''
  778. Wait till an object in object_list is ready/readable.
  779. Returns list of those objects in object_list which are ready/readable.
  780. '''
  781. with _WaitSelector() as selector:
  782. for obj in object_list:
  783. selector.register(obj, selectors.EVENT_READ)
  784. if timeout is not None:
  785. deadline = time.monotonic() + timeout
  786. while True:
  787. ready = selector.select(timeout)
  788. if ready:
  789. return [key.fileobj for (key, events) in ready]
  790. else:
  791. if timeout is not None:
  792. timeout = deadline - time.monotonic()
  793. if timeout < 0:
  794. return ready
  795. #
  796. # Make connection and socket objects sharable if possible
  797. #
  798. if sys.platform == 'win32':
  799. def reduce_connection(conn):
  800. handle = conn.fileno()
  801. with socket.fromfd(handle, socket.AF_INET, socket.SOCK_STREAM) as s:
  802. from . import resource_sharer
  803. ds = resource_sharer.DupSocket(s)
  804. return rebuild_connection, (ds, conn.readable, conn.writable)
  805. def rebuild_connection(ds, readable, writable):
  806. sock = ds.detach()
  807. return Connection(sock.detach(), readable, writable)
  808. reduction.register(Connection, reduce_connection)
  809. def reduce_pipe_connection(conn):
  810. access = ((_winapi.FILE_GENERIC_READ if conn.readable else 0) |
  811. (_winapi.FILE_GENERIC_WRITE if conn.writable else 0))
  812. dh = reduction.DupHandle(conn.fileno(), access)
  813. return rebuild_pipe_connection, (dh, conn.readable, conn.writable)
  814. def rebuild_pipe_connection(dh, readable, writable):
  815. handle = dh.detach()
  816. return PipeConnection(handle, readable, writable)
  817. reduction.register(PipeConnection, reduce_pipe_connection)
  818. else:
  819. def reduce_connection(conn):
  820. df = reduction.DupFd(conn.fileno())
  821. return rebuild_connection, (df, conn.readable, conn.writable)
  822. def rebuild_connection(df, readable, writable):
  823. fd = df.detach()
  824. return Connection(fd, readable, writable)
  825. reduction.register(Connection, reduce_connection)