Search code examples
linuxtcplinux-kernel

Get TCP port number from struct tcp_sock


How can I get the TCP port number of a tcp_sock structure in the Linux kernel? The instance of tcp_sock is in the context of tcp_recvmsg().


Solution

  • In tcp_recvmsg(), the struct tcp_sock object is derived from parameter struct sock sk. You can do inet_sk(sk)->inet_sport to get the destination port of the TCP packet. inet_sk(sk)->inet_dport should get the source port of the packet.