mirror of
https://github.com/daeuniverse/dae.git
synced 2025-07-28 13:49:19 +07:00
optimize: adapt to bpf legacy
This commit is contained in:
31
pkg/ebpf_internal/rawsock.go
Normal file
31
pkg/ebpf_internal/rawsock.go
Normal file
@ -0,0 +1,31 @@
|
||||
// Copied from https://github.com/cilium/ebpf/blob/v0.10.0/example_sock_elf_test.go
|
||||
|
||||
package internal
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func OpenRawSock(index int) (int, error) {
|
||||
sock, err := syscall.Socket(syscall.AF_PACKET, syscall.SOCK_RAW|syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC, int(Htons(syscall.ETH_P_ALL)))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
sll := syscall.SockaddrLinklayer{
|
||||
Ifindex: index,
|
||||
Protocol: Htons(syscall.ETH_P_ALL),
|
||||
}
|
||||
if err := syscall.Bind(sock, &sll); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return sock, nil
|
||||
}
|
||||
|
||||
// Htons converts the unsigned short integer hostshort from host byte order to network byte order.
|
||||
func Htons(i uint16) uint16 {
|
||||
b := make([]byte, 2)
|
||||
binary.BigEndian.PutUint16(b, i)
|
||||
return *(*uint16)(unsafe.Pointer(&b[0]))
|
||||
}
|
Reference in New Issue
Block a user