-
Notifications
You must be signed in to change notification settings - Fork 7
/
capture_linux.go
59 lines (51 loc) · 1.43 KB
/
capture_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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
"net"
"unsafe"
)
// Setup the given device to join the EAP(OL) link layer multicast group.
func joinMulticastGroup(device string) (fd int) {
iface, err := net.InterfaceByName(device)
if err != nil {
log.WithFields(logrus.Fields{"interface": device}).Fatal(err)
}
fd, err = unix.Socket(unix.AF_PACKET, unix.SOCK_RAW, unix.ETH_P_PAE)
if err != nil {
log.WithFields(logrus.Fields{"interface": device}).Fatal(err)
}
mreq := unix.PacketMreq{
Ifindex: int32(iface.Index),
Type: unix.PACKET_MR_MULTICAST,
Alen: hwAddressLength,
Address: [8]uint8{0x01, 0x80, 0xc2, 0x00, 0x00, 0x03},
}
_, _, errNo := unix.Syscall6(
unix.SYS_SETSOCKOPT,
uintptr(fd),
uintptr(unix.SOL_PACKET),
uintptr(unix.PACKET_ADD_MEMBERSHIP),
uintptr(unsafe.Pointer(&mreq)),
unix.SizeofPacketMreq,
0,
)
if errNo > 0 {
log.WithFields(logrus.Fields{"interface": device, "error": errNo}).Fatal("Could not join EAP link-layer multicast group")
}
sockAddrLinkLayer := unix.RawSockaddrLinklayer{
Family: unix.AF_PACKET,
Protocol: unix.ETH_P_PAE,
Ifindex: int32(iface.Index),
}
_, _, errNo = unix.Syscall(
unix.SYS_BIND,
uintptr(fd),
uintptr(unsafe.Pointer(&sockAddrLinkLayer)),
unsafe.Sizeof(sockAddrLinkLayer),
)
if errNo > 0 {
log.WithFields(logrus.Fields{"interface": device, "error": errNo}).Fatal("Error binding interface to socket")
}
return fd
}