ebpf-networking icon indicating copy to clipboard operation
ebpf-networking copied to clipboard

network.py actually contains C source

Open taspelund opened this issue 3 years ago • 4 comments

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!

taspelund avatar Dec 16 '21 08:12 taspelund

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

mmelo-yottaa avatar Feb 28 '22 14:02 mmelo-yottaa

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 avatar Feb 28 '22 14:02 mmelo-yottaa

you can try those, untested but the code is straightforward

mmelo-yottaa avatar Feb 28 '22 14:02 mmelo-yottaa

This is helpful!

janetat avatar Mar 18 '23 01:03 janetat