lizrice / ebpf-networking

The Beginner's Guide to eBPF Programming for Networking
Apache License 2.0
103 stars 3 forks source link

network.py actually contains C source #1

Open taspelund opened 2 years ago

taspelund commented 2 years ago

Hi! I just stumbled on this awesome repo after watching the eBPF Day presentation! It looks like the network.py file may have been mistakenly added to the repo with the same contents as the network.c file:

┌─╼[~/ebpf-networking] [main] 
└────╼ diff network.c network.py | wc
      0       0       0

Just figured I'd bring this to your attention in case there are other folks who want to follow along with the example here.

Thanks!

mmelo-yottaa commented 2 years ago

yeah just stumbled on that - https://github.com/lizrice/ebpf-networking/blob/main/network.py

mmelo-yottaa commented 2 years ago
import os
from time import sleep
from pyroute2 import IPRoute
from bcc import BPF

interface = "eth0"

b = BPF(src_file="network.c")
b.attach_kprobe(event="tcp_v4_connect", fn_name="tcpconnect")
print("Ready")

try:
    b.trace_print()
except KeyboardInterrupt:
    print("unloading")

exit()
import os
import socket
from time import sleep
from pyroute2 import IPRoute
from bcc import BPF

interface = "eth0"

b = BPF(src_file="network.c")
f = b.load_func("socket_filter", BPF.SOCKET_FILTER)
BPF.attach_raw_socket(f, interface)
fd = f.sock
sock = socket.fromfd(fd, socket.PF_PACKET, socket.SOCK_RAW, socket.IPPROTO_IP)
sock.setblocking(True)

print("Ready")

try:
    while True:
        packet_str = os.read(fd, 4096)
        print("Userspace Data: {}".format(packet_str))
except KeyboardInterrupt:
    print("unloading")

exit()
mmelo-yottaa commented 2 years ago

you can try those, untested but the code is straightforward

janetat commented 1 year ago

This is helpful!