net/packet: s/ParsedPacket/Parsed/ to avoid package stuttering.

Signed-off-by: David Anderson <danderson@tailscale.com>
This commit is contained in:
David Anderson
2020-11-09 23:49:09 -08:00
parent c48253e63b
commit 093431f5dd
8 changed files with 65 additions and 65 deletions
+3 -3
View File
@@ -4,9 +4,9 @@
// Package packet contains packet parsing and marshaling utilities.
//
// ParsedPacket provides allocation-free minimal packet header
// decoding, for use in packet filtering. The other types in the
// package are for constructing and marshaling packets into []bytes.
// Parsed provides allocation-free minimal packet header decoding, for
// use in packet filtering. The other types in the package are for
// constructing and marshaling packets into []bytes.
//
// To support allocation-free parsing, this package defines IPv4 and
// IPv6 address types. You should prefer to use netaddr's types,
+15 -15
View File
@@ -29,10 +29,10 @@ var (
put32 = binary.BigEndian.PutUint32
)
// ParsedPacket is a minimal decoding of a packet suitable for use in filters.
// Parsed is a minimal decoding of a packet suitable for use in filters.
//
// In general, it only supports IPv4. The IPv6 parsing is very minimal.
type ParsedPacket struct {
type Parsed struct {
// b is the byte buffer that this decodes.
b []byte
// subofs is the offset of IP subprotocol.
@@ -55,7 +55,7 @@ type ParsedPacket struct {
// NextHeader
type NextHeader uint8
func (p *ParsedPacket) String() string {
func (p *Parsed) String() string {
if p.IPVersion == 6 {
return fmt.Sprintf("IPv6{Proto=%d}", p.IPProto)
}
@@ -108,7 +108,7 @@ func ipChecksum(b []byte) uint16 {
// It performs extremely simple packet decoding for basic IPv4 packet types.
// It extracts only the subprotocol id, IP addresses, and (if any) ports,
// and shouldn't need any memory allocation.
func (q *ParsedPacket) Decode(b []byte) {
func (q *Parsed) Decode(b []byte) {
q.b = b
if len(b) < ipHeaderLength {
@@ -224,7 +224,7 @@ func (q *ParsedPacket) Decode(b []byte) {
}
}
func (q *ParsedPacket) IPHeader() IP4Header {
func (q *Parsed) IPHeader() IP4Header {
ipid := get16(q.b[4:6])
return IP4Header{
IPID: ipid,
@@ -234,7 +234,7 @@ func (q *ParsedPacket) IPHeader() IP4Header {
}
}
func (q *ParsedPacket) ICMPHeader() ICMP4Header {
func (q *Parsed) ICMPHeader() ICMP4Header {
return ICMP4Header{
IP4Header: q.IPHeader(),
Type: ICMP4Type(q.b[q.subofs+0]),
@@ -242,7 +242,7 @@ func (q *ParsedPacket) ICMPHeader() ICMP4Header {
}
}
func (q *ParsedPacket) UDPHeader() UDP4Header {
func (q *Parsed) UDPHeader() UDP4Header {
return UDP4Header{
IP4Header: q.IPHeader(),
SrcPort: q.SrcPort,
@@ -252,37 +252,37 @@ func (q *ParsedPacket) UDPHeader() UDP4Header {
// Buffer returns the entire packet buffer.
// This is a read-only view; that is, q retains the ownership of the buffer.
func (q *ParsedPacket) Buffer() []byte {
func (q *Parsed) Buffer() []byte {
return q.b
}
// Sub returns the IP subprotocol section.
// This is a read-only view; that is, q retains the ownership of the buffer.
func (q *ParsedPacket) Sub(begin, n int) []byte {
func (q *Parsed) Sub(begin, n int) []byte {
return q.b[q.subofs+begin : q.subofs+begin+n]
}
// Payload returns the payload of the IP subprotocol section.
// This is a read-only view; that is, q retains the ownership of the buffer.
func (q *ParsedPacket) Payload() []byte {
func (q *Parsed) Payload() []byte {
return q.b[q.dataofs:q.length]
}
// Trim trims the buffer to its IPv4 length.
// Sometimes packets arrive from an interface with extra bytes on the end.
// This removes them.
func (q *ParsedPacket) Trim() []byte {
func (q *Parsed) Trim() []byte {
return q.b[:q.length]
}
// IsTCPSyn reports whether q is a TCP SYN packet
// (i.e. the first packet in a new connection).
func (q *ParsedPacket) IsTCPSyn() bool {
func (q *Parsed) IsTCPSyn() bool {
return (q.TCPFlags & TCPSynAck) == TCPSyn
}
// IsError reports whether q is an IPv4 ICMP "Error" packet.
func (q *ParsedPacket) IsError() bool {
func (q *Parsed) IsError() bool {
if q.IPProto == ICMP && len(q.b) >= q.subofs+8 {
switch ICMP4Type(q.b[q.subofs]) {
case ICMP4Unreachable, ICMP4TimeExceeded:
@@ -293,7 +293,7 @@ func (q *ParsedPacket) IsError() bool {
}
// IsEchoRequest reports whether q is an IPv4 ICMP Echo Request.
func (q *ParsedPacket) IsEchoRequest() bool {
func (q *Parsed) IsEchoRequest() bool {
if q.IPProto == ICMP && len(q.b) >= q.subofs+8 {
return ICMP4Type(q.b[q.subofs]) == ICMP4EchoRequest &&
ICMP4Code(q.b[q.subofs+1]) == ICMP4NoCode
@@ -302,7 +302,7 @@ func (q *ParsedPacket) IsEchoRequest() bool {
}
// IsEchoRequest reports whether q is an IPv4 ICMP Echo Response.
func (q *ParsedPacket) IsEchoResponse() bool {
func (q *Parsed) IsEchoResponse() bool {
if q.IPProto == ICMP && len(q.b) >= q.subofs+8 {
return ICMP4Type(q.b[q.subofs]) == ICMP4EchoReply &&
ICMP4Code(q.b[q.subofs+1]) == ICMP4NoCode
+13 -13
View File
@@ -41,7 +41,7 @@ var icmpRequestBuffer = []byte{
0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x5f, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64,
}
var icmpRequestDecode = ParsedPacket{
var icmpRequestDecode = Parsed{
b: icmpRequestBuffer,
subofs: 20,
dataofs: 24,
@@ -67,7 +67,7 @@ var icmpReplyBuffer = []byte{
0x72, 0x65, 0x70, 0x6c, 0x79, 0x5f, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64,
}
var icmpReplyDecode = ParsedPacket{
var icmpReplyDecode = Parsed{
b: icmpReplyBuffer,
subofs: 20,
dataofs: 24,
@@ -91,7 +91,7 @@ var ipv6PacketBuffer = []byte{
0x85, 0x00, 0x38, 0x04, 0x00, 0x00, 0x00, 0x00,
}
var ipv6PacketDecode = ParsedPacket{
var ipv6PacketDecode = Parsed{
b: ipv6PacketBuffer,
IPVersion: 6,
IPProto: ICMPv6,
@@ -103,7 +103,7 @@ var unknownPacketBuffer = []byte{
0x45, 0x74, 0x63, 0x70, 0x5f, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64,
}
var unknownPacketDecode = ParsedPacket{
var unknownPacketDecode = Parsed{
b: unknownPacketBuffer,
IPVersion: 0,
IPProto: Unknown,
@@ -123,7 +123,7 @@ var tcpPacketBuffer = []byte{
0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x5f, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64,
}
var tcpPacketDecode = ParsedPacket{
var tcpPacketDecode = Parsed{
b: tcpPacketBuffer,
subofs: 20,
dataofs: 40,
@@ -151,7 +151,7 @@ var udpRequestBuffer = []byte{
0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x5f, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64,
}
var udpRequestDecode = ParsedPacket{
var udpRequestDecode = Parsed{
b: udpRequestBuffer,
subofs: 20,
dataofs: 28,
@@ -178,7 +178,7 @@ var udpReplyBuffer = []byte{
0x72, 0x65, 0x70, 0x6c, 0x79, 0x5f, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64,
}
var udpReplyDecode = ParsedPacket{
var udpReplyDecode = Parsed{
b: udpReplyBuffer,
subofs: 20,
dataofs: 28,
@@ -191,10 +191,10 @@ var udpReplyDecode = ParsedPacket{
DstPort: 123,
}
func TestParsedPacket(t *testing.T) {
func TestParsed(t *testing.T) {
tests := []struct {
name string
qdecode ParsedPacket
qdecode Parsed
want string
}{
{"tcp", tcpPacketDecode, "TCP{1.2.3.4:123 > 5.6.7.8:567}"},
@@ -226,7 +226,7 @@ func TestDecode(t *testing.T) {
tests := []struct {
name string
buf []byte
want ParsedPacket
want Parsed
}{
{"icmp", icmpRequestBuffer, icmpRequestDecode},
{"ipv6", ipv6PacketBuffer, ipv6PacketDecode},
@@ -237,7 +237,7 @@ func TestDecode(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var got ParsedPacket
var got Parsed
got.Decode(tt.buf)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("mismatch\n got: %#v\nwant: %#v", got, tt.want)
@@ -246,7 +246,7 @@ func TestDecode(t *testing.T) {
}
allocs := testing.AllocsPerRun(1000, func() {
var got ParsedPacket
var got Parsed
got.Decode(tests[0].buf)
})
if allocs != 0 {
@@ -267,7 +267,7 @@ func BenchmarkDecode(b *testing.B) {
for _, bench := range benches {
b.Run(bench.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
var p ParsedPacket
var p Parsed
p.Decode(bench.buf)
}
})