asyncquery.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
  2. # Copyright (C) 2003-2017 Nominum, Inc.
  3. #
  4. # Permission to use, copy, modify, and distribute this software and its
  5. # documentation for any purpose with or without fee is hereby granted,
  6. # provided that the above copyright notice and this permission notice
  7. # appear in all copies.
  8. #
  9. # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
  10. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
  12. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  15. # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. """Talk to a DNS server."""
  17. import base64
  18. import contextlib
  19. import random
  20. import socket
  21. import struct
  22. import time
  23. import urllib.parse
  24. from typing import Any, Dict, Optional, Tuple, Union, cast
  25. import dns.asyncbackend
  26. import dns.exception
  27. import dns.inet
  28. import dns.message
  29. import dns.name
  30. import dns.quic
  31. import dns.rcode
  32. import dns.rdataclass
  33. import dns.rdatatype
  34. import dns.transaction
  35. from dns._asyncbackend import NullContext
  36. from dns.query import (
  37. BadResponse,
  38. HTTPVersion,
  39. NoDOH,
  40. NoDOQ,
  41. UDPMode,
  42. _check_status,
  43. _compute_times,
  44. _make_dot_ssl_context,
  45. _matches_destination,
  46. _remaining,
  47. have_doh,
  48. ssl,
  49. )
  50. if have_doh:
  51. import httpx
  52. # for brevity
  53. _lltuple = dns.inet.low_level_address_tuple
  54. def _source_tuple(af, address, port):
  55. # Make a high level source tuple, or return None if address and port
  56. # are both None
  57. if address or port:
  58. if address is None:
  59. if af == socket.AF_INET:
  60. address = "0.0.0.0"
  61. elif af == socket.AF_INET6:
  62. address = "::"
  63. else:
  64. raise NotImplementedError(f"unknown address family {af}")
  65. return (address, port)
  66. else:
  67. return None
  68. def _timeout(expiration, now=None):
  69. if expiration is not None:
  70. if not now:
  71. now = time.time()
  72. return max(expiration - now, 0)
  73. else:
  74. return None
  75. async def send_udp(
  76. sock: dns.asyncbackend.DatagramSocket,
  77. what: Union[dns.message.Message, bytes],
  78. destination: Any,
  79. expiration: Optional[float] = None,
  80. ) -> Tuple[int, float]:
  81. """Send a DNS message to the specified UDP socket.
  82. *sock*, a ``dns.asyncbackend.DatagramSocket``.
  83. *what*, a ``bytes`` or ``dns.message.Message``, the message to send.
  84. *destination*, a destination tuple appropriate for the address family
  85. of the socket, specifying where to send the query.
  86. *expiration*, a ``float`` or ``None``, the absolute time at which
  87. a timeout exception should be raised. If ``None``, no timeout will
  88. occur. The expiration value is meaningless for the asyncio backend, as
  89. asyncio's transport sendto() never blocks.
  90. Returns an ``(int, float)`` tuple of bytes sent and the sent time.
  91. """
  92. if isinstance(what, dns.message.Message):
  93. what = what.to_wire()
  94. sent_time = time.time()
  95. n = await sock.sendto(what, destination, _timeout(expiration, sent_time))
  96. return (n, sent_time)
  97. async def receive_udp(
  98. sock: dns.asyncbackend.DatagramSocket,
  99. destination: Optional[Any] = None,
  100. expiration: Optional[float] = None,
  101. ignore_unexpected: bool = False,
  102. one_rr_per_rrset: bool = False,
  103. keyring: Optional[Dict[dns.name.Name, dns.tsig.Key]] = None,
  104. request_mac: Optional[bytes] = b"",
  105. ignore_trailing: bool = False,
  106. raise_on_truncation: bool = False,
  107. ignore_errors: bool = False,
  108. query: Optional[dns.message.Message] = None,
  109. ) -> Any:
  110. """Read a DNS message from a UDP socket.
  111. *sock*, a ``dns.asyncbackend.DatagramSocket``.
  112. See :py:func:`dns.query.receive_udp()` for the documentation of the other
  113. parameters, and exceptions.
  114. Returns a ``(dns.message.Message, float, tuple)`` tuple of the received message, the
  115. received time, and the address where the message arrived from.
  116. """
  117. wire = b""
  118. while True:
  119. (wire, from_address) = await sock.recvfrom(65535, _timeout(expiration))
  120. if not _matches_destination(
  121. sock.family, from_address, destination, ignore_unexpected
  122. ):
  123. continue
  124. received_time = time.time()
  125. try:
  126. r = dns.message.from_wire(
  127. wire,
  128. keyring=keyring,
  129. request_mac=request_mac,
  130. one_rr_per_rrset=one_rr_per_rrset,
  131. ignore_trailing=ignore_trailing,
  132. raise_on_truncation=raise_on_truncation,
  133. )
  134. except dns.message.Truncated as e:
  135. # See the comment in query.py for details.
  136. if (
  137. ignore_errors
  138. and query is not None
  139. and not query.is_response(e.message())
  140. ):
  141. continue
  142. else:
  143. raise
  144. except Exception:
  145. if ignore_errors:
  146. continue
  147. else:
  148. raise
  149. if ignore_errors and query is not None and not query.is_response(r):
  150. continue
  151. return (r, received_time, from_address)
  152. async def udp(
  153. q: dns.message.Message,
  154. where: str,
  155. timeout: Optional[float] = None,
  156. port: int = 53,
  157. source: Optional[str] = None,
  158. source_port: int = 0,
  159. ignore_unexpected: bool = False,
  160. one_rr_per_rrset: bool = False,
  161. ignore_trailing: bool = False,
  162. raise_on_truncation: bool = False,
  163. sock: Optional[dns.asyncbackend.DatagramSocket] = None,
  164. backend: Optional[dns.asyncbackend.Backend] = None,
  165. ignore_errors: bool = False,
  166. ) -> dns.message.Message:
  167. """Return the response obtained after sending a query via UDP.
  168. *sock*, a ``dns.asyncbackend.DatagramSocket``, or ``None``,
  169. the socket to use for the query. If ``None``, the default, a
  170. socket is created. Note that if a socket is provided, the
  171. *source*, *source_port*, and *backend* are ignored.
  172. *backend*, a ``dns.asyncbackend.Backend``, or ``None``. If ``None``,
  173. the default, then dnspython will use the default backend.
  174. See :py:func:`dns.query.udp()` for the documentation of the other
  175. parameters, exceptions, and return type of this method.
  176. """
  177. wire = q.to_wire()
  178. (begin_time, expiration) = _compute_times(timeout)
  179. af = dns.inet.af_for_address(where)
  180. destination = _lltuple((where, port), af)
  181. if sock:
  182. cm: contextlib.AbstractAsyncContextManager = NullContext(sock)
  183. else:
  184. if not backend:
  185. backend = dns.asyncbackend.get_default_backend()
  186. stuple = _source_tuple(af, source, source_port)
  187. if backend.datagram_connection_required():
  188. dtuple = (where, port)
  189. else:
  190. dtuple = None
  191. cm = await backend.make_socket(af, socket.SOCK_DGRAM, 0, stuple, dtuple)
  192. async with cm as s:
  193. await send_udp(s, wire, destination, expiration)
  194. (r, received_time, _) = await receive_udp(
  195. s,
  196. destination,
  197. expiration,
  198. ignore_unexpected,
  199. one_rr_per_rrset,
  200. q.keyring,
  201. q.mac,
  202. ignore_trailing,
  203. raise_on_truncation,
  204. ignore_errors,
  205. q,
  206. )
  207. r.time = received_time - begin_time
  208. # We don't need to check q.is_response() if we are in ignore_errors mode
  209. # as receive_udp() will have checked it.
  210. if not (ignore_errors or q.is_response(r)):
  211. raise BadResponse
  212. return r
  213. async def udp_with_fallback(
  214. q: dns.message.Message,
  215. where: str,
  216. timeout: Optional[float] = None,
  217. port: int = 53,
  218. source: Optional[str] = None,
  219. source_port: int = 0,
  220. ignore_unexpected: bool = False,
  221. one_rr_per_rrset: bool = False,
  222. ignore_trailing: bool = False,
  223. udp_sock: Optional[dns.asyncbackend.DatagramSocket] = None,
  224. tcp_sock: Optional[dns.asyncbackend.StreamSocket] = None,
  225. backend: Optional[dns.asyncbackend.Backend] = None,
  226. ignore_errors: bool = False,
  227. ) -> Tuple[dns.message.Message, bool]:
  228. """Return the response to the query, trying UDP first and falling back
  229. to TCP if UDP results in a truncated response.
  230. *udp_sock*, a ``dns.asyncbackend.DatagramSocket``, or ``None``,
  231. the socket to use for the UDP query. If ``None``, the default, a
  232. socket is created. Note that if a socket is provided the *source*,
  233. *source_port*, and *backend* are ignored for the UDP query.
  234. *tcp_sock*, a ``dns.asyncbackend.StreamSocket``, or ``None``, the
  235. socket to use for the TCP query. If ``None``, the default, a
  236. socket is created. Note that if a socket is provided *where*,
  237. *source*, *source_port*, and *backend* are ignored for the TCP query.
  238. *backend*, a ``dns.asyncbackend.Backend``, or ``None``. If ``None``,
  239. the default, then dnspython will use the default backend.
  240. See :py:func:`dns.query.udp_with_fallback()` for the documentation
  241. of the other parameters, exceptions, and return type of this
  242. method.
  243. """
  244. try:
  245. response = await udp(
  246. q,
  247. where,
  248. timeout,
  249. port,
  250. source,
  251. source_port,
  252. ignore_unexpected,
  253. one_rr_per_rrset,
  254. ignore_trailing,
  255. True,
  256. udp_sock,
  257. backend,
  258. ignore_errors,
  259. )
  260. return (response, False)
  261. except dns.message.Truncated:
  262. response = await tcp(
  263. q,
  264. where,
  265. timeout,
  266. port,
  267. source,
  268. source_port,
  269. one_rr_per_rrset,
  270. ignore_trailing,
  271. tcp_sock,
  272. backend,
  273. )
  274. return (response, True)
  275. async def send_tcp(
  276. sock: dns.asyncbackend.StreamSocket,
  277. what: Union[dns.message.Message, bytes],
  278. expiration: Optional[float] = None,
  279. ) -> Tuple[int, float]:
  280. """Send a DNS message to the specified TCP socket.
  281. *sock*, a ``dns.asyncbackend.StreamSocket``.
  282. See :py:func:`dns.query.send_tcp()` for the documentation of the other
  283. parameters, exceptions, and return type of this method.
  284. """
  285. if isinstance(what, dns.message.Message):
  286. tcpmsg = what.to_wire(prepend_length=True)
  287. else:
  288. # copying the wire into tcpmsg is inefficient, but lets us
  289. # avoid writev() or doing a short write that would get pushed
  290. # onto the net
  291. tcpmsg = len(what).to_bytes(2, "big") + what
  292. sent_time = time.time()
  293. await sock.sendall(tcpmsg, _timeout(expiration, sent_time))
  294. return (len(tcpmsg), sent_time)
  295. async def _read_exactly(sock, count, expiration):
  296. """Read the specified number of bytes from stream. Keep trying until we
  297. either get the desired amount, or we hit EOF.
  298. """
  299. s = b""
  300. while count > 0:
  301. n = await sock.recv(count, _timeout(expiration))
  302. if n == b"":
  303. raise EOFError("EOF")
  304. count = count - len(n)
  305. s = s + n
  306. return s
  307. async def receive_tcp(
  308. sock: dns.asyncbackend.StreamSocket,
  309. expiration: Optional[float] = None,
  310. one_rr_per_rrset: bool = False,
  311. keyring: Optional[Dict[dns.name.Name, dns.tsig.Key]] = None,
  312. request_mac: Optional[bytes] = b"",
  313. ignore_trailing: bool = False,
  314. ) -> Tuple[dns.message.Message, float]:
  315. """Read a DNS message from a TCP socket.
  316. *sock*, a ``dns.asyncbackend.StreamSocket``.
  317. See :py:func:`dns.query.receive_tcp()` for the documentation of the other
  318. parameters, exceptions, and return type of this method.
  319. """
  320. ldata = await _read_exactly(sock, 2, expiration)
  321. (l,) = struct.unpack("!H", ldata)
  322. wire = await _read_exactly(sock, l, expiration)
  323. received_time = time.time()
  324. r = dns.message.from_wire(
  325. wire,
  326. keyring=keyring,
  327. request_mac=request_mac,
  328. one_rr_per_rrset=one_rr_per_rrset,
  329. ignore_trailing=ignore_trailing,
  330. )
  331. return (r, received_time)
  332. async def tcp(
  333. q: dns.message.Message,
  334. where: str,
  335. timeout: Optional[float] = None,
  336. port: int = 53,
  337. source: Optional[str] = None,
  338. source_port: int = 0,
  339. one_rr_per_rrset: bool = False,
  340. ignore_trailing: bool = False,
  341. sock: Optional[dns.asyncbackend.StreamSocket] = None,
  342. backend: Optional[dns.asyncbackend.Backend] = None,
  343. ) -> dns.message.Message:
  344. """Return the response obtained after sending a query via TCP.
  345. *sock*, a ``dns.asyncbacket.StreamSocket``, or ``None``, the
  346. socket to use for the query. If ``None``, the default, a socket
  347. is created. Note that if a socket is provided
  348. *where*, *port*, *source*, *source_port*, and *backend* are ignored.
  349. *backend*, a ``dns.asyncbackend.Backend``, or ``None``. If ``None``,
  350. the default, then dnspython will use the default backend.
  351. See :py:func:`dns.query.tcp()` for the documentation of the other
  352. parameters, exceptions, and return type of this method.
  353. """
  354. wire = q.to_wire()
  355. (begin_time, expiration) = _compute_times(timeout)
  356. if sock:
  357. # Verify that the socket is connected, as if it's not connected,
  358. # it's not writable, and the polling in send_tcp() will time out or
  359. # hang forever.
  360. await sock.getpeername()
  361. cm: contextlib.AbstractAsyncContextManager = NullContext(sock)
  362. else:
  363. # These are simple (address, port) pairs, not family-dependent tuples
  364. # you pass to low-level socket code.
  365. af = dns.inet.af_for_address(where)
  366. stuple = _source_tuple(af, source, source_port)
  367. dtuple = (where, port)
  368. if not backend:
  369. backend = dns.asyncbackend.get_default_backend()
  370. cm = await backend.make_socket(
  371. af, socket.SOCK_STREAM, 0, stuple, dtuple, timeout
  372. )
  373. async with cm as s:
  374. await send_tcp(s, wire, expiration)
  375. (r, received_time) = await receive_tcp(
  376. s, expiration, one_rr_per_rrset, q.keyring, q.mac, ignore_trailing
  377. )
  378. r.time = received_time - begin_time
  379. if not q.is_response(r):
  380. raise BadResponse
  381. return r
  382. async def tls(
  383. q: dns.message.Message,
  384. where: str,
  385. timeout: Optional[float] = None,
  386. port: int = 853,
  387. source: Optional[str] = None,
  388. source_port: int = 0,
  389. one_rr_per_rrset: bool = False,
  390. ignore_trailing: bool = False,
  391. sock: Optional[dns.asyncbackend.StreamSocket] = None,
  392. backend: Optional[dns.asyncbackend.Backend] = None,
  393. ssl_context: Optional[ssl.SSLContext] = None,
  394. server_hostname: Optional[str] = None,
  395. verify: Union[bool, str] = True,
  396. ) -> dns.message.Message:
  397. """Return the response obtained after sending a query via TLS.
  398. *sock*, an ``asyncbackend.StreamSocket``, or ``None``, the socket
  399. to use for the query. If ``None``, the default, a socket is
  400. created. Note that if a socket is provided, it must be a
  401. connected SSL stream socket, and *where*, *port*,
  402. *source*, *source_port*, *backend*, *ssl_context*, and *server_hostname*
  403. are ignored.
  404. *backend*, a ``dns.asyncbackend.Backend``, or ``None``. If ``None``,
  405. the default, then dnspython will use the default backend.
  406. See :py:func:`dns.query.tls()` for the documentation of the other
  407. parameters, exceptions, and return type of this method.
  408. """
  409. (begin_time, expiration) = _compute_times(timeout)
  410. if sock:
  411. cm: contextlib.AbstractAsyncContextManager = NullContext(sock)
  412. else:
  413. if ssl_context is None:
  414. ssl_context = _make_dot_ssl_context(server_hostname, verify)
  415. af = dns.inet.af_for_address(where)
  416. stuple = _source_tuple(af, source, source_port)
  417. dtuple = (where, port)
  418. if not backend:
  419. backend = dns.asyncbackend.get_default_backend()
  420. cm = await backend.make_socket(
  421. af,
  422. socket.SOCK_STREAM,
  423. 0,
  424. stuple,
  425. dtuple,
  426. timeout,
  427. ssl_context,
  428. server_hostname,
  429. )
  430. async with cm as s:
  431. timeout = _timeout(expiration)
  432. response = await tcp(
  433. q,
  434. where,
  435. timeout,
  436. port,
  437. source,
  438. source_port,
  439. one_rr_per_rrset,
  440. ignore_trailing,
  441. s,
  442. backend,
  443. )
  444. end_time = time.time()
  445. response.time = end_time - begin_time
  446. return response
  447. def _maybe_get_resolver(
  448. resolver: Optional["dns.asyncresolver.Resolver"],
  449. ) -> "dns.asyncresolver.Resolver":
  450. # We need a separate method for this to avoid overriding the global
  451. # variable "dns" with the as-yet undefined local variable "dns"
  452. # in https().
  453. if resolver is None:
  454. # pylint: disable=import-outside-toplevel,redefined-outer-name
  455. import dns.asyncresolver
  456. resolver = dns.asyncresolver.Resolver()
  457. return resolver
  458. async def https(
  459. q: dns.message.Message,
  460. where: str,
  461. timeout: Optional[float] = None,
  462. port: int = 443,
  463. source: Optional[str] = None,
  464. source_port: int = 0, # pylint: disable=W0613
  465. one_rr_per_rrset: bool = False,
  466. ignore_trailing: bool = False,
  467. client: Optional["httpx.AsyncClient"] = None,
  468. path: str = "/dns-query",
  469. post: bool = True,
  470. verify: Union[bool, str] = True,
  471. bootstrap_address: Optional[str] = None,
  472. resolver: Optional["dns.asyncresolver.Resolver"] = None,
  473. family: int = socket.AF_UNSPEC,
  474. http_version: HTTPVersion = HTTPVersion.DEFAULT,
  475. ) -> dns.message.Message:
  476. """Return the response obtained after sending a query via DNS-over-HTTPS.
  477. *client*, a ``httpx.AsyncClient``. If provided, the client to use for
  478. the query.
  479. Unlike the other dnspython async functions, a backend cannot be provided
  480. in this function because httpx always auto-detects the async backend.
  481. See :py:func:`dns.query.https()` for the documentation of the other
  482. parameters, exceptions, and return type of this method.
  483. """
  484. try:
  485. af = dns.inet.af_for_address(where)
  486. except ValueError:
  487. af = None
  488. if af is not None and dns.inet.is_address(where):
  489. if af == socket.AF_INET:
  490. url = f"https://{where}:{port}{path}"
  491. elif af == socket.AF_INET6:
  492. url = f"https://[{where}]:{port}{path}"
  493. else:
  494. url = where
  495. extensions = {}
  496. if bootstrap_address is None:
  497. # pylint: disable=possibly-used-before-assignment
  498. parsed = urllib.parse.urlparse(url)
  499. if parsed.hostname is None:
  500. raise ValueError("no hostname in URL")
  501. if dns.inet.is_address(parsed.hostname):
  502. bootstrap_address = parsed.hostname
  503. extensions["sni_hostname"] = parsed.hostname
  504. if parsed.port is not None:
  505. port = parsed.port
  506. if http_version == HTTPVersion.H3 or (
  507. http_version == HTTPVersion.DEFAULT and not have_doh
  508. ):
  509. if bootstrap_address is None:
  510. resolver = _maybe_get_resolver(resolver)
  511. assert parsed.hostname is not None # for mypy
  512. answers = await resolver.resolve_name(parsed.hostname, family)
  513. bootstrap_address = random.choice(list(answers.addresses()))
  514. return await _http3(
  515. q,
  516. bootstrap_address,
  517. url,
  518. timeout,
  519. port,
  520. source,
  521. source_port,
  522. one_rr_per_rrset,
  523. ignore_trailing,
  524. verify=verify,
  525. post=post,
  526. )
  527. if not have_doh:
  528. raise NoDOH # pragma: no cover
  529. # pylint: disable=possibly-used-before-assignment
  530. if client and not isinstance(client, httpx.AsyncClient):
  531. raise ValueError("session parameter must be an httpx.AsyncClient")
  532. # pylint: enable=possibly-used-before-assignment
  533. wire = q.to_wire()
  534. headers = {"accept": "application/dns-message"}
  535. h1 = http_version in (HTTPVersion.H1, HTTPVersion.DEFAULT)
  536. h2 = http_version in (HTTPVersion.H2, HTTPVersion.DEFAULT)
  537. backend = dns.asyncbackend.get_default_backend()
  538. if source is None:
  539. local_address = None
  540. local_port = 0
  541. else:
  542. local_address = source
  543. local_port = source_port
  544. if client:
  545. cm: contextlib.AbstractAsyncContextManager = NullContext(client)
  546. else:
  547. transport = backend.get_transport_class()(
  548. local_address=local_address,
  549. http1=h1,
  550. http2=h2,
  551. verify=verify,
  552. local_port=local_port,
  553. bootstrap_address=bootstrap_address,
  554. resolver=resolver,
  555. family=family,
  556. )
  557. cm = httpx.AsyncClient(http1=h1, http2=h2, verify=verify, transport=transport)
  558. async with cm as the_client:
  559. # see https://tools.ietf.org/html/rfc8484#section-4.1.1 for DoH
  560. # GET and POST examples
  561. if post:
  562. headers.update(
  563. {
  564. "content-type": "application/dns-message",
  565. "content-length": str(len(wire)),
  566. }
  567. )
  568. response = await backend.wait_for(
  569. the_client.post(
  570. url,
  571. headers=headers,
  572. content=wire,
  573. extensions=extensions,
  574. ),
  575. timeout,
  576. )
  577. else:
  578. wire = base64.urlsafe_b64encode(wire).rstrip(b"=")
  579. twire = wire.decode() # httpx does a repr() if we give it bytes
  580. response = await backend.wait_for(
  581. the_client.get(
  582. url,
  583. headers=headers,
  584. params={"dns": twire},
  585. extensions=extensions,
  586. ),
  587. timeout,
  588. )
  589. # see https://tools.ietf.org/html/rfc8484#section-4.2.1 for info about DoH
  590. # status codes
  591. if response.status_code < 200 or response.status_code > 299:
  592. raise ValueError(
  593. f"{where} responded with status code {response.status_code}"
  594. f"\nResponse body: {response.content!r}"
  595. )
  596. r = dns.message.from_wire(
  597. response.content,
  598. keyring=q.keyring,
  599. request_mac=q.request_mac,
  600. one_rr_per_rrset=one_rr_per_rrset,
  601. ignore_trailing=ignore_trailing,
  602. )
  603. r.time = response.elapsed.total_seconds()
  604. if not q.is_response(r):
  605. raise BadResponse
  606. return r
  607. async def _http3(
  608. q: dns.message.Message,
  609. where: str,
  610. url: str,
  611. timeout: Optional[float] = None,
  612. port: int = 853,
  613. source: Optional[str] = None,
  614. source_port: int = 0,
  615. one_rr_per_rrset: bool = False,
  616. ignore_trailing: bool = False,
  617. verify: Union[bool, str] = True,
  618. backend: Optional[dns.asyncbackend.Backend] = None,
  619. hostname: Optional[str] = None,
  620. post: bool = True,
  621. ) -> dns.message.Message:
  622. if not dns.quic.have_quic:
  623. raise NoDOH("DNS-over-HTTP3 is not available.") # pragma: no cover
  624. url_parts = urllib.parse.urlparse(url)
  625. hostname = url_parts.hostname
  626. if url_parts.port is not None:
  627. port = url_parts.port
  628. q.id = 0
  629. wire = q.to_wire()
  630. (cfactory, mfactory) = dns.quic.factories_for_backend(backend)
  631. async with cfactory() as context:
  632. async with mfactory(
  633. context, verify_mode=verify, server_name=hostname, h3=True
  634. ) as the_manager:
  635. the_connection = the_manager.connect(where, port, source, source_port)
  636. (start, expiration) = _compute_times(timeout)
  637. stream = await the_connection.make_stream(timeout)
  638. async with stream:
  639. # note that send_h3() does not need await
  640. stream.send_h3(url, wire, post)
  641. wire = await stream.receive(_remaining(expiration))
  642. _check_status(stream.headers(), where, wire)
  643. finish = time.time()
  644. r = dns.message.from_wire(
  645. wire,
  646. keyring=q.keyring,
  647. request_mac=q.request_mac,
  648. one_rr_per_rrset=one_rr_per_rrset,
  649. ignore_trailing=ignore_trailing,
  650. )
  651. r.time = max(finish - start, 0.0)
  652. if not q.is_response(r):
  653. raise BadResponse
  654. return r
  655. async def quic(
  656. q: dns.message.Message,
  657. where: str,
  658. timeout: Optional[float] = None,
  659. port: int = 853,
  660. source: Optional[str] = None,
  661. source_port: int = 0,
  662. one_rr_per_rrset: bool = False,
  663. ignore_trailing: bool = False,
  664. connection: Optional[dns.quic.AsyncQuicConnection] = None,
  665. verify: Union[bool, str] = True,
  666. backend: Optional[dns.asyncbackend.Backend] = None,
  667. hostname: Optional[str] = None,
  668. server_hostname: Optional[str] = None,
  669. ) -> dns.message.Message:
  670. """Return the response obtained after sending an asynchronous query via
  671. DNS-over-QUIC.
  672. *backend*, a ``dns.asyncbackend.Backend``, or ``None``. If ``None``,
  673. the default, then dnspython will use the default backend.
  674. See :py:func:`dns.query.quic()` for the documentation of the other
  675. parameters, exceptions, and return type of this method.
  676. """
  677. if not dns.quic.have_quic:
  678. raise NoDOQ("DNS-over-QUIC is not available.") # pragma: no cover
  679. if server_hostname is not None and hostname is None:
  680. hostname = server_hostname
  681. q.id = 0
  682. wire = q.to_wire()
  683. the_connection: dns.quic.AsyncQuicConnection
  684. if connection:
  685. cfactory = dns.quic.null_factory
  686. mfactory = dns.quic.null_factory
  687. the_connection = connection
  688. else:
  689. (cfactory, mfactory) = dns.quic.factories_for_backend(backend)
  690. async with cfactory() as context:
  691. async with mfactory(
  692. context,
  693. verify_mode=verify,
  694. server_name=server_hostname,
  695. ) as the_manager:
  696. if not connection:
  697. the_connection = the_manager.connect(where, port, source, source_port)
  698. (start, expiration) = _compute_times(timeout)
  699. stream = await the_connection.make_stream(timeout)
  700. async with stream:
  701. await stream.send(wire, True)
  702. wire = await stream.receive(_remaining(expiration))
  703. finish = time.time()
  704. r = dns.message.from_wire(
  705. wire,
  706. keyring=q.keyring,
  707. request_mac=q.request_mac,
  708. one_rr_per_rrset=one_rr_per_rrset,
  709. ignore_trailing=ignore_trailing,
  710. )
  711. r.time = max(finish - start, 0.0)
  712. if not q.is_response(r):
  713. raise BadResponse
  714. return r
  715. async def _inbound_xfr(
  716. txn_manager: dns.transaction.TransactionManager,
  717. s: dns.asyncbackend.Socket,
  718. query: dns.message.Message,
  719. serial: Optional[int],
  720. timeout: Optional[float],
  721. expiration: float,
  722. ) -> Any:
  723. """Given a socket, does the zone transfer."""
  724. rdtype = query.question[0].rdtype
  725. is_ixfr = rdtype == dns.rdatatype.IXFR
  726. origin = txn_manager.from_wire_origin()
  727. wire = query.to_wire()
  728. is_udp = s.type == socket.SOCK_DGRAM
  729. if is_udp:
  730. udp_sock = cast(dns.asyncbackend.DatagramSocket, s)
  731. await udp_sock.sendto(wire, None, _timeout(expiration))
  732. else:
  733. tcp_sock = cast(dns.asyncbackend.StreamSocket, s)
  734. tcpmsg = struct.pack("!H", len(wire)) + wire
  735. await tcp_sock.sendall(tcpmsg, expiration)
  736. with dns.xfr.Inbound(txn_manager, rdtype, serial, is_udp) as inbound:
  737. done = False
  738. tsig_ctx = None
  739. while not done:
  740. (_, mexpiration) = _compute_times(timeout)
  741. if mexpiration is None or (
  742. expiration is not None and mexpiration > expiration
  743. ):
  744. mexpiration = expiration
  745. if is_udp:
  746. timeout = _timeout(mexpiration)
  747. (rwire, _) = await udp_sock.recvfrom(65535, timeout)
  748. else:
  749. ldata = await _read_exactly(tcp_sock, 2, mexpiration)
  750. (l,) = struct.unpack("!H", ldata)
  751. rwire = await _read_exactly(tcp_sock, l, mexpiration)
  752. r = dns.message.from_wire(
  753. rwire,
  754. keyring=query.keyring,
  755. request_mac=query.mac,
  756. xfr=True,
  757. origin=origin,
  758. tsig_ctx=tsig_ctx,
  759. multi=(not is_udp),
  760. one_rr_per_rrset=is_ixfr,
  761. )
  762. done = inbound.process_message(r)
  763. yield r
  764. tsig_ctx = r.tsig_ctx
  765. if query.keyring and not r.had_tsig:
  766. raise dns.exception.FormError("missing TSIG")
  767. async def inbound_xfr(
  768. where: str,
  769. txn_manager: dns.transaction.TransactionManager,
  770. query: Optional[dns.message.Message] = None,
  771. port: int = 53,
  772. timeout: Optional[float] = None,
  773. lifetime: Optional[float] = None,
  774. source: Optional[str] = None,
  775. source_port: int = 0,
  776. udp_mode: UDPMode = UDPMode.NEVER,
  777. backend: Optional[dns.asyncbackend.Backend] = None,
  778. ) -> None:
  779. """Conduct an inbound transfer and apply it via a transaction from the
  780. txn_manager.
  781. *backend*, a ``dns.asyncbackend.Backend``, or ``None``. If ``None``,
  782. the default, then dnspython will use the default backend.
  783. See :py:func:`dns.query.inbound_xfr()` for the documentation of
  784. the other parameters, exceptions, and return type of this method.
  785. """
  786. if query is None:
  787. (query, serial) = dns.xfr.make_query(txn_manager)
  788. else:
  789. serial = dns.xfr.extract_serial_from_query(query)
  790. af = dns.inet.af_for_address(where)
  791. stuple = _source_tuple(af, source, source_port)
  792. dtuple = (where, port)
  793. if not backend:
  794. backend = dns.asyncbackend.get_default_backend()
  795. (_, expiration) = _compute_times(lifetime)
  796. if query.question[0].rdtype == dns.rdatatype.IXFR and udp_mode != UDPMode.NEVER:
  797. s = await backend.make_socket(
  798. af, socket.SOCK_DGRAM, 0, stuple, dtuple, _timeout(expiration)
  799. )
  800. async with s:
  801. try:
  802. async for _ in _inbound_xfr(
  803. txn_manager, s, query, serial, timeout, expiration
  804. ):
  805. pass
  806. return
  807. except dns.xfr.UseTCP:
  808. if udp_mode == UDPMode.ONLY:
  809. raise
  810. s = await backend.make_socket(
  811. af, socket.SOCK_STREAM, 0, stuple, dtuple, _timeout(expiration)
  812. )
  813. async with s:
  814. async for _ in _inbound_xfr(txn_manager, s, query, serial, timeout, expiration):
  815. pass