File: dnscache.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (496 lines) | stat: -rw-r--r-- 13,172 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
 * Copyright (c) 2002-2012 Balabit
 * Copyright (c) 1998-2012 Balázs Scheidler
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */

#include "dnscache.h"
#include "messages.h"
#include "timeutils/cache.h"
#include "tls-support.h"

#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <time.h>

#include <iv.h>
#include <iv_list.h>

typedef struct _DNSCacheEntry DNSCacheEntry;
typedef struct _DNSCacheKey DNSCacheKey;

struct _DNSCacheKey
{
  gint family;
  union
  {
    struct in_addr ip;
#if SYSLOG_NG_ENABLE_IPV6
    struct in6_addr ip6;
#endif
  } addr;
};

struct _DNSCacheEntry
{
  struct iv_list_head list;
  DNSCacheKey key;
  time_t resolved;
  gchar *hostname;
  gsize hostname_len;
  /* whether this entry is a positive (successful DNS lookup) or negative (failed DNS lookup, contains an IP address) match */
  gboolean positive;
};

struct _DNSCache
{
  GHashTable *cache;
  const DNSCacheOptions *options;
  struct iv_list_head cache_list;
  struct iv_list_head persist_list;
  gint persistent_count;
  time_t hosts_mtime;
  time_t hosts_checktime;
};



static gboolean
dns_cache_key_equal(DNSCacheKey *e1, DNSCacheKey *e2)
{
  if (e1->family == e2->family)
    {
      if ((e1->family == AF_INET && memcmp(&e1->addr.ip, &e2->addr.ip, sizeof(e1->addr.ip)) == 0))
        return TRUE;
#if SYSLOG_NG_ENABLE_IPV6
      if ((e1->family == AF_INET6 && memcmp(&e1->addr.ip6, &e2->addr.ip6, sizeof(e1->addr.ip6)) == 0))
        return TRUE;
#endif
    }
  return FALSE;
}

static guint
dns_cache_key_hash(DNSCacheKey *e)
{
  if (e->family == AF_INET)
    {
      return ntohl(e->addr.ip.s_addr);
    }
#if SYSLOG_NG_ENABLE_IPV6
  else if (e->family == AF_INET6)
    {
      guint32 *a32 = (guint32 *) &e->addr.ip6.s6_addr;
      return (0x80000000 | (a32[0] ^ a32[1] ^ a32[2] ^ a32[3]));
    }
#endif
  else
    {
      g_assert_not_reached();
      return 0;
    }
}

static void
dns_cache_entry_free(DNSCacheEntry *e)
{
  iv_list_del(&e->list);

  g_free(e->hostname);
  g_free(e);
}

static inline void
dns_cache_fill_key(DNSCacheKey *key, gint family, void *addr)
{
  key->family = family;
  switch (family)
    {
    case AF_INET:
      key->addr.ip = *(struct in_addr *) addr;
      break;
#if SYSLOG_NG_ENABLE_IPV6
    case AF_INET6:
      key->addr.ip6 = *(struct in6_addr *) addr;
      break;
#endif
    default:
      g_assert_not_reached();
      break;
    }
}

static void
dns_cache_store(DNSCache *self, gboolean persistent, gint family, void *addr, const gchar *hostname, gboolean positive)
{
  DNSCacheEntry *entry;
  guint hash_size;

  iv_validate_now();

  entry = g_new(DNSCacheEntry, 1);

  dns_cache_fill_key(&entry->key, family, addr);
  entry->hostname = g_strdup(hostname);
  entry->hostname_len = strlen(hostname);
  entry->positive = positive;
  INIT_IV_LIST_HEAD(&entry->list);
  if (!persistent)
    {
      entry->resolved = iv_now.tv_sec;
      iv_list_add(&entry->list, &self->cache_list);
    }
  else
    {
      entry->resolved = 0;
      iv_list_add(&entry->list, &self->persist_list);
    }
  hash_size = g_hash_table_size(self->cache);
  g_hash_table_replace(self->cache, &entry->key, entry);

  if (persistent && hash_size != g_hash_table_size(self->cache))
    self->persistent_count++;

  /* persistent elements are not counted */
  if ((gint) (g_hash_table_size(self->cache) - self->persistent_count) > self->options->cache_size)
    {
      DNSCacheEntry *entry_to_remove = iv_list_entry(self->cache_list.next, DNSCacheEntry, list);

      /* remove oldest element */
      g_hash_table_remove(self->cache, &entry_to_remove->key);
    }
}

void
dns_cache_store_persistent(DNSCache *self, gint family, void *addr, const gchar *hostname)
{
  dns_cache_store(self, TRUE, family, addr, hostname, TRUE);
}

void
dns_cache_store_dynamic(DNSCache *self, gint family, void *addr, const gchar *hostname, gboolean positive)
{
  dns_cache_store(self, FALSE, family, addr, hostname, positive);
}

static void
dns_cache_cleanup_persistent_hosts(DNSCache *self)
{
  struct iv_list_head *ilh, *ilh2;

  iv_list_for_each_safe(ilh, ilh2, &self->persist_list)
  {
    DNSCacheEntry *entry = iv_list_entry(ilh, DNSCacheEntry, list);

    g_hash_table_remove(self->cache, &entry->key);
    self->persistent_count--;
  }
}

static void
dns_cache_check_hosts(DNSCache *self, glong t)
{
  struct stat st;

  if (G_LIKELY(self->hosts_checktime == t))
    return;

  self->hosts_checktime = t;

  if (!self->options->hosts || stat(self->options->hosts, &st) < 0)
    {
      dns_cache_cleanup_persistent_hosts(self);
      return;
    }

  if (self->hosts_mtime == -1 || st.st_mtime > self->hosts_mtime)
    {
      FILE *hosts;

      self->hosts_mtime = st.st_mtime;
      dns_cache_cleanup_persistent_hosts(self);
      hosts = fopen(self->options->hosts, "r");
      if (hosts)
        {
          gchar buf[4096];
          char *strtok_saveptr;

          while (fgets(buf, sizeof(buf), hosts))
            {
              gchar *p, *ip;
              gint len;
              gint family;
              union
              {
                struct in_addr ip4;
#if SYSLOG_NG_ENABLE_IPV6
                struct in6_addr ip6;
#endif
              } ia;

              if (buf[0] == 0 || buf[0] == '\n' || buf[0] == '#')
                continue;

              len = strlen(buf);
              if (buf[len - 1] == '\n')
                buf[len-1] = 0;

              p = strtok_r(buf, " \t", &strtok_saveptr);
              if (!p)
                continue;
              ip = p;

#if SYSLOG_NG_ENABLE_IPV6
              if (strchr(ip, ':') != NULL)
                family = AF_INET6;
              else
#endif
                family = AF_INET;

              p = strtok_r(NULL, " \t", &strtok_saveptr);
              if (!p)
                continue;
              inet_pton(family, ip, &ia);
              dns_cache_store_persistent(self, family, &ia, p);
            }
          fclose(hosts);
        }
      else
        {
          msg_error("Error loading dns cache hosts file",
                    evt_tag_str("filename", self->options->hosts),
                    evt_tag_error("error"));
        }

    }
}

/*
 * @hostname        is set to the stored hostname,
 * @positive        is set whether the match was a DNS match or failure
 *
 * Returns TRUE if the cache was able to serve the request (e.g. had a
 * matching entry at all).
 */
gboolean
dns_cache_lookup(DNSCache *self, gint family, void *addr, const gchar **hostname, gsize *hostname_len,
                 gboolean *positive)
{
  DNSCacheKey key;
  DNSCacheEntry *entry;
  time_t now;

  iv_validate_now();
  now = iv_now.tv_sec;
  dns_cache_check_hosts(self, now);

  dns_cache_fill_key(&key, family, addr);
  entry = g_hash_table_lookup(self->cache, &key);
  if (entry)
    {
      if (entry->resolved &&
          ((entry->positive && entry->resolved < now - self->options->expire) ||
           (!entry->positive && entry->resolved < now - self->options->expire_failed)))
        {
          /* the entry is not persistent and is too old */
        }
      else
        {
          *hostname = entry->hostname;
          *hostname_len = entry->hostname_len;
          *positive = entry->positive;
          return TRUE;
        }
    }
  *hostname = NULL;
  *positive = FALSE;
  return FALSE;
}

DNSCache *
dns_cache_new(const DNSCacheOptions *options)
{
  DNSCache *self = g_new0(DNSCache, 1);

  self->cache = g_hash_table_new_full((GHashFunc) dns_cache_key_hash, (GEqualFunc) dns_cache_key_equal, NULL,
                                      (GDestroyNotify) dns_cache_entry_free);
  INIT_IV_LIST_HEAD(&self->cache_list);
  INIT_IV_LIST_HEAD(&self->persist_list);
  self->hosts_mtime = -1;
  self->hosts_checktime = 0;
  self->persistent_count = 0;
  self->options = options;
  return self;
}

void
dns_cache_free(DNSCache *self)
{
  g_hash_table_destroy(self->cache);
  g_free(self);
}

void
dns_cache_options_defaults(DNSCacheOptions *options)
{
  options->cache_size = 1007;
  options->expire = 3600;
  options->expire_failed = 60;
  options->hosts = NULL;
}

void
dns_cache_options_destroy(DNSCacheOptions *options)
{
  g_free(options->hosts);
  options->hosts = NULL;
}

/**************************************************************************
 * The global API that manages DNSCache instances on its own. Callers need
 * not be aware of underlying data structures and locking, they can simply
 * call these functions to lookup/query the DNS cache.
 *
 * I would prefer these to be moved into a higher level implementation
 * detail.
 **************************************************************************/

TLS_BLOCK_START
{
  DNSCache *dns_cache;
}
TLS_BLOCK_END;

#define dns_cache __tls_deref(dns_cache)

/* DNS cache related options are global, independent of the configuration
 * (e.g.  GlobalConfig instance), and they are stored in the
 * "effective_dns_cache_options" variable below.
 *
 * The reasons for using the global variable are as explained below.
 *
 * Some notes:
 *   1) DNS cache contents are better retained between configuration reloads
 *   2) There are multiple DNSCache instances as they are per-thread data structs.
 *
 * The usual pattern would be:
 *    DNSCache->options -> DNSCacheOptions
 *
 * And options would be a reference and would point inside a DNSCacheOptions
 * instance contained within the GlobalConfig.
 *
 * The problem with this approach is that we don't want to recreate DNSCache
 * instances when reloading the configuration (as we want to keep their
 * config), and this would mean that we'd have to update the "options"
 * pointers in each of the existing instances.  And those instances are
 * per-thread variables, making it a bit more difficult to track them.
 *
 * For this reason, it was a lot simpler to use a global variable to hold
 * configuration options, one that can be updated as the configuration is
 * reloaded.  Then DNSCache instances transparently take the options changes
 * into account as they continue to resolve names.
 *
 * I could come up with even better solutions to this problem (like using a
 * layer above GlobalConfig, a state that encapsulates per-execution state
 * of syslog-ng), however, right now that would be an overkill and I want to
 * get the DNSCache refactors into the master tree.
 */

static DNSCacheOptions effective_dns_cache_options;
G_LOCK_DEFINE_STATIC(unused_dns_caches);
static GList *unused_dns_caches;

gboolean
dns_caching_lookup(gint family, void *addr, const gchar **hostname, gsize *hostname_len, gboolean *positive)
{
  return dns_cache_lookup(dns_cache, family, addr, hostname, hostname_len, positive);
}

void
dns_caching_store(gint family, void *addr, const gchar *hostname, gboolean positive)
{
  dns_cache_store_dynamic(dns_cache, family, addr, hostname, positive);
}

void
dns_caching_update_options(const DNSCacheOptions *new_options)
{
  DNSCacheOptions *options = &effective_dns_cache_options;

  if (options->hosts)
    g_free(options->hosts);

  options->cache_size = new_options->cache_size;
  options->expire = new_options->expire;
  options->expire_failed = new_options->expire_failed;
  options->hosts = g_strdup(new_options->hosts);
}

void
dns_caching_thread_init(void)
{
  g_assert(dns_cache == NULL);

  G_LOCK(unused_dns_caches);
  if (unused_dns_caches)
    {
      dns_cache = unused_dns_caches->data;
      unused_dns_caches = g_list_delete_link(unused_dns_caches, unused_dns_caches);
    }
  G_UNLOCK(unused_dns_caches);

  if (!dns_cache)
    dns_cache = dns_cache_new(&effective_dns_cache_options);
}

void
dns_caching_thread_deinit(void)
{
  g_assert(dns_cache != NULL);

  G_LOCK(unused_dns_caches);
  unused_dns_caches = g_list_prepend(unused_dns_caches, dns_cache);
  G_UNLOCK(unused_dns_caches);
  dns_cache = NULL;
}

void
dns_caching_global_init(void)
{
  dns_cache_options_defaults(&effective_dns_cache_options);
}

void
dns_caching_global_deinit(void)
{
  G_LOCK(unused_dns_caches);
  g_list_foreach(unused_dns_caches, (GFunc) dns_cache_free, NULL);
  g_list_free(unused_dns_caches);
  unused_dns_caches = NULL;
  G_UNLOCK(unused_dns_caches);
  dns_cache_options_destroy(&effective_dns_cache_options);
}