~ubuntu-branches/ubuntu/trusty/syslog-ng/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/dnscache.c

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS), Gergely Nagy
  • Date: 2011-10-11 14:30:48 UTC
  • mfrom: (1.3.7)
  • Revision ID: package-import@ubuntu.com-20111011143048-r1iljux9xbvj3lwh
Tags: 3.3.1.dfsg-1
* New upstream release with important fixes from upstream git tree with
  non-free manpages removed.
* Drop syslog-ng.conf(5) (closes: #496521).
* syslog-ng(8) is generated, and does not mention -Q anymore
  (closes: #616069).
* Supports CAP_SYSLOG on recent kernels (closes: #630172).
* Does not use g_timeout_add_seconds anymore (closes: #609154).

[ Gergely Nagy <algernon@madhouse-project.org> ]
* Update debian/copyright to DEP-5 format.
* Simplified the logrotate file by merging identical entries.
* Include local configuration files from /etc/syslog-ng/conf.d/ (Closes:
  #609050).
* Update syslog-ng.conf to be fully 3.3 compliant.
* Compress both source and binaries (except the syslog-ng meta
  package) with xz, instead of gzip.
* Use dpkg triggers to restart syslog-ng when appropriate.
* Include DFSG-free manual pages for all binaries.
* Build with Hardening enabled.
* Mention syslog(3) in /etc/default/syslog-ng, instead of
  <linux/kernel.h> (Closes: #608605)
* Support 'status' in the init script.
  Patch from Peter Eisentraut <petere@debian.org> (Closes: #644458)
* Build-Depend on libevtlog-dev (>= 0.2.12-5~) for correct shlibs.
* Use [linux-any] in Build-Depends instead of hardcoded links.
  (Closes: #634715)
* Use $SYSLOGNG_OPTS in the init script when reloading syslog-ng.
  (Closes: #589081)

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
#include "dnscache.h"
26
26
#include "messages.h"
 
27
#include "timeutils.h"
 
28
#include "tls-support.h"
27
29
 
28
30
#include <sys/types.h>
29
31
#include <netinet/in.h>
56
58
  DNSCacheKey key;
57
59
  time_t resolved;
58
60
  gchar *hostname;
 
61
  /* whether this entry is a positive (successful DNS lookup) or negative (failed DNS lookup, contains an IP address) match */
 
62
  gboolean positive;
59
63
};
60
64
 
61
65
 
62
 
static GHashTable *cache;
63
 
static DNSCacheEntry cache_first, cache_last, persist_first, persist_last;
 
66
TLS_BLOCK_START
 
67
{
 
68
  GHashTable *cache;
 
69
  DNSCacheEntry cache_first;
 
70
  DNSCacheEntry cache_last;
 
71
  DNSCacheEntry persist_first;
 
72
  DNSCacheEntry persist_last;
 
73
}
 
74
TLS_BLOCK_END;
 
75
 
 
76
#define cache  __tls_deref(cache)
 
77
#define cache_first __tls_deref(cache_first)
 
78
#define cache_last __tls_deref(cache_last)
 
79
#define persist_first __tls_deref(persist_first)
 
80
#define persist_last __tls_deref(persist_last)
 
81
 
64
82
static gint dns_cache_size = 1007;
65
83
static gint dns_cache_expire = 3600;
66
84
static gint dns_cache_expire_failed = 60;
155
173
}
156
174
 
157
175
static void
158
 
dns_cache_check_hosts(void)
 
176
dns_cache_check_hosts(glong t)
159
177
{
160
178
  struct stat st;
161
 
  time_t t = time(NULL);
162
179
 
163
180
  if (G_LIKELY(dns_cache_hosts_checktime == t))
164
181
    return;
218
235
              if (!p)
219
236
                continue;
220
237
              inet_pton(family, ip, &ia);
221
 
              dns_cache_store(TRUE, family, &ia, p);
 
238
              dns_cache_store(TRUE, family, &ia, p, TRUE);
222
239
            }
223
240
          fclose(hosts);
224
241
        }
233
250
    }
234
251
}
235
252
 
 
253
/*
 
254
 * @hostname        is set to the stored hostname,
 
255
 * @positive        is set whether the match was a DNS match or failure
 
256
 *
 
257
 * Returns TRUE if the cache was able to serve the request (e.g. had a
 
258
 * matching entry at all).
 
259
 */
236
260
gboolean
237
 
dns_cache_lookup(gint family, void *addr, const gchar **hostname)
 
261
dns_cache_lookup(gint family, void *addr, const gchar **hostname, gboolean *positive)
238
262
{
239
263
  DNSCacheKey key;
240
264
  DNSCacheEntry *entry;
241
265
  time_t now;
242
266
  
243
 
  dns_cache_check_hosts();
 
267
  now = cached_g_current_time_sec();
 
268
  dns_cache_check_hosts(now);
244
269
  
245
270
  dns_cache_fill_key(&key, family, addr);
246
271
  entry = g_hash_table_lookup(cache, &key);
247
272
  if (entry)
248
273
    {
249
 
      now = time(NULL);
250
274
      if (entry->resolved && 
251
 
          ((entry->hostname && entry->resolved < now - dns_cache_expire) ||
252
 
           (!entry->hostname && entry->resolved < now - dns_cache_expire_failed)))
 
275
          ((entry->positive && entry->resolved < now - dns_cache_expire) ||
 
276
           (!entry->positive && entry->resolved < now - dns_cache_expire_failed)))
253
277
        {
254
278
          /* the entry is not persistent and is too old */
255
279
        }
256
280
      else
257
281
        {
258
282
          *hostname = entry->hostname;
 
283
          *positive = entry->positive;
259
284
          return TRUE;
260
285
        }
261
286
    }
 
287
  *hostname = NULL;
 
288
  *positive = FALSE;
262
289
  return FALSE;
263
290
}
264
291
 
265
292
void
266
 
dns_cache_store(gboolean persistent, gint family, void *addr, const gchar *hostname)
 
293
dns_cache_store(gboolean persistent, gint family, void *addr, const gchar *hostname, gboolean positive)
267
294
{
268
295
  DNSCacheEntry *entry;
269
296
  guint hash_size;
272
299
 
273
300
  dns_cache_fill_key(&entry->key, family, addr);
274
301
  entry->hostname = hostname ? g_strdup(hostname) : NULL;
 
302
  entry->positive = positive;
275
303
  if (!persistent)
276
304
    {
277
 
      entry->resolved = time(NULL);
 
305
      entry->resolved = cached_g_current_time_sec();
278
306
      dns_cache_entry_insert_before(&cache_last, entry);
279
307
    }
280
308
  else