forked from akrennmair/gopcap
-
Notifications
You must be signed in to change notification settings - Fork 50
/
pcap_test.go
225 lines (184 loc) · 4.65 KB
/
pcap_test.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package pcap
import (
"fmt"
"net"
"os"
"testing"
"time"
)
func TestPcap(t *testing.T) {
h, err := OpenOffline("test/pcap_files/Network_Join_Nokia_Mobile.pcap")
if h == nil {
t.Fail()
return
}
_ = err
}
type pcapNewHandleFunc func(intf string, filter string, readTo int32) (h *Pcap, err error)
func testPcapHandle(t *testing.T, newHandle pcapNewHandleFunc) {
port := 54321
h, err := newHandle("lo", fmt.Sprintf("udp dst port %d", port), 2000)
if h == nil || err != nil {
if h != nil {
h.Close()
}
t.Fatalf("Failed to create/init pcap handle err:%s", err)
}
numPkts := 5
go udpSvr(port, numPkts, t)
go udpClient(port, numPkts, 1*time.Second, t)
pktsRecvd := 0
for pkt := h.Next(); pkt != nil; pkt = h.Next() {
pkt.Decode()
t.Logf("Packet:%s dataLen:%d", pkt, len(pkt.Payload))
pktsRecvd += 1
if pktsRecvd >= numPkts {
// some platforms ignore read timeout:
// https://github.com/the-tcpdump-group/libpcap/issues/550
break
}
}
if pktsRecvd != numPkts {
t.Fatalf("Capture failed pkts-send:%d, pkts-recvd:%d", numPkts, pktsRecvd)
}
t.Logf("Successfully captured %d packets", numPkts)
}
func TestPcapCreate(t *testing.T) {
testPcapHandle(t, pcapCreate)
}
func TestPcapOpenLive(t *testing.T) {
testPcapHandle(t, pcapOpenLive)
}
func TestPcapDump(t *testing.T) {
port := 54321
h, err := pcapOpenLive("lo", fmt.Sprintf("udp dst port %d", port), 2000)
if h == nil || err != nil {
if h != nil {
h.Close()
}
t.Fatalf("Failed to create/init pcap handle err:%s", err)
}
ofile := "test/pcap_files/dump_test.pcap"
d, err := h.DumpOpen(&ofile)
if d == nil || err != nil {
return
}
numPkts := 5
go udpSvr(port, numPkts, t)
go udpClient(port, numPkts, 1*time.Second, t)
r, err := h.PcapLoop(5, d)
if r < 0 || err != nil {
return
}
h.PcapDumpClose(d)
h.Close()
newh, err := OpenOffline(ofile)
if newh == nil {
t.Fatalf("Failed to open pcap:%s", err)
return
}
t.Log("Successfully open pcap")
pktsRecvd := 0
for pkt := newh.Next(); pkt != nil; pkt = newh.Next() {
pkt.Decode()
t.Logf("Packet:%s dataLen:%d", pkt, len(pkt.Payload))
pktsRecvd += 1
if pktsRecvd >= numPkts {
// some platforms ignore read timeout:
// https://github.com/the-tcpdump-group/libpcap/issues/550
break
}
}
newh.Close()
if pktsRecvd != numPkts {
t.Fatalf("Capture failed pkts-send:%d, pkts-recvd:%d", numPkts, pktsRecvd)
}
t.Logf("Successfully captured %d packets", numPkts)
err = os.Remove(ofile)
if err != nil {
t.Fatalf("Failed to remote pcap file: %s", err)
}
return
}
func pcapCreate(intf string, filter string, readTo int32) (h *Pcap, err error) {
h, err = Create("lo")
if h == nil || err != nil {
return
}
err = h.SetSnapLen(65535)
if err != nil {
return
}
err = h.SetReadTimeout(readTo)
if err != nil {
return
}
err = h.SetBufferSize(3 * 1024 * 1024)
if err != nil {
return
}
err = h.Activate()
if err != nil {
return
}
err = h.SetFilter(filter)
if err != nil {
return
}
return
}
func pcapOpenLive(intf string, filter string, readTo int32) (h *Pcap, err error) {
h, err = OpenLive(intf, 65535, true, readTo)
if h == nil || err != nil {
return
}
err = h.SetFilter(filter)
if err != nil {
return
}
return
}
// Udp client which sends a fixed num of packets to given port after a fixed delay.
// Delay ensures that capture code is ready to recv packets.
func udpClient(port int, numPkts int, wait time.Duration, t *testing.T) {
time.Sleep(wait)
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("127.0.0.1:%d", port))
if err != nil {
t.Logf("ERROR Failed to resolve udp addr err:%s", err)
return
}
conn, err := net.DialUDP("udp", nil, addr)
if err != nil {
t.Logf("ERROR Failed to dial udp port:%d err:%s", port, err)
return
}
t.Logf("Start packets to port:%d", port)
pkt := []byte("hello")
for i := 0; i < numPkts; i++ {
if l, err := conn.Write(pkt); err != nil || l != len(pkt) {
t.Logf("ERROR Failed to send packet size:%d wlen:%d err:%s", len(pkt), l, err)
}
}
t.Logf("Completed sending packets to port:%d", port)
}
// Udp server which listens on a port and recvs a fixed number of packets.
func udpSvr(port int, numPkts int, t *testing.T) {
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("localhost:%d", port))
if err != nil {
t.Logf("ERROR UDP Server: failed to resolve udp addr err:%s", err)
return
}
sock, err := net.ListenUDP("udp", addr)
if err != nil {
t.Logf("ERROR UDP Server: failed to listen on port:%d err:%s", port, err)
return
}
buf := make([]byte, 10, 1024)
for i := 0; i < numPkts; i++ {
_, _, err := sock.ReadFromUDP(buf)
if err != nil {
t.Logf("Failed to recv packets on port:%d err:%s", port, err)
}
}
sock.Close()
}