From: Bob Halley Date: Fri, 12 Jun 2020 20:49:27 +0000 (-0700) Subject: fix recvfrom, tls timing, and other misc things X-Git-Tag: v2.0.0rc1~112^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=67fc7ed876e7244f0b7b1d55c7f7e57fa69fcaaf;p=thirdparty%2Fdnspython.git fix recvfrom, tls timing, and other misc things --- diff --git a/dns/_asyncio_backend.py b/dns/_asyncio_backend.py index 5f14c4a3..f82eb823 100644 --- a/dns/_asyncio_backend.py +++ b/dns/_asyncio_backend.py @@ -61,7 +61,8 @@ class DatagramSocket(dns._asyncbackend.DatagramSocket): # no timeout for asyncio sendto self.transport.sendto(what, destination) - async def recvfrom(self, timeout): + async def recvfrom(self, size, timeout): + # ignore size as there's no way I know to tell protocol about it done = _get_running_loop().create_future() assert self.protocol.recvfrom is None self.protocol.recvfrom = done diff --git a/dns/_curio_backend.py b/dns/_curio_backend.py index 5f6877ed..699276d3 100644 --- a/dns/_curio_backend.py +++ b/dns/_curio_backend.py @@ -31,9 +31,9 @@ class DatagramSocket(dns._asyncbackend.DatagramSocket): return await self.socket.sendto(what, destination) raise dns.exception.Timeout(timeout=timeout) - async def recvfrom(self, timeout): + async def recvfrom(self, size, timeout): async with _maybe_timeout(timeout): - return await self.socket.recvfrom(65535) + return await self.socket.recvfrom(size) raise dns.exception.Timeout(timeout=timeout) async def close(self): @@ -53,9 +53,9 @@ class StreamSocket(dns._asyncbackend.DatagramSocket): return await self.socket.sendall(what) raise dns.exception.Timeout(timeout=timeout) - async def recv(self, count, timeout): + async def recv(self, size, timeout): async with _maybe_timeout(timeout): - return await self.socket.recv(count) + return await self.socket.recv(size) raise dns.exception.Timeout(timeout=timeout) async def close(self): diff --git a/dns/_trio_backend.py b/dns/_trio_backend.py index d6a93873..04915111 100644 --- a/dns/_trio_backend.py +++ b/dns/_trio_backend.py @@ -31,9 +31,9 @@ class DatagramSocket(dns._asyncbackend.DatagramSocket): return await self.socket.sendto(what, destination) raise dns.exception.Timeout(timeout=timeout) - async def recvfrom(self, timeout): + async def recvfrom(self, size, timeout): with _maybe_timeout(timeout): - return await self.socket.recvfrom(65535) + return await self.socket.recvfrom(size) raise dns.exception.Timeout(timeout=timeout) async def close(self): @@ -54,9 +54,9 @@ class StreamSocket(dns._asyncbackend.DatagramSocket): return await self.stream.send_all(what) raise dns.exception.Timeout(timeout=timeout) - async def recv(self, count, timeout): + async def recv(self, size, timeout): with _maybe_timeout(timeout): - return await self.stream.receive_some(count) + return await self.stream.receive_some(size) raise dns.exception.Timeout(timeout=timeout) async def close(self): @@ -94,7 +94,6 @@ class Backend(dns._asyncbackend.Backend): s = None tls = False if ssl_context: - print('TLS') tls = True try: stream = trio.SSLStream(stream, ssl_context, @@ -102,7 +101,6 @@ class Backend(dns._asyncbackend.Backend): except Exception: await stream.aclose() raise - print('SOCKET') return StreamSocket(af, stream, tls) raise NotImplementedError(f'unsupported socket type {socktype}') diff --git a/dns/asyncquery.py b/dns/asyncquery.py index 47a4ff06..d1c17933 100644 --- a/dns/asyncquery.py +++ b/dns/asyncquery.py @@ -126,7 +126,7 @@ async def receive_udp(sock, destination, expiration=None, wire = b'' while 1: - (wire, from_address) = await sock.recvfrom(65535) + (wire, from_address) = await sock.recvfrom(65535, _timeout(expiration)) if _addresses_equal(sock.family, from_address, destination) or \ (dns.inet.is_multicast(destination[0]) and from_address[1:] == destination[1:]): @@ -179,7 +179,7 @@ async def udp(q, where, timeout=None, port=53, source=None, source_port=0, *sock*, a ``dns.asyncbackend.DatagramSocket``, or ``None``, the socket to use for the query. If ``None``, the default, a socket is created. Note that if a socket is provided, the - *source* and *source_port* are ignored. + *source*, *source_port*, and *backend* are ignored. *backend*, a ``dns.asyncbackend.Backend``, or ``None``. If ``None``, the default, then dnspython will use the default backend. @@ -248,13 +248,13 @@ async def udp_with_fallback(q, where, timeout=None, port=53, source=None, *udp_sock*, a ``dns.asyncbackend.DatagramSocket``, or ``None``, the socket to use for the UDP query. If ``None``, the default, a - socket is created. Note that if a socket is provided the *source* - and *source_port* are ignored for the UDP query. + socket is created. Note that if a socket is provided the *source*, + *source_port*, and *backend* are ignored for the UDP query. *tcp_sock*, a ``dns.asyncbackend.StreamSocket``, or ``None``, the socket to use for the TCP query. If ``None``, the default, a socket is created. Note that if a socket is provided *where*, - *source* and *source_port* are ignored for the TCP query. + *source*, *source_port*, and *backend* are ignored for the TCP query. *backend*, a ``dns.asyncbackend.Backend``, or ``None``. If ``None``, the default, then dnspython will use the default backend. @@ -380,7 +380,7 @@ async def tcp(q, where, timeout=None, port=53, source=None, source_port=0, *sock*, a ``dns.asyncbacket.StreamSocket``, or ``None``, the socket to use for the query. If ``None``, the default, a socket is created. Note that if a socket is provided - *where*, *port*, *source* and *source_port* are ignored. + *where*, *port*, *source*, *source_port*, and *backend* are ignored. *backend*, a ``dns.asyncbackend.Backend``, or ``None``. If ``None``, the default, then dnspython will use the default backend. @@ -452,7 +452,8 @@ async def tls(q, where, timeout=None, port=853, source=None, source_port=0, to use for the query. If ``None``, the default, a socket is created. Note that if a socket is provided, it must be a connected SSL stream socket, and *where*, *port*, - *source*, *source_port*, and *ssl_context* are ignored. + *source*, *source_port*, *backend*, *ssl_context*, and *server_hostname* + are ignored. *backend*, a ``dns.asyncbackend.Backend``, or ``None``. If ``None``, the default, then dnspython will use the default backend. @@ -469,6 +470,7 @@ async def tls(q, where, timeout=None, port=853, source=None, source_port=0, """ if not backend: backend = dns.asyncbackend.get_default_backend() + (begin_time, expiration) = _compute_times(timeout) if not sock: if ssl_context is None: ssl_context = ssl.create_default_context() @@ -489,8 +491,12 @@ async def tls(q, where, timeout=None, port=853, source=None, source_port=0, # # If a socket was provided, there's no special TLS handling needed. # - return await tcp(q, where, timeout, port, source, source_port, - one_rr_per_rrset, ignore_trailing, s, backend) + timeout = _timeout(expiration) + response = await tcp(q, where, timeout, port, source, source_port, + one_rr_per_rrset, ignore_trailing, s, backend) + end_time = time.time() + response.time = end_time - begin_time + return response finally: if not sock and s: await s.close()