dae/pkg/ebpf_internal/rawsock.go

24 lines
575 B
Go

// Copied from https://github.com/cilium/ebpf/blob/v0.10.0/example_sock_elf_test.go
package internal
import (
"github.com/v2rayA/dae/common"
"syscall"
)
func OpenRawSock(index int) (int, error) {
sock, err := syscall.Socket(syscall.AF_PACKET, syscall.SOCK_RAW|syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC, int(common.Htons(syscall.ETH_P_ALL)))
if err != nil {
return 0, err
}
sll := syscall.SockaddrLinklayer{
Ifindex: index,
Protocol: common.Htons(syscall.ETH_P_ALL),
}
if err := syscall.Bind(sock, &sll); err != nil {
return 0, err
}
return sock, nil
}