xref: /openbsd/usr.sbin/smtpd/unpack_dns.h (revision d3140113)
1*d3140113Seric /*	$OpenBSD: unpack_dns.h,v 1.2 2021/06/14 17:58:16 eric Exp $	*/
2ed4a73a0Ssunil 
3ed4a73a0Ssunil /*
4ed4a73a0Ssunil  * Copyright (c) 2011-2014 Eric Faurot <eric@faurot.net>
5ed4a73a0Ssunil  *
6ed4a73a0Ssunil  * Permission to use, copy, modify, and distribute this software for any
7ed4a73a0Ssunil  * purpose with or without fee is hereby granted, provided that the above
8ed4a73a0Ssunil  * copyright notice and this permission notice appear in all copies.
9ed4a73a0Ssunil  *
10ed4a73a0Ssunil  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11ed4a73a0Ssunil  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12ed4a73a0Ssunil  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13ed4a73a0Ssunil  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14ed4a73a0Ssunil  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15ed4a73a0Ssunil  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16ed4a73a0Ssunil  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17ed4a73a0Ssunil  */
18ed4a73a0Ssunil 
19ed4a73a0Ssunil #include <sys/types.h>
20ed4a73a0Ssunil 
21ed4a73a0Ssunil #include <netinet/in.h>
22ed4a73a0Ssunil 
23ed4a73a0Ssunil #include <arpa/nameser.h>
24ed4a73a0Ssunil 
25ed4a73a0Ssunil struct unpack {
26ed4a73a0Ssunil 	const char	*buf;
27ed4a73a0Ssunil 	size_t		 len;
28ed4a73a0Ssunil 	size_t		 offset;
29ed4a73a0Ssunil 	const char	*err;
30ed4a73a0Ssunil };
31ed4a73a0Ssunil 
32ed4a73a0Ssunil struct dns_header {
33ed4a73a0Ssunil 	uint16_t	id;
34ed4a73a0Ssunil 	uint16_t	flags;
35ed4a73a0Ssunil 	uint16_t	qdcount;
36ed4a73a0Ssunil 	uint16_t	ancount;
37ed4a73a0Ssunil 	uint16_t	nscount;
38ed4a73a0Ssunil 	uint16_t	arcount;
39ed4a73a0Ssunil };
40ed4a73a0Ssunil 
41ed4a73a0Ssunil struct dns_query {
42ed4a73a0Ssunil 	char		q_dname[MAXDNAME];
43ed4a73a0Ssunil 	uint16_t	q_type;
44ed4a73a0Ssunil 	uint16_t	q_class;
45ed4a73a0Ssunil };
46ed4a73a0Ssunil 
47ed4a73a0Ssunil struct dns_rr {
48ed4a73a0Ssunil 	char		rr_dname[MAXDNAME];
49ed4a73a0Ssunil 	uint16_t	rr_type;
50ed4a73a0Ssunil 	uint16_t	rr_class;
51ed4a73a0Ssunil 	uint32_t	rr_ttl;
52ed4a73a0Ssunil 	union {
53ed4a73a0Ssunil 		struct {
54ed4a73a0Ssunil 			char	cname[MAXDNAME];
55ed4a73a0Ssunil 		} cname;
56ed4a73a0Ssunil 		struct {
57ed4a73a0Ssunil 			uint16_t	preference;
58ed4a73a0Ssunil 			char		exchange[MAXDNAME];
59ed4a73a0Ssunil 		} mx;
60ed4a73a0Ssunil 		struct {
61ed4a73a0Ssunil 			char	nsname[MAXDNAME];
62ed4a73a0Ssunil 		} ns;
63ed4a73a0Ssunil 		struct {
64ed4a73a0Ssunil 			char	ptrname[MAXDNAME];
65ed4a73a0Ssunil 		} ptr;
66ed4a73a0Ssunil 		struct {
67ed4a73a0Ssunil 			char		mname[MAXDNAME];
68ed4a73a0Ssunil 			char		rname[MAXDNAME];
69ed4a73a0Ssunil 			uint32_t	serial;
70ed4a73a0Ssunil 			uint32_t	refresh;
71ed4a73a0Ssunil 			uint32_t	retry;
72ed4a73a0Ssunil 			uint32_t	expire;
73ed4a73a0Ssunil 			uint32_t	minimum;
74ed4a73a0Ssunil 		} soa;
75ed4a73a0Ssunil 		struct {
76ed4a73a0Ssunil 			struct in_addr	addr;
77ed4a73a0Ssunil 		} in_a;
78ed4a73a0Ssunil 		struct {
79ed4a73a0Ssunil 			struct in6_addr	addr6;
80ed4a73a0Ssunil 		} in_aaaa;
81ed4a73a0Ssunil 		struct {
82ed4a73a0Ssunil 			uint16_t	 rdlen;
83ed4a73a0Ssunil 			const void	*rdata;
84ed4a73a0Ssunil 		} other;
85ed4a73a0Ssunil 	} rr;
86ed4a73a0Ssunil };
87ed4a73a0Ssunil 
88ed4a73a0Ssunil void	 unpack_init(struct unpack *, const char *, size_t);
89ed4a73a0Ssunil int	 unpack_header(struct unpack *, struct dns_header *);
90ed4a73a0Ssunil int	 unpack_rr(struct unpack *, struct dns_rr *);
91ed4a73a0Ssunil int	 unpack_query(struct unpack *, struct dns_query *);
92ed4a73a0Ssunil char    *print_dname(const char *, char *, size_t);
93ed4a73a0Ssunil ssize_t	 dname_expand(const unsigned char *, size_t, size_t, size_t *,
94ed4a73a0Ssunil 	    char *, size_t);
95ed4a73a0Ssunil 
96