3 * Copyright (C) 2006,2007 Florian octo Forster
4 * Copyright (C) 2009 Mirko Buffoni
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
21 * Mirko Buffoni <briareos at eswat.org>
29 #include "configfile.h"
31 #include "utils_dns.h"
43 struct counter_list_s *next;
45 typedef struct counter_list_s counter_list_t;
50 static const char *config_keys[] =
54 "SelectNumericQueryTypes"
56 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
57 static int select_numeric_qtype = 1;
59 #define PCAP_SNAPLEN 1460
60 static char *pcap_device = NULL;
62 static counter_t tr_queries;
63 static counter_t tr_responses;
64 static counter_list_t *qtype_list;
65 static counter_list_t *opcode_list;
66 static counter_list_t *rcode_list;
68 static pthread_t listen_thread;
69 static int listen_thread_init = 0;
70 /* The `traffic' mutex if for `tr_queries' and `tr_responses' */
71 static pthread_mutex_t traffic_mutex = PTHREAD_MUTEX_INITIALIZER;
72 static pthread_mutex_t qtype_mutex = PTHREAD_MUTEX_INITIALIZER;
73 static pthread_mutex_t opcode_mutex = PTHREAD_MUTEX_INITIALIZER;
74 static pthread_mutex_t rcode_mutex = PTHREAD_MUTEX_INITIALIZER;
79 static counter_list_t *counter_list_search (counter_list_t **list, unsigned int key)
81 counter_list_t *entry;
83 for (entry = *list; entry != NULL; entry = entry->next)
84 if (entry->key == key)
90 static counter_list_t *counter_list_create (counter_list_t **list,
91 unsigned int key, unsigned int value)
93 counter_list_t *entry;
95 entry = (counter_list_t *) malloc (sizeof (counter_list_t));
99 memset (entry, 0, sizeof (counter_list_t));
101 entry->value = value;
109 counter_list_t *last;
112 while (last->next != NULL)
121 static void counter_list_add (counter_list_t **list,
122 unsigned int key, unsigned int increment)
124 counter_list_t *entry;
126 entry = counter_list_search (list, key);
130 entry->value += increment;
134 counter_list_create (list, key, increment);
138 static int dns_config (const char *key, const char *value)
140 if (strcasecmp (key, "Interface") == 0)
142 if (pcap_device != NULL)
144 if ((pcap_device = strdup (value)) == NULL)
147 else if (strcasecmp (key, "IgnoreSource") == 0)
150 ignore_list_add_name (value);
152 else if (strcasecmp (key, "SelectNumericQueryTypes") == 0)
154 if ((value != NULL) && IS_FALSE (value))
155 select_numeric_qtype = 0;
157 select_numeric_qtype = 1;
167 static void dns_child_callback (const rfc1035_header_t *dns)
171 /* This is a query */
173 if (!select_numeric_qtype)
175 const char *str = qtype_str(dns->qtype);
176 if ((str == NULL) || (str[0] == '#'))
180 pthread_mutex_lock (&traffic_mutex);
181 tr_queries += dns->length;
182 pthread_mutex_unlock (&traffic_mutex);
186 pthread_mutex_lock (&qtype_mutex);
187 counter_list_add (&qtype_list, dns->qtype, 1);
188 pthread_mutex_unlock (&qtype_mutex);
193 /* This is a reply */
194 pthread_mutex_lock (&traffic_mutex);
195 tr_responses += dns->length;
196 pthread_mutex_unlock (&traffic_mutex);
198 pthread_mutex_lock (&rcode_mutex);
199 counter_list_add (&rcode_list, dns->rcode, 1);
200 pthread_mutex_unlock (&rcode_mutex);
203 /* FIXME: Are queries, replies or both interesting? */
204 pthread_mutex_lock (&opcode_mutex);
205 counter_list_add (&opcode_list, dns->opcode, 1);
206 pthread_mutex_unlock (&opcode_mutex);
209 static void *dns_child_loop (void __attribute__((unused)) *dummy)
212 char pcap_error[PCAP_ERRBUF_SIZE];
213 struct bpf_program fp;
217 /* Don't block any signals */
220 sigemptyset (&sigmask);
221 pthread_sigmask (SIG_SETMASK, &sigmask, NULL);
224 /* Passing `pcap_device == NULL' is okay and the same as passign "any" */
225 DEBUG ("dns plugin: Creating PCAP object..");
226 pcap_obj = pcap_open_live ((pcap_device != NULL) ? pcap_device : "any",
228 0 /* Not promiscuous */,
231 if (pcap_obj == NULL)
233 ERROR ("dns plugin: Opening interface `%s' "
235 (pcap_device != NULL) ? pcap_device : "any",
240 memset (&fp, 0, sizeof (fp));
241 if (pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0) < 0)
243 ERROR ("dns plugin: pcap_compile failed");
246 if (pcap_setfilter (pcap_obj, &fp) < 0)
248 ERROR ("dns plugin: pcap_setfilter failed");
252 DEBUG ("dns plugin: PCAP object created.");
254 dnstop_set_pcap_obj (pcap_obj);
255 dnstop_set_callback (dns_child_callback);
257 status = pcap_loop (pcap_obj,
258 -1 /* loop forever */,
259 handle_pcap /* callback */,
260 NULL /* Whatever this means.. */);
262 ERROR ("dns plugin: Listener thread is exiting "
263 "abnormally: %s", pcap_geterr (pcap_obj));
265 DEBUG ("dns plugin: Child is exiting.");
267 pcap_close (pcap_obj);
268 listen_thread_init = 0;
272 } /* static void dns_child_loop (void) */
274 static int dns_init (void)
276 /* clean up an old thread */
279 pthread_mutex_lock (&traffic_mutex);
282 pthread_mutex_unlock (&traffic_mutex);
284 if (listen_thread_init != 0)
287 status = pthread_create (&listen_thread, NULL, dns_child_loop,
292 ERROR ("dns plugin: pthread_create failed: %s",
293 sstrerror (errno, errbuf, sizeof (errbuf)));
297 listen_thread_init = 1;
302 static void submit_counter (const char *type, const char *type_instance,
306 value_list_t vl = VALUE_LIST_INIT;
308 values[0].counter = value;
312 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
313 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
314 sstrncpy (vl.type, type, sizeof (vl.type));
315 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
317 plugin_dispatch_values (&vl);
318 } /* void submit_counter */
320 static void submit_octets (counter_t queries, counter_t responses)
323 value_list_t vl = VALUE_LIST_INIT;
325 values[0].counter = queries;
326 values[1].counter = responses;
330 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
331 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
332 sstrncpy (vl.type, "dns_octets", sizeof (vl.type));
334 plugin_dispatch_values (&vl);
335 } /* void submit_counter */
337 static int dns_read (void)
339 unsigned int keys[T_MAX];
340 unsigned int values[T_MAX];
346 pthread_mutex_lock (&traffic_mutex);
347 values[0] = tr_queries;
348 values[1] = tr_responses;
349 pthread_mutex_unlock (&traffic_mutex);
351 if ((values[0] != 0) || (values[1] != 0))
352 submit_octets (values[0], values[1]);
354 pthread_mutex_lock (&qtype_mutex);
355 for (ptr = qtype_list, len = 0;
356 (ptr != NULL) && (len < T_MAX);
357 ptr = ptr->next, len++)
359 keys[len] = ptr->key;
360 values[len] = ptr->value;
362 pthread_mutex_unlock (&qtype_mutex);
364 for (i = 0; i < len; i++)
366 DEBUG ("dns plugin: qtype = %u; counter = %u;", keys[i], values[i]);
367 submit_counter ("dns_qtype", qtype_str (keys[i]), values[i]);
370 pthread_mutex_lock (&opcode_mutex);
371 for (ptr = opcode_list, len = 0;
372 (ptr != NULL) && (len < T_MAX);
373 ptr = ptr->next, len++)
375 keys[len] = ptr->key;
376 values[len] = ptr->value;
378 pthread_mutex_unlock (&opcode_mutex);
380 for (i = 0; i < len; i++)
382 DEBUG ("dns plugin: opcode = %u; counter = %u;", keys[i], values[i]);
383 submit_counter ("dns_opcode", opcode_str (keys[i]), values[i]);
386 pthread_mutex_lock (&rcode_mutex);
387 for (ptr = rcode_list, len = 0;
388 (ptr != NULL) && (len < T_MAX);
389 ptr = ptr->next, len++)
391 keys[len] = ptr->key;
392 values[len] = ptr->value;
394 pthread_mutex_unlock (&rcode_mutex);
396 for (i = 0; i < len; i++)
398 DEBUG ("dns plugin: rcode = %u; counter = %u;", keys[i], values[i]);
399 submit_counter ("dns_rcode", rcode_str (keys[i]), values[i]);
405 void module_register (void)
407 plugin_register_config ("dns", dns_config, config_keys, config_keys_num);
408 plugin_register_init ("dns", dns_init);
409 plugin_register_read ("dns", dns_read);
410 } /* void module_register */