user2233706
user2233706

Reputation: 7217

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().

Upvotes: 3

Views: 841

Answers (2)

Integrated_Rk
Integrated_Rk

Reputation: 1

Use htons((unsigned short int)inet_sk(sk)->inet_sport) and htons((unsigned short int)inet_sk(sk)->inet_dport) to get ports as per wireshark port

Upvotes: 0

user2233706
user2233706

Reputation: 7217

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.

Upvotes: 2

Related Questions