-
Notifications
You must be signed in to change notification settings - Fork 1
/
packetcapture.go
42 lines (37 loc) · 1.01 KB
/
packetcapture.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package ezstack
// captures ZNP frames and writes them to a textual log file so you can reverse-engineer
// communications / capture messages for use with tests.
//
// TODO: research if we can utilize pcap-ng format so the packets could be opened in Wireshark?
import (
"context"
"fmt"
"os"
"time"
"github.com/function61/hautomo/pkg/ezstack/znp"
)
func runPacketCapture(ctx context.Context, packetCaptureFilename string, networkProcessor *znp.Znp) error {
packetCaptureFile, err := os.Create(packetCaptureFilename)
if err != nil {
return err
}
defer packetCaptureFile.Close()
for {
select {
case <-ctx.Done():
return packetCaptureFile.Close() // double close intentional
case frame := <-networkProcessor.InFramesLog():
if _, err := fmt.Fprintf(
packetCaptureFile,
"%s CommandType=%d Subsystem=%d Command=%d Payload=%x\n",
time.Now().Format(time.RFC3339Nano),
frame.CommandType,
frame.Subsystem,
frame.Command,
frame.Payload,
); err != nil {
return err
}
}
}
}