mirror of
https://github.com/fosrl/newt.git
synced 2026-03-07 19:26:41 +00:00
Handle reply correctly
This commit is contained in:
@@ -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)
|
for {
|
||||||
return
|
n, peer, err := conn.ReadFrom(replyBuf)
|
||||||
}
|
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())
|
logger.Debug("ICMP Handler: Received %d bytes from %s", n, peer.String())
|
||||||
|
|
||||||
// Parse the reply
|
// Parse the reply
|
||||||
replyMsg, err := icmp.ParseMessage(1, replyBuf[:n])
|
replyMsg, err := icmp.ParseMessage(1, replyBuf[:n])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Info("ICMP Handler: Failed to parse ICMP reply: %v", err)
|
logger.Debug("ICMP Handler: Failed to parse ICMP message: %v", err)
|
||||||
return
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if it's an echo reply
|
// Check if it's an echo reply (type 0), not an echo request (type 8)
|
||||||
if replyMsg.Type != ipv4.ICMPTypeEchoReply {
|
if replyMsg.Type != ipv4.ICMPTypeEchoReply {
|
||||||
logger.Debug("ICMP Handler: Received non-echo-reply type: %v", replyMsg.Type)
|
logger.Debug("ICMP Handler: Received non-echo-reply type: %v (expected echo reply), continuing to wait", replyMsg.Type)
|
||||||
return
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
echoReply, ok := replyMsg.Body.(*icmp.Echo)
|
reply, ok := replyMsg.Body.(*icmp.Echo)
|
||||||
if !ok {
|
if !ok {
|
||||||
logger.Info("ICMP Handler: Invalid echo reply body type")
|
logger.Debug("ICMP Handler: Invalid echo reply body type, continuing to wait")
|
||||||
return
|
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)",
|
||||||
|
|||||||
Reference in New Issue
Block a user