forked from abrander/pnmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisten_linux.go
41 lines (33 loc) · 840 Bytes
/
listen_linux.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
package main
import (
"log"
"net"
"syscall"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/mdlayher/raw"
)
func listen(deviceName string, out chan gopacket.Packet) {
intf, err := net.InterfaceByName(deviceName)
if err != nil {
log.Printf("error: %s", err.Error())
}
conn, err := raw.ListenPacket(intf, syscall.ETH_P_ALL, nil)
if err != nil {
log.Printf("error: %s", err.Error())
}
buffer := make([]byte, 65536)
for {
l, _, err := conn.ReadFrom(buffer)
if err != nil {
log.Fatalf("error: %s", err.Error())
break
}
packet := gopacket.NewPacket(buffer[0:l], layers.LayerTypeEthernet, gopacket.Default)
packet.Metadata().Timestamp = time.Now()
packet.Metadata().CaptureInfo.CaptureLength = l
packet.Metadata().CaptureInfo.Length = l
filter(packet, out)
}
}