summaryrefslogtreecommitdiff
path: root/src/anet.c
diff options
context:
space:
mode:
authorMatt Stancliff <matt@genges.com>2014-03-03 10:57:27 -0500
committerMatt Stancliff <matt@genges.com>2014-03-04 17:36:45 -0500
commite5b1e7be64d82b0360af3bed0eaa22ca942be4f4 (patch)
tree53892bfc43ea1381bbb9411de3bafc3c86c4d7b2 /src/anet.c
parent8d011492a0802c45114bab63c41b7311648632be (diff)
downloadredis-e5b1e7be64d82b0360af3bed0eaa22ca942be4f4.tar.gz
Bind source address for cluster communication
The first address specified as a bind parameter (server.bindaddr[0]) gets used as the source IP for cluster communication. If no bind address is specified by the user, the behavior is unchanged. This patch allows multiple Redis Cluster instances to communicate when running on the same interface of the same host.
Diffstat (limited to 'src/anet.c')
-rw-r--r--src/anet.c32
1 files changed, 28 insertions, 4 deletions
diff --git a/src/anet.c b/src/anet.c
index 2851318e2..91545a0d5 100644
--- a/src/anet.c
+++ b/src/anet.c
@@ -234,11 +234,12 @@ static int anetCreateSocket(char *err, int domain) {
#define ANET_CONNECT_NONE 0
#define ANET_CONNECT_NONBLOCK 1
-static int anetTcpGenericConnect(char *err, char *addr, int port, int flags)
+static int anetTcpGenericConnect(char *err, char *addr, int port,
+ char *source_addr, int flags)
{
int s = ANET_ERR, rv;
char portstr[6]; /* strlen("65535") + 1; */
- struct addrinfo hints, *servinfo, *p;
+ struct addrinfo hints, *servinfo, *bservinfo, *p, *b;
snprintf(portstr,sizeof(portstr),"%d",port);
memset(&hints,0,sizeof(hints));
@@ -258,6 +259,24 @@ static int anetTcpGenericConnect(char *err, char *addr, int port, int flags)
if (anetSetReuseAddr(err,s) == ANET_ERR) goto error;
if (flags & ANET_CONNECT_NONBLOCK && anetNonBlock(err,s) != ANET_OK)
goto error;
+ if (source_addr) {
+ int bound = 0;
+ /* Using getaddrinfo saves us from self-determining IPv4 vs IPv6 */
+ if ((rv = getaddrinfo(source_addr, NULL, &hints, &bservinfo)) != 0) {
+ anetSetError(err, "%s", gai_strerror(rv));
+ goto end;
+ }
+ for (b = bservinfo; b != NULL; b = b->ai_next) {
+ if (bind(s,b->ai_addr,b->ai_addrlen) != -1) {
+ bound = 1;
+ break;
+ }
+ }
+ if (!bound) {
+ anetSetError(err, "bind: %s", strerror(errno));
+ goto end;
+ }
+ }
if (connect(s,p->ai_addr,p->ai_addrlen) == -1) {
/* If the socket is non-blocking, it is ok for connect() to
* return an EINPROGRESS error here. */
@@ -287,12 +306,17 @@ end:
int anetTcpConnect(char *err, char *addr, int port)
{
- return anetTcpGenericConnect(err,addr,port,ANET_CONNECT_NONE);
+ return anetTcpGenericConnect(err,addr,port,NULL,ANET_CONNECT_NONE);
}
int anetTcpNonBlockConnect(char *err, char *addr, int port)
{
- return anetTcpGenericConnect(err,addr,port,ANET_CONNECT_NONBLOCK);
+ return anetTcpGenericConnect(err,addr,port,NULL,ANET_CONNECT_NONBLOCK);
+}
+
+int anetTcpNonBlockBindConnect(char *err, char *addr, int port, char *source_addr)
+{
+ return anetTcpGenericConnect(err,addr,port,source_addr,ANET_CONNECT_NONBLOCK);
}
int anetUnixGenericConnect(char *err, char *path, int flags)