root/src/modules/dns.c

Revision 88a71780101cbf23034aa0cb840f9f0368fda2dd, 21.6 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 years ago)

fixes #126

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  *       notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  *       copyright notice, this list of conditions and the following
13  *       disclaimer in the documentation and/or other materials provided
14  *       with the distribution.
15  *     * Neither the name OmniTI Computer Consulting, Inc. nor the names
16  *       of its contributors may be used to endorse or promote products
17  *       derived from this software without specific prior written
18  *       permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "noit_defines.h"
34
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <netdb.h>
38 #include <errno.h>
39 #include <assert.h>
40 #include <arpa/inet.h>
41
42 #include "noit_module.h"
43 #include "noit_check.h"
44 #include "noit_check_tools.h"
45 #include "utils/noit_log.h"
46 #include "utils/noit_atomic.h"
47 #include "udns/udns.h"
48
49 static void eventer_dns_utm_fn(struct dns_ctx *, int, void *);
50 static int dns_eventer_callback(eventer_t, int, void *, struct timeval *);
51
52 static noit_log_stream_t nlerr = NULL;
53 static noit_log_stream_t nldeb = NULL;
54
55 static noit_hash_table dns_rtypes = NOIT_HASH_EMPTY;
56 static noit_hash_table dns_ctypes = NOIT_HASH_EMPTY;
57
58 static noit_hash_table dns_ctx_store = NOIT_HASH_EMPTY;
59 static pthread_mutex_t dns_ctx_store_lock;
60 typedef struct dns_ctx_handle {
61   char *ns;
62   struct dns_ctx *ctx;
63   noit_atomic32_t refcnt;
64   eventer_t e; /* evetner handling UDP traffic */
65   eventer_t timeout; /* the timeout managed by libudns */
66 } dns_ctx_handle_t;
67
68 static dns_ctx_handle_t *default_ctx_handle = NULL;
69 static void dns_ctx_handle_free(void *vh) {
70   dns_ctx_handle_t *h = vh;
71   assert(h->timeout == NULL);
72   free(h->ns);
73   dns_close(h->ctx);
74   dns_free(h->ctx);
75 }
76 static dns_ctx_handle_t *dns_ctx_alloc(const char *ns) {
77   void *vh;
78   dns_ctx_handle_t *h = NULL;
79   pthread_mutex_lock(&dns_ctx_store_lock);
80   if(ns == NULL && default_ctx_handle != NULL) {
81     /* special case -- default context */
82     h = default_ctx_handle;
83     noit_atomic_inc32(&h->refcnt);
84     goto bail;
85   }
86   if(ns &&
87      noit_hash_retrieve(&dns_ctx_store, ns, strlen(ns), &vh)) {
88     h = (dns_ctx_handle_t *)vh;
89     noit_atomic_inc32(&h->refcnt);
90   }
91   else {
92     int failed = 0;
93     h = calloc(1, sizeof(*h));
94     h->ns = ns ? strdup(ns) : NULL;
95     h->ctx = dns_new(NULL);
96     if(dns_init(h->ctx, 0) != 0) failed++;
97     if(ns) {
98       if(dns_add_serv(h->ctx, NULL) < 0) failed++;
99       if(dns_add_serv(h->ctx, ns) < 0) failed++;
100     }
101     if(dns_open(h->ctx) < 0) failed++;
102     if(failed) {
103       noitL(nlerr, "dns_open failed\n");
104       free(h->ns);
105       free(h);
106       h = NULL;
107       goto bail;
108     }
109     dns_set_tmcbck(h->ctx, eventer_dns_utm_fn, h);
110     h->e = eventer_alloc();
111     h->e->mask = EVENTER_READ | EVENTER_EXCEPTION;
112     h->e->closure = h;
113     h->e->callback = dns_eventer_callback;
114     h->e->fd = dns_sock(h->ctx);
115     eventer_add(h->e);
116     h->refcnt = 1;
117     if(!ns)
118       default_ctx_handle = h;
119     else
120       noit_hash_store(&dns_ctx_store, h->ns, strlen(h->ns), h);
121   }
122  bail:
123   pthread_mutex_unlock(&dns_ctx_store_lock);
124   return h;
125 }
126 static void dns_ctx_release(dns_ctx_handle_t *h) {
127   if(h->ns == NULL) {
128     /* Special case for the default */
129     noit_atomic_dec32(&h->refcnt);
130     return;
131   }
132   pthread_mutex_lock(&dns_ctx_store_lock);
133   if(noit_atomic_dec32(&h->refcnt) == 0) {
134     /* I was the last one */
135     assert(noit_hash_delete(&dns_ctx_store, h->ns, strlen(h->ns),
136                             NULL, dns_ctx_handle_free));
137   }
138   pthread_mutex_unlock(&dns_ctx_store_lock);
139 }
140
141 static noit_hash_table active_events = NOIT_HASH_EMPTY;
142 static pthread_mutex_t active_events_lock;
143
144 typedef struct dns_check_info {
145   stats_t current;
146   int timed_out;
147   noit_module_t *self;
148   noit_check_t *check;
149   eventer_t timeout_event;
150   dns_ctx_handle_t *h;
151   char *error;
152   int nrr;
153
154   /* These make up the query itself */
155   unsigned char dn[DNS_MAXDN];
156   enum dns_class query_ctype;
157   enum dns_type query_rtype;
158 } dns_check_info_t;
159
160 static int __isactive_ci(struct dns_check_info *ci) {
161   void *u;
162   int exists = 0;
163   pthread_mutex_lock(&active_events_lock);
164   if(noit_hash_retrieve(&active_events, (void *)&ci, sizeof(ci), &u))
165     exists = 1;
166   pthread_mutex_unlock(&active_events_lock);
167   return exists;
168 }
169 static void __activate_ci(struct dns_check_info *ci) {
170   struct dns_check_info **holder;
171   holder = calloc(1, sizeof(*holder));
172   *holder = ci;
173   pthread_mutex_lock(&active_events_lock);
174   assert(noit_hash_store(&active_events, (void *)holder, sizeof(*holder), ci));
175   pthread_mutex_unlock(&active_events_lock);
176 }
177 static void __deactivate_ci(struct dns_check_info *ci) {
178   pthread_mutex_lock(&active_events_lock);
179   assert(noit_hash_delete(&active_events, (void *)&ci, sizeof(ci), free, NULL));
180   pthread_mutex_unlock(&active_events_lock);
181 }
182
183 static void dns_check_log_results(struct dns_check_info *ci) {
184   struct timeval duration;
185
186   gettimeofday(&ci->current.whence, NULL);
187   sub_timeval(ci->current.whence, ci->check->last_fire_time, &duration);
188   ci->current.duration = duration.tv_sec * 1000 + duration.tv_usec / 1000;
189
190   ci->current.state = (ci->error || ci->nrr == 0) ? NP_BAD : NP_GOOD;
191   ci->current.available = ci->timed_out ? NP_UNAVAILABLE : NP_AVAILABLE;
192   if(ci->error) {
193     ci->current.status = strdup(ci->error);
194   }
195   else if(!ci->current.status) {
196     char buff[48];
197     snprintf(buff, sizeof(buff), "%d %s",
198              ci->nrr, ci->nrr == 1 ? "record" : "records");
199     ci->current.status = strdup(buff);
200   }
201
202   noit_check_set_stats(ci->self, ci->check, &ci->current);
203   if(ci->error) free(ci->error);
204   if(ci->current.status) free(ci->current.status);
205   ci->error = NULL;
206   memset(&ci->current, 0, sizeof(ci->current));
207 }
208
209 static int dns_interpolate_inaddr_arpa(char *buff, int len, const char *ip) {
210   const char *b, *e;
211   char *o = buff;
212   int il;
213   /* This function takes a dot delimited string as input and
214    * reverses the parts split on dot.
215    */
216   il = strlen(ip);
217   if(len <= il) {
218     /* not enough room for ip and '\0' */
219     if(len > 0) buff[0] = '\0';
220     return 0;
221   }
222   e = ip + il;
223   b = e - 1;
224   while(b >= ip) {
225     const char *term;
226     while(b >= ip && *b != '.') b--;  /* Rewind to previous part */
227     term = b + 1; /* term is one ahead, we went past it */
228     if(term != e) memcpy(o, term, e - term); /* no sense in copying nothing */
229     o += e - term; /* advance the term length */
230     e = b;
231     b = e - 1;
232     if(e >= ip) *o++ = '.'; /* we must be at . */
233   }
234   *o = '\0';
235   assert((o - buff) == il);
236   return o - buff;
237 }
238
239 static int dns_module_init(noit_module_t *self) {
240   const struct dns_nameval *nv;
241   struct dns_ctx *pctx;
242   int i;
243   pthread_mutex_init(&dns_ctx_store_lock, NULL);
244   pthread_mutex_init(&active_events_lock, NULL);
245   /* HASH the rr types */
246   for(i=0, nv = dns_type_index(i); nv->name; nv = dns_type_index(++i))
247     noit_hash_store(&dns_rtypes,
248                     nv->name, strlen(nv->name),
249                     (void *)nv);
250   /* HASH the class types */
251   for(i=0, nv = dns_class_index(i); nv->name; nv = dns_class_index(++i))
252     noit_hash_store(&dns_ctypes,
253                     nv->name, strlen(nv->name),
254                     (void *)nv);
255
256   noit_check_interpolate_register_oper_fn("inaddrarpa",
257                                           dns_interpolate_inaddr_arpa);
258
259   if (dns_init(NULL, 0) < 0 || (pctx = dns_new(NULL)) == NULL) {
260     noitL(nlerr, "Unable to initialize dns subsystem\n");
261     return -1;
262   }
263   dns_free(pctx);
264   if(dns_ctx_alloc(NULL) == NULL) {
265     noitL(nlerr, "Error setting up default dns resolver context.\n");
266     return -1;
267   }
268   return 0;
269 }
270
271 static void dns_check_cleanup(noit_module_t *self, noit_check_t *check) {
272 }
273
274 static int dns_eventer_callback(eventer_t e, int mask, void *closure,
275                                 struct timeval *now) {
276   dns_ctx_handle_t *h = closure;
277   dns_ioevent(h->ctx, now->tv_sec);
278   return EVENTER_READ | EVENTER_EXCEPTION;
279 }
280
281 static int dns_check_timeout(eventer_t e, int mask, void *closure,
282                              struct timeval *now) {
283   struct dns_check_info *ci;
284   ci = closure;
285   ci->timeout_event = NULL;
286   ci->check->flags &= ~NP_RUNNING;
287   dns_check_log_results(ci);
288   __deactivate_ci(ci);
289   return 0;
290 }
291
292 static int dns_invoke_timeouts(eventer_t e, int mask, void *closure,
293                                struct timeval *now) {
294   dns_ctx_handle_t *h = closure;
295   dns_timeouts(h->ctx, 0, now->tv_sec);
296   return 0;
297 }
298 static void eventer_dns_utm_fn(struct dns_ctx *ctx, int timeout, void *data) {
299   dns_ctx_handle_t *h = data;
300   eventer_t e = NULL, newe = NULL;
301   if(ctx == NULL) e = eventer_remove(h->timeout);
302   else {
303     assert(h->ctx == ctx);
304     if(timeout < 0) e = eventer_remove(h->timeout);
305     else {
306       newe = eventer_alloc();
307       newe->mask = EVENTER_TIMER;
308       newe->callback = dns_invoke_timeouts;
309       newe->closure = h;
310       gettimeofday(&newe->whence, NULL);
311       newe->whence.tv_sec += timeout;
312     }
313   }
314   if(e) eventer_free(e);
315   if(newe) eventer_add(newe);
316   h->timeout = newe;
317 }
318
319 static char *encode_txt(char *dst, const unsigned char *src, int len) {
320   int i;
321   for(i=0; i<len; i++) {
322     if(src[i] >= 127 || src[i] <= 31) {
323       snprintf(dst, 4, "\\%02x", src[i]);
324       dst += 3;
325     }
326     else if(src[i] == '\\') {
327       *dst++ = '\\';
328       *dst++ = '\\';
329     }
330     else {
331       *dst++ = (char)src[i];
332     }
333   }
334   *dst = '\0';
335   return dst;
336 }
337
338 static void decode_rr(struct dns_check_info *ci, struct dns_parse *p,
339                       struct dns_rr *rr, char **output) {
340   char buff[DNS_MAXDN], *txt_str, *c;
341   u_int32_t ttl, vu;
342   int32_t vs;
343   int totalsize;
344   const unsigned char *pkt = p->dnsp_pkt;
345   const unsigned char *end = p->dnsp_end;
346   const unsigned char *dptr = rr->dnsrr_dptr;
347   const unsigned char *dend = rr->dnsrr_dend;
348   unsigned char *dn = rr->dnsrr_dn;
349   const unsigned char *tmp;
350
351   /* Not interested unless it is the answer to my exact question */
352   if (!dns_dnequal(ci->dn, dn)) return;
353
354   if (!p->dnsp_rrl && !rr->dnsrr_dn[0] && rr->dnsrr_typ == DNS_T_OPT) {
355     /* We don't handle EDNS0 OPT records */
356     goto decode_err;
357   }
358   noitL(nldeb, "%s. %u %s %s\n", dns_dntosp(dn), rr->dnsrr_ttl,
359         dns_classname(rr->dnsrr_cls),
360         dns_typename(rr->dnsrr_typ));
361
362   ttl = rr->dnsrr_ttl;
363   noit_stats_set_metric(&ci->current, "ttl", METRIC_UINT32, &ttl);
364
365   switch(rr->dnsrr_typ) {
366    case DNS_T_A:
367     if (rr->dnsrr_dsz != 4) goto decode_err;
368     snprintf(buff, sizeof(buff), "%d.%d.%d.%d",
369              dptr[0], dptr[1], dptr[2], dptr[3]);
370     break;
371
372    case DNS_T_AAAA:
373     if (rr->dnsrr_dsz != 16) goto decode_err;
374     inet_ntop(AF_INET6, dptr, buff, 16);
375     break;
376
377    case DNS_T_TXT:
378     totalsize = 0;
379     for(tmp = dptr; tmp < dend; totalsize += *tmp, tmp += *tmp + 1)
380       if(tmp + *tmp + 1 > dend) goto decode_err;
381     /* worst case: every character escaped + '\0' */
382     txt_str = alloca(totalsize * 3 + 1);
383     if(!txt_str) goto decode_err;
384     c = txt_str;
385     for(tmp = dptr; tmp < dend; tmp += *tmp + 1)
386       c = encode_txt(c, tmp+1, *tmp);
387     break;
388
389    case DNS_T_MX:
390     snprintf(buff, sizeof(buff), "%d ", dns_get16(dptr));
391     tmp = dptr + 2;
392     if(dns_getdn(pkt, &tmp, end, dn, DNS_MAXDN) <= 0 || tmp != dend)
393       goto decode_err;
394     dns_dntop(dn, buff + strlen(buff), sizeof(buff) - strlen(buff));
395     break;
396
397    case DNS_T_SOA:
398      if(dns_getdn(pkt, &dptr, end, dn, DNS_MAXDN) <= 0) goto decode_err;
399      dns_dntop(dn, buff, sizeof(buff));
400      noit_stats_set_metric(&ci->current, "name-server", METRIC_STRING, buff);
401      if(dns_getdn(pkt, &dptr, end, dn, DNS_MAXDN) <= 0) goto decode_err;
402      dns_dntop(dn, buff, sizeof(buff));
403      noit_stats_set_metric(&ci->current, "email-addr", METRIC_STRING, buff);
404      if(dptr + 5 * sizeof(u_int32_t) != dend) goto decode_err;
405      vu = dns_get32(dptr); dptr += sizeof(u_int32_t);
406      noit_stats_set_metric(&ci->current, "serial", METRIC_UINT32, &vu);
407      /* the serial is what we elect to store as the "answer" as text...
408       * because it rarely changes and that seems the most interesting thing
409       * to track change-log-style.
410       */
411      snprintf(buff, sizeof(buff), "%u", vu);
412      vs = dns_get32(dptr); dptr += sizeof(int32_t);
413      noit_stats_set_metric(&ci->current, "refresh", METRIC_UINT32, &vs);
414      vs = dns_get32(dptr); dptr += sizeof(int32_t);
415      noit_stats_set_metric(&ci->current, "retry", METRIC_UINT32, &vs);
416      vs = dns_get32(dptr); dptr += sizeof(int32_t);
417      noit_stats_set_metric(&ci->current, "expiry", METRIC_UINT32, &vs);
418      vs = dns_get32(dptr); dptr += sizeof(int32_t);
419      noit_stats_set_metric(&ci->current, "minimum", METRIC_UINT32, &vs);
420      break;
421
422    case DNS_T_CNAME:
423    case DNS_T_PTR:
424    case DNS_T_NS:
425    case DNS_T_MB:
426    case DNS_T_MD:
427    case DNS_T_MF:
428    case DNS_T_MG:
429    case DNS_T_MR:
430     if(dns_getdn(pkt, &dptr, end, dn, DNS_MAXDN) <= 0) goto decode_err;
431     dns_dntop(dn, buff, sizeof(buff));
432     break;
433
434    default:
435     break;
436   }
437   if(*output) {
438     int newlen = strlen(*output) + strlen(", ") + strlen(buff) + 1;
439     char *newstr;
440     newstr = malloc(newlen);
441     snprintf(newstr, newlen, "%s, %s", *output, buff);
442     free(*output);
443     *output = newstr;
444   }
445   else
446     *output = strdup(buff);
447   ci->nrr++;
448   return;
449
450  decode_err:
451   ci->error = strdup("RR decode error");
452   return;
453 }
454
455 static void dns_cb(struct dns_ctx *ctx, void *result, void *data) {
456   int r = dns_status(ctx);
457   struct dns_check_info *ci = data;
458   struct dns_parse p;
459   struct dns_rr rr;
460   unsigned nrr;
461   unsigned char dn[DNS_MAXDN];
462   const unsigned char *pkt, *cur, *end;
463   char *result_str = NULL;
464
465   /* If out ci isn't active, we must have timed out already */
466   if(!__isactive_ci(ci)) {
467     if(result) free(result);
468     return;
469   }
470
471   ci->timed_out = 0;
472   /* If we don't have a result, explode */
473   if (!result) {
474     ci->error = strdup(dns_strerror(r));
475     goto cleanup;
476   }
477
478   /* Process the packet */
479   pkt = result; end = pkt + r; cur = dns_payload(pkt);
480   dns_getdn(pkt, &cur, end, dn, sizeof(dn));
481   dns_initparse(&p, NULL, pkt, cur, end);
482   p.dnsp_qcls = p.dnsp_qtyp = 0;
483   nrr = 0;
484
485   while((r = dns_nextrr(&p, &rr)) > 0) {
486     if (!dns_dnequal(dn, rr.dnsrr_dn)) continue;
487     if ((ci->query_ctype == DNS_C_ANY || ci->query_ctype == rr.dnsrr_cls) &&
488         (ci->query_rtype == DNS_T_ANY || ci->query_rtype == rr.dnsrr_typ))
489       ++nrr;
490     else if (rr.dnsrr_typ == DNS_T_CNAME && !nrr) {
491       if (dns_getdn(pkt, &rr.dnsrr_dptr, end,
492                     p.dnsp_dnbuf, sizeof(p.dnsp_dnbuf)) <= 0 ||
493           rr.dnsrr_dptr != rr.dnsrr_dend) {
494         ci->error = strdup("protocol error");
495         break;
496       }
497       else {
498         noitL(nldeb, "%s.\n", dns_dntosp(dn));
499         noitL(nldeb, " CNAME %s.\n", dns_dntosp(p.dnsp_dnbuf));
500         dns_dntodn(p.dnsp_dnbuf, dn, sizeof(dn));
501         noitL(nldeb, " ---> '%s'\n", dn);
502       }
503     }
504   }
505   if (!r && !nrr) {
506     ci->error = strdup("no data");
507   }
508
509   dns_rewind(&p, NULL);
510   p.dnsp_qtyp = ci->query_rtype == DNS_T_ANY ? 0 : ci->query_rtype;
511   p.dnsp_qcls = ci->query_ctype == DNS_C_ANY ? 0 : ci->query_ctype;
512   while(dns_nextrr(&p, &rr))
513     decode_rr(ci, &p, &rr, &result_str);
514   noit_stats_set_metric(&ci->current, "answer", METRIC_STRING, result_str);
515
516  cleanup:
517   if(result) free(result);
518   if(ci->timeout_event) {
519     eventer_t e = eventer_remove(ci->timeout_event);
520     ci->timeout_event = NULL;
521     if(e) eventer_free(e);
522   }
523   ci->check->flags &= ~NP_RUNNING;
524   dns_check_log_results(ci);
525   __deactivate_ci(ci);
526 }
527
528 static int dns_check_send(noit_module_t *self, noit_check_t *check) {
529   void *vnv_pair = NULL;
530   struct dns_nameval *nv_pair;
531   eventer_t newe;
532   struct timeval p_int, now;
533   struct dns_check_info *ci = check->closure;
534   const char *config_val;
535   const char *rtype = NULL;
536   const char *nameserver = NULL;
537   const char *ctype = "IN";
538   const char *query = NULL;
539   char interpolated_nameserver[1024];
540   char interpolated_query[1024];
541   noit_hash_table check_attrs_hash = NOIT_HASH_EMPTY;
542
543   gettimeofday(&now, NULL);
544   memcpy(&check->last_fire_time, &now, sizeof(now));
545   ci->current.state = NP_BAD;
546   ci->current.available = NP_UNAVAILABLE;
547   ci->timed_out = 1;
548   ci->nrr = 0;
549
550   if(!strcmp(check->name, "in-addr.arpa")) {
551     /* in-addr.arpa defaults:
552      *   nameserver to NULL
553      *   rtype to PTR
554      *   query to %[:inaddrarpa:target].in-addr.arpa
555      */
556     nameserver = NULL;
557     rtype = "PTR";
558     query = "%[:inaddrarpa:target].in-addr.arpa";
559   }
560   else {
561     nameserver = "%[target]";
562     rtype = "A";
563     query = "%[name]";
564   }
565 #define CONFIG_OVERRIDE(a) \
566   if(noit_hash_retr_str(check->config, #a, strlen(#a), \
567                         &config_val) && \
568      strlen(config_val) > 0) \
569     a = config_val
570   CONFIG_OVERRIDE(ctype);
571   CONFIG_OVERRIDE(nameserver);
572   CONFIG_OVERRIDE(rtype);
573   CONFIG_OVERRIDE(query);
574
575   noit_check_make_attrs(check, &check_attrs_hash);
576 #define CA_STORE(a,b) noit_hash_store(&check_attrs_hash, a, strlen(a), b)
577   CA_STORE("target", check->target);
578   CA_STORE("name", check->name);
579   CA_STORE("module", check->module);
580
581   if(nameserver) {
582     noit_check_interpolate(interpolated_nameserver,
583                            sizeof(interpolated_nameserver),
584                            nameserver,
585                            &check_attrs_hash, check->config);
586     nameserver = interpolated_nameserver;
587   }
588   if(query) {
589     noit_check_interpolate(interpolated_query,
590                            sizeof(interpolated_query),
591                            query,
592                            &check_attrs_hash, check->config);
593     query = interpolated_query;
594   }
595   noit_hash_destroy(&check_attrs_hash, NULL, NULL);
596
597   check->flags |= NP_RUNNING;
598   noitL(nldeb, "dns_check_send(%p,%s,%s,%s,%s,%s)\n",
599         self, check->target, nameserver ? nameserver : "default",
600         query ? query : "null", ctype, rtype);
601
602   __activate_ci(ci);
603   /* If this ci has a handle and it isn't the one we need,
604    * we should release it
605    */
606   if(ci->h &&
607      ((ci->h->ns == NULL && nameserver != NULL) ||
608       (ci->h->ns != NULL && nameserver == NULL) ||
609       (ci->h->ns && strcmp(ci->h->ns, nameserver)))) {
610     dns_ctx_release(ci->h);
611     ci->h = NULL;
612   }
613   /* use the cached one, unless we don't have one */
614   if(!ci->h) ci->h = dns_ctx_alloc(nameserver);
615
616   /* Lookup out class */
617   if(!noit_hash_retrieve(&dns_ctypes, ctype, strlen(ctype),
618                          &vnv_pair)) {
619     ci->error = strdup("bad class");
620   }
621   else {
622     nv_pair = (struct dns_nameval *)vnv_pair;
623     ci->query_ctype = nv_pair->val;
624   }
625   /* Lookup out rr type */
626   if(!noit_hash_retrieve(&dns_rtypes, rtype, strlen(rtype),
627                          &vnv_pair)) {
628     ci->error = strdup("bad rr type");
629   }
630   else {
631     nv_pair = (struct dns_nameval *)vnv_pair;
632     ci->query_rtype = nv_pair->val;
633   }
634
635   if(!ci->error) {
636     /* Submit the query */
637     int abs;
638     if(!dns_ptodn(query, strlen(query), ci->dn, sizeof(ci->dn), &abs) ||
639        !dns_submit_dn(ci->h->ctx, ci->dn, ci->query_ctype, ci->query_rtype,
640                       abs | DNS_NOSRCH, NULL, dns_cb, ci)) {
641       ci->error = strdup("submission error");
642     }
643     else {
644       dns_timeouts(ci->h->ctx, -1, now.tv_sec);
645     }
646   }
647
648   /* we could have completed by now... if so, we've nothing to do */
649
650   if(!__isactive_ci(ci)) return 0;
651
652   if(ci->error) {
653     /* Errors here are easy, fail and avoid scheduling a timeout */
654     ci->check->flags &= ~NP_RUNNING;
655     dns_check_log_results(ci);
656     __deactivate_ci(ci);
657     return 0;
658   }
659
660   newe = eventer_alloc();
661   newe->mask = EVENTER_TIMER;
662   gettimeofday(&now, NULL);
663   p_int.tv_sec = check->timeout / 1000;
664   p_int.tv_usec = (check->timeout % 1000) * 1000;
665   add_timeval(now, p_int, &newe->whence);
666   newe->closure = ci;
667   newe->callback = dns_check_timeout;
668   ci->timeout_event = newe;
669   eventer_add(newe);
670
671   return 0;
672 }
673
674 static int dns_initiate_check(noit_module_t *self, noit_check_t *check,
675                               int once, noit_check_t *cause) {
676   struct dns_check_info *ci;
677   if(!check->closure)
678     check->closure = calloc(1, sizeof(struct dns_check_info));
679   ci = check->closure;
680   ci->check = check;
681   ci->self = self;
682   INITIATE_CHECK(dns_check_send, self, check);
683   return 0;
684 }
685
686 static int dns_config(noit_module_t *self, noit_hash_table *options) {
687   return 0;
688 }
689
690 static int dns_onload(noit_image_t *self) {
691   nlerr = noit_log_stream_find("error/dns");
692   nldeb = noit_log_stream_find("debug/dns");
693   if(!nlerr) nlerr = noit_stderr;
694   if(!nldeb) nldeb = noit_debug;
695   eventer_name_callback("dns/dns_eventer_callback", dns_eventer_callback);
696   eventer_name_callback("dns/dns_check_timeout", dns_check_timeout);
697   eventer_name_callback("dns/dns_invoke_timeouts", dns_invoke_timeouts);
698   return 0;
699 }
700
701 #include "dns.xmlh"
702 noit_module_t dns = {
703   {
704     NOIT_MODULE_MAGIC,
705     NOIT_MODULE_ABI_VERSION,
706     "dns",
707     "DNS RR checker",
708     dns_xml_description,
709     dns_onload
710   },
711   dns_config,
712   dns_module_init,
713   dns_initiate_check,
714   dns_check_cleanup
715 };
716
Note: See TracBrowser for help on using the browser.