dns_strerror.c revision 1.1.1.1
1/*	$NetBSD: dns_strerror.c,v 1.1.1.1 2009/06/23 10:08:43 tron Exp $	*/
2
3/*++
4/* NAME
5/*	dns_strerror 3
6/* SUMMARY
7/*	name service lookup error code to string
8/* SYNOPSIS
9/*	#include <dhs.h>
10/*
11/*	const char *dns_strerror(code)
12/*	int	code;
13/* DESCRIPTION
14/*	dns_strerror() maps a name service lookup error to printable string.
15/*	The result is for read-only purposes, and unknown codes share a
16/*	common string buffer.
17/* LICENSE
18/* .ad
19/* .fi
20/*	The Secure Mailer license must be distributed with this software.
21/* AUTHOR(S)
22/*	Wietse Venema
23/*	IBM T.J. Watson Research
24/*	P.O. Box 704
25/*	Yorktown Heights, NY 10598, USA
26/*--*/
27
28/* System library. */
29
30#include <sys_defs.h>
31#include <netdb.h>
32
33/* Utility library. */
34
35#include <vstring.h>
36
37/* DNS library. */
38
39#include "dns.h"
40
41 /*
42  * Mapping from error code to printable string. The herror() routine does
43  * something similar, but has output only to the stderr stream.
44  */
45struct dns_error_map {
46    unsigned error;
47    const char *text;
48};
49
50static struct dns_error_map dns_error_map[] = {
51    HOST_NOT_FOUND, "Host not found",
52    TRY_AGAIN, "Host not found, try again",
53    NO_RECOVERY, "Non-recoverable error",
54    NO_DATA, "Host found but no data record of requested type",
55};
56
57/* dns_strerror - map resolver error code to printable string */
58
59const char *dns_strerror(unsigned error)
60{
61    static VSTRING *unknown = 0;
62    unsigned i;
63
64    for (i = 0; i < sizeof(dns_error_map) / sizeof(dns_error_map[0]); i++)
65	if (dns_error_map[i].error == error)
66	    return (dns_error_map[i].text);
67    if (unknown == 0)
68	unknown = vstring_alloc(sizeof("Unknown error XXXXXX"));
69    vstring_sprintf(unknown, "Unknown error %u", error);
70    return (vstring_str(unknown));
71}
72