fix(mavlink): let a UDP client reconnect from a new port (#28518)

Once PX4 has latched onto a UDP client address, it keeps sending there
forever. A client that is restarted comes back with a different ephemeral
source port, so PX4 talks into the void and only a reboot brings the link
back. This is easy to hit when connecting straight to PX4's local port
(e.g. 18570) instead of listening on 14550/14540, which is what makes
some setups such as docker easier.

Instead of dropping the latched address on a timer, hand it over only
when someone else actually shows up: every incoming packet either
refreshes the latch, or - if it comes from a different source and the
current client has been quiet for 3 seconds - takes it over. A client
that only listens is therefore left alone, and an address configured
with -t or -c is never replaced.

The UDP read now resets nread before polling. It is declared outside the
loop and only assigned when POLLIN is set, so on a POLLERR wake-up the
stale value would have the previous buffer parsed again. It also lets
the source address comparison rely on nread to know it is valid.

Assisted-by: Claude:claude-opus-5

Signed-off-by: Julian Oes <julian@oes.ch>
This commit is contained in:
Julian Oes authored and GitHub committed 2026-09-14 13:46:23 +12:00
1 parent d57d7f3b11
commit bc1eb6c062
2 files changed
+51 -1

No files matched your search

+31 -1
View File
@@ -442,9 +442,35 @@ public:
struct sockaddr_in &get_client_source_address() { return _src_addr; }
void set_client_source_initialized() { _src_addr_initialized = true; }
/**
* Latch onto a client address that we discovered from an incoming packet.
* Also counts as having just heard from it, see client_source_can_be_replaced().
*/
void set_client_source_initialized()
{
_src_addr_initialized = true;
_src_addr_discovered = true;
mark_client_source_seen();
}
bool get_client_source_initialized() { return _src_addr_initialized; }
void mark_client_source_seen() { _time_last_client_source_packet = hrt_absolute_time(); }
/**
* Whether another source is allowed to take over as our client.
*
* This is the case if we discovered the current client ourselves (rather than
* having it configured using -t or -c), and it has stopped talking to us. That
* way a client which reconnects from a different source port - which happens
* whenever it is restarted - can take over, instead of us sending into the void
* until the next reboot.
*/
bool client_source_can_be_replaced() const
{
return _src_addr_discovered
&& hrt_elapsed_time(&_time_last_client_source_packet) > CLIENT_SOURCE_TIMEOUT;
}
#endif
uint64_t get_start_time() { return _mavlink_start_time; }
@@ -627,7 +653,11 @@ private:
sockaddr_in _src_addr {};
sockaddr_in _bcast_addr {};
static constexpr hrt_abstime CLIENT_SOURCE_TIMEOUT{3_s};
hrt_abstime _time_last_client_source_packet{0};
bool _src_addr_initialized{false};
bool _src_addr_discovered{false};
bool _broadcast_address_found{false};
bool _broadcast_address_not_found_warned{false};
bool _broadcast_failed_warned{false};
+20
View File
@@ -3854,6 +3854,8 @@ MavlinkReceiver::run()
#if defined(MAVLINK_UDP)
else if (_mavlink.get_protocol() == Protocol::UDP) {
nread = 0;
if (fds[0].revents & POLLIN) {
nread = recvfrom(_mavlink.get_socket_fd(), buf, sizeof(buf), 0, (struct sockaddr *)&srcaddr, &addrlen);
}
@@ -3879,6 +3881,24 @@ MavlinkReceiver::run()
PX4_INFO("partner IP: %s", inet_ntoa(srcaddr.sin_addr));
}
} else if (nread > 0) {
if ((srcaddr.sin_addr.s_addr == srcaddr_last.sin_addr.s_addr)
&& (srcaddr.sin_port == srcaddr_last.sin_port)) {
// Our client is still there, keep the address latched.
_mavlink.mark_client_source_seen();
} else if (_mavlink.client_source_can_be_replaced()) {
// Our client stopped talking to us a while ago and someone else is
// talking to us now. This is what a client which was restarted looks
// like, as it comes back with a new source port, so switch over to it.
srcaddr_last.sin_addr.s_addr = srcaddr.sin_addr.s_addr;
srcaddr_last.sin_port = srcaddr.sin_port;
_mavlink.set_client_source_initialized();
PX4_INFO("new partner IP: %s:%d", inet_ntoa(srcaddr.sin_addr), ntohs(srcaddr.sin_port));
}
}
}