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