-
Notifications
You must be signed in to change notification settings - Fork 18
/
pcap.go
183 lines (156 loc) · 4.7 KB
/
pcap.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package iex
import (
"bufio"
"compress/gzip"
"encoding/binary"
"io"
"net"
"github.com/google/gopacket"
"github.com/google/gopacket/pcapgo"
"github.com/timpalpant/go-iex/iextp"
_ "github.com/timpalpant/go-iex/iextp/deep"
_ "github.com/timpalpant/go-iex/iextp/tops"
)
const (
magicGzip1 = 0x1f
magicGzip2 = 0x8b
pcapNGMagic uint32 = 0x0A0D0D0A
maxDatagramSize = 65536
)
// PacketDataSource represents a source of decoded network packets
// from a pcap dump or live network connection.
type PacketDataSource interface {
// NextPayload returns the next decoded packet payload.
//
// NOTE: The underlying byte array may be reused in
// subsequent calls to NextPayload.
NextPayload() ([]byte, error)
}
// DEPRECATED: Use NewPacketConnDataSource or NewPcapDataSource.
func NewPacketDataSource(r io.Reader) (PacketDataSource, error) {
// Check for live-streaming packet connection.
if conn, ok := r.(net.PacketConn); ok {
return NewPacketConnDataSource(conn), nil
}
// Otherwise it must be data from a pcap or pcap-ng dump.
return NewPcapDataSource(r)
}
// PacketConnDataSource implements PacketDataSource for live UDP
// data connections that implement net.PacketConn.
type PacketConnDataSource struct {
conn net.PacketConn
buf []byte
}
// NewPacketConnDataSource creates a new PacketConnDataSource
// from the given net.PacketConn.
func NewPacketConnDataSource(conn net.PacketConn) *PacketConnDataSource {
return &PacketConnDataSource{
conn: conn,
buf: make([]byte, maxDatagramSize),
}
}
// NextPayload implements PacketDataSource.
func (pcds *PacketConnDataSource) NextPayload() ([]byte, error) {
n, _, err := pcds.conn.ReadFrom(pcds.buf)
return pcds.buf[:n], err
}
// GopacketDataSource implements PacketDataSource for gopacket.PacketSource.
// It can be used to source the packet payload data from a pcap or pcap-ng file.
type GopacketDataSource struct {
packetSource *gopacket.PacketSource
}
func NewGopacketDataSource(packetSource *gopacket.PacketSource) *GopacketDataSource {
return &GopacketDataSource{packetSource}
}
// Create a new GopacketDataSource from the given pcap or pcap-ng file data.
func NewPcapDataSource(r io.Reader) (*GopacketDataSource, error) {
input := bufio.NewReader(r)
gzipMagic, err := input.Peek(2)
if err != nil {
return nil, err
}
if gzipMagic[0] == magicGzip1 && gzipMagic[1] == magicGzip2 {
if gzf, err := gzip.NewReader(input); err != nil {
return nil, err
} else {
input = bufio.NewReader(gzf)
}
}
magicBuf, err := input.Peek(4)
if err != nil {
return nil, err
}
magic := binary.LittleEndian.Uint32(magicBuf)
var packetSource *gopacket.PacketSource
if magic == pcapNGMagic {
packetReader, err := pcapgo.NewNgReader(input, pcapgo.DefaultNgReaderOptions)
if err != nil {
return nil, err
}
packetSource = gopacket.NewPacketSource(packetReader, packetReader.LinkType())
} else {
packetReader, err := pcapgo.NewReader(input)
if err != nil {
return nil, err
}
packetSource = gopacket.NewPacketSource(packetReader, packetReader.LinkType())
}
return NewGopacketDataSource(packetSource), nil
}
// NextPayload implements PacketDataSource.
func (gds *GopacketDataSource) NextPayload() ([]byte, error) {
for {
packet, err := gds.packetSource.NextPacket()
if err != nil {
return nil, err
}
if app := packet.ApplicationLayer(); app != nil {
return app.Payload(), nil
}
}
}
// PcapScanner is a high-level reader for iterating through messages from
// from IEX pcap dumps or streaming UDP connections.
type PcapScanner struct {
packetSource PacketDataSource
currentSegment []iextp.Message
currentMsgIndex int
}
// Create a new PcapScanner with the given source of network packets.
func NewPcapScanner(packetDataSource PacketDataSource) *PcapScanner {
return &PcapScanner{
packetSource: packetDataSource,
}
}
// Get the next Message in the pcap dump.
// Returns io.EOF if the underlying packet source has no more data.
func (p *PcapScanner) NextMessage() (iextp.Message, error) {
for p.currentMsgIndex >= len(p.currentSegment) {
if err := p.nextSegment(); err != nil {
return nil, err
}
}
msg := p.currentSegment[p.currentMsgIndex]
p.currentMsgIndex++
return msg, nil
}
// Read packets until we find the next one with > 0 messages.
// Returns an error if the underlying packet source returns an error,
// or if the payload cannot be decoded as an IEX-TP segment.
func (p *PcapScanner) nextSegment() error {
for {
payload, err := p.packetSource.NextPayload()
if err != nil {
return err
}
segment := iextp.Segment{}
if err := segment.Unmarshal(payload); err != nil {
return err
}
if len(segment.Messages) != 0 {
p.currentSegment = segment.Messages
p.currentMsgIndex = 0
return nil
}
}
}