Skip to content

Commit

Permalink
refactor(pcap): switch to pcapgo
Browse files Browse the repository at this point in the history
  • Loading branch information
haruue committed May 8, 2024
1 parent 8cab86b commit 7456e59
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions io/pcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ package io
import (
"context"
"hash/crc32"
"io"
"net"
"os"
"sort"
"strings"
"time"

"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
"github.com/google/gopacket/pcapgo"
)

var _ PacketIO = (*pcapPacketIO)(nil)

type pcapPacketIO struct {
pcap *pcap.Handle
pcapFile io.ReadCloser
pcap *pcapgo.Reader
lastTime *time.Time
ioCancel context.CancelFunc
config PcapPacketIOConfig
Expand All @@ -29,12 +32,18 @@ type PcapPacketIOConfig struct {
}

func NewPcapPacketIO(config PcapPacketIOConfig) (PacketIO, error) {
handle, err := pcap.OpenOffline(config.PcapFile)
pcapFile, err := os.Open(config.PcapFile)
if err != nil {
return nil, err
}

handle, err := pcapgo.NewReader(pcapFile)
if err != nil {
return nil, err
}

return &pcapPacketIO{
pcapFile: pcapFile,
pcap: handle,
lastTime: nil,
ioCancel: nil,
Expand Down Expand Up @@ -87,8 +96,7 @@ func (p *pcapPacketIO) SetCancelFunc(cancelFunc context.CancelFunc) error {
}

func (p *pcapPacketIO) Close() error {
p.pcap.Close()
return nil
return p.pcapFile.Close()
}

// Intentionally slow down the replay
Expand Down

0 comments on commit 7456e59

Please sign in to comment.