1/*
2 * Copyright 2006-2020, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5
6/*
7 * Portions Copyright (C) 2004, 2005, 2008, 2009  Internet Systems Consortium, Inc. ("ISC")
8 * Portions Copyright (C) 1996-2003  Internet Software Consortium.
9 *
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
15 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
17 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
18 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
19 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
20 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
21 * SOFTWARE.
22 */
23
24/*
25 * Copyright (c) 1983, 1989, 1993
26 *    The Regents of the University of California.  All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 *    notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 *    notice, this list of conditions and the following disclaimer in the
35 *    documentation and/or other materials provided with the distribution.
36 * 3. Neither the name of the University nor the names of its contributors
37 *    may be used to endorse or promote products derived from this software
38 *    without specific prior written permission.
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 */
52#ifndef _ARPA_NAMESER_H_
53#define _ARPA_NAMESER_H_
54
55#include <sys/types.h>
56
57#ifdef __cplusplus
58extern "C" {
59#endif
60
61/*
62 * Define constants based on RFC 883, RFC 1034, RFC 1035
63 */
64#define NS_PACKETSZ		512		/* default UDP packet size */
65#define NS_MAXDNAME		1025	/* maximum domain name */
66#define NS_MAXMSG		65535	/* maximum message size */
67#define NS_MAXCDNAME	255		/* maximum compressed domain name */
68#define NS_MAXLABEL		63		/* maximum length of domain label */
69#define NS_MAXLABELS	128		/* theoretical max #/labels per domain name */
70#define NS_MAXNNAME		256		/* maximum uncompressed (binary) domain name */
71#define NS_MAXPADDR		40
72#define NS_HFIXEDSZ		12		/* #/bytes of fixed data in header */
73#define NS_QFIXEDSZ		4		/* #/bytes of fixed data in query */
74#define NS_RRFIXEDSZ	10		/* #/bytes of fixed data in r record */
75#define NS_INT32SZ		4		/* #/bytes of data in a uint32_t */
76#define NS_INT16SZ		2		/* #/bytes of data in a uint16_t */
77#define NS_INT8SZ		1		/* #/bytes of data in a uint8_t */
78#define NS_INADDRSZ		4		/* IPv4 T_A */
79#define NS_IN6ADDRSZ	16		/* IPv6 T_AAAA */
80#define NS_CMPRSFLGS	0xc0	/* Flag bits indicating name compression. */
81#define NS_DEFAULTPORT	53		/* For both TCP and UDP. */
82
83/*
84 * These can be expanded with synonyms, just keep ns_parse.c:ns_parserecord()
85 * in synch with it.
86 */
87typedef enum __ns_sect {
88	ns_s_qd = 0,		/* Query: Question. */
89	ns_s_zn = 0,		/* Update: Zone. */
90	ns_s_an = 1,		/* Query: Answer. */
91	ns_s_pr = 1,		/* Update: Prerequisites. */
92	ns_s_ns = 2,		/* Query: Name servers. */
93	ns_s_ud = 2,		/* Update: Update. */
94	ns_s_ar = 3,		/* Query|Update: Additional records. */
95	ns_s_max = 4
96} ns_sect;
97
98/*
99 * Network name (compressed or not) type. Equivalent to a pointer when used
100 * in a function prototype. Can be const'd.
101 */
102typedef unsigned char ns_nname[NS_MAXNNAME];
103typedef const unsigned char *ns_nname_ct;
104typedef unsigned char *ns_nname_t;
105
106struct ns_namemap { ns_nname_ct base; int len; };
107typedef struct ns_namemap *ns_namemap_t;
108typedef const struct ns_namemap *ns_namemap_ct;
109
110/*
111 * This is a message handle.  It is caller allocated and has no dynamic data.
112 * This structure is intended to be opaque to all but ns_parse.c, thus the
113 * leading _'s on the member names.  Use the accessor functions, not the _'s.
114 */
115typedef struct __ns_msg {
116	const unsigned char	*_msg;
117	const unsigned char	*_eom;
118	uint16_t		_id;
119	uint16_t		_flags;
120	uint16_t		_counts[ns_s_max];
121	const unsigned char	*_sections[ns_s_max];
122	ns_sect			_sect;
123	int				_rrnum;
124	const unsigned char	*_msg_ptr;
125} ns_msg;
126
127/*
128 * This is a newmsg handle, used when constructing new messages with
129 * ns_newmsg_init(), et al.
130 */
131struct ns_newmsg {
132	ns_msg			msg;
133	const unsigned char	*dnptrs[25];
134	const unsigned char	**lastdnptr;
135};
136typedef struct ns_newmsg ns_newmsg;
137
138/* Accessor macros - this is part of the public interface. */
139#define ns_msg_id(handle) ((handle)._id + 0)
140#define ns_msg_base(handle) ((handle)._msg + 0)
141#define ns_msg_end(handle) ((handle)._eom + 0)
142#define ns_msg_size(handle) ((handle)._eom - (handle)._msg)
143#define ns_msg_count(handle, section) ((handle)._counts[section] + 0)
144
145/* This is a parsed record. It is caller allocated and has no dynamic data. */
146typedef	struct __ns_rr {
147	char			name[NS_MAXDNAME];
148	uint16_t		type;
149	uint16_t		rr_class;
150	uint32_t		ttl;
151	uint16_t		rdlength;
152	const unsigned char	*rdata;
153} ns_rr;
154
155/* Same thing, but using uncompressed network binary names, and real C types. */
156typedef struct __ns_rr2 {
157	ns_nname		nname;
158	size_t			nnamel;
159	int				type;
160	int				rr_class;
161	unsigned int			ttl;
162	int				rdlength;
163	const unsigned char	*rdata;
164} ns_rr2;
165
166/* Accessor macros - this is part of the public interface. */
167#define ns_rr_name(rr)	(((rr).name[0] != '\0') ? (rr).name : ".")
168#define ns_rr_nname(rr)	((const ns_nname_t)(rr).nname)
169#define ns_rr_nnamel(rr) ((rr).nnamel + 0)
170#define ns_rr_type(rr)	((ns_type)((rr).type + 0))
171#define ns_rr_class(rr)	((ns_class)((rr).rr_class + 0))
172#define ns_rr_ttl(rr)	((rr).ttl + 0)
173#define ns_rr_rdlen(rr)	((rr).rdlength + 0)
174#define ns_rr_rdata(rr)	((rr).rdata + 0)
175
176/*
177 * These don't have to be in the same order as in the packet flags word,
178 * and they can even overlap in some cases, but they will need to be kept
179 * in synch with ns_parse.c:ns_flagdata[].
180 */
181typedef enum __ns_flag {
182	ns_f_qr,		/* Question/Response. */
183	ns_f_opcode,	/* Operation code. */
184	ns_f_aa,		/* Authoritative Answer. */
185	ns_f_tc,		/* Truncation occurred. */
186	ns_f_rd,		/* Recursion Desired. */
187	ns_f_ra,		/* Recursion Available. */
188	ns_f_z,			/* MBZ. */
189	ns_f_ad,		/* Authentic Data (DNSSEC). */
190	ns_f_cd,		/* Checking Disabled (DNSSEC). */
191	ns_f_rcode,		/* Response code. */
192	ns_f_max
193} ns_flag;
194
195/* Currently defined opcodes. */
196typedef enum __ns_opcode {
197	ns_o_query = 0,		/* Standard query. */
198	ns_o_iquery = 1,	/* Inverse query (deprecated/unsupported). */
199	ns_o_status = 2,	/* Name server status query (unsupported). */
200						/* Opcode 3 is undefined/reserved. */
201	ns_o_notify = 4,	/* Zone change notification. */
202	ns_o_update = 5,	/* Zone update message. */
203	ns_o_max = 6
204} ns_opcode;
205
206/* Currently defined response codes. */
207typedef	enum __ns_rcode {
208	ns_r_noerror = 0,	/* No error occurred. */
209	ns_r_formerr = 1,	/* Format error. */
210	ns_r_servfail = 2,	/* Server failure. */
211	ns_r_nxdomain = 3,	/* Name error. */
212	ns_r_notimpl = 4,	/* Unimplemented. */
213	ns_r_refused = 5,	/* Operation refused. */
214
215	/* these are for BIND_UPDATE */
216	ns_r_yxdomain = 6,	/* Name exists */
217	ns_r_yxrrset = 7,	/* RRset exists */
218	ns_r_nxrrset = 8,	/* RRset does not exist */
219	ns_r_notauth = 9,	/* Not authoritative for zone */
220	ns_r_notzone = 10,	/* Zone of record different from zone section */
221	ns_r_max = 11,
222
223	/* The following are EDNS extended rcodes */
224	ns_r_badvers = 16,
225
226	/* The following are TSIG errors */
227	ns_r_badsig = 16,
228	ns_r_badkey = 17,
229	ns_r_badtime = 18
230} ns_rcode;
231
232/* BIND_UPDATE */
233typedef enum __ns_update_operation {
234	ns_uop_delete = 0,
235	ns_uop_add = 1,
236	ns_uop_max = 2
237} ns_update_operation;
238
239/* This structure is used for TSIG authenticated messages */
240struct ns_tsig_key {
241	char			name[NS_MAXDNAME];
242	char			alg[NS_MAXDNAME];
243	unsigned char	*data;
244	int				len;
245};
246typedef struct ns_tsig_key ns_tsig_key;
247
248/* This structure is used for TSIG authenticated TCP messages */
249struct ns_tcp_tsig_state {
250	int				counter;
251	struct dst_key	*key;
252	void			*ctx;
253	unsigned char	sig[NS_PACKETSZ];
254	int				siglen;
255};
256typedef struct ns_tcp_tsig_state ns_tcp_tsig_state;
257
258#define NS_TSIG_FUDGE 300
259#define NS_TSIG_TCP_COUNT 100
260#define NS_TSIG_ALG_HMAC_MD5 "HMAC-MD5.SIG-ALG.REG.INT"
261
262#define NS_TSIG_ERROR_NO_TSIG -10
263#define NS_TSIG_ERROR_NO_SPACE -11
264#define NS_TSIG_ERROR_FORMERR -12
265
266/* Currently defined type values for resources and queries. */
267typedef enum __ns_type {
268	ns_t_invalid = 0,	/* Cookie. */
269	ns_t_a = 1,			/* Host address. */
270	ns_t_ns = 2,		/* Authoritative server. */
271	ns_t_md = 3,		/* Mail destination. */
272	ns_t_mf = 4,		/* Mail forwarder. */
273	ns_t_cname = 5,		/* Canonical name. */
274	ns_t_soa = 6,		/* Start of authority zone. */
275	ns_t_mb = 7,		/* Mailbox domain name. */
276	ns_t_mg = 8,		/* Mail group member. */
277	ns_t_mr = 9,		/* Mail rename name. */
278	ns_t_null = 10,		/* Null resource record. */
279	ns_t_wks = 11,		/* Well known service. */
280	ns_t_ptr = 12,		/* Domain name pointer. */
281	ns_t_hinfo = 13,	/* Host information. */
282	ns_t_minfo = 14,	/* Mailbox information. */
283	ns_t_mx = 15,		/* Mail routing information. */
284	ns_t_txt = 16,		/* Text strings. */
285	ns_t_rp = 17,		/* Responsible person. */
286	ns_t_afsdb = 18,	/* AFS cell database. */
287	ns_t_x25 = 19,		/* X_25 calling address. */
288	ns_t_isdn = 20,		/* ISDN calling address. */
289	ns_t_rt = 21,		/* Router. */
290	ns_t_nsap = 22,		/* NSAP address. */
291	ns_t_nsap_ptr = 23,	/* Reverse NSAP lookup (deprecated). */
292	ns_t_sig = 24,		/* Security signature. */
293	ns_t_key = 25,		/* Security key. */
294	ns_t_px = 26,		/* X.400 mail mapping. */
295	ns_t_gpos = 27,		/* Geographical position (withdrawn). */
296	ns_t_aaaa = 28,		/* Ip6 Address. */
297	ns_t_loc = 29,		/* Location Information. */
298	ns_t_nxt = 30,		/* Next domain (security). */
299	ns_t_eid = 31,		/* Endpoint identifier. */
300	ns_t_nimloc = 32,	/* Nimrod Locator. */
301	ns_t_srv = 33,		/* Server Selection. */
302	ns_t_atma = 34,		/* ATM Address */
303	ns_t_naptr = 35,	/* Naming Authority PoinTeR */
304	ns_t_kx = 36,		/* Key Exchange */
305	ns_t_cert = 37,		/* Certification record */
306	ns_t_a6 = 38,		/* IPv6 address (deprecates AAAA) */
307	ns_t_dname = 39,	/* Non-terminal DNAME (for IPv6) */
308	ns_t_sink = 40,		/* Kitchen sink (experimentatl) */
309	ns_t_opt = 41,		/* EDNS0 option (meta-RR) */
310	ns_t_apl = 42,		/* Address prefix list (RFC 3123) */
311	ns_t_ds = 43,		/* Delegation Signer */
312	ns_t_sshfp = 44,	/* SSH Fingerprint */
313	ns_t_ipseckey = 45,	/* IPSEC Key */
314	ns_t_rrsig = 46,	/* RRset Signature */
315	ns_t_nsec = 47,		/* Negative security */
316	ns_t_dnskey = 48,	/* DNS Key */
317	ns_t_dhcid = 49,	/* Dynamic host configuratin identifier */
318	ns_t_nsec3 = 50,	/* Negative security type 3 */
319	ns_t_nsec3param = 51, /* Negative security type 3 parameters */
320	ns_t_hip = 55,		/* Host Identity Protocol */
321	ns_t_spf = 99,		/* Sender Policy Framework */
322	ns_t_tkey = 249,	/* Transaction key */
323	ns_t_tsig = 250,	/* Transaction signature. */
324	ns_t_ixfr = 251,	/* Incremental zone transfer. */
325	ns_t_axfr = 252,	/* Transfer zone of authority. */
326	ns_t_mailb = 253,	/* Transfer mailbox records. */
327	ns_t_maila = 254,	/* Transfer mail agent records. */
328	ns_t_any = 255,		/* Wildcard match. */
329	ns_t_zxfr = 256,	/* BIND-specific, nonstandard. */
330	ns_t_dlv = 32769,	/* DNSSEC look-aside validatation. */
331	ns_t_max = 65536
332} ns_type;
333
334/* Exclusively a QTYPE? (not also an RTYPE) */
335#define	ns_t_qt_p(t) (ns_t_xfr_p(t) || (t) == ns_t_any || \
336		      (t) == ns_t_mailb || (t) == ns_t_maila)
337/* Some kind of meta-RR? (not a QTYPE, but also not an RTYPE) */
338#define	ns_t_mrr_p(t) ((t) == ns_t_tsig || (t) == ns_t_opt)
339/* Exclusively an RTYPE? (not also a QTYPE or a meta-RR) */
340#define ns_t_rr_p(t) (!ns_t_qt_p(t) && !ns_t_mrr_p(t))
341#define ns_t_udp_p(t) ((t) != ns_t_axfr && (t) != ns_t_zxfr)
342#define ns_t_xfr_p(t) ((t) == ns_t_axfr || (t) == ns_t_ixfr || \
343		       (t) == ns_t_zxfr)
344
345/* Values for class field */
346typedef enum __ns_class {
347	ns_c_invalid = 0,	/* Cookie. */
348	ns_c_in = 1,		/* Internet. */
349	ns_c_2 = 2,			/* unallocated/unsupported. */
350	ns_c_chaos = 3,		/* MIT Chaos-net. */
351	ns_c_hs = 4,		/* MIT Hesiod. */
352
353	/* Query class values which do not appear in resource records */
354	ns_c_none = 254,	/* for prereq. sections in update requests */
355	ns_c_any = 255,		/* Wildcard match. */
356	ns_c_max = 65536
357} ns_class;
358
359/* DNSSEC constants. */
360
361typedef enum __ns_key_types {
362	ns_kt_rsa = 1,		/* key type RSA/MD5 */
363	ns_kt_dh  = 2,		/* Diffie Hellman */
364	ns_kt_dsa = 3,		/* Digital Signature Standard (MANDATORY) */
365	ns_kt_private = 254	/* Private key type starts with OID */
366} ns_key_types;
367
368typedef enum __ns_cert_types {
369	cert_t_pkix = 1,	/* PKIX (X.509v3) */
370	cert_t_spki = 2,	/* SPKI */
371	cert_t_pgp  = 3,	/* PGP */
372	cert_t_url  = 253,	/* URL private type */
373	cert_t_oid  = 254	/* OID private type */
374} ns_cert_types;
375
376/* Flags field of the KEY RR rdata. */
377#define	NS_KEY_TYPEMASK		0xC000	/* Mask for "type" bits */
378#define	NS_KEY_TYPE_AUTH_CONF	0x0000	/* Key usable for both */
379#define	NS_KEY_TYPE_CONF_ONLY	0x8000	/* Key usable for confidentiality */
380#define	NS_KEY_TYPE_AUTH_ONLY	0x4000	/* Key usable for authentication */
381#define	NS_KEY_TYPE_NO_KEY	0xC000	/* No key usable for either; no key */
382/* The type bits can also be interpreted independently, as single bits: */
383#define	NS_KEY_NO_AUTH		0x8000	/* Key unusable for authentication */
384#define	NS_KEY_NO_CONF		0x4000	/* Key unusable for confidentiality */
385#define	NS_KEY_RESERVED2	0x2000	/* Security is *mandatory* if bit=0 */
386#define	NS_KEY_EXTENDED_FLAGS	0x1000	/* reserved - must be zero */
387#define	NS_KEY_RESERVED4	0x0800  /* reserved - must be zero */
388#define	NS_KEY_RESERVED5	0x0400  /* reserved - must be zero */
389#define	NS_KEY_NAME_TYPE	0x0300	/* these bits determine the type */
390#define	NS_KEY_NAME_USER	0x0000	/* key is assoc. with user */
391#define	NS_KEY_NAME_ENTITY	0x0200	/* key is assoc. with entity eg host */
392#define	NS_KEY_NAME_ZONE	0x0100	/* key is zone key */
393#define	NS_KEY_NAME_RESERVED	0x0300	/* reserved meaning */
394#define	NS_KEY_RESERVED8	0x0080  /* reserved - must be zero */
395#define	NS_KEY_RESERVED9	0x0040  /* reserved - must be zero */
396#define	NS_KEY_RESERVED10	0x0020  /* reserved - must be zero */
397#define	NS_KEY_RESERVED11	0x0010  /* reserved - must be zero */
398#define	NS_KEY_SIGNATORYMASK	0x000F	/* key can sign RR's of same name */
399#define	NS_KEY_RESERVED_BITMASK (NS_KEY_RESERVED2 | NS_KEY_RESERVED4 \
400	| NS_KEY_RESERVED5 | NS_KEY_RESERVED8 | NS_KEY_RESERVED9 \
401	| NS_KEY_RESERVED10 | NS_KEY_RESERVED11)
402#define NS_KEY_RESERVED_BITMASK2 0xFFFF /* no bits defined here */
403
404/* The Algorithm field of the KEY and SIG RR's is an integer, {1..254} */
405#define	NS_ALG_MD5RSA		1	/* MD5 with RSA */
406#define	NS_ALG_DH               2	/* Diffie Hellman KEY */
407#define	NS_ALG_DSA              3	/* DSA KEY */
408#define	NS_ALG_DSS              NS_ALG_DSA
409#define	NS_ALG_EXPIRE_ONLY	253	/* No alg, no security */
410#define	NS_ALG_PRIVATE_OID	254	/* Key begins with OID giving alg */
411
412/* Protocol values  */
413/* value 0 is reserved */
414#define NS_KEY_PROT_TLS         1
415#define NS_KEY_PROT_EMAIL       2
416#define NS_KEY_PROT_DNSSEC      3
417#define NS_KEY_PROT_IPSEC       4
418#define NS_KEY_PROT_ANY		255
419
420/* Signatures */
421#define	NS_MD5RSA_MIN_BITS	 512	/* Size of a mod or exp in bits */
422#define	NS_MD5RSA_MAX_BITS	4096
423	/* Total of binary mod and exp */
424#define	NS_MD5RSA_MAX_BYTES	((NS_MD5RSA_MAX_BITS+7/8)*2+3)
425	/* Max length of text sig block */
426#define	NS_MD5RSA_MAX_BASE64	(((NS_MD5RSA_MAX_BYTES+2)/3)*4)
427#define NS_MD5RSA_MIN_SIZE	((NS_MD5RSA_MIN_BITS+7)/8)
428#define NS_MD5RSA_MAX_SIZE	((NS_MD5RSA_MAX_BITS+7)/8)
429
430#define NS_DSA_SIG_SIZE         41
431#define NS_DSA_MIN_SIZE         213
432#define NS_DSA_MAX_BYTES        405
433
434/* Offsets into SIG record rdata to find various values */
435#define	NS_SIG_TYPE	0	/* Type flags */
436#define	NS_SIG_ALG	2	/* Algorithm */
437#define	NS_SIG_LABELS	3	/* How many labels in name */
438#define	NS_SIG_OTTL	4	/* Original TTL */
439#define	NS_SIG_EXPIR	8	/* Expiration time */
440#define	NS_SIG_SIGNED	12	/* Signature time */
441#define	NS_SIG_FOOT	16	/* Key footprint */
442#define	NS_SIG_SIGNER	18	/* Domain name of who signed it */
443
444/* How RR types are represented as bit-flags in NXT records */
445#define	NS_NXT_BITS 8
446#define	NS_NXT_BIT_SET(  n,p) (p[(n)/NS_NXT_BITS] |=  (0x80>>((n)%NS_NXT_BITS)))
447#define	NS_NXT_BIT_CLEAR(n,p) (p[(n)/NS_NXT_BITS] &= ~(0x80>>((n)%NS_NXT_BITS)))
448#define	NS_NXT_BIT_ISSET(n,p) (p[(n)/NS_NXT_BITS] &   (0x80>>((n)%NS_NXT_BITS)))
449#define NS_NXT_MAX 127
450
451/* EDNS0 extended flags, host order. */
452#define NS_OPT_DNSSEC_OK	0x8000U
453#define NS_OPT_NSID			3
454
455/* Inline versions of get/put short/long. Pointer is advanced. */
456#define NS_GET16(s, cp) do { \
457	register const unsigned char *t_cp = (const unsigned char *)(cp); \
458	(s) = ((uint16_t)t_cp[0] << 8) \
459	    | ((uint16_t)t_cp[1]) \
460	    ; \
461	(cp) += NS_INT16SZ; \
462} while (0)
463
464#define NS_GET32(l, cp) do { \
465	register const unsigned char *t_cp = (const unsigned char *)(cp); \
466	(l) = ((uint32_t)t_cp[0] << 24) \
467	    | ((uint32_t)t_cp[1] << 16) \
468	    | ((uint32_t)t_cp[2] << 8) \
469	    | ((uint32_t)t_cp[3]) \
470	    ; \
471	(cp) += NS_INT32SZ; \
472} while (0)
473
474#define NS_PUT16(s, cp) do { \
475	register uint16_t t_s = (uint16_t)(s); \
476	register unsigned char *t_cp = (unsigned char *)(cp); \
477	*t_cp++ = t_s >> 8; \
478	*t_cp   = t_s; \
479	(cp) += NS_INT16SZ; \
480} while (0)
481
482#define NS_PUT32(l, cp) do { \
483	register uint32_t t_l = (uint32_t)(l); \
484	register unsigned char *t_cp = (unsigned char *)(cp); \
485	*t_cp++ = t_l >> 24; \
486	*t_cp++ = t_l >> 16; \
487	*t_cp++ = t_l >> 8; \
488	*t_cp   = t_l; \
489	(cp) += NS_INT32SZ; \
490} while (0)
491
492/*
493 * ANSI C identifier hiding for bind's lib/nameser.
494 */
495#define	ns_msg_getflag		__ns_msg_getflag
496#define ns_get16			__ns_get16
497#define ns_get32			__ns_get32
498#define ns_put16			__ns_put16
499#define ns_put32			__ns_put32
500#define ns_initparse		__ns_initparse
501#define ns_skiprr			__ns_skiprr
502#define ns_parserr			__ns_parserr
503#define	ns_sprintrr			__ns_sprintrr
504#define	ns_sprintrrf		__ns_sprintrrf
505#define	ns_format_ttl		__ns_format_ttl
506#define	ns_parse_ttl		__ns_parse_ttl
507#define ns_datetosecs		__ns_datetosecs
508#define	ns_name_ntol		__ns_name_ntol
509#define	ns_name_ntop		__ns_name_ntop
510#define	ns_name_pton		__ns_name_pton
511#define	ns_name_unpack		__ns_name_unpack
512#define	ns_name_pack		__ns_name_pack
513#define	ns_name_compress	__ns_name_compress
514#define	ns_name_uncompress	__ns_name_uncompress
515#define	ns_name_skip		__ns_name_skip
516#define	ns_name_rollback	__ns_name_rollback
517#define	ns_samedomain		__ns_samedomain
518#define	ns_subdomain		__ns_subdomain
519#define	ns_makecanon		__ns_makecanon
520#define	ns_samename			__ns_samename
521
522#define ns_name_length		__ns_name_length
523#define ns_name_eq			__ns_name_eq
524#define ns_name_owned		__ns_name_owned
525#define ns_name_map			__ns_name_map
526#define ns_name_labels		__ns_name_labels
527#define ns_newmsg_init		__ns_newmsg_init
528#define ns_newmsg_copy		__ns_newmsg_copy
529#define ns_newmsg_id		__ns_newmsg_id
530#define ns_newmsg_flag		__ns_newmsg_flag
531#define ns_newmsg_q			__ns_newmsg_q
532#define ns_newmsg_rr		__ns_newmsg_rr
533#define ns_newmsg_done		__ns_newmsg_done
534#define ns_rdata_unpack		__ns_rdata_unpack
535#define ns_rdata_equal		__ns_rdata_equal
536#define ns_rdata_refers		__ns_rdata_refers
537
538int		ns_msg_getflag(ns_msg, int);
539uint16_t ns_get16(const unsigned char *);
540uint32_t ns_get32(const unsigned char *);
541void	ns_put16(uint16_t, unsigned char *);
542void	ns_put32(uint32_t, unsigned char *);
543int		ns_initparse(const unsigned char *, int, ns_msg *);
544int		ns_skiprr(const unsigned char *, const unsigned char *, ns_sect, int);
545int		ns_parserr(ns_msg *, ns_sect, int, ns_rr *);
546int		ns_sprintrr(const ns_msg *, const ns_rr *,
547			const char *, const char *, char *, size_t);
548int		ns_sprintrrf(const unsigned char *, size_t, const char *,
549			ns_class, ns_type, unsigned long, const unsigned char *,
550			size_t, const char *, const char *,
551			char *, size_t);
552int		ns_format_ttl(unsigned long, char *, size_t);
553int		ns_parse_ttl(const char *, unsigned long *);
554uint32_t ns_datetosecs(const char *cp, int *errp);
555int		ns_name_ntol(const unsigned char *, unsigned char *, size_t);
556int		ns_name_ntop(const unsigned char *, char *, size_t);
557int		ns_name_pton(const char *, unsigned char *, size_t);
558int		ns_name_unpack(const unsigned char *, const unsigned char *,
559			const unsigned char *, unsigned char *, size_t);
560int		ns_name_pack(const unsigned char *, unsigned char *, int,
561			const unsigned char **, const unsigned char **);
562int		ns_name_uncompress(const unsigned char *, const unsigned char *,
563			const unsigned char *, char *, size_t);
564int		ns_name_compress(const char *, unsigned char *, size_t,
565			const unsigned char **, const unsigned char **);
566int		ns_name_skip(const unsigned char **, const unsigned char *);
567void	ns_name_rollback(const unsigned char *, const unsigned char **,
568			const unsigned char **);
569int		ns_samedomain(const char *, const char *);
570int		ns_subdomain(const char *, const char *);
571int		ns_makecanon(const char *, char *, size_t);
572int		ns_samename(const char *, const char *);
573
574ssize_t	ns_name_length(ns_nname_ct nname, size_t nameSize);
575int		ns_name_eq(ns_nname_ct a, size_t aLength, ns_nname_ct b,
576			size_t bLength);
577int		ns_name_owned(ns_namemap_ct a, int aLength, ns_namemap_ct b,
578			int bLength);
579int		ns_name_map(ns_nname_ct nname, size_t nameLength, ns_namemap_t map,
580			int mapSize);
581int		ns_name_labels(ns_nname_ct, size_t);
582int		ns_newmsg_init(unsigned char *buffer, size_t bufsiz, ns_newmsg *);
583int		ns_newmsg_copy(ns_newmsg *, ns_msg *);
584void	ns_newmsg_id(ns_newmsg *handle, uint16_t id);
585void	ns_newmsg_flag(ns_newmsg *handle, ns_flag flag, unsigned int value);
586int		ns_newmsg_q(ns_newmsg *handle, ns_nname_ct qname, ns_type qtype,
587			ns_class qclass);
588int		ns_newmsg_rr(ns_newmsg *handle, ns_sect sect, ns_nname_ct name,
589			ns_type type, ns_class rr_class, uint32_t ttl, uint16_t rdlen,
590			const unsigned char *rdata);
591size_t	ns_newmsg_done(ns_newmsg *handle);
592ssize_t	ns_rdata_unpack(const unsigned char *, const unsigned char *, ns_type,
593			const unsigned char *, size_t, unsigned char *, size_t);
594int		ns_rdata_equal(ns_type, const unsigned char *, size_t,
595			const unsigned char *, size_t);
596int		ns_rdata_refers(ns_type, const unsigned char *, size_t,
597			const unsigned char *);
598
599#ifdef __cplusplus
600}
601#endif
602
603#include <arpa/nameser_compat.h>
604
605#endif	/* _ARPA_NAMESER_H_ */
606