user2369060
user2369060

Reputation: 871

How can I get destination ip from tcp socket resulting from accept?

I do not mean what getsockname returns that is the address of the local interface, I mean the destination ip in the underlying ip packets (there are cases where those two are not equal, for instance in a transparent proxy scenario).

I do not want to use raw sockets to do that...

So I was wondering if there was some clever trick to find out the destination ip that appears in the packets...

Thanks in advance.

Upvotes: 1

Views: 1225

Answers (3)

xinnjie
xinnjie

Reputation: 732

@user2369060's answer is right. In practice, Envoy indeed use SO_ORIGINAL_DST to get the original ip to achieve its functionality.

Upvotes: 0

user2369060
user2369060

Reputation: 871

I just found the answer to my question, the option SO_ORIGINAL_DST:

 getsockopt(socket, SOL_IP, SO_ORIGINAL_DST, &destAddr, &destAddrLen);

Hope this helps someone over there!

Upvotes: 2

user207421
user207421

Reputation: 310850

getsockname() returns the target IP address of the SYN. Because of the weak end system model (also discussed here), that may or may not be the address of the NIC via which the SYN was received.

Upvotes: 1

Related Questions