To determine the preferred source address from the available multiple ipv6 address i can use following command ip -6 route destaddr. My question is ip is an utility, Is there any system call or an library API available for the same. Because my whole task is to determine the source address with any system call or an library API. And then use it in my applications.
Asked
Active
Viewed 267 times
0
-
Routing information is dynamically changing, so you only really know which interface information went through after the fact. You can use the `IPV6_PKTINFO` socket option to retrieve the destination address of received packets as control messages using `recvmsg`. – zneak May 08 '14 at 07:04
2 Answers
0
Not getting your exact requirement. if you want to know the your own system IP then you may try any one of them.
ifconfig
ip addr show
ip addr show eth0
hostname -i
or you can set any ipv6 ip address to interface(eth0) and using these commands you can know the address.
/sbin/ip -6 addr show dev eth0
/sbin/ifconfig <interface>

Rocoder
- 1,083
- 2
- 15
- 26
-
He's asking if there are C APIs to find the same result without resorting to an external program. If you don't get the exact requirements of a question, it could be advisable to comment and ask for clarifications rather than write an answer. – zneak May 08 '14 at 05:58
-
Computers can be connected to multiple networks and have multiple IP addresses. OP wants to know the IP address of the interface that has the shortest route to some given destination address, without invoking an external program. – zneak May 08 '14 at 06:05
-
@zneak yes i should have asked first. but as you said,then it can be algorithm based like spanning tree or OSPF – Rocoder May 08 '14 at 06:13
-
@zneak you got me a right. @ rohit ip -6 route get DstAddr returns me the most preferred local ip which could be used for this DstAddr. But ip is an utility what i want is an system call/library API which could do this for me. And the reason why i want this is that in my application i use an third party stack. In application to send the data i need to command that stack to send the data using one of the local ips.hence i need to know before sending the data the most appropriate source address which could be used. – pratik03 May 08 '14 at 12:11
-
@pratik03 `getaddrinfo` will return the IP address. [link](http://man7.org/linux/man-pages/man3/getaddrinfo.3.html) but whether it will find the most appropriate one or not that's the issue. – Rocoder May 08 '14 at 13:15
0
Hope it helps you,Linux will treat the latest ip address as the source address in the interface of the ip addresses,Linux implements this
http://www.rfc-editor.org/rfc/rfc3484.txt
Algorithm for selsction.This algorithm first creates the potential list of the ip addresses for source addresses. For more information have a look at this
http://www.davidc.net/networking/ipv6-source-address-selection-linux.

Dharma
- 2,425
- 3
- 26
- 40
-
what you gave me is actually an source address selection algorithm. I am fine with it. What i want in an system call/library API which could perform the same algorithm which you described here. – pratik03 May 08 '14 at 12:14