在tcp_recvmsg()中,struct tcp_sock对象是从参数struct sock sk中派生的。您可以执行inet_sk(sk)->inet_sport以获取TCP数据包的目标端口。 inet_sk(sk)->inet_dport应该获取数据包的源端口。
使用htons((unsigned short int)inet_sk(sk)->inet_sport)和htons((unsigned short int)inet_sk(sk)->inet_dport)获取端口,如wireshark端口所示。
htons
调用。在我看来,如果没有解释为什么这比被接受的答案更好,这并没有增加任何东西。 - Bluehorn