Feature: for Linux, add net namespace option for network interface packet sources(afpacket, pcap).
- Dominant language
- Go
- Stars
- 6.8k
- Forks
- 1.2k
- PR merge metrics
- No merged PRs in 30d
Description
It seems to capture packets from a net namespace must call syscall.Setns first to switch over the namespcae, like:
```golang
package main
import (
"fmt"
"os"
"syscall"
"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
)
func main() {
// specify the namespace you want to capture traffic in
ns := "mynamespace"
// switch to the specified namespace
err := syscall.Setns(fmt.Sprintf("/var/run/netns/%s", ns), syscall.CLONE_NEWNET)
if err != nil {
fmt.Fprintf(os.Stderr, "error switching to namespace: %v\n", err)
os.Exit(1)
}
// open the network interface for capturing traffic
handle, err := pcap.OpenLive("eth0", 1600, true, pcap.BlockForever)
if err != nil {
fmt.Fprintf(os.Stderr, "error opening interface: %v\n", err)
os.Exit(1)
}
defer handle.Close()
// create a packet capture filter
filter := "tcp and port 80"
// set the packet capture filter
err = handle.SetBPFFilter(filter)
if err != nil {
fmt.Fprintf(os.Stderr, "error setting BPF filter: %v\n", err)
os.Exit(1)
}
// start capturing packets
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
// process captured packet here
fmt.Println(packet)
}
}
````
Will it be OK to add like ```WithNetNS``` feature to the packet sources, I'd like to summit a PR for this.
Contributor guide
Assessment
This issue has not been assessed yet.