diff --git a/ping/destination.go b/ping/destination.go index 6105024..8648ecc 100644 --- a/ping/destination.go +++ b/ping/destination.go @@ -17,6 +17,9 @@ import ( "github.com/sagernet/sing/common/logger" ) +// Although its theoretical maximum may be 64k, I don’t yet know of any practical use case for that. For memory-usage reasons, I’m just using a 2k buffer. +const maxICMPPacketSize = 2048 + var _ tun.DirectRouteDestination = (*Destination)(nil) type Destination struct { @@ -77,7 +80,7 @@ func ConnectDestination( func (d *Destination) loopRead() { defer d.Close() for { - buffer := buf.NewPacket() + buffer := buf.NewSize(maxICMPPacketSize) err := d.conn.SetReadDeadline(time.Now().Add(d.timeout)) if err != nil { d.logger.ErrorContext(d.ctx, E.Cause(err, "set read deadline for ICMP conn")) diff --git a/ping/destination_gvisor.go b/ping/destination_gvisor.go index 0bac803..25fd36e 100644 --- a/ping/destination_gvisor.go +++ b/ping/destination_gvisor.go @@ -93,7 +93,7 @@ func ConnectGVisor( func (d *GVisorDestination) loopRead() { defer d.endpoint.Close() for { - buffer := buf.NewPacket() + buffer := buf.NewSize(maxICMPPacketSize) err := d.conn.SetReadDeadline(time.Now().Add(d.timeout)) if err != nil { d.logger.ErrorContext(d.ctx, E.Cause(err, "set read deadline for ICMP conn")) diff --git a/ping/socket_linux_unprivileged.go b/ping/socket_linux_unprivileged.go index 599eab9..3742cc8 100644 --- a/ping/socket_linux_unprivileged.go +++ b/ping/socket_linux_unprivileged.go @@ -125,7 +125,7 @@ func (c *UnprivilegedConn) fetchResponse(conn *net.UDPConn, identifier uint16) { return } } - buffer := buf.NewPacket() + buffer := buf.NewSize(maxICMPPacketSize) cmsgBuffer := buf.NewSize(1024) n, oobN, _, addr, err := conn.ReadMsgUDPAddrPort(buffer.FreeBytes(), cmsgBuffer.FreeBytes()) if err != nil {