summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeoff Garside <geoff@geoffgarside.co.uk>2011-06-17 17:41:28 +0100
committerGeoff Garside <geoff@geoffgarside.co.uk>2011-06-17 17:41:28 +0100
commitb4664b41c71fbcd7cda8644fd5e26ebf479d5df0 (patch)
tree7e2cca783e5cb758738dfcfdd936b0d8c1f3d053
parent5f5b3d9787198bca6724b576ed778acd89c072db (diff)
Add redisSetReuseAddr(c, fd) static function.
Extract setting SO_REUSEADDR socket option into separate function so the same code can be more easily used by redisCreateSocket and other functions.
-rw-r--r--net.c16
1 files changed, 12 insertions, 4 deletions
diff --git a/net.c b/net.c
index 98eee5d..9364851 100644
--- a/net.c
+++ b/net.c
@@ -62,16 +62,24 @@ static void __redisSetErrorFromErrno(redisContext *c, int type, const char *pref
__redisSetError(c,type,buf);
}
+static int redisSetReuseAddr(redisContext *c, int fd) {
+ int on = 1;
+ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
+ __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
+ close(fd);
+ return REDIS_ERR;
+ }
+ return REDIS_OK;
+}
+
static int redisCreateSocket(redisContext *c, int type) {
- int s, on = 1;
+ int s;
if ((s = socket(type, SOCK_STREAM, 0)) == -1) {
__redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
return REDIS_ERR;
}
if (type == AF_INET) {
- if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
- __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
- close(s);
+ if (redisSetReuseAddr(c,s) == REDIS_ERR) {
return REDIS_ERR;
}
}