summaryrefslogtreecommitdiff
path: root/usr.sbin/smtpd/dnsutil.h
blob: b2bce7afc55876ac69a8e797a288c83799f518c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Copyright (c) 2009,2010	Eric Faurot	<eric@faurot.net>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>

#include "dnsdefs.h"

struct packed {
	char		*data;
	size_t		 len;
	size_t		 offset;
	const char	*err;
};

struct header {
	uint16_t	id;
	uint16_t	flags;
	uint16_t	qdcount;
	uint16_t	ancount;
	uint16_t	nscount;
	uint16_t	arcount;
};

struct query {
	char			 q_dname[DOMAIN_MAXLEN];
	uint16_t		 q_type;
	uint16_t		 q_class;
};

struct rr {
	char		 rr_dname[DOMAIN_MAXLEN];
	uint16_t	 rr_type;
	uint16_t	 rr_class;
	uint32_t	 rr_ttl;
	union {
		struct {
			char	cname[DOMAIN_MAXLEN];
		} cname;
		struct {
			uint16_t	preference;
			char		exchange[DOMAIN_MAXLEN];
		} mx;
		struct {
			char	nsname[DOMAIN_MAXLEN];
		} ns;
		struct {
			char	ptrname[DOMAIN_MAXLEN];
		} ptr;
		struct {
			char		mname[DOMAIN_MAXLEN];
			char		rname[DOMAIN_MAXLEN];
			uint32_t	serial;
			uint32_t	refresh;
			uint32_t	retry;
			uint32_t	expire;
			uint32_t	minimum;
		} soa;
		struct {
			struct in_addr	addr;
		} in_a;
		struct {
			struct in6_addr	addr6;
		} in_aaaa;
		struct {
			uint16_t	 rdlen;
			const void	*rdata;
		} other;
	} rr;
};

struct rr_dynamic {
	const char		*rd_dname;
	uint16_t		 rd_type;
	uint16_t		 rd_class;
	uint32_t		 rd_ttl;
	union rr_subtype {
		struct rr_cname {
			char		*cname;
		} cname;
		struct rr_mx {
			uint16_t	 preference;
			char		*exchange;
		} mx;
		struct rr_ns {
			char		*nsname;
		} ns;
		struct rr_ptr {
			char		*ptrname;
		} ptr;
		struct rr_soa {
			char		*mname;
			char		*rname;
			uint32_t	 serial;
			uint32_t	 refresh;
			uint32_t	 retry;
			uint32_t	 expire;
			uint32_t	 minimum;
		} soa;
		struct rr_in_a {
			struct in_addr	 addr;
		} in_a;
		struct rr_in_aaaa {
			struct in6_addr	 addr6;
		} in_aaaa;
		struct rr_other {
			uint16_t	 rdlen;
			void		*rdata;
		} other;
	} rd;
};



/* pack.c */
void	packed_init(struct packed*, char*, size_t);

int	unpack_data(struct packed*, void*, size_t);
int	unpack_u16(struct packed*, uint16_t*);
int	unpack_u32(struct packed*, uint32_t*);
int	unpack_inaddr(struct packed*, struct in_addr*);
int	unpack_in6addr(struct packed*, struct in6_addr*);
int	unpack_dname(struct packed*, char*, size_t);
int	unpack_header(struct packed*, struct header*);
int	unpack_query(struct packed*, struct query*);
int	unpack_rr(struct packed*, struct rr*);

int	pack_data(struct packed*, const void*, size_t);
int	pack_u16(struct packed*, uint16_t);
int	pack_u32(struct packed*, uint32_t);
int	pack_inaddr(struct packed*, struct in_addr);
int	pack_in6addr(struct packed*, struct in6_addr);
int	pack_header(struct packed*, const struct header*);
int	pack_dname(struct packed*, const char*);
int	pack_query(struct packed*, uint16_t, uint16_t, const char*);
int	pack_rrdynamic(struct packed*, const struct rr_dynamic *rr);

/* sockaddr.c */
int	sockaddr_from_rr(struct sockaddr *, struct rr *);
int	sockaddr_from_str(struct sockaddr *, int, const char *);
ssize_t	sockaddr_as_fqdn(const struct sockaddr *, char *, size_t);
void	sockaddr_set_port(struct sockaddr *, int);
int	sockaddr_connect(const struct sockaddr *, int);
int	sockaddr_listen(const struct sockaddr *, int);

/* print.c */
const char *print_host(struct sockaddr*, char*, size_t);
const char *print_addr(struct sockaddr*, char*, size_t);
const char *print_dname(const char*, char*, size_t);
const char *print_header(struct header*, char*, size_t);
const char *print_query(struct query*, char*, size_t);
const char *print_rr(struct rr*, char*, size_t);
const char *print_rrdynamic(struct rr_dynamic*, char*, size_t);

const char *typetostr(uint16_t);
const char *classtostr(uint16_t);
const char *rcodetostr(uint16_t);

uint16_t    strtotype(const char*);
uint16_t    strtoclass(const char*);
const char *inet6_ntoa(struct in6_addr);

/* dname.c */
size_t	    dname_len(const char *);
size_t	    dname_depth(const char *);
ssize_t	    dname_from_fqdn(const char*, char*, size_t);
ssize_t	    dname_from_sockaddr(const struct sockaddr *, char*, size_t);
int	    dname_is_in(const char*, const char*);
int	    dname_is_wildcard(const char *);
int	    dname_is_reverse(const char *);
int	    dname_check_label(const char*, size_t);
const char* dname_up(const char*, unsigned int);

/* res_random.c */
unsigned int res_randomid(void);