Skip to content

Commit

Permalink
Merge pull request #110 from ken-schneider/main
Browse files Browse the repository at this point in the history
[bugfix] Update windows buffer size
  • Loading branch information
SuperQ committed Jul 24, 2024
2 parents f9e4712 + ec7a40f commit 01c023c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
21 changes: 19 additions & 2 deletions utils_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,33 @@
package probing

import (
"math"

"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
)

const (
minimumBufferLength = 2048
)

// Returns the length of an ICMP message, plus the IP packet header.
// Calculated as:
// len(ICMP request data) + 2 * (len(ICMP header) + len(IP header))
//
// On Windows, the buffer needs to be able to contain:
// - Response IP Header
// - Response ICMP Header
// - Request IP Header
// - Request ICMP Header
// - Request Data
func (p *Pinger) getMessageLength() int {
if p.ipv4 {
return p.Size + 8 + ipv4.HeaderLen
calculatedLength := p.Size + (ipv4.HeaderLen+8)*2
return int(math.Max(float64(calculatedLength), float64(minimumBufferLength)))
}
return p.Size + 8 + ipv6.HeaderLen
calculatedLength := p.Size + (ipv6.HeaderLen+8)*2
return int(math.Max(float64(calculatedLength), float64(minimumBufferLength)))
}

// Attempts to match the ID of an ICMP packet.
Expand Down
56 changes: 56 additions & 0 deletions utils_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//go:build windows
// +build windows

package probing

import "testing"

func TestGetMessageLength(t *testing.T) {
tests := []struct {
description string
pinger *Pinger
expected int
}{
{
description: "IPv4 total size < 2048",
pinger: &Pinger{
Size: 24, // default size
ipv4: true,
},
expected: 2048,
},
{
description: "IPv4 total size > 2048",
pinger: &Pinger{
Size: 1993, // 2048 - 2 * (ipv4.HeaderLen + 8) + 1
ipv4: true,
},
expected: 2049,
},
{
description: "IPv6 total size < 2048",
pinger: &Pinger{
Size: 24,
ipv4: false,
},
expected: 2048,
},
{
description: "IPv6 total size > 2048",
pinger: &Pinger{
Size: 1953, // 2048 - 2 * (ipv6.HeaderLen + 8) + 1
ipv4: false,
},
expected: 2049,
},
}

for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
actual := tt.pinger.getMessageLength()
if tt.expected != actual {
t.Fatalf("unexpected message length, expected: %d, actual %d", tt.expected, actual)
}
})
}
}

0 comments on commit 01c023c

Please sign in to comment.