From: Joshua Callary <62225867+Rinzler1011@users.noreply.github.com>
---
pcap/parser.go | 87 +++++++++++++++++++++++++++++
pcap/parser_test.go | 27 +++++++++
pcap/testing/srt_capture_test.pcap | Bin 0 -> 85 bytes
3 files changed, 114 insertions(+)
create mode 100644 pcap/parser.go
create mode 100644 pcap/parser_test.go
create mode 100644 pcap/testing/srt_capture_test.pcap
diff --git a/pcap/parser.go b/pcap/parser.go
new file mode 100644
index 0000000..a35ebc6
--- /dev/null
+++ b/pcap/parser.go
@@ -0,0 +1,87 @@
+package pcap
+
+import (
+ "encoding/binary"
+ "errors"
+ "io"
+ "os"
+)
+
+type PcapHeader struct {
+ MagicNumber uint32
+ VersionMajor uint16
+ VersionMinor uint16
+ ThisZone int32
+ SigFigs uint32
+ SnapLen uint32
+ Network uint32
+}
+
+type PacketHeader struct {
+ TsSec uint32
+ TsUsec uint32
+ InclLen uint32
+ OrigLen uint32
+}
+
+type Packet struct {
+ Header PacketHeader
+ Data []byte
+}
+
+type PcapFile struct {
+ Header PcapHeader
+ Packets []Packet
+}
+
+func ReadPcapFile(filePath string) (*PcapFile, error) {
+ file, err := os.Open(filePath)
+ if err != nil {
+ return nil, err
+ }
+ defer file.Close()
+
+ return readPcap(file)
+}
+
+func readPcap(reader io.Reader) (*PcapFile, error) {
+ var header PcapHeader
+ err := binary.Read(reader, binary.LittleEndian, &header)
+
+ if err != nil {
+ return nil, err
+ }
+
+ if header.MagicNumber != 0xa1b2c3d4 && header.MagicNumber != 0xd4c3b2a1 {
+ return nil, errors.New("invalid magic number in pcap file")
+ }
+
+ var packets []Packet
+
+ for {
+ var packetHeader PacketHeader
+ err := binary.Read(reader, binary.LittleEndian, &packetHeader)
+ if err == io.EOF {
+ break
+ }
+ if err != nil {
+ return nil, err
+ }
+
+ data := make([]byte, packetHeader.InclLen)
+ _, err = io.ReadFull(reader, data)
+ if err != nil {
+ return nil, err
+ }
+
+ packets = append(packets, Packet{
+ Header: packetHeader,
+ Data: data,
+ })
+ }
+
+ return &PcapFile{
+ Header: header,
+ Packets: packets,
+ }, nil
+}
diff --git a/pcap/parser_test.go b/pcap/parser_test.go
new file mode 100644
index 0000000..089b838
--- /dev/null
+++ b/pcap/parser_test.go
@@ -0,0 +1,27 @@
+package pcap
+
+import (
+ "fmt"
+ "testing"
+)
+
+func TestReadPcapFile(t *testing.T) {
+ filePath := "testing/srt_capture_test.pcap"
+
+ pcapFile, err := ReadPcapFile(filePath)
+ if err != nil {
+ t.Fatalf("Failed to read PCAP file: %v", err)
+ }
+
+ if len(pcapFile.Packets) == 0 {
+ t.Fatal("Should be at least one packet but there is 0")
+ }
+
+ fmt.Printf("PCAP Header: %+v\n", pcapFile.Header)
+
+ for i, packet := range pcapFile.Packets {
+ fmt.Printf("Packet %d: %+v\n", i, packet.Header)
+
+ fmt.Printf("Data %d: %x\n", i, packet.Data)
+ }
+}
diff --git a/pcap/testing/srt_capture_test.pcap b/pcap/testing/srt_capture_test.pcap
new file mode 100644
index 0000000000000000000000000000000000000000..03ae76967451f36caa6c4ea4771de6b4363d56fc
GIT binary patch
literal 85
zcmca|c+)~A1{MYc-~cmSbY`a2DKInW0(l_J1jMck44SJJF)%m?GBDILFffAfsW)PJ
V45I%uLQ;!M6mnCGixbmRxd25<5L5sF
literal 0
HcmV?d00001
--
2.39.2 (Apple Git-143)
On Fri Jul 19, 2024 at 10:25 AM UTC, Joshua Callary wrote:
> From: Joshua Callary <62225867+Rinzler1011@users.noreply.github.com>
> +type PcapHeader struct {
We are within the PCAP package, so we can avoid possible stuttering and
name things as they are here, thus this can become `Header`. This
comment applies throughout.
> +func ReadPcapFile(filePath string) (*PcapFile, error) {
This func isn't needed, readPcap (as it is currently called) takes
an io.Reader. It is more idiomatic to open the file then pass to the
Reader, without a helper.
> +func readPcap(reader io.Reader) (*PcapFile, error) {
I think this would be better named as Decode
-g
>> +type PcapHeader struct {
> We are within the PCAP package, so we can avoid possible stuttering and
> name things as they are here, thus this can become `Header`. This
> comment applies throughout.
>
>> +func ReadPcapFile(filePath string) (*PcapFile, error) {
> This func isn't needed, readPcap (as it is currently called) takes
> an io.Reader. It is more idiomatic to open the file then pass to the
> Reader, without a helper.
>
>> +func readPcap(reader io.Reader) (*PcapFile, error) {
> I think this would be better named as Decode
>
> -g
Thanks Gav! Agreed.
Josh, here are some helpful links:
- https://go.dev/doc/effective_go
- https://go.dev/wiki/CodeReviewComments
- https://google.github.io/styleguide/go/decisions
I saw you submitted a new change
https://lists.sr.ht/~otl/untangledco/%3C20240719152925.69019-1-joshuacallary@miraitechnologies.com.au%3E
What I've done is squashed the two changes into one, since your second
change is really just a revision of the first. I wanted to get your
good work in rather than bog you down with requests. Just something to
keep in mind for next time :)
You can use the --amend flag of git commit to revise your previous
commit. For example after going through Gavin-John's comments, instead
of making a new commit, we can run the command:
git commit --amend
to revise the previous commit.
Thanks again for submitting :) Welcome to the whacky open source
world! ;)