开发者

C - TCP Checksum (using raw sockets) -- how to source IP address

开发者 https://www.devze.com 2023-03-16 12:02 出处:网络
When using raw sockets to send TCP data, it is allowed to leave the source ip address zero, so that the kernel puts in the correct value. This is helpful, especially when several interfaces (with diff

When using raw sockets to send TCP data, it is allowed to leave the source ip address zero, so that the kernel puts in the correct value. This is helpful, especially when several interfaces (with different IP addresses) are in use.

My problem is now: To calculate to TCP checksum, I would need to know, what the source IP address is going to be in the end. This seems to be impossible to me?

Is there anyway to determine t开发者_如何学编程he source IP of my outgoing packets?

(An alternative would be, to specifically bind my raw socket to an address, but I'd rather not want to do that).

/edit: Using Linux


If you don't bind your socket, the kernel has to find the source address based on the destination address.

Basically a route lookup is done and a destination interface determined. After that, the IP is taken from that interface: the source for your packet.

So your question turns into performing a route lookup, the way ip route get does.

EDIT

@nos mentioned using a different socket (UDP) and connecting it to that destination address. Getting it's locally-bound name using getsockname should give you the source address that would be used for that destination.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号