Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Support for binding to more than one IP address. #939

Closed
wants to merge 6 commits into from

3 participants

@ptjm

This change allows bind to accept a comma-delimited list of IP addresses. My motivation is to listen on localhost + an address on an internal network but not on a public network.

I changed server.ipfd into a small fixed array rather than allocating dynamically since it's a bit simpler and it seems unlikely anyone will want to explicitly bind to more than a small number of addresses.

Thanks.

@taf2

I would find this very helpful - we're in the process of migrating to a different network layout and being able to setup our redis servers to listen on multiple interfaces would make this easier.

@ptjm ptjm Merge branch 'unstable' of git://github.com/antirez/redis into bind-m…
…ultiple-addresses

This includes Salvatore Sanfilippos' version of this feature.

Conflicts:
	redis.conf
	src/aof.c
	src/rdb.c
	src/redis.c
	src/redis.h
6f5cc5b
@ptjm

This feature is implemented in commit 98eecb7. Thanks a lot.

@ptjm ptjm closed this
@antirez
Owner

Hello, thanks and sorry as I failed to notice there was already an implementation of that...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 8, 2013
  1. @ptjm
  2. @ptjm
Commits on May 7, 2013
  1. @ptjm

    Merge branch 'unstable' into bind-multiple-addresses

    ptjm authored
    Conflicts:
    	src/redis.c
    	src/redis.h
Commits on May 17, 2013
  1. @ptjm

    Merge branch 'unstable' of git://github.com/antirez/redis into bind-m…

    ptjm authored
    …ultiple-addresses
    
    Conflicts:
    	src/redis.c
  2. @ptjm

    Fixed error in merge.

    ptjm authored
Commits on Jul 13, 2013
  1. @ptjm

    Merge branch 'unstable' of git://github.com/antirez/redis into bind-m…

    ptjm authored
    …ultiple-addresses
    
    This includes Salvatore Sanfilippos' version of this feature.
    
    Conflicts:
    	redis.conf
    	src/aof.c
    	src/rdb.c
    	src/redis.c
    	src/redis.h
This page is out of date. Refresh to see the latest.
Showing with 55 additions and 0 deletions.
  1. +1 −0  src/aof.c
  2. +1 −0  src/rdb.c
  3. +52 −0 src/redis.c
  4. +1 −0  src/redis.h
View
1  src/aof.c
@@ -955,6 +955,7 @@ int rewriteAppendOnlyFile(char *filename) {
int rewriteAppendOnlyFileBackground(void) {
pid_t childpid;
long long start;
+ int j;
if (server.aof_child_pid != -1) return REDIS_ERR;
start = ustime();
View
1  src/rdb.c
@@ -718,6 +718,7 @@ int rdbSave(char *filename) {
int rdbSaveBackground(char *filename) {
pid_t childpid;
long long start;
+ int j;
if (server.rdb_child_pid != -1) return REDIS_ERR;
View
52 src/redis.c
@@ -1428,6 +1428,31 @@ void initServer() {
/* Open the TCP listening sockets. */
if (server.port != 0) {
+<<<<<<< HEAD
+ if (!server.bindaddr || !strchr(server.bindaddr, ',')) {
+ server.ipfd[0] = anetTcpServer(server.neterr,server.port,server.bindaddr);
+ if (server.ipfd[0] == ANET_ERR) {
+ redisLog(REDIS_WARNING, "Opening port %d: %s",
+ server.port, server.neterr);
+ exit(1);
+ }
+ }
+ else {
+ int cnt = 0;
+ sds * ba = sdssplitlen(server.bindaddr, strlen(server.bindaddr), ",", 1, &cnt);
+ if (cnt > REDIS_MAX_IP)
+ cnt = REDIS_MAX_IP;
+ for (j = 0; j < cnt; j++) {
+ server.ipfd[j] = anetTcpServer(server.neterr,server.port,ba[j]);
+ if (server.ipfd[j] == ANET_ERR) {
+ redisLog(REDIS_WARNING, "Opening port %d: %s",
+ server.port, server.neterr);
+ exit(1);
+ }
+ }
+ if (ba)
+ zfree(ba);
+=======
/* Force binding of 0.0.0.0 if no bind address is specified, always
* entering the loop if j == 0. */
if (server.bindaddr_count == 0) server.bindaddr[0] = NULL;
@@ -1452,6 +1477,7 @@ void initServer() {
exit(1);
}
server.ipfd_count++;
+>>>>>>> 34e20658307ae47bba4b4e14198534b361f4a642
}
}
@@ -1464,9 +1490,13 @@ void initServer() {
exit(1);
}
}
+<<<<<<< HEAD
+ if (server.ipfd[0] < 0 && server.sofd < 0) {
+=======
/* Abort if there are no listening sockets at all. */
if (server.ipfd_count == 0 && server.sofd < 0) {
+>>>>>>> 34e20658307ae47bba4b4e14198534b361f4a642
redisLog(REDIS_WARNING, "Configured to not listen anywhere, exiting.");
exit(1);
}
@@ -1521,6 +1551,11 @@ void initServer() {
redisPanic("Can't create the serverCron time event.");
exit(1);
}
+<<<<<<< HEAD
+ for (j = 0; j < REDIS_MAX_IP; j++)
+ if (server.ipfd[j] > 0 && aeCreateFileEvent(server.el,server.ipfd[j],AE_READABLE,
+ acceptTcpHandler,NULL) == AE_ERR) redisPanic("Unrecoverable error creating server.ipfd file event.");
+=======
/* Create an event handler for accepting new connections in TCP and Unix
* domain sockets. */
@@ -1532,6 +1567,7 @@ void initServer() {
"Unrecoverable error creating server.ipfd file event.");
}
}
+>>>>>>> 34e20658307ae47bba4b4e14198534b361f4a642
if (server.sofd > 0 && aeCreateFileEvent(server.el,server.sofd,AE_READABLE,
acceptUnixHandler,NULL) == AE_ERR) redisPanic("Unrecoverable error creating server.sofd file event.");
@@ -1988,6 +2024,7 @@ void closeListeningSockets(int unlink_unix_socket) {
int prepareForShutdown(int flags) {
int save = flags & REDIS_SHUTDOWN_SAVE;
int nosave = flags & REDIS_SHUTDOWN_NOSAVE;
+ int j;
redisLog(REDIS_WARNING,"User requested shutdown...");
/* Kill the saving child if there is a background saving in progress.
@@ -2028,7 +2065,18 @@ int prepareForShutdown(int flags) {
unlink(server.pidfile);
}
/* Close the listening sockets. Apparently this allows faster restarts. */
+<<<<<<< HEAD
+ for (j = 0; j < REDIS_MAX_IP; j++)
+ if (server.ipfd[j] != -1) close(server.ipfd[j]);
+ if (server.sofd != -1) close(server.sofd);
+ if (server.unixsocket) {
+ redisLog(REDIS_NOTICE,"Removing the unix socket file.");
+ unlink(server.unixsocket); /* don't care if this fails */
+ }
+
+=======
closeListeningSockets(1);
+>>>>>>> 34e20658307ae47bba4b4e14198534b361f4a642
redisLog(REDIS_WARNING,"Redis is now ready to exit, bye bye...");
return REDIS_OK;
}
@@ -2986,7 +3034,11 @@ int main(int argc, char **argv) {
exit(1);
}
}
+<<<<<<< HEAD
+ if (server.ipfd[0] > 0)
+=======
if (server.ipfd_count > 0)
+>>>>>>> 34e20658307ae47bba4b4e14198534b361f4a642
redisLog(REDIS_NOTICE,"The server is now ready to accept connections on port %d", server.port);
if (server.sofd > 0)
redisLog(REDIS_NOTICE,"The server is now ready to accept connections at %s", server.unixsocket);
View
1  src/redis.h
@@ -94,6 +94,7 @@
#define REDIS_REPL_PING_SLAVE_PERIOD 10
#define REDIS_RUN_ID_SIZE 40
#define REDIS_OPS_SEC_SAMPLES 16
+#define REDIS_MAX_IP 4 /* number of addresses to listen on */
#define REDIS_DEFAULT_REPL_BACKLOG_SIZE (1024*1024) /* 1mb */
#define REDIS_DEFAULT_REPL_BACKLOG_TIME_LIMIT (60*60) /* 1 hour */
#define REDIS_REPL_BACKLOG_MIN_SIZE (1024*16) /* 16k */
Something went wrong with that request. Please try again.