C - TCP checksum (using raw sockets) - how to specify the source IP address

When using raw sockets to send TCP data, the source ip address allowed to leave zero, so the kernel sets the correct value. This is useful, especially when multiple interfaces are used (with different IP addresses).

Now my problem is: calculate the TCP checksum, I will need to know what the source IP address will be at the end. Does this seem impossible to me?

Is there any way to determine the IP source of my outgoing packets?

(An alternative would be to bind my raw socket to an address, but I would not want to do this).

/ edit: Using Linux

+6
source share
1 answer

If you do not bind your socket, the kernel should find the source address based on the destination address.

Basically, a route search is performed and the destination interface is determined. After that, the IP address is taken from this interface: the source for your packet.

So, your question turns into doing a route search, as does ip route get .

EDIT

@nos is mentioned using a different socket (UDP) and connecting it to this destination address. Retrieving a locally bound name using getsockname should indicate the source address that will be used for this destination.

+1
source

Source: https://habr.com/ru/post/891663/


All Articles