From ae52e9a1d1f120d358830a3d2ef68fa7683bde2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C4=81vis=20Mos=C4=81ns?= Date: Tue, 3 Dec 2024 18:17:49 +0200 Subject: [PATCH] Make `send()` more robust by handling previous ACK --- lib/tftp/tftp.rb | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/tftp/tftp.rb b/lib/tftp/tftp.rb index 86ee945..8d0dd86 100644 --- a/lib/tftp/tftp.rb +++ b/lib/tftp/tftp.rb @@ -129,15 +129,18 @@ def send(tag, sock, io) log :warn, "#{tag} Timeout at block ##{seq}" return end - msg, _ = sock.recvfrom(4, 0) - pkt = Packet.parse(msg) - if pkt.class != Packet::ACK - log :warn, "#{tag} Expected ACK but got: #{pkt.class}" - return - end - if pkt.seq != seq - log :warn, "#{tag} Seq mismatch: #{seq} != #{pkt.seq}" - return + loop do + msg, _ = sock.recvfrom(4, 0) + pkt = Packet.parse(msg) + if pkt.class != Packet::ACK + log :warn, "#{tag} Expected ACK but got: #{pkt.class}" + return + end + break if pkt.seq == seq + if pkt.seq > seq + log :warn, "#{tag} Seq mismatch: #{seq} != #{pkt.seq}" + return + end end # Increment with wrap around at 16 bit boundary, # because of tftp block number field size limit.