xref: /openbsd-src/regress/lib/libc/asr/bin/common.h (revision a9e850503858625db8c9b559960909d88f57d4ab)
1*a9e85050Seric /*	$OpenBSD: common.h,v 1.2 2018/12/15 15:16:12 eric Exp $	*/
24c1e55dcSeric /*
34c1e55dcSeric  * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
44c1e55dcSeric  *
54c1e55dcSeric  * Permission to use, copy, modify, and distribute this software for any
64c1e55dcSeric  * purpose with or without fee is hereby granted, provided that the above
74c1e55dcSeric  * copyright notice and this permission notice appear in all copies.
84c1e55dcSeric  *
94c1e55dcSeric  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
104c1e55dcSeric  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
114c1e55dcSeric  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
124c1e55dcSeric  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
134c1e55dcSeric  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
144c1e55dcSeric  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
154c1e55dcSeric  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
164c1e55dcSeric  */
174c1e55dcSeric #include <sys/types.h>
184c1e55dcSeric #include <sys/socket.h>
194c1e55dcSeric 
204c1e55dcSeric #include <arpa/nameser.h>
214c1e55dcSeric 
224c1e55dcSeric #include <netinet/in.h>
234c1e55dcSeric 
244c1e55dcSeric #include <netdb.h>
254c1e55dcSeric 
264c1e55dcSeric 
274c1e55dcSeric #define unpack_rr		__unpack_rr
284c1e55dcSeric #define unpack_header		__unpack_header
294c1e55dcSeric #define packed_init		__packed_init
304c1e55dcSeric #define unpack_query		__unpack_query
314c1e55dcSeric #define sockaddr_from_str	__sockaddr_from_str
324c1e55dcSeric #define print_addr		__print_addr
334c1e55dcSeric 
344c1e55dcSeric extern int long_err;
354c1e55dcSeric extern int gai_errno;
364c1e55dcSeric extern int rrset_errno;
374c1e55dcSeric 
384c1e55dcSeric const char *classtostr(uint16_t);
394c1e55dcSeric const char *typetostr(uint16_t);
404c1e55dcSeric const char *rcodetostr(uint16_t);
414c1e55dcSeric 
424c1e55dcSeric uint16_t strtotype(const char*);
434c1e55dcSeric uint16_t strtoclass(const char*);
44*a9e85050Seric int strtoresopt(const char*);
45*a9e85050Seric void parseresopt(const char*);
464c1e55dcSeric 
474c1e55dcSeric void	print_rrsetinfo(struct rrsetinfo *);
484c1e55dcSeric void	print_addrinfo(struct addrinfo *);
494c1e55dcSeric void	print_errors(void);
504c1e55dcSeric void	print_hostent(struct hostent *);
514c1e55dcSeric void	print_netent(struct netent *);
524c1e55dcSeric 
534c1e55dcSeric int	sockaddr_from_str(struct sockaddr *, int, const char *);
544c1e55dcSeric int	addr_from_str(char *, int *, int *, const char *);
554c1e55dcSeric char*	gethostarg(char *);
564c1e55dcSeric 
574c1e55dcSeric #define QR_MASK		(0x1 << 15)
584c1e55dcSeric #define OPCODE_MASK	(0xf << 11)
594c1e55dcSeric #define AA_MASK		(0x1 << 10)
604c1e55dcSeric #define TC_MASK		(0x1 <<  9)
614c1e55dcSeric #define RD_MASK		(0x1 <<  8)
624c1e55dcSeric #define RA_MASK		(0x1 <<  7)
634c1e55dcSeric #define Z_MASK		(0x7 <<  4)
644c1e55dcSeric #define RCODE_MASK	(0xf)
654c1e55dcSeric 
664c1e55dcSeric #define OPCODE(v)	((v) & OPCODE_MASK)
674c1e55dcSeric #define RCODE(v)	((v) & RCODE_MASK)
684c1e55dcSeric 
694c1e55dcSeric 
704c1e55dcSeric struct packed {
714c1e55dcSeric 	char		*data;
724c1e55dcSeric 	size_t		 len;
734c1e55dcSeric 	size_t		 offset;
744c1e55dcSeric 	const char	*err;
754c1e55dcSeric };
764c1e55dcSeric 
774c1e55dcSeric struct header {
784c1e55dcSeric 	uint16_t	id;
794c1e55dcSeric 	uint16_t	flags;
804c1e55dcSeric 	uint16_t	qdcount;
814c1e55dcSeric 	uint16_t	ancount;
824c1e55dcSeric 	uint16_t	nscount;
834c1e55dcSeric 	uint16_t	arcount;
844c1e55dcSeric };
854c1e55dcSeric 
864c1e55dcSeric struct query {
874c1e55dcSeric 	char		q_dname[MAXDNAME];
884c1e55dcSeric 	uint16_t	q_type;
894c1e55dcSeric 	uint16_t	q_class;
904c1e55dcSeric };
914c1e55dcSeric 
924c1e55dcSeric struct rr {
934c1e55dcSeric 	char		rr_dname[MAXDNAME];
944c1e55dcSeric 	uint16_t	rr_type;
954c1e55dcSeric 	uint16_t	rr_class;
964c1e55dcSeric 	uint32_t	rr_ttl;
974c1e55dcSeric 	union {
984c1e55dcSeric 		struct {
994c1e55dcSeric 			char	cname[MAXDNAME];
1004c1e55dcSeric 		} cname;
1014c1e55dcSeric 		struct {
1024c1e55dcSeric 			uint16_t	preference;
1034c1e55dcSeric 			char		exchange[MAXDNAME];
1044c1e55dcSeric 		} mx;
1054c1e55dcSeric 		struct {
1064c1e55dcSeric 			char	nsname[MAXDNAME];
1074c1e55dcSeric 		} ns;
1084c1e55dcSeric 		struct {
1094c1e55dcSeric 			char	ptrname[MAXDNAME];
1104c1e55dcSeric 		} ptr;
1114c1e55dcSeric 		struct {
1124c1e55dcSeric 			char		mname[MAXDNAME];
1134c1e55dcSeric 			char		rname[MAXDNAME];
1144c1e55dcSeric 			uint32_t	serial;
1154c1e55dcSeric 			uint32_t	refresh;
1164c1e55dcSeric 			uint32_t	retry;
1174c1e55dcSeric 			uint32_t	expire;
1184c1e55dcSeric 			uint32_t	minimum;
1194c1e55dcSeric 		} soa;
1204c1e55dcSeric 		struct {
1214c1e55dcSeric 			struct in_addr	addr;
1224c1e55dcSeric 		} in_a;
1234c1e55dcSeric 		struct {
1244c1e55dcSeric 			struct in6_addr	addr6;
1254c1e55dcSeric 		} in_aaaa;
1264c1e55dcSeric 		struct {
1274c1e55dcSeric 			uint16_t	 rdlen;
1284c1e55dcSeric 			const void	*rdata;
1294c1e55dcSeric 		} other;
1304c1e55dcSeric 	} rr;
1314c1e55dcSeric };
1324c1e55dcSeric 
1334c1e55dcSeric void	packed_init(struct packed*, char*, size_t);
1344c1e55dcSeric int	pack_header(struct packed*, const struct header*);
1354c1e55dcSeric int	pack_query(struct packed*, uint16_t, uint16_t, const char*);
1364c1e55dcSeric int	unpack_header(struct packed*, struct header*);
1374c1e55dcSeric int	unpack_query(struct packed*, struct query*);
1384c1e55dcSeric int	unpack_rr(struct packed*, struct rr*);
139