xref: /openbsd/usr.sbin/nsd/dnstap/dnstap.h (revision 73471bf0)
1 /* dnstap support for NSD */
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 NSD_DNSTAP_H
36 #define NSD_DNSTAP_H
37 
38 #include "dnstap/dnstap_config.h"
39 
40 #ifdef USE_DNSTAP
41 
42 struct nsd_options;
43 struct fstrm_io;
44 struct fstrm_queue;
45 
46 struct dt_env {
47 	/** dnstap I/O thread */
48 	struct fstrm_iothr *iothr;
49 
50 	/** dnstap I/O thread input queue */
51 	struct fstrm_iothr_queue *ioq;
52 
53 	/** dnstap "identity" field, NULL if disabled */
54 	char *identity;
55 
56 	/** dnstap "version" field, NULL if disabled */
57 	char *version;
58 
59 	/** length of "identity" field */
60 	unsigned len_identity;
61 
62 	/** length of "version" field */
63 	unsigned len_version;
64 
65 	/** whether to log Message/AUTH_QUERY */
66 	unsigned log_auth_query_messages : 1;
67 	/** whether to log Message/AUTH_RESPONSE */
68 	unsigned log_auth_response_messages : 1;
69 };
70 
71 /**
72  * Create dnstap environment object. Afterwards, call dt_apply_cfg() to fill in
73  * the config variables and dt_init() to fill in the per-worker state. Each
74  * worker needs a copy of this object but with its own I/O queue (the fq field
75  * of the structure) to ensure lock-free access to its own per-worker circular
76  * queue.  Duplicate the environment object if more than one worker needs to
77  * share access to the dnstap I/O socket.
78  * @param socket_path: path to dnstap logging socket, must be non-NULL.
79  * @param num_workers: number of worker threads, must be > 0.
80  * @return dt_env object, NULL on failure.
81  */
82 struct dt_env *
83 dt_create(const char *socket_path, unsigned num_workers);
84 
85 /**
86  * Apply config settings.
87  * @param env: dnstap environment object.
88  * @param cfg: new config settings.
89  */
90 void
91 dt_apply_cfg(struct dt_env *env, struct nsd_options *cfg);
92 
93 /**
94  * Initialize per-worker state in dnstap environment object.
95  * @param env: dnstap environment object to initialize, created with dt_create().
96  * @return: true on success, false on failure.
97  */
98 int
99 dt_init(struct dt_env *env);
100 
101 /**
102  * Delete dnstap environment object. Closes dnstap I/O socket and deletes all
103  * per-worker I/O queues.
104  */
105 void
106 dt_delete(struct dt_env *env);
107 
108 /**
109  * Create and send a new dnstap "Message" event of type AUTH_QUERY.
110  * @param env: dnstap environment object.
111  * @param local_addr: address/port of server (local address).
112  * @param addr: address/port of client.
113  * @param is_tcp: true for tcp, false for udp.
114  * @param zone: zone name, or NULL. in wireformat.
115  * @param zonelen: length of zone in bytes.
116  * @param pkt: query message.
117  * @param pktlen: length of pkt.
118  */
119 void
120 dt_msg_send_auth_query(struct dt_env *env,
121 #ifdef INET6
122 	struct sockaddr_storage* local_addr,
123 	struct sockaddr_storage* addr,
124 #else
125 	struct sockaddr_in* local_addr,
126 	struct sockaddr_in* addr,
127 #endif
128 	int is_tcp, uint8_t* zone, size_t zonelen, uint8_t* pkt, size_t pktlen);
129 
130 /**
131  * Create and send a new dnstap "Message" event of type AUTH_RESPONSE.
132  * @param env: dnstap environment object.
133  * @param local_addr: address/port of server (local address).
134  * @param addr: address/port of client.
135  * @param is_tcp: true for tcp, false for udp.
136  * @param zone: zone name, or NULL. in wireformat.
137  * @param zonelen: length of zone in bytes.
138  * @param pkt: response message.
139  * @param pktlen: length of pkt.
140  */
141 void
142 dt_msg_send_auth_response(struct dt_env *env,
143 #ifdef INET6
144 	struct sockaddr_storage* local_addr,
145 	struct sockaddr_storage* addr,
146 #else
147 	struct sockaddr_in* local_addr,
148 	struct sockaddr_in* addr,
149 #endif
150 	int is_tcp, uint8_t* zone, size_t zonelen, uint8_t* pkt, size_t pktlen);
151 
152 #endif /* USE_DNSTAP */
153 
154 #endif /* NSD_DNSTAP_H */
155