Skip to content

Commit

Permalink
udp_connection: allow remote IP and port to change
Browse files Browse the repository at this point in the history
It does not really make sense that a device always needs to keep its IP
and port because a wifi connection might drop for a bit and therefore
the IP and port can change. Therefore, we no longer ignore messages
after the IP/port have changed and instead just accept it.
  • Loading branch information
julianoes committed Sep 26, 2017
1 parent 8fa3b3c commit a4b05bb
Showing 1 changed file with 19 additions and 28 deletions.
47 changes: 19 additions & 28 deletions core/udp_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,34 +202,25 @@ void UdpConnection::receive(UdpConnection *parent)
int new_remote_port_number = ntohs(src_addr.sin_port);
std::string new_remote_ip(inet_ntoa(src_addr.sin_addr));

if (parent->_remote_ip.empty()) {

if (parent->_remote_port_number == 0 ||
parent->_remote_port_number == new_remote_port_number) {
// Set IP if we don't know it yet.
parent->_remote_ip = new_remote_ip;
parent->_remote_port_number = new_remote_port_number;

LogInfo() << "Partner IP: " << parent->_remote_ip
<< ":" << parent->_remote_port_number;

} else {

LogWarn() << "Ignoring message from remote port " << new_remote_port_number
<< " instead of " << parent->_remote_port_number;
continue;
}

} else if (parent->_remote_ip.compare(new_remote_ip) != 0) {
LogWarn() << "Ignoring message from IP: " << new_remote_ip;
continue;

} else {
if (parent->_remote_port_number != new_remote_port_number) {
LogWarn() << "Ignoring message from remote port " << new_remote_port_number
<< " instead of " << parent->_remote_port_number;
continue;
}
if (parent->_remote_ip.empty() ||
parent->_remote_port_number == 0) {

// Set IP if we don't know it yet.
parent->_remote_ip = new_remote_ip;
parent->_remote_port_number = new_remote_port_number;

LogInfo() << "New device on: " << parent->_remote_ip
<< ":" << parent->_remote_port_number;

} else if (parent->_remote_ip.compare(new_remote_ip) != 0 ||
parent->_remote_port_number != new_remote_port_number) {

// It is possible that wifi disconnects and a device might get a new
// IP and/or UDP port.
parent->_remote_ip = new_remote_ip;
parent->_remote_port_number = new_remote_port_number;

LogInfo() << "Device changed to: " << new_remote_ip;
}
}

Expand Down

0 comments on commit a4b05bb

Please sign in to comment.