xref: /openbsd-src/usr.sbin/tcpdump/nameser.h (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /* @(#) $Id: nameser.h,v 1.5 2007/10/07 16:41:05 deraadt Exp $ (LBL) */
2 /*
3  * Copyright (c) 1983, 1989, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *      @(#)nameser.h	8.2 (Berkeley) 2/16/94
31  * -
32  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
33  *
34  * Permission to use, copy, modify, and distribute this software for any
35  * purpose with or without fee is hereby granted, provided that the above
36  * copyright notice and this permission notice appear in all copies, and that
37  * the name of Digital Equipment Corporation not be used in advertising or
38  * publicity pertaining to distribution of the document or software without
39  * specific, written prior permission.
40  *
41  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
42  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
43  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
44  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
45  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
46  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
47  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
48  * SOFTWARE.
49  * -
50  * --Copyright--
51  */
52 
53 #ifndef _NAMESER_H_
54 #define	_NAMESER_H_
55 
56 #include <sys/types.h>
57 
58 /*
59  * Define constants based on rfc883
60  */
61 #define PACKETSZ	512		/* maximum packet size */
62 #define MAXDNAME	256		/* maximum domain name */
63 #define MAXCDNAME	255		/* maximum compressed domain name */
64 #define MAXLABEL	63		/* maximum length of domain label */
65 	/* Number of bytes of fixed size data in query structure */
66 #define QFIXEDSZ	4
67 	/* number of bytes of fixed size data in resource record */
68 #define RRFIXEDSZ	10
69 
70 /*
71  * Internet nameserver port number
72  */
73 #define NAMESERVER_PORT	53
74 #define MULTICASTDNS_PORT	5353
75 
76 /*
77  * Currently defined opcodes
78  */
79 #define QUERY		0x0		/* standard query */
80 #define IQUERY		0x1		/* inverse query */
81 #define STATUS		0x2		/* nameserver status query */
82 #if 0
83 #define xxx		0x3		/* 0x3 reserved */
84 #endif
85 	/* non standard - supports ALLOW_UPDATES stuff from Mike Schwartz */
86 #define UPDATEA		0x9		/* add resource record */
87 #define UPDATED		0xa		/* delete a specific resource record */
88 #define UPDATEDA	0xb		/* delete all named resource record */
89 #define UPDATEM		0xc		/* modify a specific resource record */
90 #define UPDATEMA	0xd		/* modify all named resource record */
91 
92 #define ZONEINIT	0xe		/* initial zone transfer */
93 #define ZONEREF		0xf		/* incremental zone referesh */
94 
95 /*
96  * Currently defined response codes
97  */
98 #define NOERROR		0		/* no error */
99 #define FORMERR		1		/* format error */
100 #define SERVFAIL	2		/* server failure */
101 #define NXDOMAIN	3		/* non existent domain */
102 #define NOTIMP		4		/* not implemented */
103 #define REFUSED		5		/* query refused */
104 	/* non standard */
105 #define NOCHANGE	0xf		/* update failed to change db */
106 
107 /*
108  * Type values for resources and queries
109  */
110 #define T_A		1		/* host address */
111 #define T_NS		2		/* authoritative server */
112 #define T_MD		3		/* mail destination */
113 #define T_MF		4		/* mail forwarder */
114 #define T_CNAME		5		/* connonical name */
115 #define T_SOA		6		/* start of authority zone */
116 #define T_MB		7		/* mailbox domain name */
117 #define T_MG		8		/* mail group member */
118 #define T_MR		9		/* mail rename name */
119 #define T_NULL		10		/* null resource record */
120 #define T_WKS		11		/* well known service */
121 #define T_PTR		12		/* domain name pointer */
122 #define T_HINFO		13		/* host information */
123 #define T_MINFO		14		/* mailbox information */
124 #define T_MX		15		/* mail routing information */
125 #define T_TXT		16		/* text strings */
126 #define	T_RP		17		/* responsible person */
127 #define	T_AFSDB		18		/* AFS cell database */
128 #define T_X25		19		/* X_25 calling address */
129 #define T_ISDN		20		/* ISDN calling address */
130 #define T_RT		21		/* router */
131 #define	T_NSAP		22		/* NSAP address */
132 #define	T_NSAP_PTR	23		/* reverse lookup for NSAP */
133 #define T_SIG		24		/* security signature */
134 #define T_KEY		25		/* security key */
135 #define T_PX		26		/* X.400 mail mapping */
136 #define T_GPOS		27		/* geographical position (withdrawn) */
137 #define T_AAAA		28		/* IP6 Address */
138 #define T_LOC		29		/* Location Information */
139 #define T_NXT		30		/* Next Valid Name in Zone */
140 #define T_EID		31		/* Endpoint identifier */
141 #define T_NIMLOC	32		/* Nimrod locator */
142 #define T_SRV		33		/* Server selection */
143 #define T_ATMA		34		/* ATM Address */
144 #define T_NAPTR		35		/* Naming Authority PoinTeR */
145 #define T_KX		36		/* Key Exchanger */
146 #define T_CERT		37		/* certificate */
147 #define T_A6		38		/* IP6 address */
148 #define T_DNAME		39		/* non-terminal redirection */
149 #define T_SINK		40		/* SINK */
150 #define T_OPT		41		/* EDNS0 option (meta-RR) */
151 #define T_APL		42		/* APL */
152 #define T_DS		43		/* Delegation Signer */
153 #define T_SSHFP		44		/* SSH Key Fingerprint */
154 #define T_RRSIG		46		/* RRSIG */
155 #define T_NSEC		47		/* NSEC */
156 #define T_DNSKEY	48		/* DNSKEY */
157 	/* non standard */
158 #define T_UINFO		100		/* user (finger) information */
159 #define T_UID		101		/* user ID */
160 #define T_GID		102		/* group ID */
161 #define T_UNSPEC	103		/* Unspecified format (binary data) */
162 #define T_UNSPECA	104		/* "unspecified ascii". Ugly MIT hack */
163 	/* Query type values which do not appear in resource records */
164 #define T_TKEY		249		/* Transaction Key [RFC2930] */
165 #define T_TSIG		250		/* Transaction Signature [RFC2845] */
166 #define T_IXFR		251		/* incremental transfer [RFC1995] */
167 #define T_AXFR		252		/* transfer zone of authority */
168 #define T_MAILB		253		/* transfer mailbox records */
169 #define T_MAILA		254		/* transfer mail agent records */
170 #define T_ANY		255		/* wildcard match */
171 
172 /*
173  * Values for class field
174  */
175 
176 #define C_IN		1		/* the arpa internet */
177 #define C_CHAOS		3		/* for chaos net (MIT) */
178 #define C_HS		4		/* for Hesiod name server (MIT) (XXX) */
179 	/* Query class values which do not appear in resource records */
180 #define C_ANY		255		/* wildcard match */
181 #define C_CACHE_FLUSH	0x8000		/* mDNS cache flush flag */
182 
183 /*
184  * Status return codes for T_UNSPEC conversion routines
185  */
186 #define CONV_SUCCESS 0
187 #define CONV_OVERFLOW -1
188 #define CONV_BADFMT -2
189 #define CONV_BADCKSUM -3
190 #define CONV_BADBUFLEN -4
191 
192 /*
193  * Structure for query header.
194  */
195 typedef struct {
196 	u_int16_t id;		/* query identification number */
197 	u_int8_t  flags1;	/* first byte of flags */
198 	u_int8_t  flags2;	/* second byte of flags */
199 	u_int16_t qdcount;	/* number of question entries */
200 	u_int16_t ancount;	/* number of answer entries */
201 	u_int16_t nscount;	/* number of authority entries */
202 	u_int16_t arcount;	/* number of resource entries */
203 } HEADER;
204 
205 /*
206  * Macros for subfields of flag fields.
207  */
208 #define DNS_QR(np)	((np)->flags1 & 0x80)		/* response flag */
209 #define DNS_OPCODE(np)	((((np)->flags1) >> 3) & 0xF)	/* purpose of message */
210 #define DNS_AA(np)	((np)->flags1 & 0x04)		/* authoritative answer */
211 #define DNS_TC(np)	((np)->flags1 & 0x02)		/* truncated message */
212 #define DNS_RD(np)	((np)->flags1 & 0x01)		/* recursion desired */
213 
214 #define DNS_RA(np)	((np)->flags2 & 0x80)	/* recursion available */
215 #define DNS_AD(np)	((np)->flags2 & 0x20)	/* authentic data from named */
216 #define DNS_CD(np)	((np)->flags2 & 0x10)	/* checking disabled by resolver */
217 #define DNS_RCODE(np)	((np)->flags2 & 0xF)	/* response code */
218 
219 /*
220  * Defines for handling compressed domain names, EDNS0 labels, etc.
221  */
222 #define INDIR_MASK	0xc0	/* 11.... */
223 #define EDNS0_MASK	0x40	/* 01.... */
224 #  define EDNS0_ELT_BITLABEL 0x01
225 
226 /*
227  * Structure for passing resource records around.
228  */
229 struct rrec {
230 	int16_t	r_zone;			/* zone number */
231 	int16_t	r_class;		/* class number */
232 	int16_t	r_type;			/* type number */
233 	u_int32_t	r_ttl;			/* time to live */
234 	int	r_size;			/* size of data area */
235 	char	*r_data;		/* pointer to data */
236 };
237 
238 /*
239  * Inline versions of get/put short/long.  Pointer is advanced.
240  * We also assume that a "u_int16_t" holds 2 "chars"
241  * and that a "u_int32_t" holds 4 "chars".
242  *
243  * These macros demonstrate the property of C whereby it can be
244  * portable or it can be elegant but never both.
245  */
246 #define GETSHORT(s, cp) { \
247 	register u_char *t_cp = (u_char *)(cp); \
248 	(s) = ((u_int16_t)t_cp[0] << 8) | (u_int16_t)t_cp[1]; \
249 	(cp) += 2; \
250 }
251 
252 #define GETLONG(l, cp) { \
253 	register u_char *t_cp = (u_char *)(cp); \
254 	(l) = (((u_int32_t)t_cp[0]) << 24) \
255 	    | (((u_int32_t)t_cp[1]) << 16) \
256 	    | (((u_int32_t)t_cp[2]) << 8) \
257 	    | (((u_int32_t)t_cp[3])); \
258 	(cp) += 4; \
259 }
260 
261 #define PUTSHORT(s, cp) { \
262 	register u_int16_t t_s = (u_int16_t)(s); \
263 	register u_char *t_cp = (u_char *)(cp); \
264 	*t_cp++ = t_s >> 8; \
265 	*t_cp   = t_s; \
266 	(cp) += 2; \
267 }
268 
269 /*
270  * Warning: PUTLONG --no-longer-- destroys its first argument.  if you
271  * were depending on this "feature", you will lose.
272  */
273 #define PUTLONG(l, cp) { \
274 	register u_int32_t t_l = (u_int32_t)(l); \
275 	register u_char *t_cp = (u_char *)(cp); \
276 	*t_cp++ = t_l >> 24; \
277 	*t_cp++ = t_l >> 16; \
278 	*t_cp++ = t_l >> 8; \
279 	*t_cp   = t_l; \
280 	(cp) += 4; \
281 }
282 
283 #endif /* !_NAMESER_H_ */
284