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