From cffb385bcf81538f4ec98ed3c21fb008866766be Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Wed, 18 May 2011 19:14:16 +0000 Subject: [PATCH] rudimentary redis handling and some die message fixes --- daemon/Makefile | 2 +- daemon/call.h | 2 ++ daemon/main.c | 27 ++++++++++++++++++++++++--- daemon/redis.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ daemon/redis.h | 30 ++++++++++++++++++++++++++++++ 5 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 daemon/redis.c create mode 100644 daemon/redis.h diff --git a/daemon/Makefile b/daemon/Makefile index 18e10845f..6f0d30afa 100644 --- a/daemon/Makefile +++ b/daemon/Makefile @@ -9,7 +9,7 @@ CFLAGS+= -I$(LIBREDISDIR) LDFLAGS= `pkg-config --libs glib-2.0` `pcre-config --libs` LDFLAGS+= -L$(LIBREDISDIR) -lhiredis -SRCS= main.c kernel.c poller.c aux.c control.c streambuf.c call.c control_udp.c +SRCS= main.c kernel.c poller.c aux.c control.c streambuf.c call.c control_udp.c redis.c OBJS= $(SRCS:.c=.o) diff --git a/daemon/call.h b/daemon/call.h index 6c6baef74..b273cda11 100644 --- a/daemon/call.h +++ b/daemon/call.h @@ -18,6 +18,7 @@ struct peer; struct callstream; struct call; struct callmaster; +struct redis; @@ -72,6 +73,7 @@ struct callmaster { struct mediaproxy_stats stats; struct poller *poller; + struct redis *redis; int kernelfd; unsigned int kernelid; u_int32_t ip; diff --git a/daemon/main.c b/daemon/main.c index f4d0a64eb..9f5b465fc 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -14,6 +14,7 @@ #include "log.h" #include "call.h" #include "kernel.h" +#include "redis.h" @@ -38,6 +39,9 @@ static int timeout; static int silent_timeout; static int port_min; static int port_max; +static u_int32_t redis_ip; +static u_int16_t redis_port; +static char *redis_key; @@ -104,6 +108,8 @@ static void options(int *argc, char ***argv) { static char *adv_ips; static char *listenps; static char *listenudps; + static char *redisps; + static GOptionEntry e[] = { { "table", 't', 0, G_OPTION_ARG_INT, &table, "Kernel table to use", "INT" }, { "ip", 'i', 0, G_OPTION_ARG_STRING, &ips, "Local IP address for RTP", "IP" }, @@ -117,6 +123,8 @@ static void options(int *argc, char ***argv) { { "foreground", 'f', 0, G_OPTION_ARG_NONE, &foreground, "Don't fork to background", NULL }, { "port-min", 'm', 0, G_OPTION_ARG_INT, &port_min, "Lowest port to use for RTP", "INT" }, { "port-max", 'M', 0, G_OPTION_ARG_INT, &port_max, "Highest port to use for RTP", "INT" }, + { "redis", 'r', 0, G_OPTION_ARG_STRING, &redisps, "Connect to Redis database", "IP:PORT" }, + { "redis-key", 'R', 0, G_OPTION_ARG_STRING, &redis_key, "Namespace key for Redis", "STRING" }, { NULL, } }; @@ -145,20 +153,27 @@ static void options(int *argc, char ***argv) { if (listenps) { if (parse_ip_port(&listenp, &listenport, listenps)) - die("Invalid IP or port (--listen)"); + die("Invalid IP or port (--listen)\n"); } if (listenudps) { if (parse_ip_port(&udp_listenp, &udp_listenport, listenudps)) - die("Invalid IP or port (--listen-udp)"); + die("Invalid IP or port (--listen-udp)\n"); } if (tos < 0 || tos > 255) - die("Invalid TOS value"); + die("Invalid TOS value\n"); if (timeout <= 0) timeout = 60; if (silent_timeout <= 0) silent_timeout = 3600; + + if (redisps) { + if (parse_ip_port(&redis_ip, &redis_port, redisps) || !redis_ip) + die("Invalid IP or port (--redis)\n"); + if (!redis_key) + die("Must specify Redis namespace key (--redis-key) when using Redis\n"); + } } @@ -236,6 +251,12 @@ int main(int argc, char **argv) { die("Failed to open UDP control connection port\n"); } + if (redis_ip) { + m->redis = redis_new(redis_ip, redis_port, redis_key); + if (!m->redis) + die("Failed to connect to Redis database\n"); + } + mylog(LOG_INFO, "Startup complete"); if (!foreground) diff --git a/daemon/redis.c b/daemon/redis.c new file mode 100644 index 000000000..34d14434d --- /dev/null +++ b/daemon/redis.c @@ -0,0 +1,49 @@ +#include +#include +#include +#include + +#include "redis.h" +#include "aux.h" +#include "log.h" + + + + + +struct redis *redis_new(u_int32_t ip, u_int16_t port, char *key) { + struct redis *r; + struct timeval tv; + redisReply *rp; + + r = malloc(sizeof(*r)); + ZERO(*r); + + sprintf(r->host, IPF, IPP(ip)); + r->port = port; + r->key = key; + + tv.tv_sec = 1; + tv.tv_usec = 0; + r->ctx = redisConnectWithTimeout(r->host, r->port, tv); + + if (!r->ctx) + goto err; + if (r->ctx->err) + goto err; + + rp = redisCommand(r->ctx, "PING"); + if (!rp) + goto err; + freeReplyObject(rp); + + return r; + +err: + if (r->ctx && r->ctx->errstr) + mylog(LOG_CRIT, "Redis error: %s\n", r->ctx->errstr); + if (r->ctx) + redisFree(r->ctx); + free(r); + return NULL; +} diff --git a/daemon/redis.h b/daemon/redis.h new file mode 100644 index 000000000..887773046 --- /dev/null +++ b/daemon/redis.h @@ -0,0 +1,30 @@ +#ifndef __REDIS_H__ +#define __REDIS_H__ + + + + +#include +#include + + + + +struct redis { + char host[32]; + int port; + + redisContext *ctx; + char *key; + int active:1; +}; + + + + +struct redis *redis_new(u_int32_t, u_int16_t, char *); + + + + +#endif