Reputation: 11
To get destination derive from an host name or to apply destination address selection algorithm (as per RFC 3484) we have an library api getaddrinfo()
. If you search on net you would find that the same API could be used for Source address selection. But when i tested it practically it's not happening.
When i did some homework on it found that in linux the kernel itself decides the appropriate source address depending upon the destination address by applying those rules (as per RFC 3484). This is done by kernel in fib6_rule_action()
method this is done while sending the data (e.g. in sendto()
).
My question is there any library API or system call which could do this for me at earlier stage meaning before sending data.
Upvotes: 1
Views: 1193
Reputation: 2020
You can get that information via linux routing sockets aka rtnetlink. Specifically it is the RTA_SRC you are looking for.
A word of warning, (rt)netlink sockets are not the easiest protocol to use and there is not much up to date documentation besides the source code. The wikipedia page for netlink might get you started. Some of the External Links seem good and the linked paper contains more references.
I suggest using a library, if you can find one and your netlink related code is any longer than the single source address query. Libnl or libmnl might be good. The former also has a nice page about routing sockets.
As a test, you can get the same functionality with the user space command ip -6 route get <dst_addr>
eg ip -6 route get 2a00:1450:4010:c04::63
.
Upvotes: 1