-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
66 lines (55 loc) · 1.51 KB
/
main.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 main
import (
"context"
"errors"
"log"
"os"
"os/signal"
"syscall"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/rlimit"
)
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc clang tcpconn ./tcp-connecting.c -- -D__TARGET_ARCH_x86 -I../headers -Wall
func main() {
if err := rlimit.RemoveMemlock(); err != nil {
log.Fatalf("Failed to remove rlimit memlock: %v", err)
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
spec, err := loadTcpconn()
if err != nil {
log.Fatalf("Failed to load tcpconn bpf spec: %v", err)
return
}
var obj tcpconnObjects
if err := spec.LoadAndAssign(&obj, nil); err != nil {
var ve *ebpf.VerifierError
if errors.As(err, &ve) {
log.Fatalf("Failed to load bpf obj: %v\n%-20v", err, ve)
} else {
log.Fatalf("Failed to load bpf obj: %v", err)
}
}
defer obj.Close()
if link, err := link.AttachTracing(link.TracingOptions{
Program: obj.TcpConnect,
}); err != nil {
log.Printf("Failed to attach fentry(tcp_connect): %v", err)
return
} else {
defer link.Close()
log.Printf("Attached fentry(tcp_connect)")
}
if link, err := link.AttachTracing(link.TracingOptions{
Program: obj.InetCskCompleteHashdance,
}); err != nil {
log.Printf("Failed to attach fexit(inet_csk_complete_hashdance): %v", err)
return
} else {
defer link.Close()
log.Printf("Attached fexit(inet_csk_complete_hashdance)")
}
log.Printf("Waiting for TCP connections...")
<-ctx.Done()
}