Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate Addr, Exclude nodes with invalid ip address #95

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/ClusterNodesParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "Buffer.h"
#include "String.h"
#include "Server.h"
#include "arpa/inet.h"
#include "string.h"

class ClusterNodesParser
{
Expand Down Expand Up @@ -55,6 +57,33 @@ class ClusterNodesParser
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the host will point an invalid address

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now i have changed it to use mAddr data instead of pointer.

}

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
{
Expand Down
2 changes: 1 addition & 1 deletion src/ClusterServerPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down