#!/bin/sh
#
# Copyright (C) 2026 iopsys Software Solutions AB
#
# See LICENSE file for license related information.
#
# DNS probe — TR-181 Method "DNS" (test DNS resolution / general Internet access
# by querying a DNS server). Unlike Ping, this performs an actual DNS lookup.
#
# Positional contract (see Ping):
#   $1 interface  $2 dest (= DNS server IP/FQDN, or "" for the system resolver)
#   $3 ipversion  $4 timeout  $5 dscp  $6 priority
# Exit 0 = query answered (Up), non-zero = no answer (Unresponsive).
#
# The name to resolve is fixed here; an integrator may change QUERY to a domain
# appropriate for the deployment.

. /usr/share/connmon/functions.sh

iface="$1"; server="$2"; timeout="${4:-1}"
QUERY="example.com"

[ "$timeout" -ge 1 ] 2>/dev/null || timeout=1

command -v nslookup >/dev/null 2>&1 || {
	log_err "nslookup not available for iface '$iface'"
	exit 1
}

# Bound the lookup by the response timeout when `timeout` is available.
RUN=""
command -v timeout >/dev/null 2>&1 && RUN="timeout $timeout"

# nslookup <name> [server]; an empty server uses the system resolver.
if [ -n "$server" ]; then
	set -- "$QUERY" "$server"
else
	set -- "$QUERY"
fi

# shellcheck disable=SC2086
$RUN nslookup "$@" >/dev/null 2>&1 && exit 0
exit 1
