diff --git a/smmapdfw/config.h.in b/smmapdfw/config.h.in index b5e36b0..c982396 100644 --- a/smmapdfw/config.h.in +++ b/smmapdfw/config.h.in @@ -1,14 +1,14 @@ /* config.h.in. Generated from configure.ac by autoheader. */ +/* enable cache */ +#undef ENABLE_CACHE + /* enable Net-SNMP */ #undef ENABLE_NETSNMP /* enable stats */ #undef ENABLE_STATS -/* enable verify cache */ -#undef ENABLE_VERIFY_CACHE - /* Define to 1 if you have the header file. */ #undef HAVE_ARPA_INET_H diff --git a/smmapdfw/configure b/smmapdfw/configure index aa59144..55ca3ad 100755 --- a/smmapdfw/configure +++ b/smmapdfw/configure @@ -1,6 +1,6 @@ #! /bin/sh # Guess values for system-dependent variables and create Makefiles. -# Generated by GNU Autoconf 2.59 for smmapdfw 0.94. +# Generated by GNU Autoconf 2.59 for smmapdfw 0.96. # # Report bugs to . # @@ -423,8 +423,8 @@ SHELL=${CONFIG_SHELL-/bin/sh} # Identity of this package. PACKAGE_NAME='smmapdfw' PACKAGE_TARNAME='smmapdfw' -PACKAGE_VERSION='0.94' -PACKAGE_STRING='smmapdfw 0.94' +PACKAGE_VERSION='0.96' +PACKAGE_STRING='smmapdfw 0.96' PACKAGE_BUGREPORT='woho@hottis.de' # Factoring default headers for most tests. @@ -953,7 +953,7 @@ if test "$ac_init_help" = "long"; then # Omit some internal or obsolete options to make the list less imposing. # This message is too long to be a string in the A/UX 3.1 sh. cat <<_ACEOF -\`configure' configures smmapdfw 0.94 to adapt to many kinds of systems. +\`configure' configures smmapdfw 0.96 to adapt to many kinds of systems. Usage: $0 [OPTION]... [VAR=VALUE]... @@ -1019,7 +1019,7 @@ fi if test -n "$ac_init_help"; then case $ac_init_help in - short | recursive ) echo "Configuration of smmapdfw 0.94:";; + short | recursive ) echo "Configuration of smmapdfw 0.96:";; esac cat <<\_ACEOF @@ -1168,7 +1168,7 @@ fi test -n "$ac_init_help" && exit 0 if $ac_init_version; then cat <<\_ACEOF -smmapdfw configure 0.94 +smmapdfw configure 0.96 generated by GNU Autoconf 2.59 Copyright (C) 2003 Free Software Foundation, Inc. @@ -1182,7 +1182,7 @@ cat >&5 <<_ACEOF This file contains any messages produced by compilers while running configure, to aid debugging if configure makes a mistake. -It was created by smmapdfw $as_me 0.94, which was +It was created by smmapdfw $as_me 0.96, which was generated by GNU Autoconf 2.59. Invocation command line was $ $0 $@ @@ -1713,7 +1713,7 @@ fi PACKAGE=smmapdfw -VERSION=0.94 +VERSION=0.96 if test "`cd $srcdir && pwd`" != "`pwd`" && test -f $srcdir/config.status; then { { echo "$as_me:$LINENO: error: source directory already configured; run \"make distclean\" there first" >&5 @@ -1852,7 +1852,7 @@ if test "${with_bdb_lib_dir+set}" = set; then withval="$with_bdb_lib_dir" case "$withval" in no) - if test "x$ENABLE_VERIFY_CACHE" = "x1"; then + if test "x$ENABLE_CACHE" = "x1"; then { { echo "$as_me:$LINENO: error: Need bdb-lib-dir" >&5 echo "$as_me: error: Need bdb-lib-dir" >&2;} { (exit 1); exit 1; }; } @@ -1873,7 +1873,7 @@ if test "${with_bdb_inc_dir+set}" = set; then withval="$with_bdb_inc_dir" case "$withval" in no) - if test "x$ENABLE_VERIFY_CACHE" = "x1"; then + if test "x$ENABLE_CACHE" = "x1"; then { { echo "$as_me:$LINENO: error: Need bdb-inc-dir" >&5 echo "$as_me: error: Need bdb-inc-dir" >&2;} { (exit 1); exit 1; }; } @@ -1992,7 +1992,7 @@ if test "${with_netsnmp_bin_dir+set}" = set; then fi; -ENABLE_VERIFY_CACHE=1 +ENABLE_CACHE=1 @@ -21143,7 +21143,7 @@ done -if test "x$ENABLE_VERIFY_CACHE" = "x1"; then +if test "x$ENABLE_CACHE" = "x1"; then echo "$as_me:$LINENO: checking for __db_ndbm_open in -ldb" >&5 echo $ECHO_N "checking for __db_ndbm_open in -ldb... $ECHO_C" >&6 if test "${ac_cv_lib_db___db_ndbm_open+set}" = set; then @@ -21427,12 +21427,12 @@ fi if test "x$BDB_NOT_FOUND" = "x1"; then echo "*** no db/dbm found, disabling verify cache" - ENABLE_VERIFY_CACHE=0 + ENABLE_CACHE=0 fi cat >>confdefs.h <<_ACEOF -#define ENABLE_VERIFY_CACHE ${ENABLE_VERIFY_CACHE} +#define ENABLE_CACHE ${ENABLE_CACHE} _ACEOF @@ -21816,7 +21816,7 @@ _ASBOX } >&5 cat >&5 <<_CSEOF -This file was extended by smmapdfw $as_me 0.94, which was +This file was extended by smmapdfw $as_me 0.96, which was generated by GNU Autoconf 2.59. Invocation command line was CONFIG_FILES = $CONFIG_FILES @@ -21879,7 +21879,7 @@ _ACEOF cat >>$CONFIG_STATUS <<_ACEOF ac_cs_version="\\ -smmapdfw config.status 0.94 +smmapdfw config.status 0.96 configured by $0, generated by GNU Autoconf 2.59, with options \\"`echo "$ac_configure_args" | sed 's/[\\""\`\$]/\\\\&/g'`\\" diff --git a/smmapdfw/configure.ac b/smmapdfw/configure.ac index 2c651a4..31eb66f 100644 --- a/smmapdfw/configure.ac +++ b/smmapdfw/configure.ac @@ -2,8 +2,8 @@ # Process this file with autoconf to produce a configure script. -AC_INIT(smmapdfw, 0.95, woho@hottis.de) -AM_INIT_AUTOMAKE(smmapdfw, 0.95) +AC_INIT(smmapdfw, 0.96, woho@hottis.de) +AM_INIT_AUTOMAKE(smmapdfw, 0.96) AM_MAINTAINER_MODE AM_CONFIG_HEADER(config.h) @@ -15,7 +15,7 @@ AC_ARG_WITH(bdb-lib-dir, Directory for Berkeley DB library files])], [ case "$withval" in no) - if test "x$ENABLE_VERIFY_CACHE" = "x1"; then + if test "x$ENABLE_CACHE" = "x1"; then AC_MSG_ERROR(Need bdb-lib-dir) fi ;; @@ -32,7 +32,7 @@ AC_ARG_WITH(bdb-inc-dir, Directory for Berkeley include files])], [ case "$withval" in no) - if test "x$ENABLE_VERIFY_CACHE" = "x1"; then + if test "x$ENABLE_CACHE" = "x1"; then AC_MSG_ERROR(Need bdb-inc-dir) fi ;; @@ -136,7 +136,7 @@ AC_ARG_WITH(netsnmp-bin-dir, ) -ENABLE_VERIFY_CACHE=1 +ENABLE_CACHE=1 dnl AC_ARG_ENABLE(verify-cache, dnl [AC_HELP_STRING(--enable-verify-cache, [ dnl Enables the cache in the verify worker. @@ -242,7 +242,7 @@ AC_CHECK_FUNCS([inet_ntoa memset select socket strchr strdup strerror strrchr st -if test "x$ENABLE_VERIFY_CACHE" = "x1"; then +if test "x$ENABLE_CACHE" = "x1"; then AC_CHECK_LIB([db], [__db_ndbm_open], [ BDB_LIBS="-ldb" ], AC_CHECK_LIB([db], [__db_ndbm_open_4002], @@ -262,10 +262,10 @@ AC_SUBST(BDB_LIBS) if test "x$BDB_NOT_FOUND" = "x1"; then echo "*** no db/dbm found, disabling verify cache" - ENABLE_VERIFY_CACHE=0 + ENABLE_CACHE=0 fi -AC_DEFINE_UNQUOTED(ENABLE_VERIFY_CACHE, ${ENABLE_VERIFY_CACHE}, [enable verify cache]) +AC_DEFINE_UNQUOTED(ENABLE_CACHE, ${ENABLE_CACHE}, [enable cache]) AC_DEFINE_UNQUOTED(ENABLE_STATS, ${ENABLE_STATS}, [enable stats]) AC_DEFINE_UNQUOTED(ENABLE_NETSNMP, ${ENABLE_NETSNMP}, [enable Net-SNMP]) diff --git a/smmapdfw/libsmmapdfw/Makefile.am b/smmapdfw/libsmmapdfw/Makefile.am index 09e5e19..9481760 100644 --- a/smmapdfw/libsmmapdfw/Makefile.am +++ b/smmapdfw/libsmmapdfw/Makefile.am @@ -1,5 +1,5 @@ -include_HEADERS=containers_public.h queue.h smtp.h cfg.h count.h safe_write.h sunos_comp.h htdns.h smmapd.h htmalloc.h stats.h +include_HEADERS=containers_public.h queue.h smtp.h cfg.h count.h safe_write.h sunos_comp.h htdns.h smmapd.h htmalloc.h stats.h htcache.h lib_LTLIBRARIES = libsmmapdfw.la -libsmmapdfw_la_SOURCES = cfg.c queue.c count.c safe_write.c config_public.c htdns.c smtp.c htmalloc.c stats.c - +libsmmapdfw_la_SOURCES = cfg.c queue.c count.c safe_write.c config_public.c htdns.c smtp.c htmalloc.c stats.c htcache.c +libsmmapdfw_la_LIBADD = @BDB_LIBS@ diff --git a/smmapdfw/libsmmapdfw/Makefile.in b/smmapdfw/libsmmapdfw/Makefile.in index afa3ee2..8e175bd 100644 --- a/smmapdfw/libsmmapdfw/Makefile.in +++ b/smmapdfw/libsmmapdfw/Makefile.in @@ -87,9 +87,10 @@ RC = @RC@ STRIP = @STRIP@ VERSION = @VERSION@ -include_HEADERS = containers_public.h queue.h smtp.h cfg.h count.h safe_write.h sunos_comp.h htdns.h smmapd.h htmalloc.h stats.h +include_HEADERS = containers_public.h queue.h smtp.h cfg.h count.h safe_write.h sunos_comp.h htdns.h smmapd.h htmalloc.h stats.h htcache.h lib_LTLIBRARIES = libsmmapdfw.la -libsmmapdfw_la_SOURCES = cfg.c queue.c count.c safe_write.c config_public.c htdns.c smtp.c htmalloc.c stats.c +libsmmapdfw_la_SOURCES = cfg.c queue.c count.c safe_write.c config_public.c htdns.c smtp.c htmalloc.c stats.c htcache.c +libsmmapdfw_la_LIBADD = @BDB_LIBS@ mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs CONFIG_HEADER = ../config.h CONFIG_CLEAN_FILES = @@ -101,9 +102,9 @@ CPPFLAGS = @CPPFLAGS@ LDFLAGS = @LDFLAGS@ LIBS = @LIBS@ libsmmapdfw_la_LDFLAGS = -libsmmapdfw_la_LIBADD = +libsmmapdfw_la_DEPENDENCIES = libsmmapdfw_la_OBJECTS = cfg.lo queue.lo count.lo safe_write.lo \ -config_public.lo htdns.lo smtp.lo htmalloc.lo stats.lo +config_public.lo htdns.lo smtp.lo htmalloc.lo stats.lo htcache.lo CFLAGS = @CFLAGS@ COMPILE = $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) LTCOMPILE = $(LIBTOOL) --mode=compile $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) @@ -119,8 +120,8 @@ DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST) TAR = tar GZIP_ENV = --best DEP_FILES = .deps/cfg.P .deps/config_public.P .deps/count.P \ -.deps/htdns.P .deps/htmalloc.P .deps/queue.P .deps/safe_write.P \ -.deps/smtp.P .deps/stats.P +.deps/htcache.P .deps/htdns.P .deps/htmalloc.P .deps/queue.P \ +.deps/safe_write.P .deps/smtp.P .deps/stats.P SOURCES = $(libsmmapdfw_la_SOURCES) OBJECTS = $(libsmmapdfw_la_OBJECTS) diff --git a/smmapdfw/libsmmapdfw/htcache.c b/smmapdfw/libsmmapdfw/htcache.c new file mode 100644 index 0000000..d6f3356 --- /dev/null +++ b/smmapdfw/libsmmapdfw/htcache.c @@ -0,0 +1,174 @@ +/* + Copyright (C) 2004, Wolfgang Hottgenroth + + This file is part of smmapdfw. + + smmapdfw is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + smmapdfw 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 General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with smmapdfw. If not, write to the Free Software Foundation, 59 + Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + + +#if HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#if ENABLE_CACHE == 1 +#define DB_DBM_HSEARCH 1 +#include +#endif + +#include "htcache.h" +#include "htmalloc.h" + +struct mydata_s { + time_t timestamp; + int value_size; + char value[1]; +}; + +typedef struct mydata_s mydata_t; + + + +htcache_t *cache_init(const char *file, int expiry) { +#if ENABLE_CACHE == 1 + htcache_t *handle = (htcache_t*) htmalloc(sizeof(htcache_t)); + handle->mutex = (pthread_mutex_t*) htmalloc(sizeof(pthread_mutex_t)); + pthread_mutex_init(handle->mutex, NULL); + pthread_mutex_unlock(handle->mutex); + handle->file = file; + handle->expiry = expiry; + + return handle; +#else + return NULL; +#endif +} + +void cache_destroy(htcache_t *handle) { +#if ENABLE_CACHE == 1 + pthread_mutex_destroy(handle->mutex); + free(handle->mutex); + free(handle); +#endif +} + +void cache_insert(htcache_t *handle, const char *key, const void *value, int value_size) { +#if ENABLE_CACHE == 1 + DBM *cache; + datum data, dkey; + int ret; + mydata_t *mydata; + + syslog(LOG_DEBUG, "cache_insert: inserting %s -> %s (size %d)", key, (char*) value, value_size); + dkey.dsize = strlen(key) + 1; /* one more for the terminating \0 */ + dkey.dptr = (char*) key; + data.dsize = (sizeof(mydata_t) + value_size + 1); + mydata = (mydata_t *) htmalloc(data.dsize); + mydata->timestamp = time(NULL); + mydata->value_size = value_size; + memcpy(mydata->value, value, value_size); + data.dptr = (char*) mydata; + + pthread_mutex_lock(handle->mutex); + if (NULL != (cache = dbm_open(handle->file, O_RDWR | O_CREAT, 0644))) { + ret = dbm_store(cache, dkey, data, DBM_INSERT); + if (ret != 0) { + syslog(LOG_DEBUG, "cache_insert: couldn't insert record"); + } else { + syslog(LOG_DEBUG, "cache_insert: record inserted"); + } + dbm_close(cache); + } + pthread_mutex_unlock(handle->mutex); + + free(mydata); +#else + syslog(LOG_DEBUG, "cache_insert: cache disabled"); +#endif /* ENABLE_CACHE */ +} + + +void *cache_lookup(htcache_t *handle, const char* key) { +#if ENABLE_CACHE == 1 + DBM *cache; + datum data, dkey; + char *output; + mydata_t *mydata; + int value_size, ret, res = -1; + + syslog(LOG_DEBUG, "cache_lookup: looking up %s, expiry %d", + key, handle->expiry); + + if (NULL != (cache = dbm_open(handle->file, O_RDONLY, 0644))) { + dkey.dsize = strlen(key) + 1; /* one more for the terminating \0 */ + dkey.dptr = (char*) key; + data = dbm_fetch(cache, dkey); + if (NULL == data.dptr) { + syslog(LOG_DEBUG, "cache_lookup: nothing found"); + dbm_close(cache); + } else { + mydata = (mydata_t *) data.dptr; + syslog(LOG_DEBUG, "cache_lookup: found: %s -> %s (size %d)", + key, data.dptr, data.dsize); + if ((mydata->timestamp + handle->expiry) > time(NULL)) { + syslog(LOG_DEBUG, "cache_lookup: not yet expired"); + value_size = mydata->value_size; + output = (char*) htmalloc(value_size+5); + memcpy(output, mydata->value, value_size); + + /* Berkeley DB frees on its own! */ + /* free(data.dptr); */ + dbm_close(cache); + res = 0; + } else { + syslog(LOG_DEBUG, "cache_lookup: expired, will be deleted from cache"); + /* free(data.dptr); --- Berkeley DB frees on its own */ + dbm_close(cache); + + pthread_mutex_lock(handle->mutex); + if (NULL != (cache = dbm_open(handle->file, O_RDWR, 0644))) { + ret = dbm_delete(cache, dkey); + if (ret != 0) { + syslog(LOG_DEBUG, "cache_insert: couldn't delete record"); + } else { + syslog(LOG_DEBUG, "cache_insert: record deleted"); + } + dbm_close(cache); + } + pthread_mutex_unlock(handle->mutex); + } + } + } + + return res; +#else + return -1; +#endif /* ENABLE_CACHE */ +} + + + + diff --git a/smmapdfw/libsmmapdfw/htcache.h b/smmapdfw/libsmmapdfw/htcache.h new file mode 100644 index 0000000..7980c57 --- /dev/null +++ b/smmapdfw/libsmmapdfw/htcache.h @@ -0,0 +1,43 @@ +/* + Copyright (C) 2004, Wolfgang Hottgenroth + + This file is part of smmapdfw. + + smmapdfw is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + smmapdfw 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 General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with smmapdfw. If not, write to the Free Software Foundation, 59 + Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + + + +#ifndef _HTCACHE_H_ +#define _HTCACHE_H_ + +#include + + +typedef struct htcache_s { + const char *file; + int expiry; + pthread_mutex_t *mutex; +} htcache_t; + +htcache_t *cache_init(const char *file, int expiry); +void cache_destroy(htcache_t *handle); +void cache_insert(htcache_t *handle, const char *key, const void *value, int value_size); +void *cache_lookup(htcache_t *handle, const char *key); + + +#endif /* _HTCACHE_H_ */ + + diff --git a/smmapdfw/verify_worker/verify_worker.c b/smmapdfw/verify_worker/verify_worker.c index 40996fb..dcdf14f 100644 --- a/smmapdfw/verify_worker/verify_worker.c +++ b/smmapdfw/verify_worker/verify_worker.c @@ -35,7 +35,7 @@ #include #include -#if ENABLE_VERIFY_CACHE == 1 +#if ENABLE_CACHE == 1 #define DB_DBM_HSEARCH 1 #include #endif @@ -287,7 +287,7 @@ int verify_work_destroy(void *handle, void *work_handle) { void cache_insert(verify_container_handle_t *vch, const char *address, int result, const char *output) { -#if ENABLE_VERIFY_CACHE == 1 +#if ENABLE_CACHE == 1 DBM *cache; datum data, key; int ret; @@ -321,11 +321,11 @@ void cache_insert(verify_container_handle_t *vch, const char *address, int resul } #else syslog(LOG_DEBUG, "cache_insert: cache disabled"); -#endif /* ENABLE_VERIFY_CACHE */ +#endif /* ENABLE_CACHE */ } int cache_lookup(verify_container_handle_t *vch, const char* address, int *result, char **output) { -#if ENABLE_VERIFY_CACHE == 1 +#if ENABLE_CACHE == 1 DBM *cache; datum data, key; mydata_t *mydata; @@ -381,7 +381,7 @@ int cache_lookup(verify_container_handle_t *vch, const char* address, int *resul return res; #else return -1; -#endif /* ENABLE_VERIFY_CACHE */ +#endif /* ENABLE_CACHE */ }