1/*++
2/* NAME
3/*	dns_strtype 3
4/* SUMMARY
5/*	name service lookup type codes and printable forms
6/* SYNOPSIS
7/*	#include <dns.h>
8/*
9/*	const char *dns_strtype(code)
10/*	int	code;
11/*
12/*	int	dns_type(strval)
13/*	const char *strval;
14/* DESCRIPTION
15/*	dns_strtype() maps a name service lookup type to printable string.
16/*	The result is for read-only purposes, and unknown codes share a
17/*	common string buffer.
18/*
19/*	dns_type() converts a name service lookup string value to a numeric
20/*	code. A null result means the code was not found. The input can be
21/*	in lower case, upper case or mixed case.
22/* LICENSE
23/* .ad
24/* .fi
25/*	The Secure Mailer license must be distributed with this software.
26/* AUTHOR(S)
27/*	Wietse Venema
28/*	IBM T.J. Watson Research
29/*	P.O. Box 704
30/*	Yorktown Heights, NY 10598, USA
31/*--*/
32
33/* System library. */
34
35#include <sys_defs.h>
36#include <string.h>
37
38#ifdef STRCASECMP_IN_STRINGS_H
39#include <strings.h>
40#endif
41
42/* Utility library. */
43
44#include <vstring.h>
45
46/* DNS library. */
47
48#include "dns.h"
49
50 /*
51  * Mapping from type code to printable string. Some names are possibly not
52  * defined on every platform, so I have #ifdef-ed them all just to be safe.
53  */
54struct dns_type_map {
55    unsigned type;
56    const char *text;
57};
58
59static struct dns_type_map dns_type_map[] = {
60#ifdef T_A
61    T_A, "A",
62#endif
63#ifdef T_AAAA
64    T_AAAA, "AAAA",
65#endif
66#ifdef T_NS
67    T_NS, "NS",
68#endif
69#ifdef T_MD
70    T_MD, "MD",
71#endif
72#ifdef T_MF
73    T_MF, "MF",
74#endif
75#ifdef T_CNAME
76    T_CNAME, "CNAME",
77#endif
78#ifdef T_SOA
79    T_SOA, "SOA",
80#endif
81#ifdef T_MB
82    T_MB, "MB",
83#endif
84#ifdef T_MG
85    T_MG, "MG",
86#endif
87#ifdef T_MR
88    T_MR, "MR",
89#endif
90#ifdef T_NULL
91    T_NULL, "NULL",
92#endif
93#ifdef T_WKS
94    T_WKS, "WKS",
95#endif
96#ifdef T_PTR
97    T_PTR, "PTR",
98#endif
99#ifdef T_HINFO
100    T_HINFO, "HINFO",
101#endif
102#ifdef T_MINFO
103    T_MINFO, "MINFO",
104#endif
105#ifdef T_MX
106    T_MX, "MX",
107#endif
108#ifdef T_TXT
109    T_TXT, "TXT",
110#endif
111#ifdef T_RP
112    T_RP, "RP",
113#endif
114#ifdef T_AFSDB
115    T_AFSDB, "AFSDB",
116#endif
117#ifdef T_X25
118    T_X25, "X25",
119#endif
120#ifdef T_ISDN
121    T_ISDN, "ISDN",
122#endif
123#ifdef T_RT
124    T_RT, "RT",
125#endif
126#ifdef T_NSAP
127    T_NSAP, "NSAP",
128#endif
129#ifdef T_NSAP_PTR
130    T_NSAP_PTR, "NSAP_PTR",
131#endif
132#ifdef T_SIG
133    T_SIG, "SIG",
134#endif
135#ifdef T_KEY
136    T_KEY, "KEY",
137#endif
138#ifdef T_PX
139    T_PX, "PX",
140#endif
141#ifdef T_GPOS
142    T_GPOS, "GPOS",
143#endif
144#ifdef T_AAAA
145    T_AAAA, "AAAA",
146#endif
147#ifdef T_LOC
148    T_LOC, "LOC",
149#endif
150#ifdef T_UINFO
151    T_UINFO, "UINFO",
152#endif
153#ifdef T_UID
154    T_UID, "UID",
155#endif
156#ifdef T_GID
157    T_GID, "GID",
158#endif
159#ifdef T_UNSPEC
160    T_UNSPEC, "UNSPEC",
161#endif
162#ifdef T_AXFR
163    T_AXFR, "AXFR",
164#endif
165#ifdef T_MAILB
166    T_MAILB, "MAILB",
167#endif
168#ifdef T_MAILA
169    T_MAILA, "MAILA",
170#endif
171#ifdef T_TLSA
172    T_TLSA, "TLSA",
173#endif
174#ifdef T_RRSIG
175    T_RRSIG, "RRSIG",
176#endif
177#ifdef T_DNAME
178    T_DNAME, "DNAME",
179#endif
180#ifdef T_ANY
181    T_ANY, "ANY",
182#endif
183};
184
185/* dns_strtype - translate DNS query type to string */
186
187const char *dns_strtype(unsigned type)
188{
189    static VSTRING *unknown = 0;
190    unsigned i;
191
192    for (i = 0; i < sizeof(dns_type_map) / sizeof(dns_type_map[0]); i++)
193	if (dns_type_map[i].type == type)
194	    return (dns_type_map[i].text);
195    if (unknown == 0)
196	unknown = vstring_alloc(sizeof("Unknown type XXXXXX"));
197    vstring_sprintf(unknown, "Unknown type %u", type);
198    return (vstring_str(unknown));
199}
200
201/* dns_type - translate string to DNS query type */
202
203unsigned dns_type(const char *text)
204{
205    unsigned i;
206
207    for (i = 0; i < sizeof(dns_type_map) / sizeof(dns_type_map[0]); i++)
208	if (strcasecmp(dns_type_map[i].text, text) == 0)
209	    return (dns_type_map[i].type);
210    return (0);
211}
212