Skip to content

Commit

Permalink
udp_connection: make remote IP/port theadsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
julianoes committed Sep 26, 2017
1 parent f7ab5c7 commit 8fa3b3c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 41 deletions.
84 changes: 45 additions & 39 deletions core/udp_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ namespace dronecore {
UdpConnection::UdpConnection(DroneCoreImpl *parent,
int local_port_number) :
Connection(parent),
_local_port_number(local_port_number),
_should_exit(false)
_local_port_number(local_port_number)
{
if (_local_port_number == 0) {
_local_port_number = DEFAULT_UDP_LOCAL_PORT;
Expand Down Expand Up @@ -132,23 +131,28 @@ DroneCore::ConnectionResult UdpConnection::stop()

bool UdpConnection::send_message(const mavlink_message_t &message)
{
if (_remote_ip.empty()) {
LogErr() << "Remote IP unknown";
return false;
}
struct sockaddr_in dest_addr;

if (_remote_port_number == 0) {
LogErr() << "Remote port unknown";
return false;
}
{
std::lock_guard<std::mutex> lock(_remote_mutex);

struct sockaddr_in dest_addr;
memset((char *)&dest_addr, 0, sizeof(dest_addr));
dest_addr.sin_family = AF_INET;
if (_remote_ip.empty()) {
LogErr() << "Remote IP unknown";
return false;
}

if (_remote_port_number == 0) {
LogErr() << "Remote port unknown";
return false;
}

inet_pton(AF_INET, _remote_ip.c_str(), &dest_addr.sin_addr.s_addr);
memset((char *)&dest_addr, 0, sizeof(dest_addr));
dest_addr.sin_family = AF_INET;

dest_addr.sin_port = htons(_remote_port_number);
inet_pton(AF_INET, _remote_ip.c_str(), &dest_addr.sin_addr.s_addr);

dest_addr.sin_port = htons(_remote_port_number);
}

uint8_t buffer[MAVLINK_MAX_PACKET_LEN];
uint16_t buffer_len = mavlink_msg_to_send_buffer(buffer, &message);
Expand Down Expand Up @@ -192,38 +196,40 @@ void UdpConnection::receive(UdpConnection *parent)
continue;
}

int new_remote_port_number = ntohs(src_addr.sin_port);
std::string new_remote_ip(inet_ntoa(src_addr.sin_addr));
{
std::lock_guard<std::mutex> lock(parent->_remote_mutex);

// TODO make calls to remote threadsafe.
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_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;
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;
LogInfo() << "Partner IP: " << parent->_remote_ip
<< ":" << parent->_remote_port_number;

} else {
} else {

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

Expand Down
6 changes: 4 additions & 2 deletions core/udp_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ class UdpConnection : public Connection
static constexpr int DEFAULT_UDP_LOCAL_PORT = 14540;

int _local_port_number;

std::mutex _remote_mutex = {};
std::string _remote_ip = {};
int _remote_port_number = 0;
std::mutex _mutex = {};

int _socket_fd = -1;
std::thread *_recv_thread = nullptr;
std::atomic_bool _should_exit;
std::atomic_bool _should_exit {false};
};

} // namespace dronecore

0 comments on commit 8fa3b3c

Please sign in to comment.