diff --git a/Makefile b/Makefile index e2549f8..b65c825 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,5 @@ TARGETS += netarch +TARGETS += puniq GOPATH = $(CURDIR) export GOPATH @@ -10,4 +11,5 @@ test: go test $(TARGETS) clean: - go clean + go clean $(TARGETS) + rm -f $(TARGETS) diff --git a/src/netarch/hd_test.go b/src/netarch/hd_test.go index 6bc3476..561686e 100644 --- a/src/netarch/hd_test.go +++ b/src/netarch/hd_test.go @@ -3,7 +3,7 @@ package netarch import ( ) -func ExampleStringDump() { +func ExampleDumpString() { DumpString("hello world, I can write!") // Output: // 00000000 68 65 6c 6c 6f 20 77 6f 72 6c 64 2c 20 49 20 63 ┆hello world, I c┆ @@ -11,7 +11,7 @@ func ExampleStringDump() { // 00000019 } -func ExampleGapstrDump() { +func ExampleDumpGapstr() { g := new(Gapstr) g = g.AppendString("Hell") g = g.AppendGap(3) diff --git a/src/pcap/example.pcap b/src/pcap/example.pcap new file mode 100644 index 0000000..931b43b Binary files /dev/null and b/src/pcap/example.pcap differ diff --git a/src/pcap/pcap_test.go b/src/pcap/pcap_test.go new file mode 100644 index 0000000..1602bcf --- /dev/null +++ b/src/pcap/pcap_test.go @@ -0,0 +1,83 @@ +package pcap + +import ( + "bytes" + "io" + "math/rand" + "os" + "testing" + "time" +) + +func randomString(prng *rand.Rand, length int) string { + var b bytes.Buffer + + for i := 0; i < length; i += 1 { + b.WriteByte(byte(prng.Int() % 256)) + } + return b.String() +} + +// Just read in a file, shouldn't be any errors. +func TestReader(t *testing.T) { + f, err := os.Open("example.pcap") + if err != nil { + t.Fatal(err) + } + defer f.Close() + + pin, err := NewReader(f) + if err != nil { + t.Fatal(err) + } + + for { + frame, err := pin.Read() + if err == io.EOF { + break + } + if err != nil { + t.Fatal(err) + } + + if len(frame.Payload) > 2000 { + t.Error("Unusually large payload") + } + } +} + +func TestReadWrite(t *testing.T) { + prng := rand.New(rand.NewSource(time.Now().Unix())) + frames := make([]Frame, 20) + + outf := new(bytes.Buffer) + pout, err := NewWriter(outf) + if err != nil { + t.Fatal(err) + } + + for _, f := range frames { + f.Header.Sec = prng.Uint32() + f.Header.Usec = prng.Uint32() + f.Header.Caplen = prng.Uint32() + f.Header.Framelen = prng.Uint32() + f.Payload = randomString(prng, prng.Intn(400)) + + pout.WriteFrame(f) + } + + pin, err := NewReader(outf) + if err != nil { + t.Fatal(err) + } + for _, f := range frames { + ff, err := pin.Read() + if err != nil { + t.Fatal(err) + } + + if *ff != f { + t.Fatal(ff, f) + } + } +} diff --git a/src/puniq/puniq.go b/src/puniq/puniq.go new file mode 100644 index 0000000..8ca8f1e --- /dev/null +++ b/src/puniq/puniq.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + "os" + "pcap" +) + +func main() { + r, err := pcap.NewReader(os.Stdin) + if err != nil { + panic(err) + } + + w, err := pcap.NewWriter(os.Stdout) + if err != nil { + panic(err) + } + + fmt.Println(r.Header) + + var lastframe *pcap.Frame = nil + for { + frame, err := r.Read() + if err != nil { + panic(err) + } + if frame == nil { + break + } + + if (lastframe != frame) { + err = w.WriteFrame(*frame) + if err != nil { + panic(err) + } + + lastframe = frame + } + } +}