diff options
author | Trond Norbye <trond.norbye@gmail.com> | 2011-06-07 23:37:02 +0200 |
---|---|---|
committer | Trond Norbye <trond.norbye@gmail.com> | 2011-07-11 16:22:38 +0200 |
commit | 5eaf559e3417d63312eb67bcec5f0640dd47d858 (patch) | |
tree | 2480c129b10840a3df85eb2d56750cf775d91e02 | |
parent | 3dc5964d9309bf8b4757c523a4491b61ca3ba8b8 (diff) | |
download | memcached-5eaf559e3417d63312eb67bcec5f0640dd47d858.tar.gz |
mcstat should try multiple addrinfo
-rw-r--r-- | programs/mcstat.c | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/programs/mcstat.c b/programs/mcstat.c index 7a869fb..22fb741 100644 --- a/programs/mcstat.c +++ b/programs/mcstat.c @@ -17,24 +17,31 @@ */ static int connect_server(const char *hostname, const char *port) { - struct addrinfo *ai = NULL; + struct addrinfo *ainfo = NULL; struct addrinfo hints = { .ai_family = AF_UNSPEC, .ai_protocol = IPPROTO_TCP, .ai_socktype = SOCK_STREAM }; - if (getaddrinfo(hostname, port, &hints, &ai) != 0) { + if (getaddrinfo(hostname, port, &hints, &ainfo) != 0) { return -1; } + int sock = -1; - if ((sock = socket(ai->ai_family, ai->ai_socktype, - ai->ai_protocol)) != -1) { - if (connect(sock, ai->ai_addr, ai->ai_addrlen) == -1) { + struct addrinfo *ai = ainfo; + while (ai != NULL) { + sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); + + if (sock != -1) { + if (connect(sock, ai->ai_addr, ai->ai_addrlen) != -1) { + break; + } close(sock); sock = -1; } + ai = ai->ai_next; } - freeaddrinfo(ai); + freeaddrinfo(ainfo); return sock; } |