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>
25 #include "configfile.h"
27 #include "utils_dns.h"
39 struct counter_list_s *next;
41 typedef struct counter_list_s counter_list_t;
46 static const char *config_keys[] =
52 static int config_keys_num = 2;
54 #define PCAP_SNAPLEN 1460
55 static char *pcap_device = NULL;
57 static counter_t tr_queries;
58 static counter_t tr_responses;
59 static counter_list_t *qtype_list;
60 static counter_list_t *opcode_list;
61 static counter_list_t *rcode_list;
63 static pthread_t listen_thread;
64 static int listen_thread_init = 0;
65 /* The `traffic' mutex if for `tr_queries' and `tr_responses' */
66 static pthread_mutex_t traffic_mutex = PTHREAD_MUTEX_INITIALIZER;
67 static pthread_mutex_t qtype_mutex = PTHREAD_MUTEX_INITIALIZER;
68 static pthread_mutex_t opcode_mutex = PTHREAD_MUTEX_INITIALIZER;
69 static pthread_mutex_t rcode_mutex = PTHREAD_MUTEX_INITIALIZER;
74 static counter_list_t *counter_list_search (counter_list_t **list, unsigned int key)
76 counter_list_t *entry;
78 DEBUG ("counter_list_search (list = %p, key = %u)",
81 for (entry = *list; entry != NULL; entry = entry->next)
82 if (entry->key == key)
85 DEBUG ("return (%p)", (void *) entry);
89 static counter_list_t *counter_list_create (counter_list_t **list,
90 unsigned int key, unsigned int value)
92 counter_list_t *entry;
94 DEBUG ("counter_list_create (list = %p, key = %u, value = %u)",
95 (void *) *list, key, value);
97 entry = (counter_list_t *) malloc (sizeof (counter_list_t));
101 memset (entry, 0, sizeof (counter_list_t));
103 entry->value = value;
111 counter_list_t *last;
114 while (last->next != NULL)
120 DEBUG ("return (%p)", (void *) entry);
124 static void counter_list_add (counter_list_t **list,
125 unsigned int key, unsigned int increment)
127 counter_list_t *entry;
129 DEBUG ("counter_list_add (list = %p, key = %u, increment = %u)",
130 (void *) *list, key, increment);
132 entry = counter_list_search (list, key);
136 entry->value += increment;
140 counter_list_create (list, key, increment);
145 static int dns_config (const char *key, const char *value)
147 if (strcasecmp (key, "Interface") == 0)
149 if (pcap_device != NULL)
151 if ((pcap_device = strdup (value)) == NULL)
154 else if (strcasecmp (key, "IgnoreSource") == 0)
157 ignore_list_add_name (value);
167 static void dns_child_callback (const rfc1035_header_t *dns)
171 /* This is a query */
172 pthread_mutex_lock (&traffic_mutex);
173 tr_queries += dns->length;
174 pthread_mutex_unlock (&traffic_mutex);
176 pthread_mutex_lock (&qtype_mutex);
177 counter_list_add (&qtype_list, dns->qtype, 1);
178 pthread_mutex_unlock (&qtype_mutex);
182 /* This is a reply */
183 pthread_mutex_lock (&traffic_mutex);
184 tr_responses += dns->length;
185 pthread_mutex_unlock (&traffic_mutex);
187 pthread_mutex_lock (&rcode_mutex);
188 counter_list_add (&rcode_list, dns->rcode, 1);
189 pthread_mutex_unlock (&rcode_mutex);
192 /* FIXME: Are queries, replies or both interesting? */
193 pthread_mutex_lock (&opcode_mutex);
194 counter_list_add (&opcode_list, dns->opcode, 1);
195 pthread_mutex_unlock (&opcode_mutex);
198 static void *dns_child_loop (void *dummy)
201 char pcap_error[PCAP_ERRBUF_SIZE];
202 struct bpf_program fp;
206 /* Don't block any signals */
209 sigemptyset (&sigmask);
210 pthread_sigmask (SIG_SETMASK, &sigmask, NULL);
213 /* Passing `pcap_device == NULL' is okay and the same as passign "any" */
214 DEBUG ("Creating PCAP object..");
215 pcap_obj = pcap_open_live (pcap_device,
217 0 /* Not promiscuous */,
220 if (pcap_obj == NULL)
222 ERROR ("dns plugin: Opening interface `%s' "
224 (pcap_device != NULL) ? pcap_device : "any",
229 memset (&fp, 0, sizeof (fp));
230 if (pcap_compile (pcap_obj, &fp, "udp port 53", 1, 0) < 0)
232 ERROR ("dns plugin: pcap_compile failed");
235 if (pcap_setfilter (pcap_obj, &fp) < 0)
237 ERROR ("dns plugin: pcap_setfilter failed");
241 DEBUG ("PCAP object created.");
243 dnstop_set_pcap_obj (pcap_obj);
244 dnstop_set_callback (dns_child_callback);
246 status = pcap_loop (pcap_obj,
247 -1 /* loop forever */,
248 handle_pcap /* callback */,
249 NULL /* Whatever this means.. */);
251 ERROR ("dns plugin: Listener thread is exiting "
252 "abnormally: %s", pcap_geterr (pcap_obj));
254 DEBUG ("child is exiting");
256 pcap_close (pcap_obj);
257 listen_thread_init = 0;
261 } /* static void dns_child_loop (void) */
263 static int dns_init (void)
265 /* clean up an old thread */
268 pthread_mutex_lock (&traffic_mutex);
271 pthread_mutex_unlock (&traffic_mutex);
273 if (listen_thread_init != 0)
276 status = pthread_create (&listen_thread, NULL, dns_child_loop,
281 ERROR ("dns plugin: pthread_create failed: %s",
282 sstrerror (errno, errbuf, sizeof (errbuf)));
286 listen_thread_init = 1;
291 static void submit_counter (const char *type, const char *type_instance,
295 value_list_t vl = VALUE_LIST_INIT;
297 values[0].counter = value;
301 vl.time = time (NULL);
302 strcpy (vl.host, hostname_g);
303 strcpy (vl.plugin, "dns");
304 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
306 plugin_dispatch_values (type, &vl);
307 } /* void submit_counter */
309 static void submit_octets (counter_t queries, counter_t responses)
312 value_list_t vl = VALUE_LIST_INIT;
314 values[0].counter = queries;
315 values[1].counter = responses;
319 vl.time = time (NULL);
320 strcpy (vl.host, hostname_g);
321 strcpy (vl.plugin, "dns");
323 plugin_dispatch_values ("dns_octets", &vl);
324 } /* void submit_counter */
326 static int dns_read (void)
328 unsigned int keys[T_MAX];
329 unsigned int values[T_MAX];
335 pthread_mutex_lock (&traffic_mutex);
336 values[0] = tr_queries;
337 values[1] = tr_responses;
338 pthread_mutex_unlock (&traffic_mutex);
340 if ((values[0] != 0) || (values[1] != 0))
341 submit_octets (values[0], values[1]);
343 pthread_mutex_lock (&qtype_mutex);
344 for (ptr = qtype_list, len = 0;
345 (ptr != NULL) && (len < T_MAX);
346 ptr = ptr->next, len++)
348 keys[len] = ptr->key;
349 values[len] = ptr->value;
351 pthread_mutex_unlock (&qtype_mutex);
353 for (i = 0; i < len; i++)
355 DEBUG ("qtype = %u; counter = %u;", keys[i], values[i]);
356 submit_counter ("dns_qtype", qtype_str (keys[i]), values[i]);
359 pthread_mutex_lock (&opcode_mutex);
360 for (ptr = opcode_list, len = 0;
361 (ptr != NULL) && (len < T_MAX);
362 ptr = ptr->next, len++)
364 keys[len] = ptr->key;
365 values[len] = ptr->value;
367 pthread_mutex_unlock (&opcode_mutex);
369 for (i = 0; i < len; i++)
371 DEBUG ("opcode = %u; counter = %u;", keys[i], values[i]);
372 submit_counter ("dns_opcode", opcode_str (keys[i]), values[i]);
375 pthread_mutex_lock (&rcode_mutex);
376 for (ptr = rcode_list, len = 0;
377 (ptr != NULL) && (len < T_MAX);
378 ptr = ptr->next, len++)
380 keys[len] = ptr->key;
381 values[len] = ptr->value;
383 pthread_mutex_unlock (&rcode_mutex);
385 for (i = 0; i < len; i++)
387 DEBUG ("rcode = %u; counter = %u;", keys[i], values[i]);
388 submit_counter ("dns_rcode", rcode_str (keys[i]), values[i]);
394 void module_register (void)
396 plugin_register_config ("dns", dns_config, config_keys, config_keys_num);
397 plugin_register_init ("dns", dns_init);
398 plugin_register_read ("dns", dns_read);
399 } /* void module_register */