Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 the 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

like image 365
david Avatar asked Jun 29 '11 17:06

david


1 Answers

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.

like image 194
cnicutar Avatar answered Oct 18 '22 05:10

cnicutar