From 6c490853474ea6e55c70098d29d86f91f48684cc Mon Sep 17 00:00:00 2001 From: Nidhi Agarwal Date: Wed, 13 Nov 2019 11:46:22 +0530 Subject: [PATCH 1/3] Throw error on empty ip address --- src/ClusterNodesParser.h | 29 +++++++++++++++++++++++++++++ src/ClusterServerPool.cpp | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/ClusterNodesParser.h b/src/ClusterNodesParser.h index e17c0c8..340b9ad 100644 --- a/src/ClusterNodesParser.h +++ b/src/ClusterNodesParser.h @@ -10,6 +10,8 @@ #include "Buffer.h" #include "String.h" #include "Server.h" +#include "arpa/inet.h" +#include "string.h" class ClusterNodesParser { @@ -55,6 +57,33 @@ public: end = mSlotEnd; return begin >= 0 && begin < end; } + bool validAddr() const { + if (mAddr.empty()) { + return false; + } + + const char* host = ""; + const char* port = strrchr(mAddr, ':'); + if (port) { + std::string tmp; + tmp.append(mAddr, port - mAddr); + host = tmp.c_str(); + } + + char dst1[INET_ADDRSTRLEN]; + int isIpv4 = inet_pton(AF_INET, host, dst1); + if (isIpv4) { + return true; + } + + char dst2[INET6_ADDRSTRLEN]; + int isIpv6 = inet_pton(AF_INET6, host, dst2); + if (isIpv6) { + return true; + } + + return false; + } private: enum State { diff --git a/src/ClusterServerPool.cpp b/src/ClusterServerPool.cpp index 35591d2..a4b8c83 100644 --- a/src/ClusterServerPool.cpp +++ b/src/ClusterServerPool.cpp @@ -101,7 +101,7 @@ void ClusterServerPool::handleResponse(Handler* h, ConnectConnection* s, Request p.addr().data(), p.flags().data(), p.master().data()); - if (p.addr().empty()) { + if (!p.validAddr()) { logWarn("redis cluster nodes get node invalid %s %s %s %s", p.nodeId().data(), p.addr().data(), From 443b2954e43f48157deb048e88f5522d80293cf6 Mon Sep 17 00:00:00 2001 From: Nidhi Agarwal Date: Thu, 21 Nov 2019 13:57:03 +0530 Subject: [PATCH 2/3] handle without port ip address validation --- src/ClusterNodesParser.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ClusterNodesParser.h b/src/ClusterNodesParser.h index 340b9ad..cb17d14 100644 --- a/src/ClusterNodesParser.h +++ b/src/ClusterNodesParser.h @@ -62,7 +62,7 @@ public: return false; } - const char* host = ""; + const char* host = mAddr; const char* port = strrchr(mAddr, ':'); if (port) { std::string tmp; From c06b1a7e316b33decaeeaf4af7f903c87b39bf66 Mon Sep 17 00:00:00 2001 From: Nidhi Agarwal Date: Thu, 21 Nov 2019 21:36:21 +0530 Subject: [PATCH 3/3] use string instead of pointor --- src/ClusterNodesParser.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ClusterNodesParser.h b/src/ClusterNodesParser.h index cb17d14..15fbaf4 100644 --- a/src/ClusterNodesParser.h +++ b/src/ClusterNodesParser.h @@ -62,7 +62,7 @@ public: return false; } - const char* host = mAddr; + const char* host = mAddr.data(); const char* port = strrchr(mAddr, ':'); if (port) { std::string tmp;