Reputation: 14563
I'm writing a simple message queue library that will basically let you set up a point-to-point messaging service between two servers either over TCP or UDP. I want to do away with the notion of client/server since they two servers will do the same thing (send and receive messages), so it doesn't really matter which is which.
I thought I'd do this by using the IP addresses of the two servers to decide which should bind a server socket and which should connect as a client (probably by choosing the one with the smallest non-equal octet to be the server, assuming it's not the same machine).
To do this I need to know the local IP address[es] of the interface[s] that has a route to the other computer. Is there an easy way to get this info?
Upvotes: 3
Views: 908
Reputation: 310884
probably by choosing the one with the smallest non-equal octet to be the server, assuming it's not the same machine
Pretty ad-hoc solution. I would have a running server multicast its existence, and a newly started one look for those multicasts, and start itself as a server if it didn't find them, and advertise itself via multicasts, and so on ...
Upvotes: 1
Reputation: 10551
$ ip route get 2a00:1450:4016:800::1011
2a00:1450:4016:800::1011 from :: via 2a01:4f8:100:63e0::1
dev eth0 src 2a01:4f8:100:6fab:cdef::1 metric 0
iproute internally uses the RTM_GETROUTE netlink message to obtain the information, and so can you. src then specifies the address that would be used to contact this host were you to use automatic binding (i.e. not calling bind(2) prior to connect, or calling bind with a wildcard for the address field).
Upvotes: 2