-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpacketprocessor_test.go
110 lines (93 loc) · 2.25 KB
/
packetprocessor_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
package netutils
import (
"os"
"testing"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcapgo"
)
func Test_IpDefrag(t *testing.T) {
tests := []struct {
name string
pcapFile string
nbPackets int
}{
{
name: "DNS UDP with IPv4 Fragmented",
pcapFile: "./pcap/dnsdump_ip4_fragmented+udp.pcap",
nbPackets: 2,
},
{
name: "DNS UDP with IPv6 Fragmented",
pcapFile: "./pcap/dnsdump_ip6_fragmented+udp.pcap",
nbPackets: 2,
},
}
done := make(chan bool)
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
f, err := os.Open(tc.pcapFile)
if err != nil {
t.Errorf("unable to open file: %s", err)
return
}
defer f.Close()
pcapHandler, err := pcapgo.NewReader(f)
if err != nil {
t.Errorf("unable to open pcap file: %s", err)
return
}
fragIP4Chan := make(chan gopacket.Packet)
fragIP6Chan := make(chan gopacket.Packet)
outputChan := make(chan gopacket.Packet, 2)
// defrag ipv4
go IPDefragger(fragIP4Chan, outputChan, outputChan, 53)
// defrag ipv6
go IPDefragger(fragIP6Chan, outputChan, outputChan, 53)
packetSource := gopacket.NewPacketSource(pcapHandler, pcapHandler.LinkType())
packetSource.DecodeOptions.Lazy = true
nbPackets := 0
timeout := time.After(1 * time.Second)
go func() {
for {
select {
case <-outputChan:
nbPackets++
case <-timeout:
goto STOP
}
}
STOP:
done <- true
}()
for {
packet, err := packetSource.NextPacket()
if err != nil {
break
}
// ipv4 fragmented packet ?
if packet.NetworkLayer().LayerType() == layers.LayerTypeIPv4 {
ip4 := packet.NetworkLayer().(*layers.IPv4)
if ip4.Flags&layers.IPv4MoreFragments == 1 || ip4.FragOffset > 0 {
fragIP4Chan <- packet
} else {
outputChan <- packet
}
}
if packet.NetworkLayer().LayerType() == layers.LayerTypeIPv6 {
v6frag := packet.Layer(layers.LayerTypeIPv6Fragment)
if v6frag != nil {
fragIP6Chan <- packet
} else {
outputChan <- packet
}
}
}
<-done
if nbPackets != tc.nbPackets {
t.Errorf("bad number of packets, wants: %d, got: %d", tc.nbPackets, nbPackets)
}
})
}
}