首页 > 解决方案 > 可以从 Linux 内核数据类型“struct sock”中检索套接字的端口吗?

问题描述

动机

我正在尝试编写一个 bpftrace 程序,以通过挂钩 kprobe 来跟踪套接字何时准备好读取sock_def_readable。我会得到一个struct sock检查。我想将它映射回我在用户区创建的套接字。

问题

如何从 a 中恢复端口号struct sock

标签: socketslinux-kernelkprobe

解决方案


我只是扩展了inet_sk... 的定义,这只是一个演员表。

#!/usr/bin/env bpftrace

#include <linux/net/inet_sock.h>

BEGIN
{
    printf("network tracing");
}

kprobe:sock_def_readable
{
  $inet_sock = (struct inet_sock *)arg0;
  printf("sock_def_readable destination port %d, source port %d \n", $inet_sock->inet_sport, $inet_sock->inet_dport);
}

推荐阅读