Skip to content

Commit

Permalink
fix bug in IP marshal and add test to catch it
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Sep 13, 2023
1 parent 009c067 commit d82c002
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion internal/tcpctl/eth/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func DecodeIPv4Header(buf []byte) (iphdr IPv4Header) {
// Put marshals the IPv4 frame onto buf. buf needs to be 20 bytes in length or Put panics.
func (iphdr *IPv4Header) Put(buf []byte) {
_ = buf[19]
buf[0] = 4 & (iphdr.VersionAndIHL & 0xf0) // ignore set version.
buf[0] = (4 << 4) | (iphdr.VersionAndIHL & 0xf) // ignore set version.
buf[1] = iphdr.ToS
binary.BigEndian.PutUint16(buf[2:], iphdr.TotalLength)
binary.BigEndian.PutUint16(buf[4:], iphdr.ID)
Expand Down
33 changes: 26 additions & 7 deletions internal/tcpctl/eth/headers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,26 @@ func TestUDPChecksum(t *testing.T) {
0x22, 0x3a, 0x20, 0x5b, 0x38, 0x31, 0x35, 0x32, 0x34, 0x36, 0x32, 0x30, 0x30, 0x30, 0x5d, 0x7d, // |": [8152462000]}|
}
// Process Ethernet header.
e := DecodeEthernetHeader(testUDPPacket[:14])
if !bytes.Equal(e.Destination[:], testUDPPacket[0:6]) {
ethData := testUDPPacket[:14]
e := DecodeEthernetHeader(ethData)
if !bytes.Equal(e.Destination[:], ethData[0:6]) {
t.Errorf("incorrect ethernet destination: %v", e.String())
}
if !bytes.Equal(e.Source[:], testUDPPacket[6:12]) {
if !bytes.Equal(e.Source[:], ethData[6:12]) {
t.Errorf("incorrect ethernet source: %v", e.String())
}
if e.AssertType() != EtherTypeIPv4 {
t.Errorf("incorrect ethertype: %v", e.String())
}

ethDataGot := make([]byte, len(ethData))
e.Put(ethDataGot)
if !bytes.Equal(ethData, ethDataGot) {
got := DecodeEthernetHeader(ethDataGot)
t.Error("ethernet marshal does not match original data", e.String(), got.String())
}
// Process IP header.
ip := DecodeIPv4Header(testUDPPacket[14:34])
ipData := testUDPPacket[14:34]
ip := DecodeIPv4Header(ipData)
if ip.Protocol != 17 {
t.Errorf("incorrect ip protocol: %v", ip.String())
}
Expand All @@ -45,9 +52,15 @@ func TestUDPChecksum(t *testing.T) {
if !bytes.Equal(ip.Destination[:], testUDPPacket[30:34]) {
t.Errorf("incorrect ip destination: %v", ip.String())
}

ipDataGot := make([]byte, len(ipData))
ip.Put(ipDataGot)
if !bytes.Equal(ipData, ipDataGot) {
got := DecodeIPv4Header(ipDataGot)
t.Error("ip marshal does not match original data", ip.String(), got.String())
}
// Process UDP header.
udp := DecodeUDPHeader(testUDPPacket[34 : 34+8])
udpData := testUDPPacket[34 : 34+8]
udp := DecodeUDPHeader(udpData)
if udp.SourcePort != 17500 {
t.Errorf("incorrect udp source port: %v", udp.String())
}
Expand All @@ -57,6 +70,12 @@ func TestUDPChecksum(t *testing.T) {
if udp.Length != 142 {
t.Errorf("incorrect udp length: %v", udp.String())
}
udpDataGot := make([]byte, len(udpData))
udp.Put(udpDataGot)
if !bytes.Equal(udpData, udpDataGot) {
got := DecodeUDPHeader(udpDataGot)
t.Error("udp marshal does not match original data", udp.String(), got.String())
}
}

func TestCRC791_oneshot(t *testing.T) {
Expand Down

0 comments on commit d82c002

Please sign in to comment.