From 0ccb2c8d890753fcd6524545646f34274d977b9c Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 3 Nov 2010 11:12:26 +0100 Subject: [PATCH] Add functiont to net.c to connect to a unix socket --- hiredis.c | 4 ++-- net.c | 29 ++++++++++++++++++++++++++++- net.h | 3 ++- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/hiredis.c b/hiredis.c index 471483e..e06e837 100644 --- a/hiredis.c +++ b/hiredis.c @@ -655,7 +655,7 @@ redisContext *redisConnect(const char *ip, int port) { redisContext *c = redisContextInit(); c->flags |= REDIS_BLOCK; c->flags |= REDIS_CONNECTED; - redisContextConnect(c,ip,port); + redisContextConnectTcp(c,ip,port); return c; } @@ -663,7 +663,7 @@ redisContext *redisConnectNonBlock(const char *ip, int port) { redisContext *c = redisContextInit(); c->flags &= ~REDIS_BLOCK; c->flags |= REDIS_CONNECTED; - redisContextConnect(c,ip,port); + redisContextConnectTcp(c,ip,port); return c; } diff --git a/net.c b/net.c index 9d49f23..599ba9d 100644 --- a/net.c +++ b/net.c @@ -31,6 +31,7 @@ #include "fmacros.h" #include #include +#include #include #include #include @@ -95,7 +96,7 @@ static int redisSetTcpNoDelay(redisContext *c, int fd) { return REDIS_OK; } -int redisContextConnect(redisContext *c, const char *addr, int port) { +int redisContextConnectTcp(redisContext *c, const char *addr, int port) { int s; int blocking = (c->flags & REDIS_BLOCK); struct sockaddr_in sa; @@ -138,3 +139,29 @@ int redisContextConnect(redisContext *c, const char *addr, int port) { c->fd = s; return REDIS_OK; } + +int redisContextConnectUnix(redisContext *c, const char *path) { + int s; + int blocking = (c->flags & REDIS_BLOCK); + struct sockaddr_un sa; + + if ((s = redisCreateSocket(c,AF_LOCAL)) == REDIS_ERR) + return REDIS_ERR; + if (!blocking && redisSetNonBlock(c,s) != REDIS_OK) + return REDIS_ERR; + + sa.sun_family = AF_LOCAL; + strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1); + if (connect(s, (struct sockaddr*)&sa, sizeof(sa)) == -1) { + if (errno == EINPROGRESS && !blocking) { + /* This is ok. */ + } else { + __redisSetError(c,REDIS_ERR_IO,NULL); + close(s); + return REDIS_ERR; + } + } + + c->fd = s; + return REDIS_OK; +} diff --git a/net.h b/net.h index f16ad63..0e56000 100644 --- a/net.h +++ b/net.h @@ -31,6 +31,7 @@ #ifndef __NET_H #define __NET_H -int redisContextConnect(redisContext *c, const char *addr, int port); +int redisContextConnectTcp(redisContext *c, const char *addr, int port); +int redisContextConnectUnix(redisContext *c, const char *path); #endif