190075Sobrien/*	$NetBSD$	*/
290075Sobrien
3132718Skan/*++
490075Sobrien/* NAME
590075Sobrien/*	dns_strerror 3
690075Sobrien/* SUMMARY
790075Sobrien/*	name service lookup error code to string
890075Sobrien/* SYNOPSIS
990075Sobrien/*	#include <dhs.h>
1090075Sobrien/*
1190075Sobrien/*	const char *dns_strerror(code)
1290075Sobrien/*	int	code;
1390075Sobrien/* DESCRIPTION
1490075Sobrien/*	dns_strerror() maps a name service lookup error to printable string.
1590075Sobrien/*	The result is for read-only purposes, and unknown codes share a
1690075Sobrien/*	common string buffer.
1790075Sobrien/* LICENSE
1890075Sobrien/* .ad
1990075Sobrien/* .fi
2090075Sobrien/*	The Secure Mailer license must be distributed with this software.
2190075Sobrien/* AUTHOR(S)
2290075Sobrien/*	Wietse Venema
2390075Sobrien/*	IBM T.J. Watson Research
2490075Sobrien/*	P.O. Box 704
2590075Sobrien/*	Yorktown Heights, NY 10598, USA
2690075Sobrien/*--*/
2790075Sobrien
2890075Sobrien/* System library. */
2990075Sobrien
3090075Sobrien#include <sys_defs.h>
3190075Sobrien#include <netdb.h>
3290075Sobrien
3390075Sobrien/* Utility library. */
3490075Sobrien
3590075Sobrien#include <vstring.h>
36132718Skan
37132718Skan/* DNS library. */
3890075Sobrien
3990075Sobrien#include "dns.h"
4090075Sobrien
4190075Sobrien /*
4290075Sobrien  * Mapping from error code to printable string. The herror() routine does
4390075Sobrien  * something similar, but has output only to the stderr stream.
4490075Sobrien  */
4590075Sobrienstruct dns_error_map {
4690075Sobrien    unsigned error;
4790075Sobrien    const char *text;
48117395Skan};
4990075Sobrien
5096263Sobrienstatic struct dns_error_map dns_error_map[] = {
51132718Skan    HOST_NOT_FOUND, "Host not found",
5290075Sobrien    TRY_AGAIN, "Host not found, try again",
5390075Sobrien    NO_RECOVERY, "Non-recoverable error",
5490075Sobrien    NO_DATA, "Host found but no data record of requested type",
5590075Sobrien};
5690075Sobrien
5790075Sobrien/* dns_strerror - map resolver error code to printable string */
5890075Sobrien
59117395Skanconst char *dns_strerror(unsigned error)
60117395Skan{
6190075Sobrien    static VSTRING *unknown = 0;
6290075Sobrien    unsigned i;
6390075Sobrien
6490075Sobrien    for (i = 0; i < sizeof(dns_error_map) / sizeof(dns_error_map[0]); i++)
6590075Sobrien	if (dns_error_map[i].error == error)
6690075Sobrien	    return (dns_error_map[i].text);
6790075Sobrien    if (unknown == 0)
6890075Sobrien	unknown = vstring_alloc(sizeof("Unknown error XXXXXX"));
6990075Sobrien    vstring_sprintf(unknown, "Unknown error %u", error);
7090075Sobrien    return (vstring_str(unknown));
71132718Skan}
72132718Skan