-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.go
95 lines (74 loc) · 1.96 KB
/
input.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package tzsp
import (
"errors"
"net"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
)
type Decoder struct {
packetSource *gopacket.PacketSource
handle *pcap.Handle
conn *net.UDPConn
}
func NewFromNetwork() (*Decoder, error) {
conn, err := net.ListenUDP("udp", &net.UDPAddr{
Port: 37008,
IP: net.ParseIP("0.0.0.0"),
})
if err != nil {
return nil, err
}
return &Decoder{conn: conn}, nil
}
func NewFromFile(fname string) (*Decoder, error) {
handle, err := pcap.OpenOffline(fname)
if err != nil {
return nil, err
}
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
return &Decoder{packetSource: packetSource, handle: handle}, nil
}
func (tzsp *Decoder) Next() (*Frame, error) {
var rlen int
var err error
var msg []byte
frame := Frame{}
if tzsp.conn != nil {
msg2 := make([]byte, 1600)
rlen, _, err = tzsp.conn.ReadFromUDP(msg2)
if err != nil {
return nil, err
}
msg = msg2[:rlen]
} else {
packet, err := tzsp.packetSource.NextPacket()
if err != nil {
return nil, err
}
frame["timestamp"] = packet.Metadata().Timestamp
udpLayer := packet.Layer(layers.LayerTypeUDP)
if udpLayer == nil {
return nil, errors.New("packet has no UDP layer (fragment?)")
}
udp := udpLayer.(*layers.UDP)
if udp.DstPort != 37008 {
return nil, errors.New("not TZSP packet")
}
app := packet.ApplicationLayer()
if app == nil {
return nil, errors.New("decode failure (application layer)")
}
errL := packet.ErrorLayer()
if errL != nil {
return nil, errors.New("decode failure (error layer)")
}
msg = app.Payload()
}
offset, err := frame.DecodeTZSP(msg)
if err != nil {
return nil, err
}
frame.DecodeIEEE80211(msg[offset:])
return &frame, nil
}