c - skb_header_pointer 和 skb_transport_header 之间的区别?

标签 c linux tcp linux-kernel netfilter

我正在尝试实现一个 netfilter 模块,在处理 sk_buff 时我发现了两种可能的方法来检索 TCP header :

struct iphdr *ip_header = (struct iphdr *)skb_network_header(skb);
struct tcphdr *tcp_header = (struct tcphdr *)skb_transport_header(skb);

struct iphdr *ip_header = skb_header_pointer(skb, 0, sizeof(struct iphdr), &_iph)
struct tcphdr *tcp_header = skb_header_pointer(skb, ip_header->ihl * 4, sizeof(struct tcphdr), &_tcph);

我应该使用哪个?

最佳答案

你应该使用 ip_hdr()来自 /include/linux/ip.htcp_hdr()来自 /include/linux/tcp.h 如果你知道不可能有 paged-skb这里:

struct iphdr *ip_header = ip_hdr(skb);
if (ip_header->protocol == IPPROTO_TCP) {
    struct tcphdr *tcp_header = tcp_hdr(skb);
    //...

skb_header_pointer() 应该被使用以防分页-skb 的出现是可能的。示例:IP , TCP , ICMP
因此,如果 header 位于分页数据中(全部或部分)- skb_header_pointer() 将正确处理它。
还要记得检查skb_header_pointer()的返回值,它可以返回NULL。

有用的链接:1 , 2

关于c - skb_header_pointer 和 skb_transport_header 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57973630/

相关文章:

javascript - 未捕获的类型错误 : Cannot read property 'tcp' of undefined in chrome extension

amazon-web-services - 如何配置 AWS 网络负载均衡器以实现端到端 HTTPS 加密,同时保持 session 关联性?

javascript - 当 JavaScript 出错时,谷歌浏览器会中断网络连接

c - 我的 program.exe 已停止工作

linux - 是否有用于 CRC32C (Castagnoli) 的 LINUX 命令行工具

c - 字符串数组和 Gnome 排序

特定日期后文件夹中的 linux cat 文件用 awk 传输

linux - 如何在 Linux Bash (Ubuntu) 中构建 "sed"命令

c - 分割字符串函数的段错误

c - 在 HP-UX 上使用 libunwind 并获取堆栈跟踪