3 * Copyright (C) 2006,2007 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
27 #include "configfile.h"
29 #include "utils_dns.h"
41 struct counter_list_s *next;
43 typedef struct counter_list_s counter_list_t;
48 static const char *config_keys[] =
52 "SelectNumericQueryTypes"
54 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
55 static int select_numeric_qtype = 1;
57 #define PCAP_SNAPLEN 1460
58 static char *pcap_device = NULL;
60 static counter_t tr_queries;
61 static counter_t tr_responses;
62 static counter_list_t *qtype_list;
63 static counter_list_t *opcode_list;
64 static counter_list_t *rcode_list;
66 static pthread_t listen_thread;
67 static int listen_thread_init = 0;
68 /* The `traffic' mutex if for `tr_queries' and `tr_responses' */
69 static pthread_mutex_t traffic_mutex = PTHREAD_MUTEX_INITIALIZER;
70 static pthread_mutex_t qtype_mutex = PTHREAD_MUTEX_INITIALIZER;
71 static pthread_mutex_t opcode_mutex = PTHREAD_MUTEX_INITIALIZER;
72 static pthread_mutex_t rcode_mutex = PTHREAD_MUTEX_INITIALIZER;
77 static counter_list_t *counter_list_search (counter_list_t **list, unsigned int key)
79 counter_list_t *entry;
81 DEBUG ("counter_list_search (list = %p, key = %u)",
84 for (entry = *list; entry != NULL; entry = entry->next)
85 if (entry->key == key)
88 DEBUG ("return (%p)", (void *) entry);
92 static counter_list_t *counter_list_create (counter_list_t **list,
93 unsigned int key, unsigned int value)
95 counter_list_t *entry;
97 DEBUG ("counter_list_create (list = %p, key = %u, value = %u)",
98 (void *) *list, key, value);
100 entry = (counter_list_t *) malloc (sizeof (counter_list_t));
104 memset (entry, 0, sizeof (counter_list_t));
106 entry->value = value;
114 counter_list_t *last;
117 while (last->next != NULL)
123 DEBUG ("return (%p)", (void *) entry);
127 static void counter_list_add (counter_list_t **list,
128 unsigned int key, unsigned int increment)
130 counter_list_t *entry;
132 DEBUG ("counter_list_add (list = %p, key = %u, increment = %u)",
133 (void *) *list, key, increment);
135 entry = counter_list_search (list, key);
139 entry->value += increment;
143 counter_list_create (list, key, increment);
148 static int dns_config (const char *key, const char *value)
150 if (strcasecmp (key, "Interface") == 0)
152 if (pcap_device != NULL)
154 if ((pcap_device = strdup (value)) == NULL)
157 else if (strcasecmp (key, "IgnoreSource") == 0)
160 ignore_list_add_name (value);
162 else if (strcasecmp (key, "SelectNumericQueryTypes") == 0)
164 if ((value != NULL) && IS_FALSE (value))
165 select_numeric_qtype = 0;
167 select_numeric_qtype = 1;
177 static void dns_child_callback (const rfc1035_header_t *dns)
181 /* This is a query */
183 if (!select_numeric_qtype)
185 const char *str = qtype_str(dns->qtype);
186 if ((str == NULL) || (str[0] == '#'))
190 pthread_mutex_lock (&traffic_mutex);
191 tr_queries += dns->length;
192 pthread_mutex_unlock (&traffic_mutex);
196 pthread_mutex_lock (&qtype_mutex);
197 counter_list_add (&qtype_list, dns->qtype, 1);
198 pthread_mutex_unlock (&qtype_mutex);
203 /* This is a reply */
204 pthread_mutex_lock (&traffic_mutex);
205 tr_responses += dns->length;
206 pthread_mutex_unlock (&traffic_mutex);
208 pthread_mutex_lock (&rcode_mutex);
209 counter_list_add (&rcode_list, dns->rcode, 1);
210 pthread_mutex_unlock (&rcode_mutex);
213 /* FIXME: Are queries, replies or both interesting? */
214 pthread_mutex_lock (&opcode_mutex);
215 counter_list_add (&opcode_list, dns->opcode, 1);
216 pthread_mutex_unlock (&opcode_mutex);
219 static void *dns_child_loop (void __attribute__((unused)) *dummy)
222 char pcap_error[PCAP_ERRBUF_SIZE];
223 struct bpf_program fp;
227 /* Don't block any signals */
230 sigemptyset (&sigmask);
231 pthread_sigmask (SIG_SETMASK, &sigmask, NULL);
234 /* Passing `pcap_device == NULL' is okay and the same as passign "any" */
235 DEBUG ("dns plugin: Creating PCAP object..");
236 pcap_obj = pcap_open_live ((pcap_device != NULL) ? pcap_device : "any",
238 0 /* Not promiscuous */,
241 if (pcap_obj == NULL)
243 ERROR ("dns plugin: Opening interface `%s' "
245 (pcap_device != NULL) ? pcap_device : "any",
250 memset (&fp, 0, sizeof (fp));
251 if (pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0) < 0)
253 ERROR ("dns plugin: pcap_compile failed");
256 if (pcap_setfilter (pcap_obj, &fp) < 0)
258 ERROR ("dns plugin: pcap_setfilter failed");
262 DEBUG ("PCAP object created.");
264 dnstop_set_pcap_obj (pcap_obj);
265 dnstop_set_callback (dns_child_callback);
267 status = pcap_loop (pcap_obj,
268 -1 /* loop forever */,
269 handle_pcap /* callback */,
270 NULL /* Whatever this means.. */);
272 ERROR ("dns plugin: Listener thread is exiting "
273 "abnormally: %s", pcap_geterr (pcap_obj));
275 DEBUG ("child is exiting");
277 pcap_close (pcap_obj);
278 listen_thread_init = 0;
282 } /* static void dns_child_loop (void) */
284 static int dns_init (void)
286 /* clean up an old thread */
289 pthread_mutex_lock (&traffic_mutex);
292 pthread_mutex_unlock (&traffic_mutex);
294 if (listen_thread_init != 0)
297 status = pthread_create (&listen_thread, NULL, dns_child_loop,
302 ERROR ("dns plugin: pthread_create failed: %s",
303 sstrerror (errno, errbuf, sizeof (errbuf)));
307 listen_thread_init = 1;
312 static void submit_counter (const char *type, const char *type_instance,
316 value_list_t vl = VALUE_LIST_INIT;
318 values[0].counter = value;
322 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
323 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
324 sstrncpy (vl.type, type, sizeof (vl.type));
325 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
327 plugin_dispatch_values (&vl);
328 } /* void submit_counter */
330 static void submit_octets (counter_t queries, counter_t responses)
333 value_list_t vl = VALUE_LIST_INIT;
335 values[0].counter = queries;
336 values[1].counter = responses;
340 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
341 sstrncpy (vl.plugin, "dns", sizeof (vl.plugin));
342 sstrncpy (vl.type, "dns_octets", sizeof (vl.type));
344 plugin_dispatch_values (&vl);
345 } /* void submit_counter */
347 static int dns_read (void)
349 unsigned int keys[T_MAX];
350 unsigned int values[T_MAX];
356 pthread_mutex_lock (&traffic_mutex);
357 values[0] = tr_queries;
358 values[1] = tr_responses;
359 pthread_mutex_unlock (&traffic_mutex);
361 if ((values[0] != 0) || (values[1] != 0))
362 submit_octets (values[0], values[1]);
364 pthread_mutex_lock (&qtype_mutex);
365 for (ptr = qtype_list, len = 0;
366 (ptr != NULL) && (len < T_MAX);
367 ptr = ptr->next, len++)
369 keys[len] = ptr->key;
370 values[len] = ptr->value;
372 pthread_mutex_unlock (&qtype_mutex);
374 for (i = 0; i < len; i++)
376 DEBUG ("qtype = %u; counter = %u;", keys[i], values[i]);
377 submit_counter ("dns_qtype", qtype_str (keys[i]), values[i]);
380 pthread_mutex_lock (&opcode_mutex);
381 for (ptr = opcode_list, len = 0;
382 (ptr != NULL) && (len < T_MAX);
383 ptr = ptr->next, len++)
385 keys[len] = ptr->key;
386 values[len] = ptr->value;
388 pthread_mutex_unlock (&opcode_mutex);
390 for (i = 0; i < len; i++)
392 DEBUG ("opcode = %u; counter = %u;", keys[i], values[i]);
393 submit_counter ("dns_opcode", opcode_str (keys[i]), values[i]);
396 pthread_mutex_lock (&rcode_mutex);
397 for (ptr = rcode_list, len = 0;
398 (ptr != NULL) && (len < T_MAX);
399 ptr = ptr->next, len++)
401 keys[len] = ptr->key;
402 values[len] = ptr->value;
404 pthread_mutex_unlock (&rcode_mutex);
406 for (i = 0; i < len; i++)
408 DEBUG ("rcode = %u; counter = %u;", keys[i], values[i]);
409 submit_counter ("dns_rcode", rcode_str (keys[i]), values[i]);
415 void module_register (void)
417 plugin_register_config ("dns", dns_config, config_keys, config_keys_num);
418 plugin_register_init ("dns", dns_init);
419 plugin_register_read ("dns", dns_read);
420 } /* void module_register */