From c046f76934f9e2f995d88aec48f8909fd2eaad5d Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 7 Nov 2009 22:06:12 +0000 Subject: [PATCH] display the best available ip for peers in the gui, instead of 0.0.0.0. This does only affect the gui, not the way ips are trusted and propagated. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@1799 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/rsserver/p3peers.cc | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/libretroshare/src/rsserver/p3peers.cc b/libretroshare/src/rsserver/p3peers.cc index 8122fbc6a..6068af8e2 100644 --- a/libretroshare/src/rsserver/p3peers.cc +++ b/libretroshare/src/rsserver/p3peers.cc @@ -280,6 +280,19 @@ bool p3Peers::isFriend(std::string id) return false; } +static struct sockaddr_in getPreferredAddress( const struct sockaddr_in& addr1,time_t ts1, + const struct sockaddr_in& addr2,time_t ts2, + const struct sockaddr_in& addr3,time_t ts3) +{ + time_t ts = ts1 ; + struct sockaddr_in addr = addr1 ; + + if(ts2 > ts && strcmp(inet_ntoa(addr2.sin_addr),"0.0.0.0")) { ts = ts2 ; addr = addr2 ; } + if(ts3 > ts && strcmp(inet_ntoa(addr3.sin_addr),"0.0.0.0")) { ts = ts3 ; addr = addr3 ; } + + return addr ; +} + bool p3Peers::getPeerDetails(std::string id, RsPeerDetails &d) { #ifdef P3PEERS_DEBUG @@ -341,12 +354,17 @@ bool p3Peers::getPeerDetails(std::string id, RsPeerDetails &d) } } + // From all addresses, show the most recent one if no address is currently in use. + struct sockaddr_in best_local_addr = (!strcmp(inet_ntoa(pcs.localaddr.sin_addr),"0.0.0.0"))?getPreferredAddress(pcs.dht.laddr,pcs.dht.ts,pcs.disc.laddr,pcs.disc.ts,pcs.peer.laddr,pcs.peer.ts):pcs.localaddr ; + struct sockaddr_in best_servr_addr = (!strcmp(inet_ntoa(pcs.serveraddr.sin_addr),"0.0.0.0"))?getPreferredAddress(pcs.dht.raddr,pcs.dht.ts,pcs.disc.raddr,pcs.disc.ts,pcs.peer.raddr,pcs.peer.ts):pcs.serveraddr ; + + /* fill from pcs */ - d.localAddr = inet_ntoa(pcs.localaddr.sin_addr); - d.localPort = ntohs(pcs.localaddr.sin_port); - d.extAddr = inet_ntoa(pcs.serveraddr.sin_addr); - d.extPort = ntohs(pcs.serveraddr.sin_port); + d.localAddr = inet_ntoa(best_local_addr.sin_addr); + d.localPort = ntohs(best_local_addr.sin_port); + d.extAddr = inet_ntoa(best_servr_addr.sin_addr); + d.extPort = ntohs(best_servr_addr.sin_port); d.lastConnect = pcs.lastcontact; d.connectPeriod = 0;