summaryrefslogtreecommitdiffhomepage
path: root/pkg/tcpip/header
diff options
context:
space:
mode:
authorGhanan Gowripalan <ghanan@google.com>2020-11-23 22:45:42 -0800
committergVisor bot <gvisor-bot@google.com>2020-11-23 22:47:55 -0800
commitba2d5cb7e1f3ac69a65d0e790f1319082ee01de2 (patch)
tree8bace42d0e2ee03d4f62178e811ab49b40caf76e /pkg/tcpip/header
parentd4951e05a00a9ec84b8065311836aa9c844f63f6 (diff)
Use time.Duration for IGMP Max Response Time field
Bug #4682 PiperOrigin-RevId: 343993297
Diffstat (limited to 'pkg/tcpip/header')
-rw-r--r--pkg/tcpip/header/igmp.go17
-rw-r--r--pkg/tcpip/header/igmp_test.go23
2 files changed, 32 insertions, 8 deletions
diff --git a/pkg/tcpip/header/igmp.go b/pkg/tcpip/header/igmp.go
index e0f5d46f4..5c5be1b9d 100644
--- a/pkg/tcpip/header/igmp.go
+++ b/pkg/tcpip/header/igmp.go
@@ -17,6 +17,7 @@ package header
import (
"encoding/binary"
"fmt"
+ "time"
"gvisor.dev/gvisor/pkg/tcpip"
)
@@ -103,7 +104,15 @@ func (b IGMP) SetType(t IGMPType) { b[igmpTypeOffset] = byte(t) }
// MaxRespTime gets the MaxRespTimeField. This is meaningful only in Membership
// Query messages, in other cases it is set to 0 by the sender and ignored by
// the receiver.
-func (b IGMP) MaxRespTime() byte { return b[igmpMaxRespTimeOffset] }
+func (b IGMP) MaxRespTime() time.Duration {
+ // As per RFC 2236 section 2.2,
+ //
+ // The Max Response Time field is meaningful only in Membership Query
+ // messages, and specifies the maximum allowed time before sending a
+ // responding report in units of 1/10 second. In all other messages, it
+ // is set to zero by the sender and ignored by receivers.
+ return DecisecondToDuration(b[igmpMaxRespTimeOffset])
+}
// SetMaxRespTime sets the MaxRespTimeField.
func (b IGMP) SetMaxRespTime(m byte) { b[igmpMaxRespTimeOffset] = m }
@@ -164,3 +173,9 @@ func IGMPCalculateChecksum(h IGMP) uint16 {
h.SetChecksum(existingXsum)
return xsum
}
+
+// DecisecondToDuration converts a value representing deci-seconds to a
+// time.Duration.
+func DecisecondToDuration(ds uint8) time.Duration {
+ return time.Duration(ds) * time.Second / 10
+}
diff --git a/pkg/tcpip/header/igmp_test.go b/pkg/tcpip/header/igmp_test.go
index 66e872880..b6126d29a 100644
--- a/pkg/tcpip/header/igmp_test.go
+++ b/pkg/tcpip/header/igmp_test.go
@@ -16,6 +16,7 @@ package header_test
import (
"testing"
+ "time"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/header"
@@ -23,10 +24,11 @@ import (
// TestIGMPHeader tests the functions within header.igmp
func TestIGMPHeader(t *testing.T) {
+ const maxRespTimeTenthSec = 0xF0
b := []byte{
- 0x11, // IGMP Type, Membership Query
- 0xF0, // Maximum Response Time
- 0xC0, 0xC0, // Checksum
+ 0x11, // IGMP Type, Membership Query
+ maxRespTimeTenthSec, // Maximum Response Time
+ 0xC0, 0xC0, // Checksum
0x01, 0x02, 0x03, 0x04, // Group Address
}
@@ -36,8 +38,8 @@ func TestIGMPHeader(t *testing.T) {
t.Errorf("got igmpHeader.Type() = %x, want = %x", got, want)
}
- if got, want := igmpHeader.MaxRespTime(), byte(0xF0); got != want {
- t.Errorf("got igmpHeader.MaxRespTime() = %x, want = %x", got, want)
+ if got, want := igmpHeader.MaxRespTime(), header.DecisecondToDuration(maxRespTimeTenthSec); got != want {
+ t.Errorf("got igmpHeader.MaxRespTime() = %s, want = %s", got, want)
}
if got, want := igmpHeader.Checksum(), uint16(0xC0C0); got != want {
@@ -59,8 +61,8 @@ func TestIGMPHeader(t *testing.T) {
respTime := byte(0x02)
igmpHeader.SetMaxRespTime(respTime)
- if got := igmpHeader.MaxRespTime(); got != respTime {
- t.Errorf("got igmpHeader.MaxRespTime() = %x, want = %x", got, respTime)
+ if got, want := igmpHeader.MaxRespTime(), header.DecisecondToDuration(respTime); got != want {
+ t.Errorf("got igmpHeader.MaxRespTime() = %s, want = %s", got, want)
}
checksum := uint16(0x0102)
@@ -99,3 +101,10 @@ func TestIGMPChecksum(t *testing.T) {
t.Errorf("got IGMPCalculateChecksum = %x, want %x", got, checksum)
}
}
+
+func TestDecisecondToDuration(t *testing.T) {
+ const valueInDeciseconds = 5
+ if got, want := header.DecisecondToDuration(valueInDeciseconds), valueInDeciseconds*time.Second/10; got != want {
+ t.Fatalf("got header.DecisecondToDuration(%d) = %s, want = %s", valueInDeciseconds, got, want)
+ }
+}