-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NETPATH-371] Move common functions to separate package, create separ…
…ate testutils package (#31819)
- Loading branch information
1 parent
0b42e2b
commit 385f25a
Showing
14 changed files
with
481 additions
and
399 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016-present Datadog, Inc. | ||
|
||
// Package common contains common functionality for both TCP and UDP | ||
// traceroute implementations | ||
package common | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/DataDog/datadog-agent/pkg/util/log" | ||
"github.com/google/gopacket" | ||
"github.com/google/gopacket/layers" | ||
"golang.org/x/net/ipv4" | ||
) | ||
|
||
const ( | ||
// IPProtoICMP is the IP protocol number for ICMP | ||
// we create our own constant here because there are | ||
// different imports for the constant in different | ||
// operating systems | ||
IPProtoICMP = 1 | ||
) | ||
|
||
type ( | ||
// Results encapsulates a response from the | ||
// traceroute | ||
Results struct { | ||
Source net.IP | ||
SourcePort uint16 | ||
Target net.IP | ||
DstPort uint16 | ||
Hops []*Hop | ||
} | ||
|
||
// Hop encapsulates information about a single | ||
// hop in a traceroute | ||
Hop struct { | ||
IP net.IP | ||
Port uint16 | ||
ICMPType layers.ICMPv4TypeCode | ||
RTT time.Duration | ||
IsDest bool | ||
} | ||
|
||
// CanceledError is sent when a listener | ||
// is canceled | ||
CanceledError string | ||
|
||
// ICMPResponse encapsulates the data from | ||
// an ICMP response packet needed for matching | ||
ICMPResponse struct { | ||
SrcIP net.IP | ||
DstIP net.IP | ||
TypeCode layers.ICMPv4TypeCode | ||
InnerSrcIP net.IP | ||
InnerDstIP net.IP | ||
InnerSrcPort uint16 | ||
InnerDstPort uint16 | ||
InnerSeqNum uint32 | ||
} | ||
) | ||
|
||
func (c CanceledError) Error() string { | ||
return string(c) | ||
} | ||
|
||
// LocalAddrForHost takes in a destionation IP and port and returns the local | ||
// address that should be used to connect to the destination | ||
func LocalAddrForHost(destIP net.IP, destPort uint16) (*net.UDPAddr, error) { | ||
// this is a quick way to get the local address for connecting to the host | ||
// using UDP as the network type to avoid actually creating a connection to | ||
// the host, just get the OS to give us a local IP and local ephemeral port | ||
conn, err := net.Dial("udp4", net.JoinHostPort(destIP.String(), strconv.Itoa(int(destPort)))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer conn.Close() | ||
localAddr := conn.LocalAddr() | ||
|
||
localUDPAddr, ok := localAddr.(*net.UDPAddr) | ||
if !ok { | ||
return nil, fmt.Errorf("invalid address type for %s: want %T, got %T", localAddr, localUDPAddr, localAddr) | ||
} | ||
|
||
return localUDPAddr, nil | ||
} | ||
|
||
// ParseICMP takes in an IPv4 header and payload and tries to convert to an ICMP | ||
// message, it returns all the fields from the packet we need to validate it's the response | ||
// we're looking for | ||
func ParseICMP(header *ipv4.Header, payload []byte) (*ICMPResponse, error) { | ||
// in addition to parsing, it is probably not a bad idea to do some validation | ||
// so we can ignore the ICMP packets we don't care about | ||
icmpResponse := ICMPResponse{} | ||
|
||
if header.Protocol != IPProtoICMP || header.Version != 4 || | ||
header.Src == nil || header.Dst == nil { | ||
return nil, fmt.Errorf("invalid IP header for ICMP packet: %+v", header) | ||
} | ||
icmpResponse.SrcIP = header.Src | ||
icmpResponse.DstIP = header.Dst | ||
|
||
var icmpv4Layer layers.ICMPv4 | ||
decoded := []gopacket.LayerType{} | ||
icmpParser := gopacket.NewDecodingLayerParser(layers.LayerTypeICMPv4, &icmpv4Layer) | ||
icmpParser.IgnoreUnsupported = true // ignore unsupported layers, we will decode them in the next step | ||
if err := icmpParser.DecodeLayers(payload, &decoded); err != nil { | ||
return nil, fmt.Errorf("failed to decode ICMP packet: %w", err) | ||
} | ||
// since we ignore unsupported layers, we need to check if we actually decoded | ||
// anything | ||
if len(decoded) < 1 { | ||
return nil, fmt.Errorf("failed to decode ICMP packet, no layers decoded") | ||
} | ||
icmpResponse.TypeCode = icmpv4Layer.TypeCode | ||
|
||
var icmpPayload []byte | ||
if len(icmpv4Layer.Payload) < 40 { | ||
log.Tracef("Payload length %d is less than 40, extending...\n", len(icmpv4Layer.Payload)) | ||
icmpPayload = make([]byte, 40) | ||
copy(icmpPayload, icmpv4Layer.Payload) | ||
// we have to set this in order for the TCP | ||
// parser to work | ||
icmpPayload[32] = 5 << 4 // set data offset | ||
} else { | ||
icmpPayload = icmpv4Layer.Payload | ||
} | ||
|
||
// a separate parser is needed to decode the inner IP and TCP headers because | ||
// gopacket doesn't support this type of nesting in a single decoder | ||
var innerIPLayer layers.IPv4 | ||
var innerTCPLayer layers.TCP | ||
innerIPParser := gopacket.NewDecodingLayerParser(layers.LayerTypeIPv4, &innerIPLayer, &innerTCPLayer) | ||
if err := innerIPParser.DecodeLayers(icmpPayload, &decoded); err != nil { | ||
return nil, fmt.Errorf("failed to decode inner ICMP payload: %w", err) | ||
} | ||
icmpResponse.InnerSrcIP = innerIPLayer.SrcIP | ||
icmpResponse.InnerDstIP = innerIPLayer.DstIP | ||
icmpResponse.InnerSrcPort = uint16(innerTCPLayer.SrcPort) | ||
icmpResponse.InnerDstPort = uint16(innerTCPLayer.DstPort) | ||
icmpResponse.InnerSeqNum = innerTCPLayer.Seq | ||
|
||
return &icmpResponse, nil | ||
} | ||
|
||
// ICMPMatch checks if an ICMP response matches the expected response | ||
// based on the local and remote IP, port, and sequence number | ||
func ICMPMatch(localIP net.IP, localPort uint16, remoteIP net.IP, remotePort uint16, seqNum uint32, response *ICMPResponse) bool { | ||
return localIP.Equal(response.InnerSrcIP) && | ||
remoteIP.Equal(response.InnerDstIP) && | ||
localPort == response.InnerSrcPort && | ||
remotePort == response.InnerDstPort && | ||
seqNum == response.InnerSeqNum | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016-present Datadog, Inc. | ||
|
||
//go:build test | ||
|
||
package common | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
"github.com/DataDog/datadog-agent/pkg/networkpath/traceroute/testutils" | ||
"github.com/google/gopacket/layers" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"golang.org/x/net/ipv4" | ||
) | ||
|
||
var ( | ||
srcIP = net.ParseIP("1.2.3.4") | ||
dstIP = net.ParseIP("5.6.7.8") | ||
|
||
innerSrcIP = net.ParseIP("10.0.0.1") | ||
innerDstIP = net.ParseIP("192.168.1.1") | ||
) | ||
|
||
func Test_parseICMP(t *testing.T) { | ||
ipv4Header := testutils.CreateMockIPv4Header(srcIP, dstIP, 1) | ||
icmpLayer := testutils.CreateMockICMPLayer(layers.ICMPv4CodeTTLExceeded) | ||
innerIPv4Layer := testutils.CreateMockIPv4Layer(innerSrcIP, innerDstIP, layers.IPProtocolTCP) | ||
innerTCPLayer := testutils.CreateMockTCPLayer(12345, 443, 28394, 12737, true, true, true) | ||
|
||
tt := []struct { | ||
description string | ||
inHeader *ipv4.Header | ||
inPayload []byte | ||
expected *ICMPResponse | ||
errMsg string | ||
}{ | ||
{ | ||
description: "empty IPv4 layer should return an error", | ||
inHeader: &ipv4.Header{}, | ||
inPayload: []byte{}, | ||
expected: nil, | ||
errMsg: "invalid IP header for ICMP packet", | ||
}, | ||
{ | ||
description: "missing ICMP layer should return an error", | ||
inHeader: ipv4Header, | ||
inPayload: []byte{}, | ||
expected: nil, | ||
errMsg: "failed to decode ICMP packet", | ||
}, | ||
{ | ||
description: "missing inner layers should return an error", | ||
inHeader: ipv4Header, | ||
inPayload: testutils.CreateMockICMPPacket(nil, icmpLayer, nil, nil, false), | ||
expected: nil, | ||
errMsg: "failed to decode inner ICMP payload", | ||
}, | ||
{ | ||
description: "ICMP packet with partial TCP header should create icmpResponse", | ||
inHeader: ipv4Header, | ||
inPayload: testutils.CreateMockICMPPacket(nil, icmpLayer, innerIPv4Layer, innerTCPLayer, true), | ||
expected: &ICMPResponse{ | ||
SrcIP: srcIP, | ||
DstIP: dstIP, | ||
InnerSrcIP: innerSrcIP, | ||
InnerDstIP: innerDstIP, | ||
InnerSrcPort: 12345, | ||
InnerDstPort: 443, | ||
InnerSeqNum: 28394, | ||
}, | ||
errMsg: "", | ||
}, | ||
{ | ||
description: "full ICMP packet should create icmpResponse", | ||
inHeader: ipv4Header, | ||
inPayload: testutils.CreateMockICMPPacket(nil, icmpLayer, innerIPv4Layer, innerTCPLayer, true), | ||
expected: &ICMPResponse{ | ||
SrcIP: srcIP, | ||
DstIP: dstIP, | ||
InnerSrcIP: innerSrcIP, | ||
InnerDstIP: innerDstIP, | ||
InnerSrcPort: 12345, | ||
InnerDstPort: 443, | ||
InnerSeqNum: 28394, | ||
}, | ||
errMsg: "", | ||
}, | ||
} | ||
|
||
for _, test := range tt { | ||
t.Run(test.description, func(t *testing.T) { | ||
actual, err := ParseICMP(test.inHeader, test.inPayload) | ||
if test.errMsg != "" { | ||
require.Error(t, err) | ||
assert.Contains(t, err.Error(), test.errMsg) | ||
assert.Nil(t, actual) | ||
return | ||
} | ||
require.Nil(t, err) | ||
require.NotNil(t, actual) | ||
// assert.Equal doesn't handle net.IP well | ||
assert.Equal(t, testutils.StructFieldCount(test.expected), testutils.StructFieldCount(actual)) | ||
assert.Truef(t, test.expected.SrcIP.Equal(actual.SrcIP), "mismatch source IPs: expected %s, got %s", test.expected.SrcIP.String(), actual.SrcIP.String()) | ||
assert.Truef(t, test.expected.DstIP.Equal(actual.DstIP), "mismatch dest IPs: expected %s, got %s", test.expected.DstIP.String(), actual.DstIP.String()) | ||
assert.Truef(t, test.expected.InnerSrcIP.Equal(actual.InnerSrcIP), "mismatch inner source IPs: expected %s, got %s", test.expected.InnerSrcIP.String(), actual.InnerSrcIP.String()) | ||
assert.Truef(t, test.expected.InnerDstIP.Equal(actual.InnerDstIP), "mismatch inner dest IPs: expected %s, got %s", test.expected.InnerDstIP.String(), actual.InnerDstIP.String()) | ||
assert.Equal(t, test.expected.InnerSrcPort, actual.InnerSrcPort) | ||
assert.Equal(t, test.expected.InnerDstPort, actual.InnerDstPort) | ||
assert.Equal(t, test.expected.InnerSeqNum, actual.InnerSeqNum) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.