-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rudimentary PCAP and binary TPM taps
- Loading branch information
Showing
8 changed files
with
262 additions
and
23 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package debug | ||
|
||
import ( | ||
"io" | ||
"sync" | ||
) | ||
|
||
type binTap struct { | ||
sync.Mutex | ||
w io.Writer | ||
} | ||
|
||
func (t *binTap) Rx() io.Writer { | ||
return t.w | ||
} | ||
|
||
func (t *binTap) Tx() io.Writer { | ||
return t.w | ||
} | ||
|
||
func NewBinTap(w io.Writer) Tap { | ||
return &binTap{w: w} | ||
} | ||
|
||
func (w *binTap) Write(data []byte) (int, error) { | ||
w.Lock() | ||
defer w.Unlock() | ||
|
||
return w.w.Write(data) | ||
} |
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,146 @@ | ||
package debug | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net" | ||
"sync" | ||
"time" | ||
|
||
"github.com/gopacket/gopacket" | ||
"github.com/gopacket/gopacket/layers" | ||
"github.com/gopacket/gopacket/pcapgo" | ||
) | ||
|
||
type pcapTap struct { | ||
in *pcapWriter | ||
out *pcapWriter | ||
} | ||
|
||
func (t *pcapTap) Rx() io.Writer { | ||
return t.in | ||
} | ||
|
||
func (t *pcapTap) Tx() io.Writer { | ||
return t.out | ||
} | ||
|
||
func NewPcapTap(w io.Writer) (Tap, error) { | ||
pw := pcapgo.NewWriter(w) | ||
return &pcapTap{ | ||
in: &pcapWriter{ | ||
in: true, | ||
writer: pw, | ||
}, | ||
out: &pcapWriter{ | ||
writer: pw, | ||
}, | ||
}, nil | ||
} | ||
|
||
type pcapWriter struct { | ||
in bool | ||
writer *pcapgo.Writer | ||
} | ||
|
||
var ( | ||
outSeq uint32 | ||
inSeq uint32 | ||
mu sync.Mutex | ||
) | ||
|
||
func (w *pcapWriter) Write(data []byte) (int, error) { | ||
mu.Lock() | ||
defer mu.Unlock() | ||
|
||
err := write(w.writer, data, w.in, inSeq, outSeq) | ||
|
||
if w.in { | ||
inSeq += uint32(len(data)) | ||
} else { | ||
outSeq += uint32(len(data)) | ||
} | ||
|
||
return len(data), err | ||
} | ||
|
||
var ethernetLayer = &layers.Ethernet{ | ||
SrcMAC: net.HardwareAddr{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, | ||
DstMAC: net.HardwareAddr{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, | ||
EthernetType: layers.EthernetTypeIPv4, | ||
} | ||
|
||
var ipLayer = &layers.IPv4{ | ||
Version: 4, | ||
TTL: 64, | ||
Flags: layers.IPv4DontFragment, | ||
Protocol: layers.IPProtocolTCP, | ||
SrcIP: net.IP{127, 0, 0, 1}, | ||
DstIP: net.IP{127, 0, 0, 1}, | ||
} | ||
|
||
var serializeOptions = gopacket.SerializeOptions{ | ||
FixLengths: true, | ||
ComputeChecksums: true, | ||
} | ||
|
||
var decodeOptions = gopacket.DecodeOptions{ | ||
Lazy: true, | ||
NoCopy: true, | ||
} | ||
|
||
const snapLen = uint32(65536) | ||
|
||
var once sync.Once | ||
var headerWriteError error | ||
|
||
func write(w *pcapgo.Writer, data []byte, in bool, inSeq uint32, outSeq uint32) error { | ||
Check failure on line 97 in tpm/debug/pcap.go GitHub Actions / ci / lint / lint
|
||
var tcpLayer = &layers.TCP{Window: 16} | ||
if in { | ||
tcpLayer.SrcPort = layers.TCPPort(2321) | ||
tcpLayer.DstPort = layers.TCPPort(50001) | ||
tcpLayer.ACK = true | ||
tcpLayer.Seq = inSeq | ||
tcpLayer.Ack = outSeq | ||
tcpLayer.PSH = true | ||
} else { | ||
tcpLayer.SrcPort = layers.TCPPort(50001) | ||
tcpLayer.DstPort = layers.TCPPort(2321) | ||
tcpLayer.ACK = false | ||
tcpLayer.Seq = outSeq | ||
} | ||
|
||
tcpLayer.SetNetworkLayerForChecksum(ipLayer) | ||
buffer := gopacket.NewSerializeBuffer() | ||
if err := gopacket.SerializeLayers(buffer, serializeOptions, | ||
ethernetLayer, | ||
ipLayer, | ||
tcpLayer, | ||
gopacket.Payload(data), | ||
); err != nil { | ||
return fmt.Errorf("failed serializing layers: %w", err) | ||
} | ||
|
||
p := gopacket.NewPacket(buffer.Bytes(), layers.LayerTypeEthernet, decodeOptions) | ||
|
||
once.Do(func() { | ||
// TODO: do once on the very first write to the output; if it's a file, check there's no pcap header yet | ||
if err := w.WriteFileHeader(snapLen, layers.LinkTypeEthernet); err != nil { | ||
headerWriteError = err | ||
} | ||
}) | ||
if headerWriteError != nil { | ||
return fmt.Errorf("failed writing pcap header: %w", headerWriteError) | ||
} | ||
|
||
ci := p.Metadata().CaptureInfo | ||
ci.CaptureLength = len(p.Data()) | ||
ci.Length = ci.CaptureLength | ||
ci.Timestamp = time.Now() | ||
|
||
if err := w.WritePacket(ci, p.Data()); err != nil { | ||
return fmt.Errorf("failed writing packet data: %w", err) | ||
} | ||
|
||
return nil | ||
} |
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,55 @@ | ||
package debug | ||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
"testing" | ||
|
||
"github.com/gopacket/gopacket" | ||
"github.com/gopacket/gopacket/pcapgo" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_PcapTapWrites(t *testing.T) { | ||
var buf bytes.Buffer | ||
tr, err := NewPcapTap(&buf) | ||
require.NoError(t, err) | ||
|
||
b, err := hex.DecodeString("8001000000160000017a000000060000010600000001") | ||
require.NoError(t, err) | ||
n, err := tr.Tx().Write(b) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, 22, n) | ||
|
||
b, err = hex.DecodeString("80010000001b000000000100000006000000010000010678434720") | ||
require.NoError(t, err) | ||
n, err = tr.Rx().Write(b) | ||
require.NoError(t, err) | ||
assert.Equal(t, 27, n) | ||
|
||
b, err = hex.DecodeString("8001000000160000017a000000060000010700000001") | ||
require.NoError(t, err) | ||
n, err = tr.Tx().Write(b) | ||
require.NoError(t, err) | ||
assert.Equal(t, 22, n) | ||
|
||
b, err = hex.DecodeString("80010000001b00000000010000000600000001000001076654504d") | ||
require.NoError(t, err) | ||
n, err = tr.Rx().Write(b) | ||
require.NoError(t, err) | ||
assert.Equal(t, 27, n) | ||
|
||
r, err := pcapgo.NewReader(&buf) | ||
require.NoError(t, err) | ||
|
||
count := 0 | ||
packetSource := gopacket.NewPacketSource(r, r.LinkType()) | ||
for packet := range packetSource.Packets() { | ||
t.Log(packet.Dump()) | ||
count += 1 | ||
} | ||
|
||
require.Equal(t, 4, count) // 4 packets expected | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package debug | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
) | ||
|
||
func NewTextTap(reads io.Writer, writes io.Writer) Tap { | ||
return &tap{ | ||
in: &wrapper{reads, true}, | ||
out: &wrapper{writes, false}, | ||
} | ||
} | ||
|
||
type wrapper struct { | ||
w io.Writer | ||
in bool | ||
} | ||
|
||
func (w *wrapper) Write(data []byte) (int, error) { | ||
if w.in { | ||
return w.w.Write([]byte(fmt.Sprintf("<- %x\n", data))) | ||
} else { | ||
return w.w.Write([]byte(fmt.Sprintf("-> %x\n", data))) | ||
} | ||
} |
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