xref: /freebsd/contrib/unbound/dnstap/dnstap.h (revision 95eb4b873b6a8b527c5bd78d7191975dfca38998)
1 /* dnstap support for Unbound */
2 
3 /*
4  * Copyright (c) 2013-2014, Farsight Security, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef UNBOUND_DNSTAP_H
36 #define UNBOUND_DNSTAP_H
37 
38 #include "dnstap/dnstap_config.h"
39 
40 #ifdef USE_DNSTAP
41 
42 struct config_file;
43 struct sldns_buffer;
44 struct dt_msg_queue;
45 
46 struct dt_env {
47 	/** the io thread (made by the struct daemon) */
48 	struct dt_io_thread* dtio;
49 
50 	/** valid in worker struct, not in daemon struct, the per-worker
51 	 * message list */
52 	struct dt_msg_queue* msgqueue;
53 
54 	/** dnstap "identity" field, NULL if disabled */
55 	char *identity;
56 
57 	/** dnstap "version" field, NULL if disabled */
58 	char *version;
59 
60 	/** length of "identity" field */
61 	unsigned len_identity;
62 
63 	/** length of "version" field */
64 	unsigned len_version;
65 
66 	/** whether to log Message/RESOLVER_QUERY */
67 	unsigned log_resolver_query_messages : 1;
68 	/** whether to log Message/RESOLVER_RESPONSE */
69 	unsigned log_resolver_response_messages : 1;
70 	/** whether to log Message/CLIENT_QUERY */
71 	unsigned log_client_query_messages : 1;
72 	/** whether to log Message/CLIENT_RESPONSE */
73 	unsigned log_client_response_messages : 1;
74 	/** whether to log Message/FORWARDER_QUERY */
75 	unsigned log_forwarder_query_messages : 1;
76 	/** whether to log Message/FORWARDER_RESPONSE */
77 	unsigned log_forwarder_response_messages : 1;
78 };
79 
80 /**
81  * Create dnstap environment object. Afterwards, call dt_apply_cfg() to fill in
82  * the config variables and dt_init() to fill in the per-worker state. Each
83  * worker needs a copy of this object but with its own I/O queue (the fq field
84  * of the structure) to ensure lock-free access to its own per-worker circular
85  * queue.  Duplicate the environment object if more than one worker needs to
86  * share access to the dnstap I/O socket.
87  * @param cfg: with config settings.
88  * @return dt_env object, NULL on failure.
89  */
90 struct dt_env *
91 dt_create(struct config_file* cfg);
92 
93 /**
94  * Apply config settings.
95  * @param env: dnstap environment object.
96  * @param cfg: new config settings.
97  */
98 void
99 dt_apply_cfg(struct dt_env *env, struct config_file *cfg);
100 
101 /**
102  * Initialize per-worker state in dnstap environment object.
103  * @param env: dnstap environment object to initialize, created with dt_create().
104  * @param base: event base for wakeup timer.
105  * @return: true on success, false on failure.
106  */
107 int
108 dt_init(struct dt_env *env, struct comm_base* base);
109 
110 /**
111  * Deletes the per-worker state created by dt_init
112  */
113 void dt_deinit(struct dt_env *env);
114 
115 /**
116  * Delete dnstap environment object. Closes dnstap I/O socket and deletes all
117  * per-worker I/O queues.
118  */
119 void
120 dt_delete(struct dt_env *env);
121 
122 /**
123  * Create and send a new dnstap "Message" event of type CLIENT_QUERY.
124  * @param env: dnstap environment object.
125  * @param qsock: address/port of client.
126  * @param rsock: local (service) address/port.
127  * @param cptype: comm_udp or comm_tcp.
128  * @param qmsg: query message.
129  * @param tstamp: timestamp or NULL if none provided.
130  */
131 void
132 dt_msg_send_client_query(struct dt_env *env,
133 			 struct sockaddr_storage *qsock,
134 			 struct sockaddr_storage *rsock,
135 			 enum comm_point_type cptype,
136 			 void *cpssl,
137 			 struct sldns_buffer *qmsg,
138 			 struct timeval* tstamp);
139 
140 /**
141  * Create and send a new dnstap "Message" event of type CLIENT_RESPONSE.
142  * @param env: dnstap environment object.
143  * @param qsock: address/port of client.
144  * @param rsock: local (service) address/port.
145  * @param cptype: comm_udp or comm_tcp.
146  * @param rmsg: response message.
147  */
148 void
149 dt_msg_send_client_response(struct dt_env *env,
150 			    struct sockaddr_storage *qsock,
151 			    struct sockaddr_storage *rsock,
152 			    enum comm_point_type cptype,
153 			    void *cpssl,
154 			    struct sldns_buffer *rmsg);
155 
156 /**
157  * Create and send a new dnstap "Message" event of type RESOLVER_QUERY or
158  * FORWARDER_QUERY. The type used is dependent on the value of the RD bit
159  * in the query header.
160  * @param env: dnstap environment object.
161  * @param rsock: address/port of server (upstream) the query is being sent to.
162  * @param qsock: address/port of server (local) the query is being sent from.
163  * @param cptype: comm_udp or comm_tcp.
164  * @param zone: query zone.
165  * @param zone_len: length of zone.
166  * @param qmsg: query message.
167  */
168 void
169 dt_msg_send_outside_query(struct dt_env *env,
170 			  struct sockaddr_storage *rsock,
171 			  struct sockaddr_storage *qsock,
172 			  enum comm_point_type cptype,
173 			  void *cpssl,
174 			  uint8_t *zone, size_t zone_len,
175 			  struct sldns_buffer *qmsg);
176 
177 /**
178  * Create and send a new dnstap "Message" event of type RESOLVER_RESPONSE or
179  * FORWARDER_RESPONSE. The type used is dependent on the value of the RD bit
180  * in the query header.
181  * @param env: dnstap environment object.
182  * @param rsock: address/port of server (upstream) the response was received from.
183  * @param qsock: address/port of server (local) the response was received to.
184  * @param cptype: comm_udp or comm_tcp.
185  * @param zone: query zone.
186  * @param zone_len: length of zone.
187  * @param qbuf: outside_network's qbuf key.
188  * @param qbuf_len: length of outside_network's qbuf key.
189  * @param qtime: time query message was sent.
190  * @param rtime: time response message was sent.
191  * @param rmsg: response message.
192  */
193 void
194 dt_msg_send_outside_response(struct dt_env *env,
195 			     struct sockaddr_storage *rsock,
196 			     struct sockaddr_storage *qsock,
197 			     enum comm_point_type cptype,
198 			     void *cpssl,
199 			     uint8_t *zone, size_t zone_len,
200 			     uint8_t *qbuf, size_t qbuf_len,
201 			     const struct timeval *qtime,
202 			     const struct timeval *rtime,
203 			     struct sldns_buffer *rmsg);
204 
205 #endif /* USE_DNSTAP */
206 
207 #endif /* UNBOUND_DNSTAP_H */
208