-
Notifications
You must be signed in to change notification settings - Fork 3
/
ipdefrag.go
74 lines (66 loc) · 1.58 KB
/
ipdefrag.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
package dnszeppelin
import (
"time"
"github.com/google/gopacket/ip4defrag"
"github.com/google/gopacket/layers"
"github.com/niclabs/dnszeppelin/ip6defrag"
)
type ipv4ToDefrag struct {
ip layers.IPv4
timestamp time.Time
}
type ipv4Defragged struct {
ip layers.IPv4
timestamp time.Time
}
type ipv6FragmentInfo struct {
ip layers.IPv6
ipFragment layers.IPv6Fragment
timestamp time.Time
}
type ipv6Defragged struct {
ip layers.IPv6
timestamp time.Time
}
func ipv4Defragger(ipInput <-chan ipv4ToDefrag, ipOut chan ipv4Defragged, gcTime time.Duration, done chan bool) {
ipv4Defragger := ip4defrag.NewIPv4Defragmenter()
ticker := time.NewTicker(1 * gcTime)
for {
select {
case packet := <-ipInput:
result, err := ipv4Defragger.DefragIPv4(&packet.ip)
if err == nil && result != nil {
ipOut <- ipv4Defragged{
*result,
packet.timestamp,
}
}
case <-ticker.C:
ipv4Defragger.DiscardOlderThan(time.Now().Add(gcTime * -1))
case <-done:
ticker.Stop()
return
}
}
}
func ipv6Defragger(ipInput <-chan ipv6FragmentInfo, ipOut chan ipv6Defragged, gcTime time.Duration, done chan bool) {
ipv4Defragger := ip6defrag.NewIPv6Defragmenter()
ticker := time.NewTicker(1 * gcTime)
for {
select {
case packet := <-ipInput:
result, err := ipv4Defragger.DefragIPv6(&packet.ip, &packet.ipFragment)
if err == nil && result != nil {
ipOut <- ipv6Defragged{
*result,
packet.timestamp,
}
}
case <-ticker.C:
ipv4Defragger.DiscardOlderThan(time.Now().Add(gcTime * -1))
case <-done:
ticker.Stop()
return
}
}
}