Handle reply correctly

This commit is contained in:
Owen
2025-12-16 12:23:12 -05:00
parent 058330d41b
commit 55be2a52a5

View File

@@ -508,35 +508,50 @@ func (h *ICMPHandler) proxyPing(srcIP, originalDstIP, actualDstIP string, ident,
return return
} }
logger.Debug("ICMP Handler: Ping sent to %s, waiting for reply", actualDstIP) logger.Debug("ICMP Handler: Ping sent to %s, waiting for reply (ident=%d, seq=%d)", actualDstIP, ident, seq)
// Wait for reply // Wait for reply - loop to filter out non-matching packets (like our own echo request)
replyBuf := make([]byte, 1500) replyBuf := make([]byte, 1500)
n, peer, err := conn.ReadFrom(replyBuf) var echoReply *icmp.Echo
if err != nil {
logger.Info("ICMP Handler: Failed to receive ping reply from %s: %v", actualDstIP, err)
return
}
logger.Debug("ICMP Handler: Received %d bytes from %s", n, peer.String()) for {
n, peer, err := conn.ReadFrom(replyBuf)
if err != nil {
logger.Info("ICMP Handler: Failed to receive ping reply from %s: %v", actualDstIP, err)
return
}
// Parse the reply logger.Debug("ICMP Handler: Received %d bytes from %s", n, peer.String())
replyMsg, err := icmp.ParseMessage(1, replyBuf[:n])
if err != nil {
logger.Info("ICMP Handler: Failed to parse ICMP reply: %v", err)
return
}
// Check if it's an echo reply // Parse the reply
if replyMsg.Type != ipv4.ICMPTypeEchoReply { replyMsg, err := icmp.ParseMessage(1, replyBuf[:n])
logger.Debug("ICMP Handler: Received non-echo-reply type: %v", replyMsg.Type) if err != nil {
return logger.Debug("ICMP Handler: Failed to parse ICMP message: %v", err)
} continue
}
echoReply, ok := replyMsg.Body.(*icmp.Echo) // Check if it's an echo reply (type 0), not an echo request (type 8)
if !ok { if replyMsg.Type != ipv4.ICMPTypeEchoReply {
logger.Info("ICMP Handler: Invalid echo reply body type") logger.Debug("ICMP Handler: Received non-echo-reply type: %v (expected echo reply), continuing to wait", replyMsg.Type)
return continue
}
reply, ok := replyMsg.Body.(*icmp.Echo)
if !ok {
logger.Debug("ICMP Handler: Invalid echo reply body type, continuing to wait")
continue
}
// Verify the ident and sequence match what we sent
if reply.ID != int(ident) || reply.Seq != int(seq) {
logger.Debug("ICMP Handler: Reply ident/seq mismatch: got ident=%d seq=%d, want ident=%d seq=%d",
reply.ID, reply.Seq, ident, seq)
continue
}
// Found matching reply
echoReply = reply
break
} }
logger.Info("ICMP Handler: Ping successful to %s, injecting reply (ident=%d, seq=%d)", logger.Info("ICMP Handler: Ping successful to %s, injecting reply (ident=%d, seq=%d)",