summaryrefslogtreecommitdiff
path: root/src/anet.c
diff options
context:
space:
mode:
authorGeoff Garside <geoff@geoffgarside.co.uk>2011-06-17 00:55:00 +0100
committerantirez <antirez@gmail.com>2013-07-08 15:49:22 +0200
commit580b7dce9b1d7adac1d0bfe33e5023f8fbf91158 (patch)
tree691f3c893dff355b0541c2dd5f14d8c44381afc9 /src/anet.c
parent071963c8555451116abf288382ecd1057393a6ae (diff)
downloadredis-580b7dce9b1d7adac1d0bfe33e5023f8fbf91158.tar.gz
Add anetSetReuseAddr(err, fd) static function.
Extract setting SO_REUSEADDR socket option into separate function so the same code can be more easily used by anetCreateSocket and other functions.
Diffstat (limited to 'src/anet.c')
-rw-r--r--src/anet.c17
1 files changed, 14 insertions, 3 deletions
diff --git a/src/anet.c b/src/anet.c
index afa1d6478..78c24bf25 100644
--- a/src/anet.c
+++ b/src/anet.c
@@ -186,8 +186,19 @@ int anetResolve(char *err, char *host, char *ipbuf, size_t ipbuf_len)
return ANET_OK;
}
+static int anetSetReuseAddr(char *err, int fd) {
+ int yes = 1;
+ /* Make sure connection-intensive things like the redis benckmark
+ * will be able to close/open sockets a zillion of times */
+ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) {
+ anetSetError(err, "setsockopt SO_REUSEADDR: %s", strerror(errno));
+ return ANET_ERR;
+ }
+ return ANET_OK;
+}
+
static int anetCreateSocket(char *err, int domain) {
- int s, on = 1;
+ int s;
if ((s = socket(domain, SOCK_STREAM, 0)) == -1) {
anetSetError(err, "creating socket: %s", strerror(errno));
return ANET_ERR;
@@ -195,8 +206,8 @@ static int anetCreateSocket(char *err, int domain) {
/* Make sure connection-intensive things like the redis benchmark
* will be able to close/open sockets a zillion of times */
- if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
- anetSetError(err, "setsockopt SO_REUSEADDR: %s", strerror(errno));
+ if (anetSetReuseAddr(err,s) == ANET_ERR) {
+ close(s);
return ANET_ERR;
}
return s;