From 253e796a59c68a0ccc57c156cfa61f97e97afad6 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 11 Jul 2013 10:25:48 +0200 Subject: example.c: it is now possible to specify server ip/port. This makes possible to use the example with IPv6 addresses and/or with a different Redis instance than 127.0.0.1:6379. --- examples/example.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/example.c b/examples/example.c index c76451f..25226a8 100644 --- a/examples/example.c +++ b/examples/example.c @@ -4,13 +4,15 @@ #include -int main(void) { +int main(int argc, char **argv) { unsigned int j; redisContext *c; redisReply *reply; + const char *hostname = (argc > 1) ? argv[1] : "127.0.0.1"; + int port = (argc > 2) ? atoi(argv[2]) : 6379; struct timeval timeout = { 1, 500000 }; // 1.5 seconds - c = redisConnectWithTimeout((char*)"127.0.0.1", 6379, timeout); + c = redisConnectWithTimeout(hostname, port, timeout); if (c == NULL || c->err) { if (c) { printf("Connection error: %s\n", c->errstr); -- cgit v1.2.3 From 06919b3f86b1c821eb1a53d4e1d7c74c78fe00e1 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 11 Jul 2013 11:39:03 +0200 Subject: Minimal IPv6 support. redisContextConnectTcp() is now able to use IPv6 addresses if there is no IPv4 address found resolving the specified hostname. --- net.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/net.c b/net.c index 483b160..603699c 100644 --- a/net.c +++ b/net.c @@ -255,9 +255,17 @@ int redisContextConnectTcp(redisContext *c, const char *addr, int port, const st hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; + /* Try with IPv6 if no IPv4 address was found. We do it in this order since + * in a Redis client you can't afford to test if you have IPv6 connectivity + * as this would add latency to every connect. Otherwise a more sensible + * route could be: Use IPv6 if both addresses are available and there is IPv6 + * connectivity. */ if ((rv = getaddrinfo(addr,_port,&hints,&servinfo)) != 0) { - __redisSetError(c,REDIS_ERR_OTHER,gai_strerror(rv)); - return REDIS_ERR; + hints.ai_family = AF_INET6; + if ((rv = getaddrinfo(addr,_port,&hints,&servinfo)) != 0) { + __redisSetError(c,REDIS_ERR_OTHER,gai_strerror(rv)); + return REDIS_ERR; + } } for (p = servinfo; p != NULL; p = p->ai_next) { if ((s = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == -1) -- cgit v1.2.3