_asyncio.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
  2. import asyncio
  3. import socket
  4. import ssl
  5. import struct
  6. import time
  7. import aioquic.quic.configuration # type: ignore
  8. import aioquic.quic.connection # type: ignore
  9. import aioquic.quic.events # type: ignore
  10. import dns.asyncbackend
  11. import dns.exception
  12. import dns.inet
  13. from dns.quic._common import (
  14. QUIC_MAX_DATAGRAM,
  15. AsyncQuicConnection,
  16. AsyncQuicManager,
  17. BaseQuicStream,
  18. UnexpectedEOF,
  19. )
  20. class AsyncioQuicStream(BaseQuicStream):
  21. def __init__(self, connection, stream_id):
  22. super().__init__(connection, stream_id)
  23. self._wake_up = asyncio.Condition()
  24. async def _wait_for_wake_up(self):
  25. async with self._wake_up:
  26. await self._wake_up.wait()
  27. async def wait_for(self, amount, expiration):
  28. while True:
  29. timeout = self._timeout_from_expiration(expiration)
  30. if self._buffer.have(amount):
  31. return
  32. self._expecting = amount
  33. try:
  34. await asyncio.wait_for(self._wait_for_wake_up(), timeout)
  35. except TimeoutError:
  36. raise dns.exception.Timeout
  37. self._expecting = 0
  38. async def wait_for_end(self, expiration):
  39. while True:
  40. timeout = self._timeout_from_expiration(expiration)
  41. if self._buffer.seen_end():
  42. return
  43. try:
  44. await asyncio.wait_for(self._wait_for_wake_up(), timeout)
  45. except TimeoutError:
  46. raise dns.exception.Timeout
  47. async def receive(self, timeout=None):
  48. expiration = self._expiration_from_timeout(timeout)
  49. if self._connection.is_h3():
  50. await self.wait_for_end(expiration)
  51. return self._buffer.get_all()
  52. else:
  53. await self.wait_for(2, expiration)
  54. (size,) = struct.unpack("!H", self._buffer.get(2))
  55. await self.wait_for(size, expiration)
  56. return self._buffer.get(size)
  57. async def send(self, datagram, is_end=False):
  58. data = self._encapsulate(datagram)
  59. await self._connection.write(self._stream_id, data, is_end)
  60. async def _add_input(self, data, is_end):
  61. if self._common_add_input(data, is_end):
  62. async with self._wake_up:
  63. self._wake_up.notify()
  64. async def close(self):
  65. self._close()
  66. # Streams are async context managers
  67. async def __aenter__(self):
  68. return self
  69. async def __aexit__(self, exc_type, exc_val, exc_tb):
  70. await self.close()
  71. async with self._wake_up:
  72. self._wake_up.notify()
  73. return False
  74. class AsyncioQuicConnection(AsyncQuicConnection):
  75. def __init__(self, connection, address, port, source, source_port, manager=None):
  76. super().__init__(connection, address, port, source, source_port, manager)
  77. self._socket = None
  78. self._handshake_complete = asyncio.Event()
  79. self._socket_created = asyncio.Event()
  80. self._wake_timer = asyncio.Condition()
  81. self._receiver_task = None
  82. self._sender_task = None
  83. self._wake_pending = False
  84. async def _receiver(self):
  85. try:
  86. af = dns.inet.af_for_address(self._address)
  87. backend = dns.asyncbackend.get_backend("asyncio")
  88. # Note that peer is a low-level address tuple, but make_socket() wants
  89. # a high-level address tuple, so we convert.
  90. self._socket = await backend.make_socket(
  91. af, socket.SOCK_DGRAM, 0, self._source, (self._peer[0], self._peer[1])
  92. )
  93. self._socket_created.set()
  94. async with self._socket:
  95. while not self._done:
  96. (datagram, address) = await self._socket.recvfrom(
  97. QUIC_MAX_DATAGRAM, None
  98. )
  99. if address[0] != self._peer[0] or address[1] != self._peer[1]:
  100. continue
  101. self._connection.receive_datagram(datagram, address, time.time())
  102. # Wake up the timer in case the sender is sleeping, as there may be
  103. # stuff to send now.
  104. await self._wakeup()
  105. except Exception:
  106. pass
  107. finally:
  108. self._done = True
  109. await self._wakeup()
  110. self._handshake_complete.set()
  111. async def _wakeup(self):
  112. self._wake_pending = True
  113. async with self._wake_timer:
  114. self._wake_timer.notify_all()
  115. async def _wait_for_wake_timer(self):
  116. async with self._wake_timer:
  117. if not self._wake_pending:
  118. await self._wake_timer.wait()
  119. self._wake_pending = False
  120. async def _sender(self):
  121. await self._socket_created.wait()
  122. while not self._done:
  123. datagrams = self._connection.datagrams_to_send(time.time())
  124. for datagram, address in datagrams:
  125. assert address == self._peer
  126. await self._socket.sendto(datagram, self._peer, None)
  127. (expiration, interval) = self._get_timer_values()
  128. try:
  129. await asyncio.wait_for(self._wait_for_wake_timer(), interval)
  130. except Exception:
  131. pass
  132. self._handle_timer(expiration)
  133. await self._handle_events()
  134. async def _handle_events(self):
  135. count = 0
  136. while True:
  137. event = self._connection.next_event()
  138. if event is None:
  139. return
  140. if isinstance(event, aioquic.quic.events.StreamDataReceived):
  141. if self.is_h3():
  142. h3_events = self._h3_conn.handle_event(event)
  143. for h3_event in h3_events:
  144. if isinstance(h3_event, aioquic.h3.events.HeadersReceived):
  145. stream = self._streams.get(event.stream_id)
  146. if stream:
  147. if stream._headers is None:
  148. stream._headers = h3_event.headers
  149. elif stream._trailers is None:
  150. stream._trailers = h3_event.headers
  151. if h3_event.stream_ended:
  152. await stream._add_input(b"", True)
  153. elif isinstance(h3_event, aioquic.h3.events.DataReceived):
  154. stream = self._streams.get(event.stream_id)
  155. if stream:
  156. await stream._add_input(
  157. h3_event.data, h3_event.stream_ended
  158. )
  159. else:
  160. stream = self._streams.get(event.stream_id)
  161. if stream:
  162. await stream._add_input(event.data, event.end_stream)
  163. elif isinstance(event, aioquic.quic.events.HandshakeCompleted):
  164. self._handshake_complete.set()
  165. elif isinstance(event, aioquic.quic.events.ConnectionTerminated):
  166. self._done = True
  167. self._receiver_task.cancel()
  168. elif isinstance(event, aioquic.quic.events.StreamReset):
  169. stream = self._streams.get(event.stream_id)
  170. if stream:
  171. await stream._add_input(b"", True)
  172. count += 1
  173. if count > 10:
  174. # yield
  175. count = 0
  176. await asyncio.sleep(0)
  177. async def write(self, stream, data, is_end=False):
  178. self._connection.send_stream_data(stream, data, is_end)
  179. await self._wakeup()
  180. def run(self):
  181. if self._closed:
  182. return
  183. self._receiver_task = asyncio.Task(self._receiver())
  184. self._sender_task = asyncio.Task(self._sender())
  185. async def make_stream(self, timeout=None):
  186. try:
  187. await asyncio.wait_for(self._handshake_complete.wait(), timeout)
  188. except TimeoutError:
  189. raise dns.exception.Timeout
  190. if self._done:
  191. raise UnexpectedEOF
  192. stream_id = self._connection.get_next_available_stream_id(False)
  193. stream = AsyncioQuicStream(self, stream_id)
  194. self._streams[stream_id] = stream
  195. return stream
  196. async def close(self):
  197. if not self._closed:
  198. self._manager.closed(self._peer[0], self._peer[1])
  199. self._closed = True
  200. self._connection.close()
  201. # sender might be blocked on this, so set it
  202. self._socket_created.set()
  203. await self._wakeup()
  204. try:
  205. await self._receiver_task
  206. except asyncio.CancelledError:
  207. pass
  208. try:
  209. await self._sender_task
  210. except asyncio.CancelledError:
  211. pass
  212. await self._socket.close()
  213. class AsyncioQuicManager(AsyncQuicManager):
  214. def __init__(
  215. self, conf=None, verify_mode=ssl.CERT_REQUIRED, server_name=None, h3=False
  216. ):
  217. super().__init__(conf, verify_mode, AsyncioQuicConnection, server_name, h3)
  218. def connect(
  219. self, address, port=853, source=None, source_port=0, want_session_ticket=True
  220. ):
  221. (connection, start) = self._connect(
  222. address, port, source, source_port, want_session_ticket
  223. )
  224. if start:
  225. connection.run()
  226. return connection
  227. async def __aenter__(self):
  228. return self
  229. async def __aexit__(self, exc_type, exc_val, exc_tb):
  230. # Copy the iterator into a list as exiting things will mutate the connections
  231. # table.
  232. connections = list(self._connections.values())
  233. for connection in connections:
  234. await connection.close()
  235. return False