forked from aquasecurity/libbpfgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map-iterator.go
66 lines (53 loc) · 1.17 KB
/
map-iterator.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
60
61
62
63
64
65
66
package libbpfgo
/*
#cgo LDFLAGS: -lelf -lz
#include "libbpfgo.h"
*/
import "C"
import (
"syscall"
"unsafe"
)
//
// BPFMapIterator (low-level API)
//
// BPFMapIterator iterates over keys in a BPF map.
type BPFMapIterator struct {
mapFD int
keySize int
err error
prev []byte
next []byte
}
// Next advances the iterator to the next key in the map.
func (it *BPFMapIterator) Next() bool {
if it.err != nil {
return false
}
prevPtr := unsafe.Pointer(nil)
if it.next != nil {
prevPtr = unsafe.Pointer(&it.next[0])
}
next := make([]byte, it.keySize)
nextPtr := unsafe.Pointer(&next[0])
retC := C.bpf_map_get_next_key(C.int(it.mapFD), prevPtr, nextPtr)
if retC < 0 {
if err := syscall.Errno(-retC); err != syscall.ENOENT {
it.err = err
}
return false
}
it.prev = it.next
it.next = next
return true
}
// Key returns the current key value of the iterator, if the most recent call
// to Next returned true.
// The slice is valid only until the next call to Next.
func (it *BPFMapIterator) Key() []byte {
return it.next
}
// Err returns the last error that ocurred while table.Iter or iter.Next.
func (it *BPFMapIterator) Err() error {
return it.err
}