Skip to content

Redis v1 #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,48 @@ AS_IF([test "x$enable_pfring" = "xyes"], [
fi
])

# Checks for libhiredis
AC_ARG_ENABLE(hiredis,
AS_HELP_STRING([--enable-hiredis], [Enable Redis support]))

AC_ARG_WITH(libhiredis_includes,
[ --with-libhiredis-includes=DIR libhiredis include directory],
[with_libhiredis_includes="$withval"],[with_libhiredis_includes="no"])

AC_ARG_WITH(libhiredis_libraries,
[ --with-libhiredis-libraries=DIR libhiredis library directory],
[with_libhiredis_libraries="$withval"],[with_libhiredis_libraries="no"])

AS_IF([test "x$enable_hiredis" = "xyes"], [
AC_DEFINE([HAVE_LIBHIREDIS],[1],[Redis support enabled])

if test "$with_libhiredis_includes" != "no"; then
CPPFLAGS="${CPPFLAGS} -I${with_libhiredis_includes}"
fi

if test "$libhiredis_libraries" != "no"; then
LDFLAGS="${LDFLAGS} -L${with_libhiredis_libraries}"
fi

LDFLAGS="${LDFLAGS} -lhiredis"

# Check for hiredis headers
AC_CHECK_HEADER("hiredis/hiredis.h",,HIREDIS_H="no")

if test "$HIREDIS_H" = "no"; then
echo -e "\n ERROR! hiredis headers not found\n"
exit 1
fi

# Check for hiredis library
AC_CHECK_LIB(hiredis,redisConnect,,HIREDIS_L="no")

if test "$HIREDIS_L" = "no"; then
echo -e "\n ERROR! hiredis library not found\n"
exit 1
fi
])

# Checks for libjansson.
AC_ARG_ENABLE(json,
AS_HELP_STRING([--enable-json], [Enable JSON support]))
Expand Down Expand Up @@ -244,4 +286,3 @@ AC_FUNC_STRNLEN
AC_CHECK_FUNCS([alarm dup2 endgrent endpwent ftruncate getcwd memset strdup strerror strtol strtoul])

AC_OUTPUT(Makefile src/Makefile)

103 changes: 90 additions & 13 deletions src/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
#include <jansson.h>
#endif /* HAVE_JSON */

#ifdef HAVE_LIBHIREDIS
#include <hiredis/hiredis.h>
#endif /* HAVE_LIBHIREDIS */

globalconfig config;

/* The 12th Carol number and 7th Carol prime, 16769023, is also a Carol emirp */
Expand Down Expand Up @@ -372,7 +376,7 @@ int cache_dns_objects(packetinfo *pi, ldns_rdf *rdf_data,
to_offset = 5;
}
break;

case LDNS_RR_TYPE_NSEC:
if (config.dnsf & DNS_CHK_DNSSEC) {
offset = 0;
Expand Down Expand Up @@ -1086,19 +1090,94 @@ void print_passet(pdns_record *l, pdns_asset *p, ldns_rr *rr,
}
#endif /* HAVE_JSON */

/* Print to log file */
if (fd) {
fprintf(fd, "%s\n", output);
fflush(fd);
#ifdef HAVE_LIBHIREDIS
int sendRedisCommand(char *output) {
redisReply *reply = redisCommand(
config.redis_context,
"PUBLISH %s %s",
config.redis_key,
output
);

int error = 0;

if (reply == NULL) {
error = 1;
dlog("[D] Reply from redis was NULL.\n");
} else {
switch (reply->type) {
case REDIS_REPLY_ERROR:
error = 1;
olog("[D] Recived Redis error: %s\n", reply->str);
break;
case REDIS_REPLY_INTEGER:
dlog("[D] Recived Redis reply: %lld\n", reply->integer);
break;
default:
error = 1;
olog("[D] Recived default clause Redis reply type: %d\n", reply->type);
break;
}
freeReplyObject(reply);
}
return error;
}
#endif /* HAVE_LIBHIREDIS */

#ifdef HAVE_LIBHIREDIS
if (config.use_redis == 1) {
int send_error = sendRedisCommand(output);
if (send_error == 1) {
dlog("[D] Trying to reconnect to Redis.\n");

int retry_error = 0;
int connect_retval = connectRedis();
if (connect_retval == 1) {
retry_error = sendRedisCommand(output);
if (retry_error == 0) {
dlog("[D] Reconnect to redis was successfull, data sent.\n");
}
}

/* Print to syslog */
if ((is_err_record && config.output_syslog_nxd) ||
(!is_err_record && config.output_syslog)) {
openlog(PDNS_IDENT, LOG_NDELAY, LOG_LOCAL7);
syslog(LOG_INFO, "%s", output);
closelog();
if (connect_retval == 0 || retry_error == 1) {
dlog("[D] Reconnect to redis was not successfull.\n");
dlog("[D] Closing connection to Redis (%s:%d).\n",
config.redis_server, config.redis_port);
redisFree(config.redis_context);

#ifdef HAVE_JSON
if ((is_err_record && config.use_json_nxd) ||
(!is_err_record && config.use_json)) {
/* json_dumps allocate memory that has to be freed */
free(output);
}
#endif /* HAVE_JSON */

free(proto);
free(rr_class);
free(rr_type);
free(rr_rcode);
exit(1);
}
}
} else {
#endif /* HAVE_LIBHIREDIS */
/* Print to log file */
if (fd) {
fprintf(fd, "%s\n", output);
fflush(fd);
}

/* Print to syslog */
if ((is_err_record && config.output_syslog_nxd) ||
(!is_err_record && config.output_syslog)) {
openlog(PDNS_IDENT, LOG_NDELAY, LOG_LOCAL7);
syslog(LOG_INFO, "%s", output);
closelog();
}
#ifdef HAVE_LIBHIREDIS
}
#endif /* HAVE_LIBHIREDIS */

if (is_err_record) {
l->last_print = l->last_seen;
Expand All @@ -1121,7 +1200,6 @@ void print_passet(pdns_record *l, pdns_asset *p, ldns_rr *rr,
free(rr_class);
free(rr_type);
free(rr_rcode);

}

pdns_record *get_pdns_record(uint64_t dnshash, packetinfo *pi,
Expand Down Expand Up @@ -1753,4 +1831,3 @@ uint16_t pdns_chk_dnsfe(uint16_t rcode)

return retcode;
}

1 change: 0 additions & 1 deletion src/dns.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,3 @@ void update_dns_stats(packetinfo *pi, uint8_t code);
uint16_t pdns_chk_dnsfe(uint16_t rcode);

#endif /* DNS_H */

Loading