2023-12-04 18:05:50 +01:00
|
|
|
# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: MPL-2.0
|
|
|
|
#
|
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
#
|
|
|
|
# See the COPYRIGHT file distributed with this work for additional
|
|
|
|
# information regarding copyright ownership.
|
|
|
|
|
|
|
|
import os
|
2024-07-16 20:06:06 +02:00
|
|
|
import time
|
|
|
|
from typing import Any, Callable, Optional
|
2023-12-04 18:05:50 +01:00
|
|
|
|
|
|
|
import dns.query
|
|
|
|
import dns.message
|
|
|
|
|
2024-07-16 20:06:06 +02:00
|
|
|
import isctest.log
|
2024-08-27 20:14:00 +02:00
|
|
|
from isctest.compat import dns_rcode
|
2023-12-04 18:05:50 +01:00
|
|
|
|
|
|
|
QUERY_TIMEOUT = 10
|
|
|
|
|
|
|
|
|
2024-07-16 20:06:06 +02:00
|
|
|
# pylint: disable=too-many-arguments
|
|
|
|
def generic_query(
|
|
|
|
query_func: Callable[..., Any],
|
2024-01-31 19:11:16 +01:00
|
|
|
message: dns.message.Message,
|
|
|
|
ip: str,
|
|
|
|
port: Optional[int] = None,
|
|
|
|
source: Optional[str] = None,
|
2023-12-21 20:25:20 +01:00
|
|
|
timeout: int = QUERY_TIMEOUT,
|
2024-07-16 20:06:06 +02:00
|
|
|
attempts: int = 10,
|
|
|
|
expected_rcode: dns_rcode = None,
|
|
|
|
) -> Any:
|
2023-12-04 18:05:50 +01:00
|
|
|
if port is None:
|
|
|
|
port = int(os.environ["PORT"])
|
2024-07-16 20:06:06 +02:00
|
|
|
res = None
|
|
|
|
for attempt in range(attempts):
|
|
|
|
try:
|
|
|
|
isctest.log.debug(
|
2024-09-16 14:43:22 +02:00
|
|
|
f"{query_func.__name__}(): ip={ip}, port={port}, source={source}, "
|
2024-07-16 20:06:06 +02:00
|
|
|
f"timeout={timeout}, attempts left={attempts-attempt}"
|
|
|
|
)
|
|
|
|
res = query_func(message, ip, timeout, port=port, source=source)
|
|
|
|
if res.rcode() == expected_rcode or expected_rcode is None:
|
|
|
|
return res
|
|
|
|
except (dns.exception.Timeout, ConnectionRefusedError) as e:
|
2024-09-16 14:43:22 +02:00
|
|
|
isctest.log.debug(f"{query_func.__name__}(): the '{e}' exceptio raised")
|
2024-07-16 20:06:06 +02:00
|
|
|
time.sleep(1)
|
|
|
|
raise dns.exception.Timeout
|
2023-12-04 18:05:50 +01:00
|
|
|
|
|
|
|
|
2024-07-16 20:06:06 +02:00
|
|
|
def udp(*args, **kwargs) -> Any:
|
|
|
|
return generic_query(dns.query.udp, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def tcp(*args, **kwargs) -> Any:
|
|
|
|
return generic_query(dns.query.tcp, *args, **kwargs)
|