Use available helpers to set fields

...and introduce a helper to set the Checksum.

PiperOrigin-RevId: 417437300
This commit is contained in:
Ghanan Gowripalan
2021-12-20 10:49:43 -08:00
committed by gVisor bot
parent fd89c0892b
commit b26b4610bf
7 changed files with 22 additions and 17 deletions
+5
View File
@@ -24,6 +24,11 @@ import (
"gvisor.dev/gvisor/pkg/tcpip/buffer"
)
// PutChecksum puts the checksum in the provided byte slice.
func PutChecksum(b []byte, xsum uint16) {
binary.BigEndian.PutUint16(b, xsum)
}
func calculateChecksum(buf []byte, odd bool, initial uint32) (uint16, bool) {
v := initial
+1 -1
View File
@@ -139,7 +139,7 @@ func (b ICMPv4) Checksum() uint16 {
// SetChecksum sets the ICMP checksum field.
func (b ICMPv4) SetChecksum(checksum uint16) {
binary.BigEndian.PutUint16(b[icmpv4ChecksumOffset:], checksum)
PutChecksum(b[icmpv4ChecksumOffset:], checksum)
}
// SourcePort implements Transport.SourcePort.
+1 -1
View File
@@ -199,7 +199,7 @@ func (b ICMPv6) Checksum() uint16 {
// SetChecksum sets the ICMP checksum field.
func (b ICMPv6) SetChecksum(checksum uint16) {
binary.BigEndian.PutUint16(b[icmpv6ChecksumOffset:], checksum)
PutChecksum(b[icmpv6ChecksumOffset:], checksum)
}
// SourcePort implements Transport.SourcePort.
+1 -1
View File
@@ -380,7 +380,7 @@ func (b IPv4) SetTotalLength(totalLength uint16) {
// SetChecksum sets the checksum field of the IPv4 header.
func (b IPv4) SetChecksum(v uint16) {
binary.BigEndian.PutUint16(b[checksum:], v)
PutChecksum(b[checksum:], v)
}
// SetFlagsFragmentOffset sets the "flags" and "fragment offset" fields of the
+8 -8
View File
@@ -289,7 +289,7 @@ func (b TCP) SetDestinationPort(port uint16) {
// SetChecksum sets the checksum field of the TCP header.
func (b TCP) SetChecksum(checksum uint16) {
binary.BigEndian.PutUint16(b[TCPChecksumOffset:], checksum)
PutChecksum(b[TCPChecksumOffset:], checksum)
}
// SetDataOffset sets the data offset field of the TCP header. headerLen should
@@ -318,8 +318,8 @@ func (b TCP) SetWindowSize(rcvwnd uint16) {
binary.BigEndian.PutUint16(b[TCPWinSizeOffset:], rcvwnd)
}
// SetUrgentPoiner sets the window size field of the TCP header.
func (b TCP) SetUrgentPoiner(urgentPointer uint16) {
// SetUrgentPointer sets the window size field of the TCP header.
func (b TCP) SetUrgentPointer(urgentPointer uint16) {
binary.BigEndian.PutUint16(b[TCPUrgentPtrOffset:], urgentPointer)
}
@@ -360,11 +360,11 @@ func (b TCP) encodeSubset(seq, ack uint32, flags TCPFlags, rcvwnd uint16) {
// Encode encodes all the fields of the TCP header.
func (b TCP) Encode(t *TCPFields) {
b.encodeSubset(t.SeqNum, t.AckNum, t.Flags, t.WindowSize)
binary.BigEndian.PutUint16(b[TCPSrcPortOffset:], t.SrcPort)
binary.BigEndian.PutUint16(b[TCPDstPortOffset:], t.DstPort)
b[TCPDataOffset] = (t.DataOffset / 4) << 4
binary.BigEndian.PutUint16(b[TCPChecksumOffset:], t.Checksum)
binary.BigEndian.PutUint16(b[TCPUrgentPtrOffset:], t.UrgentPointer)
b.SetSourcePort(t.SrcPort)
b.SetDestinationPort(t.DstPort)
b.SetDataOffset(t.DataOffset)
b.SetChecksum(t.Checksum)
b.SetUrgentPointer(t.UrgentPointer)
}
// EncodePartial updates a subset of the fields of the TCP header. It is useful
+5 -5
View File
@@ -101,7 +101,7 @@ func (b UDP) SetDestinationPort(port uint16) {
// SetChecksum sets the "checksum" field of the UDP header.
func (b UDP) SetChecksum(checksum uint16) {
binary.BigEndian.PutUint16(b[udpChecksum:], checksum)
PutChecksum(b[udpChecksum:], checksum)
}
// SetLength sets the "length" field of the UDP header.
@@ -125,10 +125,10 @@ func (b UDP) IsChecksumValid(src, dst tcpip.Address, payloadChecksum uint16) boo
// Encode encodes all the fields of the UDP header.
func (b UDP) Encode(u *UDPFields) {
binary.BigEndian.PutUint16(b[udpSrcPort:], u.SrcPort)
binary.BigEndian.PutUint16(b[udpDstPort:], u.DstPort)
binary.BigEndian.PutUint16(b[udpLength:], u.Length)
binary.BigEndian.PutUint16(b[udpChecksum:], u.Checksum)
b.SetSourcePort(u.SrcPort)
b.SetDestinationPort(u.DstPort)
b.SetLength(u.Length)
b.SetChecksum(u.Checksum)
}
// SetSourcePortWithChecksumUpdate implements ChecksummableTransport.
+1 -1
View File
@@ -1089,7 +1089,7 @@ func (l *TCP) ToBytes() ([]byte, error) {
h.SetWindowSize(32768)
}
if l.UrgentPointer != nil {
h.SetUrgentPoiner(*l.UrgentPointer)
h.SetUrgentPointer(*l.UrgentPointer)
}
copy(b[header.TCPMinimumSize:], l.Options)
header.AddTCPOptionPadding(b[header.TCPMinimumSize:], len(l.Options))