-
Notifications
You must be signed in to change notification settings - Fork 1
/
pool.go
37 lines (29 loc) · 817 Bytes
/
pool.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
package main
import (
"log"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
)
// START OMIT
const poolSize = 20 // tune to taste
func process(packetChan chan gopacket.Packet) {
for packet := range packetChan {
ip4Layer := packet.Layer(layers.LayerTypeIPv4)
if ip4Layer == nil {
log.Println("Not an IPv4 packet")
continue
}
// Good processing goes here...
}
}
func main() {
pcapFile, _ := pcap.OpenOffline("packets.pcap")
defer pcapFile.Close()
packetSource := gopacket.NewPacketSource(pcapFile, pcapFile.LinkType())
packetChan := make(chan gopacket.Packet, poolSize)
defer close(packetChan)
for i := 0; i < poolSize; i++ { go process(packetChan) }
for packet := range packetSource.Packets() { packetChan <- packet }
}
// END OMIT