Skip to content

Commit

Permalink
TCP sendto removal + error when UDP for hostnames
Browse files Browse the repository at this point in the history
- UDP inet_pton(remote_ip)'s result is not checked -> the server ends up
  sending packets to the null address when a hostname is given instead
of a IPv4 address. A error is now raised

- TCP is using sendto() instead of send(). We can thus remove a useless
inet_pton() call (which moreover differs from the gethostbyaddr used in
setup_port() - possible issues when using hostnames)

Signed-off-by: Benoit Maurin <[email protected]>
  • Loading branch information
unjambonakap committed Oct 4, 2024
1 parent 2328ed6 commit c27f2cb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
11 changes: 3 additions & 8 deletions src/mavsdk/core/tcp_client_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,11 @@ bool TcpClientConnection::send_message(const mavlink_message_t& message)
auto flags = MSG_NOSIGNAL;
#endif

const auto send_len = sendto(
_socket_fd.get(),
reinterpret_cast<char*>(buffer),
buffer_len,
flags,
reinterpret_cast<const sockaddr*>(&dest_addr),
sizeof(dest_addr));
const auto send_len =
send(_socket_fd.get(), reinterpret_cast<const char*>(buffer), buffer_len, flags);

if (send_len != buffer_len) {
LogErr() << "sendto failure: " << GET_ERROR(errno);
LogErr() << "send failure: " << GET_ERROR(errno);
_is_ok = false;
return false;
}
Expand Down
11 changes: 9 additions & 2 deletions src/mavsdk/core/udp_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ ConnectionResult UdpConnection::setup_port()

struct sockaddr_in addr {};
addr.sin_family = AF_INET;
inet_pton(AF_INET, _local_ip.c_str(), &(addr.sin_addr));
if (inet_pton(AF_INET, _local_ip.c_str(), &(addr.sin_addr)) != 1) {
LogErr() << "inet_pton failure for address: " << _local_ip;
return ConnectionResult::SocketError;
}
addr.sin_port = htons(_local_port_number);

if (bind(_socket_fd.get(), reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) != 0) {
Expand Down Expand Up @@ -129,7 +132,11 @@ bool UdpConnection::send_message(const mavlink_message_t& message)
struct sockaddr_in dest_addr {};
dest_addr.sin_family = AF_INET;

inet_pton(AF_INET, remote.ip.c_str(), &dest_addr.sin_addr.s_addr);
if (inet_pton(AF_INET, remote.ip.c_str(), &dest_addr.sin_addr.s_addr) != 1) {
LogErr() << "inet_pton failure for address: " << remote.ip;
send_successful = false;
continue;
}
dest_addr.sin_port = htons(remote.port_number);

uint8_t buffer[MAVLINK_MAX_PACKET_LEN];
Expand Down

0 comments on commit c27f2cb

Please sign in to comment.