Skip to content
Snippets Groups Projects
Commit e8948bae authored by Nicolas Pope's avatar Nicolas Pope
Browse files

Prevent self connections

parent 52cadcc9
Branches
No related tags found
1 merge request!316Resolves #343 GUI and Frame Refactor
...@@ -28,9 +28,12 @@ class Listener { ...@@ -28,9 +28,12 @@ class Listener {
void connection(std::shared_ptr<Peer> &s); void connection(std::shared_ptr<Peer> &s);
void onConnection(connecthandler_t h) { handler_connect_.push_back(h); }; void onConnection(connecthandler_t h) { handler_connect_.push_back(h); };
inline int port() const { return port_; }
private: private:
SOCKET descriptor_; SOCKET descriptor_;
Protocol *default_proto_; Protocol *default_proto_;
int port_;
//sockaddr_in slocalAddr; //sockaddr_in slocalAddr;
std::vector<connecthandler_t> handler_connect_; std::vector<connecthandler_t> handler_connect_;
}; };
......
...@@ -102,7 +102,7 @@ Listener::Listener(const char *pUri) : default_proto_(NULL) { ...@@ -102,7 +102,7 @@ Listener::Listener(const char *pUri) : default_proto_(NULL) {
if (uri.getProtocol() == URI::SCHEME_TCP) { if (uri.getProtocol() == URI::SCHEME_TCP) {
descriptor_ = tcpListen(uri); descriptor_ = tcpListen(uri);
std::cout << "Listening: " << pUri << " - " << descriptor_ << std::endl; port_ = uri.getPort();
} else if (uri.getProtocol() == URI::SCHEME_WS) { } else if (uri.getProtocol() == URI::SCHEME_WS) {
descriptor_ = wsListen(uri); descriptor_ = wsListen(uri);
} else { } else {
......
...@@ -149,13 +149,23 @@ bool Universe::listen(const string &addr) { ...@@ -149,13 +149,23 @@ bool Universe::listen(const string &addr) {
} }
Peer *Universe::connect(const string &addr) { Peer *Universe::connect(const string &addr) {
// Check if already connected // Check if already connected or if self
{ {
UNIQUE_LOCK(net_mutex_,lk); UNIQUE_LOCK(net_mutex_,lk);
if (peer_by_uri_.find(addr) != peer_by_uri_.end()) { if (peer_by_uri_.find(addr) != peer_by_uri_.end()) {
return peer_by_uri_[addr]; return peer_by_uri_[addr];
} }
ftl::URI u(addr);
if (u.getHost() == "localhost" || u.getHost() == "127.0.0.1") {
for (const auto *l : listeners_) {
if (l->port() == u.getPort()) {
throw FTL_Error("Cannot connect to self");
}
}
} }
}
auto p = new Peer(addr.c_str(), this, &disp_); auto p = new Peer(addr.c_str(), this, &disp_);
if (!p) return nullptr; if (!p) return nullptr;
...@@ -263,6 +273,29 @@ Peer *Universe::getPeer(const UUID &id) const { ...@@ -263,6 +273,29 @@ Peer *Universe::getPeer(const UUID &id) const {
void Universe::_periodic() { void Universe::_periodic() {
auto i = reconnects_.begin(); auto i = reconnects_.begin();
while (i != reconnects_.end()) { while (i != reconnects_.end()) {
std::string addr = i->peer->getURI();
{
UNIQUE_LOCK(net_mutex_,lk);
ftl::URI u(addr);
bool removed = false;
if (u.getHost() == "localhost" || u.getHost() == "127.0.0.1") {
for (const auto *l : listeners_) {
if (l->port() == u.getPort()) {
LOG(ERROR) << "Cannot connect to self";
garbage_.push_back((*i).peer);
i = reconnects_.erase(i);
removed = true;
break;
}
}
}
if (removed) continue;
}
if ((*i).peer->reconnect()) { if ((*i).peer->reconnect()) {
UNIQUE_LOCK(net_mutex_,lk); UNIQUE_LOCK(net_mutex_,lk);
peers_.push_back((*i).peer); peers_.push_back((*i).peer);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment